MathGroup Archive 2007

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

Search the Archive

Re: LegendreP Evaluation Mystery

  • To: mathgroup at smc.vnet.net
  • Subject: [mg75083] Re: LegendreP Evaluation Mystery
  • From: "Norbert Marxer" <marxer at mec.li>
  • Date: Mon, 16 Apr 2007 20:14:02 -0400 (EDT)
  • References: <evvaqs$95r$1@smc.vnet.net>

On 16 Apr., 10:06, "Antonio" <ane... at gmail.com> wrote:
> Dear all,
>
> I have found that Mathematica v5.2 evaluates BesselJ and LegendreP
> differently. Why is this? I have written this test below to illustrate
> the timing.
>
> In[11]:=
> k=10000;
> n=51;
> m=Random[Integer,{0,n}];
> x=Random[Real,{0,1}];
> P[y_]=LegendreP[n,m,y];
> J[y_]=BesselJ[n,y];
> Timing[Do[LegendreP[n,m,x],{k}]]
> Timing[Do[P[x],{k}]]
> Timing[Do[BesselJ[n,x],{k}]]
> Timing[Do[J[x],{k}]]
>
> Out[17]=
> {4.531 Second,Null}
>
> Out[18]=
> {0.484 Second,Null}
>
> Out[19]=
> {0.61 Second,Null}
>
> Out[20]=
> {0.657 Second,Null}
>
> The emphasis here is with the Associated Legendre Function, since it is
> a bottel neck of my current calculations for big n's (n>50), used
> inside NIntegrate. The form LegendreP[n,m,x] takes longer to evaluate
> than in extended form (P[y])? If I try to use the extended form to do
> some numerical integration, it results in a wrong result even though
> it is faster.
>
> When plotting the functions, it seems to show some numerical
> instabilities (for low m):
>
> n = 51; m = 3;
> Plot[LegendreP[n, m, x], {x, 0, 1}, PlotRange -> All, PlotPoints ->
> 100];
> Plot[P[x], {x, 0, 1}, PlotRange -> All, PlotPoints -> 100];
>
> And it is worse for m=0. Does Mathematica evaluate LegendreP
> differently for high n's, why does it take so long? Is there any way
> that I build an array of extended Associated Legendre functions, so as
> to speed up calculations and wouldn't fail numerically as above?
>
> Antonio

Hello

If you use SetDelayed (:=) instead of Set (=) in your defintions for
m, x, P and J then all your mysteries and questions will (probably)
evaporate.

With your definition the random numbers are evaluated only once and
therefore m is constant.

Similarly your associated Legendre polynomial (P) is evaluated only
once (for specific values of n and m) and set to a specific constant
polynomial with the independent variable y. The evaluation of this
polynomial takes obviously less time than the calculation and
evaluation of the associated Legendre polynomial.

Best Regards
Norbert Marxer



  • Prev by Date: Re: Continued Fractions
  • Next by Date: Re: Continued Fractions
  • Previous by thread: LegendreP Evaluation Mystery
  • Next by thread: Re: LegendreP Evaluation Mystery