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 template<unsigned P, unsigned Q>
34 Matrix<T, P, Q> block(unsigned, unsigned) const;
36 Matrix &operator*=(T);
37 Matrix &operator/=(T);
38 Matrix &operator+=(const Matrix &);
39 Matrix &operator-=(const Matrix &);
41 Matrix &exchange_rows(unsigned, unsigned);
42 Matrix &multiply_row(unsigned, T);
43 Matrix &add_row(unsigned, unsigned, T);
46 template<typename T, unsigned M, unsigned N>
47 inline Matrix<T, M, N>::Matrix()
49 std::fill(data, data+M*N, T());
52 template<typename T, unsigned M, unsigned N>
53 inline Matrix<T, M, N>::Matrix(const T *d)
55 std::copy(d, d+M*N, data);
58 template<typename T, unsigned M, unsigned N>
60 inline Matrix<T, M, N>::Matrix(const Matrix<U, M, N> &other)
62 for(unsigned i=0; i<M; ++i)
63 for(unsigned j=0; j<N; ++j)
64 element(i, j) = other(i, j);
67 template<typename T, unsigned M, unsigned N>
68 inline Matrix<T, M, N> Matrix<T, M, N>::from_columns(const Vector<T, M> *v)
71 for(unsigned i=0; i<M; ++i)
72 for(unsigned j=0; j<N; ++j)
77 template<typename T, unsigned M, unsigned N>
78 inline Matrix<T, M, N> Matrix<T, M, N>::from_rows(const Vector<T, N> *v)
81 for(unsigned i=0; i<M; ++i)
82 for(unsigned j=0; j<N; ++j)
87 template<typename T, unsigned M, unsigned N>
88 template<unsigned P, unsigned Q>
89 inline Matrix<T, P, Q> Matrix<T, M, N>::block(unsigned y, unsigned x) const
92 for(unsigned j=0; j<P; ++j)
93 for(unsigned i=0; i<Q; ++i)
94 r(j, i) = element(y+j, x+i);
98 template<typename T, unsigned M, unsigned N>
99 inline Matrix<T, M, N> &Matrix<T, M, N>::operator*=(T s)
101 for(unsigned i=0; i<M*N; ++i)
106 template<typename T, unsigned M, unsigned N>
107 inline Matrix<T, M, N> operator*(const Matrix<T, M, N> &m, T s)
109 Matrix<T, M, N> r(m);
113 template<typename T, unsigned M, unsigned N>
114 inline Matrix<T, M, N> operator*(T s, const Matrix<T, M, N> &m)
119 template<typename T, unsigned M, unsigned P, unsigned N>
120 inline Matrix<T, M, N> operator*(const Matrix<T, M, P> &m1, const Matrix<T, P, N> &m2)
123 for(unsigned i=0; i<M; ++i)
124 for(unsigned j=0; j<N; ++j)
125 for(unsigned k=0; k<P; ++k)
126 r(i, j) += m1(i, k)*m2(k, j);
130 template<typename T, unsigned M, unsigned N>
131 inline Vector<T, M> operator*(const Matrix<T, M, N> &m, const Vector<T, N> &v)
134 for(unsigned i=0; i<M; ++i)
135 for(unsigned j=0; j<N; ++j)
136 r[i] += m(i, j)*v[j];
140 template<typename T, unsigned M, unsigned N>
141 inline Vector<T, N> operator*(const Vector<T, M> &v, const Matrix<T, M, N> &m)
144 for(unsigned j=0; j<N; ++j)
145 for(unsigned i=0; i<M; ++i)
146 r[j] += v[i]*m(i, j);
150 template<typename T, unsigned M, unsigned N>
151 inline Matrix<T, M, N> &Matrix<T, M, N>::operator/=(T s)
153 for(unsigned i=0; i<M*N; ++i)
158 template<typename T, unsigned M, unsigned N>
159 inline Matrix<T, M, N> operator/(const Matrix<T, M, N> &m, T s)
161 Matrix<T, M, N> r(m);
165 template<typename T, unsigned M, unsigned N>
166 inline Matrix<T, M, N> &Matrix<T, M, N>::operator+=(const Matrix<T, M, N> &m)
168 for(unsigned i=0; i<M*N; ++i)
169 data[i] += m.data[i];
173 template<typename T, unsigned M, unsigned N>
174 inline Matrix<T, M, N> operator+(const Matrix<T, M, N> &m1, const Matrix<T, M, N> &m2)
176 Matrix<T, M, N> r(m1);
180 template<typename T, unsigned M, unsigned N>
181 inline Matrix<T, M, N> &Matrix<T, M, N>::operator-=(const Matrix<T, M, N> &m)
183 for(unsigned i=0; i<M*N; ++i)
184 data[i] -= m.data[i];
188 template<typename T, unsigned M, unsigned N>
189 inline Matrix<T, M, N> operator-(const Matrix<T, M, N> &m1, const Matrix<T, M, N> &m2)
191 Matrix<T, M, N> r(m1);
195 template<typename T, unsigned M, unsigned N>
196 inline bool operator==(const Matrix<T, M, N> &a, const Matrix<T, M, N> &b)
198 for(unsigned j=0; j<N; ++j)
199 for(unsigned i=0; i<M; ++i)
205 template<typename T, unsigned M, unsigned N>
206 inline Matrix<T, M, N> &Matrix<T, M, N>::exchange_rows(unsigned i, unsigned j)
209 for(unsigned k=0; k<N; ++k)
210 swap(element(i, k), element(j, k));
214 template<typename T, unsigned M, unsigned N>
215 inline Matrix<T, M, N> &Matrix<T, M, N>::multiply_row(unsigned i, T s)
217 for(unsigned k=0; k<N; ++k)
222 template<typename T, unsigned M, unsigned N>
223 inline Matrix<T, M, N> &Matrix<T, M, N>::add_row(unsigned i, unsigned j, T s)
225 for(unsigned k=0; k<N; ++k)
226 element(j, k) += element(i, k)*s;
230 template<typename T, unsigned M, unsigned N>
231 inline Matrix<T, N, M> transpose(const Matrix<T, M, N> &m)
234 for(unsigned j=0; j<N; ++j)
235 for(unsigned i=0; i<M; ++i)