Skip to content

Commit 4996796

Browse files
Release Managervbraun
Release Manager
authored andcommitted
Trac #14801: Piecewise functions done right
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
2 parents bdccb9d + 966859c commit 4996796

File tree

12 files changed

+2992
-1667
lines changed

12 files changed

+2992
-1667
lines changed

src/doc/de/thematische_anleitungen/sage_gymnasium.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ Tupel besteht, welches das Interval des Definitionsbereichs angibt, also z.B. ``
572572
`[-\infty, 0]` und einer für das Interval geltende Funktionsgleichung. Als letztes Argument muss angegeben werden,
573573
welche Variable durch die Funktion gebunden werden soll::
574574

575-
sage: f = Piecewise([[(-oo,0), -x^2],[(0,oo), x^2]], x)
575+
sage: f = piecewise([[(-oo,0), -x^2],[(0,oo), x^2]], var=x)
576576
sage: f(3)
577577
9
578578
sage: f(-3)

src/doc/en/constructions/calculus.rst

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ You can find critical points of a piecewise defined function:
8181
sage: f2 = 1-x
8282
sage: f3 = 2*x
8383
sage: f4 = 10*x-x^2
84-
sage: f = Piecewise([[(0,1),f1],[(1,2),f2],[(2,3),f3],[(3,10),f4]])
84+
sage: f = piecewise([((0,1),f1), ((1,2),f2), ((2,3),f3), ((3,10),f4)])
8585
sage: f.critical_points()
8686
[5.0]
8787

@@ -177,9 +177,10 @@ where :math:`f(x)=1`, :math:`0<x<1`:
177177
::
178178

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

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

213214
sage: f1(x) = x^2
214215
sage: f2(x) = 5-x^2
215-
sage: f = Piecewise([[(0,1),f1],[(1,2),f2]])
216-
sage: f.trapezoid(4)
217-
Piecewise defined function with 4 parts, [[(0, 1/2), 1/2*x],
218-
[(1/2, 1), 9/2*x - 2], [(1, 3/2), 1/2*x + 2],
219-
[(3/2, 2), -7/2*x + 8]]
220-
sage: f.riemann_sum_integral_approximation(6,mode="right")
221-
19/6
222-
sage: f.integral()
223-
Piecewise defined function with 2 parts,
224-
[[(0, 1), x |--> 1/3*x^3], [(1, 2), x |--> -1/3*x^3 + 5*x - 13/3]]
225-
sage: f.integral(definite=True)
226-
3
216+
sage: f = piecewise([[[0,1], f1], [RealSet.open_closed(1,2), f2]])
217+
sage: t = f.trapezoid(2); t
218+
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)
219+
sage: t.integral()
220+
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)
221+
sage: t.integral(definite=True)
222+
9/4
227223

228224
.. index: Laplace transform
229225
@@ -242,7 +238,7 @@ computation.
242238
(x, s)
243239
sage: f1(x) = 1
244240
sage: f2(x) = 1-x
245-
sage: f = Piecewise([[(0,1),f1],[(1,2),f2]])
241+
sage: f = piecewise([((0,1),f1), ((1,2),f2)])
246242
sage: f.laplace(x, s)
247243
-e^(-s)/s + (s + 1)*e^(-2*s)/s^2 + 1/s - e^(-s)/s^2
248244

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

383379
sage: f1 = lambda x: -1
384380
sage: f2 = lambda x: 2
385-
sage: f = Piecewise([[(0,pi/2),f1],[(pi/2,pi),f2]])
381+
sage: f = piecewise([((0,pi/2),f1), ((pi/2,pi),f2)])
386382
sage: f.fourier_series_cosine_coefficient(5,pi)
387383
-3/5/pi
388384
sage: f.fourier_series_sine_coefficient(2,pi)

src/doc/en/constructions/plotting.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ You can plot piecewise-defined functions:
3434

3535
::
3636

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

4545
Other function plots can be produced as well:
4646

src/sage/calculus/wester.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@
526526
sage: # Verify(Simplify(Integrate(x)
527527
sage: # if(x<0) (-x) else x),
528528
sage: # Simplify(if(x<0) (-x^2/2) else x^2/2));
529-
sage: f = piecewise([ [[-10,0], -x], [[0,10], x]])
529+
sage: f = piecewise([ ((-10,0), -x), ((0,10), x)])
530530
sage: f.integral(definite=True)
531531
100
532532

src/sage/functions/all.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
from piecewise import piecewise, Piecewise
1+
from sage.misc.lazy_import import lazy_import
2+
3+
lazy_import('sage.functions.piecewise_old', 'Piecewise') # deprecated
4+
lazy_import('sage.functions.piecewise', 'piecewise')
25

36
from trig import ( sin, cos, sec, csc, cot, tan,
47
asin, acos, atan,

src/sage/functions/other.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1554,7 +1554,7 @@ def __init__(self):
15541554
sage: loads(dumps(binomial(n,k)))
15551555
binomial(n, k)
15561556
"""
1557-
GinacFunction.__init__(self, "binomial", nargs=2,
1557+
GinacFunction.__init__(self, "binomial", nargs=2, preserved_arg=1,
15581558
conversions=dict(maxima='binomial',
15591559
mathematica='Binomial',
15601560
sympy='binomial'))

0 commit comments

Comments
 (0)