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 template<unsigned P, unsigned Q>
40 Matrix<T, P, Q> select(const Vector<unsigned, P> &, const Vector<unsigned, Q> &) const;
42 template<unsigned P, unsigned Q>
43 Matrix<T, P, Q> block(unsigned, unsigned) const;
45 Matrix &operator*=(T);
46 Matrix &operator/=(T);
47 Matrix &operator+=(const Matrix &);
48 Matrix &operator-=(const Matrix &);
50 Matrix &exchange_rows(unsigned, unsigned);
51 Matrix &multiply_row(unsigned, T);
52 Matrix &add_row(unsigned, unsigned, T);
55 template<typename T, unsigned M, unsigned N>
56 inline Matrix<T, M, N>::Matrix()
58 std::fill(data, data+M*N, T());
61 template<typename T, unsigned M, unsigned N>
62 inline Matrix<T, M, N>::Matrix(const T *d)
64 std::copy(d, d+M*N, data);
67 template<typename T, unsigned M, unsigned N>
69 inline Matrix<T, M, N>::Matrix(const Matrix<U, M, N> &other)
71 for(unsigned i=0; i<M; ++i)
72 for(unsigned j=0; j<N; ++j)
73 element(i, j) = other(i, j);
76 template<typename T, unsigned M, unsigned N>
77 inline Matrix<T, M, N> Matrix<T, M, N>::from_columns(const Vector<T, M> *v)
80 for(unsigned i=0; i<M; ++i)
81 for(unsigned j=0; j<N; ++j)
86 template<typename T, unsigned M, unsigned N>
87 inline Matrix<T, M, N> Matrix<T, M, N>::from_rows(const Vector<T, N> *v)
90 for(unsigned i=0; i<M; ++i)
91 for(unsigned j=0; j<N; ++j)
96 template<typename T, unsigned M, unsigned N>
97 template<unsigned P, unsigned Q>
98 inline Matrix<T, P, Q> Matrix<T, M, N>::select(const Vector<unsigned, P> &row_indices, const Vector<unsigned, Q> &col_indices) const
101 for(unsigned j=0; j<P; ++j)
102 for(unsigned i=0; i<Q; ++i)
103 r(j, i) = element(row_indices[j], col_indices[i]);
107 template<typename T, unsigned M, unsigned N>
108 template<unsigned P, unsigned Q>
109 inline Matrix<T, P, Q> Matrix<T, M, N>::block(unsigned y, unsigned x) const
112 for(unsigned j=0; j<P; ++j)
113 for(unsigned i=0; i<Q; ++i)
114 r(j, i) = element(y+j, x+i);
118 template<typename T, unsigned M, unsigned N>
119 inline Matrix<T, M, N> &Matrix<T, M, N>::operator*=(T s)
121 for(unsigned i=0; i<M*N; ++i)
126 template<typename T, unsigned M, unsigned N>
127 inline Matrix<T, M, N> operator*(const Matrix<T, M, N> &m, T s)
129 Matrix<T, M, N> r(m);
133 template<typename T, unsigned M, unsigned N>
134 inline Matrix<T, M, N> operator*(T s, const Matrix<T, M, N> &m)
139 template<typename T, unsigned M, unsigned P, unsigned N>
140 inline Matrix<T, M, N> operator*(const Matrix<T, M, P> &m1, const Matrix<T, P, N> &m2)
143 for(unsigned i=0; i<M; ++i)
144 for(unsigned j=0; j<N; ++j)
145 for(unsigned k=0; k<P; ++k)
146 r(i, j) += m1(i, k)*m2(k, j);
150 template<typename T, unsigned M, unsigned N>
151 inline Vector<T, M> operator*(const Matrix<T, M, N> &m, const Vector<T, N> &v)
154 for(unsigned i=0; i<M; ++i)
155 for(unsigned j=0; j<N; ++j)
156 r[i] += m(i, j)*v[j];
160 template<typename T, unsigned M, unsigned N>
161 inline Vector<T, N> operator*(const Vector<T, M> &v, const Matrix<T, M, N> &m)
164 for(unsigned j=0; j<N; ++j)
165 for(unsigned i=0; i<M; ++i)
166 r[j] += v[i]*m(i, j);
170 template<typename T, unsigned M, unsigned N>
171 inline Matrix<T, M, N> &Matrix<T, M, N>::operator/=(T s)
173 for(unsigned i=0; i<M*N; ++i)
178 template<typename T, unsigned M, unsigned N>
179 inline Matrix<T, M, N> operator/(const Matrix<T, M, N> &m, T s)
181 Matrix<T, M, N> r(m);
185 template<typename T, unsigned M, unsigned N>
186 inline Matrix<T, M, N> &Matrix<T, M, N>::operator+=(const Matrix<T, M, N> &m)
188 for(unsigned i=0; i<M*N; ++i)
189 data[i] += m.data[i];
193 template<typename T, unsigned M, unsigned N>
194 inline Matrix<T, M, N> operator+(const Matrix<T, M, N> &m1, const Matrix<T, M, N> &m2)
196 Matrix<T, M, N> r(m1);
200 template<typename T, unsigned M, unsigned N>
201 inline Matrix<T, M, N> &Matrix<T, M, N>::operator-=(const Matrix<T, M, N> &m)
203 for(unsigned i=0; i<M*N; ++i)
204 data[i] -= m.data[i];
208 template<typename T, unsigned M, unsigned N>
209 inline Matrix<T, M, N> operator-(const Matrix<T, M, N> &m1, const Matrix<T, M, N> &m2)
211 Matrix<T, M, N> r(m1);
215 template<typename T, unsigned M, unsigned N>
216 inline bool operator==(const Matrix<T, M, N> &a, const Matrix<T, M, N> &b)
218 for(unsigned j=0; j<N; ++j)
219 for(unsigned i=0; i<M; ++i)
225 template<typename T, unsigned M, unsigned N>
226 inline Matrix<T, M, N> &Matrix<T, M, N>::exchange_rows(unsigned i, unsigned j)
229 for(unsigned k=0; k<N; ++k)
230 swap(element(i, k), element(j, k));
234 template<typename T, unsigned M, unsigned N>
235 inline Matrix<T, M, N> &Matrix<T, M, N>::multiply_row(unsigned i, T s)
237 for(unsigned k=0; k<N; ++k)
242 template<typename T, unsigned M, unsigned N>
243 inline Matrix<T, M, N> &Matrix<T, M, N>::add_row(unsigned i, unsigned j, T s)
245 for(unsigned k=0; k<N; ++k)
246 element(j, k) += element(i, k)*s;
250 template<typename T, unsigned M, unsigned N>
251 inline Matrix<T, N, M> transpose(const Matrix<T, M, N> &m)
254 for(unsigned j=0; j<N; ++j)
255 for(unsigned i=0; i<M; ++i)