Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
uam_task_ctrl
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Pep Martí Saumell
uam_task_ctrl
Commits
3f6f97e2
Commit
3f6f97e2
authored
8 years ago
by
asantamaria
Browse files
Options
Downloads
Patches
Plain Diff
testing old calcpinv
parent
dc5fff77
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/common_fc.cpp
+45
-4
45 additions, 4 deletions
src/common_fc.cpp
with
45 additions
and
4 deletions
src/common_fc.cpp
+
45
−
4
View file @
3f6f97e2
...
...
@@ -8,10 +8,51 @@ CCommon_Fc::~CCommon_Fc(){}
Eigen
::
MatrixXd
CCommon_Fc
::
CalcPinv
(
const
Eigen
::
MatrixXd
&
a
,
double
epsilon
)
{
Eigen
::
JacobiSVD
<
Eigen
::
MatrixXd
>
svdd
(
a
,
Eigen
::
ComputeThinU
|
Eigen
::
ComputeThinV
);
double
tolerance
=
epsilon
*
std
::
max
(
a
.
cols
(),
a
.
rows
())
*
svdd
.
singularValues
().
array
().
abs
()(
0
);
Eigen
::
MatrixXd
mpinv
=
svdd
.
matrixV
()
*
(
svdd
.
singularValues
().
array
().
abs
()
>
tolerance
).
select
(
svdd
.
singularValues
().
array
().
inverse
(),
0
).
matrix
().
asDiagonal
()
*
svdd
.
matrixU
().
adjoint
();
return
mpinv
;
// Eigen::JacobiSVD<Eigen::MatrixXd> svdd(a, Eigen::ComputeThinU | Eigen::ComputeThinV);
// double tolerance = epsilon * std::max(a.cols(), a.rows()) *svdd.singularValues().array().abs()(0);
// Eigen::MatrixXd mpinv = svdd.matrixV() * (svdd.singularValues().array().abs() > tolerance).select(svdd.singularValues().array().inverse(), 0).matrix().asDiagonal() * svdd.matrixU().adjoint();
// return mpinv;
// see : http://en.wikipedia.org/wiki/Moore-Penrose_pseudoinverse#The_general_case_and_the_SVD_method
const
Eigen
::
MatrixXd
*
m
;
Eigen
::
MatrixXd
t
;
Eigen
::
MatrixXd
m_pinv
;
// transpose so SVD decomp can work...
if
(
a
.
rows
()
<
a
.
cols
()
)
{
t
=
a
.
transpose
();
m
=
&
t
;
}
else
m
=
&
a
;
// SVD
//JacobiSVD<Eigen::MatrixXd> svd = m->jacobiSvd(Eigen::ComputeFullU | Eigen::ComputeFullV);
Eigen
::
JacobiSVD
<
Eigen
::
MatrixXd
>
svd
=
m
->
jacobiSvd
(
Eigen
::
ComputeThinU
|
Eigen
::
ComputeThinV
);
Eigen
::
MatrixXd
vSingular
=
svd
.
singularValues
();
// Build a diagonal matrix with the Inverted Singular values
// The pseudo inverted singular matrix is easy to compute :
// is formed by replacing every nonzero entry by its reciprocal (inversing).
Eigen
::
MatrixXd
vPseudoInvertedSingular
(
svd
.
matrixV
().
cols
(),
1
);
for
(
int
iRow
=
0
;
iRow
<
vSingular
.
rows
();
iRow
++
)
{
if
(
fabs
(
vSingular
(
iRow
))
<=
epsilon
)
vPseudoInvertedSingular
(
iRow
,
0
)
=
0.
;
else
vPseudoInvertedSingular
(
iRow
,
0
)
=
1.
/
vSingular
(
iRow
);
}
// A little optimization here
Eigen
::
MatrixXd
mAdjointU
=
svd
.
matrixU
().
adjoint
().
block
(
0
,
0
,
vSingular
.
rows
(),
svd
.
matrixU
().
adjoint
().
cols
());
// Pseudo-Inversion : V * S * U'
m_pinv
=
(
svd
.
matrixV
()
*
vPseudoInvertedSingular
.
asDiagonal
())
*
mAdjointU
;
// transpose back if necessary
if
(
a
.
rows
()
<
a
.
cols
()
)
return
m_pinv
.
transpose
();
return
m_pinv
;
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment