34 #include <visp3/core/vpColVector.h>
35 #include <visp3/core/vpConfig.h>
36 #include <visp3/core/vpException.h>
37 #include <visp3/core/vpMath.h>
38 #include <visp3/core/vpMatrix.h>
39 #include <visp3/core/vpMatrixException.h>
44 #ifdef VISP_HAVE_EIGEN3
48 #if defined(VISP_HAVE_OPENCV)
49 #include <opencv2/core/core.hpp>
52 #ifdef VISP_HAVE_LAPACK
54 #include <gsl/gsl_linalg.h>
58 typedef MKL_INT integer;
60 #if defined(VISP_HAVE_LAPACK_BUILT_IN)
61 typedef long int integer;
65 extern "C" int dgesdd_(
char *jobz, integer *m, integer *n,
double *a, integer *lda,
double *s,
double *u, integer *ldu,
66 double *vt, integer *ldvt,
double *work, integer *lwork, integer *iwork, integer *info);
261 #if defined(VISP_HAVE_LAPACK)
263 #elif defined(VISP_HAVE_EIGEN3)
265 #elif defined(VISP_HAVE_OPENCV)
274 #if defined(VISP_HAVE_OPENCV)
347 int rows = (int)this->
getRows();
348 int cols = (int)this->
getCols();
349 cv::Mat m(rows, cols, CV_64F, this->
data);
350 cv::SVD opencvSVD(m);
351 cv::Mat opencvV = opencvSVD.vt;
352 cv::Mat opencvW = opencvSVD.w;
353 V.
resize((
unsigned int)opencvV.rows, (
unsigned int)opencvV.cols);
354 w.
resize((
unsigned int)(opencvW.rows * opencvW.cols));
356 memcpy(V.
data, opencvV.data, (
size_t)(8 * opencvV.rows * opencvV.cols));
358 memcpy(w.
data, opencvW.data, (
size_t)(8 * opencvW.rows * opencvW.cols));
359 this->
resize((
unsigned int)opencvSVD.u.rows, (
unsigned int)opencvSVD.u.cols);
360 memcpy(this->
data, opencvSVD.u.data, (
size_t)(8 * opencvSVD.u.rows * opencvSVD.u.cols));
365 #if defined(VISP_HAVE_LAPACK)
458 gsl_vector *work = gsl_vector_alloc(nc);
488 gsl_linalg_SV_decomp(&A, &V_, &S, work);
495 gsl_vector_free(work);
516 double *a =
new double[
static_cast<unsigned int>(nr * nc)];
518 memcpy(a, U.
data, this->getRows() * this->getCols() *
sizeof(
double));
524 integer m = (integer)(nc);
525 integer n = (integer)(nr);
528 integer ldvt = std::min<integer>(nr, nc);
534 integer *iwork =
new integer[8 *
static_cast<integer
>(std::min<integer>(nr, nc))];
547 dgesdd_((
char *)
"S", &m, &n, a, &lda, s, u, &ldu, vt, &ldvt, &wkopt, &lwork, iwork, &info);
549 work =
new double[
static_cast<unsigned int>(lwork)];
551 dgesdd_((
char *)
"S", &m, &n, a, &lda, s, u, &ldu, vt, &ldvt, work, &lwork, iwork, &info);
572 #ifdef VISP_HAVE_EIGEN3
654 Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> > M(this->
data, this->
getRows(),
656 Eigen::JacobiSVD<Eigen::MatrixXd>
svd(M, Eigen::ComputeThinU | Eigen::ComputeThinV);
658 Eigen::Map<Eigen::VectorXd> w_(w.
data, w.
size());
666 Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> > V_(V.
data, V.
getRows(), V.
getCols());
667 Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> > U_(this->
data,
rowNum,
colNum);
668 w_ =
svd.singularValues();
unsigned int getCols() const
double * data
Address of the first element of the data array.
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
unsigned int rowNum
Number of rows in the array.
unsigned int size() const
Return the number of elements of the 2D array.
unsigned int getRows() const
unsigned int colNum
Number of columns in the array.
Implementation of column vector and the associated operations.
void resize(unsigned int i, bool flagNullify=true)
error that can be emitted by ViSP classes.
error that can be emitted by the vpMatrix class and its derivatives
Implementation of a matrix and operations on matrices.
void svdLapack(vpColVector &w, vpMatrix &V)
void solveBySVD(const vpColVector &B, vpColVector &x) const
void svd(vpColVector &w, vpMatrix &V)
void svdOpenCV(vpColVector &w, vpMatrix &V)
vpMatrix pseudoInverse(double svThreshold=1e-6) const
void svdEigen3(vpColVector &w, vpMatrix &V)
vpMatrix transpose() const