Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

7
  • $\begingroup$ Can we understand all the differences between Piecewise and Which, just from the fact that one evaluates its arguments and the other doesn't? $\endgroup$
    – a06e
    Commented Nov 27, 2017 at 10:00
  • 1
    $\begingroup$ @becko I didn't attempt to meticulously discover all specific differences. But the intended use is clear. So if you were instead asking: "which should I use?", I would suggest deciding based on what I described above. $\endgroup$
    – Szabolcs
    Commented Nov 27, 2017 at 10:08
  • 3
    $\begingroup$ @becko Note that it isn't even sufficient to look at how each evaluates. You'd also need to look at how other functions (like D above) handle them. That makes listing all differences an impossibly difficult task. Does Simplify, Refine, etc. handle Which? I'm sure there will be some functions which only handle Piecewise and not Which. You'd have to go through all symbolic functions ... $\endgroup$
    – Szabolcs
    Commented Nov 27, 2017 at 10:10
  • 2
    $\begingroup$ I see the difficulty. This is the thing I dislike the most about Mathematica. You cannot summarize its syntax in a few simple rules. Other languages prioritize simplicity, so that understanding the rules and predicting execution behavior is relatively easy. But Mathematica instead prioritizes having these all powerful functions, which are very handy most of the time, but when you try to understand how they work, or when something does not execute like you thought it would, it's a pain. $\endgroup$
    – a06e
    Commented Nov 27, 2017 at 11:16
  • 3
    $\begingroup$ @becko: Mathematica is not a programming language in the usual sense. It is an expression rewriting system. Some rewrite rules behave like programming constructs, so using If is like programming. However, in symbolic analysis, If is too undisciplined to be deconstructed abstractly. Piecewise has the necessary discipline for symbolic analysis, so the rewrite rules behind things like Integrate can manage to rewrite it without evaluating it. If needs to be evaluated before symbolic rules can safely rewrite it. $\endgroup$
    – John Doty
    Commented May 20, 2018 at 16:25