1 #ifndef MSP_LINAL_MATRIX_H_
2 #define MSP_LINAL_MATRIX_H_
11 A general mathematical matrix with M rows and N columns.
13 template<typename T, unsigned M, unsigned N>
23 Matrix(const Matrix<U, M, N> &);
25 static Matrix from_columns(const Vector<T, M> *);
26 static Matrix from_rows(const Vector<T, N> *);
28 T &element(unsigned i, unsigned j) { return data[i+M*j]; }
29 const T &element(unsigned i, unsigned j) const { return data[i+M*j]; }
30 T &operator()(unsigned i, unsigned j) { return element(i, j); }
31 const T &operator()(unsigned i, unsigned j) const { return element(i, j); }
33 Matrix &operator*=(T);
34 Matrix &operator/=(T);
35 Matrix &operator+=(const Matrix &);
36 Matrix &operator-=(const Matrix &);
38 Matrix &exchange_rows(unsigned, unsigned);
39 Matrix &multiply_row(unsigned, T);
40 Matrix &add_row(unsigned, unsigned, T);
43 template<typename T, unsigned M, unsigned N>
44 inline Matrix<T, M, N>::Matrix()
46 std::fill(data, data+M*N, T());
49 template<typename T, unsigned M, unsigned N>
50 inline Matrix<T, M, N>::Matrix(const T *d)
52 std::copy(d, d+M*N, data);
55 template<typename T, unsigned M, unsigned N>
57 inline Matrix<T, M, N>::Matrix(const Matrix<U, M, N> &other)
59 for(unsigned i=0; i<M; ++i)
60 for(unsigned j=0; j<N; ++j)
61 element(i, j) = other(i, j);
64 template<typename T, unsigned M, unsigned N>
65 inline Matrix<T, M, N> Matrix<T, M, N>::from_columns(const Vector<T, M> *v)
68 for(unsigned i=0; i<M; ++i)
69 for(unsigned j=0; j<N; ++j)
73 template<typename T, unsigned M, unsigned N>
74 inline Matrix<T, M, N> Matrix<T, M, N>::from_rows(const Vector<T, N> *v)
77 for(unsigned i=0; i<M; ++i)
78 for(unsigned j=0; j<N; ++j)
82 template<typename T, unsigned M, unsigned N>
83 inline Matrix<T, M, N> &Matrix<T, M, N>::operator*=(T s)
85 for(unsigned i=0; i<M*N; ++i)
90 template<typename T, unsigned M, unsigned N>
91 inline Matrix<T, M, N> operator*(const Matrix<T, M, N> &m, T s)
97 template<typename T, unsigned M, unsigned N>
98 inline Matrix<T, M, N> operator*(T s, const Matrix<T, M, N> &m)
103 template<typename T, unsigned M, unsigned P, unsigned N>
104 inline Matrix<T, M, N> operator*(const Matrix<T, M, P> &m1, const Matrix<T, P, N> &m2)
107 for(unsigned i=0; i<M; ++i)
108 for(unsigned j=0; j<N; ++j)
109 for(unsigned k=0; k<P; ++k)
110 r(i, j) += m1(i, k)*m2(k, j);
114 template<typename T, unsigned M, unsigned N>
115 inline Vector<T, M> operator*(const Matrix<T, M, N> &m, const Vector<T, N> &v)
118 for(unsigned i=0; i<M; ++i)
119 for(unsigned j=0; j<N; ++j)
120 r[i] += m(i, j)*v[j];
124 template<typename T, unsigned M, unsigned N>
125 inline Vector<T, N> operator*(const Vector<T, M> &v, const Matrix<T, M, N> &m)
128 for(unsigned j=0; j<N; ++j)
129 for(unsigned i=0; i<M; ++i)
130 r[j] += v[i]*m(i, j);
134 template<typename T, unsigned M, unsigned N>
135 inline Matrix<T, M, N> &Matrix<T, M, N>::operator/=(T s)
137 for(unsigned i=0; i<M*N; ++i)
142 template<typename T, unsigned M, unsigned N>
143 inline Matrix<T, M, N> operator/(const Matrix<T, M, N> &m, T s)
145 Matrix<T, M, N> r(m);
149 template<typename T, unsigned M, unsigned N>
150 inline Matrix<T, M, N> &Matrix<T, M, N>::operator+=(const Matrix<T, M, N> &m)
152 for(unsigned i=0; i<M*N; ++i)
153 data[i] += m.data[i];
157 template<typename T, unsigned M, unsigned N>
158 inline Matrix<T, M, N> operator+(const Matrix<T, M, N> &m1, const Matrix<T, M, N> &m2)
160 Matrix<T, M, N> r(m1);
164 template<typename T, unsigned M, unsigned N>
165 inline Matrix<T, M, N> &Matrix<T, M, N>::operator-=(const Matrix<T, M, N> &m)
167 for(unsigned i=0; i<M*N; ++i)
168 data[i] -= m.data[i];
172 template<typename T, unsigned M, unsigned N>
173 inline Matrix<T, M, N> operator-(const Matrix<T, M, N> &m1, const Matrix<T, M, N> &m2)
175 Matrix<T, M, N> r(m1);
179 template<typename T, unsigned M, unsigned N>
180 inline bool operator==(const Matrix<T, M, N> &a, const Matrix<T, M, N> &b)
182 for(unsigned j=0; j<N; ++j)
183 for(unsigned i=0; i<M; ++i)
189 template<typename T, unsigned M, unsigned N>
190 inline Matrix<T, M, N> &Matrix<T, M, N>::exchange_rows(unsigned i, unsigned j)
193 for(unsigned k=0; k<N; ++k)
194 swap(element(i, k), element(j, k));
198 template<typename T, unsigned M, unsigned N>
199 inline Matrix<T, M, N> &Matrix<T, M, N>::multiply_row(unsigned i, T s)
201 for(unsigned k=0; k<N; ++k)
206 template<typename T, unsigned M, unsigned N>
207 inline Matrix<T, M, N> &Matrix<T, M, N>::add_row(unsigned i, unsigned j, T s)
209 for(unsigned k=0; k<N; ++k)
210 element(j, k) += element(i, k)*s;
214 template<typename T, unsigned M, unsigned N>
215 inline Matrix<T, N, M> transpose(const Matrix<T, M, N> &m)
218 for(unsigned j=0; j<N; ++j)
219 for(unsigned i=0; i<M; ++i)