MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Better recursive method?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg72822] Re: [mg72802] Better recursive method?
  • From: Bob Hanlon <hanlonr at cox.net>
  • Date: Sun, 21 Jan 2007 05:37:32 -0500 (EST)
  • Reply-to: hanlonr at cox.net

Use Simplify in the definition of pi to keep the complexity of the expression from growing unnecessarily 

Clear[n,pi];
pi[0,theta_]:=0;
pi[1,theta_]:=1;
pi[i_,theta_]:=pi[i,theta]=Simplify[
        (2n-1)/(n-1) Cos[theta] pi[i-1,theta]-n/(n-1) pi[i-2,theta]];

pi[40,Pi/4]//Timing

{4.7574130000000014*Second, (1/(524288*Sqrt[2]*(-1 + n)^39))*(-1 + 2*n + 72*n^2 - 288*n^3 - 1816*n^4 + 12912*n^5 + 
    5376*n^6 - 251904*n^7 + 598656*n^8 + 1706752*n^9 - 11354112*n^10 + 13246464*n^11 + 63092224*n^12 - 
    263689216*n^13 + 253989888*n^14 + 911228928*n^15 - 3580728576*n^16 + 4528556544*n^17 + 3750897664*n^18 - 
    25007235072*n^19 + 45057976320*n^20 - 30315253760*n^21 - 44281528320*n^22 + 155489402880*n^23 - 
    228225269760*n^24 + 190602706944*n^25 - 41819701248*n^26 - 138994515968*n^27 + 255847432192*n^28 - 
    267452153856*n^29 + 202105159680*n^30 - 117021081600*n^31 + 52585758720*n^32 - 18160680960*n^33 + 
    4679270400*n^34 - 849346560*n^35 + 96993280*n^36 - 5242880*n^37)}


Bob Hanlon

---- nandan <joshi.nandan at gmail.com> wrote: 
> 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
> 

--

Bob Hanlon
hanlonr at cox.net



  • Prev by Date: Kernel Quits without error message. (Was: Better recursive method?)
  • Next by Date: Re: how to quickly read a >10MB big file
  • Previous by thread: Re: Better recursive method?
  • Next by thread: Re: Better recursive method?