KWStyle - itkVariableSizeMatrix.txx
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkVariableSizeMatrix.txx.html,v $
5   Language:  C++
6   Date:      $Date: 2006/01/17 19:15:49 $
7   Version:   $Revision: 1.4 $
8
9   Copyright (c) Insight Software Consortium. All rights reserved.
10   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11
12      This software is distributed WITHOUT ANY WARRANTY; without even 
13      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14      PURPOSE.  See the above copyright notices for more information.
15
16 =========================================================================*/
17 DEF #ifndef _itkVariableSizeMatrix_txx
18 DEF #define _itkVariableSizeMatrix_txx
19
20 #include "itkVariableSizeMatrix.h" 
21 #include "itkNumericTraits.h"
22 #include "vnl/algo/vnl_matrix_inverse.h"
23 #include "vnl/vnl_transpose.h"
24 #include "vnl/vnl_matrix.h"
25
26 namespace itk
27 {
28
29 template<class T>
30 VariableSizeMatrix<T>
31 ::VariableSizeMatrix(unsigned int rows, unsigned int cols) 
32 IND,IND,IND **: m_Matrix(rows,cols) {}
33  
34
35 /**
36  *  Product by a Vector
37  */
38 template<class T>
39 Array<T> 
40 VariableSizeMatrix<T>
41 ::operator*( const Array<T> & vect ) const
42 {
43   unsigned int rows = this->Rows();
44   unsigned int cols = this->Cols();
45   if( vect.Size() != rows )
46     {
47 LEN     itkExceptionMacro( << "Matrix with " << this->Cols() << " columns cannot be "
48         << "multiplied with array of length: " << vect.Size() );
49     }
50   
51   Array<T> result(rows);
52   for( unsigned int r=0; r<this->rows; r++) 
53     {
54     T sum = NumericTraits<T>::Zero;   
55     for( unsigned int c=0; c< this->cols; c++ ) 
56       {
57       sum += m_Matrix(r,c) * vect[c];
58       }
59     result[r] = sum;
60     }
61   return result;
62 }
63
64
65 EML
66  
67 /**
68  *  Product by a matrix
69  */
70 template<class T >
71 VariableSizeMatrix<T>
72 VariableSizeMatrix<T>
73 ::operator*( const Self & matrix ) const
74 {
75   Self result;
76   result = m_Matrix * matrix.m_Matrix;
77   return result;
78 }
79
80   
81 /**
82  *  Matrix Addition 
83  */
84 template<class T >
85 VariableSizeMatrix<T>
86 VariableSizeMatrix<T>
87 ::operator+( const Self & matrix ) const
88 {
89   if( (matrix.Rows() != this->Rows()) ||
90 IND ******(matrix.Cols() != this->Cols()))
91     { 
92     itkExceptionMacro( << "Matrix with size (" << matrix.Rows() << "," << 
93       matrix.Cols() << ") cannot be subtracted from matrix with size (" <<
94       this->Rows() << "," << this->Cols() );
95     }
96   
97   Self result;
98   for( unsigned int r=0; r< this->Rows(); r++) 
99     {
100     for( unsigned int c=0; c< this->Cols(); c++ ) 
101       {
102       result.m_Matrix(r,c) = m_Matrix(r,c) + matrix.m_Matrix(r,c);
103       }
104     }
105   return result;
106 }
107
108   
109
110 /**
111  *  Matrix Addition in-place
112  */
113 template<class T>
114 const VariableSizeMatrix<T> &
115 VariableSizeMatrix<T>
116 ::operator+=( const Self & matrix ) 
117 {
118   if( (matrix.Rows() != this->Rows()) ||
119 IND ******(matrix.Cols() != this->Cols()))
120     { 
121     itkExceptionMacro( << "Matrix with size (" << matrix.Rows() << "," << 
122       matrix.Cols() << ") cannot be subtracted from matrix with size (" <<
123       this->Rows() << "," << this->Cols() );
124     }
125   
126   for( unsigned int r=0; r<this->Rows(); r++) 
127     {
128     for( unsigned int c=0; c<this->Cols(); c++ ) 
129       {
130       m_Matrix(r,c) += matrix.m_Matrix(r,c);
131       }
132     }
133   return *this;
134 }
135
136
137 EML
138   
139 /**
140  *  Matrix Subtraction
141  */
142 template<class T>
143 VariableSizeMatrix<T>
144 VariableSizeMatrix<T>
145 ::operator-( const Self & matrix ) const
146 {
147   if( (matrix.Rows() != this->Rows()) ||
148 IND ******(matrix.Cols() != this->Cols()))
149     { 
150     itkExceptionMacro( << "Matrix with size (" << matrix.Rows() << "," << 
151       matrix.Cols() << ") cannot be subtracted from matrix with size (" <<
152       this->Rows() << "," << this->Cols() );
153     }
154   
155   Self result;
156   for( unsigned int r=0; r<this->Rows(); r++) 
157     {
158     for( unsigned int c=0; c<this->Cols(); c++ ) 
159       {
160       result.m_Matrix(r,c) = m_Matrix(r,c) - matrix.m_Matrix(r,c);
161       }
162     }
163   return result;
164 }
165
166
167 EML
168 /**
169  *  Matrix subtraction in-place 
170  */
171 template<class T>
172 const VariableSizeMatrix<T> &
173 VariableSizeMatrix<T>
174 ::operator-=( const Self & matrix ) 
175 {
176   if( (matrix.Rows() != this->Rows()) ||
177 IND ******(matrix.Cols() != this->Cols()))
178     { 
179     itkExceptionMacro( << "Matrix with size (" << matrix.Rows() << "," << 
180       matrix.Cols() << ") cannot be subtracted from matrix with size (" <<
181       this->Rows() << "," << this->Cols() );
182     }
183   
184   for( unsigned int r=0; r<this->Rows(); r++) 
185     {
186     for( unsigned int c=0; c<this->Cols(); c++ ) 
187       {
188       m_Matrix(r,c) -= matrix.m_Matrix(r,c);
189       }
190     }
191   return *this;
192 }
193
194
195 EML
196 EML
197 EML
198 /**
199  *  Product by a vnl_matrix
200  */
201 template<class T>
202 vnl_matrix<T> 
203 VariableSizeMatrix<T>
204 ::operator*( const vnl_matrix<T> & matrix ) const
205 {
206   return m_Matrix * matrix;
207 }
208
209
210  
211 /**
212  *  Product by a matrix
213  */
214 template<class T>
215 void
216 VariableSizeMatrix<T>
217 ::operator*=( const Self & matrix ) 
218 {
219   m_Matrix *= matrix.m_Matrix;
220 }
221
222  
223 /**
224  *  Product by a vnl_matrix
225  */
226 template<class T>
227 void
228 VariableSizeMatrix<T>
229 ::operator*=( const vnl_matrix<T> & matrix ) 
230 {
231   m_Matrix *= matrix;
232 }
233
234
235 EML
236 /**
237  *  Product by a vnl_vector
238  */
239 template<class T>
240 vnl_vector<T>
241 VariableSizeMatrix<T>
242 ::operator*( const vnl_vector<T> & vc ) const
243 {
244   return m_Matrix * vc;
245 }
246
247
248 EML
249 /**
250  *  Assignment
251  */
252 template<class T>
253 const VariableSizeMatrix<T> &
254 VariableSizeMatrix<T>
255 ::operator=( const Self  & matrix )
256 {
257   m_Matrix = matrix.m_Matrix;
258   return *this;
259 }
260
261 template<class T>
262 const VariableSizeMatrix<T> &
263 VariableSizeMatrix<T>
264 ::operator=( const vnl_matrix<T>  & matrix )
265 {
266   m_Matrix = matrix;
267   return *this;
268 }
269
270 /**
271  *  Comparison
272  */
273 template<class T>
274 bool
275 VariableSizeMatrix<T>
276 ::operator==( const Self & matrix ) const  
277 {
278   if( (matrix.Rows() != this->Rows()) ||
279 IND ******(matrix.Cols() != this->Cols()))
280     { 
281     return false;
282     }
283   bool equal = true;
284   
285   for( unsigned int r=0; r<this->Rows(); r++) 
286     {
287     for( unsigned int c=0; c<this->Cols(); c++ ) 
288       {
289       if (m_Matrix(r,c) != matrix.m_Matrix(r,c))
290         {
291         equal = false;
292         break;
293         }
294       }
295     }
296   return equal;
297 }
298
299 template<class T>
300 bool
301 VariableSizeMatrix<T>
302 ::operator!=( const Self & matrix ) const
303 {
304   return !this->operator==(matrix);
305 }
306
307
308 /**
309  *  Returns the inverse matrix
310  */
311 template<class T>
312 vnl_matrix<T>
313 VariableSizeMatrix<T>
314 ::GetInverse( void ) const
315 {
316   vnl_matrix<T> temp = vnl_matrix_inverse<T>( m_Matrix );
317   return temp;
318 }
319
320
321 /**
322  *  Returns the transposed matrix
323  */
324 template<class T>
325 vnl_matrix<T>
326 VariableSizeMatrix<T>
327 ::GetTranspose( void ) const
328 {
329   return m_Matrix.transpose();
330 }
331
332  
333
334 // end namespace itk
335
336
337 #endif
338

Generated by KWStyle 1.0b on Tuesday January,17 at 02:14:51PM
© Kitware Inc.