ViSP  2.6.2
vpMatrix.h
1 /****************************************************************************
2  *
3  * $Id: vpMatrix.h 3842 2012-07-13 22:21:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2012 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 & cppPrint(std::ostream & os, const char * matrixName = NULL, bool octet = false);
181 
182  void printSize() { std::cout << getRows() <<" x " << getCols() <<" " ; }
184 
185  static bool saveMatrix(const char *filename, const vpMatrix &M, const bool binary = false, const char *Header = "");
186  static bool loadMatrix(const char *filename, vpMatrix &M, const bool binary = false, char *Header = NULL);
187 
200  static inline bool saveMatrix(std::string filename, const vpMatrix &M,
201  const bool binary = false,
202  const char *Header = "")
203  {
204  return vpMatrix::saveMatrix(filename.c_str(), M, binary, Header);
205  }
206 
217  static inline bool loadMatrix(std::string filename, vpMatrix &M,
218  const bool binary = false, char *Header = NULL)
219  {
220  return vpMatrix::loadMatrix(filename.c_str(), M, binary, Header);
221  }
222 
223  //---------------------------------
224  // Copy / assignment
225  //---------------------------------
228  vpMatrix (const vpMatrix& m);
230 
231  // Assignment from an array
232  vpMatrix &operator<<(double*);
233 
235  vpMatrix &operator=(const vpMatrix &B);
237  vpMatrix &operator=(const double x);
238  void diag(const vpColVector &A);
240 
241  //---------------------------------
242  // Access/modification operators
243  //---------------------------------
246  inline double *operator[](unsigned int n) { return rowPtrs[n]; }
249  inline double *operator[](unsigned int n) const {return rowPtrs[n];}
251 
252  //---------------------------------
253  // Matrix operations (Static).
254  //---------------------------------
255 
256  static void mult2Matrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C);
257  static void add2Matrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C);
258  static void add2WeightedMatrices(const vpMatrix &A, const double &wA, const vpMatrix &B,const double &wB, vpMatrix &C);
259  static void sub2Matrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C);
260  static void negateMatrix(const vpMatrix &A, vpMatrix &C);
261  static void multMatrixVector(const vpMatrix &A, const vpColVector &b, vpColVector &c);
262 
263  static vpMatrix computeCovarianceMatrix(const vpMatrix &A, const vpColVector &x, const vpColVector &b);
264  static vpMatrix computeCovarianceMatrix(const vpMatrix &A, const vpColVector &x, const vpColVector &b, const vpMatrix &w);
265 
266  //---------------------------------
267  // Matrix operations.
268  //---------------------------------
271  // operation A = A + B
272  vpMatrix &operator+=(const vpMatrix &B);
273  // operation A = A - B
274  vpMatrix &operator-=(const vpMatrix &B);
275 
276  vpMatrix operator*(const vpMatrix &B) const;
277  vpMatrix operator+(const vpMatrix &B) const;
278  vpMatrix operator-(const vpMatrix &B) const;
279  vpMatrix operator-() const;
280 
281  //---------------------------------
282  // Matrix/vector operations.
283  //---------------------------------
284 
285  vpColVector operator*(const vpColVector &b) const;
286  // operation c = A * b (A is unchanged, c and b are translation vectors)
287  vpTranslationVector operator*(const vpTranslationVector &b) const;
288  //---------------------------------
289  // Matrix/real operations.
290  //---------------------------------
291 
293  vpMatrix &operator+=(const double x);
295  vpMatrix &operator-=(const double x);
297  vpMatrix &operator*=(const double x);
299  vpMatrix &operator/=(double x);
300 
301  // Cij = Aij * x (A is unchanged)
302  vpMatrix operator*(const double x) const;
303  // Cij = Aij / x (A is unchanged)
304  vpMatrix operator/(const double x) const;
305 
307  double sumSquare() const;
308 
309  // return the determinant of the matrix.
310  double det(vpDetMethod method = LU_DECOMPOSITION) const;
311 
312  //Compute the exponential matrix of a square matrix
313  vpMatrix expm();
314 
315  //-------------------------------------------------
316  // Columns, Rows extraction, SubMatrix
317  //-------------------------------------------------
320  vpRowVector row(const unsigned int i);
323  vpColVector column(const unsigned int j);
325  void init(const vpMatrix &m, unsigned int r, unsigned int c,
326  unsigned int nrows, unsigned int ncols);
328 
329  //-------------------------------------------------
330  // transpose
331  //-------------------------------------------------
334  // Compute the transpose C = A^T
335  vpMatrix t() const;
336 
337  // Compute the transpose C = A^T
338  vpMatrix transpose()const;
339  void transpose(vpMatrix & C )const;
340 
341  vpMatrix AAt() const;
342  void AAt(vpMatrix &B) const;
343 
344  vpMatrix AtA() const;
345  void AtA(vpMatrix &B) const;
347 
348 
349  //-------------------------------------------------
350  // Kronecker product
351  //-------------------------------------------------
354 
355  // Stacks columns of a matrix in a vector
356  void stackColumns(vpColVector &out );
357 
358  // Stacks columns of a matrix in a vector
359  vpColVector stackColumns();
360 
361  // Stacks columns of a matrix in a vector
362  void stackRows(vpRowVector &out );
363 
364  // Stacks columns of a matrix in a vector
365  vpRowVector stackRows();
366 
367  // Compute Kronecker product matrix
368  void kron(const vpMatrix &m1 , vpMatrix &out);
369 
370  // Compute Kronecker product matrix
371  vpMatrix kron(const vpMatrix &m1);
373 
374  // Compute Kronecker product matrix
375  static void kron(const vpMatrix &m1, const vpMatrix &m2 , vpMatrix &out);
376 
377  // Compute Kronecker product matrix
378  static vpMatrix kron(const vpMatrix &m1, const vpMatrix &m2 );
379 
380 
381  //-------------------------------------------------
382  // LU decomposition
383  //-------------------------------------------------
386 #ifndef DOXYGEN_SHOULD_SKIP_THIS
387  void LUDcmp(unsigned int* perm, int& d);
390  void LUBksb(unsigned int* perm, vpColVector& b);
391 #endif // doxygen should skip this
392 
393  // inverse matrix A using the LU decomposition
394  vpMatrix inverseByLU() const;
396 
397  //-------------------------------------------------
398  // SVD decomposition
399  //-------------------------------------------------
400 
401 #ifndef DOXYGEN_SHOULD_SKIP_THIS
402  void svdFlake(vpColVector& w, vpMatrix& v);
403  void svdNr(vpColVector& w, vpMatrix& v);
404 #ifdef VISP_HAVE_GSL
405  void svdGsl(vpColVector& w, vpMatrix& v);
406 #endif
407 #if (VISP_HAVE_OPENCV_VERSION >= 0x020100) // Require opencv >= 2.1.0
408  void svdOpenCV(vpColVector& w, vpMatrix& v);
409 #endif
410 #ifdef VISP_HAVE_LAPACK
411  void svdLapack(vpColVector& W, vpMatrix& V);
412 #endif
413  void SVBksb(const vpColVector& w,
415  const vpMatrix& v,
416  const vpColVector& b, vpColVector& x);
417 #endif
418 
421  // singular value decomposition SVD
422 
423  void svd(vpColVector& w, vpMatrix& v);
424 
425  // solve Ax=B using the SVD decomposition (usage A = solveBySVD(B,x) )
426  void solveBySVD(const vpColVector &B, vpColVector &x) const ;
427  // solve Ax=B using the SVD decomposition (usage x=A.solveBySVD(B))
428  vpColVector solveBySVD(const vpColVector &B) const ;
429 
431  vpMatrix pseudoInverse(double svThreshold=1e-6) const;
434  unsigned int pseudoInverse(vpMatrix &Ap, double svThreshold=1e-6) const;
437  unsigned int pseudoInverse(vpMatrix &Ap, vpColVector &sv, double svThreshold=1e-6) const ;
440  unsigned int pseudoInverse(vpMatrix &Ap,
441  vpColVector &sv, double svThreshold,
442  vpMatrix &ImA,
443  vpMatrix &ImAt) const ;
446  unsigned int pseudoInverse(vpMatrix &Ap,
447  vpColVector &sv, double svThreshold,
448  vpMatrix &ImA,
449  vpMatrix &ImAt,
450  vpMatrix &kerA) const ;
451 
452  unsigned int kernel(vpMatrix &KerA, double svThreshold=1e-6);
454 
455  //-------------------------------------------------
456  // Eigen values and vectors
457  //-------------------------------------------------
458 
462  // compute the eigen values using the Gnu Scientific library
463  vpColVector eigenValues();
464  void eigenValues(vpColVector &evalue, vpMatrix &evector);
466 
468  static vpMatrix stackMatrices(const vpMatrix &A,const vpMatrix &B) ;
470  static void stackMatrices(const vpMatrix &A,const vpMatrix &B, vpMatrix &C) ;
471  // Juxtapose to matrices C = [ A B ]
472  static vpMatrix juxtaposeMatrices(const vpMatrix &A,const vpMatrix &B) ;
473  // Juxtapose to matrices C = [ A B ]
474  static void juxtaposeMatrices(const vpMatrix &A,const vpMatrix &B, vpMatrix &C) ;
475 
476  // Create a diagonal matrix with the element of a vector DAii = Ai
477  static void createDiagonalMatrix(const vpColVector &A, vpMatrix &DA) ;
478 
479  // Stack the matrix A below the current one, copy if not initialized this = [ this A ]^T
480  void stackMatrices(const vpMatrix &A);
481 
482  // Insert matrix A in the current matrix at the given position (r, c).
483  void insert(const vpMatrix&A, const unsigned int r, const unsigned int c);
484  // Insert matrix B in matrix A at the given position (r, c).
485  static vpMatrix insert(const vpMatrix &A,const vpMatrix &B, const unsigned int r, const unsigned int c) ;
486  // Insert matrix B in matrix A (not modified) at the given position (r, c), the result is given in matrix C.
487  static void insert(const vpMatrix &A, const vpMatrix &B, vpMatrix &C, const unsigned int r, const unsigned int c) ;
488 
489  // -------------------------
490  // Norms
491  // -------------------------
494  // Euclidean norm ||x||=sqrt(sum(x_i^2))
495  double euclideanNorm () const;
496  // Infinity norm ||x||=max(sum(fabs(x_i)))
497  double infinityNorm () const;
499 
500  private:
501  double detByLU() const;
502 
503 };
504 
505 
507 
508 
510 
511 
513 VISP_EXPORT vpMatrix operator*(const double &x, const vpMatrix &A) ;
514 
516 VISP_EXPORT vpColVector operator*(const double &x, const vpColVector &A) ;
517 
518 
519 
520 #endif
521 
522 
523 /*
524  * Local variables:
525  * c-basic-offset: 2
526  * End:
527  */
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:217
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:3409
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:3482
void printSize()
Definition: vpMatrix.h:182
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:249
static bool saveMatrix(std::string filename, const vpMatrix &M, const bool binary=false, const char *Header="")
Definition: vpMatrix.h:200