问题描述
我不太理解为什么numpy.linalg.solve()
给出更精确的答案,而numpy.linalg.inv()
却有些分解,给出了(我相信是)估计值.
I do not quite understand why numpy.linalg.solve()
gives the more precise answer, whereas numpy.linalg.inv()
breaks down somewhat, giving (what I believe are) estimates.
对于一个具体的例子,我正在求解方程式C^{-1} * d
,其中C
表示矩阵,而d
是向量数组.为了便于讨论,C
的尺寸为形状(1000,1000)
,d
的形状为(1,1000)
.
For a concrete example, I am solving the equation C^{-1} * d
where C
denotes a matrix, and d
is a vector-array. For the sake of discussion, the dimensions of C
are shape (1000,1000)
and d
is shape (1,1000)
.
numpy.linalg.solve(A, b)
求解x的等式A*x=b
,即x = A^{-1} * b.
因此,我可以通过以下方式解决此等式
numpy.linalg.solve(A, b)
solves the equation A*x=b
for x, i.e. x = A^{-1} * b.
Therefore, I could either solve this equation by
(1)
inverse = numpy.linalg.inv(C)
result = inverse * d
或(2)
numpy.linalg.solve(C, d)
方法(2)给出的结果要精确得多.为什么是这样?
Method (2) gives far more precise results. Why is this?
究竟发生了什么,使得一个比另一个更好"?
What exactly is happening such that one "works better" than the other?
推荐答案
np.linalg.solve(A, b)
不会不计算 A 的逆.而是调用 gesv
LAPACK例程之一,该例程首先将 A 使用LU分解,然后使用正向和反向替换求解 x (请参见此处).
np.linalg.solve(A, b)
does not compute the inverse of A. Instead it calls one of the gesv
LAPACK routines, which first factorizes A using LU decomposition, then solves for x using forward and backward substitution (see here).
np.linalg.inv
使用相同的方法通过求解 A·A中的 A 来计算 A 的逆. = I 其中, I 是身份*.分解步骤与上述步骤完全相同,但是需要更多的浮点运算来求解 A (一个 n×n 矩阵),而不是 x (长为 n 的向量).此外,如果您随后想通过身份 A ·b = x 获得 x ,那么额外的矩阵乘法将导致更多的浮动点操作,因此会降低性能并增加数值误差.
np.linalg.inv
uses the same method to compute the inverse of A by solving for A in A·A = I where I is the identity*. The factorization step is exactly the same as above, but it takes more floating point operations to solve for A (an n×n matrix) than for x (an n-long vector). Additionally, if you then wanted to obtain x via the identity A·b = x then the extra matrix multiplication would incur yet more floating point operations, and therefore slower performance and more numerical error.
不需要计算 A 的中间步骤-直接获取 x 更快,更准确.
There's no need for the intermediate step of computing A - it is faster and more accurate to obtain x directly.
* inv
的相关来源为此处.不幸的是,由于它是C模板,所以要理解它有点棘手.需要注意的重要一点是,身份矩阵正作为参数B
传递给LAPACK求解器.
* The relevant bit of source for inv
is here. Unfortunately it's a bit tricky to understand since it's templated C. The important thing to note is that an identity matrix is being passed to the LAPACK solver as parameter B
.
这篇关于为什么numpy.linalg.solve()比numpy.linalg.inv()提供更精确的矩阵求逆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!