def test_extended_real(): x = symbols('x') assert ask(Q.extended_real(x), Q.positive(x)) == True assert ask(Q.extended_real(-x), Q.positive(x)) == True assert ask(Q.extended_real(-x), Q.negative(x)) == True assert ask(Q.extended_real(x+S.Infinity), Q.real(x)) == True
def test_functions_in_assumptions(): from sympy.logic.boolalg import Equivalent, Xor x = symbols("x") assert ask(x, Q.negative, Q.real(x) >> Q.positive(x)) is False assert ask(x, Q.negative, Equivalent(Q.real(x), Q.positive(x))) is False assert ask(x, Q.negative, Xor(Q.real(x), Q.negative(x))) is False
def Pow(expr, assumptions): """ Real**Integer -> Real Positive**Real -> Real Real**(Integer/Even) -> Real if base is nonnegative Real**(Integer/Odd) -> Real Real**Imaginary -> ? Imaginary**Real -> ? Real**Real -> ? """ if expr.is_number: return AskRealHandler._number(expr, assumptions) if ask(Q.imaginary(expr.base), assumptions): if ask(Q.real(expr.exp), assumptions): if ask(Q.odd(expr.exp), assumptions): return False elif ask(Q.even(expr.exp), assumptions): return True elif ask(Q.real(expr.base), assumptions): if ask(Q.real(expr.exp), assumptions): if expr.exp.is_Rational and \ ask(Q.even(expr.exp.q), assumptions): return ask(Q.positive(expr.base), assumptions) elif ask(Q.integer(expr.exp), assumptions): return True elif ask(Q.positive(expr.base), assumptions): return True elif ask(Q.negative(expr.base), assumptions): return False
def test_float_1(): z = 1.0 assert ask(Q.commutative(z)) == True assert ask(Q.integer(z)) == True assert ask(Q.rational(z)) == True assert ask(Q.real(z)) == True assert ask(Q.complex(z)) == True assert ask(Q.irrational(z)) == False assert ask(Q.imaginary(z)) == False assert ask(Q.positive(z)) == True assert ask(Q.negative(z)) == False assert ask(Q.even(z)) == False assert ask(Q.odd(z)) == True assert ask(Q.bounded(z)) == True assert ask(Q.infinitesimal(z)) == False assert ask(Q.prime(z)) == False assert ask(Q.composite(z)) == True z = 7.2123 assert ask(Q.commutative(z)) == True assert ask(Q.integer(z)) == False assert ask(Q.rational(z)) == True assert ask(Q.real(z)) == True assert ask(Q.complex(z)) == True assert ask(Q.irrational(z)) == False assert ask(Q.imaginary(z)) == False assert ask(Q.positive(z)) == True assert ask(Q.negative(z)) == False assert ask(Q.even(z)) == False assert ask(Q.odd(z)) == False assert ask(Q.bounded(z)) == True assert ask(Q.infinitesimal(z)) == False assert ask(Q.prime(z)) == False assert ask(Q.composite(z)) == False
def refine_atan2(expr, assumptions): """ Handler for the atan2 function Examples ======== >>> from sympy import Symbol, Q, refine, atan2 >>> from sympy.assumptions.refine import refine_atan2 >>> from sympy.abc import x, y >>> refine_atan2(atan2(y,x), Q.real(y) & Q.positive(x)) atan(y/x) >>> refine_atan2(atan2(y,x), Q.negative(y) & Q.negative(x)) atan(y/x) - pi >>> refine_atan2(atan2(y,x), Q.positive(y) & Q.negative(x)) atan(y/x) + pi """ from sympy.functions.elementary.complexes import atan from sympy.core import S y, x = expr.args if ask(Q.real(y) & Q.positive(x), assumptions): return atan(y / x) elif ask(Q.negative(y) & Q.negative(x), assumptions): return atan(y / x) - S.Pi elif ask(Q.positive(y) & Q.negative(x), assumptions): return atan(y / x) + S.Pi else: return expr
def test_refine(): m0 = OperationsOnlyMatrix([[Abs(x)**2, sqrt(x**2)], [sqrt(x**2)*Abs(y)**2, sqrt(y**2)*Abs(x)**2]]) m1 = m0.refine(Q.real(x) & Q.real(y)) assert m1 == Matrix([[x**2, Abs(x)], [y**2*Abs(x), x**2*Abs(y)]]) m1 = m0.refine(Q.positive(x) & Q.positive(y)) assert m1 == Matrix([[x**2, x], [x*y**2, x**2*y]]) m1 = m0.refine(Q.negative(x) & Q.negative(y)) assert m1 == Matrix([[x**2, -x], [-x*y**2, -x**2*y]])
def Pow(expr, assumptions): """ Real**Integer -> Real Positive**Real -> Real Real**(Integer/Even) -> Real if base is nonnegative Real**(Integer/Odd) -> Real Imaginary**(Integer/Even) -> Real Imaginary**(Integer/Odd) -> not Real Imaginary**Real -> ? since Real could be 0 (giving real) or 1 (giving imaginary) b**Imaginary -> Real if log(b) is imaginary and b != 0 and exponent != integer multiple of I*pi/log(b) Real**Real -> ? e.g. sqrt(-1) is imaginary and sqrt(2) is not """ if expr.is_number: return AskRealHandler._number(expr, assumptions) if expr.base.func == exp: if ask(Q.imaginary(expr.base.args[0]), assumptions): if ask(Q.imaginary(expr.exp), assumptions): return True # If the i = (exp's arg)/(I*pi) is an integer or half-integer # multiple of I*pi then 2*i will be an integer. In addition, # exp(i*I*pi) = (-1)**i so the overall realness of the expr # can be determined by replacing exp(i*I*pi) with (-1)**i. i = expr.base.args[0]/I/pi if ask(Q.integer(2*i), assumptions): return ask(Q.real(((-1)**i)**expr.exp), assumptions) return if ask(Q.imaginary(expr.base), assumptions): if ask(Q.integer(expr.exp), assumptions): odd = ask(Q.odd(expr.exp), assumptions) if odd is not None: return not odd return if ask(Q.imaginary(expr.exp), assumptions): imlog = ask(Q.imaginary(log(expr.base)), assumptions) if imlog is not None: # I**i -> real, log(I) is imag; # (2*I)**i -> complex, log(2*I) is not imag return imlog if ask(Q.real(expr.base), assumptions): if ask(Q.real(expr.exp), assumptions): if expr.exp.is_Rational and \ ask(Q.even(expr.exp.q), assumptions): return ask(Q.positive(expr.base), assumptions) elif ask(Q.integer(expr.exp), assumptions): return True elif ask(Q.positive(expr.base), assumptions): return True elif ask(Q.negative(expr.base), assumptions): return False
def test_I(): I = S.ImaginaryUnit z = I assert ask(Q.commutative(z)) == True assert ask(Q.integer(z)) == False assert ask(Q.rational(z)) == False assert ask(Q.real(z)) == False assert ask(Q.complex(z)) == True assert ask(Q.irrational(z)) == False assert ask(Q.imaginary(z)) == True assert ask(Q.positive(z)) == False assert ask(Q.negative(z)) == False assert ask(Q.even(z)) == False assert ask(Q.odd(z)) == False assert ask(Q.bounded(z)) == True assert ask(Q.infinitesimal(z)) == False assert ask(Q.prime(z)) == False assert ask(Q.composite(z)) == False z = 1 + I assert ask(Q.commutative(z)) == True assert ask(Q.integer(z)) == False assert ask(Q.rational(z)) == False assert ask(Q.real(z)) == False assert ask(Q.complex(z)) == True assert ask(Q.irrational(z)) == False assert ask(Q.imaginary(z)) == False assert ask(Q.positive(z)) == False assert ask(Q.negative(z)) == False assert ask(Q.even(z)) == False assert ask(Q.odd(z)) == False assert ask(Q.bounded(z)) == True assert ask(Q.infinitesimal(z)) == False assert ask(Q.prime(z)) == False assert ask(Q.composite(z)) == False z = I*(1+I) assert ask(Q.commutative(z)) == True assert ask(Q.integer(z)) == False assert ask(Q.rational(z)) == False assert ask(Q.real(z)) == False assert ask(Q.complex(z)) == True assert ask(Q.irrational(z)) == False assert ask(Q.imaginary(z)) == False assert ask(Q.positive(z)) == False assert ask(Q.negative(z)) == False assert ask(Q.even(z)) == False assert ask(Q.odd(z)) == False assert ask(Q.bounded(z)) == True assert ask(Q.infinitesimal(z)) == False assert ask(Q.prime(z)) == False assert ask(Q.composite(z)) == False
def test_negative(): x, y = symbols('x,y') assert ask(Q.negative(x), Q.negative(x)) == True assert ask(Q.negative(x), Q.positive(x)) == False assert ask(Q.negative(x), ~Q.real(x)) == False assert ask(Q.negative(x), Q.prime(x)) == False assert ask(Q.negative(x), ~Q.prime(x)) == None assert ask(Q.negative(-x), Q.positive(x)) == True assert ask(Q.negative(-x), ~Q.positive(x)) == None assert ask(Q.negative(-x), Q.negative(x)) == False assert ask(Q.negative(-x), Q.positive(x)) == True assert ask(Q.negative(x-1), Q.negative(x)) == True assert ask(Q.negative(x+y)) == None assert ask(Q.negative(x+y), Q.negative(x)) == None assert ask(Q.negative(x+y), Q.negative(x) & Q.negative(y)) == True assert ask(Q.negative(x**2)) == None assert ask(Q.negative(x**2), Q.real(x)) == False assert ask(Q.negative(x**1.4), Q.real(x)) == None assert ask(Q.negative(x*y)) == None assert ask(Q.negative(x*y), Q.positive(x) & Q.positive(y)) == False assert ask(Q.negative(x*y), Q.positive(x) & Q.negative(y)) == True assert ask(Q.negative(x*y), Q.complex(x) & Q.complex(y)) == None assert ask(Q.negative(x**y)) == None assert ask(Q.negative(x**y), Q.negative(x) & Q.even(y)) == False assert ask(Q.negative(x**y), Q.negative(x) & Q.odd(y)) == True assert ask(Q.negative(x**y), Q.positive(x) & Q.integer(y)) == False assert ask(Q.negative(Abs(x))) == False
def Pow(expr, assumptions): """ Imaginary**integer -> Imaginary if integer % 2 == 1 Imaginary**integer -> real if integer % 2 == 0 Imaginary**Imaginary -> ? Imaginary**Real -> ? """ if expr.is_number: return AskImaginaryHandler._number(expr, assumptions) if ask(Q.imaginary(expr.base), assumptions): if ask(Q.real(expr.exp), assumptions): if ask(Q.odd(expr.exp), assumptions): return True elif ask(Q.even(expr.exp), assumptions): return False elif ask(Q.real(expr.base), assumptions): if ask(Q.real(expr.exp), assumptions): if expr.exp.is_Rational and \ ask(Q.even(expr.exp.q), assumptions): return ask(Q.negative(expr.base),assumptions) elif ask(Q.integer(expr.exp), assumptions): return False elif ask(Q.positive(expr.base), assumptions): return False elif ask(Q.negative(expr.base), assumptions): return True
def log(expr, assumptions): r = ask(Q.real(expr.args[0]), assumptions) if r is not True: return r if ask(Q.positive(expr.args[0] - 1), assumptions): return True if ask(Q.negative(expr.args[0] - 1), assumptions): return False
def refine_atan2(expr, assumptions): """ Handler for the atan2 function Examples ======== >>> from sympy import Symbol, Q, refine, atan2 >>> from sympy.assumptions.refine import refine_atan2 >>> from sympy.abc import x, y >>> refine_atan2(atan2(y,x), Q.real(y) & Q.positive(x)) atan(y/x) >>> refine_atan2(atan2(y,x), Q.negative(y) & Q.negative(x)) atan(y/x) - pi >>> refine_atan2(atan2(y,x), Q.positive(y) & Q.negative(x)) atan(y/x) + pi >>> refine_atan2(atan2(y,x), Q.zero(y) & Q.negative(x)) pi >>> refine_atan2(atan2(y,x), Q.positive(y) & Q.zero(x)) pi/2 >>> refine_atan2(atan2(y,x), Q.negative(y) & Q.zero(x)) -pi/2 >>> refine_atan2(atan2(y,x), Q.zero(y) & Q.zero(x)) nan """ from sympy.functions.elementary.trigonometric import atan from sympy.core import S y, x = expr.args if ask(Q.real(y) & Q.positive(x), assumptions): return atan(y / x) elif ask(Q.negative(y) & Q.negative(x), assumptions): return atan(y / x) - S.Pi elif ask(Q.positive(y) & Q.negative(x), assumptions): return atan(y / x) + S.Pi elif ask(Q.zero(y) & Q.negative(x), assumptions): return S.Pi elif ask(Q.positive(y) & Q.zero(x), assumptions): return S.Pi / 2 elif ask(Q.negative(y) & Q.zero(x), assumptions): return -S.Pi / 2 elif ask(Q.zero(y) & Q.zero(x), assumptions): return S.NaN else: return expr
def Pow(expr, assumptions): if expr.is_number: return expr.evalf() > 0 if ask(Q.positive(expr.base), assumptions): return True if ask(Q.negative(expr.base), assumptions): if ask(Q.even(expr.exp), assumptions): return True if ask(Q.even(expr.exp), assumptions): return False
def Add(expr, assumptions): if expr.is_number: return AskPositiveHandler._number(expr, assumptions) for arg in expr.args: if ask(Q.positive(arg), assumptions) is not True: break else: # if all argument's are positive return True
def test_real(): x, y = symbols('x,y') assert ask(Q.real(x)) == None assert ask(Q.real(x), Q.real(x)) == True assert ask(Q.real(x), Q.nonzero(x)) == True assert ask(Q.real(x), Q.positive(x)) == True assert ask(Q.real(x), Q.negative(x)) == True assert ask(Q.real(x), Q.integer(x)) == True assert ask(Q.real(x), Q.even(x)) == True assert ask(Q.real(x), Q.prime(x)) == True assert ask(Q.real(x/sqrt(2)), Q.real(x)) == True assert ask(Q.real(x/sqrt(-2)), Q.real(x)) == False I = S.ImaginaryUnit assert ask(Q.real(x+1), Q.real(x)) == True assert ask(Q.real(x+I), Q.real(x)) == False assert ask(Q.real(x+I), Q.complex(x)) == None assert ask(Q.real(2*x), Q.real(x)) == True assert ask(Q.real(I*x), Q.real(x)) == False assert ask(Q.real(I*x), Q.imaginary(x)) == True assert ask(Q.real(I*x), Q.complex(x)) == None assert ask(Q.real(x**2), Q.real(x)) == True assert ask(Q.real(sqrt(x)), Q.negative(x)) == False assert ask(Q.real(x**y), Q.real(x) & Q.integer(y)) == True assert ask(Q.real(x**y), Q.real(x) & Q.real(y)) == None assert ask(Q.real(x**y), Q.positive(x) & Q.real(y)) == True # trigonometric functions assert ask(Q.real(sin(x))) == None assert ask(Q.real(cos(x))) == None assert ask(Q.real(sin(x)), Q.real(x)) == True assert ask(Q.real(cos(x)), Q.real(x)) == True # exponential function assert ask(Q.real(exp(x))) == None assert ask(Q.real(exp(x)), Q.real(x)) == True assert ask(Q.real(x + exp(x)), Q.real(x)) == True # Q.complexes assert ask(Q.real(re(x))) == True assert ask(Q.real(im(x))) == True
def test_real(): x, y = symbols('x,y') assert ask(Q.real(x)) == None assert ask(Q.real(x), Q.real(x)) == True assert ask(Q.real(x), Q.nonzero(x)) == True assert ask(Q.real(x), Q.positive(x)) == True assert ask(Q.real(x), Q.negative(x)) == True assert ask(Q.real(x), Q.integer(x)) == True assert ask(Q.real(x), Q.even(x)) == True assert ask(Q.real(x), Q.prime(x)) == True assert ask(Q.real(x / sqrt(2)), Q.real(x)) == True assert ask(Q.real(x / sqrt(-2)), Q.real(x)) == False I = S.ImaginaryUnit assert ask(Q.real(x + 1), Q.real(x)) == True assert ask(Q.real(x + I), Q.real(x)) == False assert ask(Q.real(x + I), Q.complex(x)) == None assert ask(Q.real(2 * x), Q.real(x)) == True assert ask(Q.real(I * x), Q.real(x)) == False assert ask(Q.real(I * x), Q.imaginary(x)) == True assert ask(Q.real(I * x), Q.complex(x)) == None assert ask(Q.real(x**2), Q.real(x)) == True assert ask(Q.real(sqrt(x)), Q.negative(x)) == False assert ask(Q.real(x**y), Q.real(x) & Q.integer(y)) == True assert ask(Q.real(x**y), Q.real(x) & Q.real(y)) == None assert ask(Q.real(x**y), Q.positive(x) & Q.real(y)) == True # trigonometric functions assert ask(Q.real(sin(x))) == None assert ask(Q.real(cos(x))) == None assert ask(Q.real(sin(x)), Q.real(x)) == True assert ask(Q.real(cos(x)), Q.real(x)) == True # exponential function assert ask(Q.real(exp(x))) == None assert ask(Q.real(exp(x)), Q.real(x)) == True assert ask(Q.real(x + exp(x)), Q.real(x)) == True # Q.complexes assert ask(Q.real(re(x))) == True assert ask(Q.real(im(x))) == True
def Pow(expr, assumptions): if expr.is_number: return AskEvenHandler._number(expr, assumptions) if ask(Q.integer(expr.exp), assumptions): if ask(Q.positive(expr.exp), assumptions): return ask(Q.even(expr.base), assumptions) elif ask(~Q.negative(expr.exp) & Q.odd(expr.base), assumptions): return False elif expr.base is S.NegativeOne: return False
def Basic(expr, assumptions): _positive = ask(Q.positive(expr), assumptions) if _positive: _integer = ask(Q.integer(expr), assumptions) if _integer: _prime = ask(Q.prime(expr), assumptions) if _prime is None: return return not _prime else: return _integer else: return _positive
def Mul(expr, assumptions): if expr.is_number: return AskPositiveHandler._number(expr, assumptions) result = True for arg in expr.args: if ask(Q.positive(arg), assumptions): continue elif ask(Q.negative(arg), assumptions): result = result ^ True else: return return result
def _(expr, assumptions): if expr.is_number: return _EvenPredicate_number(expr, assumptions) if ask(Q.integer(expr.exp), assumptions): if ask(Q.positive(expr.exp), assumptions): return ask(Q.even(expr.base), assumptions) elif ask(~Q.negative(expr.exp) & Q.odd(expr.base), assumptions): return False elif expr.base is S.NegativeOne: return False
def _(expr, assumptions): """ * Imaginary**Odd -> Imaginary * Imaginary**Even -> Real * b**Imaginary -> !Imaginary if exponent is an integer multiple of I*pi/log(b) * Imaginary**Real -> ? * Positive**Real -> Real * Negative**Integer -> Real * Negative**(Integer/2) -> Imaginary * Negative**Real -> not Imaginary if exponent is not Rational """ if expr.is_number: return _Imaginary_number(expr, assumptions) if expr.base == E: a = expr.exp / I / pi return ask(Q.integer(2 * a) & ~Q.integer(a), assumptions) if expr.base.func == exp or (expr.base.is_Pow and expr.base.base == E): if ask(Q.imaginary(expr.base.exp), assumptions): if ask(Q.imaginary(expr.exp), assumptions): return False i = expr.base.exp / I / pi if ask(Q.integer(2 * i), assumptions): return ask(Q.imaginary((S.NegativeOne**i)**expr.exp), assumptions) if ask(Q.imaginary(expr.base), assumptions): if ask(Q.integer(expr.exp), assumptions): odd = ask(Q.odd(expr.exp), assumptions) if odd is not None: return odd return if ask(Q.imaginary(expr.exp), assumptions): imlog = ask(Q.imaginary(log(expr.base)), assumptions) if imlog is not None: # I**i -> real; (2*I)**i -> complex ==> not imaginary return False if ask(Q.real(expr.base) & Q.real(expr.exp), assumptions): if ask(Q.positive(expr.base), assumptions): return False else: rat = ask(Q.rational(expr.exp), assumptions) if not rat: return rat if ask(Q.integer(expr.exp), assumptions): return False else: half = ask(Q.integer(2 * expr.exp), assumptions) if half: return ask(Q.negative(expr.base), assumptions) return half
def _(expr, assumptions): if expr.is_number: return _PositivePredicate_number(expr, assumptions) if ask(Q.positive(expr.base), assumptions): if ask(Q.real(expr.exp), assumptions): return True if ask(Q.negative(expr.base), assumptions): if ask(Q.even(expr.exp), assumptions): return True if ask(Q.odd(expr.exp), assumptions): return False
def Pow(expr, assumptions): if expr.is_number: return AskPositiveHandler._number(expr, assumptions) if ask(Q.positive(expr.base), assumptions): if ask(Q.real(expr.exp), assumptions): return True if ask(Q.negative(expr.base), assumptions): if ask(Q.even(expr.exp), assumptions): return True if ask(Q.odd(expr.exp), assumptions): return False
def _(expr, assumptions): if expr.is_number: return _PositivePredicate_number(expr, assumptions) result = True for arg in expr.args: if ask(Q.positive(arg), assumptions): continue elif ask(Q.negative(arg), assumptions): result = result ^ True else: return return result
def Mul(expr, assumptions): if expr.is_number: return AskNegativeHandler._number(expr, assumptions) result = None for arg in expr.args: if result is None: result = False if ask(Q.negative(arg), assumptions): result = not result elif ask(Q.positive(arg), assumptions): pass else: return return result
def Add(expr, assumptions): if expr.is_number: return AskPositiveHandler._number(expr, assumptions) nonneg = 0 for arg in expr.args: if ask(Q.positive(arg), assumptions) is not True: if ask(Q.negative(arg), assumptions) is False: nonneg += 1 else: break else: if nonneg < len(expr.args): return True
def refine_sign(expr, assumptions): """ Handler for sign. Examples ======== >>> from sympy.assumptions.refine import refine_sign >>> from sympy import Symbol, Q, sign, im >>> x = Symbol('x', real = True) >>> expr = sign(x) >>> refine_sign(expr, Q.positive(x) & Q.nonzero(x)) 1 >>> refine_sign(expr, Q.negative(x) & Q.nonzero(x)) -1 >>> refine_sign(expr, Q.zero(x)) 0 >>> y = Symbol('y', imaginary = True) >>> expr = sign(y) >>> refine_sign(expr, Q.positive(im(y))) I >>> refine_sign(expr, Q.negative(im(y))) -I """ arg = expr.args[0] if ask(Q.zero(arg), assumptions): return S.Zero if ask(Q.real(arg)): if ask(Q.positive(arg), assumptions): return S.One if ask(Q.negative(arg), assumptions): return S.NegativeOne if ask(Q.imaginary(arg)): arg_re, arg_im = arg.as_real_imag() if ask(Q.positive(arg_im), assumptions): return S.ImaginaryUnit if ask(Q.negative(arg_im), assumptions): return -S.ImaginaryUnit return expr
def _(expr, assumptions): if expr.is_number: return _NegativePredicate_number(expr, assumptions) result = None for arg in expr.args: if result is None: result = False if ask(Q.negative(arg), assumptions): result = not result elif ask(Q.positive(arg), assumptions): pass else: return return result
def log(expr, assumptions): if ask(Q.real(expr.args[0]), assumptions): if ask(Q.positive(expr.args[0]), assumptions): return False return # XXX it should be enough to do # return ask(Q.nonpositive(expr.args[0]), assumptions) # but ask(Q.nonpositive(exp(x)), Q.imaginary(x)) -> None; # it should return True since exp(x) will be either 0 or complex if expr.args[0].func == exp: if expr.args[0].args[0] in [I, -I]: return True im = ask(Q.imaginary(expr.args[0]), assumptions) if im is False: return False
def simplify_multiple_exp_sum(expr, do_simplify=False, optims=None): if optims is None: # optims = sympy.codegen.rewriting.optims_c99 + (logsumexp_2terms_opt,) optims = ( sympy.codegen.rewriting.log1p_opt, logsumexp_2terms_opt, ) if not (ask(Q.positive(expr)) or ask(Q.negative(expr))): if expr.args: return expr.func(*[ simplify_multiple_exp_sum(arg, do_simplify, optims) for arg in expr.args ]) return expr sign = 1 if ask(Q.positive(expr)) else -1 # expand log so that the resulting expression is a sum # given it is a multiplication/division before # expand_log apparently is not aware of assumptions given by a context manager # Therefore, use the force optin for now. log_expr = sy.expand_log(sy.log(sign * expr), force=True) log_expr = sympy.codegen.rewriting.optimize(log_expr, optims) val = sign * sy.exp(log_expr, evaluate=False) return val
def Pow(expr, assumptions): """ Real ** Even -> NonNegative Real ** Odd -> same_as_base NonNegative ** Positive -> NonNegative """ if expr.is_number: return AskNegativeHandler._number(expr, assumptions) if ask(Q.real(expr.base), assumptions): if ask(Q.positive(expr.base), assumptions): return False if ask(Q.even(expr.exp), assumptions): return False if ask(Q.odd(expr.exp), assumptions): return ask(Q.negative(expr.base), assumptions)
def Add(expr, assumptions): """ Return True if expr is bounded, False if not and None if unknown. TRUTH TABLE B U ? + - x + - x +---+-----+-----+ B | B | U |? ? ?| legend: +---+-----+-----+ B = Bounded + |U ? ?|U ? ?| U = Unbounded U - |? U ?|? U ?| ? = unknown boundedness x |? ? ?|? ? ?| + = positive sign +-----+--+--+ - = negative sign ? |? ? ?| x = sign unknown +--+--+ All Bounded -> True 1 Unbounded and the rest Bounded -> False >1 Unbounded, all with same known sign -> False Any Unknown and unknown sign -> None Else -> None When the signs are not the same you can have an undefined result as in oo - oo, hence 'bounded' is also undefined. """ sign = -1 # sign of unknown or unbounded result = True for arg in expr.args: _bounded = ask(Q.bounded(arg), assumptions) if _bounded: continue s = ask(Q.positive(arg), assumptions) # if there has been more than one sign or if the sign of this arg # is None and Bounded is None or there was already # an unknown sign, return None if sign != -1 and s != sign or \ s == None and (s == _bounded or s == sign): return None else: sign = s # once False, do not change if result is not False: result = _bounded return result
def test_bounded(): x, y = symbols('x,y') assert ask(Q.bounded(x)) == False assert ask(Q.bounded(x), Q.bounded(x)) == True assert ask(Q.bounded(x), Q.bounded(y)) == False assert ask(Q.bounded(x), Q.complex(x)) == False assert ask(Q.bounded(x + 1)) == False assert ask(Q.bounded(x + 1), Q.bounded(x)) == True assert ask(Q.bounded(x + y)) == None assert ask(Q.bounded(x + y), Q.bounded(x)) == False assert ask(Q.bounded(x + 1), Q.bounded(x) & Q.bounded(y)) == True assert ask(Q.bounded(2 * x)) == False assert ask(Q.bounded(2 * x), Q.bounded(x)) == True assert ask(Q.bounded(x * y)) == None assert ask(Q.bounded(x * y), Q.bounded(x)) == False assert ask(Q.bounded(x * y), Q.bounded(x) & Q.bounded(y)) == True assert ask(Q.bounded(x**2)) == False assert ask(Q.bounded(2**x)) == False assert ask(Q.bounded(2**x), Q.bounded(x)) == True assert ask(Q.bounded(x**x)) == False assert ask(Q.bounded(Rational(1, 2)**x)) == None assert ask(Q.bounded(Rational(1, 2)**x), Q.positive(x)) == True assert ask(Q.bounded(Rational(1, 2)**x), Q.negative(x)) == False assert ask(Q.bounded(sqrt(x))) == False # sign function assert ask(Q.bounded(sign(x))) == True assert ask(Q.bounded(sign(x)), ~Q.bounded(x)) == True # exponential functions assert ask(Q.bounded(log(x))) == False assert ask(Q.bounded(log(x)), Q.bounded(x)) == True assert ask(Q.bounded(exp(x))) == False assert ask(Q.bounded(exp(x)), Q.bounded(x)) == True assert ask(Q.bounded(exp(2))) == True # trigonometric functions assert ask(Q.bounded(sin(x))) == True assert ask(Q.bounded(sin(x)), ~Q.bounded(x)) == True assert ask(Q.bounded(cos(x))) == True assert ask(Q.bounded(cos(x)), ~Q.bounded(x)) == True assert ask(Q.bounded(2 * sin(x))) == True assert ask(Q.bounded(sin(x)**2)) == True assert ask(Q.bounded(cos(x)**2)) == True assert ask(Q.bounded(cos(x) + sin(x))) == True
def test_bounded(): x, y = symbols('x,y') assert ask(Q.bounded(x)) == False assert ask(Q.bounded(x), Q.bounded(x)) == True assert ask(Q.bounded(x), Q.bounded(y)) == False assert ask(Q.bounded(x), Q.complex(x)) == False assert ask(Q.bounded(x+1)) == False assert ask(Q.bounded(x+1), Q.bounded(x)) == True assert ask(Q.bounded(x+y)) == None assert ask(Q.bounded(x+y), Q.bounded(x)) == False assert ask(Q.bounded(x+1), Q.bounded(x) & Q.bounded(y)) == True assert ask(Q.bounded(2*x)) == False assert ask(Q.bounded(2*x), Q.bounded(x)) == True assert ask(Q.bounded(x*y)) == None assert ask(Q.bounded(x*y), Q.bounded(x)) == False assert ask(Q.bounded(x*y), Q.bounded(x) & Q.bounded(y)) == True assert ask(Q.bounded(x**2)) == False assert ask(Q.bounded(2**x)) == False assert ask(Q.bounded(2**x), Q.bounded(x)) == True assert ask(Q.bounded(x**x)) == False assert ask(Q.bounded(Rational(1,2) ** x)) == None assert ask(Q.bounded(Rational(1,2) ** x), Q.positive(x)) == True assert ask(Q.bounded(Rational(1,2) ** x), Q.negative(x)) == False assert ask(Q.bounded(sqrt(x))) == False # sign function assert ask(Q.bounded(sign(x))) == True assert ask(Q.bounded(sign(x)), ~Q.bounded(x)) == True # exponential functions assert ask(Q.bounded(log(x))) == False assert ask(Q.bounded(log(x)), Q.bounded(x)) == True assert ask(Q.bounded(exp(x))) == False assert ask(Q.bounded(exp(x)), Q.bounded(x)) == True assert ask(Q.bounded(exp(2))) == True # trigonometric functions assert ask(Q.bounded(sin(x))) == True assert ask(Q.bounded(sin(x)), ~Q.bounded(x)) == True assert ask(Q.bounded(cos(x))) == True assert ask(Q.bounded(cos(x)), ~Q.bounded(x)) == True assert ask(Q.bounded(2*sin(x))) == True assert ask(Q.bounded(sin(x)**2)) == True assert ask(Q.bounded(cos(x)**2)) == True assert ask(Q.bounded(cos(x) + sin(x))) == True
def Pow(expr, assumptions): """ Imaginary**Odd -> Imaginary Imaginary**Even -> Real b**Imaginary -> !Imaginary if exponent is an integer multiple of I*pi/log(b) Imaginary**Real -> ? Positive**Real -> Real Negative**Integer -> Real Negative**(Integer/2) -> Imaginary Negative**Real -> not Imaginary if exponent is not Rational """ if expr.is_number: return AskImaginaryHandler._number(expr, assumptions) if expr.base.func == exp: if ask(Q.imaginary(expr.base.args[0]), assumptions): if ask(Q.imaginary(expr.exp), assumptions): return False i = expr.base.args[0]/I/pi if ask(Q.integer(2*i), assumptions): return ask(Q.imaginary(((-1)**i)**expr.exp), assumptions) if ask(Q.imaginary(expr.base), assumptions): if ask(Q.integer(expr.exp), assumptions): odd = ask(Q.odd(expr.exp), assumptions) if odd is not None: return odd return if ask(Q.imaginary(expr.exp), assumptions): imlog = ask(Q.imaginary(log(expr.base)), assumptions) if imlog is not None: return False # I**i -> real; (2*I)**i -> complex ==> not imaginary if ask(Q.real(expr.base) & Q.real(expr.exp), assumptions): if ask(Q.positive(expr.base), assumptions): return False else: rat = ask(Q.rational(expr.exp), assumptions) if not rat: return rat if ask(Q.integer(expr.exp), assumptions): return False else: half = ask(Q.integer(2*expr.exp), assumptions) if half: return ask(Q.negative(expr.base), assumptions) return half
def Pow(expr, assumptions): """ Imaginary**Odd -> Imaginary Imaginary**Even -> Real b**Imaginary -> !Imaginary if exponent is an integer multiple of I*pi/log(b) Imaginary**Real -> ? Positive**Real -> Real Negative**Integer -> Real Negative**(Integer/2) -> Imaginary Negative**Real -> not Imaginary if exponent is not Rational """ if expr.is_number: return AskImaginaryHandler._number(expr, assumptions) if expr.base.func == exp: if ask(Q.imaginary(expr.base.args[0]), assumptions): if ask(Q.imaginary(expr.exp), assumptions): return False i = expr.base.args[0] / I / pi if ask(Q.integer(2 * i), assumptions): return ask(Q.imaginary(((-1)**i)**expr.exp), assumptions) if ask(Q.imaginary(expr.base), assumptions): if ask(Q.integer(expr.exp), assumptions): odd = ask(Q.odd(expr.exp), assumptions) if odd is not None: return odd return if ask(Q.imaginary(expr.exp), assumptions): imlog = ask(Q.imaginary(log(expr.base)), assumptions) if imlog is not None: return False # I**i -> real; (2*I)**i -> complex ==> not imaginary if ask(Q.real(expr.base) & Q.real(expr.exp), assumptions): if ask(Q.positive(expr.base), assumptions): return False else: rat = ask(Q.rational(expr.exp), assumptions) if not rat: return rat if ask(Q.integer(expr.exp), assumptions): return False else: half = ask(Q.integer(2 * expr.exp), assumptions) if half: return ask(Q.negative(expr.base), assumptions) return half
def test_Rational_number(): r = Rational(3, 4) assert ask(Q.commutative(r)) == True assert ask(Q.integer(r)) == False assert ask(Q.rational(r)) == True assert ask(Q.real(r)) == True assert ask(Q.complex(r)) == True assert ask(Q.irrational(r)) == False assert ask(Q.imaginary(r)) == False assert ask(Q.positive(r)) == True assert ask(Q.negative(r)) == False assert ask(Q.even(r)) == False assert ask(Q.odd(r)) == False assert ask(Q.bounded(r)) == True assert ask(Q.infinitesimal(r)) == False assert ask(Q.prime(r)) == False assert ask(Q.composite(r)) == False r = Rational(1, 4) assert ask(Q.positive(r)) == True assert ask(Q.negative(r)) == False r = Rational(5, 4) assert ask(Q.negative(r)) == False assert ask(Q.positive(r)) == True r = Rational(5, 3) assert ask(Q.positive(r)) == True assert ask(Q.negative(r)) == False r = Rational(-3, 4) assert ask(Q.positive(r)) == False assert ask(Q.negative(r)) == True r = Rational(-1, 4) assert ask(Q.positive(r)) == False assert ask(Q.negative(r)) == True r = Rational(-5, 4) assert ask(Q.negative(r)) == True assert ask(Q.positive(r)) == False r = Rational(-5, 3) assert ask(Q.positive(r)) == False assert ask(Q.negative(r)) == True
def test_Rational_number(): r = Rational(3,4) assert ask(Q.commutative(r)) == True assert ask(Q.integer(r)) == False assert ask(Q.rational(r)) == True assert ask(Q.real(r)) == True assert ask(Q.complex(r)) == True assert ask(Q.irrational(r)) == False assert ask(Q.imaginary(r)) == False assert ask(Q.positive(r)) == True assert ask(Q.negative(r)) == False assert ask(Q.even(r)) == False assert ask(Q.odd(r)) == False assert ask(Q.bounded(r)) == True assert ask(Q.infinitesimal(r)) == False assert ask(Q.prime(r)) == False assert ask(Q.composite(r)) == False r = Rational(1,4) assert ask(Q.positive(r)) == True assert ask(Q.negative(r)) == False r = Rational(5,4) assert ask(Q.negative(r)) == False assert ask(Q.positive(r)) == True r = Rational(5,3) assert ask(Q.positive(r)) == True assert ask(Q.negative(r)) == False r = Rational(-3,4) assert ask(Q.positive(r)) == False assert ask(Q.negative(r)) == True r = Rational(-1,4) assert ask(Q.positive(r)) == False assert ask(Q.negative(r)) == True r = Rational(-5,4) assert ask(Q.negative(r)) == True assert ask(Q.positive(r)) == False r = Rational(-5,3) assert ask(Q.positive(r)) == False assert ask(Q.negative(r)) == True
def Add(expr, assumptions): """ Return True if expr is bounded, False if not and None if unknown. TRUTH TABLE B U ? + - x + - x +---+-----+-----+ B | B | U |? ? ?| legend: +---+-----+-----+ B = Bounded + |U ? ?|U ? ?| U = Unbounded U - |? U ?|? U ?| ? = unknown boundedness x |? ? ?|? ? ?| + = positive sign +-----+--+--+ - = negative sign ? |? ? ?| x = sign unknown +--+--+ All Bounded -> True Any Unbounded and all same sign -> False Any Unknown and unknown sign -> None Else -> None When the signs are not the same you can have an undefined (hence bounded undefined) result as in oo - oo """ result = True sign = -1 # not assigned yet for arg in expr.args: _bounded = ask(Q.bounded(arg), assumptions) if _bounded: continue if result is None and _bounded is None and sign is None: return None if result is not False: result = _bounded pos = ask(Q.positive(arg), assumptions) if sign == -1: sign = pos continue if sign != pos: return None if sign is None and pos is None: return None return result
def Pow(expr, assumptions): """ Imaginary**integer/odd -> Imaginary Imaginary**integer/even -> Real if integer % 2 == 0 b**Imaginary -> !Imaginary if exponent is an integer multiple of I*pi/log(b) Imaginary**Real -> ? Negative**even root -> Imaginary Negative**odd root -> Real Negative**Real -> Imaginary Real**Integer -> Real Real**Positive -> Real """ if expr.is_number: return AskImaginaryHandler._number(expr, assumptions) if expr.base.func == C.exp: if ask(Q.imaginary(expr.base.args[0]), assumptions): if ask(Q.imaginary(expr.exp), assumptions): return False i = expr.base.args[0] / I / pi if ask(Q.integer(2 * i), assumptions): return ask(Q.imaginary(((-1)**i)**expr.exp), assumptions) if ask(Q.imaginary(expr.base), assumptions): if ask(Q.integer(expr.exp), assumptions): odd = ask(Q.odd(expr.exp), assumptions) if odd is not None: return odd return if ask(Q.imaginary(expr.exp), assumptions): imlog = ask(Q.imaginary(C.log(expr.base)), assumptions) if imlog is not None: return False # I**i -> real; (2*I)**i -> complex ==> not imaginary if ask(Q.real(expr.base), assumptions): if ask(Q.real(expr.exp), assumptions): if ask( Q.rational(expr.exp) & Q.even(denom(expr.exp)), assumptions): return ask(Q.negative(expr.base), assumptions) elif ask(Q.integer(expr.exp), assumptions): return False elif ask(Q.positive(expr.base), assumptions): return False elif ask(Q.negative(expr.base), assumptions): return True
def test_odd(): x, y, z, t = symbols('x,y,z,t') assert ask(Q.odd(x)) == None assert ask(Q.odd(x), Q.odd(x)) == True assert ask(Q.odd(x), Q.integer(x)) == None assert ask(Q.odd(x), ~Q.integer(x)) == False assert ask(Q.odd(x), Q.rational(x)) == None assert ask(Q.odd(x), Q.positive(x)) == None assert ask(Q.odd(-x), Q.odd(x)) == True assert ask(Q.odd(2*x)) == None assert ask(Q.odd(2*x), Q.integer(x)) == False assert ask(Q.odd(2*x), Q.odd(x)) == False assert ask(Q.odd(2*x), Q.irrational(x)) == False assert ask(Q.odd(2*x), ~Q.integer(x)) == None assert ask(Q.odd(3*x), Q.integer(x)) == None assert ask(Q.odd(x/3), Q.odd(x)) == None assert ask(Q.odd(x/3), Q.even(x)) == None assert ask(Q.odd(x+1), Q.even(x)) == True assert ask(Q.odd(x+2), Q.even(x)) == False assert ask(Q.odd(x+2), Q.odd(x)) == True assert ask(Q.odd(3-x), Q.odd(x)) == False assert ask(Q.odd(3-x), Q.even(x)) == True assert ask(Q.odd(3+x), Q.odd(x)) == False assert ask(Q.odd(3+x), Q.even(x)) == True assert ask(Q.odd(x+y), Q.odd(x) & Q.odd(y)) == False assert ask(Q.odd(x+y), Q.odd(x) & Q.even(y)) == True assert ask(Q.odd(x-y), Q.even(x) & Q.odd(y)) == True assert ask(Q.odd(x-y), Q.odd(x) & Q.odd(y)) == False assert ask(Q.odd(x+y+z), Q.odd(x) & Q.odd(y) & Q.even(z)) == False assert ask(Q.odd(x+y+z+t), Q.odd(x) & Q.odd(y) & Q.even(z) & Q.integer(t)) == None assert ask(Q.odd(2*x + 1), Q.integer(x)) == True assert ask(Q.odd(2*x + y), Q.integer(x) & Q.odd(y)) == True assert ask(Q.odd(2*x + y), Q.integer(x) & Q.even(y)) == False assert ask(Q.odd(2*x + y), Q.integer(x) & Q.integer(y)) == None assert ask(Q.odd(x*y), Q.odd(x) & Q.even(y)) == False assert ask(Q.odd(x*y), Q.odd(x) & Q.odd(y)) == True assert ask(Q.odd(2*x*y), Q.rational(x) & Q.rational(x)) == None assert ask(Q.odd(2*x*y), Q.irrational(x) & Q.irrational(x)) == None assert ask(Q.odd(Abs(x)), Q.odd(x)) == True
def test_odd(): x, y, z, t = symbols('x,y,z,t') assert ask(Q.odd(x)) == None assert ask(Q.odd(x), Q.odd(x)) == True assert ask(Q.odd(x), Q.integer(x)) == None assert ask(Q.odd(x), ~Q.integer(x)) == False assert ask(Q.odd(x), Q.rational(x)) == None assert ask(Q.odd(x), Q.positive(x)) == None assert ask(Q.odd(-x), Q.odd(x)) == True assert ask(Q.odd(2 * x)) == None assert ask(Q.odd(2 * x), Q.integer(x)) == False assert ask(Q.odd(2 * x), Q.odd(x)) == False assert ask(Q.odd(2 * x), Q.irrational(x)) == False assert ask(Q.odd(2 * x), ~Q.integer(x)) == None assert ask(Q.odd(3 * x), Q.integer(x)) == None assert ask(Q.odd(x / 3), Q.odd(x)) == None assert ask(Q.odd(x / 3), Q.even(x)) == None assert ask(Q.odd(x + 1), Q.even(x)) == True assert ask(Q.odd(x + 2), Q.even(x)) == False assert ask(Q.odd(x + 2), Q.odd(x)) == True assert ask(Q.odd(3 - x), Q.odd(x)) == False assert ask(Q.odd(3 - x), Q.even(x)) == True assert ask(Q.odd(3 + x), Q.odd(x)) == False assert ask(Q.odd(3 + x), Q.even(x)) == True assert ask(Q.odd(x + y), Q.odd(x) & Q.odd(y)) == False assert ask(Q.odd(x + y), Q.odd(x) & Q.even(y)) == True assert ask(Q.odd(x - y), Q.even(x) & Q.odd(y)) == True assert ask(Q.odd(x - y), Q.odd(x) & Q.odd(y)) == False assert ask(Q.odd(x + y + z), Q.odd(x) & Q.odd(y) & Q.even(z)) == False assert ask(Q.odd(x + y + z + t), Q.odd(x) & Q.odd(y) & Q.even(z) & Q.integer(t)) == None assert ask(Q.odd(2 * x + 1), Q.integer(x)) == True assert ask(Q.odd(2 * x + y), Q.integer(x) & Q.odd(y)) == True assert ask(Q.odd(2 * x + y), Q.integer(x) & Q.even(y)) == False assert ask(Q.odd(2 * x + y), Q.integer(x) & Q.integer(y)) == None assert ask(Q.odd(x * y), Q.odd(x) & Q.even(y)) == False assert ask(Q.odd(x * y), Q.odd(x) & Q.odd(y)) == True assert ask(Q.odd(2 * x * y), Q.rational(x) & Q.rational(x)) == None assert ask(Q.odd(2 * x * y), Q.irrational(x) & Q.irrational(x)) == None assert ask(Q.odd(Abs(x)), Q.odd(x)) == True
def Add(expr, assumptions): """ Positive + Positive -> Positive, Negative + Negative -> Negative """ if expr.is_number: return AskNegativeHandler._number(expr, assumptions) nonpos = 0 for arg in expr.args: if ask(Q.negative(arg), assumptions) is not True: if ask(Q.positive(arg), assumptions) is False: nonpos += 1 else: break else: if nonpos < len(expr.args): return True
def test_zero_0(): z = Integer(0) assert ask(Q.nonzero(z)) == False assert ask(Q.commutative(z)) == True assert ask(Q.integer(z)) == True assert ask(Q.rational(z)) == True assert ask(Q.real(z)) == True assert ask(Q.complex(z)) == True assert ask(Q.imaginary(z)) == False assert ask(Q.positive(z)) == False assert ask(Q.negative(z)) == False assert ask(Q.even(z)) == True assert ask(Q.odd(z)) == False assert ask(Q.bounded(z)) == True assert ask(Q.infinitesimal(z)) == True assert ask(Q.prime(z)) == False assert ask(Q.composite(z)) == False
def test_E(): z = S.Exp1 assert ask(Q.commutative(z)) == True assert ask(Q.integer(z)) == False assert ask(Q.rational(z)) == False assert ask(Q.real(z)) == True assert ask(Q.complex(z)) == True assert ask(Q.irrational(z)) == True assert ask(Q.imaginary(z)) == False assert ask(Q.positive(z)) == True assert ask(Q.negative(z)) == False assert ask(Q.even(z)) == False assert ask(Q.odd(z)) == False assert ask(Q.bounded(z)) == True assert ask(Q.infinitesimal(z)) == False assert ask(Q.prime(z)) == False assert ask(Q.composite(z)) == False
def _(expr, assumptions): _positive = ask(Q.positive(expr), assumptions) if _positive: _integer = ask(Q.integer(expr), assumptions) if _integer: _prime = ask(Q.prime(expr), assumptions) if _prime is None: return # Positive integer which is not prime is not # necessarily composite if expr.equals(1): return False return not _prime else: return _integer else: return _positive
def Basic(expr, assumptions): _positive = ask(Q.positive(expr), assumptions) if _positive: _integer = ask(Q.integer(expr), assumptions) if _integer: _prime = ask(Q.prime(expr), assumptions) if _prime is None: return # Positive integer which is not prime is not # necessarily composite if expr.equals(1): return False return not _prime else: return _integer else: return _positive