Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 10 down vote accepted

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.

share|improve this answer
    
...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). –  payne Nov 7 '13 at 19:29
    
@payne Despite that Piecewise does evaluate its arguments. –  Szabolcs Nov 7 '13 at 19:40
    
Yes, I noticed that too. :) The test: Piecewise[{{True, Print[here]}, {False, Print[there]}}] If[True, Print[here], Print[there]] –  payne Nov 7 '13 at 19:44
    
@payne Or just your example with bar ... which gets evaluated. –  Szabolcs Nov 7 '13 at 19:59

Try this:

bar[a_] := a
If[a > b, Evaluate[bar[a]], Evaluate[bar[b]]]
share|improve this answer
    
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. –  payne Nov 7 '13 at 19:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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