square_root_inverse
parent
d1ab2c7288
commit
16da0895a8
|
@ -569,6 +569,23 @@ void svd(const Matrix& A, Matrix& U, Vector& s, Matrix& V) {
|
||||||
svd(U,s,V); // call in-place version
|
svd(U,s,V); // call in-place version
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ************************************************************************* */
|
||||||
|
// TODO, would be faster with Cholesky
|
||||||
|
Matrix inverse_square_root(const Matrix& A) {
|
||||||
|
size_t m = A.size2(), n = A.size1();
|
||||||
|
if (m!=n)
|
||||||
|
throw invalid_argument("inverse_square_root: A must be square");
|
||||||
|
|
||||||
|
// Perform SVD, TODO: symmetric SVD?
|
||||||
|
Matrix U,V;
|
||||||
|
Vector S;
|
||||||
|
svd(A,U,S,V);
|
||||||
|
|
||||||
|
// invert and sqrt diagonal of S
|
||||||
|
for(size_t i = 0; i<m; i++) S(i) = pow(S(i),-0.5);
|
||||||
|
return vector_scale(S, V); // V*S;
|
||||||
|
}
|
||||||
|
|
||||||
/* ************************************************************************* */
|
/* ************************************************************************* */
|
||||||
|
|
||||||
} // namespace gtsam
|
} // namespace gtsam
|
||||||
|
|
|
@ -238,6 +238,9 @@ void svd(const Matrix& A, Matrix& U, Vector& S, Matrix& V);
|
||||||
// in-place version
|
// in-place version
|
||||||
void svd(Matrix& A, Vector& S, Matrix& V);
|
void svd(Matrix& A, Vector& S, Matrix& V);
|
||||||
|
|
||||||
|
/** Use SVD to calculate inverse square root of a matrix */
|
||||||
|
Matrix inverse_square_root(const Matrix& A);
|
||||||
|
|
||||||
// macro for unit tests
|
// macro for unit tests
|
||||||
#define EQUALITY(expected,actual)\
|
#define EQUALITY(expected,actual)\
|
||||||
{ if (!assert_equal(expected,actual)) \
|
{ if (!assert_equal(expected,actual)) \
|
||||||
|
|
Loading…
Reference in New Issue