`diff` to allow Python scalar or 0d array `append` and `prepend`?

Open
#930 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
api

Research direction

Start with the linked diff specification and compare its append/prepend requirements with the array-api-strict behavior shown in the examples. Determine whether Python scalars and 0d arrays should be permitted, then align the specification and implementation with matching tests; the issue names no implementation files or tests.

Written by the indexing model from the issue text.

Description

The specification of diff requires append and prepend to be arrays with the same shape as the first argument except along axis.

array-api-strict currently does not:

import array_api_strict as xp
xp.diff(xp.ones((2, 3)), append=xp.asarray(10), axis=0)
# Array([[0., 0., 0.],
#        [9., 9., 9.]], dtype=array_api_strict.float64)
xp.diff(xp.ones((2, 3)), append=xp.asarray(10), axis=1)
# Array([[0., 0., 9.],
#        [0., 0., 9.]], dtype=array_api_strict.float64)

Like NumPy, CuPy, and JAX, it expands the value as necessary (and follows the same promotion rules as other functions. I see that following the usual promotion rules was already tabled, so I won't bring that up again right now.)

This suggest that the operation can be well-defined, and I think it can be useful (e.g. prepend 0 and append shape[axis]). It would be even more useful to accept Python scalars, following the example of clip (which also requires array min/max to have the same type as x, but allows Python scalars). This would avoid requiring the user to do something like:

append = xp.full(x.shape[:axis] + (1,) + x.shape[axis:], append, dtype=xp.result_type(x, prepend, append))
Dominant language
Python
Stars
281
Forks
52
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from data-apis/array-api

All issues in data-apis/array-api

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.