Visual Servoing Platform  version 3.6.1 under development (2025-02-01)
vpMatrix_pseudo_inverse_eigen.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 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  * Pseudo inverse computation.
32  */
33 
34 #include <visp3/core/vpConfig.h>
35 
36 #include "private/vpMatrix_pseudo_inverse.h"
37 
38 BEGIN_VISP_NAMESPACE
39 
40 #ifndef DOXYGEN_SHOULD_SKIP_THIS
41 
42 #if defined(VISP_HAVE_EIGEN3)
83 vpMatrix vpMatrix::pseudoInverseEigen3(double svThreshold) const
84 {
85  unsigned int nrows = getRows();
86  unsigned int ncols = getCols();
87  int rank_out;
88  vpMatrix U, V, Ap;
89  vpColVector sv;
90 
91  U = *this;
92  U.svdEigen3(sv, V);
93 
94  compute_pseudo_inverse(U, sv, V, nrows, ncols, svThreshold, Ap, rank_out, nullptr, nullptr, nullptr, nullptr);
95 
96  return Ap;
97 }
98 
143 unsigned int vpMatrix::pseudoInverseEigen3(vpMatrix &Ap, double svThreshold) const
144 {
145  unsigned int nrows = getRows();
146  unsigned int ncols = getCols();
147  int rank_out;
148  vpMatrix U, V;
149  vpColVector sv;
150 
151  U = *this;
152  U.svdEigen3(sv, V);
153 
154  compute_pseudo_inverse(U, sv, V, nrows, ncols, svThreshold, Ap, rank_out, nullptr, nullptr, nullptr, nullptr);
155 
156  return static_cast<unsigned int>(rank_out);
157 }
158 
210 unsigned int vpMatrix::pseudoInverseEigen3(vpMatrix &Ap, vpColVector &sv, double svThreshold) const
211 {
212  unsigned int nrows = getRows();
213  unsigned int ncols = getCols();
214  int rank_out;
215  vpMatrix U, V;
216 
217  Ap.resize(ncols, nrows, false, false);
218 
219  U = *this;
220  U.svdEigen3(sv, V);
221 
222  compute_pseudo_inverse(U, sv, V, nrows, ncols, svThreshold, Ap, rank_out, nullptr, nullptr, nullptr, nullptr);
223 
224  return static_cast<unsigned int>(rank_out);
225 }
226 
337 unsigned int vpMatrix::pseudoInverseEigen3(vpMatrix &Ap, vpColVector &sv, double svThreshold, vpMatrix &imA,
338  vpMatrix &imAt, vpMatrix &kerAt) const
339 {
340  unsigned int nrows = getRows();
341  unsigned int ncols = getCols();
342  int rank_out;
343  vpMatrix U, V;
344 
345  if (nrows < ncols) {
346  U.resize(ncols, ncols, true);
347  U.insert(*this, 0, 0);
348  }
349  else {
350  U = *this;
351  }
352 
353  U.svdEigen3(sv, V);
354 
355  compute_pseudo_inverse(U, sv, V, nrows, ncols, svThreshold, Ap, rank_out, nullptr, &imA, &imAt, &kerAt);
356 
357  return static_cast<unsigned int>(rank_out);
358 }
359 
400 vpMatrix vpMatrix::pseudoInverseEigen3(int rank_in) const
401 {
402  unsigned int nrows = getRows();
403  unsigned int ncols = getCols();
404  int rank_out;
405  double svThreshold = 1e-26;
406  vpMatrix U, V, Ap;
407  vpColVector sv;
408 
409  U = *this;
410  U.svdEigen3(sv, V);
411 
412  compute_pseudo_inverse(U, sv, V, nrows, ncols, svThreshold, Ap, rank_out, &rank_in, nullptr, nullptr, nullptr);
413 
414  return Ap;
415 }
416 
467 int vpMatrix::pseudoInverseEigen3(vpMatrix &Ap, int rank_in) const
468 {
469  unsigned int nrows = getRows();
470  unsigned int ncols = getCols();
471  int rank_out;
472  double svThreshold = 1e-26;
473  vpMatrix U, V;
474  vpColVector sv;
475 
476  Ap.resize(ncols, nrows, false, false);
477 
478  U = *this;
479  U.svdEigen3(sv, V);
480 
481  compute_pseudo_inverse(U, sv, V, nrows, ncols, svThreshold, Ap, rank_out, &rank_in, nullptr, nullptr, nullptr);
482 
483  return rank_out;
484 }
485 
543 int vpMatrix::pseudoInverseEigen3(vpMatrix &Ap, vpColVector &sv, int rank_in) const
544 {
545  unsigned int nrows = getRows();
546  unsigned int ncols = getCols();
547  int rank_out;
548  double svThreshold = 1e-26;
549  vpMatrix U, V;
550 
551  Ap.resize(ncols, nrows, false, false);
552 
553  U = *this;
554  U.svdEigen3(sv, V);
555 
556  compute_pseudo_inverse(U, sv, V, nrows, ncols, svThreshold, Ap, rank_out, &rank_in, nullptr, nullptr, nullptr);
557 
558  return rank_out;
559 }
560 
676 int vpMatrix::pseudoInverseEigen3(vpMatrix &Ap, vpColVector &sv, int rank_in, vpMatrix &imA, vpMatrix &imAt,
677  vpMatrix &kerAt) const
678 {
679  unsigned int nrows = getRows();
680  unsigned int ncols = getCols();
681  int rank_out;
682  double svThreshold = 1e-26;
683  vpMatrix U, V;
684 
685  if (nrows < ncols) {
686  U.resize(ncols, ncols, true);
687  U.insert(*this, 0, 0);
688  }
689  else {
690  U = *this;
691  }
692  U.svdEigen3(sv, V);
693 
694  compute_pseudo_inverse(U, sv, V, nrows, ncols, svThreshold, Ap, rank_out, &rank_in, &imA, &imAt, &kerAt);
695 
696  return rank_out;
697 }
698 #endif
699 
700 #endif // #ifndef DOXYGEN_SHOULD_SKIP_THIS
701 
702 END_VISP_NAMESPACE
unsigned int getCols() const
Definition: vpArray2D.h:337
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition: vpArray2D.h:362
unsigned int getRows() const
Definition: vpArray2D.h:347
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:169
vpMatrix pseudoInverseEigen3(double svThreshold=1e-6) const
void insert(const vpMatrix &A, unsigned int r, unsigned int c)
Definition: vpMatrix.cpp:1133
void svdEigen3(vpColVector &w, vpMatrix &V)