Skip to content

Allow mixed ndarray-sparse ops where the shape doesn't change. #275

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

Merged
merged 1 commit into from
Aug 12, 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
14 changes: 11 additions & 3 deletions sparse/coo/umath.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,6 @@ def __init__(self, func, *args, **kwargs):
kwargs : dict
Extra arguments to pass to the function.
"""

from .core import COO
from ..sparse_array import SparseArray

Expand All @@ -426,14 +425,18 @@ def __init__(self, func, *args, **kwargs):
self.kwargs = kwargs
self.cache = {}

self._get_fill_value()
self._check_broadcast()
self._get_fill_value()

def get_result(self):
from .core import COO
if self.args is None:
return NotImplemented

if self.shape == self.ndarray_shape:
args = [a.todense() if isinstance(a, COO) else a for a in self.args]
return self.func(*args, **self.kwargs)

if any(s == 0 for s in self.shape):
data = np.empty((0,), dtype=self.fill_value.dtype)
coords = np.empty((0, len(self.shape)), dtype=np.intp)
Expand Down Expand Up @@ -487,7 +490,7 @@ def _get_fill_value(self):
arg.fill_value if isinstance(arg, COO) else _zero_of_dtype(arg.dtype) for arg in self.args)
fill_value = self.func(*zero_args, **self.kwargs)[()]

if not equivalent(fill_value, fill_value_array).all():
if not equivalent(fill_value, fill_value_array).all() and self.shape != self.ndarray_shape:
raise ValueError('Performing a mixed sparse-dense operation that would result in a dense array. '
'Please make sure that func(sparse_fill_values, ndarrays) is a constant array.')

Expand All @@ -512,12 +515,17 @@ def _check_broadcast(self):
non_ndarray_shape = _get_nary_broadcast_shape(
*tuple(arg.shape for arg in self.args if isinstance(arg, COO))
)
ndarray_shape = _get_nary_broadcast_shape(
*tuple(arg.shape for arg in self.args if isinstance(arg, np.ndarray))
)

if full_shape != non_ndarray_shape:
raise ValueError('Please make sure that the broadcast shape of just the sparse arrays is '
'the same as the broadcast shape of all the operands.')

self.shape = full_shape
self.ndarray_shape = ndarray_shape
self.non_ndarray_shape = non_ndarray_shape

def _get_func_coords_data(self, mask):
"""
Expand Down
4 changes: 2 additions & 2 deletions tests/test_coo.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ def test_sparsearray_elemwise(format):


def test_ndarray_densification_fails():
xs = sparse.random((3, 4), density=0.5)
xs = sparse.random((2, 3, 4), density=0.5)
y = np.random.rand(3, 4)

with pytest.raises(ValueError):
Expand Down Expand Up @@ -1536,7 +1536,7 @@ def test_cache_csr():
def test_empty_shape():
x = COO(np.empty((0, 1), dtype=np.int8), [1.0])
assert x.shape == ()
assert ((2 * x).todense() == np.array(2.0)).all()
assert_eq(2 * x, np.float_(2.0))


def test_single_dimension():
Expand Down