How to solve homogeneous linear equations with NumPy? How to solve homogeneous linear equations with NumPy? numpy numpy

How to solve homogeneous linear equations with NumPy?


You can use an SVD or a QR decomposition to compute the null space of the linear system, e.g., something like:

import numpydef null(A, eps=1e-15):    u, s, vh = numpy.linalg.svd(A)    null_space = numpy.compress(s <= eps, vh, axis=0)    return null_space.T

This yields for your example:

>>> Amatrix([[-0.75,  0.25,  0.25,  0.25],        [ 1.  , -1.  ,  0.  ,  0.  ],        [ 1.  ,  0.  , -1.  ,  0.  ],        [ 1.  ,  0.  ,  0.  , -1.  ]])>>> null(A).Tarray([[-0.5, -0.5, -0.5, -0.5]])>>> (A*null(A)).Tmatrix([[ 1.66533454e-16, -1.66533454e-16, -2.22044605e-16, -2.22044605e-16]])

See also the section Numerical computation of null space on Wikipedia.


For that matter, the best solution of an over constrained homogeneous linear system is the eigenvector associated with the smallest eigenvalue. So given U as the coefficient matrix of the system, the solution is:

import numpy as npdef solution(U):    # find the eigenvalues and eigenvector of U(transpose).U    e_vals, e_vecs = np.linalg.eig(np.dot(U.T, U))      # extract the eigenvector (column) associated with the minimum eigenvalue    return e_vecs[:, np.argmin(e_vals)]