scijit.optimize.fminbound

scijit.optimize.fminbound(f, x1, x2, args=(), xtol=1e-05, maxfun=500, full_output=False, disp=1)

Bounded scalar minimizer on a fixed interval.

Unlike brent() and golden(), [x1, x2] is a hard box: the search never leaves it, so the answer is the constrained minimum, which may sit on an endpoint.

Callback style A: f is a plain @njit f(x) -> float.

Parameters:
f@njit function f(x) -> float

Function to minimize.

x1, x2float

Interval bounds, required (there is no default box). x1 > x2 raises ValueError.

argstuple, optional

Extra arguments for f, unpacked into every call as f(x, *args). Must be a tuple; anything else raises TypeError. Default ().

xtolfloat, optional

Absolute tolerance on the minimizer position. Default 1e-5, looser than the other minimizers here.

maxfunint, optional

Cap on function evaluations, not iterations. Default 500.

full_outputbool, optional

False (default) returns x alone. True returns (x, fun, status, nfev), with status THIRD. Inside @njit it must be a compile-time constant; see Notes.

dispint, optional

Print level. 0 and 1 print nothing on a converged solve, 2 prints the termination block. Default 1. 3 raises NotImplementedError; see Notes.

Returns:
xfloat

The minimiser, when full_output is False.

(x, fun, status, nfev)tuple

When full_output is True. status is 0 on success and 1 when maxfun was reached, and it is the THIRD element, not the last.

Raises:
ValueError

If either bound is not finite, or if x1 > x2.

TypeError

If args is not a tuple.

NotImplementedError

If disp >= 3.

Warns:
OptimizeWarning

On either failure exit, when disp > 0: "Maximum number of function evaluations exceeded --- increase maxfun argument." and "NaN result encountered.", each wrapped in newlines.

See also

scipy.optimize.fminbound

The scipy routine this mirrors.

scijit.optimize.brent

Unbounded, seeded rather than bracketed.

scijit.optimize.minimize_scalar

method='bounded' reaches this one.

Notes

full_output selects the RETURN SHAPE, and a compiled function has one return type per signature, so inside @njit the flag has to be readable when the call compiles. A literal, an omitted default and a module-level constant all are; a variable is not, and raises TypingError naming the constraint. From Python a runtime value is fine.

disp is an int print level, not a bool. 0 and 1 print nothing on a converged solve, and signal a maxfun limit through a warning rather than a print. 2 prints the termination block. 3 would add a per-iteration Func-count table whose Procedure column names the step type (golden or parabolic) each iteration took; the bounded-Brent core does not report that, so 3 raises NotImplementedError rather than printing a table missing a column.

Pure @njit, safe to call from a numba.prange loop.

https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.fminbound.html

Examples

>>> from numba import njit
>>> from scijit.optimize import fminbound
>>> @njit
... def q(x):
...     return (x - 1.5) ** 2 + 0.5
>>> @njit
... def run():
...     return fminbound(q, 0.0, 3.0)
>>> round(run(), 8)
1.5
>>> @njit
... def run_full():
...     return fminbound(q, 0.0, 3.0, (), 1e-5, 500, True, 0)
>>> run_full()
(1.5, 0.5, 0, 6)