1 #ifndef MSP_LINAL_MATRIX_H_
2 #define MSP_LINAL_MATRIX_H_
11 A general mathematical matrix.
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, unsigned);
29 const T &element(unsigned, unsigned) const;
30 T &operator()(unsigned, unsigned);
31 const T &operator()(unsigned, unsigned) const;
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> &m)
59 std::copy(m.data, m.data+M*N, data);
62 template<typename T, unsigned M, unsigned N>
63 inline Matrix<T, M, N> Matrix<T, M, N>::from_columns(const Vector<T, M> *v)
66 for(unsigned i=0; i<M; ++i)
67 for(unsigned j=0; j<N; ++j)
71 template<typename T, unsigned M, unsigned N>
72 inline Matrix<T, M, N> Matrix<T, M, N>::from_rows(const Vector<T, N> *v)
75 for(unsigned i=0; i<M; ++i)
76 for(unsigned j=0; j<N; ++j)
80 template<typename T, unsigned M, unsigned N>
81 inline T &Matrix<T, M, N>::element(unsigned i, unsigned j)
86 template<typename T, unsigned M, unsigned N>
87 inline const T &Matrix<T, M, N>::element(unsigned i, unsigned j) const
92 template<typename T, unsigned M, unsigned N>
93 inline T &Matrix<T, M, N>::operator()(unsigned i, unsigned j)
98 template<typename T, unsigned M, unsigned N>
99 inline const T &Matrix<T, M, N>::operator()(unsigned i, unsigned j) const
101 return element(i, j);
104 template<typename T, unsigned M, unsigned N>
105 inline Matrix<T, M, N> &Matrix<T, M, N>::operator*=(T s)
107 for(unsigned i=0; i<M*N; ++i)
112 template<typename T, unsigned M, unsigned N>
113 inline Matrix<T, M, N> operator*(const Matrix<T, M, N> &m, T s)
115 Matrix<T, M, N> r(m);
119 template<typename T, unsigned M, unsigned N>
120 inline Matrix<T, M, N> operator*(T s, const Matrix<T, M, N> &m)
125 template<typename T, unsigned M, unsigned P, unsigned N>
126 inline Matrix<T, M, N> operator*(const Matrix<T, M, P> &m1, const Matrix<T, P, N> &m2)
129 for(unsigned i=0; i<M; ++i)
130 for(unsigned j=0; j<N; ++j)
131 for(unsigned k=0; k<P; ++k)
132 r(i, j) += m1(i, k)*m2(k, j);
136 template<typename T, unsigned M, unsigned N>
137 inline Vector<T, M> operator*(const Matrix<T, M, N> &m, const Vector<T, N> &v)
140 for(unsigned i=0; i<M; ++i)
141 for(unsigned j=0; j<N; ++j)
142 r[i] += m(i, j)*v[j];
146 template<typename T, unsigned M, unsigned N>
147 inline Vector<T, N> operator*(const Vector<T, M> &v, const Matrix<T, M, N> &m)
150 for(unsigned j=0; j<N; ++j)
151 for(unsigned i=0; i<M; ++i)
152 r[j] += v[i]*m(i, j);
156 template<typename T, unsigned M, unsigned N>
157 inline Matrix<T, M, N> &Matrix<T, M, N>::operator/=(T s)
159 for(unsigned i=0; i<M*N; ++i)
164 template<typename T, unsigned M, unsigned N>
165 inline Matrix<T, M, N> operator/(const Matrix<T, M, N> &m, T s)
167 Matrix<T, M, N> r(m);
171 template<typename T, unsigned M, unsigned N>
172 inline Matrix<T, M, N> &Matrix<T, M, N>::operator+=(const Matrix<T, M, N> &m)
174 for(unsigned i=0; i<M*N; ++i)
175 data[i] += m.data[i];
179 template<typename T, unsigned M, unsigned N>
180 inline Matrix<T, M, N> operator+(const Matrix<T, M, N> &m1, const Matrix<T, M, N> &m2)
182 Matrix<T, M, N> r(m1);
186 template<typename T, unsigned M, unsigned N>
187 inline Matrix<T, M, N> &Matrix<T, M, N>::operator-=(const Matrix<T, M, N> &m)
189 for(unsigned i=0; i<M*N; ++i)
190 data[i] -= m.data[i];
194 template<typename T, unsigned M, unsigned N>
195 inline Matrix<T, M, N> operator-(const Matrix<T, M, N> &m1, const Matrix<T, M, N> &m2)
197 Matrix<T, M, N> r(m1);
201 template<typename T, unsigned M, unsigned N>
202 inline bool operator==(const Matrix<T, M, N> &a, const Matrix<T, M, N> &b)
204 for(unsigned j=0; j<N; ++j)
205 for(unsigned i=0; i<M; ++i)
211 template<typename T, unsigned M, unsigned N>
212 inline Matrix<T, M, N> &Matrix<T, M, N>::exchange_rows(unsigned i, unsigned j)
214 for(unsigned k=0; k<N; ++k)
215 std::swap(element(i, k), element(j, k));
219 template<typename T, unsigned M, unsigned N>
220 inline Matrix<T, M, N> &Matrix<T, M, N>::multiply_row(unsigned i, T s)
222 for(unsigned k=0; k<N; ++k)
227 template<typename T, unsigned M, unsigned N>
228 inline Matrix<T, M, N> &Matrix<T, M, N>::add_row(unsigned i, unsigned j, T s)
230 for(unsigned k=0; k<N; ++k)
231 element(j, k) += element(i, k)*s;
235 template<typename T, unsigned M, unsigned N>
236 inline Matrix<T, N, M> transpose(const Matrix<T, M, N> &m)
239 for(unsigned j=0; j<N; ++j)
240 for(unsigned i=0; i<M; ++i)