Piecewise
Details
- The condi are typically inequalities such as
. - The condi are evaluated in turn, until one of them is found to yield True.
- If all preceding condi yield False, then the vali corresponding to the first condi that yields True is returned as the value of the piecewise function.
- If any of the preceding condi do not literally yield False, the Piecewise function is returned in symbolic form.
- Only those vali explicitly included in the returned form are evaluated.
- Elements of the form {vali,False} are dropped, as are all elements after the first {vali,True}.
- Piecewise[conds] automatically evaluates to Piecewise[conds,0].
- Piecewise can be used in such functions as Integrate, Minimize, Reduce, DSolve, and Simplify, as well as their numeric analogs.
- Piecewise[{{v1,c1},{v2,c2},…}] can be input in the form
. The piecewise operator can be entered asv1 c1 v2 c2 …
pw
or \[Piecewise]. The grid of values and conditions can be constructed by first entering
, then using
and
. - In StandardForm and TraditionalForm, Piecewise[{{v1,c1},{v2,c2},…}] is normally output using a brace, as in
.v1 c1 v2 c2 …
Examples
open all close allBasic Examples (3)
Set up a piecewise function with different pieces below and above zero:
Plot[Piecewise[{{x ^ 2, x < 0}, {x, x > 0}}], {x, -2, 2}]Find the derivative of a piecewise function:
D[Piecewise[{{x ^ 2, x < 0}, {x, x > 0}}], x]Use
pw
to enter and
and then
for each additional piecewise case:
| | |
| -- | -- |
| v1 | d1 |
| v2 | d2 |
| v3 | d3 |//InputFormScope (12)
pw = Piecewise[{{Sin[x] / x, x < 0}, {1, x == 0}}, -x ^ 2 / 100 + 1]Evaluate it at specific points:
pw /. {{x -> -5}, {x -> 0}, {x -> 5}}Plot[pw, {x, -10, 10}]Refine[pw, x < -2]Automatic simplification of Piecewise functions:
Piecewise[{{e1, True}, {e2, d2}, {e3, d3}}, e]Piecewise[{{e1, d1}, {e2, d2}, {e3, True}, {e4, d4}, {e5, d5}}, e]Piecewise[{{e1, d1}, {e2, d2}, {e3, d2 && d3}, {e4, d4}}, e]Remove False conditions:
Piecewise[{{e1, d1}, {e2, d2}, {e3, False}, {e4, d4}, {e5, d5}}, e]Merge cases with the same values:
Piecewise[{{e1, d1}, {e23, d2}, {e23, d3}, {e4, d4}}, e]If values are not specified in a region, they are assumed to be zero:
Plot[Piecewise[{{Sqrt[x], x > 0}}], {x, -2, 2}]This specifies that the default value should be 1:
Plot[Piecewise[{{Sqrt[x], x > 0}}, 1], {x, -2, 2}]Compute limits of piecewise functions:
pw = Piecewise[{{1, Abs[z] ≤ 1}}];Limit[pw, z -> I]Compute the limit in the direction of the positive imaginary axis:
Limit[pw, z -> I, Direction -> I]Compute the series of a piecewise function:
Series[Piecewise[{{Cos[x], x ≤ 0}}, E ^ x], {x, a, 2}]Integrate a piecewise function:
int = Integrate[Piecewise[{{E ^ x, x ^ 2 ≤ 1}}, Sin[x]], x]Integration constants are chosen to make the result continuous:
Plot[int, {x, -2, 2}]Compute a definite integral of a piecewise function:
Integrate[Piecewise[{{1, x ^ 2 + y ^ 2 < 1}}], {x, -∞, ∞}, {y, -∞, ∞}]Laplace transform of a piecewise function:
LaplaceTransform[Piecewise[{{Sqrt[t], t > 0}}], t, s]Solve a piecewise differential equation:
DSolve[x''[t] + x[t] == Piecewise[{{Sqrt[t], t > 0}}], x[t], t]Reduce[x ^ 2 + Piecewise[{{x, 0 < x < 1}, {x ^ 3, 1 < x < 2}}] == 1, x]Plot[x ^ 2 + Piecewise[{{x, 0 < x < 1}, {x ^ 3, 1 < x < 2}}], {x, -3, 3}]Integrating an implicitly piecewise integrand can give an explicit Piecewise result:
Integrate[Max[x, x ^ 2, x ^ 3], x]Symbolic minimization can give piecewise functions:
Minimize[a x ^ 2 + b, x]Applications (1)
Properties & Relations (11)
PiecewiseExpand converts nested piecewise functions into a single piecewise function:
pw = Piecewise[{{Piecewise[{{1, x ≥ 0}}, 2], Piecewise[{{x, x ≤ 1}}, x / 2] ^ 2 ≥ 1 / 2}}, 3]PiecewiseExpand[pw]Min, Max, UnitStep, and Clip are piecewise functions of real arguments:
PiecewiseExpand /@ {Min[x, y], Max[x, y, z], UnitStep[x], Clip[x, {a, b}]}Abs, Sign, and Arg are piecewise functions when their arguments are assumed to be real:
Assuming[Element[x, Reals], PiecewiseExpand /@ {Abs[x], Sign[x], Arg[x]}]KroneckerDelta and DiscreteDelta are piecewise functions of complex arguments:
PiecewiseExpand /@ {KroneckerDelta[x, y], DiscreteDelta[x, y]}Boole is a piecewise function of a Boolean argument:
PiecewiseExpand[Boole[a]]If, Which, and Switch can be interpreted as piecewise functions:
PiecewiseExpand /@ {If[a, x, y], Which[a, x, b, y, c, z], Switch[u, v, x, w, y]}Convert Floor, Ceiling, Round, IntegerPart, and FractionalPart for finite ranges:
Assuming[{0 ≤ x ≤ 3}, PiecewiseExpand /@ {Floor[x], Ceiling[x], Round[x], IntegerPart[x], FractionalPart[x]}]Convert Mod and Quotient when the number of cases is finite:
Assuming[0 ≤ k ≤ 3m, PiecewiseExpand /@ {Mod[k, m], Quotient[k, m]}]UnitBox and UnitTriangle are piecewise functions of real arguments:
PiecewiseExpand /@ {UnitBox[x], UnitTriangle[x]}Convert SquareWave, TriangleWave, and SawtoothWave for finite ranges:
Assuming[{0 ≤ x ≤ 3}, PiecewiseExpand /@ {SquareWave[x], TriangleWave[x], SawtoothWave[x]}]BernsteinBasis and BSplineBasis are piecewise functions of real arguments:
PiecewiseExpand /@ {BernsteinBasis[3, 2, x], BSplineBasis[3, 2, x]}Possible Issues (1)
Derivatives are computed piece-by-piece, unless the function is univariate in a real variable:
D[Piecewise[{{(x ^ 2 - 1) / (x - 1), x ≠ 1}}, 2], x]To specify that
is real, use inequalities in the first condition:
D[Piecewise[{{(x ^ 2 - 1) / (x - 1), x < 1 || x > 1}}, 2], x]This function is discontinuous at
:
D[Piecewise[{{(x ^ 2 - 1) / (x - 1), x < 1 || x > 1}}, 1], x]Tech Notes
Related Guides
Related Links
History
Introduced in 2004 (5.1) | Updated in 2008 (7.0)
Text
Wolfram Research (2004), Piecewise, Wolfram Language function, https://reference.wolfram.com/language/ref/Piecewise.html (updated 2008).
CMS
Wolfram Language. 2004. "Piecewise." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2008. https://reference.wolfram.com/language/ref/Piecewise.html.
APA
Wolfram Language. (2004). Piecewise. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Piecewise.html
BibTeX
@misc{reference.wolfram_2026_piecewise, author="Wolfram Research", title="{Piecewise}", year="2008", howpublished="\url{https://reference.wolfram.com/language/ref/Piecewise.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_piecewise, organization={Wolfram Research}, title={Piecewise}, year={2008}, url={https://reference.wolfram.com/language/ref/Piecewise.html}, note=[Accessed: 13-June-2026]}