pythoncachingsympybessel-functionsmpmath

Does sympy/mpmath cache results and what speed implications would that have?


I performed the following calculation:

from sympy import mpmath as mp
mp.besseljzero(1000, 100)

which understandably took some time > 10s if not more (didn't time it).

Subsequent calls were significantly faster which made me think it caches the results. I was wondering what other functions/caclulations sympy cahces?

Does this mean if I want to calculate mp.besseljzero(n, m) for n < N and m < M it's best to calculate mp.besseljzero(N-1, M-1) and then access the other results?

Thanks in advance.

I should add that I found this info on sympy FAQ. It seems that some sort of caching is supported. If there is more documentation you can point me to that would be helpful!

https://github.com/sympy/sympy/wiki/Faq


Solution

  • mpmath has its own memoization, separate from the rest of Sympy, which resets when the precision changes.

    SymPy has a cache, which caches the most expensive operations. In SymPy versions before 0.7.6, the cache is unbounded, which can lead to memory issues. In 0.7.6 upward, the cache is an LRU cache, which uses less memory, but is a little slower. To get the speed back, you can install the package fastcache, which is an LRU cache written in C, which improves the performance by quite a bit.

    The FAQ page you reference shows how to disable the cache, or clear it manually.