I'd like to calculate the mathematical rank of a matrix using scipy. The most obvious function numpy.rank
calculates the dimension of an array (ie. scalars have dimension 0, vectors 1, matrices 2, etc...). I am aware that the numpy.linalg.lstsq
module has this capability, but I was wondering if such a fundamental operation is built into the matrix class somewhere.
Here is an explicit example:
from numpy import matrix, rank A = matrix([[1,3,7],[2,8,3],[7,8,1]]) print rank(A)
This gives 2
the dimension, where I'm looking for an answer of 3
.
The maximum number of linearly independent vectors in a matrix is equal to the number of non-zero rows in its row echelon matrix. Therefore, to find the rank of a matrix, we simply transform the matrix to its row echelon form and count the number of non-zero rows.
Numpy provides numpy.linalg.matrix_rank()
:
>>> import numpy >>> numpy.__version__ '1.5.1' >>> A = numpy.matrix([[1,3,7],[2,8,3],[7,8,1]]) >>> numpy.linalg.matrix_rank(A) 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With