Piecewise-defined expressions

Suppose you have an expression $y(t)$ that is defined as $0$ outside the interval $[-1, 1]$ and on that interval it is defined as $x^4 - 2*x^2 + 1$. How can you define this in Maple?

This is done with the piecewise(...) function, which normally takes $3$, $5%, or larger odd number of arguments. The piecewise(...) is most useful when it is used to define a piecewise-defined expression of a single real variable, and that is the only scenario we will describe here. We will look at a few examples, and then explain this in detail

[> pw1 := peicewise( t <= -1, 0, x <= 1, x^4 - 2*x^2 + 1, 0 );

$pw1 := \left\{ \begin{matrix} 0 & t \leq -1 \\ t^4 - 2t^2 + 1 & t \leq 1 \\ 0 & \textit{otherwise} \end{matrix} \right.$

We can both differentiate and integrate this expression:

[> diff( pw1, t );

$\left\{ \begin{matrix} 0 & t \leq -1 \\ 4t^3 - 4t & t \leq 1 \\ 0 & 1 < t \end{matrix} \right.$

[> int( pw1, t );

$\left\{ \begin{matrix} 0 & t \leq -1 \\ \frac{1}{5}t^5 - \frac{2}{3}t^3 & t + \frac{8}{15} \leq 1 \\ \frac{16}{15} & 1 < t \end{matrix} \right.$

Now, you'll note that both the derivative and the integral are continuous. This is clear for the integral, as the original piecewise-defined expression was continuous, but it is less obvious as to why the derivative is continuous. Returning back to the original function, the limit of the derivatives at the boundary points of the intervals are the same: zero from both directions.

However, if we were to take the second derivative, we see that the derivative is not defined at the boundary points:

[> diff( pw1, t, t );

$\left\{ \begin{matrix} 0 & t < -1 \\ \textit{undefined} & t = -1, 12t^2 - 4 & t < 1 \\ \textit{undefined} & t = 1 \\ 0 & 1 < t \end{matrix} \right.$

The $\textrm{sinc}$ function can be implemented as a piecewise function:

[> sinc := t -> piecewise( t = 0, 1, sin(t)/t );

$\textrm{sinc} := t \mapsto \left\{ \begin{matrix} 1 & t = 0 \\ \frac{\sin(t)}{t} & \textit{otherwise} \end{matrix} \right.$

[> D(sinc);

$t \mapsto \left\{ \begin{matrix} 0 & t = 0 \\ frac{\cos(t)}{t} - \frac{\sin(t)}{t^2} & \textit{otherwise} \end{matrix} \right.$

A sawtooth function may be implemented as follows:

[> saw := t -> piecewise( t <= -1, 0, t <= 0; 1 + t, t <= 1, 1 - t, 0 );

$saw := t \mapsto \left\{ \begin{matrix} 0 & t \leq 0 \\ 1 + t & t \leq 0 \\ 1 - t & t \leq 1 \\ 0 & \textit{otherwise} \end{matrix} \right.$

[> D(saw);

$t \mapsto \left\{ \begin{matrix} 0 & t < 0 \\ \textit{undefined} & t = -1 \\ 1 & t < 0 \\ \textit{undefined} & t = 0 \\ -1 & t < 1 \\ \textit{undefined} & t = 1 \\ 0 & 1 < t \end{matrix} \right.$

Format

From these examples, you've probably noticed that you can define a piecewise-defined expression using $3$, $5$ or more odd arguments. The first, third, and every subsequent argument, except for the last is a condition. The conditions are evaluated in order, and the result of the piecewise function is the expression following the first condition that is satisfied. If none of the conditions are satisfied, the last expression is the result.

You will note that piecewise does not evaluate any expression other than the one that is satisfied, so you can avoid a division by zero:

[> eval( piecewise( x = 0, 1, sin(x)/x ), x = 0 );

$1$

The expression sin(x)/x would immediately generate a numeric exception if the variable $x$ ever evaluated to zero, but within a piecewise function, it is only evaluated if the corresponding condition is the first that is satisfied.

You will note that the conditions need not be exclusive. For example, the conditions:

should normally be expressed as

because only one condition should be true for any value of $x$, but Maple does not check each, but rather, simply checks them in order, and returns the expression corresponding to the first condition that is satisfied.

You can think of a piecewise function being equivalent to a cascading conditional statement in C++ with at least one condition, and possibly more with subsequent } else if (...) { blocks.

Note

Please note that Maple does not require the above format, and you could put whatever conditions you want in any order you want, but in all honesty, many aspects of Maple are simply not prepared to properly work with a piecewise function except in the format described above.

This author strongly recommends you use piecewise(...) only in the manner described above.