有良好的C矩阵求逆程序 [英] Good matrix inversion routines in C
问题描述
作为一个Python code的数值计算的一部分,我必须有所反转大型(稀疏)矩阵(100×100〜)很多次。我真的想加快程序,方式之一向我建议是在C调用一个子程序矩阵求逆一步。
是否有任何建议的高效和完善的测试C例程完成这个任务?
感谢您。
>>>从numpy的进口*
>>>从numpy.linalg进口INV
>>>从scipy.sparse进口csr_matrix
>>> M =矩阵([3,1,5],[1,0,8],[2,1,4]])
>>> S = csr_matrix(米)
>>> INVS = INV(一)#逆稀疏矩阵
>>>点(A,INVA)#检查的结果,应该是机器precision眼内(3)
csr_matrix([[1.00000000e-00,2.77555756e-17,3.60822483e-16],
[0.00000000e + 00,+ 1.00000000e 00,0.00000000e + 00]
[-1.11022302e-16,0.00000000e + 00,1.00000000e + 00]])
难道真是你需要逆?您可能能够实现自己的目标,而不倒置:
在这里你的案例真的的需要逆是罕见的。此外,该
稀疏矩阵的逆不一定稀疏。通常情况下,
反相是比LU分解更加昂贵,而且容易
舍入误差。
块引用>- http://mail.scipy.org/ pipermail / SciPy的用户/ 2007年10月/ 013936.html
- > <一href=\"http://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.linalg.factorized.html#scipy.sparse.linalg.factorized\" rel=\"nofollow\">http://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.linalg.factorized.html#scipy.sparse.linalg.factorized
As part of a python code for a numerical calculation, I must invert somewhat large (sparse) matrices (~100x100) many times. I would really like to speed up the program, and one of the ways suggested to me is to call to a subroutine in C for the matrix inversion step.
Are there any recommended efficient and well-tested C routines for this task?
Thank you.
解决方案>>> from numpy import * >>> from numpy.linalg import inv >>> from scipy.sparse import csr_matrix >>> m = matrix([[3,1,5],[1,0,8],[2,1,4]]) >>> s = csr_matrix(m) >>> invs = inv(a) # Inverse sparse matrix >>> dot(a,inva) # Check the result, should be eye(3) within machine precision csr_matrix([[ 1.00000000e-00, 2.77555756e-17, 3.60822483e-16], [ 0.00000000e+00, 1.00000000e+00, 0.00000000e+00], [ -1.11022302e-16, 0.00000000e+00, 1.00000000e+00]])
Is it really the inverse you need? You may be able to achieve your goal without inversion:
Cases where you really need the inverse are rare. Moreover, the inverse of a sparse matrix is not necessarily sparse. Typically, inverting is more expensive than an LU factorization and prone to rounding errors.
-- http://mail.scipy.org/pipermail/scipy-user/2007-October/013936.html
这篇关于有良好的C矩阵求逆程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!