How can I get Mathematica to simplify the true and false clauses for If[] statements?

For example:

bar[a_] := a
FullSimplify[If[a > b, bar[a], bar[b]]]

If[a > b, bar[a], bar[b]]

I was hoping for at least If[a>b, a, b]. I thought that it might be hesitating to simplify bar[] because of possible side effects in the function, but I noticed this:

FullSimplify[If[a + a + a > b + b + b, a + a + a, b + b + b]]

If[a > b, a + a + a, b + b + b]

Note that the conditional clause was simplified, but nothing else was. But if the form is slightly different, it will simplify:

FullSimplify[If[a > b, a - a + a, b - b + b]]

{a a > b
{b True

How can I get simplified expressions within If statements? I feel like I'm missing something very basic.


2 Answers 2


If is primarily a programming function. It tells the computer what to do. It's not the best idea to use it to represent a mathematical function (as opposed to an algorithm).

To represent mathematical functions, use Piecewise,

Piecewise[{{bar[a], a > b}, {bar[b], True}}]

Mathematica tries to unify various uses to be easier to use for users, but it's good to be aware that numerical, symbolical, or "algorithmic" functions are really different use cases (another example would be UnitStep vs HeavisideTheta vs If[something>0, ...]. UnitStep is excellent for fast numerical calculations. HeavisideTheta is knows that it should be undefined for 0. And If is usable for (conditional) evaluation control while programming, i.e. one can use it to build programs/algorithms.

  • $\begingroup$ ...and to add key detail revealed by @JasonB's answer: the If[] method has a 'HoldRest' attribute (2nd and 3rd arguments held unevaluated), and Piecewise[] has 'HoldAll' (all arguments held unevaluated). For my application, I can use Evaluate[] where needed to force things, which seems to let the simplifier get at the arguments (for both If and Piecewise). $\endgroup$
    – payne
    Commented Nov 7, 2013 at 19:29
  • $\begingroup$ @payne Despite that Piecewise does evaluate its arguments. $\endgroup$
    – Szabolcs
    Commented Nov 7, 2013 at 19:40
  • $\begingroup$ Yes, I noticed that too. :) The test: Piecewise[{{True, Print[here]}, {False, Print[there]}}] If[True, Print[here], Print[there]] $\endgroup$
    – payne
    Commented Nov 7, 2013 at 19:44
  • $\begingroup$ @payne Or just your example with bar ... which gets evaluated. $\endgroup$
    – Szabolcs
    Commented Nov 7, 2013 at 19:59

Try this:

bar[a_] := a
If[a > b, Evaluate[bar[a]], Evaluate[bar[b]]]
  • $\begingroup$ This gives the effect I was looking for. And your answer provided the hint needed to figure out WHY: the If[] function has a 'HoldRest' attribute, which causes Mathematica to hold the 2nd and 3rd arguments unevaluated. $\endgroup$
    – payne
    Commented Nov 7, 2013 at 19:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.