Visual Servoing Platform  version 3.6.1 under development (2024-04-19)
vpMatrix.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
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 https://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 
34 #ifndef vpMatrix_H
35 #define vpMatrix_H
36 
37 #include <visp3/core/vpArray2D.h>
38 #include <visp3/core/vpConfig.h>
39 #include <visp3/core/vpException.h>
40 #include <visp3/core/vpForceTwistMatrix.h>
41 #include <visp3/core/vpHomogeneousMatrix.h>
42 #include <visp3/core/vpRotationMatrix.h>
43 #include <visp3/core/vpTime.h>
44 #include <visp3/core/vpVelocityTwistMatrix.h>
45 
46 #include <iostream>
47 #include <math.h>
48 
49 class vpRowVector;
50 class vpColVector;
54 class vpForceTwistMatrix;
55 
145 class VISP_EXPORT vpMatrix : public vpArray2D<double>
146 {
147 public:
152  typedef enum
153  {
154  LU_DECOMPOSITION
155  } vpDetMethod;
156 
157 public:
162  vpMatrix() : vpArray2D<double>(0, 0) { }
163 
170  vpMatrix(unsigned int r, unsigned int c) : vpArray2D<double>(r, c) { }
171 
179  vpMatrix(unsigned int r, unsigned int c, double val) : vpArray2D<double>(r, c, val) { }
180  vpMatrix(const vpMatrix &M, unsigned int r, unsigned int c, unsigned int nrows, unsigned int ncols);
181 
194  vpMatrix(const vpArray2D<double> &A) : vpArray2D<double>(A) { }
195 
196  vpMatrix(const vpMatrix &A) : vpArray2D<double>(A) { }
197 
198 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
199  vpMatrix(vpMatrix &&A);
200  explicit vpMatrix(const std::initializer_list<double> &list);
201  explicit vpMatrix(unsigned int nrows, unsigned int ncols, const std::initializer_list<double> &list);
202  explicit vpMatrix(const std::initializer_list<std::initializer_list<double> > &lists);
203 #endif
204 
209  void clear()
210  {
211  if (data != nullptr) {
212  free(data);
213  data = nullptr;
214  }
215 
216  if (rowPtrs != nullptr) {
217  free(rowPtrs);
218  rowPtrs = nullptr;
219  }
220  rowNum = colNum = dsize = 0;
221  }
222 
223  //-------------------------------------------------
224  // Setting a diagonal matrix
225  //-------------------------------------------------
236  static unsigned int getLapackMatrixMinSize() { return m_lapack_min_size; }
237 
250  static void setLapackMatrixMinSize(unsigned int min_size) { m_lapack_min_size = min_size; }
252 
253  //-------------------------------------------------
254  // Setting a diagonal matrix
255  //-------------------------------------------------
258  void diag(const double &val = 1.0);
259  void diag(const vpColVector &A);
260  // Initialize an identity matrix n-by-n
261  void eye();
262  void eye(unsigned int n);
263  // Initialize an identity matrix m-by-n
264  void eye(unsigned int m, unsigned int n);
266 
267  //---------------------------------
268  // Assignment
269  //---------------------------------
272  vpMatrix &operator<<(double *);
273  vpMatrix &operator<<(double val);
274  vpMatrix &operator,(double val);
276  vpMatrix &operator=(const vpMatrix &A);
277 
278 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
280 
281  vpMatrix &operator=(const std::initializer_list<double> &list);
282  vpMatrix &operator=(const std::initializer_list<std::initializer_list<double> > &lists);
283 #endif
284  vpMatrix &operator=(double x);
286 
287  //-------------------------------------------------
288  // Stacking
289  //-------------------------------------------------
292  // Stack the matrix A below the current one, copy if not initialized this =
293  // [ this A ]^T
294  void stack(const vpMatrix &A);
295  void stack(const vpRowVector &r);
296  void stack(const vpColVector &c);
297  // Stacks columns of a matrix in a vector
298  void stackColumns(vpColVector &out);
299 
300  // Stacks columns of a matrix in a vector
301  vpColVector stackColumns();
302 
303  // Stacks columns of a matrix in a vector
304  void stackRows(vpRowVector &out);
305 
306  // Stacks columns of a matrix in a vector
307  vpRowVector stackRows();
309 
310  //---------------------------------
311  // Matrix insertion
312  //---------------------------------
315  // Insert matrix A in the current matrix at the given position (r, c).
316  void insert(const vpMatrix &A, unsigned int r, unsigned int c);
318 
319  //-------------------------------------------------
320  // Columns, Rows, Diag extraction, SubMatrix
321  //-------------------------------------------------
324  vpMatrix extract(unsigned int r, unsigned int c, unsigned int nrows, unsigned int ncols) const;
325  vpColVector getCol(unsigned int j) const;
326  vpColVector getCol(unsigned int j, unsigned int i_begin, unsigned int size) const;
327  vpRowVector getRow(unsigned int i) const;
328  vpRowVector getRow(unsigned int i, unsigned int j_begin, unsigned int size) const;
329  vpColVector getDiag() const;
330  void init(const vpMatrix &M, unsigned int r, unsigned int c, unsigned int nrows, unsigned int ncols);
332 
333  //---------------------------------
334  // Matrix operations.
335  //---------------------------------
338  // return the determinant of the matrix.
339  double det(vpDetMethod method = LU_DECOMPOSITION) const;
340  double detByLU() const;
341 #if defined(VISP_HAVE_EIGEN3)
342  double detByLUEigen3() const;
343 #endif
344 #if defined(VISP_HAVE_LAPACK)
345  double detByLULapack() const;
346 #endif
347 #if defined(VISP_HAVE_OPENCV)
348  double detByLUOpenCV() const;
349 #endif
350 
351  // Compute the exponential matrix of a square matrix
352  vpMatrix expm() const;
353 
354  // operation A = A + B
355  vpMatrix &operator+=(const vpMatrix &B);
356  // operation A = A - B
357  vpMatrix &operator-=(const vpMatrix &B);
358  vpMatrix operator*(const vpMatrix &B) const;
359  vpMatrix operator*(const vpRotationMatrix &R) const;
360  vpMatrix operator*(const vpHomogeneousMatrix &R) const;
361  vpMatrix operator*(const vpVelocityTwistMatrix &V) const;
362  vpMatrix operator*(const vpForceTwistMatrix &V) const;
363  // operation t_out = A * t (A is unchanged, t and t_out are translation
364  // vectors)
366  vpColVector operator*(const vpColVector &v) const;
367  vpMatrix operator+(const vpMatrix &B) const;
368  vpMatrix operator-(const vpMatrix &B) const;
369  vpMatrix operator-() const;
370 
372  vpMatrix &operator+=(double x);
374  vpMatrix &operator-=(double x);
376  vpMatrix &operator*=(double x);
378  vpMatrix &operator/=(double x);
379 
380  // Cij = Aij * x (A is unchanged)
381  vpMatrix operator*(double x) const;
382  // Cij = Aij / x (A is unchanged)
383  vpMatrix operator/(double x) const;
384 
390  double sum() const;
391  double sumSquare() const;
392 
393  //-------------------------------------------------
394  // Hadamard product
395  //-------------------------------------------------
397  vpMatrix hadamard(const vpMatrix &m) const;
398 
399  //-------------------------------------------------
400  // Kronecker product
401  //-------------------------------------------------
404  // Compute Kronecker product matrix
405  void kron(const vpMatrix &m1, vpMatrix &out) const;
406 
407  // Compute Kronecker product matrix
408  vpMatrix kron(const vpMatrix &m1) const;
410 
411  //-------------------------------------------------
412  // Transpose
413  //-------------------------------------------------
416  // Compute the transpose C = A^T
417  vpMatrix t() const;
418 
419  // Compute the transpose C = A^T
420  vpMatrix transpose() const;
421  void transpose(vpMatrix &At) const;
422 
423  vpMatrix AAt() const;
424  void AAt(vpMatrix &B) const;
425 
426  vpMatrix AtA() const;
427  void AtA(vpMatrix &B) const;
429 
430  //-------------------------------------------------
431  // Matrix inversion
432  //-------------------------------------------------
435  // inverse matrix A using the LU decomposition
436  vpMatrix inverseByLU() const;
437 
438 #if defined(VISP_HAVE_EIGEN3)
439  vpMatrix inverseByLUEigen3() const;
440 #endif
441 #if defined(VISP_HAVE_LAPACK)
442  vpMatrix inverseByLULapack() const;
443 #endif
444 #if defined(VISP_HAVE_OPENCV)
445  vpMatrix inverseByLUOpenCV() const;
446 #endif
447 
448  // inverse matrix A using the Cholesky decomposition (only for real
449  // symmetric matrices)
450  vpMatrix inverseByCholesky() const;
451 
452 #if defined(VISP_HAVE_LAPACK)
453  vpMatrix inverseByCholeskyLapack() const;
454 #endif
455 #if defined(VISP_HAVE_OPENCV)
456  vpMatrix inverseByCholeskyOpenCV() const;
457 #endif
458 
459  // inverse matrix A using the QR decomposition
460  vpMatrix inverseByQR() const;
461 #if defined(VISP_HAVE_LAPACK)
462  vpMatrix inverseByQRLapack() const;
463 #endif
464 
465  // inverse triangular matrix
466  vpMatrix inverseTriangular(bool upper = true) const;
467 
468  vpMatrix pseudoInverse(double svThreshold = 1e-6) const;
469  unsigned int pseudoInverse(vpMatrix &Ap, double svThreshold = 1e-6) const;
470  unsigned int pseudoInverse(vpMatrix &Ap, vpColVector &sv, double svThreshold = 1e-6) const;
471  unsigned int pseudoInverse(vpMatrix &Ap, vpColVector &sv, double svThreshold, vpMatrix &imA, vpMatrix &imAt) const;
472  unsigned int pseudoInverse(vpMatrix &Ap, vpColVector &sv, double svThreshold, vpMatrix &imA, vpMatrix &imAt, vpMatrix &kerAt) const;
473  vpMatrix pseudoInverse(int rank_in) const;
474  int pseudoInverse(vpMatrix &Ap, int rank_in) const;
475  int pseudoInverse(vpMatrix &Ap, vpColVector &sv, int rank_in) const;
476  int pseudoInverse(vpMatrix &Ap, vpColVector &sv, int rank_in, vpMatrix &imA, vpMatrix &imAt) const;
477  int pseudoInverse(vpMatrix &Ap, vpColVector &sv, int rank_in, vpMatrix &imA, vpMatrix &imAt, vpMatrix &kerAt) const;
478 
479 #if defined(VISP_HAVE_LAPACK)
480  vpMatrix pseudoInverseLapack(double svThreshold = 1e-6) const;
481  unsigned int pseudoInverseLapack(vpMatrix &Ap, double svThreshold = 1e-6) const;
482  unsigned int pseudoInverseLapack(vpMatrix &Ap, vpColVector &sv, double svThreshold = 1e-6) const;
483  unsigned int pseudoInverseLapack(vpMatrix &Ap, vpColVector &sv, double svThreshold, vpMatrix &imA, vpMatrix &imAt, vpMatrix &kerAt) const;
484  vpMatrix pseudoInverseLapack(int rank_in) const;
485  int pseudoInverseLapack(vpMatrix &Ap, int rank_in) const;
486  int pseudoInverseLapack(vpMatrix &Ap, vpColVector &sv, int rank_in) const;
487  int pseudoInverseLapack(vpMatrix &Ap, vpColVector &sv, int rank_in, vpMatrix &imA, vpMatrix &imAt, vpMatrix &kerAt) const;
488 #endif
489 #if defined(VISP_HAVE_EIGEN3)
490  vpMatrix pseudoInverseEigen3(double svThreshold = 1e-6) const;
491  unsigned int pseudoInverseEigen3(vpMatrix &Ap, double svThreshold = 1e-6) const;
492  unsigned int pseudoInverseEigen3(vpMatrix &Ap, vpColVector &sv, double svThreshold = 1e-6) const;
493  unsigned int pseudoInverseEigen3(vpMatrix &Ap, vpColVector &sv, double svThreshold, vpMatrix &imA, vpMatrix &imAt, vpMatrix &kerAt) const;
494  vpMatrix pseudoInverseEigen3(int rank_in) const;
495  int pseudoInverseEigen3(vpMatrix &Ap, int rank_in) const;
496  int pseudoInverseEigen3(vpMatrix &Ap, vpColVector &sv, int rank_in) const;
497  int pseudoInverseEigen3(vpMatrix &Ap, vpColVector &sv, int rank_in, vpMatrix &imA, vpMatrix &imAt, vpMatrix &kerAt) const;
498 #endif
499 #if defined(VISP_HAVE_OPENCV)
500  vpMatrix pseudoInverseOpenCV(double svThreshold = 1e-6) const;
501  unsigned int pseudoInverseOpenCV(vpMatrix &Ap, double svThreshold = 1e-6) const;
502  unsigned int pseudoInverseOpenCV(vpMatrix &Ap, vpColVector &sv, double svThreshold = 1e-6) const;
503  unsigned int pseudoInverseOpenCV(vpMatrix &Ap, vpColVector &sv, double svThreshold, vpMatrix &imA, vpMatrix &imAt, vpMatrix &kerAt) const;
504  vpMatrix pseudoInverseOpenCV(int rank_in) const;
505  int pseudoInverseOpenCV(vpMatrix &Ap, int rank_in) const;
506  int pseudoInverseOpenCV(vpMatrix &Ap, vpColVector &sv, int rank_in) const;
507  int pseudoInverseOpenCV(vpMatrix &Ap, vpColVector &sv, int rank_in, vpMatrix &imA, vpMatrix &imAt, vpMatrix &kerAt) const;
508 #endif
510 
511  //-------------------------------------------------
512  // SVD decomposition
513  //-------------------------------------------------
514 
517  double cond(double svThreshold = 1e-6) const;
518  unsigned int kernel(vpMatrix &kerAt, double svThreshold = 1e-6) const;
519  unsigned int nullSpace(vpMatrix &kerA, double svThreshold = 1e-6) const;
520  unsigned int nullSpace(vpMatrix &kerA, int dim) const;
521 
522  // solve Ax=B using the SVD decomposition (usage A = solveBySVD(B,x) )
523  void solveBySVD(const vpColVector &B, vpColVector &x) const;
524  // solve Ax=B using the SVD decomposition (usage x=A.solveBySVD(B))
525  vpColVector solveBySVD(const vpColVector &B) const;
526 
527  // singular value decomposition SVD
528  void svd(vpColVector &w, vpMatrix &V);
529 #ifdef VISP_HAVE_EIGEN3
530  void svdEigen3(vpColVector &w, vpMatrix &V);
531 #endif
532 #if defined(VISP_HAVE_LAPACK)
533  void svdLapack(vpColVector &w, vpMatrix &V);
534 #endif
535 #if defined(VISP_HAVE_OPENCV) // Require opencv >= 2.1.1
536  void svdOpenCV(vpColVector &w, vpMatrix &V);
537 #endif
539 
540  //-------------------------------------------------
541  // QR decomposition
542  //-------------------------------------------------
543 
546  unsigned int qr(vpMatrix &Q, vpMatrix &R, bool full = false, bool squareR = false, double tol = 1e-6) const;
547  unsigned int qrPivot(vpMatrix &Q, vpMatrix &R, vpMatrix &P, bool full = false, bool squareR = false,
548  double tol = 1e-6) const;
549  void solveByQR(const vpColVector &b, vpColVector &x) const;
550  vpColVector solveByQR(const vpColVector &b) const;
552 
553  //-------------------------------------------------
554  // Eigen values and vectors
555  //-------------------------------------------------
559  // Compute the eigen values using Lapack.
560  vpColVector eigenValues() const;
561  void eigenValues(vpColVector &evalue, vpMatrix &evector) const;
563 
564  //-------------------------------------------------
565  // Norms
566  //-------------------------------------------------
569  double frobeniusNorm() const;
570  double inducedL2Norm() const;
571  double infinityNorm() const;
573 
574  //---------------------------------
575  // Printing
576  //---------------------------------
579  std::ostream &cppPrint(std::ostream &os, const std::string &matrixName = "A", bool octet = false) const;
580  std::ostream &csvPrint(std::ostream &os) const;
581  std::ostream &maplePrint(std::ostream &os) const;
582  std::ostream &matlabPrint(std::ostream &os) const;
583  int print(std::ostream &s, unsigned int length, const std::string &intro = "") const;
584  void printSize() const { std::cout << getRows() << " x " << getCols() << " "; }
586 
587  //------------------------------------------------------------------
588  // Static functionalities
589  //------------------------------------------------------------------
590 
591  //---------------------------------
592  // Setting a diagonal matrix with Static Public Member Functions
593  //---------------------------------
596  // Create a diagonal matrix with the element of a vector DAii = Ai
597  static void createDiagonalMatrix(const vpColVector &A, vpMatrix &DA);
599 
600  //---------------------------------
601  // Matrix insertion with Static Public Member Functions
602  //---------------------------------
605  // Insert matrix B in matrix A at the given position (r, c).
606  static vpMatrix insert(const vpMatrix &A, const vpMatrix &B, unsigned int r, unsigned int c);
607  // Insert matrix B in matrix A (not modified) at the given position (r, c),
608  // the result is given in matrix C.
609  static void insert(const vpMatrix &A, const vpMatrix &B, vpMatrix &C, unsigned int r, unsigned int c);
610 
611  //---------------------------------
612  // Stacking with Static Public Member Functions
613  //---------------------------------
616  // Juxtapose to matrices C = [ A B ]
617  static vpMatrix juxtaposeMatrices(const vpMatrix &A, const vpMatrix &B);
618  // Juxtapose to matrices C = [ A B ]
619  static void juxtaposeMatrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C);
620  // Stack two matrices C = [ A B ]^T
621  static vpMatrix stack(const vpMatrix &A, const vpMatrix &B);
622  static vpMatrix stack(const vpMatrix &A, const vpRowVector &r);
623  static vpMatrix stack(const vpMatrix &A, const vpColVector &c);
624 
625  // Stack two matrices C = [ A B ]^T
626  static void stack(const vpMatrix &A, const vpMatrix &B, vpMatrix &C);
627  static void stack(const vpMatrix &A, const vpRowVector &r, vpMatrix &C);
628  static void stack(const vpMatrix &A, const vpColVector &c, vpMatrix &C);
630 
631  //---------------------------------
632  // Matrix operations Static Public Member Functions
633  //---------------------------------
636  static void add2Matrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C);
637  static void add2Matrices(const vpColVector &A, const vpColVector &B, vpColVector &C);
638  static void add2WeightedMatrices(const vpMatrix &A, const double &wA, const vpMatrix &B, const double &wB,
639  vpMatrix &C);
640  static void computeHLM(const vpMatrix &H, const double &alpha, vpMatrix &HLM);
641  static void mult2Matrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C);
642  static void mult2Matrices(const vpMatrix &A, const vpMatrix &B, vpRotationMatrix &C);
643  static void mult2Matrices(const vpMatrix &A, const vpMatrix &B, vpHomogeneousMatrix &C);
644  static void mult2Matrices(const vpMatrix &A, const vpColVector &B, vpColVector &C);
645  static void multMatrixVector(const vpMatrix &A, const vpColVector &v, vpColVector &w);
646  static void negateMatrix(const vpMatrix &A, vpMatrix &C);
647  static void sub2Matrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C);
648  static void sub2Matrices(const vpColVector &A, const vpColVector &B, vpColVector &C);
650 
651  //---------------------------------
652  // Kronecker product Static Public Member Functions
653  //---------------------------------
656  // Compute Kronecker product matrix
657  static void kron(const vpMatrix &m1, const vpMatrix &m2, vpMatrix &out);
658 
659  // Compute Kronecker product matrix
660  static vpMatrix kron(const vpMatrix &m1, const vpMatrix &m2);
662 
663  //---------------------------------
664  // Covariance computation Static Public Member Functions
665  //---------------------------------
668  static vpMatrix computeCovarianceMatrix(const vpMatrix &A, const vpColVector &x, const vpColVector &b);
669  static vpMatrix computeCovarianceMatrix(const vpMatrix &A, const vpColVector &x, const vpColVector &b,
670  const vpMatrix &w);
671  static vpMatrix computeCovarianceMatrixVVS(const vpHomogeneousMatrix &cMo, const vpColVector &deltaS,
672  const vpMatrix &Ls, const vpMatrix &W);
673  static vpMatrix computeCovarianceMatrixVVS(const vpHomogeneousMatrix &cMo, const vpColVector &deltaS,
674  const vpMatrix &Ls);
676 
677  //---------------------------------
678  // Matrix I/O Static Public Member Functions
679  //---------------------------------
748  static inline bool loadMatrix(const std::string &filename, vpArray2D<double> &M, bool binary = false,
749  char *header = nullptr)
750  {
751  return vpArray2D<double>::load(filename, M, binary, header);
752  }
753 
823  static inline bool loadMatrixYAML(const std::string &filename, vpArray2D<double> &M, char *header = nullptr)
824  {
825  return vpArray2D<double>::loadYAML(filename, M, header);
826  }
827 
896  static inline bool saveMatrix(const std::string &filename, const vpArray2D<double> &M, bool binary = false,
897  const char *header = "")
898  {
899  return vpArray2D<double>::save(filename, M, binary, header);
900  }
901 
972  static inline bool saveMatrixYAML(const std::string &filename, const vpArray2D<double> &M, const char *header = "")
973  {
974  return vpArray2D<double>::saveYAML(filename, M, header);
975  }
977 
978 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
979  vp_deprecated double euclideanNorm() const;
980 
989  vp_deprecated void init() { }
990 
994  vp_deprecated void stackMatrices(const vpMatrix &A) { stack(A); }
999  vp_deprecated static vpMatrix stackMatrices(const vpMatrix &A, const vpMatrix &B) { return stack(A, B); }
1004  vp_deprecated static void stackMatrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C) { stack(A, B, C); }
1009  vp_deprecated static vpMatrix stackMatrices(const vpMatrix &A, const vpRowVector &B);
1014  vp_deprecated static void stackMatrices(const vpMatrix &A, const vpRowVector &B, vpMatrix &C);
1019  vp_deprecated static vpMatrix stackMatrices(const vpColVector &A, const vpColVector &B);
1024  vp_deprecated static void stackMatrices(const vpColVector &A, const vpColVector &B, vpColVector &C);
1025 
1029  vp_deprecated void setIdentity(const double &val = 1.0);
1030 
1031  vp_deprecated vpRowVector row(unsigned int i);
1032  vp_deprecated vpColVector column(unsigned int j);
1033 
1034  // Deprecated functions using GSL
1035 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1039  vp_deprecated double detByLUGsl() const
1040  {
1041 #if defined(VISP_HAVE_LAPACK)
1042  return detByLULapack();
1043 #else
1044  throw(vpException(vpException::fatalError, "Undefined detByLULapack(). Install Lapack 3rd party"));
1045 #endif
1046  }
1047 
1051  vp_deprecated vpMatrix inverseByLUGsl() const
1052  {
1053 #if defined(VISP_HAVE_LAPACK)
1054  return inverseByLULapack();
1055 #else
1056  throw(vpException(vpException::fatalError, "Undefined inverseByLULapack(). Install Lapack 3rd party"));
1057 #endif
1058  }
1059 
1063  vp_deprecated vpMatrix inverseByCholeskyGsl() const
1064  {
1065 #if defined(VISP_HAVE_LAPACK)
1066  return inverseByCholeskyLapack();
1067 #else
1068  throw(vpException(vpException::fatalError, "Undefined inverseByCholeskyLapack(). Install Lapack 3rd party"));
1069 #endif
1070  }
1071 
1075  vp_deprecated vpMatrix inverseByQRGsl() const
1076  {
1077 #if defined(VISP_HAVE_LAPACK)
1078  return inverseByQRLapack();
1079 #else
1080  throw(vpException(vpException::fatalError, "Undefined inverseByQRLapack(). Install Lapack 3rd party"));
1081 #endif
1082  }
1083 
1087  vp_deprecated vpMatrix pseudoInverseGsl(double svThreshold = 1e-6) const
1088  {
1089 #if defined(VISP_HAVE_LAPACK)
1090  return pseudoInverseLapack(svThreshold);
1091 #else
1092  (void)svThreshold;
1093  throw(vpException(vpException::fatalError, "Undefined pseudoInverseLapack(). Install Lapack 3rd party"));
1094 #endif
1095  }
1096 
1100  vp_deprecated unsigned int pseudoInverseGsl(vpMatrix &Ap, double svThreshold = 1e-6) const
1101  {
1102 #if defined(VISP_HAVE_LAPACK)
1103  return pseudoInverseLapack(Ap, svThreshold);
1104 #else
1105  (void)Ap;
1106  (void)svThreshold;
1107  throw(vpException(vpException::fatalError, "Undefined pseudoInverseLapack(). Install Lapack 3rd party"));
1108 #endif
1109  }
1110 
1114  vp_deprecated unsigned int pseudoInverseGsl(vpMatrix &Ap, vpColVector &sv, double svThreshold = 1e-6) const
1115  {
1116 #if defined(VISP_HAVE_LAPACK)
1117  return pseudoInverseLapack(Ap, sv, svThreshold);
1118 #else
1119  (void)Ap;
1120  (void)sv;
1121  (void)svThreshold;
1122  throw(vpException(vpException::fatalError, "Undefined pseudoInverseLapack(). Install Lapack 3rd party"));
1123 #endif
1124  }
1125 
1129  vp_deprecated unsigned int pseudoInverseGsl(vpMatrix &Ap, vpColVector &sv, double svThreshold, vpMatrix &imA, vpMatrix &imAt,
1130  vpMatrix &kerAt) const
1131  {
1132 #if defined(VISP_HAVE_LAPACK)
1133  return pseudoInverseLapack(Ap, sv, svThreshold, imA, imAt, kerAt);
1134 #else
1135  (void)Ap;
1136  (void)sv;
1137  (void)svThreshold;
1138  (void)imA;
1139  (void)imAt;
1140  (void)kerAt;
1141  throw(vpException(vpException::fatalError, "Undefined pseudoInverseLapack(). Install Lapack 3rd party"));
1142 #endif
1143  }
1144 
1148  vp_deprecated void svdGsl(vpColVector &w, vpMatrix &V)
1149  {
1150 #if defined(VISP_HAVE_LAPACK)
1151  svdLapack(w, V);
1152 #else
1153  (void)w;
1154  (void)V;
1155  throw(vpException(vpException::fatalError, "Undefined svdLapack(). Install Lapack 3rd party"));
1156 #endif
1157  }
1158 
1159 #endif // ifndef DOXYGEN_SHOULD_SKIP_THIS
1161 #endif
1162 
1163 private:
1164  static unsigned int m_lapack_min_size;
1165  static const unsigned int m_lapack_min_size_default;
1166 
1167 #if defined(VISP_HAVE_LAPACK)
1168  static void blas_dgemm(char trans_a, char trans_b, unsigned int M_, unsigned int N_, unsigned int K_, double alpha,
1169  double *a_data, unsigned int lda_, double *b_data, unsigned int ldb_, double beta,
1170  double *c_data, unsigned int ldc_);
1171  static void blas_dgemv(char trans, unsigned int M_, unsigned int N_, double alpha, double *a_data, unsigned int lda_,
1172  double *x_data, int incx_, double beta, double *y_data, int incy_);
1173  static void blas_dsyev(char jobz, char uplo, unsigned int n_, double *a_data, unsigned int lda_, double *w_data,
1174  double *work_data, int lwork_, int &info_);
1175 #endif
1176 
1177  static void computeCovarianceMatrixVVS(const vpHomogeneousMatrix &cMo, const vpColVector &deltaS, const vpMatrix &Ls,
1178  vpMatrix &Js, vpColVector &deltaP);
1179 };
1180 
1182 #if defined(VISP_USE_MSVC) && defined(visp_EXPORTS)
1183 const __declspec(selectany) unsigned int vpMatrix::m_lapack_min_size_default = 0;
1184 __declspec(selectany) unsigned int vpMatrix::m_lapack_min_size = vpMatrix::m_lapack_min_size_default;
1185 #endif
1186 
1187 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1188 VISP_EXPORT
1189 #endif
1190 vpMatrix operator*(const double &x, const vpMatrix &A);
1191 #endif
Implementation of a generic 2D array used as base class for matrices and vectors.
Definition: vpArray2D.h:126
unsigned int getCols() const
Definition: vpArray2D.h:327
static bool loadYAML(const std::string &filename, vpArray2D< Type > &A, char *header=nullptr)
Definition: vpArray2D.h:767
void insert(const vpArray2D< Type > &A, unsigned int r, unsigned int c)
Definition: vpArray2D.h:484
static bool saveYAML(const std::string &filename, const vpArray2D< Type > &A, const char *header="")
Definition: vpArray2D.h:950
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:600
static bool load(const std::string &filename, vpArray2D< Type > &A, bool binary=false, char *header=nullptr)
Definition: vpArray2D.h:653
vpArray2D< Type > t() const
Compute the transpose of the array.
Definition: vpArray2D.h:1132
unsigned int getRows() const
Definition: vpArray2D.h:337
vpArray2D< Type > & operator=(Type x)
Set all the elements of the array to x.
Definition: vpArray2D.h:512
vpArray2D< Type > hadamard(const vpArray2D< Type > &m) const
Definition: vpArray2D.h:1116
static bool save(const std::string &filename, const vpArray2D< Type > &A, bool binary=false, const char *header="")
Definition: vpArray2D.h:855
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
vpColVector operator*(const double &x, const vpColVector &v)
error that can be emitted by ViSP classes.
Definition: vpException.h:59
@ fatalError
Fatal error.
Definition: vpException.h:84
Implementation of an homogeneous matrix and operations on such kind of matrices.
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:146
static void setLapackMatrixMinSize(unsigned int min_size)
Definition: vpMatrix.h:250
int pseudoInverseOpenCV(vpMatrix &Ap, int rank_in) const
vpMatrix(unsigned int r, unsigned int c)
Definition: vpMatrix.h:170
vpMatrix(unsigned int r, unsigned int c, double val)
Definition: vpMatrix.h:179
vpMatrix pseudoInverseOpenCV(double svThreshold=1e-6) const
int pseudoInverseLapack(vpMatrix &Ap, int rank_in) const
unsigned int pseudoInverseLapack(vpMatrix &Ap, double svThreshold=1e-6) const
static unsigned int getLapackMatrixMinSize()
Definition: vpMatrix.h:236
vpMatrix pseudoInverseEigen3(double svThreshold=1e-6) const
unsigned int pseudoInverseOpenCV(vpMatrix &Ap, double svThreshold=1e-6) const
void printSize() const
Definition: vpMatrix.h:584
unsigned int pseudoInverseEigen3(vpMatrix &Ap, vpColVector &sv, double svThreshold, vpMatrix &imA, vpMatrix &imAt, vpMatrix &kerAt) const
int pseudoInverseLapack(vpMatrix &Ap, vpColVector &sv, int rank_in) const
static bool loadMatrix(const std::string &filename, vpArray2D< double > &M, bool binary=false, char *header=nullptr)
Definition: vpMatrix.h:748
int pseudoInverseEigen3(vpMatrix &Ap, vpColVector &sv, int rank_in, vpMatrix &imA, vpMatrix &imAt, vpMatrix &kerAt) const
int pseudoInverseLapack(vpMatrix &Ap, vpColVector &sv, int rank_in, vpMatrix &imA, vpMatrix &imAt, vpMatrix &kerAt) const
vpMatrix pseudoInverseOpenCV(int rank_in) const
vp_deprecated void stackMatrices(const vpMatrix &A)
Definition: vpMatrix.h:994
unsigned int pseudoInverseEigen3(vpMatrix &Ap, vpColVector &sv, double svThreshold=1e-6) const
int pseudoInverseEigen3(vpMatrix &Ap, vpColVector &sv, int rank_in) const
vpMatrix(const vpArray2D< double > &A)
Definition: vpMatrix.h:194
unsigned int pseudoInverseEigen3(vpMatrix &Ap, double svThreshold=1e-6) const
static vp_deprecated vpMatrix stackMatrices(const vpMatrix &A, const vpMatrix &B)
Definition: vpMatrix.h:999
vpMatrix()
Definition: vpMatrix.h:162
unsigned int pseudoInverseLapack(vpMatrix &Ap, vpColVector &sv, double svThreshold=1e-6) const
vpMatrix pseudoInverseLapack(int rank_in) const
unsigned int pseudoInverseLapack(vpMatrix &Ap, vpColVector &sv, double svThreshold, vpMatrix &imA, vpMatrix &imAt, vpMatrix &kerAt) const
vpMatrix pseudoInverseEigen3(int rank_in) const
int pseudoInverseOpenCV(vpMatrix &Ap, vpColVector &sv, int rank_in, vpMatrix &imA, vpMatrix &imAt, vpMatrix &kerAt) const
int pseudoInverseOpenCV(vpMatrix &Ap, vpColVector &sv, int rank_in) const
vpMatrix pseudoInverseLapack(double svThreshold=1e-6) const
vpMatrix(const vpMatrix &A)
Definition: vpMatrix.h:196
vp_deprecated void init()
Definition: vpMatrix.h:989
void clear()
Definition: vpMatrix.h:209
int pseudoInverseEigen3(vpMatrix &Ap, int rank_in) const
static bool saveMatrixYAML(const std::string &filename, const vpArray2D< double > &M, const char *header="")
Definition: vpMatrix.h:972
static vp_deprecated void stackMatrices(const vpMatrix &A, const vpMatrix &B, vpMatrix &C)
Definition: vpMatrix.h:1004
unsigned int pseudoInverseOpenCV(vpMatrix &Ap, vpColVector &sv, double svThreshold=1e-6) const
static bool loadMatrixYAML(const std::string &filename, vpArray2D< double > &M, char *header=nullptr)
Definition: vpMatrix.h:823
static bool saveMatrix(const std::string &filename, const vpArray2D< double > &M, bool binary=false, const char *header="")
Definition: vpMatrix.h:896
unsigned int pseudoInverseOpenCV(vpMatrix &Ap, vpColVector &sv, double svThreshold, vpMatrix &imA, vpMatrix &imAt, vpMatrix &kerAt) const
Implementation of a rotation matrix and operations on such kind of matrices.
Implementation of row vector and the associated operations.
Definition: vpRowVector.h:107
Class that consider the case of a translation vector.
vpMatrix operator*(const double &x, const vpMatrix &A)