def reshape_axes(axes, shape, newshape):
 if (len(axes) != len(shape)):
  raise ValueError('axes do not match shape')
 if (product(shape) != product(newshape)):
  raise ValueError(('can not reshape %s to %s' % (shape, newshape)))
 if ((not axes) or (not newshape)):
  return ''
 lendiff = max(0, (len(shape) - len(newshape)))
 if lendiff:
  newshape = (newshape + ((1,) * lendiff))
 i = (len(shape) - 1)
 prodns = 1
 prods = 1
 result = []
 for ns in newshape[::(-1)]:
  prodns *= ns
  while ((i > 0) and (shape[i] == 1) and (ns != 1)):
   i -= 1
  if ((ns == shape[i]) and (prodns == (prods * shape[i]))):
   prods *= shape[i]
   result.append(axes[i])
   i -= 1
  else:
   result.append('Q')
 return ''.join(reversed(result[lendiff:]))

def reshape_axes ( axes , shape , newshape ) : if ( len ( axes ) != len ( shape ) ) : raise ValueError ( 'axes do not match shape' ) if ( product ( shape ) != product ( newshape ) ) : raise ValueError ( ( 'can not reshape %s to %s' % ( shape , newshape ) ) ) if ( ( not axes ) or ( not newshape ) ) : return '' lendiff = max ( 0 , ( len ( shape ) - len ( newshape ) ) ) if lendiff : newshape = ( newshape + ( ( 1 , ) * lendiff ) ) i = ( len ( shape ) - 1 ) prodns = 1 prods = 1 result = [ ] for ns in newshape [ : : ( - 1 ) ] : prodns *= ns while ( ( i > 0 ) and ( shape [ i ] == 1 ) and ( ns != 1 ) ) : i -= 1 if ( ( ns == shape [ i ] ) and ( prodns == ( prods * shape [ i ] ) ) ) : prods *= shape [ i ] result . append ( axes [ i ] ) i -= 1 else : result . append ( 'Q' ) return '' . join ( reversed ( result [ lendiff : ] ) )
return axes matching new shape .