20.1 Piecewise-defined functions

Module: sage.functions.piecewise

SAGE implements a very simple class of piecewise-defined functions. Functions must be piecewise polynomial, though some methods apply more generally. Only compactly supported functions are currently implemented. Moreover, the coefficients should be rational and the support should be 'connected'. The intervals of polynomial support can be in terms of rationals and $ \pi$ , or in terms of floats.

Implemented methods: latex outout __call__ plotting fourier series fourier series value coefficients (also sine series and cosine series) partial sum (in string format) plot of partial sum laplace transform latex output option domain range list addition (of functions) multiplication (of functions, or fcn*scalar - ie, *right* multiplication by QQ) critical points

TODO: Implement (a) functions defined on infinite intervals, (b) max/min location and values, (c) left multiplication by a scalar. (d) Extend the implementation of the trick to pass SAGE's pi back and forth with Maxima's [[Passing the constants to maxima is already implemented; maybe need passing them back?]]

(For more general non-polynomial piecewise functions, it appears a new class of functions (for example, 'ElementaryFunctionRing') is needed. This a preliminary 'todo'.)

Author: David Joyner (2006-04)

Class: Piecewise

class Piecewise
Piecewise( self, list_of_pairs)

list_of_pairs is a list of pairs (fcn,I), where fcn is a SAGE function (such as a polynomial over RR, or functions using the lambda notation), and I is an interval such as I = (1,3). Two consecutive intervals must share a common endpoint.

We assume that these definitions are consistent (ie, no checking is done).

Functions: base_ring,$  $ cosine_series_coefficient,$  $ critical_points,$  $ domain,$  $ end_points,$  $ fourier_series_cosine_coefficient,$  $ fourier_series_partial_sum,$  $ fourier_series_sine_coefficient,$  $ fourier_series_value,$  $ functions,$  $ integral,$  $ intervals,$  $ laplace_transform,$  $ latex,$  $ length,$  $ list,$  $ plot,$  $ plot_fourier_series_partial_sum,$  $ sine_series_coefficient,$  $ which_function

base_ring( self)

Returns the base-ring (ie, QQ[x]) - useful when this class is extended.

cosine_series_coefficient( self, n, L)

Returns the n-th cosine series coefficient of $ \cos(n\pi x/L)$ , $ a_n$ .

INPUT:
    self -- the function f(x), defined over 0 < x < L (no checking is done
                                                to insure this)
    n    -- an integer n>=0
    L    -- (the period)/2

OUTPUT:
    $a_n = \frac{2}{L}\int_{-L}^L f(x)\cos(n\pi x/L)dx$ such that
\[
f(x) \sim \frac{a_0}{2} + 
           \sum_{n=1}^\infty a_n\cos(\frac{n\pi x}{L}),\ \ 0<x<L.
\]

sage: f = lambda x:x
sage: f = Piecewise([[(0,1),f]])
sage: f.cosine_series_coefficient(2,1)  
0
sage: f.cosine_series_coefficient(3,1)
(-4/(9*(pi^2)))
sage: f1 = lambda x:-1
sage: f2 = lambda x:2
sage: f = Piecewise([[(0,pi/2),f1],[(pi/2,pi),f2]])
sage: f.cosine_series_coefficient(2,pi)
0
sage: f.cosine_series_coefficient(3,pi)
(2/pi)
sage: f.cosine_series_coefficient(111,pi)
(2/(37*pi))

critical_points( self)

Function to return the critical points. Uses maxima, which prints the warning to use results with caution. Only works for piecewise functions whose parts are polynomials with real critical not occurring on the interval endpoints.

sage: x = PolynomialRing(RationalField()).gen()
sage: f1 = x^0
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.critical_points()
[5.0]

fourier_series_cosine_coefficient( self, n, L)

Returns the n-th Fourier series coefficient of $ \cos(n\pi x/L)$ , $ a_n$ .

INPUT:
    self -- the function f(x), defined over -L < x < L
    n    -- an integer n>=0
    L    -- (the period)/2

OUTPUT:
    $a_n = \frac{1}{L}\int_{-L}^L f(x)\cos(n\pi x/L)dx$

       sage: f = lambda x:x^2
       sage: f = Piecewise([[(-1,1),f]])
       sage: f.fourier_series_cosine_coefficient(2,1)
       (1/(pi^2))
