"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "mpmath/calculus/optimization.py" between
mpmath-0.19.tar.gz and mpmath-1.0.0.tar.gz

About: mpmath is a Python library for arbitrary-precision floating-point arithmetic.

optimization.py  (mpmath-0.19):optimization.py  (mpmath-1.0.0)
skipping to change at line 687 skipping to change at line 687
x0 = x1 x0 = x1
break break
l /= 2 l /= 2
x1 = x0 + l*s x1 = x0 + l*s
yield (x0, fxnorm) yield (x0, fxnorm)
############# #############
# UTILITIES # # UTILITIES #
############# #############
str2solver = {'newton':Newton, 'secant':Secant,'mnewton':MNewton, str2solver = {'newton':Newton, 'secant':Secant, 'mnewton':MNewton,
'halley':Halley, 'muller':Muller, 'bisect':Bisection, 'halley':Halley, 'muller':Muller, 'bisect':Bisection,
'illinois':Illinois, 'pegasus':Pegasus, 'anderson':Anderson, 'illinois':Illinois, 'pegasus':Pegasus, 'anderson':Anderson,
'ridder':Ridder, 'anewton':ANewton, 'mdnewton':MDNewton} 'ridder':Ridder, 'anewton':ANewton, 'mdnewton':MDNewton}
def findroot(ctx, f, x0, solver=Secant, tol=None, verbose=False, verify=True, ** kwargs): def findroot(ctx, f, x0, solver='secant', tol=None, verbose=False, verify=True, **kwargs):
r""" r"""
Find a solution to `f(x) = 0`, using *x0* as starting point or Find a solution to `f(x) = 0`, using *x0* as starting point or
interval for *x*. interval for *x*.
Multidimensional overdetermined systems are supported. Multidimensional overdetermined systems are supported.
You can specify them using a function or a list of functions. You can specify them using a function or a list of functions.
If the found root does not satisfy `|f(x)^2 < \mathrm{tol}|`, If the found root does not satisfy `|f(x)|^2 \leq \mathrm{tol}`,
an exception is raised (this can be disabled with *verify=False*). an exception is raised (this can be disabled with *verify=False*).
**Arguments** **Arguments**
*f* *f*
one dimensional function one dimensional function
*x0* *x0*
starting point, several starting points or interval (depends on solver) starting point, several starting points or interval (depends on solver)
*tol* *tol*
the returned solution has an error smaller than this the returned solution has an error smaller than this
*verbose* *verbose*
print additional information for each iteration if true print additional information for each iteration if true
*verify* *verify*
verify the solution and raise a ValueError if `|f(x) > \mathrm{tol}|` verify the solution and raise a ValueError if `|f(x)|^2 > \mathrm{tol}`
*solver* *solver*
a generator for *f* and *x0* returning approximative solution and error a generator for *f* and *x0* returning approximative solution and error
*maxsteps* *maxsteps*
after how many steps the solver will cancel after how many steps the solver will cancel
*df* *df*
first derivative of *f* (used by some solvers) first derivative of *f* (used by some solvers)
*d2f* *d2f*
second derivative of *f* (used by some solvers) second derivative of *f* (used by some solvers)
*multidimensional* *multidimensional*
force multidimensional solving force multidimensional solving
skipping to change at line 888 skipping to change at line 888
>>> findroot(lambda x: x**2, (-1, 1), solver='anderson') #doctest:+ELLIP SIS >>> findroot(lambda x: x**2, (-1, 1), solver='anderson') #doctest:+ELLIP SIS
Traceback (most recent call last): Traceback (most recent call last):
... ...
ZeroDivisionError ZeroDivisionError
It fails even for better starting points, because there is no sign change:: It fails even for better starting points, because there is no sign change::
>>> findroot(lambda x: x**2, (-1, .5), solver='anderson') >>> findroot(lambda x: x**2, (-1, .5), solver='anderson')
Traceback (most recent call last): Traceback (most recent call last):
... ...
ValueError: Could not find root within given tolerance. (1 > 2.1684e-19) ValueError: Could not find root within given tolerance. (1.0 > 2.1684043 4497100886801e-19)
Try another starting point or tweak arguments. Try another starting point or tweak arguments.
""" """
prec = ctx.prec prec = ctx.prec
try: try:
ctx.prec += 20 ctx.prec += 20
# initialize arguments # initialize arguments
if tol is None: if tol is None:
tol = ctx.eps * 2**10 tol = ctx.eps * 2**10
skipping to change at line 971 skipping to change at line 971
print_('error:', error) print_('error:', error)
i += 1 i += 1
if error < tol * max(1, norm(x)) or i >= maxsteps: if error < tol * max(1, norm(x)) or i >= maxsteps:
break break
if not isinstance(x, (list, tuple, ctx.matrix)): if not isinstance(x, (list, tuple, ctx.matrix)):
xl = [x] xl = [x]
else: else:
xl = x xl = x
if verify and norm(f(*xl))**2 > tol: # TODO: better condition? if verify and norm(f(*xl))**2 > tol: # TODO: better condition?
raise ValueError('Could not find root within given tolerance. ' raise ValueError('Could not find root within given tolerance. '
'(%g > %g)\n' '(%s > %s)\n'
'Try another starting point or tweak arguments.' 'Try another starting point or tweak arguments.'
% (norm(f(*xl))**2, tol)) % (norm(f(*xl))**2, tol))
return x return x
finally: finally:
ctx.prec = prec ctx.prec = prec
def multiplicity(ctx, f, root, tol=None, maxsteps=10, **kwargs): def multiplicity(ctx, f, root, tol=None, maxsteps=10, **kwargs):
""" """
Return the multiplicity of a given root of f. Return the multiplicity of a given root of f.
 End of changes. 6 change blocks. 
6 lines changed or deleted 6 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)