39 #include <visp3/core/vpColVector.h>
40 #include <visp3/core/vpConfig.h>
41 #include <visp3/core/vpDebug.h>
42 #include <visp3/core/vpException.h>
43 #include <visp3/core/vpMath.h>
44 #include <visp3/core/vpMatrix.h>
45 #include <visp3/core/vpMatrixException.h>
51 #ifdef VISP_HAVE_EIGEN3
55 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101)
56 #include <opencv2/core/core.hpp>
59 #ifdef VISP_HAVE_LAPACK
61 #include <gsl/gsl_linalg.h>
65 typedef MKL_INT integer;
67 #if defined(VISP_HAVE_LAPACK_BUILT_IN)
68 typedef long int integer;
72 extern "C" int dgesdd_(
char *jobz, integer *m, integer *n,
double *a, integer *lda,
double *s,
double *u, integer *ldu,
73 double *vt, integer *ldvt,
double *work, integer *lwork, integer *iwork, integer *info);
85 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101)
155 int rows = (int)this->
getRows();
156 int cols = (int)this->
getCols();
157 cv::Mat m(rows, cols, CV_64F, this->
data);
158 cv::SVD opencvSVD(m);
159 cv::Mat opencvV = opencvSVD.vt;
160 cv::Mat opencvW = opencvSVD.w;
161 V.
resize((
unsigned int)opencvV.rows, (
unsigned int)opencvV.cols);
162 w.
resize((
unsigned int)(opencvW.rows * opencvW.cols));
164 memcpy(V.
data, opencvV.data, (
size_t)(8 * opencvV.rows * opencvV.cols));
166 memcpy(w.
data, opencvW.data, (
size_t)(8 * opencvW.rows * opencvW.cols));
167 this->
resize((
unsigned int)opencvSVD.u.rows, (
unsigned int)opencvSVD.u.cols);
168 memcpy(this->
data, opencvSVD.u.data, (
size_t)(8 * opencvSVD.u.rows * opencvSVD.u.cols));
173 #if defined(VISP_HAVE_LAPACK)
261 gsl_vector *work = gsl_vector_alloc(nc);
290 gsl_linalg_SV_decomp(&A, &V_, &S, work);
297 gsl_vector_free(work);
304 integer m = (integer)(this->
getCols());
305 integer n = (integer)(this->
getRows());
308 integer ldvt = (std::min)(m, n);
314 integer *iwork =
new integer[8 *
static_cast<integer
>((std::min)(n, m))];
317 double *a =
new double[
static_cast<unsigned int>(lda * n)];
320 double *vt = this->
data;
323 dgesdd_((
char *)
"S", &m, &n, a, &lda, s, u, &ldu, vt, &ldvt, &wkopt, &lwork, iwork, &info);
325 work =
new double[
static_cast<unsigned int>(lwork)];
327 dgesdd_((
char *)
"S", &m, &n, a, &lda, s, u, &ldu, vt, &ldvt, work, &lwork, iwork, &info);
342 #ifdef VISP_HAVE_EIGEN3
414 Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> > M(this->
data, this->
getRows(),
417 Eigen::JacobiSVD<Eigen::MatrixXd>
svd(M, Eigen::ComputeThinU | Eigen::ComputeThinV);
419 Eigen::Map<Eigen::VectorXd> w_(w.
data, w.
size());
420 Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> > V_(V.
data, V.
getRows(),
422 Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> > U_(this->
data, this->
getRows(),
424 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 emited by the vpMatrix class and its derivates
Implementation of a matrix and operations on matrices.
void svdLapack(vpColVector &w, vpMatrix &V)
void svd(vpColVector &w, vpMatrix &V)
void svdOpenCV(vpColVector &w, vpMatrix &V)
void svdEigen3(vpColVector &w, vpMatrix &V)
vpMatrix transpose() const