Skip to content

searchsorted needs to broadcast batch shapes #1014

Open

Description

@mdhaber

Ran into this in scipy/scipy#26324:

import numpy as np
from scipy._external import array_api_extra as xpx

rng = np.random.default_rng(2398198374572881)
shape_1d = (5,)
shape_2d = (2, 5)

x = np.sort(rng.random(shape_1d), axis=-1)
y = rng.random(shape_2d)
xpx.searchsorted(x, y)  # OK

x = np.sort(rng.random(shape_2d), axis=-1)
y = rng.random(shape_1d)
xpx.searchsorted(x, y)  # not OK
# ValueError: input operand has more dimensions than allowed by the axis remapping

My workaround there was to broadcast the batch shapes (e.g. batch_shape = xp.broadcast_shapes(x1.shape[:-1], x2.shape[:-1])) and reshape the arguments accordingly.

Activity

  1. lucascolley commented on Oct 7, 2026

    @lucascolley
    Member

    PR welcome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions