Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate Matrix Rank using scipy

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.

like image 748
Hooked Avatar asked Mar 18 '10 23:03

Hooked


People also ask

How do you calculate matrix rank?

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.


1 Answers

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 
like image 60
Simon Avatar answered Sep 25 '22 14:09

Simon