Skip to content
fn

searchsorted

→Tensor
searchsorted(sorted_sequence: Tensor, input: Tensor | Scalar, out_int32: _bool = ..., right: _bool = ..., side: str | None = ..., sorter: Tensor | None = ...)

Implementing kernel

C++ engine symbols that back this Python API.

Find insertion points to maintain order in a sorted sequence.

For each v = input[i] returns the smallest j such that v < sorted_sequence[j] (or <= when right=True). Equivalent to per-element binary search.

Parameters

sorted_sequenceTensor
1-D sorted sequence of boundaries.
inputTensor | Scalar
Query values (arbitrary shape). A Python number is searched as a 0-d value on the sequence's device. The two operands are promoted to their common dtype, as the reference does: a bool beside an integer, or an integer beside a float, is searched at the wider dtype.
rightbool= False
Use <= rather than < for the comparison.

Returns

Tensor

int64 tensor of insertion indices with the same shape as input.

Notes

Sister function bucketize reverses the argument order (values first).

Examples

>>> import lucid
>>> seq = lucid.tensor([1.0, 3.0, 5.0])
>>> lucid.searchsorted(seq, lucid.tensor([0.5, 2.0, 4.0, 6.0]))
Tensor([0, 1, 2, 3])