Piecewise

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

  • 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
    v1c1
    v2c2
    . The piecewise operator can be entered as 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
    v1c1
    v2c2
    .

Examples

open allclose all

Basic Examples  (3)

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

In[1]:=
Click for copyable input
Out[1]=

Find the derivative of a piecewise function:

In[1]:=
Click for copyable input
Out[1]=

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

In[1]:=
Click for copyable input

Scope  (12)

Applications  (1)

Properties & Relations  (11)

Possible Issues  (1)

See Also

PiecewiseExpand  $MaxPiecewiseCases  Boole  Max  Min  RealAbs  Abs  RealSign  Sign  Clip  Ramp  UnitStep  UnitBox  UnitTriangle  Floor  Ceiling  Round  IntegerPart  Mod  SquareWave  TriangleWave  BezierFunction  BSplineFunction  Which  If

Tutorials

Introduced in 2004
(5.1)