Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix empty tensordot. #256

Merged
merged 1 commit into from
May 26, 2019
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
8 changes: 8 additions & 0 deletions sparse/coo/common.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from functools import reduce, wraps
from itertools import chain
import operator
import warnings
from collections.abc import Iterable
Expand Down Expand Up @@ -148,6 +149,13 @@ def tensordot(a, b, axes=2):
newshape_b = (N2, -1)
oldb = [bs[axis] for axis in notin]

if any(dim == 0 for dim in chain(newshape_a, newshape_b)):
res = asCOO(np.empty(olda + oldb), check=False)
if isinstance(a, np.ndarray) or isinstance(b, np.ndarray):
res = res.todense()

return res

at = a.transpose(newaxes_a).reshape(newshape_a)
bt = b.transpose(newaxes_b).reshape(newshape_b)
res = _dot(at, bt)
Expand Down
9 changes: 9 additions & 0 deletions tests/test_coo.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,15 @@ def test_tensordot(a_shape, b_shape, axes):
# assert isinstance(sparse.tensordot(a, sb, axes), COO)


def test_tensordot_empty():
x1 = np.empty((0, 0, 0))
x2 = np.empty((0, 0, 0))
s1 = sparse.COO.from_numpy(x1)
s2 = sparse.COO.from_numpy(x2)

assert_eq(np.tensordot(x1, x2), sparse.tensordot(s1, s2))


@pytest.mark.parametrize('a_shape, b_shape', [
((3, 1, 6, 5), (2, 1, 4, 5, 6)),
((2, 1, 4, 5, 6), (3, 1, 6, 5)),
Expand Down