How to get rank of a matrix in Eigen library?

8.2k views Asked by At
2

There are 2 answers

1
Guillaume Gris On BEST ANSWER

You need to convert your matrix to a rank-revealing decomposition. For instance FullPivLU. If you have a matrix3f it looks like this :

FullPivLU<Matrix3f> lu_decomp(your_matrix);
auto rank = lu_decomp.rank();

Edit

Decomposing the matrix is the most common way to get the rank. Although, LU is not the most reliable way to achieve it for floating values as explained on the rank article on wikipedia

When applied to floating point computations on computers, basic Gaussian elimination (LU decomposition) can be unreliable, and a rank-revealing decomposition should be used instead. An effective alternative is the singular value decomposition (SVD), but there are other less expensive choices, such as QR decomposition with pivoting (so-called rank-revealing QR factorization), which are still more numerically robust than Gaussian elimination. Numerical determination of rank requires a criterion for deciding when a value, such as a singular value from the SVD, should be treated as zero, a practical choice which depends on both the matrix and the application.

So you might get more accurate results with Eigen::ColPivHouseholderQR< MatrixType >

0
pem On

use QR or SVD decomposition and check the resulted matrix should also work. SVD may be more reliable.