Visual Servoing Platform  version 3.0.0
vpMatrix.h
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Matrix manipulation.
32  *
33  * Authors:
34  * Eric Marchand
35  *
36  *****************************************************************************/
37 
38 #ifndef vpMatrix_H
39 #define vpMatrix_H
40 
41 #include <visp3/core/vpConfig.h>
42 #include <visp3/core/vpException.h>
43 #include <visp3/core/vpTime.h>
44 #include <visp3/core/vpArray2D.h>
45 #include <visp3/core/vpRotationMatrix.h>
46 #include <visp3/core/vpHomogeneousMatrix.h>
47 #include <visp3/core/vpVelocityTwistMatrix.h>
48 #include <visp3/core/vpForceTwistMatrix.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;
63 class vpForceTwistMatrix;
64 
92 class VISP_EXPORT vpMatrix : public vpArray2D<double>
93 {
94  public:
99  typedef enum {
100  LU_DECOMPOSITION
101  } vpDetMethod;
102 
103  public:
107  vpMatrix() : vpArray2D<double>(0, 0) {};
114  vpMatrix(unsigned int r, unsigned int c) : vpArray2D<double>(r, c) {};
122  vpMatrix(unsigned int r, unsigned int c, double val) : vpArray2D<double>(r, c, val) {};
123  vpMatrix(const vpMatrix &M, unsigned int r, unsigned int c,
124  unsigned int nrows, unsigned int ncols) ;
136  vpMatrix(const vpArray2D<double>& A) : vpArray2D<double>(A) {};
137 
139  virtual ~vpMatrix() {};
140 
145  void clear()
146  {
147  if (data != NULL ) {
148  free(data);
149  data=NULL;
150  }
151 
152  if (rowPtrs!=NULL) {
153  free(rowPtrs);
154  rowPtrs=NULL ;
155  }
156  rowNum = colNum = dsize = 0;
157  }
158 
159  //-------------------------------------------------
160  // Setting a diagonal matrix
161  //-------------------------------------------------
164  void diag(const double &val = 1.0);
165  void diag(const vpColVector &A);
166  // Initialize an identity matrix n-by-n
167  void eye();
168  void eye(unsigned int n) ;
169  // Initialize an identity matrix m-by-n
170  void eye(unsigned int m, unsigned int n) ;
172 
173  //---------------------------------
174  // Assignment
175  //---------------------------------
178  vpMatrix &operator<<(double*);
180  vpMatrix &operator=(const double x);
182 
183  //-------------------------------------------------
184  // Stacking
185  //-------------------------------------------------
188  // Stack the matrix A below the current one, copy if not initialized this = [ this A ]^T
189  void stack(const vpMatrix &A);
190  void stack(const vpRowVector &r);
191  // Stacks columns of a matrix in a vector
192  void stackColumns(vpColVector &out );
193 
194  // Stacks columns of a matrix in a vector
195  vpColVector stackColumns();
196 
197  // Stacks columns of a matrix in a vector
198  void stackRows(vpRowVector &out );
199 
200  // Stacks columns of a matrix in a vector
201  vpRowVector stackRows();
203 
204  //---------------------------------
205  // Matrix insertion with Static Public Member Functions
206  //---------------------------------
209  // Insert matrix A in the current matrix at the given position (r, c).
210  void insert(const vpMatrix&A, const unsigned int r, const unsigned int c);
212 
213  //-------------------------------------------------
214  // Columns, Rows extraction, SubMatrix
215  //-------------------------------------------------
218  vpRowVector getRow(const unsigned int i) const;
219  vpRowVector getRow(const unsigned int i, const unsigned int j_begin, const unsigned int size) const;
220  vpColVector getCol(const unsigned int j) const;
221  vpColVector getCol(const unsigned int j, const unsigned int i_begin, const unsigned int size) const;
222  void init(const vpMatrix &M, unsigned int r, unsigned int c, unsigned int nrows, unsigned int ncols);
224 
225  //---------------------------------
226  // Matrix operations.
227  //---------------------------------
230  // operation A = A + B
231  vpMatrix &operator+=(const vpMatrix &B);
232  // operation A = A - B
233  vpMatrix &operator-=(const vpMatrix &B);
234  vpMatrix operator*(const vpMatrix &B) const;
235  vpMatrix operator*(const vpRotationMatrix &R) const;
236  vpMatrix operator*(const vpVelocityTwistMatrix &V) const;
237  vpMatrix operator*(const vpForceTwistMatrix &V) const;
238  // operation t_out = A * t (A is unchanged, t and t_out are translation vectors)
240  vpColVector operator*(const vpColVector &v) const;
241  vpMatrix operator+(const vpMatrix &B) const;
242  vpMatrix operator-(const vpMatrix &B) const;
243  vpMatrix operator-() const;
244 
246  vpMatrix &operator+=(const double x);
248  vpMatrix &operator-=(const double x);
250  vpMatrix &operator*=(const double x);
252  vpMatrix &operator/=(double x);
253 
254  // Cij = Aij * x (A is unchanged)
255  vpMatrix operator*(const double x) const;
256  // Cij = Aij / x (A is unchanged)
257  vpMatrix operator/(const double x) const;
258 
264  double sum() const;
265  double sumSquare() const;
266  // return the determinant of the matrix.
267  double det(vpDetMethod method = LU_DECOMPOSITION) const;
268 
269  //Compute the exponential matrix of a square matrix
270  vpMatrix expm() const;
271 
272  //-------------------------------------------------
273  // Kronecker product
274  //-------------------------------------------------
277  // Compute Kronecker product matrix
278  void kron(const vpMatrix &m1, vpMatrix &out) const;
279 
280  // Compute Kronecker product matrix
281  vpMatrix kron(const vpMatrix &m1) const;
283 
284  //-------------------------------------------------
285  // Transpose
286  //-------------------------------------------------
289  // Compute the transpose C = A^T
290  vpMatrix t() const;
291 
292  // Compute the transpose C = A^T
293  vpMatrix transpose()const;
294  void transpose(vpMatrix & C )const;
295 
296  vpMatrix AAt() const;
297  void AAt(vpMatrix &B) const;
298 
299  vpMatrix AtA() const;
300  void AtA(vpMatrix &B) const;
302 
303  //-------------------------------------------------
304  // Matrix inversion
305  //-------------------------------------------------
308 #ifndef DOXYGEN_SHOULD_SKIP_THIS
309  void LUDcmp(unsigned int* perm, int& d);
312  void LUBksb(unsigned int* perm, vpColVector& b);
313 
314 #endif // doxygen should skip this
315  // inverse matrix A using the LU decomposition
316  vpMatrix inverseByLU() const;
317 #if defined(VISP_HAVE_LAPACK_C)
318  // inverse matrix A using the Cholesky decomposition (only for real symmetric matrices)
319  vpMatrix inverseByCholesky() const;
320  //lapack implementation of inverse by Cholesky
321  vpMatrix inverseByCholeskyLapack() const;
322  // inverse matrix A using the QR decomposition
323  vpMatrix inverseByQR() const;
324  //lapack implementation of inverse by QR
325  vpMatrix inverseByQRLapack() const;
326 #endif
327  vpMatrix pseudoInverse(double svThreshold=1e-6) const;
331  unsigned int pseudoInverse(vpMatrix &Ap, double svThreshold=1e-6) const;
334  unsigned int pseudoInverse(vpMatrix &Ap, vpColVector &sv, double svThreshold=1e-6) const ;
337  unsigned int pseudoInverse(vpMatrix &Ap,
338  vpColVector &sv, double svThreshold,
339  vpMatrix &ImA,
340  vpMatrix &ImAt) const ;
343  unsigned int pseudoInverse(vpMatrix &Ap,
344  vpColVector &sv, double svThreshold,
345  vpMatrix &ImA,
346  vpMatrix &ImAt,
347  vpMatrix &kerA) const ;
349 
350  //-------------------------------------------------
351  // SVD decomposition
352  //-------------------------------------------------
353 
354 #ifndef DOXYGEN_SHOULD_SKIP_THIS
355  void svdFlake(vpColVector& w, vpMatrix& v);
356  void svdNr(vpColVector& w, vpMatrix& v);
357 #ifdef VISP_HAVE_GSL
358  void svdGsl(vpColVector& w, vpMatrix& v);
359 #endif
360 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101) // Require opencv >= 2.1.1
361  void svdOpenCV(vpColVector& w, vpMatrix& v);
362 #endif
363 #ifdef VISP_HAVE_LAPACK_C
364  void svdLapack(vpColVector& W, vpMatrix& V);
365 #endif
366  void SVBksb(const vpColVector& w,
368  const vpMatrix& v,
369  const vpColVector& b, vpColVector& x);
370 #endif
371 
374  // singular value decomposition SVD
375 
376  void svd(vpColVector& w, vpMatrix& v);
377 
378  // solve Ax=B using the SVD decomposition (usage A = solveBySVD(B,x) )
379  void solveBySVD(const vpColVector &B, vpColVector &x) const ;
380  // solve Ax=B using the SVD decomposition (usage x=A.solveBySVD(B))
381  vpColVector solveBySVD(const vpColVector &B) const ;
382 
383  unsigned int kernel(vpMatrix &KerA, double svThreshold=1e-6) const;
384  double cond() const;
386 
387  //-------------------------------------------------
388  // Eigen values and vectors
389  //-------------------------------------------------
393  // compute the eigen values using the Gnu Scientific library
394  vpColVector eigenValues() const;
395  void eigenValues(vpColVector &evalue, vpMatrix &evector) const;
397 
398  //-------------------------------------------------
399  // Norms
400  //-------------------------------------------------
403  double euclideanNorm() const;
404  double infinityNorm() const;
406 
407  //---------------------------------
408  // Printing
409  //---------------------------------
412  int print(std::ostream& s, unsigned int length, char const* intro=0) const;
413  std::ostream & matlabPrint(std::ostream & os) const;
414  std::ostream & maplePrint(std::ostream & os) const;
415  std::ostream & csvPrint(std::ostream & os) const;
416  std::ostream & cppPrint(std::ostream & os, const char * matrixName = NULL, bool octet = false) const;
417  void printSize() const { std::cout << getRows() <<" x " << getCols() <<" " ; }
419 
420  //------------------------------------------------------------------
421  // Static functionalities
422  //------------------------------------------------------------------
423 
424  //---------------------------------
425  // Setting a diagonal matrix with Static Public Member Functions
426  //---------------------------------
429  // Create a diagonal matrix with the element of a vector DAii = Ai
430  static void createDiagonalMatrix(const vpColVector &A, vpMatrix &DA) ;
432 
433  //---------------------------------
434  // Matrix insertion with Static Public Member Functions
435  //---------------------------------
438  // Insert matrix B in matrix A at the given position (r, c).
439  static vpMatrix insert(const vpMatrix &A,const vpMatrix &B, const unsigned int r, const unsigned int c) ;
440  // Insert matrix B in matrix A (not modified) at the given position (r, c), the result is given in matrix C.
441  static void insert(const vpMatrix &A, const vpMatrix &B, vpMatrix &C, const unsigned int r, const unsigned int c) ;
442 
443  //---------------------------------
444  // Stacking with Static Public Member Functions
445  //---------------------------------
448  // Juxtapose to matrices C = [ A B ]
449  static vpMatrix juxtaposeMatrices(const vpMatrix &A,const vpMatrix &B) ;
450  // Juxtapose to matrices C = [ A B ]
451  static void juxtaposeMatrices(const vpMatrix &A,const vpMatrix &B, vpMatrix &C) ;
452  // Stack two matrices C = [ A B ]^T
453  static vpMatrix stack(const vpMatrix &A, const vpMatrix &B) ;
454  static vpMatrix stack(const vpMatrix &A, const vpRowVector &r) ;
455 
456  // Stack two matrices C = [ A B ]^T
457  static void stack(const vpMatrix &A, const vpMatrix &B, vpMatrix &C);
458  static void stack(const vpMatrix &A, const vpRowVector &r, vpMatrix &C);
460 
461  //---------------------------------
462  // Matrix operations Static Public Member Functions
463  //---------------------------------
466  static void add2Matrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C);
467  static void add2Matrices(const vpColVector &A, const vpColVector &B, vpColVector &C);
468  static void add2WeightedMatrices(const vpMatrix &A, const double &wA, const vpMatrix &B,const double &wB, vpMatrix &C);
469  static void computeHLM(const vpMatrix &H, const double &alpha, vpMatrix &HLM);
470  static void mult2Matrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C);
471  static void mult2Matrices(const vpMatrix &A, const vpMatrix &B, vpRotationMatrix &C);
472  static void mult2Matrices(const vpMatrix &A, const vpMatrix &B, vpHomogeneousMatrix &C);
473  static void mult2Matrices(const vpMatrix &A, const vpColVector &B, vpColVector &C);
474  static void multMatrixVector(const vpMatrix &A, const vpColVector &v, vpColVector &w);
475  static void negateMatrix(const vpMatrix &A, vpMatrix &C);
476  static void sub2Matrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C);
477  static void sub2Matrices(const vpColVector &A, const vpColVector &B, vpColVector &C);
479 
480  //---------------------------------
481  // Kronecker product Static Public Member Functions
482  //---------------------------------
485  // Compute Kronecker product matrix
486  static void kron(const vpMatrix &m1, const vpMatrix &m2 , vpMatrix &out);
487 
488  // Compute Kronecker product matrix
489  static vpMatrix kron(const vpMatrix &m1, const vpMatrix &m2 );
491 
492  //---------------------------------
493  // Covariance computation Static Public Member Functions
494  //---------------------------------
497  static vpMatrix computeCovarianceMatrix(const vpMatrix &A, const vpColVector &x, const vpColVector &b);
498  static vpMatrix computeCovarianceMatrix(const vpMatrix &A, const vpColVector &x, const vpColVector &b, const vpMatrix &w);
499  static vpMatrix computeCovarianceMatrixVVS(const vpHomogeneousMatrix &cMo, const vpColVector &deltaS, const vpMatrix &Ls, const vpMatrix &W);
500  static vpMatrix computeCovarianceMatrixVVS(const vpHomogeneousMatrix &cMo, const vpColVector &deltaS, const vpMatrix &Ls);
502 
503  //---------------------------------
504  // Matrix I/O Static Public Member Functions
505  //---------------------------------
518  static inline bool loadMatrix(const std::string &filename, vpArray2D<double> &M,
519  const bool binary = false, char *header = NULL)
520  {
521  return vpArray2D<double>::load(filename, M, binary, header);
522  }
523 
533  static inline bool loadMatrixYAML(const std::string &filename, vpArray2D<double> &M, char *header = NULL)
534  {
535  return vpArray2D<double>::loadYAML(filename, M, header);
536  }
537 
550  static inline bool saveMatrix(const std::string &filename, const vpArray2D<double> &M,
551  const bool binary = false,
552  const char *header = "")
553  {
554  return vpArray2D<double>::save(filename, M, binary, header);
555  }
556 
567  static inline bool saveMatrixYAML(const std::string &filename, const vpArray2D<double> &M, const char *header = "")
568  {
569  return vpArray2D<double>::saveYAML(filename, M, header);
570  }
572 
573 
574 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
575 
582  vp_deprecated void init() { };
586  vp_deprecated void stackMatrices(const vpMatrix &A) { stack(A); };
590  vp_deprecated static vpMatrix stackMatrices(const vpMatrix &A, const vpMatrix &B) { return vpMatrix::stack(A, B); };
594  vp_deprecated static void stackMatrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C) { vpMatrix::stack(A, B, C); };
598  vp_deprecated static vpMatrix stackMatrices(const vpMatrix &A, const vpRowVector &B);
602  vp_deprecated static void stackMatrices(const vpMatrix &A, const vpRowVector &B, vpMatrix &C);
606  vp_deprecated static vpMatrix stackMatrices(const vpColVector &A, const vpColVector &B);
610  vp_deprecated static void stackMatrices(const vpColVector &A, const vpColVector &B, vpColVector &C);
611 
615  vp_deprecated void setIdentity(const double & val=1.0) ;
617 #endif
618 
619  private:
620  double detByLU() const;
621  static void computeCovarianceMatrixVVS(const vpHomogeneousMatrix &cMo, const vpColVector &deltaS, const vpMatrix &Ls, vpMatrix &Js, vpColVector &deltaP);
622 
623 };
624 
625 
627 
628 #ifndef DOXYGEN_SHOULD_SKIP_THIS
629 VISP_EXPORT
630 #endif
631 vpMatrix operator*(const double &x, const vpMatrix &A) ;
632 
633 #endif
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:92
static bool save(const std::string &filename, const vpArray2D< Type > &A, const bool binary=false, const char *header="")
Definition: vpArray2D.h:468
static bool saveYAML(const std::string &filename, const vpArray2D< Type > &A, const char *header="")
Definition: vpArray2D.h:560
static bool loadMatrix(const std::string &filename, vpArray2D< double > &M, const bool binary=false, char *header=NULL)
Definition: vpMatrix.h:518
vpArray2D< Type > & operator=(Type x)
Set all the elements of the array to x.
Definition: vpArray2D.h:239
static bool loadYAML(const std::string &filename, vpArray2D< Type > &A, char *header=NULL)
Definition: vpArray2D.h:392
Implementation of an homogeneous matrix and operations on such kind of matrices.
Implementation of row vector and the associated operations.
Definition: vpRowVector.h:70
vpColVector operator*(const double &x, const vpColVector &v)
void stack(const vpMatrix &A)
Definition: vpMatrix.cpp:2922
vp_deprecated void stackMatrices(const vpMatrix &A)
Definition: vpMatrix.h:586
Implementation of a generic 2D array used as vase class of matrices and vectors.
Definition: vpArray2D.h:70
unsigned int getCols() const
Return the number of columns of the 2D array.
Definition: vpArray2D.h:154
void printSize() const
Definition: vpMatrix.h:417
vpMatrix(unsigned int r, unsigned int c, double val)
Definition: vpMatrix.h:122
vpMatrix()
Definition: vpMatrix.h:107
Implementation of a rotation matrix and operations on such kind of matrices.
vpColVector operator*(const double &x, const vpColVector &v)
static vp_deprecated void stackMatrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C)
Definition: vpMatrix.h:594
vp_deprecated void init()
Definition: vpMatrix.h:582
static bool load(const std::string &filename, vpArray2D< Type > &A, const bool binary=false, char *header=NULL)
Definition: vpArray2D.h:308
void clear()
Definition: vpMatrix.h:145
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:267
Implementation of a velocity twist matrix and operations on such kind of matrices.
unsigned int getRows() const
Return the number of rows of the 2D array.
Definition: vpArray2D.h:152
static vp_deprecated vpMatrix stackMatrices(const vpMatrix &A, const vpMatrix &B)
Definition: vpMatrix.h:590
vpMatrix(unsigned int r, unsigned int c)
Definition: vpMatrix.h:114
Implementation of a force/torque twist matrix and operations on such kind of matrices.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
virtual ~vpMatrix()
Destructor (Memory de-allocation)
Definition: vpMatrix.h:139
static bool saveMatrixYAML(const std::string &filename, const vpArray2D< double > &M, const char *header="")
Definition: vpMatrix.h:567
static bool saveMatrix(const std::string &filename, const vpArray2D< double > &M, const bool binary=false, const char *header="")
Definition: vpMatrix.h:550
vpMatrix(const vpArray2D< double > &A)
Definition: vpMatrix.h:136
Class that consider the case of a translation vector.
static bool loadMatrixYAML(const std::string &filename, vpArray2D< double > &M, char *header=NULL)
Definition: vpMatrix.h:533