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
Merge requests
!335
Resolve "Covariances manipulations in a specific file"
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Resolve "Covariances manipulations in a specific file"
274-covariances-manipulations-in-a-specific-file
into
devel
Overview
0
Commits
3
Pipelines
2
Changes
1
Merged
Joan Solà Ortega
requested to merge
274-covariances-manipulations-in-a-specific-file
into
devel
5 years ago
Overview
0
Commits
3
Pipelines
2
Changes
1
Expand
Closes
#274 (closed)
Edited
5 years ago
by
Joan Solà Ortega
0
0
Merge request reports
Viewing commit
1e03cb1f
Prev
Next
Show latest version
1 file
+
108
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
1e03cb1f
Add forgotten file covariance.h
· 1e03cb1f
Joan Solà Ortega
authored
5 years ago
include/core/math/covariance.h
0 → 100644
+
108
−
0
Options
/**
* \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_ */
Loading