ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/branches/new_design/OOPSE-2.0/src/math/RectMatrix.hpp
Revision: 1816
Committed: Wed Dec 1 19:10:51 2004 UTC (19 years, 7 months ago) by tim
File size: 18506 byte(s)
Log Message:
spell check

File Contents

# Content
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 <cmath>
38 #include "Vector.hpp"
39
40 namespace oopse {
41
42 /**
43 * @class RectMatrix RectMatrix.hpp "math/RectMatrix.hpp"
44 * @brief rectangular matrix class
45 */
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 /** 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 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() {}
79
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 /**
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 Column 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 Column index
107 */
108 Real operator()(unsigned int i, unsigned int j) const {
109
110 return data_[i][j];
111 }
112
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
125
126 /** Returns the pointer of internal array */
127 Real* getArrayPointer() {
128 return &data_[0][0];
129 }
130
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 v[i] = data_[row][i];
141
142 return v;
143 }
144
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 for (unsigned int i = 0; i < Row; i++)
153 data_[row][i] = v[i];
154 }
155
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> getColumn(unsigned int col) {
162 Vector<Real, Col> v;
163
164 for (unsigned int j = 0; j < Col; j++)
165 v[j] = data_[j][col];
166
167 return v;
168 }
169
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 setColumn(unsigned int col, const Vector<Real, Col>& v){
176
177 for (unsigned int j = 0; j < Col; j++)
178 data_[j][col] = v[j];
179 }
180
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 for (unsigned int k = 0; k < Col; k++)
190 std::swap(data_[i][k], data_[j][k]);
191 }
192
193 /**
194 * swap two Columns of this matrix
195 * @param i the first Column
196 * @param j the second Column
197 */
198 void swapColumn(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 * 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 return true;
219 }
220
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 /** 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 /**
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 /**
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() const{
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];
380 };
381
382 /** Negate the value of every element of this matrix. */
383 template<typename Real, unsigned int Row, unsigned int Col>
384 inline RectMatrix<Real, Row, Col> operator -(const RectMatrix<Real, Row, Col>& m) {
385 RectMatrix<Real, Row, Col> result(m);
386
387 result.negate();
388
389 return result;
390 }
391
392 /**
393 * Return the sum of two matrixes (m1 + m2).
394 * @return the sum of two matrixes
395 * @param m1 the first matrix
396 * @param m2 the second matrix
397 */
398 template<typename Real, unsigned int Row, unsigned int Col>
399 inline RectMatrix<Real, Row, Col> operator + (const RectMatrix<Real, Row, Col>& m1,const RectMatrix<Real, Row, Col>& m2) {
400 RectMatrix<Real, Row, Col> result;
401
402 result.add(m1, m2);
403
404 return result;
405 }
406
407 /**
408 * Return the difference of two matrixes (m1 - m2).
409 * @return the sum of two matrixes
410 * @param m1 the first matrix
411 * @param m2 the second matrix
412 */
413 template<typename Real, unsigned int Row, unsigned int Col>
414 inline RectMatrix<Real, Row, Col> operator - (const RectMatrix<Real, Row, Col>& m1, const RectMatrix<Real, Row, Col>& m2) {
415 RectMatrix<Real, Row, Col> result;
416
417 result.sub(m1, m2);
418
419 return result;
420 }
421
422 /**
423 * Return the multiplication of scalra and matrix (m * s).
424 * @return the multiplication of a scalra and a matrix
425 * @param m the matrix
426 * @param s the scalar
427 */
428 template<typename Real, unsigned int Row, unsigned int Col>
429 inline RectMatrix<Real, Row, Col> operator *(const RectMatrix<Real, Row, Col>& m, Real s) {
430 RectMatrix<Real, Row, Col> result;
431
432 result.mul(s, m);
433
434 return result;
435 }
436
437 /**
438 * Return the multiplication of a scalra and a matrix (s * m).
439 * @return the multiplication of a scalra and a matrix
440 * @param s the scalar
441 * @param m the matrix
442 */
443 template<typename Real, unsigned int Row, unsigned int Col>
444 inline RectMatrix<Real, Row, Col> operator *(Real s, const RectMatrix<Real, Row, Col>& m) {
445 RectMatrix<Real, Row, Col> result;
446
447 result.mul(s, m);
448
449 return result;
450 }
451
452 /**
453 * Return the multiplication of two matrixes (m1 * m2).
454 * @return the multiplication of two matrixes
455 * @param m1 the first matrix
456 * @param m2 the second matrix
457 */
458 template<typename Real, unsigned int Row, unsigned int Col, unsigned int SameDim>
459 inline RectMatrix<Real, Row, Col> operator *(const RectMatrix<Real, Row, SameDim>& m1, const RectMatrix<Real, SameDim, Col>& m2) {
460 RectMatrix<Real, Row, Col> result;
461
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);
466
467 return result;
468 }
469
470 /**
471 * Return the multiplication of a matrix and a vector (m * v).
472 * @return the multiplication of a matrix and a vector
473 * @param m the matrix
474 * @param v the vector
475 */
476 template<typename Real, unsigned int Row, unsigned int Col>
477 inline Vector<Real, Row> operator *(const RectMatrix<Real, Row, Col>& m, const Vector<Real, Col>& v) {
478 Vector<Real, Row> result;
479
480 for (unsigned int i = 0; i < Row ; i++)
481 for (unsigned int j = 0; j < Col ; j++)
482 result[i] += m(i, j) * v[j];
483
484 return result;
485 }
486
487 /**
488 * Return the scalar division of matrix (m / s).
489 * @return the scalar division of matrix
490 * @param m the matrix
491 * @param s the scalar
492 */
493 template<typename Real, unsigned int Row, unsigned int Col>
494 inline RectMatrix<Real, Row, Col> operator /(const RectMatrix<Real, Row, Col>& m, Real s) {
495 RectMatrix<Real, Row, Col> result;
496
497 result.div(s, m);
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

Properties

Name Value
svn:executable *