ViSP  2.8.0
vpMatrix.h
1 /****************************************************************************
2  *
3  * $Id: vpMatrix.h 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Matrix manipulation.
36  *
37  * Authors:
38  * Eric Marchand
39  *
40  *****************************************************************************/
41 
42 
43 
44 #ifndef vpMatrix_H
45 #define vpMatrix_H
46 
47 #include <visp/vpConfig.h>
48 #include <visp/vpTime.h>
49 
50 #ifdef VISP_HAVE_GSL
51 # include <gsl/gsl_math.h>
52 # include <gsl/gsl_eigen.h>
53 #endif
54 
55 #include <iostream>
56 #include <math.h>
57 
58 class vpRowVector;
59 class vpColVector;
61 
62 
63 class vpColVector;
65 class vpRowVector;
66 
67 
68 
96 class VISP_EXPORT vpMatrix
97 {
98  public:
103  typedef enum {
104  LU_DECOMPOSITION
105  } vpDetMethod;
106 
107 
108 protected:
110  unsigned int rowNum;
112  unsigned int colNum;
113 
114  public:
116  double *data;
117  protected:
119  double **rowPtrs;
120 
122  unsigned int dsize;
124  unsigned int trsize;
125 
126  public:
128  vpMatrix() ;
130  vpMatrix(unsigned int r, unsigned int c) ;
131 
133  vpMatrix(const vpMatrix &m, unsigned int r, unsigned int c,
134  unsigned int nrows, unsigned int ncols) ;
135 
137  virtual ~vpMatrix();
138 
140  void init() ;
141 
143  void kill() ;
144 
145  // Initialize an identity matrix n-by-n
146  void eye(unsigned int n) ;
147  // Initialize an identity matrix m-by-n
148  void eye(unsigned int m, unsigned int n) ;
149  void setIdentity(const double & val=1.0) ;
150 
151  //---------------------------------
152  // Set/get Matrix size
153  //---------------------------------
156  inline unsigned int getRows() const { return rowNum ;}
159  inline unsigned int getCols() const { return colNum; }
160 
161  // Set the size of the matrix A, initialization with a zero matrix
162  void resize(const unsigned int nrows, const unsigned int ncols,
163  const bool nullify = true);
164 
165  double getMinValue() const;
166 
167  double getMaxValue() const;
169 
170  //---------------------------------
171  // Printing
172  //---------------------------------
173 
174  friend VISP_EXPORT std::ostream &operator << (std::ostream &s,const vpMatrix &m);
177 
178  int print(std::ostream& s, unsigned int length, char const* intro=0);
179  std::ostream & matlabPrint(std::ostream & os);
180  std::ostream & maplePrint(std::ostream & os);
181  std::ostream & cppPrint(std::ostream & os, const char * matrixName = NULL, bool octet = false);
182 
183  void printSize() { std::cout << getRows() <<" x " << getCols() <<" " ; }
185 
186  static bool saveMatrix(const char *filename, const vpMatrix &M, const bool binary = false, const char *Header = "");
187  static bool loadMatrix(const char *filename, vpMatrix &M, const bool binary = false, char *Header = NULL);
188 
201  static inline bool saveMatrix(std::string filename, const vpMatrix &M,
202  const bool binary = false,
203  const char *Header = "")
204  {
205  return vpMatrix::saveMatrix(filename.c_str(), M, binary, Header);
206  }
207 
218  static inline bool loadMatrix(std::string filename, vpMatrix &M,
219  const bool binary = false, char *Header = NULL)
220  {
221  return vpMatrix::loadMatrix(filename.c_str(), M, binary, Header);
222  }
223 
224  //---------------------------------
225  // Copy / assignment
226  //---------------------------------
229  vpMatrix (const vpMatrix& m);
231 
232  // Assignment from an array
233  vpMatrix &operator<<(double*);
234 
236  vpMatrix &operator=(const vpMatrix &B);
238  vpMatrix &operator=(const double x);
239  void diag(const vpColVector &A);
241 
242  //---------------------------------
243  // Access/modification operators
244  //---------------------------------
247  inline double *operator[](unsigned int n) { return rowPtrs[n]; }
250  inline double *operator[](unsigned int n) const {return rowPtrs[n];}
252 
253  //---------------------------------
254  // Matrix operations (Static).
255  //---------------------------------
256 
257  static void mult2Matrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C);
258  static void add2Matrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C);
259  static void add2WeightedMatrices(const vpMatrix &A, const double &wA, const vpMatrix &B,const double &wB, vpMatrix &C);
260  static void sub2Matrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C);
261  static void negateMatrix(const vpMatrix &A, vpMatrix &C);
262  static void multMatrixVector(const vpMatrix &A, const vpColVector &b, vpColVector &c);
263 
264  static vpMatrix computeCovarianceMatrix(const vpMatrix &A, const vpColVector &x, const vpColVector &b);
265  static vpMatrix computeCovarianceMatrix(const vpMatrix &A, const vpColVector &x, const vpColVector &b, const vpMatrix &w);
266 
267  //---------------------------------
268  // Matrix operations.
269  //---------------------------------
272  // operation A = A + B
273  vpMatrix &operator+=(const vpMatrix &B);
274  // operation A = A - B
275  vpMatrix &operator-=(const vpMatrix &B);
276 
277  vpMatrix operator*(const vpMatrix &B) const;
278  vpMatrix operator+(const vpMatrix &B) const;
279  vpMatrix operator-(const vpMatrix &B) const;
280  vpMatrix operator-() const;
281 
282  //---------------------------------
283  // Matrix/vector operations.
284  //---------------------------------
285 
286  vpColVector operator*(const vpColVector &b) const;
287  // operation c = A * b (A is unchanged, c and b are translation vectors)
288  vpTranslationVector operator*(const vpTranslationVector &b) const;
289  //---------------------------------
290  // Matrix/real operations.
291  //---------------------------------
292 
294  vpMatrix &operator+=(const double x);
296  vpMatrix &operator-=(const double x);
298  vpMatrix &operator*=(const double x);
300  vpMatrix &operator/=(double x);
301 
302  // Cij = Aij * x (A is unchanged)
303  vpMatrix operator*(const double x) const;
304  // Cij = Aij / x (A is unchanged)
305  vpMatrix operator/(const double x) const;
306 
308  double sumSquare() const;
309 
310  // return the determinant of the matrix.
311  double det(vpDetMethod method = LU_DECOMPOSITION) const;
312 
313  //Compute the exponential matrix of a square matrix
314  vpMatrix expm();
315 
316  //-------------------------------------------------
317  // Columns, Rows extraction, SubMatrix
318  //-------------------------------------------------
321  vpRowVector row(const unsigned int i);
324  vpColVector column(const unsigned int j);
326  void init(const vpMatrix &m, unsigned int r, unsigned int c,
327  unsigned int nrows, unsigned int ncols);
329 
330  //-------------------------------------------------
331  // transpose
332  //-------------------------------------------------
335  // Compute the transpose C = A^T
336  vpMatrix t() const;
337 
338  // Compute the transpose C = A^T
339  vpMatrix transpose()const;
340  void transpose(vpMatrix & C )const;
341 
342  vpMatrix AAt() const;
343  void AAt(vpMatrix &B) const;
344 
345  vpMatrix AtA() const;
346  void AtA(vpMatrix &B) const;
348 
349 
350  //-------------------------------------------------
351  // Kronecker product
352  //-------------------------------------------------
355 
356  // Stacks columns of a matrix in a vector
357  void stackColumns(vpColVector &out );
358 
359  // Stacks columns of a matrix in a vector
360  vpColVector stackColumns();
361 
362  // Stacks columns of a matrix in a vector
363  void stackRows(vpRowVector &out );
364 
365  // Stacks columns of a matrix in a vector
366  vpRowVector stackRows();
367 
368  // Compute Kronecker product matrix
369  void kron(const vpMatrix &m1 , vpMatrix &out);
370 
371  // Compute Kronecker product matrix
372  vpMatrix kron(const vpMatrix &m1);
374 
375  // Compute Kronecker product matrix
376  static void kron(const vpMatrix &m1, const vpMatrix &m2 , vpMatrix &out);
377 
378  // Compute Kronecker product matrix
379  static vpMatrix kron(const vpMatrix &m1, const vpMatrix &m2 );
380 
381 
382  //-------------------------------------------------
383  // Matrix inversion
384  //-------------------------------------------------
387 #ifndef DOXYGEN_SHOULD_SKIP_THIS
388  void LUDcmp(unsigned int* perm, int& d);
391  void LUBksb(unsigned int* perm, vpColVector& b);
392 
393 #endif // doxygen should skip this
394  // inverse matrix A using the LU decomposition
395  vpMatrix inverseByLU() const;
396 #if defined(VISP_HAVE_LAPACK)
397  // inverse matrix A using the Cholesky decomposition (only for real symmetric matrices)
398  vpMatrix inverseByCholesky() const;
399  //lapack implementation of inverse by Cholesky
400  vpMatrix inverseByCholeskyLapack() const;
401  // inverse matrix A using the QR decomposition
402  vpMatrix inverseByQR() const;
403  //lapack implementation of inverse by QR
404  vpMatrix inverseByQRLapack() const;
405 #endif
406  vpMatrix pseudoInverse(double svThreshold=1e-6) const;
410  unsigned int pseudoInverse(vpMatrix &Ap, double svThreshold=1e-6) const;
413  unsigned int pseudoInverse(vpMatrix &Ap, vpColVector &sv, double svThreshold=1e-6) const ;
416  unsigned int pseudoInverse(vpMatrix &Ap,
417  vpColVector &sv, double svThreshold,
418  vpMatrix &ImA,
419  vpMatrix &ImAt) const ;
422  unsigned int pseudoInverse(vpMatrix &Ap,
423  vpColVector &sv, double svThreshold,
424  vpMatrix &ImA,
425  vpMatrix &ImAt,
426  vpMatrix &kerA) const ;
428 
429  //-------------------------------------------------
430  // SVD decomposition
431  //-------------------------------------------------
432 
433 #ifndef DOXYGEN_SHOULD_SKIP_THIS
434  void svdFlake(vpColVector& w, vpMatrix& v);
435  void svdNr(vpColVector& w, vpMatrix& v);
436 #ifdef VISP_HAVE_GSL
437  void svdGsl(vpColVector& w, vpMatrix& v);
438 #endif
439 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101) // Require opencv >= 2.1.1
440  void svdOpenCV(vpColVector& w, vpMatrix& v);
441 #endif
442 #ifdef VISP_HAVE_LAPACK
443  void svdLapack(vpColVector& W, vpMatrix& V);
444 #endif
445  void SVBksb(const vpColVector& w,
447  const vpMatrix& v,
448  const vpColVector& b, vpColVector& x);
449 #endif
450 
453  // singular value decomposition SVD
454 
455  void svd(vpColVector& w, vpMatrix& v);
456 
457  // solve Ax=B using the SVD decomposition (usage A = solveBySVD(B,x) )
458  void solveBySVD(const vpColVector &B, vpColVector &x) const ;
459  // solve Ax=B using the SVD decomposition (usage x=A.solveBySVD(B))
460  vpColVector solveBySVD(const vpColVector &B) const ;
461 
462  unsigned int kernel(vpMatrix &KerA, double svThreshold=1e-6);
464 
465  //-------------------------------------------------
466  // Eigen values and vectors
467  //-------------------------------------------------
468 
472  // compute the eigen values using the Gnu Scientific library
473  vpColVector eigenValues();
474  void eigenValues(vpColVector &evalue, vpMatrix &evector);
476 
478  static vpMatrix stackMatrices(const vpMatrix &A,const vpMatrix &B) ;
480  static void stackMatrices(const vpMatrix &A,const vpMatrix &B, vpMatrix &C) ;
481  // Juxtapose to matrices C = [ A B ]
482  static vpMatrix juxtaposeMatrices(const vpMatrix &A,const vpMatrix &B) ;
483  // Juxtapose to matrices C = [ A B ]
484  static void juxtaposeMatrices(const vpMatrix &A,const vpMatrix &B, vpMatrix &C) ;
485 
486  // Create a diagonal matrix with the element of a vector DAii = Ai
487  static void createDiagonalMatrix(const vpColVector &A, vpMatrix &DA) ;
488 
489  // Stack the matrix A below the current one, copy if not initialized this = [ this A ]^T
490  void stackMatrices(const vpMatrix &A);
491 
492  // Insert matrix A in the current matrix at the given position (r, c).
493  void insert(const vpMatrix&A, const unsigned int r, const unsigned int c);
494  // Insert matrix B in matrix A at the given position (r, c).
495  static vpMatrix insert(const vpMatrix &A,const vpMatrix &B, const unsigned int r, const unsigned int c) ;
496  // Insert matrix B in matrix A (not modified) at the given position (r, c), the result is given in matrix C.
497  static void insert(const vpMatrix &A, const vpMatrix &B, vpMatrix &C, const unsigned int r, const unsigned int c) ;
498 
499  // -------------------------
500  // Norms
501  // -------------------------
504  // Euclidean norm ||x||=sqrt(sum(x_i^2))
505  double euclideanNorm () const;
506  // Infinity norm ||x||=max(sum(fabs(x_i)))
507  double infinityNorm () const;
509 
510  private:
511  double detByLU() const;
512 
513 };
514 
515 
517 
518 
520 
521 
523 VISP_EXPORT vpMatrix operator*(const double &x, const vpMatrix &A) ;
524 
526 VISP_EXPORT vpColVector operator*(const double &x, const vpColVector &A) ;
527 
528 
529 
530 #endif
531 
532 
533 /*
534  * Local variables:
535  * c-basic-offset: 2
536  * End:
537  */
Definition of the vpMatrix class.
Definition: vpMatrix.h:96
static bool loadMatrix(std::string filename, vpMatrix &M, const bool binary=false, char *Header=NULL)
Definition: vpMatrix.h:218
Definition of the row vector class.
Definition: vpRowVector.h:73
static bool saveMatrix(const char *filename, const vpMatrix &M, const bool binary=false, const char *Header="")
Definition: vpMatrix.cpp:3440
VISP_EXPORT vpImagePoint operator*(const vpImagePoint &ip1, const double scale)
Definition: vpImagePoint.h:468
double * data
address of the first element of the data array
Definition: vpMatrix.h:116
unsigned int trsize
Total row space.
Definition: vpMatrix.h:124
double ** rowPtrs
address of the first element of each rows
Definition: vpMatrix.h:119
static bool loadMatrix(const char *filename, vpMatrix &M, const bool binary=false, char *Header=NULL)
Definition: vpMatrix.cpp:3513
void printSize()
Definition: vpMatrix.h:183
unsigned int rowNum
number of rows
Definition: vpMatrix.h:110
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
unsigned int getCols() const
Return the number of columns of the matrix.
Definition: vpMatrix.h:159
unsigned int dsize
Current size (rowNum * colNum)
Definition: vpMatrix.h:122
unsigned int colNum
number of columns
Definition: vpMatrix.h:112
Class that consider the case of a translation vector.
double * operator[](unsigned int n) const
read elements Aij (usage : x = A[i][j] )
Definition: vpMatrix.h:250
static bool saveMatrix(std::string filename, const vpMatrix &M, const bool binary=false, const char *Header="")
Definition: vpMatrix.h:201