MatrixVector.h 14.4 KB
 Peter Gottschling committed Feb 15, 2008 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 // ============================================================================ // == == // == AMDiS - Adaptive multidimensional simulations == // == == // ============================================================================ // == == // == crystal growth group == // == == // == Stiftung caesar == // == Ludwig-Erhard-Allee 2 == // == 53175 Bonn == // == germany == // == == // ============================================================================ // == == // == http://www.caesar.de/cg/AMDiS == // == == // ============================================================================ /** \file MatrixVector.h */ #ifndef AMDIS_MATRIXVECTOR_H #define AMDIS_MATRIXVECTOR_H #include #include "Global.h" #include "MemoryManager.h" #include "Serializable.h" namespace AMDiS { template class DOFVector; // ============================================================================ // ===== class Vector ========================================================= // ============================================================================ /** \brief * Class for efficient vector operations of fixed size numerical vectors. */ template class Vector : public Serializable { public: MEMORY_MANAGED(Vector); /** \brief * Constructor. */ Vector(int i = 0) : size(i) { if (size == 0) valArray = NULL; else valArray = new T[size]; }; inline bool used() const { return (valArray != NULL); }; /** \brief * Change the size of the vector to newSize. */ inline void resize(int newSize) { if (size != newSize) { if (valArray) delete [] valArray; valArray = new T[newSize]; size = newSize; } }; /** \brief * Copy constructor. */ Vector(const Vector& rhs) : Serializable(),size(rhs.size) { valArray = new T[rhs.size]; *this = rhs; // uses operator=() }; /** \brief * Destructor. */ virtual ~Vector() { delete [] valArray; }; /** \brief * Assignement operator */ inline const Vector& operator=(const Vector& rhs) {  Thomas Witkowski committed May 20, 2008 97  TEST_EXIT_DBG(rhs.size == size)("invalid size\n");  Peter Gottschling committed Feb 15, 2008 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193  T *rhsIt, *thisIt; for(rhsIt = rhs.begin(), thisIt = this->begin(); rhsIt != rhs.end(); ++rhsIt, ++thisIt) { *thisIt = *rhsIt; } return *this; }; /** \brief * Assignement operator */ inline const Vector& operator=(const T& scal) { T *thisIt; for(thisIt = this->begin(); thisIt != this->end(); ++thisIt) { *thisIt = scal; } return *this; }; /** \brief * Assignement operator */ inline const Vector& operator=(const T* vec) { T *thisIt; const T *vecIt; for(thisIt = this->begin(), vecIt = &vec[0]; thisIt != this->end(); ++thisIt, ++vecIt) { *thisIt = *vecIt; } return *this; }; /** \brief * Sets all entries to scal. */ inline const Vector& set(const T& scal) { return *this = scal; }; /** \brief * Sets all entries. */ inline const Vector& setValues(const T* values) { T *thisIt; const T *valuesIt; for(thisIt = this->begin(), valuesIt = values; thisIt != this->end(); ++thisIt, ++valuesIt) { *thisIt = *valuesIt; } return *this; }; /** \brief * Sets all entries. */ inline void fill(const T value) { for (T *thisIt = this->begin(); thisIt != this->end(); thisIt++) { *thisIt = value; } } /** \brief * Comparison operator. */ inline bool operator==(const Vector& rhs) const { if(size != rhs.size) return false; T *rhsIt, *thisIt; for(rhsIt = rhs.begin(), thisIt = this->begin(); rhsIt != rhs.end(); ++rhsIt, ++thisIt) { if(*thisIt != *rhsIt) return false; } return true; }; /** \brief * Comparison operator. */ inline bool operator!=(const Vector& rhs) const { return !(*this==rhs); } /** \brief * Access to the i-th vector element. */ inline T& operator[](int i) {  Thomas Witkowski committed May 20, 2008 194  TEST_EXIT_DBG(i < size && i >= 0)("invalid index\n");  Peter Gottschling committed Feb 15, 2008 195 196 197 198 199 200 201  return valArray[i]; }; /** \brief * Access to the i-th vector element for const vectors. */ inline const T& operator[] (int i) const {  Thomas Witkowski committed May 20, 2008 202  TEST_EXIT_DBG(i < size && i >= 0)("invalid index\n");  Peter Gottschling committed Feb 15, 2008 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234  return valArray[i]; }; /** \brief * Returns pointer to the first vector element. */ inline T *begin() const { return valArray; }; /** \brief * Returns pointer after the last vector element. */ inline T *end() const { return valArray + size; }; /** \brief * Returns \ref size. */ virtual int getSize() const { return size; }; /** \brief * Returns \ref valArray as T-array */ inline T *getValArray() { return valArray; }; void print() const {  Thomas Witkowski committed Aug 25, 2008 235  std::cout << this->size << " vector: " << std::endl;  Peter Gottschling committed Feb 15, 2008 236  for (int i = 0; i < size; i++) {  Thomas Witkowski committed Aug 25, 2008 237  std::cout << this->valArray[i] << " ";  Peter Gottschling committed Feb 15, 2008 238  }  Thomas Witkowski committed Aug 25, 2008 239  std::cout << std::endl;  Peter Gottschling committed Feb 15, 2008 240 241 242 243  }; // ===== Serializable implementation =====  Thomas Witkowski committed Aug 25, 2008 244  void serialize(std::ostream &out) {  Peter Gottschling committed Feb 15, 2008 245 246 247 248  out.write(reinterpret_cast(&size), sizeof(int)); out.write(reinterpret_cast(valArray), size * sizeof(T)); };  Thomas Witkowski committed Aug 25, 2008 249  void deserialize(std::istream &in) {  Peter Gottschling committed Feb 15, 2008 250 251 252 253  in.read(reinterpret_cast(&size), sizeof(int)); in.read(reinterpret_cast(valArray), size * sizeof(T)); };  Thomas Witkowski committed Aug 25, 2008 254  std::string getTypeName() const {  Peter Gottschling committed Feb 15, 2008 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307  return "Vector"; }; protected: /** \brief * Size of the vector. */ int size; /** \brief * Internal storage pointer. */ T *valArray; }; // ============================================================================ // ===== class Matrix ========================================================= // ============================================================================ /** \brief * Class for efficient matrix operations of fixed size numerical matrices. */ template class Matrix : public Vector { public: MEMORY_MANAGED(Matrix); /** \brief * Constructor. */ Matrix(int r, int c) : Vector(r*c), rows(r), cols(c) {}; /** \brief * Changes the size of the matrix to newRows x newCols. */ inline void resize(int newRows, int newCols) { if ((newRows != rows) || (newCols != cols)) { Vector::resize(newRows * newCols); rows = newRows; cols = newCols; } }; /** \brief * Assignement operator. */  Thomas Witkowski committed Aug 25, 2008 308  inline const Matrix& operator=(const T& scal) {  Peter Gottschling committed Feb 15, 2008 309 310 311  return static_cast&>(Vector::operator=(scal)); };  Thomas Witkowski committed Aug 25, 2008 312  inline bool operator==(const Matrix& rhs) const {  Peter Gottschling committed Feb 15, 2008 313 314 315 316 317 318 319 320  if (rows != rhs.getNumRows()) return false; if (cols != rhs.getNumCols()) return false; return Vector::operator == (rhs); }; /** \brief * Comparison operator. */  Thomas Witkowski committed Aug 25, 2008 321 322  inline bool operator!=(const Matrix& rhs) const { return !(*this == rhs);  Peter Gottschling committed Feb 15, 2008 323 324 325 326 327  } /** \brief * Acces to i-th matrix row. */  Thomas Witkowski committed Aug 25, 2008 328  inline T *operator[](int i) {  Peter Gottschling committed Feb 15, 2008 329 330 331 332 333 334  return this->valArray + cols * i; }; /** \brief * Acces to i-th matrix row for constant matrices. */  Thomas Witkowski committed Aug 25, 2008 335  inline const T *operator[](int i) const {  Peter Gottschling committed Feb 15, 2008 336 337 338 339 340 341  return this->valArray + cols * i; }; /** \brief * Returns \ref rows. */  Thomas Witkowski committed Aug 25, 2008 342  inline int getNumRows() const {  Peter Gottschling committed Feb 15, 2008 343 344 345 346 347 348  return rows; }; /** \brief * Return \ref cols. */  Thomas Witkowski committed Aug 25, 2008 349  inline int getNumCols() const {  Peter Gottschling committed Feb 15, 2008 350 351 352 353 354 355  return cols; }; /** \brief * Returns \ref rows. */  Thomas Witkowski committed Aug 25, 2008 356  inline int getSize() const {  Peter Gottschling committed Feb 15, 2008 357 358 359 360 361 362 363 364 365 366 367  return rows; }; /** \brief * Returns pointer after the last vector element. */ inline T *end() const { return this->valArray + (cols * rows); }; void print() const {  Thomas Witkowski committed Aug 25, 2008 368  std::cout << this->rows << " x " << this->cols << " matrix: " << std::endl;  Peter Gottschling committed Feb 15, 2008 369 370  for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) {  Thomas Witkowski committed Aug 25, 2008 371  std::cout << this->valArray[i * cols + j] << " ";  Peter Gottschling committed Feb 15, 2008 372  }  Thomas Witkowski committed Aug 25, 2008 373  std::cout << std::endl;  Peter Gottschling committed Feb 15, 2008 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394  } }; protected: /** \brief * Number of matrix rows. */ int rows; /** \brief * Number of matrix columns. */ int cols; }; /** \brief * Matrix vector multiplication. */ template inline const Vector& mv(const Matrix& m, const Vector& v, Vector& result) {  Thomas Witkowski committed May 20, 2008 395 396  TEST_EXIT_DBG(m.getNumCols() == v.getSize())("m and v not compatible\n"); TEST_EXIT_DBG(v.getSize() == result.getSize())("wrong result size\n");  Peter Gottschling committed Feb 15, 2008 397 398 399 400 401  T *resultIt, *mIt, *vIt; for (resultIt = result.begin(), mIt = m.begin(); resultIt != result.end();  Thomas Witkowski committed Aug 25, 2008 402 403 404 405  ++resultIt) { *resultIt = 0; for (vIt = v.begin(); vIt != v.end(); ++vIt, ++mIt) { *resultIt += *mIt * *vIt;  Peter Gottschling committed Feb 15, 2008 406  }  Thomas Witkowski committed Aug 25, 2008 407  }  Peter Gottschling committed Feb 15, 2008 408 409 410 411  return result; }; /** \brief  Thomas Witkowski committed Aug 25, 2008 412  * Computes x(Ay)^T, with x and y vectors, and A a matrix.  Peter Gottschling committed Feb 15, 2008 413 414  */ template  Thomas Witkowski committed Aug 25, 2008 415  inline T xAy(const Vector& x, const Matrix& a, const Vector& y)  Peter Gottschling committed Feb 15, 2008 416  {  Thomas Witkowski committed Aug 25, 2008 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439  TEST_EXIT_DBG(a.getNumCols() == x.getSize())("A and x not compatible\n"); TEST_EXIT_DBG(a.getNumCols() == y.getSize())("A and y not compatible\n"); T result = 0; T tmp = 0; T *aIt, *xIt, *yIt; for (aIt = a.begin(), xIt = x.begin(); aIt != a.end(); ++xIt) { tmp = 0; for (yIt = y.begin(); yIt != y.end(); ++yIt, ++aIt) { tmp += *aIt * *yIt; } result += *xIt * tmp; } return result; } /** \brief * Matrix vector multiplication. */ template inline const Vector& operator*=(const Vector& v, const Matrix& m) {  Peter Gottschling committed Feb 15, 2008 440 441 442 443 444 445 446  return mv(m, v, v); }; /** \brief * Matrix vector multiplication. */ template  Thomas Witkowski committed Aug 25, 2008 447  inline Vector operator*(const Matrix& m, const Vector& v) {  Peter Gottschling committed Feb 15, 2008 448 449 450 451 452 453 454 455 456  Vector result(v.getSize()); return mv(m, v, result); }; /** \brief * Scalar product. */ template  Thomas Witkowski committed Aug 25, 2008 457  inline double operator*(const Vector& v1, const Vector& v2)  Peter Gottschling committed Feb 15, 2008 458  {  Thomas Witkowski committed Aug 25, 2008 459  double result = 0.0;  Peter Gottschling committed Feb 15, 2008 460 461 462 463  T *v1It, *v2It; for (v1It = v1.begin(), v2It = v2.begin(); v1It != v1.end();  Thomas Witkowski committed Aug 25, 2008 464 465 466  ++v1It, ++v2It) { result += *v1It * *v2It; }  Peter Gottschling committed Feb 15, 2008 467 468 469 470 471 472 473 474 475 476  return result; }; /** \brief * Vector addition. */ template inline const Vector& add(const Vector& v1, const Vector& v2, Vector& result) {  Thomas Witkowski committed May 20, 2008 477 478  TEST_EXIT_DBG(v1.getSize() == v2.getSize())("invalid size in test v1 == v2\n"); TEST_EXIT_DBG(v2.getSize() == result.getSize())("invalid size in test v2 == result\n");  Peter Gottschling committed Feb 15, 2008 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497  T *v1It, *v2It, *resultIt; for (v1It = v1.begin(), v2It = v2.begin(), resultIt = result.begin(); v1It != v1.end(); ++v1It, ++v2It, ++resultIt) { *resultIt = *v1It + *v2It; } return result; }; /** \brief * scalar * vector */ template inline const Vector& mult(const T& scal, const Vector& v, Vector& result) {  Thomas Witkowski committed May 20, 2008 498  TEST_EXIT_DBG(v.getSize() == result.getSize())("invalid size\n");  Thomas Witkowski committed Aug 25, 2008 499   Peter Gottschling committed Feb 15, 2008 500 501 502  T *vIt, *resultIt; for (vIt = v.begin(), resultIt = result.begin(); vIt != v.end();  Thomas Witkowski committed Aug 25, 2008 503 504 505  ++vIt, ++resultIt) { *resultIt = scal * *vIt; }  Peter Gottschling committed Feb 15, 2008 506 507 508 509 510 511 512 513 514 515  return result; }; /** \brief * vector + scalar */ template inline const Vector& add(const Vector& v, const T& scal, Vector& result) {  Thomas Witkowski committed May 20, 2008 516  TEST_EXIT_DBG(v.getSize() == result.getSize())("invalid size\n");  Peter Gottschling committed Feb 15, 2008 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535  T *vIt, *resultIt; for (vIt = v.begin(), resultIt = result.begin(); vIt != v.end(); ++vIt, ++resultIt) { *resultIt = *vIt + scal; } return result; }; /** \brief * y = a * x + y. */ template inline const Vector& axpy(const T& a, const Vector &x, Vector &y) {  Thomas Witkowski committed May 20, 2008 536  TEST_EXIT_DBG(x.getSize() == y.getSize())("invalid size\n");  Peter Gottschling committed Feb 15, 2008 537 538 539  T *xIt, *yIt; for (xIt = x.begin(), yIt = y.begin(); xIt != x.end();  Thomas Witkowski committed Aug 25, 2008 540 541 542  ++xIt, ++yIt) { *yIt += a * *xIt; }  Peter Gottschling committed Feb 15, 2008 543 544 545 546 547  return y; }; template  Thomas Witkowski committed Aug 25, 2008 548  inline const Vector& operator*=(Vector& v, const T& scal) {  Peter Gottschling committed Feb 15, 2008 549 550 551 552  return mult(scal, v, v); }; template  Thomas Witkowski committed Aug 25, 2008 553  inline Vector operator*(const Vector& v, const T& scal) {  Peter Gottschling committed Feb 15, 2008 554 555 556 557 558 559  Vector result = v; result *= scal; return result; }; template  Thomas Witkowski committed Aug 25, 2008 560  inline const Vector& operator+(const Vector& v1, const T& scal) {  Peter Gottschling committed Feb 15, 2008 561 562 563 564 565  Vector result(v1.getSize()); return add(v1, scal, result); }; template  Thomas Witkowski committed Aug 25, 2008 566  inline const Vector& operator+=(Vector& v1, const Vector& v2) {  Peter Gottschling committed Feb 15, 2008 567 568 569 570  return add(v1, v2, v1); }; template  Thomas Witkowski committed Aug 25, 2008 571  inline Vector operator+(const Vector& v1, const Vector& v2) {  Peter Gottschling committed Feb 15, 2008 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604  Vector result = v1; result += v2; return result; }; template const Vector& operator-=(Vector& v1, const Vector& v2){ return axpy(-1.0, v2, v1); }; template Vector operator-(const Vector& v1, const Vector& v2){ Vector result = v1; result -= v2; return result; }; template inline double norm(const Vector *v) { T *vIt; double result = 0; for (vIt = v->begin(); vIt != v->end(); ++vIt) { result += *vIt * *vIt; } return sqrt(result); }; template void vectorProduct(const Vector& x, const Vector& y, Vector& z) {  Thomas Witkowski committed May 20, 2008 605 606  FUNCNAME("vectorProduct()"); TEST_EXIT_DBG(Global::getGeo(WORLD) == 3)("DIM_OF_WORLD != 3\n");  Peter Gottschling committed Feb 15, 2008 607 608 609 610 611 612 613 614  z[0] = x[1] * y[2] - x[2] * y[1]; z[1] = x[2] * y[0] - x[0] * y[2]; z[2] = x[0] * y[1] - x[1] * y[0]; }; } #endif // AMDIS_MATRIXVECTOR_H