Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
wolf
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
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
mobile_robotics
wolf_projects
wolf_lib
wolf
Commits
1e03cb1f
Commit
1e03cb1f
authored
5 years ago
by
Joan Solà Ortega
Browse files
Options
Downloads
Patches
Plain Diff
Add forgotten file covariance.h
parent
b3343999
No related branches found
No related tags found
1 merge request
!335
Resolve "Covariances manipulations in a specific file"
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
include/core/math/covariance.h
+108
-0
108 additions, 0 deletions
include/core/math/covariance.h
with
108 additions
and
0 deletions
include/core/math/covariance.h
0 → 100644
+
108
−
0
View file @
1e03cb1f
/**
* \file covariance.h
*
* Created on: Feb 26, 2020
* \author: jsola
*/
#ifndef MATH_COVARIANCE_H_
#define MATH_COVARIANCE_H_
#include
<Eigen/Dense>
namespace
wolf
{
template
<
typename
T
,
int
N
,
int
RC
>
inline
bool
isSymmetric
(
const
Eigen
::
Matrix
<
T
,
N
,
N
,
RC
>&
M
,
const
T
eps
=
wolf
::
Constants
::
EPS
)
{
return
M
.
isApprox
(
M
.
transpose
(),
eps
);
}
template
<
typename
T
,
int
N
,
int
RC
>
inline
bool
isPositiveSemiDefinite
(
const
Eigen
::
Matrix
<
T
,
N
,
N
,
RC
>&
M
,
const
T
&
eps
=
Constants
::
EPS
)
{
Eigen
::
SelfAdjointEigenSolver
<
Eigen
::
Matrix
<
T
,
N
,
N
,
RC
>
>
eigensolver
(
M
);
if
(
eigensolver
.
info
()
==
Eigen
::
Success
)
{
// All eigenvalues must be >= 0:
return
(
eigensolver
.
eigenvalues
().
array
()
>=
eps
).
all
();
}
return
false
;
}
template
<
typename
T
,
int
N
,
int
RC
>
inline
bool
isCovariance
(
const
Eigen
::
Matrix
<
T
,
N
,
N
,
RC
>&
M
,
const
T
&
eps
=
Constants
::
EPS
)
{
return
isSymmetric
(
M
)
&&
isPositiveSemiDefinite
(
M
,
eps
);
}
#define WOLF_ASSERT_COVARIANCE_MATRIX(x) \
assert(isCovariance(x, Constants::EPS_SMALL) && "Not a covariance");
#define WOLF_ASSERT_INFORMATION_MATRIX(x) \
assert(isCovariance(x, double(0.0)) && "Not an information matrix");
template
<
typename
T
,
int
N
,
int
RC
>
inline
bool
makePosDef
(
Eigen
::
Matrix
<
T
,
N
,
N
,
RC
>&
M
,
const
T
&
eps
=
Constants
::
EPS
)
{
Eigen
::
SelfAdjointEigenSolver
<
Eigen
::
Matrix
<
T
,
N
,
N
,
RC
>
>
eigensolver
(
M
);
if
(
eigensolver
.
info
()
==
Eigen
::
Success
)
{
// All eigenvalues must be >= 0:
double
epsilon
=
eps
;
while
((
eigensolver
.
eigenvalues
().
array
()
<
eps
).
any
())
{
//std::cout << "----- any negative eigenvalue or too close to zero\n";
//std::cout << "previous eigenvalues: " << eigensolver.eigenvalues().transpose() << std::endl;
//std::cout << "previous determinant: " << M.determinant() << std::endl;
M
=
eigensolver
.
eigenvectors
()
*
eigensolver
.
eigenvalues
().
cwiseMax
(
epsilon
).
asDiagonal
()
*
eigensolver
.
eigenvectors
().
transpose
();
eigensolver
.
compute
(
M
);
//std::cout << "epsilon used: " << epsilon << std::endl;
//std::cout << "posterior eigenvalues: " << eigensolver.eigenvalues().transpose() << std::endl;
//std::cout << "posterior determinant: " << M.determinant() << std::endl;
epsilon
*=
10
;
}
WOLF_ASSERT_COVARIANCE_MATRIX
(
M
);
return
epsilon
!=
eps
;
}
else
WOLF_ERROR
(
"Couldn't compute covariance eigen decomposition"
);
return
false
;
}
inline
Eigen
::
MatrixXd
computeSqrtUpper
(
const
Eigen
::
MatrixXd
&
_info
)
{
// impose symmetry
Eigen
::
MatrixXd
info
=
_info
.
selfadjointView
<
Eigen
::
Upper
>
();
// Normal Cholesky factorization
Eigen
::
LLT
<
Eigen
::
MatrixXd
>
llt_of_info
(
info
);
Eigen
::
MatrixXd
R
=
llt_of_info
.
matrixU
();
// Good factorization
if
(
info
.
isApprox
(
R
.
transpose
()
*
R
,
Constants
::
EPS
))
return
R
;
// Not good factorization: SelfAdjointEigenSolver
Eigen
::
SelfAdjointEigenSolver
<
Eigen
::
MatrixXd
>
es
(
info
);
Eigen
::
VectorXd
eval
=
es
.
eigenvalues
().
real
().
cwiseMax
(
Constants
::
EPS
);
R
=
eval
.
cwiseSqrt
().
asDiagonal
()
*
es
.
eigenvectors
().
real
().
transpose
();
return
R
;
}
}
#endif
/* MATH_COVARIANCE_H_ */
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