Here is an example of my issue:
condition=m>0;
f[m_] = Which[condition == True, 1, condition == False, 2]
f[-10]
returns
Which[(m > 0) == False, 2]
Why? I do not understand. What is even more odd is that flipping order inside "which", reverses where the problem occurs.
condition = m > 0;
f[m_] = Which[condition == False, 2, condition == True, 1];
f[-10]
returns
2
but the function now fails for say $f[10]$.
I know that I can fix the issue just by saying $m>0$ and $m<0$ but the conditions in my real code are much more complicated so I cannot do that. I must be making some syntax error. Thank you.
==
for writing equations or testing numerical equality. See also===
. Second, don't usecondition === True
andcondition === False
. Usecondition
andNot[condition]
. $\endgroup$f[m_] := 2 - Boole[m > 0]
$\endgroup$Which
has the attributeHoldAll
, it's best not to have the def. off
depend on external values. E.g.f[m_] := With[{condition = m > 0}, Which[condition, 1, ! condition, 2]]
$\endgroup$condition === False
before, and it can make sense if you cannot be surecondition
has evaluated toTrue
orFalse
. $\endgroup$