GammaLib  1.7.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GMatrixSymmetric.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GMatrixSymmetric.hpp - Symmetric matrix class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2006-2017 by Juergen Knoedlseder *
5  * ----------------------------------------------------------------------- *
6  * *
7  * This program is free software: you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation, either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19  * *
20  ***************************************************************************/
21 /**
22  * @file GMatrixSymmetric.hpp
23  * @brief Symmetric matrix class definition
24  * @author Juergen Knoedlseder
25  */
26 
27 #ifndef GMATRIXSYMMETRIC_HPP
28 #define GMATRIXSYMMETRIC_HPP
29 
30 /* __ Includes ___________________________________________________________ */
31 #include <string>
32 #include "GMatrixBase.hpp"
33 
34 /* __ Forward declarations _______________________________________________ */
35 class GMatrix;
36 class GMatrixSparse;
37 class GVector;
38 
39 
40 /***********************************************************************//**
41  * @class GMatrixSymmetric
42  *
43  * @brief Symmetric matrix class interface definition
44  *
45  * This class implements a symmetric matrix. The class stores only a triangle
46  * physically, imposing thus strict matrix symmetry.
47  *
48  * For a description of the common matrix methods, please refer to the
49  * GMatrixBase class.
50  *
51  * Matrix allocation is done using the constructors
52  *
53  * GMatrixSymmetric symmetricmatrix(rows, columns);
54  * GMatrixSymmetric symmetricmatrix(matrix);
55  * GMatrixSymmetric symmetricmatrix(sparsematrix);
56  * GMatrixSymmetric symmetricmatrix(symmetricmatrix);
57  *
58  * where @p rows and @p columns specify the number of rows and columns of
59  * the matrix. Storage conversion constructors exist that allow allocating
60  * a symmetric matrix by copying from a sparse matrix of type GMatrixSparse
61  * and a general matrix of type GMatrix. Exceptions will be thrown in case
62  * that the matrix from which the object should be allocated is not
63  * symmetric.
64  *
65  * Methods are available to extract the lower or the upper triangle of the
66  * matrix:
67  *
68  * matrix.extract_lower_triangle();
69  * matrix.extract_upper_triangle();
70  ***************************************************************************/
71 class GMatrixSymmetric : public GMatrixBase {
72 
73 public:
74  // Constructors and destructors
75  GMatrixSymmetric(void);
76  explicit GMatrixSymmetric(const int& rows, const int& columns);
77  GMatrixSymmetric(const GMatrix& matrix);
78  GMatrixSymmetric(const GMatrixSparse& matrix);
79  GMatrixSymmetric(const GMatrixSymmetric& matrix);
80  virtual ~GMatrixSymmetric(void);
81 
82  // Implemented pure virtual base class operators
83  virtual double& operator()(const int& row, const int& column);
84  virtual const double& operator()(const int& row, const int& column) const;
85  virtual GVector operator*(const GVector& v) const;
86 
87  // Other operators
88  virtual GMatrixSymmetric& operator=(const GMatrixSymmetric& matrix);
89  virtual GMatrixSymmetric& operator=(const double& value);
90  virtual GMatrixSymmetric operator+(const GMatrixSymmetric& matrix) const;
91  virtual GMatrixSymmetric operator+(const double& scalar) const;
92  virtual GMatrixSymmetric operator-(const GMatrixSymmetric& matrix) const;
93  virtual GMatrixSymmetric operator-(const double& scalar) const;
94  virtual GMatrix operator*(const GMatrixSymmetric& matrix) const;
95  virtual GMatrixSymmetric operator-(void) const;
96  virtual GMatrixSymmetric& operator+=(const GMatrixSymmetric& matrix);
97  virtual GMatrixSymmetric& operator+=(const double& scalar);
98  virtual GMatrixSymmetric& operator-=(const GMatrixSymmetric& matrix);
99  virtual GMatrixSymmetric& operator-=(const double& scalar);
100  virtual GMatrixSymmetric& operator*=(const double& scaler);
101  virtual GMatrixSymmetric& operator/=(const double& scalar);
102 
103  // Implemented pure virtual base class methods
104  virtual void clear(void);
105  virtual GMatrixSymmetric* clone(void) const;
106  virtual std::string classname(void) const;
107  virtual double& at(const int& row, const int& column);
108  virtual const double& at(const int& row, const int& column) const;
109  virtual GVector row(const int& row) const;
110  virtual void row(const int& row, const GVector& vector);
111  virtual GVector column(const int& column) const;
112  virtual void column(const int& column, const GVector& vector);
113  virtual void add_to_row(const int& row, const GVector& vector);
114  virtual void add_to_column(const int& column, const GVector& vector);
115  virtual double fill(void) const;
116  virtual double min(void) const;
117  virtual double max(void) const;
118  virtual double sum(void) const;
119  virtual std::string print(const GChatter& chatter = NORMAL) const;
120 
121  // Other methods
122  GMatrixSymmetric transpose(void) const;
123  GMatrixSymmetric invert(void) const;
124  GVector solve(const GVector& vector) const;
125  GMatrixSymmetric abs(void) const;
126  GMatrix extract_lower_triangle(void) const;
127  GMatrix extract_upper_triangle(void) const;
128  GMatrixSymmetric cholesky_decompose(const bool& compress = true) const;
129  GVector cholesky_solver(const GVector& vector, const bool& compress = true) const;
130  GMatrixSymmetric cholesky_invert(const bool& compress = true) const;
131 
132 private:
133  // Private methods
134  void init_members(void);
135  void copy_members(const GMatrixSymmetric& matrix);
136  void free_members(void);
137  void alloc_members(const int& rows, const int& columns);
138  void set_inx(void);
139 
140  // Private data area
141  int m_num_inx; //!< Number of indices in array
142  int* m_inx; //!< Index array of non-zero rows/columns
143 };
144 
145 
146 /***********************************************************************//**
147  * @brief Return class name
148  *
149  * @return String containing the class name ("GMatrixSymmetric").
150  ***************************************************************************/
151 inline
152 std::string GMatrixSymmetric::classname(void) const
153 {
154  return ("GMatrixSymmetric");
155 }
156 
157 
158 /***********************************************************************//**
159  * @brief Return transposed matrix
160  *
161  * @return Transposed matrix.
162  *
163  * Returns transposed matrix of the matrix. As the transposed matrix of a
164  * symmetric matrix is identical to the original matrix, this method simply
165  * returns the actual matrix.
166  ***************************************************************************/
167 inline
169 {
170  return (*this);
171 }
172 
173 
174 /***********************************************************************//**
175  * @brief Return minimum matrix element
176  *
177  * @return Minimum matrix element
178  *
179  * Returns the smallest element in the matrix.
180  ***************************************************************************/
181 inline
182 double GMatrixSymmetric::min(void) const
183 {
184  return (get_min_element());
185 }
186 
187 
188 /***********************************************************************//**
189  * @brief Return maximum matrix element
190  *
191  * @return Maximum matrix element
192  *
193  * Returns the largest element in the matrix.
194  ***************************************************************************/
195 inline
196 double GMatrixSymmetric::max(void) const
197 {
198  return (get_max_element());
199 }
200 
201 
202 /***********************************************************************//**
203  * @brief Binary matrix addition
204  *
205  * @param[in] matrix Matrix.
206  * @return Result of matrix addition.
207  *
208  * Returns the sum of two matrices. The method makes use of the unary
209  * addition operator.
210  ***************************************************************************/
211 inline
213 {
214  GMatrixSymmetric result = *this;
215  result += matrix;
216  return result;
217 }
218 
219 
220 /***********************************************************************//**
221  * @brief Binary matrix scalar addition
222  *
223  * @param[in] scalar Scalar.
224  * @return Matrix with @p scalar added.
225  *
226  * Returns a matrix where a @p scalar has been added to each matrix element.
227  ***************************************************************************/
228 inline
230 {
231  GMatrixSymmetric result = *this;
232  result += scalar;
233  return result;
234 }
235 
236 
237 /***********************************************************************//**
238  * @brief Binary matrix subtraction
239  *
240  * @param[in] matrix Matrix.
241  * @return Result of matrix subtraction.
242  *
243  * Returns the difference between two matrices. The method makes use of the
244  * unary subtraction operator.
245  ***************************************************************************/
246 inline
248 {
249  GMatrixSymmetric result = *this;
250  result -= matrix;
251  return result;
252 }
253 
254 
255 /***********************************************************************//**
256  * @brief Binary matrix scalar subtraction
257  *
258  * @param[in] scalar Scalar.
259  * @return Matrix with @p scalar subtracted.
260  *
261  * Returns a matrix where a @p scalar has been subtracted from each matrix
262  * element.
263  ***************************************************************************/
264 inline
266 {
267  GMatrixSymmetric result = *this;
268  result -= scalar;
269  return result;
270 }
271 
272 
273 /***********************************************************************//**
274  * @brief Scale matrix elements
275  *
276  * @param[in] scalar Scale factor.
277  * @return Matrix with elements multiplied by @p scalar.
278  *
279  * Returns a matrix where all elements have been multiplied by the specified
280  * @p scalar value.
281  ***************************************************************************/
282 inline
284 {
285  scale_elements(scalar);
286  return *this;
287 }
288 
289 
290 /***********************************************************************//**
291  * @brief Divide matrix elements
292  *
293  * @param[in] scalar Scalar.
294  * @return Matrix with elements divided by @p scalar.
295  *
296  * Returns a matrix where all elements have been divided by the specified
297  * @p scalar value.
298  ***************************************************************************/
299 inline
301 {
302  *this *= 1.0/scalar;
303  return *this;
304 }
305 
306 
307 /***********************************************************************//**
308  * @brief Multiply matrix by scalar
309  *
310  * @param[in] matrix Matrix.
311  * @param[in] scalar Scalar.
312  * @return Matrix divided by @p scalar.
313  *
314  * Returns a matrix where each element is multiplied by @p scalar.
315  ***************************************************************************/
316 inline
317 GMatrixSymmetric operator*(const GMatrixSymmetric& matrix, const double& scalar)
318 {
319  GMatrixSymmetric result = matrix;
320  result *= scalar;
321  return result;
322 }
323 
324 
325 /***********************************************************************//**
326  * @brief Multiply matrix by scalar
327  *
328  * @param[in] scalar Scalar.
329  * @param[in] matrix Matrix.
330  * @return Matrix divided by @p scalar.
331  *
332  * Returns a matrix where each element is multiplied by @p scalar.
333  ***************************************************************************/
334 inline
335 GMatrixSymmetric operator*(const double& scalar, const GMatrixSymmetric& matrix)
336 {
337  GMatrixSymmetric result = matrix;
338  result *= scalar;
339  return result;
340 }
341 
342 
343 /***********************************************************************//**
344  * @brief Divide matrix by scalar
345  *
346  * @param[in] matrix Matrix.
347  * @param[in] scalar Scalar.
348  * @return Matrix divided by @p scalar.
349  *
350  * Returns a matrix where each element is divided by @p scalar.
351  ***************************************************************************/
352 inline
353 GMatrixSymmetric operator/(const GMatrixSymmetric& matrix, const double& scalar)
354 {
355  GMatrixSymmetric result = matrix;
356  result /= scalar;
357  return result;
358 }
359 
360 #endif /* GMATRIXSYMMETRIC_HPP */
double get_min_element(void) const
Return minimum matrix element.
GMatrix extract_lower_triangle(void) const
Extract lower triangle of matrix.
GMatrixSymmetric cholesky_invert(const bool &compress=true) const
Invert matrix using a Cholesky decomposition.
GArf operator/(const GArf &arf, const double &scale)
Auxiliary Response File vision operator friend.
Definition: GArf.hpp:357
void scale_elements(const double &scalar)
Scale all matrix elements with a scalar.
void free_members(void)
Delete class members.
Abstract matrix base class interface definition.
virtual ~GMatrixSymmetric(void)
Destructor.
GMatrixSymmetric(void)
Void constructor.
Sparse matrix class interface definition.
virtual void add_to_row(const int &row, const GVector &vector)
Add row to matrix elements.
virtual std::string classname(void) const
Return class name.
Symmetric matrix class interface definition.
virtual double & operator()(const int &row, const int &column)
Return reference to matrix element.
virtual std::string print(const GChatter &chatter=NORMAL) const
Print matrix.
virtual void clear(void)
Clear matrix.
int m_num_inx
Number of indices in array.
GMatrixSymmetric invert(void) const
Return inverted matrix.
virtual GVector column(const int &column) const
Extract column as vector from matrix.
GVector cholesky_solver(const GVector &vector, const bool &compress=true) const
Cholesky solver.
virtual GMatrixSymmetric & operator-=(const GMatrixSymmetric &matrix)
Unary matrix subtraction operator.
virtual GMatrixSymmetric & operator+=(const GMatrixSymmetric &matrix)
Unary matrix addition operator.
virtual GMatrixSymmetric * clone(void) const
Clone matrix.
virtual GMatrixSymmetric & operator*=(const double &scaler)
Scale matrix elements.
Abstract matrix base class definition.
const int & rows(void) const
Return number of matrix rows.
virtual GMatrixSymmetric operator-(void) const
Negate matrix elements.
virtual GMatrixSymmetric & operator=(const GMatrixSymmetric &matrix)
Matrix assignment operator.
double get_max_element(void) const
Returns maximum matrix element.
GChatter
Definition: GTypemaps.hpp:33
virtual GMatrixSymmetric & operator/=(const double &scalar)
Divide matrix elements.
GArf operator*(const GArf &arf, const double &scale)
Auxiliary Response File scaling operator friend.
Definition: GArf.hpp:325
virtual GMatrixSymmetric operator+(const GMatrixSymmetric &matrix) const
Binary matrix addition.
GMatrixSymmetric transpose(void) const
Return transposed matrix.
virtual GVector operator*(const GVector &v) const
Vector multiplication.
GMatrix extract_upper_triangle(void) const
Extract upper triangle of matrix.
void init_members(void)
Initialise class mambers.
virtual double max(void) const
Return maximum matrix element.
virtual double sum(void) const
Sum matrix elements.
virtual double fill(void) const
Determine fill of matrix.
void copy_members(const GMatrixSymmetric &matrix)
Copy class members.
GMatrixSymmetric abs(void) const
Return absolute of matrix.
GMatrixSymmetric cholesky_decompose(const bool &compress=true) const
Return Cholesky decomposition.
GVector solve(const GVector &vector) const
Solves linear matrix equation.
virtual void add_to_column(const int &column, const GVector &vector)
Add vector column into matrix.
virtual GVector row(const int &row) const
Extract row as vector from matrix.
virtual double & at(const int &row, const int &column)
Return reference to matrix element.
Generic matrix class definition.
Definition: GMatrix.hpp:79
Vector class.
Definition: GVector.hpp:46
int * m_inx
Index array of non-zero rows/columns.
void alloc_members(const int &rows, const int &columns)
Allocates matrix memory.
virtual double min(void) const
Return minimum matrix element.
const int & columns(void) const
Return number of matrix columns.
void set_inx(void)
Set index selection.