 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.3em}{0.0ex}}\mathbf{and}\phantom{\rule[-0.0ex]{0.3em}{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 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(4<{x}^{2}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{and}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}x<8,f\left(x\right)\right)$
 ${\mathrm{eq1}}{≔}\left\{\begin{array}{cc}{f}{}\left({x}\right)& {4}{<}{{x}}^{{2}}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}{\mathbf{and}}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}{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(a
 ${\mathrm{eq2}}{≔}\left\{\begin{array}{cc}{1}& {\mathrm{a~}}{<}{x}{<}{\mathrm{b~}}\\ {2}& {\mathrm{b~}}{<}{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)

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.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)

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

 > $f≔\mathrm{piecewise}\left(x=0\vee x=1,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(x\ne 0\wedge 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(n::'\mathrm{integer}'\wedge 3
 ${h}{≔}\left\{\begin{array}{cc}{{n}}^{{2}}& {n}{::}{?}{\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}{::}{?}\\ {{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)

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

 > $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)
 > $\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)