Matrix
Basic operations on square matrices.
26 lines tested
Usage: Matrix<int, 3> A; A.d = 1,2,3, 4,5,6, 7,8,9; array<int, 3> vec = 1,2,3; vec = (A^N) * vec;
content/data-structures/Matrix.h — Ulf Lundstrom, source: My head
template<class T, int N> struct Matrix {
typedef Matrix M;
array<array<T, N>, N> d{};
M operator*(const M& m) const {
M a;
rep(i,0,N) rep(j,0,N)
rep(k,0,N) a.d[i][k] += d[i][j] * m.d[j][k];
return a;
}
array<T, N> operator*(const array<T, N>& vec) const {
array<T, N> ret{};
rep(i,0,N) rep(j,0,N) ret[i] += d[i][j] * vec[j];
return ret;
}
M operator^(ll p) const {
assert(p >= 0);
M a, b(*this);
rep(i,0,N) a.d[i][i] = 1;
while (p) {
if (p&1) a = a*b;
b = b*b;
p >>= 1;
}
return a;
}
};