2019-03-04 20:12:22 -05:00
|
|
|
|
|
|
|
jax.ops package
|
|
|
|
=================
|
|
|
|
|
|
|
|
.. currentmodule:: jax.ops
|
|
|
|
|
|
|
|
.. automodule:: jax.ops
|
|
|
|
|
2021-05-10 17:44:18 -04:00
|
|
|
.. _syntactic-sugar-for-ops:
|
2019-03-04 20:12:22 -05:00
|
|
|
|
|
|
|
Indexed update operators
|
|
|
|
------------------------
|
|
|
|
|
2021-05-10 17:44:18 -04:00
|
|
|
JAX is intended to be used with a functional style of programming, and
|
2019-07-20 14:40:31 +01:00
|
|
|
does not support NumPy-style indexed assignment directly. Instead, JAX provides
|
2021-05-10 17:44:18 -04:00
|
|
|
alternative pure functional operators for indexed updates to arrays.
|
|
|
|
|
|
|
|
JAX array types have a property ``at``, which can be used as
|
|
|
|
follows (where ``idx`` is a NumPy index expression).
|
|
|
|
|
|
|
|
========================= ===================================================
|
|
|
|
Alternate syntax Equivalent in-place expression
|
|
|
|
========================= ===================================================
|
2021-09-24 09:59:10 -04:00
|
|
|
``x.at[idx].get()`` ``x[idx]``
|
2021-05-10 17:44:18 -04:00
|
|
|
``x.at[idx].set(y)`` ``x[idx] = y``
|
|
|
|
``x.at[idx].add(y)`` ``x[idx] += y``
|
|
|
|
``x.at[idx].multiply(y)`` ``x[idx] *= y``
|
|
|
|
``x.at[idx].divide(y)`` ``x[idx] /= y``
|
|
|
|
``x.at[idx].power(y)`` ``x[idx] **= y``
|
|
|
|
``x.at[idx].min(y)`` ``x[idx] = np.minimum(x[idx], y)``
|
|
|
|
``x.at[idx].max(y)`` ``x[idx] = np.maximum(x[idx], y)``
|
|
|
|
========================= ===================================================
|
|
|
|
|
|
|
|
None of these expressions modify the original `x`; instead they return
|
2021-09-01 20:43:13 -07:00
|
|
|
a modified copy of `x`. However, inside a :py:func:`jit` compiled function,
|
2021-08-11 17:32:36 -04:00
|
|
|
expressions like ``x = x.at[idx].set(y)`` are guaranteed to be applied in-place.
|
|
|
|
|
|
|
|
By default, JAX assumes that all indices are in-bounds. There is experimental
|
|
|
|
support for giving more precise semantics to out-of-bounds indexed accesses,
|
|
|
|
via the ``mode`` parameter to functions such as ``get`` and ``set``. Valid
|
|
|
|
values for ``mode`` include ``"clip"``, which means that out-of-bounds indices
|
|
|
|
will be clamped into range, and ``"fill"``/``"drop"``, which are aliases and
|
|
|
|
mean that out-of-bounds reads will be filled with a scalar ``fill_value``,
|
|
|
|
and out-of-bounds writes will be discarded.
|
2021-05-10 17:44:18 -04:00
|
|
|
|
|
|
|
|
|
|
|
Indexed update functions (deprecated)
|
|
|
|
-------------------------------------
|
|
|
|
|
|
|
|
The following functions are aliases for the ``x.at[idx].set(y)``
|
2021-08-11 17:32:36 -04:00
|
|
|
style operators. Use the ``x.at[idx]`` operators instead.
|
2019-03-04 20:12:22 -05:00
|
|
|
|
|
|
|
.. autosummary::
|
|
|
|
:toctree: _autosummary
|
|
|
|
|
|
|
|
index
|
|
|
|
index_update
|
|
|
|
index_add
|
2020-04-13 16:16:34 -04:00
|
|
|
index_mul
|
2019-06-23 15:32:43 -07:00
|
|
|
index_min
|
|
|
|
index_max
|
2019-07-20 14:40:31 +01:00
|
|
|
|
2020-04-17 01:30:06 +00:00
|
|
|
|
|
|
|
|
2019-07-20 14:40:31 +01:00
|
|
|
Other operators
|
|
|
|
---------------
|
|
|
|
|
|
|
|
.. autosummary::
|
|
|
|
:toctree: _autosummary
|
|
|
|
|
2021-04-09 12:06:51 -07:00
|
|
|
segment_max
|
|
|
|
segment_min
|
|
|
|
segment_prod
|
2019-05-08 21:18:17 -04:00
|
|
|
segment_sum
|