Skip to content

feat: add diff to specification #791

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 2 commits into from
Sep 19, 2024
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 spec/draft/API_specification/utility_functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ Objects in API

all
any
diff
44 changes: 43 additions & 1 deletion src/array_api_stubs/_draft/utility_functions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__all__ = ["all", "any"]
__all__ = ["all", "any", "diff"]


from ._types import Optional, Tuple, Union, array
Expand Down Expand Up @@ -84,3 +84,45 @@ def any(
.. versionchanged:: 2022.12
Added complex data type support.
"""


def diff(
x: array,
/,
*,
axis: int = -1,
n: int = 1,
prepend: Optional[array] = None,
append: Optional[array] = None,
) -> array:
"""
Calculates the n-th discrete forward difference along a specified axis.

Parameters
----------
x: array
input array. Should have a numeric data type.
axis: int
axis along which to compute differences. A valid ``axis`` must be an integer on the interval ``[-N, N)``, where ``N`` is the rank (number of dimensions) of ``x``. If an ``axis`` is specified as a negative integer, the function must determine the axis along which to compute differences by counting backward from the last dimension (where ``-1`` refers to the last dimension). If provided an invalid ``axis``, the function must raise an exception. Default: ``-1``.
n: int
number of times to recursively compute differences. Default: ``1``.
prepend: Optional[array]
values to prepend to a specified axis prior to computing differences. Must have the same shape as ``x``, except for the axis specified by ``axis`` which may have any size. Should have the same data type as ``x``. Default: ``None``.
append: Optional[array]
values to append to a specified axis prior to computing differences. Must have the same shape as ``x``, except for the axis specified by ``axis`` which may have any size. Should have the same data type as ``x``. Default: ``None``.

Returns
-------
out: array
an array containing the n-th differences. Should have the same data type as ``x``. Must have the same shape as ``x``, except for the axis specified by ``axis`` which must have a size determined as follows:

- Let ``M`` be the number of elements along an axis specified by ``axis``.
- Let ``N1`` be the number of prepended values along an axis specified by ``axis``.
- Let ``N2`` be the number of appended values along an axis specified by ``axis``.
- The final size of the axis specified by ``axis`` must be ``M + N1 + N2 - n``.

Notes
-----

- The first-order differences are given by ``out[i] = x[i+1] - x[i]`` along a specified axis. Higher-order differences must be calculated recursively (e.g., by calling ``diff(out, axis=axis, n=n-1)``).
"""
Loading