Geant4-11
G4ErrorMatrix.hh
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26//
27// Class Description:
28//
29// Simplified version of CLHEP HepMatrix class
30
31// History:
32// - Imported from CLHEP and modified: P. Arce May 2007
33// --------------------------------------------------------------------
34
35#ifndef G4ErrorMatrix_hh
36#define G4ErrorMatrix_hh
37
38#include <vector>
39#include "G4Types.hh"
40
42
43typedef std::vector<G4double>::iterator G4ErrorMatrixIter;
44typedef std::vector<G4double>::const_iterator G4ErrorMatrixConstIter;
45
47{
48 public: // with description
50 // Default constructor. Gives 0 x 0 matrix.
51 // Another G4ErrorMatrix can be assigned to it.
52
54 // Constructor. Gives an unitialized p x q matrix.
55
57 // Constructor. Gives an initialized p x q matrix.
58 // If i=0, it is initialized to all 0. If i=1, the diagonal elements
59 // are set to 1.0.
60
63 // Copy and move constructor.
64
66 // Constructors from G4ErrorSymG4ErrorMatrix, DiagG4ErrorMatrix and Vector.
67
68 virtual ~G4ErrorMatrix();
69 // Destructor.
70
71 inline virtual G4int num_row() const;
72 // Returns the number of rows.
73
74 inline virtual G4int num_col() const;
75 // Returns the number of columns.
76
77 inline virtual const G4double& operator()(G4int row, G4int col) const;
78 inline virtual G4double& operator()(G4int row, G4int col);
79 // Read or write a matrix element.
80 // ** Note that the indexing starts from (1,1). **
81
83 // Multiply a G4ErrorMatrix by a floating number.
84
86 // Divide a G4ErrorMatrix by a floating number.
87
92 // Add or subtract a G4ErrorMatrix.
93 // When adding/subtracting Vector, G4ErrorMatrix must have num_col of one.
94
98 // Assignment and move operators.
99
100 G4ErrorMatrix operator-() const;
101 // unary minus, ie. flip the sign of each element.
102
104 // Apply a function to all elements of the matrix.
105
106 G4ErrorMatrix T() const;
107 // Returns the transpose of a G4ErrorMatrix.
108
109 G4ErrorMatrix sub(G4int min_row, G4int max_row, G4int min_col,
110 G4int max_col) const;
111 // Returns a sub matrix of a G4ErrorMatrix.
112 // WARNING: rows and columns are numbered from 1
113
114 void sub(G4int row, G4int col, const G4ErrorMatrix& m1);
115 // Sub matrix of this G4ErrorMatrix is replaced with m1.
116 // WARNING: rows and columns are numbered from 1
117
118 inline G4ErrorMatrix inverse(G4int& ierr) const;
119 // Invert a G4ErrorMatrix. G4ErrorMatrix must be square and is not changed.
120 // Returns ierr = 0 (zero) when successful, otherwise non-zero.
121
122 virtual void invert(G4int& ierr);
123 // Invert a G4ErrorMatrix. G4ErrorMatrix must be square.
124 // N.B. the contents of the matrix are replaced by the inverse.
125 // Returns ierr = 0 (zero) when successful, otherwise non-zero.
126 // This method has less overhead then inverse().
127
128 G4double determinant() const;
129 // calculate the determinant of the matrix.
130
131 G4double trace() const;
132 // calculate the trace of the matrix (sum of diagonal elements).
133
135 {
136 typedef std::vector<G4double>::const_iterator G4ErrorMatrixConstIter;
137
138 public:
141
142 private:
145 };
147 {
148 public:
150 const G4double& operator[](G4int) const;
151
152 private:
155 };
156 // helper classes for implementing m[i][j]
157
160 // Read or write a matrix element.
161 // While it may not look like it, you simply do m[i][j] to get an element.
162 // ** Note that the indexing starts from [0][0]. **
163
164 protected:
165 virtual inline G4int num_size() const;
166 virtual void invertHaywood4(G4int& ierr);
167 virtual void invertHaywood5(G4int& ierr);
168 virtual void invertHaywood6(G4int& ierr);
169
170 public:
171 static void error(const char* s);
172
173 private:
174 friend class G4ErrorMatrix_row;
176 friend class G4ErrorSymMatrix;
177 // Friend classes.
178
179 friend G4ErrorMatrix operator+(const G4ErrorMatrix& m1,
180 const G4ErrorMatrix& m2);
181 friend G4ErrorMatrix operator-(const G4ErrorMatrix& m1,
182 const G4ErrorMatrix& m2);
183 friend G4ErrorMatrix operator*(const G4ErrorMatrix& m1,
184 const G4ErrorMatrix& m2);
185 friend G4ErrorMatrix operator*(const G4ErrorMatrix& m1,
186 const G4ErrorSymMatrix& m2);
188 const G4ErrorMatrix& m2);
190 const G4ErrorSymMatrix& m2);
191 // Multiply a G4ErrorMatrix by a G4ErrorMatrix or Vector.
192
193 // solve the system of linear eq
194
198 G4int, G4int, G4int);
199 friend void back_solve(const G4ErrorMatrix& R, G4ErrorMatrix* b);
201 G4int k2, G4int rowmin, G4int rowmax);
202 // Does a column Givens update.
203
205 G4int k2, G4int colmin, G4int colmax);
207 G4int, G4int, G4int);
208 friend void house_with_update(G4ErrorMatrix* a, G4int row, G4int col);
210 G4int col);
212 G4int row, G4int col);
213
214 G4int dfact_matrix(G4double& det, G4int* ir);
215 // factorize the matrix. If successful, the return code is 0. On
216 // return, det is the determinant and ir[] is row-interchange
217 // matrix. See CERNLIB's DFACT routine.
218
220 // invert the matrix. See CERNLIB DFINV.
221
222 std::vector<G4double> m;
223
226};
227
228// Operations other than member functions for G4ErrorMatrix
229
233// Multiplication operators
234// Note that m *= m1 is always faster than m = m * m1.
235
237// m = m1 / t. (m /= t is faster if you can use it.)
238
240// m = m1 + m2;
241// Note that m += m1 is always faster than m = m + m1.
242
244// m = m1 - m2;
245// Note that m -= m1 is always faster than m = m - m1.
246
248// Direct sum of two matrices. The direct sum of A and B is the matrix
249// A 0
250// 0 B
251
252std::ostream& operator<<(std::ostream& s, const G4ErrorMatrix& q);
253// Read in, write out G4ErrorMatrix into a stream.
254
255//
256// Specialized linear algebra functions
257//
258
261// Works like backsolve, except matrix does not need to be upper
262// triangular. For nonsquare matrix, it solves in the least square sense.
263
266// Finds the inverse of a matrix using QR decomposition. Note, often what
267// you really want is solve or backsolve, they can be much quicker than
268// inverse in many calculations.
269
272// Does a QR decomposition of a matrix.
273
275// Solves R*x = b where R is upper triangular. Also has a variation that
276// solves a number of equations of this form in one step, where b is a matrix
277// with each column a different vector. See also solve.
278
280 G4int row, G4int col, G4int row_start, G4int col_start);
281void col_house(G4ErrorMatrix* a, const G4ErrorMatrix& v, G4int row, G4int col,
282 G4int row_start, G4int col_start);
283// Does a column Householder update.
284
286 G4int row_min = 1, G4int row_max = 0);
287// do a column Givens update
288
290 G4int col_min = 1, G4int col_max = 0);
291// do a row Givens update
292
293// void givens(G4double a, G4double b, G4double *c, G4double *s);
294// algorithm 5.1.5 in Golub and Van Loan
295
296// Returns a Householder vector to zero elements.
297
298void house_with_update(G4ErrorMatrix* a, G4int row = 1, G4int col = 1);
300 G4int col = 1);
301// Finds and does Householder reflection on matrix.
302
304 G4int row, G4int col, G4int row_start, G4int col_start);
305void row_house(G4ErrorMatrix* a, const G4ErrorMatrix& v, G4int row, G4int col,
306 G4int row_start, G4int col_start);
307// Does a row Householder update.
308
309#include "G4ErrorMatrix.icc"
310
311#endif
void qr_decomp(G4ErrorMatrix *A, G4ErrorMatrix *hsm)
void col_house(G4ErrorMatrix *a, const G4ErrorMatrix &v, G4double vnormsq, G4int row, G4int col, G4int row_start, G4int col_start)
G4ErrorMatrix qr_solve(const G4ErrorMatrix &A, const G4ErrorMatrix &b)
std::vector< G4double >::iterator G4ErrorMatrixIter
G4ErrorMatrix qr_inverse(const G4ErrorMatrix &A)
void row_givens(G4ErrorMatrix *A, G4double c, G4double s, G4int k1, G4int k2, G4int col_min=1, G4int col_max=0)
void col_givens(G4ErrorMatrix *A, G4double c, G4double s, G4int k1, G4int k2, G4int row_min=1, G4int row_max=0)
G4ErrorMatrix dsum(const G4ErrorMatrix &, const G4ErrorMatrix &)
void row_house(G4ErrorMatrix *a, const G4ErrorMatrix &v, G4double vnormsq, G4int row, G4int col, G4int row_start, G4int col_start)
void house_with_update(G4ErrorMatrix *a, G4int row=1, G4int col=1)
std::vector< G4double >::const_iterator G4ErrorMatrixConstIter
void back_solve(const G4ErrorMatrix &R, G4ErrorMatrix *b)
static constexpr double s
Definition: G4SIunits.hh:154
static constexpr double m2
Definition: G4SIunits.hh:110
double G4double
Definition: G4Types.hh:83
int G4int
Definition: G4Types.hh:85
const G4double A[17]
const G4double & operator[](G4int) const
G4ErrorMatrix_row_const(const G4ErrorMatrix &, G4int)
std::vector< G4double >::const_iterator G4ErrorMatrixConstIter
G4ErrorMatrix_row(G4ErrorMatrix &, G4int)
friend void row_house(G4ErrorMatrix *, const G4ErrorMatrix &, G4double, G4int, G4int, G4int, G4int)
G4ErrorMatrix apply(G4double(*f)(G4double, G4int, G4int)) const
std::vector< G4double > m
virtual void invertHaywood4(G4int &ierr)
const G4ErrorMatrix_row_const operator[](G4int) const
G4ErrorMatrix operator-() const
friend void col_house(G4ErrorMatrix *, const G4ErrorMatrix &, G4double, G4int, G4int, G4int, G4int)
friend void row_givens(G4ErrorMatrix *A, G4double c, G4double s, G4int k1, G4int k2, G4int colmin, G4int colmax)
virtual void invert(G4int &ierr)
G4ErrorMatrix & operator/=(G4double t)
friend void house_with_update(G4ErrorMatrix *a, G4ErrorMatrix *v, G4int row, G4int col)
friend void tridiagonal(G4ErrorSymMatrix *a, G4ErrorMatrix *hsm)
G4ErrorMatrix T() const
friend void house_with_update(G4ErrorMatrix *a, G4int row, G4int col)
G4int dfinv_matrix(G4int *ir)
virtual ~G4ErrorMatrix()
G4int dfact_matrix(G4double &det, G4int *ir)
G4double determinant() const
friend G4ErrorMatrix operator*(const G4ErrorMatrix &m1, const G4ErrorMatrix &m2)
friend G4ErrorMatrix operator+(const G4ErrorMatrix &m1, const G4ErrorMatrix &m2)
virtual void invertHaywood5(G4int &ierr)
G4ErrorMatrix & operator=(const G4ErrorMatrix &m2)
virtual G4int num_col() const
virtual void invertHaywood6(G4int &ierr)
G4ErrorMatrix & operator*=(G4double t)
G4ErrorMatrix & operator-=(const G4ErrorMatrix &m2)
virtual G4int num_size() const
G4ErrorMatrix inverse(G4int &ierr) const
virtual G4int num_row() const
static void error(const char *s)
G4ErrorMatrix_row operator[](G4int)
G4ErrorMatrix(G4ErrorMatrix &&)=default
G4ErrorMatrix & operator+=(const G4ErrorMatrix &m2)
friend void house_with_update2(G4ErrorSymMatrix *a, G4ErrorMatrix *v, G4int row, G4int col)
G4ErrorMatrix & operator=(G4ErrorMatrix &&)=default
G4double trace() const
friend void col_givens(G4ErrorMatrix *A, G4double c, G4double s, G4int k1, G4int k2, G4int rowmin, G4int rowmax)
virtual const G4double & operator()(G4int row, G4int col) const
virtual G4double & operator()(G4int row, G4int col)
G4ErrorMatrix sub(G4int min_row, G4int max_row, G4int min_col, G4int max_col) const
friend void back_solve(const G4ErrorMatrix &R, G4ErrorMatrix *b)
friend G4ErrorMatrix qr_solve(G4ErrorMatrix *, const G4ErrorMatrix &b)
BasicVector3D< float > operator*(const BasicVector3D< float > &v, double a)
BasicVector3D< float > operator/(const BasicVector3D< float > &v, double a)
BasicVector3D< float > operator-(const BasicVector3D< float > &v)
std::ostream & operator<<(std::ostream &, const BasicVector3D< float > &)
BasicVector3D< float > operator+(const BasicVector3D< float > &v)