BOSS 7.0.3
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 22 of file EvtMatrix.hh.

Constructor & Destructor Documentation

◆ EvtMatrix()

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

Definition at line 28 of file EvtMatrix.hh.

28: _range( 0 ) {};

◆ ~EvtMatrix()

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

Definition at line 73 of file EvtMatrix.hh.

74{
75 for( int row = 0; row < _range; row++ )
76 delete[] _mat[ row ];
77 delete[] _mat;
78}

Member Function Documentation

◆ det()

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

Definition at line 97 of file EvtMatrix.hh.

98{
99 if ( _range == 1 )
100 return _mat[ 0 ][ 0 ];
101
102 // There's no need to define the range 2 determinant manually, but it may
103 // speed up the calculation.
104 if ( _range == 2 )
105 return _mat[ 0 ][ 0 ] * _mat[ 1 ][ 1 ] - _mat[ 0 ][ 1 ] * _mat[ 1 ][ 0 ];
106
107 T sum = 0.;
108
109 for ( int col = 0; col < _range; col++ )
110 {
111 EvtMatrix< T >* minor = min( 0, col );
112 sum += std::pow( -1., col ) * _mat[ 0 ][ col ] * minor->det();
113 delete minor;
114 }
115
116 return sum;
117}

Referenced by EvtMatrix< T >::det(), and EvtMatrix< T >::inverse().

◆ dump()

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

Definition at line 81 of file EvtMatrix.hh.

82{
83 std::ostringstream str;
84
85 for ( int row = 0; row < _range; row++ )
86 {
87 str << "|";
88 for ( int col = 0; col < _range; col++ )
89 str << "\t" << _mat[ row ][ col ];
90 str << "\t|" << std::endl;
91 }
92
93 return str.str();
94}

◆ inverse()

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

Definition at line 140 of file EvtMatrix.hh.

141{
142 EvtMatrix< T >* inv = new EvtMatrix< T >();
143 inv->setRange( _range );
144
145 if ( det() == 0 )
146 {
147 std::cerr << "This matrix has a null determinant and cannot be inverted. Returning zero matrix." << std::endl;
148 for ( int row = 0; row < _range; row++ )
149 for ( int col = 0; col < _range; col++ )
150 (*inv)( row, col ) = 0.;
151 return inv;
152 }
153
154 T determinant = det();
155
156 for ( int row = 0; row < _range; row++ )
157 for ( int col = 0; col < _range; col++ )
158 {
159 EvtMatrix< T >* minor = min( row, col );
160 inv->_mat[col][row] = std::pow( -1., row + col ) * minor->det() / determinant;
161 delete minor;
162 }
163
164 return inv;
165}
void setRange(int range)
Definition: EvtMatrix.hh:45

◆ min()

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

Definition at line 121 of file EvtMatrix.hh.

122{
123 EvtMatrix< T >* minor = new EvtMatrix< T >();
124 minor->setRange( _range - 1 );
125
126 int minIndex = 0;
127
128 for ( int r = 0; r < _range; r++ )
129 for ( int c = 0; c < _range; c++ )
130 if ( ( r != row ) && ( c != col ) )
131 {
132 (*minor)( minIndex / ( _range - 1 ), minIndex % ( _range - 1 ) ) = _mat[ r ][ c ];
133 minIndex++;
134 }
135
136 return minor;
137}

◆ operator()()

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

Definition at line 32 of file EvtMatrix.hh.

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

◆ operator[]()

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

Definition at line 33 of file EvtMatrix.hh.

33{ return _mat[ row ]; }

◆ setRange()

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

Definition at line 45 of file EvtMatrix.hh.

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

Referenced by EvtMatrix< T >::inverse(), EvtMatrix< T >::min(), and operator*().

Friends And Related Function Documentation

◆ 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: