BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
EvtMatrix< T > Class Template Reference

#include <EvtMatrix.hh>

Public Member Functions

 EvtMatrix ()
 ~EvtMatrix ()
void setRange (int range)
T & operator() (int row, int col)
T * operator[] (int row)
det ()
EvtMatrixmin (int row, int col)
EvtMatrixinverse ()
std::string dump ()

Friends

template<class M>
EvtMatrix< M > * operator* (const EvtMatrix< M > &left, const EvtMatrix< M > &right)

Detailed Description

template<class T>
class EvtMatrix< T >

Definition at line 20 of file EvtMatrix.hh.

Constructor & Destructor Documentation

◆ EvtMatrix()

template<class T>
EvtMatrix< T >::EvtMatrix ( )
inline

Definition at line 26 of file EvtMatrix.hh.

26: _range( 0 ){};

Referenced by det(), inverse(), min(), and operator*.

◆ ~EvtMatrix()

template<class T>
EvtMatrix< T >::~EvtMatrix ( )

Definition at line 64 of file EvtMatrix.hh.

64 {
65 for ( int row = 0; row < _range; row++ ) delete[] _mat[row];
66 delete[] _mat;
67}

Member Function Documentation

◆ det()

template<class T>
T EvtMatrix< T >::det ( )

Definition at line 82 of file EvtMatrix.hh.

82 {
83 if ( _range == 1 ) return _mat[0][0];
84
85 // There's no need to define the range 2 determinant manually, but it may
86 // speed up the calculation.
87 if ( _range == 2 ) return _mat[0][0] * _mat[1][1] - _mat[0][1] * _mat[1][0];
88
89 T sum = 0.;
90
91 for ( int col = 0; col < _range; col++ )
92 {
93 EvtMatrix<T>* minor = min( 0, col );
94 sum += std::pow( -1., col ) * _mat[0][col] * minor->det();
95 delete minor;
96 }
97
98 return sum;
99}
#define min(a, b)

Referenced by det(), and inverse().

◆ dump()

template<class T>
std::string EvtMatrix< T >::dump ( )

Definition at line 69 of file EvtMatrix.hh.

69 {
71
72 for ( int row = 0; row < _range; row++ )
73 {
74 str << "|";
75 for ( int col = 0; col < _range; col++ ) str << "\t" << _mat[row][col];
76 str << "\t|" << std::endl;
77 }
78
79 return str.str();
80}

◆ inverse()

template<class T>
EvtMatrix< T > * EvtMatrix< T >::inverse ( )

Definition at line 119 of file EvtMatrix.hh.

119 {
121 inv->setRange( _range );
122
123 if ( det() == 0 )
124 {
126 << "This matrix has a null determinant and cannot be inverted. Returning zero matrix."
127 << std::endl;
128 for ( int row = 0; row < _range; row++ )
129 for ( int col = 0; col < _range; col++ ) ( *inv )( row, col ) = 0.;
130 return inv;
131 }
132
133 T determinant = det();
134
135 for ( int row = 0; row < _range; row++ )
136 for ( int col = 0; col < _range; col++ )
137 {
139 inv->_mat[col][row] = std::pow( -1., row + col ) * minor->det() / determinant;
140 delete minor;
141 }
142
143 return inv;
144}
void setRange(int range)
Definition EvtMatrix.hh:41

◆ min()

template<class T>
EvtMatrix< T > * EvtMatrix< T >::min ( int row,
int col )

Definition at line 102 of file EvtMatrix.hh.

102 {
104 minor->setRange( _range - 1 );
105
106 int minIndex = 0;
107
108 for ( int r = 0; r < _range; r++ )
109 for ( int c = 0; c < _range; c++ )
110 if ( ( r != row ) && ( c != col ) )
111 {
112 ( *minor )( minIndex / ( _range - 1 ), minIndex % ( _range - 1 ) ) = _mat[r][c];
113 minIndex++;
114 }
115
116 return minor;
117}

◆ operator()()

template<class T>
T & EvtMatrix< T >::operator() ( int row,
int col )
inline

Definition at line 30 of file EvtMatrix.hh.

30{ return _mat[row][col]; }

◆ operator[]()

template<class T>
T * EvtMatrix< T >::operator[] ( int row)
inline

Definition at line 31 of file EvtMatrix.hh.

31{ return _mat[row]; }

◆ setRange()

template<class T>
void EvtMatrix< T >::setRange ( int range)
inline

Definition at line 41 of file EvtMatrix.hh.

41 {
42 // If the range is changed, delete any previous matrix stored
43 // and allocate elements with the newly specified range.
44 if ( _range != range )
45 {
46 if ( _range )
47 {
48 for ( int row = 0; row < _range; row++ ) delete[] _mat[row];
49 delete[] _mat;
50 }
51
52 _mat = new T*[range];
53 for ( int row = 0; row < range; row++ ) _mat[row] = new T[range];
54
55 // Set the new range.
56 _range = range;
57 }
58
59 // Since user is willing to change the range, reset the matrix elements.
60 for ( int row = 0; row < _range; row++ )
61 for ( int col = 0; col < _range; col++ ) _mat[row][col] = 0.;
62}

Referenced by inverse(), min(), and operator*().

◆ operator*

template<class T>
template<class M>
EvtMatrix< M > * operator* ( const EvtMatrix< M > & left,
const EvtMatrix< M > & right )
friend

The documentation for this class was generated from the following file: