ctx_base.py (mpmath-0.18) | : | ctx_base.py (mpmath-0.19) | ||
---|---|---|---|---|

skipping to change at line 441 | skipping to change at line 441 | |||

def maxcalls(ctx, f, N): | def maxcalls(ctx, f, N): | |||

""" | """ | |||

Return a wrapped copy of *f* that raises ``NoConvergence`` when *f* | Return a wrapped copy of *f* that raises ``NoConvergence`` when *f* | |||

has been called more than *N* times:: | has been called more than *N* times:: | |||

>>> from mpmath import * | >>> from mpmath import * | |||

>>> mp.dps = 15 | >>> mp.dps = 15 | |||

>>> f = maxcalls(sin, 10) | >>> f = maxcalls(sin, 10) | |||

>>> print(sum(f(n) for n in range(10))) | >>> print(sum(f(n) for n in range(10))) | |||

1.95520948210738 | 1.95520948210738 | |||

>>> f(10) | >>> f(10) # doctest: +IGNORE_EXCEPTION_DETAIL | |||

Traceback (most recent call last): | Traceback (most recent call last): | |||

... | ... | |||

NoConvergence: maxcalls: function evaluated 10 times | NoConvergence: maxcalls: function evaluated 10 times | |||

""" | """ | |||

counter = [0] | counter = [0] | |||

def f_maxcalls_wrapped(*args, **kwargs): | def f_maxcalls_wrapped(*args, **kwargs): | |||

counter[0] += 1 | counter[0] += 1 | |||

if counter[0] > N: | if counter[0] > N: | |||

raise ctx.NoConvergence("maxcalls: function evaluated %i times" % N) | raise ctx.NoConvergence("maxcalls: function evaluated %i times" % N) | |||

skipping to change at line 469 | skipping to change at line 469 | |||

is equal to or higher than the working precision:: | is equal to or higher than the working precision:: | |||

>>> from mpmath import * | >>> from mpmath import * | |||

>>> mp.dps = 15; mp.pretty = True | >>> mp.dps = 15; mp.pretty = True | |||

>>> f = memoize(maxcalls(sin, 1)) | >>> f = memoize(maxcalls(sin, 1)) | |||

>>> f(2) | >>> f(2) | |||

0.909297426825682 | 0.909297426825682 | |||

>>> f(2) | >>> f(2) | |||

0.909297426825682 | 0.909297426825682 | |||

>>> mp.dps = 25 | >>> mp.dps = 25 | |||

>>> f(2) | >>> f(2) # doctest: +IGNORE_EXCEPTION_DETAIL | |||

Traceback (most recent call last): | Traceback (most recent call last): | |||

... | ... | |||

NoConvergence: maxcalls: function evaluated 1 times | NoConvergence: maxcalls: function evaluated 1 times | |||

""" | """ | |||

f_cache = {} | f_cache = {} | |||

def f_cached(*args, **kwargs): | def f_cached(*args, **kwargs): | |||

if kwargs: | if kwargs: | |||

key = args, tuple(kwargs.items()) | key = args, tuple(kwargs.items()) | |||

else: | else: | |||

End of changes. 2 change blocks. | ||||

2 lines changed or deleted | | 2 lines changed or added |