Reputation: 53
Let's say we have a matrix A
of size NxN
, and A
is sparse and N
is very large. So we naturally want to store is as as scipy sparse matrix.
We also have a dense numpy array q
of size NxK
, where K
is relatively smaller.
How do we most efficiently perform q.T * A * q
, where *
is matrix multiplication, to obtain a KxK
result?
One part of what we want can be done efficiently, that is just A * q
, but once you do that you materialize a dense array that you then need to multiply with another dense array.
Any way to do it faster than q.T.dot(A.dot(q))
?
Upvotes: 0
Views: 170
Reputation: 231738
So you have
(k,N) * (N,N) * (N,k) => (k,k)
One of those dot product results in a dense array; that times another dense is also dense. With a multiplication like this you quickly loose the sparsity.
If q
has a lot of 0s, and you want to preserve the sparse matrix nature of the problem, make q
sparse before doing this multiplication.
Upvotes: 0