Efficient sparse matrix multiplication special case

Viewed 96

Suppose that I have a sparse matrix X with dimensions (N*J) x K. I want to compute the following sum:

sum_Xi = 0
for i in range(N):
    Xi = X[i*J:(i+1)*J,:]                 # (J x K)
    Xi_sum = Xi.sum(axis=0)               # (1 x K)
    temp = Xi_sum.transpose().dot(Xi_sum) # (K x K)
    sum_Xi += temp

That is, my sparse matrix has this "block" structure with N blocks of dimension J x K. The end result of this sum is K x K. Obviously, the sum above is very inefficient, but doesn't rely on any intermediate (and potentially large) matrices.

My current approach is the following

V = csr_matrix((np.ones(N*J), (np.repeat(range(N), J), range(N*J))))  # (N x (N*J))
temp = V.dot(X)                                                       # (N x K)  
sum_Xi = temp.transpose().dot(temp).toarray().                        # (K x K)

which is significantly faster, but K <<< N < N*J so I am not thrilled with having this V and V.dot(X) sit in memory when the end result is so much smaller.

Any advice?

Thank you in advance!!

Edit: making V csr instead of csc is obvious improvement, changing above.

0 Answers
Related