Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
vpMatrix_svd.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Matrix SVD decomposition.
33  *
34  * Authors:
35  * Eric Marchand
36  *
37  *****************************************************************************/
38 
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>
46 
47 #include <cmath> // std::fabs
48 #include <iostream>
49 #include <limits> // numeric_limits
50 
51 #ifdef VISP_HAVE_EIGEN3
52 #include <Eigen/SVD>
53 #endif
54 
55 #ifdef VISP_HAVE_GSL
56 #include <gsl/gsl_linalg.h>
57 #endif
58 
59 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101) // Require opencv >= 2.1.1
60 #include <opencv2/core/core.hpp>
61 #endif
62 
63 #ifdef VISP_HAVE_LAPACK
64 #ifdef VISP_HAVE_LAPACK_BUILT_IN
65 typedef long int integer;
66 #else
67 typedef int integer;
68 #endif
69 
70 extern "C" int dgesdd_(char *jobz, integer *m, integer *n, double *a, integer *lda, double *s, double *u, integer *ldu,
71  double *vt, integer *ldvt, double *work, integer *lwork, integer *iwork, integer *info);
72 
73 #include <stdio.h>
74 #include <string.h>
75 #endif
76 /*---------------------------------------------------------------------
77 
78 SVD related functions
79 
80 ---------------------------------------------------------------------*/
81 
82 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101) // Require opencv >= 2.1.1
83 
151 {
152  int rows = (int)this->getRows();
153  int cols = (int)this->getCols();
154  cv::Mat m(rows, cols, CV_64F, this->data);
155  cv::SVD opencvSVD(m);
156  cv::Mat opencvV = opencvSVD.vt;
157  cv::Mat opencvW = opencvSVD.w;
158  V.resize((unsigned int)opencvV.rows, (unsigned int)opencvV.cols);
159  w.resize((unsigned int)(opencvW.rows * opencvW.cols));
160 
161  memcpy(V.data, opencvV.data, (size_t)(8 * opencvV.rows * opencvV.cols));
162  V = V.transpose();
163  memcpy(w.data, opencvW.data, (size_t)(8 * opencvW.rows * opencvW.cols));
164  this->resize((unsigned int)opencvSVD.u.rows, (unsigned int)opencvSVD.u.cols);
165  memcpy(this->data, opencvSVD.u.data, (size_t)(8 * opencvSVD.u.rows * opencvSVD.u.cols));
166 }
167 
168 #endif
169 
170 #ifdef VISP_HAVE_LAPACK
171 
238 {
239  w.resize(this->getCols());
240  V.resize(this->getCols(), this->getCols());
241 
242  integer m = (integer)(this->getCols());
243  integer n = (integer)(this->getRows());
244  integer lda = m;
245  integer ldu = m;
246  integer ldvt = (std::min)(m, n);
247  integer info, lwork;
248 
249  double wkopt;
250  double *work;
251 
252  integer *iwork = new integer[8 * static_cast<integer>((std::min)(n, m))];
253 
254  double *s = w.data;
255  double *a = new double[static_cast<unsigned int>(lda * n)];
256  memcpy(a, this->data, this->getRows() * this->getCols() * sizeof(double));
257  double *u = V.data;
258  double *vt = this->data;
259 
260  lwork = -1;
261  dgesdd_((char *)"S", &m, &n, a, &lda, s, u, &ldu, vt, &ldvt, &wkopt, &lwork, iwork, &info);
262  lwork = (int)wkopt;
263  work = new double[static_cast<unsigned int>(lwork)];
264 
265  dgesdd_((char *)"S", &m, &n, a, &lda, s, u, &ldu, vt, &ldvt, work, &lwork, iwork, &info);
266 
267  if (info > 0) {
268  throw(vpMatrixException(vpMatrixException::fatalError, "The algorithm computing SVD failed to converge."));
269  }
270 
271  V = V.transpose();
272  delete[] work;
273  delete[] iwork;
274  delete[] a;
275 }
276 #endif
277 
278 #ifdef VISP_HAVE_GSL
279 
347 {
348  w.resize(this->getCols());
349  V.resize(this->getCols(), this->getCols());
350 
351  unsigned int nc = getCols();
352  unsigned int nr = getRows();
353  gsl_vector *work = gsl_vector_alloc(nc);
354 
355  gsl_matrix A;
356  A.size1 = nr;
357  A.size2 = nc;
358  A.tda = A.size2;
359  A.data = this->data;
360  A.owner = 0;
361  A.block = 0;
362 
363  gsl_matrix V_;
364  V_.size1 = nc;
365  V_.size2 = nc;
366  V_.tda = V_.size2;
367  V_.data = V.data;
368  V_.owner = 0;
369  V_.block = 0;
370 
371  gsl_vector S;
372  S.size = nc;
373  S.stride = 1;
374  S.data = w.data;
375  S.owner = 0;
376  S.block = 0;
377 
378  gsl_linalg_SV_decomp(&A, &V_, &S, work);
379 
380  gsl_vector_free(work);
381 }
382 #endif // # #GSL
383 
384 #ifdef VISP_HAVE_EIGEN3
385 
452 {
453  w.resize(this->getCols());
454  V.resize(this->getCols(), this->getCols());
455 
456  Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> > M(this->data, this->getRows(),
457  this->getCols());
458 
459  Eigen::JacobiSVD<Eigen::MatrixXd> svd(M, Eigen::ComputeThinU | Eigen::ComputeThinV);
460 
461  Eigen::Map<Eigen::VectorXd> w_(w.data, w.size());
462  Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> > V_(V.data, V.getRows(),
463  V.getCols());
464  Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> > U_(this->data, this->getRows(),
465  this->getCols());
466  w_ = svd.singularValues();
467  V_ = svd.matrixV();
468  U_ = svd.matrixU();
469 }
470 #endif
void svd(vpColVector &w, vpMatrix &V)
Definition: vpMatrix.cpp:1792
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:104
void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify=true, const bool recopy_=true)
Definition: vpArray2D.h:171
double * data
Address of the first element of the data array.
Definition: vpArray2D.h:84
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:158
unsigned int getCols() const
Definition: vpArray2D.h:146
void svdLapack(vpColVector &w, vpMatrix &V)
vpMatrix transpose() const
Definition: vpMatrix.cpp:394
void svdOpenCV(vpColVector &w, vpMatrix &V)
unsigned int getRows() const
Definition: vpArray2D.h:156
void svdGsl(vpColVector &w, vpMatrix &V)
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
error that can be emited by the vpMatrix class and its derivates
void svdEigen3(vpColVector &w, vpMatrix &V)
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:244