Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpMatrix_pseudo_inverse_lapack.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#ifdef VISP_HAVE_LAPACK
39#ifdef VISP_HAVE_GSL
40#include <gsl/gsl_eigen.h>
41#include <gsl/gsl_linalg.h>
42#include <gsl/gsl_math.h>
43#elif defined(VISP_HAVE_MKL)
44#include <mkl.h>
45#endif
46#endif
47
49
50#ifndef DOXYGEN_SHOULD_SKIP_THIS
51
52#if defined(VISP_HAVE_LAPACK)
93vpMatrix vpMatrix::pseudoInverseLapack(double svThreshold) const
94{
95 unsigned int nrows = getRows();
96 unsigned int ncols = getCols();
97 int rank_out;
98
99 vpMatrix U, V, Ap;
100 vpColVector sv;
101
102 U = *this;
103 U.svdLapack(sv, V);
104
105 compute_pseudo_inverse(U, sv, V, nrows, ncols, svThreshold, Ap, rank_out, nullptr, nullptr, nullptr, nullptr);
106
107 return Ap;
108}
109
154unsigned int vpMatrix::pseudoInverseLapack(vpMatrix &Ap, double svThreshold) const
155{
156 unsigned int nrows = getRows();
157 unsigned int ncols = getCols();
158 int rank_out;
159
160 vpMatrix U, V;
161 vpColVector sv;
162
163 U = *this;
164 U.svdLapack(sv, V);
165
166 compute_pseudo_inverse(U, sv, V, nrows, ncols, svThreshold, Ap, rank_out, nullptr, nullptr, nullptr, nullptr);
167
168 return static_cast<unsigned int>(rank_out);
169}
170
222unsigned int vpMatrix::pseudoInverseLapack(vpMatrix &Ap, vpColVector &sv, double svThreshold) const
223{
224 unsigned int nrows = getRows();
225 unsigned int ncols = getCols();
226 int rank_out;
227
228 vpMatrix U, V;
229
230 Ap.resize(ncols, nrows, false, false);
231
232 U = *this;
233 U.svdLapack(sv, V);
234
235 compute_pseudo_inverse(U, sv, V, nrows, ncols, svThreshold, Ap, rank_out, nullptr, nullptr, nullptr, nullptr);
236
237 return static_cast<unsigned int>(rank_out);
238}
239
350unsigned int vpMatrix::pseudoInverseLapack(vpMatrix &Ap, vpColVector &sv, double svThreshold, vpMatrix &imA,
351 vpMatrix &imAt, vpMatrix &kerAt) const
352{
353 unsigned int nrows = getRows();
354 unsigned int ncols = getCols();
355 int rank_out;
356 vpMatrix U, V;
357
358 if (nrows < ncols) {
359 U.resize(ncols, ncols, true);
360 U.insert(*this, 0, 0);
361 }
362 else {
363 U = *this;
364 }
365
366 U.svdLapack(sv, V);
367
368 compute_pseudo_inverse(U, sv, V, nrows, ncols, svThreshold, Ap, rank_out, nullptr, &imA, &imAt, &kerAt);
369
370 return static_cast<unsigned int>(rank_out);
371}
372
414{
415 unsigned int nrows = getRows();
416 unsigned int ncols = getCols();
417 int rank_out;
418 double svThreshold = 1e-26;
419
420 vpMatrix U, V, Ap;
421 vpColVector sv;
422
423 U = *this;
424 U.svdLapack(sv, V);
425
426 compute_pseudo_inverse(U, sv, V, nrows, ncols, svThreshold, Ap, rank_out, &rank_in, nullptr, nullptr, nullptr);
427
428 return Ap;
429}
430
481int vpMatrix::pseudoInverseLapack(vpMatrix &Ap, int rank_in) const
482{
483 unsigned int nrows = getRows();
484 unsigned int ncols = getCols();
485 int rank_out;
486 double svThreshold = 1e-26;
487
488 vpMatrix U, V;
489 vpColVector sv;
490
491 U = *this;
492 U.svdLapack(sv, V);
493
494 compute_pseudo_inverse(U, sv, V, nrows, ncols, svThreshold, Ap, rank_out, &rank_in, nullptr, nullptr, nullptr);
495
496 return rank_out;
497}
498
556int vpMatrix::pseudoInverseLapack(vpMatrix &Ap, vpColVector &sv, int rank_in) const
557{
558 unsigned int nrows = getRows();
559 unsigned int ncols = getCols();
560 int rank_out;
561 double svThreshold = 1e-26;
562 vpMatrix U, V;
563
564 Ap.resize(ncols, nrows, false, false);
565
566 U = *this;
567 U.svdLapack(sv, V);
568
569 compute_pseudo_inverse(U, sv, V, nrows, ncols, svThreshold, Ap, rank_out, &rank_in, nullptr, nullptr, nullptr);
570
571 return rank_out;
572}
573
689int vpMatrix::pseudoInverseLapack(vpMatrix &Ap, vpColVector &sv, int rank_in, vpMatrix &imA, vpMatrix &imAt,
690 vpMatrix &kerAt) const
691{
692 unsigned int nrows = getRows();
693 unsigned int ncols = getCols();
694 int rank_out;
695 double svThreshold = 1e-26;
696 vpMatrix U, V;
697
698 if (nrows < ncols) {
699 U.resize(ncols, ncols, true);
700 U.insert(*this, 0, 0);
701 }
702 else {
703 U = *this;
704 }
705
706 U.svdLapack(sv, V);
707
708 compute_pseudo_inverse(U, sv, V, nrows, ncols, svThreshold, Ap, rank_out, &rank_in, &imA, &imAt, &kerAt);
709
710 return rank_out;
711}
712
713#endif
714
715#endif // #ifndef DOXYGEN_SHOULD_SKIP_THIS
716
717END_VISP_NAMESPACE
unsigned int getCols() const
Definition vpArray2D.h:423
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition vpArray2D.h:448
unsigned int getRows() const
Definition vpArray2D.h:433
Implementation of column vector and the associated operations.
Implementation of a matrix and operations on matrices.
Definition vpMatrix.h:175
void svdLapack(vpColVector &w, vpMatrix &V)
vpMatrix pseudoInverseLapack(double svThreshold=1e-6) const
void insert(const vpMatrix &A, unsigned int r, unsigned int c)