File: operations.rst

package info (click to toggle)
python-sparse 0.16.0a9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,948 kB
  • sloc: python: 9,959; makefile: 8; sh: 3
file content (273 lines) | stat: -rw-r--r-- 8,892 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
.. currentmodule:: sparse

Operations on :obj:`COO` and :obj:`GCXS` arrays
===============================================

.. _operations-operators:

Operators
---------

:obj:`COO` and :obj:`GCXS` objects support a number of operations. They interact with scalars,
:doc:`Numpy arrays <numpy:reference/generated/numpy.ndarray>`, other :obj:`COO` and :obj:`GCXS` objects,
and :obj:`scipy.sparse.spmatrix` objects, all following standard Python and Numpy
conventions.

For example, the following Numpy expression produces equivalent
results for both Numpy arrays, COO arrays, or a mix of the two:

.. code-block:: python

   np.log(X.dot(beta.T) + 1)

However some operations are not supported, like operations that
implicitly cause dense structures, or numpy functions that are not
yet implemented for sparse arrays.

.. code-block:: python

   np.linalg.cholesky(x)  # sparse cholesky not implemented


This page describes those valid operations, and their limitations.

:obj:`elemwise`
~~~~~~~~~~~~~~~
This function allows you to apply any arbitrary broadcasting function to any number of arguments
where the arguments can be :obj:`SparseArray` objects or :obj:`scipy.sparse.spmatrix` objects.
For example, the following will add two arrays:

.. code-block:: python

   sparse.elemwise(np.add, x, y)


.. warning:: Previously, :obj:`elemwise` was a method of the :obj:`COO` class. Now,
   it has been moved to the :obj:`sparse` module.

.. _operations-auto-densification:

Auto-Densification
~~~~~~~~~~~~~~~~~~
Operations that would result in dense matrices, such as
operations with :doc:`Numpy arrays <numpy:reference/generated/numpy.ndarray>`
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.

.. code-block:: python

   x + 0
   x != y
   x + y
   x == 5
   5 * x
   x / 7.3
   x != 0
   x == 0
   ~x
   x + 5

We also support operations with a nonzero fill value. These are operations
that map zero values to nonzero values, such as :code:`x + 1` or :code:`~x`.
In these cases, they will produce an output with a fill value of :code:`1` or :code:`True`,
assuming the original array has a fill value of :code:`0` or :code:`False` respectively.