sage: f = lambda x:x^2
       sage: f = Piecewise([[(-pi,pi),f]])
       sage: f.fourier_series_cosine_coefficient(2,pi)
       1
       sage: f1 = lambda x:-1
       sage: f2 = lambda x:2
       sage: f = Piecewise([[(0,pi/2),f1],[(pi/2,pi),f2]])
       sage: f.fourier_series_cosine_coefficient(5,pi)
       (-3/(5*pi))

fourier_series_partial_sum( self, N, L)

Returns the partial sum

$\displaystyle f(x) \sim \frac{a_0}{2} +
\sum_{n=1}^N [a_n\cos(\frac{n\pi x}{L}) + b_n\sin(\frac{n\pi x}{L})],
$

as a string.

sage: f = lambda x:x^2
sage: f = Piecewise([[(-1,1),f]])
sage: f.fourier_series_partial_sum(3,1)
'1/3 + ((-4/(pi^2))*cos(1*pi*x/1) + 0*sin(1*pi*x/1)) +
((1/(pi^2))*cos(2*pi*x/1) + 0*sin(2*pi*x/1))'
sage: f1 = lambda x:-1
sage: f2 = lambda x:2
sage: f = Piecewise([[(0,pi/2),f1],[(pi/2,pi),f2]])
sage: f.fourier_series_partial_sum(3,pi)
'1/4 + ((-3/pi)*cos(1*pi*x/pi) + (1/pi)*sin(1*pi*x/pi)) + (0*cos(2*pi*x/pi)
+ (-3/pi)*sin(2*pi*x/pi))'

fourier_series_sine_coefficient( self, n, L)

Returns the n-th Fourier series coefficient of $ \sin(n\pi x/L)$ , $ b_n$ .

INPUT:
    self -- the function f(x), defined over -L < x < L
    n    -- an integer n>0
    L    -- (the period)/2

OUTPUT:
    $b_n = \frac{1}{L}\int_{-L}^L f(x)\sin(n\pi x/L)dx$

sage: f = lambda x:x^2
sage: f = Piecewise([[(-1,1),f]])
sage: f.fourier_series_sine_coefficient(2,1)  # L=1, n=2
0

fourier_series_value( self, x, L)

Returns the value of the Fourier series coefficient of self at $ x$ ,

$\displaystyle f(x) \sim \frac{a_0}{2} +
\sum_{n=1}^\infty [a_n\cos(\frac{n\pi x}{L}) + b_n\sin(\frac{n\pi x}{L})],
   -L<x<L.
$

This method applies to piecewise non-polynomial functions as well.

   INPUT:
       self -- the function f(x), defined over -L < x < L
       x    -- a real number 
       L    -- (the period)/2

   OUTPUT:
       $(f^*(x+)+f^*(x-)/2$, where $f^*$ denotes the function $f$ 
       extended to $\R$ with period $2L$ (Dirichlet's Theorem for 
Fourier series).

       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([[(-10,1),f1],[(1,2),f2],[(2,3),f3],[(3,10),f4]])
       sage: f.fourier_series_value(101,10)  
       1/2
       sage: f.fourier_series_value(100,10)
       1
       sage: f.fourier_series_value(10,10)
       0.91294525072762767
       sage: f.fourier_series_value(20,10)
       1
       sage: f.fourier_series_value(30,10)
       0.91294525072762767
       sage: f1 = lambda x:-1
       sage: f2 = lambda x:2
sage: f = Piecewise([[(-pi,0),lambda x:0],[(0,pi/2),f1],[(pi/2,pi),f2]])
       sage: f.fourier_series_value(-1,pi)
       0
       sage: f.fourier_series_value(20,pi)
       -1
       sage: f.fourier_series_value(pi/2,pi)
       1/2

functions( self)

Returns the list of functions (the "pieces").

integral( self)

Returns the definite integral (as computed by maxima) $ \sum_I \int_I self\vert _I$ , as I runs over the intervals belonging to self.

       sage: f1 = lambda x:1
       sage: f2 = lambda x:1-x
       sage: f = Piecewise([[(0,1),f1],[(1,2),f2]])
       sage: f.integral()
       1/2
sage: f1 = lambda x:-1
       sage: f2 = lambda x:2
       sage: f = Piecewise([[(0,pi/2),f1],[(pi/2,pi),f2]])
       sage: f.integral()
       (pi/2)

intervals( self)

A piecewise non-polynomial example.

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.intervals()
[(0, 1), (1, 2), (2, 3), (3, 10)]

laplace_transform( self, [var=0], [latex_output=s])

Returns the laplace transform of self, as a function of var. We assume that a piecewise function is 0 outside of its domain and that the left-most endpoint of the domain is 0.

sage: f1 = lambda x:1
sage: f2 = lambda x:1-x
sage: f = Piecewise([[(0,1),f1],[(1,2),f2]])
sage: f.laplace_transform()
'1/s - e^-s/s + (s + 1)*e^-(2*s)/s^2 - e^-s/s^2'
sage: f.laplace_transform("w",latex_output=1)
' - \\frac{e^{ - w}}{w} - \\frac{e^{ - w}}{w^2} + \\frac{\\left(w +
1\\right)e^{ - 2w}}{w^2} + \\frac{1}{w}'
sage: f.laplace_transform("w",True)
' - \\frac{e^{ - w}}{w} - \\frac{e^{ - w}}{w^2} + \\frac{\\left(w +
1\\right)e^{ - 2w}}{w^2} + \\frac{1}{w}'
sage: f.laplace_transform("w")
'1/w - e^-w/w + (w + 1)*e^-(2*w)/w^2 - e^-w/w^2'

