bundles / scipy 1.18.0 / scipy / optimize / _elementwise / bracket_root
function
scipy.optimize._elementwise:bracket_root
Signature
def bracket_root(f, xl0, xr0 = None, *, xmin = None, xmax = None, factor = None, args = (), kwargs = None, maxiter = 1000)Summary
Bracket the root of a monotonic, real-valued function of a real variable.
Extended Summary
For each element of the output of f, bracket_root seeks the scalar bracket endpoints xl and xr such that sign(f(xl)) == -sign(f(xr)) elementwise.
The function is guaranteed to find a valid bracket if the function is monotonic, but it may find a bracket under other conditions.
This function works elementwise when xl0, xr0, xmin, xmax, factor, and the elements of args are (mutually broadcastable) arrays.
Parameters
f: callableThe function for which the root is to be bracketed. The signature must be
f(x: array, *args) -> arraywhere each element of
xis a finite real andargsis a tuple, which may contain an arbitrary number of arrays that are broadcastable withx.fmust be an elementwise function: each elementf(x)[i]must equalf(x[i])for all indicesi. It must not mutate the arrayxor the arrays inargs.xl0, xr0: float array_likeStarting guess of bracket, which need not contain a root. If
xr0is not provided,xr0 = xl0 + 1. Must be broadcastable with all other array inputs.xmin, xmax: float array_like, optionalMinimum and maximum allowable endpoints of the bracket, inclusive. Must be broadcastable with all other array inputs.
factor: float array_like, default: 2The factor used to grow the bracket. See Notes.
args: tuple of array_like, optionalAdditional positional array arguments to be passed to
f. If the callable for which the root is desired requires arguments that are not broadcastable withx, wrap that callable withfsuch thatfaccepts onlyxand broadcastable*args.kwargs: dict of str:array_like, optionalAdditional keyword arguments to be passed to
f. Seeargs.maxiter: int, default: 1000The maximum number of iterations of the algorithm to perform.
Returns
res: _RichResultAn object similar to an instance of scipy.optimize.OptimizeResult with the following attributes. The descriptions are written as though the values will be scalars; however, if
freturns an array, the outputs will be arrays of the same shape.success
Truewhere the algorithm terminated successfully (status0);Falseotherwise.status
An integer representing the exit status of the algorithm.
0The algorithm produced a valid bracket.-1The bracket expanded to the allowable limits without success.-2The maximum number of iterations was reached.-3A non-finite value was encountered.-4Iteration was terminated bycallback.-5: The initial bracket does not satisfy`xmin <= xl0 < xr0 < xmax`.
bracket
The lower and upper endpoints of the bracket, if the algorithm terminated successfully.
f_bracket
The values of
fevaluated at the endpoints ofres.bracket, respectively.nfev
The number of abscissae at which
fwas evaluated to find the root. This is distinct from the number of timesfis called because the the function may evaluated at multiple points in a single call.nit
The number of iterations of the algorithm that were performed.
Notes
This function generalizes an algorithm found in pieces throughout the scipy.stats codebase. The strategy is to iteratively grow the bracket (l, r) until f(l) < 0 < f(r) or f(r) < 0 < f(l). The bracket grows to the left as follows.
If
xminis not provided, the distance betweenxl0andlis iteratively increased byfactor.If
xminis provided, the distance betweenxminandlis iteratively decreased byfactor. Note that this also increases the bracket size.
Growth of the bracket to the right is analogous.
Growth of the bracket in one direction stops when the endpoint is no longer finite, the function value at the endpoint is no longer finite, or the endpoint reaches its limiting value (xmin or xmax). Iteration terminates when the bracket stops growing in both directions, the bracket surrounds the root, or a root is found (by chance).
If two brackets are found - that is, a bracket is found on both sides in the same iteration, the smaller of the two is returned.
If roots of the function are found, both xl and xr are set to the leftmost root.
Array API Standard Support
bracket_root has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable SCIPY_ARRAY_API=1 and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.
==================== ==================== ==================== Library CPU GPU ==================== ==================== ==================== NumPy ✅ n/a CuPy n/a ✅ PyTorch ✅ ✅ JAX ⛔ ⛔ Dask ⛔ n/a ==================== ==================== ====================
See dev-arrayapi for more information.
Examples
Suppose we wish to find the root of the following function.def f(x, c=5): return x**3 - 2*x - c✓
from scipy.optimize import elementwise res_bracket = elementwise.bracket_root(f, 0)✓
res_bracket.success res_bracket.bracket✗
(np.float64(2.0), np.float64(4.0))Indeed, the values of the function at the bracket endpoints have opposite signs.
res_bracket.f_bracket
✗(np.float64(-1.0), np.float64(51.0))Once we have a valid bracket, `find_root` can be used to provide a precise root.
res_root = elementwise.find_root(f, res_bracket.bracket)
✓res_root.x
✗np.float64(2.0945514815423265)`bracket_root` and `find_root` accept arrays for most arguments. For instance, to find the root for a few values of the parameter ``c`` at once:
import numpy as np c = np.asarray([3, 4, 5]) res_bracket = elementwise.bracket_root(f, 0, args=(c,)) res_bracket.bracket res_root = elementwise.find_root(f, res_bracket.bracket, args=(c,))✓
res_root.x
✗array([1.89329, 2. , 2.09455])
See also
Aliases
scipy.optimize._elementwise.bracket_root