Function defined in pieces for different conditions—Wolfram Documentation (original) (raw)

Piecewise[{{val1,cond1},{val2,cond2},…}]

represents a piecewise function with values vali in the regions defined by the conditions condi.

Piecewise[{{val1,cond1},…},val]

uses default value val if none of the condi apply. The default for val is 0.

Details

Examples

open all close all

Basic Examples (3)

Set up a piecewise function with different pieces below and above zero:

Find the derivative of a piecewise function:

Use pw to enter  and and then TemplateBox[{ctrl, return}, Key1, BaseStyle -> {ExampleText, FontWeight -> Plain, FontFamily -> Source Sans Pro}] for each additional piecewise case:

Scope (12)

Define a piecewise function:

Evaluate it at specific points:

Plot it:

Refine it under assumptions:

Automatic simplification of Piecewise functions:

Remove unreachable cases:

Remove False conditions:

Merge cases with the same values:

If values are not specified in a region, they are assumed to be zero:

This specifies that the default value should be 1:

Compute limits of piecewise functions:

Compute the limit in the direction of the positive imaginary axis:

Compute the series of a piecewise function:

Integrate a piecewise function:

Integration constants are chosen to make the result continuous:

Compute a definite integral of a piecewise function:

Laplace transform of a piecewise function:

Solve a piecewise differential equation:

Reduce a piecewise equation:

Integrating an implicitly piecewise integrand can give an explicit Piecewise result:

Symbolic minimization can give piecewise functions:

Applications (1)

Compute the volume of an ellipsoid:

Properties & Relations (11)

PiecewiseExpand converts nested piecewise functions into a single piecewise function:

Min, Max, UnitStep, and Clip are piecewise functions of real arguments:

Abs, Sign, and Arg are piecewise functions when their arguments are assumed to be real:

KroneckerDelta and DiscreteDelta are piecewise functions of complex arguments:

Boole is a piecewise function of a Boolean argument:

If, Which, and Switch can be interpreted as piecewise functions:

Convert Floor, Ceiling, Round, IntegerPart, and FractionalPart for finite ranges:

Convert Mod and Quotient when the number of cases is finite:

UnitBox and UnitTriangle are piecewise functions of real arguments:

Convert SquareWave, TriangleWave, and SawtoothWave for finite ranges:

BernsteinBasis and BSplineBasis are piecewise functions of real arguments:

Possible Issues (1)

Derivatives are computed piece-by-piece, unless the function is univariate in a real variable:

To specify that is real, use inequalities in the first condition:

This function is discontinuous at :

Wolfram Research (2004), Piecewise, Wolfram Language function, https://reference.wolfram.com/language/ref/Piecewise.html (updated 2008).

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_2025_piecewise, author="Wolfram Research", title="{Piecewise}", year="2008", howpublished="\url{https://reference.wolfram.com/language/ref/Piecewise.html}", note=[Accessed: 22-October-2025]}

BibLaTeX

@online{reference.wolfram_2025_piecewise, organization={Wolfram Research}, title={Piecewise}, year={2008}, url={https://reference.wolfram.com/language/ref/Piecewise.html}, note=[Accessed: 22-October-2025]}