MatrixVectorOperations.h 14.2 KB
Newer Older
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<typename T>
  inline const Vector<T>& mv(const Matrix<T>& m, const Vector<T>& v, Vector<T>& 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");

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<typename T> 
  inline const Vector<T>& add(const Vector<T>& v1, const Vector<T>& v2, Vector<T>& 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");
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<typename T, typename S>
  inline const Vector<T>& mult(const S& scal,
			       const Vector<T>& v, 
			       Vector<T>& result)
  {
    TEST_EXIT_DBG(v.getSize() == result.getSize())("invalid size\n");

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<typename T>
  inline const Vector<T>& add(const Vector<T>& v, const T& scal, Vector<T>& result)
  {
    TEST_EXIT_DBG(v.getSize() == result.getSize())("invalid size\n");
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<typename T, typename S>
  inline const Vector<T>& axpy(const S& a,
			       const Vector<T> &x,
			       Vector<T> &y)
  {
    TEST_EXIT_DBG(x.getSize() == y.getSize())("invalid size\n");
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<typename T>
  inline const Vector<T>& operator*=(const Vector<T>& v, const Matrix<T>& m) 
  {
    return mv(m, v, v);
  }

  /// Matrix vector multiplication: vector := matrix * vector
  template<typename T>
  inline Vector<T> operator*(const Matrix<T>& m, const Vector<T>& v) 
  {
    Vector<T> result(m.getNumCols());
    return mv(m, v, result);
  }

  /// Scalar product: scalar := vector * vector
  template<typename T, typename S> 
  inline typename traits::mult_type<T,S>::type 
  operator*(const Vector<T>& v1, const Vector<S>& v2) 
  {
    typename traits::mult_type<T,S>::type result;
    nullify(result);

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 <typename T, typename S>
  Vector<T>& operator*=(Vector<T>& v, S scal)
  {
    T *vIt;
    for (vIt = v.begin(); vIt != v.end(); ++vIt) 
      *vIt *= scal;
    return v;
  }

  /// vector := vector * scalar (elementwise)
  template <typename T, typename S>
  Vector<typename traits::mult_type<T, S>::type>
  operator*(Vector<T> const& v, S scal) 
  {
    Vector<typename traits::mult_type<T, S>::type> result = v;
    result *= scal;
    return result;
  }

  /// vector /= scalar (elementwise)
  template <typename T, typename S>
  Vector<T>& operator/=(Vector<T>& v, S scal)
  {
    T *vIt;
    for (vIt = v.begin(); vIt != v.end(); ++vIt) 
      *vIt /= scal;
    return v;
  }

  /// vector := vector / scalar (elementwise)
  template <typename T, typename S>
  Vector<typename traits::mult_type<T, S>::type>
  operator/(Vector<T> const& v, S scal) 
  {
    Vector<typename traits::mult_type<T, S>::type> result = v;
    result /= scal;
    return result;
  }
  
  // plus / minus
  // ------------

  /// vector += scalar
  template <typename T, typename S>
  typename boost::enable_if<typename traits::is_scalar<S>::type,
    Vector<T> >::type&
  operator+=(Vector<T>& x, S scal) 
  {
    T *xIt;
    for (xIt = x.begin(); xIt != x.end(); ++xIt) 
      *xIt += scal;
    return x;
  }

  /// vector := vector + scalar
  template <typename T, typename S>
  typename boost::enable_if<typename traits::is_scalar<S>::type,
    Vector<T> >::type
  operator+(Vector<T> result, T scal) 
  {
    result += scal;
    return result;
  }

  /// vector += vector
  template <typename T, typename S>
  Vector<T>& operator+=(Vector<T>& x, const Vector<S>& y) 
  {
    T *xIt;
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 <typename T, typename S>
  Vector<T> operator+(Vector<T> result, const Vector<S>& v2) 
  {
    result += v2;
    return result;
  }

  /// vector -= vector
  template <typename T, typename S>
  Vector<T>& operator-=(Vector<T>& x, const Vector<S>& y)
  {
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 <typename T, typename S>
  Vector<T> operator-(Vector<T> result, const Vector<S>& v2)
  {
    result -= v2;
    return result;
  }
  
  // special operators
  // -----------------

  /// 2-norm of a vector
  template<typename T>
  inline T norm(const Vector<T> *v)
  {
    T result; nullify(result);
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<typename T>
  inline T norm(const Vector<T>& v)
  {
    return norm(&v);
  }

  /// cross-product of two vectors: vector := vector x vector (only in 3d)
  template<typename T>
  void vectorProduct(const Vector<T>& x, 
		     const Vector<T>& y, 
		     Vector<T>& 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<typename T, typename S>
  inline typename traits::mult_type<T,S>::type 
  operator*(const WorldVector<T>& v1, const WorldVector<S>& v2) 
  {
    typename traits::mult_type<T,S>::type result;
    nullify(result);

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<typename T, typename S>
  WorldVector<T>
  operator*(WorldVector<T> const& v, S scal)
  {
    WorldVector<T> result = v;
    result *= scal; // calls operator*=(Vector<T>, S)
    return result;
  }

  /// vector := scalar * vector (elementwise)
  template<typename T, typename S>
  WorldVector<T>
  operator*(S scal, WorldVector<T> const& v)
  {
    WorldVector<T> result = v;
    result *= scal; // calls operator*=(Vector<T>, S)
    return result;
  }

  /// vector := vector / scalar (elementwise)
  template<typename T, typename S>
  WorldVector<T>
  operator/(WorldVector<T> const& v, S scal)
  {
    WorldVector<T> result = v;
    result /= scal;  // calls operator/=(Vector<T>, S)
    return result;
  }
  
  /// matrix *= scalar (elementwise)
//   template<typename T>
//   WorldMatrix<T>& operator*=(WorldMatrix<T>& m, T scal)
//   {
//     for (T* mIt = m.begin(); mIt != m.end(); mIt++)
//       *mIt *= scal;
// 
//     return m;
//   }
  
  /// matrix := matrix * scalar (elementwise)
  template <typename T, typename S>
  WorldMatrix<T>
  operator*(WorldMatrix<T> const& m, S scal)
  {
    WorldMatrix<T> result = m;
    result *= scal; // calls operator*=(Vector<T>, S)
    return result;
  }

  /// matrix := scalar * matrix (elementwise)
  template <typename T, typename S>
  WorldMatrix<T>
  operator*(S scal, WorldMatrix<T> const& m)
  {
    WorldMatrix<T> result = m;
    result *= scal; // calls operator*=(Vector<T>, S)
    return result;
  }

  /// matrix := matrix / scalar (elementwise)
  template <typename T, typename S>
  WorldMatrix<T>
  operator/(WorldMatrix<T> const& m, S scal)
  {
    WorldMatrix<T> result = m;
    result /= scal; // calls operator/=(Vector<T>, S)
    return result;
  }

  /// vector := matrix * vector
  template<typename T>
  WorldVector<T> operator*(const WorldMatrix<T>& M, const WorldVector<T>& v )
  {
    WorldVector<T> res;
    res.multMatrixVec(M,v);
    return res;
  }

  /// matrix := matrix * matrix
  template<typename T>
  WorldVector<WorldVector<T> > 
  operator*(const WorldVector<WorldVector<T> >& A, const WorldVector<WorldVector<T> >& B)
  {
    WorldVector<WorldVector<T> > 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<T> directly
#if 0
  template<typename T>
  WorldVector<T>& operator+=(WorldVector<T>& v1,
			     const WorldVector<T>& v2)
  {
    add(v1, v2, v1);
    return v1;
  }

  template<typename T>
  WorldVector<T>& operator-=(WorldVector<T>& v1,
			     const WorldVector<T>& v2)
  {
    axpy(-1.0, v2, v1);
    return v1;
  }
#endif

  /// vector := vector + vector
  template <typename T, typename S>
  WorldVector<T>&
  operator+=(WorldVector<T>& v1, WorldVector<S> const& v2)
  {
    static_cast<Vector<T>&>(v1) += static_cast<Vector<S> const&>(v2);
    return v1;
  }
  
  /// vector := vector + vector
  template <typename T, typename S>
  WorldVector<typename traits::add_type<T, S>::type>
  operator+(WorldVector<T> result, const WorldVector<S>& v2)
  {
    result += v2; // calls operator+=(Vector<T>, Vector<T>)
    return result;
  }

  /// vector := vector - vector
  template <typename T, typename S>
  WorldVector<typename traits::add_type<T, S>::type>
  operator-(WorldVector<T> result, const WorldVector<S>& v2)
  {
    result -= v2; // calls operator-=(Vector<T>, Vector<T>)
    return result;
  }

  /// matrix += matrix
  template <typename T, typename S>
  WorldMatrix<T>& operator+=(WorldMatrix<T>& m1, const WorldMatrix<S>& m2)
  {
    T* m1It;
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 <typename T, typename S>
  WorldMatrix<T> operator+(WorldMatrix<T> M1, const WorldMatrix<S>& M2 )
  {
    M1 += M2;
    return M1;
  }
  
  /// matrix -= matrix
  template <typename T, typename S>
  WorldMatrix<T>& operator-=(WorldMatrix<T>& m1, const WorldMatrix<S>& m2)
  {
    T *m1It;
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 <typename T, typename S>
  WorldMatrix<T> operator-(WorldMatrix<T> M1, const WorldMatrix<S>& M2 )
  {
    M1 -= M2;
    return M1;
  }
  
  // unary minus operators
  // ---------------------

  /// vector := -vector (elementwise)
  template<typename T>
  WorldVector<T> operator-(WorldVector<T> v)
  {
    v *= -1.0;
    return v;
  }

  /// matrix := -matrix (elementwise)
  template<typename T>
  WorldMatrix<T> operator-(WorldMatrix<T> v)
  {
    v *= -1.0;
    return v;
  }
  
  // comparison operators
  // --------------------

  /// test for less-then (elementwise) up to DBL_TOL
  inline bool operator<(const WorldVector<double>& v1, const WorldVector<double>& 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<double>& v1, const WorldVector<double>& 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<typename T>
  void set_to_zero(WorldVector<WorldVector<T> >& mat)
  {
    nullify(mat);
  }

// NOTE: call norm(Vector<T>) directly
#if 0
  inline double norm(const WorldVector<double>& 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<typename T, GeoIndex d>
  double absteukl(const FixVec<T,d>& a,const FixVec<T,d>& 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