A piecewise-defined function is a function specified by different rules on different parts of its domain. Each rule is accompanied by a condition identifying the inputs to which it applies. The rules together define a single function, not several unrelated functions. Piecewise definitions are useful when a relationship changes at a threshold, when a formula depends on the sign of an input, or when a complicated function is represented by simpler expressions on smaller regions.
Definition and notation
Let be a set and let be a partition of : the sets are nonempty, pairwise disjoint, and their union is . If each takes values in the same codomain , then a function can be defined by
For a finite number of pieces, the usual notation is a brace followed by a list of expressions and conditions:
More generally, the conditions may overlap, provided that every applicable rule assigns the same value on an overlap. Every point in the stated domain must be covered, and no point may receive conflicting values. Otherwise, the specification does not define a function on that domain.
The pieces need not be intervals, and the inputs need not be real numbers. They may be regions of a multidimensional space or subsets described by logical conditions. In elementary mathematics, however, the most common examples have a real variable and finitely many interval pieces.
A piecewise definition is a way of describing a function, not an intrinsic distinction between separate classes of functions. The same function may have both a piecewise expression and a single-formula expression.
Examples and endpoint conventions
The absolute value function has the definition
The two expressions agree at zero, so assigning zero to either branch would give the same function.
By contrast, endpoint assignments matter when adjacent rules give different values. For example,
has . Replacing the conditions with and would define a different function, although the values away from zero would be unchanged.
On a graph, an included endpoint is conventionally marked with a filled dot and an excluded endpoint with an open circle. A graph should not contain a connecting line across a jump unless that line itself belongs to the function.
Piecewise definitions can also have infinitely many pieces. For example, a function may be defined separately on every interval , where ranges over the integers. Such definitions require particular care if the boundaries of the pieces accumulate near a point.
Continuity at boundaries
A piecewise formula does not imply discontinuity. A function may be continuous everywhere even though its defining expression changes.
For an interior boundary point of a real interval, continuity requires
These are one-sided limits, calculated using the rules valid immediately to the left and right of . At an endpoint of the domain, continuity is understood relative to the domain and may require only a one-sided limit.
For example, consider
The left-hand limit at is , while the right-hand limit and the assigned value are . Thus is continuous at exactly when
If both one-sided limits exist and are finite but unequal, the boundary is a jump discontinuity. If they agree but differ from the assigned value, the discontinuity can be removed by changing that value.
Continuity on the interior of each piece does not by itself establish continuity at the boundaries. For infinitely many pieces, checking individual junctions may also be insufficient: accumulation points of junctions require separate analysis.
Differentiation
Inside a piece, the derivative is calculated from the rule valid there. At a boundary, differentiability must be checked using the function’s actual value and the difference quotient.
For an interior point , the relevant one-sided derivatives are
The derivative exists at if both limits exist as finite numbers and are equal. Differentiability implies continuity, so a jump cannot have an ordinary derivative.
For the preceding example, continuity gives . The left-hand derivative at is , and the right-hand derivative is . Therefore, differentiability at the junction requires
The absolute value function illustrates a continuous function that is not differentiable at a junction: its one-sided derivatives at zero are and .
Higher smoothness requires further matching conditions. A function can have a continuous first derivative while its second derivative fails to exist at a boundary.
Integration
For a finite subdivision
the integral can be evaluated by adding the contributions from the pieces:
This identity applies when the relevant integrals exist. In particular, a bounded function that is continuous except at finitely many subdivision points is Riemann integrable on a closed bounded interval. Changing its values at finitely many points does not change that integral.
When constructing an antiderivative from separate formulas, the constants of integration must be chosen consistently. For example, if is continuous across a junction, a global antiderivative must join continuously there; its constants cannot be selected independently without regard to the neighboring pieces.
For unbounded functions, infinitely many pieces, or singularities at boundaries, convergence must be checked rather than inferred from the piecewise notation alone.
Important forms
Piecewise constant functions are constant on each piece. An indicator function is a basic example: it equals on a specified set and outside it. In one variable, step functions commonly use interval pieces.
Piecewise affine functions use formulas of the form . They are often called piecewise linear, especially in numerical and applied work, although an affine map with a nonzero constant term is not linear in the strict linear-algebraic sense.
Piecewise polynomial functions use a polynomial on each piece. A spline is a piecewise polynomial function with specified matching conditions at the junctions, called knots. These conditions control continuity and differentiability.
Terms such as piecewise continuous and piecewise smooth describe regularity on pieces rather than merely the use of cases notation. Their precise conventions vary, particularly concerning the number of pieces and the existence of finite one-sided limits.
Applications and computational considerations
In interpolation and numerical analysis, piecewise formulas allow local approximations without requiring one high-degree polynomial over an entire interval. The finite element method uses functions defined locally on elements of a mesh, with compatibility conditions selected for the problem.
In probability, a probability density function is often given by one expression on its support and zero outside it. Piecewise formulas also describe cumulative distribution functions with different behavior over different ranges.
Threshold-based models appear in pricing, taxation, control systems, and optimization. A progressive tax schedule, for example, can produce a continuous piecewise affine total-tax function even though the marginal rate changes at income thresholds.
In software, piecewise functions are implemented with conditional branches or array masks. Mathematical cases should specify a unique value independently of evaluation order; programming languages may instead resolve overlapping conditions by choosing the first matching branch. Numerical implementations must also handle boundary comparisons and avoid evaluating expressions outside the regions where they are valid.
In automatic differentiation, differentiating the selected branch does not establish that the original function is differentiable at a switching point. A program may return a branch-dependent value at a corner where no ordinary derivative exists.