r""" Utils functions which allow to shift the order of each row of a 2d matrix Parameters ---------- M : ndarray, shape (nr, nc) Matrix to shift shifts: int or ndarray, shape (nr,) Returns ------- Shifted array Examples -------- >>> M = np.array
(M, shifts)
| 400 | |
| 401 | |
| 402 | def roll_cols(M, shifts): |
| 403 | r""" |
| 404 | Utils functions which allow to shift the order of each row of a 2d matrix |
| 405 | |
| 406 | Parameters |
| 407 | ---------- |
| 408 | M : ndarray, shape (nr, nc) |
| 409 | Matrix to shift |
| 410 | shifts: int or ndarray, shape (nr,) |
| 411 | |
| 412 | Returns |
| 413 | ------- |
| 414 | Shifted array |
| 415 | |
| 416 | Examples |
| 417 | -------- |
| 418 | >>> M = np.array([[1,2,3],[4,5,6],[7,8,9]]) |
| 419 | >>> roll_cols(M, 2) |
| 420 | array([[2, 3, 1], |
| 421 | [5, 6, 4], |
| 422 | [8, 9, 7]]) |
| 423 | >>> roll_cols(M, np.array([[1],[2],[1]])) |
| 424 | array([[3, 1, 2], |
| 425 | [5, 6, 4], |
| 426 | [9, 7, 8]]) |
| 427 | |
| 428 | References |
| 429 | ---------- |
| 430 | https://stackoverflow.com/questions/66596699/how-to-shift-columns-or-rows-in-a-tensor-with-different-offsets-in-pytorch |
| 431 | """ |
| 432 | nx = get_backend(M) |
| 433 | |
| 434 | n_rows, n_cols = M.shape |
| 435 | |
| 436 | arange1 = nx.tile( |
| 437 | nx.reshape(nx.arange(n_cols, type_as=shifts), (1, n_cols)), (n_rows, 1) |
| 438 | ) |
| 439 | arange2 = (arange1 - shifts) % n_cols |
| 440 | |
| 441 | return nx.take_along_axis(M, arange2, 1) |
| 442 | |
| 443 | |
| 444 | def derivative_cost_on_circle(theta, u_values, v_values, u_cdf, v_cdf, p=2): |
no test coverage detected