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 1603 by tim, Tue Oct 19 21:28:55 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  
164 <            for (unsigned int j = 0; j < Col; j++)
165 <                data_[j][col] = v[j];
153 <         }        
164 >                for (unsigned int j = 0; j < Col; j++)
165 >                    v[j] = data_[j][col];
166  
167 <        /**
168 <         * swap two rows of this matrix
157 <         * @param i the first row
158 <         * @param j the second row
159 <         */
160 <        void swapRow(unsigned int i, unsigned int j){
161 <                assert(i < Row && j < Row);
167 >                return v;
168 >            }
169  
170 <                for (unsigned int k = 0; k < Col; k++)
171 <                    std::swap(data_[i][k], data_[j][k]);
172 <        }
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  
167       /**
168         * swap two colums of this matrix
169         * @param i the first colum
170         * @param j the second colum
171         */
172        void swapColum(unsigned int i, unsigned int j){
173                assert(i < Col && j < Col);
174                
175                for (unsigned int k = 0; k < Row; k++)
176                    std::swap(data_[k][i], data_[k][j]);
177        }
178
179        /**
180         * Tests if this matrix is identical to matrix m
181         * @return true if this matrix is equal to the matrix m, return false otherwise
182         * @m matrix to be compared
183         *
184         * @todo replace operator == by template function equal
185         */
186        bool operator ==(const RectMatrix<Real, Row, Col>& m) {
187            for (unsigned int i = 0; i < Row; i++)
177                  for (unsigned int j = 0; j < Col; j++)
178 <                    if (!equal(data_[i][j], m.data_[i][j]))
179 <                        return false;
178 >                    data_[j][col] = v[j];
179 >             }        
180  
181 <            return true;
182 <        }
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 <         * Tests if this matrix is not equal to matrix m
191 <         * @return true if this matrix is not equal to the matrix m, return false otherwise
198 <         * @m matrix to be compared
199 <         */
200 <        bool operator !=(const RectMatrix<Real, Row, Col>& m) {
201 <            return !(*this == m);
202 <        }
189 >                    for (unsigned int k = 0; k < Col; k++)
190 >                        std::swap(data_[i][k], data_[j][k]);
191 >            }
192  
193 <        /** Negates the value of this matrix in place. */          
194 <        inline void negate() {
195 <            for (unsigned int i = 0; i < Row; i++)
196 <                for (unsigned int j = 0; j < Col; j++)
197 <                    data_[i][j] = -data_[i][j];
198 <        }
199 <        
200 <        /**
201 <        * Sets the value of this matrix to the negation of matrix m.
202 <        * @param m the source matrix
203 <        */
215 <        inline void negate(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 itself and m (*this += m).
223 <        * @param m the other matrix
224 <        */
225 <        inline void add( const RectMatrix<Real, Row, Col>& m ) {
226 <            for (unsigned int i = 0; i < Row; i++)
227 <                for (unsigned int j = 0; j < Col; j++)        
228 <                data_[i][j] += m.data_[i][j];
229 <        }
230 <        
231 <        /**
232 <        * Sets the value of this matrix to the sum of m1 and m2 (*this = m1 + m2).
233 <        * @param m1 the first matrix
234 <        * @param m2 the second matrix
235 <        */
236 <        inline void add( const RectMatrix<Real, Row, Col>& m1, const RectMatrix<Real, Row, Col>& m2 ) {
237 <            for (unsigned int i = 0; i < Row; i++)
238 <                for (unsigned int j = 0; j < Col; j++)        
239 <                data_[i][j] = m1.data_[i][j] + m2.data_[i][j];
240 <        }
241 <        
242 <        /**
243 <        * Sets the value of this matrix to the difference  of itself and m (*this -= m).
244 <        * @param m the other matrix
245 <        */
246 <        inline void sub( const RectMatrix<Real, Row, Col>& m ) {
247 <            for (unsigned int i = 0; i < Row; i++)
248 <                for (unsigned int j = 0; j < Col; j++)        
249 <                    data_[i][j] -= m.data_[i][j];
250 <        }
251 <        
252 <        /**
253 <        * Sets the value of this matrix to the difference of matrix m1 and m2 (*this = m1 - m2).
254 <        * @param m1 the first matrix
255 <        * @param m2 the second matrix
256 <        */
257 <        inline void sub( const RectMatrix<Real, Row, Col>& m1, const RectMatrix<Real, Row, Col>& m2){
258 <            for (unsigned int i = 0; i < Row; i++)
259 <                for (unsigned int j = 0; j < Col; j++)        
260 <                    data_[i][j] = m1.data_[i][j] - m2.data_[i][j];
261 <        }
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 multiplication of itself (*this *= s).
207 <        * @param s the scalar value
208 <        */
209 <        inline void mul( double s ) {
210 <            for (unsigned int i = 0; i < Row; i++)
211 <                for (unsigned int j = 0; j < Col; j++)  
212 <                    data_[i][j] *= s;
213 <        }
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 <        * Sets the value of this matrix to the scalar multiplication of matrix m  (*this = s * m).
275 <        * @param s the scalar value
276 <        * @param m the matrix
277 <        */
278 <        inline void mul( double s, const RectMatrix<Real, Row, Col>& m ) {
279 <            for (unsigned int i = 0; i < Row; i++)
280 <                for (unsigned int j = 0; j < Col; j++)  
281 <                    data_[i][j] = s * m.data_[i][j];
282 <        }
218 >                return true;
219 >            }
220  
221 <        /**
222 <        * Sets the value of this matrix to the scalar division of itself  (*this /= s ).
223 <        * @param s the scalar value
224 <        */            
225 <        inline void div( double s) {
226 <            for (unsigned int i = 0; i < Row; i++)
227 <                for (unsigned int j = 0; j < Col; j++)  
228 <                    data_[i][j] /= s;
292 <        }
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 scalar division of matrix m  (*this = m /s).
232 <        * @param s the scalar value
233 <        * @param m the matrix
234 <        */
235 <        inline void div( double s, const RectMatrix<Real, Row, Col>& m ) {
300 <            for (unsigned int i = 0; i < Row; i++)
301 <                for (unsigned int j = 0; j < Col; j++)  
302 <                    data_[i][j] = m.data_[i][j] / s;
303 <        }
304 <
305 <        /**
306 <         *  Multiples a scalar into every element of this matrix.
307 <         * @param s the scalar value
308 <         */
309 <        RectMatrix<Real, Row, Col>& operator *=(const double s) {
310 <            this->mul(s);
311 <            return *this;
312 <        }
313 <
314 <        /**
315 <         *  Divides every element of this matrix by a scalar.
316 <         * @param s the scalar value
317 <         */
318 <        RectMatrix<Real, Row, Col>& operator /=(const double s) {
319 <            this->div(s);
320 <            return *this;
321 <        }
322 <
323 <        /**
324 <         * Sets the value of this matrix to the sum of the other matrix and itself (*this += m).
325 <         * @param m the other matrix
326 <         */
327 <        RectMatrix<Real, Row, Col>& operator += (const RectMatrix<Real, Row, Col>& m) {
328 <            add(m);
329 <            return *this;
330 <         }
331 <
332 <        /**
333 <         * Sets the value of this matrix to the differerence of itself and the other matrix (*this -= m)
334 <         * @param m the other matrix
335 <         */
336 <        RectMatrix<Real, Row, Col>& operator -= (const RectMatrix<Real, Row, Col>& m){
337 <            sub(m);
338 <            return *this;
339 <        }
340 <
341 <        /** Return the transpose of this matrix */
342 <        RectMatrix<Real,  Col, Row> transpose(){
343 <            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 481 | Line 507 | namespace oopse {
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) << "\t";
510 >                o << m(i, j);
511 >                if (j != Col -1)
512 >                    o << "\t";
513              }
514              o << ")" << std::endl;
515          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines