Python (NumPy, SciPy), finding the null space of a matrix
Sympy makes this straightforward.
>>> from sympy import Matrix>>> A = [[2, 3, 5], [-4, 2, 3], [0, 0, 0]]>>> A = Matrix(A)>>> A * A.nullspace()[0]Matrix([[0],[0],[0]])>>> A.nullspace()[Matrix([[-1/16],[-13/8],[ 1]])]
You get the SVD decomposition of the matrix A
. s
is a vector of eigenvalues. You are interested in almost zero eigenvalues (see $A*x=\lambda*x$ where $\abs(\lambda)<\epsilon$), which is given by the vector of logical values null_mask
.
Then, you extract from the list vh
the eigenvectors corresponding to the almost zero eigenvalues, which is exactly what you are looking for: a way to span the null space. Basically, you extract the rows and then transpose the results so that you get a matrix with eigenvectors as columns.
It appears to be working okay for me:
A = matrix([[2,3,5],[-4,2,3],[0,0,0]])A * null(A)>>> [[ 4.02455846e-16]>>> [ 1.94289029e-16]>>> [ 0.00000000e+00]]