def refine_abs(expr, assumptions): """ Handler for the absolute value. Examples ======== >>> from sympy import Symbol, Q, refine, Abs >>> from sympy.assumptions.refine import refine_abs >>> from sympy.abc import x >>> refine_abs(Abs(x), Q.real(x)) >>> refine_abs(Abs(x), Q.positive(x)) x >>> refine_abs(Abs(x), Q.negative(x)) -x """ from sympy.core.logic import fuzzy_not arg = expr.args[0] if ask(Q.real(arg), assumptions) and fuzzy_not(ask(Q.negative(arg), assumptions)): # if it's nonnegative return arg if ask(Q.negative(arg), assumptions): return -arg
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_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 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 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 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_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 Pow(expr, assumptions): """ Unbounded ** NonZero -> Unbounded Bounded ** Bounded -> Bounded Abs()<=1 ** Positive -> Bounded Abs()>=1 ** Negative -> Bounded Otherwise unknown """ base_bounded = ask(Q.bounded(expr.base), assumptions) exp_bounded = ask(Q.bounded(expr.exp), assumptions) if base_bounded is None and exp_bounded is None: # Common Case return None if base_bounded is False and ask(Q.nonzero(expr.exp), assumptions): return False if base_bounded and exp_bounded: return True if (abs(expr.base) <= 1) is True and ask(Q.positive(expr.exp), assumptions): return True if (abs(expr.base) >= 1) is True and ask(Q.negative(expr.exp), assumptions): return True if (abs(expr.base) >= 1) is True and exp_bounded is False: return False return None
def test_composite_proposition(): from sympy.logic.boolalg import Equivalent, Implies x = symbols('x') assert ask(True) is True assert ask(~Q.negative(x), Q.positive(x)) is True assert ask(~Q.real(x), Q.commutative(x)) is None assert ask(Q.negative(x) & Q.integer(x), Q.positive(x)) is False assert ask(Q.negative(x) & Q.integer(x)) is None assert ask(Q.real(x) | Q.integer(x), Q.positive(x)) is True assert ask(Q.real(x) | Q.integer(x)) is None assert ask(Q.real(x) >> Q.positive(x), Q.negative(x)) is False assert ask(Implies(Q.real(x), Q.positive(x), evaluate=False), Q.negative(x)) is False assert ask(Implies(Q.real(x), Q.positive(x), evaluate=False)) is None assert ask(Equivalent(Q.integer(x), Q.even(x)), Q.even(x)) is True assert ask(Equivalent(Q.integer(x), Q.even(x))) is None assert ask(Equivalent(Q.positive(x), Q.integer(x)), Q.integer(x)) is None
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 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 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 _(expr, assumptions): """ * Unbounded ** NonZero -> Unbounded * Bounded ** Bounded -> Bounded * Abs()<=1 ** Positive -> Bounded * Abs()>=1 ** Negative -> Bounded * Otherwise unknown """ if expr.base == E: return ask(Q.finite(expr.exp), assumptions) base_bounded = ask(Q.finite(expr.base), assumptions) exp_bounded = ask(Q.finite(expr.exp), assumptions) if base_bounded is None and exp_bounded is None: # Common Case return None if base_bounded is False and ask(Q.nonzero(expr.exp), assumptions): return False if base_bounded and exp_bounded: return True if (abs(expr.base) <= 1) == True and ask(Q.positive(expr.exp), assumptions): return True if (abs(expr.base) >= 1) == True and ask(Q.negative(expr.exp), assumptions): return True if (abs(expr.base) >= 1) == True and exp_bounded is False: return False return None
def _(expr, assumptions): return ask(Q.negative_infinite(expr) | Q.negative(expr) | Q.zero(expr) | Q.positive(expr) | Q.positive_infinite(expr), assumptions)
def MatPow(expr, assumptions): # only for integer powers base, exp = expr.args int_exp = ask(Q.integer(exp), assumptions) if int_exp and ask(~Q.negative(exp), assumptions): return ask(Q.fullrank(base), assumptions) return None
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 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 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 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 _(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 _RealPredicate_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_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 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 MatPow(expr, assumptions): # only for integer powers base, exp = expr.args int_exp = ask(Q.integer(exp), assumptions) if not int_exp: return None non_negative = ask(~Q.negative(exp), assumptions) if (non_negative or non_negative == False and ask(Q.invertible(base), assumptions)): return ask(Q.complex_elements(base), assumptions) return None
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) 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): # only for integer powers base, exp = expr.args int_exp = ask(Q.integer(exp), assumptions) if not int_exp: return None non_negative = ask(~Q.negative(exp), assumptions) if (non_negative or non_negative == False and ask(Q.invertible(base), assumptions)): return ask(Q.diagonal(base), assumptions) return None
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 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 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 test_positive(): x, y, z, w = symbols('x,y,z,w') assert ask(Q.positive(x), Q.positive(x)) == True assert ask(Q.positive(x), Q.negative(x)) == False assert ask(Q.positive(x), Q.nonzero(x)) == None assert ask(Q.positive(-x), Q.positive(x)) == False assert ask(Q.positive(-x), Q.negative(x)) == True assert ask(Q.positive(x + y), Q.positive(x) & Q.positive(y)) == True assert ask(Q.positive(x + y), Q.positive(x) & Q.negative(y)) == None assert ask(Q.positive(2 * x), Q.positive(x)) == True assumptions = Q.positive(x) & Q.negative(y) & Q.negative(z) & Q.positive(w) assert ask(Q.positive(x * y * z)) == None assert ask(Q.positive(x * y * z), assumptions) == True assert ask(Q.positive(-x * y * z), assumptions) == False assert ask(Q.positive(x**2), Q.positive(x)) == True assert ask(Q.positive(x**2), Q.negative(x)) == True #exponential assert ask(Q.positive(exp(x)), Q.real(x)) == True assert ask(Q.positive(x + exp(x)), Q.real(x)) == None #absolute value assert ask(Q.positive(Abs(x))) == None # Abs(0) = 0 assert ask(Q.positive(Abs(x)), Q.positive(x)) == True
def test_positive(): x, y, z, w = symbols('x,y,z,w') assert ask(Q.positive(x), Q.positive(x)) == True assert ask(Q.positive(x), Q.negative(x)) == False assert ask(Q.positive(x), Q.nonzero(x)) == None assert ask(Q.positive(-x), Q.positive(x)) == False assert ask(Q.positive(-x), Q.negative(x)) == True assert ask(Q.positive(x+y), Q.positive(x) & Q.positive(y)) == True assert ask(Q.positive(x+y), Q.positive(x) & Q.negative(y)) == None assert ask(Q.positive(2*x), Q.positive(x)) == True assumptions = Q.positive(x) & Q.negative(y) & Q.negative(z) & Q.positive(w) assert ask(Q.positive(x*y*z)) == None assert ask(Q.positive(x*y*z), assumptions) == True assert ask(Q.positive(-x*y*z), assumptions) == False assert ask(Q.positive(x**2), Q.positive(x)) == True assert ask(Q.positive(x**2), Q.negative(x)) == True #exponential assert ask(Q.positive(exp(x)), Q.real(x)) == True assert ask(Q.positive(x + exp(x)), Q.real(x)) == None #absolute value assert ask(Q.positive(Abs(x))) == None # Abs(0) = 0 assert ask(Q.positive(Abs(x)), Q.positive(x)) == True
def Add(expr, assumptions): """ Positive + Positive -> Positive, Negative + Negative -> Negative """ if expr.is_number: return AskNegativeHandler._number(expr, assumptions) for arg in expr.args: if not ask(Q.negative(arg), assumptions): break else: # if all argument's are negative return True
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 refine_abs(expr, assumptions): """ Handler for the absolute value. Examples:: >>> from sympy import Symbol, Q, refine, Abs >>> from sympy.assumptions.refine import refine_abs >>> from sympy.abc import x >>> refine_abs(Abs(x), Q.real(x)) >>> refine_abs(Abs(x), Q.positive(x)) x >>> refine_abs(Abs(x), Q.negative(x)) -x """ arg = expr.args[0] if ask(Q.real(arg), assumptions) and \ fuzzy_not(ask(Q.negative(arg), assumptions)): # if it's nonnegative return arg if ask(Q.negative(arg), assumptions): return -arg
def refine_abs(expr, assumptions): """ Handler for the absolute value. Examples ======== >>> from sympy import Symbol, Q, refine, Abs >>> from sympy.assumptions.refine import refine_abs >>> from sympy.abc import x >>> refine_abs(Abs(x), Q.real(x)) >>> refine_abs(Abs(x), Q.positive(x)) x >>> refine_abs(Abs(x), Q.negative(x)) -x """ from sympy.core.logic import fuzzy_not from sympy import Abs arg = expr.args[0] if ask(Q.real(arg), assumptions) and \ fuzzy_not(ask(Q.negative(arg), assumptions)): # if it's nonnegative return arg if ask(Q.negative(arg), assumptions): return -arg # arg is Mul if isinstance(arg, Mul): r = [refine(abs(a), assumptions) for a in arg.args] non_abs = [] in_abs = [] for i in r: if isinstance(i, Abs): in_abs.append(i.args[0]) else: non_abs.append(i) return Mul(*non_abs) * Abs(Mul(*in_abs))
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 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_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): """ Unbounded ** Whatever -> Unbounded Bounded ** Unbounded -> Unbounded if base > 1 Bounded ** Unbounded -> Unbounded if base < 1 """ base_bounded = ask(Q.bounded(expr.base), assumptions) if not base_bounded: return False if ask(Q.bounded(expr.exp), assumptions):# and base_bounded: return True if expr.base.is_number:# and base_bounded and not exp_bounded: # We need to implement relations for this if abs(expr.base) > 1: return False return ask(~Q.negative(expr.exp), assumptions)
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