Search code examples
matheigen

How can you find the condition number in Eigen?


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.


Solution

  • 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.