MATHEMATICA TUTORIAL

# Logical and Piecewise Functions

Nested logical and piecewise functions can be expanded out much like nested arithmetic functions. You can do this using LogicalExpand and PiecewiseExpand.

 LogicalExpand[expr] expand out logical functions in expr PiecewiseExpand[expr] expand out piecewise functions in expr PiecewiseExpand[expr,assum] expand out with the specified assumptions

Expanding out logical and piecewise functions.

LogicalExpand puts logical expressions into a standard disjunctive normal form (DNF), consisting of an OR of ANDs.

By default, Mathematica leaves this expression unchanged.
 Out[1]=
LogicalExpand expands this into an OR of ANDs.
 Out[2]=

LogicalExpand works on all logical functions, always converting them into a standard OR of ANDs form. Sometimes the results are inevitably quite large.

Xor can be expressed as an OR of ANDs.
 Out[3]=

Any collection of nested conditionals can always in effect be flattened into a piecewise normal form consisting of a single Piecewise object. You can do this in Mathematica using PiecewiseExpand.

By default, Mathematica leaves this expression unchanged.
 Out[4]=
PiecewiseExpand flattens it into a single Piecewise object.
 Out[5]=

Functions like Max and Abs, as well as Clip and UnitStep, implicitly involve conditionals, and combinations of them can again be reduced to a single Piecewise object using PiecewiseExpand.

This gives a result as a single Piecewise object.
 Out[6]=
With assumed real, this can also be written as a Piecewise object.
 Out[7]=

Functions like Floor, Mod, and FractionalPart can also be expressed in terms of Piecewise objects, though in principle they can involve an infinite number of cases.

Without a bound on , this would yield an infinite number of cases.
 Out[8]=

Mathematica by default limits the number of cases that Mathematica will explicitly generate in the expansion of any single piecewise function such as Floor at any stage in a computation. You can change this limit by resetting the value of \$MaxPiecewiseCases.

## Tutorial CollectionTutorial Collection

New to Mathematica? Find your learning path »
Have a question? Ask support »