0
$\begingroup$

I encountered a strange issue with the associated Legendre polynomials implemented with LegendreP[l,m,z]. Quite simply, the time used for the numerical computation of those quantities depends on whether m is odd or even :)

For m even and z a real number, LegendreP[l,m,z] evaluate in a few milliseconds quite consistantly across a wide range of order l. For m odd and z a real number, LegendreP[l,m,z] evaluate an order of magnitude slower and it gets worse with increasing order l.

I know that m odd means that LegendreP[l,m,z] involve the manipulation of square roots. If this is the reason, can anyone think of a workaround to get m odd evaluating as efficiently as m even ?

I use Mathematica 10.0.2 on Max OSX 10.10.4.

Thanks in advance.

 time0 = Table[Timing[LegendreP[l, 0, 2.]][[1]], {l, 1, 100}]; time1 = Table[Timing[LegendreP[l, 1, 2.]][[1]], {l, 1, 100}]; time2 = Table[Timing[LegendreP[l, 2, 2.]][[1]], {l, 1, 100}]; time3 = Table[Timing[LegendreP[l, 3, 2.]][[1]], {l, 1, 100}]; Show[ListPlot[{time0, time1, time2, time3}, PlotLegends -> Automatic], PlotRange -> All] 
$\endgroup$
7
  • $\begingroup$ It may help if you post your code comparing the timings $\endgroup$ Commented Aug 10, 2015 at 19:59
  • $\begingroup$ something very trivial like : time0 = Table[Timing[LegendreP[l, 0, 2.]][[1]], {l, 1, 100}]; time1 = Table[Timing[LegendreP[l, 1, 2.]][[1]], {l, 1, 100}]; time2 = Table[Timing[LegendreP[l, 2, 2.]][[1]], {l, 1, 100}]; time3 = Table[Timing[LegendreP[l, 3, 2.]][[1]], {l, 1, 100}]; Show[ListPlot[{time0, time1, time2, time3}, PlotLegends -> Automatic], PlotRange -> All] $\endgroup$ Commented Aug 10, 2015 at 20:07
  • $\begingroup$ Sorry for the mess… I'm not used to nicely format code… $\endgroup$ Commented Aug 10, 2015 at 20:08
  • $\begingroup$ On my version 9 on windows, it takes no time at all to calculate any of those. $\endgroup$ Commented Aug 10, 2015 at 22:23
  • $\begingroup$ @coussin, You can format inline code and code blocks by selecting the code and clicking the {} button above the edit window. The edit window help button ? is also useful for learning how to format your questions and answers. You may also find this this meta Q&A helpful $\endgroup$ Commented Aug 11, 2015 at 0:51

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.