|
| 1 | +// Copyright (c) 2018, Joseph Mirabel |
| 2 | +// Authors: Joseph Mirabel (joseph.mirabel@laas.fr) |
| 3 | +// |
| 4 | +// This file is part of sot-core. |
| 5 | +// sot-core is free software: you can redistribute it |
| 6 | +// and/or modify it under the terms of the GNU Lesser General Public |
| 7 | +// License as published by the Free Software Foundation, either version |
| 8 | +// 3 of the License, or (at your option) any later version. |
| 9 | +// |
| 10 | +// sot-core is distributed in the hope that it will be |
| 11 | +// useful, but WITHOUT ANY WARRANTY; without even the implied warranty |
| 12 | +// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | +// General Lesser Public License for more details. You should have |
| 14 | +// received a copy of the GNU Lesser General Public License along with |
| 15 | +// sot-core. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | + |
| 18 | +#include <sot/core/debug.hh> |
| 19 | +#include <sot/core/matrix-svd.hh> |
| 20 | + |
| 21 | +namespace Eigen { |
| 22 | + |
| 23 | +void pseudoInverse( dg::Matrix& _inputMatrix, |
| 24 | + dg::Matrix& _inverseMatrix, |
| 25 | + const double threshold) { |
| 26 | + JacobiSVD<dg::Matrix> svd(_inputMatrix, ComputeThinU | ComputeThinV); |
| 27 | + JacobiSVD<dg::Matrix>::SingularValuesType m_singularValues=svd.singularValues(); |
| 28 | + JacobiSVD<dg::Matrix>::SingularValuesType singularValues_inv; |
| 29 | + singularValues_inv.resizeLike(m_singularValues); |
| 30 | + for ( long i=0; i<m_singularValues.size(); ++i) { |
| 31 | + if ( m_singularValues(i) > threshold ) |
| 32 | + singularValues_inv(i)=1.0/m_singularValues(i); |
| 33 | + else singularValues_inv(i)=0; |
| 34 | + } |
| 35 | + _inverseMatrix = (svd.matrixV()*singularValues_inv.asDiagonal()*svd.matrixU().transpose()); |
| 36 | +} |
| 37 | + |
| 38 | +void dampedInverse( const JacobiSVD <dg::Matrix>& svd, |
| 39 | + dg::Matrix& _inverseMatrix, |
| 40 | + const double threshold) { |
| 41 | + typedef JacobiSVD<dg::Matrix>::SingularValuesType SV_t; |
| 42 | + ArrayWrapper<const SV_t> sigmas (svd.singularValues()); |
| 43 | + |
| 44 | + SV_t sv_inv (sigmas / (sigmas.cwiseAbs2() + threshold * threshold)); |
| 45 | + const dg::Matrix::Index m = sv_inv.size(); |
| 46 | + |
| 47 | + _inverseMatrix.noalias() = |
| 48 | + ( svd.matrixV().leftCols(m) * sv_inv.asDiagonal() * svd.matrixU().leftCols(m).transpose()); |
| 49 | +} |
| 50 | + |
| 51 | +void dampedInverse( const dg::Matrix& _inputMatrix, |
| 52 | + dg::Matrix& _inverseMatrix, |
| 53 | + dg::Matrix& Uref, |
| 54 | + dg::Vector& Sref, |
| 55 | + dg::Matrix& Vref, |
| 56 | + const double threshold) { |
| 57 | + sotDEBUGIN(15); |
| 58 | + sotDEBUG(5) << "Input Matrix: "<<_inputMatrix<<std::endl; |
| 59 | + JacobiSVD<dg::Matrix> svd(_inputMatrix, ComputeThinU | ComputeThinV); |
| 60 | + |
| 61 | + dampedInverse (svd, _inverseMatrix, threshold); |
| 62 | + |
| 63 | + Uref = svd.matrixU(); |
| 64 | + Vref = svd.matrixV(); |
| 65 | + Sref = svd.singularValues(); |
| 66 | + |
| 67 | + sotDEBUGOUT(15); |
| 68 | +} |
| 69 | + |
| 70 | +void dampedInverse( const dg::Matrix& _inputMatrix, |
| 71 | + dg::Matrix& _inverseMatrix, |
| 72 | + const double threshold) { |
| 73 | + sotDEBUGIN(15); |
| 74 | + sotDEBUG(5) << "Input Matrix: "<<_inputMatrix<<std::endl; |
| 75 | + |
| 76 | + JacobiSVD<dg::Matrix> svd(_inputMatrix, ComputeThinU | ComputeFullV); |
| 77 | + dampedInverse (svd, _inverseMatrix, threshold); |
| 78 | + |
| 79 | + sotDEBUGOUT(15); |
| 80 | +} |
| 81 | + |
| 82 | +} |
0 commit comments