Hi, I wanted to compute the value of the function ifn at certain values of n. But I am receiving the following error when I was using the following code(given at the end). Error: evaluation nested too deeply: infinite recursion / options(expressions=)? I feel that since the function Grx is recursively related, perhaps making the code too complicated too handle. Can anyone let me know if there is any other way to handle this problem? d1=10.5 Grx<-function(x,r) { G0=Grx(x,0) G0=1; fu1<-function(t){exp(-t^2/(2*r*(r+1)))*Grx(x,r-1)} return(integrate(fu1,0,x)$value) } grx<-function(x,r) { Grx(x,r)*exp(-x^2/(2*(r+1))) } ifn<-function(n) { w=n*d1/2;S1=0; for(k in 1:(n-1)) { S1=S1+choose(n,k)*grx(w,k)*grx(w,n-k-1) } return(S1); } ifn(7) Thanks in advance. Shant [[alternative HTML version deleted]]
We replied once that your function Grx is not correct. It will recurse to an infinite depth because there is no condition to end the calls. I would expect that there would be some test at the beginning of the function to see if it should be called again. You need to debug your function, or rewrite to compute what you want. You have not described the problem you are trying to solve, so no solution can be provided. On Sun, Aug 22, 2010 at 12:03 AM, Shant Ch <sha1one at yahoo.com> wrote:> > > Hi, > > > I wanted to compute the value of the function ifn at certain values of n. But I > am receiving the following error when I was using the following code(given at > the end). > > Error: evaluation nested too deeply: infinite recursion / options(expressions=)? > > I feel that since the function Grx is recursively related, perhaps making the > code too complicated too handle. Can anyone let me know if there is any other > way to handle this problem? > > > d1=10.5 > Grx<-function(x,r) > { > G0=Grx(x,0) > G0=1; > fu1<-function(t){exp(-t^2/(2*r*(r+1)))*Grx(x,r-1)} > return(integrate(fu1,0,x)$value) > } > > grx<-function(x,r) > { > Grx(x,r)*exp(-x^2/(2*(r+1))) > } > > ifn<-function(n) > { > w=n*d1/2;S1=0; > for(k in ?1:(n-1)) > { > ?S1=S1+choose(n,k)*grx(w,k)*grx(w,n-k-1) > } > return(S1); > } > > ifn(7) > > Thanks in advance. > > Shant > > > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Shant Ch > Sent: Saturday, August 21, 2010 9:04 PM > To: r-help at r-project.org > Subject: [R] Recursion problem > > > > Hi, > > > I wanted to compute the value of the function ifn at certain > values of n. But I > am receiving the following error when I was using the > following code(given at > the end). > > Error: evaluation nested too deeply: infinite recursion / > options(expressions=)? > > I feel that since the function Grx is recursively related, > perhaps making the > code too complicated too handle. Can anyone let me know if > there is any other > way to handle this problem? > > > d1=10.5 > Grx<-function(x,r) > { > G0=Grx(x,0) > G0=1; > fu1<-function(t){exp(-t^2/(2*r*(r+1)))*Grx(x,r-1)} > return(integrate(fu1,0,x)$value) > }Your keep calling Grz(x, r-1) forever, as r descends from its starting value towards -Inf. Are you try to use the G0=Grz(x,0) G0=1 to say what to return when r==0 (vaguely like Haskell)? If so the proper syntax would be Grx <- function (x, r) { retval <- if (r < 0) { # must r be integral? stop("r must be nonnegative") } else if (r == 0) { 1 } else { fu1 <- function(t) { exp(-t^2/(2 * r * (r + 1))) * Grx(x, r - 1) } integrate(fu1, 0, x)$value } # Uncomment next line to trace the recursion # cat("x=", x, ", r=", r, " -> ", retval, "\n") retval } Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> > grx<-function(x,r) > { > Grx(x,r)*exp(-x^2/(2*(r+1))) > } > > ifn<-function(n) > { > w=n*d1/2;S1=0; > for(k in 1:(n-1)) > { > S1=S1+choose(n,k)*grx(w,k)*grx(w,n-k-1) > } > return(S1); > } > > ifn(7) > > Thanks in advance. > > Shant > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >