GMatrix.hpp
11.9 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
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
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
/***************************************************************************
* GMatrix.hpp - matrix class *
* ----------------------------------------------------------------------- *
* copyright : (C) 2006 by Jurgen Knodlseder *
* ----------------------------------------------------------------------- *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef GMATRIX_HPP
#define GMATRIX_HPP
/* __ Includes ___________________________________________________________ */
#include "GException.hpp"
#include "GVector.hpp"
/* __ Namespaces _________________________________________________________ */
using namespace std;
/* __ Enumerators ________________________________________________________ */
/* __ Structures _________________________________________________________ */
/* __ Prototypes _________________________________________________________ */
/***************************************************************************
* GMatrix class definition *
***************************************************************************/
class GMatrix {
// Operator friends
friend GMatrix operator+ (const double& a, const GMatrix& b);
friend GMatrix operator- (const double& a, const GMatrix& b);
friend GMatrix operator* (const double& a, const GMatrix& b);
// I/O friends
friend ostream& operator<< (ostream& os, const GMatrix& m);
// Friend functions
friend GMatrix transpose(const GMatrix& m);
friend GMatrix fabs(const GMatrix& m);
public:
// Constructors and destructors
GMatrix(unsigned rows, unsigned cols);
GMatrix(const GMatrix& m);
virtual ~GMatrix();
// Matrix element access operators
virtual double& operator() (unsigned row, unsigned col);
virtual const double& operator() (unsigned row, unsigned col) const;
// Matrix assignment operators
GMatrix& operator= (const GMatrix& m);
GMatrix& operator= (const double& d);
// Generic matrix operators (structure independent)
virtual GMatrix& operator+= (const GMatrix& m);
virtual GMatrix& operator-= (const GMatrix& m);
virtual GMatrix& operator+= (const double& d);
virtual GMatrix& operator-= (const double& d);
virtual GMatrix& operator*= (const double& d);
virtual GMatrix& operator/= (const double& d);
virtual int operator== (const GMatrix& m) const;
virtual int operator!= (const GMatrix& m) const;
// Specific matrix operators (structure dependent)
GMatrix operator+ (const GMatrix& m) const;
GMatrix operator- (const GMatrix& m) const;
GMatrix operator* (const GMatrix& m) const;
virtual GVector operator* (const GVector& v) const;
GMatrix operator+ (const double& d) const;
GMatrix operator- (const double& d) const;
GMatrix operator* (const double& d) const;
GMatrix operator/ (const double& d) const;
GMatrix operator- () const;
// Generic matrix functions (structure independent)
virtual double min() const; // Minimum physical element
virtual double max() const; // Maximum physical element
virtual double sum() const; // Sum of matrix elements
virtual unsigned rows() const; // Return number of rows
virtual unsigned cols() const; // Return number of columns
// Specific matrix functions (structure dependent)
virtual void transpose(); // Inplace transpose of matrix
// Exception: Matrix indices out of range
class out_of_range : public GException {
public:
out_of_range(string origin, unsigned row, unsigned col,
unsigned rows, unsigned cols);
};
// Exception: Vector - Matrix mismatch
class vec_mat_mismatch : public GException {
public:
vec_mat_mismatch(string origin, unsigned num, unsigned rows, unsigned cols);
};
// Exception: Matrix dimensions mismatch (addition, etc.)
class dim_add_mismatch : public GException {
public:
dim_add_mismatch(string origin, unsigned rows1, unsigned rows2,
unsigned cols1, unsigned cols2);
};
// Exception: Matrix dimensions mismatch (multiplication)
class dim_mult_mismatch : public GException {
public:
dim_mult_mismatch(string origin, unsigned cols, unsigned rows);
};
protected:
// Void constructor to be used by derived classes which have own constructor
GMatrix() { }
// Private data area
unsigned m_rows; // Number of rows
unsigned m_cols; // Number of columns
unsigned m_elements; // Number of (physical) elements in matrix
unsigned* m_colstart; // Column start indices (m_cols+1)
double* m_data; // Matrix data
};
/***************************************************************************
* Generic inline members (to be inherited) *
***************************************************************************/
// Matrix unary addition operator: GMatrix += GMatrix
inline
GMatrix& GMatrix::operator+= (const GMatrix& m)
{
if (m_rows != m.m_rows || m_cols != m.m_cols)
throw dim_add_mismatch("GMatrix += operator", m_rows, m.m_rows, m_cols, m.m_cols);
for (unsigned i = 0; i < m_elements; ++i)
m_data[i] += m.m_data[i];
return *this;
}
// Matrix unary subtraction operator: GMatrix -= GMatrix
inline
GMatrix& GMatrix::operator-= (const GMatrix& m)
{
if (m_rows != m.m_rows || m_cols != m.m_cols)
throw dim_add_mismatch("GMatrix -= operator", m_rows, m.m_rows, m_cols, m.m_cols);
for (unsigned i = 0; i < m_elements; ++i)
m_data[i] -= m.m_data[i];
return *this;
}
// Scalar unary addition operator: GMatrix += double
inline
GMatrix& GMatrix::operator+= (const double& d)
{
for (unsigned i = 0; i < m_elements; ++i)
m_data[i] += d;
return *this;
}
// Scalar unary subtraction operator: GMatrix -= double
inline
GMatrix& GMatrix::operator-= (const double& d)
{
for (unsigned i = 0; i < m_elements; ++i)
m_data[i] -= d;
return *this;
}
// Scalar unary multiplication operator: GMatrix *= double
inline
GMatrix& GMatrix::operator*= (const double& d)
{
for (unsigned i = 0; i < m_elements; ++i)
m_data[i] *= d;
return *this;
}
// Scalar unary division operator: GMatrix /= double
inline
GMatrix& GMatrix::operator/= (const double& d)
{
for (unsigned i = 0; i < m_elements; ++i)
m_data[i] /= d;
return *this;
}
// Equality operator: GMatrix == GMatrix
inline
int GMatrix::operator== (const GMatrix &m) const
{
int result = 1;
if (m_rows == m.m_rows && m_cols == m.m_cols && m_elements == m.m_elements) {
for (unsigned i = 0; i < m_elements; ++i) {
if (m_data[i] != m.m_data[i]) {
result = 0;
break;
}
}
}
else
result = 0;
return result;
}
// Non equality operator: GMatrix != GMatrix
inline
int GMatrix::operator!= (const GMatrix &m) const
{
int result = 0;
if (m_rows == m.m_rows && m_cols == m.m_cols && m_elements == m.m_elements) {
for (unsigned i = 0; i < m_elements; ++i) {
if (m_data[i] != m.m_data[i]) {
result = 1;
break;
}
}
}
else
result = 1;
return result;
}
// Matrix minimum: GMatrix.min()
inline
double GMatrix::min() const
{
double result = m_data[0];
for (unsigned i = 1; i < m_elements; ++i) {
if (m_data[i] < result)
result = m_data[i];
}
return result;
}
// Matrix maximum: GMatrix.max()
inline
double GMatrix::max() const
{
double result = m_data[0];
for (unsigned i = 1; i < m_elements; ++i) {
if (m_data[i] > result)
result = m_data[i];
}
return result;
}
// Matrix sum: GMatrix.sum()
inline
double GMatrix::sum() const
{
double result = 0.0;
for (unsigned row = 0; row < m_rows; ++row) {
for (unsigned col = 0; col < m_cols; ++col)
result += (*this)(row,col);
}
return result;
}
// Return number of rows: GMatrix.rows()
inline
unsigned GMatrix::rows() const
{
return m_rows;
}
// Return number of columns: GMatrix.cols()
inline
unsigned GMatrix::cols() const
{
return m_cols;
}
/***************************************************************************
* Specific inline members (may need specific function for derived class) *
***************************************************************************/
// Scalar assignment operator: GMatrix = double
inline
GMatrix& GMatrix::operator= (const double& d)
{
for (unsigned i = 0; i < m_elements; ++i)
m_data[i] = d;
return *this;
}
// Matrix element access operator: GMatrix(row,col)
inline
double& GMatrix::operator() (unsigned row, unsigned col)
{
#if defined(G_RANGE_CHECK)
if (row >= m_rows || col >= m_cols)
throw out_of_range("GMatrix access", row, col, m_rows, m_cols);
#endif
return m_data[m_colstart[col]+row];
}
// Matrix element access operator (const version): GMatrix(row,col)
inline
const double& GMatrix::operator() (unsigned row, unsigned col) const
{
#if defined(G_RANGE_CHECK)
if (row >= m_rows || col >= m_cols)
throw out_of_range("GMatrix access", row, col, m_rows, m_cols);
#endif
return m_data[m_colstart[col]+row];
}
// Matrix addition: GMatrix + GMatrix
inline
GMatrix GMatrix::operator+ (const GMatrix& m) const
{
GMatrix result = *this;
result += m;
return result;
}
// Matrix subtraction: GMatrix - GMatrix
inline
GMatrix GMatrix::operator- (const GMatrix& m) const
{
GMatrix result = *this;
result -= m;
return result;
}
// Matrix scaling: GMatrix + double
inline
GMatrix GMatrix::operator+ (const double& d) const
{
GMatrix result = (*this);
result += d;
return result;
}
// Matrix scaling: GMatrix - double
inline
GMatrix GMatrix::operator- (const double& d) const
{
GMatrix result = (*this);
result -= d;
return result;
}
// Matrix scaling: GMatrix * double
inline
GMatrix GMatrix::operator* (const double& d) const
{
GMatrix result = (*this);
result *= d;
return result;
}
// Matrix inverse scaling: GMatrix * double
inline
GMatrix GMatrix::operator/ (const double& d) const
{
GMatrix result = (*this);
result /= d;
return result;
}
// Unary minus operator: -GMatrix
inline
GMatrix GMatrix::operator- ( ) const
{
GMatrix result = *this;
for (unsigned i = 0; i < m_elements; ++i)
result.m_data[i] = -result.m_data[i];
return result;
}
// Matrix transpose: GMatrix.transpose()
inline
void GMatrix::transpose()
{
for (unsigned row = 0; row < m_rows; ++row) {
double* ptr0 = m_data + row;
double* ptr2 = m_data + m_colstart[row]+row+1;
for (unsigned col = row+1; col < m_cols; ++col) {
double* ptr1 = ptr0 + m_colstart[col];
double swap = *ptr1;
*ptr1 = *ptr2;
*ptr2++ = swap;
}
}
}
/***************************************************************************
* Inline friends *
***************************************************************************/
// Matrix addition: double + GMatrix
inline
GMatrix operator+ (const double& a, const GMatrix& b)
{
GMatrix result = b;
result += a;
return result;
}
// Matrix subtraction: double - GMatrix
inline
GMatrix operator- (const double& a, const GMatrix& b)
{
GMatrix result = b;
result -= a;
return result;
}
// Matrix scaling: double * GMatrix
inline
GMatrix operator* (const double& a, const GMatrix& b)
{
GMatrix result = b;
result *= a;
return result;
}
// Matrix transpose: transpose(GMatrix)
inline
GMatrix transpose(const GMatrix& m)
{
GMatrix result = m;
result.transpose();
return result;
}
// Matrix absolute: fabs(GMatrix)
inline
GMatrix fabs(const GMatrix& m)
{
GMatrix result = m;
for (unsigned i = 0; i < result.m_elements; ++i)
result.m_data[i] = fabs(result.m_data[i]);
return result;
}
#endif /* GMATRIX_HPP */