SF_OPTIMIZE
Description
SciFortran module for minimization and root finding
Quick access
- Routines:
adaptive_mix(),bisect(),bracket(),brent(),brentq(),broyden1(),broyden_mix(),curvefit(),dbrent(),fmin(),fmin_bfgs(),fmin_cg(),fmin_cgminimize(),fmin_cgplus(),fsolve(),fzero(),leastsq(),linear_mix(),newton()
Used modules
cgfit_routinesbroyden_routinessf_constants: contains mathematical, numerical and physical constantssf_linalg: SciFortran module for linear algebra
Subroutines and functions
- interface sf_optimize/fmin_cg(p, f, df, iter, fret[, ftol, itmax, istop, iverbose, err, deps])
Generic interface to minimize a function of several variables with the Fletcher-Reeves-Polak-Ribiere conjugate gradient method (Numerical Recipes 10.6), with the line minimizations done with the Brent method. The gradient is supplied by the user (
fmin_cg_df) or computed numerically (fmin_cg_f). The location of the minimum is returned inp, the number of iterations initerand the value of the function infret.The convergence is controlled by
istopandftol: with \(a = |F_n-F_{n-1}|/(1+|F_n|)\) and \(b = \|p_n-p_{n-1}\|^2/(1+\|p_n\|^2)\), the iteration stops when \(a<ftol\) and \(b<ftol\) (istop=0), when \(a<ftol\) (istop=1) or when \(b<ftol\) (istop=2). The last values of \(a\) and \(b\) are returned inerr.- Parameters:
p (•) [real, inout] – in: starting point; out: location of the minimum
f [real] – function to be minimized, real(8) function f(x) with real(8) x(:)
df [real] – gradient of f, real(8) function df(x) returning a vector with the size of x
iter [integer, out] – out: number of iterations performed
fret [real, out] – out: value of f at the minimum
- Options:
ftol [real] – convergence tolerance, default 1d-5
itmax [integer] – maximum number of iterations, default 500
istop [integer] – stopping criterion, 0 (default) both a and b < ftol, 1 a < ftol, 2 b < ftol
iverbose [logical] – print information on the convergence, default .false.
err (2) [real] – out: last values of the convergence measures [a,b]
deps [real] – step of the numerical derivative, default 0
- interface sf_optimize/fmin_cgplus(p, func, fjac, iter, fret, fcn[, ftol, itmax, imethod, iverb1, iverb2, deps])
Generic interface to minimize a function of several variables with the nonlinear conjugate gradient method of the CG+ package (Gilbert and Nocedal), a very old Fortran 77 code. The gradient is supplied by the user (
fmin_cgplus_df) or computed numerically (fmin_cgplus_f). The variant of the method is selected byimethod, and the iteration stops when every component of the gradient is smaller than \(ftol(1+|f|)\). The location of the minimum is returned inp.- Parameters:
p (•) [real, inout] – in: starting point; out: location of the minimum
func [external] – function to be minimized, real(8) function func(x) with real(8) x(:)
fjac [external] – gradient of func, real(8) function fjac(x) returning a vector with the size of x
iter [integer, out] – out: number of iterations performed
fret [real] – out: value of func at the minimum
fcn [real] – function to be minimized, real(8) function fcn(x) with real(8) x(:)
- Options:
ftol [real] – convergence tolerance on the gradient, default 1d-5
itmax [integer] – maximum number of iterations, default 1000
imethod [integer] – 1 Fletcher-Reeves, 2 Polak-Ribiere (default), 3 positive Polak-Ribiere
iverb1 [integer] – output frequency, < 0 none (default), 0 first and last iteration, > 0 every iverb1 iterations
iverb2 [integer] – amount of output, 0 (default) to 3: 1 initial x and gradient, 2 x every iteration, 3 x and gradient
deps [real] – optional: step of the numerical derivative, default 0
- interface sf_optimize/fmin_cgminimize(p, fcn, iter, fret[, ftol, itmax, iverbose, mode, new_version, hh_par])
Generic interface to minimize a function of several variables with a conjugate gradient routine adapted from a very old Fortran 77 code, a quasi-Newton minimizer with numerical gradient. The function is a function (
fmin_cgminimize_func) or a subroutine (fmin_cgminimize_sub). Two versions of the minimizer are available, selected bynew_version. The location of the minimum is returned inp.- Parameters:
p (•) [real, inout] – in: starting point; out: location of the minimum
fcn [real] – function to be minimized, real(8) function fcn(x) with real(8) x(:)
iter [integer] – out: number of iterations performed
fret [real] – out: value of the function at the minimum
- Options:
ftol [real] – convergence tolerance, default 1d-5
itmax [integer] – maximum number of iterations, default 1000 (calls: 2*N*itmax)
iverbose [logical] – print information, default .false.
mode [integer] – mode of the minimizer, default 1 (the value passed is currently ignored)
new_version [logical] – .true. uses the new minimizer, .false. (default) the old one
hh_par [real] – step of the numerical gradient, default 1d-5
- interface sf_optimize/leastsq(func, a, m, dfunc[, tol, info])
Generic interface to minimize the sum of squares of \(m\) nonlinear functions of \(n\) unknowns, \(m\ge n\), i.e. to find
aso that \(f(a)^T f(a)\) is minimum, with the Levenberg-Marquardt routines of MINPACK. The Jacobian is computed numerically (lmdif,leastsq_lmdif_funcandleastsq_lmdif_sub) or supplied by the user (lmder,leastsq_lmder_funcandleastsq_lmder_sub). The functions are a functionfunc(a,m)returning a vector of size \(m\), or a subroutinefunc(a,m,f); the Jacobiandfunc(a,m)is a matrix of shape \([m,n]\).The program stops, writing the reason to the file
LEASTSQ_ERROR.err, if the status returned by MINPACK is not 1.- Parameters:
func [external]
a (•) [real] – in: starting guess; out: solution, size n
m [integer] – number of functions, m >= n
dfunc [external]
- Options:
tol [real] – optional: tolerance, default 1d-15
info [integer] – optional out: status of the MINPACK routine
- interface sf_optimize/curvefit(model_func, a, xdata, ydata, model_dfunc[, tol, info])
Generic interface to fit a model function to data with the non-linear least squares Levenberg-Marquardt routines of MINPACK: the parameters
aofmodel_func(x,a)are adjusted to minimize the sum of the squares ofmodel_func(xdata,a)-ydata. The Jacobian is computed numerically (lmdif,curvefit_lmdif_funcandcurvefit_lmdif_sub) or supplied by the user (lmder,curvefit_lmder_funcandcurvefit_lmder_sub). The model is a functionmodel_func(x,a)returning a vector with the size ofx, or a subroutinemodel_func(x,a,f); the derivativemodel_dfunc(x,a)is a matrix of shape \([size(x),size(a)]\). The model is evaluated on the whole arrayxdataat once.- Parameters:
model_func [external]
a (•) [real] – in: starting guess of the parameters; out: fitted parameters, size n
xdata (•) [real, in,required] – abscissas of the data, size m
ydata (size(xdata)) [real] – ordinates of the data, size m
model_dfunc [external]
- Options:
tol [real] – optional: tolerance, default 1d-15
info [integer] – optional out: status of the MINPACK routine
- interface sf_optimize/dbrent(func, dfunc, xmin[, brack, tol, niter])
Generic interface to find the minimum of a function of one variable,
func, with the Brent method using the derivative: the user supplies the derivativedfunc(dbrent_wgrad), or it is computed numerically (dbrent_nograd). The abscissa of the minimum is returned inxmin. The bracketing of the minimum,brack, works as inbrent: a bracketing triplet, or two starting points from which it is searched withbracket, with default points 0 and 1. The program stops ifbrackhas a single element.- Parameters:
func [external]
dfunc [external]
xmin [real, inout] – abscissa of the minimum
- Options:
brack (•) [real] – optional: 2 starting points or 3 bracketing points, default [0,1]
tol [real] – optional: fractional precision of the minimum, default 1d-9
niter [integer] – optional: maximum number of iterations, default 200
- interface sf_optimize/fmin_bfgs(func, grad, x[, l, u, nbd, factr, pgtol, iprint, nloop])
Generic interface to minimize a function of several variables with the limited-memory BFGS algorithm with bounds, L-BFGS-B (Zhu, Byrd, Lu and Nocedal), subject to the optional bounds \(l\le x\le u\). The gradient is supplied by the user (
bfgs_with_grad) or computed numerically (bfgs_no_grad). The type of the bounds is given bynbd, the bounds are ignored ifnbdis absent. The location of the minimum is returned inx.- Parameters:
func [external]
grad [external]
x (•) [real, allocatable] – in: starting point; out: location of the minimum
- Options:
l (•) [real, allocatable] – optional: lower bounds, size n
u (•) [real, allocatable] – optional: upper bounds, size n
nbd (•) [integer, allocatable] – optional: 0 none, 1 lower, 2 both, 3 upper bound
factr [real] – optional: f tolerance in eps units, default 1d7
pgtol [real] – optional: projected gradient tolerance, default 1d-5
iprint [integer] – optional: <0 (default) no output, larger prints more
nloop [integer] – optional: maximum number of loops
- interface sf_optimize/linear_mix(x, fx, alpha)
Generic interface for the linear mixing of an array of rank 1 to 7, real (
d_linear_mix_1tod_linear_mix_7) or complex (c_linear_mix_1toc_linear_mix_7): \(x \leftarrow x + \alpha F_x\), with \(F_x = x_{out}-x_{in}\) the residual of the iteration.- Parameters:
x (various shapes) [real/complex, inout/in,required] – in: x_in; out: mixed x
fx (various shapes) [real/complex, in] – x_out-x_in
alpha [real, in] – mixing parameter
- interface sf_optimize/adaptive_mix(x, fx, alpha, iter, z, fz)
Generic interface for the adaptive linear mixing, real (
d_adaptive_mix) or complex (c_adaptive_mix): \(x \leftarrow x + \beta F_x\), with an element-wise mixing parameter \(\beta_j\), which starts fromalpha, is increased byalphaat every iteration in which the residual \(F_{x,j}\) keeps its sign, up to 1, and is reset toalphaif the sign changes (oscillations). The previous residual is stored between calls: it is reset wheniteris 1.- Parameters:
x (•) [real, inout/in,required] – in: x_in; out: mixed x
fx (size(x)) [real, in] – residual, x_out-x_in
alpha [real, in] – mixing parameter, initial and minimum speed
iter [integer, in] – iteration number, 1 resets the memory
z (•) [complex, inout/in,required] – z_in
fz (size(z)) [complex, in] – z_out-z_in
- interface sf_optimize/broyden_mix(x, fx, alpha, m, iter[, w0])
Generic interface for the modified Broyden mixing of Johnson (Phys. Rev. B 38, 12807, 1988), real (
d_broyden_mix) or complex (c_broyden_mix): the mixed vector is built from the residual \(F_x = x_{out}-x_{in}\) and from the history of the lastMiterations, stored between calls and reset wheniteris 1. The first iteration is a linear mixing withalpha, as are all the iterations ifM=0. The weight of the initial iteration isw0.- Parameters:
x (•) [real/complex, inout/in,required] – in: x_in; out: mixed x
fx (size(x)) [real/complex, in] – residual, x_out-x_in
alpha [real, in] – mixing parameter
m [integer, in] – number of previous iterations kept in memory, 0 is linear mixing
iter [integer, in] – iteration number, 1 resets the memory
- Options:
w0 [real] – optional: weight of the initial iteration, default 0.01
- interface sf_optimize/fsolve(func, x, dfunc[, tol, info, icheck, maxfev])
Generic interface to solve a system of \(n\) nonlinear equations in \(n\) unknowns, \(f(x)=0\), with the hybrid Powell method of MINPACK. The Jacobian is computed numerically (
hybrd,fsolve_hybrd_funcandfsolve_hybrd_sub) or supplied by the user (hybrj,fsolve_hybrj_funcandfsolve_hybrj_sub). The system is a functionfunc(x)returning a vector with the size ofx, or a subroutinefunc(x,f); the Jacobiandfunc(x)is a matrix of shape \([n,n]\). Ificheckis true, the default, the program stops, writing the reason to a file, when the status returned by MINPACK is not a success.- Parameters:
func [external]
x (•) [real] – in: starting guess; out: solution, size n
dfunc [external]
- Options:
tol [real] – optional: tolerance, default 1d-15
info [integer] – optional out: status of the MINPACK routine
icheck [logical] – optional: .true. (default) stops if the status is not a success
maxfev [integer] – optional: maximum number of function evaluations, default 200
- subroutine sf_optimize/brent(func, xmin[, brack, tol, niter])
This subroutine finds the minimum of a function of one variable,
func, with the Brent method (golden section search and parabolic interpolation). The abscissa of the minimum is returned inxmin. The minimum must be bracketed:brackcontains either a bracketing triplet(a,x,b), witha<x<bandfunc(x)lower than bothfunc(a)andfunc(b), or two starting points, from which a bracketing triplet is searched downhill withbracket. Ifbrackis absent the points 0 and 1 are used as starting points. The program stops ifbrackhas a single element.- Parameters:
func [external] – function whose minimum is searched, real(8) function func(x) with real(8) x
xmin [real, inout] – abscissa of the minimum
- Options:
brack (•) [real] – optional: 2 starting points or 3 bracketing points, default [0,1]
tol [real] – optional: fractional precision of the minimum, default 1d-9
niter [integer] – optional: maximum number of iterations, default 200
- subroutine sf_optimize/bracket(ax, bx, cx, fa, fb, fc, func)
Given a function
funcand two distinct initial pointsaxandbx, this subroutine searches in the downhill direction, defined by the function evaluated at the initial points, and returns three pointsax,bx,cxwhich bracket a minimum of the function, i.e.bxis betweenaxandcxandfunc(bx)is lower than bothfunc(ax)andfunc(cx). The values of the function at the three points are returned too.- Parameters:
ax [real, inout] – in: first initial point; out: first point of the bracketing triplet
bx [real, inout] – in: second initial point; out: middle point of the bracketing triplet
cx [real, out] – out: last point of the bracketing triplet
fa [real, out] – out: func(ax)
fb [real, out] – out: func(bx)
fc [real, out] – out: func(cx)
func [external] – function whose minimum is bracketed, real(8) func(x) with real(8) x
- subroutine sf_optimize/fmin(fn, start[, lambda, tol, conv_check, max_fun_calls, fun_calls, num_restart, ierr])
This subroutine minimizes a function of several variables with the Nelder-Mead downhill simplex algorithm (
NELMIN), a simplex function minimisation procedure due to Nelder and Mead (1965), as implemented by O’Neill (Applied Statistics 20, 338, 1971). The convergence check, carried out everyconv_checkiterations, is on the variance of the function values at the vertices of the simplex; if the simplex is found not to be at a minimum it is restarted. The minimum value of the function is not returned.- Parameters:
fn [external] – function to be minimized, real(8) function fn(x) with real(8) x(:)
start (•) [real, in,required] – in: starting point; out: coordinates of the minimum
- Options:
lambda (size(start)) [real] – optional: size and shape of the initial simplex, one step per variable, default 1
tol [real] – optional: terminating limit for the variance of the function values, > 0, default 1d-8
conv_check [integer] – optional: the convergence is checked every conv_check iterations, >= 1, default 10
max_fun_calls [integer] – optional: maximum number of function evaluations, default 500
fun_calls [integer] – optional out: number of function evaluations used
num_restart [integer] – optional out: number of restarts
ierr [integer] – optional out: 0 ok, 1 illegal tol or conv_check, 2 max_fun_calls exceeded
- subroutine sf_optimize/broyden1(ff, x[, icheck, maxits, tol, tol1, tolmin, stpmx, noexit])
This subroutine finds a zero of a system of \(n\) nonlinear equations in \(n\) unknowns, \(f(x)=0\), with the Broyden method and a backtracking line search (Numerical Recipes
broydn): the Jacobian is computed numerically at the start, and then updated with the Broyden formula, restarting from a new numerical Jacobian when the update fails. On outputxcontains the solution. If the Jacobian turns singular the program stops, unlessnoexitis present and true.- Parameters:
ff [real] – system of equations, vector function of x
x (•) [real, inout] – in: starting guess; out: solution, size n
- Options:
icheck [logical] – optional out: .true. if converged to a spurious minimum of \(|f|^2\)
maxits [integer] – optional: maximum number of iterations, default 200
tol [real] – optional: tolerance on the function values, default 1d-8
tol1 [real] – optional: tolerance at the starting point, default 0.01*tol
tolmin [real] – optional: spurious convergence tolerance, default 1d-7
stpmx [real] – optional: scaled maximum step of the line search, default 100
noexit [logical] – optional: .true. returns on singular Jacobian, set .false. if so
- function sf_optimize/brentq(func, a, b[, tol])
This function returns a zero of the function
funcin the interval[a,b], found with the Brent method. The function must change sign in the interval, i.e. the root must be bracketed: otherwise the program stops.- Parameters:
func [external] – function whose zero is searched, real(8) function func(x) with real(8) x
a [real, in] – lower end of the interval
b [real, in] – upper end of the interval
- Options:
tol [real] – optional: tolerance, default epsilon(a)
- Result:
fzero [real] – root of func in [a,b]
- subroutine sf_optimize/bisect(f, x1, x2[, eps, niter, flag])
This subroutine finds a zero of the function
fin the interval[x1,x2]with the bisection method. On outputx1andx2both contain the root. The flagflagis 0 if the function has the same sign at the two ends of the interval (no solution), the number of iterations if a root is found, and minus the number of iterations if the point found is a singularity rather than a root, i.e. \(|f|\ge 1\) there.- Parameters:
f [external] – function whose zero is searched, real(8) function f(x) with real(8) x
x1 [real] – in: lower end of the interval; out: root
x2 [real] – in: upper end of the interval; out: root
- Options:
eps [real] – optional: width of the final interval, default 1d-9
niter [integer] – optional: maximum number of iterations, default 200
flag [integer] – optional out: >0 root found in flag iterations, 0 no solution, <0 singularity
- subroutine sf_optimize/fzero(f, b, c, iflag[, rguess, tol_rel, tol_abs])
This subroutine searches for a zero of the function
fbetween the given valuesbandc, until the width of the interval collapses within the toleranceabs(b-c) <= 2*(rw*abs(b)+ae), withrwthe relative andaethe absolute tolerance. The method is an efficient combination of bisection and the secant rule.Control returns to the user in all cases, therefore
iflagmust be checked after each call. Its possible values are:1:bis within the requested tolerance of a zero. The interval(b,c)collapsed to the requested tolerance, the function changes sign in(b,c), and \(|f(x)|\) decreased as(b,c)collapsed.2: \(f(b)=0\). However, the interval(b,c)may not have collapsed to the requested tolerance.3:bmay be near a singular point of \(f(x)\). The interval(b,c)collapsed to the requested tolerance and the function changes sign in(b,c), but \(|f(x)|\) increased as(b,c)collapsed, i.e. \(\max(|f(b_{in})|,|f(c_{in})|) < |f(b_{out})|\).4: no change in sign of \(f(x)\) was found although the interval(b,c)collapsed to the requested tolerance. The user must examine this case and decide whetherbis near a local minimum of \(f(x)\), or near a zero of even multiplicity, or neither of these.5: too many (more than 500) function evaluations used.
- Parameters:
f [external] – function whose zero is searched, real(8) function f(x) with real(8) x
b [real] – in: one end of the interval; out: best approximation of the zero
c [real] – in: other end of the interval; out: other end of the final interval
iflag [integer] – out: status code, see the list above
- Options:
rguess [real] – optional: a better guess of the zero, default b; it is ignored if not interior to (b,c)
tol_rel [real] – optional: relative error rw of the stopping criterion; if smaller than machine precision it is set to it
tol_abs [real] – optional: absolute error ae of the stopping criterion; if (b,c) contains the origin it should be nonzero
- subroutine sf_optimize/newton(f, xinit[, eps, niter])
This subroutine finds a zero of the function
fwith the Newton method, starting fromxinit, the derivative being evaluated numerically with finite differences. On outputxinitcontains the root.- Parameters:
f [external] – function whose zero is searched, real(8) function f(x) with real(8) x
xinit [real, inout] – in: starting point; out: root
- Options:
eps [real] – optional: tolerance, default 1d-9
niter [integer] – optional: maximum number of iterations, default 200