def permutedims(expr, perm):
 if (not isinstance(expr, NDimArray)):
  raise TypeError('expression has to be an N-dim array')
 from sympy.combinatorics import Permutation
 if (not isinstance(perm, Permutation)):
  perm = Permutation(list(perm))
 if (perm.size != expr.rank()):
  raise ValueError('wrong permutation size')
 iperm = (~ perm)
 indices_span = perm([range(i) for i in expr.shape])
 new_array = ([None] * len(expr))
 for (i, idx) in enumerate(itertools.product(*indices_span)):
  t = iperm(idx)
  new_array[i] = expr[t]
 new_shape = perm(expr.shape)
 return expr.func(new_array, new_shape)

def permutedims ( expr , perm ) : if ( not isinstance ( expr , NDimArray ) ) : raise TypeError ( 'expression has to be an N-dim array' ) from sympy . combinatorics import Permutation if ( not isinstance ( perm , Permutation ) ) : perm = Permutation ( list ( perm ) ) if ( perm . size != expr . rank ( ) ) : raise ValueError ( 'wrong permutation size' ) iperm = ( ~ perm ) indices_span = perm ( [ range ( i ) for i in expr . shape ] ) new_array = ( [ None ] * len ( expr ) ) for ( i , idx ) in enumerate ( itertools . product ( * indices_span ) ) : t = iperm ( idx ) new_array [ i ] = expr [ t ] new_shape = perm ( expr . shape ) return expr . func ( new_array , new_shape )
permutes the indices of an array .