def _(expr, args): if all(i.is_commutative for i in args): _addsort(args) _eval_numbers(expr, args) return expr.func(*args, evaluate=False) else: return expr._new_rawargs(*args)
def __new__(cls, *args, **kwargs): # Here, often we get `evaluate=False` to prevent SymPy evaluation (e.g., # when `cls==EvalDerivative`), but in all cases we at least apply a small # set of basic simplifications # (a+b)+c -> a+b+c (flattening) nested, others = split(args, lambda e: isinstance(e, Add)) args = flatten(e.args for e in nested) + list(others) # a+0 -> a args = [i for i in args if i != 0] # Reorder for homogeneity with pure SymPy types _addsort(args) return super().__new__(cls, *args, **kwargs)
def __new__(cls, *args, base=None, **kwargs): kwargs['evaluate'] = False # a+0 -> a args = [i for i in args if i != 0] # Reorder for homogeneity with pure SymPy types _addsort(args) obj = super().__new__(cls, *args, **kwargs) try: obj.base = base except AttributeError: # This might happen if e.g. one attempts a (re)construction with # one sole argument. The (re)constructed EvalDerivative degenerates # to an object of different type, in classic SymPy style. That's fine assert len(args) <= 1 assert not obj.is_Add return obj return obj
def _(expr, args): if all(i.is_commutative for i in args): _addsort(args) # In-place sorting return expr.func(*args, evaluate=False) else: return expr._new_rawargs(*args)