def inverse_interpolate(vals, ds):
    """
    Find ts such that lerp_sample(vals, ts) = ds
    """

    assert (ds >= vals.min()).all()
    assert (ds <= vals.max()).all()

    idx = (ds[:, None] <= vals[None]).argmax(axis=-1)
    m = idx > 0

    assert (vals[idx] >= ds).all()
    assert (vals[idx[m] - 1] < ds[m]).all()

    ts = np.zeros_like(ds)
    bucket_sizes = vals[idx[m]] - vals[idx[m] - 1]
    ts[m] = idx[m] - 1 + (ds[m] - vals[idx[m] - 1]) / bucket_sizes
    return ts

    Find ts such that lerp_sample(vals, ts) = ds
    
infinigen/core/util/math.py