]> git.tdb.fi Git - libs/math.git/blob - source/linal/matrix.h
ff73165d02c703c0efe263b27f23a522c54b7383
[libs/math.git] / source / linal / matrix.h
1 #ifndef MSP_LINAL_MATRIX_H_
2 #define MSP_LINAL_MATRIX_H_
3
4 #include <algorithm>
5 #include "vector.h"
6
7 namespace Msp {
8 namespace LinAl {
9
10 /**
11 A general mathematical matrix.
12 */
13 template<typename T, unsigned M, unsigned N>
14 class Matrix
15 {
16 private:
17         T data[M*N];
18
19 public:
20         Matrix();
21         Matrix(const T *);
22         template<typename U>
23         Matrix(const Matrix<U, M, N> &);
24
25         static Matrix from_columns(const Vector<T, M> *);
26         static Matrix from_rows(const Vector<T, N> *);
27
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); }
32
33         Matrix &operator*=(T);
34         Matrix &operator/=(T);
35         Matrix &operator+=(const Matrix &);
36         Matrix &operator-=(const Matrix &);
37
38         Matrix &exchange_rows(unsigned, unsigned);
39         Matrix &multiply_row(unsigned, T);
40         Matrix &add_row(unsigned, unsigned, T);
41 };
42
43 template<typename T, unsigned M, unsigned N>
44 inline Matrix<T, M, N>::Matrix()
45 {
46         std::fill(data, data+M*N, T());
47 }
48
49 template<typename T, unsigned M, unsigned N>
50 inline Matrix<T, M, N>::Matrix(const T *d)
51 {
52         std::copy(d, d+M*N, data);
53 }
54
55 template<typename T, unsigned M, unsigned N>
56 template<typename U>
57 inline Matrix<T, M, N>::Matrix(const Matrix<U, M, N> &m)
58 {
59         std::copy(m.data, m.data+M*N, data);
60 }
61
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)
64 {
65         Matrix<T, M, N> m;
66         for(unsigned i=0; i<M; ++i)
67                 for(unsigned j=0; j<N; ++j)
68                         m(i, j) = v[j][i];
69 }
70
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)
73 {
74         Matrix<T, M, N> m;
75         for(unsigned i=0; i<M; ++i)
76                 for(unsigned j=0; j<N; ++j)
77                         m(i, j) = v[i][j];
78 }
79
80 template<typename T, unsigned M, unsigned N>
81 inline Matrix<T, M, N> &Matrix<T, M, N>::operator*=(T s)
82 {
83         for(unsigned i=0; i<M*N; ++i)
84                 data[i] *= s;
85         return *this;
86 }
87
88 template<typename T, unsigned M, unsigned N>
89 inline Matrix<T, M, N> operator*(const Matrix<T, M, N> &m, T s)
90 {
91         Matrix<T, M, N> r(m);
92         return r *= s;
93 }
94
95 template<typename T, unsigned M, unsigned N>
96 inline Matrix<T, M, N> operator*(T s, const Matrix<T, M, N> &m)
97 {
98         return m*s;
99 }
100
101 template<typename T, unsigned M, unsigned P, unsigned N>
102 inline Matrix<T, M, N> operator*(const Matrix<T, M, P> &m1, const Matrix<T, P, N> &m2)
103 {
104         Matrix<T, M, N> r;
105         for(unsigned i=0; i<M; ++i)
106                 for(unsigned j=0; j<N; ++j)
107                         for(unsigned k=0; k<P; ++k)
108                                 r(i, j) += m1(i, k)*m2(k, j);
109         return r;
110 }
111
112 template<typename T, unsigned M, unsigned N>
113 inline Vector<T, M> operator*(const Matrix<T, M, N> &m, const Vector<T, N> &v)
114 {
115         Vector<T, M> r;
116         for(unsigned i=0; i<M; ++i)
117                 for(unsigned j=0; j<N; ++j)
118                         r[i] += m(i, j)*v[j];
119         return r;
120 }
121
122 template<typename T, unsigned M, unsigned N>
123 inline Vector<T, N> operator*(const Vector<T, M> &v, const Matrix<T, M, N> &m)
124 {
125         Vector<T, N> r;
126         for(unsigned j=0; j<N; ++j)
127                 for(unsigned i=0; i<M; ++i)
128                         r[j] += v[i]*m(i, j);
129         return r;
130 }
131
132 template<typename T, unsigned M, unsigned N>
133 inline Matrix<T, M, N> &Matrix<T, M, N>::operator/=(T s)
134 {
135         for(unsigned i=0; i<M*N; ++i)
136                 data[i] /= s;
137         return *this;
138 }
139
140 template<typename T, unsigned M, unsigned N>
141 inline Matrix<T, M, N> operator/(const Matrix<T, M, N> &m, T s)
142 {
143         Matrix<T, M, N> r(m);
144         return r /= s;
145 }
146
147 template<typename T, unsigned M, unsigned N>
148 inline Matrix<T, M, N> &Matrix<T, M, N>::operator+=(const Matrix<T, M, N> &m)
149 {
150         for(unsigned i=0; i<M*N; ++i)
151                 data[i] += m.data[i];
152         return *this;
153 }
154
155 template<typename T, unsigned M, unsigned N>
156 inline Matrix<T, M, N> operator+(const Matrix<T, M, N> &m1, const Matrix<T, M, N> &m2)
157 {
158         Matrix<T, M, N> r(m1);
159         return r += m2;
160 }
161
162 template<typename T, unsigned M, unsigned N>
163 inline Matrix<T, M, N> &Matrix<T, M, N>::operator-=(const Matrix<T, M, N> &m)
164 {
165         for(unsigned i=0; i<M*N; ++i)
166                 data[i] -= m.data[i];
167         return *this;
168 }
169
170 template<typename T, unsigned M, unsigned N>
171 inline Matrix<T, M, N> operator-(const Matrix<T, M, N> &m1, const Matrix<T, M, N> &m2)
172 {
173         Matrix<T, M, N> r(m1);
174         return r -= m2;
175 }
176
177 template<typename T, unsigned M, unsigned N>
178 inline bool operator==(const Matrix<T, M, N> &a, const Matrix<T, M, N> &b)
179 {
180         for(unsigned j=0; j<N; ++j)
181                 for(unsigned i=0; i<M; ++i)
182                         if(a(i, j)!=b(i, j))
183                                 return false;
184         return true;
185 }
186
187 template<typename T, unsigned M, unsigned N>
188 inline Matrix<T, M, N> &Matrix<T, M, N>::exchange_rows(unsigned i, unsigned j)
189 {
190         for(unsigned k=0; k<N; ++k)
191                 std::swap(element(i, k), element(j, k));
192         return *this;
193 }
194
195 template<typename T, unsigned M, unsigned N>
196 inline Matrix<T, M, N> &Matrix<T, M, N>::multiply_row(unsigned i, T s)
197 {
198         for(unsigned k=0; k<N; ++k)
199                 element(i, k) *= s;
200         return *this;
201 }
202
203 template<typename T, unsigned M, unsigned N>
204 inline Matrix<T, M, N> &Matrix<T, M, N>::add_row(unsigned i, unsigned j, T s)
205 {
206         for(unsigned k=0; k<N; ++k)
207                 element(j, k) += element(i, k)*s;
208         return *this;
209 }
210
211 template<typename T, unsigned M, unsigned N>
212 inline Matrix<T, N, M> transpose(const Matrix<T, M, N> &m)
213 {
214         Matrix<T, N, M> r;
215         for(unsigned j=0; j<N; ++j)
216                 for(unsigned i=0; i<M; ++i)
217                         r(j, i) = m(i, j);
218         return r;
219 }
220
221 } // namespace LinAl
222 } // namespace Msp
223
224 #endif