3
$\begingroup$

I need to implement the following recursive function in Mathematica.

$$F[-2*a,b,2b;2]=\left(\frac{a-1/2}{a-1/2+b}\right)F\left[-2*(a-1),b,2b;2\right]$$

The conditions of the functions are: if $a=0$, then $F[0,b,2*b,2]=1$, if $a<0$, then $F[-2*a,b,2*b,2]=0$

Is it possible to do it recursively. What I did is:

hyperrec[a_Integer, b_Integer] := 
 Simplify[((a - (1/2))/((a - 1/2) + b))*hyperrec[(a - 1), b]]

But I am getting error of hold!

$\endgroup$
1
  • $\begingroup$ btw, you should not define a function as F[-2*a,b,2b;2] it should be just something like F[a,b,c,d,....] etc... I am assuming your F function is what you called hyperrec in your code. $\endgroup$
    – Nasser
    Jul 5, 2022 at 5:36

2 Answers 2

3
$\begingroup$

You did not show what you expected or an example call. So I do not know if this is what you expected or not. If not, will delete this. It is always better to give an example of what you want as output as one does not know what your code represents.

hyperrec[a_Integer, b_Integer] := 1/;a==0
hyperrec[a_Integer, b_Integer] := 0/;a<0
hyperrec[a_Integer, b_Integer] := Simplify[((a - (1/2))/((a - 1/2) + b))*hyperrec[(a - 1), b]]

enter image description here

$\endgroup$
3
  • $\begingroup$ It's working perfectly. $\endgroup$
    – Jasmine
    Jul 5, 2022 at 5:46
  • $\begingroup$ Is it possible to return the answer as a decimal? $\endgroup$
    – Jasmine
    Jul 5, 2022 at 5:48
  • 1
    $\begingroup$ @Jasmine Sure. Just add N@result where result is what shows above as final value. So something like N@hyperrec[2,2] gives 0.0857143 $\endgroup$
    – Nasser
    Jul 5, 2022 at 5:49
3
$\begingroup$
Clear["Global`*"]

hyperrec[0, b_] := 1;
hyperrec[a_Integer?Positive, b_] := 
  Simplify[((a - (1/2))/((a - 1/2) + b))*hyperrec[(a - 1), b]];
hyperrec[a_, b_] := 0;

Use RSolve or RSolveValue to get a closed form result

hyperrec2[a_Integer?NonNegative, b_] = 
 RSolveValue[{hyperrec2[a, b] == ((a - (1/2))/((a - 1/2) + b))*
      hyperrec2[(a - 1), b], hyperrec2[0, b] == 1}, 
       hyperrec2[a, b], {a, b}] // FullSimplify

(* (Gamma[1/2 + a] Gamma[1/2 + b])/(Sqrt[π] Gamma[1/2 + a + b]) *)

hyperrec2[a_, b_] := 0;

Verifying that the functions are equivalent,

amax = 20; (hyperrec[#, b] & /@ Range[-1, amax]) == 
  Table[hyperrec2[a, b], {a, -1, amax}] // FullSimplify

(* True *)
$\endgroup$

Your Answer

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

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