GMatrixSymmetric.hpp
11.7 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
/***************************************************************************
* GMatrixSymmetric.hpp - Symmetric matrix class *
* ----------------------------------------------------------------------- *
* copyright (C) 2006-2014 by Juergen Knoedlseder *
* ----------------------------------------------------------------------- *
* *
* 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 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
***************************************************************************/
/**
* @file GMatrixSymmetric.hpp
* @brief Symmetric matrix class definition
* @author Juergen Knoedlseder
*/
#ifndef GMATRIXSYMMETRIC_HPP
#define GMATRIXSYMMETRIC_HPP
/* __ Includes ___________________________________________________________ */
#include <string>
#include "GMatrixBase.hpp"
/* __ Forward declarations _______________________________________________ */
class GMatrix;
class GMatrixSparse;
class GVector;
/***********************************************************************//**
* @class GMatrixSymmetric
*
* @brief Symmetric matrix class interface definition
*
* This class implements a symmetric matrix. The class stores only a triangle
* physically, imposing thus strict matrix symmetry.
*
* For a description of the common matrix methods, please refer to the
* GMatrixBase class.
*
* Matrix allocation is done using the constructors
*
* GMatrixSymmetric symmetricmatrix(rows, columns);
* GMatrixSymmetric symmetricmatrix(matrix);
* GMatrixSymmetric symmetricmatrix(sparsematrix);
* GMatrixSymmetric symmetricmatrix(symmetricmatrix);
*
* where @p rows and @p columns specify the number of rows and columns of
* the matrix. Storage conversion constructors exist that allow allocating
* a symmetric matrix by copying from a sparse matrix of type GMatrixSparse
* and a general matrix of type GMatrix. Exceptions will be thrown in case
* that the matrix from which the object should be allocated is not
* symmetric.
*
* Methods are available to extract the lower or the upper triangle of the
* matrix:
*
* matrix.extract_lower_triangle();
* matrix.extract_upper_triangle();
***************************************************************************/
class GMatrixSymmetric : public GMatrixBase {
public:
// Constructors and destructors
GMatrixSymmetric(void);
explicit GMatrixSymmetric(const int& rows, const int& columns);
GMatrixSymmetric(const GMatrix& matrix);
GMatrixSymmetric(const GMatrixSparse& matrix);
GMatrixSymmetric(const GMatrixSymmetric& matrix);
virtual ~GMatrixSymmetric(void);
// Implemented pure virtual base class operators
virtual double& operator()(const int& row, const int& column);
virtual const double& operator()(const int& row, const int& column) const;
virtual GVector operator*(const GVector& v) const;
// Other operators
virtual GMatrixSymmetric& operator=(const GMatrixSymmetric& matrix);
virtual GMatrixSymmetric& operator=(const double& value);
virtual GMatrixSymmetric operator+(const GMatrixSymmetric& matrix) const;
virtual GMatrixSymmetric operator-(const GMatrixSymmetric& matrix) const;
virtual GMatrix operator*(const GMatrixSymmetric& matrix) const;
virtual GMatrixSymmetric operator-(void) const;
virtual GMatrixSymmetric& operator+=(const GMatrixSymmetric& matrix);
virtual GMatrixSymmetric& operator-=(const GMatrixSymmetric& matrix);
virtual GMatrixSymmetric& operator*=(const double& scaler);
virtual GMatrixSymmetric& operator/=(const double& scalar);
// Implemented pure virtual base class methods
virtual void clear(void);
virtual GMatrixSymmetric* clone(void) const;
virtual std::string classname(void) const;
virtual double& at(const int& row, const int& column);
virtual const double& at(const int& row, const int& column) const;
virtual GVector row(const int& row) const;
virtual void row(const int& row, const GVector& vector);
virtual GVector column(const int& column) const;
virtual void column(const int& column, const GVector& vector);
virtual void add_to_row(const int& row, const GVector& vector);
virtual void add_to_column(const int& column, const GVector& vector);
virtual double fill(void) const;
virtual double min(void) const;
virtual double max(void) const;
virtual double sum(void) const;
virtual std::string print(const GChatter& chatter = NORMAL) const;
// Other methods
GMatrixSymmetric transpose(void) const;
GMatrixSymmetric invert(void) const;
GVector solve(const GVector& vector) const;
GMatrixSymmetric abs(void) const;
GMatrix extract_lower_triangle(void) const;
GMatrix extract_upper_triangle(void) const;
GMatrixSymmetric cholesky_decompose(const bool& compress = true) const;
GVector cholesky_solver(const GVector& vector, const bool& compress = true) const;
GMatrixSymmetric cholesky_invert(const bool& compress = true) const;
private:
// Private methods
void init_members(void);
void copy_members(const GMatrixSymmetric& matrix);
void free_members(void);
void alloc_members(const int& rows, const int& columns);
void set_inx(void);
// Private data area
int m_num_inx; //!< Number of indices in array
int* m_inx; //!< Index array of non-zero rows/columns
};
/***********************************************************************//**
* @brief Return class name
*
* @return String containing the class name ("GMatrixSymmetric").
***************************************************************************/
inline
std::string GMatrixSymmetric::classname(void) const
{
return ("GMatrixSymmetric");
}
/***********************************************************************//**
* @brief Return transposed matrix
*
* @return Transposed matrix.
*
* Returns transposed matrix of the matrix. As the transposed matrix of a
* symmetric matrix is identical to the original matrix, this method simply
* returns the actual matrix.
***************************************************************************/
inline
GMatrixSymmetric GMatrixSymmetric::transpose(void) const
{
return (*this);
}
/***********************************************************************//**
* @brief Return minimum matrix element
*
* @return Minimum matrix element
*
* Returns the smallest element in the matrix.
***************************************************************************/
inline
double GMatrixSymmetric::min(void) const
{
return (get_min_element());
}
/***********************************************************************//**
* @brief Return maximum matrix element
*
* @return Maximum matrix element
*
* Returns the largest element in the matrix.
***************************************************************************/
inline
double GMatrixSymmetric::max(void) const
{
return (get_max_element());
}
/***********************************************************************//**
* @brief Binary matrix addition
*
* @param[in] matrix Matrix.
* @return Result of matrix addition.
*
* Returns the sum of two matrices. The method makes use of the unary
* addition operator.
***************************************************************************/
inline
GMatrixSymmetric GMatrixSymmetric::operator+(const GMatrixSymmetric& matrix) const
{
GMatrixSymmetric result = *this;
result += matrix;
return result;
}
/***********************************************************************//**
* @brief Binary matrix subtraction
*
* @param[in] matrix Matrix.
* @return Result of matrix subtraction.
*
* Returns the difference between two matrices. The method makes use of the
* unary subtraction operator.
***************************************************************************/
inline
GMatrixSymmetric GMatrixSymmetric::operator-(const GMatrixSymmetric& matrix) const
{
GMatrixSymmetric result = *this;
result -= matrix;
return result;
}
/***********************************************************************//**
* @brief Scale matrix elements
*
* @param[in] scalar Scale factor.
* @return Matrix with elements multiplied by @p scalar.
*
* Returns a matrix where all elements have been multiplied by the specified
* @p scalar value.
***************************************************************************/
inline
GMatrixSymmetric& GMatrixSymmetric::operator*=(const double& scalar)
{
scale_elements(scalar);
return *this;
}
/***********************************************************************//**
* @brief Divide matrix elements
*
* @param[in] scalar Scalar.
* @return Matrix with elements divided by @p scalar.
*
* Returns a matrix where all elements have been divided by the specified
* @p scalar value.
***************************************************************************/
inline
GMatrixSymmetric& GMatrixSymmetric::operator/=(const double& scalar)
{
*this *= 1.0/scalar;
return *this;
}
/***********************************************************************//**
* @brief Multiply matrix by scalar
*
* @param[in] matrix Matrix.
* @param[in] scalar Scalar.
* @return Matrix divided by @p scalar.
*
* Returns a matrix where each element is multiplied by @p scalar.
***************************************************************************/
inline
GMatrixSymmetric operator*(const GMatrixSymmetric& matrix, const double& scalar)
{
GMatrixSymmetric result = matrix;
result *= scalar;
return result;
}
/***********************************************************************//**
* @brief Multiply matrix by scalar
*
* @param[in] scalar Scalar.
* @param[in] matrix Matrix.
* @return Matrix divided by @p scalar.
*
* Returns a matrix where each element is multiplied by @p scalar.
***************************************************************************/
inline
GMatrixSymmetric operator*(const double& scalar, const GMatrixSymmetric& matrix)
{
GMatrixSymmetric result = matrix;
result *= scalar;
return result;
}
/***********************************************************************//**
* @brief Divide matrix by scalar
*
* @param[in] matrix Matrix.
* @param[in] scalar Scalar.
* @return Matrix divided by @p scalar.
*
* Returns a matrix where each element is divided by @p scalar.
***************************************************************************/
inline
GMatrixSymmetric operator/(const GMatrixSymmetric& matrix, const double& scalar)
{
GMatrixSymmetric result = matrix;
result /= scalar;
return result;
}
#endif /* GMATRIXSYMMETRIC_HPP */