Skip to content

RFC: add diff for computing the n-th forward difference #784

Closed
@kgryte

Description

@kgryte
Contributor

This RFC proposes the addition of a new API in the array API specification for computing the n-th forward difference along a specified dimension.

Overview

Based on array comparison data, the API is available across all major array libraries in the PyData ecosystem.

diff was originally discussed in #187 as a potential standardization candidate and has been requested by downstream libraries, such as xarray.

Prior art

Proposal

def diff(x: array, /, *, n: int = 1, axis: int = -1, prepend = Optional[array], append = Optional[array]) -> array

Questions

  • NumPy supports prepend and append as scalar values and subsequently wraps using asarray. Are we okay limiting to only arrays within the specification? Libraries, such as NumPy, would be free to accept scalars; this would just not be considered portable behavior.
  • Apart from scalars, NumPy requires that prepend and append arrays match the shape of x except along the specified axis, thus precluding broadcasting. Is there ever a situation in which broadcasting would make sense?
  • The output array must have the same dtype as the input array. Consequently, when x has a boolean dtype, the output array must also have a boolean dtype. Similarly, unsigned integer input arrays result in unsigned integer output arrays. Are we okay requiring that diff support boolean and unsigned integer dtypes? Or should we limit portable behavior to floating-point (real and complex) and signed integers?

Activity

added
RFCRequest for comments. Feature requests and proposed changes.
on Apr 4, 2024
added this to the v2024 milestone on Apr 4, 2024
rgommers

rgommers commented on Apr 4, 2024

@rgommers
Member

Are we okay limiting to only arrays within the specification?

That seems fine to me. prepend/append are rarely used, so there doesn't seem a need to make this really flexible.

Is there ever a situation in which broadcasting would make sense?

I don't see it in SciPy, nor can I think of a real need for this.

Or should we limit portable behavior to floating-point (real and complex) and signed integers?

This sounds good to me.

added a commit that references this issue on Apr 18, 2024
added
AcceptedRFC feature request which has been accepted.
and removed on Apr 18, 2024
added a commit that references this issue on Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    AcceptedRFC feature request which has been accepted.RFCRequest for comments. Feature requests and proposed changes.

    Type

    No type

    Projects

    Status

    Stage 1

    Relationships

    None yet

      Development

      Participants

      @rgommers@kgryte

      Issue actions

        RFC: add `diff` for computing the n-th forward difference · Issue #784 · data-apis/array-api