# Source code for sympy.assumptions.handlers.order

"""
AskHandlers related to order relations: positive, negative, etc.
"""
from __future__ import print_function, division

from sympy.assumptions import Q, ask
from sympy.assumptions.handlers import CommonHandler
from sympy.core.logic import fuzzy_not, fuzzy_and, fuzzy_or

"""
This is called by ask() when key='negative'

Test that an expression is less (strict) than zero.

Examples
========

>>> from sympy import ask, Q, pi
False
False

"""

@staticmethod
def Expr(expr, assumptions):
return expr.is_negative

@staticmethod
def _number(expr, assumptions):
r, i = expr.as_real_imag()
# If the imaginary part can symbolically be shown to be zero then
# we just evaluate the real part; otherwise we evaluate the imaginary
# part to see if it actually evaluates to zero and if it does then
# we make the comparison between the real part and zero.
if not i:
r = r.evalf(2)
if r._prec != 1:
return r < 0
else:
i = i.evalf(2)
if i._prec != 1:
if i != 0:
return False
r = r.evalf(2)
if r._prec != 1:
return r < 0

@staticmethod
def Basic(expr, assumptions):
if expr.is_number:

[docs]    @staticmethod
"""
Positive + Positive -> Positive,
Negative + Negative -> Negative
"""
if expr.is_number:

r = ask(Q.real(expr), assumptions)
if r is not True:
return r

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

@staticmethod
def Mul(expr, assumptions):
if expr.is_number:
result = None
for arg in expr.args:
if result is None:
result = False
result = not result
pass
else:
return
return result

[docs]    @staticmethod
def Pow(expr, assumptions):
"""
Real ** Even -> NonNegative
Real ** Odd  -> same_as_base
NonNegative ** Positive -> NonNegative
"""
if expr.is_number:
return False
return False

ImaginaryUnit, Abs = [staticmethod(CommonHandler.AlwaysFalse)]*2

@staticmethod
def exp(expr, assumptions):
return False

@staticmethod
def Expr(expr, assumptions):
return expr.is_nonnegative

@staticmethod
def Basic(expr, assumptions):
if expr.is_number:
notnegative = fuzzy_not(AskNegativeHandler._number(expr, assumptions))
if notnegative:
else:
return notnegative

"""
Handler for key 'zero'
Test that an expression is not identically zero
"""

@staticmethod
def Expr(expr, assumptions):
return expr.is_nonzero

@staticmethod
def Basic(expr, assumptions):
if ask(Q.real(expr)) is False:
return False
if expr.is_number:
# if there are no symbols just evalf
i = expr.evalf(2)
def nonz(i):
if i._prec != 1:
return i != 0
return fuzzy_or(nonz(i) for i in i.as_real_imag())

@staticmethod
if all(ask(Q.positive(x), assumptions) for x in expr.args) \
or all(ask(Q.negative(x), assumptions) for x in expr.args):
return True

@staticmethod
def Mul(expr, assumptions):
for arg in expr.args:
result = ask(Q.nonzero(arg), assumptions)
if result:
continue
return result
return True

@staticmethod
def Pow(expr, assumptions):

NaN = staticmethod(CommonHandler.AlwaysTrue)

@staticmethod
def Abs(expr, assumptions):

@staticmethod
def Expr(expr, assumptions):
return expr.is_zero

@staticmethod
def Basic(expr, assumptions):

@staticmethod
def Mul(expr, assumptions):
# TODO: This should be deducible from the nonzero handler
return fuzzy_or(ask(Q.zero(arg), assumptions) for arg in expr.args)

@staticmethod
def Expr(expr, assumptions):
return expr.is_nonpositive

@staticmethod
def Basic(expr, assumptions):
if expr.is_number:
notpositive = fuzzy_not(AskPositiveHandler._number(expr, assumptions))
if notpositive:
else:
return notpositive

"""
Handler for key 'positive'
Test that an expression is greater (strict) than zero
"""

@staticmethod
def Expr(expr, assumptions):
return expr.is_positive

@staticmethod
def _number(expr, assumptions):
r, i = expr.as_real_imag()
# If the imaginary part can symbolically be shown to be zero then
# we just evaluate the real part; otherwise we evaluate the imaginary
# part to see if it actually evaluates to zero and if it does then
# we make the comparison between the real part and zero.
if not i:
r = r.evalf(2)
if r._prec != 1:
return r > 0
else:
i = i.evalf(2)
if i._prec != 1:
if i != 0:
return False
r = r.evalf(2)
if r._prec != 1:
return r > 0

@staticmethod
def Basic(expr, assumptions):
if expr.is_number:

@staticmethod
def Mul(expr, assumptions):
if expr.is_number:
result = True
for arg in expr.args:
continue
result = result ^ True
else:
return
return result

@staticmethod
if expr.is_number:

r = ask(Q.real(expr), assumptions)
if r is not True:
return r

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

@staticmethod
def Pow(expr, assumptions):
if expr.is_number:
return True
return True
return False

@staticmethod
def exp(expr, assumptions):
return True
from sympy import pi, I

@staticmethod
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

@staticmethod
def factorial(expr, assumptions):
x = expr.args[0]
if ask(Q.integer(x) & Q.positive(x), assumptions):
return True

ImaginaryUnit = staticmethod(CommonHandler.AlwaysFalse)

@staticmethod
def Abs(expr, assumptions):

@staticmethod
def Trace(expr, assumptions):
return True

@staticmethod
def Determinant(expr, assumptions):
return True

@staticmethod
def MatrixElement(expr, assumptions):
if (expr.i == expr.j
return True

@staticmethod
def atan(expr, assumptions):

@staticmethod
def asin(expr, assumptions):
x = expr.args[0]
if ask(Q.positive(x) & Q.nonpositive(x - 1), assumptions):
return True
if ask(Q.negative(x) & Q.nonnegative(x + 1), assumptions):
return False

@staticmethod
def acos(expr, assumptions):
x = expr.args[0]
if ask(Q.nonpositive(x - 1) & Q.nonnegative(x + 1), assumptions):
return True

@staticmethod
def acot(expr, assumptions):