latex( self)

sage: f1 = lambda x:1
sage: f2 = lambda x:1-x
sage: f = Piecewise([[(0,1),f1],[(1,2),f2]])
sage: f.latex()
'\begin{array}{ll} \left\{ 1,\& 0 < x < 1 ,\right. \end{array}'

plot( self)

Returns the plot of self.

Keyword arguments are passed onto the plot command for each piece of the function. E.g., the plot_points keyword affects each segment of the plot.

       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: P = f.plot(rgbcolor=(0.7,0.1,0), plot_points=40)

Remember: to view this, type P.save("<path>/myplot.png") and then open it in a graphics viewer such as GIMP.

plot_fourier_series_partial_sum( self, N, L, xmin, xmax)

Plots the partial sum

$\displaystyle f(x) \sim \frac{a_0}{2} +
\sum_{n=1}^N [a_n\cos(\frac{n\pi x}{L}) + b_n\sin(\frac{n\pi x}{L})],
$

over xmin < x < xmin.

sage: f1 = lambda x:-2
sage: f2 = lambda x:1
sage: f3 = lambda x:-1
sage: f4 = lambda x:2
sage: f = Piecewise([[(-pi,-pi/2),f1],[(-pi/2,0),f2],[(0,pi/2),f3],[(pi/2,pi),f4]])
sage: P = f.plot_fourier_series_partial_sum(3,pi,-5,5)    # long time
sage: f1 = lambda x:-1
sage: f2 = lambda x:2
sage: f = Piecewise([[(0,pi/2),f1],[(pi/2,pi),f2]])
sage: P = f.plot_fourier_series_partial_sum(15,pi,-5,5)   # long time

Remember, to view this type P.save("<path>/myplot.png") and then open it in a graphics viewer such as GIMP.

sine_series_coefficient( self, n, L)

Returns the n-th sine series coefficient of $ \sin(n\pi x/L)$ , $ b_n$ .

INPUT:
    self -- the function f(x), defined over 0 < x < L (no checking is done
                                                to insure this)
    n    -- an integer n>0
    L    -- (the period)/2

OUTPUT:
    $b_n = \frac{2}{L}\int_{-L}^L f(x)\sin(n\pi x/L)dx$ such that
\[
f(x) \sim \sum_{n=1}^\infty b_n\sin(\frac{n\pi x}{L}),\ \ 0<x<L.
\]

sage: f = lambda x:1
sage: f = Piecewise([[(0,1),f]])
sage: f.sine_series_coefficient(2,1)  
0
sage: f.sine_series_coefficient(3,1)
(4/(3*pi))

which_function( self, x0)

Returns the function piece used to evaluate self at x0.

