CLHEP 2.4.6.4
C++ Class Library for High Energy Physics
Loading...
Searching...
No Matches
DiagMatrix.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 software written by Nobu Katayama and Mike Smyth, Cornell University.
9//
10// DiagMatrix is a class for diagonal matrix. This is useful for a covariance
11// matrix of measured quantities since they are uncorrelated to each other
12// and therefore diagonal. It is obviously smaller and faster to manipulate.
13//
14
15#ifndef _DIAGMatrix_H_
16#define _DIAGMatrix_H_
17
18#include <vector>
19
20#include "CLHEP/Matrix/defs.h"
21#include "CLHEP/Matrix/GenMatrix.h"
22
23namespace CLHEP {
24
25class HepRandom;
26
27class HepMatrix;
28class HepSymMatrix;
29class HepVector;
30
31/**
32 * @author
33 * @ingroup matrix
34 */
36public:
37 inline HepDiagMatrix();
38 // Default constructor. Gives 0x0 matrix. Another Matrix can be assigned
39 // to it.
40
41 explicit HepDiagMatrix(int p);
42 HepDiagMatrix(int p, int);
43 // Constructor. Gives p x p diagonal matrix.
44 // With a second argument, either 0 or 1, the matrix is initialized.
45 // 0 means a zero matrix, 1 means the identity matrix.
46
47 HepDiagMatrix(int p, HepRandom &r);
48
49 HepDiagMatrix(const HepDiagMatrix &hm1);
50 // Copy constructor.
51
52 virtual ~HepDiagMatrix();
53 // Destructor.
54
55 inline int num_row() const;
56 inline int num_col() const;
57 // Returns the number of rows/columns. (Should be equal.)
58
59 double &operator()(int row, int col);
60 const double &operator()(int row, int col) const;
61 // Read or write a matrix element. row must be equal to col.
62 // ** Note that indexing starts from (1,1). **
63
64 double &fast(int row, int col);
65 const double &fast(int row, int col) const;
66 // fast element access.
67 // Must be row>=col;
68 // ** Note that indexing starts from (1,1). **
69
70 void assign(const HepMatrix &hm2);
71 // Assigns hm2 to d, assuming hm2 is a diagnal matrix.
72
73 void assign(const HepSymMatrix &hm2);
74 // Assigns hm2 to d, assuming hm2 is a diagnal matrix.
75
76 void assign(const HepDiagMatrix &hm2);
77 // Another form of assignment. For consistency.
78
79 HepDiagMatrix & operator*=(double t);
80 // Multiply a DiagMatrix by a floating number
81
82 HepDiagMatrix & operator/=(double t);
83 // Divide a DiagMatrix by a floating number
84
87 // Add or subtract a DiagMatrix.
88
90 // Assignment operator. To assign SymMatrix to DiagMatrix, use d<<s.
91
93 // unary minus, ie. flip the sign of each element.
94
96 // Returns the transpose of a DiagMatrix (which is itself).
97
98 HepDiagMatrix apply(double (*f)(double,
99 int, int)) const;
100 // Apply a function to all elements of the matrix.
101
102 HepSymMatrix similarity(const HepMatrix &hm1) const;
103 // Returns hm1*s*hm1.T().
104 HepSymMatrix similarityT(const HepMatrix &hm1) const;
105 // Returns hm1.T()*s*hm1.
106
107 double similarity(const HepVector &) const;
108 // Returns v.T()*s*v (This is a scaler).
109
110 HepDiagMatrix sub(int min_row, int max_row) const;
111 // Returns a sub matrix of a SymMatrix.
112 HepDiagMatrix sub(int min_row, int max_row);
113 // SGI CC bug. I have to have both with/without const. I should not need
114 // one without const.
115
116 void sub(int row, const HepDiagMatrix &hm1);
117 // Sub matrix of this SymMatrix is replaced with hm1.
118
119 HepDiagMatrix inverse(int&ierr) const;
120 // Invert a Matrix. The matrix is not changed
121 // Returns 0 when successful, otherwise non-zero.
122
123 void invert(int&ierr);
124 // Invert a Matrix.
125 // N.B. the contents of the matrix are replaced by the inverse.
126 // Returns ierr = 0 when successful, otherwise non-zero.
127 // This method has less overhead then inverse().
128
129 inline void invert();
130 // Invert a matrix. Throw std::runtime_error on failure.
131
132 inline HepDiagMatrix inverse() const;
133 // Invert a matrix. Throw std::runtime_error on failure.
134
135 double determinant() const;
136 // calculate the determinant of the matrix.
137
138 double trace() const;
139 // calculate the trace of the matrix (sum of diagonal elements).
140
142 public:
144 inline double & operator[](int);
145 private:
146 HepDiagMatrix& _a;
147 int _r;
148 };
150 public:
152 inline const double & operator[](int) const;
153 private:
154 const HepDiagMatrix& _a;
155 int _r;
156 };
157 // helper classes to implement m[i][j]
158
161 // Read or write a matrix element.
162 // While it may not look like it, you simply do m[i][j] to get an
163 // element.
164 // ** Note that the indexing starts from [0][0]. **
165
166protected:
167 inline int num_size() const;
168
169private:
170 friend class HepDiagMatrix_row;
172 friend class HepMatrix;
173 friend class HepSymMatrix;
174
175 friend HepDiagMatrix operator*(const HepDiagMatrix &hm1,
176 const HepDiagMatrix &hm2);
177 friend HepDiagMatrix operator+(const HepDiagMatrix &hm1,
178 const HepDiagMatrix &hm2);
179 friend HepDiagMatrix operator-(const HepDiagMatrix &hm1,
180 const HepDiagMatrix &hm2);
181 friend HepMatrix operator*(const HepDiagMatrix &hm1, const HepMatrix &hm2);
182 friend HepMatrix operator*(const HepMatrix &hm1, const HepDiagMatrix &hm2);
183 friend HepVector operator*(const HepDiagMatrix &hm1, const HepVector &hm2);
184
185#ifdef DISABLE_ALLOC
186 std::vector<double > m;
187#else
188 std::vector<double,Alloc<double,25> > m;
189#endif
190 int nrow;
191#if defined(__sun) || !defined(__GNUG__)
192//
193// Sun CC 4.0.1 has this bug.
194//
195 static double zero;
196#else
197 static const double zero;
198#endif
199};
200
201std::ostream& operator<<(std::ostream &s, const HepDiagMatrix &q);
202// Write out Matrix, SymMatrix, DiagMatrix and Vector into ostream.
203
204HepMatrix operator*(const HepMatrix &hm1, const HepDiagMatrix &hm2);
205HepMatrix operator*(const HepDiagMatrix &hm1, const HepMatrix &hm2);
206HepDiagMatrix operator*(double t, const HepDiagMatrix &d1);
207HepDiagMatrix operator*(const HepDiagMatrix &d1, double t);
208// Multiplication operators
209// Note that m *= hm1 is always faster than m = m * hm1
210
211HepDiagMatrix operator/(const HepDiagMatrix &hm1, double t);
212// d = d1 / t. (d /= t is faster if you can use it.)
213
214HepMatrix operator+(const HepMatrix &hm1, const HepDiagMatrix &d2);
215HepMatrix operator+(const HepDiagMatrix &d1, const HepMatrix &hm2);
219// Addition operators
220
221HepMatrix operator-(const HepMatrix &hm1, const HepDiagMatrix &d2);
222HepMatrix operator-(const HepDiagMatrix &d1, const HepMatrix &hm2);
226// Subtraction operators
227
228HepDiagMatrix dsum(const HepDiagMatrix &s1, const HepDiagMatrix &s2);
229// Direct sum of two diagonal matricies;
230
231} // namespace CLHEP
232
233#ifdef ENABLE_BACKWARDS_COMPATIBILITY
234// backwards compatibility will be enabled ONLY in CLHEP 1.9
235using namespace CLHEP;
236#endif
237
238#ifndef HEP_DEBUG_INLINE
239#include "CLHEP/Matrix/DiagMatrix.icc"
240#endif
241
242#endif /*!_DIAGMatrix_H*/
HepDiagMatrix_row_const(const HepDiagMatrix &, int)
HepDiagMatrix_row(HepDiagMatrix &, int)
HepDiagMatrix_row operator[](int)
HepDiagMatrix T() const
double & fast(int row, int col)
double determinant() const
Definition: DiagMatrix.cc:711
HepDiagMatrix & operator*=(double t)
Definition: DiagMatrix.cc:521
HepDiagMatrix sub(int min_row, int max_row) const
Definition: DiagMatrix.cc:118
HepDiagMatrix & operator/=(double t)
Definition: DiagMatrix.cc:515
HepDiagMatrix & operator+=(const HepDiagMatrix &hm2)
Definition: DiagMatrix.cc:476
virtual ~HepDiagMatrix()
Definition: DiagMatrix.cc:103
void assign(const HepMatrix &hm2)
Definition: DiagMatrix.cc:598
HepDiagMatrix operator-() const
Definition: DiagMatrix.cc:176
void assign(const HepDiagMatrix &hm2)
HepSymMatrix similarityT(const HepMatrix &hm1) const
Definition: DiagMatrix.cc:671
friend HepDiagMatrix operator*(const HepDiagMatrix &hm1, const HepDiagMatrix &hm2)
Definition: DiagMatrix.cc:413
HepSymMatrix similarity(const HepMatrix &hm1) const
Definition: DiagMatrix.cc:628
const double & operator()(int row, int col) const
double & operator()(int row, int col)
HepDiagMatrix & operator-=(const HepDiagMatrix &hm2)
Definition: DiagMatrix.cc:508
double trace() const
Definition: DiagMatrix.cc:719
friend HepDiagMatrix operator+(const HepDiagMatrix &hm1, const HepDiagMatrix &hm2)
Definition: DiagMatrix.cc:221
HepDiagMatrix & operator=(const HepDiagMatrix &hm2)
Definition: DiagMatrix.cc:547
const double & fast(int row, int col) const
HepDiagMatrix inverse(int &ierr) const
HepDiagMatrix apply(double(*f)(double, int, int)) const
Definition: DiagMatrix.cc:582
int num_size() const
HepDiagMatrix inverse() const
void f(void g())
Definition: excDblThrow.cc:38
std::ostream & operator<<(std::ostream &s, const HepDiagMatrix &q)
Definition: DiagMatrix.cc:560
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