1 #ifndef MSP_LIEAL_MATMIX_H_
2 #define MSP_LIEAL_MATMIX_H_
10 A general mathematical matrix.
12 template<typename T, unsigned M, unsigned N>
22 Matrix(const Matrix<U, M, N> &);
23 static Matrix from_columns(const Vector<T, M> *);
24 static Matrix from_rows(const Vector<T, N> *);
26 T &operator()(unsigned, unsigned);
27 const T &operator()(unsigned, unsigned) const;
29 Matrix &operator*=(T);
30 Matrix &operator/=(T);
31 Matrix &operator+=(const Matrix &);
32 Matrix &operator-=(const Matrix &);
34 Matrix<T, N, M> transpose() const;
37 template<typename T, unsigned M, unsigned N>
38 inline T &Matrix<T, M, N>::operator()(unsigned i, unsigned j)
43 template<typename T, unsigned M, unsigned N>
44 inline const T &Matrix<T, M, N>::operator()(unsigned i, unsigned j) const
49 template<typename T, unsigned M, unsigned N>
50 inline Matrix<T, M, N> &Matrix<T, M, N>::operator*=(T s)
52 for(unsigned i=0; i<M*N; ++i)
57 template<typename T, unsigned M, unsigned N>
58 inline Matrix<T, M, N> operator*(const Matrix<T, M, N> &m, T s)
64 template<typename T, unsigned M, unsigned N>
65 inline Matrix<T, M, N> operator*(T s, const Matrix<T, M, N> &m)
70 template<typename T, unsigned M, unsigned N>
71 inline Matrix<T, M, N> &Matrix<T, M, N>::operator/=(T s)
73 for(unsigned i=0; i<M*N; ++i)
78 template<typename T, unsigned M, unsigned N>
79 inline Matrix<T, M, N> operator/(const Matrix<T, M, N> &m, T s)
85 template<typename T, unsigned M, unsigned N>
86 inline Matrix<T, M, N> &Matrix<T, M, N>::operator+=(const Matrix<T, M, N> &m)
88 for(unsigned i=0; i<M*N; ++i)
93 template<typename T, unsigned M, unsigned N>
94 inline Matrix<T, M, N> operator+(const Matrix<T, M, N> &m1, const Matrix<T, M, N> &m2)
96 Matrix<T, M, N> r(m1);
100 template<typename T, unsigned M, unsigned N>
101 inline Matrix<T, M, N> &Matrix<T, M, N>::operator-=(const Matrix<T, M, N> &m)
103 for(unsigned i=0; i<M*N; ++i)
104 data[i] -= m.data[i];
108 template<typename T, unsigned M, unsigned N>
109 inline Matrix<T, M, N> operator-(const Matrix<T, M, N> &m1, const Matrix<T, M, N> &m2)
111 Matrix<T, M, N> r(m1);
115 template<typename T, unsigned M, unsigned P, unsigned N>
116 Matrix<T, M, N> operator*(const Matrix<T, M, P> &m1, const Matrix<T, P, N> &m2)
119 for(unsigned i=0; i<M; ++i)
120 for(unsigned j=0; j<N; ++j)
121 for(unsigned k=0; k<P; ++k)
122 r.at(i, j) += m1(i, k)*m2(k, j);
126 template<typename T, unsigned M, unsigned N>
127 Vector<T, M> operator*(const Matrix<T, M, N> &m, const Vector<T, N> &v)
130 for(unsigned i=0; i<M; ++i)
131 for(unsigned j=0; j<N; ++j)
132 r[i] += m(i, j)*v[j];
136 template<typename T, unsigned M, unsigned N>
137 Vector<T, N> operator*(const Vector<T, M> &v, const Matrix<T, M, N> &m)
140 for(unsigned j=0; j<N; ++j)
141 for(unsigned i=0; i<M; ++i)
142 r[j] += v[i]*m.at(i, j);