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>
17 typedef T ElementType;
26 Matrix(const Matrix<U, M, N> &);
28 static Matrix from_columns(const Vector<T, M> *);
29 static Matrix from_rows(const Vector<T, N> *);
31 unsigned rows() const { return M; }
32 unsigned columns() const { return N; }
34 T &element(unsigned i, unsigned j) { return data[i+M*j]; }
35 const T &element(unsigned i, unsigned j) const { return data[i+M*j]; }
36 T &operator()(unsigned i, unsigned j) { return element(i, j); }
37 const T &operator()(unsigned i, unsigned j) const { return element(i, j); }
39 Vector<T, M> column(unsigned i) const { return Vector<T, M>(data+M*i); }
40 Vector<T, N> row(unsigned i) const { return Vector<T, N>(data+i, M); }
42 template<unsigned P, unsigned Q>
43 Matrix<T, P, Q> select(const Vector<unsigned, P> &, const Vector<unsigned, Q> &) const;
45 template<unsigned P, unsigned Q>
46 Matrix<T, P, Q> block(unsigned, unsigned) const;
48 Matrix &operator*=(T);
49 Matrix &operator/=(T);
50 Matrix &operator+=(const Matrix &);
51 Matrix &operator-=(const Matrix &);
53 Matrix &exchange_rows(unsigned, unsigned);
54 Matrix &multiply_row(unsigned, T);
55 Matrix &add_row(unsigned, unsigned, T);
58 template<typename T, unsigned M, unsigned N>
59 inline Matrix<T, M, N>::Matrix()
61 std::fill(data, data+M*N, T());
64 template<typename T, unsigned M, unsigned N>
65 inline Matrix<T, M, N>::Matrix(const T *d)
67 std::copy(d, d+M*N, data);
70 template<typename T, unsigned M, unsigned N>
72 inline Matrix<T, M, N>::Matrix(const Matrix<U, M, N> &other)
74 for(unsigned i=0; i<M; ++i)
75 for(unsigned j=0; j<N; ++j)
76 element(i, j) = other(i, j);
79 template<typename T, unsigned M, unsigned N>
80 inline Matrix<T, M, N> Matrix<T, M, N>::from_columns(const Vector<T, M> *v)
83 for(unsigned i=0; i<M; ++i)
84 for(unsigned j=0; j<N; ++j)
89 template<typename T, unsigned M, unsigned N>
90 inline Matrix<T, M, N> Matrix<T, M, N>::from_rows(const Vector<T, N> *v)
93 for(unsigned i=0; i<M; ++i)
94 for(unsigned j=0; j<N; ++j)
99 template<typename T, unsigned M, unsigned N>
100 template<unsigned P, unsigned Q>
101 inline Matrix<T, P, Q> Matrix<T, M, N>::select(const Vector<unsigned, P> &row_indices, const Vector<unsigned, Q> &col_indices) const
104 for(unsigned j=0; j<P; ++j)
105 for(unsigned i=0; i<Q; ++i)
106 r(j, i) = element(row_indices[j], col_indices[i]);
110 template<typename T, unsigned M, unsigned N>
111 template<unsigned P, unsigned Q>
112 inline Matrix<T, P, Q> Matrix<T, M, N>::block(unsigned y, unsigned x) const
115 for(unsigned j=0; j<P; ++j)
116 for(unsigned i=0; i<Q; ++i)
117 r(j, i) = element(y+j, x+i);
121 template<typename T, unsigned M, unsigned N>
122 inline Matrix<T, M, N> &Matrix<T, M, N>::operator*=(T s)
124 for(unsigned i=0; i<M*N; ++i)
129 template<typename T, unsigned M, unsigned N>
130 inline Matrix<T, M, N> operator*(const Matrix<T, M, N> &m, T s)
132 Matrix<T, M, N> r(m);
136 template<typename T, unsigned M, unsigned N>
137 inline Matrix<T, M, N> operator*(T s, const Matrix<T, M, N> &m)
142 template<typename T, unsigned M, unsigned P, unsigned N>
143 inline Matrix<T, M, N> operator*(const Matrix<T, M, P> &m1, const Matrix<T, P, N> &m2)
146 for(unsigned i=0; i<M; ++i)
147 for(unsigned j=0; j<N; ++j)
148 for(unsigned k=0; k<P; ++k)
149 r(i, j) += m1(i, k)*m2(k, j);
153 template<typename T, unsigned M, unsigned N>
154 inline Vector<T, M> operator*(const Matrix<T, M, N> &m, const Vector<T, N> &v)
157 for(unsigned i=0; i<M; ++i)
158 for(unsigned j=0; j<N; ++j)
159 r[i] += m(i, j)*v[j];
163 template<typename T, unsigned M, unsigned N>
164 inline Vector<T, N> operator*(const Vector<T, M> &v, const Matrix<T, M, N> &m)
167 for(unsigned j=0; j<N; ++j)
168 for(unsigned i=0; i<M; ++i)
169 r[j] += v[i]*m(i, j);
173 template<typename T, unsigned M, unsigned N>
174 inline Matrix<T, M, N> &Matrix<T, M, N>::operator/=(T s)
176 for(unsigned i=0; i<M*N; ++i)
181 template<typename T, unsigned M, unsigned N>
182 inline Matrix<T, M, N> operator/(const Matrix<T, M, N> &m, T s)
184 Matrix<T, M, N> r(m);
188 template<typename T, unsigned M, unsigned N>
189 inline Matrix<T, M, N> &Matrix<T, M, N>::operator+=(const Matrix<T, M, N> &m)
191 for(unsigned i=0; i<M*N; ++i)
192 data[i] += m.data[i];
196 template<typename T, unsigned M, unsigned N>
197 inline Matrix<T, M, N> operator+(const Matrix<T, M, N> &m1, const Matrix<T, M, N> &m2)
199 Matrix<T, M, N> r(m1);
203 template<typename T, unsigned M, unsigned N>
204 inline Matrix<T, M, N> &Matrix<T, M, N>::operator-=(const Matrix<T, M, N> &m)
206 for(unsigned i=0; i<M*N; ++i)
207 data[i] -= m.data[i];
211 template<typename T, unsigned M, unsigned N>
212 inline Matrix<T, M, N> operator-(const Matrix<T, M, N> &m1, const Matrix<T, M, N> &m2)
214 Matrix<T, M, N> r(m1);
218 template<typename T, unsigned M, unsigned N>
219 inline bool operator==(const Matrix<T, M, N> &a, const Matrix<T, M, N> &b)
221 for(unsigned j=0; j<N; ++j)
222 for(unsigned i=0; i<M; ++i)
228 template<typename T, unsigned M, unsigned N>
229 inline Matrix<T, M, N> &Matrix<T, M, N>::exchange_rows(unsigned i, unsigned j)
232 for(unsigned k=0; k<N; ++k)
233 swap(element(i, k), element(j, k));
237 template<typename T, unsigned M, unsigned N>
238 inline Matrix<T, M, N> &Matrix<T, M, N>::multiply_row(unsigned i, T s)
240 for(unsigned k=0; k<N; ++k)
245 template<typename T, unsigned M, unsigned N>
246 inline Matrix<T, M, N> &Matrix<T, M, N>::add_row(unsigned i, unsigned j, T s)
248 for(unsigned k=0; k<N; ++k)
249 element(j, k) += element(i, k)*s;
253 template<typename T, unsigned M, unsigned N>
254 inline Matrix<T, N, M> transpose(const Matrix<T, M, N> &m)
257 for(unsigned j=0; j<N; ++j)
258 for(unsigned i=0; i<M; ++i)