Home:ALL Converter>How to solve homogeneous linear equations with NumPy?

How to solve homogeneous linear equations with NumPy?

Ask Time:2009-12-03T03:28:39         Author:faceclean

Json Formatter

If I have homogeneous linear equations like this

array([[-0.75,  0.25,  0.25,  0.25],
       [ 1.  , -1.  ,  0.  ,  0.  ],
       [ 1.  ,  0.  , -1.  ,  0.  ],
       [ 1.  ,  0.  ,  0.  , -1.  ]])

And I want to get a non-zero solution for it. How can it be done with NumPy?

EDIT

linalg.solve only works on A * x = b where b does not contains only 0.

Author:faceclean,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/1835246/how-to-solve-homogeneous-linear-equations-with-numpy
rcs :

You can use an SVD or a QR decomposition to compute the null space of the linear system, e.g., something like:\n\nimport numpy\n\ndef null(A, eps=1e-15):\n u, s, vh = numpy.linalg.svd(A)\n null_space = numpy.compress(s <= eps, vh, axis=0)\n return null_space.T\n\n\nThis yields for your example:\n\n>>> A\nmatrix([[-0.75, 0.25, 0.25, 0.25],\n [ 1. , -1. , 0. , 0. ],\n [ 1. , 0. , -1. , 0. ],\n [ 1. , 0. , 0. , -1. ]])\n\n>>> null(A).T\narray([[-0.5, -0.5, -0.5, -0.5]])\n\n>>> (A*null(A)).T\nmatrix([[ 1.66533454e-16, -1.66533454e-16, -2.22044605e-16, -2.22044605e-16]])\n\n\nSee also the section Numerical computation of null space on Wikipedia.",
2009-12-02T21:39:25
Sasha Nicolas :

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:\n\nimport numpy as np\n\ndef solution(U):\n # find the eigenvalues and eigenvector of U(transpose).U\n e_vals, e_vecs = np.linalg.eig(np.dot(U.T, U)) \n # extract the eigenvector (column) associated with the minimum eigenvalue\n return e_vecs[:, np.argmin(e_vals)] \n",
2016-02-03T21:47:52
yy