GSymMatrix.hpp
7.3 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
/***************************************************************************
* GSymMatrix.hpp - symmetric 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 GSYMMATRIX_HPP
#define GSYMMATRIX_HPP
/* __ Includes ___________________________________________________________ */
#include "GException.hpp"
#include "GVector.hpp"
#include "GMatrix.hpp"
/* __ Namespaces _________________________________________________________ */
using namespace std;
/* __ Enumerators ________________________________________________________ */
/* __ Structures _________________________________________________________ */
/* __ Prototypes _________________________________________________________ */
/***************************************************************************
* GSymMatrix class definition *
***************************************************************************/
class GSymMatrix : public GMatrix {
// Binary operator friends
friend GSymMatrix operator* (const double& a, const GSymMatrix& b);
friend GSymMatrix operator* (const GSymMatrix& a, const double& b);
friend GSymMatrix operator/ (const GSymMatrix& a, const double& b);
// I/O friends
// USE_BASE: friend ostream& operator<< (ostream& os, const GSymMatrix& m);
// Friend functions
friend GSymMatrix transpose(const GSymMatrix& m);
friend GSymMatrix fabs(const GSymMatrix& m);
friend GSymMatrix cholesky_decompose(const GSymMatrix& m, int compress = 1);
friend GSymMatrix cholesky_invert(const GSymMatrix& m, int compress = 1);
public:
// Constructors and destructors (not inherited)
GSymMatrix(int rows, int cols);
GSymMatrix(const GSymMatrix& m);
virtual ~GSymMatrix();
// Matrix element access operators
virtual double& operator() (int row, int col);
virtual const double& operator() (int row, int col) const;
// Matrix assignment operators (not inherited)
virtual GSymMatrix& operator= (const GSymMatrix& m);
// Binary operators
virtual GSymMatrix operator+ (const GSymMatrix& m) const;
virtual GSymMatrix operator- (const GSymMatrix& m) const;
virtual GSymMatrix operator* (const GSymMatrix& m) const;
virtual GVector operator* (const GVector& v) const;
// USE_BASE: virtual int operator== (const GSymMatrix& m) const;
// USE_BASE: virtual int operator!= (const GSymMatrix& m) const;
// Unary operators
GSymMatrix operator- () const;
// USE_BASE: virtual GSymMatrix& operator+= (const GSymMatrix& m);
// USE_BASE: virtual GSymMatrix& operator-= (const GSymMatrix& m);
virtual GSymMatrix& operator*= (const GSymMatrix& m);
virtual GSymMatrix& operator*= (const double& d);
virtual GSymMatrix& operator/= (const double& d);
// Matrix functions
// USE_BASE: virtual int rows() const { return m_rows; }
// USE_BASE: virtual int cols() const { return m_cols; }
// USE_BASE: virtual void clear();
// USE_BASE: virtual double min() const;
// USE_BASE: virtual double max() const;
virtual double sum() const;
virtual void transpose() { return; }
virtual GVector extract_row(int row) const;
virtual GVector extract_col(int col) const;
virtual GMatrix convert_to_full() const;
virtual GMatrix extract_lower_triangle() const;
virtual GMatrix extract_upper_triangle() const;
virtual void cholesky_decompose(int compress = 1);
virtual GVector cholesky_solver(const GVector& v, int compress = 1);
virtual void cholesky_invert(int compress = 1);
private:
void set_inx(void); // Set indices of non-zero rows/columns
int m_num_inx; // Number of non-zero rows/columns
int* m_inx; // Indices of non-zero rows/columns
};
/***************************************************************************
* Inline member functions *
***************************************************************************/
// Matrix element access operator
inline
double& GSymMatrix::operator() (int row, int col)
{
#if defined(G_RANGE_CHECK)
if (row >= m_rows || col >= m_cols)
throw out_of_range("GSymMatrix access", row, col, m_rows, m_cols);
#endif
int inx = (row >= col) ? m_colstart[col]+(row-col) : m_colstart[row]+(col-row);
return m_data[inx];
}
// Matrix element access operator (const version)
inline
const double& GSymMatrix::operator() (int row, int col) const
{
#if defined(G_RANGE_CHECK)
if (row >= m_rows || col >= m_cols)
throw out_of_range("GSymMatrix access", row, col, m_rows, m_cols);
#endif
int inx = (row >= col) ? m_colstart[col]+(row-col) : m_colstart[row]+(col-row);
return m_data[inx];
}
// Binary matrix addition
inline
GSymMatrix GSymMatrix::operator+ (const GSymMatrix& m) const
{
GSymMatrix result = *this;
result += m;
return result;
}
// Binary matrix subtraction
inline
GSymMatrix GSymMatrix::operator- (const GSymMatrix& m) const
{
GSymMatrix result = *this;
result -= m;
return result;
}
// Binary matrix multiplication
inline
GSymMatrix GSymMatrix::operator* (const GSymMatrix& m) const
{
GSymMatrix result = *this;
result *= m;
return result;
}
// Unary scalar multiplication
inline
GSymMatrix& GSymMatrix::operator*= (const double& d)
{
this->GMatrix::operator*=(d);
return *this;
}
// Unary scalar division
inline
GSymMatrix& GSymMatrix::operator/= (const double& d)
{
this->GMatrix::operator/=(d);
return *this;
}
/***************************************************************************
* Inline friends *
***************************************************************************/
// Binary matrix scaling (matrix is left operand)
inline
GSymMatrix operator* (const GSymMatrix& a, const double& b)
{
GSymMatrix result = a;
result *= b;
return result;
}
// Binary matrix scaling (matrix is right operand)
inline
GSymMatrix operator* (const double& a, const GSymMatrix& b)
{
GSymMatrix result = b;
result *= a;
return result;
}
// Binary matrix division (matrix is left operand)
inline
GSymMatrix operator/ (const GSymMatrix& a, const double& b)
{
GSymMatrix result = a;
result /= b;
return result;
}
// Matrix transpose function
inline
GSymMatrix transpose(const GSymMatrix& m)
{
return m;
}
// Cholesky decomposition
inline
GSymMatrix cholesky_decompose(const GSymMatrix& m, int compress)
{
GSymMatrix result = m;
result.cholesky_decompose(compress);
return result;
}
// Matrix inversion using Cholesky decomposition
inline
GSymMatrix cholesky_invert(const GSymMatrix& m, int compress)
{
GSymMatrix result = m;
result.cholesky_invert(compress);
return result;
}
#endif /* GSYMMATRIX_HPP */