ML Reference
MeVis/Foundation/Sources/MLLinearAlgebra/mlFloatingPointMatrix.h
Go to the documentation of this file.
00001 // **InsertLicense** code
00002 //=====================================================================================
00004 
00009 //=====================================================================================
00010 #ifndef __mlFloatingPointMatrix_H
00011 #define __mlFloatingPointMatrix_H
00012 
00013 //------------------------------------------------------------------------
00014 // Includes
00015 //----------------------------------------------------------------------
00016 #ifndef __mlLinearAlgebraSystem_H
00017 #include "mlLinearAlgebraSystem.h"
00018 #endif
00019 
00020 //------------------------------------------------------------------------
00021 // Put all stuff in a specific ML_LA_NAMESPACE namespace to avoid
00022 // collisions with other libraries.
00023 //------------------------------------------------------------------------
00024 ML_LA_START_NAMESPACE
00025 
00028 template <class VectorT, size_t size>
00029 class FloatingPointMatrix
00030 {
00031 public:
00033   typedef typename VectorT::ComponentType ComponentType;
00034 
00036   typedef VectorT VectorType;
00037 
00040   enum { RowCount = size };
00041 
00044   enum { ColumnCount = VectorT::Size };
00045 
00048   enum { ComponentCount = RowCount*ColumnCount };
00049 
00050   // Constant indexed read access to row vectors, indexes must not exceed the valid range or errors will occur
00051   // and the value for index 0 is returned.
00052   const VectorT& operator[](const size_t i) const;
00053   
00054   // Indexed read/write access to row vectors, indexes must not exceed the valid range or errors will occur
00055   // and the value for index 0 is returned.
00056   VectorT& operator[](const size_t i);
00057 
00058   // Linear read/write access to matrix elements correctly ordered starting 
00059   // with first, second, ... elements of first row up to last element in last row.
00060   // The first DT may NOT be used as an array base to address all DTs directly
00061   // in memory since values are organized as vectors and not as an array.
00062   // idx has to be smaller than ComponentCount. Other idx values must be avoided by the caller,
00063   // because they lead to an error post and a the return of value at index 0.
00064   ComponentType &linearIndexed(const size_t idx);
00065   
00066   // Linear and constant read access to matrix elements correctly ordered starting 
00067   // with first, second, ... elements of first row up to last element in last row.
00068   // idx has to be smaller than ComponentCount. Other idx values must be avoided by the caller,
00069   // because they lead to an error post and a the return of value at index 0.
00070   ComponentType linearIndexedConst(const size_t idx) const;
00071  
00072   // Return sum of all MLAbs values of all matrix elements. This is useful
00073   // for testing differences of matrices less than a certain value.
00074   ComponentType compAbsSum() const;
00075 
00079   void setValuesFromPtr(const ComponentType* const values);
00080   
00084   void getValuesToPtr(ComponentType* values) const;
00085   
00086 protected:
00088   VectorT v[size];
00089 };
00090 
00091 //-----------------------------------------------------------------------------------
00092 
00093 template <class VectorT, size_t size>
00094 const VectorT& FloatingPointMatrix<VectorT,size>::operator[](const size_t i) const
00095 {
00096   ML_TRACE_IN_TIME_CRITICAL("FloatingPointMatrix::operator[] const");
00097   
00098   if (i >= size){
00099     ML_PRINT_ERROR("FloatingPointMatrix::operator[] const, illegal index", ML_BAD_INDEX, "Using index 0");
00100     return v[0];
00101   }
00102   return v[i];
00103 }
00104 
00105 //-----------------------------------------------------------------------------------
00106 
00107 template <class VectorT, size_t size>
00108 VectorT& FloatingPointMatrix<VectorT,size>::operator[](const size_t i)
00109 {
00110   ML_TRACE_IN_TIME_CRITICAL("FloatingPointMatrix::operator[]");
00111   
00112   if (i >= size){
00113     ML_PRINT_ERROR("FloatingPointMatrix::operator[], illegal index", ML_BAD_INDEX, "Using index 0");
00114     return v[0];
00115   }
00116   return v[i];
00117 }
00118 
00119 //-----------------------------------------------------------------------------------
00120 
00121 template <class VectorT, size_t size>
00122 inline typename VectorT::ComponentType &FloatingPointMatrix<VectorT,size>::linearIndexed(const size_t idx)
00123 {
00124   ML_TRACE_IN_TIME_CRITICAL("FloatingPointMatrix::linearIndexed()");
00125   ML_TRY
00126   {
00127     if (idx < ComponentCount) {
00128       return v[idx/VectorT::Size][idx%VectorT::Size];
00129     } else {
00130       ML_PRINT_ERROR("FloatingPointMatrix::linearIndexed(), illegal index", ML_BAD_INDEX, "Using index 0 instead");
00131     }
00132   }
00133   ML_CATCH_RETHROW;
00134   return v[0][0];
00135 }
00136 
00137 //-----------------------------------------------------------------------------------
00138 
00139 template <class VectorT, size_t size>
00140 inline typename VectorT::ComponentType FloatingPointMatrix<VectorT,size>::linearIndexedConst(const size_t idx) const
00141 {
00142   ML_TRACE_IN_TIME_CRITICAL("FloatingPointMatrix::linearIndexedConst()");
00143   
00144   typename VectorT::ComponentType retVal = v[0][0];
00145   ML_TRY
00146   {
00147     if (idx < ComponentCount) {
00148       retVal = v[idx/VectorT::Size][idx%VectorT::Size];
00149     } else {
00150       ML_PRINT_ERROR("FloatingPointMatrix::linearIndexedConst(), illegal index", ML_BAD_INDEX, "Using index 0 instead");
00151     }
00152   }
00153   ML_CATCH_RETHROW;
00154   return retVal;
00155 }
00156 
00157 //-----------------------------------------------------------------------------------
00158 
00159 template <class VectorT, size_t size>
00160 inline typename VectorT::ComponentType FloatingPointMatrix<VectorT,size>::compAbsSum() const 
00161 {
00162   ML_TRACE_IN_TIME_CRITICAL("FloatingPointMatrix::compAbsSum()");
00163   
00164   typename VectorT::ComponentType retVal = 0;
00165   for (size_t i=0;i<RowCount;i++) {
00166       for (size_t j=0;j<ColumnCount;j++) {
00167         retVal += MLAbs(v[i][j]);
00168       }
00169   }
00170   return retVal;
00171 }
00172 
00173 //-----------------------------------------------------------------------------------
00174 
00175 template <class VectorT, size_t size>
00176 void FloatingPointMatrix<VectorT,size>::setValuesFromPtr(const ComponentType* const values)
00177 {
00178   ML_TRACE_IN_TIME_CRITICAL("FloatingPointMatrix::setValuesFromPtr");
00179   ML_TRY
00180   {
00181     size_t n = 0;
00182     for (size_t i=0;i<RowCount;i++) {
00183       for (size_t j=0;j<ColumnCount;j++) {
00184         v[i][j] = values[n++];
00185       }
00186     }
00187   }
00188   ML_CATCH_RETHROW;
00189 }
00190 
00191 //-----------------------------------------------------------------------------------
00192 
00193 template <class VectorT, size_t size>
00194 void FloatingPointMatrix<VectorT,size>::getValuesToPtr(ComponentType* values) const
00195 {
00196   ML_TRACE_IN_TIME_CRITICAL("FloatingPointMatrix::getValuesFromPtr");
00197   ML_TRY
00198   {
00199     size_t n = 0;
00200     for (size_t i=0;i<RowCount;i++) {
00201       for (size_t j=0;j<ColumnCount;j++) {
00202         values[n++] = v[i][j];
00203       }
00204     }
00205   }
00206   ML_CATCH_RETHROW;
00207 }
00208 
00209 //-----------------------------------------------------------------------------------
00210 
00211 ML_LA_END_NAMESPACE
00212 
00213 
00214 #endif // __mlFloatingPointVector_H
00215 
00216 
00217