aesara.tensor.searchsorted(x, v, side='left', sorter=None)[source]#

Find indices where elements should be inserted to maintain order.

This wraps numpy.searchsorted. Find the indices into a sorted array x such that, if the corresponding elements in v were inserted before the indices, the order of x would be preserved.

  • x (1-D tensor (array-like)) – Input array. If sorter is None, then it must be sorted in ascending order, otherwise sorter must be an array of indices which sorts it.

  • v (tensor (array-like)) – Contains the values to be inserted into x.

  • side ({'left', 'right'}, optional.) – If 'left' (default), the index of the first suitable location found is given. If 'right', return the last such index. If there is no suitable index, return either 0 or N (where N is the length of x).

  • sorter (1-D tensor of integers (array-like), optional) – Contains indices that sort array x into ascending order. They are typically the result of argsort.


indices – Array of insertion points with the same shape as v.

Return type:

tensor of integers (int64)


  • Binary search is used to find the required insertion points.

  • This Op is working only on CPU currently.


>>> from aesara import tensor as at
>>> from aesara.tensor import extra_ops
>>> x = at.dvector()
>>> idx = x.searchsorted(3)
>>> idx.eval({x: [1,2,3,4,5]})
>>> extra_ops.searchsorted([1,2,3,4,5], 3).eval()
>>> extra_ops.searchsorted([1,2,3,4,5], 3, side='right').eval()
>>> extra_ops.searchsorted([1,2,3,4,5], [-10, 10, 2, 3]).eval()
array([0, 5, 1, 2])

New in version 0.9.