ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-2.0/src/math/RectMatrix.hpp
(Generate patch)

Comparing trunk/OOPSE-2.0/src/math/RectMatrix.hpp (file contents):
Revision 1569 by tim, Thu Oct 14 23:28:09 2004 UTC vs.
Revision 1630 by tim, Thu Oct 21 21:31:39 2004 UTC

# Line 46 | 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;
71 <        }
66 >            /** copy constructor */
67 >            RectMatrix(const RectMatrix<Real, Row, Col>& m) {
68 >                *this = m;
69 >            }
70 >            
71 >            /** destructor*/
72 >            ~RectMatrix() {}
73  
74 <        /** copy constructor */
75 <        RectMatrix(const RectMatrix<Real, Row, Col>& m) {
76 <            *this = m;
77 <        }
78 <        
79 <        /** destructor*/
80 <        ~RectMatrix() {}
81 <
72 <        /** copy assignment operator */
73 <        RectMatrix<Real, Row, Col>& operator =(const RectMatrix<Real, Row, Col>& m) {
74 <            if (this == &m)
74 >            /** 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                  return *this;
83 +            }
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
87 <         * @param j colum index
88 <         */
89 <        double& operator()(unsigned int i, unsigned int j) {
90 <            //assert( i < Row && j < Col);
91 <            return data_[i][j];
92 <        }
85 >            /**
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  
96 <        /**
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 <        double operator()(unsigned int i, unsigned int j) const  {
103 <            
104 <            return data_[i][j];  
105 <        }
96 >            /**
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  
107 <        /**
108 <         * Returns a row of  this matrix as a vector.
109 <         * @return a row of  this matrix as a vector
110 <         * @param row the row index
109 <         */                
110 <        Vector<Real, Row> getRow(unsigned int row) {
111 <            Vector<Real, Row> v;
107 >            /** Returns the pointer of internal array */
108 >            Real* getArrayPointer() {
109 >                return &data_[0][0];
110 >            }
111  
112 <            for (unsigned int i = 0; i < Row; i++)
113 <                v[i] = data_[row][i];
112 >            /**
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  
120 <            return v;
121 <        }
120 >                for (unsigned int i = 0; i < Row; i++)
121 >                    v[i] = data_[row][i];
122  
123 <        /**
124 <         * Sets a row of  this matrix
121 <         * @param row the row index
122 <         * @param v the vector to be set
123 <         */                
124 <         void setRow(unsigned int row, const Vector<Real, Row>& v) {
123 >                return v;
124 >            }
125  
126 <            for (unsigned int i = 0; i < Row; i++)
127 <                data_[row][i] = v[i];
128 <         }
126 >            /**
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  
133 <        /**
134 <         * Returns a column of  this matrix as a vector.
135 <         * @return a column of  this matrix as a vector
133 <         * @param col the column index
134 <         */                
135 <        Vector<Real, Col> getColum(unsigned int col) {
136 <            Vector<Real, Col> v;
133 >                for (unsigned int i = 0; i < Row; i++)
134 >                    data_[row][i] = v[i];
135 >             }
136  
137 <            for (unsigned int j = 0; j < Col; j++)
138 <                v[j] = data_[j][col];
137 >            /**
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  
145 <            return v;
146 <        }
145 >                for (unsigned int j = 0; j < Col; j++)
146 >                    v[j] = data_[j][col];
147  
148 <        /**
149 <         * Sets a column of  this matrix
146 <         * @param col the column index
147 <         * @param v the vector to be set
148 <         */                
149 <         void setColum(unsigned int col, const Vector<Real, Col>& v){
148 >                return v;
149 >            }
150  
151 <            for (unsigned int j = 0; j < Col; j++)
152 <                data_[j][col] = v[j];
153 <         }        
151 >            /**
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  
155        /**
156         * Tests if this matrix is identical to matrix m
157         * @return true if this matrix is equal to the matrix m, return false otherwise
158         * @m matrix to be compared
159         *
160         * @todo replace operator == by template function equal
161         */
162        bool operator ==(const RectMatrix<Real, Row, Col>& m) {
163            for (unsigned int i = 0; i < Row; i++)
158                  for (unsigned int j = 0; j < Col; j++)
159 <                    if (!equal(data_[i][j], m.data_[i][j]))
160 <                        return false;
159 >                    data_[j][col] = v[j];
160 >             }        
161  
162 <            return true;
163 <        }
162 >            /**
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  
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
174 <         * @m matrix to be compared
175 <         */
176 <        bool operator !=(const RectMatrix<Real, Row, Col>& m) {
177 <            return !(*this == m);
178 <        }
170 >                    for (unsigned int k = 0; k < Col; k++)
171 >                        std::swap(data_[i][k], data_[j][k]);
172 >            }
173  
174 <        /** Negates the value of this matrix in place. */          
175 <        inline void negate() {
176 <            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 <        }
180 <        
181 <        /**
182 <        * Sets the value of this matrix to the negation of matrix m.
183 <        * @param m the source matrix
184 <        */
191 <        inline void negate(const RectMatrix<Real, Row, Col>& m) {
192 <            for (unsigned int i = 0; i < Row; i++)
193 <                for (unsigned int j = 0; j < Col; j++)
194 <                    data_[i][j] = -m.data_[i][j];        
195 <        }
196 <        
197 <        /**
198 <        * Sets the value of this matrix to the sum of itself and m (*this += m).
199 <        * @param m the other matrix
200 <        */
201 <        inline void add( const RectMatrix<Real, Row, Col>& m ) {
202 <            for (unsigned int i = 0; i < Row; i++)
203 <                for (unsigned int j = 0; j < Col; j++)        
204 <                data_[i][j] += m.data_[i][j];
205 <        }
206 <        
207 <        /**
208 <        * Sets the value of this matrix to the sum of m1 and m2 (*this = m1 + m2).
209 <        * @param m1 the first matrix
210 <        * @param m2 the second matrix
211 <        */
212 <        inline void add( const RectMatrix<Real, Row, Col>& m1, const RectMatrix<Real, Row, Col>& m2 ) {
213 <            for (unsigned int i = 0; i < Row; i++)
214 <                for (unsigned int j = 0; j < Col; j++)        
215 <                data_[i][j] = m1.data_[i][j] + m2.data_[i][j];
216 <        }
217 <        
218 <        /**
219 <        * Sets the value of this matrix to the difference  of itself and m (*this -= m).
220 <        * @param m the other matrix
221 <        */
222 <        inline void sub( 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 difference of matrix m1 and m2 (*this = m1 - m2).
230 <        * @param m1 the first matrix
231 <        * @param m2 the second matrix
232 <        */
233 <        inline void sub( const RectMatrix<Real, Row, Col>& m1, const RectMatrix<Real, Row, Col>& m2){
234 <            for (unsigned int i = 0; i < Row; i++)
235 <                for (unsigned int j = 0; j < Col; j++)        
236 <                    data_[i][j] = m1.data_[i][j] - m2.data_[i][j];
237 <        }
174 >           /**
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  
186 <        /**
187 <        * Sets the value of this matrix to the scalar multiplication of itself (*this *= s).
188 <        * @param s the scalar value
189 <        */
190 <        inline void mul( double s ) {
191 <            for (unsigned int i = 0; i < Row; i++)
192 <                for (unsigned int j = 0; j < Col; j++)  
193 <                    data_[i][j] *= s;
194 <        }
186 >            /**
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  
199 <        /**
200 <        * Sets the value of this matrix to the scalar multiplication of matrix m  (*this = s * m).
251 <        * @param s the scalar value
252 <        * @param m the matrix
253 <        */
254 <        inline void mul( double s, const RectMatrix<Real, Row, Col>& m ) {
255 <            for (unsigned int i = 0; i < Row; i++)
256 <                for (unsigned int j = 0; j < Col; j++)  
257 <                    data_[i][j] = s * m.data_[i][j];
258 <        }
199 >                return true;
200 >            }
201  
202 <        /**
203 <        * Sets the value of this matrix to the scalar division of itself  (*this /= s ).
204 <        * @param s the scalar value
205 <        */            
206 <        inline void div( double s) {
207 <            for (unsigned int i = 0; i < Row; i++)
208 <                for (unsigned int j = 0; j < Col; j++)  
209 <                    data_[i][j] /= s;
268 <        }
202 >            /**
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  
211 <        /**
212 <        * Sets the value of this matrix to the scalar division of matrix m  (*this = m /s).
213 <        * @param s the scalar value
214 <        * @param m the matrix
215 <        */
216 <        inline void div( double s, const RectMatrix<Real, Row, Col>& m ) {
276 <            for (unsigned int i = 0; i < Row; i++)
277 <                for (unsigned int j = 0; j < Col; j++)  
278 <                    data_[i][j] = m.data_[i][j] / s;
279 <        }
280 <
281 <        /**
282 <         *  Multiples a scalar into every element of this matrix.
283 <         * @param s the scalar value
284 <         */
285 <        RectMatrix<Real, Row, Col>& operator *=(const double s) {
286 <            this->mul(s);
287 <            return *this;
288 <        }
289 <
290 <        /**
291 <         *  Divides every element of this matrix by a scalar.
292 <         * @param s the scalar value
293 <         */
294 <        RectMatrix<Real, Row, Col>& operator /=(const double s) {
295 <            this->div(s);
296 <            return *this;
297 <        }
298 <
299 <        /**
300 <         * Sets the value of this matrix to the sum of the other matrix and itself (*this += m).
301 <         * @param m the other matrix
302 <         */
303 <        RectMatrix<Real, Row, Col>& operator += (const RectMatrix<Real, Row, Col>& m) {
304 <            add(m);
305 <            return *this;
306 <         }
307 <
308 <        /**
309 <         * Sets the value of this matrix to the differerence of itself and the other matrix (*this -= m)
310 <         * @param m the other matrix
311 <         */
312 <        RectMatrix<Real, Row, Col>& operator -= (const RectMatrix<Real, Row, Col>& m){
313 <            sub(m);
314 <            return *this;
315 <        }
316 <
317 <        /** Return the transpose of this matrix */
318 <        RectMatrix<Real,  Col, Row> transpose(){
319 <            RectMatrix<Real,  Col, Row> result;
211 >            /** 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 <            for (unsigned int i = 0; i < Row; i++)
219 <                for (unsigned int j = 0; j < Col; j++)              
220 <                    result(j, i) = data_[i][j];
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  
270 <            return result;
271 <        }
270 >            /**
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 >
280 >            /**
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 >
291 >            /**
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 >
301 >            /**
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 >
312 >            /**
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 >
321 >            /**
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 >
330 >            /**
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 >
339 >            /**
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 >
348 >            /** 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          
359          protected:
360              Real data_[Row][Col];
# Line 448 | Line 479 | namespace oopse {
479  
480          return result;
481      }    
482 +
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 +            o << "(";
490 +            for (unsigned int j = 0; j < Col ; j++) {
491 +                o << m(i, j);
492 +                if (j != Col -1)
493 +                    o << "\t";
494 +            }
495 +            o << ")" << std::endl;
496 +        }
497 +        return o;        
498 +    }    
499   }
500   #endif //MATH_RECTMATRIX_HPP

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines