我不認爲稀疏模塊有明確的逆方法,但它確實有稀疏求解器。類似這個玩具的例子工程:
>>> a = np.random.rand(3, 3)
>>> a
array([[ 0.31837307, 0.11282832, 0.70878689],
[ 0.32481098, 0.94713997, 0.5034967 ],
[ 0.391264 , 0.58149983, 0.34353628]])
>>> np.linalg.inv(a)
array([[-0.29964242, -3.43275347, 5.64936743],
[-0.78524966, 1.54400931, -0.64281108],
[ 1.67045482, 1.29614174, -2.43525829]])
>>> a_sps = scipy.sparse.csc_matrix(a)
>>> lu_obj = scipy.sparse.linalg.splu(a_sps)
>>> lu_obj.solve(np.eye(3))
array([[-0.29964242, -0.78524966, 1.67045482],
[-3.43275347, 1.54400931, 1.29614174],
[ 5.64936743, -0.64281108, -2.43525829]])
請注意,結果是轉置!
如果您期望反函數也是稀疏的,並且上一次求解的密集返回不適合內存,那麼您也可以一次生成一行(列),提取非零值,並建立從這些稀疏逆矩陣:
>>> for k in xrange(3) :
... b = np.zeros((3,))
... b[k] = 1
... print lu_obj.solve(b)
...
[-0.29964242 -0.78524966 1.67045482]
[-3.43275347 1.54400931 1.29614174]
[ 5.64936743 -0.64281108 -2.43525829]
'scipy'文件說你不需要緻密矩陣,所以我有點困惑。 http://docs.scipy.org/doc/scipy-0.8.x/reference/sparse.html – BenDundee 2013-02-27 17:22:27
scipy的版本0.12(目前處於beta測試階段)具有函數scipy.sparse.linalg.inv。 – 2013-02-27 17:51:30