Piecewise



In mathematics, a piecewise-defined function (also called a piecewise function, a hybrid function, or definition by cases) is a function whose domain is partitioned into several intervals ("subdomains") on which the function may be defined differently. Piecewise definition is actually a way of specifying the function, rather than a characteristic of the resulting function itself.

A function property holds piecewise for a function, if the function can be piecewise defined in a way that the property holds for every subdomain. Examples of functions with such piecewise properties are piecewise constant functions, piecewise linear functions (see the figure), piecewise continuous functions, and piecewise differentiable functions.

Notation and interpretation
Piecewise functions can be defined using the common functional notation, where the body of the function is an array of functions and associated subdomains. A semicolon or comma may follow the subfunction or subdomain columns. The $$\text{if}$$ or $$\text{for}$$ usually appears, and belongs at the start of the right column.

The subdomains together must cover the whole domain; often it is also required that they are pairwise disjoint, i.e. form a partition of the domain. In order for the overall function to be called "piecewise", the subdomains are usually required to be intervals (some may be degenerated intervals, i.e. single points or unbounded intervals). For bounded intervals, the number of subdomains is required to be finite, for unbounded intervals it is often only required to be locally finite. For example, consider the piecewise definition of the absolute value function:
 * $$|x| = \begin{cases}

-x, & \text{if } x < 0 \\ +x, & \text{if } x \ge 0. \end{cases} $$ For all values of $$x$$ less than zero, the first sub-function ($$-x$$) is used, which negates the sign of the input value, making negative numbers positive. For all values of $$x$$ greater than or equal to zero, the second sub-function ($x$) is used, which evaluates trivially to the input value itself.

The following table documents the absolute value function at certain values of $$x$$:

In order to evaluate a piecewise-defined function at a given input value, the appropriate subdomain needs to be chosen in order to select the correct sub-function—and produce the correct output value.

Examples
\exp\left( -\frac{1}{1 - x^2}\right), & x \in (-1,1) \\ 0,                                   & \text{otherwise} \end{cases}$$ and some other common Bump functions. These are infinitely differentiable, but analyticity holds only piecewise.
 * Piecewise linear function, a function composed of line segments
 * Step function, a function composed of constant sub-functions
 * Boxcar function,
 * Heaviside step function
 * Sign function
 * Absolute value
 * Triangular function
 * Broken power law, a function composed of power-law sub-functions
 * Spline, a function composed of polynomial sub-functions, possessing a high degree of smoothness at the places where the polynomial pieces connect
 * B-spline
 * PDIFF
 * $$f(x)= \begin{cases}

Continuity and differentiability of piecewise-defined functions
A piecewise-defined function is continuous on a given interval in its domain if the following conditions are met:
 * its sub-functions are continuous on the corresponding intervals (subdomains),
 * there is no discontinuity at an endpoint of any subdomain within that interval.

The pictured function, for example, is piecewise-continuous throughout its subdomains, but is not continuous on the entire domain, as it contains a jump discontinuity at $$x_0$$. The filled circle indicates that the value of the right sub-function is used in this position.

For a piecewise-defined function to be differentiable on a given interval in its domain, the following conditions have to fulfilled in addition to those for continuity above:
 * its sub-functions are differentiable on the corresponding open intervals,
 * the one-sided derivatives exist at all intervals' endpoints,
 * at the points where two subintervals touch, the corresponding one-sided derivatives of the two neighboring subintervals coincide.

Applications
In applied mathematical analysis, "piecewise-regular" functions have been found to be consistent with many models of the human visual system, where images are perceived at a first stage as consisting of smooth regions separated by edges. In particular, shearlets have been used as a representation system to provide sparse approximations of this model class in 2D and 3D.

Piecewise defined functions are also commonly used for interpolation, such as in nearest-neighbor interpolation.