ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-2.0/src/math/RectMatrix.hpp
Revision: 1630
Committed: Thu Oct 21 21:31:39 2004 UTC (19 years, 8 months ago) by tim
File size: 17850 byte(s)
Log Message:
Snapshot and SnapshotManager in progress

File Contents

# User Rev Content
1 tim 1564 /*
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 tim 1567 #include <cmath>
38 tim 1564 #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 tim 1630 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 tim 1564
59 tim 1630 /** 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 tim 1564
66 tim 1630 /** copy constructor */
67     RectMatrix(const RectMatrix<Real, Row, Col>& m) {
68     *this = m;
69     }
70    
71     /** destructor*/
72     ~RectMatrix() {}
73 tim 1564
74 tim 1630 /** copy assignment operator */
75     RectMatrix<Real, Row, Col>& operator =(const RectMatrix<Real, Row, Col>& m) {
76     if (this == &m)
77     return *this;
78    
79     for (unsigned int i = 0; i < Row; i++)
80     for (unsigned int j = 0; j < Col; j++)
81     data_[i][j] = m.data_[i][j];
82 tim 1564 return *this;
83 tim 1630 }
84 tim 1564
85 tim 1630 /**
86     * Return the reference of a single element of this matrix.
87     * @return the reference of a single element of this matrix
88     * @param i row index
89     * @param j colum index
90     */
91     Real& operator()(unsigned int i, unsigned int j) {
92     //assert( i < Row && j < Col);
93     return data_[i][j];
94     }
95 tim 1564
96 tim 1630 /**
97     * Return the value of a single element of this matrix.
98     * @return the value of a single element of this matrix
99     * @param i row index
100     * @param j colum index
101     */
102     Real operator()(unsigned int i, unsigned int j) const {
103    
104     return data_[i][j];
105     }
106 tim 1564
107 tim 1630 /** Returns the pointer of internal array */
108     Real* getArrayPointer() {
109     return &data_[0][0];
110     }
111 tim 1564
112 tim 1630 /**
113     * Returns a row of this matrix as a vector.
114     * @return a row of this matrix as a vector
115     * @param row the row index
116     */
117     Vector<Real, Row> getRow(unsigned int row) {
118     Vector<Real, Row> v;
119 tim 1564
120 tim 1630 for (unsigned int i = 0; i < Row; i++)
121     v[i] = data_[row][i];
122 tim 1564
123 tim 1630 return v;
124     }
125 tim 1564
126 tim 1630 /**
127     * Sets a row of this matrix
128     * @param row the row index
129     * @param v the vector to be set
130     */
131     void setRow(unsigned int row, const Vector<Real, Row>& v) {
132 tim 1564
133 tim 1630 for (unsigned int i = 0; i < Row; i++)
134     data_[row][i] = v[i];
135     }
136 tim 1564
137 tim 1630 /**
138     * Returns a column of this matrix as a vector.
139     * @return a column of this matrix as a vector
140     * @param col the column index
141     */
142     Vector<Real, Col> getColum(unsigned int col) {
143     Vector<Real, Col> v;
144 tim 1564
145 tim 1630 for (unsigned int j = 0; j < Col; j++)
146     v[j] = data_[j][col];
147 tim 1564
148 tim 1630 return v;
149     }
150 tim 1564
151 tim 1630 /**
152     * Sets a column of this matrix
153     * @param col the column index
154     * @param v the vector to be set
155     */
156     void setColum(unsigned int col, const Vector<Real, Col>& v){
157 tim 1564
158 tim 1630 for (unsigned int j = 0; j < Col; j++)
159     data_[j][col] = v[j];
160     }
161 tim 1594
162 tim 1630 /**
163     * swap two rows of this matrix
164     * @param i the first row
165     * @param j the second row
166     */
167     void swapRow(unsigned int i, unsigned int j){
168     assert(i < Row && j < Row);
169 tim 1594
170 tim 1630 for (unsigned int k = 0; k < Col; k++)
171     std::swap(data_[i][k], data_[j][k]);
172     }
173 tim 1594
174 tim 1630 /**
175     * swap two colums of this matrix
176     * @param i the first colum
177     * @param j the second colum
178     */
179     void swapColum(unsigned int i, unsigned int j){
180     assert(i < Col && j < Col);
181    
182     for (unsigned int k = 0; k < Row; k++)
183     std::swap(data_[k][i], data_[k][j]);
184     }
185 tim 1564
186 tim 1630 /**
187     * Tests if this matrix is identical to matrix m
188     * @return true if this matrix is equal to the matrix m, return false otherwise
189     * @m matrix to be compared
190     *
191     * @todo replace operator == by template function equal
192     */
193     bool operator ==(const RectMatrix<Real, Row, Col>& m) {
194     for (unsigned int i = 0; i < Row; i++)
195     for (unsigned int j = 0; j < Col; j++)
196     if (!equal(data_[i][j], m.data_[i][j]))
197     return false;
198 tim 1564
199 tim 1630 return true;
200     }
201 tim 1564
202 tim 1630 /**
203     * Tests if this matrix is not equal to matrix m
204     * @return true if this matrix is not equal to the matrix m, return false otherwise
205     * @m matrix to be compared
206     */
207     bool operator !=(const RectMatrix<Real, Row, Col>& m) {
208     return !(*this == m);
209     }
210 tim 1564
211 tim 1630 /** Negates the value of this matrix in place. */
212     inline void negate() {
213     for (unsigned int i = 0; i < Row; i++)
214     for (unsigned int j = 0; j < Col; j++)
215     data_[i][j] = -data_[i][j];
216     }
217    
218     /**
219     * Sets the value of this matrix to the negation of matrix m.
220     * @param m the source matrix
221     */
222     inline void negate(const RectMatrix<Real, Row, Col>& m) {
223     for (unsigned int i = 0; i < Row; i++)
224     for (unsigned int j = 0; j < Col; j++)
225     data_[i][j] = -m.data_[i][j];
226     }
227    
228     /**
229     * Sets the value of this matrix to the sum of itself and m (*this += m).
230     * @param m the other matrix
231     */
232     inline void add( const RectMatrix<Real, Row, Col>& m ) {
233     for (unsigned int i = 0; i < Row; i++)
234     for (unsigned int j = 0; j < Col; j++)
235     data_[i][j] += m.data_[i][j];
236     }
237    
238     /**
239     * Sets the value of this matrix to the sum of m1 and m2 (*this = m1 + m2).
240     * @param m1 the first matrix
241     * @param m2 the second matrix
242     */
243     inline void add( const RectMatrix<Real, Row, Col>& m1, const RectMatrix<Real, Row, Col>& m2 ) {
244     for (unsigned int i = 0; i < Row; i++)
245     for (unsigned int j = 0; j < Col; j++)
246     data_[i][j] = m1.data_[i][j] + m2.data_[i][j];
247     }
248    
249     /**
250     * Sets the value of this matrix to the difference of itself and m (*this -= m).
251     * @param m the other matrix
252     */
253     inline void sub( const RectMatrix<Real, Row, Col>& m ) {
254     for (unsigned int i = 0; i < Row; i++)
255     for (unsigned int j = 0; j < Col; j++)
256     data_[i][j] -= m.data_[i][j];
257     }
258    
259     /**
260     * Sets the value of this matrix to the difference of matrix m1 and m2 (*this = m1 - m2).
261     * @param m1 the first matrix
262     * @param m2 the second matrix
263     */
264     inline void sub( const RectMatrix<Real, Row, Col>& m1, const RectMatrix<Real, Row, Col>& m2){
265     for (unsigned int i = 0; i < Row; i++)
266     for (unsigned int j = 0; j < Col; j++)
267     data_[i][j] = m1.data_[i][j] - m2.data_[i][j];
268     }
269 tim 1564
270 tim 1630 /**
271     * Sets the value of this matrix to the scalar multiplication of itself (*this *= s).
272     * @param s the scalar value
273     */
274     inline void mul( Real s ) {
275     for (unsigned int i = 0; i < Row; i++)
276     for (unsigned int j = 0; j < Col; j++)
277     data_[i][j] *= s;
278     }
279 tim 1564
280 tim 1630 /**
281     * Sets the value of this matrix to the scalar multiplication of matrix m (*this = s * m).
282     * @param s the scalar value
283     * @param m the matrix
284     */
285     inline void mul( Real s, const RectMatrix<Real, Row, Col>& m ) {
286     for (unsigned int i = 0; i < Row; i++)
287     for (unsigned int j = 0; j < Col; j++)
288     data_[i][j] = s * m.data_[i][j];
289     }
290 tim 1564
291 tim 1630 /**
292     * Sets the value of this matrix to the scalar division of itself (*this /= s ).
293     * @param s the scalar value
294     */
295     inline void div( Real s) {
296     for (unsigned int i = 0; i < Row; i++)
297     for (unsigned int j = 0; j < Col; j++)
298     data_[i][j] /= s;
299     }
300 tim 1564
301 tim 1630 /**
302     * Sets the value of this matrix to the scalar division of matrix m (*this = m /s).
303     * @param s the scalar value
304     * @param m the matrix
305     */
306     inline void div( Real s, const RectMatrix<Real, Row, Col>& m ) {
307     for (unsigned int i = 0; i < Row; i++)
308     for (unsigned int j = 0; j < Col; j++)
309     data_[i][j] = m.data_[i][j] / s;
310     }
311 tim 1564
312 tim 1630 /**
313     * Multiples a scalar into every element of this matrix.
314     * @param s the scalar value
315     */
316     RectMatrix<Real, Row, Col>& operator *=(const Real s) {
317     this->mul(s);
318     return *this;
319     }
320 tim 1564
321 tim 1630 /**
322     * Divides every element of this matrix by a scalar.
323     * @param s the scalar value
324     */
325     RectMatrix<Real, Row, Col>& operator /=(const Real s) {
326     this->div(s);
327     return *this;
328     }
329 tim 1564
330 tim 1630 /**
331     * Sets the value of this matrix to the sum of the other matrix and itself (*this += m).
332     * @param m the other matrix
333     */
334     RectMatrix<Real, Row, Col>& operator += (const RectMatrix<Real, Row, Col>& m) {
335     add(m);
336     return *this;
337     }
338 tim 1564
339 tim 1630 /**
340     * Sets the value of this matrix to the differerence of itself and the other matrix (*this -= m)
341     * @param m the other matrix
342     */
343     RectMatrix<Real, Row, Col>& operator -= (const RectMatrix<Real, Row, Col>& m){
344     sub(m);
345     return *this;
346     }
347 tim 1564
348 tim 1630 /** Return the transpose of this matrix */
349     RectMatrix<Real, Col, Row> transpose(){
350     RectMatrix<Real, Col, Row> result;
351    
352     for (unsigned int i = 0; i < Row; i++)
353     for (unsigned int j = 0; j < Col; j++)
354     result(j, i) = data_[i][j];
355    
356     return result;
357     }
358 tim 1564
359     protected:
360     Real data_[Row][Col];
361     };
362    
363     /** Negate the value of every element of this matrix. */
364     template<typename Real, unsigned int Row, unsigned int Col>
365     inline RectMatrix<Real, Row, Col> operator -(const RectMatrix<Real, Row, Col>& m) {
366     RectMatrix<Real, Row, Col> result(m);
367    
368     result.negate();
369    
370     return result;
371     }
372    
373     /**
374     * Return the sum of two matrixes (m1 + m2).
375     * @return the sum of two matrixes
376     * @param m1 the first matrix
377     * @param m2 the second matrix
378     */
379     template<typename Real, unsigned int Row, unsigned int Col>
380     inline RectMatrix<Real, Row, Col> operator + (const RectMatrix<Real, Row, Col>& m1,const RectMatrix<Real, Row, Col>& m2) {
381     RectMatrix<Real, Row, Col> result;
382    
383     result.add(m1, m2);
384    
385     return result;
386     }
387    
388     /**
389     * Return the difference of two matrixes (m1 - m2).
390     * @return the sum of two matrixes
391     * @param m1 the first matrix
392     * @param m2 the second matrix
393     */
394     template<typename Real, unsigned int Row, unsigned int Col>
395     inline RectMatrix<Real, Row, Col> operator - (const RectMatrix<Real, Row, Col>& m1, const RectMatrix<Real, Row, Col>& m2) {
396     RectMatrix<Real, Row, Col> result;
397    
398     result.sub(m1, m2);
399    
400     return result;
401     }
402    
403     /**
404     * Return the multiplication of scalra and matrix (m * s).
405     * @return the multiplication of a scalra and a matrix
406     * @param m the matrix
407     * @param s the scalar
408     */
409     template<typename Real, unsigned int Row, unsigned int Col>
410     inline RectMatrix<Real, Row, Col> operator *(const RectMatrix<Real, Row, Col>& m, Real s) {
411     RectMatrix<Real, Row, Col> result;
412    
413     result.mul(s, m);
414    
415     return result;
416     }
417    
418     /**
419     * Return the multiplication of a scalra and a matrix (s * m).
420     * @return the multiplication of a scalra and a matrix
421     * @param s the scalar
422     * @param m the matrix
423     */
424     template<typename Real, unsigned int Row, unsigned int Col>
425     inline RectMatrix<Real, Row, Col> operator *(Real s, const RectMatrix<Real, Row, Col>& m) {
426     RectMatrix<Real, Row, Col> result;
427    
428     result.mul(s, m);
429    
430     return result;
431     }
432    
433     /**
434     * Return the multiplication of two matrixes (m1 * m2).
435     * @return the multiplication of two matrixes
436     * @param m1 the first matrix
437     * @param m2 the second matrix
438     */
439     template<typename Real, unsigned int Row, unsigned int Col, unsigned int SameDim>
440     inline RectMatrix<Real, Row, Col> operator *(const RectMatrix<Real, Row, SameDim>& m1, const RectMatrix<Real, SameDim, Col>& m2) {
441     RectMatrix<Real, Row, Col> result;
442    
443     for (unsigned int i = 0; i < Row; i++)
444     for (unsigned int j = 0; j < Col; j++)
445     for (unsigned int k = 0; k < SameDim; k++)
446 tim 1569 result(i, j) += m1(i, k) * m2(k, j);
447 tim 1564
448     return result;
449     }
450    
451     /**
452     * Return the multiplication of a matrix and a vector (m * v).
453     * @return the multiplication of a matrix and a vector
454     * @param m the matrix
455     * @param v the vector
456     */
457     template<typename Real, unsigned int Row, unsigned int Col>
458     inline Vector<Real, Row> operator *(const RectMatrix<Real, Row, Col>& m, const Vector<Real, Col>& v) {
459     Vector<Real, Row> result;
460    
461     for (unsigned int i = 0; i < Row ; i++)
462     for (unsigned int j = 0; j < Col ; j++)
463     result[i] += m(i, j) * v[j];
464    
465     return result;
466     }
467    
468     /**
469     * Return the scalar division of matrix (m / s).
470     * @return the scalar division of matrix
471     * @param m the matrix
472     * @param s the scalar
473     */
474     template<typename Real, unsigned int Row, unsigned int Col>
475     inline RectMatrix<Real, Row, Col> operator /(const RectMatrix<Real, Row, Col>& m, Real s) {
476     RectMatrix<Real, Row, Col> result;
477    
478     result.div(s, m);
479    
480     return result;
481     }
482 tim 1586
483     /**
484     * Write to an output stream
485     */
486     template<typename Real, unsigned int Row, unsigned int Col>
487     std::ostream &operator<< ( std::ostream& o, const RectMatrix<Real, Row, Col>& m) {
488     for (unsigned int i = 0; i < Row ; i++) {
489 tim 1603 o << "(";
490 tim 1586 for (unsigned int j = 0; j < Col ; j++) {
491 tim 1606 o << m(i, j);
492     if (j != Col -1)
493     o << "\t";
494 tim 1586 }
495     o << ")" << std::endl;
496     }
497     return o;
498     }
499 tim 1564 }
500     #endif //MATH_RECTMATRIX_HPP

Properties

Name Value
svn:executable *