Calculate Matrix Rank using scipy

matrix, numpy, python, scipy

Solution

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

Problem

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`.

Original source