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!
JavaScript
x
19
19
1
import numpy as np
2
import scipy.linalg as la
3
import scipy.sparse.linalg as spla
4
import time
5
from scipy import sparse
6
from scipy.sparse import csc_matrix
7
A = np.random.randint(100, size=(10000, 10000))
8
B = np.triu(A, -100)
9
10
start = time.time()
11
(P, L, U) = la.lu(B)
12
end = time.time()
13
print('Time to decompose B with lu =', end - start)
14
15
start = time.time()
16
mtx = spla.spilu(csc_matrix(B))
17
end = time.time()
18
print('Time to decompose B with spilu =', end - start)
19
The computation time is
JavaScript
1
3
1
Time to decompose B with lu = 4.7765138149261475
2
Time to decompose B with spilu = 14.165712594985962
3
Advertisement
Answer
JavaScript
1
9
1
(B==0).sum()
2
Out[5]: 49510694
3
4
B.shape
5
Out[6]: (10000, 10000)
6
7
(B==0).sum()/100000000
8
Out[7]: 0.49510694
9
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.