我试图找到给定矩阵的零空间(Ax=0 的解空间)。我找到了两个例子,但我似乎都无法工作。此外,我无法理解他们要做什么才能到达那里,所以我无法调试。我希望有人能够引导我解决这个问题。
文档页面( numpy.linalg.svd
和 numpy.compress
)对我来说是不透明的。我学会了通过创建矩阵 C = [A|0]
、找到简化的行梯形形式并按行求解变量来做到这一点。我似乎无法理解这些示例是如何完成的。
感谢您的帮助!
这是我的样本矩阵,与 wikipedia example 相同:
A = matrix([
[2,3,5],
[-4,2,3]
])
方法( found here 和 here ):
import scipy
from scipy import linalg, matrix
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)
当我尝试时,我得到一个空矩阵:
Python 2.6.6 (r266:84292, Sep 15 2010, 16:22:56)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import scipy
>>> from scipy import linalg, matrix
>>> 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)
...
>>> A = matrix([
... [2,3,5],
... [-4,2,3]
... ])
>>>
>>> null(A)
array([], shape=(3, 0), dtype=float64)
>>>
最佳答案
Sympy 使这变得简单。
>>> 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]])]
关于numpy - Python(NumPy、SciPy),查找矩阵的零空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5889142/