CLHEP 2.4.6.4
C++ Class Library for High Energy Physics
Loading...
Searching...
No Matches
SymMatrix.h
Go to the documentation of this file.
1// -*- C++ -*-
2// CLASSDOC OFF
3// ---------------------------------------------------------------------------
4// CLASSDOC ON
5//
6// This file is a part of the CLHEP - a Class Library for High Energy Physics.
7//
8// This is the definition of the HepSymMatrix class.
9//
10// This software written by Nobu Katayama and Mike Smyth, Cornell University.
11//
12// .SS Usage
13//
14// This is very much like the Matrix, except of course it is restricted to
15// Symmetric Matrix. All the operations for Matrix can also be done here
16// (except for the +=,-=,*= that don't yield a symmetric matrix. e.g.
17// +=(const Matrix &) is not defined)
18
19// The matrix is stored as a lower triangular matrix.
20// In addition to the (row, col) method of finding element, fast(row, col)
21// returns an element with checking to see if row and col need to be
22// interchanged so that row >= col.
23
24// New operations are:
25//
26// .ft B
27// sym = s.similarity(m);
28//
29// This returns m*s*m.T(). This is a similarity
30// transform when m is orthogonal, but nothing
31// restricts m to be orthogonal. It is just
32// a more efficient way to calculate m*s*m.T,
33// and it realizes that this should be a
34// HepSymMatrix (the explicit operation m*s*m.T
35// will return a Matrix, not realizing that
36// it is symmetric).
37//
38// .ft B
39// sym = similarityT(m);
40//
41// This returns m.T()*s*m.
42//
43// .ft B
44// s << m;
45//
46// This takes the matrix m, and treats it
47// as symmetric matrix that is copied to s.
48// This is useful for operations that yield
49// symmetric matrix, but which the computer
50// is too dumb to realize.
51//
52// .ft B
53// s = vT_times_v(const HepVector v);
54//
55// calculates v.T()*v.
56//
57// ./"This code has been written by Mike Smyth, and the algorithms used are
58// ./"described in the thesis "A Tracking Library for a Silicon Vertex Detector"
59// ./"(Mike Smyth, Cornell University, June 1993).
60// ./"This is file contains C++ stuff for doing things with Matrixes.
61// ./"To turn on bound checking, define MATRIX_BOUND_CHECK before including
62// ./"this file.
63//
64
65#ifndef _SYMMatrix_H_
66#define _SYMMatrix_H_
67
68#include <vector>
69
70#include "CLHEP/Matrix/defs.h"
71#include "CLHEP/Matrix/GenMatrix.h"
72#include "CLHEP/Utility/thread_local.h"
73
74namespace CLHEP {
75
76class HepRandom;
77
78class HepMatrix;
79class HepDiagMatrix;
80class HepVector;
81
82/**
83 * @author
84 * @ingroup matrix
85 */
86class HepSymMatrix : public HepGenMatrix {
87public:
88 inline HepSymMatrix();
89 // Default constructor. Gives 0x0 symmetric matrix.
90 // Another SymMatrix can be assigned to it.
91
92 explicit HepSymMatrix(int p);
93 HepSymMatrix(int p, int);
94 // Constructor. Gives p x p symmetric matrix.
95 // With a second argument, the matrix is initialized. 0 means a zero
96 // matrix, 1 means the identity matrix.
97
98 HepSymMatrix(int p, HepRandom &r);
99
100 HepSymMatrix(const HepSymMatrix &hm1);
101 // Copy constructor.
102
103 HepSymMatrix(const HepDiagMatrix &hm1);
104 // Constructor from DiagMatrix
105
106 virtual ~HepSymMatrix();
107 // Destructor.
108
109 inline int num_row() const;
110 inline int num_col() const;
111 // Returns number of rows/columns.
112
113 const double & operator()(int row, int col) const;
114 double & operator()(int row, int col);
115 // Read and write a SymMatrix element.
116 // ** Note that indexing starts from (1,1). **
117
118 const double & fast(int row, int col) const;
119 double & fast(int row, int col);
120 // fast element access.
121 // Must be row>=col;
122 // ** Note that indexing starts from (1,1). **
123
124 void assign(const HepMatrix &hm2);
125 // Assigns hm2 to s, assuming hm2 is a symmetric matrix.
126
127 void assign(const HepSymMatrix &hm2);
128 // Another form of assignment. For consistency.
129
130 HepSymMatrix & operator*=(double t);
131 // Multiply a SymMatrix by a floating number.
132
133 HepSymMatrix & operator/=(double t);
134 // Divide a SymMatrix by a floating number.
135
136 HepSymMatrix & operator+=( const HepSymMatrix &hm2);
138 HepSymMatrix & operator-=( const HepSymMatrix &hm2);
140 // Add or subtract a SymMatrix.
141
142 HepSymMatrix & operator=( const HepSymMatrix &hm2);
143 HepSymMatrix & operator=( const HepDiagMatrix &hm2);
144 // Assignment operators. Notice that there is no SymMatrix = Matrix.
145
146 HepSymMatrix operator- () const;
147 // unary minus, ie. flip the sign of each element.
148
150 // Returns the transpose of a SymMatrix (which is itself).
151
152 HepSymMatrix apply(double (*f)(double, int, int)) const;
153 // Apply a function to all elements of the matrix.
154
155 HepSymMatrix similarity(const HepMatrix &hm1) const;
156 HepSymMatrix similarity(const HepSymMatrix &hm1) const;
157 // Returns hm1*s*hm1.T().
158
159 HepSymMatrix similarityT(const HepMatrix &hm1) const;
160 // temporary. test of new similarity.
161 // Returns hm1.T()*s*hm1.
162
163 double similarity(const HepVector &v) const;
164 // Returns v.T()*s*v (This is a scaler).
165
166 HepSymMatrix sub(int min_row, int max_row) const;
167 // Returns a sub matrix of a SymMatrix.
168 void sub(int row, const HepSymMatrix &hm1);
169 // Sub matrix of this SymMatrix is replaced with hm1.
170 HepSymMatrix sub(int min_row, int max_row);
171 // SGI CC bug. I have to have both with/without const. I should not need
172 // one without const.
173
174 inline HepSymMatrix inverse(int &ifail) const;
175 // Invert a Matrix. The matrix is not changed
176 // Returns 0 when successful, otherwise non-zero.
177
178 void invert(int &ifail);
179 // Invert a Matrix.
180 // N.B. the contents of the matrix are replaced by the inverse.
181 // Returns ierr = 0 when successful, otherwise non-zero.
182 // This method has less overhead then inverse().
183
184 inline void invert();
185 // Invert a matrix. Throw std::runtime_error on failure.
186
187 inline HepSymMatrix inverse() const;
188 // Invert a matrix. Throw std::runtime_error on failure.
189
190 double determinant() const;
191 // calculate the determinant of the matrix.
192
193 double trace() const;
194 // calculate the trace of the matrix (sum of diagonal elements).
195
197 public:
199 inline double & operator[](int);
200 private:
201 HepSymMatrix& _a;
202 int _r;
203 };
205 public:
207 inline const double & operator[](int) const;
208 private:
209 const HepSymMatrix& _a;
210 int _r;
211 };
212 // helper class to implement m[i][j]
213
216 // Read or write a matrix element.
217 // While it may not look like it, you simply do m[i][j] to get an
218 // element.
219 // ** Note that the indexing starts from [0][0]. **
220
221 // Special-case inversions for 5x5 and 6x6 symmetric positive definite:
222 // These set ifail=0 and invert if the matrix was positive definite;
223 // otherwise ifail=1 and the matrix is left unaltered.
224 void invertCholesky5 (int &ifail);
225 void invertCholesky6 (int &ifail);
226
227 // Inversions for 5x5 and 6x6 forcing use of specific methods: The
228 // behavior (though not the speed) will be identical to invert(ifail).
229 void invertHaywood4 (int & ifail);
230 void invertHaywood5 (int &ifail);
231 void invertHaywood6 (int &ifail);
232 void invertBunchKaufman (int &ifail);
233
234protected:
235 inline int num_size() const;
236
237private:
238 friend class HepSymMatrix_row;
240 friend class HepMatrix;
241 friend class HepDiagMatrix;
242
243 friend void tridiagonal(HepSymMatrix *a,HepMatrix *hsm);
244 friend double condition(const HepSymMatrix &m);
245 friend void diag_step(HepSymMatrix *t,int begin,int end);
246 friend void diag_step(HepSymMatrix *t,HepMatrix *u,int begin,int end);
248 friend HepVector house(const HepSymMatrix &a,int row,int col);
249 friend void house_with_update2(HepSymMatrix *a,HepMatrix *v,int row,int col);
250
251 friend HepSymMatrix operator+(const HepSymMatrix &hm1,
252 const HepSymMatrix &hm2);
253 friend HepSymMatrix operator-(const HepSymMatrix &hm1,
254 const HepSymMatrix &hm2);
255 friend HepMatrix operator*(const HepSymMatrix &hm1, const HepSymMatrix &hm2);
256 friend HepMatrix operator*(const HepSymMatrix &hm1, const HepMatrix &hm2);
257 friend HepMatrix operator*(const HepMatrix &hm1, const HepSymMatrix &hm2);
258 friend HepVector operator*(const HepSymMatrix &hm1, const HepVector &hm2);
259 // Multiply a Matrix by a Matrix or Vector.
260
261 friend HepSymMatrix vT_times_v(const HepVector &v);
262 // Returns v * v.T();
263
264#ifdef DISABLE_ALLOC
265 std::vector<double > m;
266#else
267 std::vector<double,Alloc<double,25> > m;
268#endif
269 int nrow;
270 int size_; // total number of elements
271
272 static CLHEP_THREAD_LOCAL double posDefFraction5x5;
273 static CLHEP_THREAD_LOCAL double adjustment5x5;
274 static const double CHOLESKY_THRESHOLD_5x5;
275 static const double CHOLESKY_CREEP_5x5;
276
277 static CLHEP_THREAD_LOCAL double posDefFraction6x6;
278 static CLHEP_THREAD_LOCAL double adjustment6x6;
279 static const double CHOLESKY_THRESHOLD_6x6;
280 static const double CHOLESKY_CREEP_6x6;
281
282 void invert4 (int & ifail);
283 void invert5 (int & ifail);
284 void invert6 (int & ifail);
285
286};
287
288//
289// Operations other than member functions for Matrix, SymMatrix, DiagMatrix
290// and Vectors implemented in Matrix.cc and Matrix.icc (inline).
291//
292
293std::ostream& operator<<(std::ostream &s, const HepSymMatrix &q);
294// Write out Matrix, SymMatrix, DiagMatrix and Vector into ostream.
295
296HepMatrix operator*(const HepMatrix &hm1, const HepSymMatrix &hm2);
297HepMatrix operator*(const HepSymMatrix &hm1, const HepMatrix &hm2);
298HepMatrix operator*(const HepSymMatrix &hm1, const HepSymMatrix &hm2);
299HepSymMatrix operator*(double t, const HepSymMatrix &s1);
300HepSymMatrix operator*(const HepSymMatrix &s1, double t);
301// Multiplication operators.
302// Note that m *= hm1 is always faster than m = m * hm1
303
304HepSymMatrix operator/(const HepSymMatrix &hm1, double t);
305// s = s1 / t. (s /= t is faster if you can use it.)
306
307HepMatrix operator+(const HepMatrix &hm1, const HepSymMatrix &s2);
308HepMatrix operator+(const HepSymMatrix &s1, const HepMatrix &hm2);
309HepSymMatrix operator+(const HepSymMatrix &s1, const HepSymMatrix &s2);
310// Addition operators
311
312HepMatrix operator-(const HepMatrix &hm1, const HepSymMatrix &s2);
313HepMatrix operator-(const HepSymMatrix &hm1, const HepMatrix &hm2);
314HepSymMatrix operator-(const HepSymMatrix &s1, const HepSymMatrix &s2);
315// subtraction operators
316
317HepSymMatrix dsum(const HepSymMatrix &s1, const HepSymMatrix &s2);
318// Direct sum of two symmetric matrices;
319
320double condition(const HepSymMatrix &m);
321// Find the conditon number of a symmetric matrix.
322
323void diag_step(HepSymMatrix *t, int begin, int end);
324void diag_step(HepSymMatrix *t, HepMatrix *u, int begin, int end);
325// Implicit symmetric QR step with Wilkinson Shift
326
328// Diagonalize a symmetric matrix.
329// It returns the matrix U so that s_old = U * s_diag * U.T()
330
331HepVector house(const HepSymMatrix &a, int row=1, int col=1);
332void house_with_update2(HepSymMatrix *a, HepMatrix *v, int row=1, int col=1);
333// Finds and does Householder reflection on matrix.
334
335void tridiagonal(HepSymMatrix *a, HepMatrix *hsm);
337// Does a Householder tridiagonalization of a symmetric matrix.
338
339} // namespace CLHEP
340
341#ifdef ENABLE_BACKWARDS_COMPATIBILITY
342// backwards compatibility will be enabled ONLY in CLHEP 1.9
343using namespace CLHEP;
344#endif
345
346#ifndef HEP_DEBUG_INLINE
347#include "CLHEP/Matrix/SymMatrix.icc"
348#endif
349
350#endif /*!_SYMMatrix_H*/
HepSymMatrix_row_const(const HepSymMatrix &, int)
HepSymMatrix_row(HepSymMatrix &, int)
HepSymMatrix & operator*=(double t)
Definition: SymMatrix.cc:611
double trace() const
Definition: SymMatrix.cc:954
friend double condition(const HepSymMatrix &m)
friend void house_with_update2(HepSymMatrix *a, HepMatrix *v, int row, int col)
double & fast(int row, int col)
void assign(const HepSymMatrix &hm2)
void invertHaywood5(int &ifail)
double determinant() const
Definition: SymMatrix.cc:940
int num_row() const
void invertCholesky6(int &ifail)
void invertHaywood4(int &ifail)
HepSymMatrix_row operator[](int)
HepSymMatrix & operator+=(const HepSymMatrix &hm2)
Definition: SymMatrix.cc:575
int num_col() const
void assign(const HepMatrix &hm2)
Definition: SymMatrix.cc:715
HepSymMatrix apply(double(*f)(double, int, int)) const
Definition: SymMatrix.cc:697
friend void tridiagonal(HepSymMatrix *a, HepMatrix *hsm)
int num_size() const
friend HepMatrix diagonalize(HepSymMatrix *s)
HepSymMatrix T() const
friend void diag_step(HepSymMatrix *t, int begin, int end)
HepSymMatrix similarityT(const HepMatrix &hm1) const
Definition: SymMatrix.cc:813
HepSymMatrix & operator=(const HepSymMatrix &hm2)
Definition: SymMatrix.cc:642
const double & operator()(int row, int col) const
void invertCholesky5(int &ifail)
HepSymMatrix sub(int min_row, int max_row) const
Definition: SymMatrix.cc:131
friend HepMatrix operator*(const HepSymMatrix &hm1, const HepSymMatrix &hm2)
Definition: SymMatrix.cc:434
HepSymMatrix operator-() const
Definition: SymMatrix.cc:211
HepSymMatrix & operator-=(const HepSymMatrix &hm2)
Definition: SymMatrix.cc:598
void invertHaywood6(int &ifail)
HepSymMatrix inverse(int &ifail) const
HepSymMatrix similarity(const HepMatrix &hm1) const
Definition: SymMatrix.cc:734
friend HepSymMatrix vT_times_v(const HepVector &v)
Definition: SymMatrix.cc:539
void invertBunchKaufman(int &ifail)
Definition: SymMatrix.cc:961
const double & fast(int row, int col) const
double & operator()(int row, int col)
virtual ~HepSymMatrix()
Definition: SymMatrix.cc:100
friend HepSymMatrix operator+(const HepSymMatrix &hm1, const HepSymMatrix &hm2)
Definition: SymMatrix.cc:253
friend HepVector house(const HepSymMatrix &a, int row, int col)
HepSymMatrix inverse() const
HepSymMatrix & operator/=(double t)
Definition: SymMatrix.cc:605
void f(void g())
Definition: excDblThrow.cc:38
void tridiagonal(HepSymMatrix *a, HepMatrix *hsm)
void house_with_update2(HepSymMatrix *a, HepMatrix *v, int row=1, int col=1)
std::ostream & operator<<(std::ostream &s, const HepDiagMatrix &q)
Definition: DiagMatrix.cc:560
HepVector house(const HepMatrix &a, int row=1, int col=1)
HepMatrix operator+(const HepMatrix &hm1, const HepDiagMatrix &d2)
Definition: DiagMatrix.cc:193
HepMatrix operator-(const HepMatrix &hm1, const HepDiagMatrix &d2)
Definition: DiagMatrix.cc:264
HepDiagMatrix operator/(const HepDiagMatrix &hm1, double t)
Definition: DiagMatrix.cc:335
HepMatrix operator*(const HepMatrix &hm1, const HepDiagMatrix &hm2)
Definition: DiagMatrix.cc:372
HepDiagMatrix dsum(const HepDiagMatrix &s1, const HepDiagMatrix &s2)
Definition: DiagMatrix.cc:161
double condition(const HepSymMatrix &m)
HepMatrix diagonalize(HepSymMatrix *s)
void diag_step(HepSymMatrix *t, int begin, int end)