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 1644 by tim, Mon Oct 25 22:46:19 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;
62 <        }
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
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 <        }
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  
113            for (unsigned int i = 0; i < Row; i++)
114                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];
128 <         }
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.
132 <         * @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;
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  
151            for (unsigned int j = 0; j < Col; j++)
152                data_[j][col] = v[j];
153         }        
154
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++)
164                  for (unsigned int j = 0; j < Col; j++)
165 <                    if (!equal(data_[i][j], m.data_[i][j]))
166 <                        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) {
177 <            return !(*this == m);
178 <        }
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  
180        /** Negates the value of this matrix in place. */          
181        inline void negate() {
182            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 <        }
186 <        
187 <        /**
188 <        * Sets the value of this matrix to the negation of matrix m.
189 <        * @param m the source matrix
190 <        */
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 <        }
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++)  
246 <                    data_[i][j] *= s;
247 <        }
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
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 <        }
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.
283 <         * @param s the scalar value
284 <         */
285 <        RectMatrix<Real, Row, Col>& operator *=(const double s) {
286 <            this->mul(s);
287 <            return *this;
288 <        }
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);
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;
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 448 | 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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines