def _roll_vectorized(M, roll_indices, axis):
 assert (axis in [0, 1])
 ndim = M.ndim
 assert (ndim == 3)
 ndim_roll = roll_indices.ndim
 assert (ndim_roll == 1)
 sh = M.shape
 (r, c) = sh[(-2):]
 assert (sh[0] == roll_indices.shape[0])
 vec_indices = np.arange(sh[0], dtype=np.int32)
 M_roll = np.empty_like(M)
 if (axis == 0):
  for ir in range(r):
   for ic in range(c):
    M_roll[:, ir, ic] = M[(vec_indices, (((- roll_indices) + ir) % r), ic)]
 elif (axis == 1):
  for ir in range(r):
   for ic in range(c):
    M_roll[:, ir, ic] = M[(vec_indices, ir, (((- roll_indices) + ic) % c))]
 return M_roll

def _roll_vectorized ( M , roll_indices , axis ) : assert ( axis in [ 0 , 1 ] ) ndim = M . ndim assert ( ndim == 3 ) ndim_roll = roll_indices . ndim assert ( ndim_roll == 1 ) sh = M . shape ( r , c ) = sh [ ( - 2 ) : ] assert ( sh [ 0 ] == roll_indices . shape [ 0 ] ) vec_indices = np . arange ( sh [ 0 ] , dtype = np . int32 ) M_roll = np . empty_like ( M ) if ( axis == 0 ) : for ir in range ( r ) : for ic in range ( c ) : M_roll [ : , ir , ic ] = M [ ( vec_indices , ( ( ( - roll_indices ) + ir ) % r ) , ic ) ] elif ( axis == 1 ) : for ir in range ( r ) : for ic in range ( c ) : M_roll [ : , ir , ic ] = M [ ( vec_indices , ir , ( ( ( - roll_indices ) + ic ) % c ) ) ] return M_roll
rolls an array of matrices along an axis according to an array of indices *roll_indices* *axis* can be either 0 or 1 .