Re: Looking for info on making recursions eat less stack.
- To: mathgroup@smc.vnet.net
- Subject: [mg11244] Re: Looking for info on making recursions eat less stack.
- From: Paul Abbott <paul@physics.uwa.edu.au>
- Date: Wed, 4 Mar 1998 01:39:09 -0500
- Organization: University of Western Australia
- References: <6ctiv2$lr4@smc.vnet.net>
Barthelet, Luc wrote: > I have some Mathematica code that use a function recursively. at a depth > of about 500, the kernel dies. ( I raised $RecursionLimit). I am > currently assuming that it is because I eat up too much stack. > > Is there any info about what to do to get a recusive function to consume > less stack? > > I kept the number of local variable to a minimum. the ByteCount of the > local variables plus the parameters of the function is 2736 (which is > quite a bit). > Still I should be able to go to deeper than 500. > > Also $RecursionLimit warning message does not match the actual deph, but > seems to be about 4 times deeper than my function. Does that make > sense? Perhaps you could post your example? Are you using Dynamic programming? This can help in many recursive problems (but can also consume large amounts of memory). For example, compare In[1]:= fib[0]=fib[1]=1; In[2]:= fib[n_]:=fib[n-1]+fib[n-2] In[3]:= fib[20]//Timing Out[3]= {1.3 Second,10946} with In[4]:= fib[n_]:=fib[n]=fib[n-1]+fib[n-2] In[5]:= fib[20]//Timing Out[5]= {0. Second,10946} Cheers, Paul ____________________________________________________________________ Paul Abbott Phone: +61-8-9380-2734 Department of Physics Fax: +61-8-9380-1014 The University of Western Australia Nedlands WA 6907 mailto:paul@physics.uwa.edu.au AUSTRALIA http://www.pd.uwa.edu.au/~paul God IS a weakly left-handed dice player ____________________________________________________________________