 piecewise - Maple Help

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, inequality, or inequation, or a boolean combination thereof, such as $x<3$ or And(x>0, x<=Pi) (which is the same as $0). The conditions can contain relations with polynomials, abs, signum, or piecewise functions, such as $0<{x}^{2}-4\wedge 0 or $\left|x\right|<4$. The left and right-hand sides of inequalities are assumed to be real quantities.
 • When using boolean combinations of relations, the use of boolean functions (such as And and Or) in piecewise functions is recommended over using boolean operators (such as and and or). While it is possible to use the operators if all relations involved are inequalities, or if all variables have been assigned values, using operators with equations or inequations involving symbolic parameters will lead to unexpected results. For more details, see the documentation of the evalb command.
 • 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 Panel.
 • 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.
 • The piecewise function evaluates its arguments on an as-needed basis. For example, if the first argument of a 3-argument call evaluates to true, the second argument is then evaluated and returned; the third argument is never evaluated.
 An exception to the above is if piecewise is passed a single argument. That argument is evaluated, and if it yields an expression sequence, the members of that sequence are used as the actual arguments to piecewise. No further evaluation of the arguments takes place. Examples

 > $\mathrm{piecewise}\left(0
 $\left\{\begin{array}{cc}{x}& {0}{<}{x}\\ {0}& {\mathrm{otherwise}}\end{array}\right\$ (1)
 > $\mathrm{eq1}≔\mathrm{piecewise}\left(\mathrm{And}\left(4<{x}^{2},x<8\right),f\left(x\right)\right)$
 ${\mathrm{eq1}}{≔}\left\{\begin{array}{cc}{f}{}\left({x}\right)& {4}{<}{{x}}^{{2}}{\wedge }{x}{<}{8}\\ {0}& {\mathrm{otherwise}}\end{array}\right\$ (2)
 > $\mathrm{simplify}\left(\mathrm{eq1}\right)$
 $\left\{\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}\right\$ (3)

Piecewise functions can have parameters in the conditions.

 > $\mathrm{assume}\left(a
 > $\mathrm{eq2}≔\mathrm{piecewise}\left(\mathrm{And}\left(a
 ${\mathrm{eq2}}{≔}\left\{\begin{array}{cc}{1}& {\mathrm{a~}}{<}{x}{\wedge }{x}{<}{\mathrm{b~}}\\ {2}& {\mathrm{b~}}{<}{x}{\wedge }{x}{<}{\mathrm{c~}}\end{array}\right\$ (4)
 > $\mathrm{convert}\left(\mathrm{eq2},\mathrm{piecewise},x\right)$
 $\left\{\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}\right\$ (5)
 > $\mathrm{assume}\left(d<0\right):$
 > $\mathrm{eq3}≔\mathrm{piecewise}\left(0
 ${\mathrm{eq3}}{≔}\left\{\begin{array}{cc}{1}& {0}{<}{\mathrm{d~}}{}{x}{-}{2}\\ {2}& {\mathrm{otherwise}}\end{array}\right\$ (6)
 > $\mathrm{convert}\left(\mathrm{eq3},\mathrm{piecewise},x\right)$
 $\left\{\begin{array}{cc}{1}& {x}{<}\frac{{2}}{{\mathrm{d~}}}\\ {2}& \frac{{2}}{{\mathrm{d~}}}{\le }{x}\end{array}\right\$ (7)

Using boolean operators in piecewise conditions can lead to unexpected results, because the conditions are evaluated according to the rules used by the evalb command.

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

Using the boolean functions And, Or, and Not instead is recommended. This delays evaluation of the conditions until their truth or falsehood can be determined definitively.

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

Piecewise functions can be simplified.

 > $p≔\mathrm{piecewise}\left(x<0,-x,0
 ${p}{≔}\left\{\begin{array}{cc}{-}{x}& {x}{<}{0}\\ {x}& {0}{<}{x}\end{array}\right\$ (20)
 > $\mathrm{eq4}≔{p}^{2}+5$
 ${\mathrm{eq4}}{≔}{\left(\left\{\begin{array}{cc}{-}{x}& {x}{<}{0}\\ {x}& {0}{<}{x}\end{array}\right\\right)}^{{2}}{+}{5}$ (21)
 > $\mathrm{simplify}\left(\mathrm{eq4}\right)$
 ${{x}}^{{2}}{+}{5}$ (22)
 > $l≔\mathrm{piecewise}\left(x<0,{x}^{2},0
 ${l}{≔}\left\{\begin{array}{cc}{{x}}^{{2}}& {x}{<}{0}\\ {x}& {0}{<}{x}\end{array}\right\$ (23)
 > $\mathrm{eq5}≔{l}^{2}+5$
 ${\mathrm{eq5}}{≔}{\left(\left\{\begin{array}{cc}{{x}}^{{2}}& {x}{<}{0}\\ {x}& {0}{<}{x}\end{array}\right\\right)}^{{2}}{+}{5}$ (24)
 > $\mathrm{simplify}\left(\mathrm{eq5}\right)$
 ${{x}}^{\left\{\begin{array}{cc}{4}& {x}{\le }{0}\\ {2}& {0}{<}{x}\end{array}\right\}{+}{5}$ (25)
 > $m≔\mathrm{piecewise}\left(x
 ${m}{≔}\left\{\begin{array}{cc}{x}& {x}{<}{\mathrm{a~}}\\ {{x}}^{{2}}& {\mathrm{b~}}{<}{x}\end{array}\right\$ (26)
 > $\mathrm{eq6}≔\mathrm{simplify}\left({m}^{2}\right)$
 ${\mathrm{eq6}}{≔}{{x}}^{{2}}{}{\left(\left\{\begin{array}{cc}{1}& {x}{<}{\mathrm{a~}}\\ {x}& {\mathrm{b~}}{<}{x}\end{array}\right\\right)}^{{2}}$ (27)

However, if the intent is to convert to a single piecewise function, convert should be used instead:

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

Other operations involving piecewise functions.

 > $k≔x↦\mathrm{piecewise}\left(x<0,-1,x<1,0,1\right)$
 ${k}{≔}{x}{↦}\left\{\begin{array}{cc}{-1}& {x}{<}{0}\\ {0}& {x}{<}{1}\\ {1}& {\mathrm{otherwise}}\end{array}\right\$ (29)
 > $k\left(\frac{1}{2}\right)$
 ${0}$ (30)
 > $j≔\mathrm{piecewise}\left(x<0,-{x}^{2},0
 ${j}{≔}\left\{\begin{array}{cc}{-}{{x}}^{{2}}& {x}{<}{0}\\ {{x}}^{{2}}& {0}{<}{x}\end{array}\right\$ (31)
 > $\mathrm{diff}\left(j,x\right)$
 $\left\{\begin{array}{cc}{-}{2}{}{x}& {x}{\le }{0}\\ {2}{}{x}& {0}{<}{x}\end{array}\right\$ (32)
 > $\mathrm{int}\left(j,x\right)$
 $\left\{\begin{array}{cc}{-}\frac{{{x}}^{{3}}}{{3}}& {x}{\le }{0}\\ \frac{{{x}}^{{3}}}{{3}}& {0}{<}{x}\end{array}\right\$ (33)
 > $\mathrm{eval}\left(j,x=2\right)$
 ${4}$ (34)
 > $\mathrm{eval}\left(j,x=-2\right)$
 ${-4}$ (35)