# | Line 34 | Line 34 | |
---|---|---|
34 | #ifndef MATH_RECTMATRIX_HPP | |
35 | #define MATH_RECTMATRIX_HPP | |
36 | ||
37 | + | #include <cmath> |
38 | #include "Vector.hpp" | |
39 | ||
40 | namespace oopse { | |
41 | ||
41 | – | template<typename T> |
42 | – | inline bool equal(T e1, T e2) { |
43 | – | return e1 == e2; |
44 | – | } |
45 | – | |
46 | – | template<> |
47 | – | inline bool equal(float e1, float e2) { |
48 | – | return e1 == e2; |
49 | – | } |
50 | – | |
51 | – | template<> |
52 | – | inline bool equal(double e1, double e2) { |
53 | – | return e1 == e2; |
54 | – | } |
55 | – | |
42 | /** | |
43 | * @class RectMatrix RectMatrix.hpp "math/RectMatrix.hpp" | |
44 | * @brief rectangular matrix class | |
# | Line 60 | Line 46 | namespace oopse { | |
46 | template<typename Real, unsigned int Row, unsigned int Col> | |
47 | class RectMatrix { | |
48 | public: | |
49 | + | typedef Real ElemType; |
50 | + | typedef Real* ElemPoinerType; |
51 | + | |
52 | + | /** default constructor */ |
53 | + | RectMatrix() { |
54 | + | for (unsigned int i = 0; i < Row; i++) |
55 | + | for (unsigned int j = 0; j < Col; j++) |
56 | + | data_[i][j] = 0.0; |
57 | + | } |
58 | ||
59 | < | /** default constructor */ |
60 | < | RectMatrix() { |
61 | < | for (unsigned int i = 0; i < Row; i++) |
62 | < | for (unsigned int j = 0; j < Col; j++) |
63 | < | data_[i][j] = 0.0; |
64 | < | } |
59 | > | /** Constructs and initializes every element of this matrix to a scalar */ |
60 | > | RectMatrix(Real s) { |
61 | > | for (unsigned int i = 0; i < Row; i++) |
62 | > | for (unsigned int j = 0; j < Col; j++) |
63 | > | data_[i][j] = s; |
64 | > | } |
65 | ||
66 | < | /** Constructs and initializes every element of this matrix to a scalar */ |
67 | < | RectMatrix(Real s) { |
68 | < | for (unsigned int i = 0; i < Row; i++) |
69 | < | for (unsigned int j = 0; j < Col; j++) |
70 | < | data_[i][j] = s; |
76 | < | } |
66 | > | RectMatrix(Real* array) { |
67 | > | for (unsigned int i = 0; i < Row; i++) |
68 | > | for (unsigned int j = 0; j < Col; j++) |
69 | > | data_[i][j] = array[i * Row + j]; |
70 | > | } |
71 | ||
72 | < | /** copy constructor */ |
73 | < | RectMatrix(const RectMatrix<Real, Row, Col>& m) { |
74 | < | *this = m; |
75 | < | } |
76 | < | |
77 | < | /** destructor*/ |
78 | < | ~RectMatrix() {} |
72 | > | /** copy constructor */ |
73 | > | RectMatrix(const RectMatrix<Real, Row, Col>& m) { |
74 | > | *this = m; |
75 | > | } |
76 | > | |
77 | > | /** destructor*/ |
78 | > | ~RectMatrix() {} |
79 | ||
80 | < | /** copy assignment operator */ |
81 | < | RectMatrix<Real, Row, Col>& operator =(const RectMatrix<Real, Row, Col>& m) { |
82 | < | if (this == &m) |
80 | > | /** copy assignment operator */ |
81 | > | RectMatrix<Real, Row, Col>& operator =(const RectMatrix<Real, Row, Col>& m) { |
82 | > | if (this == &m) |
83 | > | return *this; |
84 | > | |
85 | > | for (unsigned int i = 0; i < Row; i++) |
86 | > | for (unsigned int j = 0; j < Col; j++) |
87 | > | data_[i][j] = m.data_[i][j]; |
88 | return *this; | |
89 | + | } |
90 | ||
91 | < | for (unsigned int i = 0; i < Row; i++) |
92 | < | for (unsigned int j = 0; j < Col; j++) |
93 | < | data_[i][j] = m.data_[i][j]; |
94 | < | return *this; |
95 | < | } |
96 | < | |
97 | < | /** |
98 | < | * Return the reference of a single element of this matrix. |
99 | < | * @return the reference of a single element of this matrix |
100 | < | * @param i row index |
101 | < | * @param j colum index |
102 | < | */ |
103 | < | double& operator()(unsigned int i, unsigned int j) { |
104 | < | //assert( i < Row && j < Col); |
105 | < | return data_[i][j]; |
106 | < | } |
91 | > | /** |
92 | > | * Return the reference of a single element of this matrix. |
93 | > | * @return the reference of a single element of this matrix |
94 | > | * @param i row index |
95 | > | * @param j colum index |
96 | > | */ |
97 | > | Real& operator()(unsigned int i, unsigned int j) { |
98 | > | //assert( i < Row && j < Col); |
99 | > | return data_[i][j]; |
100 | > | } |
101 | ||
102 | < | /** |
103 | < | * Return the value of a single element of this matrix. |
104 | < | * @return the value of a single element of this matrix |
105 | < | * @param i row index |
106 | < | * @param j colum index |
107 | < | */ |
108 | < | double operator()(unsigned int i, unsigned int j) const { |
109 | < | |
110 | < | return data_[i][j]; |
111 | < | } |
102 | > | /** |
103 | > | * Return the value of a single element of this matrix. |
104 | > | * @return the value of a single element of this matrix |
105 | > | * @param i row index |
106 | > | * @param j colum index |
107 | > | */ |
108 | > | Real operator()(unsigned int i, unsigned int j) const { |
109 | > | |
110 | > | return data_[i][j]; |
111 | > | } |
112 | ||
113 | < | /** |
114 | < | * Returns a row of this matrix as a vector. |
115 | < | * @return a row of this matrix as a vector |
116 | < | * @param row the row index |
117 | < | */ |
118 | < | Vector<Real, Row> getRow(unsigned int row) { |
119 | < | Vector<Real, Row> v; |
113 | > | /** |
114 | > | * Copy the internal data to an array |
115 | > | * @param array the pointer of destination array |
116 | > | */ |
117 | > | void getArray(Real* array) { |
118 | > | for (unsigned int i = 0; i < Row; i++) { |
119 | > | for (unsigned int j = 0; j < Col; j++) { |
120 | > | array[i * Row + j] = data_[i][j]; |
121 | > | } |
122 | > | } |
123 | > | } |
124 | ||
127 | – | for (unsigned int i = 0; i < Row; i++) |
128 | – | v[i] = data_[row][i]; |
125 | ||
126 | < | return v; |
127 | < | } |
126 | > | /** Returns the pointer of internal array */ |
127 | > | Real* getArrayPointer() { |
128 | > | return &data_[0][0]; |
129 | > | } |
130 | ||
131 | < | /** |
132 | < | * Sets a row of this matrix |
133 | < | * @param row the row index |
134 | < | * @param v the vector to be set |
135 | < | */ |
136 | < | void setRow(unsigned int row, const Vector<Real, Row>& v) { |
131 | > | /** |
132 | > | * Returns a row of this matrix as a vector. |
133 | > | * @return a row of this matrix as a vector |
134 | > | * @param row the row index |
135 | > | */ |
136 | > | Vector<Real, Row> getRow(unsigned int row) { |
137 | > | Vector<Real, Row> v; |
138 | ||
139 | < | for (unsigned int i = 0; i < Row; i++) |
140 | < | data_[row][i] = v[i]; |
142 | < | } |
139 | > | for (unsigned int i = 0; i < Row; i++) |
140 | > | v[i] = data_[row][i]; |
141 | ||
142 | < | /** |
143 | < | * Returns a column of this matrix as a vector. |
146 | < | * @return a column of this matrix as a vector |
147 | < | * @param col the column index |
148 | < | */ |
149 | < | Vector<Real, Col> getColum(unsigned int col) { |
150 | < | Vector<Real, Col> v; |
142 | > | return v; |
143 | > | } |
144 | ||
145 | < | for (unsigned int j = 0; j < Col; j++) |
146 | < | v[j] = data_[j][col]; |
145 | > | /** |
146 | > | * Sets a row of this matrix |
147 | > | * @param row the row index |
148 | > | * @param v the vector to be set |
149 | > | */ |
150 | > | void setRow(unsigned int row, const Vector<Real, Row>& v) { |
151 | ||
152 | < | return v; |
153 | < | } |
152 | > | for (unsigned int i = 0; i < Row; i++) |
153 | > | data_[row][i] = v[i]; |
154 | > | } |
155 | ||
156 | < | /** |
157 | < | * Sets a column of this matrix |
158 | < | * @param col the column index |
159 | < | * @param v the vector to be set |
160 | < | */ |
161 | < | void setColum(unsigned int col, const Vector<Real, Col>& v){ |
156 | > | /** |
157 | > | * Returns a column of this matrix as a vector. |
158 | > | * @return a column of this matrix as a vector |
159 | > | * @param col the column index |
160 | > | */ |
161 | > | Vector<Real, Col> getColum(unsigned int col) { |
162 | > | Vector<Real, Col> v; |
163 | ||
165 | – | for (unsigned int j = 0; j < Col; j++) |
166 | – | data_[j][col] = v[j]; |
167 | – | } |
168 | – | |
169 | – | /** |
170 | – | * Tests if this matrix is identical to matrix m |
171 | – | * @return true if this matrix is equal to the matrix m, return false otherwise |
172 | – | * @m matrix to be compared |
173 | – | * |
174 | – | * @todo replace operator == by template function equal |
175 | – | */ |
176 | – | bool operator ==(const RectMatrix<Real, Row, Col>& m) { |
177 | – | for (unsigned int i = 0; i < Row; i++) |
164 | for (unsigned int j = 0; j < Col; j++) | |
165 | < | if (!equal(data_[i][j], m.data_[i][j])) |
180 | < | return false; |
165 | > | v[j] = data_[j][col]; |
166 | ||
167 | < | return true; |
168 | < | } |
167 | > | return v; |
168 | > | } |
169 | ||
170 | < | /** |
171 | < | * Tests if this matrix is not equal to matrix m |
172 | < | * @return true if this matrix is not equal to the matrix m, return false otherwise |
173 | < | * @m matrix to be compared |
174 | < | */ |
175 | < | bool operator !=(const RectMatrix<Real, Row, Col>& m) { |
191 | < | return !(*this == m); |
192 | < | } |
170 | > | /** |
171 | > | * Sets a column of this matrix |
172 | > | * @param col the column index |
173 | > | * @param v the vector to be set |
174 | > | */ |
175 | > | void setColum(unsigned int col, const Vector<Real, Col>& v){ |
176 | ||
194 | – | /** Negates the value of this matrix in place. */ |
195 | – | inline void negate() { |
196 | – | for (unsigned int i = 0; i < Row; i++) |
177 | for (unsigned int j = 0; j < Col; j++) | |
178 | < | data_[i][j] = -data_[i][j]; |
179 | < | } |
200 | < | |
201 | < | /** |
202 | < | * Sets the value of this matrix to the negation of matrix m. |
203 | < | * @param m the source matrix |
204 | < | */ |
205 | < | inline void negate(const RectMatrix<Real, Row, Col>& m) { |
206 | < | for (unsigned int i = 0; i < Row; i++) |
207 | < | for (unsigned int j = 0; j < Col; j++) |
208 | < | data_[i][j] = -m.data_[i][j]; |
209 | < | } |
210 | < | |
211 | < | /** |
212 | < | * Sets the value of this matrix to the sum of itself and m (*this += m). |
213 | < | * @param m the other matrix |
214 | < | */ |
215 | < | inline void add( const RectMatrix<Real, Row, Col>& m ) { |
216 | < | for (unsigned int i = 0; i < Row; i++) |
217 | < | for (unsigned int j = 0; j < Col; j++) |
218 | < | data_[i][j] += m.data_[i][j]; |
219 | < | } |
220 | < | |
221 | < | /** |
222 | < | * Sets the value of this matrix to the sum of m1 and m2 (*this = m1 + m2). |
223 | < | * @param m1 the first matrix |
224 | < | * @param m2 the second matrix |
225 | < | */ |
226 | < | inline void add( const RectMatrix<Real, Row, Col>& m1, const RectMatrix<Real, Row, Col>& m2 ) { |
227 | < | for (unsigned int i = 0; i < Row; i++) |
228 | < | for (unsigned int j = 0; j < Col; j++) |
229 | < | data_[i][j] = m1.data_[i][j] + m2.data_[i][j]; |
230 | < | } |
231 | < | |
232 | < | /** |
233 | < | * Sets the value of this matrix to the difference of itself and m (*this -= m). |
234 | < | * @param m the other matrix |
235 | < | */ |
236 | < | inline void sub( const RectMatrix<Real, Row, Col>& m ) { |
237 | < | for (unsigned int i = 0; i < Row; i++) |
238 | < | for (unsigned int j = 0; j < Col; j++) |
239 | < | data_[i][j] -= m.data_[i][j]; |
240 | < | } |
241 | < | |
242 | < | /** |
243 | < | * Sets the value of this matrix to the difference of matrix m1 and m2 (*this = m1 - m2). |
244 | < | * @param m1 the first matrix |
245 | < | * @param m2 the second matrix |
246 | < | */ |
247 | < | inline void sub( const RectMatrix<Real, Row, Col>& m1, const RectMatrix<Real, Row, Col>& m2){ |
248 | < | for (unsigned int i = 0; i < Row; i++) |
249 | < | for (unsigned int j = 0; j < Col; j++) |
250 | < | data_[i][j] = m1.data_[i][j] - m2.data_[i][j]; |
251 | < | } |
178 | > | data_[j][col] = v[j]; |
179 | > | } |
180 | ||
181 | < | /** |
182 | < | * Sets the value of this matrix to the scalar multiplication of itself (*this *= s). |
183 | < | * @param s the scalar value |
184 | < | */ |
185 | < | inline void mul( double s ) { |
186 | < | for (unsigned int i = 0; i < Row; i++) |
187 | < | for (unsigned int j = 0; j < Col; j++) |
260 | < | data_[i][j] *= s; |
261 | < | } |
181 | > | /** |
182 | > | * swap two rows of this matrix |
183 | > | * @param i the first row |
184 | > | * @param j the second row |
185 | > | */ |
186 | > | void swapRow(unsigned int i, unsigned int j){ |
187 | > | assert(i < Row && j < Row); |
188 | ||
189 | < | /** |
190 | < | * Sets the value of this matrix to the scalar multiplication of matrix m (*this = s * m). |
191 | < | * @param s the scalar value |
266 | < | * @param m the matrix |
267 | < | */ |
268 | < | inline void mul( double s, const RectMatrix<Real, Row, Col>& m ) { |
269 | < | for (unsigned int i = 0; i < Row; i++) |
270 | < | for (unsigned int j = 0; j < Col; j++) |
271 | < | data_[i][j] = s * m.data_[i][j]; |
272 | < | } |
189 | > | for (unsigned int k = 0; k < Col; k++) |
190 | > | std::swap(data_[i][k], data_[j][k]); |
191 | > | } |
192 | ||
193 | < | /** |
194 | < | * Sets the value of this matrix to the scalar division of itself (*this /= s ). |
195 | < | * @param s the scalar value |
196 | < | */ |
197 | < | inline void div( double s) { |
198 | < | for (unsigned int i = 0; i < Row; i++) |
199 | < | for (unsigned int j = 0; j < Col; j++) |
200 | < | data_[i][j] /= s; |
201 | < | } |
193 | > | /** |
194 | > | * swap two colums of this matrix |
195 | > | * @param i the first colum |
196 | > | * @param j the second colum |
197 | > | */ |
198 | > | void swapColum(unsigned int i, unsigned int j){ |
199 | > | assert(i < Col && j < Col); |
200 | > | |
201 | > | for (unsigned int k = 0; k < Row; k++) |
202 | > | std::swap(data_[k][i], data_[k][j]); |
203 | > | } |
204 | ||
205 | < | /** |
206 | < | * Sets the value of this matrix to the scalar division of matrix m (*this = m /s). |
207 | < | * @param s the scalar value |
208 | < | * @param m the matrix |
209 | < | */ |
210 | < | inline void div( double s, const RectMatrix<Real, Row, Col>& m ) { |
211 | < | for (unsigned int i = 0; i < Row; i++) |
212 | < | for (unsigned int j = 0; j < Col; j++) |
213 | < | data_[i][j] = m.data_[i][j] / s; |
214 | < | } |
205 | > | /** |
206 | > | * Tests if this matrix is identical to matrix m |
207 | > | * @return true if this matrix is equal to the matrix m, return false otherwise |
208 | > | * @m matrix to be compared |
209 | > | * |
210 | > | * @todo replace operator == by template function equal |
211 | > | */ |
212 | > | bool operator ==(const RectMatrix<Real, Row, Col>& m) { |
213 | > | for (unsigned int i = 0; i < Row; i++) |
214 | > | for (unsigned int j = 0; j < Col; j++) |
215 | > | if (!equal(data_[i][j], m.data_[i][j])) |
216 | > | return false; |
217 | ||
218 | < | /** |
219 | < | * Multiples a scalar into every element of this matrix. |
297 | < | * @param s the scalar value |
298 | < | */ |
299 | < | RectMatrix<Real, Row, Col>& operator *=(const double s) { |
300 | < | this->mul(s); |
301 | < | return *this; |
302 | < | } |
218 | > | return true; |
219 | > | } |
220 | ||
221 | < | /** |
222 | < | * Divides every element of this matrix by a scalar. |
223 | < | * @param s the scalar value |
224 | < | */ |
225 | < | RectMatrix<Real, Row, Col>& operator /=(const double s) { |
226 | < | this->div(s); |
227 | < | return *this; |
228 | < | } |
221 | > | /** |
222 | > | * Tests if this matrix is not equal to matrix m |
223 | > | * @return true if this matrix is not equal to the matrix m, return false otherwise |
224 | > | * @m matrix to be compared |
225 | > | */ |
226 | > | bool operator !=(const RectMatrix<Real, Row, Col>& m) { |
227 | > | return !(*this == m); |
228 | > | } |
229 | ||
230 | < | /** |
231 | < | * Sets the value of this matrix to the sum of the other matrix and itself (*this += m). |
232 | < | * @param m the other matrix |
233 | < | */ |
234 | < | RectMatrix<Real, Row, Col>& operator += (const RectMatrix<Real, Row, Col>& m) { |
235 | < | add(m); |
319 | < | return *this; |
320 | < | } |
321 | < | |
322 | < | /** |
323 | < | * Sets the value of this matrix to the differerence of itself and the other matrix (*this -= m) |
324 | < | * @param m the other matrix |
325 | < | */ |
326 | < | RectMatrix<Real, Row, Col>& operator -= (const RectMatrix<Real, Row, Col>& m){ |
327 | < | sub(m); |
328 | < | return *this; |
329 | < | } |
330 | < | |
331 | < | /** Return the transpose of this matrix */ |
332 | < | RectMatrix<Real, Col, Row> transpose(){ |
333 | < | RectMatrix<Real, Col, Row> result; |
230 | > | /** Negates the value of this matrix in place. */ |
231 | > | inline void negate() { |
232 | > | for (unsigned int i = 0; i < Row; i++) |
233 | > | for (unsigned int j = 0; j < Col; j++) |
234 | > | data_[i][j] = -data_[i][j]; |
235 | > | } |
236 | ||
237 | < | for (unsigned int i = 0; i < Row; i++) |
238 | < | for (unsigned int j = 0; j < Col; j++) |
239 | < | result(j, i) = data_[i][j]; |
237 | > | /** |
238 | > | * Sets the value of this matrix to the negation of matrix m. |
239 | > | * @param m the source matrix |
240 | > | */ |
241 | > | inline void negate(const RectMatrix<Real, Row, Col>& m) { |
242 | > | for (unsigned int i = 0; i < Row; i++) |
243 | > | for (unsigned int j = 0; j < Col; j++) |
244 | > | data_[i][j] = -m.data_[i][j]; |
245 | > | } |
246 | > | |
247 | > | /** |
248 | > | * Sets the value of this matrix to the sum of itself and m (*this += m). |
249 | > | * @param m the other matrix |
250 | > | */ |
251 | > | inline void add( const RectMatrix<Real, Row, Col>& m ) { |
252 | > | for (unsigned int i = 0; i < Row; i++) |
253 | > | for (unsigned int j = 0; j < Col; j++) |
254 | > | data_[i][j] += m.data_[i][j]; |
255 | > | } |
256 | > | |
257 | > | /** |
258 | > | * Sets the value of this matrix to the sum of m1 and m2 (*this = m1 + m2). |
259 | > | * @param m1 the first matrix |
260 | > | * @param m2 the second matrix |
261 | > | */ |
262 | > | inline void add( const RectMatrix<Real, Row, Col>& m1, const RectMatrix<Real, Row, Col>& m2 ) { |
263 | > | for (unsigned int i = 0; i < Row; i++) |
264 | > | for (unsigned int j = 0; j < Col; j++) |
265 | > | data_[i][j] = m1.data_[i][j] + m2.data_[i][j]; |
266 | > | } |
267 | > | |
268 | > | /** |
269 | > | * Sets the value of this matrix to the difference of itself and m (*this -= m). |
270 | > | * @param m the other matrix |
271 | > | */ |
272 | > | inline void sub( const RectMatrix<Real, Row, Col>& m ) { |
273 | > | for (unsigned int i = 0; i < Row; i++) |
274 | > | for (unsigned int j = 0; j < Col; j++) |
275 | > | data_[i][j] -= m.data_[i][j]; |
276 | > | } |
277 | > | |
278 | > | /** |
279 | > | * Sets the value of this matrix to the difference of matrix m1 and m2 (*this = m1 - m2). |
280 | > | * @param m1 the first matrix |
281 | > | * @param m2 the second matrix |
282 | > | */ |
283 | > | inline void sub( const RectMatrix<Real, Row, Col>& m1, const RectMatrix<Real, Row, Col>& m2){ |
284 | > | for (unsigned int i = 0; i < Row; i++) |
285 | > | for (unsigned int j = 0; j < Col; j++) |
286 | > | data_[i][j] = m1.data_[i][j] - m2.data_[i][j]; |
287 | > | } |
288 | ||
289 | < | return result; |
290 | < | } |
289 | > | /** |
290 | > | * Sets the value of this matrix to the scalar multiplication of itself (*this *= s). |
291 | > | * @param s the scalar value |
292 | > | */ |
293 | > | inline void mul( Real s ) { |
294 | > | for (unsigned int i = 0; i < Row; i++) |
295 | > | for (unsigned int j = 0; j < Col; j++) |
296 | > | data_[i][j] *= s; |
297 | > | } |
298 | > | |
299 | > | /** |
300 | > | * Sets the value of this matrix to the scalar multiplication of matrix m (*this = s * m). |
301 | > | * @param s the scalar value |
302 | > | * @param m the matrix |
303 | > | */ |
304 | > | inline void mul( Real s, const RectMatrix<Real, Row, Col>& m ) { |
305 | > | for (unsigned int i = 0; i < Row; i++) |
306 | > | for (unsigned int j = 0; j < Col; j++) |
307 | > | data_[i][j] = s * m.data_[i][j]; |
308 | > | } |
309 | > | |
310 | > | /** |
311 | > | * Sets the value of this matrix to the scalar division of itself (*this /= s ). |
312 | > | * @param s the scalar value |
313 | > | */ |
314 | > | inline void div( Real s) { |
315 | > | for (unsigned int i = 0; i < Row; i++) |
316 | > | for (unsigned int j = 0; j < Col; j++) |
317 | > | data_[i][j] /= s; |
318 | > | } |
319 | > | |
320 | > | /** |
321 | > | * Sets the value of this matrix to the scalar division of matrix m (*this = m /s). |
322 | > | * @param s the scalar value |
323 | > | * @param m the matrix |
324 | > | */ |
325 | > | inline void div( Real s, const RectMatrix<Real, Row, Col>& m ) { |
326 | > | for (unsigned int i = 0; i < Row; i++) |
327 | > | for (unsigned int j = 0; j < Col; j++) |
328 | > | data_[i][j] = m.data_[i][j] / s; |
329 | > | } |
330 | > | |
331 | > | /** |
332 | > | * Multiples a scalar into every element of this matrix. |
333 | > | * @param s the scalar value |
334 | > | */ |
335 | > | RectMatrix<Real, Row, Col>& operator *=(const Real s) { |
336 | > | this->mul(s); |
337 | > | return *this; |
338 | > | } |
339 | > | |
340 | > | /** |
341 | > | * Divides every element of this matrix by a scalar. |
342 | > | * @param s the scalar value |
343 | > | */ |
344 | > | RectMatrix<Real, Row, Col>& operator /=(const Real s) { |
345 | > | this->div(s); |
346 | > | return *this; |
347 | > | } |
348 | > | |
349 | > | /** |
350 | > | * Sets the value of this matrix to the sum of the other matrix and itself (*this += m). |
351 | > | * @param m the other matrix |
352 | > | */ |
353 | > | RectMatrix<Real, Row, Col>& operator += (const RectMatrix<Real, Row, Col>& m) { |
354 | > | add(m); |
355 | > | return *this; |
356 | > | } |
357 | > | |
358 | > | /** |
359 | > | * Sets the value of this matrix to the differerence of itself and the other matrix (*this -= m) |
360 | > | * @param m the other matrix |
361 | > | */ |
362 | > | RectMatrix<Real, Row, Col>& operator -= (const RectMatrix<Real, Row, Col>& m){ |
363 | > | sub(m); |
364 | > | return *this; |
365 | > | } |
366 | > | |
367 | > | /** Return the transpose of this matrix */ |
368 | > | RectMatrix<Real, Col, Row> transpose(){ |
369 | > | RectMatrix<Real, Col, Row> result; |
370 | > | |
371 | > | for (unsigned int i = 0; i < Row; i++) |
372 | > | for (unsigned int j = 0; j < Col; j++) |
373 | > | result(j, i) = data_[i][j]; |
374 | > | |
375 | > | return result; |
376 | > | } |
377 | ||
378 | protected: | |
379 | Real data_[Row][Col]; | |
# | Line 426 | Line 462 | namespace oopse { | |
462 | for (unsigned int i = 0; i < Row; i++) | |
463 | for (unsigned int j = 0; j < Col; j++) | |
464 | for (unsigned int k = 0; k < SameDim; k++) | |
465 | < | result(i, j) = m1(i, k) * m2(k, j); |
465 | > | result(i, j) += m1(i, k) * m2(k, j); |
466 | ||
467 | return result; | |
468 | } | |
# | Line 462 | Line 498 | namespace oopse { | |
498 | ||
499 | return result; | |
500 | } | |
501 | + | |
502 | + | /** |
503 | + | * Write to an output stream |
504 | + | */ |
505 | + | template<typename Real, unsigned int Row, unsigned int Col> |
506 | + | std::ostream &operator<< ( std::ostream& o, const RectMatrix<Real, Row, Col>& m) { |
507 | + | for (unsigned int i = 0; i < Row ; i++) { |
508 | + | o << "("; |
509 | + | for (unsigned int j = 0; j < Col ; j++) { |
510 | + | o << m(i, j); |
511 | + | if (j != Col -1) |
512 | + | o << "\t"; |
513 | + | } |
514 | + | o << ")" << std::endl; |
515 | + | } |
516 | + | return o; |
517 | + | } |
518 | } | |
519 | #endif //MATH_RECTMATRIX_HPP |
– | Removed lines |
+ | Added lines |
< | Changed lines |
> | Changed lines |