Invert large sparse matrices using scipy

I need to invert a large sparse matrix. I cannot escape from matrix inversion, the only shortcut would be to simply get an idea of ​​the main diagonal elements and ignore off-diagonal elements (I would prefer, but it would be acceptable as a solution).

The matrices I need to invert are usually large (40,000 * 40,000) and have only a few non-zero diagonals. My current approach is to build everything sparse and then

posterior_covar = np.linalg.inv ( hessian.todense() )

it obviously takes a lot of time and a lot of memory.

Any clues, or is it just a matter of patience or reducing the problem?

+5
source share
1 answer

, , . - :

>>> 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]
+5

All Articles