How can you find the condition number in Eigen?
Asked Answered
C

3

5

In Matlab there are cond and rcond, and in LAPACK too. Is there any routine in Eigen to find the condition number of a matrix?

I have a Cholesky decomposition of a matrix and I want to check if it is close to singularity, but cannot find a similar function in the docs.

UPDATE: I think I can use something like this algorithm, which makes use of the triangular factorization. The method by Ilya is useful for more accurate answers, so I will mark it as correct.

Centigram answered 6/11, 2015 at 20:49 Comment(0)
S
8

Probably easiest way to compute the condition number is using the expression:

cond(A) = max(sigma) / min(sigma)

where sigma is an array of singular values, the result of SVD. Eigen author suggests this code:

JacobiSVD<MatrixXd> svd(A);
double cond = svd.singularValues()(0) 
    / svd.singularValues()(svd.singularValues().size()-1);

Other ways are (less efficient)

cond(A) = max(lambda) / min(lambda)
cond(A) = norm2(A) * norm2(A^-1)

where lambda is an array of eigenvalues.

It looks like Cholesky decomposition does not directly help here, but I cant tell for sure at the moment.

Speight answered 6/11, 2015 at 23:57 Comment(1)
Yeah, know about the Eigenvalue method, but in theory there is a faster one that works with just estimates?Centigram
N
0

You could use the Gershgorin circle theorem to get a rough estimate.

But as Ilya Popov has already pointed out, calculating the eigenvalues/singular values is more reliable. However, it doesn't make sense to calculate all eigenvalues, which gets very expensive. You only need the largest and the smallest eigenvalues, for that you can use the Power method for the largest and Inverse Iteration for the smallest eigenvalue.

Or you could use a library that can do this already, e.g. Spectra.

Naturism answered 21/4, 2016 at 8:33 Comment(0)
C
0

You can use norms. In my robotics experience, this is computationally faster than singular values:

pseudoInverse(matrix).norm() * matrix.norm()

I found this to be 2.6X faster than singular values for 6x6 matrices. It's also recommended in this book:

B. Siciliano, and O. Khatib, Springer Handbook of Robotics. Berlin: Springer Science and Business Media, 2008, p. 236.

Carolinacaroline answered 20/9, 2018 at 16:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.