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