# Source code for sympy.assumptions.refine

from sympy.core import S, Add, Expr
from sympy.core.logic import fuzzy_not

[docs]def refine(expr, assumptions=True):
"""
Simplify an expression using assumptions.

Gives the form of expr that would be obtained if symbols
in it were replaced by explicit numerical expressions satisfying
the assumptions.

Examples
========

>>> from sympy import refine, sqrt, Q
>>> from sympy.abc import x
>>> refine(sqrt(x**2), Q.real(x))
Abs(x)
>>> refine(sqrt(x**2), Q.positive(x))
x

"""
if not expr.is_Atom:
args = [refine(arg, assumptions) for arg in expr.args]
# TODO: this will probably not work with Integral or Polynomial
expr = expr.func(*args)
name = expr.__class__.__name__
handler = handlers_dict.get(name, None)
if handler is None:
return expr
new_expr = handler(expr, assumptions)
if (new_expr is None) or (expr == new_expr):
return expr
if not isinstance(new_expr, Expr):
return new_expr
return refine(new_expr, assumptions)

[docs]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 it's nonnegative
return arg
return -arg

[docs]def refine_Pow(expr, assumptions):
"""
Handler for instances of Pow.

>>> from sympy import Symbol, Q
>>> from sympy.assumptions.refine import refine_Pow
>>> from sympy.abc import x,y,z
>>> refine_Pow((-1)**x, Q.real(x))
>>> refine_Pow((-1)**x, Q.even(x))
1
>>> refine_Pow((-1)**x, Q.odd(x))
-1

For powers of -1, even parts of the exponent can be simplified:

>>> refine_Pow((-1)**(x+y), Q.even(x))
(-1)**y
>>> refine_Pow((-1)**(x+y+z), Q.odd(x) & Q.odd(z))
(-1)**y
>>> refine_Pow((-1)**(x+y+2), Q.odd(x))
(-1)**(y + 1)
>>> refine_Pow((-1)**(x+3), True)
(-1)**(x + 1)

"""
from sympy.core import Pow, Rational
from sympy.functions.elementary.complexes import Abs
from sympy.functions import sign
if isinstance(expr.base, Abs):
return expr.base.args[0] ** expr.exp
if expr.base.is_number:
return abs(expr.base) ** expr.exp
return sign(expr.base) * abs(expr.base) ** expr.exp
if isinstance(expr.exp, Rational):
if type(expr.base) is Pow:
return abs(expr.base.base) ** (expr.base.exp * expr.exp)

if expr.base is S.NegativeOne:

# For powers of (-1) we can remove
#  - even terms
#  - pairs of odd terms
#  - a single odd term + 1
#  - A numerical constant N can be replaced with mod(N,2)

terms = set(terms)
even_terms = set([])
odd_terms = set([])
initial_number_of_terms = len(terms)

for t in terms:

terms -= even_terms
if len(odd_terms) % 2:
terms -= odd_terms
new_coeff = (coeff + S.One) % 2
else:
terms -= odd_terms
new_coeff = coeff % 2

if new_coeff != coeff or len(terms) < initial_number_of_terms:

[docs]def refine_exp(expr, assumptions):
"""
Handler for exponential function.

>>> from sympy import Symbol, Q, exp, I, pi
>>> from sympy.assumptions.refine import refine_exp
>>> from sympy.abc import x
>>> refine_exp(exp(pi*I*2*x), Q.real(x))
>>> refine_exp(exp(pi*I*2*x), Q.integer(x))
1

"""
arg = expr.args[0]
if arg.is_Mul:
coeff = arg.as_coefficient(S.Pi*S.ImaginaryUnit)
if coeff:
return S.One
return S.NegativeOne
return -S.ImaginaryUnit
return S.ImaginaryUnit

[docs]def refine_Relational(expr, assumptions):
"""
Handler for Relational

>>> from sympy.assumptions.refine import refine_Relational
>>> from sympy.abc import x
>>> refine_Relational(x<0, ~Q.is_true(x<0))
False
"""

handlers_dict = {
'Abs': refine_abs,
'Pow': refine_Pow,
'exp': refine_exp,
'Equality' : refine_Relational,
'Unequality' : refine_Relational,
'GreaterThan' : refine_Relational,
'LessThan' : refine_Relational,
'StrictGreaterThan' : refine_Relational,
'StrictLessThan' : refine_Relational
}