Skip to content

Why is ‘scipy.sparse.linalg.spilu’ less efficient than ‘’ for sparse matrix?

I posted this question on, 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 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) =
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



Out[5]: 49510694

Out[6]: (10000, 10000)

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.