MatrixVector.h 14.4 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
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 <vector>
#include "Global.h"
#include "MemoryManager.h"
#include "Serializable.h"

namespace AMDiS {

  template<typename T> class DOFVector;

  // ============================================================================
  // ===== class Vector =========================================================
  // ============================================================================

  /** \brief
   * Class for efficient vector operations of fixed size numerical vectors.
   */
  template<typename T>
  class Vector : public Serializable
  {
  public:
    MEMORY_MANAGED(Vector<T>);

    /** \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<T>& 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<T>& operator=(const Vector<T>& rhs) {
97
      TEST_EXIT_DBG(rhs.size == size)("invalid size\n");
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<T>& operator=(const T& scal) {
      T *thisIt;
      for(thisIt = this->begin();
	  thisIt != this->end();
	  ++thisIt)
	{
	  *thisIt = scal;
	}
      return *this;
    };

    /** \brief
     * Assignement operator
     */
    inline const Vector<T>& 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<T>& set(const T& scal) {
      return *this = scal;
    };

    /** \brief
     * Sets all entries.
     */
    inline const Vector<T>& 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<T>& 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<T>& rhs) const {
      return !(*this==rhs);
    }

    /** \brief
     * Access to the i-th vector element.
     */
    inline T& operator[](int i) {
194
      TEST_EXIT_DBG(i < size && i >= 0)("invalid index\n");
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 {
202
      TEST_EXIT_DBG(i < size && i >= 0)("invalid index\n");
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 {
235
      std::cout << this->size << " vector: " << std::endl;
236
      for (int i = 0; i < size; i++) {
237
	std::cout << this->valArray[i] << " ";
238
      }
239
      std::cout << std::endl;
240
241
242
243
    };

    // ===== Serializable implementation =====
  
244
    void serialize(std::ostream &out) {
245
246
247
248
      out.write(reinterpret_cast<const char*>(&size), sizeof(int));
      out.write(reinterpret_cast<const char*>(valArray), size * sizeof(T));
    };

249
    void deserialize(std::istream &in) {
250
251
252
253
      in.read(reinterpret_cast<char*>(&size), sizeof(int));
      in.read(reinterpret_cast<char*>(valArray), size * sizeof(T));
    };

254
    std::string getTypeName() const { 
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<typename T>
  class Matrix : public Vector<T>
  {
  public:
    MEMORY_MANAGED(Matrix<T>);

    /** \brief
     * Constructor.
     */
    Matrix(int r, int c)
      : Vector<T>(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<T>::resize(newRows * newCols);
	rows = newRows;
	cols = newCols;
      }
    };

    /** \brief
     * Assignement operator.
     */
308
    inline const Matrix<T>& operator=(const T& scal) {
309
310
311
      return static_cast<const Matrix<T>&>(Vector<T>::operator=(scal));
    };

312
    inline bool operator==(const Matrix<T>& rhs) const {
313
314
315
316
317
318
319
320
      if (rows != rhs.getNumRows()) return false;
      if (cols != rhs.getNumCols()) return false;
      return Vector<T>::operator == (rhs);
    };

    /** \brief
     * Comparison operator.
     */
321
322
    inline bool operator!=(const Matrix<T>& rhs) const {
      return !(*this == rhs);
323
324
325
326
327
    }

    /** \brief
     * Acces to i-th matrix row.
     */
328
    inline T *operator[](int i) {
329
330
331
332
333
334
      return this->valArray + cols * i;
    };

    /** \brief
     * Acces to i-th matrix row for constant matrices.
     */
335
    inline const T *operator[](int i) const {
336
337
338
339
340
341
      return this->valArray + cols * i;
    };

    /** \brief
     * Returns \ref rows.
     */
342
    inline int getNumRows() const { 
343
344
345
346
347
348
      return rows; 
    };

    /** \brief
     * Return \ref cols.
     */
349
    inline int getNumCols() const { 
350
351
352
353
354
355
      return cols; 
    };

    /** \brief
     * Returns \ref rows.
     */
356
    inline int getSize() const {
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 {
368
      std::cout << this->rows << " x " << this->cols << " matrix: " << std::endl;
369
370
      for (int i = 0; i < rows; i++) {
	for (int j = 0; j < cols; j++) {
371
	  std::cout << this->valArray[i * cols + j] << " ";
372
	}
373
	std::cout << std::endl;
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<typename T>
  inline const Vector<T>& mv(const Matrix<T>& m, const Vector<T>& v, Vector<T>& result)
  {
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");
397
398
399
400
401

    T *resultIt, *mIt, *vIt;

    for (resultIt = result.begin(), mIt = m.begin(); 
	 resultIt != result.end(); 
402
403
404
405
	 ++resultIt) {
      *resultIt = 0;
      for (vIt = v.begin(); vIt != v.end(); ++vIt, ++mIt) {
	*resultIt += *mIt * *vIt;
406
      }
407
    }
408
409
410
411
    return result;
  };

  /** \brief
412
   * Computes x(Ay)^T, with x and y vectors, and A a matrix.
413
414
   */
  template<typename T>
415
  inline T xAy(const Vector<T>& x, const Matrix<T>& a, const Vector<T>& y) 
416
  {
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<typename T>
  inline const Vector<T>& operator*=(const Vector<T>& v, const Matrix<T>& m) {
440
441
442
443
444
445
446
    return mv(m, v, v);
  };

  /** \brief
   * Matrix vector multiplication.
   */
  template<typename T>
447
  inline Vector<T> operator*(const Matrix<T>& m, const Vector<T>& v) {
448
449
450
451
452
453
454
455
456
    Vector<T> result(v.getSize());
    return mv(m, v, result);
  };


  /** \brief
   * Scalar product.
   */
  template<typename T> 
457
  inline double operator*(const Vector<T>& v1, const Vector<T>& v2) 
458
  {
459
    double result = 0.0;
460
461
462
463

    T *v1It, *v2It;
    for (v1It = v1.begin(), v2It = v2.begin();
	 v1It != v1.end();
464
465
466
	 ++v1It, ++v2It) {
      result += *v1It * *v2It;
    }
467
468
469
470
471
472
473
474
475
476

    return result;
  };

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

500
501
502
    T *vIt, *resultIt;
    for (vIt = v.begin(), resultIt = result.begin();
	 vIt != v.end();
503
504
505
	 ++vIt, ++resultIt) {
      *resultIt = scal * *vIt;
    }
506
507
508
509
510
511
512
513
514
515

    return result;
  };

  /** \brief
   * vector + scalar
   */
  template<typename T>
  inline const Vector<T>& add(const Vector<T>& v, const T& scal, Vector<T>& result)
  {
516
    TEST_EXIT_DBG(v.getSize() == result.getSize())("invalid size\n");
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<typename T>
  inline const Vector<T>& axpy(const T& a,
			       const Vector<T> &x,
			       Vector<T> &y)
  {
536
    TEST_EXIT_DBG(x.getSize() == y.getSize())("invalid size\n");
537
538
539
    T *xIt, *yIt;
    for (xIt = x.begin(), yIt = y.begin();
	 xIt != x.end();
540
541
542
	 ++xIt, ++yIt) {
      *yIt += a * *xIt;
    }
543
544
545
546
547

    return y;
  };

  template<typename T>
548
  inline const Vector<T>& operator*=(Vector<T>& v, const T& scal) {
549
550
551
552
    return mult(scal, v, v);
  };

  template<typename T>
553
  inline Vector<T> operator*(const Vector<T>& v, const T& scal) {
554
555
556
557
558
559
    Vector<T> result = v;
    result *= scal;
    return result;
  };

  template<typename T>
560
  inline const Vector<T>& operator+(const Vector<T>& v1, const T& scal) {
561
562
563
564
565
    Vector<T> result(v1.getSize());
    return add(v1, scal, result);
  };

  template<typename T>
566
  inline const Vector<T>& operator+=(Vector<T>& v1, const Vector<T>& v2) {
567
568
569
570
    return add(v1, v2, v1);
  };

  template<typename T>
571
  inline Vector<T> operator+(const Vector<T>& v1, const Vector<T>& v2) {
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<T> result = v1;
    result += v2;
    return result;
  };

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

  template<typename T>
  Vector<T> operator-(const Vector<T>& v1, const Vector<T>& v2){
    Vector<T> result = v1;
    result -= v2;
    return result;
  };

  template<typename T>
  inline double norm(const Vector<T> *v)
  {
    T *vIt;
    double result = 0;
    for (vIt = v->begin(); vIt != v->end(); ++vIt) {
      result += *vIt * *vIt;
    }
    return sqrt(result);
  };

  template<typename T>
  void vectorProduct(const Vector<T>& x, 
		     const Vector<T>& y, 
		     Vector<T>& z)
  {
605
606
    FUNCNAME("vectorProduct()");
    TEST_EXIT_DBG(Global::getGeo(WORLD) == 3)("DIM_OF_WORLD != 3\n");
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