MatrixVectorOperations.h 14.2 KB
 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 ``````/****************************************************************************** * * AMDiS - Adaptive multidimensional simulations * * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved. * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis * * Authors: * Simon Vey, Thomas Witkowski, Andreas Naumann, Simon Praetorius, et al. * * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. * * * This file is part of AMDiS * * See also license.opensource.txt in the distribution. * ******************************************************************************/ /** \file MatrixVectorOperations.h */ #ifndef AMDIS_MATVEC_OPERATIONS_H #define AMDIS_MATVEC_OPERATIONS_H #include "Traits.h" namespace AMDiS { // --------------------------------------------------------------------------- // Operations with Vector and Matrix /// Matrix vector multiplication. template inline const Vector& mv(const Matrix& m, const Vector& v, Vector& result) { TEST_EXIT_DBG(m.getNumCols() == v.getSize())("m and v not compatible\n"); TEST_EXIT_DBG(v.getSize() == result.getSize())("wrong result size\n"); `````` Praetorius, Simon committed Mar 18, 2015 42 43 44 `````` T *resultIt; T const* mIt; T const* vIt; `````` 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 `````` for (resultIt = result.begin(), mIt = m.begin(); resultIt != result.end(); ++resultIt) { *resultIt = 0; for (vIt = v.begin(); vIt != v.end(); ++vIt, ++mIt) *resultIt += *mIt * *vIt; } return result; } /// Vector addition. template inline const Vector& add(const Vector& v1, const Vector& v2, Vector& result) { 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"); `````` Praetorius, Simon committed Mar 18, 2015 63 64 65 `````` T const* v1It; T const* v2It; T* resultIt; `````` 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 `````` for (v1It = v1.begin(), v2It = v2.begin(), resultIt = result.begin(); v1It != v1.end(); ++v1It, ++v2It, ++resultIt) *resultIt = *v1It + *v2It; return result; } /// scalar * vector template inline const Vector& mult(const S& scal, const Vector& v, Vector& result) { TEST_EXIT_DBG(v.getSize() == result.getSize())("invalid size\n"); `````` Praetorius, Simon committed Mar 18, 2015 82 83 `````` T const* vIt; T* resultIt; `````` 84 85 86 87 88 89 90 91 92 93 94 95 96 `````` for (vIt = v.begin(), resultIt = result.begin(); vIt != v.end(); ++vIt, ++resultIt) *resultIt = scal * *vIt; return result; } /// vector + scalar template inline const Vector& add(const Vector& v, const T& scal, Vector& result) { TEST_EXIT_DBG(v.getSize() == result.getSize())("invalid size\n"); `````` Praetorius, Simon committed Mar 18, 2015 97 98 `````` T const* vIt; T* resultIt; `````` 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 `````` for (vIt = v.begin(), resultIt = result.begin(); vIt != v.end(); ++vIt, ++resultIt) *resultIt = *vIt + scal; return result; } /// y = a * x + y. template inline const Vector& axpy(const S& a, const Vector &x, Vector &y) { TEST_EXIT_DBG(x.getSize() == y.getSize())("invalid size\n"); `````` Praetorius, Simon committed Mar 18, 2015 114 115 `````` T const* xIt; T* yIt; `````` 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 `````` for (xIt = x.begin(), yIt = y.begin(); xIt != x.end(); ++xIt, ++yIt) *yIt += a * *xIt; return y; } // times / divides // --------------- /// Matrix vector multiplication: vector := matrix * vector template inline const Vector& operator*=(const Vector& v, const Matrix& m) { return mv(m, v, v); } /// Matrix vector multiplication: vector := matrix * vector template inline Vector operator*(const Matrix& m, const Vector& v) { Vector result(m.getNumCols()); return mv(m, v, result); } /// Scalar product: scalar := vector * vector template inline typename traits::mult_type::type operator*(const Vector& v1, const Vector& v2) { typename traits::mult_type::type result; nullify(result); `````` Praetorius, Simon committed Mar 18, 2015 150 151 `````` T const* v1It; S const* v2It; `````` 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 194 195 196 197 198 199 200 201 202 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 `````` for (v1It = v1.begin(), v2It = v2.begin(); v1It != v1.end(); ++v1It, ++v2It) result += *v1It * *v2It; return result; } /// vector *= scalar (elementwise) template Vector& operator*=(Vector& v, S scal) { T *vIt; for (vIt = v.begin(); vIt != v.end(); ++vIt) *vIt *= scal; return v; } /// vector := vector * scalar (elementwise) template Vector::type> operator*(Vector const& v, S scal) { Vector::type> result = v; result *= scal; return result; } /// vector /= scalar (elementwise) template Vector& operator/=(Vector& v, S scal) { T *vIt; for (vIt = v.begin(); vIt != v.end(); ++vIt) *vIt /= scal; return v; } /// vector := vector / scalar (elementwise) template Vector::type> operator/(Vector const& v, S scal) { Vector::type> result = v; result /= scal; return result; } // plus / minus // ------------ /// vector += scalar template typename boost::enable_if::type, Vector >::type& operator+=(Vector& x, S scal) { T *xIt; for (xIt = x.begin(); xIt != x.end(); ++xIt) *xIt += scal; return x; } /// vector := vector + scalar template typename boost::enable_if::type, Vector >::type operator+(Vector result, T scal) { result += scal; return result; } /// vector += vector template Vector& operator+=(Vector& x, const Vector& y) { T *xIt; `````` Praetorius, Simon committed Mar 18, 2015 230 `````` S const* yIt; `````` 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 `````` for (xIt = x.begin(), yIt = y.begin(); xIt != x.end(); ++xIt, ++yIt) *xIt += *yIt; return x; } /// vector := vector + vector template Vector operator+(Vector result, const Vector& v2) { result += v2; return result; } /// vector -= vector template Vector& operator-=(Vector& x, const Vector& y) { `````` Praetorius, Simon committed Mar 18, 2015 248 249 `````` T* xIt; S const* yIt; `````` 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 `````` for (xIt = x.begin(), yIt = y.begin(); xIt != x.end(); ++xIt, ++yIt) *xIt -= *yIt; return x; } /// vector := vector - vector template Vector operator-(Vector result, const Vector& v2) { result -= v2; return result; } // special operators // ----------------- /// 2-norm of a vector template inline T norm(const Vector *v) { T result; nullify(result); `````` Praetorius, Simon committed Mar 18, 2015 271 `````` for (T const* vIt = v->begin(); vIt != v->end(); ++vIt) `````` 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 308 309 `````` result += *vIt * *vIt; return std::sqrt(result); } /// 2-norm of a vector template inline T norm(const Vector& v) { return norm(&v); } /// cross-product of two vectors: vector := vector x vector (only in 3d) template void vectorProduct(const Vector& x, const Vector& y, Vector& z) { FUNCNAME_DBG("vectorProduct()"); TEST_EXIT_DBG(Global::getGeo(WORLD) == 3)("DIM_OF_WORLD != 3\n"); 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]; } // --------------------------------------------------------------------------- // Operations with WorldVector and WorldMatrix // times / divides // --------------- /// Scalar product: scalar := vector * vector template inline typename traits::mult_type::type operator*(const WorldVector& v1, const WorldVector& v2) { typename traits::mult_type::type result; nullify(result); `````` Praetorius, Simon committed Mar 18, 2015 310 311 `````` T const* v1It; S const* v2It; `````` 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 `````` for (v1It = v1.begin(), v2It = v2.begin(); v1It != v1.end(); ++v1It, ++v2It) result += *v1It * *v2It; return result; } /// vector := vector * scalar (elementwise) template WorldVector operator*(WorldVector const& v, S scal) { WorldVector result = v; result *= scal; // calls operator*=(Vector, S) return result; } /// vector := scalar * vector (elementwise) template WorldVector operator*(S scal, WorldVector const& v) { WorldVector result = v; result *= scal; // calls operator*=(Vector, S) return result; } /// vector := vector / scalar (elementwise) template WorldVector operator/(WorldVector const& v, S scal) { WorldVector result = v; result /= scal; // calls operator/=(Vector, S) return result; } /// matrix *= scalar (elementwise) // template // WorldMatrix& operator*=(WorldMatrix& m, T scal) // { // for (T* mIt = m.begin(); mIt != m.end(); mIt++) // *mIt *= scal; // // return m; // } /// matrix := matrix * scalar (elementwise) template WorldMatrix operator*(WorldMatrix const& m, S scal) { WorldMatrix result = m; result *= scal; // calls operator*=(Vector, S) return result; } /// matrix := scalar * matrix (elementwise) template WorldMatrix operator*(S scal, WorldMatrix const& m) { WorldMatrix result = m; result *= scal; // calls operator*=(Vector, S) return result; } /// matrix := matrix / scalar (elementwise) template WorldMatrix operator/(WorldMatrix const& m, S scal) { WorldMatrix result = m; result /= scal; // calls operator/=(Vector, S) return result; } /// vector := matrix * vector template WorldVector operator*(const WorldMatrix& M, const WorldVector& v ) { WorldVector res; res.multMatrixVec(M,v); return res; } /// matrix := matrix * matrix template WorldVector > operator*(const WorldVector >& A, const WorldVector >& B) { WorldVector > result; nullify(result); for (size_t r = 0; r < num_rows(A); r++) for (size_t c = 0; c < num_cols(A); c++) for (size_t i = 0; i < num_cols(A); i++) result[r][c] += A[r][i] * B[i][c]; return result; } // plus / minus // ------------ // NOTE: call operators of Vector directly #if 0 template WorldVector& operator+=(WorldVector& v1, const WorldVector& v2) { add(v1, v2, v1); return v1; } template WorldVector& operator-=(WorldVector& v1, const WorldVector& v2) { axpy(-1.0, v2, v1); return v1; } #endif /// vector := vector + vector template WorldVector& operator+=(WorldVector& v1, WorldVector const& v2) { static_cast&>(v1) += static_cast const&>(v2); return v1; } /// vector := vector + vector template WorldVector::type> operator+(WorldVector result, const WorldVector& v2) { result += v2; // calls operator+=(Vector, Vector) return result; } /// vector := vector - vector template WorldVector::type> operator-(WorldVector result, const WorldVector& v2) { result -= v2; // calls operator-=(Vector, Vector) return result; } /// matrix += matrix template WorldMatrix& operator+=(WorldMatrix& m1, const WorldMatrix& m2) { T* m1It; `````` Praetorius, Simon committed Mar 18, 2015 467 `````` S const* m2It; `````` 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 `````` for (m1It = m1.begin(), m2It = m2.begin(); m1It != m1.end(); m1It++, m2It++) *m1It += *m2It; return m1; } /// matrix := matrix + matrix template WorldMatrix operator+(WorldMatrix M1, const WorldMatrix& M2 ) { M1 += M2; return M1; } /// matrix -= matrix template WorldMatrix& operator-=(WorldMatrix& m1, const WorldMatrix& m2) { T *m1It; `````` Praetorius, Simon committed Mar 18, 2015 489 `````` S const* m2It; `````` 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 `````` for (m1It = m1.begin(), m2It = m2.begin(); m1It != m1.end(); m1It++, m2It++) *m1It -= *m2It; return m1; } /// matrix := matrix - matrix template WorldMatrix operator-(WorldMatrix M1, const WorldMatrix& M2 ) { M1 -= M2; return M1; } // unary minus operators // --------------------- /// vector := -vector (elementwise) template WorldVector operator-(WorldVector v) { v *= -1.0; return v; } /// matrix := -matrix (elementwise) template WorldMatrix operator-(WorldMatrix v) { v *= -1.0; return v; } // comparison operators // -------------------- /// test for less-then (elementwise) up to DBL_TOL inline bool operator<(const WorldVector& v1, const WorldVector& v2) { int dow = Global::getGeo(WORLD); for (int i = 0; i < dow; i++) { if (std::abs(v1[i] - v2[i]) < DBL_TOL) continue; return v1[i] < v2[i]; } return false; } /// test for equality (elementwise) up to DBL_TOL inline bool operator==(const WorldVector& v1, const WorldVector& v2) { int dow = Global::getGeo(WORLD); for (int i = 0; i < dow; i++) if (std::abs(v1[i] - v2[i]) > DBL_TOL) return false; return true; } // special operators // ----------------- /// wrapper for nullify template void set_to_zero(WorldVector >& mat) { nullify(mat); } // NOTE: call norm(Vector) directly #if 0 inline double norm(const WorldVector& v) { double val = 0.0; for (int i = 0; i < Global::getGeo(WORLD); i++) val += v[i] * v[i]; return sqrt(val); } #endif /// returns the euclidian distance of a and b template double absteukl(const FixVec& a,const FixVec& b) { double erg = 0.0; for (int i = 0; i < a.getSize(); ++i) erg += sqr(a[i] - b[i]); return std::sqrt(erg); } } // end namespace AMDiS #endif // AMDIS_MATVEC_OPERATIONS_H ``````