sage: x = PolynomialRing(RationalField()).gen()
       sage: f1 = lambda z:1
       sage: f2 = 1-x
       sage: f3 = lambda y:exp(y)
       sage: f4 = lambda t:sin(2*t)
       sage: f = Piecewise([[(0,1),f1],[(1,2),f2],[(2,3),f3],[(3,10),f4]])
       sage: f.which_function(3/2)
-x + 1

Special Functions: __add__,$  $ __call__,$  $ __mul__,$  $ __repr__

__add__( self, other)

Returns the piecewise defined function which is the sum of self and other.

sage: x = PolynomialRing(RationalField()).gen()
sage: f1 = x^0
       sage: f2 = 1-x
       sage: f3 = 2*x
       sage: f4 = 10-x
       sage: f = Piecewise([[(0,1),f1],[(1,2),f2],[(2,3),f3],[(3,10),f4]])
sage: g1 = x-2
       sage: g2 = x-5
       sage: g = Piecewise([[(0,5),g1],[(5,10),g2]])
sage: h = f+g
sage: h
       Piecewise defined function with 5 parts, [[[0, 1], x - 1], [[1, 2],
-1], [[2, 3], 3*x - 2], [[3, 5], 8], [[5, 10], 5]]

Note that in this case the functions must be defined using polynomial expressions *not* using the lambda notation.

__call__( self, x0)

Evaluates self at x0. Returns the average value of the jump if x0 is an interior endpoint of one of the intervals of self and the usual value otherwise.

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(0.5)
1
sage: f(2.5)
12.182493960703473
sage: f(1)
1/2

__mul__( self, other)

Returns the piecewise defined function which is the product of one piecewise function (self) with another one (other).

sage: x = PolynomialRing(RationalField()).gen()
sage: f1 = x^0
       sage: f2 = 1-x
       sage: f3 = 2*x
       sage: f4 = 10-x
       sage: f = Piecewise([[(0,1),f1],[(1,2),f2],[(2,3),f3],[(3,10),f4]])
sage: g1 = x-2
       sage: g2 = x-5
       sage: g = Piecewise([[(0,5),g1],[(5,10),g2]])
sage: h = f*g
sage: h
Piecewise defined function with 5 parts, [[[0, 1], x - 2], [[1, 2], -x^2 +
3*x - 2], [[2, 3], 2*x^2 - 4*x], [[3, 5], -x^2 + 12*x - 20], [[5, 10], -x^2
+ 15*x - 50]]
       sage: g*(11/2)
       Piecewise defined function with 2 parts, [[[0, 5], 11/2*x - 11],
[[5, 10], 11/2*x - 55/2]]

Note that in this method the functions must be defined using polynomial expressions *not* using the lambda notation.

Class: PiecewisePolynomial

class PiecewisePolynomial
PiecewisePolynomial( self, list_of_pairs)

list_of_pairs is a list of pairs (fcn,I), where fcn is a SAGE function (such as a polynomial over RR, or functions using the lambda notation), and I is an interval such as I = (1,3). Two consecutive intervals must share a common endpoint.

We assume that these definitions are consistent (ie, no checking is done).

Functions: base_ring,$  $ cosine_series_coefficient,$  $ critical_points,$  $ domain,$  $ end_points,$  $ fourier_series_cosine_coefficient,$  $ fourier_series_partial_sum,$  $ fourier_series_sine_coefficient,$  $ fourier_series_value,$  $ functions,$  $ integral,$  $ intervals,$  $ laplace_transform,$  $ latex,$  $ length,$  $ list,$  $ plot,$  $ plot_fourier_series_partial_sum,$  $ sine_series_coefficient,$  $ which_function

base_ring( self)

Returns the base-ring (ie, QQ[x]) - useful when this class is extended.

cosine_series_coefficient( self, n, L)

Returns the n-th cosine series coefficient of $ \cos(n\pi x/L)$ , $ a_n$ .

INPUT:
    self -- the function f(x), defined over 0 < x < L (no checking is done
                                                to insure this)
    n    -- an integer n>=0
    L    -- (the period)/2

OUTPUT:
    $a_n = \frac{2}{L}\int_{-L}^L f(x)\cos(n\pi x/L)dx$ such that
