DOC: explicitly state that `count_nonzero` with `axis=()` is unspecified.

Open
#937 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
48/100
Issue type
Documentation
Clarity
Mostly clear
Activity status
Stale
Tech stack
numpy, pytorch
Domain
documentation

Research direction

Start with the count_nonzero specification at the linked API page, then identify the other reduction-like functions that accept tuple values for axis. Update the relevant documentation to state that axis=() behavior is unspecified and implementation-defined, and verify that the wording covers the intended functions.

Written by the indexing model from the issue text.

Description

The current docs [1] only state that If a tuple of integers, the number of non-zero values must be computed over multiple axes.

[1] https://data-apis.org/array-api/draft/API_specification/generated/array_api.count_nonzero.html

While one could argue that an empty tuple is not a tuple of integers, it could still be worth it to explicitly state that the behavior with axis=() is unspecificed and thus implementation-defined.

IRL, it is implementation-defined:

In [5]: a = np.arange(3*4*5).reshape(3, 4, 5)

In [6]: np.count_nonzero(a, axis=()).shape
Out[6]: (3, 4, 5)

In [7]: torch.count_nonzero(torch.as_tensor(a), dim=())
Out[7]: tensor(59)

In fact, it'd be best to add the same phrasing to all reduction-like functions which accept tuples for axis.

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.