| 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 | * 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; | 
| 167 |  | 
| 168 | return true; | 
| 169 | } | 
| 170 |  | 
| 171 | /** | 
| 172 | * Tests if this matrix is not equal to matrix m | 
| 173 | * @return true if this matrix is not equal to the matrix m, return false otherwise | 
| 174 | * @m matrix to be compared | 
| 175 | */ | 
| 176 | bool operator !=(const RectMatrix<Real, Row, Col>& m) { | 
| 177 | return !(*this == m); | 
| 178 | } | 
| 179 |  | 
| 180 | /** Negates the value of this matrix in place. */ | 
| 181 | inline void negate() { | 
| 182 | for (unsigned int i = 0; i < Row; i++) | 
| 183 | for (unsigned int j = 0; j < Col; j++) | 
| 184 | data_[i][j] = -data_[i][j]; | 
| 185 | } | 
| 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 | } | 
| 238 |  | 
| 239 | /** | 
| 240 | * Sets the value of this matrix to the scalar multiplication of itself (*this *= s). | 
| 241 | * @param s the scalar value | 
| 242 | */ | 
| 243 | inline void mul( double s ) { | 
| 244 | for (unsigned int i = 0; i < Row; i++) | 
| 245 | for (unsigned int j = 0; j < Col; j++) | 
| 246 | data_[i][j] *= s; | 
| 247 | } | 
| 248 |  | 
| 249 | /** | 
| 250 | * Sets the value of this matrix to the scalar multiplication of matrix m  (*this = s * m). | 
| 251 | * @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 | } | 
| 259 |  | 
| 260 | /** | 
| 261 | * Sets the value of this matrix to the scalar division of itself  (*this /= s ). | 
| 262 | * @param s the scalar value | 
| 263 | */ | 
| 264 | inline void div( double s) { | 
| 265 | for (unsigned int i = 0; i < Row; i++) | 
| 266 | for (unsigned int j = 0; j < Col; j++) | 
| 267 | data_[i][j] /= s; | 
| 268 | } | 
| 269 |  | 
| 270 | /** | 
| 271 | * Sets the value of this matrix to the scalar division of matrix m  (*this = m /s). | 
| 272 | * @param s the scalar value | 
| 273 | * @param m the matrix | 
| 274 | */ | 
| 275 | inline void div( double s, const RectMatrix<Real, Row, Col>& m ) { | 
| 276 | for (unsigned int i = 0; i < Row; i++) | 
| 277 | for (unsigned int j = 0; j < Col; j++) | 
| 278 | data_[i][j] = m.data_[i][j] / s; | 
| 279 | } | 
| 280 |  | 
| 281 | /** | 
| 282 | *  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 | } | 
| 289 |  | 
| 290 | /** | 
| 291 | *  Divides every element of this matrix by a scalar. | 
| 292 | * @param s the scalar value | 
| 293 | */ | 
| 294 | RectMatrix<Real, Row, Col>& operator /=(const double s) { | 
| 295 | this->div(s); | 
| 296 | return *this; | 
| 297 | } | 
| 298 |  | 
| 299 | /** | 
| 300 | * Sets the value of this matrix to the sum of the other matrix and itself (*this += m). | 
| 301 | * @param m the other matrix | 
| 302 | */ | 
| 303 | RectMatrix<Real, Row, Col>& operator += (const RectMatrix<Real, Row, Col>& m) { | 
| 304 | 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; | 
| 320 |  | 
| 321 | for (unsigned int i = 0; i < Row; i++) | 
| 322 | for (unsigned int j = 0; j < Col; j++) | 
| 323 | result(j, i) = data_[i][j]; | 
| 324 |  | 
| 325 | return result; | 
| 326 | } | 
| 327 |  | 
| 328 | protected: | 
| 329 | Real data_[Row][Col]; | 
| 330 | }; | 
| 331 |  | 
| 332 | /** Negate the value of every element of this matrix. */ | 
| 333 | template<typename Real, unsigned int Row, unsigned int Col> | 
| 334 | inline RectMatrix<Real, Row, Col> operator -(const RectMatrix<Real, Row, Col>& m) { | 
| 335 | RectMatrix<Real, Row, Col> result(m); | 
| 336 |  | 
| 337 | result.negate(); | 
| 338 |  | 
| 339 | return result; | 
| 340 | } | 
| 341 |  | 
| 342 | /** | 
| 343 | * Return the sum of two matrixes  (m1 + m2). | 
| 344 | * @return the sum of two matrixes | 
| 345 | * @param m1 the first matrix | 
| 346 | * @param m2 the second matrix | 
| 347 | */ | 
| 348 | template<typename Real, unsigned int Row, unsigned int Col> | 
| 349 | inline RectMatrix<Real, Row, Col> operator + (const RectMatrix<Real, Row, Col>& m1,const RectMatrix<Real, Row, Col>& m2) { | 
| 350 | RectMatrix<Real, Row, Col> result; | 
| 351 |  | 
| 352 | result.add(m1, m2); | 
| 353 |  | 
| 354 | return result; | 
| 355 | } | 
| 356 |  | 
| 357 | /** | 
| 358 | * Return the difference of two matrixes  (m1 - m2). | 
| 359 | * @return the sum of two matrixes | 
| 360 | * @param m1 the first matrix | 
| 361 | * @param m2 the second matrix | 
| 362 | */ | 
| 363 | template<typename Real, unsigned int Row, unsigned int Col> | 
| 364 | inline RectMatrix<Real, Row, Col> operator - (const RectMatrix<Real, Row, Col>& m1, const RectMatrix<Real, Row, Col>& m2) { | 
| 365 | RectMatrix<Real, Row, Col> result; | 
| 366 |  | 
| 367 | result.sub(m1, m2); | 
| 368 |  | 
| 369 | return result; | 
| 370 | } | 
| 371 |  | 
| 372 | /** | 
| 373 | * Return the multiplication of scalra and  matrix  (m * s). | 
| 374 | * @return the multiplication of a scalra and  a matrix | 
| 375 | * @param m the matrix | 
| 376 | * @param s the scalar | 
| 377 | */ | 
| 378 | template<typename Real, unsigned int Row, unsigned int Col> | 
| 379 | inline RectMatrix<Real, Row, Col> operator *(const RectMatrix<Real, Row, Col>& m, Real s) { | 
| 380 | RectMatrix<Real, Row, Col> result; | 
| 381 |  | 
| 382 | result.mul(s, m); | 
| 383 |  | 
| 384 | return result; | 
| 385 | } | 
| 386 |  | 
| 387 | /** | 
| 388 | * Return the multiplication of a scalra and  a matrix  (s * m). | 
| 389 | * @return the multiplication of a scalra and  a matrix | 
| 390 | * @param s the scalar | 
| 391 | * @param m the matrix | 
| 392 | */ | 
| 393 | template<typename Real, unsigned int Row, unsigned int Col> | 
| 394 | inline RectMatrix<Real, Row, Col> operator *(Real s, const RectMatrix<Real, Row, Col>& m) { | 
| 395 | RectMatrix<Real, Row, Col> result; | 
| 396 |  | 
| 397 | result.mul(s, m); | 
| 398 |  | 
| 399 | return result; | 
| 400 | } | 
| 401 |  | 
| 402 | /** | 
| 403 | * Return the multiplication of two matrixes  (m1 * m2). | 
| 404 | * @return the multiplication of two matrixes | 
| 405 | * @param m1 the first matrix | 
| 406 | * @param m2 the second matrix | 
| 407 | */ | 
| 408 | template<typename Real, unsigned int Row, unsigned int Col, unsigned int SameDim> | 
| 409 | inline RectMatrix<Real, Row, Col> operator *(const RectMatrix<Real, Row, SameDim>& m1, const RectMatrix<Real, SameDim, Col>& m2) { | 
| 410 | RectMatrix<Real, Row, Col> result; | 
| 411 |  | 
| 412 | for (unsigned int i = 0; i < Row; i++) | 
| 413 | for (unsigned int j = 0; j < Col; j++) | 
| 414 | for (unsigned int k = 0; k < SameDim; k++) | 
| 415 | result(i, j)  += m1(i, k) * m2(k, j); | 
| 416 |  | 
| 417 | return result; | 
| 418 | } | 
| 419 |  | 
| 420 | /** | 
| 421 | * Return the multiplication of  a matrix and a vector  (m * v). | 
| 422 | * @return the multiplication of a matrix and a vector | 
| 423 | * @param m the matrix | 
| 424 | * @param v the vector | 
| 425 | */ | 
| 426 | template<typename Real, unsigned int Row, unsigned int Col> | 
| 427 | inline Vector<Real, Row> operator *(const RectMatrix<Real, Row, Col>& m, const Vector<Real, Col>& v) { | 
| 428 | Vector<Real, Row> result; | 
| 429 |  | 
| 430 | for (unsigned int i = 0; i < Row ; i++) | 
| 431 | for (unsigned int j = 0; j < Col ; j++) | 
| 432 | result[i] += m(i, j) * v[j]; | 
| 433 |  | 
| 434 | return result; | 
| 435 | } | 
| 436 |  | 
| 437 | /** | 
| 438 | * Return the scalar division of matrix   (m / s). | 
| 439 | * @return the scalar division of matrix | 
| 440 | * @param m the matrix | 
| 441 | * @param s the scalar | 
| 442 | */ | 
| 443 | template<typename Real, unsigned int Row, unsigned int Col> | 
| 444 | inline RectMatrix<Real, Row, Col> operator /(const RectMatrix<Real, Row, Col>& m, Real s) { | 
| 445 | RectMatrix<Real, Row, Col> result; | 
| 446 |  | 
| 447 | result.div(s, m); | 
| 448 |  | 
| 449 | return result; | 
| 450 | } | 
| 451 |  | 
| 452 | /** | 
| 453 | * Write to an output stream | 
| 454 | */ | 
| 455 | template<typename Real,  unsigned int Row, unsigned int Col> | 
| 456 | std::ostream &operator<< ( std::ostream& o, const RectMatrix<Real, Row, Col>& m) { | 
| 457 | for (unsigned int i = 0; i < Row ; i++) { | 
| 458 | o << "(" | 
| 459 | for (unsigned int j = 0; j < Col ; j++) { | 
| 460 | o << m(i, j) << "\t" | 
| 461 | } | 
| 462 | o << ")" << std::endl; | 
| 463 | } | 
| 464 | return o; | 
| 465 | } | 
| 466 | } | 
| 467 | #endif //MATH_RECTMATRIX_HPP |