\[
f(x) \sim \frac{a_0}{2} + 
           \sum_{n=1}^\infty a_n\cos(\frac{n\pi x}{L}),\ \ 0<x<L.
\]

sage: f = lambda x:x
sage: f = Piecewise([[(0,1),f]])
sage: f.cosine_series_coefficient(2,1)  
0
sage: f.cosine_series_coefficient(3,1)
(-4/(9*(pi^2)))
sage: f1 = lambda x:-1
sage: f2 = lambda x:2
sage: f = Piecewise([[(0,pi/2),f1],[(pi/2,pi),f2]])
sage: f.cosine_series_coefficient(2,pi)
0
sage: f.cosine_series_coefficient(3,pi)
(2/pi)
sage: f.cosine_series_coefficient(111,pi)
(2/(37*pi))

critical_points( self)

Function to return the critical points. Uses maxima, which prints the warning to use results with caution. Only works for piecewise functions whose parts are polynomials with real critical not occurring on the interval endpoints.

sage: x = PolynomialRing(RationalField()).gen()
sage: f1 = x^0
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.critical_points()
[5.0]

fourier_series_cosine_coefficient( self, n, L)

Returns the n-th Fourier series coefficient of $ \cos(n\pi x/L)$ , $ a_n$ .

INPUT:
    self -- the function f(x), defined over -L < x < L
    n    -- an integer n>=0
    L    -- (the period)/2

OUTPUT:
    $a_n = \frac{1}{L}\int_{-L}^L f(x)\cos(n\pi x/L)dx$

       sage: f = lambda x:x^2
       sage: f = Piecewise([[(-1,1),f]])
       sage: f.fourier_series_cosine_coefficient(2,1)
       (1/(pi^2))
sage: f = lambda x:x^2
       sage: f = Piecewise([[(-pi,pi),f]])
       sage: f.fourier_series_cosine_coefficient(2,pi)
       1
       sage: f1 = lambda x:-1
       sage: f2 = lambda x:2
       sage: f = Piecewise([[(0,pi/2),f1],[(pi/2,pi),f2]])
       sage: f.fourier_series_cosine_coefficient(5,pi)
       (-3/(5*pi))

fourier_series_partial_sum( self, N, L)

Returns the partial sum

$\displaystyle f(x) \sim \frac{a_0}{2} +
\sum_{n=1}^N [a_n\cos(\frac{n\pi x}{L}) + b_n\sin(\frac{n\pi x}{L})],
$

as a string.

sage: f = lambda x:x^2
sage: f = Piecewise([[(-1,1),f]])
sage: f.fourier_series_partial_sum(3,1)
'1/3 + ((-4/(pi^2))*cos(1*pi*x/1) + 0*sin(1*pi*x/1)) +
((1/(pi^2))*cos(2*pi*x/1) + 0*sin(2*pi*x/1))'
sage: f1 = lambda x:-1
sage: f2 = lambda x:2
sage: f = Piecewise([[(0,pi/2),f1],[(pi/2,pi),f2]])
sage: f.fourier_series_partial_sum(3,pi)
'1/4 + ((-3/pi)*cos(1*pi*x/pi) + (1/pi)*sin(1*pi*x/pi)) + (0*cos(2*pi*x/pi)
+ (-3/pi)*sin(2*pi*x/pi))'

fourier_series_sine_coefficient( self, n, L)

Returns the n-th Fourier series coefficient of $ \sin(n\pi x/L)$ , $ b_n$ .

INPUT:
    self -- the function f(x), defined over -L < x < L
    n    -- an integer n>0
    L    -- (the period)/2

OUTPUT:
    $b_n = \frac{1}{L}\int_{-L}^L f(x)\sin(n\pi x/L)dx$

sage: f = lambda x:x^2
sage: f = Piecewise([[(-1,1),f]])
sage: f.fourier_series_sine_coefficient(2,1)  # L=1, n=2
0

fourier_series_value( self, x, L)

Returns the value of the Fourier series coefficient of self at $ x$ ,

$\displaystyle f(x) \sim \frac{a_0}{2} +
\sum_{n=1}^\infty [a_n\cos(\frac{n\pi x}{L}) + b_n\sin(\frac{n\pi x}{L})],
   -L<x<L.
$

