Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpMatrix.h
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 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 
97 class VISP_EXPORT vpMatrix : public vpArray2D<double>
98 {
99  public:
104  typedef enum {
105  LU_DECOMPOSITION
106  } vpDetMethod;
107 
108  public:
112  vpMatrix() : vpArray2D<double>(0, 0) {}
119  vpMatrix(unsigned int r, unsigned int c) : vpArray2D<double>(r, c) {}
127  vpMatrix(unsigned int r, unsigned int c, double val) : vpArray2D<double>(r, c, val) {}
128  vpMatrix(const vpMatrix &M, unsigned int r, unsigned int c,
129  unsigned int nrows, unsigned int ncols) ;
141  vpMatrix(const vpArray2D<double>& A) : vpArray2D<double>(A) {}
142 
144  virtual ~vpMatrix() {}
145 
150  void clear()
151  {
152  if (data != NULL ) {
153  free(data);
154  data=NULL;
155  }
156 
157  if (rowPtrs!=NULL) {
158  free(rowPtrs);
159  rowPtrs=NULL ;
160  }
161  rowNum = colNum = dsize = 0;
162  }
163 
164  //-------------------------------------------------
165  // Setting a diagonal matrix
166  //-------------------------------------------------
169  void diag(const double &val = 1.0);
170  void diag(const vpColVector &A);
171  // Initialize an identity matrix n-by-n
172  void eye();
173  void eye(unsigned int n) ;
174  // Initialize an identity matrix m-by-n
175  void eye(unsigned int m, unsigned int n) ;
177 
178  //---------------------------------
179  // Assignment
180  //---------------------------------
183  vpMatrix &operator<<(double*);
185  vpMatrix &operator=(const double x);
187 
188  //-------------------------------------------------
189  // Stacking
190  //-------------------------------------------------
193  // Stack the matrix A below the current one, copy if not initialized this = [ this A ]^T
194  void stack(const vpMatrix &A);
195  void stack(const vpRowVector &r);
196  // Stacks columns of a matrix in a vector
197  void stackColumns(vpColVector &out );
198 
199  // Stacks columns of a matrix in a vector
200  vpColVector stackColumns();
201 
202  // Stacks columns of a matrix in a vector
203  void stackRows(vpRowVector &out );
204 
205  // Stacks columns of a matrix in a vector
206  vpRowVector stackRows();
208 
209  //---------------------------------
210  // Matrix insertion with Static Public Member Functions
211  //---------------------------------
214  // Insert matrix A in the current matrix at the given position (r, c).
215  void insert(const vpMatrix&A, const unsigned int r, const unsigned int c);
217 
218  //-------------------------------------------------
219  // Columns, Rows extraction, SubMatrix
220  //-------------------------------------------------
223  vpRowVector getRow(const unsigned int i) const;
224  vpRowVector getRow(const unsigned int i, const unsigned int j_begin, const unsigned int size) const;
225  vpColVector getCol(const unsigned int j) const;
226  vpColVector getCol(const unsigned int j, const unsigned int i_begin, const unsigned int size) const;
227  void init(const vpMatrix &M, unsigned int r, unsigned int c, unsigned int nrows, unsigned int ncols);
229 
230  //---------------------------------
231  // Matrix operations.
232  //---------------------------------
235  // operation A = A + B
236  vpMatrix &operator+=(const vpMatrix &B);
237  // operation A = A - B
238  vpMatrix &operator-=(const vpMatrix &B);
239  vpMatrix operator*(const vpMatrix &B) const;
240  vpMatrix operator*(const vpRotationMatrix &R) const;
241  vpMatrix operator*(const vpVelocityTwistMatrix &V) const;
242  vpMatrix operator*(const vpForceTwistMatrix &V) const;
243  // operation t_out = A * t (A is unchanged, t and t_out are translation vectors)
245  vpColVector operator*(const vpColVector &v) const;
246  vpMatrix operator+(const vpMatrix &B) const;
247  vpMatrix operator-(const vpMatrix &B) const;
248  vpMatrix operator-() const;
249 
251  vpMatrix &operator+=(const double x);
253  vpMatrix &operator-=(const double x);
255  vpMatrix &operator*=(const double x);
257  vpMatrix &operator/=(double x);
258 
259  // Cij = Aij * x (A is unchanged)
260  vpMatrix operator*(const double x) const;
261  // Cij = Aij / x (A is unchanged)
262  vpMatrix operator/(const double x) const;
263 
269  double sum() const;
270  double sumSquare() const;
271  // return the determinant of the matrix.
272  double det(vpDetMethod method = LU_DECOMPOSITION) const;
273 
274  //Compute the exponential matrix of a square matrix
275  vpMatrix expm() const;
276 
277  //-------------------------------------------------
278  // Kronecker product
279  //-------------------------------------------------
282  // Compute Kronecker product matrix
283  void kron(const vpMatrix &m1, vpMatrix &out) const;
284 
285  // Compute Kronecker product matrix
286  vpMatrix kron(const vpMatrix &m1) const;
288 
289  //-------------------------------------------------
290  // Transpose
291  //-------------------------------------------------
294  // Compute the transpose C = A^T
295  vpMatrix t() const;
296 
297  // Compute the transpose C = A^T
298  vpMatrix transpose()const;
299  void transpose(vpMatrix & C )const;
300 
301  vpMatrix AAt() const;
302  void AAt(vpMatrix &B) const;
303 
304  vpMatrix AtA() const;
305  void AtA(vpMatrix &B) const;
307 
308  //-------------------------------------------------
309  // Matrix inversion
310  //-------------------------------------------------
313 #ifndef DOXYGEN_SHOULD_SKIP_THIS
314  void LUDcmp(unsigned int* perm, int& d);
317  void LUBksb(unsigned int* perm, vpColVector& b);
318 
319 #endif // doxygen should skip this
320  // inverse matrix A using the LU decomposition
321  vpMatrix inverseByLU() const;
322 #if defined(VISP_HAVE_LAPACK_C)
323  // inverse matrix A using the Cholesky decomposition (only for real symmetric matrices)
324  vpMatrix inverseByCholesky() const;
325  //lapack implementation of inverse by Cholesky
326  vpMatrix inverseByCholeskyLapack() const;
327  // inverse matrix A using the QR decomposition
328  vpMatrix inverseByQR() const;
329  //lapack implementation of inverse by QR
330  vpMatrix inverseByQRLapack() const;
331 #endif
332  vpMatrix pseudoInverse(double svThreshold=1e-6) const;
336  unsigned int pseudoInverse(vpMatrix &Ap, double svThreshold=1e-6) const;
339  unsigned int pseudoInverse(vpMatrix &Ap, vpColVector &sv, double svThreshold=1e-6) const ;
342  unsigned int pseudoInverse(vpMatrix &Ap,
343  vpColVector &sv, double svThreshold,
344  vpMatrix &ImA,
345  vpMatrix &ImAt) const ;
348  unsigned int pseudoInverse(vpMatrix &Ap,
349  vpColVector &sv, double svThreshold,
350  vpMatrix &ImA,
351  vpMatrix &ImAt,
352  vpMatrix &kerA) const ;
354 
355  //-------------------------------------------------
356  // SVD decomposition
357  //-------------------------------------------------
358 
359 #ifndef DOXYGEN_SHOULD_SKIP_THIS
360  void svdFlake(vpColVector& w, vpMatrix& v);
361  void svdNr(vpColVector& w, vpMatrix& v);
362 #ifdef VISP_HAVE_GSL
363  void svdGsl(vpColVector& w, vpMatrix& v);
364 #endif
365 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101) // Require opencv >= 2.1.1
366  void svdOpenCV(vpColVector& w, vpMatrix& v);
367 #endif
368 #ifdef VISP_HAVE_LAPACK_C
369  void svdLapack(vpColVector& W, vpMatrix& V);
370 #endif
371  void SVBksb(const vpColVector& w,
373  const vpMatrix& v,
374  const vpColVector& b, vpColVector& x);
375 #endif
376 
379  // singular value decomposition SVD
380 
381  void svd(vpColVector& w, vpMatrix& v);
382 
383  // solve Ax=B using the SVD decomposition (usage A = solveBySVD(B,x) )
384  void solveBySVD(const vpColVector &B, vpColVector &x) const ;
385  // solve Ax=B using the SVD decomposition (usage x=A.solveBySVD(B))
386  vpColVector solveBySVD(const vpColVector &B) const ;
387 
388  unsigned int kernel(vpMatrix &KerA, double svThreshold=1e-6) const;
389  double cond() const;
391 
392  //-------------------------------------------------
393  // Eigen values and vectors
394  //-------------------------------------------------
398  // compute the eigen values using the Gnu Scientific library
399  vpColVector eigenValues() const;
400  void eigenValues(vpColVector &evalue, vpMatrix &evector) const;
402 
403  //-------------------------------------------------
404  // Norms
405  //-------------------------------------------------
408  double euclideanNorm() const;
409  double infinityNorm() const;
411 
412  //---------------------------------
413  // Printing
414  //---------------------------------
417  std::ostream & cppPrint(std::ostream & os, const std::string &matrixName = "A", bool octet = false) const;
418  std::ostream & csvPrint(std::ostream & os) const;
419  std::ostream & maplePrint(std::ostream & os) const;
420  std::ostream & matlabPrint(std::ostream & os) const;
421  int print(std::ostream& s, unsigned int length, char const* intro=0) const;
422  void printSize() const { std::cout << getRows() <<" x " << getCols() <<" " ; }
424 
425  //------------------------------------------------------------------
426  // Static functionalities
427  //------------------------------------------------------------------
428 
429  //---------------------------------
430  // Setting a diagonal matrix with Static Public Member Functions
431  //---------------------------------
434  // Create a diagonal matrix with the element of a vector DAii = Ai
435  static void createDiagonalMatrix(const vpColVector &A, vpMatrix &DA) ;
437 
438  //---------------------------------
439  // Matrix insertion with Static Public Member Functions
440  //---------------------------------
443  // Insert matrix B in matrix A at the given position (r, c).
444  static vpMatrix insert(const vpMatrix &A,const vpMatrix &B, const unsigned int r, const unsigned int c) ;
445  // Insert matrix B in matrix A (not modified) at the given position (r, c), the result is given in matrix C.
446  static void insert(const vpMatrix &A, const vpMatrix &B, vpMatrix &C, const unsigned int r, const unsigned int c) ;
447 
448  //---------------------------------
449  // Stacking with Static Public Member Functions
450  //---------------------------------
453  // Juxtapose to matrices C = [ A B ]
454  static vpMatrix juxtaposeMatrices(const vpMatrix &A,const vpMatrix &B) ;
455  // Juxtapose to matrices C = [ A B ]
456  static void juxtaposeMatrices(const vpMatrix &A,const vpMatrix &B, vpMatrix &C) ;
457  // Stack two matrices C = [ A B ]^T
458  static vpMatrix stack(const vpMatrix &A, const vpMatrix &B) ;
459  static vpMatrix stack(const vpMatrix &A, const vpRowVector &r) ;
460 
461  // Stack two matrices C = [ A B ]^T
462  static void stack(const vpMatrix &A, const vpMatrix &B, vpMatrix &C);
463  static void stack(const vpMatrix &A, const vpRowVector &r, vpMatrix &C);
465 
466  //---------------------------------
467  // Matrix operations Static Public Member Functions
468  //---------------------------------
471  static void add2Matrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C);
472  static void add2Matrices(const vpColVector &A, const vpColVector &B, vpColVector &C);
473  static void add2WeightedMatrices(const vpMatrix &A, const double &wA, const vpMatrix &B,const double &wB, vpMatrix &C);
474  static void computeHLM(const vpMatrix &H, const double &alpha, vpMatrix &HLM);
475  static void mult2Matrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C);
476  static void mult2Matrices(const vpMatrix &A, const vpMatrix &B, vpRotationMatrix &C);
477  static void mult2Matrices(const vpMatrix &A, const vpMatrix &B, vpHomogeneousMatrix &C);
478  static void mult2Matrices(const vpMatrix &A, const vpColVector &B, vpColVector &C);
479  static void multMatrixVector(const vpMatrix &A, const vpColVector &v, vpColVector &w);
480  static void negateMatrix(const vpMatrix &A, vpMatrix &C);
481  static void sub2Matrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C);
482  static void sub2Matrices(const vpColVector &A, const vpColVector &B, vpColVector &C);
484 
485  //---------------------------------
486  // Kronecker product Static Public Member Functions
487  //---------------------------------
490  // Compute Kronecker product matrix
491  static void kron(const vpMatrix &m1, const vpMatrix &m2 , vpMatrix &out);
492 
493  // Compute Kronecker product matrix
494  static vpMatrix kron(const vpMatrix &m1, const vpMatrix &m2 );
496 
497  //---------------------------------
498  // Covariance computation Static Public Member Functions
499  //---------------------------------
502  static vpMatrix computeCovarianceMatrix(const vpMatrix &A, const vpColVector &x, const vpColVector &b);
503  static vpMatrix computeCovarianceMatrix(const vpMatrix &A, const vpColVector &x, const vpColVector &b, const vpMatrix &w);
504  static vpMatrix computeCovarianceMatrixVVS(const vpHomogeneousMatrix &cMo, const vpColVector &deltaS, const vpMatrix &Ls, const vpMatrix &W);
505  static vpMatrix computeCovarianceMatrixVVS(const vpHomogeneousMatrix &cMo, const vpColVector &deltaS, const vpMatrix &Ls);
507 
508  //---------------------------------
509  // Matrix I/O Static Public Member Functions
510  //---------------------------------
523  static inline bool loadMatrix(const std::string &filename, vpArray2D<double> &M,
524  const bool binary = false, char *header = NULL)
525  {
526  return vpArray2D<double>::load(filename, M, binary, header);
527  }
528 
538  static inline bool loadMatrixYAML(const std::string &filename, vpArray2D<double> &M, char *header = NULL)
539  {
540  return vpArray2D<double>::loadYAML(filename, M, header);
541  }
542 
555  static inline bool saveMatrix(const std::string &filename, const vpArray2D<double> &M,
556  const bool binary = false,
557  const char *header = "")
558  {
559  return vpArray2D<double>::save(filename, M, binary, header);
560  }
561 
572  static inline bool saveMatrixYAML(const std::string &filename, const vpArray2D<double> &M, const char *header = "")
573  {
574  return vpArray2D<double>::saveYAML(filename, M, header);
575  }
577 
578 
579 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
580 
587  vp_deprecated void init() { }
591  vp_deprecated void stackMatrices(const vpMatrix &A) { stack(A); }
595  vp_deprecated static vpMatrix stackMatrices(const vpMatrix &A, const vpMatrix &B) { return vpMatrix::stack(A, B); }
599  vp_deprecated static void stackMatrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C) { vpMatrix::stack(A, B, C); }
603  vp_deprecated static vpMatrix stackMatrices(const vpMatrix &A, const vpRowVector &B);
607  vp_deprecated static void stackMatrices(const vpMatrix &A, const vpRowVector &B, vpMatrix &C);
611  vp_deprecated static vpMatrix stackMatrices(const vpColVector &A, const vpColVector &B);
615  vp_deprecated static void stackMatrices(const vpColVector &A, const vpColVector &B, vpColVector &C);
616 
620  vp_deprecated void setIdentity(const double & val=1.0) ;
621 
622  vp_deprecated vpRowVector row(const unsigned int i);
623  vp_deprecated vpColVector column(const unsigned int j);
624 
626 #endif
627 
628  private:
629  double detByLU() const;
630  static void computeCovarianceMatrixVVS(const vpHomogeneousMatrix &cMo, const vpColVector &deltaS, const vpMatrix &Ls, vpMatrix &Js, vpColVector &deltaP);
631 
632 };
633 
634 
636 
637 #ifndef DOXYGEN_SHOULD_SKIP_THIS
638 VISP_EXPORT
639 #endif
640 vpMatrix operator*(const double &x, const vpMatrix &A) ;
641 
642 #endif
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:97
static bool save(const std::string &filename, const vpArray2D< Type > &A, const bool binary=false, const char *header="")
Definition: vpArray2D.h:499
static bool saveYAML(const std::string &filename, const vpArray2D< Type > &A, const char *header="")
Definition: vpArray2D.h:588
static bool loadMatrix(const std::string &filename, vpArray2D< double > &M, const bool binary=false, char *header=NULL)
Definition: vpMatrix.h:523
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:417
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:2981
vp_deprecated void stackMatrices(const vpMatrix &A)
Definition: vpMatrix.h:591
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:422
vpMatrix(unsigned int r, unsigned int c, double val)
Definition: vpMatrix.h:127
vpMatrix()
Definition: vpMatrix.h:112
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:599
vp_deprecated void init()
Definition: vpMatrix.h:587
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:150
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:595
vpMatrix(unsigned int r, unsigned int c)
Definition: vpMatrix.h:119
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:144
static bool saveMatrixYAML(const std::string &filename, const vpArray2D< double > &M, const char *header="")
Definition: vpMatrix.h:572
static bool saveMatrix(const std::string &filename, const vpArray2D< double > &M, const bool binary=false, const char *header="")
Definition: vpMatrix.h:555
vpMatrix(const vpArray2D< double > &A)
Definition: vpMatrix.h:141
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:538