Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get rank of a matrix in Eigen library?

How to get rank of a matrix in eigen?

like image 970
abdolahS Avatar asked Dec 19 '22 02:12

abdolahS


1 Answers

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 >

like image 183
Guillaume Gris Avatar answered Jan 01 '23 18:01

Guillaume Gris