def eval(cls, arg): from sympy import (Equality, GreaterThan, LessThan, StrictGreaterThan, StrictLessThan, Unequality) if isinstance(arg, Number) or arg in (True, False): return false if arg else true #if arg.is_Not: #return arg.args[0] # ONLY CHANGE # Simplify Relational objects. if isinstance(arg, Equality): return Unequality(*arg.args) if isinstance(arg, Unequality): return Equality(*arg.args) if isinstance(arg, StrictLessThan): return GreaterThan(*arg.args) if isinstance(arg, StrictGreaterThan): return LessThan(*arg.args) if isinstance(arg, LessThan): return StrictGreaterThan(*arg.args) if isinstance(arg, GreaterThan): return StrictLessThan(*arg.args)
def test_lt_gt(): from sympy import sympify as S x, y = Symbol("x"), Symbol("y") assert (x >= y) == GreaterThan(x, y) assert (x >= 0) == GreaterThan(x, 0) assert (x <= y) == LessThan(x, y) assert (x <= 0) == LessThan(x, 0) assert (0 <= x) == GreaterThan(x, 0) assert (0 >= x) == LessThan(x, 0) assert (S(0) >= x) == GreaterThan(0, x) assert (S(0) <= x) == LessThan(0, x) assert (x > y) == StrictGreaterThan(x, y) assert (x > 0) == StrictGreaterThan(x, 0) assert (x < y) == StrictLessThan(x, y) assert (x < 0) == StrictLessThan(x, 0) assert (0 < x) == StrictGreaterThan(x, 0) assert (0 > x) == StrictLessThan(x, 0) assert (S(0) > x) == StrictGreaterThan(0, x) assert (S(0) < x) == StrictLessThan(0, x) e = x**2 + 4 * x + 1 assert (e >= 0) == GreaterThan(e, 0) assert (0 <= e) == GreaterThan(e, 0) assert (e > 0) == StrictGreaterThan(e, 0) assert (0 < e) == StrictGreaterThan(e, 0) assert (e <= 0) == LessThan(e, 0) assert (0 >= e) == LessThan(e, 0) assert (e < 0) == StrictLessThan(e, 0) assert (0 > e) == StrictLessThan(e, 0) assert (S(0) >= e) == GreaterThan(0, e) assert (S(0) <= e) == LessThan(0, e) assert (S(0) < e) == StrictLessThan(0, e) assert (S(0) > e) == StrictGreaterThan(0, e)
def test_Idx_inequalities(): i14 = Idx("i14", (1, 4)) i79 = Idx("i79", (7, 9)) i46 = Idx("i46", (4, 6)) i35 = Idx("i35", (3, 5)) assert i14 <= 5 assert i14 < 5 assert not (i14 >= 5) assert not (i14 > 5) assert 5 >= i14 assert 5 > i14 assert not (5 <= i14) assert not (5 < i14) assert LessThan(i14, 5) assert StrictLessThan(i14, 5) assert not GreaterThan(i14, 5) assert not StrictGreaterThan(i14, 5) assert i14 <= 4 assert isinstance(i14 < 4, StrictLessThan) assert isinstance(i14 >= 4, GreaterThan) assert not (i14 > 4) assert isinstance(i14 <= 1, LessThan) assert not (i14 < 1) assert i14 >= 1 assert isinstance(i14 > 1, StrictGreaterThan) assert not (i14 <= 0) assert not (i14 < 0) assert i14 >= 0 assert i14 > 0 from sympy.abc import x assert isinstance(i14 < x, StrictLessThan) assert isinstance(i14 > x, StrictGreaterThan) assert isinstance(i14 <= x, LessThan) assert isinstance(i14 >= x, GreaterThan) assert i14 < i79 assert i14 <= i79 assert not (i14 > i79) assert not (i14 >= i79) assert i14 <= i46 assert isinstance(i14 < i46, StrictLessThan) assert isinstance(i14 >= i46, GreaterThan) assert not (i14 > i46) assert isinstance(i14 < i35, StrictLessThan) assert isinstance(i14 > i35, StrictGreaterThan) assert isinstance(i14 <= i35, LessThan) assert isinstance(i14 >= i35, GreaterThan) iNone1 = Idx("iNone1") iNone2 = Idx("iNone2") assert isinstance(iNone1 < iNone2, StrictLessThan) assert isinstance(iNone1 > iNone2, StrictGreaterThan) assert isinstance(iNone1 <= iNone2, LessThan) assert isinstance(iNone1 >= iNone2, GreaterThan)
def signbit(x): """signbit(x) Returns True if signbit is set (less than zero). """ return StrictLessThan(x, 0)
class TestAllGood(object): # These latex strings should parse to the corresponding SymPy expression GOOD_PAIRS = [ ("0", Rational(0)), ("1", Rational(1)), ("-3.14", Rational(-314, 100)), ("5-3", _Add(5, _Mul(-1, 3))), ("(-7.13)(1.5)", _Mul(Rational('-7.13'), Rational('1.5'))), ("\\left(-7.13\\right)\\left(1.5\\right)", _Mul(Rational('-7.13'), Rational('1.5'))), ("x", x), ("2x", 2 * x), ("x^2", x**2), ("x^{3 + 1}", x**_Add(3, 1)), ("x^{\\left\\{3 + 1\\right\\}}", x**_Add(3, 1)), ("-3y + 2x", _Add(_Mul(2, x), Mul(-1, 3, y, evaluate=False))), ("-c", -c), ("a \\cdot b", a * b), ("a / b", a / b), ("a \\div b", a / b), ("a + b", a + b), ("a + b - a", Add(a, b, _Mul(-1, a), evaluate=False)), ("a^2 + b^2 = c^2", Eq(a**2 + b**2, c**2)), ("a^2 + b^2 != 2c^2", Ne(a**2 + b**2, 2 * c**2)), ("a\\mod b", Mod(a, b)), ("\\sin \\theta", sin(theta)), ("\\sin(\\theta)", sin(theta)), ("\\sin\\left(\\theta\\right)", sin(theta)), ("\\sin^{-1} a", asin(a)), ("\\sin a \\cos b", _Mul(sin(a), cos(b))), ("\\sin \\cos \\theta", sin(cos(theta))), ("\\sin(\\cos \\theta)", sin(cos(theta))), ("\\arcsin(a)", asin(a)), ("\\arccos(a)", acos(a)), ("\\arctan(a)", atan(a)), ("\\sinh(a)", sinh(a)), ("\\cosh(a)", cosh(a)), ("\\tanh(a)", tanh(a)), ("\\sinh^{-1}(a)", asinh(a)), ("\\cosh^{-1}(a)", acosh(a)), ("\\tanh^{-1}(a)", atanh(a)), ("\\arcsinh(a)", asinh(a)), ("\\arccosh(a)", acosh(a)), ("\\arctanh(a)", atanh(a)), ("\\arsinh(a)", asinh(a)), ("\\arcosh(a)", acosh(a)), ("\\artanh(a)", atanh(a)), ("\\operatorname{arcsinh}(a)", asinh(a)), ("\\operatorname{arccosh}(a)", acosh(a)), ("\\operatorname{arctanh}(a)", atanh(a)), ("\\operatorname{arsinh}(a)", asinh(a)), ("\\operatorname{arcosh}(a)", acosh(a)), ("\\operatorname{artanh}(a)", atanh(a)), ("\\operatorname{gcd}(a, b)", UnevaluatedExpr(gcd(a, b))), ("\\operatorname{lcm}(a, b)", UnevaluatedExpr(lcm(a, b))), ("\\operatorname{gcd}(a,b)", UnevaluatedExpr(gcd(a, b))), ("\\operatorname{lcm}(a,b)", UnevaluatedExpr(lcm(a, b))), ("\\operatorname{floor}(a)", floor(a)), ("\\operatorname{ceil}(b)", ceiling(b)), ("\\cos^2(x)", cos(x)**2), ("\\cos(x)^2", cos(x)**2), ("\\gcd(a, b)", UnevaluatedExpr(gcd(a, b))), ("\\lcm(a, b)", UnevaluatedExpr(lcm(a, b))), ("\\gcd(a,b)", UnevaluatedExpr(gcd(a, b))), ("\\lcm(a,b)", UnevaluatedExpr(lcm(a, b))), ("\\floor(a)", floor(a)), ("\\ceil(b)", ceiling(b)), ("\\max(a, b)", Max(a, b)), ("\\min(a, b)", Min(a, b)), ("\\frac{a}{b}", a / b), ("\\frac{a + b}{c}", _Mul(a + b, _Pow(c, -1))), ("\\frac{7}{3}", Rational(7, 3)), ("(\\csc x)(\\sec y)", csc(x) * sec(y)), ("\\lim_{x \\to 3} a", Limit(a, x, 3)), ("\\lim_{x \\rightarrow 3} a", Limit(a, x, 3)), ("\\lim_{x \\Rightarrow 3} a", Limit(a, x, 3)), ("\\lim_{x \\longrightarrow 3} a", Limit(a, x, 3)), ("\\lim_{x \\Longrightarrow 3} a", Limit(a, x, 3)), ("\\lim_{x \\to 3^{+}} a", Limit(a, x, 3, dir='+')), ("\\lim_{x \\to 3^{-}} a", Limit(a, x, 3, dir='-')), ("\\infty", oo), ("\\infty\\%", oo), ("\\$\\infty", oo), ("-\\infty", -oo), ("-\\infty\\%", -oo), ("-\\$\\infty", -oo), ("\\lim_{x \\to \\infty} \\frac{1}{x}", Limit(_Mul(1, _Pow(x, -1)), x, oo)), ("\\frac{d}{dx} x", Derivative(x, x)), ("\\frac{d}{dt} x", Derivative(x, t)), # ("f(x)", f(x)), # ("f(x, y)", f(x, y)), # ("f(x, y, z)", f(x, y, z)), # ("\\frac{d f(x)}{dx}", Derivative(f(x), x)), # ("\\frac{d\\theta(x)}{dx}", Derivative(theta(x), x)), ("|x|", _Abs(x)), ("\\left|x\\right|", _Abs(x)), ("||x||", _Abs(_Abs(x))), ("|x||y|", _Abs(x) * _Abs(y)), ("||x||y||", _Abs(_Abs(x) * _Abs(y))), ("\\lfloor x\\rfloor", floor(x)), ("\\lceil y\\rceil", ceiling(y)), ("\\pi^{|xy|}", pi**_Abs(x * y)), ("\\frac{\\pi}{3}", _Mul(pi, _Pow(3, -1))), ("\\sin{\\frac{\\pi}{2}}", sin(_Mul(pi, _Pow(2, -1)), evaluate=False)), ("a+bI", a + I * b), ("e^{I\\pi}", Integer(-1)), ("\\int x dx", Integral(x, x)), ("\\int x d\\theta", Integral(x, theta)), ("\\int (x^2 - y)dx", Integral(x**2 - y, x)), ("\\int x + a dx", Integral(_Add(x, a), x)), ("\\int da", Integral(1, a)), ("\\int_0^7 dx", Integral(1, (x, 0, 7))), ("\\int_a^b x dx", Integral(x, (x, a, b))), ("\\int^b_a x dx", Integral(x, (x, a, b))), ("\\int_{a}^b x dx", Integral(x, (x, a, b))), ("\\int^{b}_a x dx", Integral(x, (x, a, b))), ("\\int_{a}^{b} x dx", Integral(x, (x, a, b))), ("\\int_{ }^{}x dx", Integral(x, x)), ("\\int^{ }_{ }x dx", Integral(x, x)), ("\\int^{b}_{a} x dx", Integral(x, (x, a, b))), # ("\\int_{f(a)}^{f(b)} f(z) dz", Integral(f(z), (z, f(a), f(b)))), ("\\int (x+a)", Integral(_Add(x, a), x)), ("\\int a + b + c dx", Integral(Add(a, b, c, evaluate=False), x)), ("\\int \\frac{dz}{z}", Integral(Pow(z, -1), z)), ("\\int \\frac{3 dz}{z}", Integral(3 * Pow(z, -1), z)), ("\\int \\frac{1}{x} dx", Integral(Pow(x, -1), x)), ("\\int \\frac{1}{a} + \\frac{1}{b} dx", Integral(_Add(_Pow(a, -1), Pow(b, -1)), x)), ("\\int \\frac{3 \\cdot d\\theta}{\\theta}", Integral(3 * _Pow(theta, -1), theta)), ("\\int \\frac{1}{x} + 1 dx", Integral(_Add(_Pow(x, -1), 1), x)), ("x_0", Symbol('x_0', real=True, positive=True)), ("x_{1}", Symbol('x_1', real=True, positive=True)), ("x_a", Symbol('x_a', real=True, positive=True)), ("x_{b}", Symbol('x_b', real=True, positive=True)), ("h_\\theta", Symbol('h_{\\theta}', real=True, positive=True)), ("h_\\theta ", Symbol('h_{\\theta}', real=True, positive=True)), ("h_{\\theta}", Symbol('h_{\\theta}', real=True, positive=True)), # ("h_{\\theta}(x_0, x_1)", Symbol('h_{theta}', real=True)(Symbol('x_{0}', real=True), Symbol('x_{1}', real=True))), ("x!", _factorial(x)), ("100!", _factorial(100)), ("\\theta!", _factorial(theta)), ("(x + 1)!", _factorial(_Add(x, 1))), ("\\left(x + 1\\right)!", _factorial(_Add(x, 1))), ("(x!)!", _factorial(_factorial(x))), ("x!!!", _factorial(_factorial(_factorial(x)))), ("5!7!", _Mul(_factorial(5), _factorial(7))), ("\\sqrt{x}", sqrt(x)), ("\\sqrt{x + b}", sqrt(_Add(x, b))), ("\\sqrt[3]{\\sin x}", root(sin(x), 3)), ("\\sqrt[y]{\\sin x}", root(sin(x), y)), ("\\sqrt[\\theta]{\\sin x}", root(sin(x), theta)), ("x < y", StrictLessThan(x, y)), ("x \\leq y", LessThan(x, y)), ("x > y", StrictGreaterThan(x, y)), ("x \\geq y", GreaterThan(x, y)), ("\\sum_{k = 1}^{3} c", Sum(c, (k, 1, 3))), ("\\sum_{k = 1}^3 c", Sum(c, (k, 1, 3))), ("\\sum^{3}_{k = 1} c", Sum(c, (k, 1, 3))), ("\\sum^3_{k = 1} c", Sum(c, (k, 1, 3))), ("\\sum_{k = 1}^{10} k^2", Sum(k**2, (k, 1, 10))), ("\\sum_{n = 0}^{\\infty} \\frac{1}{n!}", Sum(_Pow(_factorial(n), -1), (n, 0, oo))), ("\\prod_{a = b}^{c} x", Product(x, (a, b, c))), ("\\prod_{a = b}^c x", Product(x, (a, b, c))), ("\\prod^{c}_{a = b} x", Product(x, (a, b, c))), ("\\prod^c_{a = b} x", Product(x, (a, b, c))), ("\\ln x", _log(x, E)), ("\\ln xy", _log(x * y, E)), ("\\log x", _log(x, 10)), ("\\log xy", _log(x * y, 10)), # ("\\log_2 x", _log(x, 2)), ("\\log_{2} x", _log(x, 2)), # ("\\log_a x", _log(x, a)), ("\\log_{a} x", _log(x, a)), ("\\log_{11} x", _log(x, 11)), ("\\log_{a^2} x", _log(x, _Pow(a, 2))), ("[x]", x), ("[a + b]", _Add(a, b)), ("\\frac{d}{dx} [ \\tan x ]", Derivative(tan(x), x)), ("2\\overline{x}", 2 * Symbol('xbar', real=True, positive=True)), ("2\\overline{x}_n", 2 * Symbol('xbar_n', real=True, positive=True)), ("\\frac{x}{\\overline{x}_n}", x / Symbol('xbar_n', real=True, positive=True)), ("\\frac{\\sin(x)}{\\overline{x}_n}", sin(x) / Symbol('xbar_n', real=True, positive=True)), ("2\\bar{x}", 2 * Symbol('xbar', real=True, positive=True)), ("2\\bar{x}_n", 2 * Symbol('xbar_n', real=True, positive=True)), ("\\sin\\left(\\theta\\right) \\cdot4", sin(theta) * 4), ("\\ln\\left(\\theta\\right)", _log(theta, E)), ("\\ln\\left(x-\\theta\\right)", _log(x - theta, E)), ("\\ln\\left(\\left(x-\\theta\\right)\\right)", _log(x - theta, E)), ("\\ln\\left(\\left[x-\\theta\\right]\\right)", _log(x - theta, E)), ("\\ln\\left(\\left\\{x-\\theta\\right\\}\\right)", _log(x - theta, E)), ("\\ln\\left(\\left|x-\\theta\\right|\\right)", _log(_Abs(x - theta), E)), ("\\frac{1}{2}xy(x+y)", Mul(Rational(1, 2), x, y, (x + y), evaluate=False)), ("\\frac{1}{2}\\theta(x+y)", Mul(Rational(1, 2), theta, (x + y), evaluate=False)), ("1-f(x)", 1 - f * x), ("\\begin{matrix}1&2\\\\3&4\\end{matrix}", Matrix([[1, 2], [3, 4]])), ("\\begin{matrix}x&x^2\\\\\\sqrt{x}&x\\end{matrix}", Matrix([[x, x**2], [_Pow(x, S.Half), x]])), ("\\begin{matrix}\\sqrt{x}\\\\\\sin(\\theta)\\end{matrix}", Matrix([_Pow(x, S.Half), sin(theta)])), ("\\begin{pmatrix}1&2\\\\3&4\\end{pmatrix}", Matrix([[1, 2], [3, 4]])), ("\\begin{bmatrix}1&2\\\\3&4\\end{bmatrix}", Matrix([[1, 2], [3, 4]])), # scientific notation ("2.5\\times 10^2", Rational(250)), ("1,500\\times 10^{-1}", Rational(150)), # e notation ("2.5E2", Rational(250)), ("1,500E-1", Rational(150)), # multiplication without cmd ("2x2y", Mul(2, x, 2, y, evaluate=False)), ("2x2", Mul(2, x, 2, evaluate=False)), ("x2", x * 2), # lin alg processing ("\\theta\\begin{matrix}1&2\\\\3&4\\end{matrix}", MatMul(theta, Matrix([[1, 2], [3, 4]]), evaluate=False)), ("\\theta\\begin{matrix}1\\\\3\\end{matrix} - \\begin{matrix}-1\\\\2\\end{matrix}", MatAdd(MatMul(theta, Matrix([[1], [3]]), evaluate=False), MatMul(-1, Matrix([[-1], [2]]), evaluate=False), evaluate=False)), ("\\theta\\begin{matrix}1&0\\\\0&1\\end{matrix}*\\begin{matrix}3\\\\-2\\end{matrix}", MatMul(theta, Matrix([[1, 0], [0, 1]]), Matrix([3, -2]), evaluate=False)), ("\\frac{1}{9}\\theta\\begin{matrix}1&2\\\\3&4\\end{matrix}", MatMul(Rational(1, 9), theta, Matrix([[1, 2], [3, 4]]), evaluate=False)), ("\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix},\\begin{pmatrix}4\\\\3\\\\1\\end{pmatrix}", [Matrix([1, 2, 3]), Matrix([4, 3, 1])]), ("\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix};\\begin{pmatrix}4\\\\3\\\\1\\end{pmatrix}", [Matrix([1, 2, 3]), Matrix([4, 3, 1])]), ("\\left\\{\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix},\\begin{pmatrix}4\\\\3\\\\1\\end{pmatrix}\\right\\}", [Matrix([1, 2, 3]), Matrix([4, 3, 1])]), ("\\left\\{\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix},\\begin{pmatrix}4\\\\3\\\\1\\end{pmatrix},\\begin{pmatrix}1\\\\1\\\\1\\end{pmatrix}\\right\\}", [Matrix([1, 2, 3]), Matrix([4, 3, 1]), Matrix([1, 1, 1])]), ("\\left\\{\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix}\\right\\}", Matrix([1, 2, 3])), ("\\left{\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix}\\right}", Matrix([1, 2, 3])), ("{\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix}}", Matrix([1, 2, 3])), # us dollars ("\\$1,000.00", Rational(1000)), ("\\$543.21", Rational(54321, 100)), ("\\$0.009", Rational(9, 1000)), # percentages ("100\\%", Rational(1)), ("1.5\\%", Rational(15, 1000)), ("0.05\\%", Rational(5, 10000)), # empty set ("\\emptyset", S.EmptySet), # divide by zero ("\\frac{1}{0}", _Pow(0, -1)), ("1+\\frac{5}{0}", _Add(1, _Mul(5, _Pow(0, -1)))), # adjacent single char sub sup ("4^26^2", _Mul(_Pow(4, 2), _Pow(6, 2))), ("x_22^2", _Mul(Symbol('x_2', real=True, positive=True), _Pow(2, 2))) ] def test_good_pair(self, s, eq): assert_equal(s, eq)
("x_a", Symbol('x_{a}')), ("x_{b}", Symbol('x_{b}')), ("h_\\theta", Symbol('h_{theta}')), ("h_{\\theta}", Symbol('h_{theta}')), ("h_{\\theta}(x_0, x_1)", Function('h_{theta}')(Symbol('x_{0}'), Symbol('x_{1}'))), ("x!", _factorial(x)), ("100!", _factorial(100)), ("\\theta!", _factorial(theta)), ("(x + 1)!", _factorial(_Add(x, 1))), ("(x!)!", _factorial(_factorial(x))), ("x!!!", _factorial(_factorial(_factorial(x)))), ("5!7!", _Mul(_factorial(5), _factorial(7))), ("\\sqrt{x}", sqrt(x)), ("\\sqrt{x + b}", sqrt(_Add(x, b))), ("\\sqrt[3]{\\sin x}", root(sin(x), 3)), ("\\sqrt[y]{\\sin x}", root(sin(x), y)), ("\\sqrt[\\theta]{\\sin x}", root(sin(x), theta)), ("x < y", StrictLessThan(x, y)), ("x \\leq y", LessThan(x, y)), ("x > y", StrictGreaterThan(x, y)), ("x \\geq y", GreaterThan(x, y)), ("\\mathit{x}", Symbol('x')), ("\\mathit{test}", Symbol('test')), ("\\mathit{TEST}", Symbol('TEST')), ("\\mathit{HELLO world}", Symbol('HELLO world')), ("\\sum_{k = 1}^{3} c", Sum(c, (k, 1, 3))), ("\\sum_{k = 1}^3 c", Sum(c, (k, 1, 3))), ("\\sum^{3}_{k = 1} c", Sum(c, (k, 1, 3))), ("\\sum^3_{k = 1} c", Sum(c, (k, 1, 3))), ("\\sum_{k = 1}^{10} k^2", Sum(k**2, (k, 1, 10))), ("\\sum_{n = 0}^{\\infty} \\frac{1}{n!}", Sum(_Pow(_factorial(n), -1), (n, 0, oo))), ("\\prod_{a = b}^{c} x", Product(x, (a, b, c))), ("\\prod_{a = b}^c x", Product(x, (a, b, c))), ("\\prod^{c}_{a = b} x", Product(x, (a, b, c))),
("h_{\\theta}", Symbol('h_{theta}')), ("h_{\\theta}(x_0, x_1)", Function('h_{theta}')(Symbol('x_{0}'), Symbol('x_{1}'))), ("x!", _factorial(x)), ("100!", _factorial(100)), ("\\theta!", _factorial(theta)), ("(x + 1)!", _factorial(_Add(x, 1))), ("(x!)!", _factorial(_factorial(x))), ("x!!!", _factorial(_factorial(_factorial(x)))), ("5!7!", _Mul(_factorial(5), _factorial(7))), ("\\sqrt{x}", sqrt(x)), ("\\sqrt{x + b}", sqrt(_Add(x, b))), ("\\sqrt[3]{\\sin x}", root(sin(x), 3)), ("\\sqrt[y]{\\sin x}", root(sin(x), y)), ("\\sqrt[\\theta]{\\sin x}", root(sin(x), theta)), ("x < y", StrictLessThan(x, y)), ("x \\leq y", LessThan(x, y)), ("x > y", StrictGreaterThan(x, y)), ("x \\geq y", GreaterThan(x, y)), ("\\mathit{x}", Symbol('x')), ("\\mathit{test}", Symbol('test')), ("\\mathit{TEST}", Symbol('TEST')), ("\\mathit{HELLO world}", Symbol('HELLO world')), ("\\sum_{k = 1}^{3} c", Sum(c, (k, 1, 3))), ("\\sum_{k = 1}^3 c", Sum(c, (k, 1, 3))), ("\\sum^{3}_{k = 1} c", Sum(c, (k, 1, 3))), ("\\sum^3_{k = 1} c", Sum(c, (k, 1, 3))), ("\\sum_{k = 1}^{10} k^2", Sum(k**2, (k, 1, 10))), ("\\sum_{n = 0}^{\\infty} \\frac{1}{n!}", Sum(_Pow(_factorial(n), -1), (n, 0, oo))), ("\\prod_{a = b}^{c} x", Product(x, (a, b, c))),
class CAD: qf = qepcad_formula methods = { StrictLessThan: lt, LessThan: le, StrictGreaterThan: gt, GreaterThan: ge, Equality: eq, Unequality: ne } complete_methods = { # a < b => a <= b + 1 StrictLessThan: lambda l, r: LessThan(l + 1, r), # a <= b => a < b + 1 LessThan: lambda l, r: StrictLessThan(l - 1, r), # a > b => a >= b + 1 StrictGreaterThan: lambda l, r: GreaterThan(l + 1, r), # a >= b => a > b + 1 GreaterThan: lambda l, r: StrictGreaterThan(l + 1, r) } @staticmethod def complete_assumptions(formula): if isinstance(formula, And): return And(*map(CAD.complete_assumptions, formula.args)) elif isinstance(formula, Or): return Or(*map(CAD.complete_assumptions, formula.args)) elif isinstance(formula, Not): return Not(*map(CAD.complete_assumptions, formula.args)) elif type(formula) in CAD.complete_methods: method = CAD.complete_methods.get(type(formula)) return And(formula, method(*formula.args)) return formula @staticmethod def to_sage(formula): if type(formula) in CAD.methods: args = map(lambda e: e._sage_(), formula.args) method = CAD.methods.get(type(formula)) return method(*args) return formula._sage_() @staticmethod def to_qepcad(formula): if isinstance(formula, And): return CAD.qf.and_(*map(CAD.to_qepcad, formula.args)) elif isinstance(formula, Or): return CAD.qf.or_(*map(CAD.to_qepcad, formula.args)) elif isinstance(formula, Not): return CAD.qf.not_(CAD.to_qepcad(*formula.args)) return CAD.qf.formula(CAD.to_sage(formula)) @staticmethod def always(formula): f = CAD.qf.formula(CAD.to_sage(formula)) return run_qepcad(f) @staticmethod def implies(assumptions, formula): if formula == True: return 'TRUE' elif formula == False: return 'FALSE' if assumptions == False: return CAD.always(formula) formula = CAD.to_sage(formula) assumptions = CAD.complete_assumptions(assumptions) assumptions_str = CAD.to_qepcad(assumptions) implication = CAD.qf.implies(assumptions_str, formula) return run_qepcad(implication) @staticmethod def is_true(result): return result == 'TRUE' @staticmethod def is_false(result): return result == 'FALSE' @staticmethod def is_unknown(result): return result == 'UNKNOWN'