In my test, I found that the built-in function LegendreP of MMA is not as fast as the user-defined function. MMA should have already compiled and optimized the built-in function. Why is it not as fast as the user-defined function?

p[l_, x_] := 
 Sum[(-1)^k ((2 l - 2 k)!)/(2^l*k!*(l - k)! (l - 2 k)!) x^(l - 
      2 k), {k, 0, Floor[l/2]}]
(LegendreP[100, x] - p[100, x]) // FullSimplify
p[100, x]; // AbsoluteTiming
LegendreP[100, x]; // AbsoluteTiming
  • 2
    $\begingroup$ presumably because, as per the documentation, "LegendreP is defined to be Hypergeometric2F1Regularized[...]...". $\endgroup$ – AccidentalFourierTransform Feb 27 at 2:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.