numpy:反转上三角矩阵
在 numpy/scipy 中,计算上三角矩阵逆的规范方法是什么?
矩阵存储为具有零个下对角线元素的 2D numpy 数组,并且结果也应存储为 2D 数组。
编辑到目前为止我发现的最好的是scipy.linalg.solve_triangle(A, np.identity(n))
。是这样吗?
In numpy
/scipy
, what's the canonical way to compute the inverse of an upper triangular matrix?
The matrix is stored as 2D numpy
array with zero sub-diagonal elements, and the result should also be stored as a 2D array.
edit The best I've found so far is scipy.linalg.solve_triangular(A, np.identity(n))
. Is that it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
本身确实不存在反转例程。 scipy.linalg.solve 是求解矩阵向量或矩阵矩阵方程的规范方法,并且可以给出有关矩阵结构的明确信息,它将用于选择正确的例程(在本例中可能相当于 BLAS3 dtrsm)。
LAPACK 确实包含用于此目的的
doptri
,并且scipy.linalg
确实公开了原始 C lapack 接口。如果逆矩阵确实是您想要的,那么您可以尝试使用它。There really isn't an inversion routine, per se.
scipy.linalg.solve
is the canonical way of solving a matrix-vector or matrix-matrix equation, and it can be given explicit information about the structure of the matrix which it will use to choose the correct routine (probably the equivalent of BLAS3 dtrsm in this case).LAPACK does include
doptri
for this purpose, andscipy.linalg
does expose a raw C lapack interface. If the inverse matrix is really what you want, then you could try using that.我同意 dtrtri 应该更明显,所以我写了一个例子。
至少对于这个简单的例子,我们得到:
这表明
dtrtri()
比inv()
更快、更准确。在本例中,inv()
和dtrtri()
都计算出一个恰好是上三角的矩阵。但是,下三角矩阵的情况并非如此,对角线上方的小条目会污染inv()
的结果。I agree that
dtrtri
should be more visible, so I wrote an example.At least for this simple example we get:
Which shows that
dtrtri()
is both faster and accurate thaninv()
. In this case, bothinv()
anddtrtri()
compute a matrix that is exactly upper triangular. However, this is not the case for a lower triangular matrix, where small entries above the diagonal pollute the result ofinv()
.