cp-book

ecnerwala's competitive programming library

View the Project on GitHub ecnerwala/cp-book

:warning: #include "num/mat.hpp"

View this file on GitHub · Last update: 2026-08-19 23:07:21-07:00

Required by

Code

Coverage Exec / Excl / Total
Lines 93.8% 15 / 0 / 16
Functions 100.0% 9 / 0 / 9
Branches 96.9% 31 / 0 / 32
Full report
#pragma once

#include <array>
#include <cstddef>

namespace wala {

// Small NxN matrix over num, row-major
template <typename num, int N> struct mat {
	std::array<num, size_t(N) * N> a{};
	num& operator[](std::array<int, 2> rc) { return a[size_t(rc[0]) * N + rc[1]]; }
	const num& operator[](std::array<int, 2> rc) const { return a[size_t(rc[0]) * N + rc[1]]; }
	num* data() { return a.data(); }
	const num* data() const { return a.data(); }
	mat& operator+=(const mat& o) { for (int i = 0; i < N*N; i++) a[i] += o.a[i]; return *this; }
	friend mat operator+(mat x, const mat& y) { x += y; return x; }
	mat& operator-=(const mat& o) { for (int i = 0; i < N*N; i++) a[i] -= o.a[i]; return *this; }
	friend mat operator-(mat x, const mat& y) { x -= y; return x; }
	friend mat operator*(const mat& x, const mat& y) {
		mat r;
		for (int i = 0; i < N; i++) for (int k = 0; k < N; k++) for (int j = 0; j < N; j++)
			r[{i, j}] += x[{i, k}] * y[{k, j}];
		return r;
	}
	mat& operator*=(const mat& o) { return *this = *this * o; }
	friend bool operator==(const mat&, const mat&) = default;
};

} // namespace wala
#include <array>
#include <cstddef>
#line 2 "src/num/mat.hpp"

#line 5 "src/num/mat.hpp"

namespace wala {

// Small NxN matrix over num, row-major
template <typename num, int N> struct mat {
	std::array<num, size_t(N) * N> a{};
	num& operator[](std::array<int, 2> rc) { return a[size_t(rc[0]) * N + rc[1]]; }
	const num& operator[](std::array<int, 2> rc) const { return a[size_t(rc[0]) * N + rc[1]]; }
	num* data() { return a.data(); }
	const num* data() const { return a.data(); }
	mat& operator+=(const mat& o) { for (int i = 0; i < N*N; i++) a[i] += o.a[i]; return *this; }
	friend mat operator+(mat x, const mat& y) { x += y; return x; }
	mat& operator-=(const mat& o) { for (int i = 0; i < N*N; i++) a[i] -= o.a[i]; return *this; }
	friend mat operator-(mat x, const mat& y) { x -= y; return x; }
	friend mat operator*(const mat& x, const mat& y) {
		mat r;
		for (int i = 0; i < N; i++) for (int k = 0; k < N; k++) for (int j = 0; j < N; j++)
			r[{i, j}] += x[{i, k}] * y[{k, j}];
		return r;
	}
	mat& operator*=(const mat& o) { return *this = *this * o; }
	friend bool operator==(const mat&, const mat&) = default;
};

} // namespace wala
// clang-format off
// @formatter:off
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpragmas"
#pragma GCC diagnostic ignored "-Wunknown-warning-option"
#pragma GCC diagnostic ignored "-Wmisleading-indentation"
#pragma GCC diagnostic ignored "-Wmultistatement-macros"
#include <bits/stdc++.h>
// src/num/mat.hpp
namespace wala{
template<typename num,int N>struct mat{
std::array<num,size_t(N)*N>a{};
num&operator[](std::array<int,2>rc){return a[size_t(rc[0])*N+rc[1]];}
const num&operator[](std::array<int,2>rc)const{return a[size_t(rc[0])*N+rc[1]];}
num*data(){return a.data();}
const num*data()const{return a.data();}
mat&operator+=(const mat&o){for(int i=0;i<N*N;i++)a[i]+=o.a[i];return*this;}
friend mat operator+(mat x,const mat&y){x+=y;return x;}
mat&operator-=(const mat&o){for(int i=0;i<N*N;i++)a[i]-=o.a[i];return*this;}
friend mat operator-(mat x,const mat&y){x-=y;return x;}
friend mat operator*(const mat&x,const mat&y){
mat r;
for(int i=0;i<N;i++)for(int k=0;k<N;k++)for(int j=0;j<N;j++)
r[{i,j}]+=x[{i,k}]*y[{k,j}];
return r;
}
mat&operator*=(const mat&o){return*this=*this*o;}
friend bool operator==(const mat&,const mat&)=default;
};
}
#pragma GCC diagnostic pop
// clang-format on
// @formatter:on
#pragma once

#include <array>
#include <cstddef>

namespace wala {

// Small NxN matrix over num, row-major
template <typename num, int N> struct mat {
	std::array<num, size_t(N) * N> a{};
	num& operator[](std::array<int, 2> rc) { return a[size_t(rc[0]) * N + rc[1]]; }
	const num& operator[](std::array<int, 2> rc) const { return a[size_t(rc[0]) * N + rc[1]]; }
	num* data() { return a.data(); }
	const num* data() const { return a.data(); }
	mat& operator+=(const mat& o) { for (int i = 0; i < N*N; i++) a[i] += o.a[i]; return *this; }
	friend mat operator+(mat x, const mat& y) { x += y; return x; }
	mat& operator-=(const mat& o) { for (int i = 0; i < N*N; i++) a[i] -= o.a[i]; return *this; }
	friend mat operator-(mat x, const mat& y) { x -= y; return x; }
	friend mat operator*(const mat& x, const mat& y) {
		mat r;
		for (int i = 0; i < N; i++) for (int k = 0; k < N; k++) for (int j = 0; j < N; j++)
			r[{i, j}] += x[{i, k}] * y[{k, j}];
		return r;
	}
	mat& operator*=(const mat& o) { return *this = *this * o; }
	friend bool operator==(const mat&, const mat&) = default;
};

} // namespace wala
Back to top page