| 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 |  | 
| 42 | /** | 
| 43 | * @class RectMatrix RectMatrix.hpp "math/RectMatrix.hpp" | 
| 44 | * @brief rectangular matrix class | 
| 45 | */ | 
| 46 | template<typename Real, unsigned int Row, unsigned int Col> | 
| 47 | class RectMatrix { | 
| 48 | public: | 
| 49 |  | 
| 50 | /** default constructor */ | 
| 51 | RectMatrix() { | 
| 52 | for (unsigned int i = 0; i < Row; i++) | 
| 53 | for (unsigned int j = 0; j < Col; j++) | 
| 54 | data_[i][j] = 0.0; | 
| 55 | } | 
| 56 |  | 
| 57 | /** Constructs and initializes every element of this matrix to a scalar */ | 
| 58 | RectMatrix(Real s) { | 
| 59 | for (unsigned int i = 0; i < Row; i++) | 
| 60 | for (unsigned int j = 0; j < Col; j++) | 
| 61 | data_[i][j] = s; | 
| 62 | } | 
| 63 |  | 
| 64 | /** copy constructor */ | 
| 65 | RectMatrix(const RectMatrix<Real, Row, Col>& m) { | 
| 66 | *this = m; | 
| 67 | } | 
| 68 |  | 
| 69 | /** destructor*/ | 
| 70 | ~RectMatrix() {} | 
| 71 |  | 
| 72 | /** copy assignment operator */ | 
| 73 | RectMatrix<Real, Row, Col>& operator =(const RectMatrix<Real, Row, Col>& m) { | 
| 74 | if (this == &m) | 
| 75 | return *this; | 
| 76 |  | 
| 77 | for (unsigned int i = 0; i < Row; i++) | 
| 78 | for (unsigned int j = 0; j < Col; j++) | 
| 79 | data_[i][j] = m.data_[i][j]; | 
| 80 | return *this; | 
| 81 | } | 
| 82 |  | 
| 83 | /** | 
| 84 | * Return the reference of a single element of this matrix. | 
| 85 | * @return the reference of a single element of this matrix | 
| 86 | * @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 | } | 
| 93 |  | 
| 94 | /** | 
| 95 | * Return the value of a single element of this matrix. | 
| 96 | * @return the value of a single element of this matrix | 
| 97 | * @param i row index | 
| 98 | * @param j colum index | 
| 99 | */ | 
| 100 | double operator()(unsigned int i, unsigned int j) const  { | 
| 101 |  | 
| 102 | return data_[i][j]; | 
| 103 | } | 
| 104 |  | 
| 105 | /** | 
| 106 | * Returns a row of  this matrix as a vector. | 
| 107 | * @return a row of  this matrix as a vector | 
| 108 | * @param row the row index | 
| 109 | */ | 
| 110 | Vector<Real, Row> getRow(unsigned int row) { | 
| 111 | Vector<Real, Row> v; | 
| 112 |  | 
| 113 | for (unsigned int i = 0; i < Row; i++) | 
| 114 | v[i] = data_[row][i]; | 
| 115 |  | 
| 116 | return v; | 
| 117 | } | 
| 118 |  | 
| 119 | /** | 
| 120 | * 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) { | 
| 125 |  | 
| 126 | for (unsigned int i = 0; i < Row; i++) | 
| 127 | data_[row][i] = v[i]; | 
| 128 | } | 
| 129 |  | 
| 130 | /** | 
| 131 | * 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; | 
| 137 |  | 
| 138 | for (unsigned int j = 0; j < Col; j++) | 
| 139 | v[j] = data_[j][col]; | 
| 140 |  | 
| 141 | return v; | 
| 142 | } | 
| 143 |  | 
| 144 | /** | 
| 145 | * 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){ | 
| 150 |  | 
| 151 | for (unsigned int j = 0; j < Col; j++) | 
| 152 | data_[j][col] = v[j]; | 
| 153 | } | 
| 154 |  | 
| 155 | /** | 
| 156 | * 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); | 
| 162 |  | 
| 163 | for (unsigned int k = 0; k < Col; k++) | 
| 164 | std::swap(data_[i][k], data_[j][k]); | 
| 165 | } | 
| 166 |  | 
| 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++) | 
| 188 | for (unsigned int j = 0; j < Col; j++) | 
| 189 | if (!equal(data_[i][j], m.data_[i][j])) | 
| 190 | return false; | 
| 191 |  | 
| 192 | return true; | 
| 193 | } | 
| 194 |  | 
| 195 | /** | 
| 196 | * 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 | } | 
| 203 |  | 
| 204 | /** Negates the value of this matrix in place. */ | 
| 205 | inline void negate() { | 
| 206 | for (unsigned int i = 0; i < Row; i++) | 
| 207 | for (unsigned int j = 0; j < Col; j++) | 
| 208 | data_[i][j] = -data_[i][j]; | 
| 209 | } | 
| 210 |  | 
| 211 | /** | 
| 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 | } | 
| 262 |  | 
| 263 | /** | 
| 264 | * Sets the value of this matrix to the scalar multiplication of itself (*this *= s). | 
| 265 | * @param s the scalar value | 
| 266 | */ | 
| 267 | inline void mul( double s ) { | 
| 268 | 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; | 
| 344 |  | 
| 345 | for (unsigned int i = 0; i < Row; i++) | 
| 346 | for (unsigned int j = 0; j < Col; j++) | 
| 347 | result(j, i) = data_[i][j]; | 
| 348 |  | 
| 349 | return result; | 
| 350 | } | 
| 351 |  | 
| 352 | protected: | 
| 353 | Real data_[Row][Col]; | 
| 354 | }; | 
| 355 |  | 
| 356 | /** Negate the value of every element of this matrix. */ | 
| 357 | template<typename Real, unsigned int Row, unsigned int Col> | 
| 358 | inline RectMatrix<Real, Row, Col> operator -(const RectMatrix<Real, Row, Col>& m) { | 
| 359 | RectMatrix<Real, Row, Col> result(m); | 
| 360 |  | 
| 361 | result.negate(); | 
| 362 |  | 
| 363 | return result; | 
| 364 | } | 
| 365 |  | 
| 366 | /** | 
| 367 | * Return the sum of two matrixes  (m1 + m2). | 
| 368 | * @return the sum of two matrixes | 
| 369 | * @param m1 the first matrix | 
| 370 | * @param m2 the second matrix | 
| 371 | */ | 
| 372 | template<typename Real, unsigned int Row, unsigned int Col> | 
| 373 | inline RectMatrix<Real, Row, Col> operator + (const RectMatrix<Real, Row, Col>& m1,const RectMatrix<Real, Row, Col>& m2) { | 
| 374 | RectMatrix<Real, Row, Col> result; | 
| 375 |  | 
| 376 | result.add(m1, m2); | 
| 377 |  | 
| 378 | return result; | 
| 379 | } | 
| 380 |  | 
| 381 | /** | 
| 382 | * Return the difference of two matrixes  (m1 - m2). | 
| 383 | * @return the sum of two matrixes | 
| 384 | * @param m1 the first matrix | 
| 385 | * @param m2 the second matrix | 
| 386 | */ | 
| 387 | template<typename Real, unsigned int Row, unsigned int Col> | 
| 388 | inline RectMatrix<Real, Row, Col> operator - (const RectMatrix<Real, Row, Col>& m1, const RectMatrix<Real, Row, Col>& m2) { | 
| 389 | RectMatrix<Real, Row, Col> result; | 
| 390 |  | 
| 391 | result.sub(m1, m2); | 
| 392 |  | 
| 393 | return result; | 
| 394 | } | 
| 395 |  | 
| 396 | /** | 
| 397 | * Return the multiplication of scalra and  matrix  (m * s). | 
| 398 | * @return the multiplication of a scalra and  a matrix | 
| 399 | * @param m the matrix | 
| 400 | * @param s the scalar | 
| 401 | */ | 
| 402 | template<typename Real, unsigned int Row, unsigned int Col> | 
| 403 | inline RectMatrix<Real, Row, Col> operator *(const RectMatrix<Real, Row, Col>& m, Real s) { | 
| 404 | RectMatrix<Real, Row, Col> result; | 
| 405 |  | 
| 406 | result.mul(s, m); | 
| 407 |  | 
| 408 | return result; | 
| 409 | } | 
| 410 |  | 
| 411 | /** | 
| 412 | * Return the multiplication of a scalra and  a matrix  (s * m). | 
| 413 | * @return the multiplication of a scalra and  a matrix | 
| 414 | * @param s the scalar | 
| 415 | * @param m the matrix | 
| 416 | */ | 
| 417 | template<typename Real, unsigned int Row, unsigned int Col> | 
| 418 | inline RectMatrix<Real, Row, Col> operator *(Real s, const RectMatrix<Real, Row, Col>& m) { | 
| 419 | RectMatrix<Real, Row, Col> result; | 
| 420 |  | 
| 421 | result.mul(s, m); | 
| 422 |  | 
| 423 | return result; | 
| 424 | } | 
| 425 |  | 
| 426 | /** | 
| 427 | * Return the multiplication of two matrixes  (m1 * m2). | 
| 428 | * @return the multiplication of two matrixes | 
| 429 | * @param m1 the first matrix | 
| 430 | * @param m2 the second matrix | 
| 431 | */ | 
| 432 | template<typename Real, unsigned int Row, unsigned int Col, unsigned int SameDim> | 
| 433 | inline RectMatrix<Real, Row, Col> operator *(const RectMatrix<Real, Row, SameDim>& m1, const RectMatrix<Real, SameDim, Col>& m2) { | 
| 434 | RectMatrix<Real, Row, Col> result; | 
| 435 |  | 
| 436 | for (unsigned int i = 0; i < Row; i++) | 
| 437 | for (unsigned int j = 0; j < Col; j++) | 
| 438 | for (unsigned int k = 0; k < SameDim; k++) | 
| 439 | result(i, j)  += m1(i, k) * m2(k, j); | 
| 440 |  | 
| 441 | return result; | 
| 442 | } | 
| 443 |  | 
| 444 | /** | 
| 445 | * Return the multiplication of  a matrix and a vector  (m * v). | 
| 446 | * @return the multiplication of a matrix and a vector | 
| 447 | * @param m the matrix | 
| 448 | * @param v the vector | 
| 449 | */ | 
| 450 | template<typename Real, unsigned int Row, unsigned int Col> | 
| 451 | inline Vector<Real, Row> operator *(const RectMatrix<Real, Row, Col>& m, const Vector<Real, Col>& v) { | 
| 452 | Vector<Real, Row> result; | 
| 453 |  | 
| 454 | for (unsigned int i = 0; i < Row ; i++) | 
| 455 | for (unsigned int j = 0; j < Col ; j++) | 
| 456 | result[i] += m(i, j) * v[j]; | 
| 457 |  | 
| 458 | return result; | 
| 459 | } | 
| 460 |  | 
| 461 | /** | 
| 462 | * Return the scalar division of matrix   (m / s). | 
| 463 | * @return the scalar division of matrix | 
| 464 | * @param m the matrix | 
| 465 | * @param s the scalar | 
| 466 | */ | 
| 467 | template<typename Real, unsigned int Row, unsigned int Col> | 
| 468 | inline RectMatrix<Real, Row, Col> operator /(const RectMatrix<Real, Row, Col>& m, Real s) { | 
| 469 | RectMatrix<Real, Row, Col> result; | 
| 470 |  | 
| 471 | result.div(s, m); | 
| 472 |  | 
| 473 | return result; | 
| 474 | } | 
| 475 |  | 
| 476 | /** | 
| 477 | * Write to an output stream | 
| 478 | */ | 
| 479 | template<typename Real,  unsigned int Row, unsigned int Col> | 
| 480 | std::ostream &operator<< ( std::ostream& o, const RectMatrix<Real, Row, Col>& m) { | 
| 481 | for (unsigned int i = 0; i < Row ; i++) { | 
| 482 | o << "("; | 
| 483 | for (unsigned int j = 0; j < Col ; j++) { | 
| 484 | o << m(i, j); | 
| 485 | if (j != Col -1) | 
| 486 | o << "\t"; | 
| 487 | } | 
| 488 | o << ")" << std::endl; | 
| 489 | } | 
| 490 | return o; | 
| 491 | } | 
| 492 | } | 
| 493 | #endif //MATH_RECTMATRIX_HPP |