piecewise - Maple Programming Help

Home : Support : Online Help : Mathematics : Mathematical Functions : piecewise

piecewise

piecewise-continuous functions

 Calling Sequence piecewise(cond_1, f_1, cond_2, f_2, ..., cond_n, f_n, f_otherwise)

Parameters

 f_i - expression cond_i - relation or boolean combination of inequalities f_otherwise - (optional) default expression

Description

 • With the piecewise function it is possible to express piecewise-continuous functions.  The semantics are as in a case statement: if cond_1 is true then f_1, else if cond_2 is true then f_2, and so on. f_otherwise gives a default case which corresponds to all conditions being false.  The default for f_otherwise is 0.
 • A condition can be a single equality or inequality, or a boolean combination of inequalities, such as $x<3$ or $0. Equalities cannot be used in a boolean expression. The conditions can contain relations with polynomials, abs, signum, or piecewise functions, such as $0<{x}^{2}-4\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{and}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}0 or $\left|x\right|<4$.  In all cases, $x$ is assumed to be a real variable.
 • The piecewise function can be differentiated, integrated, simplified, plotted, and used in the following types of differential equations:  constant coefficients and discontinuous perturbation function, general first-order linear, Riccati, and some other classes which are handled by integration or variation of parameter.  See dsolve[piecewise] for more details. series, limit, abs, and signum can handle the piecewise function.
 • If parameters are involved in the conditions, the system requires useful assumptions in order to perform computations.  For example, piecewise(a*x<1,f(x)) cannot be manipulated unless Maple can determine whether a is positive or negative using the assume system. See "What Assumptions" in the context-sensitive menu.
 • There exist convert procedures to convert a piecewise function to an expression containing the Heaviside function and vice versa. A piecewise function can also be converted to a list representation, called pwlist. Expressions containing abs or signum can be converted to piecewise functions.
 • To enter a piecewise function in 2-D Math notation, you can use either the palettes or command completion. To add an additional line to this piecewise function, press Ctrl + Shift + R. See 2-D Math Shortcut Keys and Hints for more information.

