GammaLib  2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GMatrix.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GMatrix.hpp - General matrix class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2006-2021 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 GMatrix.hpp
23  * @brief Generic matrix class definition
24  * @author Juergen Knoedlseder
25  */
26 
27 #ifndef GMATRIX_HPP
28 #define GMATRIX_HPP
29 
30 /* __ Includes ___________________________________________________________ */
31 #include <string>
32 #include "GMatrixBase.hpp"
33 
34 /* __ Forward declarations _______________________________________________ */
35 class GMatrixSymmetric;
36 class GMatrixSparse;
37 class GVector;
38 
39 
40 /***********************************************************************//**
41  * @class GMatrix
42  *
43  * @brief Generic matrix class definition
44  *
45  * This class implements a generic matrix. The class is a non-specialized
46  * representation of a matrix, and all other matrix storage classes can be
47  * converted into that class.
48  *
49  * For a description of the common matrix methods, please refer to the
50  * GMatrixBase class.
51  *
52  * Matrix allocation is done using the constructors
53  *
54  * GMatrix matrix(rows, columns);
55  * GMatrix matrix(matrix);
56  * GMatrix matrix(sparsematrix);
57  * GMatrix matrix(symmetricmatrix);
58  *
59  * where @p rows and @p columns specify the number of rows and columns of
60  * the matrix. Storage conversion constructors exist that allow allocating
61  * a generic matrix by copying from a sparse matrix of type GMatrixSparse
62  * and a symmetric matrix of type GMatrixSymmetric.
63  *
64  * To support using the GMatrix for coordinate transformations, methods
65  * are available to compute matrices for the three Euler angles:
66  *
67  * matrix.eulerx(angle);
68  * matrix.eulery(angle);
69  * matrix.eulerz(angle);
70  *
71  * Methods are also available to extract the lower or the upper triangle of
72  * a matrix:
73  *
74  * matrix.extract_lower_triangle();
75  * matrix.extract_upper_triangle();
76  *
77  * Matrix elements are stored column-wise by the class.
78  ***************************************************************************/
79 class GMatrix : public GMatrixBase {
80 
81 public:
82  // Constructors and destructors
83  GMatrix(void);
84  explicit GMatrix(const int& rows, const int& columns);
85  GMatrix(const GMatrix& matrix);
86  GMatrix(const GMatrixSymmetric& matrix);
87  GMatrix(const GMatrixSparse& matrix);
88  virtual ~GMatrix(void);
89 
90  // Implemented pure virtual base class operators
91  virtual double& operator()(const int& row, const int& column);
92  virtual const double& operator()(const int& row, const int& column) const;
93  virtual GVector operator*(const GVector& vector) const;
94 
95  // Other operators
96  virtual GMatrix& operator=(const GMatrix& matrix);
97  virtual GMatrix& operator=(const double& value);
98  virtual GMatrix operator+(const GMatrix& matrix) const;
99  virtual GMatrix operator+(const double& scalar) const;
100  virtual GMatrix operator-(const GMatrix& matrix) const;
101  virtual GMatrix operator-(const double& scalar) const;
102  virtual GMatrix operator*(const GMatrix& matrix) const;
103  virtual GMatrix operator-(void) const;
104  virtual GMatrix& operator+=(const GMatrix& matrix);
105  virtual GMatrix& operator+=(const double& scalar);
106  virtual GMatrix& operator-=(const GMatrix& matrix);
107  virtual GMatrix& operator-=(const double& scalar);
108  virtual GMatrix& operator*=(const GMatrix& matrix);
109  virtual GMatrix& operator*=(const double& scalar);
110  virtual GMatrix& operator/=(const double& scalar);
111 
112  // Implemented pure virtual base class methods
113  virtual void clear(void);
114  virtual GMatrix* clone(void) const;
115  virtual std::string classname(void) const;
116  virtual double& at(const int& row, const int& column);
117  virtual const double& at(const int& row, const int& column) const;
118  virtual GVector row(const int& row) const;
119  virtual void row(const int& row, const GVector& vector);
120  virtual GVector column(const int& column) const;
121  virtual void column(const int& column, const GVector& vector);
122  virtual void add_to_row(const int& row, const GVector& vector);
123  virtual void add_to_column(const int& column, const GVector& vector);
124  virtual double fill(void) const;
125  virtual double min(void) const;
126  virtual double max(void) const;
127  virtual double sum(void) const;
128  virtual std::string print(const GChatter& chatter = NORMAL) const;
129 
130  // Other methods
131  GMatrix transpose(void) const;
132  GMatrix invert(void) const;
133  GVector solve(const GVector& vector) const;
134  GMatrix abs(void) const;
135  GMatrix extract_lower_triangle(void) const;
136  GMatrix extract_upper_triangle(void) const;
137  void eulerx(const double& angle);
138  void eulery(const double& angle);
139  void eulerz(const double& angle);
140 
141 private:
142  // Private methods
143  void init_members(void);
144  void copy_members(const GMatrix& matrix);
145  void free_members(void);
146  void alloc_members(const int& rows, const int& columns);
147 };
148 
149 
150 /***********************************************************************//**
151  * @brief Return class name
152  *
153  * @return String containing the class name ("GMatrix").
154  ***************************************************************************/
155 inline
156 std::string GMatrix::classname(void) const
157 {
158  return ("GMatrix");
159 }
160 
161 
162 /***********************************************************************//**
163  * @brief Return reference to matrix element
164  *
165  * @param[in] row Matrix row [0,...,rows()-1].
166  * @param[in] column Matrix column [0,...,columns()-1].
167  * @return Reference to matrix element.
168  *
169  * Returns a reference to the matrix element at @p row and @p column.
170  ***************************************************************************/
171 inline
172 double& GMatrix::operator()(const int& row, const int& column)
173 {
174  return (m_data[m_colstart[column]+row]);
175 }
176 
177 
178 /***********************************************************************//**
179  * @brief Return reference to matrix element (const version)
180  *
181  * @param[in] row Matrix row [0,...,rows()-1].
182  * @param[in] column Matrix column [0,...,columns()-1].
183  * @return Const reference to matrix element.
184  *
185  * Returns a const reference to the matrix element at @p row and @p column.
186  ***************************************************************************/
187 inline
188 const double& GMatrix::operator()(const int& row, const int& column) const
189 {
190  return (m_data[m_colstart[column]+row]);
191 }
192 
193 
194 /***********************************************************************//**
195  * @brief Binary matrix addition
196  *
197  * @param[in] matrix Matrix.
198  * @return Result of matrix addition.
199  *
200  * Returns the sum of two matrices. The method makes use of the unary
201  * addition operator.
202  ***************************************************************************/
203 inline
204 GMatrix GMatrix::operator+(const GMatrix& matrix) const
205 {
206  GMatrix result = *this;
207  result += matrix;
208  return result;
209 }
210 
211 
212 /***********************************************************************//**
213  * @brief Binary matrix scalar addition
214  *
215  * @param[in] scalar Scalar.
216  * @return Matrix with @p scalar added.
217  *
218  * Returns a matrix where a @p scalar has been added to each matrix element.
219  ***************************************************************************/
220 inline
221 GMatrix GMatrix::operator+(const double& scalar) const
222 {
223  GMatrix result = *this;
224  result += scalar;
225  return result;
226 }
227 
228 
229 /***********************************************************************//**
230  * @brief Binary matrix subtraction
231  *
232  * @param[in] matrix Matrix.
233  * @return Result of matrix subtraction.
234  *
235  * Returns the difference between two matrices. The method makes use of the
236  * unary subtraction operator.
237  ***************************************************************************/
238 inline
239 GMatrix GMatrix::operator-(const GMatrix& matrix) const
240 {
241  GMatrix result = *this;
242  result -= matrix;
243  return result;
244 }
245 
246 
247 /***********************************************************************//**
248  * @brief Binary matrix scalar subtraction
249  *
250  * @param[in] scalar Scalar.
251  * @return Matrix with @p scalar subtracted.
252  *
253  * Returns a matrix where a @p scalar has been subtracted from each matrix
254  * element.
255  ***************************************************************************/
256 inline
257 GMatrix GMatrix::operator-(const double& scalar) const
258 {
259  GMatrix result = *this;
260  result -= scalar;
261  return result;
262 }
263 
264 
265 /***********************************************************************//**
266  * @brief Binary matrix multiplication
267  *
268  * @param[in] matrix Matrix.
269  * @return Result of matrix multiplication.
270  *
271  * Returns the product of two matrices. The method makes use of the unary
272  * multiplication operator.
273  ***************************************************************************/
274 inline
275 GMatrix GMatrix::operator*(const GMatrix& matrix) const
276 {
277  GMatrix result = *this;
278  result *= matrix;
279  return result;
280 }
281 
282 
283 /***********************************************************************//**
284  * @brief Scale matrix elements
285  *
286  * @param[in] scalar Scale factor.
287  * @return Matrix with elements multiplied by @p scalar.
288  *
289  * Returns a matrix where all elements have been multiplied by the specified
290  * @p scalar value.
291  ***************************************************************************/
292 inline
293 GMatrix& GMatrix::operator*=(const double& scalar)
294 {
295  scale_elements(scalar);
296  return *this;
297 }
298 
299 
300 /***********************************************************************//**
301  * @brief Divide matrix elements
302  *
303  * @param[in] scalar Scalar.
304  * @return Matrix with elements divided by @p scalar.
305  *
306  * Returns a matrix where all elements have been divided by the specified
307  * @p scalar value.
308  ***************************************************************************/
309 inline
310 GMatrix& GMatrix::operator/=(const double& scalar)
311 {
312  *this *= 1.0/scalar;
313  return *this;
314 }
315 
316 
317 /***********************************************************************//**
318  * @brief Return minimum matrix element
319  *
320  * @return Minimum element in matrix.
321  *
322  * Returns the smallest element in the matrix.
323  ***************************************************************************/
324 inline
325 double GMatrix::min(void) const
326 {
327  return get_min_element();
328 }
329 
330 
331 /***********************************************************************//**
332  * @brief Return maximum matrix element
333  *
334  * @return Maximum element in matrix.
335  *
336  * Returns the largest element in the matrix.
337  ***************************************************************************/
338 inline
339 double GMatrix::max(void) const
340 {
341  return get_max_element();
342 }
343 
344 
345 /***********************************************************************//**
346  * @brief Return matrix element sum
347  *
348  * @return Sum of all matrix elements.
349  *
350  * Returns the sum of all matrix elements.
351  ***************************************************************************/
352 inline
353 double GMatrix::sum(void) const
354 {
355  return get_element_sum();
356 }
357 
358 
359 /***********************************************************************//**
360  * @brief Multiply matrix by scalar
361  *
362  * @param[in] matrix Matrix.
363  * @param[in] scalar Scalar.
364  * @return Matrix divided by @p scalar.
365  *
366  * Returns a matrix where each element has been multiplied by a @p scalar.
367  ***************************************************************************/
368 inline
369 GMatrix operator*(const GMatrix& matrix, const double& scalar)
370 {
371  GMatrix result = matrix;
372  result *= scalar;
373  return result;
374 }
375 
376 
377 /***********************************************************************//**
378  * @brief Multiply matrix by scalar
379  *
380  * @param[in] scalar Scalar.
381  * @param[in] matrix Matrix.
382  * @return Matrix divided by @p scalar.
383  *
384  * Returns a matrix where each element has been multiplied by a @p scalar.
385  ***************************************************************************/
386 inline
387 GMatrix operator*(const double& scalar, const GMatrix& matrix)
388 {
389  GMatrix result = matrix;
390  result *= scalar;
391  return result;
392 }
393 
394 
395 /***********************************************************************//**
396  * @brief Divide matrix by scalar
397  *
398  * @param[in] matrix Matrix.
399  * @param[in] scalar Scalar.
400  * @return Matrix divided by @p scalar.
401  *
402  * Returns a matrix where each element has been divided by a @p scalar.
403  ***************************************************************************/
404 inline
405 GMatrix operator/(const GMatrix& matrix, const double& scalar)
406 {
407  GMatrix result = matrix;
408  result /= scalar;
409  return result;
410 }
411 
412 #endif /* GMATRIX_HPP */
double get_min_element(void) const
Return minimum matrix element.
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.
Abstract matrix base class interface definition.
virtual std::string print(const GChatter &chatter=NORMAL) const
Print matrix.
Definition: GMatrix.cpp:1232
virtual GMatrix & operator/=(const double &scalar)
Divide matrix elements.
Definition: GMatrix.hpp:310
Sparse matrix class interface definition.
GMatrix invert(void) const
Return inverted matrix.
Definition: GMatrix.cpp:932
virtual double & at(const int &row, const int &column)
Return reference to matrix element.
Definition: GMatrix.cpp:580
double get_element_sum(void) const
Returns sum over all matrix elements.
virtual void add_to_row(const int &row, const GVector &vector)
Add row to matrix elements.
Definition: GMatrix.cpp:812
virtual GMatrix operator-(void) const
Negate matrix elements.
Definition: GMatrix.cpp:324
virtual std::string classname(void) const
Return class name.
Definition: GMatrix.hpp:156
virtual GVector column(const int &column) const
Extract column as vector from matrix.
Definition: GMatrix.cpp:718
Symmetric matrix class interface definition.
void copy_members(const GMatrix &matrix)
Copy class members.
Definition: GMatrix.cpp:1292
virtual void add_to_column(const int &column, const GVector &vector)
Add vector column into matrix.
Definition: GMatrix.cpp:857
virtual GVector row(const int &row) const
Extract row as vector from matrix.
Definition: GMatrix.cpp:637
void eulery(const double &angle)
Set Euler rotation matrix around y axis.
Definition: GMatrix.cpp:1157
virtual void clear(void)
Clear matrix.
Definition: GMatrix.cpp:541
virtual double max(void) const
Return maximum matrix element.
Definition: GMatrix.hpp:339
void init_members(void)
Initialise class members.
Definition: GMatrix.cpp:1280
virtual GMatrix & operator=(const GMatrix &matrix)
Matrix assignment operator.
Definition: GMatrix.cpp:228
virtual double & operator()(const int &row, const int &column)
Return reference to matrix element.
Definition: GMatrix.hpp:172
void eulerx(const double &angle)
Set Euler rotation matrix around x axis.
Definition: GMatrix.cpp:1120
double * m_data
Matrix data.
Abstract matrix base class definition.
const int & rows(void) const
Return number of matrix rows.
GMatrix transpose(void) const
Return transposed matrix.
Definition: GMatrix.cpp:903
GVector solve(const GVector &vector) const
Solves linear matrix equation.
Definition: GMatrix.cpp:962
void eulerz(const double &angle)
Set Euler rotation matrix around z axis.
Definition: GMatrix.cpp:1194
virtual GMatrix operator+(const GMatrix &matrix) const
Binary matrix addition.
Definition: GMatrix.hpp:204
virtual GVector operator*(const GVector &vector) const
Vector multiplication.
Definition: GMatrix.cpp:289
double get_max_element(void) const
Returns maximum matrix element.
virtual GMatrix & operator-=(const GMatrix &matrix)
Unary matrix subtraction operator.
Definition: GMatrix.cpp:412
GChatter
Definition: GTypemaps.hpp:33
double angle(const GVector &a, const GVector &b)
Computes angle between vectors.
Definition: GVector.cpp:974
GArf operator*(const GArf &arf, const double &scale)
Auxiliary Response File scaling operator friend.
Definition: GArf.hpp:325
virtual double fill(void) const
Return fill of matrix.
Definition: GMatrix.cpp:1011
virtual GMatrix & operator*=(const GMatrix &matrix)
Unary matrix multiplication operator.
Definition: GMatrix.cpp:479
virtual GMatrix * clone(void) const
Clone matrix.
Definition: GMatrix.cpp:559
virtual double min(void) const
Return minimum matrix element.
Definition: GMatrix.hpp:325
void free_members(void)
Delete class members.
Definition: GMatrix.cpp:1302
GMatrix(void)
Void matrix constructor.
Definition: GMatrix.cpp:70
virtual ~GMatrix(void)
Destructor.
Definition: GMatrix.cpp:204
virtual double sum(void) const
Return matrix element sum.
Definition: GMatrix.hpp:353
GMatrix extract_lower_triangle(void) const
Extract lower triangle of matrix.
Definition: GMatrix.cpp:1049
GMatrix abs(void) const
Return absolute of matrix.
Definition: GMatrix.cpp:983
Generic matrix class definition.
Definition: GMatrix.hpp:79
Vector class.
Definition: GVector.hpp:46
int * m_colstart
Column start indices (m_cols+1)
const int & columns(void) const
Return number of matrix columns.
virtual GMatrix & operator+=(const GMatrix &matrix)
Unary matrix addition operator.
Definition: GMatrix.cpp:351
GMatrix extract_upper_triangle(void) const
Extract upper triangle of matrix.
Definition: GMatrix.cpp:1088
void alloc_members(const int &rows, const int &columns)
Allocate matrix memory.
Definition: GMatrix.cpp:1324