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 CMatrixTemplateNumeric_H
00029 #define CMatrixTemplateNumeric_H
00030
00031 #include <mrpt/math/CMatrixTemplate.h>
00032 #include <mrpt/math/matrix_iterators.h>
00033 #include <mrpt/math/matrices_metaprogramming.h>
00034 #include <mrpt/math/CMatrixViews.h>
00035 #include <mrpt/utils/CSerializable.h>
00036
00037 #include <mrpt/system/os.h>
00038 #include <cmath>
00039 #include <limits>
00040
00041 namespace mrpt
00042 {
00043 namespace poses
00044 {
00045 class CPose2D;
00046 class CPose3D;
00047 class CPoint2D;
00048 class CPoint3D;
00049 }
00050 namespace math
00051 {
00052 using namespace mrpt::system;
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110 template <class T>
00111 class BASE_IMPEXP CMatrixTemplateNumeric : public CMatrixTemplate<T>
00112 {
00113 public:
00114 typedef CMatrixTemplate<T> BASE;
00115 typedef CMatrixTemplateNumeric<T> mrpt_autotype;
00116 DECLARE_MRPT_CONTAINER_TYPES
00117 DECLARE_MRPT_CONTAINER_IS_MATRIX
00118 DECLARE_MRPT_MATRIX_ITERATORS
00119 DECLARE_COMMON_MATRIX_MEMBERS(T)
00120
00121
00122 inline CMatrixTemplateNumeric( const TPose2D &p) : CMatrixTemplate<T>( 3,1 ) { mrpt::math::containerFromPoseOrPoint(*this,p); }
00123 inline CMatrixTemplateNumeric( const TPose3D &p) : CMatrixTemplate<T>( 6,1 ) { mrpt::math::containerFromPoseOrPoint(*this,p); }
00124 inline CMatrixTemplateNumeric( const TPose3DQuat &p) : CMatrixTemplate<T>( 7,1 ) { mrpt::math::containerFromPoseOrPoint(*this,p); }
00125 inline CMatrixTemplateNumeric( const TPoint2D &p) : CMatrixTemplate<T>( 2,1 ) { mrpt::math::containerFromPoseOrPoint(*this,p); }
00126 inline CMatrixTemplateNumeric( const TPoint3D &p) : CMatrixTemplate<T>( 3,1 ) { mrpt::math::containerFromPoseOrPoint(*this,p); }
00127
00128 inline CMatrixTemplateNumeric( const mrpt::poses::CPose2D &p) : CMatrixTemplate<T>( 3,1 ) { mrpt::math::containerFromPoseOrPoint(*this,p); }
00129 inline CMatrixTemplateNumeric( const mrpt::poses::CPose3D &p) : CMatrixTemplate<T>( 6,1 ) { mrpt::math::containerFromPoseOrPoint(*this,p); }
00130 inline CMatrixTemplateNumeric( const mrpt::poses::CPose3DQuat &p) : CMatrixTemplate<T>( 7,1 ) { mrpt::math::containerFromPoseOrPoint(*this,p); }
00131 inline CMatrixTemplateNumeric( const mrpt::poses::CPoint2D &p) : CMatrixTemplate<T>( 2,1 ) { mrpt::math::containerFromPoseOrPoint(*this,p); }
00132 inline CMatrixTemplateNumeric( const mrpt::poses::CPoint3D &p) : CMatrixTemplate<T>( 3,1 ) { mrpt::math::containerFromPoseOrPoint(*this,p); }
00133
00134
00135 CMatrixTemplateNumeric();
00136
00137
00138 inline CMatrixTemplateNumeric(bool,bool,bool) : CMatrixTemplate<T>( 0,0 )
00139 {
00140 }
00141
00142
00143 CMatrixTemplateNumeric(size_t row, size_t col);
00144
00145
00146 CMatrixTemplateNumeric(const CMatrixTemplate<T> & m, const size_t cropRowCount, const size_t cropColCount) : CMatrixTemplate<T>(m,cropRowCount,cropColCount)
00147 { }
00148
00149
00150 template <size_t NROWS,size_t NCOLS>
00151 explicit CMatrixTemplateNumeric(const CMatrixFixedNumeric<T,NROWS,NCOLS> &M ) {
00152 this->assignMatrix(M);
00153 }
00154
00155
00156 template <size_t NROWS,size_t NCOLS>
00157 CMatrixTemplateNumeric& operator =(const CMatrixFixedNumeric<T,NROWS,NCOLS> &M ) {
00158 return this->assignMatrix(M);
00159 }
00160
00161
00162
00163 template <class R>
00164 CMatrixTemplateNumeric<T>& operator = (const CMatrixTemplate<R>& m)
00165 {
00166 CMatrixTemplate<T>::realloc( m.getRowCount(), m.getColCount() );
00167
00168 for (size_t i=0; i < CMatrixTemplate<T>::getRowCount(); i++)
00169 for (size_t j=0; j < CMatrixTemplate<T>::getColCount(); j++)
00170 CMatrixTemplate<T>::m_Val[i][j] = static_cast<T>(m.get_unsafe(i,j));
00171 return *this;
00172 }
00173
00174
00175 template <class R>
00176 inline CMatrixTemplateNumeric(const CMatrixTemplate<R>& m) {
00177 *this = m;
00178 }
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188 template <typename V, size_t N>
00189 CMatrixTemplateNumeric(size_t row, size_t col, V (&theArray)[N] ) : CMatrixTemplate<T>( row, col, theArray )
00190 { }
00191
00192
00193
00194 template <typename V>
00195 CMatrixTemplateNumeric(size_t row, size_t col, const V &theVector ) : CMatrixTemplate<T>( row, col, theVector )
00196 { }
00197
00198
00199
00200 virtual ~CMatrixTemplateNumeric()
00201 { }
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214 template <typename V, size_t N>
00215 CMatrixTemplateNumeric& operator = (V (&theArray)[N] )
00216 {
00217 CMatrixTemplate<T>::operator = (theArray);
00218 return *this;
00219 }
00220
00221
00222
00223
00224 void setSize(size_t row, size_t col);
00225
00226
00227
00228
00229 void resize(size_t row, size_t col);
00230
00231
00232 inline void resize(const CMatrixTemplateSize &siz) {
00233 setSize(siz[0],siz[1]);
00234 }
00235
00236
00237
00238 void laplacian( CMatrixTemplateNumeric<T> &ret ) const;
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249 void svd(CMatrixTemplateNumeric<T> &U, std::vector<T> &W,CMatrixTemplateNumeric<T> &V) const;
00250
00251
00252
00253
00254
00255
00256 CMatrixTemplateNumeric<T> largestEigenvector(
00257 T resolution = 0.01f,
00258 size_t maxIterations = 6,
00259 int *out_Iterations = NULL,
00260 float *out_estimatedResolution = NULL ) const;
00261
00262
00263
00264 CMatrixTemplateNumeric<T>& operator ^= (const unsigned int& pow);
00265
00266
00267
00268 void scalarPow(T s);
00269
00270
00271
00272 void zeros(const size_t row, const size_t col);
00273
00274
00275
00276 void zeros();
00277
00278
00279
00280 void ones(const size_t row, const size_t col);
00281
00282
00283
00284 void ones();
00285
00286
00287
00288 void unit (const size_t row);
00289
00290
00291 inline void eye(const size_t size) { this->unit(size); }
00292
00293
00294
00295 void unit();
00296
00297
00298 inline void eye() { this->unit(); }
00299
00300
00301
00302 CMatrixTemplateNumeric<T> solve (const CMatrixTemplateNumeric<T>& v) const;
00303
00304
00305
00306 CMatrixTemplateNumeric<T> adj() const;
00307
00308
00309
00310
00311
00312
00313
00314 T cofact (size_t row, size_t col) const;
00315
00316
00317
00318 T cond();
00319
00320
00321
00322 bool isDiagonal() const;
00323
00324
00325
00326 bool isScalar() const;
00327
00328
00329
00330 bool isUnit() const;
00331
00332
00333
00334 bool isNull() const;
00335
00336
00337
00338 bool isSymmetric() const;
00339
00340
00341
00342 bool isSkewSymmetric() const;
00343
00344
00345
00346 bool isUpperTriangular() const;
00347
00348
00349
00350 bool isLowerTriangular() const;
00351
00352
00353
00354
00355 void matrix_floor();
00356
00357
00358
00359
00360 void matrix_floor(CMatrixTemplateNumeric<T> &out);
00361
00362
00363
00364
00365 void matrix_ceil();
00366
00367
00368
00369
00370 void find_index_max_value(size_t &umax, size_t &vmax, T &max_val) const;
00371
00372
00373
00374 T maximumDiagonal() const;
00375
00376
00377
00378
00379 void find_index_min_value(size_t &umin, size_t &vmin, T &min_val) const;
00380
00381
00382
00383
00384 void force_symmetry();
00385
00386
00387
00388
00389 void mean( std::vector<T> &outMeanVector ) const;
00390
00391
00392
00393
00394 void meanAndStd(
00395 std::vector<T> &outMeanVector,
00396 std::vector<T> &outStdVector ) const;
00397
00398
00399
00400
00401 void meanAndStdAll(
00402 T &outMean,
00403 T &outStd ) const;
00404
00405 void asCol(CMatrixTemplateNumeric<T> &aux) const;
00406
00407 void asRow(CMatrixTemplateNumeric<T> &aux) const;
00408
00409
00410
00411
00412
00413
00414
00415
00416 void findElementsPassingMahalanobisThreshold(
00417 double stdTimes,
00418 std::vector<size_t> &rowIndexes,
00419 std::vector<size_t> &colIndexes,
00420 bool below = false ) const;
00421
00422
00423
00424
00425
00426 T sum(
00427 size_t firstRow = 0,
00428 size_t firstCol = 0,
00429 size_t lastRow = std::numeric_limits<size_t>::max(),
00430 size_t lastCol = std::numeric_limits<size_t>::max() ) const;
00431
00432
00433
00434
00435 void multiplyByMatrixAndByTransposeNonSymmetric(
00436 const CMatrixTemplateNumeric<T> &C,
00437 CMatrixTemplateNumeric<T> &R,
00438 bool accumOnOutput = false,
00439 bool substractInsteadOfSum = false
00440 ) const;
00441
00442 };
00443
00444
00445
00446
00447
00448
00449 typedef CMatrixTemplateNumeric<float> CMatrixFloat;
00450
00451
00452
00453
00454
00455 typedef CMatrixTemplateNumeric<double> CMatrixDouble;
00456
00457
00458
00459
00460 typedef CMatrixTemplateNumeric<unsigned int> CMatrixUInt;
00461
00462
00463
00464
00465 typedef CMatrixTemplate<bool> CMatrixBool;
00466
00467 #ifdef HAVE_LONG_DOUBLE
00468
00469
00470
00471 typedef CMatrixTemplateNumeric<long double> CMatrixLongDouble;
00472 #else
00473
00474
00475
00476 typedef CMatrixTemplateNumeric<double> CMatrixLongDouble;
00477 #endif
00478
00479 namespace detail
00480 {
00481
00482 template <> inline bool isMatrixTypeResizable<CMatrixFloat>(const CMatrixFloat&) { return true; }
00483 template <> inline bool isMatrixTypeResizable<CMatrixDouble>(const CMatrixDouble&) { return true; }
00484 template <> inline bool isMatrixTypeResizable<CMatrixLongDouble>(const CMatrixLongDouble&) { return true; }
00485 }
00486
00487 namespace detail {
00488
00489
00490
00491 template<typename T> class VicinityTraits<CMatrixTemplateNumeric<T> > {
00492 public:
00493 inline static void initialize(CMatrixTemplateNumeric<T> &mat,size_t N) {
00494 mat.setSize(N,N);
00495 mat.fill(0);
00496 }
00497 inline static void insertInContainer(CMatrixTemplateNumeric<T> &mat,size_t r,size_t c,const T &t) {
00498 mat.get_unsafe(r,c)=t;
00499 }
00500 };
00501 }
00502
00503 }
00504
00505
00506 namespace utils
00507 {
00508
00509 template<typename T> struct TTypeName <mrpt::math::CMatrixTemplateNumeric<T> > {
00510 static std::string get() { return std::string("CMatrixTemplateNumeric<")+ std::string( TTypeName<T>::get() )+std::string(">"); }
00511 };
00512 }
00513
00514 }
00515
00516 #endif