[SciPy-user] null space of a matrix

Bastian Weber bastian.weber@gmx-topmail...
Sat Dec 13 09:57:15 CST 2008


Hello to the list!

In Summer of 2005 there was a short discussion on how to compute the
null space of a given matrix.

The starting message can be found here:
http://projects.scipy.org/pipermail/scipy-user/2005-June/004645.html

Then Ryan summarized the discussion in this short function:

> def null(A, eps=1e-15):
>     u, s, vh = scipy.linalg.svd(A)
>     null_mask = (s <= eps)
>     null_space = scipy.compress(null_mask, vh, axis=0)
>     return scipy.transpose(null_space)

Applying this to a n by m matrix with n <> m did not work. I think it is
probably because s is containing only the nonzero singular values.

So I extended the code a little bit such that, len(s) now = m with m the
bigger dim of A.

import scipy
import scipy.linalg

def null(A, eps=1e-15):
    """
    computes the null space of the real matrix A
    """
    n, m = scipy.shape(A)
    if n > m :
        return scipy.transpose(null(scipy.transpose(A), eps))
        return null(scipy.transpose(A), eps)
    u, s, vh = scipy.linalg.svd(A)
    s=scipy.append(s,scipy.zeros(m))[0:m]
    null_mask = (s <= eps)
    null_space = scipy.compress(null_mask, vh, axis=0)
    return scipy.transpose(null_space)


# example:
K=scipy.matrix(scipy.rand(3,7))
#       matrix type to have convient multiplication later
G=K.transpose()


# results should be "almost" zero
print K*null(K)
print null(G)*G




As my knowledge of linear algebra is more or less on average level of
engineering students I dont know whether this extended code is correct
or usefull. Just in case, feel free to use it.

Any comments are very welcome.


Bastian.


More information about the SciPy-user mailing list