Examples

 > $\mathrm{piecewise}\left(0
 ${{}\begin{array}{cc}{x}& {0}{<}{x}\\ {0}& {\mathrm{otherwise}}\end{array}$ (1)
 > $\mathrm{eq1}≔\mathrm{piecewise}\left(4<{x}^{2}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{and}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}x<8,f\left(x\right)\right)$
 ${\mathrm{eq1}}{:=}{{}\begin{array}{cc}{f}{}\left({x}\right)& {4}{<}{{x}}^{{2}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{and}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{x}{<}{8}\\ {0}& {\mathrm{otherwise}}\end{array}$ (2)
 > $\mathrm{simplify}\left(\mathrm{eq1}\right)$
 ${{}\begin{array}{cc}{f}{}\left({x}\right)& {x}{<}{-}{2}\\ {0}& {x}{\le }{2}\\ {f}{}\left({x}\right)& {x}{<}{8}\\ {0}& {8}{\le }{x}\end{array}$ (3)

Piecewise functions can have parameters in the conditions.

 > $\mathrm{assume}\left(a
 > $\mathrm{eq2}≔\mathrm{piecewise}\left(a
 ${\mathrm{eq2}}{:=}{{}\begin{array}{cc}{1}& {\mathrm{a~}}{<}{x}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{and}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{x}{<}{\mathrm{b~}}\\ {2}& {\mathrm{b~}}{<}{x}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{and}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{x}{<}{\mathrm{c~}}\end{array}$ (4)
 > $\mathrm{convert}\left(\mathrm{eq2},\mathrm{piecewise},x\right)$
 ${{}\begin{array}{cc}{0}& {x}{\le }{\mathrm{a~}}\\ {1}& {x}{<}{\mathrm{b~}}\\ {0}& {x}{=}{\mathrm{b~}}\\ {2}& {x}{<}{\mathrm{c~}}\\ {0}& {\mathrm{c~}}{\le }{x}\end{array}$ (5)
 > $\mathrm{assume}\left(d<0\right):$
 > $\mathrm{eq3}≔\mathrm{piecewise}\left(0
 ${\mathrm{eq3}}{:=}{{}\begin{array}{cc}{1}& {0}{<}{\mathrm{d~}}{}{x}{-}{2}\\ {2}& {\mathrm{otherwise}}\end{array}$ (6)
 > $\mathrm{convert}\left(\mathrm{eq3},\mathrm{piecewise},x\right)$
 ${{}\begin{array}{cc}{1}& {x}{<}\frac{{2}}{{\mathrm{d~}}}\\ {2}& \frac{{2}}{{\mathrm{d~}}}{\le }{x}\end{array}$ (7)

Note that logical operators, involved in the piecewise conditions, are evaluated according to the same rules as used by the evalb command

 > $\mathrm{piecewise}\left(x=0\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{or}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}x=1,a,b\right)$
 ${\mathrm{b~}}$ (8)
 > $\mathrm{piecewise}\left(x\ne 0\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{and}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}0
 ${{}\begin{array}{cc}{\mathrm{a~}}& {0}{<}{y}\\ {\mathrm{b~}}& {\mathrm{otherwise}}\end{array}$ (9)
 > $\mathrm{piecewise}\left(n::\mathrm{integer}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{and}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}3
 ${{n}}^{{3}}$ (10)
 > $x=0\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{or}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}x=1$
 ${\mathrm{false}}$ (11)
 > $x\ne 0\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{and}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}0
 ${0}{<}{y}$ (12)
 > $n::\mathrm{integer}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{and}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}3
 ${\mathrm{false}}$ (13)

As an alternative, use the inert functions And, Or, and Not to construct boolean expressions involving multiple conditions

 > $f≔\mathrm{piecewise}\left(\mathrm{Or}\left(x=0,x=1\right),a,b\right)$
 ${f}{:=}{{}\begin{array}{cc}{\mathrm{a~}}& {\mathrm{Or}}{}\left({x}{=}{0}{,}{x}{=}{1}\right)\\ {\mathrm{b~}}& {\mathrm{otherwise}}\end{array}$ (14)
 > $f\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}assuming\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}0
 ${{}\begin{array}{cc}{\mathrm{a~}}& {x}{=}{1}\\ {\mathrm{b~}}& {\mathrm{otherwise}}\end{array}$ (15)
 > $g≔\mathrm{piecewise}\left(\mathrm{And}\left(x\ne 0,0
 ${g}{:=}{{}\begin{array}{cc}{\mathrm{a~}}& {\mathrm{And}}{}\left({x}{\ne }{0}{,}{0}{<}{y}\right)\\ {\mathrm{b~}}& {\mathrm{otherwise}}\end{array}$ (16)
 > $g\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}assuming\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}0
 ${{}\begin{array}{cc}{\mathrm{a~}}& {x}{\ne }{0}\\ {\mathrm{b~}}& {\mathrm{otherwise}}\end{array}$ (17)
 > $h≔\mathrm{piecewise}\left(\mathrm{And}\left(n::\mathrm{integer},3
 ${h}{:=}{{}\begin{array}{cc}{{n}}^{{2}}& {\mathrm{And}}{}\left({n}{::}{\mathrm{integer}}{,}{3}{<}{n}\right)\\ {{n}}^{{3}}& {\mathrm{otherwise}}\end{array}$ (18)
 > $h\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}assuming\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}3
 ${{}\begin{array}{cc}{{n}}^{{2}}& {n}{::}{\mathrm{integer}}\\ {{n}}^{{3}}& {\mathrm{otherwise}}\end{array}$ (19)

Piecewise functions can be simplified

 > $p≔\mathrm{piecewise}\left(x<0,-x,0
 ${p}{:=}{{}\begin{array}{cc}{-}{x}& {x}{<}{0}\\ {x}& {0}{<}{x}\end{array}$ (20)
 > $\mathrm{eq4}≔{p}^{2}+5$
 ${\mathrm{eq4}}{:=}{\left({{}\begin{array}{cc}{-}{x}& {x}{<}{0}\\ {x}& {0}{<}{x}\end{array}\right)}^{{2}}{+}{5}$ (21)
 > $\mathrm{simplify}\left(\mathrm{eq4}\right)$
 ${{x}}^{{2}}{+}{5}$ (22)
 > $l≔\mathrm{piecewise}\left(x<0,{x}^{2},0
 ${l}{:=}{{}\begin{array}{cc}{{x}}^{{2}}& {x}{<}{0}\\ {x}& {0}{<}{x}\end{array}$ (23)
 > $\mathrm{eq5}≔{l}^{2}+5$
 ${\mathrm{eq5}}{:=}{\left({{}\begin{array}{cc}{{x}}^{{2}}& {x}{<}{0}\\ {x}& {0}{<}{x}\end{array}\right)}^{{2}}{+}{5}$ (24)
 > $\mathrm{simplify}\left(\mathrm{eq5}\right)$
 ${{}\begin{array}{cc}{{x}}^{{4}}{+}{5}& {x}{\le }{0}\\ {{x}}^{{2}}{+}{5}& {0}{<}{x}\end{array}$ (25)

However, if a piecewise function contains parameters, it cannot be simplified directly.

 > $m≔\mathrm{piecewise}\left(x
 ${m}{:=}{{}\begin{array}{cc}{x}& {x}{<}{\mathrm{a~}}\\ {{x}}^{{2}}& {\mathrm{b~}}{<}{x}\end{array}$ (26)
 > $\mathrm{eq6}≔\mathrm{simplify}\left({m}^{2}\right)$
 ${\mathrm{eq6}}{:=}{\left({{}\begin{array}{cc}{x}& {x}{<}{\mathrm{a~}}\\ {{x}}^{{2}}& {\mathrm{b~}}{<}{x}\end{array}\right)}^{{2}}$ (27)
 > $\mathrm{convert}\left(\mathrm{eq6},\mathrm{piecewise},x\right)$
 ${{}\begin{array}{cc}{{x}}^{{2}}& {x}{<}{\mathrm{a~}}\\ {0}& {x}{\le }{\mathrm{b~}}\\ {{x}}^{{4}}& {\mathrm{b~}}{<}{x}\end{array}$ (28)

Other operations involving piecewise functions.

 > $k≔x→\mathrm{piecewise}\left(x<0,-1,x<1,0,1\right)$
 ${k}{:=}{x}{→}{\mathrm{piecewise}}{}\left({x}{<}{0}{,}{-}{1}{,}{x}{<}{1}{,}{0}{,}{1}\right)$ (29)
 > $k\left(\frac{1}{2}\right)$
 ${0}$ (30)
 > $j≔\mathrm{piecewise}\left(x<0,-{x}^{2},0
 ${j}{:=}{{}\begin{array}{cc}{-}{{x}}^{{2}}& {x}{<}{0}\\ {{x}}^{{2}}& {0}{<}{x}\end{array}$ (31)
 > $\frac{\partial }{\partial x}j$
 ${{}\begin{array}{cc}{-}{2}{}{x}& {x}{\le }{0}\\ {2}{}{x}& {0}{<}{x}\end{array}$ (32)
 > $∫j\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}ⅆx$
 ${{}\begin{array}{cc}{-}\frac{{1}}{{3}}{}{{x}}^{{3}}& {x}{\le }{0}\\ \frac{{1}}{{3}}{}{{x}}^{{3}}& {0}{<}{x}\end{array}$ (33)
 > $\genfrac{}{}{0}{}{j}{\phantom{x=2}}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}|\phantom{\rule[-0.0ex]{0.1em}{0.0ex}}\genfrac{}{}{0}{}{\phantom{j}}{x=2}$
 ${4}$ (34)
 > $\genfrac{}{}{0}{}{j}{\phantom{x=-2}}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}|\phantom{\rule[-0.0ex]{0.1em}{0.0ex}}\genfrac{}{}{0}{}{\phantom{j}}{x=-2}$
 ${-}{4}$ (35)