def test_extended_real(): x = symbols('x') assert ask(x, Q.extended_real, Assume(x, Q.positive)) == True assert ask(-x, Q.extended_real, Assume(x, Q.positive)) == True assert ask(-x, Q.extended_real, Assume(x, Q.negative)) == True assert ask(x+S.Infinity, Q.extended_real, Assume(x, Q.real)) == True
def test_eliminate_assumptions(): a, b = map(Predicate, symbols('ab')) x, y = symbols('xy') assert eliminate_assume(Assume(x, a)) == a assert eliminate_assume(Assume(x, a), symbol=x) == a assert eliminate_assume(Assume(x, a), symbol=y) == None assert eliminate_assume(Assume(x, a, False)) == ~a assert eliminate_assume(Assume(x, a), symbol=y) == None assert eliminate_assume(Assume(x, a) | Assume(x, b)) == a | b assert eliminate_assume(Assume(x, a) | Assume(x, b, False)) == a | ~b
def test_global(): """Test for global assumptions""" x, y = symbols('x y') global_assumptions.add(Assume(x > 0)) assert Assume(x > 0) in global_assumptions global_assumptions.remove(Assume(x > 0)) assert not Assume(x > 0) in global_assumptions # same with multiple of assumptions global_assumptions.add(Assume(x > 0), Assume(y > 0)) assert Assume(x > 0) in global_assumptions assert Assume(y > 0) in global_assumptions global_assumptions.clear() assert not Assume(x > 0) in global_assumptions assert not Assume(y > 0) in global_assumptions
def compute_known_facts(): """Compute the various forms of knowledge compilation used by the assumptions system. """ # Compute the known facts in CNF form for logical inference fact_string = " -{ Known facts in CNF }-\n" cnf = to_cnf(known_facts) fact_string += "known_facts_cnf = And( \\\n ", fact_string += ", \\\n ".join(map(str, cnf.args)) fact_string += "\n)\n" # Compute the quick lookup for single facts from sympy.abc import x mapping = {} for key in known_facts_keys: mapping[key] = set([key]) for other_key in known_facts_keys: if other_key != key: if ask(x, other_key, Assume(x, key, False), disable_preprocessing=True): mapping[key].add(Not(other_key)) fact_string += "\n\n -{ Known facts in compressed sets }-\n" fact_string += "known_facts_dict = { \\\n ", fact_string += ", \\\n ".join(["%s: %s" % item for item in mapping.items()]) fact_string += "\n}\n" return fact_string
def test_Predicate_wraps_Assume(): x = symbols('x') integer = Predicate('integer') assump = integer(x) assert (assump.expr, assump.key) == (x, integer) assump = Assume(x, integer) assert (assump.expr, assump.key) == (x, integer)
def test_custom_context(): """Test ask with custom assumptions context""" x = symbols('x') assert ask(x, Q.integer) == None local_context = AssumptionsContext() local_context.add(Assume(x, Q.integer)) assert ask(x, Q.integer, context = local_context) == True assert ask(x, Q.integer) == None
def test_global(): """Test ask with global assumptions""" x = symbols('x') assert ask(x, Q.integer) == None global_assumptions.add(Assume(x, Q.integer)) assert ask(x, Q.integer) == True global_assumptions.clear() assert ask(x, Q.integer) == None
def test_infinitesimal(): x, y = symbols('x y') assert ask(x, Q.infinitesimal) == None assert ask(x, Q.infinitesimal, Assume(x, Q.infinitesimal)) == True assert ask(2*x, Q.infinitesimal, Assume(x, Q.infinitesimal)) == True assert ask(x*y, Q.infinitesimal, Assume(x, Q.infinitesimal)) == None assert ask(x*y, Q.infinitesimal, Assume(x, Q.infinitesimal) & \ Assume(y, Q.infinitesimal)) == True assert ask(x*y, Q.infinitesimal, Assume(x, Q.infinitesimal) & \ Assume(y, Q.bounded)) == True assert ask(x**2, Q.infinitesimal, Assume(x, Q.infinitesimal)) == True
def test_commutative(): """By default objects are Q.commutative that is why it returns True for both key=True and key=False""" x, y = symbols('xy') assert ask(x, Q.commutative) == True assert ask(x, Q.commutative, Assume(x, Q.commutative, False)) == False assert ask(x, Q.commutative, Assume(x, Q.complex)) == True assert ask(x, Q.commutative, Assume(x, Q.imaginary)) == True assert ask(x, Q.commutative, Assume(x, Q.real)) == True assert ask(x, Q.commutative, Assume(x, Q.positive)) == True assert ask(x, Q.commutative, Assume(y, Q.commutative, False)) == True assert ask(2*x, Q.commutative) == True assert ask(2*x, Q.commutative, Assume(x, Q.commutative, False)) == False assert ask(x + 1, Q.commutative) == True assert ask(x + 1, Q.commutative, Assume(x, Q.commutative, False)) == False assert ask(x**2, Q.commutative) == True assert ask(x**2, Q.commutative, Assume(x, Q.commutative, False)) == False assert ask(log(x), Q.commutative) == True
def Symbol(expr, assumptions): """ Handles Symbol. Example: >>> from sympy import Symbol, Assume, Q >>> from sympy.assumptions.handlers.calculus import AskBoundedHandler >>> from sympy.abc import x >>> a = AskBoundedHandler() >>> a.Symbol(x, Assume(x, Q.positive)) False >>> a.Symbol(x, Assume(x, Q.bounded)) True """ if Assume(expr, 'bounded') in conjuncts(assumptions): return True return False
def test_positive_xfail(): assert ask(1/(1 + x**2), Q.positive, Assume(x, Q.real)) == True
def test_positive(): x, y, z, w = symbols('xyzw') assert ask(x, Q.positive, Assume(x, Q.positive)) == True assert ask(x, Q.positive, Assume(x, Q.negative)) == False assert ask(x, Q.positive, Assume(x, Q.nonzero)) == None assert ask(-x, Q.positive, Assume(x, Q.positive)) == False assert ask(-x, Q.positive, Assume(x, Q.negative)) == True assert ask(x+y, Q.positive, Assume(x, Q.positive) & \ Assume(y, Q.positive)) == True assert ask(x+y, Q.positive, Assume(x, Q.positive) & \ Assume(y, Q.negative)) == None assert ask(2*x, Q.positive, Assume(x, Q.positive)) == True assumptions = Assume(x, Q.positive) & Assume(y, Q.negative) & \ Assume(z, Q.negative) & Assume(w, Q.positive) assert ask(x*y*z, Q.positive) == None assert ask(x*y*z, Q.positive, assumptions) == True assert ask(-x*y*z, Q.positive, assumptions) == False assert ask(x**2, Q.positive, Assume(x, Q.positive)) == True assert ask(x**2, Q.positive, Assume(x, Q.negative)) == True #exponential assert ask(exp(x), Q.positive, Assume(x, Q.real)) == True assert ask(x + exp(x), Q.positive, Assume(x, Q.real)) == None #absolute value assert ask(Abs(x), Q.positive) == None # Abs(0) = 0 assert ask(Abs(x), Q.positive, Assume(x, Q.positive)) == True
def test_prime(): x, y = symbols('x y') assert ask(x, Q.prime, Assume(x, Q.prime)) == True assert ask(x, Q.prime, Assume(x, Q.prime, False)) == False assert ask(x, Q.prime, Assume(x, Q.integer)) == None assert ask(x, Q.prime, Assume(x, Q.integer, False)) == False assert ask(2*x, Q.prime, Assume(x, Q.integer)) == False assert ask(x*y, Q.prime) == None assert ask(x*y, Q.prime, Assume(x, Q.prime)) == None assert ask(x*y, Q.prime, Assume(x, Q.integer) & \ Assume(y, Q.integer)) == False assert ask(x**2, Q.prime, Assume(x, Q.integer)) == False assert ask(x**2, Q.prime, Assume(x, Q.prime)) == False assert ask(x**y, Q.prime, Assume(x, Q.integer) & \ Assume(y, Q.integer)) == False
def test_odd(): x, y, z, t = symbols('x y z t') assert ask(x, Q.odd) == None assert ask(x, Q.odd, Assume(x, Q.odd)) == True assert ask(x, Q.odd, Assume(x, Q.integer)) == None assert ask(x, Q.odd, Assume(x, Q.integer, False)) == False assert ask(x, Q.odd, Assume(x, Q.rational)) == None assert ask(x, Q.odd, Assume(x, Q.positive)) == None assert ask(-x, Q.odd, Assume(x, Q.odd)) == True assert ask(2*x, Q.odd) == None assert ask(2*x, Q.odd, Assume(x, Q.integer)) == False assert ask(2*x, Q.odd, Assume(x, Q.odd)) == False assert ask(2*x, Q.odd, Assume(x, Q.irrational)) == False assert ask(2*x, Q.odd, Assume(x, Q.integer, False)) == None assert ask(3*x, Q.odd, Assume(x, Q.integer)) == None assert ask(x/3, Q.odd, Assume(x, Q.odd)) == None assert ask(x/3, Q.odd, Assume(x, Q.even)) == None assert ask(x+1, Q.odd, Assume(x, Q.even)) == True assert ask(x+2, Q.odd, Assume(x, Q.even)) == False assert ask(x+2, Q.odd, Assume(x, Q.odd)) == True assert ask(3-x, Q.odd, Assume(x, Q.odd)) == False assert ask(3-x, Q.odd, Assume(x, Q.even)) == True assert ask(3+x, Q.odd, Assume(x, Q.odd)) == False assert ask(3+x, Q.odd, Assume(x, Q.even)) == True assert ask(x+y, Q.odd, Assume(x, Q.odd) & Assume(y, Q.odd)) == False assert ask(x+y, Q.odd, Assume(x, Q.odd) & Assume(y, Q.even)) == True assert ask(x-y, Q.odd, Assume(x, Q.even) & Assume(y, Q.odd)) == True assert ask(x-y, Q.odd, Assume(x, Q.odd) & Assume(y, Q.odd)) == False assert ask(x+y+z, Q.odd, Assume(x, Q.odd) & Assume(y, Q.odd) & \ Assume(z, Q.even)) == False assert ask(x+y+z+t, Q.odd, Assume(x, Q.odd) & Assume(y, Q.odd) & \ Assume(z, Q.even) & Assume(t, Q.integer)) == None assert ask(2*x + 1, Q.odd, Assume(x, Q.integer)) == True assert ask(2*x + y, Q.odd, Assume(x, Q.integer) & Assume(y, Q.odd)) == True assert ask(2*x + y, Q.odd, Assume(x, Q.integer) & Assume(y, Q.even)) == False assert ask(2*x + y, Q.odd, Assume(x, Q.integer) & Assume(y, Q.integer)) == None assert ask(x*y, Q.odd, Assume(x, Q.odd) & Assume(y, Q.even)) == False assert ask(x*y, Q.odd, Assume(x, Q.odd) & Assume(y, Q.odd)) == True assert ask(2*x*y, Q.odd, Assume(x, Q.rational) & Assume(x, Q.rational)) == None assert ask(2*x*y, Q.odd, Assume(x, Q.irrational) & Assume(x, Q.irrational)) == None assert ask(Abs(x), Q.odd, Assume(x, Q.odd)) == True
def test_nonzero(): x, y = symbols('xy') assert ask(x, Q.nonzero) == None assert ask(x, Q.nonzero, Assume(x, Q.real)) == None assert ask(x, Q.nonzero, Assume(x, Q.positive)) == True assert ask(x, Q.nonzero, Assume(x, Q.negative)) == True assert ask(x, Q.nonzero, Assume(x, Q.negative) | Assume(x, Q.positive)) == True assert ask(x+y, Q.nonzero) == None assert ask(x+y, Q.nonzero, Assume(x, Q.positive) & Assume(y, Q.positive)) == True assert ask(x+y, Q.nonzero, Assume(x, Q.positive) & Assume(y, Q.negative)) == None assert ask(x+y, Q.nonzero, Assume(x, Q.negative) & Assume(y, Q.negative)) == True assert ask(2*x, Q.nonzero) == None assert ask(2*x, Q.nonzero, Assume(x, Q.positive)) == True assert ask(2*x, Q.nonzero, Assume(x, Q.negative)) == True assert ask(x*y, Q.nonzero, Assume(x, Q.nonzero)) == None assert ask(x*y, Q.nonzero, Assume(x, Q.nonzero) & Assume(y, Q.nonzero)) == True assert ask(Abs(x), Q.nonzero) == None assert ask(Abs(x), Q.nonzero, Assume(x, Q.nonzero)) == True
def test_negative(): x, y = symbols('xy') assert ask(x, Q.negative, Assume(x, Q.negative)) == True assert ask(x, Q.negative, Assume(x, Q.positive)) == False assert ask(x, Q.negative, Assume(x, Q.real, False)) == False assert ask(x, Q.negative, Assume(x, Q.prime)) == False assert ask(x, Q.negative, Assume(x, Q.prime, False)) == None assert ask(-x, Q.negative, Assume(x, Q.positive)) == True assert ask(-x, Q.negative, Assume(x, Q.positive, False)) == None assert ask(-x, Q.negative, Assume(x, Q.negative)) == False assert ask(-x, Q.negative, Assume(x, Q.positive)) == True assert ask(x-1, Q.negative, Assume(x, Q.negative)) == True assert ask(x+y, Q.negative) == None assert ask(x+y, Q.negative, Assume(x, Q.negative)) == None assert ask(x+y, Q.negative, Assume(x, Q.negative) &\ Assume(y, Q.negative)) == True assert ask(x**2, Q.negative) == None assert ask(x**2, Q.negative, Assume(x, Q.real)) == False assert ask(x**1.4, Q.negative, Assume(x, Q.real)) == None assert ask(x*y, Q.negative) == None assert ask(x*y, Q.negative, Assume(x, Q.positive) & \ Assume(y, Q.positive)) == False assert ask(x*y, Q.negative, Assume(x, Q.positive) & \ Assume(y, Q.negative)) == True assert ask(x*y, Q.negative, Assume(x, Q.complex) & \ Assume(y, Q.complex)) == None assert ask(x**y, Q.negative) == None assert ask(x**y, Q.negative, Assume(x, Q.negative) & \ Assume(y, Q.even)) == False assert ask(x**y, Q.negative, Assume(x, Q.negative) & \ Assume(y, Q.odd)) == True assert ask(x**y, Q.negative, Assume(x, Q.positive) & \ Assume(y, Q.integer)) == False assert ask(Abs(x), Q.negative) == False
def test_False(): """Test Assume object with False keys""" x = symbols('x') assump = Assume(x, 'integer', False) assert assump == ~Assume(x, 'integer')
def test_imaginary(): x, y, z = symbols('x y z') I = S.ImaginaryUnit assert ask(x, Q.imaginary) == None assert ask(x, Q.imaginary, Assume(x, Q.real)) == False assert ask(x, Q.imaginary, Assume(x, Q.prime)) == False assert ask(x+1, Q.imaginary, Assume(x, Q.real)) == False assert ask(x+1, Q.imaginary, Assume(x, Q.imaginary)) == False assert ask(x+I, Q.imaginary, Assume(x, Q.real)) == False assert ask(x+I, Q.imaginary, Assume(x, Q.imaginary)) == True assert ask(x+y, Q.imaginary, Assume(x, Q.imaginary) & \ Assume(y, Q.imaginary)) == True assert ask(x+y, Q.imaginary, Assume(x, Q.real) & \ Assume(y, Q.real)) == False assert ask(x+y, Q.imaginary, Assume(x, Q.imaginary) & \ Assume(y, Q.real)) == False assert ask(x+y, Q.imaginary, Assume(x, Q.complex) & \ Assume(y, Q.real)) == None assert ask(I*x, Q.imaginary, Assume(x, Q.real)) == True assert ask(I*x, Q.imaginary, Assume(x, Q.imaginary)) == False assert ask(I*x, Q.imaginary, Assume(x, Q.complex)) == None assert ask(x*y, Q.imaginary, Assume(x, Q.imaginary) & \ Assume(y, Q.real)) == True assert ask(x+y+z, Q.imaginary, Assume(x, Q.real) & \ Assume(y, Q.real) & Assume(z, Q.real)) == False assert ask(x+y+z, Q.imaginary, Assume(x, Q.real) & \ Assume(y, Q.real) & Assume(z, Q.imaginary)) == None assert ask(x+y+z, Q.imaginary, Assume(x, Q.real) & \ Assume(y, Q.imaginary) & Assume(z, Q.imaginary)) == False
def test_equal(): """Test for equality""" x = symbols('x') assert Assume(x, 'positive', True) == Assume(x, 'positive', True) assert Assume(x, 'positive', True) != Assume(x, 'positive', False) assert Assume(x, 'positive', False) == Assume(x, 'positive', False)
def test_even(): x, y, z, t = symbols('x y z t') assert ask(x, Q.even) == None assert ask(x, Q.even, Assume(x, Q.integer)) == None assert ask(x, Q.even, Assume(x, Q.integer, False)) == False assert ask(x, Q.even, Assume(x, Q.rational)) == None assert ask(x, Q.even, Assume(x, Q.positive)) == None assert ask(2*x, Q.even) == None assert ask(2*x, Q.even, Assume(x, Q.integer)) == True assert ask(2*x, Q.even, Assume(x, Q.even)) == True assert ask(2*x, Q.even, Assume(x, Q.irrational)) == False assert ask(2*x, Q.even, Assume(x, Q.odd)) == True assert ask(2*x, Q.even, Assume(x, Q.integer, False)) == None assert ask(3*x, Q.even, Assume(x, Q.integer)) == None assert ask(3*x, Q.even, Assume(x, Q.even)) == True assert ask(3*x, Q.even, Assume(x, Q.odd)) == False assert ask(x+1, Q.even, Assume(x, Q.odd)) == True assert ask(x+1, Q.even, Assume(x, Q.even)) == False assert ask(x+2, Q.even, Assume(x, Q.odd)) == False assert ask(x+2, Q.even, Assume(x, Q.even)) == True assert ask(7-x, Q.even, Assume(x, Q.odd)) == True assert ask(7+x, Q.even, Assume(x, Q.odd)) == True assert ask(x+y, Q.even, Assume(x, Q.odd) & Assume(y, Q.odd)) == True assert ask(x+y, Q.even, Assume(x, Q.odd) & Assume(y, Q.even)) == False assert ask(x+y, Q.even, Assume(x, Q.even) & Assume(y, Q.even)) == True assert ask(2*x + 1, Q.even, Assume(x, Q.integer)) == False assert ask(2*x*y, Q.even, Assume(x, Q.rational) & Assume(x, Q.rational)) == None assert ask(2*x*y, Q.even, Assume(x, Q.irrational) & Assume(x, Q.irrational)) == None assert ask(x+y+z, Q.even, Assume(x, Q.odd) & Assume(y, Q.odd) & \ Assume(z, Q.even)) == True assert ask(x+y+z+t, Q.even, Assume(x, Q.odd) & Assume(y, Q.odd) & \ Assume(z, Q.even) & Assume(t, Q.integer)) == None assert ask(Abs(x), Q.even, Assume(x, Q.even)) == True assert ask(Abs(x), Q.even, Assume(x, Q.even, False)) == None assert ask(re(x), Q.even, Assume(x, Q.even)) == True assert ask(re(x), Q.even, Assume(x, Q.even, False)) == None assert ask(im(x), Q.even, Assume(x, Q.even)) == True assert ask(im(x), Q.even, Assume(x, Q.real)) == True
def test_complex(): x, y = symbols('xy') assert ask(x, Q.complex) == None assert ask(x, Q.complex, Assume(x, Q.complex)) == True assert ask(x, Q.complex, Assume(y, Q.complex)) == None assert ask(x, Q.complex, Assume(x, Q.complex, False)) == False assert ask(x, Q.complex, Assume(x, Q.real)) == True assert ask(x, Q.complex, Assume(x, Q.real, False)) == None assert ask(x, Q.complex, Assume(x, Q.rational)) == True assert ask(x, Q.complex, Assume(x, Q.irrational)) == True assert ask(x, Q.complex, Assume(x, Q.positive)) == True assert ask(x, Q.complex, Assume(x, Q.imaginary)) == True # a+b assert ask(x+1, Q.complex, Assume(x, Q.complex)) == True assert ask(x+1, Q.complex, Assume(x, Q.real)) == True assert ask(x+1, Q.complex, Assume(x, Q.rational)) == True assert ask(x+1, Q.complex, Assume(x, Q.irrational)) == True assert ask(x+1, Q.complex, Assume(x, Q.imaginary)) == True assert ask(x+1, Q.complex, Assume(x, Q.integer)) == True assert ask(x+1, Q.complex, Assume(x, Q.even)) == True assert ask(x+1, Q.complex, Assume(x, Q.odd)) == True assert ask(x+y, Q.complex, Assume(x, Q.complex) & Assume(y, Q.complex)) == True assert ask(x+y, Q.complex, Assume(x, Q.real) & Assume(y, Q.imaginary)) == True # a*x +b assert ask(2*x+1, Q.complex, Assume(x, Q.complex)) == True assert ask(2*x+1, Q.complex, Assume(x, Q.real)) == True assert ask(2*x+1, Q.complex, Assume(x, Q.positive)) == True assert ask(2*x+1, Q.complex, Assume(x, Q.rational)) == True assert ask(2*x+1, Q.complex, Assume(x, Q.irrational)) == True assert ask(2*x+1, Q.complex, Assume(x, Q.imaginary)) == True assert ask(2*x+1, Q.complex, Assume(x, Q.integer)) == True assert ask(2*x+1, Q.complex, Assume(x, Q.even)) == True assert ask(2*x+1, Q.complex, Assume(x, Q.odd)) == True # x**2 assert ask(x**2, Q.complex, Assume(x, Q.complex)) == True assert ask(x**2, Q.complex, Assume(x, Q.real)) == True assert ask(x**2, Q.complex, Assume(x, Q.positive)) == True assert ask(x**2, Q.complex, Assume(x, Q.rational)) == True assert ask(x**2, Q.complex, Assume(x, Q.irrational)) == True assert ask(x**2, Q.complex, Assume(x, Q.imaginary)) == True assert ask(x**2, Q.complex, Assume(x, Q.integer)) == True assert ask(x**2, Q.complex, Assume(x, Q.even)) == True assert ask(x**2, Q.complex, Assume(x, Q.odd)) == True # 2**x assert ask(2**x, Q.complex, Assume(x, Q.complex)) == True assert ask(2**x, Q.complex, Assume(x, Q.real)) == True assert ask(2**x, Q.complex, Assume(x, Q.positive)) == True assert ask(2**x, Q.complex, Assume(x, Q.rational)) == True assert ask(2**x, Q.complex, Assume(x, Q.irrational)) == True assert ask(2**x, Q.complex, Assume(x, Q.imaginary)) == True assert ask(2**x, Q.complex, Assume(x, Q.integer)) == True assert ask(2**x, Q.complex, Assume(x, Q.even)) == True assert ask(2**x, Q.complex, Assume(x, Q.odd)) == True assert ask(x**y, Q.complex, Assume(x, Q.complex) & \ Assume(y, Q.complex)) == True # trigonometric expressions assert ask(sin(x), Q.complex) == True assert ask(sin(2*x + 1), Q.complex) == True assert ask(cos(x), Q.complex) == True assert ask(cos(2*x+1), Q.complex) == True # exponential assert ask(exp(x), Q.complex) == True assert ask(exp(x), Q.complex) == True # Q.complexes assert ask(Abs(x), Q.complex) == True assert ask(re(x), Q.complex) == True assert ask(im(x), Q.complex) == True
def test_real(): x, y = symbols('x y') assert ask(x, Q.real) == None assert ask(x, Q.real, Assume(x, Q.real)) == True assert ask(x, Q.real, Assume(x, Q.nonzero)) == True assert ask(x, Q.real, Assume(x, Q.positive)) == True assert ask(x, Q.real, Assume(x, Q.negative)) == True assert ask(x, Q.real, Assume(x, Q.integer)) == True assert ask(x, Q.real, Assume(x, Q.even)) == True assert ask(x, Q.real, Assume(x, Q.prime)) == True assert ask(x/sqrt(2), Q.real, Assume(x, Q.real)) == True assert ask(x/sqrt(-2), Q.real, Assume(x, Q.real)) == False I = S.ImaginaryUnit assert ask(x+1, Q.real, Assume(x, Q.real)) == True assert ask(x+I, Q.real, Assume(x, Q.real)) == False assert ask(x+I, Q.real, Assume(x, Q.complex)) == None assert ask(2*x, Q.real, Assume(x, Q.real)) == True assert ask(I*x, Q.real, Assume(x, Q.real)) == False assert ask(I*x, Q.real, Assume(x, Q.imaginary)) == True assert ask(I*x, Q.real, Assume(x, Q.complex)) == None assert ask(x**2, Q.real, Assume(x, Q.real)) == True assert ask(sqrt(x), Q.real, Assume(x, Q.negative)) == False assert ask(x**y, Q.real, Assume(x, Q.real) & Assume(y, Q.integer)) == True assert ask(x**y, Q.real, Assume(x, Q.real) & Assume(y, Q.real)) == None assert ask(x**y, Q.real, Assume(x, Q.positive) & \ Assume(y, Q.real)) == True # trigonometric functions assert ask(sin(x), Q.real) == None assert ask(cos(x), Q.real) == None assert ask(sin(x), Q.real, Assume(x, Q.real)) == True assert ask(cos(x), Q.real, Assume(x, Q.real)) == True # exponential function assert ask(exp(x), Q.real) == None assert ask(exp(x), Q.real, Assume(x, Q.real)) == True assert ask(x + exp(x), Q.real, Assume(x, Q.real)) == True # Q.complexes assert ask(re(x), Q.real) == True assert ask(im(x), Q.real) == True
def test_assume(): x = symbols('x') assump = Assume(x, 'integer') assert assump.expr == x assert assump.key == Q.integer
def test_integer(): x = symbols('x') assert ask(x, Q.integer) == None assert ask(x, Q.integer, Assume(x, Q.integer)) == True assert ask(x, Q.integer, Assume(x, Q.integer, False)) == False assert ask(x, Q.integer, Assume(x, Q.real, False)) == False assert ask(x, Q.integer, Assume(x, Q.positive, False)) == None assert ask(x, Q.integer, Assume(x, Q.even) | Assume(x, Q.odd)) == True assert ask(2*x, Q.integer, Assume(x, Q.integer)) == True assert ask(2*x, Q.integer, Assume(x, Q.even)) == True assert ask(2*x, Q.integer, Assume(x, Q.prime)) == True assert ask(2*x, Q.integer, Assume(x, Q.rational)) == None assert ask(2*x, Q.integer, Assume(x, Q.real)) == None assert ask(sqrt(2)*x, Q.integer, Assume(x, Q.integer)) == False assert ask(x/2, Q.integer, Assume(x, Q.odd)) == False assert ask(x/2, Q.integer, Assume(x, Q.even)) == True assert ask(x/3, Q.integer, Assume(x, Q.odd)) == None assert ask(x/3, Q.integer, Assume(x, Q.even)) == None
def test_bounded(): x, y = symbols('xy') assert ask(x, Q.bounded) == False assert ask(x, Q.bounded, Assume(x, Q.bounded)) == True assert ask(x, Q.bounded, Assume(y, Q.bounded)) == False assert ask(x, Q.bounded, Assume(x, Q.complex)) == False assert ask(x+1, Q.bounded) == False assert ask(x+1, Q.bounded, Assume(x, Q.bounded)) == True assert ask(x+y, Q.bounded) == None assert ask(x+y, Q.bounded, Assume(x, Q.bounded)) == False assert ask(x+1, Q.bounded, Assume(x, Q.bounded) & \ Assume(y, Q.bounded)) == True assert ask(2*x, Q.bounded) == False assert ask(2*x, Q.bounded, Assume(x, Q.bounded)) == True assert ask(x*y, Q.bounded) == None assert ask(x*y, Q.bounded, Assume(x, Q.bounded)) == False assert ask(x*y, Q.bounded, Assume(x, Q.bounded) & \ Assume(y, Q.bounded)) == True assert ask(x**2, Q.bounded) == False assert ask(2**x, Q.bounded) == False assert ask(2**x, Q.bounded, Assume(x, Q.bounded)) == True assert ask(x**x, Q.bounded) == False assert ask(Rational(1,2) ** x, Q.bounded) == True assert ask(x ** Rational(1,2), Q.bounded) == False # sign function assert ask(sign(x), Q.bounded) == True assert ask(sign(x), Q.bounded, Assume(x, Q.bounded, False)) == True # exponential functions assert ask(log(x), Q.bounded) == False assert ask(log(x), Q.bounded, Assume(x, Q.bounded)) == True assert ask(exp(x), Q.bounded) == False assert ask(exp(x), Q.bounded, Assume(x, Q.bounded)) == True assert ask(exp(2), Q.bounded) == True # trigonometric functions assert ask(sin(x), Q.bounded) == True assert ask(sin(x), Q.bounded, Assume(x, Q.bounded, False)) == True assert ask(cos(x), Q.bounded) == True assert ask(cos(x), Q.bounded, Assume(x, Q.bounded, False)) == True assert ask(2*sin(x), Q.bounded) == True assert ask(sin(x)**2, Q.bounded) == True assert ask(cos(x)**2, Q.bounded) == True assert ask(cos(x) + sin(x), Q.bounded) == True
def test_rational(): x, y = symbols('xy') assert ask(x, Q.rational, Assume(x, Q.integer)) == True assert ask(x, Q.rational, Assume(x, Q.irrational)) == False assert ask(x, Q.rational, Assume(x, Q.real)) == None assert ask(x, Q.rational, Assume(x, Q.positive)) == None assert ask(x, Q.rational, Assume(x, Q.negative)) == None assert ask(x, Q.rational, Assume(x, Q.nonzero)) == None assert ask(2*x, Q.rational, Assume(x, Q.rational)) == True assert ask(2*x, Q.rational, Assume(x, Q.integer)) == True assert ask(2*x, Q.rational, Assume(x, Q.even)) == True assert ask(2*x, Q.rational, Assume(x, Q.odd)) == True assert ask(2*x, Q.rational, Assume(x, Q.irrational)) == False assert ask(x/2, Q.rational, Assume(x, Q.rational)) == True assert ask(x/2, Q.rational, Assume(x, Q.integer)) == True assert ask(x/2, Q.rational, Assume(x, Q.even)) == True assert ask(x/2, Q.rational, Assume(x, Q.odd)) == True assert ask(x/2, Q.rational, Assume(x, Q.irrational)) == False assert ask(1/x, Q.rational, Assume(x, Q.rational)) == True assert ask(1/x, Q.rational, Assume(x, Q.integer)) == True assert ask(1/x, Q.rational, Assume(x, Q.even)) == True assert ask(1/x, Q.rational, Assume(x, Q.odd)) == True assert ask(1/x, Q.rational, Assume(x, Q.irrational)) == False assert ask(2/x, Q.rational, Assume(x, Q.rational)) == True assert ask(2/x, Q.rational, Assume(x, Q.integer)) == True assert ask(2/x, Q.rational, Assume(x, Q.even)) == True assert ask(2/x, Q.rational, Assume(x, Q.odd)) == True assert ask(2/x, Q.rational, Assume(x, Q.irrational)) == False # with multiple symbols assert ask(x*y, Q.rational, Assume(x, Q.irrational) & \ Assume(y, Q.irrational)) == None assert ask(y/x, Q.rational, Assume(x, Q.rational) & \ Assume(y, Q.rational)) == True assert ask(y/x, Q.rational, Assume(x, Q.integer) & \ Assume(y, Q.rational)) == True assert ask(y/x, Q.rational, Assume(x, Q.even) & \ Assume(y, Q.rational)) == True assert ask(y/x, Q.rational, Assume(x, Q.odd) & \ Assume(y, Q.rational)) == True assert ask(y/x, Q.rational, Assume(x, Q.irrational) & \ Assume(y, Q.rational)) == False
def test_pretty(): x = symbols('x') assert pretty(Assume(x, 'positive')) == "Assume(x, 'positive')"