Skip to content
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

DOC: Better doc for jnp.heaviside #24113

Merged
merged 1 commit into from
Oct 4, 2024
Merged
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
41 changes: 40 additions & 1 deletion jax/_src/numpy/ufuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3122,9 +3122,48 @@ def isnan(x: ArrayLike, /) -> Array:
return lax.ne(x, x)


@implements(np.heaviside, module='numpy')
@jit
def heaviside(x1: ArrayLike, x2: ArrayLike, /) -> Array:
r"""Compute the heaviside step function.

JAX implementation of :obj:`numpy.heaviside`.

The heaviside step function is defined by:

.. math::

\mathrm{heaviside}(x1, x2) = \begin{cases}
0., & x < 0\\
x2, & x = 0\\
1., & x > 0.
\end{cases}

Args:
x1: input array or scalar. ``complex`` dtype are not supported.
x2: scalar or array. Specifies the return values when ``x1`` is ``0``. ``complex``
dtype are not supported. ``x1`` and ``x2`` must either have same shape or
broadcast compatible.

Returns:
An array containing the heaviside step function of ``x1``, promoting to
inexact dtype.

Examples:
>>> x1 = jnp.array([[-2, 0, 3],
... [5, -1, 0],
... [0, 7, -3]])
>>> x2 = jnp.array([2, 0.5, 1])
>>> jnp.heaviside(x1, x2)
Array([[0. , 0.5, 1. ],
[1. , 0. , 1. ],
[2. , 1. , 0. ]], dtype=float32)
>>> jnp.heaviside(x1, 0.5)
Array([[0. , 0.5, 1. ],
[1. , 0. , 0.5],
[0.5, 1. , 0. ]], dtype=float32)
>>> jnp.heaviside(-3, x2)
Array([0., 0., 0.], dtype=float32)
"""
check_arraylike("heaviside", x1, x2)
x1, x2 = promote_dtypes_inexact(x1, x2)
zero = _lax_const(x1, 0)
Expand Down