if len(candidates) != len(f.args): exponents += [pc.multiplication(*[arg for arg in f.args if arg not in candidates])**e] m[s.z] = pc.multiplication(*exponents) canonical_form.add_rule(pc.multiplication(s.x),s.x) canonical_form.add_rule(pc.addition(s.x),s.x) canonical_form.add_rule(pc.exponentiation(s.x),s.x) canonical_form.add_rule(1/s.x, s.x**-1) canonical_form.add_rule(pc.exp(s.x), pc.e**s.x) canonical_form.add_rule(s.x**s.y, s.z, normalize_exponentiation,condition=pc.equal(pc.DominantType(pc.Type(s.y),pc.Types.Real),pc.Types.Real)) canonical_form.add_rule(pc.exp(s.x),pc.e**s.x) canonical_form.add_rule(pc.sqrt(s.x),s.x**(1/pc.S(2))) canonical_form.add_rule(s.x>s.y,s.y<s.x) canonical_form.add_rule(s.x>=s.y,s.y<=s.x) canonical_form.add_rule(s.x<=s.y,pc.Or(s.x<s.y,pc.equal(s.x,s.y))) canonical_form.add_rule(pc.unequal(s.x,s.y),pc.Not(pc.equal(s.x,s.y))); canonical_form.add_rule(abs(s.x),pc.Max(s.x,-s.x),condition=pc.equal(pc.DominantType(pc.Type(s.x),pc.Types.Real),pc.Types.Real)) canonical_form.add_rule(pc.Max(s.a,s.b),-pc.Min(-s.a,-s.b)) canonical_form.add_rule(pc.tan(s.x),pc.sin(s.x)/pc.cos(s.x)) canonical_form.add_rule(pc.cot(s.x),pc.cos(s.x)/pc.sin(s.x))
evaluator.add_rule(pc.Type(pc.real(s.x)),pc.Types.Real) evaluator.add_rule(pc.Type(pc.imag(s.x)),pc.Types.Real) evaluator.add_rule(pc.Type(pc.conjugate(s.x)),pc.Types.Complex) evaluator.add_rule(pc.Type(pc.Indicator(s.x)),pc.Types.Natural) evaluator.add_rule(pc.Type(pc.OuterPiecewise(s.x)),pc.Type(s.x)) evaluator.add_rule(pc.Type(pc.InnerPiecewise((s.a,s.b),s.x)),pc.DominantType(pc.Type(s.a),pc.Type(pc.InnerPiecewise(s.x)))) evaluator.add_rule(pc.Type(pc.InnerPiecewise((s.a,s.b))),pc.Type(s.a)) evaluator.add_rule(pc.Type(pc.derivative(s.x,s.y)),pc.Type(s.x)) evaluator.add_rule(pc.Type(pc.evaluated_at(s.x,s.y,s.z)),pc.DominantType(pc.Type(s.x),pc.Type(s.z))) evaluator.add_rule(pc.Type(pc.tmp(s.x)),pc.Type(s.x)) evaluator.add_rule(pc.Type(pc.sqrt(s.x)),pc.Type(s.x**(1/pc.S(2)))) evaluator.add_rule(pc.Type(pc.atan2(s.x,s.y)),pc.DominantType(pc.Type(s.x),pc.Type(s.x),pc.Types.Rational)) for f in [pc.exp,pc.log,pc.sin,pc.cos,pc.asin ,pc.acos,pc.tan,pc.atan,pc.cot,pc.acot,pc.sinh,pc.cosh,pc.asinh,pc.acosh,pc.tanh,pc.atanh,pc.coth,pc.acoth]: evaluator.add_rule(pc.Type(f(s.x)),pc.DominantType(pc.Type(s.x),pc.Types.Rational)) def issubtype(x,t): return pc.equal(pc.DominantType(pc.Type(x),t),t) from .logic_evaluator import is_mpmath from mpmath import mp def mpmath_type_evaluator(m): v = m[s.x].value if isinstance(v,mp.mpf):
evaluator.add_rule(pc.derivative(s.x,s.x),1) evaluator.add_rule(pc.derivative(s.y,s.x),0,check_if_excluded_derivative,condition=pc.Not(contains_atomic(s.y,s.x))); evaluator.add_rule(pc.derivative(s.a+s.b,s.x),pc.derivative(s.a,s.x)+pc.derivative(s.b,s.x)) evaluator.add_rule(pc.derivative(s.a*s.b,s.x),pc.derivative(s.a,s.x)*s.b+pc.derivative(s.b,s.x)*s.a) evaluator.add_rule(pc.derivative(-s.x,s.x),-1) evaluator.add_rule(pc.derivative(1/s.x,s.x),-s.x**-2) evaluator.add_rule(pc.derivative(pc.log(s.x),s.x),1/s.x) evaluator.add_rule(pc.derivative(pc.sin(s.x),s.x),pc.cos(s.x)) evaluator.add_rule(pc.derivative(pc.cos(s.x),s.x),-pc.sin(s.x)) # TODO: Add assumptions to expressions: the following is only valid if x != -1,1 or x != -i,i evaluator.add_rule(pc.derivative(pc.asin(s.x),s.x),1/pc.sqrt(1-s.x**2)) evaluator.add_rule(pc.derivative(pc.acos(s.x),s.x),-1/pc.sqrt(1-s.x**2)) evaluator.add_rule(pc.derivative(pc.atan(s.x),s.x),1/(1+s.x**2)) evaluator.add_rule(pc.derivative(pc.acot(s.x),s.x),-1/(1+s.x**2)) evaluator.add_rule(pc.derivative(s.x**s.n,s.x),s.n*s.x**(s.n-1),condition=(pc.equal(pc.Type(s.n)))); evaluator.add_rule(pc.derivative(s.a**s.b,s.x),pc.derivative(s.b*pc.log(s.a),s.x)*s.a**s.b); evaluator.add_rule(pc.derivative(pc.OuterPiecewise(s.a),s.x),pc.OuterPiecewise(pc.derivative(s.a,s.x))) evaluator.add_rule(pc.derivative(pc.InnerPiecewise((s.a,s.b),s.c),s.x),pc.InnerPiecewise((pc.derivative(s.a,s.x),s.b),pc.derivative(pc.InnerPiecewise(s.c),s.x)),condition=pc.Not(contains_atomic(s.b,s.x))) evaluator.add_rule(pc.derivative(pc.InnerPiecewise((s.a,s.b)),s.x),(pc.derivative(s.a,s.x),s.b),condition=pc.Not(contains_atomic(s.b,s.x))) evaluator.add_rule(pc.OuterPiecewise(s.a)**s.x,pc.OuterPiecewise(s.a**s.x))
canonical_form.add_rule(pc.multiplication(s.x), s.x) canonical_form.add_rule(pc.addition(s.x), s.x) canonical_form.add_rule(pc.exponentiation(s.x), s.x) canonical_form.add_rule(1 / s.x, s.x**-1) canonical_form.add_rule(pc.exp(s.x), pc.e**s.x) canonical_form.add_rule(s.x**s.y, s.z, normalize_exponentiation, condition=pc.equal( pc.DominantType(pc.Type(s.y), pc.Types.Real), pc.Types.Real)) canonical_form.add_rule(pc.exp(s.x), pc.e**s.x) canonical_form.add_rule(pc.sqrt(s.x), s.x**(1 / pc.S(2))) canonical_form.add_rule(s.x > s.y, s.y < s.x) canonical_form.add_rule(s.x >= s.y, s.y <= s.x) canonical_form.add_rule(s.x <= s.y, pc.Or(s.x < s.y, pc.equal(s.x, s.y))) canonical_form.add_rule(pc.unequal(s.x, s.y), pc.Not(pc.equal(s.x, s.y))) canonical_form.add_rule(abs(s.x), pc.Max(s.x, -s.x), condition=pc.equal( pc.DominantType(pc.Type(s.x), pc.Types.Real), pc.Types.Real)) canonical_form.add_rule(pc.Max(s.a, s.b), -pc.Min(-s.a, -s.b)) canonical_form.add_rule(pc.tan(s.x), pc.sin(s.x) / pc.cos(s.x))
condition=pc.Not(contains_atomic(s.y, s.x))) evaluator.add_rule(pc.derivative(s.a + s.b, s.x), pc.derivative(s.a, s.x) + pc.derivative(s.b, s.x)) evaluator.add_rule( pc.derivative(s.a * s.b, s.x), pc.derivative(s.a, s.x) * s.b + pc.derivative(s.b, s.x) * s.a) evaluator.add_rule(pc.derivative(-s.x, s.x), -1) evaluator.add_rule(pc.derivative(1 / s.x, s.x), -s.x**-2) evaluator.add_rule(pc.derivative(pc.log(s.x), s.x), 1 / s.x) evaluator.add_rule(pc.derivative(pc.sin(s.x), s.x), pc.cos(s.x)) evaluator.add_rule(pc.derivative(pc.cos(s.x), s.x), -pc.sin(s.x)) # TODO: Add assumptions to expressions: the following is only valid if x != -1,1 or x != -i,i evaluator.add_rule(pc.derivative(pc.asin(s.x), s.x), 1 / pc.sqrt(1 - s.x**2)) evaluator.add_rule(pc.derivative(pc.acos(s.x), s.x), -1 / pc.sqrt(1 - s.x**2)) evaluator.add_rule(pc.derivative(pc.atan(s.x), s.x), 1 / (1 + s.x**2)) evaluator.add_rule(pc.derivative(pc.acot(s.x), s.x), -1 / (1 + s.x**2)) evaluator.add_rule(pc.derivative(s.x**s.n, s.x), s.n * s.x**(s.n - 1), condition=(pc.equal(pc.Type(s.n)))) evaluator.add_rule(pc.derivative(s.a**s.b, s.x), pc.derivative(s.b * pc.log(s.a), s.x) * s.a**s.b) evaluator.add_rule(pc.derivative(pc.OuterPiecewise(s.a), s.x), pc.OuterPiecewise(pc.derivative(s.a, s.x))) evaluator.add_rule(pc.derivative(pc.InnerPiecewise((s.a, s.b), s.c), s.x), pc.InnerPiecewise((pc.derivative(s.a, s.x), s.b), pc.derivative(pc.InnerPiecewise(s.c),
evaluator.add_rule(pc.OperationType(abs(s.x)), pc.Types.Real) #evaluator.add_rule(pc.Type(pc.conjugate(s.x)),pc.Type(s.x)) evaluator.add_rule(pc.Type(pc.Indicator(s.x)), pc.Types.Natural) evaluator.add_rule(pc.Type(pc.OuterPiecewise(s.x)), pc.Type(s.x)) evaluator.add_rule( pc.Type(pc.InnerPiecewise((s.a, s.b), s.x)), pc.DominantType(pc.Type(s.a), pc.Type(pc.InnerPiecewise(s.x)))) evaluator.add_rule(pc.Type(pc.InnerPiecewise((s.a, s.b))), pc.Type(s.a)) evaluator.add_rule(pc.Type(pc.derivative(s.x, s.y)), pc.Type(s.x)) evaluator.add_rule(pc.Type(pc.evaluated_at(s.x, s.y, s.z)), pc.DominantType(pc.Type(s.x), pc.Type(s.z))) evaluator.add_rule(pc.Type(pc.tmp(s.x)), pc.Type(s.x)) evaluator.add_rule(pc.Type(pc.sqrt(s.x)), pc.Type(s.x**(1 / pc.S(2)))) evaluator.add_rule( pc.Type(pc.atan2(s.x, s.y)), pc.DominantType(pc.Type(s.x), pc.Type(s.x), pc.Types.Rational)) for f in [ pc.exp, pc.log, pc.sin, pc.cos, pc.asin, pc.acos, pc.tan, pc.atan, pc.cot, pc.acot, pc.sinh, pc.cosh, pc.asinh, pc.acosh, pc.tanh, pc.atanh, pc.coth, pc.acoth ]: evaluator.add_rule(pc.Type(f(s.x)), pc.DominantType(pc.Type(s.x), pc.Types.Rational)) def issubtype(x, t):