def diff(self, mi): """ :arg mi: a multi-index (tuple) indicating how many x/y derivatives are to be taken. """ try: expr = self.cache_by_mi[mi] except KeyError: from six import iteritems from sumpy.symbolic import Add closest_mi = self.get_closest_cached_mi(mi) expr = self.cache_by_mi[closest_mi] # Try to reduce the derivative using recurrences first, and if that # fails fall back to derivative taking. for next_deriv, next_mi in (self.get_derivative_taking_sequence( closest_mi, mi)): recurrence = (self.wrangler.try_get_recurrence_for_derivative( next_mi, self.cache_by_mi)) if recurrence is not None: expr = Add(*tuple( coeff * self.cache_by_mi[ident] for ident, coeff in iteritems(recurrence))) else: expr = expr.diff(next_deriv) self.cache_by_mi[next_mi] = expr return expr
def diff(self, mi): """ :arg mi: a multi-index (tuple) indicating how many x/y derivatives are to be taken. """ try: expr = self.cache_by_mi[mi] except KeyError: from six import iteritems from sumpy.symbolic import Add closest_mi = self.get_closest_cached_mi(mi) expr = self.cache_by_mi[closest_mi] # Try to reduce the derivative using recurrences first, and if that # fails fall back to derivative taking. for next_deriv, next_mi in ( self.get_derivative_taking_sequence(closest_mi, mi)): recurrence = ( self.wrangler.try_get_recurrence_for_derivative( next_mi, self.cache_by_mi)) if recurrence is not None: expr = Add(*tuple( coeff * self.cache_by_mi[ident] for ident, coeff in iteritems(recurrence))) else: expr = expr.diff(next_deriv) self.cache_by_mi[next_mi] = expr return expr
def test_cse_not_possible(): # No substitution possible. e = Add(x, y) substs, reduced = cse([e]) assert substs == [] assert reduced == [x + y] # issue 6329 eq = (meijerg((1, 2), (y, 4), (5,), [], x) + meijerg((1, 3), (y, 4), (5,), [], x)) assert cse(eq) == ([], [eq])
def test_nested_substitution(): # Substitution within a substitution. e = Add(Pow(w*x + y, 2), sqrt(w*x + y)) substs, reduced = cse([e]) assert substs == [(x0, w*x + y)] assert reduced == [sqrt(x0) + x0**2]
def test_cse_single(): # Simple substitution. e = Add(Pow(x + y, 2), sqrt(x + y)) substs, reduced = cse([e]) assert substs == [(x0, x + y)] assert reduced == [sqrt(x0) + x0**2]