Skip to content

Add unique_counts() and fix the description of unique_all() #317

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 5 commits into from
Nov 8, 2021
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
43 changes: 41 additions & 2 deletions spec/API_specification/set_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ A conforming implementation of the array API standard must provide and support t
The shapes of two of the output arrays for this function depend on the data values in the input array; hence, array libraries which build computation graphs (e.g., JAX, Dask, etc.) may find this function difficult to implement without knowing array values. Accordingly, such libraries may choose to omit this function. See {ref}`data-dependent-output-shapes` section for more details.
:::

Returns the unique elements of an input array `x`.
Returns the unique elements of an input array `x`, the first occurring indices for each unique element in `x`, the indices from the set of unique elements that reconstruct `x`, and the corresponding counts for each unique element in `x`.

```{note}
Uniqueness should be determined based on value equality (i.e., `x_i == x_j`). For input arrays having floating-point data types, value-based equality implies the following behavior.
Expand Down Expand Up @@ -55,6 +55,45 @@ Each `nan` value should have a count of one, while the counts for signed zeros s
The order of unique elements is not specified and may vary between implementations.
```

(function-unique-counts)=
### unique_counts(x, /)

:::{admonition} Data-dependent output shape
:class: important

The shapes of two of the output arrays for this function depend on the data values in the input array; hence, array libraries which build computation graphs (e.g., JAX, Dask, etc.) may find this function difficult to implement without knowing array values. Accordingly, such libraries may choose to omit this function. See {ref}`data-dependent-output-shapes` section for more details.
:::

Returns the unique elements of an input array `x` and the corresponding counts for each unique element in `x`.

```{note}
Uniqueness should be determined based on value equality (i.e., `x_i == x_j`). For input arrays having floating-point data types, value-based equality implies the following behavior.

- As `nan` values compare as `False`, `nan` values should be considered distinct.
- As `-0` and `+0` compare as `True`, signed zeros should not be considered distinct, and the corresponding unique element will be implementation-dependent (e.g., an implementation could choose to return `-0` if `-0` occurs before `+0`).

Each `nan` value should have a count of one, while the counts for signed zeros should be aggregated as a single count.
```

#### Parameters

- **x**: _<array>_

- input array. If `x` has more than one dimension, the function must flatten `x` and return the unique elements of the flattened array.

#### Returns

- **out**: _Tuple\[ <array>, <array> ]_

- a namedtuple `(values, counts)` whose

- first element must have the field name `values` and must be an array containing the unique elements of `x`. The array must have the same data type as `x`.
- second element must have the field name `counts` and must be an array containing the number of times each unique element occurs in `x`. The returned array must have same shape as `values` and must have the default integer data type.

```{note}
The order of unique elements is not specified and may vary between implementations.
```

(function-unique-inverse)=
### unique_inverse(x, /)

Expand Down Expand Up @@ -126,4 +165,4 @@ Uniqueness should be determined based on value equality (i.e., `x_i == x_j`). Fo

```{note}
The order of unique elements is not specified and may vary between implementations.
```
```