If densification is needed, it must be explicit. In other words, you must call
:obj:`SparseArray.todense` on the :obj:`SparseArray` object. If both operands are :obj:`SparseArray`,
both must be densified.

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`
--------------------------------------------
Certain operations with :obj:`scipy.sparse.spmatrix` are also supported.
For example, the following are all allowed if :code:`y` is a :obj:`scipy.sparse.spmatrix`:

.. code-block:: python

   x + y
   x - y
   x * y
   x > y
   x < y

In general, operating on a :code:`scipy.sparse.spmatrix` is the same as operating
on :obj:`COO` or :obj:`GCXS`, as long as it is to the right of the operator.

.. note:: Results are not guaranteed if :code:`x` is a :obj:`scipy.sparse.spmatrix`.
   For this reason, we recommend that all Scipy sparse matrices should be explicitly
   converted to :obj:`COO` or :obj:`GCXS` before any operations.


Broadcasting
------------
All binary operators support :doc:`broadcasting <numpy:user/basics.broadcasting>`.
This means that (under certain conditions) you can perform binary operations
on arrays with unequal shape. Namely, when the shape is missing a dimension,
or when a dimension is :code:`1`. For example, performing a binary operation
on two :obj:`COO` arrays with shapes :code:`(4,)` and :code:`(5, 1)` yields
an object of shape :code:`(5, 4)`. The same happens with arrays of shape
:code:`(1, 4)` and :code:`(5, 1)`. However, :code:`(4, 1)` and :code:`(5, 1)`
will raise a :obj:`ValueError`.

.. _operations-elemwise:

Element-wise Operations
-----------------------
:obj:`COO` and :obj:`GCXS` arrays support a variety of element-wise operations. However, as
with operators, operations that map zero to a nonzero value are not supported.

To illustrate, the following are all possible, and will produce another
:obj:`SparseArray`:

.. code-block:: python

   np.abs(x)
   np.sin(x)
   np.sqrt(x)
   np.conj(x)
   np.expm1(x)
   np.log1p(x)
   np.exp(x)
   np.cos(x)
   np.log(x)

As above, in the last three cases, an array with a nonzero fill value will be produced.

Notice that you can apply any unary or binary :doc:`numpy.ufunc <numpy:reference/ufuncs>` to :obj:`COO`
arrays, and :obj:`numpy.ndarray` objects and scalars and it will work so
long as the result is not dense. When applying to :obj:`numpy.ndarray` objects,
we check that operating on the array with zero would always produce a zero.

.. _operations-reductions:

Reductions
----------
:obj:`COO` and :obj:`GCXS` objects support a number of reductions. However, not all important
reductions are currently implemented (help welcome!) All of the following
currently work:

.. code-block:: python

   x.sum(axis=1)
   np.max(x)
   np.min(x, axis=(0, 2))
   x.prod()


:obj:`SparseArray.reduce`
~~~~~~~~~~~~~~~~~~~~~~~~~
This method can take an arbitrary :doc:`numpy.ufunc <numpy:reference/ufuncs>` and performs a
reduction using that method. For example, the following will perform
a sum:

.. code-block:: python

   x.reduce(np.add, axis=1)

.. note::
   This library currently performs reductions by grouping together all
   coordinates along the supplied axes and reducing those. Then, if the
   number in a group is deficient, it reduces an extra time with zero.
   As a result, if reductions can change by adding multiple zeros to
   it, this method won't be accurate. However, it works in most cases.

Partial List of Supported Reductions
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Although any binary :doc:`numpy.ufunc <numpy:reference/ufuncs>` should work for reductions, when calling
in the form :code:`x.reduction()`, the following reductions are supported:

* :obj:`COO.sum`
* :obj:`COO.max`
* :obj:`COO.min`
* :obj:`COO.prod`

.. _operations-indexing:

Indexing
--------
:obj:`COO` and :obj:`GCXS` arrays can be :obj:`indexed <numpy.doc.indexing>` just like regular
:obj:`numpy.ndarray` objects. They support integer, slice and boolean indexing.
However, currently, numpy advanced indexing is not properly supported. This
means that all of the following work like in Numpy, except that they will produce
:obj:`SparseArray` arrays rather than :obj:`numpy.ndarray` objects, and will produce
scalars where expected. Assume that :code:`z.shape` is :code:`(5, 6, 7)`

.. code-block:: python

   z[0]
   z[1, 3]
   z[1, 4, 3]
   z[:3, :2, 3]
   z[::-1, 1, 3]
   z[-1]

All of the following will raise an :obj:`IndexError`, like in Numpy 1.13 and later.

.. code-block:: python

   z[6]
   z[3, 6]
   z[1, 4, 8]
   z[-6]


Advanced Indexing
~~~~~~~~~~~~~~~~~

Advanced indexing (indexing arrays with other arrays) is supported, but only for indexing
with a *single array*. Indexing a single array with multiple arrays is not supported at
this time. As above, if  :code:`z.shape` is :code:`(5, 6, 7)`, all of the following will
work like NumPy:

.. code-block:: python

   z[[0, 1, 2]]
   z[1, [3]]
   z[1, 4, [3, 6]]
   z[:3, :2, [1, 5]]


Package Configuration
---------------------

By default, when performing something like ``np.array(COO)``, we allow the array
to be converted into a dense one. To prevent this and raise a :obj:`RuntimeError`
instead, set the environment variable ``SPARSE_AUTO_DENSIFY`` to ``0``.

If it is desired to raise a warning if creating a sparse array that takes no less
memory than an equivalent desne array, set the environment variable
``SPARSE_WARN_ON_TOO_DENSE`` to ``1``.

.. _operations-other:

Other Operations
----------------
:obj:`COO` and :obj:`GCXS` arrays support a number of other common operations. Among them are
:obj:`dot`, :obj:`tensordot`, :obj:`einsum`, :obj:`concatenate`
and :obj:`stack`, :obj:`transpose <COO.transpose>` and :obj:`reshape <COO.reshape>`.
You can view the full list on the :doc:`API reference page <generated/sparse>`.

.. note:: Some operations require zero fill-values (such as :obj:`nonzero <COO.nonzero>`)
   and others (such as :obj:`concatenate`) require that all inputs have consistent fill-values.
   For details, check the API reference.