As indicated by its name, PiecewiseExpand
's most natural application is to Piecewise
expressions but nonetheless, it is still documented as being applicable to (unevaluated) If
, Which
and Switch
expressions. Has anyone ever had any experience of usefully applying PiecewiseExpand
to these procedural-like conditionals?
Applications like
Clear@f;
f[x_] := Which[
EvenQ@x, "even",
LessThan[10]@x, "lessThan10",
GreaterThan[20]@x, "greaterThan20"];
PiecewiseExpand[f[x], Assumptions -> x \[Element] Integers]
don't expand as expected in more general programming contexts whereas PiecewiseExpand
's simplifying engine seems more apt in mathematical settings in which case Piecewise
then seems more natural.
Clear@f;
f[x_] := Piecewise[{
{"even", Mod[x, 2] == 0},
{"lessThan10", x < 10},
{"greaterThan20", x > 20}}];
PiecewiseExpand[f[x], Assumptions -> x \[Element] Integers]
PiecewiseExpand
being effectively applied toIf
,Which
orSwitch
. Could you perhaps provide a link or two? $\endgroup$PiecewiseExpand
andIf
(and in many casesSimplify`PWToUnitStep
) quite a bit, because for simple binary decision I found e.g.PiecewiseExpand@If[x > 2, 1, 3]
is easier to code and cleaner thanPiecewise[{{1, x > 2}}, 3]
. For more info just check the link above. $\endgroup$If
,Which
,Switch
? Then I think it's better to clarify a bit in the body of question. $\endgroup$Piecewise
vs. control-flow/proceduralIf
etc. is the robustness @xzczd refers to (in algebraic/numerical solvers) was more apparent in earlier versions. Over time,PiecewiseExpand
has been added under the hood to the solvers because (I surmise) users did appreciate the difference betweenPiecewise
andIf
. I don't recall seeing someone usePiecewise
to select, say, a method subroutine depending option values. The confusion seems to be one-way. $\endgroup$