CASToR  1.1
Tomographic Reconstruction (PET/SPECT)
 All Classes Files Functions Variables Typedefs Macros Groups Pages
oMatrix.cc
Go to the documentation of this file.
1 
2 /*
3  Implementation of class oMatrix
4 
5  - separators: X
6  - doxygen: X
7  - default initialization: X
8  - CASTOR_DEBUG: none
9  - CASTOR_VERBOSE: none
10 */
11 
18 #include "oMatrix.hh"
19 
20 
21 // =====================================================================
22 // ---------------------------------------------------------------------
23 // ---------------------------------------------------------------------
24 // =====================================================================
25 /*
26  \brief oMatrix constructor.
27  Initialize the member variables to their default values.
28 */
30 {
31  m_lin = 0;
32  m_col = 0;
33 
34  m2p_Mat = NULL;
35 }
36 
37 
38 
39 // =====================================================================
40 // ---------------------------------------------------------------------
41 // ---------------------------------------------------------------------
42 // =====================================================================
43 /*
44  \param nl : a number of lines
45  \param nc : a number of colons
46  \brief oMatrix constructor.
47  Instanciate a Matrix structure with the number of lines and colons provided in parameters
48 */
49 oMatrix::oMatrix(uint16_t nl, uint16_t nc)
50 {
51  m_lin = nl;
52  m_col = nc;
53  m2p_Mat = new FLTNBLUT *[nl];
54 
55  for(uint16_t l=0 ; l<nl ; l++)
56  m2p_Mat[l] = new FLTNBLUT[nc];
57 }
58 
59 
60 
61 // =====================================================================
62 // ---------------------------------------------------------------------
63 // ---------------------------------------------------------------------
64 // =====================================================================
65 /*
66  \brief oMatrix destructor.
67  Free memory of the oMatrix object.
68 */
70 {
71  for(uint16_t l=0 ; l<m_lin ; l++)
72  if(m2p_Mat[l]) delete[] m2p_Mat[l];
73 
74  if(m2p_Mat) delete[] m2p_Mat;
75 }
76 
77 
78 
79 // =====================================================================
80 // ---------------------------------------------------------------------
81 // ---------------------------------------------------------------------
82 // =====================================================================
83 /*
84  \fn Allocate
85  \param nl : a number of lines
86  \param nc : a number of colons
87  \brief Instanciate a Matrix structure with the number of lines and colons provided in parameters
88 */
89 void oMatrix::Allocate(uint16_t nl, uint16_t nc)
90 {
91  // No verbosity in oMatrix structure. Show this only if CASTOR_DEBUG is enabled (for bug tracking)
92  #ifdef CASTOR_DEBUG
93  Cout("oMatrix::Allocate() ...");
94  #endif
95 
96  // Free memory in case the matrix had already been allocated
97  if(m2p_Mat != NULL)
98  {
99  for(uint16_t l=0 ; l<m_lin ; l++)
100  if(m2p_Mat[l]) delete[] m2p_Mat[l];
101 
102  delete[] m2p_Mat;
103  }
104 
105  m_lin = nl;
106  m_col = nc;
107  m2p_Mat = new FLTNBLUT *[nl];
108 
109  for(uint16_t l=0 ; l<nl ; l++)
110  m2p_Mat[l] = new FLTNBLUT[nc];
111 }
112 
113 
114 
115 // =====================================================================
116 // ---------------------------------------------------------------------
117 // ---------------------------------------------------------------------
118 // =====================================================================
119 /*
120  \fn SetMatriceElt
121  \param l : a line index
122  \param c : a colon index
123  \param a_val : a value to initialize the matrix element with
124  \brief set the matrix element corresponding to the argument indices with the provided value.
125  \return 0 if success, positive value otherwise
126 */
127 int oMatrix::SetMatriceElt(uint16_t l, uint16_t c, FLTNBLUT a_val)
128 {
129  if(l>=m_lin || c>=m_col)
130  {
131  Cerr("***** oMatrix::SetMatriceElt()-> Nb of (lin,col) ("<<l+1<<","<<c+1<<") in parameters ");
132  Cerr("> to the number of (lin,col) of this matrix ("<<m_lin<<","<<m_col<<") !" << endl);
133  return 1;
134  }
135 
136  m2p_Mat[l][c] = a_val;
137 
138  return 0;
139 }
140 
141 
142 
143 // =====================================================================
144 // ---------------------------------------------------------------------
145 // ---------------------------------------------------------------------
146 // =====================================================================
154 FLTNBLUT oMatrix::GetMatriceElt(uint16_t l,uint16_t c)
155 {
156  return m2p_Mat[l][c];
157 }
158 
159 
160 
161 // =====================================================================
162 // ---------------------------------------------------------------------
163 // ---------------------------------------------------------------------
164 // =====================================================================
165 /*
166  \fn Multiplication
167  \param ap_Mtx : a line index
168  \param ap_MtxResult : a colon index
169  \brief Multiply the member matrix with the matrix provided in 1st parameter
170  Return the result in the matric provided in 2nd parameter
171  \return 0 if success, positive value otherwise
172 */
173 int oMatrix::Multiplication(oMatrix *ap_Mtx, oMatrix *ap_MtxResult)
174 {
175  double sum=0;
176 
177  if ( m_col != ap_Mtx->m_lin )
178  {
179  Cerr("***** oMatrix::Multiplication()-> Not matching number of colons and lines of the two matrices !");
180  return 1;
181  }
182  else if (ap_MtxResult == NULL)
183  {
184  Cerr("***** oMatrix::Multiplication()-> The resulting matrix has not been allocated !");
185  return 1;
186  }
187  else
188  {
189  for ( uint16_t tl = 0; tl < m_lin; tl++ )
190  for ( uint16_t c = 0; c < ap_Mtx->m_col; c++ )
191  {
192  for ( uint16_t l = 0; l < ap_Mtx->m_lin; l++ )
193  {
194  sum += GetMatriceElt(tl,l) * ap_Mtx->GetMatriceElt(l,c);
195  }
196  ap_MtxResult->SetMatriceElt(tl,c,sum);
197  sum = 0;
198  }
199  }
200 
201  return 0;
202 }
int SetMatriceElt(uint16_t l, uint16_t c, FLTNBLUT a_val)
Set the matrix element corresponding to the argument indices with the provided value.
Definition: oMatrix.cc:127
~oMatrix()
oMatrix destructor. Free memory of the oMatrix object.
Definition: oMatrix.cc:69
oMatrix()
oMatrix constructor. Initialize the member variables to their default values.
Definition: oMatrix.cc:29
#define Cerr(MESSAGE)
#define FLTNBLUT
Definition: gVariables.hh:61
int Multiplication(oMatrix *a_Mtx, oMatrix *a_MtxResult)
Multiply the member matrix with the matrix provided in 1st parameter Return the result in the matric ...
Definition: oMatrix.cc:173
uint16_t m_lin
Definition: oMatrix.hh:84
Declaration of class oMatrix.
FLTNBLUT GetMatriceElt(uint16_t l, uint16_t c)
Definition: oMatrix.cc:154
uint16_t m_col
Definition: oMatrix.hh:85
Structure designed for basic matrices operations.
Definition: oMatrix.hh:20
#define Cout(MESSAGE)
FLTNBLUT ** m2p_Mat
Definition: oMatrix.hh:87
void Allocate(uint16_t nl, uint16_t nc)
Instanciate a Matrix structure with the number of lines and colons provided in parameters.
Definition: oMatrix.cc:89