The following code will not even finish on my system:
import numpy as np
from scipy import sparse
p = 100
n = 50
X = np.random.randn(p,n)
L = sparse.eye(p,p, format='csc')
X.T.dot(L).dot(X)
Is there any explanation why this matrix multiplication is hanging?
X.T.dot(L)is not, as you may think, a 50×100 matrix, but an array of 50×100 sparse matrices of 100×100It seems that the problem is that
X‘sdotmethod, it being an array, doesn’t know about sparse matrices. So you must either convert the sparse matrix to dense using itstodenseortoarraymethod. The former returns amatrixobject, the latter anarray:Alternatively, sparse matrices have a
dotmethod that knows about arrays: