diff --git a/mpmath/ctx_fp.py b/mpmath/ctx_fp.py index b93d0e86..0ee9be74 100644 --- a/mpmath/ctx_fp.py +++ b/mpmath/ctx_fp.py @@ -1,4 +1,5 @@ import cmath +import functools import math import sys @@ -18,7 +19,6 @@ class FPContext(StandardBaseContext): # Override SpecialFunctions implementation ctx.loggamma = libfp.loggamma - ctx._bernoulli_cache = {} ctx.pretty = False ctx._init_aliases() @@ -56,12 +56,9 @@ class FPContext(StandardBaseContext): f_wrapped.__doc__ = function_docs.__dict__.get(name, f.__doc__) setattr(cls, name, f_wrapped) + @functools.lru_cache def bernoulli(ctx, n): - cache = ctx._bernoulli_cache - if n in cache: - return cache[n] - cache[n] = to_float(mpf_bernoulli(n, 53, 'n'), strict=True) - return cache[n] + return to_float(mpf_bernoulli(n, 53, 'n'), strict=True) pi = libfp.pi e = math.e