Find indices where v should be inserted into a to maintain order. Args: a: tensor, the sorted reference points that we are scanning to see where v should lie. v: tensor, the query points that we are pretending to insert into a. Does not need to be sorted. All but the last dime
(a, v)
| 4 | |
| 5 | |
| 6 | def searchsorted(a, v): |
| 7 | """Find indices where v should be inserted into a to maintain order. |
| 8 | |
| 9 | Args: |
| 10 | a: tensor, the sorted reference points that we are scanning to see where v |
| 11 | should lie. |
| 12 | v: tensor, the query points that we are pretending to insert into a. Does |
| 13 | not need to be sorted. All but the last dimensions should match or expand |
| 14 | to those of a, the last dimension can differ. |
| 15 | |
| 16 | Returns: |
| 17 | (idx_lo, idx_hi), where a[idx_lo] <= v < a[idx_hi], unless v is out of the |
| 18 | range [a[0], a[-1]] in which case idx_lo and idx_hi are both the first or |
| 19 | last index of a. |
| 20 | """ |
| 21 | i = torch.arange(a.shape[-1], device=a.device) |
| 22 | v_ge_a = v[..., None, :] >= a[..., :, None] |
| 23 | idx_lo = torch.max(torch.where(v_ge_a, i[..., :, None], i[..., :1, None]), -2).values |
| 24 | idx_hi = torch.min(torch.where(~v_ge_a, i[..., :, None], i[..., -1:, None]), -2).values |
| 25 | return idx_lo, idx_hi |
| 26 | |
| 27 | |
| 28 | def query(tq, t, y, outside_value=0): |
no outgoing calls
no test coverage detected