Allan Clark
2010-Feb-11 15:54 UTC
[R] Recall function: "evaluation nested too deeply: infinite recursion / options(expressions=)?"
hello all
i searched the archives and couldn't get a solution to the following
question.
i have the following function:
F=function(z,v)
{
if (v==-.5) return(1) else return(2*v/z + 1/Recall(z,v-1))
}
and while testing whether it works (ie F(z,v) is approx =
besselK(z,1+v)/besselK(z,V). the recursion formula allows one to calculate
besselK(z,1+v)/besselK(z,V) for large values of z ) i got the following error :
"evaluation nested too deeply: infinite recursion /
options(expressions=)?"
i.e.
zz=1
nn=1000
R=array(0,dim=nn)
for (i in 1:nn)
{
print(i)
R[i]=F(zz,-0.5+(i-1))
}
the error occurs when i==713
but when one evaluates F(1,-.5+(713-1)) i get [1] 1423.001
note also that if v starts getting large, evaluation of F also gives the error.
e.g. F(1, 713.5)
any help?
seems like looping works nicely here
F2=function(z,v)
{
#check that the v is correct! ie -.5, .5, 3/2, 5/2, .....
if (v==-0.5)
{
return(1)
}else
{
ntimes = v+1.5
R=array(0,dim=ntimes)
R[1]=1
for (i in 2:ntimes)
{
R[i]= 2*v/z + 1/R[i-1]
}
return(R[ntimes])
}
}
Allan Clark
=======Lecturer in Statistical Sciences Department
University of Cape Town
7701 Rondebosch
South Africa
TEL (Office): +27-21-650-3228
FAX: +27-21-650-4773
http://web.uct.ac.za/depts/stats/aclark.htm
[[alternative HTML version deleted]]
