MCPcopy Create free account
hub / github.com/PythonOT/POT / roll_cols

Function roll_cols

ot/lp/solver_1d.py:402–441  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

400
401
402def 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
444def derivative_cost_on_circle(theta, u_values, v_values, u_cdf, v_cdf, p=2):

Callers 2

ot_cost_on_circleFunction · 0.85

Calls 5

get_backendFunction · 0.85
tileMethod · 0.45
reshapeMethod · 0.45
arangeMethod · 0.45
take_along_axisMethod · 0.45

Tested by

no test coverage detected