This method applies to piecewise non-polynomial functions as well.

   INPUT:
       self -- the function f(x), defined over -L < x < L
       x    -- a real number 
       L    -- (the period)/2

   OUTPUT:
       $(f^*(x+)+f^*(x-)/2$, where $f^*$ denotes the function $f$ 
       extended to $\R$ with period $2L$ (Dirichlet's Theorem for 
Fourier series).

       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([[(-10,1),f1],[(1,2),f2],[(2,3),f3],[(3,10),f4]])
       sage: f.fourier_series_value(101,10)  
       1/2
       sage: f.fourier_series_value(100,10)
       1
       sage: f.fourier_series_value(10,10)
       0.91294525072762767
       sage: f.fourier_series_value(20,10)
       1
       sage: f.fourier_series_value(30,10)
       0.91294525072762767
       sage: f1 = lambda x:-1
       sage: f2 = lambda x:2
sage: f = Piecewise([[(-pi,0),lambda x:0],[(0,pi/2),f1],[(pi/2,pi),f2]])
       sage: f.fourier_series_value(-1,pi)
       0
       sage: f.fourier_series_value(20,pi)
       -1
       sage: f.fourier_series_value(pi/2,pi)
       1/2

functions( self)

Returns the list of functions (the "pieces").

integral( self)

Returns the definite integral (as computed by maxima) $ \sum_I \int_I self\vert _I$ , as I runs over the intervals belonging to self.

       sage: f1 = lambda x:1
       sage: f2 = lambda x:1-x
       sage: f = Piecewise([[(0,1),f1],[(1,2),f2]])
       sage: f.integral()
       1/2
sage: f1 = lambda x:-1
       sage: f2 = lambda x:2
       sage: f = Piecewise([[(0,pi/2),f1],[(pi/2,pi),f2]])
       sage: f.integral()
       (pi/2)

intervals( self)

A piecewise non-polynomial example.

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.intervals()
[(0, 1), (1, 2), (2, 3), (3, 10)]

laplace_transform( self, [var=0], [latex_output=s])

Returns the laplace transform of self, as a function of var. We assume that a piecewise function is 0 outside of its domain and that the left-most endpoint of the domain is 0.

sage: f1 = lambda x:1
sage: f2 = lambda x:1-x
sage: f = Piecewise([[(0,1),f1],[(1,2),f2]])
sage: f.laplace_transform()
'1/s - e^-s/s + (s + 1)*e^-(2*s)/s^2 - e^-s/s^2'
sage: f.laplace_transform("w",latex_output=1)
' - \\frac{e^{ - w}}{w} - \\frac{e^{ - w}}{w^2} + \\frac{\\left(w +
1\\right)e^{ - 2w}}{w^2} + \\frac{1}{w}'
sage: f.laplace_transform("w",True)
' - \\frac{e^{ - w}}{w} - \\frac{e^{ - w}}{w^2} + \\frac{\\left(w +
1\\right)e^{ - 2w}}{w^2} + \\frac{1}{w}'
sage: f.laplace_transform("w")
'1/w - e^-w/w + (w + 1)*e^-(2*w)/w^2 - e^-w/w^2'

latex( self)

sage: f1 = lambda x:1
sage: f2 = lambda x:1-x
sage: f = Piecewise([[(0,1),f1],[(1,2),f2]])
sage: f.latex()
'\begin{array}{ll} \left\{ 1,\& 0 < x < 1 ,\right. \end{array}'

plot( self)

Returns the plot of self.

Keyword arguments are passed onto the plot command for each piece of the function. E.g., the plot_points keyword affects each segment of the plot.

       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: P = f.plot(rgbcolor=(0.7,0.1,0), plot_points=40)

Remember: to view this, type P.save("<path>/myplot.png") and then open it in a graphics viewer such as GIMP.

plot_fourier_series_partial_sum( self, N, L, xmin, xmax)

Plots the partial sum

$\displaystyle f(x) \sim \frac{a_0}{2} +
\sum_{n=1}^N [a_n\cos(\frac{n\pi x}{L}) + b_n\sin(\frac{n\pi x}{L})],
$

over xmin < x < xmin.

