-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathGMatrix.h
409 lines (362 loc) · 7.46 KB
/
GMatrix.h
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
97
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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
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
308
309
310
311
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
#ifndef GMATRIX_H
#define GMATRIX_H
#include <cstddef>
#include <cstring>
#include "Error.h"
#include "MatrixInterface.h"
#include "GVector.h"
namespace flux {
namespace la {
/**
* Generische Matrizen-Klasse.
* Die Elemente der Matrix werden in Fortran-Reihenfolge abgespeichert,
* d.h. spaltenweise von links nach rechts in einem fortlaufenden
* Speicherbereich.
*
* @author Michael Weitzel <[email protected]>
*/
template< typename T > class GMatrix : public MatrixInterface<T>
{
protected:
/** Anzahl der Zeilen */
size_t rows_;
/** Anzahl der Spalten */
size_t cols_;
/** Speicher für Matrixelemente */
T * matrix_storage_;
public:
/**
* Constructor
*/
inline GMatrix()
: rows_(0), cols_(0), matrix_storage_(0) { }
/**
* Constructor.
*
* @param r Anzahl der Zeilen
* @param c Anzahl der Spalten
*/
inline GMatrix(size_t r, size_t c)
: rows_(r), cols_(c)
{
size_t i = rows_*cols_;
matrix_storage_ = new T[i];
while (i) matrix_storage_[--i] = T();
}
/**
* Constructor mit Initialisierung der Matrix.
*
* @param r Anzahl der Zeilen
* @param c Anzahl der Spalten
* @param init Initialisierungswert
*/
inline GMatrix(size_t r, size_t c, T const & init)
: rows_(r), cols_(c)
{
size_t i = rows_*cols_;
matrix_storage_ = new T[i];
while (i) matrix_storage_[--i] = T(init);
}
/**
* Copy-Constructor.
*
* @param copy zu kopierendes Objekt
*/
inline GMatrix(GMatrix const & copy)
: rows_(copy.rows_), cols_(copy.cols_)
{
size_t i = rows_*cols_;
matrix_storage_ = new T[i];
while (i)
{
i--;
matrix_storage_[i] = T(copy.matrix_storage_[i]);
}
}
/**
* Destructor.
*/
virtual inline ~GMatrix() { delete[] matrix_storage_; }
public:
/**
* Zuweisungsoperator.
* Für die Kopie der Elemente wird der Zuweisungsoperator der Klasse T
* verwendet.
*
* @param copy GMatrix mit zu kopierenden Inhalten
*
*/
inline GMatrix & operator= (GMatrix const & copy)
{
size_t i = copy.rows_*copy.cols_;
if (i != rows_*cols_)
{
if (matrix_storage_)
delete[] matrix_storage_;
matrix_storage_ = new T[i];
}
rows_ = copy.rows_;
cols_ = copy.cols_;
while (i>0)
{
i--;
matrix_storage_[i] = copy.matrix_storage_[i];
}
return *this;
}
/**
* Elementzugriff (Lesen/Schreiben).
*
* @param i Zeile
* @param j Spalte
* @return Referenz auf das Objekt mit Index (i,j)
*/
inline T & operator() (size_t i, size_t j)
{
fASSERT(i<rows_ and j<cols_);
return matrix_storage_[j*rows_+i];
}
/**
* Elementzugriff (Schreiben).
*
* @param i Zeile
* @param j Spalte
* @param val Wert
*/
inline virtual void set(size_t i, size_t j, T const & val)
{
fASSERT(i<rows_ and j<cols_);
matrix_storage_[j*rows_+i] = val;
}
/**
* Elementzugriff (Lesen).
*
* @param i Zeile
* @param j Spalte
* @return const-Referenz auf das Objekt mit Index (i,j)
*/
inline virtual T const & get(size_t i, size_t j) const
{
fASSERT(i<rows_ and j<cols_);
return matrix_storage_[j*rows_+i];
}
/**
* Rückgabe der Anzahl der Zeilen.
*
* @return Anzahl der Zeilen
*/
inline virtual size_t rows() const { return rows_; }
/**
* Rückgabe der Anzahl der Spalten.
*
* @return Anzahl der Spalten
*/
inline virtual size_t cols() const { return cols_; }
/**
* Füllt die Matrix mit einem Wert.
*
* @param val Wert
*/
inline virtual void fill(T const & val)
{
for (size_t i = 0; i<rows_*cols_; i++)
matrix_storage_[i] = val;
}
/**
* Gibt die Matrix in einem Fortran-kompatiblen Format zurück.
*
* @return rohe Matrix in Fortran-Ordnung
*/
inline virtual operator T * () const { return matrix_storage_; }
/**
* Spaltenvertauschung
*
* @param j1 erste Spalte
* @param j2 zweite Spalte
*/
inline virtual void swapColumns(size_t j1, size_t j2)
{
fASSERT(j1<cols_ and j2<cols_);
T tmp;
size_t i;
for (i=0; i<rows_; i++)
{
tmp = get(i,j1);
set(i,j1,get(i,j2));
set(i,j2,tmp);
}
}
/**
* Zeilenvertauschung
*
* @param i1 erste Zeile
* @param i2 zweite Zeile
*/
inline virtual void swapRows(size_t i1, size_t i2)
{
fASSERT(i1<rows_ and i2<rows_);
T tmp;
size_t j;
for (j=0; j<cols_; j++)
{
tmp = get(i1,j);
set(i1,j,get(i2,j));
set(i2,j,tmp);
}
}
virtual void transpose()
{
size_t i,j;
if (rows_ == cols_)
{
T tmp;
for (i=1; i<rows_; i++)
for (j=0; j<i; j++)
{
tmp = get(i,j);
set(i,j,get(j,i));
set(j,i,tmp);
}
}
else
{
GMatrix< T > tmpM(*this);
size_t stmp = rows_;
rows_ = cols_;
cols_ = stmp;
for (i=0; i<cols_; i++)
for (j=0; j<rows_; j++)
set(j,i,tmpM.get(i,j));
}
}
inline virtual T * getRow(size_t i, T * row = 0) const
{
fASSERT(i<rows_);
if (row == 0)
row = new T[cols_];
for (size_t j=0; j<cols_; j++)
row[j] = matrix_storage_[j*rows_+i];
return row;
}
inline virtual T * getCol(size_t j, T * col = 0) const
{
fASSERT(j<cols_);
if (col == 0)
col = new T[rows_];
for (size_t i=0; i<rows_; i++)
col[i] = matrix_storage_[j*rows_+i];
return col;
}
inline virtual void setRow(size_t i, T const * row)
{
fASSERT(i<rows_);
if (row == 0) return;
for (size_t j=0; j<cols_; j++)
matrix_storage_[j*rows_+i] = row[j];
}
inline virtual void setCol(size_t j, T const * col)
{
fASSERT(j<cols_);
if (col == 0) return;
for (size_t i=0; i<rows_; i++)
matrix_storage_[j*rows_+i] = col[i];
}
GMatrix< T > operator+ (GMatrix< T > const & Rval) const
{
size_t i,j;
GMatrix< T > C(rows(), cols());
for (i=0; i<rows_; i++)
for (j=0; j<cols_; j++)
C.set(i,j,get(i,j) + Rval.get(i,j));
return C;
}
GMatrix< T > & operator+= (GMatrix< T > const & Rval)
{
size_t i,j;
for (i=0; i<rows_; i++)
for (j=0; j<cols_; j++)
set(i,j,get(i,j) + Rval.get(i,j));
return *this;
}
GMatrix< T > operator- (GMatrix< T > const & Rval) const
{
size_t i,j;
GMatrix< T > C(rows(), cols());
for (i=0; i<rows_; i++)
for (j=0; j<cols(); j++)
C.set(i,j,get(i,j) - Rval.get(i,j));
return C;
}
GMatrix< T > & operator-= (GMatrix< T > const & Rval)
{
size_t i,j;
for (i=0; i<rows_; i++)
for (j=0; j<cols_; j++)
set(i,j,get(i,j) - Rval.get(i,j));
return *this;
}
GMatrix< T > operator* (GMatrix< T > const & Rval) const
{
size_t i,j,k;
GMatrix< T > C(rows_,Rval.cols_);
for (i=0; i<C.rows_; i++)
for (j=0; j<C.cols_; j++)
{
T & c_ij = C(i,j);
for (k=0; k<cols_; k++)
c_ij = c_ij + get(i,k) * Rval.get(k,j);
}
return C;
}
GVector< T > operator* (GVector< T > const & rval) const
{
size_t i,k;
GVector< T > c(rows_);
for (i=0; i<rows_; i++)
{
T & c_i = c(i);
for (k=0; k<cols_; k++)
c_i = c_i + get(i,k) * rval.get(k);
}
return c;
}
GMatrix< T > & operator*= (double rval)
{
size_t i,j;
for (i=0; i<rows_; i++)
for (j=0; j<cols_; j++)
set(i,j,get(i,j) * rval);
return *this;
}
GMatrix< T > operator* (double rval) const
{
size_t i,j;
GMatrix< T > M(rows_,cols_);
for (i=0; i<rows_; i++)
for (j=0; j < cols(); j++)
M.set(i, j, get(i, j) * rval);
return M;
}
GMatrix< T > & operator/= (double rval)
{
size_t i,j;
fASSERT(rval != 0.);
for (i=0; i<rows_; i++)
for (j=0; j<cols(); j++)
set(i,j,get(i,j) / rval);
return *this;
}
GMatrix< T > operator/ (double rval) const
{
size_t i,j;
GMatrix< T > M(rows(), cols());
fASSERT(rval != 0.);
for (i=0; i<rows_; i++)
for (j=0; j<cols_; j++)
M.set(i,j,get(i,j) / rval);
return M;
}
};
} // namespace flux::la
} // namespace flux
#endif