scijit.optimize.golden

scijit.optimize.golden(f, args=(), brack=None, tol=1.4901161193847656e-08, full_output=False, maxiter=5000)

Golden-section minimizer.

Callback style A: f is a plain @njit function taking one float and returning one float:

@njit
def f(x):
    return (x - 1.5) ** 2 + 0.5

x = golden(f)
x, fun, nfev = golden(f, full_output=True)
Parameters:
f@njit function f(x) -> float

Objective to minimise.

argstuple, optional

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

brackNone or tuple, optional

Bracket for the search. None (default), a 2-sequence (xa, xb) giving the downhill search its starting points, or a 3-sequence (xa, xb, xc) used directly. See Notes.

tolfloat, optional

Relative tolerance on the minimiser. Default sqrt(eps) = 1.4901161193847656e-08.

full_outputbool, optional

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

maxiterint, optional

Iteration cap. Default 5000.

Returns:
xfloat

The minimiser, when full_output is False.

(x, fun, nfev)tuple

When full_output is True.

Raises:
ValueError

If brack is a 3-sequence whose points are not ordered, or whose middle value is not below both ends; or if brack has a length other than 2 or 3.

TypeError

If args is not a tuple.

RuntimeError

If the bracket search finds no valid bracket, or reaches its own iteration limit.

numba.core.errors.TypingError

From inside @njit, if full_output is a runtime variable.

See also

scipy.optimize.golden

The scipy routine this mirrors.

scijit.optimize.minimize_scalar

The same engines behind a method argument.

scijit.optimize.fminbound

Minimises on a closed interval instead.

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.

brack takes three spellings. None runs the downhill bracket search from (0.0, 1.0). A 2-sequence runs it from those two points. A 3-sequence is used directly after two checks, (xa < xb) and (xb < xc) after swapping so xa < xc, and (f(xb) < f(xa)) and (f(xb) < f(xc)); each raises ValueError. The 3-sequence path counts exactly 3 evaluations. A sequence of any other length raises ValueError.

Inside @njit brack must be a tuple or None; a list is a heterogeneous-container question numba answers only for a literal, and a tuple is the spelling that types from both entry points.

Pure @njit, no state and no callback slot, so it is safe to call from a numba.prange loop.

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

Examples

>>> from numba import njit
>>> from scijit.optimize import golden
>>> @njit
... def q(x):
...     return (x - 1.5) ** 2 + 0.5
>>> @njit
... def run():
...     return golden(q, full_output=True)
>>> x, fun, nfev = run()
>>> round(x, 8), round(fun, 8), nfev
(1.50000001, 0.5, 43)