bundles / scipy 2.0.0.dev0+git20260907.41eeb59 / scipy / optimize / _bracket / _bracket_root
function
scipy.optimize._bracket:_bracket_root
source: build-install/usr/lib/python3.14/site-packages/scipy/optimize/_bracket.py:78
Signature
def _bracket_root(func, xl0, xr0 = None, *, xmin = None, xmax = None, factor = None, args = (), kwargs = None, maxiter = 1000, preserve_shape = False)Summary
Bracket the root of a monotonic scalar function of one variable
Extended Summary
This function works elementwise when xl0, xr0, xmin, xmax, factor, and the elements of args are broadcastable arrays.
Parameters
func: callableThe function for which the root is to be bracketed. The signature must be
func(x: ndarray, *args) -> ndarraywhere each element of
xis a finite real andargsis a tuple, which may contain an arbitrary number of arrays that are broadcastable withx.funcmust be an elementwise function: each elementfunc(x)[i]must equalfunc(x[i])for all indicesi.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 one another.xmin, xmax: float array_like, optionalMinimum and maximum allowable endpoints of the bracket, inclusive. Must be broadcastable with
xl0andxr0.factor: float array_like, default: 2The factor used to grow the bracket. See notes for details.
args: tuple, optionalAdditional positional arguments to be passed to
func. Must be arrays broadcastable withxl0,xr0,xmin, andxmax. If the callable to be bracketed requires arguments that are not broadcastable with these arrays, wrap that callable withfuncsuch thatfuncaccepts onlyxand broadcastable arrays.kwargs: dict of str:array_like, optionalAdditional keyword arguments to be passed to
func. Seeargs.maxiter: int, optionalThe maximum number of iterations of the algorithm to perform.
preserve_shape: bool, default: FalseWhether calls to
funcmust preserve the broadcasted shape of the arguments to _bracket_root. See bracket_root documentation.
Returns
res: _RichResultAn instance of scipy._lib._util._RichResult with the following attributes. The descriptions are written as though the values will be scalars; however, if
funcreturns an array, the outputs will be arrays of the same shape.xl,xr
The lower and upper ends of the bracket, if the algorithm terminated successfully.
fl,fr
The function value at the lower and upper ends of the bracket.
nfev
The number of function evaluations required to find the bracket. This is distinct from the number of times
funcis called because the function may evaluated at multiple points in a single call.nit
The number of iterations of the algorithm that were performed.
status
An integer representing the exit status of the algorithm.
0The algorithm produced a valid bracket.-1The bracket expanded to the allowable limits without finding a bracket.-2The maximum number of iterations was reached.-3A non-finite value was encountered.-4Iteration was terminated bycallback.-5: The initial bracket does not satisfyxmin <= xl0 < xr0 < xmax.1The algorithm is proceeding normally (incallbackonly).2A bracket was found in the opposite search direction (incallbackonly).
success
Truewhen the algorithm terminated successfully (status0).
Notes
This function generalizes an algorithm found in pieces throughout scipy.stats. The strategy is to iteratively grow the bracket (l, r) until func(l) < 0 < func(r). 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 (accidentally).
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 l and r are set to the leftmost root.
Aliases
scipy.optimize._bracket._bracket_root