details:   http://hg.sympy.org/sympy/rev/0a5e8fa81903
changeset: 1780:0a5e8fa81903
user:      Robert <[EMAIL PROTECTED]>
date:      Sat Feb 02 10:45:40 2008 +0100
description:
Implements Piecewise function (#353).

Ondrej: The patch didn't pass review, but Andy fixed that in the following
patch, so it goes in.

Signed-off-by: Robert <[EMAIL PROTECTED]>
Signed-off-by: Ondrej Certik <[EMAIL PROTECTED]>

diffs (85 lines):

diff -r a4a3dd165e1b -r 0a5e8fa81903 sympy/functions/__init__.py
--- a/sympy/functions/__init__.py       Wed Oct 08 20:55:04 2008 +0200
+++ b/sympy/functions/__init__.py       Sat Feb 02 10:45:40 2008 +0100
@@ -24,6 +24,7 @@
 from elementary.exponential import exp, log, LambertW
 from elementary.hyperbolic import sinh, cosh, tanh, coth, asinh, acosh, atanh, 
acoth
 from elementary.integers import floor, ceiling
+from elementary.piecewise import Piecewise
 
 from special.error_functions import erf
 from special.gamma_functions import gamma, lowergamma, uppergamma, polygamma, 
loggamma
diff -r a4a3dd165e1b -r 0a5e8fa81903 sympy/functions/elementary/piecewise.py
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sympy/functions/elementary/piecewise.py   Sat Feb 02 10:45:40 2008 +0100
@@ -0,0 +1,52 @@
+from sympy import *
+
+
+class Piecewise(Function):
+    """
+    > Container for a Piecewise Function
+    -- Piecewise represents a piecewise function within the SymPy context.
+
+    Example Usage:
+
+    from sympy import Piecewise, oo, log, symbols
+    x = symbols('x')
+    p = Piecewise(x, [[(-oo, -1), 1], [(1, 2), x*x], [(3, oo), log(x)]])
+
+    Explanation:
+    - The first argument is the variable of the piecewise function.
+    - The second argument is a Python list of sublists.
+    - The first entry in the sublist is a tuple with the range of a function.
+    - The second entry is the actual function over the specified range.
+    """
+    nargs=1
+
+    def __new__(cls, *args, **options):
+        if cls is Function:
+            if len(args) == 1 and isinstance(args[0], str):
+                return FunctionClass(Function, *args)
+            else:
+                print args
+                print type(args[0])
+                raise Exception("You need to specify exactly one string")
+                args = map(Basic.sympify, args)
+        return Basic.__new__(cls, *args, **options)
+
+    def fdiff(self, arg=1):
+        """ Returns the differentiated piecewise function """
+        e = self.args[1]
+
+        for i in range(0, len(self.args[1])):
+            e[i][1] = diff(e[i][1], self.args[0])
+
+    def canonize(self, arg, elems):
+        arg = Basic.sympify(arg)
+        for x in self.elems:
+            cond = x[0]
+            if isinstance(cond, (Basic.Number, Basic.Number)):
+                if cond[0] <= arg and arg <= cond[1]:
+                    return x[1]
+            else:
+                cond = Basic.sympify(cond)
+                if isinstance(cond, Bool):
+                    if cond:
+                        return x[1]
diff -r a4a3dd165e1b -r 0a5e8fa81903 
sympy/functions/elementary/tests/test_piecewise.py
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sympy/functions/elementary/tests/test_piecewise.py        Sat Feb 02 
10:45:40 2008 +0100
@@ -0,0 +1,14 @@
+from sympy import Piecewise, oo, log, symbols
+
+x = symbols('x')
+
+
+def test_piecewise():
+    p = Piecewise(x, [[(-oo, -1), 1], [(1, 2), x*x], [(3, oo), log(x)]])
+
+
+    p.fdiff()
+
+    assert p.args[1][0] == [(-oo, -1), 0]
+    assert p.args[1][1] == [(1, 2), 2*x]
+    assert p.args[1][2] == [(3, oo), 1/x]

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

Reply via email to