Mathematica 9 is now available
Previous section-----Next section

3.3.9 Advanced Topic: 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.

In[1]:=  (a || b) && c

Out[1]=

LogicalExpand expands this into an OR of ANDs.

In[2]:=  LogicalExpand[%]

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.

In[3]:=  LogicalExpand[Xor[a, b, c]]

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.

In[4]:=  If[x > 0, If[x < 1, a, b], c]

Out[4]=

PiecewiseExpand flattens it into a single Piecewise object.

In[5]:=  PiecewiseExpand[%]

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.

In[6]:=  PiecewiseExpand[Max[Min[a, b], c]]

Out[6]=

With x assumed real, this can also be written as a Piecewise object.

In[7]:=  PiecewiseExpand[Abs[x], x \[Element] Reals]

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 x, this would yield an infinite number of cases.

In[8]:=  PiecewiseExpand[Floor[x^2], 0 < x < 2]

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.



Any questions about topics on this page? Click here to get an individual response.Buy NowMore Information
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.