np.searchsorted
полезно для такой логики:
def radio_index2xy_v(radio_index, n_x, n_y):
sgn = np.array([0, 1, 1, -1, -1, 0])
col = np.array([-1, 1, 0, 1, 0, -1])
coeffs = np.array([[-1, -1],
[0, -1],
[-n_x, n_x - 1],
[n_y - 1, 2*n_x + n_y - 2],
[2*n_x + 2*n_y - 3, 0],
[-1, -1]])
cusps = np.cumsum([0, n_x, n_y-1, n_x-1, n_y-2])
idx = cusps.searchsorted(radio_index)
out = coeffs[idx]
out[np.arange(idx.size), col[idx]] += sgn[idx] * radio_index
return out
Демо-версия:
>>> radio_index2xy_v(np.arange(20), 5, 4)
array([[-1, -1],
[ 0, 0],
[ 0, 1],
[ 0, 2],
[ 0, 3],
[ 0, 4],
[ 1, 4],
[ 2, 4],
[ 3, 4],
[ 3, 3],
[ 3, 2],
[ 3, 1],
[ 3, 0],
[ 2, 0],
[ 1, 0],
[-1, -1],
[-1, -1],
[-1, -1],
[-1, -1],
[-1, -1]])