How to get rank of a matrix in Eigen library?
Asked Answered
K

2

5

How to get rank of a matrix in eigen?

Kasiekask answered 25/6, 2015 at 5:31 Comment(1)
Build FullPivLU or ColPivHouseholderQR or whatever from the matrix, those have rank().Merry
C
10

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 >

Clam answered 25/6, 2015 at 5:50 Comment(1)
does computing decomposition performance is better than computing rank itself? ( and why eigen used decomposition?)Kasiekask
D
2

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

Deyo answered 25/6, 2015 at 6:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.