def diff(expression, variable): vals = WildResults() f,a,b = wilds('f a b') expression = expression.simplify() if variable not in expression: return symbolic(0) elif expression.match(variable): return symbolic(1) elif expression.match(f(a,b), vals) and vals.f in _known_functions: return _diff_known_function(expression, variable) elif expression.match(f(a), vals) and vals.f in _known_functions: return _diff_known_function(vals.f(vals.a), vals.a) * diff(vals.a, variable) raise DifferentiationError("d/d%s %s" % (variable,expression))
def _simplify_known_values(exp): a,b,c = core.wilds('a b c') vals = {} if exp.match(a(b,c), vals) \ and 'numeric' in vals['a'].kargs \ and isinstance(vals['b'], core._KnownValue) \ and isinstance(vals['c'], core._KnownValue): cast = vals['a'].kargs['cast'] if 'cast' in vals['a'].kargs else (lambda x: x) nfn = getattr(operator, vals['a'].kargs['numeric']) return core.symbolic(nfn(cast(vals['b'].value()), cast(vals['c'].value()))) else: return exp
def _simplify_bitops(exp): a,b = core.wilds('a b') vals = core.WildResults() if exp.match(a ^ a): return core.symbolic(0) elif exp.match(a | a, vals): return vals.a elif exp.match(a & a, vals): return vals.a elif exp.match((a << b) >> b, vals) or exp.match((a >> b) << b, vals): return vals.a else: return exp
def _simplify_bitops(exp): a, b = core.wilds('a b') vals = core.WildResults() if exp.match(a ^ a): return core.symbolic(0) elif exp.match(a | a, vals): return vals.a elif exp.match(a & a, vals): return vals.a elif exp.match((a << b) >> b, vals) or exp.match((a >> b) << b, vals): return vals.a else: return exp
def _simplify_known_values(exp): a, b, c = core.wilds('a b c') vals = {} if exp.match(a(b,c), vals) \ and 'numeric' in vals['a'].kargs \ and isinstance(vals['b'], core._KnownValue) \ and isinstance(vals['c'], core._KnownValue): cast = vals['a'].kargs['cast'] if 'cast' in vals['a'].kargs else ( lambda x: x) nfn = getattr(operator, vals['a'].kargs['numeric']) return core.symbolic( nfn(cast(vals['b'].value()), cast(vals['c'].value()))) else: return exp