Experts;
Given
f = (#1^#2) &
Is there a way to define 'f' above such that if #1 and #2 are both zero, then the value of the pure function 'f' should be 1 ?
so that when I write
f[0,0]
it will return 1 and not Indeterminate?
btw, I know I can write
f = (If[#1 == 0 && #2 == 0, 1, #1^#2]) &
But I wanted a general rule or pattern, so I do not have to write these checks, as the pure function can be more complicated (many # in it ) and I do not want to do many of these 'if then else' checks for each possible 0^0 that might show up.
thanks
Update:
May be I should clarify more why I am doing this.
I have a user selects a function from a menu. The function is
a x^n0 + b y^n1 + c x^n2 y^n3
Where in the above, the parameters 'n0', 'n1', 'n2' and 'n3' also can be selected from sliders, and these can be zero.
Now, 'x' and 'y' are coordinates, and these can be zero also.
Therefore, it is possible that 0^0 can be encountered when evaluating the above function.
There are many cases to check for, when doing it myself. For example 'y^n3' can be 0^0 and not the other, y^n1 can be 0^0 and not the other, x^n2 y^n3 can be both 0^0 and not the others, etc.., and so I have to define many different cases. (16 possible cases I think).
And I am trying to avoid this. If I can tell Mathematica to replace 0^0 by 1 at a lower level, then life will be simpler.
Update 12/7/11 Thanks for everyone's answers and comments, all are very useful and solve my problem and I learned from them.
I selected Leonid answer as it allowed me to solve my problem with least amount of additional coding.
Here is an small example
Manipulate[Row[{format[x, n], "=", eval[x, n]}],
{{x, 0.0, "x="}, 0, 1, .1, Appearance -> "Labeled"},
{{n, 0.0, "n="}, 0, 1, .1, Appearance -> "Labeled"},
Initialization :>
(
format[x_, n_] := HoldForm["(" x ")"^n];
eval = Unevaluated[#1^#2] /. HoldPattern[0.0^0.0] :> 0.0 &
)
]
I use real numbers everywhere in my code (it is a numerical pde solver), so that is why I used 0.0 in the above and not 0^0 to fit with what I am doing.
Of course there are many ways to do things in Mathematica, but a design idiom I often use is to write the "function" (actually, a pattern) with decreasing specificity. Mathematica has the property that it will apply more specific patterns before less specific.
So, for your case I'd just write:
Clear[f];
f[0, 0] = 1;
f[a_, b_] := a^b;
I assume you expect to work with integer values since that's the usual context for this type of situation, e.g. when evaluating Bernstein basis functions.
I agree with the answer of @Deep Yellow, but if you insist on a pure function, here is one way:
f = Unevaluated[#1^#2] /. HoldPattern[0^0] :> 1 &
EDIT
Staying within the realm of pure functions, the situation you described in your edit can be addressed in the same way as my solution to your specific original example. You can automate this with a tiny bit of metaprogramming, defining the following function transformer:
z2zProtect[Function[body_]] :=
Function[Unevaluated[body] /. HoldPattern[0^0] :> 1]
Then, my previous code can be rewritten as:
f = z2zProtect[#1^#2 &]
But you can is this more generally, for example:
ff = z2zProtect[#1^#2 + 2 #2^#3 + 3 #3^#4 &]
In[26]:= ff[0,0,0,0]
Out[26]= 6
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With