ViSP  2.9.0
vpMatrix.h
1 /****************************************************************************
2  *
3  * $Id: vpMatrix.h 4574 2014-01-09 08:48:51Z 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 
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; }
164 
165  // Set the size of the matrix A, initialization with a zero matrix
166  void resize(const unsigned int nrows, const unsigned int ncols,
167  const bool nullify = true);
168 
169  double getMinValue() const;
170 
171  double getMaxValue() const;
173 
174  //---------------------------------
175  // Printing
176  //---------------------------------
177 
178  friend VISP_EXPORT std::ostream &operator << (std::ostream &s,const vpMatrix &m);
181 
182  int print(std::ostream& s, unsigned int length, char const* intro=0);
183  std::ostream & matlabPrint(std::ostream & os) const;
184  std::ostream & maplePrint(std::ostream & os) const;
185  std::ostream & csvPrint(std::ostream & os) const;
186  std::ostream & cppPrint(std::ostream & os, const char * matrixName = NULL, bool octet = false) const;
187 
188  void printSize() { std::cout << getRows() <<" x " << getCols() <<" " ; }
190 
191 
192  //---------------------------------
193  // Copy / assignment
194  //---------------------------------
197  vpMatrix (const vpMatrix& m);
199 
200  // Assignment from an array
201  vpMatrix &operator<<(double*);
202 
204  vpMatrix &operator=(const vpMatrix &B);
206  vpMatrix &operator=(const vpHomography &H);
208  vpMatrix &operator=(const double x);
209  void diag(const vpColVector &A);
211 
212  //---------------------------------
213  // Access/modification operators
214  //---------------------------------
217  inline double *operator[](unsigned int i) { return rowPtrs[i]; }
220  inline double *operator[](unsigned int i) const {return rowPtrs[i];}
222 
223  //---------------------------------
224  // Matrix operations (Static).
225  //---------------------------------
226 
227  static void add2Matrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C);
228  static void add2WeightedMatrices(const vpMatrix &A, const double &wA, const vpMatrix &B,const double &wB, vpMatrix &C);
229  static vpMatrix computeCovarianceMatrix(const vpMatrix &A, const vpColVector &x, const vpColVector &b);
230  static vpMatrix computeCovarianceMatrix(const vpMatrix &A, const vpColVector &x, const vpColVector &b, const vpMatrix &w);
231  static void computeHLM(const vpMatrix &H, const double &alpha, vpMatrix &HLM);
232 
233  // Create a diagonal matrix with the element of a vector DAii = Ai
234  static void createDiagonalMatrix(const vpColVector &A, vpMatrix &DA) ;
235  // Insert matrix A in the current matrix at the given position (r, c).
236  void insert(const vpMatrix&A, const unsigned int r, const unsigned int c);
237  // Insert matrix B in matrix A at the given position (r, c).
238  static vpMatrix insert(const vpMatrix &A,const vpMatrix &B, const unsigned int r, const unsigned int c) ;
239  // Insert matrix B in matrix A (not modified) at the given position (r, c), the result is given in matrix C.
240  static void insert(const vpMatrix &A, const vpMatrix &B, vpMatrix &C, const unsigned int r, const unsigned int c) ;
241 
242  // Juxtapose to matrices C = [ A B ]
243  static vpMatrix juxtaposeMatrices(const vpMatrix &A,const vpMatrix &B) ;
244  // Juxtapose to matrices C = [ A B ]
245  static void juxtaposeMatrices(const vpMatrix &A,const vpMatrix &B, vpMatrix &C) ;
246  // Compute Kronecker product matrix
247  static void kron(const vpMatrix &m1, const vpMatrix &m2 , vpMatrix &out);
248 
249  // Compute Kronecker product matrix
250  static vpMatrix kron(const vpMatrix &m1, const vpMatrix &m2 );
251 
262  static inline bool loadMatrix(std::string filename, vpMatrix &M,
263  const bool binary = false, char *Header = NULL)
264  {
265  return vpMatrix::loadMatrix(filename.c_str(), M, binary, Header);
266  }
267  static bool loadMatrix(const char *filename, vpMatrix &M, const bool binary = false, char *Header = NULL);
268  static void mult2Matrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C);
269  static void multMatrixVector(const vpMatrix &A, const vpColVector &b, vpColVector &c);
270  static void negateMatrix(const vpMatrix &A, vpMatrix &C);
283  static inline bool saveMatrix(std::string filename, const vpMatrix &M,
284  const bool binary = false,
285  const char *Header = "")
286  {
287  return vpMatrix::saveMatrix(filename.c_str(), M, binary, Header);
288  }
289  static bool saveMatrix(const char *filename, const vpMatrix &M, const bool binary = false, const char *Header = "");
290 
301  static inline bool saveMatrixYAML(std::string filename, const vpMatrix &M, const char *header = "")
302  {
303  return vpMatrix::saveMatrixYAML(filename.c_str(), M, header);
304  }
305  static bool saveMatrixYAML(const char *filename, const vpMatrix &M, const char *header = "");
315  static inline bool loadMatrixYAML(std::string filename, vpMatrix &M, char *header = NULL)
316  {
317  return vpMatrix::loadMatrixYAML(filename.c_str(), M, header);
318  }
319  static bool loadMatrixYAML(const char *filename, vpMatrix &M, char *header = NULL);
320 
321 
322  // Stack the matrix A below the current one, copy if not initialized this = [ this A ]^T
323  void stackMatrices(const vpMatrix &A);
325  static vpMatrix stackMatrices(const vpMatrix &A,const vpMatrix &B) ;
327  static void stackMatrices(const vpMatrix &A,const vpMatrix &B, vpMatrix &C) ;
328  static void sub2Matrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C);
329 
330 
331  //---------------------------------
332  // Matrix operations.
333  //---------------------------------
336  // operation A = A + B
337  vpMatrix &operator+=(const vpMatrix &B);
338  // operation A = A - B
339  vpMatrix &operator-=(const vpMatrix &B);
340 
341  vpMatrix operator*(const vpMatrix &B) const;
342  vpMatrix operator*(const vpHomography &H) const;
343  vpMatrix operator+(const vpMatrix &B) const;
344  vpMatrix operator-(const vpMatrix &B) const;
345  vpMatrix operator-() const;
346 
347  //---------------------------------
348  // Matrix/vector operations.
349  //---------------------------------
350 
351  vpColVector operator*(const vpColVector &b) const;
352  // operation c = A * b (A is unchanged, c and b are translation vectors)
353  vpTranslationVector operator*(const vpTranslationVector &b) const;
354  //---------------------------------
355  // Matrix/real operations.
356  //---------------------------------
357 
359  vpMatrix &operator+=(const double x);
361  vpMatrix &operator-=(const double x);
363  vpMatrix &operator*=(const double x);
365  vpMatrix &operator/=(double x);
366 
367  // Cij = Aij * x (A is unchanged)
368  vpMatrix operator*(const double x) const;
369  // Cij = Aij / x (A is unchanged)
370  vpMatrix operator/(const double x) const;
371 
373  double sumSquare() const;
374 
375  // return the determinant of the matrix.
376  double det(vpDetMethod method = LU_DECOMPOSITION) const;
377 
378  //Compute the exponential matrix of a square matrix
379  vpMatrix expm();
380 
381  //-------------------------------------------------
382  // Columns, Rows extraction, SubMatrix
383  //-------------------------------------------------
386  vpRowVector row(const unsigned int i);
389  vpColVector column(const unsigned int j);
391  void init(const vpMatrix &m, unsigned int r, unsigned int c,
392  unsigned int nrows, unsigned int ncols);
394 
395  //-------------------------------------------------
396  // transpose
397  //-------------------------------------------------
400  // Compute the transpose C = A^T
401  vpMatrix t() const;
402 
403  // Compute the transpose C = A^T
404  vpMatrix transpose()const;
405  void transpose(vpMatrix & C )const;
406 
407  vpMatrix AAt() const;
408  void AAt(vpMatrix &B) const;
409 
410  vpMatrix AtA() const;
411  void AtA(vpMatrix &B) const;
413 
414 
415  //-------------------------------------------------
416  // Kronecker product
417  //-------------------------------------------------
420 
421  // Stacks columns of a matrix in a vector
422  void stackColumns(vpColVector &out );
423 
424  // Stacks columns of a matrix in a vector
425  vpColVector stackColumns();
426 
427  // Stacks columns of a matrix in a vector
428  void stackRows(vpRowVector &out );
429 
430  // Stacks columns of a matrix in a vector
431  vpRowVector stackRows();
432 
433  // Compute Kronecker product matrix
434  void kron(const vpMatrix &m1 , vpMatrix &out);
435 
436  // Compute Kronecker product matrix
437  vpMatrix kron(const vpMatrix &m1);
439 
440  //-------------------------------------------------
441  // Matrix inversion
442  //-------------------------------------------------
445 #ifndef DOXYGEN_SHOULD_SKIP_THIS
446  void LUDcmp(unsigned int* perm, int& d);
449  void LUBksb(unsigned int* perm, vpColVector& b);
450 
451 #endif // doxygen should skip this
452  // inverse matrix A using the LU decomposition
453  vpMatrix inverseByLU() const;
454 #if defined(VISP_HAVE_LAPACK)
455  // inverse matrix A using the Cholesky decomposition (only for real symmetric matrices)
456  vpMatrix inverseByCholesky() const;
457  //lapack implementation of inverse by Cholesky
458  vpMatrix inverseByCholeskyLapack() const;
459  // inverse matrix A using the QR decomposition
460  vpMatrix inverseByQR() const;
461  //lapack implementation of inverse by QR
462  vpMatrix inverseByQRLapack() const;
463 #endif
464  vpMatrix pseudoInverse(double svThreshold=1e-6) const;
468  unsigned int pseudoInverse(vpMatrix &Ap, double svThreshold=1e-6) const;
471  unsigned int pseudoInverse(vpMatrix &Ap, vpColVector &sv, double svThreshold=1e-6) const ;
474  unsigned int pseudoInverse(vpMatrix &Ap,
475  vpColVector &sv, double svThreshold,
476  vpMatrix &ImA,
477  vpMatrix &ImAt) const ;
480  unsigned int pseudoInverse(vpMatrix &Ap,
481  vpColVector &sv, double svThreshold,
482  vpMatrix &ImA,
483  vpMatrix &ImAt,
484  vpMatrix &kerA) const ;
486 
487  //-------------------------------------------------
488  // SVD decomposition
489  //-------------------------------------------------
490 
491 #ifndef DOXYGEN_SHOULD_SKIP_THIS
492  void svdFlake(vpColVector& w, vpMatrix& v);
493  void svdNr(vpColVector& w, vpMatrix& v);
494 #ifdef VISP_HAVE_GSL
495  void svdGsl(vpColVector& w, vpMatrix& v);
496 #endif
497 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101) // Require opencv >= 2.1.1
498  void svdOpenCV(vpColVector& w, vpMatrix& v);
499 #endif
500 #ifdef VISP_HAVE_LAPACK
501  void svdLapack(vpColVector& W, vpMatrix& V);
502 #endif
503  void SVBksb(const vpColVector& w,
505  const vpMatrix& v,
506  const vpColVector& b, vpColVector& x);
507 #endif
508 
511  // singular value decomposition SVD
512 
513  void svd(vpColVector& w, vpMatrix& v);
514 
515  // solve Ax=B using the SVD decomposition (usage A = solveBySVD(B,x) )
516  void solveBySVD(const vpColVector &B, vpColVector &x) const ;
517  // solve Ax=B using the SVD decomposition (usage x=A.solveBySVD(B))
518  vpColVector solveBySVD(const vpColVector &B) const ;
519 
520  unsigned int kernel(vpMatrix &KerA, double svThreshold=1e-6);
521  double cond();
523 
524  //-------------------------------------------------
525  // Eigen values and vectors
526  //-------------------------------------------------
527 
531  // compute the eigen values using the Gnu Scientific library
532  vpColVector eigenValues();
533  void eigenValues(vpColVector &evalue, vpMatrix &evector);
535 
536  // -------------------------
537  // Norms
538  // -------------------------
541  // Euclidean norm ||x||=sqrt(sum(x_i^2))
542  double euclideanNorm () const;
543  // Infinity norm ||x||=max(sum(fabs(x_i)))
544  double infinityNorm () const;
546 
547  private:
548  double detByLU() const;
549 
550 };
551 
552 
554 
555 
557 
558 
560 VISP_EXPORT vpMatrix operator*(const double &x, const vpMatrix &A) ;
561 
563 VISP_EXPORT vpColVector operator*(const double &x, const vpColVector &A) ;
564 
565 
566 
567 #endif
568 
569 
570 /*
571  * Local variables:
572  * c-basic-offset: 2
573  * End:
574  */
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:262
static bool saveMatrixYAML(std::string filename, const vpMatrix &M, const char *header="")
Definition: vpMatrix.h:301
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:315
void printSize()
Definition: vpMatrix.h:188
double * operator[](unsigned int i) const
read elements Aij (usage : x = A[i][j] )
Definition: vpMatrix.h:220
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:283