Visual Servoing Platform  version 3.1.0
vpMatrix_svd.cpp
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 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 #ifndef DOXYGEN_SHOULD_SKIP_THIS
52 
53 #ifdef VISP_HAVE_EIGEN3
54 #include <Eigen/SVD>
55 #endif
56 
57 #ifdef VISP_HAVE_GSL
58 #include <gsl/gsl_linalg.h>
59 #endif
60 
61 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101) // Require opencv >= 2.1.1
62 #include <opencv2/core/core.hpp>
63 #endif
64 
65 #ifdef VISP_HAVE_LAPACK
66 #ifdef VISP_HAVE_LAPACK_BUILT_IN
67 typedef long int integer;
68 #else
69 typedef int integer;
70 #endif
71 
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);
74 
75 #include <stdio.h>
76 #include <string.h>
77 #endif
78 /*---------------------------------------------------------------------
79 
80 SVD related functions
81 
82 ---------------------------------------------------------------------*/
83 
84 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101) // Require opencv >= 2.1.1
85 
152 void vpMatrix::svdOpenCV(vpColVector &w, vpMatrix &V)
153 {
154  int rows = (int)this->getRows();
155  int cols = (int)this->getCols();
156  cv::Mat m(rows, cols, CV_64F, this->data);
157  cv::SVD opencvSVD(m);
158  cv::Mat opencvV = opencvSVD.vt;
159  cv::Mat opencvW = opencvSVD.w;
160  V.resize((unsigned int)opencvV.rows, (unsigned int)opencvV.cols);
161  w.resize((unsigned int)(opencvW.rows * opencvW.cols));
162 
163  memcpy(V.data, opencvV.data, (size_t)(8 * opencvV.rows * opencvV.cols));
164  V = V.transpose();
165  memcpy(w.data, opencvW.data, (size_t)(8 * opencvW.rows * opencvW.cols));
166  this->resize((unsigned int)opencvSVD.u.rows, (unsigned int)opencvSVD.u.cols);
167  memcpy(this->data, opencvSVD.u.data, (size_t)(8 * opencvSVD.u.rows * opencvSVD.u.cols));
168 }
169 
170 #endif
171 
172 #ifdef VISP_HAVE_LAPACK
173 
239 void vpMatrix::svdLapack(vpColVector &w, vpMatrix &V)
240 {
241  w.resize(this->getCols());
242  V.resize(this->getCols(), this->getCols());
243 
244  integer m = (integer)(this->getCols());
245  integer n = (integer)(this->getRows());
246  integer lda = m;
247  integer ldu = m;
248  integer ldvt = (std::min)(m, n);
249  integer info, lwork;
250 
251  double wkopt;
252  double *work;
253 
254  integer *iwork = new integer[8 * static_cast<integer>((std::min)(n, m))];
255 
256  double *s = w.data;
257  double *a = new double[static_cast<unsigned int>(lda * n)];
258  memcpy(a, this->data, this->getRows() * this->getCols() * sizeof(double));
259  double *u = V.data;
260  double *vt = this->data;
261 
262  lwork = -1;
263  dgesdd_((char *)"S", &m, &n, a, &lda, s, u, &ldu, vt, &ldvt, &wkopt, &lwork, iwork, &info);
264  lwork = (int)wkopt;
265  work = new double[static_cast<unsigned int>(lwork)];
266 
267  dgesdd_((char *)"S", &m, &n, a, &lda, s, u, &ldu, vt, &ldvt, work, &lwork, iwork, &info);
268 
269  if (info > 0) {
270  throw(vpMatrixException(vpMatrixException::fatalError, "The algorithm computing SVD failed to converge."));
271  }
272 
273  V = V.transpose();
274  delete[] work;
275  delete[] iwork;
276  delete[] a;
277 }
278 #endif
279 
280 #ifdef VISP_HAVE_GSL
281 
348 void vpMatrix::svdGsl(vpColVector &w, vpMatrix &V)
349 {
350  w.resize(this->getCols());
351  V.resize(this->getCols(), this->getCols());
352 
353  unsigned int nc = getCols();
354  unsigned int nr = getRows();
355  gsl_vector *work = gsl_vector_alloc(nc);
356 
357  gsl_matrix A;
358  A.size1 = nr;
359  A.size2 = nc;
360  A.tda = A.size2;
361  A.data = this->data;
362  A.owner = 0;
363  A.block = 0;
364 
365  gsl_matrix V_;
366  V_.size1 = nc;
367  V_.size2 = nc;
368  V_.tda = V_.size2;
369  V_.data = V.data;
370  V_.owner = 0;
371  V_.block = 0;
372 
373  gsl_vector S;
374  S.size = nc;
375  S.stride = 1;
376  S.data = w.data;
377  S.owner = 0;
378  S.block = 0;
379 
380  gsl_linalg_SV_decomp(&A, &V_, &S, work);
381 
382  gsl_vector_free(work);
383 }
384 #endif // # #GSL
385 
386 #ifdef VISP_HAVE_EIGEN3
387 
453 void vpMatrix::svdEigen3(vpColVector &w, vpMatrix &V)
454 {
455  w.resize(this->getCols());
456  V.resize(this->getCols(), this->getCols());
457 
458  Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> > M(this->data, this->getRows(),
459  this->getCols());
460 
461  Eigen::JacobiSVD<Eigen::MatrixXd> svd(M, Eigen::ComputeThinU | Eigen::ComputeThinV);
462 
463  Eigen::Map<Eigen::VectorXd> w_(w.data, w.size());
464  Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> > V_(V.data, V.getRows(),
465  V.getCols());
466  Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> > U_(this->data, this->getRows(),
467  this->getCols());
468  w_ = svd.singularValues();
469  V_ = svd.matrixV();
470  U_ = svd.matrixU();
471 }
472 #endif
473 
474 #endif // #ifndef DOXYGEN_SHOULD_SKIP_THIS
void svd(vpColVector &w, vpMatrix &V)
Definition: vpMatrix.cpp:1791
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
unsigned int getRows() const
Definition: vpArray2D.h:156
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
vpMatrix transpose() const
Definition: vpMatrix.cpp:394
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 resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:241