def round_core(core, mode, rank): new_dims = [mode] + [m for m in range(core.ndim) if m != mode] mode_dim = core.shape[mode] flat_core = core.transpose(new_dims).reshape((mode_dim, -1), order='F') u, s, vt = np.linalg.svd(flat_core, full_matrices=False) rfactor = u[:, :rank] new_core = (np.ascontiguousarray( s[:rank, np.newaxis] * vt[:rank, :]).reshape( rank, *[core.shape[i] for i in new_dims[1:]], order='F').transpose(inverse_permutation(np.array(new_dims)))) return rfactor, new_core
def return_order(a): return np.take(range(1, len(a) + 1), inverse_permutation(np.argsort(a)))