Skip to content
fn

searchsorted_op

→TensorImplPtr
int searchsorted_op(const int & sorted_1d, const int & values, bool right)

Python wrappers

Public Python APIs implemented by this engine symbol.

Per-query insertion point into a sorted 1-D (or row-wise N-D) reference.

Evaluates each query via binary search in O(log⁡N)O(\log N) time and O(1)O(1) auxiliary memory per query, ordering NaN after +inf to match the reference. Not differentiable — integer output.

Math

Let ss be the sorted reference and vv the query tensor. Then

y…,j=∑i=0N−11 ⁣[si≺v…,j] y_{\ldots, j} = \sum_{i=0}^{N-1} \mathbb{1}\!\left[ s_i \prec v_{\ldots, j} \right]

with ≺\prec being << when right = false (leftmost insertion) and ≤\le when right = true (rightmost insertion).

See Also

bucketize_op — same operation with the argument order flipped.

Parameters

sorted_1dTensorImplPtr
1-D ascending-sorted reference of length NN, or an N-D tensor whose leading N−1N - 1 dimensions match values.
valuesTensorImplPtr
Query tensor of arbitrary shape S.
rightbool
If true, returns the rightmost (post-equal) insertion point; otherwise the leftmost.

Returns

TensorImplPtr

int64 tensor of shape S whose entries lie in [0,N][0, N].

Raises

Failure
If either input is null or the shapes are incompatible.