CLHEP VERSION Reference Documentation
CLHEP Home Page
CLHEP Documentation
CLHEP Bug Reports
Main Page
Namespaces
Classes
Files
File List
File Members
Matrix
Matrix
Matrix/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
&m1);
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
&m2);
75
// Assigns m2 to d, assuming m2 is a diagnal matrix.
76
77
void
assign
(
const
HepSymMatrix
&m2);
78
// Assigns m2 to d, assuming m2 is a diagnal matrix.
79
80
void
assign
(
const
HepDiagMatrix
&m2);
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
&m2);
90
HepDiagMatrix
&
operator-=
(
const
HepDiagMatrix
&m2);
91
// Add or subtract a DiagMatrix.
92
93
HepDiagMatrix
&
operator=
(
const
HepDiagMatrix
&m2);
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
&m1)
const
;
107
// Returns m1*s*m1.T().
108
HepSymMatrix
similarityT
(
const
HepMatrix
&m1)
const
;
109
// Returns m1.T()*s*m1.
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
&m1);
121
// Sub matrix of this SymMatrix is replaced with m1.
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
:
147
inline
HepDiagMatrix_row
(
HepDiagMatrix
&,
int
);
148
inline
double
&
operator[]
(
int
);
149
private
:
150
HepDiagMatrix
& _a;
151
int
_r;
152
};
153
class
HepDiagMatrix_row_const
{
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
;
175
friend
class
HepDiagMatrix_row_const
;
176
friend
class
HepMatrix
;
177
friend
class
HepSymMatrix
;
178
179
friend
HepDiagMatrix
operator*
(
const
HepDiagMatrix
&m1,
180
const
HepDiagMatrix
&m2);
181
friend
HepDiagMatrix
operator+
(
const
HepDiagMatrix
&m1,
182
const
HepDiagMatrix
&m2);
183
friend
HepDiagMatrix
operator-
(
const
HepDiagMatrix
&m1,
184
const
HepDiagMatrix
&m2);
185
friend
HepMatrix
operator*
(
const
HepDiagMatrix
&m1,
const
HepMatrix
&m2);
186
friend
HepMatrix
operator*
(
const
HepMatrix
&m1,
const
HepDiagMatrix
&m2);
187
friend
HepVector
operator*
(
const
HepDiagMatrix
&m1,
const
HepVector &m2);
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 &m1,
const
HepDiagMatrix &m2);
209
HepMatrix
operator*
(
const
HepDiagMatrix &m1,
const
HepMatrix &m2);
210
HepDiagMatrix
operator*
(
double
t,
const
HepDiagMatrix &d1);
211
HepDiagMatrix
operator*
(
const
HepDiagMatrix &d1,
double
t);
212
// Multiplication operators
213
// Note that m *= m1 is always faster than m = m * m1
214
215
HepDiagMatrix
operator/
(
const
HepDiagMatrix &m1,
double
t);
216
// d = d1 / t. (d /= t is faster if you can use it.)
217
218
HepMatrix
operator+
(
const
HepMatrix &m1,
const
HepDiagMatrix &d2);
219
HepMatrix
operator+
(
const
HepDiagMatrix &d1,
const
HepMatrix &m2);
220
HepDiagMatrix
operator+
(
const
HepDiagMatrix &m1,
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 &m1,
const
HepDiagMatrix &d2);
226
HepMatrix
operator-
(
const
HepDiagMatrix &d1,
const
HepMatrix &m2);
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
Generated on Mon May 6 2013 04:04:10 for CLHEP by
1.8.1.2