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

RandomObjects/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
27namespace CLHEP {
28
29class HepRandom;
30
31class HepMatrix;
32class HepSymMatrix;
33class HepVector;
34
39class HepDiagMatrix: public HepGenMatrix {
40public:
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
52
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
84 // Multiply a DiagMatrix by a floating number
85
87 // Divide a DiagMatrix by a floating number
88
91 // Add or subtract a DiagMatrix.
92
94 // Assignment operator. To assign SymMatrix to DiagMatrix, use d<<s.
95
97 // unary minus, ie. flip the sign of each element.
98
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
107 // Returns hm1*s*hm1.T().
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
145 class HepDiagMatrix_row {
146 public:
148 inline double & operator[](int);
149 private:
150 HepDiagMatrix& _a;
151 int _r;
152 };
154 public:
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
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
170protected:
171 inline int num_size() const;
172
173private:
174 friend class HepDiagMatrix_row;
175 friend class HepDiagMatrix_row_const;
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
205std::ostream& operator<<(std::ostream &s, const HepDiagMatrix &q);
206// Write out Matrix, SymMatrix, DiagMatrix and Vector into ostream.
207
208HepMatrix operator*(const HepMatrix &hm1, const HepDiagMatrix &hm2);
209HepMatrix operator*(const HepDiagMatrix &hm1, const HepMatrix &hm2);
210HepDiagMatrix operator*(double t, const HepDiagMatrix &d1);
211HepDiagMatrix operator*(const HepDiagMatrix &d1, double t);
212// Multiplication operators
213// Note that m *= hm1 is always faster than m = m * hm1
214
215HepDiagMatrix operator/(const HepDiagMatrix &hm1, double t);
216// d = d1 / t. (d /= t is faster if you can use it.)
217
218HepMatrix operator+(const HepMatrix &hm1, const HepDiagMatrix &d2);
219HepMatrix operator+(const HepDiagMatrix &d1, const HepMatrix &hm2);
223// Addition operators
224
225HepMatrix operator-(const HepMatrix &hm1, const HepDiagMatrix &d2);
226HepMatrix operator-(const HepDiagMatrix &d1, const HepMatrix &hm2);
230// Subtraction operators
231
232HepDiagMatrix 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
239using namespace CLHEP;
240#endif
241
242#ifndef HEP_DEBUG_INLINE
243#include "CLHEP/Matrix/DiagMatrix.icc"
244#endif
245
246#endif
HepDiagMatrix_row_const(const HepDiagMatrix &, int)
HepDiagMatrix_row(HepDiagMatrix &, int)
HepDiagMatrix_row operator[](int)
HepDiagMatrix T() const
void assign(const HepSymMatrix &hm2)
virtual ~HepDiagMatrix()
double & fast(int row, int col)
double determinant() const
HepDiagMatrix & operator*=(double t)
HepDiagMatrix sub(int min_row, int max_row) const
HepDiagMatrix & operator/=(double t)
double similarity(const HepVector &) const
HepDiagMatrix & operator+=(const HepDiagMatrix &hm2)
void assign(const HepMatrix &hm2)
void sub(int row, const HepDiagMatrix &hm1)
HepDiagMatrix operator-() const
HepDiagMatrix(int p, int)
HepDiagMatrix(const HepDiagMatrix &hm1)
void assign(const HepDiagMatrix &hm2)
HepSymMatrix similarityT(const HepMatrix &hm1) const
friend HepDiagMatrix operator*(const HepDiagMatrix &hm1, const HepDiagMatrix &hm2)
HepDiagMatrix sub(int min_row, int max_row)
HepSymMatrix similarity(const HepMatrix &hm1) const
HepDiagMatrix(int p, HepRandom &r)
const double & operator()(int row, int col) const
double & operator()(int row, int col)
HepDiagMatrix & operator-=(const HepDiagMatrix &hm2)
double trace() const
friend HepDiagMatrix operator+(const HepDiagMatrix &hm1, const HepDiagMatrix &hm2)
void invert(int &ierr)
HepDiagMatrix & operator=(const HepDiagMatrix &hm2)
const double & fast(int row, int col) const
HepDiagMatrix inverse(int &ierr) const
HepDiagMatrix apply(double(*f)(double, int, int)) const
int num_size() const
HepDiagMatrix inverse() const
void f(void g())
Hep3Vector operator-(const Hep3Vector &, const Hep3Vector &)
Hep3Vector operator+(const Hep3Vector &, const Hep3Vector &)
HepLorentzRotation operator*(const HepRotation &r, const HepLorentzRotation &lt)
std::ostream & operator<<(std::ostream &os, const HepAxisAngle &aa)
Definition AxisAngle.cc:86
HepDiagMatrix dsum(const HepDiagMatrix &s1, const HepDiagMatrix &s2)
HepLorentzVector operator/(const HepLorentzVector &, double a)