In Mathematica, I am surprised that the following code does not do what I expect (what I expect is in comments after each evaluation). The problem seems to be due to the fact that when I define test with setDelayed (as I believe it should be?) but call it with k, k is then seen as a symbol and is always considered different than 0, irrespective of the replacement value I use thereafter. Is there a way to define this equality test in such a way that it will use the replacement value for k?
ClearAll[test];
test[k_, x_] := If[k === 0, x, x^2];
test[1, 4] (* evaluates to 16 as expected *)
test[0, 4] (* evaluates to 4 as expected *)
test[k, x] /. {k -> 1, x -> 4} (* evaluates to 16 as expected *)
test[k, x] /. {k -> 0, x -> 4} (* evaluates to 16 instead of 4 *)
*** EDIT 1 ***
Following the answer by @lericr, here is some more context: I have functions which I have taken the habit of implementing using the SetDelayed notation, as that seems to be the way these are implemented in Mathematica. These functions have a fraction which sometimes has a zero denominator, and sometimes a zero numerator as well, yielding Indeterminate results. However, I know how to manage those situations since when that happens other elements in parent functions treat those cases differently. Therefore, I have implemented these functions with an If clause to capture cases when I might have infinity or undetermined and manage apropriately.
That said, these functions take about 8 arguments, and 3 of those must be used in the conditional If clause, so perhaps the solutions proposed do not adapt well to that situation (I have difficulty understanding why the currently proposed solutions work).
Among the other things I tried as well was using /.{Indeterminate->0,ComplexInfinity->0}
for example, but I have abandoned this way of doing things for a reason the specifics of which I forget but it did not seem to do the job in my case. I hope this clarifies a bit: I always find the position of someone not knowing an answer but having to provide a minimal working example for a question a bit difficult, as a good MWE often requires knowing the answer :)
*** EDIT 2 *** This edit corresponds to the latest updates/comments for the answer by @lericr. My full code is an accumulation of beginner-style Mathematica which is too convoluted to post and would likely be cofusing. But the reason why I use replacement can maybe be illustrated (though by no means justified, as I might be using it for the wrong reasons) by the following code.
Clear[subFuncA, subFuncB, mainFunc, otherFunc]
subFuncA[j_, k_, x_] := If[j === 0 && k === 0, x, x^2];
subFuncB[j_, k_, x_] := If[j === 0 || k === 0, 7 + x, 11 + x];
mainFunc[j_, k_, x_] := subFuncA[j, k, x] + subFuncB[j, k, x];
otherFunc[j_, k_, x_] := j + k - x;
Do[
Do[
params = {j -> jj, k -> kk, x -> 4};
Print[otherFunc[j, k, x] /. params];
Print[Unevaluated[mainFunc[j, k, x]] /. params];
, {kk, 0, 1}]
, {jj, 0, 1}]
In the above code, the two essential characteristics of what I am trying to achieve are visble: first, I have a set of parameters which I define once in order to reuse in multiple functions (here mainFunc and otherFunc); second, mainFunc calls two subfunctions which are the ones doing the conditional testing. So this is the reason why I used conditionals and replacement, though perhaps a With statement would have been preferable, I am not too sure...
I have tried all three methods available so far in the answer by @lericr: two are in the post, and one is in a comment by @att, and all three proposed solutions work! So I am happy with that answer. I have to say that the third solution, using Unevaluated, seems like perhaps (?) the most appropriate since from what I gather the other two are hacky? More importantly for me, I sort of understand why Unevaluated would work, whereas using _Integer or Equal still does not make much sense to me.
If
intest
seems to be handling the indeterminates already, so I don't understand what problem remains. I want a minimal example that demonstrates the problem. It "works" in the sense of reproducing the problem. $\endgroup$