13

I'm new to Wolfram Mathematica so the the question may seem quite simple.

I define the following function:

d[i_, j_] := 
 Sum[α^(i + j - 2 k) (-1)^(-k) Sqrt[i!] Sqrt[j!]/((i - k)! (j - k)! k!), {k, 0, Min[i, j]}]

Then, when I set α = 0 and try to evaluate d[0, 0] I get the following warning:

Power::indet: Indeterminate expression 0^0 encountered.

How can I input the condition Power[0,0]=1 in my function ?

Please help!

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Suro
  • 1,169
  • 1
  • 9
  • 17

2 Answers2

14

Although it is best to avoid this when possible, using the undocumented function Internal`InheritedBlock you can temporarily add a rule to Power:

α = 0;

d[i_, j_] := Internal`InheritedBlock[{Power},
  Unprotect[Power];
  Power[0, 0] = 1;
  Sum[α^(i + j - 2 k) (-1)^(-k) Sqrt[i!] Sqrt[j!]/((i - k)! (j - k)! k!), {k, 0, 
    Min[i, j]}]
 ]

d[0, 0]
1

Be aware that user rules for System functions may be ignored when working with packed arrays. See: Block attributes of Equal

Generally better is to use a proxy replacement for Power with your own behavior:

Attributes[myPower] = {Listable};
myPower[0, 0] = 1;
myPower[x_, y_] := x^y;

d[i_, j_] :=
 Sum[myPower[α, (i + j - 2 k)] (-1)^(-k) Sqrt[
    i!] Sqrt[j!]/((i - k)! (j - k)! k!), {k, 0, Min[i, j]}]

The Listable attribute makes sure that zeroes in vectors and matrices are also handled in the way we defined:

In[2]:= myPower[0, {{1, 1}, {1, 0}}]

Out[2]= {{0, 0}, {0, 1}}
István Zachar
  • 47,032
  • 20
  • 143
  • 291
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • The OP's coding gives me 1 for d[0,0]. Maybe she or he only had a dirty kernel? – eldo Sep 24 '14 at 20:33
  • @eldo I think you forgot to also start with \[Alpha] = 0 as specified. – Mr.Wizard Sep 24 '14 at 20:37
  • Thank you so much for the best answer! – Suro Sep 24 '14 at 20:50
  • Since the second part of your post seems to be the definite answer (and the safest) for these kinds of questions (this and this), I've included the more general case. Hope you don't mind it! – István Zachar Oct 31 '18 at 19:42
  • Also, do you have any idea how to make myPower compilable or use it in a CompiledFunction? – István Zachar Oct 31 '18 at 20:18
  • @István Thanks for the addition. I frankly don't do a lot with compilation but I'll try to give that some attention; don't get your hopes up! – Mr.Wizard Oct 31 '18 at 22:01
  • @Mr.Wizard this definition does not work for myPower[0.,0] – ABCDEMMM Jun 09 '21 at 01:00
  • @ABCDEMMM That's true. As with many of my answers I did not intend this to be an exhaustive definition ready for deployment as-is in production code. You might add a rule like myPower[__?PossibleZeroQ] = 1 to handle additional cases. – Mr.Wizard Jun 13 '21 at 00:34
1

Here is a variation of @Daniel's answer from his comment:

α = 0;
Block[{α}, d[0, 0]]

1

Carl Woll
  • 130,679
  • 6
  • 243
  • 355