| 1 | /* | 
| 2 | * Copyright (C) 2000-2004  Object Oriented Parallel Simulation Engine (OOPSE) project | 
| 3 | * | 
| 4 | * Contact: oopse@oopse.org | 
| 5 | * | 
| 6 | * This program is free software; you can redistribute it and/or | 
| 7 | * modify it under the terms of the GNU Lesser General Public License | 
| 8 | * as published by the Free Software Foundation; either version 2.1 | 
| 9 | * of the License, or (at your option) any later version. | 
| 10 | * All we ask is that proper credit is given for our work, which includes | 
| 11 | * - but is not limited to - adding the above copyright notice to the beginning | 
| 12 | * of your source code files, and to any copyright notice that you may distribute | 
| 13 | * with programs based on this work. | 
| 14 | * | 
| 15 | * This program is distributed in the hope that it will be useful, | 
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
| 18 | * GNU Lesser General Public License for more details. | 
| 19 | * | 
| 20 | * You should have received a copy of the GNU Lesser General Public License | 
| 21 | * along with this program; if not, write to the Free Software | 
| 22 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. | 
| 23 | * | 
| 24 | */ | 
| 25 |  | 
| 26 |  | 
| 27 | /** | 
| 28 | * @file RectMatrix.hpp | 
| 29 | * @author Teng Lin | 
| 30 | * @date 10/11/2004 | 
| 31 | * @version 1.0 | 
| 32 | */ | 
| 33 |  | 
| 34 | #ifndef MATH_RECTMATRIX_HPP | 
| 35 | #define MATH_RECTMATRIX_HPP | 
| 36 |  | 
| 37 | #include <cmath> | 
| 38 | #include "Vector.hpp" | 
| 39 |  | 
| 40 | namespace oopse { | 
| 41 | const double epsilon = 0.000001; | 
| 42 |  | 
| 43 | template<typename T> | 
| 44 | inline bool equal(T e1, T e2) { | 
| 45 | return e1 == e2; | 
| 46 | } | 
| 47 |  | 
| 48 | template<> | 
| 49 | inline bool equal(float e1, float e2) { | 
| 50 | return fabs(e1 - e2) < epsilon; | 
| 51 | } | 
| 52 |  | 
| 53 | template<> | 
| 54 | inline bool equal(double e1, double e2) { | 
| 55 | return fabs(e1 - e2) < epsilon; | 
| 56 | } | 
| 57 |  | 
| 58 | /** | 
| 59 | * @class RectMatrix RectMatrix.hpp "math/RectMatrix.hpp" | 
| 60 | * @brief rectangular matrix class | 
| 61 | */ | 
| 62 | template<typename Real, unsigned int Row, unsigned int Col> | 
| 63 | class RectMatrix { | 
| 64 | public: | 
| 65 |  | 
| 66 | /** default constructor */ | 
| 67 | RectMatrix() { | 
| 68 | for (unsigned int i = 0; i < Row; i++) | 
| 69 | for (unsigned int j = 0; j < Col; j++) | 
| 70 | data_[i][j] = 0.0; | 
| 71 | } | 
| 72 |  | 
| 73 | /** Constructs and initializes every element of this matrix to a scalar */ | 
| 74 | RectMatrix(Real s) { | 
| 75 | for (unsigned int i = 0; i < Row; i++) | 
| 76 | for (unsigned int j = 0; j < Col; j++) | 
| 77 | data_[i][j] = s; | 
| 78 | } | 
| 79 |  | 
| 80 | /** copy constructor */ | 
| 81 | RectMatrix(const RectMatrix<Real, Row, Col>& m) { | 
| 82 | *this = m; | 
| 83 | } | 
| 84 |  | 
| 85 | /** destructor*/ | 
| 86 | ~RectMatrix() {} | 
| 87 |  | 
| 88 | /** copy assignment operator */ | 
| 89 | RectMatrix<Real, Row, Col>& operator =(const RectMatrix<Real, Row, Col>& m) { | 
| 90 | if (this == &m) | 
| 91 | return *this; | 
| 92 |  | 
| 93 | for (unsigned int i = 0; i < Row; i++) | 
| 94 | for (unsigned int j = 0; j < Col; j++) | 
| 95 | data_[i][j] = m.data_[i][j]; | 
| 96 | return *this; | 
| 97 | } | 
| 98 |  | 
| 99 | /** | 
| 100 | * Return the reference of a single element of this matrix. | 
| 101 | * @return the reference of a single element of this matrix | 
| 102 | * @param i row index | 
| 103 | * @param j colum index | 
| 104 | */ | 
| 105 | double& operator()(unsigned int i, unsigned int j) { | 
| 106 | //assert( i < Row && j < Col); | 
| 107 | return data_[i][j]; | 
| 108 | } | 
| 109 |  | 
| 110 | /** | 
| 111 | * Return the value of a single element of this matrix. | 
| 112 | * @return the value of a single element of this matrix | 
| 113 | * @param i row index | 
| 114 | * @param j colum index | 
| 115 | */ | 
| 116 | double operator()(unsigned int i, unsigned int j) const  { | 
| 117 |  | 
| 118 | return data_[i][j]; | 
| 119 | } | 
| 120 |  | 
| 121 | /** | 
| 122 | * Returns a row of  this matrix as a vector. | 
| 123 | * @return a row of  this matrix as a vector | 
| 124 | * @param row the row index | 
| 125 | */ | 
| 126 | Vector<Real, Row> getRow(unsigned int row) { | 
| 127 | Vector<Real, Row> v; | 
| 128 |  | 
| 129 | for (unsigned int i = 0; i < Row; i++) | 
| 130 | v[i] = data_[row][i]; | 
| 131 |  | 
| 132 | return v; | 
| 133 | } | 
| 134 |  | 
| 135 | /** | 
| 136 | * Sets a row of  this matrix | 
| 137 | * @param row the row index | 
| 138 | * @param v the vector to be set | 
| 139 | */ | 
| 140 | void setRow(unsigned int row, const Vector<Real, Row>& v) { | 
| 141 |  | 
| 142 | for (unsigned int i = 0; i < Row; i++) | 
| 143 | data_[row][i] = v[i]; | 
| 144 | } | 
| 145 |  | 
| 146 | /** | 
| 147 | * Returns a column of  this matrix as a vector. | 
| 148 | * @return a column of  this matrix as a vector | 
| 149 | * @param col the column index | 
| 150 | */ | 
| 151 | Vector<Real, Col> getColum(unsigned int col) { | 
| 152 | Vector<Real, Col> v; | 
| 153 |  | 
| 154 | for (unsigned int j = 0; j < Col; j++) | 
| 155 | v[j] = data_[j][col]; | 
| 156 |  | 
| 157 | return v; | 
| 158 | } | 
| 159 |  | 
| 160 | /** | 
| 161 | * Sets a column of  this matrix | 
| 162 | * @param col the column index | 
| 163 | * @param v the vector to be set | 
| 164 | */ | 
| 165 | void setColum(unsigned int col, const Vector<Real, Col>& v){ | 
| 166 |  | 
| 167 | for (unsigned int j = 0; j < Col; j++) | 
| 168 | data_[j][col] = v[j]; | 
| 169 | } | 
| 170 |  | 
| 171 | /** | 
| 172 | * Tests if this matrix is identical to matrix m | 
| 173 | * @return true if this matrix is equal to the matrix m, return false otherwise | 
| 174 | * @m matrix to be compared | 
| 175 | * | 
| 176 | * @todo replace operator == by template function equal | 
| 177 | */ | 
| 178 | bool operator ==(const RectMatrix<Real, Row, Col>& m) { | 
| 179 | for (unsigned int i = 0; i < Row; i++) | 
| 180 | for (unsigned int j = 0; j < Col; j++) | 
| 181 | if (!equal(data_[i][j], m.data_[i][j])) | 
| 182 | return false; | 
| 183 |  | 
| 184 | return true; | 
| 185 | } | 
| 186 |  | 
| 187 | /** | 
| 188 | * Tests if this matrix is not equal to matrix m | 
| 189 | * @return true if this matrix is not equal to the matrix m, return false otherwise | 
| 190 | * @m matrix to be compared | 
| 191 | */ | 
| 192 | bool operator !=(const RectMatrix<Real, Row, Col>& m) { | 
| 193 | return !(*this == m); | 
| 194 | } | 
| 195 |  | 
| 196 | /** Negates the value of this matrix in place. */ | 
| 197 | inline void negate() { | 
| 198 | for (unsigned int i = 0; i < Row; i++) | 
| 199 | for (unsigned int j = 0; j < Col; j++) | 
| 200 | data_[i][j] = -data_[i][j]; | 
| 201 | } | 
| 202 |  | 
| 203 | /** | 
| 204 | * Sets the value of this matrix to the negation of matrix m. | 
| 205 | * @param m the source matrix | 
| 206 | */ | 
| 207 | inline void negate(const RectMatrix<Real, Row, Col>& m) { | 
| 208 | for (unsigned int i = 0; i < Row; i++) | 
| 209 | for (unsigned int j = 0; j < Col; j++) | 
| 210 | data_[i][j] = -m.data_[i][j]; | 
| 211 | } | 
| 212 |  | 
| 213 | /** | 
| 214 | * Sets the value of this matrix to the sum of itself and m (*this += m). | 
| 215 | * @param m the other matrix | 
| 216 | */ | 
| 217 | inline void add( const RectMatrix<Real, Row, Col>& m ) { | 
| 218 | for (unsigned int i = 0; i < Row; i++) | 
| 219 | for (unsigned int j = 0; j < Col; j++) | 
| 220 | data_[i][j] += m.data_[i][j]; | 
| 221 | } | 
| 222 |  | 
| 223 | /** | 
| 224 | * Sets the value of this matrix to the sum of m1 and m2 (*this = m1 + m2). | 
| 225 | * @param m1 the first matrix | 
| 226 | * @param m2 the second matrix | 
| 227 | */ | 
| 228 | inline void add( const RectMatrix<Real, Row, Col>& m1, const RectMatrix<Real, Row, Col>& m2 ) { | 
| 229 | for (unsigned int i = 0; i < Row; i++) | 
| 230 | for (unsigned int j = 0; j < Col; j++) | 
| 231 | data_[i][j] = m1.data_[i][j] + m2.data_[i][j]; | 
| 232 | } | 
| 233 |  | 
| 234 | /** | 
| 235 | * Sets the value of this matrix to the difference  of itself and m (*this -= m). | 
| 236 | * @param m the other matrix | 
| 237 | */ | 
| 238 | inline void sub( const RectMatrix<Real, Row, Col>& m ) { | 
| 239 | for (unsigned int i = 0; i < Row; i++) | 
| 240 | for (unsigned int j = 0; j < Col; j++) | 
| 241 | data_[i][j] -= m.data_[i][j]; | 
| 242 | } | 
| 243 |  | 
| 244 | /** | 
| 245 | * Sets the value of this matrix to the difference of matrix m1 and m2 (*this = m1 - m2). | 
| 246 | * @param m1 the first matrix | 
| 247 | * @param m2 the second matrix | 
| 248 | */ | 
| 249 | inline void sub( const RectMatrix<Real, Row, Col>& m1, const RectMatrix<Real, Row, Col>& m2){ | 
| 250 | for (unsigned int i = 0; i < Row; i++) | 
| 251 | for (unsigned int j = 0; j < Col; j++) | 
| 252 | data_[i][j] = m1.data_[i][j] - m2.data_[i][j]; | 
| 253 | } | 
| 254 |  | 
| 255 | /** | 
| 256 | * Sets the value of this matrix to the scalar multiplication of itself (*this *= s). | 
| 257 | * @param s the scalar value | 
| 258 | */ | 
| 259 | inline void mul( double s ) { | 
| 260 | for (unsigned int i = 0; i < Row; i++) | 
| 261 | for (unsigned int j = 0; j < Col; j++) | 
| 262 | data_[i][j] *= s; | 
| 263 | } | 
| 264 |  | 
| 265 | /** | 
| 266 | * Sets the value of this matrix to the scalar multiplication of matrix m  (*this = s * m). | 
| 267 | * @param s the scalar value | 
| 268 | * @param m the matrix | 
| 269 | */ | 
| 270 | inline void mul( double s, const RectMatrix<Real, Row, Col>& m ) { | 
| 271 | for (unsigned int i = 0; i < Row; i++) | 
| 272 | for (unsigned int j = 0; j < Col; j++) | 
| 273 | data_[i][j] = s * m.data_[i][j]; | 
| 274 | } | 
| 275 |  | 
| 276 | /** | 
| 277 | * Sets the value of this matrix to the scalar division of itself  (*this /= s ). | 
| 278 | * @param s the scalar value | 
| 279 | */ | 
| 280 | inline void div( double s) { | 
| 281 | for (unsigned int i = 0; i < Row; i++) | 
| 282 | for (unsigned int j = 0; j < Col; j++) | 
| 283 | data_[i][j] /= s; | 
| 284 | } | 
| 285 |  | 
| 286 | /** | 
| 287 | * Sets the value of this matrix to the scalar division of matrix m  (*this = m /s). | 
| 288 | * @param s the scalar value | 
| 289 | * @param m the matrix | 
| 290 | */ | 
| 291 | inline void div( double s, const RectMatrix<Real, Row, Col>& m ) { | 
| 292 | for (unsigned int i = 0; i < Row; i++) | 
| 293 | for (unsigned int j = 0; j < Col; j++) | 
| 294 | data_[i][j] = m.data_[i][j] / s; | 
| 295 | } | 
| 296 |  | 
| 297 | /** | 
| 298 | *  Multiples a scalar into every element of this matrix. | 
| 299 | * @param s the scalar value | 
| 300 | */ | 
| 301 | RectMatrix<Real, Row, Col>& operator *=(const double s) { | 
| 302 | this->mul(s); | 
| 303 | return *this; | 
| 304 | } | 
| 305 |  | 
| 306 | /** | 
| 307 | *  Divides every element of this matrix by a scalar. | 
| 308 | * @param s the scalar value | 
| 309 | */ | 
| 310 | RectMatrix<Real, Row, Col>& operator /=(const double s) { | 
| 311 | this->div(s); | 
| 312 | return *this; | 
| 313 | } | 
| 314 |  | 
| 315 | /** | 
| 316 | * Sets the value of this matrix to the sum of the other matrix and itself (*this += m). | 
| 317 | * @param m the other matrix | 
| 318 | */ | 
| 319 | RectMatrix<Real, Row, Col>& operator += (const RectMatrix<Real, Row, Col>& m) { | 
| 320 | add(m); | 
| 321 | return *this; | 
| 322 | } | 
| 323 |  | 
| 324 | /** | 
| 325 | * Sets the value of this matrix to the differerence of itself and the other matrix (*this -= m) | 
| 326 | * @param m the other matrix | 
| 327 | */ | 
| 328 | RectMatrix<Real, Row, Col>& operator -= (const RectMatrix<Real, Row, Col>& m){ | 
| 329 | sub(m); | 
| 330 | return *this; | 
| 331 | } | 
| 332 |  | 
| 333 | /** Return the transpose of this matrix */ | 
| 334 | RectMatrix<Real,  Col, Row> transpose(){ | 
| 335 | RectMatrix<Real,  Col, Row> result; | 
| 336 |  | 
| 337 | for (unsigned int i = 0; i < Row; i++) | 
| 338 | for (unsigned int j = 0; j < Col; j++) | 
| 339 | result(j, i) = data_[i][j]; | 
| 340 |  | 
| 341 | return result; | 
| 342 | } | 
| 343 |  | 
| 344 | protected: | 
| 345 | Real data_[Row][Col]; | 
| 346 | }; | 
| 347 |  | 
| 348 | /** Negate the value of every element of this matrix. */ | 
| 349 | template<typename Real, unsigned int Row, unsigned int Col> | 
| 350 | inline RectMatrix<Real, Row, Col> operator -(const RectMatrix<Real, Row, Col>& m) { | 
| 351 | RectMatrix<Real, Row, Col> result(m); | 
| 352 |  | 
| 353 | result.negate(); | 
| 354 |  | 
| 355 | return result; | 
| 356 | } | 
| 357 |  | 
| 358 | /** | 
| 359 | * Return the sum of two matrixes  (m1 + m2). | 
| 360 | * @return the sum of two matrixes | 
| 361 | * @param m1 the first matrix | 
| 362 | * @param m2 the second matrix | 
| 363 | */ | 
| 364 | template<typename Real, unsigned int Row, unsigned int Col> | 
| 365 | inline RectMatrix<Real, Row, Col> operator + (const RectMatrix<Real, Row, Col>& m1,const RectMatrix<Real, Row, Col>& m2) { | 
| 366 | RectMatrix<Real, Row, Col> result; | 
| 367 |  | 
| 368 | result.add(m1, m2); | 
| 369 |  | 
| 370 | return result; | 
| 371 | } | 
| 372 |  | 
| 373 | /** | 
| 374 | * Return the difference of two matrixes  (m1 - m2). | 
| 375 | * @return the sum of two matrixes | 
| 376 | * @param m1 the first matrix | 
| 377 | * @param m2 the second matrix | 
| 378 | */ | 
| 379 | template<typename Real, unsigned int Row, unsigned int Col> | 
| 380 | inline RectMatrix<Real, Row, Col> operator - (const RectMatrix<Real, Row, Col>& m1, const RectMatrix<Real, Row, Col>& m2) { | 
| 381 | RectMatrix<Real, Row, Col> result; | 
| 382 |  | 
| 383 | result.sub(m1, m2); | 
| 384 |  | 
| 385 | return result; | 
| 386 | } | 
| 387 |  | 
| 388 | /** | 
| 389 | * Return the multiplication of scalra and  matrix  (m * s). | 
| 390 | * @return the multiplication of a scalra and  a matrix | 
| 391 | * @param m the matrix | 
| 392 | * @param s the scalar | 
| 393 | */ | 
| 394 | template<typename Real, unsigned int Row, unsigned int Col> | 
| 395 | inline RectMatrix<Real, Row, Col> operator *(const RectMatrix<Real, Row, Col>& m, Real s) { | 
| 396 | RectMatrix<Real, Row, Col> result; | 
| 397 |  | 
| 398 | result.mul(s, m); | 
| 399 |  | 
| 400 | return result; | 
| 401 | } | 
| 402 |  | 
| 403 | /** | 
| 404 | * Return the multiplication of a scalra and  a matrix  (s * m). | 
| 405 | * @return the multiplication of a scalra and  a matrix | 
| 406 | * @param s the scalar | 
| 407 | * @param m the matrix | 
| 408 | */ | 
| 409 | template<typename Real, unsigned int Row, unsigned int Col> | 
| 410 | inline RectMatrix<Real, Row, Col> operator *(Real s, const RectMatrix<Real, Row, Col>& m) { | 
| 411 | RectMatrix<Real, Row, Col> result; | 
| 412 |  | 
| 413 | result.mul(s, m); | 
| 414 |  | 
| 415 | return result; | 
| 416 | } | 
| 417 |  | 
| 418 | /** | 
| 419 | * Return the multiplication of two matrixes  (m1 * m2). | 
| 420 | * @return the multiplication of two matrixes | 
| 421 | * @param m1 the first matrix | 
| 422 | * @param m2 the second matrix | 
| 423 | */ | 
| 424 | template<typename Real, unsigned int Row, unsigned int Col, unsigned int SameDim> | 
| 425 | inline RectMatrix<Real, Row, Col> operator *(const RectMatrix<Real, Row, SameDim>& m1, const RectMatrix<Real, SameDim, Col>& m2) { | 
| 426 | RectMatrix<Real, Row, Col> result; | 
| 427 |  | 
| 428 | for (unsigned int i = 0; i < Row; i++) | 
| 429 | for (unsigned int j = 0; j < Col; j++) | 
| 430 | for (unsigned int k = 0; k < SameDim; k++) | 
| 431 | result(i, j)  = m1(i, k) * m2(k, j); | 
| 432 |  | 
| 433 | return result; | 
| 434 | } | 
| 435 |  | 
| 436 | /** | 
| 437 | * Return the multiplication of  a matrix and a vector  (m * v). | 
| 438 | * @return the multiplication of a matrix and a vector | 
| 439 | * @param m the matrix | 
| 440 | * @param v the vector | 
| 441 | */ | 
| 442 | template<typename Real, unsigned int Row, unsigned int Col> | 
| 443 | inline Vector<Real, Row> operator *(const RectMatrix<Real, Row, Col>& m, const Vector<Real, Col>& v) { | 
| 444 | Vector<Real, Row> result; | 
| 445 |  | 
| 446 | for (unsigned int i = 0; i < Row ; i++) | 
| 447 | for (unsigned int j = 0; j < Col ; j++) | 
| 448 | result[i] += m(i, j) * v[j]; | 
| 449 |  | 
| 450 | return result; | 
| 451 | } | 
| 452 |  | 
| 453 | /** | 
| 454 | * Return the scalar division of matrix   (m / s). | 
| 455 | * @return the scalar division of matrix | 
| 456 | * @param m the matrix | 
| 457 | * @param s the scalar | 
| 458 | */ | 
| 459 | template<typename Real, unsigned int Row, unsigned int Col> | 
| 460 | inline RectMatrix<Real, Row, Col> operator /(const RectMatrix<Real, Row, Col>& m, Real s) { | 
| 461 | RectMatrix<Real, Row, Col> result; | 
| 462 |  | 
| 463 | result.div(s, m); | 
| 464 |  | 
| 465 | return result; | 
| 466 | } | 
| 467 | } | 
| 468 | #endif //MATH_RECTMATRIX_HPP |