]> git.tdb.fi Git - libs/math.git/blob - source/linal/matrix.h
Fix Matrix template copy constructor
[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> &other)
58 {
59         for(unsigned i=0; i<M; ++i)
60                 for(unsigned j=0; j<N; ++j)
61                         element(i, j) = other(i, j);
62 }
63
64 template<typename T, unsigned M, unsigned N>
65 inline Matrix<T, M, N> Matrix<T, M, N>::from_columns(const Vector<T, M> *v)
66 {
67         Matrix<T, M, N> m;
68         for(unsigned i=0; i<M; ++i)
69                 for(unsigned j=0; j<N; ++j)
70                         m(i, j) = v[j][i];
71 }
72
73 template<typename T, unsigned M, unsigned N>
74 inline Matrix<T, M, N> Matrix<T, M, N>::from_rows(const Vector<T, N> *v)
75 {
76         Matrix<T, M, N> m;
77         for(unsigned i=0; i<M; ++i)
78                 for(unsigned j=0; j<N; ++j)
79                         m(i, j) = v[i][j];
80 }
81
82 template<typename T, unsigned M, unsigned N>
83 inline Matrix<T, M, N> &Matrix<T, M, N>::operator*=(T s)
84 {
85         for(unsigned i=0; i<M*N; ++i)
86                 data[i] *= s;
87         return *this;
88 }
89
90 template<typename T, unsigned M, unsigned N>
91 inline Matrix<T, M, N> operator*(const Matrix<T, M, N> &m, T s)
92 {
93         Matrix<T, M, N> r(m);
94         return r *= s;
95 }
96
97 template<typename T, unsigned M, unsigned N>
98 inline Matrix<T, M, N> operator*(T s, const Matrix<T, M, N> &m)
99 {
100         return m*s;
101 }
102
103 template<typename T, unsigned M, unsigned P, unsigned N>
104 inline Matrix<T, M, N> operator*(const Matrix<T, M, P> &m1, const Matrix<T, P, N> &m2)
105 {
106         Matrix<T, M, N> r;
107         for(unsigned i=0; i<M; ++i)
108                 for(unsigned j=0; j<N; ++j)
109                         for(unsigned k=0; k<P; ++k)
110                                 r(i, j) += m1(i, k)*m2(k, j);
111         return r;
112 }
113
114 template<typename T, unsigned M, unsigned N>
115 inline Vector<T, M> operator*(const Matrix<T, M, N> &m, const Vector<T, N> &v)
116 {
117         Vector<T, M> r;
118         for(unsigned i=0; i<M; ++i)
119                 for(unsigned j=0; j<N; ++j)
120                         r[i] += m(i, j)*v[j];
121         return r;
122 }
123
124 template<typename T, unsigned M, unsigned N>
125 inline Vector<T, N> operator*(const Vector<T, M> &v, const Matrix<T, M, N> &m)
126 {
127         Vector<T, N> r;
128         for(unsigned j=0; j<N; ++j)
129                 for(unsigned i=0; i<M; ++i)
130                         r[j] += v[i]*m(i, j);
131         return r;
132 }
133
134 template<typename T, unsigned M, unsigned N>
135 inline Matrix<T, M, N> &Matrix<T, M, N>::operator/=(T s)
136 {
137         for(unsigned i=0; i<M*N; ++i)
138                 data[i] /= s;
139         return *this;
140 }
141
142 template<typename T, unsigned M, unsigned N>
143 inline Matrix<T, M, N> operator/(const Matrix<T, M, N> &m, T s)
144 {
145         Matrix<T, M, N> r(m);
146         return r /= s;
147 }
148
149 template<typename T, unsigned M, unsigned N>
150 inline Matrix<T, M, N> &Matrix<T, M, N>::operator+=(const Matrix<T, M, N> &m)
151 {
152         for(unsigned i=0; i<M*N; ++i)
153                 data[i] += m.data[i];
154         return *this;
155 }
156
157 template<typename T, unsigned M, unsigned N>
158 inline Matrix<T, M, N> operator+(const Matrix<T, M, N> &m1, const Matrix<T, M, N> &m2)
159 {
160         Matrix<T, M, N> r(m1);
161         return r += m2;
162 }
163
164 template<typename T, unsigned M, unsigned N>
165 inline Matrix<T, M, N> &Matrix<T, M, N>::operator-=(const Matrix<T, M, N> &m)
166 {
167         for(unsigned i=0; i<M*N; ++i)
168                 data[i] -= m.data[i];
169         return *this;
170 }
171
172 template<typename T, unsigned M, unsigned N>
173 inline Matrix<T, M, N> operator-(const Matrix<T, M, N> &m1, const Matrix<T, M, N> &m2)
174 {
175         Matrix<T, M, N> r(m1);
176         return r -= m2;
177 }
178
179 template<typename T, unsigned M, unsigned N>
180 inline bool operator==(const Matrix<T, M, N> &a, const Matrix<T, M, N> &b)
181 {
182         for(unsigned j=0; j<N; ++j)
183                 for(unsigned i=0; i<M; ++i)
184                         if(a(i, j)!=b(i, j))
185                                 return false;
186         return true;
187 }
188
189 template<typename T, unsigned M, unsigned N>
190 inline Matrix<T, M, N> &Matrix<T, M, N>::exchange_rows(unsigned i, unsigned j)
191 {
192         for(unsigned k=0; k<N; ++k)
193                 std::swap(element(i, k), element(j, k));
194         return *this;
195 }
196
197 template<typename T, unsigned M, unsigned N>
198 inline Matrix<T, M, N> &Matrix<T, M, N>::multiply_row(unsigned i, T s)
199 {
200         for(unsigned k=0; k<N; ++k)
201                 element(i, k) *= s;
202         return *this;
203 }
204
205 template<typename T, unsigned M, unsigned N>
206 inline Matrix<T, M, N> &Matrix<T, M, N>::add_row(unsigned i, unsigned j, T s)
207 {
208         for(unsigned k=0; k<N; ++k)
209                 element(j, k) += element(i, k)*s;
210         return *this;
211 }
212
213 template<typename T, unsigned M, unsigned N>
214 inline Matrix<T, N, M> transpose(const Matrix<T, M, N> &m)
215 {
216         Matrix<T, N, M> r;
217         for(unsigned j=0; j<N; ++j)
218                 for(unsigned i=0; i<M; ++i)
219                         r(j, i) = m(i, j);
220         return r;
221 }
222
223 } // namespace LinAl
224 } // namespace Msp
225
226 #endif