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 1606 by tim, Tue Oct 19 23:01:03 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  
158 <        /**
159 <         * swap two rows of this matrix
160 <         * @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);
158 >                for (unsigned int j = 0; j < Col; j++)
159 >                    data_[j][col] = v[j];
160 >             }        
161  
162 <                for (unsigned int k = 0; k < Col; k++)
163 <                    std::swap(data_[i][k], data_[j][k]);
164 <        }
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 <         * swap two colums of this matrix
172 <         * @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 <        }
170 >                    for (unsigned int k = 0; k < Col; k++)
171 >                        std::swap(data_[i][k], data_[j][k]);
172 >            }
173  
174 <        /**
175 <         * Tests if this matrix is identical to matrix m
176 <         * @return true if this matrix is equal to the matrix m, return false otherwise
177 <         * @m matrix to be compared
178 <         *
179 <         * @todo replace operator == by template function equal
180 <         */
181 <        bool operator ==(const RectMatrix<Real, Row, Col>& m) {
182 <            for (unsigned int i = 0; i < Row; i++)
183 <                for (unsigned int j = 0; j < Col; j++)
184 <                    if (!equal(data_[i][j], m.data_[i][j]))
190 <                        return false;
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 <            return true;
187 <        }
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 <         * Tests if this matrix is not equal to matrix m
197 <         * @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 <        }
199 >                return true;
200 >            }
201  
202 <        /** Negates the value of this matrix in place. */          
203 <        inline void negate() {
204 <            for (unsigned int i = 0; i < Row; i++)
205 <                for (unsigned int j = 0; j < Col; j++)
206 <                    data_[i][j] = -data_[i][j];
207 <        }
208 <        
209 <        /**
212 <        * Sets the value of this matrix to the negation of matrix m.
213 <        * @param m the source matrix
214 <        */
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 <        }
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 multiplication of itself (*this *= s).
213 <        * @param s the scalar value
214 <        */
215 <        inline void mul( double s ) {
216 <            for (unsigned int i = 0; i < Row; i++)
269 <                for (unsigned int j = 0; j < Col; j++)  
270 <                    data_[i][j] *= s;
271 <        }
272 <
273 <        /**
274 <        * 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 <        }
283 <
284 <        /**
285 <        * Sets the value of this matrix to the scalar division of itself  (*this /= s ).
286 <        * @param s the scalar value
287 <        */            
288 <        inline void div( double s) {
289 <            for (unsigned int i = 0; i < Row; i++)
290 <                for (unsigned int j = 0; j < Col; j++)  
291 <                    data_[i][j] /= s;
292 <        }
293 <
294 <        /**
295 <        * Sets the value of this matrix to the scalar division of matrix m  (*this = m /s).
296 <        * @param s the scalar value
297 <        * @param m the matrix
298 <        */
299 <        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;
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];

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines