From: Fabian Seoane <fab...@fseoane.net> Added test and docs --- sympy/functions/elementary/complexes.py | 19 ++++++++++++++++--- sympy/functions/elementary/tests/test_complexes.py | 6 +++++- 2 files changed, 21 insertions(+), 4 deletions(-)
diff --git a/sympy/functions/elementary/complexes.py b/sympy/functions/elementary/complexes.py index 2e94716..250d808 100644 --- a/sympy/functions/elementary/complexes.py +++ b/sympy/functions/elementary/complexes.py @@ -193,6 +193,20 @@ def _eval_is_zero(self): return (self[0] is S.Zero) class abs(Function): + """Return the absolute value of the argument. This is an extension of the built-in + function abs to accept symbolic values + + Examples + + >>> from sympy import abs, Symbol + >>> abs(-1) + 1 + >>> x = Symbol('x', real=True) + >>> abs(-x) + abs(x) + >>> abs(x**2) + x**2 + """ nargs = 1 @@ -228,9 +242,8 @@ def eval(cls, arg): return sqrt( (arg * arg.conjugate()).expand() ) if arg.is_Pow: base, exponent = arg.as_base_exp() - if exponent.is_Number: - if exponent.is_even: - return arg + if exponent.is_even and base.is_real: + return arg return @classmethod diff --git a/sympy/functions/elementary/tests/test_complexes.py b/sympy/functions/elementary/tests/test_complexes.py index 8227cc4..28d97a9 100644 --- a/sympy/functions/elementary/tests/test_complexes.py +++ b/sympy/functions/elementary/tests/test_complexes.py @@ -98,14 +98,18 @@ def test_abs(): assert x**(2*n) == abs(x)**(2*n) def test_abs_real(): + # test some properties of abs that only apply + # to real numbers x = Symbol('x', complex=True) assert sqrt(x**2) != abs(x) + assert abs(x**2) != x**2 + x = Symbol('x', real=True) assert sqrt(x**2) == abs(x) + assert abs(x**2) == x**2 def test_abs_properties(): x = Symbol('x') - assert abs(x).is_real == True assert abs(x).is_positive == None assert abs(x).is_nonnegative == True -- 1.6.1.2 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sympy-patches" group. To post to this group, send email to sympy-patches@googlegroups.com To unsubscribe from this group, send email to sympy-patches+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sympy-patches?hl=en -~----------~----~----~----~------~----~------~--~---