Skip to content

Add support for mixed ndarray-sparse element-wise operations. #182

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 3 commits into from
Sep 12, 2018
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
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Changelog
=========

* :feature:`124` (via :issue:`182`) Allow mixed :code:`ndarray`-:obj:`COO` operations if the result is sparse.
* :feature:`179` (via :issue:`180`) Allow specifying a fill-value when converting from NumPy arrays.
* :feature:`175` Added :code:`COO.any` and :code:`COO.all` methods.
* :feature:`172` Indexing for :code:`COO` now accepts a single one-dimensional array index.
Expand Down
23 changes: 20 additions & 3 deletions docs/operations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ Auto-Densification
~~~~~~~~~~~~~~~~~~
Operations that would result in dense matrices, such as
operations with :doc:`Numpy arrays <reference/generated/numpy.ndarray>`
objects a :obj:`ValueError`.
raises a :obj:`ValueError`. For example, the following will raise a
:obj:`ValueError` if :code:`x` is a :obj:`numpy.ndarray`:

.. code-block:: python

x + y

However, all of the following are valid operations.

Expand All @@ -77,9 +82,21 @@ If densification is needed, it must be explicit. In other words, you must call
:obj:`COO.todense` on the :obj:`COO` object. If both operands are :obj:`COO`,
both must be densified.

.. warning:: Previously, operations with Numpy arrays were sometimes supported. Now,
it is necessary to convert Numpy arrays to :obj:`COO` objects.
Operations with NumPy arrays
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In certain situations, operations with NumPy arrays are also supported. For example,
the following will work if :code:`x` is :obj:`COO` and :code:`y` is a NumPy array:

.. code-block:: python

x * y

The following conditions must be met when performing element-wise operations with
NumPy arrays:

* The operation must produce a consistent fill-values. In other words, the resulting
array must also be sparse.
* Operating on the NumPy arrays must not increase the size when broadcasting the arrays.

Operations with :obj:`scipy.sparse.spmatrix`
--------------------------------------------
Expand Down
5 changes: 5 additions & 0 deletions sparse/coo/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1317,6 +1317,11 @@ def reshape(self, shape):
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]])
"""
if isinstance(shape, Iterable):
shape = tuple(shape)
else:
shape = (shape,)

if self.shape == shape:
return self
if any(d == -1 for d in shape):
Expand Down
Loading