CLHEP VERSION Reference Documentation
   
CLHEP Home Page     CLHEP Documentation     CLHEP Bug Reports

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