def matrixrank(A, tol=1e-08):
    """
    http://mail.scipy.org/pipermail/numpy-discussion/2008-February/031218.html
    """
s = linalg.svd(A, compute_uv=0)
return sum(np.where(s > tol, 1, 0))
matrices = [np.array([(2, 4, 1, 3), (-1, -2, 1, 0), (0, 0, 2, 2), (3, 6, 2,
    5)]), np.array([(1, 2, 3), (2, 4, 6), (4, 5, 6)]).T, np.array([(1, 2, 3,
    1), (2, 4, 6, 2), (4, 5, 6, 3)]).T, np.array([(1, 2, 3, 1), (2, 4, 6, 3
    ), (4, 5, 6, 3)]).T, np.array([(1, 2, 3), (2, 4, 6), (4, 5, 6), (7, 8, 
    9)]).T]
for A in matrices:
    B = independent_columns(A)
How to find degenerate rows/columns in a covariance matrix