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"]]
Nespecially is used by Mathematica. ?N for more information – Nasser Jun 03 '15 at 21:09g? Also, is the bottleneckSumorIntegrate?(you can doAbsoluteTimingseparately on them to find out). – Mahdi Jun 04 '15 at 00:12k. 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:39gis 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 intofout. The bottleneck also seems to be onIntegraterather thanSum. – maria Jun 04 '15 at 09:32Compileis unlikely to help here given List of compilable functions – dr.blochwave Jun 04 '15 at 10:01