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
6 changes: 6 additions & 0 deletions docs/generated/sparse.COO.all.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
COO.all
=======

.. currentmodule:: sparse

.. automethod:: COO.all
6 changes: 6 additions & 0 deletions docs/generated/sparse.COO.any.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
COO.any
=======

.. currentmodule:: sparse

.. automethod:: COO.any
7 changes: 5 additions & 2 deletions docs/generated/sparse.COO.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ COO
:toctree:

COO.reduce

COO.sum
COO.max
COO.min
COO.prod
COO.min
COO.max
COO.any
COO.all

.. rubric:: :ref:`Converting to other formats <converting>`
.. autosummary::
Expand Down
110 changes: 109 additions & 1 deletion sparse/coo/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,114 @@ def max(self, axis=None, keepdims=False, out=None):
"""
return np.maximum.reduce(self, out=out, axis=axis, keepdims=keepdims)

def any(self, axis=None, keepdims=False, out=None):
"""
See if any values along array are ``True``. Uses all axes by default.

Parameters
----------
axis : Union[int, Iterable[int]], optional
The axes along which to minimize. Uses all axes by default.
keepdims : bool, optional
Whether or not to keep the dimensions of the original array.

Returns
-------
COO
The reduced output sparse array.

See Also
--------
:obj:`numpy.all` : Equivalent numpy function.

Notes
-----
* This function internally calls :obj:`COO.sum_duplicates` to bring the array into
canonical form.
* The :code:`out` parameter is provided just for compatibility with Numpy and
isn't actually supported.

Examples
--------
You can use :obj:`COO.min` to minimize an array across any dimension.

>>> x = np.array([[False, False],
... [False, True ],
... [True, False],
... [True, True ]])
>>> s = COO.from_numpy(x)
>>> s2 = s.any(axis=1)
>>> s2.todense() # doctest: +SKIP
array([False, True, True, True])

You can also use the :code:`keepdims` argument to keep the dimensions after the
minimization.

>>> s3 = s.any(axis=1, keepdims=True)
>>> s3.shape
(4, 1)

By default, this reduces the array down to one number, minimizing along all axes.

>>> s.any()
True
"""
return np.logical_or.reduce(self, out=out, axis=axis, keepdims=keepdims)

def all(self, axis=None, keepdims=False, out=None):
"""
See if all values in an array are ``True``. Uses all axes by default.

Parameters
----------
axis : Union[int, Iterable[int]], optional
The axes along which to minimize. Uses all axes by default.
keepdims : bool, optional
Whether or not to keep the dimensions of the original array.

Returns
-------
COO
The reduced output sparse array.

See Also
--------
:obj:`numpy.all` : Equivalent numpy function.

Notes
-----
* This function internally calls :obj:`COO.sum_duplicates` to bring the array into
canonical form.
* The :code:`out` parameter is provided just for compatibility with Numpy and
isn't actually supported.

Examples
--------
You can use :obj:`COO.min` to minimize an array across any dimension.

>>> x = np.array([[False, False],
... [False, True ],
... [True, False],
... [True, True ]])
>>> s = COO.from_numpy(x)
>>> s2 = s.all(axis=1)
>>> s2.todense() # doctest: +SKIP
array([False, False, False, True])

You can also use the :code:`keepdims` argument to keep the dimensions after the
minimization.

>>> s3 = s.all(axis=1, keepdims=True)
>>> s3.shape
(4, 1)

By default, this reduces the array down to one boolean, minimizing along all axes.

>>> s.all()
False
"""
return np.logical_and.reduce(self, out=out, axis=axis, keepdims=keepdims)

def min(self, axis=None, keepdims=False, out=None):
"""
Minimize along the given axes. Uses all axes by default.
Expand Down Expand Up @@ -865,7 +973,7 @@ def min(self, axis=None, keepdims=False, out=None):
>>> s3.shape
(5, 1)

By default, this reduces the array down to one number, minimizing along all axes.
By default, this reduces the array down to one boolean, minimizing along all axes.

>>> s.min()
0
Expand Down
46 changes: 30 additions & 16 deletions sparse/tests/test_coo.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ def test_reductions(reduction, axis, keepdims, kwargs, eqkwargs):
assert_eq(xx, yy, **eqkwargs)


@pytest.mark.parametrize('reduction,kwargs,eqkwargs', [
('any', {}, {}),
('all', {}, {}),
])
@pytest.mark.parametrize('axis', [None, 0, 1, 2, (0, 2), -3, (1, -1)])
@pytest.mark.parametrize('keepdims', [True, False])
def test_reductions_bool(reduction, axis, keepdims, kwargs, eqkwargs):
x = sparse.random((2, 3, 4), density=.25).astype(bool)
y = x.todense()
xx = getattr(x, reduction)(axis=axis, keepdims=keepdims, **kwargs)
yy = getattr(y, reduction)(axis=axis, keepdims=keepdims, **kwargs)
assert_eq(xx, yy, **eqkwargs)


@pytest.mark.parametrize('reduction,kwargs,eqkwargs', [
(np.max, {}, {}),
(np.sum, {}, {}),
Expand Down Expand Up @@ -433,25 +447,25 @@ def test_trinary_broadcasting(shapes, func):

@pytest.mark.parametrize('shapes, func', [
([
(2,),
(3, 2),
(4, 3, 2),
], lambda x, y, z: (x + y) * z),
(2,),
(3, 2),
(4, 3, 2),
], lambda x, y, z: (x + y) * z),
([
(3,),
(2, 3),
(2, 2, 3),
], lambda x, y, z: x * (y + z)),
(3,),
(2, 3),
(2, 2, 3),
], lambda x, y, z: x * (y + z)),
([
(2,),
(2, 2),
(2, 2, 2),
], lambda x, y, z: x * y * z),
(2,),
(2, 2),
(2, 2, 2),
], lambda x, y, z: x * y * z),
([
(4,),
(4, 4),
(4, 4, 4),
], lambda x, y, z: x + y + z),
(4,),
(4, 4),
(4, 4, 4),
], lambda x, y, z: x + y + z),
])
@pytest.mark.parametrize('value', [
np.nan,
Expand Down