13
$\begingroup$

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!

$\endgroup$
2
  • 1
    $\begingroup$ Could add alpha as a symbolic parameter, and eval symbolically before substituting a value. d[i_, j_, alf_] := Module[{aa}, Sum[aa^(i + j - 2 k)*(-1)^(-k)*Sqrt[i!] *Sqrt[j!]/((i - k)!*(j - k)!*k!), {k, 0, Min[i, j]}] /. aa->alf] In[16]:= d[0,0,0] Out[16]= 1 $\endgroup$ Commented Sep 24, 2014 at 20:37
  • $\begingroup$ (8403626) $\endgroup$
    – Mr.Wizard
    Commented Oct 27, 2014 at 6:19

2 Answers 2

14
$\begingroup$

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}}
$\endgroup$
8
  • $\begingroup$ The OP's coding gives me 1 for d[0,0]. Maybe she or he only had a dirty kernel? $\endgroup$
    – eldo
    Commented Sep 24, 2014 at 20:33
  • $\begingroup$ @eldo I think you forgot to also start with \[Alpha] = 0 as specified. $\endgroup$
    – Mr.Wizard
    Commented Sep 24, 2014 at 20:37
  • $\begingroup$ Thank you so much for the best answer! $\endgroup$
    – Suro
    Commented Sep 24, 2014 at 20:50
  • $\begingroup$ 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! $\endgroup$ Commented Oct 31, 2018 at 19:42
  • $\begingroup$ Also, do you have any idea how to make myPower compilable or use it in a CompiledFunction? $\endgroup$ Commented Oct 31, 2018 at 20:18
1
$\begingroup$

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

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

1

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.