GammaLib  1.7.0.dev
 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-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 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 negate(void) const;
135  GMatrix abs(void) const;
136  GMatrix extract_lower_triangle(void) const;
137  GMatrix extract_upper_triangle(void) const;
138  void eulerx(const double& angle);
139  void eulery(const double& angle);
140  void eulerz(const double& angle);
141 
142 private:
143  // Private methods
144  void init_members(void);
145  void copy_members(const GMatrix& matrix);
146  void free_members(void);
147  void alloc_members(const int& rows, const int& columns);
148 };
149 
150 
151 /***********************************************************************//**
152  * @brief Return class name
153  *
154  * @return String containing the class name ("GMatrix").
155  ***************************************************************************/
156 inline
157 std::string GMatrix::classname(void) const
158 {
159  return ("GMatrix");
160 }
161 
162 
163 /***********************************************************************//**
164  * @brief Return reference to matrix element
165  *
166  * @param[in] row Matrix row [0,...,rows()-1].
167  * @param[in] column Matrix column [0,...,columns()-1].
168  * @return Reference to matrix element.
169  *
170  * Returns a reference to the matrix element at @p row and @p column.
171  ***************************************************************************/
172 inline
173 double& GMatrix::operator()(const int& row, const int& column)
174 {
175  return (m_data[m_colstart[column]+row]);
176 }
177 
178 
179 /***********************************************************************//**
180  * @brief Return reference to matrix element (const version)
181  *
182  * @param[in] row Matrix row [0,...,rows()-1].
183  * @param[in] column Matrix column [0,...,columns()-1].
184  * @return Const reference to matrix element.
185  *
186  * Returns a const reference to the matrix element at @p row and @p column.
187  ***************************************************************************/
188 inline
189 const double& GMatrix::operator()(const int& row, const int& column) const
190 {
191  return (m_data[m_colstart[column]+row]);
192 }
193 
194 
195 /***********************************************************************//**
196  * @brief Binary matrix addition
197  *
198  * @param[in] matrix Matrix.
199  * @return Result of matrix addition.
200  *
201  * Returns the sum of two matrices. The method makes use of the unary
202  * addition operator.
203  ***************************************************************************/
204 inline
205 GMatrix GMatrix::operator+(const GMatrix& matrix) const
206 {
207  GMatrix result = *this;
208  result += matrix;
209  return result;
210 }
211 
212 
213 /***********************************************************************//**
214  * @brief Binary matrix scalar addition
215  *
216  * @param[in] scalar Scalar.
217  * @return Matrix with @p scalar added.
218  *
219  * Returns a matrix where a @p scalar has been added to each matrix element.
220  ***************************************************************************/
221 inline
222 GMatrix GMatrix::operator+(const double& scalar) const
223 {
224  GMatrix result = *this;
225  result += scalar;
226  return result;
227 }
228 
229 
230 /***********************************************************************//**
231  * @brief Binary matrix subtraction
232  *
233  * @param[in] matrix Matrix.
234  * @return Result of matrix subtraction.
235  *
236  * Returns the difference between two matrices. The method makes use of the
237  * unary subtraction operator.
238  ***************************************************************************/
239 inline
240 GMatrix GMatrix::operator-(const GMatrix& matrix) const
241 {
242  GMatrix result = *this;
243  result -= matrix;
244  return result;
245 }
246 
247 
248 /***********************************************************************//**
249  * @brief Binary matrix scalar subtraction
250  *
251  * @param[in] scalar Scalar.
252  * @return Matrix with @p scalar subtracted.
253  *
254  * Returns a matrix where a @p scalar has been subtracted from each matrix
255  * element.
256  ***************************************************************************/
257 inline
258 GMatrix GMatrix::operator-(const double& scalar) const
259 {
260  GMatrix result = *this;
261  result -= scalar;
262  return result;
263 }
264 
265 
266 /***********************************************************************//**
267  * @brief Binary matrix multiplication
268  *
269  * @param[in] matrix Matrix.
270  * @return Result of matrix multiplication.
271  *
272  * Returns the product of two matrices. The method makes use of the unary
273  * multiplication operator.
274  ***************************************************************************/
275 inline
276 GMatrix GMatrix::operator*(const GMatrix& matrix) const
277 {
278  GMatrix result = *this;
279  result *= matrix;
280  return result;
281 }
282 
283 
284 /***********************************************************************//**
285  * @brief Scale matrix elements
286  *
287  * @param[in] scalar Scale factor.
288  * @return Matrix with elements multiplied by @p scalar.
289  *
290  * Returns a matrix where all elements have been multiplied by the specified
291  * @p scalar value.
292  ***************************************************************************/
293 inline
294 GMatrix& GMatrix::operator*=(const double& scalar)
295 {
296  scale_elements(scalar);
297  return *this;
298 }
299 
300 
301 /***********************************************************************//**
302  * @brief Divide matrix elements
303  *
304  * @param[in] scalar Scalar.
305  * @return Matrix with elements divided by @p scalar.
306  *
307  * Returns a matrix where all elements have been divided by the specified
308  * @p scalar value.
309  ***************************************************************************/
310 inline
311 GMatrix& GMatrix::operator/=(const double& scalar)
312 {
313  *this *= 1.0/scalar;
314  return *this;
315 }
316 
317 
318 /***********************************************************************//**
319  * @brief Return minimum matrix element
320  *
321  * @return Minimum element in matrix.
322  *
323  * Returns the smallest element in the matrix.
324  ***************************************************************************/
325 inline
326 double GMatrix::min(void) const
327 {
328  return get_min_element();
329 }
330 
331 
332 /***********************************************************************//**
333  * @brief Return maximum matrix element
334  *
335  * @return Maximum element in matrix.
336  *
337  * Returns the largest element in the matrix.
338  ***************************************************************************/
339 inline
340 double GMatrix::max(void) const
341 {
342  return get_max_element();
343 }
344 
345 
346 /***********************************************************************//**
347  * @brief Return matrix element sum
348  *
349  * @return Sum of all matrix elements.
350  *
351  * Returns the sum of all matrix elements.
352  ***************************************************************************/
353 inline
354 double GMatrix::sum(void) const
355 {
356  return get_element_sum();
357 }
358 
359 
360 /***********************************************************************//**
361  * @brief Multiply matrix by scalar
362  *
363  * @param[in] matrix Matrix.
364  * @param[in] scalar Scalar.
365  * @return Matrix divided by @p scalar.
366  *
367  * Returns a matrix where each element has been multiplied by a @p scalar.
368  ***************************************************************************/
369 inline
370 GMatrix operator*(const GMatrix& matrix, const double& scalar)
371 {
372  GMatrix result = matrix;
373  result *= scalar;
374  return result;
375 }
376 
377 
378 /***********************************************************************//**
379  * @brief Multiply matrix by scalar
380  *
381  * @param[in] scalar Scalar.
382  * @param[in] matrix Matrix.
383  * @return Matrix divided by @p scalar.
384  *
385  * Returns a matrix where each element has been multiplied by a @p scalar.
386  ***************************************************************************/
387 inline
388 GMatrix operator*(const double& scalar, const GMatrix& matrix)
389 {
390  GMatrix result = matrix;
391  result *= scalar;
392  return result;
393 }
394 
395 
396 /***********************************************************************//**
397  * @brief Divide matrix by scalar
398  *
399  * @param[in] matrix Matrix.
400  * @param[in] scalar Scalar.
401  * @return Matrix divided by @p scalar.
402  *
403  * Returns a matrix where each element has been divided by a @p scalar.
404  ***************************************************************************/
405 inline
406 GMatrix operator/(const GMatrix& matrix, const double& scalar)
407 {
408  GMatrix result = matrix;
409  result /= scalar;
410  return result;
411 }
412 
413 #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:1167
virtual GMatrix & operator/=(const double &scalar)
Divide matrix elements.
Definition: GMatrix.hpp:311
Sparse matrix class interface definition.
GMatrix invert(void) const
Return inverted matrix.
Definition: GMatrix.cpp:876
virtual double & at(const int &row, const int &column)
Return reference to matrix element.
Definition: GMatrix.cpp:549
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:765
virtual GMatrix operator-(void) const
Negate matrix elements.
Definition: GMatrix.cpp:316
virtual std::string classname(void) const
Return class name.
Definition: GMatrix.hpp:157
virtual GVector column(const int &column) const
Extract column as vector from matrix.
Definition: GMatrix.cpp:676
Symmetric matrix class interface definition.
void copy_members(const GMatrix &matrix)
Copy class members.
Definition: GMatrix.cpp:1227
virtual void add_to_column(const int &column, const GVector &vector)
Add vector column into matrix.
Definition: GMatrix.cpp:806
virtual GVector row(const int &row) const
Extract row as vector from matrix.
Definition: GMatrix.cpp:599
void eulery(const double &angle)
Set Euler rotation matrix around y axis.
Definition: GMatrix.cpp:1093
virtual void clear(void)
Clear matrix.
Definition: GMatrix.cpp:511
virtual double max(void) const
Return maximum matrix element.
Definition: GMatrix.hpp:340
void init_members(void)
Initialise class members.
Definition: GMatrix.cpp:1215
virtual GMatrix & operator=(const GMatrix &matrix)
Matrix assignment operator.
Definition: GMatrix.cpp:224
virtual double & operator()(const int &row, const int &column)
Return reference to matrix element.
Definition: GMatrix.hpp:173
void eulerx(const double &angle)
Set Euler rotation matrix around x axis.
Definition: GMatrix.cpp:1056
double * m_data
Matrix data.
GMatrix negate(void) const
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:847
GVector solve(const GVector &vector) const
Solves linear matrix equation.
Definition: GMatrix.cpp:906
void eulerz(const double &angle)
Set Euler rotation matrix around z axis.
Definition: GMatrix.cpp:1130
virtual GMatrix operator+(const GMatrix &matrix) const
Binary matrix addition.
Definition: GMatrix.hpp:205
virtual GVector operator*(const GVector &vector) const
Vector multiplication.
Definition: GMatrix.cpp:284
double get_max_element(void) const
Returns maximum matrix element.
virtual GMatrix & operator-=(const GMatrix &matrix)
Unary matrix subtraction operator.
Definition: GMatrix.cpp:395
GChatter
Definition: GTypemaps.hpp:33
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:955
virtual GMatrix & operator*=(const GMatrix &matrix)
Unary matrix multiplication operator.
Definition: GMatrix.cpp:452
virtual GMatrix * clone(void) const
Clone matrix.
Definition: GMatrix.cpp:529
virtual double min(void) const
Return minimum matrix element.
Definition: GMatrix.hpp:326
void free_members(void)
Delete class members.
Definition: GMatrix.cpp:1237
GMatrix(void)
Void matrix constructor.
Definition: GMatrix.cpp:73
virtual ~GMatrix(void)
Destructor.
Definition: GMatrix.cpp:200
virtual double sum(void) const
Return matrix element sum.
Definition: GMatrix.hpp:354
GMatrix extract_lower_triangle(void) const
Extract lower triangle of matrix.
Definition: GMatrix.cpp:993
GMatrix abs(void) const
Return absolute of matrix.
Definition: GMatrix.cpp:927
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:343
GMatrix extract_upper_triangle(void) const
Extract upper triangle of matrix.
Definition: GMatrix.cpp:1028
void alloc_members(const int &rows, const int &columns)
Allocate matrix memory.
Definition: GMatrix.cpp:1259