square_root_inverse

release/4.3a0
Frank Dellaert 2009-12-08 20:48:13 +00:00
parent d1ab2c7288
commit 16da0895a8
2 changed files with 20 additions and 0 deletions

View File

@ -569,6 +569,23 @@ void svd(const Matrix& A, Matrix& U, Vector& s, Matrix& V) {
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

View File

@ -238,6 +238,9 @@ void svd(const Matrix& A, Matrix& U, Vector& S, Matrix& V);
// in-place version
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
#define EQUALITY(expected,actual)\
{ if (!assert_equal(expected,actual)) \