|
1 | 1 | __all__ = [
|
| 2 | + "bincount", |
2 | 3 | "cumulative_sum",
|
3 | 4 | "cumulative_prod",
|
4 | 5 | "max",
|
|
14 | 15 | from ._types import Optional, Tuple, Union, array, dtype
|
15 | 16 |
|
16 | 17 |
|
| 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 | + |
17 | 57 | def cumulative_prod(
|
18 | 58 | x: array,
|
19 | 59 | /,
|
|
0 commit comments