sage: f1 = lambda x:-2
sage: f2 = lambda x:1
sage: f3 = lambda x:-1
sage: f4 = lambda x:2
sage: f = Piecewise([[(-pi,-pi/2),f1],[(-pi/2,0),f2],[(0,pi/2),f3],[(pi/2,pi),f4]])
sage: P = f.plot_fourier_series_partial_sum(3,pi,-5,5)    # long time
sage: f1 = lambda x:-1
sage: f2 = lambda x:2
sage: f = Piecewise([[(0,pi/2),f1],[(pi/2,pi),f2]])
sage: P = f.plot_fourier_series_partial_sum(15,pi,-5,5)   # long time

Remember, to view this type P.save("<path>/myplot.png") and then open it in a graphics viewer such as GIMP.

sine_series_coefficient( self, n, L)

Returns the n-th sine series coefficient of $ \sin(n\pi x/L)$ , $ b_n$ .

INPUT:
    self -- the function f(x), defined over 0 < x < L (no checking is done
                                                to insure this)
    n    -- an integer n>0
    L    -- (the period)/2

OUTPUT:
    $b_n = \frac{2}{L}\int_{-L}^L f(x)\sin(n\pi x/L)dx$ such that
\[
f(x) \sim \sum_{n=1}^\infty b_n\sin(\frac{n\pi x}{L}),\ \ 0<x<L.
\]

sage: f = lambda x:1
sage: f = Piecewise([[(0,1),f]])
sage: f.sine_series_coefficient(2,1)  
0
sage: f.sine_series_coefficient(3,1)
(4/(3*pi))

which_function( self, x0)

Returns the function piece used to evaluate self at x0.

sage: x = PolynomialRing(RationalField()).gen()
       sage: f1 = lambda z:1
       sage: f2 = 1-x
       sage: f3 = lambda y:exp(y)
       sage: f4 = lambda t:sin(2*t)
       sage: f = Piecewise([[(0,1),f1],[(1,2),f2],[(2,3),f3],[(3,10),f4]])
       sage: f.which_function(3/2)
-x + 1

Special Functions: __add__,$  $ __call__,$  $ __mul__,$  $ __repr__

__add__( self, other)

Returns the piecewise defined function which is the sum of self and other.

sage: x = PolynomialRing(RationalField()).gen()
sage: f1 = x^0
       sage: f2 = 1-x
       sage: f3 = 2*x
       sage: f4 = 10-x
       sage: f = Piecewise([[(0,1),f1],[(1,2),f2],[(2,3),f3],[(3,10),f4]])
sage: g1 = x-2
       sage: g2 = x-5
       sage: g = Piecewise([[(0,5),g1],[(5,10),g2]])
sage: h = f+g
sage: h
       Piecewise defined function with 5 parts, [[[0, 1], x - 1], [[1, 2],
-1], [[2, 3], 3*x - 2], [[3, 5], 8], [[5, 10], 5]]

Note that in this case the functions must be defined using polynomial expressions *not* using the lambda notation.

__call__( self, x0)

Evaluates self at x0. Returns the average value of the jump if x0 is an interior endpoint of one of the intervals of self and the usual value otherwise.

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(0.5)
1
sage: f(2.5)
12.182493960703473
sage: f(1)
1/2

__mul__( self, other)

Returns the piecewise defined function which is the product of one piecewise function (self) with another one (other).

sage: x = PolynomialRing(RationalField()).gen()
sage: f1 = x^0
       sage: f2 = 1-x
       sage: f3 = 2*x
       sage: f4 = 10-x
       sage: f = Piecewise([[(0,1),f1],[(1,2),f2],[(2,3),f3],[(3,10),f4]])
sage: g1 = x-2
       sage: g2 = x-5
       sage: g = Piecewise([[(0,5),g1],[(5,10),g2]])
sage: h = f*g
sage: h
Piecewise defined function with 5 parts, [[[0, 1], x - 2], [[1, 2], -x^2 +
3*x - 2], [[2, 3], 2*x^2 - 4*x], [[3, 5], -x^2 + 12*x - 20], [[5, 10], -x^2
+ 15*x - 50]]
       sage: g*(11/2)
       Piecewise defined function with 2 parts, [[[0, 5], 11/2*x - 11],
[[5, 10], 11/2*x - 55/2]]

Note that in this method the functions must be defined using polynomial expressions *not* using the lambda notation.

See About this document... for information on suggesting changes.