00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef mrpt_math_forwddecls_H
00029 #define mrpt_math_forwddecls_H
00030
00031 #include <mrpt/config.h>
00032 #include <mrpt/base/link_pragmas.h>
00033 #include <mrpt/utils/types.h>
00034 #include <cmath>
00035 #include <cstdlib>
00036 #include <algorithm>
00037
00038
00039
00040
00041
00042
00043 namespace mrpt
00044 {
00045 namespace utils
00046 {
00047 class BASE_IMPEXP CStream;
00048 template<class T> inline T square(const T x);
00049 }
00050
00051 namespace poses
00052 {
00053 class CPoint2D;
00054 class CPoint3D;
00055 class CPose2D;
00056 class CPose3D;
00057 class CPose3DQuat;
00058 }
00059
00060 namespace math
00061 {
00062 struct TPoint2D;
00063 struct TPoint3D;
00064 struct TPose2D;
00065 struct TPose3D;
00066 struct TPose3DQuat;
00067
00068
00069
00070 enum TMatrixTextFileFormat
00071 {
00072 MATRIX_FORMAT_ENG = 0,
00073 MATRIX_FORMAT_FIXED = 1,
00074 MATRIX_FORMAT_INT = 2
00075 };
00076
00077
00078
00079 enum TConstructorFlags_Matrices
00080 {
00081 UNINITIALIZED_MATRIX = 0
00082 };
00083
00084
00085 template <class T> class CVectorTemplate;
00086
00087 template <class T> class CMatrixTemplate;
00088 template <class T> class BASE_IMPEXP CMatrixTemplateNumeric;
00089 template <typename T,size_t NROWS,size_t NCOLS> class CMatrixFixedNumeric;
00090 template<typename MATRIXTYPE> class CArbitrarySubmatrixView;
00091
00092 template <typename T, std::size_t N> class CArray;
00093 template <typename T> class CArray<T,0>;
00094 template <typename T, std::size_t N> class CArrayPOD;
00095 template <typename T, std::size_t N> class CArrayNumeric;
00096
00097 class BASE_IMPEXP CMatrix;
00098 class BASE_IMPEXP CMatrixD;
00099
00100
00101
00102
00103 #define DECLARE_COMMON_CONTAINERS_MEMBERS(NUMTYPE) \
00104 \
00105 \
00106 inline size_t countNonZero() const { return mrpt::math::countNonZero(*this); } \
00107 \
00108 inline void fill(const NUMTYPE &val) { std::fill(mrpt_autotype::begin(),mrpt_autotype::end(),val); } \
00109 \
00110 inline NUMTYPE maximum(size_t *maxIndex = NULL) const { return mrpt::math::maximum(*this,maxIndex); } \
00111 \
00112 inline NUMTYPE minimum(size_t *minIndex = NULL) const { return mrpt::math::minimum(*this,minIndex ); } \
00113 \
00114 inline void minimum_maximum(NUMTYPE &out_min, NUMTYPE &out_max, \
00115 size_t *minIndex = NULL, size_t *maxIndex = NULL) const \
00116 { mrpt::math::minimum_maximum(*this,out_min,out_max,minIndex,maxIndex); } \
00117 \
00118 inline NUMTYPE norm_inf(size_t *maxIndex = NULL) const { return mrpt::math::norm_inf(*this,maxIndex); } \
00119 \
00120 inline NUMTYPE sumAll() const { return mrpt::math::sum(*this); } \
00121 \
00122 template <typename RET_TYPE> inline RET_TYPE sumAllRetType() const { return mrpt::math::sumRetType<mrpt_autotype,NUMTYPE>(*this); } \
00123 \
00124 template <class CONTAINEROUT> inline void cumsum(CONTAINEROUT &out) const { mrpt::math::cumsum(*this,out); } \
00125 \
00126 template <class CONTAINEROUT> inline CONTAINEROUT cumsum() const { CONTAINEROUT o; mrpt::math::cumsum(*this,o); return o;} \
00127 \
00128 template <class CONTAINER2> inline size_t countCommonElements(const CONTAINER2 &b) const { return mrpt::math::countCommonElements(*this,b); } \
00129 \
00130 inline std::vector<double> histogram(double limit_min,double limit_max,size_t number_bins,bool do_normalization = false ) const { \
00131 return mrpt::math::histogram(*this,limit_min,limit_max,number_bins,do_normalization); } \
00132 \
00133 template <class F> mrpt_autotype & applyToAllElements( NUMTYPE (*function)(NUMTYPE) ) { \
00134 std::for_each(this->begin(),this->end(), function); \
00135 return *this; } \
00136 inline mrpt_autotype & Sqrt() { for (typename mrpt_autotype::iterator it=this->begin();it!=this->end();++it) *it = ::sqrt(*it); return *this; } \
00137 inline mrpt_autotype & Abs() { for (typename mrpt_autotype::iterator it=this->begin();it!=this->end();++it) *it = ::fabs(*it); return *this; } \
00138 inline mrpt_autotype & Square() { for (typename mrpt_autotype::iterator it=this->begin();it!=this->end();++it) *it = mrpt::utils::square(*it); return *this; } \
00139 inline mrpt_autotype & Exp() { for (typename mrpt_autotype::iterator it=this->begin();it!=this->end();++it) *it = ::exp(*it); return *this; } \
00140 inline mrpt_autotype & Log() { for (typename mrpt_autotype::iterator it=this->begin();it!=this->end();++it) *it = ::log(*it); return *this; } \
00141 \
00142 inline NUMTYPE squareNorm() const { return mrpt::math::squareNorm(*this); } \
00143 \
00144 inline NUMTYPE norm() const { return mrpt::math::norm(*this); } \
00145 \
00146 inline double mean() const { return mrpt::math::mean(*this); } \
00147 inline void adjustRange(const NUMTYPE min_val,const NUMTYPE max_val) { mrpt::math::adjustRange(*this,min_val,max_val); } \
00148 inline void normalize(const NUMTYPE min_val=0,const NUMTYPE max_val=1) { mrpt::math::adjustRange(*this,min_val,max_val); } \
00149 inline double std(bool unbiased=true) const { return mrpt::math::stddev(*this); } \
00150 void meanAndStd(double &out_mean, double &out_std, bool unbiased = true) { \
00151 return mrpt::math::meanAndStd(*this,out_mean,out_std,unbiased); } \
00152 \
00153 inline void loadFromTextFileAsVector(const std::string &file) { mrpt::math::detail::loadFromTextFileAsVector(*this,file); } \
00154 \
00155 inline void saveToTextFileAsVector(const std::string &file, mrpt::math::TMatrixTextFileFormat fileFormat = mrpt::math::MATRIX_FORMAT_ENG ,bool asColumnVector=false) const { mrpt::math::detail::saveToTextFileAsVector(*this,file,fileFormat ,asColumnVector); }
00156
00157
00158
00159
00160
00161 #define DECLARE_COMMON_MATRIX_MEMBERS(NUMTYPE) \
00162 DECLARE_COMMON_CONTAINERS_MEMBERS(NUMTYPE) \
00163 \
00164 \
00165 inline mrpt_autotype operator+() {\
00166 return *this;\
00167 }\
00168 inline mrpt_autotype operator-() const {\
00169 mrpt_autotype res(*this);\
00170 for (size_t i=0;i<mrpt_autotype::getRowCount();++i) for (size_t j=0;j<mrpt_autotype::getColCount();++j) res.set_unsafe(i,j,-res.get_unsafe(i,j));\
00171 return res;\
00172 }\
00173 mrpt_autotype &operator+=(const NUMTYPE n) {\
00174 for (size_t i=0;i<mrpt_autotype::getRowCount();++i) for (size_t j=0;j<mrpt_autotype::getColCount();++j) mrpt_autotype::get_unsafe(i,j)+=n;\
00175 return *this;\
00176 }\
00177 mrpt_autotype &operator-=(const NUMTYPE n) {\
00178 for (size_t i=0;i<mrpt_autotype::getRowCount();++i) for (size_t j=0;j<mrpt_autotype::getColCount();++j) mrpt_autotype::get_unsafe(i,j)-=n;\
00179 return *this;\
00180 }\
00181 mrpt_autotype &operator*=(const NUMTYPE n) {\
00182 for (size_t i=0;i<mrpt_autotype::getRowCount();++i) for (size_t j=0;j<mrpt_autotype::getColCount();++j) mrpt_autotype::get_unsafe(i,j)*=n;\
00183 return *this;\
00184 }\
00185 inline mrpt_autotype& operator/=(const NUMTYPE n) {\
00186 return (*this)*=NUMTYPE(1)/n; } \
00187 template<typename OTHERMATRIX> \
00188 inline RET_VOID_ASSERT_MRPTMATRIX(OTHERMATRIX) operator*=(const OTHERMATRIX &m) {\
00189 mrpt::math::detail::multiply_AB(*this,m,*this); } \
00190 template<typename OTHERMATRIX> mrpt_autotype &add_At(const OTHERMATRIX &m) {\
00191 ASSERT_((m.getRowCount()==mrpt_autotype::getColCount())&&(m.getColCount()==mrpt_autotype::getRowCount()));\
00192 for (size_t i=0;i<mrpt_autotype::getRowCount();++i) for (size_t j=0;j<mrpt_autotype::getColCount();++j) mrpt_autotype::get_unsafe(i,j)+=m.get_unsafe(j,i);\
00193 return *this;\
00194 }\
00195 template<typename OTHERMATRIX> mrpt_autotype &add_Ac(const OTHERMATRIX &m,const NUMTYPE c) {\
00196 ASSERT_((m.getRowCount()==mrpt_autotype::getRowCount())&&(m.getColCount()==mrpt_autotype::getColCount()));\
00197 for (size_t i=0;i<mrpt_autotype::getRowCount();++i) for (size_t j=0;j<mrpt_autotype::getColCount();++j) mrpt_autotype::get_unsafe(i,j)+=c*m.get_unsafe(i,j);\
00198 return *this;\
00199 }\
00200 template<typename OTHERMATRIX> mrpt_autotype &substract_At(const OTHERMATRIX &m) {\
00201 ASSERT_((m.getRowCount()==mrpt_autotype::getColCount())&&(m.getColCount()==mrpt_autotype::getRowCount()));\
00202 for (size_t i=0;i<mrpt_autotype::getRowCount();++i) for (size_t j=0;j<mrpt_autotype::getColCount();++j) mrpt_autotype::get_unsafe(i,j)-=m.get_unsafe(j,i);\
00203 return *this;\
00204 } \
00205 \
00206 template<typename OTHERMATRIX> mrpt_autotype & substract_An(const OTHERMATRIX& m, const size_t n) {\
00207 ASSERT_((m.getRowCount()==mrpt_autotype::getRowCount())&&(m.getColCount()==mrpt_autotype::getColCount()));\
00208 for (size_t i=0;i<mrpt_autotype::getRowCount();++i) for (size_t j=0;j<mrpt_autotype::getColCount();++j) mrpt_autotype::get_unsafe(i,j)-=n*m.get_unsafe(i,j);\
00209 return *this;\
00210 }\
00211 \
00212 template<typename OTHERMATRIX> mrpt_autotype &substract_Ac(const OTHERMATRIX &m,const NUMTYPE c) {\
00213 ASSERT_((m.getRowCount()==mrpt_autotype::getRowCount())&&(m.getColCount()==mrpt_autotype::getColCount()));\
00214 for (size_t i=0;i<mrpt_autotype::getRowCount();++i) for (size_t j=0;j<mrpt_autotype::getColCount();++j) mrpt_autotype::get_unsafe(i,j)-=c*m.get_unsafe(i,j);\
00215 return *this;\
00216 }\
00217 \
00218 template<typename OTHERMATRIX> mrpt_autotype &add_AAt(const OTHERMATRIX &m) {\
00219 ASSERT_((mrpt_autotype::getRowCount()==mrpt_autotype::getColCount())&&(mrpt_autotype::getRowCount()==m.getRowCount())&&(mrpt_autotype::getColCount()==m.getColCount()));\
00220 for (size_t i=0;i<mrpt_autotype::getRowCount();++i) for (size_t j=0;j<mrpt_autotype::getColCount();++j) mrpt_autotype::get_unsafe(i,j)+=m.get_unsafe(i,j)+m.get_unsafe(j,i);\
00221 return *this;\
00222 }\
00223 \
00224 template<typename OTHERMATRIX> mrpt_autotype &substract_AAt(const OTHERMATRIX &m) {\
00225 ASSERT_((mrpt_autotype::getRowCount()==mrpt_autotype::getColCount())&&(mrpt_autotype::getRowCount()==m.getRowCount())&&(mrpt_autotype::getColCount()==m.getColCount()));\
00226 for (size_t i=0;i<mrpt_autotype::getRowCount();++i) for (size_t j=0;j<mrpt_autotype::getColCount();++j) mrpt_autotype::get_unsafe(i,j)-=m.get_unsafe(i,j)+m.get_unsafe(j,i);\
00227 return *this;\
00228 }\
00229 \
00230 bool empty() const { return this->getColCount()==0 || this->getRowCount()==0; } \
00231 template <class MATRIX1,class MATRIX2> \
00232 inline void multiply( const MATRIX1& A, const MATRIX2 &B ) { mrpt::math::detail::multiply_AB(A,B,*this); } \
00233 template <class MATRIX1,class MATRIX2> \
00234 inline void multiply_AB( const MATRIX1& A, const MATRIX2 &B ) { mrpt::math::detail::multiply_AB(A,B,*this); } \
00235 template <typename MATRIX1,typename MATRIX2> inline void multiply_AtB(const MATRIX1 &A,const MATRIX2 &B) { mrpt::math::detail::multiply_AtB(A,B,*this); } \
00236 \
00237 template<typename OTHERVECTOR1,typename OTHERVECTOR2> \
00238 inline void multiply_Ab(const OTHERVECTOR1 &vIn,OTHERVECTOR2 &vOut,bool accumToOutput = false) const {\
00239 mrpt::math::detail::multiply_Ab(*this,vIn,vOut,accumToOutput); } \
00240 \
00241 template<typename OTHERVECTOR1,typename OTHERVECTOR2> \
00242 inline void multiply_Atb(const OTHERVECTOR1 &vIn,OTHERVECTOR2 &vOut,bool accumToOutput = false) const {\
00243 mrpt::math::detail::multiply_Atb(*this,vIn,vOut,accumToOutput); } \
00244 template <typename MAT_C, typename MAT_R> \
00245 inline void multiply_HCHt(const MAT_C &C,MAT_R &R,bool accumResultInOutput=false,bool allow_submatrix_mult=false) const { \
00246 mrpt::math::detail::multiply_HCHt(*this,C,R,accumResultInOutput,allow_submatrix_mult); } \
00247 \
00248 template <typename MAT_C> NUMTYPE multiply_HCHt_scalar(const MAT_C &C) const { return mrpt::math::detail::multiply_HCHt_scalar(*this,C); } \
00249 \
00250 template <typename MAT_C> NUMTYPE multiply_HtCH_scalar(const MAT_C &C) const { return mrpt::math::detail::multiply_HCHt_scalar(*this,C); } \
00251 \
00252 template<typename MAT_A> inline void multiply_AAt_scalar(const MAT_A &A,typename MAT_A::value_type f) {\
00253 mrpt::math::detail::multiply_AAt_scalar(A,f,*this); }\
00254 \
00255 template<typename MAT_A> inline void multiply_AtA_scalar(const MAT_A &A,typename MAT_A::value_type f) {\
00256 mrpt::math::detail::multiply_AtA_scalar(A,f,*this); }\
00257 \
00258 template <typename MAT_C, typename MAT_R> \
00259 inline void multiply_HtCH(const MAT_C &C,MAT_R &R,bool accumResultInOutput=false,bool allow_submatrix_mult=false) const { \
00260 mrpt::math::detail::multiply_HtCH(*this,C,R,accumResultInOutput,allow_submatrix_mult); } \
00261 template <class MAT_A,class MAT_OUT> \
00262 inline void multiply_subMatrix(const MAT_A &A,MAT_OUT &outResult,const size_t A_cols_offset,const size_t A_rows_offset,const size_t A_col_count) const { \
00263 mrpt::math::detail::multiply_subMatrix(*this,A,outResult,A_cols_offset,A_rows_offset,A_col_count); } \
00264 template <class MAT_A,class MAT_B,class MAT_C> \
00265 void multiply_ABC(const MAT_A &A, const MAT_B &B, const MAT_C &C) { \
00266 mrpt::math::detail::multiply_ABC(A,B,C, *this); } \
00267 template <class MAT_A,class MAT_B,class MAT_C> \
00268 void multiply_ABCt(const MAT_A &A, const MAT_B &B, const MAT_C &C) { \
00269 mrpt::math::detail::multiply_ABCt(A,B,C, *this); } \
00270 template <class MAT_A,class MAT_B> \
00271 inline void multiply_ABt(const MAT_A &A,const MAT_B &B) { \
00272 mrpt::math::detail::multiply_ABt(A,B,*this); } \
00273 template <class MAT_A> \
00274 inline void multiply_AAt(const MAT_A &A) { \
00275 mrpt::math::detail::multiply_AAt(A,*this); } \
00276 template <class MAT_A> \
00277 inline void multiply_AtA(const MAT_A &A) { \
00278 mrpt::math::detail::multiply_AtA(A,*this); } \
00279 template <class MAT_A,class MAT_B> \
00280 inline void multiply_result_is_symmetric(const MAT_A &A,const MAT_B &B) { \
00281 mrpt::math::detail::multiply_result_is_symmetric(A,B,*this); } \
00282 \
00283 \
00284 template<typename OTHERMATRIX> inline mrpt_autotype &assignMatrix(const OTHERMATRIX &m) {\
00285 setSize(m.getRowCount(),m.getColCount());\
00286 for (size_t i=0;i<mrpt_autotype::getRowCount();++i) for (size_t j=0;j<mrpt_autotype::getColCount();++j) mrpt_autotype::set_unsafe(i,j,m.get_unsafe(i,j));\
00287 return *this;\
00288 }\
00289 \
00290 template<typename OTHERMATRIX> inline mrpt_autotype &assignMatrixTransposed(const OTHERMATRIX &m) {\
00291 setSize(m.getColCount(),m.getRowCount());\
00292 for (size_t i=0;i<m.getRowCount();++i) for (size_t j=0;j<m.getColCount();++j) set_unsafe(j,i,m.get_unsafe(i,j));\
00293 return *this;\
00294 }\
00295 \
00296 \
00297 inline mrpt_autotype & operator=( const TPose2D &p) { this->setSize(3,1); mrpt::math::containerFromPoseOrPoint(*this,p); return *this; } \
00298 inline mrpt_autotype & operator=( const TPose3D &p) { this->setSize(6,1); mrpt::math::containerFromPoseOrPoint(*this,p); return *this;} \
00299 inline mrpt_autotype & operator=( const TPose3DQuat &p) { this->setSize(7,1); mrpt::math::containerFromPoseOrPoint(*this,p); return *this;} \
00300 inline mrpt_autotype & operator=( const TPoint2D &p) { this->setSize(2,1); mrpt::math::containerFromPoseOrPoint(*this,p); return *this;} \
00301 inline mrpt_autotype & operator=( const TPoint3D &p) { this->setSize(3,1); mrpt::math::containerFromPoseOrPoint(*this,p); return *this;} \
00302 inline mrpt_autotype & operator=( const mrpt::poses::CPose2D &p) { this->setSize(3,1); mrpt::math::containerFromPoseOrPoint(*this,p); return *this;} \
00303 inline mrpt_autotype & operator=( const mrpt::poses::CPose3D &p) { this->setSize(6,1); mrpt::math::containerFromPoseOrPoint(*this,p); return *this;} \
00304 inline mrpt_autotype & operator=( const mrpt::poses::CPose3DQuat &p) { this->setSize(7,1); mrpt::math::containerFromPoseOrPoint(*this,p); return *this;} \
00305 inline mrpt_autotype & operator=( const mrpt::poses::CPoint2D &p) { this->setSize(2,1); mrpt::math::containerFromPoseOrPoint(*this,p); return *this;} \
00306 inline mrpt_autotype & operator=( const mrpt::poses::CPoint3D &p) { this->setSize(3,1); mrpt::math::containerFromPoseOrPoint(*this,p); return *this;} \
00307 \
00308 \
00309 inline NUMTYPE _E(const size_t row, const size_t col) const { return mrpt_autotype::get_unsafe(row-1,col-1); } \
00310 \
00311 inline NUMTYPE & _E(const size_t row, const size_t col) { return mrpt_autotype::get_unsafe(row-1,col-1); } \
00312 \
00313 inline NUMTYPE _A(const size_t n) const { return mrpt_autotype::get_unsafe(n/mrpt_autotype::getColCount(),n%mrpt_autotype::getColCount()); } \
00314 \
00315 inline NUMTYPE &_A(const size_t n) { return mrpt_autotype::get_unsafe(n/mrpt_autotype::getColCount(),n%mrpt_autotype::getColCount()); } \
00316 inline bool IsSquare() const { return mrpt_autotype::getColCount()==mrpt_autotype::getRowCount(); } \
00317 inline bool isSquare() const { return mrpt_autotype::getColCount()==mrpt_autotype::getRowCount(); } \
00318 \
00319 inline bool isSingular(const NUMTYPE epsilon=0) const { \
00320 if (!isSquare()) return false; else return std::abs(this->det())<=epsilon; } \
00321 \
00322 inline NUMTYPE trace() const { return mrpt::math::detail::trace(*this); }\
00323 template <class MAT_OUT> \
00324 void pseudoInverse( MAT_OUT &out ) const { \
00325 mrpt_autotype A, Ainv; A.multiply_AtA(*this); A.inv_fast(Ainv); out.multiply_ABt(Ainv,*this); } \
00326 template <class MAT_OUT> \
00327 MAT_OUT pseudoInverse() const { \
00328 mrpt_autotype A, Ainv; A.multiply_AtA(*this); A.inv_fast(Ainv); \
00329 MAT_OUT out; out.multiply_ABt(Ainv,*this); return out; } \
00330 inline NUMTYPE det() const { return mrpt::math::detail::detMatrix(*this); } \
00331 template <class MATRIXOUT> inline void inv( MATRIXOUT& out ) const { mrpt::math::detail::invMatrix(*this,out); } \
00332 inline mrpt_autotype inv() const { mrpt_autotype ret; mrpt::math::detail::invMatrix(*this,ret); return ret; } \
00333 template <class MATRIXOUT> inline void inv_fast( MATRIXOUT& out ) { mrpt::math::detail::invMatrix_destroySrc(*this,out); } \
00334 \
00335 template <class MAT2,class MAT3> inline void leftDivideSquare(const MAT2 &A,MAT3 &RES) const { mrpt::math::detail::leftDivideSquare(*this,A,RES); } \
00336 \
00337 template <class MAT2,class MAT3> inline void rightDivideSquare(const MAT2 &B, MAT3 &RES) const { mrpt::math::detail::rightDivideSquare(*this,B,RES); } \
00338 \
00339 template <typename MAT2> inline void fastLeftDivideSquare(MAT2 &A) { mrpt::math::detail::fastLeftDivideSquare(*this,A); } \
00340 \
00341 template <typename MAT2> inline void fastRightDivideSquare(MAT2 &B) { mrpt::math::detail::fastRightDivideSquare(*this,B); } \
00342 \
00343 inline void saveToTextFile(const std::string &file,mrpt::math::TMatrixTextFileFormat fileFormat = MATRIX_FORMAT_ENG,bool appendMRPTHeader=false,const std::string &userHeader = std::string("") ) const { \
00344 mrpt::math::detail::saveMatrixToTextFile(*this, file,fileFormat,appendMRPTHeader,userHeader); } \
00345 \
00346 inline std::string inMatlabFormat(const size_t decimal_digits=6) const { return detail::matrix_inMatlabFormat(*this,decimal_digits); } \
00347 inline int pivot(const size_t row) { return mrpt::math::detail::matrix_pivot(*this,row); } \
00348 \
00349 \
00350 template <class MATRIXOUT> inline bool chol(MATRIXOUT &out) const { return mrpt::math::detail::chol(*this,out); } \
00351 \
00352 template <class MATRIX1,class MATRIX2> \
00353 inline RET_VOID_ASSERT_MRPTMATRICES(MATRIX1,MATRIX2) eigenVectors( MATRIX1& eVecs, MATRIX2& eVals) const { \
00354 ARRAY_TYPE_SAMESIZE_ROWS_OF(mrpt_autotype) eigVals; \
00355 eigVals.resize(this->getRowCount()); \
00356 mrpt::math::detail::eigenVectorsMatrix(*this, &eVecs,eigVals); \
00357 const size_t n = eigVals.size(); \
00358 eVals.zeros(n,n); for (size_t i=0;i<n;i++) eVals.get_unsafe(i,i)=eigVals[i]; \
00359 } \
00360 \
00361 template <class MATRIX1,class VECTOR1> \
00362 inline void eigenVectorsVec( MATRIX1& eVecs, VECTOR1& eVals) const { mrpt::math::detail::eigenVectorsMatrix(*this, &eVecs,eVals); } \
00363 \
00364 template <class VECTOR1> \
00365 inline void eigenValues( VECTOR1& eVals) const { mrpt::math::detail::eigenVectorsMatrix(*this,static_cast<mrpt_autotype*>(NULL),eVals); } \
00366 inline void setIdentity() {\
00367 ASSERT_(isSquare());\
00368 set_unsafe(0,0,NUMTYPE(1));\
00369 for (size_t i=1;i<mrpt_autotype::getRowCount();++i) {\
00370 set_unsafe(i,i,NUMTYPE(1));\
00371 for (size_t j=0;j<i;++j) {\
00372 set_unsafe(i,j,NUMTYPE(0));\
00373 set_unsafe(j,i,NUMTYPE(0));\
00374 }\
00375 }\
00376 }\
00377 inline void setIdentity(size_t N) {\
00378 setSize(N,N);\
00379 setIdentity();\
00380 }\
00381 inline size_t rank(NUMTYPE eps=1e-7) const {\
00382 return mrpt::math::detail::rank(*this,eps);\
00383 }\
00384 \
00385 template <class MATRIXLIKE> inline void insertMatrix(const size_t nRow, const size_t nCol, const MATRIXLIKE &in) \
00386 { detail::insertMatrixInto(*this,nRow,nCol,in); } \
00387 \
00388 template <class MATRIXLIKE> inline void insertMatrixTranspose(const size_t nRow, const size_t nCol, const MATRIXLIKE &in) \
00389 { detail::insertMatrixTransposeInto(*this,nRow,nCol,in); } \
00390 \
00391 template <class MATOUT> inline void extractMatrix(const size_t first_row, const size_t first_col, MATOUT &outMat) const { detail::extractMatrix(*this,first_row,first_col,outMat); } \
00392 \
00393 template <class MATOUT> inline void extractMatrix(const size_t first_row, const size_t first_col, const size_t nRows, const size_t nCols, MATOUT &outMat) const { outMat.setSize(nRows,nCols); detail::extractMatrix(*this,first_row,first_col,outMat); } \
00394 template <class VECLIKE> inline void extractRow(const size_t nRow, VECLIKE &out, const size_t startingCol = 0) const { detail::extractRowFromMatrix(*this,nRow,out,startingCol); } \
00395 template <class VECLIKE> inline void extractCol(const size_t nCol, VECLIKE &out, const size_t startingRow = 0) const { detail::extractColFromMatrix(*this,nCol,out,startingRow); } \
00396 template <class VECLIKE> inline void insertRow(const size_t nRow, VECLIKE &in, const size_t startingCol = 0) { detail::insertRowToMatrix(*this,nRow,in,startingCol); } \
00397 template <class VECLIKE> inline void insertCol(const size_t nCol, VECLIKE &in, const size_t startingRow = 0) { detail::insertColToMatrix(*this,nCol,in,startingRow); } \
00398
00399
00400
00401
00402 namespace detail
00403 {
00404 template<class MATRIX1,class MATRIX2>
00405 bool chol(const MATRIX1 &in, MATRIX2 &out);
00406 template<class MATRIX1>
00407 typename MATRIX1::value_type trace(const MATRIX1 &m);
00408
00409 template <class MATRIX1,class MATRIX2,class MATRIXRES>
00410 void multiply_AB(const MATRIX1& m1,const MATRIX2& m2, MATRIXRES& RESULT );
00411 template<class MATRIX1, class OTHERVECTOR1,class OTHERVECTOR2>
00412 void multiply_Ab(const MATRIX1 &m, const OTHERVECTOR1 &vIn,OTHERVECTOR2 &vOut,bool accumToOutput );
00413 template<class MATRIX1, class OTHERVECTOR1,class OTHERVECTOR2>
00414 void multiply_Atb(const MATRIX1 &m, const OTHERVECTOR1 &vIn,OTHERVECTOR2 &vOut,bool accumToOutput );
00415 template <class MATRIX1,class MATRIX2>
00416 void multiply_AAt( const MATRIX1& m1, MATRIX2& RESULT );
00417 template <class MATRIX1,class MATRIX2>
00418 void multiply_AtA( const MATRIX1& m1, MATRIX2& RESULT );
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431 template <typename MAT_H, typename MAT_C, typename MAT_R>
00432 void multiply_HCHt(const MAT_H &H,const MAT_C &C,MAT_R &R,bool accumResultInOutput=false,bool allow_submatrix_mult=false);
00433 template <typename MAT_H, typename MAT_C, typename MAT_R>
00434 void multiply_HtCH(const MAT_H &H,const MAT_C &C,MAT_R &R,bool accumResultInOutput=false,bool allow_submatrix_mult=false);
00435 template <typename VECTOR_H, typename MAT_C> typename MAT_C::value_type
00436 multiply_HCHt_scalar(const VECTOR_H &H, const MAT_C &C);
00437
00438 template <class MAT_X,class MAT_A,class MAT_OUT>
00439 void multiply_subMatrix(const MAT_X &X,const MAT_A &A,MAT_OUT &outResult,const size_t A_cols_offset,const size_t A_rows_offset,const size_t A_col_count);
00440
00441 template <class MAT_A,class MAT_B,class MAT_C,class MAT_OUT>
00442 void multiply_ABC(const MAT_A &A, const MAT_B &B, const MAT_C &C, MAT_OUT & RES);
00443 template <class MAT_A,class MAT_B,class MAT_C,class MAT_OUT>
00444 void multiply_ABCt(const MAT_A &A, const MAT_B &B, const MAT_C &C, MAT_OUT & RES);
00445
00446 template <class MAT_A,class MAT_B,class MAT_OUT>
00447 void multiply_ABt(const MAT_A &A,const MAT_B &B, MAT_OUT &out);
00448 template <class MAT_A,class MAT_B,class MAT_OUT>
00449 void multiply_AtB(const MAT_A &A,const MAT_B &B, MAT_OUT &out);
00450 template <class MAT_A,class MAT_OUT>
00451 void multiply_AAt(const MAT_A &A, MAT_OUT &out);
00452 template <class MAT_A,class MAT_OUT>
00453 void multiply_AAt(const MAT_A &A,MAT_OUT &out);
00454 template <class MAT_A,class MAT_B,class MAT_OUT>
00455 void multiply_result_is_symmetric(const MAT_A &A,const MAT_B &B, MAT_OUT &out);
00456 template<typename MAT_IN,typename MAT_OUT>
00457 void multiply_AAt_scalar(const MAT_IN &in,typename MAT_IN::value_type scalar,MAT_OUT &out);
00458 template<typename MAT_IN,typename MAT_OUT>
00459 void multiply_AtA_scalar(const MAT_IN &in,typename MAT_IN::value_type scalar,MAT_OUT &out);
00460
00461
00462 template<typename MatrixType> size_t rank(const MatrixType &m,typename MatrixType::value_type eps=1e-7);
00463
00464
00465 template <class MATRIXIN,class MATRIXOUT> inline void invMatrix( const MATRIXIN &M, MATRIXOUT &out_inv );
00466 template <class MATRIXIN,class MATRIXOUT> inline void invMatrix_destroySrc( MATRIXIN &M, MATRIXOUT &out_inv );
00467
00468
00469
00470 template <class MATRIXIN,class MATRIXOUT> inline void invMatrix_special_2x2( const MATRIXIN &M, MATRIXOUT &out_inv );
00471 template <class MATRIXIN,class MATRIXOUT> inline void invMatrix_special_3x3( const MATRIXIN &M, MATRIXOUT &out_inv );
00472
00473
00474
00475 template <class MATRIX> RET_ELEMENT_ASSERT_MRPTCONTAINER(MATRIX)
00476 detMatrix(const MATRIX& M);
00477
00478
00479
00480 template <class MATRIX> inline typename MATRIX::value_type detMatrix_special_2x2(const MATRIX &M);
00481 template <class MATRIX> inline typename MATRIX::value_type detMatrix_special_3x3(const MATRIX &M);
00482 template <class MATRIX> typename MATRIX::value_type detMatrix_special_4x4(const MATRIX &M);
00483
00484
00485
00486 template <class MATRIX1,class MATRIX2,class VECTOR1> void
00487 eigenVectorsMatrix(const MATRIX1 &M,MATRIX2 *eVecs,VECTOR1 &eVals );
00488
00489
00490 template <class MATRIX1,class MATRIX2,class VECTOR1> void eigenVectorsMatrix_special_2x2(const MATRIX1 &M,MATRIX2 *eVecs,VECTOR1 &eVals );
00491
00492
00493 template <> inline void eigenVectorsMatrix<CMatrixFixedNumeric<float,2,2>,CMatrixFixedNumeric<float,2,2>,CArrayNumeric<float,2> >(const CMatrixFixedNumeric<float,2,2> &M,CMatrixFixedNumeric<float,2,2> *eVecs,CArrayNumeric<float,2> &eVals ) { eigenVectorsMatrix_special_2x2(M,eVecs,eVals); }
00494 template <> inline void eigenVectorsMatrix<CMatrixFixedNumeric<double,2,2>,CMatrixFixedNumeric<double,2,2>,CArrayNumeric<double,2> >(const CMatrixFixedNumeric<double,2,2> &M,CMatrixFixedNumeric<double,2,2> *eVecs,CArrayNumeric<double,2> &eVals ) { eigenVectorsMatrix_special_2x2(M,eVecs,eVals); }
00495
00496
00497 template <class MAT> void
00498 saveMatrixToTextFile(const MAT &theMatrix, const std::string &file, mrpt::math::TMatrixTextFileFormat fileFormat = MATRIX_FORMAT_ENG, bool appendMRPTHeader = false, const std::string &userHeader = std::string("") );
00499 template <class MATRIX>
00500 std::string matrix_inMatlabFormat(const MATRIX &m,const size_t decimal_digits);
00501
00502 template <typename T,size_t NROWS,size_t NCOLS> void fixedToDynMatrix( const CMatrixFixedNumeric<T,NROWS,NCOLS> &SRC, CMatrixTemplateNumeric<T> &DST);
00503 template <class MAT1,class MAT2> void insertMatrixInto( MAT1 &M,const size_t nRow,const size_t nCol,const MAT2 &in);
00504 template <class MAT1,class MAT2> void insertMatrixTransposeInto( MAT1 &M,const size_t nRow,const size_t nCol,const MAT2 &in);
00505
00506 template <class MATORG, class MATDEST> void extractMatrix(const MATORG &origin,const size_t first_row,const size_t first_col, MATDEST &out_subMatrix);
00507 template <class MAT,class VEC> void extractRowFromMatrix(const MAT &m, size_t nRow, VEC &out, const size_t startingCol);
00508 template <class MAT,class VEC> void extractColFromMatrix(const MAT &m, size_t nCol, VEC &out, const size_t startingRow);
00509 template <class MAT,class VEC> void insertRowToMatrix(MAT &m, size_t nRow, const VEC &in, const size_t startingCol);
00510 template <class MAT,class VEC> void insertColToMatrix(MAT &m, size_t nCol, const VEC &in, const size_t startingRow);
00511
00512 template <class MATRIX> int matrix_pivot(MATRIX &M, const size_t row);
00513
00514 template <typename MAT1,typename MAT2,typename MAT3> inline void leftDivideSquare(const MAT1 &C,const MAT2 &A,MAT3 &RES);
00515 template <typename MAT1,typename MAT2,typename MAT3> inline void rightDivideSquare(const MAT1 &C,const MAT2 &B, MAT3 &RES);
00516 template <typename MAT1,typename MAT2> inline void fastLeftDivideSquare(MAT1 &inout_CB,MAT2 &willBeDestroyed_A);
00517 template <typename MAT1,typename MAT2> inline void fastRightDivideSquare(MAT1 &inout_CA,MAT2 &willBeDestroyed_B);
00518
00519
00520
00521 template <class MATRIX> inline bool isMatrixTypeResizable(const MATRIX&) { return false; }
00522
00523
00524 template <class CONTAINER> void loadFromTextFileAsVector(CONTAINER &M, const std::string &file);
00525 template <class CONTAINER> void saveToTextFileAsVector(const CONTAINER &M, const std::string &file, mrpt::math::TMatrixTextFileFormat fileFormat, bool asColumnVector);
00526
00527
00528 TPoint2D BASE_IMPEXP lightFromPose(const mrpt::poses::CPoint2D &p);
00529 TPoint3D BASE_IMPEXP lightFromPose(const mrpt::poses::CPoint3D &p);
00530 TPose2D BASE_IMPEXP lightFromPose(const mrpt::poses::CPose2D &p);
00531 TPose3D BASE_IMPEXP lightFromPose(const mrpt::poses::CPose3D &p);
00532 TPose3DQuat BASE_IMPEXP lightFromPose(const mrpt::poses::CPose3DQuat &p);
00533
00534 }
00535
00536
00537
00538
00539 template <class CONTAINER> size_t countNonZero(const CONTAINER &a);
00540 template <class CONTAINER> typename CONTAINER::value_type maximum(const CONTAINER &v, size_t *maxIndex = NULL);
00541 template <class CONTAINER> typename CONTAINER::value_type minimum(const CONTAINER &v, size_t *minIndex = NULL);
00542 template <class CONTAINER> void minimum_maximum(const CONTAINER &v,typename CONTAINER::mrpt_autotype::value_type & out_min,typename CONTAINER::mrpt_autotype::value_type& out_max,size_t *minIndex = static_cast<size_t*>(NULL), size_t *maxIndex = static_cast<size_t*>(NULL));
00543 template <class CONTAINER> typename CONTAINER::value_type norm_inf(const CONTAINER &v, size_t *maxIndex = NULL);
00544 template <class CONTAINER> typename CONTAINER::value_type squareNorm(const CONTAINER &v);
00545 template <class CONTAINER> inline typename CONTAINER::value_type norm(const CONTAINER &v);
00546 template <class CONTAINER> inline double mean(const CONTAINER &v);
00547 template <class CONTAINER> inline typename CONTAINER::value_type sum(const CONTAINER &v);
00548 template <class CONTAINER,typename RET> inline RET sumRetType(const CONTAINER &v);
00549 template <class CONTAINER> void adjustRange(CONTAINER &m, const typename CONTAINER::value_type minVal,const typename CONTAINER::value_type maxVal);
00550 template <class CONTAINER1,class CONTAINER2> void cumsum(const CONTAINER1 &in_data, CONTAINER2 &out_cumsum);
00551 template <class CONTAINER1,class CONTAINER2> size_t countCommonElements(const CONTAINER1 &a,const CONTAINER2 &b);
00552 template <class CONTAINER> std::vector<double> histogram(const CONTAINER &v,double limit_min,double limit_max,size_t number_bins,bool do_normalization=false, std::vector<double> *out_bin_centers=NULL);
00553 template<class VECTORLIKE> double stddev(const VECTORLIKE &v, bool unbiased=true);
00554 template<class VECTORLIKE> void meanAndStd(const VECTORLIKE &v,double &out_mean, double &out_std, bool unbiased = true);
00555
00556
00557 template <class CONTAINER> CONTAINER & containerFromPoseOrPoint(CONTAINER &C, const TPoint2D &p);
00558 template <class CONTAINER> CONTAINER & containerFromPoseOrPoint(CONTAINER &C, const TPoint3D &p);
00559 template <class CONTAINER> CONTAINER & containerFromPoseOrPoint(CONTAINER &C, const TPose2D &p);
00560 template <class CONTAINER> CONTAINER & containerFromPoseOrPoint(CONTAINER &C, const TPose3D &p);
00561 template <class CONTAINER> CONTAINER & containerFromPoseOrPoint(CONTAINER &C, const TPose3DQuat &p);
00562
00563 template <class CONTAINER> inline CONTAINER & containerFromPoseOrPoint(CONTAINER &C, const mrpt::poses::CPoint2D &p) { return containerFromPoseOrPoint(C, mrpt::math::detail::lightFromPose(p)); }
00564 template <class CONTAINER> inline CONTAINER & containerFromPoseOrPoint(CONTAINER &C, const mrpt::poses::CPoint3D &p) { return containerFromPoseOrPoint(C, mrpt::math::detail::lightFromPose(p)); }
00565 template <class CONTAINER> inline CONTAINER & containerFromPoseOrPoint(CONTAINER &C, const mrpt::poses::CPose2D &p) { return containerFromPoseOrPoint(C, mrpt::math::detail::lightFromPose(p)); }
00566 template <class CONTAINER> inline CONTAINER & containerFromPoseOrPoint(CONTAINER &C, const mrpt::poses::CPose3D &p) { return containerFromPoseOrPoint(C, mrpt::math::detail::lightFromPose(p)); }
00567 template <class CONTAINER> inline CONTAINER & containerFromPoseOrPoint(CONTAINER &C, const mrpt::poses::CPose3DQuat &p) { return containerFromPoseOrPoint(C, mrpt::math::detail::lightFromPose(p)); }
00568
00569
00570
00571 namespace detail {
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581 template<typename T> class VicinityTraits;
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593 template<typename MatrixType,typename T,typename ReturnType,size_t D> struct getVicinity;
00594
00595 }
00596
00597
00598
00599 template <class T> T wrapTo2Pi(T a);
00600
00601
00602 }
00603 }
00604
00605 #endif