Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions sparse/coo/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,17 @@ def matmul(a, b):
"Cannot perform dot product on types %s, %s" %
(type(a), type(b)))

# When one of the input is less than 2-d, it is equivalent to dot
if a.ndim <= 2 or b.ndim <= 2:
# When b is 2-d, it is equivalent to dot
if b.ndim <= 2:
return dot(a, b)

# when a is 2-d, we need to transpose result after dot
if a.ndim <= 2:
res = dot(a, b)
axes = list(range(res.ndim))
axes.insert(-1, axes.pop(0))
return res.transpose(axes)

# If a can be squeeze to a vector, use dot will be faster
if a.ndim <= b.ndim and np.prod(a.shape[:-1]) == 1:
res = dot(a.reshape(-1), b)
Expand Down
1 change: 1 addition & 0 deletions sparse/tests/test_coo.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ def test_tensordot(a_shape, b_shape, axes):
((5,), (5, 6)),
((4, 5), (5,)),
((5,), (5,)),
((3, 4), (1, 2, 4, 3)),
])
def test_matmul(a_shape, b_shape):
sa = sparse.random(a_shape, density=0.5)
Expand Down