def derive_by_array(expr, dx):
 from sympy.matrices import MatrixBase
 array_types = (collections.Iterable, MatrixBase, NDimArray)
 if isinstance(dx, array_types):
  dx = ImmutableDenseNDimArray(dx)
  for i in dx:
   if (not i._diff_wrt):
    raise ValueError('cannot derive by this array')
 if isinstance(expr, array_types):
  expr = ImmutableDenseNDimArray(expr)
  if isinstance(dx, array_types):
   new_array = [[y.diff(x) for y in expr] for x in dx]
   return type(expr)(new_array, (dx.shape + expr.shape))
  else:
   return expr.diff(dx)
 elif isinstance(dx, array_types):
  return ImmutableDenseNDimArray([expr.diff(i) for i in dx], dx.shape)
 else:
  return diff(expr, dx)

def derive_by_array ( expr , dx ) : from sympy . matrices import MatrixBase array_types = ( collections . Iterable , MatrixBase , NDimArray ) if isinstance ( dx , array_types ) : dx = ImmutableDenseNDimArray ( dx ) for i in dx : if ( not i . _diff_wrt ) : raise ValueError ( 'cannot derive by this array' ) if isinstance ( expr , array_types ) : expr = ImmutableDenseNDimArray ( expr ) if isinstance ( dx , array_types ) : new_array = [ [ y . diff ( x ) for y in expr ] for x in dx ] return type ( expr ) ( new_array , ( dx . shape + expr . shape ) ) else : return expr . diff ( dx ) elif isinstance ( dx , array_types ) : return ImmutableDenseNDimArray ( [ expr . diff ( i ) for i in dx ] , dx . shape ) else : return diff ( expr , dx )
derivative by arrays .