Recursive function in Mathematica

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!

• 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. Jul 5 at 5:36

2 Answers

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]]


• It's working perfectly. Jul 5 at 5:46
• Is it possible to return the answer as a decimal? Jul 5 at 5:48
• @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 Jul 5 at 5:49
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 *)
`