Skip to content

Commit

Permalink
Trac #14801: Piecewise functions done right
Browse files Browse the repository at this point in the history
Rewrite piecewise functions as symbolic functions.

For a (late) discussion about interface issues see
https://groups.google.com/forum/?hl=en#!topic/sage-devel/dgwUMsdiHfM

URL: http://trac.sagemath.org/14801
Reported by: vbraun
Ticket author(s): Volker Braun, Ralf Stephan
Reviewer(s): Volker Braun, Ralf Stephan
  • Loading branch information
Release Manager authored and vbraun committed Apr 22, 2016
2 parents bdccb9d + 966859c commit 4996796
Show file tree
Hide file tree
Showing 12 changed files with 2,992 additions and 1,667 deletions.
2 changes: 1 addition & 1 deletion src/doc/de/thematische_anleitungen/sage_gymnasium.rst
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ Tupel besteht, welches das Interval des Definitionsbereichs angibt, also z.B. ``
`[-\infty, 0]` und einer für das Interval geltende Funktionsgleichung. Als letztes Argument muss angegeben werden,
welche Variable durch die Funktion gebunden werden soll::

sage: f = Piecewise([[(-oo,0), -x^2],[(0,oo), x^2]], x)
sage: f = piecewise([[(-oo,0), -x^2],[(0,oo), x^2]], var=x)
sage: f(3)
9
sage: f(-3)
Expand Down
28 changes: 12 additions & 16 deletions src/doc/en/constructions/calculus.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ You can find critical points of a piecewise defined function:
sage: f2 = 1-x
sage: f3 = 2*x
sage: f4 = 10*x-x^2
sage: f = Piecewise([[(0,1),f1],[(1,2),f2],[(2,3),f3],[(3,10),f4]])
sage: f = piecewise([((0,1),f1), ((1,2),f2), ((2,3),f3), ((3,10),f4)])
sage: f.critical_points()
[5.0]

Expand Down Expand Up @@ -177,9 +177,10 @@ where :math:`f(x)=1`, :math:`0<x<1`:
::

sage: x = PolynomialRing(QQ, 'x').gen()
sage: f = Piecewise([[(0,1),1*x^0]])
sage: f = piecewise([((0,1),1*x^0)])
sage: g = f.convolution(f)
sage: h = f.convolution(g)
sage: set_verbose(-1)
sage: P = f.plot(); Q = g.plot(rgbcolor=(1,1,0)); R = h.plot(rgbcolor=(0,1,1))

To view this, type ``show(P+Q+R)``.
Expand Down Expand Up @@ -212,18 +213,13 @@ where :math:`f` is a piecewise defined function, can

sage: f1(x) = x^2
sage: f2(x) = 5-x^2
sage: f = Piecewise([[(0,1),f1],[(1,2),f2]])
sage: f.trapezoid(4)
Piecewise defined function with 4 parts, [[(0, 1/2), 1/2*x],
[(1/2, 1), 9/2*x - 2], [(1, 3/2), 1/2*x + 2],
[(3/2, 2), -7/2*x + 8]]
sage: f.riemann_sum_integral_approximation(6,mode="right")
19/6
sage: f.integral()
Piecewise defined function with 2 parts,
[[(0, 1), x |--> 1/3*x^3], [(1, 2), x |--> -1/3*x^3 + 5*x - 13/3]]
sage: f.integral(definite=True)
3
sage: f = piecewise([[[0,1], f1], [RealSet.open_closed(1,2), f2]])
sage: t = f.trapezoid(2); t
piecewise(x|-->1/2*x on (0, 1/2), x|-->3/2*x - 1/2 on (1/2, 1), x|-->7/2*x - 5/2 on (1, 3/2), x|-->-7/2*x + 8 on (3/2, 2); x)
sage: t.integral()
piecewise(x|-->1/4*x^2 on (0, 1/2), x|-->3/4*x^2 - 1/2*x + 1/8 on (1/2, 1), x|-->7/4*x^2 - 5/2*x + 9/8 on (1, 3/2), x|-->-7/4*x^2 + 8*x - 27/4 on (3/2, 2); x)
sage: t.integral(definite=True)
9/4

.. index: Laplace transform
Expand All @@ -242,7 +238,7 @@ computation.
(x, s)
sage: f1(x) = 1
sage: f2(x) = 1-x
sage: f = Piecewise([[(0,1),f1],[(1,2),f2]])
sage: f = piecewise([((0,1),f1), ((1,2),f2)])
sage: f.laplace(x, s)
-e^(-s)/s + (s + 1)*e^(-2*s)/s^2 + 1/s - e^(-s)/s^2

Expand Down Expand Up @@ -382,7 +378,7 @@ illustrating how the Gibbs phenomenon is mollified).

sage: f1 = lambda x: -1
sage: f2 = lambda x: 2
sage: f = Piecewise([[(0,pi/2),f1],[(pi/2,pi),f2]])
sage: f = piecewise([((0,pi/2),f1), ((pi/2,pi),f2)])
sage: f.fourier_series_cosine_coefficient(5,pi)
-3/5/pi
sage: f.fourier_series_sine_coefficient(2,pi)
Expand Down
14 changes: 7 additions & 7 deletions src/doc/en/constructions/plotting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ You can plot piecewise-defined functions:

::

sage: f1 = lambda x:1
sage: f2 = lambda x:1-x
sage: f3 = lambda x:exp(x)
sage: f4 = lambda x:sin(2*x)
sage: f = Piecewise([[(0,1),f1],[(1,2),f2],[(2,3),f3],[(3,10),f4]])
sage: f.plot()
Graphics object consisting of 4 graphics primitives
sage: f1 = 1
sage: f2 = 1-x
sage: f3 = exp(x)
sage: f4 = sin(2*x)
sage: f = piecewise([((0,1),f1), ((1,2),f2), ((2,3),f3), ((3,10),f4)])
sage: f.plot(x,0,10)
Graphics object consisting of 1 graphics primitive

Other function plots can be produced as well:

Expand Down
2 changes: 1 addition & 1 deletion src/sage/calculus/wester.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@
sage: # Verify(Simplify(Integrate(x)
sage: # if(x<0) (-x) else x),
sage: # Simplify(if(x<0) (-x^2/2) else x^2/2));
sage: f = piecewise([ [[-10,0], -x], [[0,10], x]])
sage: f = piecewise([ ((-10,0), -x), ((0,10), x)])
sage: f.integral(definite=True)
100
Expand Down
5 changes: 4 additions & 1 deletion src/sage/functions/all.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from piecewise import piecewise, Piecewise
from sage.misc.lazy_import import lazy_import

lazy_import('sage.functions.piecewise_old', 'Piecewise') # deprecated
lazy_import('sage.functions.piecewise', 'piecewise')

from trig import ( sin, cos, sec, csc, cot, tan,
asin, acos, atan,
Expand Down
2 changes: 1 addition & 1 deletion src/sage/functions/other.py
Original file line number Diff line number Diff line change
Expand Up @@ -1554,7 +1554,7 @@ def __init__(self):
sage: loads(dumps(binomial(n,k)))
binomial(n, k)
"""
GinacFunction.__init__(self, "binomial", nargs=2,
GinacFunction.__init__(self, "binomial", nargs=2, preserved_arg=1,
conversions=dict(maxima='binomial',
mathematica='Binomial',
sympy='binomial'))
Expand Down
Loading

0 comments on commit 4996796

Please sign in to comment.