ViSP  2.10.0
vpMatrix.h
1 /****************************************************************************
2  *
3  * $Id: vpMatrix.h 5238 2015-01-30 13:52:25Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 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/vpException.h>
49 #include <visp/vpTime.h>
50 
51 #ifdef VISP_HAVE_GSL
52 # include <gsl/gsl_math.h>
53 # include <gsl/gsl_eigen.h>
54 #endif
55 
56 #include <iostream>
57 #include <math.h>
58 
59 class vpRowVector;
60 class vpColVector;
62 
63 
64 class vpColVector;
66 class vpRowVector;
67 class vpHomography;
68 
69 
70 
98 class VISP_EXPORT vpMatrix
99 {
100  public:
105  typedef enum {
106  LU_DECOMPOSITION
107  } vpDetMethod;
108 
109 
110 protected:
112  unsigned int rowNum;
114  unsigned int colNum;
115 
116  public:
118  double *data;
119  protected:
121  double **rowPtrs;
122 
124  unsigned int dsize;
126  unsigned int trsize;
127 
128  public:
130  vpMatrix() ;
132  vpMatrix(unsigned int r, unsigned int c) ;
133  vpMatrix(unsigned int r, unsigned int c, double val);
135  vpMatrix(const vpMatrix &m, unsigned int r, unsigned int c,
136  unsigned int nrows, unsigned int ncols) ;
137 
138  vpMatrix(const vpHomography& H);
139 
141  virtual ~vpMatrix();
142 
144  void init() ;
145 
147  void kill() ;
148 
149  // Initialize an identity matrix n-by-n
150  void eye(unsigned int n) ;
151  // Initialize an identity matrix m-by-n
152  void eye(unsigned int m, unsigned int n) ;
153  void setIdentity(const double & val=1.0) ;
154 
155  //---------------------------------
156  // Set/get Matrix size
157  //---------------------------------
160  inline unsigned int getRows() const { return rowNum ;}
163  inline unsigned int getCols() const { return colNum; }
165  inline unsigned int size() const { return colNum*rowNum; }
166 
167  // Set the size of the matrix A, initialization with a zero matrix
168  void resize(const unsigned int nrows, const unsigned int ncols,
169  const bool nullify = true);
170 
171  double getMinValue() const;
172 
173  double getMaxValue() const;
175 
176  //---------------------------------
177  // Printing
178  //---------------------------------
179 
180  friend VISP_EXPORT std::ostream &operator << (std::ostream &s,const vpMatrix &m);
183 
184  int print(std::ostream& s, unsigned int length, char const* intro=0);
185  std::ostream & matlabPrint(std::ostream & os) const;
186  std::ostream & maplePrint(std::ostream & os) const;
187  std::ostream & csvPrint(std::ostream & os) const;
188  std::ostream & cppPrint(std::ostream & os, const char * matrixName = NULL, bool octet = false) const;
189 
190  void printSize() { std::cout << getRows() <<" x " << getCols() <<" " ; }
192 
193 
194  //---------------------------------
195  // Copy / assignment
196  //---------------------------------
199  vpMatrix (const vpMatrix& m);
201 
202  // Assignment from an array
203  vpMatrix &operator<<(double*);
204 
206  vpMatrix &operator=(const vpMatrix &B);
208  vpMatrix &operator=(const vpHomography &H);
210  vpMatrix &operator=(const double x);
211  void diag(const vpColVector &A);
213 
214  //---------------------------------
215  // Access/modification operators
216  //---------------------------------
219  inline double *operator[](unsigned int i) { return rowPtrs[i]; }
222  inline double *operator[](unsigned int i) const {return rowPtrs[i];}
224 
225  //---------------------------------
226  // Matrix operations (Static).
227  //---------------------------------
228 
229  static void add2Matrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C);
230  static void add2WeightedMatrices(const vpMatrix &A, const double &wA, const vpMatrix &B,const double &wB, vpMatrix &C);
231  static vpMatrix computeCovarianceMatrix(const vpMatrix &A, const vpColVector &x, const vpColVector &b);
232  static vpMatrix computeCovarianceMatrix(const vpMatrix &A, const vpColVector &x, const vpColVector &b, const vpMatrix &w);
233  static void computeHLM(const vpMatrix &H, const double &alpha, vpMatrix &HLM);
234 
235  // Create a diagonal matrix with the element of a vector DAii = Ai
236  static void createDiagonalMatrix(const vpColVector &A, vpMatrix &DA) ;
237  // Insert matrix A in the current matrix at the given position (r, c).
238  void insert(const vpMatrix&A, const unsigned int r, const unsigned int c);
239  // Insert matrix B in matrix A at the given position (r, c).
240  static vpMatrix insert(const vpMatrix &A,const vpMatrix &B, const unsigned int r, const unsigned int c) ;
241  // Insert matrix B in matrix A (not modified) at the given position (r, c), the result is given in matrix C.
242  static void insert(const vpMatrix &A, const vpMatrix &B, vpMatrix &C, const unsigned int r, const unsigned int c) ;
243 
244  // Juxtapose to matrices C = [ A B ]
245  static vpMatrix juxtaposeMatrices(const vpMatrix &A,const vpMatrix &B) ;
246  // Juxtapose to matrices C = [ A B ]
247  static void juxtaposeMatrices(const vpMatrix &A,const vpMatrix &B, vpMatrix &C) ;
248  // Compute Kronecker product matrix
249  static void kron(const vpMatrix &m1, const vpMatrix &m2 , vpMatrix &out);
250 
251  // Compute Kronecker product matrix
252  static vpMatrix kron(const vpMatrix &m1, const vpMatrix &m2 );
253 
264  static inline bool loadMatrix(std::string filename, vpMatrix &M,
265  const bool binary = false, char *Header = NULL)
266  {
267  return vpMatrix::loadMatrix(filename.c_str(), M, binary, Header);
268  }
269  static bool loadMatrix(const char *filename, vpMatrix &M, const bool binary = false, char *Header = NULL);
270  static void mult2Matrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C);
271  static void multMatrixVector(const vpMatrix &A, const vpColVector &b, vpColVector &c);
272  static void negateMatrix(const vpMatrix &A, vpMatrix &C);
285  static inline bool saveMatrix(std::string filename, const vpMatrix &M,
286  const bool binary = false,
287  const char *Header = "")
288  {
289  return vpMatrix::saveMatrix(filename.c_str(), M, binary, Header);
290  }
291  static bool saveMatrix(const char *filename, const vpMatrix &M, const bool binary = false, const char *Header = "");
292 
303  static inline bool saveMatrixYAML(std::string filename, const vpMatrix &M, const char *header = "")
304  {
305  return vpMatrix::saveMatrixYAML(filename.c_str(), M, header);
306  }
307  static bool saveMatrixYAML(const char *filename, const vpMatrix &M, const char *header = "");
317  static inline bool loadMatrixYAML(std::string filename, vpMatrix &M, char *header = NULL)
318  {
319  return vpMatrix::loadMatrixYAML(filename.c_str(), M, header);
320  }
321  static bool loadMatrixYAML(const char *filename, vpMatrix &M, char *header = NULL);
322 
323 
324  // Stack the matrix A below the current one, copy if not initialized this = [ this A ]^T
325  void stackMatrices(const vpMatrix &A);
327  static vpMatrix stackMatrices(const vpMatrix &A,const vpMatrix &B) ;
329  static void stackMatrices(const vpMatrix &A,const vpMatrix &B, vpMatrix &C) ;
330  static void sub2Matrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C);
331 
332 
333  //---------------------------------
334  // Matrix operations.
335  //---------------------------------
338  // operation A = A + B
339  vpMatrix &operator+=(const vpMatrix &B);
340  // operation A = A - B
341  vpMatrix &operator-=(const vpMatrix &B);
342 
343  vpMatrix operator*(const vpMatrix &B) const;
344  vpMatrix operator*(const vpHomography &H) const;
345  vpMatrix operator+(const vpMatrix &B) const;
346  vpMatrix operator-(const vpMatrix &B) const;
347  vpMatrix operator-() const;
348 
349  //---------------------------------
350  // Matrix/vector operations.
351  //---------------------------------
352 
353  vpColVector operator*(const vpColVector &b) const;
354  // operation c = A * b (A is unchanged, c and b are translation vectors)
355  vpTranslationVector operator*(const vpTranslationVector &b) const;
356  //---------------------------------
357  // Matrix/real operations.
358  //---------------------------------
359 
361  vpMatrix &operator+=(const double x);
363  vpMatrix &operator-=(const double x);
365  vpMatrix &operator*=(const double x);
367  vpMatrix &operator/=(double x);
368 
369  // Cij = Aij * x (A is unchanged)
370  vpMatrix operator*(const double x) const;
371  // Cij = Aij / x (A is unchanged)
372  vpMatrix operator/(const double x) const;
373 
379  double sum() const;
385  double sumSquare() const;
386 
387  // return the determinant of the matrix.
388  double det(vpDetMethod method = LU_DECOMPOSITION) const;
389 
390  //Compute the exponential matrix of a square matrix
391  vpMatrix expm();
392 
393  //-------------------------------------------------
394  // Columns, Rows extraction, SubMatrix
395  //-------------------------------------------------
398  vpRowVector getRow(const unsigned int i) const;
399  vpRowVector getRow(const unsigned int i, const unsigned int j_begin, const unsigned int size) const;
400  vpColVector getCol(const unsigned int j) const;
401  vpColVector getCol(const unsigned int j, const unsigned int i_begin, const unsigned int size) const;
402  void init(const vpMatrix &M, unsigned int r, unsigned int c, unsigned int nrows, unsigned int ncols);
404 
405  //-------------------------------------------------
406  // transpose
407  //-------------------------------------------------
410  // Compute the transpose C = A^T
411  vpMatrix t() const;
412 
413  // Compute the transpose C = A^T
414  vpMatrix transpose()const;
415  void transpose(vpMatrix & C )const;
416 
417  vpMatrix AAt() const;
418  void AAt(vpMatrix &B) const;
419 
420  vpMatrix AtA() const;
421  void AtA(vpMatrix &B) const;
423 
424 
425  //-------------------------------------------------
426  // Kronecker product
427  //-------------------------------------------------
430 
431  // Stacks columns of a matrix in a vector
432  void stackColumns(vpColVector &out );
433 
434  // Stacks columns of a matrix in a vector
435  vpColVector stackColumns();
436 
437  // Stacks columns of a matrix in a vector
438  void stackRows(vpRowVector &out );
439 
440  // Stacks columns of a matrix in a vector
441  vpRowVector stackRows();
442 
443  // Compute Kronecker product matrix
444  void kron(const vpMatrix &m1 , vpMatrix &out);
445 
446  // Compute Kronecker product matrix
447  vpMatrix kron(const vpMatrix &m1);
449 
450  //-------------------------------------------------
451  // Matrix inversion
452  //-------------------------------------------------
455 #ifndef DOXYGEN_SHOULD_SKIP_THIS
456  void LUDcmp(unsigned int* perm, int& d);
459  void LUBksb(unsigned int* perm, vpColVector& b);
460 
461 #endif // doxygen should skip this
462  // inverse matrix A using the LU decomposition
463  vpMatrix inverseByLU() const;
464 #if defined(VISP_HAVE_LAPACK)
465  // inverse matrix A using the Cholesky decomposition (only for real symmetric matrices)
466  vpMatrix inverseByCholesky() const;
467  //lapack implementation of inverse by Cholesky
468  vpMatrix inverseByCholeskyLapack() const;
469  // inverse matrix A using the QR decomposition
470  vpMatrix inverseByQR() const;
471  //lapack implementation of inverse by QR
472  vpMatrix inverseByQRLapack() const;
473 #endif
474  vpMatrix pseudoInverse(double svThreshold=1e-6) const;
478  unsigned int pseudoInverse(vpMatrix &Ap, double svThreshold=1e-6) const;
481  unsigned int pseudoInverse(vpMatrix &Ap, vpColVector &sv, double svThreshold=1e-6) const ;
484  unsigned int pseudoInverse(vpMatrix &Ap,
485  vpColVector &sv, double svThreshold,
486  vpMatrix &ImA,
487  vpMatrix &ImAt) const ;
490  unsigned int pseudoInverse(vpMatrix &Ap,
491  vpColVector &sv, double svThreshold,
492  vpMatrix &ImA,
493  vpMatrix &ImAt,
494  vpMatrix &kerA) const ;
496 
497  //-------------------------------------------------
498  // SVD decomposition
499  //-------------------------------------------------
500 
501 #ifndef DOXYGEN_SHOULD_SKIP_THIS
502  void svdFlake(vpColVector& w, vpMatrix& v);
503  void svdNr(vpColVector& w, vpMatrix& v);
504 #ifdef VISP_HAVE_GSL
505  void svdGsl(vpColVector& w, vpMatrix& v);
506 #endif
507 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101) // Require opencv >= 2.1.1
508  void svdOpenCV(vpColVector& w, vpMatrix& v);
509 #endif
510 #ifdef VISP_HAVE_LAPACK
511  void svdLapack(vpColVector& W, vpMatrix& V);
512 #endif
513  void SVBksb(const vpColVector& w,
515  const vpMatrix& v,
516  const vpColVector& b, vpColVector& x);
517 #endif
518 
521  // singular value decomposition SVD
522 
523  void svd(vpColVector& w, vpMatrix& v);
524 
525  // solve Ax=B using the SVD decomposition (usage A = solveBySVD(B,x) )
526  void solveBySVD(const vpColVector &B, vpColVector &x) const ;
527  // solve Ax=B using the SVD decomposition (usage x=A.solveBySVD(B))
528  vpColVector solveBySVD(const vpColVector &B) const ;
529 
530  unsigned int kernel(vpMatrix &KerA, double svThreshold=1e-6);
531  double cond();
533 
534  //-------------------------------------------------
535  // Eigen values and vectors
536  //-------------------------------------------------
537 
541  // compute the eigen values using the Gnu Scientific library
542  vpColVector eigenValues();
543  void eigenValues(vpColVector &evalue, vpMatrix &evector);
545 
546  // -------------------------
547  // Norms
548  // -------------------------
551  // Euclidean norm ||x||=sqrt(sum(x_i^2))
552  double euclideanNorm () const;
553  // Infinity norm ||x||=max(sum(fabs(x_i)))
554  double infinityNorm () const;
556 
557 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
558 
561  vp_deprecated vpRowVector row(const unsigned int i);
562  vp_deprecated vpColVector column(const unsigned int j);
563 #endif
564 
565  private:
566  double detByLU() const;
567 
568 };
569 
570 
572 
573 
575 
576 
578 VISP_EXPORT vpMatrix operator*(const double &x, const vpMatrix &A) ;
579 
581 VISP_EXPORT vpColVector operator*(const double &x, const vpColVector &A) ;
582 
583 
584 
585 #endif
586 
587 
588 /*
589  * Local variables:
590  * c-basic-offset: 2
591  * End:
592  */
unsigned int size() const
Return the number of elements of the matrix.
Definition: vpMatrix.h:165
Definition of the vpMatrix class.
Definition: vpMatrix.h:98
static bool loadMatrix(std::string filename, vpMatrix &M, const bool binary=false, char *Header=NULL)
Definition: vpMatrix.h:264
static bool saveMatrixYAML(std::string filename, const vpMatrix &M, const char *header="")
Definition: vpMatrix.h:303
Definition of the row vector class.
Definition: vpRowVector.h:73
vpColVector operator*(const double &x, const vpColVector &B)
multiplication by a scalar Ci = x*Bi
double * data
address of the first element of the data array
Definition: vpMatrix.h:118
unsigned int trsize
Total row space.
Definition: vpMatrix.h:126
This class aims to compute the homography wrt.two images.
Definition: vpHomography.h:178
double ** rowPtrs
address of the first element of each rows
Definition: vpMatrix.h:121
static bool loadMatrixYAML(std::string filename, vpMatrix &M, char *header=NULL)
Definition: vpMatrix.h:317
void printSize()
Definition: vpMatrix.h:190
double * operator[](unsigned int i) const
read elements Aij (usage : x = A[i][j] )
Definition: vpMatrix.h:222
unsigned int rowNum
number of rows
Definition: vpMatrix.h:112
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:163
unsigned int dsize
Current size (rowNum * colNum)
Definition: vpMatrix.h:124
unsigned int colNum
number of columns
Definition: vpMatrix.h:114
Class that consider the case of a translation vector.
static bool saveMatrix(std::string filename, const vpMatrix &M, const bool binary=false, const char *Header="")
Definition: vpMatrix.h:285