Skip to content

Commit ba41463

Browse files
committed
feat: add bincount to the specification
Closes: #812
1 parent 4ddeaca commit ba41463

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

spec/draft/API_specification/statistical_functions.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Objects in API
1818
:toctree: generated
1919
:template: method.rst
2020

21+
bincount
2122
cumulative_prod
2223
cumulative_sum
2324
max

src/array_api_stubs/_draft/statistical_functions.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
__all__ = [
2+
"bincount",
23
"cumulative_sum",
34
"cumulative_prod",
45
"max",
@@ -14,6 +15,45 @@
1415
from ._types import Optional, Tuple, Union, array, dtype
1516

1617

18+
def bincount(
19+
x: array, /, weights: Optional[array] = None, *, minlength: int = 0
20+
) -> array:
21+
"""
22+
Counts the number of occurrences of each element in ``x``.
23+
24+
.. admonition:: Data-dependent output shape
25+
:class: important
26+
27+
The shape of the output array for this function depends on the data values in ``x``; hence, array libraries which build computation graphs (e.g., JAX, Dask, etc.) can find this function difficult to implement without knowing the values in ``x``. Accordingly, such libraries **may** choose to omit this function. See :ref:`data-dependent-output-shapes` section for more details.
28+
29+
Parameters
30+
----------
31+
x: array
32+
input array. **Should** be a one-dimensional array. **Must** have an integer data type.
33+
weights: Optional[array]
34+
an array of weights for each element in ``x``. **Must** have the same shape as ``x``. **Must** have a numeric data type. If not provided, each bin in the returned array **must** give the number of occurrences of its index value in ``x``. If provided, each bin in the returned array **must** be a sum of the weights corresponding to the respective index values in ``x`` (i.e., if value ``n`` is found at index ``i`` in ``x``, then ``out[n] += weights[i]``, instead of ``out[n] += 1``). Default: ``None``.
35+
minlength: int
36+
minimum number of bins. **Must** be a nonnegative integer. Default: ``0``.
37+
38+
Returns
39+
-------
40+
out: array
41+
an array containing the number of occurrences. Let ``N`` equal ``max(xp.max(x)+1, minlength)``. The returned array **should** have shape ``(N,)``.
42+
43+
If ``weights`` is not ``None``, the returned array **must** have the same data type as ``weights``.
44+
45+
If ``weights`` is ``None``, the returned array **must** have the same data type as ``x``, unless ``x`` has an integer data type supporting a smaller range of values than the default integer data type (e.g., ``x`` has an ``int16`` or ``uint32`` data type and the default integer data type is ``int64``). In those latter cases:
46+
47+
- if ``x`` has a signed integer data type (e.g., ``int16``), the returned array **must** have the default integer data type.
48+
- if ``x`` has an unsigned integer data type (e.g., ``uint16``), the returned array **must** have an unsigned integer data type having the same number of bits as the default integer data type (e.g., if the default integer data type is ``int32``, the returned array **must** have a ``uint32`` data type).
49+
50+
Notes
51+
-----
52+
53+
- If ``x`` contains negative values, behavior is unspecified and thus implementation-defined.
54+
"""
55+
56+
1757
def cumulative_prod(
1858
x: array,
1959
/,

0 commit comments

Comments
 (0)