Re: Better recursive method?

*To*: mathgroup at smc.vnet.net*Subject*: [mg72818] Re: Better recursive method?*From*: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>*Date*: Sun, 21 Jan 2007 05:21:19 -0500 (EST)*Organization*: Uni Leipzig*References*: <eosh0f$9bs$1@smc.vnet.net>*Reply-to*: kuska at informatik.uni-leipzig.de

Hi, and transform the recursion into and iteration doesn not help ? and using pi[0,_] := 0; pi[1,_] := 1; pi[i_, theta_] := pi[i, theta] =(2n-1)/(n-1) Cos[theta] pi[i-1, theta] - n/(n-1) pi[i-2,theta] would be the first step and pi[n_, theta_] := Module[{p1,p2}, p1=0; p2=1; Do[ p1=p2*(2i-1)/(i-1) Cos[theta] - i/(i-1) p1; tmp=p2; p2=p1; p1=tmp,{i,2,n}] p2 ] the second one ... BTW why do you use "i" in the patteren and "n" in the recursion ?? Regards Jens > Hallo: > > I have following recursive functions: > pi(i, theta) := (2n-1)/(n-1) cos(theta) pi(i-1, theta) - n/(n-1) > pi(i-2,theta) > tau(i,theta) := n pi(i, theta) - (n+1) pi(i-1,theta) > > with intial values: > pi(0,theta) := 0 > pi(1,theta) := 1 > > These should calculate the following function: > S1(theta) := sum(i=1-nstop) (2i+1)/(i(i+1)) (a(i) pi(i,theta) - b(i) > tau(i,theta)) (where nstop=40) > > This should be pretty easy to calculate for Mathematica. I have written > it the following way: > pi[0,theta] := 0; > pi[1,theta] := 1; > pi[i_, theta_] := pi[i, theta] =(2n-1)/(n-1) Cos[theta] pi[i-1, theta] > - n/(n-1) pi[i-2,theta] > tau[i_,theta_] := n pi[i, theta] - (n+1) pi[i-1,theta] > S1Temp[i_,theta_] := (2i+1)/(i(i+1)) (a[i] pi[i,theta] - b[i] > tau[i,theta]) > S1(theta) := Sum[ S1Temp[i,theta], {i, 1, nstop}] > > Well, it works fine, but it takes enormous time. I have introduced > recursive function to save time for calculation of Legendre Polynomials > of which the function 'pi' and 'tau' consist of. But the same function > if I write with 2 loops each for 'theta' and 'i' in IDL, it calculates > in fraction of a second. Even the simple calculation of 'pi[40,pi/4]' > took more than 350 seconds. > > What could be the problem in my logic. Is there any other better way to > write recursive functions in Mathematica? > > Any help will be appreciated! > > Regards, > nandan >