brent — find the minimum of a function with Brent's method of golden section search accelerated by inverse quadratic interpolation.


#include <brent.h>
int (objective_t)( double x,
  void * arg,
  double * fx);
extern int brent( objective_t * f,
  void * arg,
  const double bracket[static 3],
  const brent_opt_t * opt,
  double * x0,
  brent_stat_t * stat);
extern const char * brent_strerror ( int errorcode);


The brent function calculates the value x0 which minimises a function f over the range specified by a given bracket.

The parameters, in detail, are:

objective_t* f

A function with prototype objective_t as in the synopsis above, for which the minimiser is sought. With arguments:

double x

the argument of the function f;

void * arg

used to pass other contextual data to the function f;

double * fx

the result of applying function, assigned on success.

The function should return an int which is zero on success and nonzero otherwise.

void * arg

Context which will be passed to the objective function f. This may be NULL if the objective does not use it (to be precise, if it does not dereference it).

double bracket[static 3]

A pointer to an array of three doubles: these should be in increasing order and the outer pair are used to bound the search for a minimiser. The middle value is used as the starting point for the search — if one has no a priori reason to use a particular value then the midpoint is a good choice.

brent_opt_t opt

A pointer to a structure of type brent_opt_t used to control the behaviour of the algorithm. This struct has members

double tolerance

The desired absolute accuracy of the minimiser x0.

size_t max_iterations

The maximum number of iterations to use.

The value NULL is permitted, and in this case sensible defaults are used.

double * x0

A pointer to which the minimising value will be written if the algorithm converges.

brent_stat_t * stat

A pointer (possibly NULL) to a structure used to report details of the algorithm's execution. This struct has members:

double minimum

The value of the objective function f at the minimiser x0.

size_t iterations

The number of iterations used.

size_t evaluations

The number of function evaluations used.

The struct is only written if the algorithm converges (i.e., when BRENT_OK is returned).


On success, returns zero (BRENT_OK) and assigns the results to x0 and (if not NULL) to stat. On error a nonzero value is returned, and the function brent_strerror can be used to obtain a description of the error.


This code is a translation of the Python implementation by Travis E. Oliphant, the translation by J. J. Green.