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
00139
00140
00141
00142
00143 inline CMatrixTemplateNumeric(TConstructorFlags_Matrices constructor_flag) : CMatrixTemplate<T>( 0,0 )
00144 { }
00145
00146
00147 CMatrixTemplateNumeric(size_t row, size_t col);
00148
00149
00150 CMatrixTemplateNumeric(const CMatrixTemplate<T> & m, const size_t cropRowCount, const size_t cropColCount) : CMatrixTemplate<T>(m,cropRowCount,cropColCount)
00151 { }
00152
00153
00154 template <size_t NROWS,size_t NCOLS>
00155 explicit CMatrixTemplateNumeric(const CMatrixFixedNumeric<T,NROWS,NCOLS> &M ) {
00156 this->assignMatrix(M);
00157 }
00158
00159
00160 template <size_t NROWS,size_t NCOLS>
00161 CMatrixTemplateNumeric& operator =(const CMatrixFixedNumeric<T,NROWS,NCOLS> &M ) {
00162 return this->assignMatrix(M);
00163 }
00164
00165
00166
00167 template <class R>
00168 CMatrixTemplateNumeric<T>& operator = (const CMatrixTemplate<R>& m)
00169 {
00170 CMatrixTemplate<T>::realloc( m.getRowCount(), m.getColCount() );
00171
00172 for (size_t i=0; i < CMatrixTemplate<T>::getRowCount(); i++)
00173 for (size_t j=0; j < CMatrixTemplate<T>::getColCount(); j++)
00174 CMatrixTemplate<T>::m_Val[i][j] = static_cast<T>(m.get_unsafe(i,j));
00175 return *this;
00176 }
00177
00178
00179 template <class R>
00180 inline CMatrixTemplateNumeric(const CMatrixTemplate<R>& m) {
00181 *this = m;
00182 }
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192 template <typename V, size_t N>
00193 CMatrixTemplateNumeric(size_t row, size_t col, V (&theArray)[N] ) : CMatrixTemplate<T>( row, col, theArray )
00194 { }
00195
00196
00197
00198 template <typename V>
00199 CMatrixTemplateNumeric(size_t row, size_t col, const V &theVector ) : CMatrixTemplate<T>( row, col, theVector )
00200 { }
00201
00202
00203
00204 virtual ~CMatrixTemplateNumeric()
00205 { }
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218 template <typename V, size_t N>
00219 CMatrixTemplateNumeric& operator = (V (&theArray)[N] )
00220 {
00221 CMatrixTemplate<T>::operator = (theArray);
00222 return *this;
00223 }
00224
00225
00226
00227
00228 void setSize(size_t row, size_t col);
00229
00230
00231
00232
00233 void resize(size_t row, size_t col);
00234
00235
00236 inline void resize(const CMatrixTemplateSize &siz) {
00237 setSize(siz[0],siz[1]);
00238 }
00239
00240
00241
00242 void laplacian( CMatrixTemplateNumeric<T> &ret ) const;
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253 void svd(CMatrixTemplateNumeric<T> &U, std::vector<T> &W,CMatrixTemplateNumeric<T> &V) const;
00254
00255
00256
00257
00258
00259
00260 CMatrixTemplateNumeric<T> largestEigenvector(
00261 T resolution = 0.01f,
00262 size_t maxIterations = 6,
00263 int *out_Iterations = NULL,
00264 float *out_estimatedResolution = NULL ) const;
00265
00266
00267
00268 CMatrixTemplateNumeric<T>& operator ^= (const unsigned int& pow);
00269
00270
00271
00272 void scalarPow(T s);
00273
00274
00275
00276 void zeros(const size_t row, const size_t col);
00277
00278
00279
00280 void zeros();
00281
00282
00283
00284 void ones(const size_t row, const size_t col);
00285
00286
00287
00288 void ones();
00289
00290
00291
00292 void unit (const size_t row);
00293
00294
00295 inline void eye(const size_t size) { this->unit(size); }
00296
00297
00298
00299 void unit();
00300
00301
00302 inline void eye() { this->unit(); }
00303
00304
00305
00306 CMatrixTemplateNumeric<T> solve (const CMatrixTemplateNumeric<T>& v) const;
00307
00308
00309
00310 CMatrixTemplateNumeric<T> adj() const;
00311
00312
00313
00314
00315
00316
00317
00318 T cofact (size_t row, size_t col) const;
00319
00320
00321
00322 T cond();
00323
00324
00325
00326 bool isDiagonal() const;
00327
00328
00329
00330 bool isScalar() const;
00331
00332
00333
00334 bool isUnit() const;
00335
00336
00337
00338 bool isNull() const;
00339
00340
00341
00342 bool isSymmetric() const;
00343
00344
00345
00346 bool isSkewSymmetric() const;
00347
00348
00349
00350 bool isUpperTriangular() const;
00351
00352
00353
00354 bool isLowerTriangular() const;
00355
00356
00357
00358
00359 void matrix_floor();
00360
00361
00362
00363
00364 void matrix_floor(CMatrixTemplateNumeric<T> &out);
00365
00366
00367
00368
00369 void matrix_ceil();
00370
00371
00372
00373
00374 void find_index_max_value(size_t &umax, size_t &vmax, T &max_val) const;
00375
00376
00377
00378 T maximumDiagonal() const;
00379
00380
00381
00382
00383 void find_index_min_value(size_t &umin, size_t &vmin, T &min_val) const;
00384
00385
00386
00387
00388 void force_symmetry();
00389
00390
00391
00392
00393 void mean( std::vector<T> &outMeanVector ) const;
00394
00395
00396
00397
00398 void meanAndStd(
00399 std::vector<T> &outMeanVector,
00400 std::vector<T> &outStdVector ) const;
00401
00402
00403
00404
00405 void meanAndStdAll(
00406 T &outMean,
00407 T &outStd ) const;
00408
00409 void asCol(CMatrixTemplateNumeric<T> &aux) const;
00410
00411 void asRow(CMatrixTemplateNumeric<T> &aux) const;
00412
00413
00414
00415
00416
00417
00418
00419
00420 void findElementsPassingMahalanobisThreshold(
00421 double stdTimes,
00422 std::vector<size_t> &rowIndexes,
00423 std::vector<size_t> &colIndexes,
00424 bool below = false ) const;
00425
00426
00427
00428
00429
00430 T sum(
00431 size_t firstRow = 0,
00432 size_t firstCol = 0,
00433 size_t lastRow = std::numeric_limits<size_t>::max(),
00434 size_t lastCol = std::numeric_limits<size_t>::max() ) const;
00435
00436
00437
00438
00439 void multiplyByMatrixAndByTransposeNonSymmetric(
00440 const CMatrixTemplateNumeric<T> &C,
00441 CMatrixTemplateNumeric<T> &R,
00442 bool accumOnOutput = false,
00443 bool substractInsteadOfSum = false
00444 ) const;
00445
00446 };
00447
00448
00449
00450
00451
00452
00453 typedef CMatrixTemplateNumeric<float> CMatrixFloat;
00454
00455
00456
00457
00458
00459 typedef CMatrixTemplateNumeric<double> CMatrixDouble;
00460
00461
00462
00463
00464 typedef CMatrixTemplateNumeric<unsigned int> CMatrixUInt;
00465
00466
00467
00468
00469 typedef CMatrixTemplate<bool> CMatrixBool;
00470
00471 #ifdef HAVE_LONG_DOUBLE
00472
00473
00474
00475 typedef CMatrixTemplateNumeric<long double> CMatrixLongDouble;
00476 #else
00477
00478
00479
00480 typedef CMatrixTemplateNumeric<double> CMatrixLongDouble;
00481 #endif
00482
00483 namespace detail
00484 {
00485
00486 template <> inline bool isMatrixTypeResizable<CMatrixFloat>(const CMatrixFloat&) { return true; }
00487 template <> inline bool isMatrixTypeResizable<CMatrixDouble>(const CMatrixDouble&) { return true; }
00488 template <> inline bool isMatrixTypeResizable<CMatrixLongDouble>(const CMatrixLongDouble&) { return true; }
00489 }
00490
00491 namespace detail {
00492
00493
00494
00495 template<typename T> class VicinityTraits<CMatrixTemplateNumeric<T> > {
00496 public:
00497 inline static void initialize(CMatrixTemplateNumeric<T> &mat,size_t N) {
00498 mat.setSize(N,N);
00499 mat.fill(0);
00500 }
00501 inline static void insertInContainer(CMatrixTemplateNumeric<T> &mat,size_t r,size_t c,const T &t) {
00502 mat.get_unsafe(r,c)=t;
00503 }
00504 };
00505 }
00506
00507 }
00508
00509
00510 namespace utils
00511 {
00512
00513 template<typename T> struct TTypeName <mrpt::math::CMatrixTemplateNumeric<T> > {
00514 static std::string get() { return std::string("CMatrixTemplateNumeric<")+ std::string( TTypeName<T>::get() )+std::string(">"); }
00515 };
00516 }
00517
00518 }
00519
00520 #endif