I posted this question on https://scicomp.stackexchange.com, but received no attention. As long as I get answer in one of them, I will inform in the other.
I have a matrix B
which is sparse and try to utilize a function scipy.sparse.linalg.spilu
specialized for sparse matrix to factorize B
. Could you please explain why this function is significantly less efficient than the function scipy.linalg.lu
for general matrix? Thank you so much!
import numpy as np import scipy.linalg as la import scipy.sparse.linalg as spla import time from scipy import sparse from scipy.sparse import csc_matrix A = np.random.randint(100, size=(10000, 10000)) B = np.triu(A, -100) start = time.time() (P, L, U) = la.lu(B) end = time.time() print('Time to decompose B with lu =', end - start) start = time.time() mtx = spla.spilu(csc_matrix(B)) end = time.time() print('Time to decompose B with spilu =', end - start)
The computation time is
Time to decompose B with lu = 4.7765138149261475 Time to decompose B with spilu = 14.165712594985962
Advertisement
Answer
(B==0).sum() Out[5]: 49510694 B.shape Out[6]: (10000, 10000) (B==0).sum()/100000000 Out[7]: 0.49510694
Your matrix B is not sparse at all. More than half of the elements in B are non-zeros. Of course spilu would be less efficient when dealing with such a dense matrix.