2

I have a sum over Legendre polynomials which I'm trying to speed up. I've come across the suggestion of using Compile to speed up code in previous answers around this topic, but I can't figure out from the documentation how to apply Compile to this function, or whether this is even the right approach to be trying.

The function g is computed outside of this function, and is nearly instantaneous. But computing fout once I get up to n=40 or so, even over 12 cores, is taking over a minute on my computer, and I could really do with speeding it up.

Any suggestions on speeding this up? Or advice on how to use Compile with this?

fout[nu_, s_, n_, i_, p_] := 
     Block[{U, f2}, If[i == 1, {U = U1}, {U = U2}];  

fin = g[z, s, 20, i]; 

ParallelTable[2 π p Sum[U[m] fin[[k + 1]] Integrate[z^k LegendreP[m, 1, z]  
         LegendreP[t, 1, z], {z, -1, 1}], {m, 0, n}, {k, 0, 20}], {t, 0, n}, 
         Method -> "CoarsestGrained"]]
maria
  • 595
  • 4
  • 7
  • 2
    Do not use UpperCase for symbols and variables. N especially is used by Mathematica. ?N for more information – Nasser Jun 03 '15 at 21:09
  • Could we have the definition of g? Also, is the bottleneck Sum or Integrate?(you can do AbsoluteTiming separately on them to find out). – Mahdi Jun 04 '15 at 00:12
  • I recommend first generating tables of integrals for a few sample values of k. If you do so, you will discover that the resulting matrices are symmetric and also that more than half of the elements are zero. This observation should speed your calculation greatly. – bbgodfrey Jun 04 '15 at 02:39
  • Thanks @Nasser, I've adjusted that. – maria Jun 04 '15 at 09:23
  • @Madhi: g is rather long to add in here. But what it basically does is sum over the Legendre polynomials, turn this sum into a polynomial in $z$ and finally take the coefficients of the polynomial to put into fout. The bottleneck also seems to be on Integrate rather than Sum. – maria Jun 04 '15 at 09:32
  • @bbgodfrey :I had noticed this before but I'm not sure how to use this observation within the code. – maria Jun 04 '15 at 09:40
  • 1
    Compile is unlikely to help here given List of compilable functions – dr.blochwave Jun 04 '15 at 10:01

1 Answers1

1

A moderate decrease in AbsoluteTiming can be achieved simply by not performing Integrate in cases where it is known to yield 0. Use as the test case as slightly simplified version of the code in the Question.

f0[n_] := ParallelTable[Sum[ Integrate[z^k LegendreP[m, 1, z] LegendreP[t, 1, z], 
            {z, -1, 1}], {m, 0, n}, {k, 0, 20}], {t, 0, n}, Method -> "CoarsestGrained"]

Then, f0[20] // AbsoluteTiming takes about 45 sec on my 4-processor laptop. This can be reduced to 14 sec with

f[n_] := ParallelTable[Sum[If[EvenQ[t + m + k], Integrate[z^k LegendreP[m, 1, z] 
           LegendreP[t, 1, z], {z, -1, 1}], 0], {k, 0, 20}, 
           {m, Max[t - k, 0], Min[t + k, n]}], {t, 0, n}]

The corresponding times for n == 40 are 411 sec and 99 sec.

It seems likely that additional savings can be achieved, but the approach here is straightforward and easy to implement.

bbgodfrey
  • 61,439
  • 17
  • 89
  • 156