1 |
/* |
2 |
* Copyright (C) 2000-2004 Object Oriented Parallel Simulation Engine (OOPSE) project |
3 |
* |
4 |
* Contact: oopse@oopse.org |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or |
7 |
* modify it under the terms of the GNU Lesser General Public License |
8 |
* as published by the Free Software Foundation; either version 2.1 |
9 |
* of the License, or (at your option) any later version. |
10 |
* All we ask is that proper credit is given for our work, which includes |
11 |
* - but is not limited to - adding the above copyright notice to the beginning |
12 |
* of your source code files, and to any copyright notice that you may distribute |
13 |
* with programs based on this work. |
14 |
* |
15 |
* This program is distributed in the hope that it will be useful, |
16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 |
* GNU Lesser General Public License for more details. |
19 |
* |
20 |
* You should have received a copy of the GNU Lesser General Public License |
21 |
* along with this program; if not, write to the Free Software |
22 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
23 |
* |
24 |
*/ |
25 |
|
26 |
|
27 |
/** |
28 |
* @file RectMatrix.hpp |
29 |
* @author Teng Lin |
30 |
* @date 10/11/2004 |
31 |
* @version 1.0 |
32 |
*/ |
33 |
|
34 |
#ifndef MATH_RECTMATRIX_HPP |
35 |
#define MATH_RECTMATRIX_HPP |
36 |
|
37 |
#include "Vector.hpp" |
38 |
|
39 |
namespace oopse { |
40 |
|
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 |
|
56 |
/** |
57 |
* @class RectMatrix RectMatrix.hpp "math/RectMatrix.hpp" |
58 |
* @brief rectangular matrix class |
59 |
*/ |
60 |
template<typename Real, unsigned int Row, unsigned int Col> |
61 |
class RectMatrix { |
62 |
public: |
63 |
|
64 |
/** default constructor */ |
65 |
RectMatrix() { |
66 |
for (unsigned int i = 0; i < Row; i++) |
67 |
for (unsigned int j = 0; j < Col; j++) |
68 |
data_[i][j] = 0.0; |
69 |
} |
70 |
|
71 |
/** Constructs and initializes every element of this matrix to a scalar */ |
72 |
RectMatrix(Real s) { |
73 |
for (unsigned int i = 0; i < Row; i++) |
74 |
for (unsigned int j = 0; j < Col; j++) |
75 |
data_[i][j] = s; |
76 |
} |
77 |
|
78 |
/** copy constructor */ |
79 |
RectMatrix(const RectMatrix<Real, Row, Col>& m) { |
80 |
*this = m; |
81 |
} |
82 |
|
83 |
/** destructor*/ |
84 |
~RectMatrix() {} |
85 |
|
86 |
/** copy assignment operator */ |
87 |
RectMatrix<Real, Row, Col>& operator =(const RectMatrix<Real, Row, Col>& m) { |
88 |
if (this == &m) |
89 |
return *this; |
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 |
} |
107 |
|
108 |
/** |
109 |
* Return the value of a single element of this matrix. |
110 |
* @return the value of a single element of this matrix |
111 |
* @param i row index |
112 |
* @param j colum index |
113 |
*/ |
114 |
double operator()(unsigned int i, unsigned int j) const { |
115 |
|
116 |
return data_[i][j]; |
117 |
} |
118 |
|
119 |
/** |
120 |
* Returns a row of this matrix as a vector. |
121 |
* @return a row of this matrix as a vector |
122 |
* @param row the row index |
123 |
*/ |
124 |
Vector<Real, Row> getRow(unsigned int row) { |
125 |
Vector<Real, Row> v; |
126 |
|
127 |
for (unsigned int i = 0; i < Row; i++) |
128 |
v[i] = data_[row][i]; |
129 |
|
130 |
return v; |
131 |
} |
132 |
|
133 |
/** |
134 |
* Sets a row of this matrix |
135 |
* @param row the row index |
136 |
* @param v the vector to be set |
137 |
*/ |
138 |
void setRow(unsigned int row, const Vector<Real, Row>& v) { |
139 |
|
140 |
for (unsigned int i = 0; i < Row; i++) |
141 |
data_[row][i] = v[i]; |
142 |
} |
143 |
|
144 |
/** |
145 |
* 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; |
151 |
|
152 |
for (unsigned int j = 0; j < Col; j++) |
153 |
v[j] = data_[j][col]; |
154 |
|
155 |
return v; |
156 |
} |
157 |
|
158 |
/** |
159 |
* Sets a column of this matrix |
160 |
* @param col the column index |
161 |
* @param v the vector to be set |
162 |
*/ |
163 |
void setColum(unsigned int col, const Vector<Real, Col>& v){ |
164 |
|
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++) |
178 |
for (unsigned int j = 0; j < Col; j++) |
179 |
if (!equal(data_[i][j], m.data_[i][j])) |
180 |
return false; |
181 |
|
182 |
return true; |
183 |
} |
184 |
|
185 |
/** |
186 |
* Tests if this matrix is not equal to matrix m |
187 |
* @return true if this matrix is not equal to the matrix m, return false otherwise |
188 |
* @m matrix to be compared |
189 |
*/ |
190 |
bool operator !=(const RectMatrix<Real, Row, Col>& m) { |
191 |
return !(*this == m); |
192 |
} |
193 |
|
194 |
/** Negates the value of this matrix in place. */ |
195 |
inline void negate() { |
196 |
for (unsigned int i = 0; i < Row; i++) |
197 |
for (unsigned int j = 0; j < Col; j++) |
198 |
data_[i][j] = -data_[i][j]; |
199 |
} |
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 |
} |
252 |
|
253 |
/** |
254 |
* Sets the value of this matrix to the scalar multiplication of itself (*this *= s). |
255 |
* @param s the scalar value |
256 |
*/ |
257 |
inline void mul( double s ) { |
258 |
for (unsigned int i = 0; i < Row; i++) |
259 |
for (unsigned int j = 0; j < Col; j++) |
260 |
data_[i][j] *= s; |
261 |
} |
262 |
|
263 |
/** |
264 |
* Sets the value of this matrix to the scalar multiplication of matrix m (*this = s * m). |
265 |
* @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 |
} |
273 |
|
274 |
/** |
275 |
* Sets the value of this matrix to the scalar division of itself (*this /= s ). |
276 |
* @param s the scalar value |
277 |
*/ |
278 |
inline void div( double s) { |
279 |
for (unsigned int i = 0; i < Row; i++) |
280 |
for (unsigned int j = 0; j < Col; j++) |
281 |
data_[i][j] /= s; |
282 |
} |
283 |
|
284 |
/** |
285 |
* Sets the value of this matrix to the scalar division of matrix m (*this = m /s). |
286 |
* @param s the scalar value |
287 |
* @param m the matrix |
288 |
*/ |
289 |
inline void div( double s, const RectMatrix<Real, Row, Col>& m ) { |
290 |
for (unsigned int i = 0; i < Row; i++) |
291 |
for (unsigned int j = 0; j < Col; j++) |
292 |
data_[i][j] = m.data_[i][j] / s; |
293 |
} |
294 |
|
295 |
/** |
296 |
* 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 |
} |
303 |
|
304 |
/** |
305 |
* Divides every element of this matrix by a scalar. |
306 |
* @param s the scalar value |
307 |
*/ |
308 |
RectMatrix<Real, Row, Col>& operator /=(const double s) { |
309 |
this->div(s); |
310 |
return *this; |
311 |
} |
312 |
|
313 |
/** |
314 |
* Sets the value of this matrix to the sum of the other matrix and itself (*this += m). |
315 |
* @param m the other matrix |
316 |
*/ |
317 |
RectMatrix<Real, Row, Col>& operator += (const RectMatrix<Real, Row, Col>& m) { |
318 |
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; |
334 |
|
335 |
for (unsigned int i = 0; i < Row; i++) |
336 |
for (unsigned int j = 0; j < Col; j++) |
337 |
result(j, i) = data_[i][j]; |
338 |
|
339 |
return result; |
340 |
} |
341 |
|
342 |
protected: |
343 |
Real data_[Row][Col]; |
344 |
}; |
345 |
|
346 |
/** Negate the value of every element of this matrix. */ |
347 |
template<typename Real, unsigned int Row, unsigned int Col> |
348 |
inline RectMatrix<Real, Row, Col> operator -(const RectMatrix<Real, Row, Col>& m) { |
349 |
RectMatrix<Real, Row, Col> result(m); |
350 |
|
351 |
result.negate(); |
352 |
|
353 |
return result; |
354 |
} |
355 |
|
356 |
/** |
357 |
* Return the sum of two matrixes (m1 + m2). |
358 |
* @return the sum of two matrixes |
359 |
* @param m1 the first matrix |
360 |
* @param m2 the second matrix |
361 |
*/ |
362 |
template<typename Real, unsigned int Row, unsigned int Col> |
363 |
inline RectMatrix<Real, Row, Col> operator + (const RectMatrix<Real, Row, Col>& m1,const RectMatrix<Real, Row, Col>& m2) { |
364 |
RectMatrix<Real, Row, Col> result; |
365 |
|
366 |
result.add(m1, m2); |
367 |
|
368 |
return result; |
369 |
} |
370 |
|
371 |
/** |
372 |
* Return the difference of two matrixes (m1 - m2). |
373 |
* @return the sum of two matrixes |
374 |
* @param m1 the first matrix |
375 |
* @param m2 the second matrix |
376 |
*/ |
377 |
template<typename Real, unsigned int Row, unsigned int Col> |
378 |
inline RectMatrix<Real, Row, Col> operator - (const RectMatrix<Real, Row, Col>& m1, const RectMatrix<Real, Row, Col>& m2) { |
379 |
RectMatrix<Real, Row, Col> result; |
380 |
|
381 |
result.sub(m1, m2); |
382 |
|
383 |
return result; |
384 |
} |
385 |
|
386 |
/** |
387 |
* Return the multiplication of scalra and matrix (m * s). |
388 |
* @return the multiplication of a scalra and a matrix |
389 |
* @param m the matrix |
390 |
* @param s the scalar |
391 |
*/ |
392 |
template<typename Real, unsigned int Row, unsigned int Col> |
393 |
inline RectMatrix<Real, Row, Col> operator *(const RectMatrix<Real, Row, Col>& m, Real s) { |
394 |
RectMatrix<Real, Row, Col> result; |
395 |
|
396 |
result.mul(s, m); |
397 |
|
398 |
return result; |
399 |
} |
400 |
|
401 |
/** |
402 |
* Return the multiplication of a scalra and a matrix (s * m). |
403 |
* @return the multiplication of a scalra and a matrix |
404 |
* @param s the scalar |
405 |
* @param m the matrix |
406 |
*/ |
407 |
template<typename Real, unsigned int Row, unsigned int Col> |
408 |
inline RectMatrix<Real, Row, Col> operator *(Real s, const RectMatrix<Real, Row, Col>& m) { |
409 |
RectMatrix<Real, Row, Col> result; |
410 |
|
411 |
result.mul(s, m); |
412 |
|
413 |
return result; |
414 |
} |
415 |
|
416 |
/** |
417 |
* Return the multiplication of two matrixes (m1 * m2). |
418 |
* @return the multiplication of two matrixes |
419 |
* @param m1 the first matrix |
420 |
* @param m2 the second matrix |
421 |
*/ |
422 |
template<typename Real, unsigned int Row, unsigned int Col, unsigned int SameDim> |
423 |
inline RectMatrix<Real, Row, Col> operator *(const RectMatrix<Real, Row, SameDim>& m1, const RectMatrix<Real, SameDim, Col>& m2) { |
424 |
RectMatrix<Real, Row, Col> result; |
425 |
|
426 |
for (unsigned int i = 0; i < Row; i++) |
427 |
for (unsigned int j = 0; j < Col; j++) |
428 |
for (unsigned int k = 0; k < SameDim; k++) |
429 |
result(i, j) = m1(i, k) * m2(k, j); |
430 |
|
431 |
return result; |
432 |
} |
433 |
|
434 |
/** |
435 |
* Return the multiplication of a matrix and a vector (m * v). |
436 |
* @return the multiplication of a matrix and a vector |
437 |
* @param m the matrix |
438 |
* @param v the vector |
439 |
*/ |
440 |
template<typename Real, unsigned int Row, unsigned int Col> |
441 |
inline Vector<Real, Row> operator *(const RectMatrix<Real, Row, Col>& m, const Vector<Real, Col>& v) { |
442 |
Vector<Real, Row> result; |
443 |
|
444 |
for (unsigned int i = 0; i < Row ; i++) |
445 |
for (unsigned int j = 0; j < Col ; j++) |
446 |
result[i] += m(i, j) * v[j]; |
447 |
|
448 |
return result; |
449 |
} |
450 |
|
451 |
/** |
452 |
* Return the scalar division of matrix (m / s). |
453 |
* @return the scalar division of matrix |
454 |
* @param m the matrix |
455 |
* @param s the scalar |
456 |
*/ |
457 |
template<typename Real, unsigned int Row, unsigned int Col> |
458 |
inline RectMatrix<Real, Row, Col> operator /(const RectMatrix<Real, Row, Col>& m, Real s) { |
459 |
RectMatrix<Real, Row, Col> result; |
460 |
|
461 |
result.div(s, m); |
462 |
|
463 |
return result; |
464 |
} |
465 |
} |
466 |
#endif //MATH_RECTMATRIX_HPP |