Updates:
        Summary: rewrite sign(x) (and possibly abs()) using Piecewise

Comment #6 on issue 1113 by asmeurer: rewrite sign(x) (and possibly abs()) using Piecewise
http://code.google.com/p/sympy/issues/detail?id=1113

Actually, you teach integrate how to integrate new functions by showing it the derivative, not the integral,
ironically enough.

So if you do this:

diff --git a/sympy/functions/elementary/complexes.py b/sympy/functions/elementary/complexes.py
index 6ff99e3..c942749 100644
--- a/sympy/functions/elementary/complexes.py
+++ b/sympy/functions/elementary/complexes.py
@@ -185,6 +185,9 @@ def eval(cls, arg):

     is_bounded = True

+    def _eval_derivative(self, x):
+        return S.Zero
+
     def _eval_conjugate(self):
         return self

You get this:

In [2]: integrate(sign(x), x)
Out[2]: x⋅sign(x)

which is the same as abs(x).

Of course, the real fix here is to replace sign() with a Piecewise, which already handles it:

In [14]: s = Piecewise((1, x>0), (0, Eq(x, 0)), (-1, x<0))

In [15]: s
Out[15]:
⎧1   for 0 < x
⎪
⎨0   for x = 0
⎪
⎩-1  for x < 0

In [16]: t = integrate(s, x)

In [17]: t # Look familiar?  This is the same as abs(x)
Out[17]:
⎧x   for 0 < x
⎪
⎨0   for x = 0
⎪
⎩-x  for x < 0

In [18]: integrate(s, (x, -1, 1)) # OOPS!  This is wrong!
Out[18]: 1

In [1]: integrate(sign(x), (x, -1, 1)) # The fixed sign() gets it right.
Out[1]: 0

In [12]: t.subs(x, 1) # and so does subs, oddly...
Out[12]: 1

In [13]: t.subs(x, -1)
Out[13]: 1

I will create another issue for the Piecewise._eval_interval problem

--
You received this message because you are subscribed to the Google Groups 
"sympy-issues" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sympy-issues?hl=en.

Reply via email to