如何用NumPy解齐线性方程组? [英] How to solve homogeneous linear equations with NumPy?
本文介绍了如何用NumPy解齐线性方程组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有这样的齐次线性方程式
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. ]])
我想为此提供一个非零的解决方案.如何使用 NumPy 完成?
And I want to get a non-zero solution for it. How can it be done with NumPy?
编辑
linalg.solve仅适用于A * x = b,其中b不仅包含0.
linalg.solve only works on A * x = b where b does not contains only 0.
推荐答案
您可以使用SVD或QR分解来计算线性系统的空空间,例如:
You can use an SVD or a QR decomposition to compute the null space of the linear system, e.g., something like:
import numpy
def 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
以您的示例为例:
>>> A
matrix([[-0.75, 0.25, 0.25, 0.25],
[ 1. , -1. , 0. , 0. ],
[ 1. , 0. , -1. , 0. ],
[ 1. , 0. , 0. , -1. ]])
>>> null(A).T
array([[-0.5, -0.5, -0.5, -0.5]])
>>> (A*null(A)).T
matrix([[ 1.66533454e-16, -1.66533454e-16, -2.22044605e-16, -2.22044605e-16]])
另请参见> 空空间的数值计算 在Wikipedia上.
See also the section Numerical computation of null space on Wikipedia.
这篇关于如何用NumPy解齐线性方程组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文