Skip to content

Commit bfabaa0

Browse files
committed
Fix scalar broadcast_to with nnz=0.
Fixes #513.
1 parent 5211441 commit bfabaa0

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

sparse/_umath.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,9 @@ def _get_expanded_coords_data(coords, data, params, broadcast_shape):
274274
if first_dim != -1:
275275
expanded_data = data[all_idx[first_dim]]
276276
else:
277-
expanded_coords = all_idx
277+
expanded_coords = (
278+
all_idx if len(data) else np.empty((0, all_idx.shape[1]), dtype=np.intp)
279+
)
278280
expanded_data = np.repeat(data, np.prod(broadcast_shape, dtype=np.int64))
279281
return np.asarray(expanded_coords), np.asarray(expanded_data)
280282

sparse/tests/test_array_function.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,8 @@ def test_property(func):
7474
xx = func(x)
7575
yy = func(y)
7676
assert xx == yy
77+
78+
79+
def test_broadcast_to_scalar():
80+
s = sparse.COO.from_numpy([0, 0, 1, 2])
81+
np.broadcast_to(np.zeros_like(s, shape=()), (3,))

0 commit comments

Comments
 (0)