It seems numpy's einsum
function does not work with scipy.sparse
matrices. Are there alternatives to do the sorts of things einsum
can do with sparse matrices?
In response to @eickenberg's answer: The particular einsum I'm wanting to is numpy.einsum("ki,kj->ij",A,A)
- the sum of the outer products of the rows.
A restriction of scipy.sparse
matrices is that they represent linear operators and are thus kept two dimensional, which leads to the question: Which operation are you seeking to do?
All einsum
operations on a pair of 2D matrices are very easy to write without einsum
using dot
, transpose
and pointwise operations, provided that the result does not exceed two dimensions.
So if you need a specific operation on a number of sparse matrices, it is probable that you can write it without einsum
.
UPDATE: A specific way to implement np.einsum("ki, kj -> ij", A, A)
is A.T.dot(A)
. In order to convince yourself, please try the following example:
import numpy as np
rng = np.random.RandomState(42)
a = rng.randn(3, 3)
b = rng.randn(3, 3)
the_einsum_ab = np.einsum("ki, kj -> ij", a, b)
the_a_transpose_times_b = a.T.dot(b)
# We write a test in order to assert equality
from numpy.testing import assert_array_equal
assert_array_equal(the_einsum_ab, the_a_transpose_times_b) # This passes, so equality
This result is slightly more general. Now if you use b = a
you obtain your specific result.