Skip to content

fix: address incorrect boundary conditions in searchsorted #898

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 6 commits into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
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
15 changes: 6 additions & 9 deletions src/array_api_stubs/_2023_12/searching_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,14 @@ def searchsorted(
side: Literal['left', 'right']
argument controlling which index is returned if a value lands exactly on an edge.

Let ``x`` be an array of rank ``N`` where ``v`` is an individual element given by ``v = x2[n,m,...,j]``.
Let ``v`` be an element of ``x2`` given by ``v = x2[j]``, where ``j`` refers to a valid index (see :ref:`indexing`).

If ``side == 'left'``, then
- If ``v`` is less than all elements in ``x1``, then ``out[j]`` must be ``0``.
- If ``v`` is greater than all elements in ``x1``, then ``out[j]`` must be ``M``, where ``M`` is the number of elements in ``x1``.
- Otherwise, each returned index ``i = out[j]`` must satisfy an index condition:

- each returned index ``i`` must satisfy the index condition ``x1[i-1] < v <= x1[i]``.
- if no index satisfies the index condition, then the returned index for that element must be ``0``.

Otherwise, if ``side == 'right'``, then

- each returned index ``i`` must satisfy the index condition ``x1[i-1] <= v < x1[i]``.
- if no index satisfies the index condition, then the returned index for that element must be ``N``, where ``N`` is the number of elements in ``x1``.
- If ``side == 'left'``, then ``x1[i-1] < v <= x1[i]``.
- If ``side == 'right'``, then ``x1[i-1] <= v < x1[i]``.

Default: ``'left'``.
sorter: Optional[array]
Expand Down
15 changes: 6 additions & 9 deletions src/array_api_stubs/_draft/searching_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,14 @@ def searchsorted(
side: Literal['left', 'right']
argument controlling which index is returned if a value lands exactly on an edge.

Let ``x`` be an array of rank ``N`` where ``v`` is an individual element given by ``v = x2[n,m,...,j]``.
Let ``v`` be an element of ``x2`` given by ``v = x2[j]``, where ``j`` refers to a valid index (see :ref:`indexing`).

If ``side == 'left'``, then
- If ``v`` is less than all elements in ``x1``, then ``out[j]`` must be ``0``.
- If ``v`` is greater than all elements in ``x1``, then ``out[j]`` must be ``M``, where ``M`` is the number of elements in ``x1``.
- Otherwise, each returned index ``i = out[j]`` must satisfy an index condition:

- each returned index ``i`` must satisfy the index condition ``x1[i-1] < v <= x1[i]``.
- if no index satisfies the index condition, then the returned index for that element must be ``0``.

Otherwise, if ``side == 'right'``, then

- each returned index ``i`` must satisfy the index condition ``x1[i-1] <= v < x1[i]``.
- if no index satisfies the index condition, then the returned index for that element must be ``N``, where ``N`` is the number of elements in ``x1``.
- If ``side == 'left'``, then ``x1[i-1] < v <= x1[i]``.
- If ``side == 'right'``, then ``x1[i-1] <= v < x1[i]``.

Default: ``'left'``.
sorter: Optional[array]
Expand Down