如何使用numpy在Python中找到矩阵的零空间? [英] How to find the Null Space of a matrix in Python using numpy?
问题描述
正如标题所述,如何找到矩阵的空空间 即方程ax = 0的平凡解决方案.
As the title says, how can I find the null space of a matrix i.e. the nontrivial solution to the equation ax=0.
我尝试使用np.linalg.solve(a,b)
来解决方程ax = b.因此,将b
设置为与矩阵a
相同维的零数组,我只会得到平凡的解,即x = 0.
I've tried to use np.linalg.solve(a,b)
, which solves the equation ax=b. So setting b
equal to an array of zeros with the same dimensions as matrix a
, I only get the trivial solution i.e. x=0.
推荐答案
来自 SciPy食谱:
import numpy as np
from numpy.linalg import svd
def nullspace(A, atol=1e-13, rtol=0):
A = np.atleast_2d(A)
u, s, vh = svd(A)
tol = max(atol, rtol * s[0])
nnz = (s >= tol).sum()
ns = vh[nnz:].conj().T
return ns
计算A
的空空间的近似基础.
Computes an approximate basis for the nullspace of A
.
此函数使用的算法基于A
的奇异值分解.
The algorithm used by this function is based on the singular value decomposition of A
.
参数:
A
:ndarray
A
最多应为二维.长度为k的一维数组将被视为形状为(1,k)的二维数组
A
should be at most 2-D. A 1-D array with length k will be treated as a 2-D with shape (1, k)
atol
:浮动
零奇异值的绝对公差.小于atol
的奇异值被认为是零.
The absolute tolerance for a zero singular value. Singular values smaller than atol
are considered to be zero.
rtol
:浮动
相对公差.小于rtol * smax的奇异值被认为是零,其中smax是最大奇异值.
The relative tolerance. Singular values less than rtol*smax are considered to be zero, where smax is the largest singular value.
如果atol
和rtol
均为正,则组合公差为两者的最大值;即:
If both atol
and rtol
are positive, the combined tolerance is the maximum of the two; that is:
tol = max(atol, rtol * smax)
小于tol
的奇异值被认为是零.
Singular values smaller than tol
are considered to be zero.
返回值:
ns
:ndarray
如果A
是形状为(m,k)的数组,则ns
将是形状为(k,n)的数组,其中n是A
的空空间的估计维数. ns
的列是空空间的基础; numpy.dot(A,ns)中的每个元素将近似为零.
If A
is an array with shape (m, k), then ns
will be an array with shape (k, n), where n is the estimated dimension of the nullspace of A
. The columns of ns
are a basis for the nullspace; each element in numpy.dot(A, ns) will be approximately zero.
这篇关于如何使用numpy在Python中找到矩阵的零空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!