Piecewise Functions

Turning conditions into numbers.

(RefLink[Boole, paclet:ref/Boole])[expr] is a basic function that turns (RefLink[True, paclet:ref/True]) and (RefLink[False, paclet:ref/False]) into 1 and 0. It is sometimes known as the characteristic function or indicator function.

This gives the area of a unit disk:

Integrate[Boole[x^2+y^2<=1],{x,-1,1},{y,-1,1}]

Piecewise functions.

It is often convenient to have functions with different forms in different regions. You can do this using (RefLink[Piecewise, paclet:ref/Piecewise]).

This plots a piecewise function:

Plot[Piecewise[{{x^2,x<0},{1-x,x>0}}],{x,-1,1}]

Piecewise functions appear in systems where there is discrete switching between different domains. They are also at the core of many computational methods, including splines and finite elements. Special cases include such functions as (RefLink[Abs, paclet:ref/Abs]), (RefLink[UnitStep, paclet:ref/UnitStep]), (RefLink[Clip, paclet:ref/Clip]), (RefLink[Sign, paclet:ref/Sign]), (RefLink[Floor, paclet:ref/Floor]), and (RefLink[Max, paclet:ref/Max]). The Wolfram Language handles piecewise functions in both symbolic and numerical situations.

This generates a square wave:

Plot[UnitStep[Sin[x]], {x, 0, 30}]

Here is the integral of the square wave:

Integrate[UnitStep[Sin[x]], {x, 0, 30}]