Skip to content

Commit 1c46ea8

Browse files
committed
Split declaration and definition of svd related functions
1 parent 324062c commit 1c46ea8

File tree

3 files changed

+88
-47
lines changed

3 files changed

+88
-47
lines changed

include/sot/core/matrix-svd.hh

Lines changed: 4 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -35,65 +35,22 @@ namespace Eigen {
3535

3636
void pseudoInverse( dg::Matrix& _inputMatrix,
3737
dg::Matrix& _inverseMatrix,
38-
const double threshold = 1e-6) {
39-
JacobiSVD<dg::Matrix> svd(_inputMatrix, ComputeThinU | ComputeThinV);
40-
JacobiSVD<dg::Matrix>::SingularValuesType m_singularValues=svd.singularValues();
41-
JacobiSVD<dg::Matrix>::SingularValuesType singularValues_inv;
42-
singularValues_inv.resizeLike(m_singularValues);
43-
for ( long i=0; i<m_singularValues.size(); ++i) {
44-
if ( m_singularValues(i) > threshold )
45-
singularValues_inv(i)=1.0/m_singularValues(i);
46-
else singularValues_inv(i)=0;
47-
}
48-
_inverseMatrix = (svd.matrixV()*singularValues_inv.asDiagonal()*svd.matrixU().transpose());
49-
}
38+
const double threshold = 1e-6);
5039

5140
void dampedInverse( const JacobiSVD <dg::Matrix>& svd,
5241
dg::Matrix& _inverseMatrix,
53-
const double threshold = 1e-6) {
54-
typedef JacobiSVD<dg::Matrix>::SingularValuesType SV_t;
55-
ArrayWrapper<const SV_t> sigmas (svd.singularValues());
56-
57-
SV_t sv_inv (sigmas / (sigmas.cwiseAbs2() + threshold * threshold));
58-
const dg::Matrix::Index m = sv_inv.size();
59-
60-
_inverseMatrix.noalias() =
61-
( svd.matrixV().leftCols(m) * sv_inv.asDiagonal() * svd.matrixU().leftCols(m).transpose());
62-
}
42+
const double threshold = 1e-6);
6343

6444
void dampedInverse( const dg::Matrix& _inputMatrix,
6545
dg::Matrix& _inverseMatrix,
6646
dg::Matrix& Uref,
6747
dg::Vector& Sref,
6848
dg::Matrix& Vref,
69-
const double threshold = 1e-6) {
70-
sotDEBUGIN(15);
71-
sotDEBUG(5) << "Input Matrix: "<<_inputMatrix<<std::endl;
72-
JacobiSVD<dg::Matrix> svd(_inputMatrix, ComputeThinU | ComputeThinV);
73-
74-
dampedInverse (svd, _inverseMatrix, threshold);
75-
76-
Uref = svd.matrixU();
77-
Vref = svd.matrixV();
78-
Sref = svd.singularValues();
79-
80-
sotDEBUGOUT(15);
81-
}
49+
const double threshold = 1e-6);
8250

8351
void dampedInverse( const dg::Matrix& _inputMatrix,
8452
dg::Matrix& _inverseMatrix,
85-
const double threshold = 1e-6) {
86-
sotDEBUGIN(15);
87-
sotDEBUG(5) << "Input Matrix: "<<_inputMatrix<<std::endl;
88-
89-
JacobiSVD<dg::Matrix> svd(_inputMatrix, ComputeThinU | ComputeFullV);
90-
dampedInverse (svd, _inverseMatrix, threshold);
91-
92-
sotDEBUGOUT(15);
93-
}
94-
95-
96-
53+
const double threshold = 1e-6);
9754

9855
}
9956

src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ SET(${LIBRARY_NAME}_SOURCES
152152
tools/periodic-call
153153
tools/device
154154
tools/trajectory
155+
156+
matrix/matrix-svd
155157
)
156158

157159
ADD_LIBRARY(${LIBRARY_NAME}

src/matrix/matrix-svd.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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

Comments
 (0)