3
$\begingroup$

What is the best way to let mathematica memoize built-in function like e.g.

SphericalHarmonicY[l,m,th,ph]

One possibility would be for sure:

 sphericalHarmonicY[l_,m_,th_,ph_]:= sphericalHarmonicY[l,m,th,ph]=SphericalHarmonicY[l, m, th, ph]

But is there some function that works like:

 Memoize[SphericalHarmonicY]

And I can still use the original definition and Mathematica will store the values in the memory and access already calculated values later on?

$\endgroup$
  • 1
    $\begingroup$ Try looking up Once. Just make sure the values you want to memoize get Withed into the Once (otherwise it will just remember SphericalHarmonicY[l,m,th,ph] symbolically rather than any particular numeric value for it). $\endgroup$ – Sjoerd Smit Oct 9 '17 at 15:00
  • 1
    $\begingroup$ I assume this question is mainly about improving performance, and Once hardly seems efficient to me. The speed-up is about half what you get with memoization (based on repeated evaluation at many different points), the initial overhead is significant, and it's not really easier to code.... $\endgroup$ – Michael E2 Oct 9 '17 at 16:39

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.