[Rd] avoiding a needless function evaluation in optimize() (PR#9523)

j.j.goeman at lumc.nl j.j.goeman at lumc.nl
Wed Feb 21 10:37:54 CET 2007

Full_Name: Jelle Goeman
Version: 2.4.0
OS: windows XP
Submission from: (NULL) (


I like to use optimize() to optimize functions whose evaluation is costly in
terms of computation time. The Brent algorithm which is implemented in optimize
was designed to optimize a function with as few function evaluations as
possible. Therefore it bothers me that optimize() always evaluates the function
twice at the optimal value. This can be seen for example by saying:

square <- function(x) {
opt <- optimize(square, c(-5,1), tol=0.1)

Looking at the code of optimize(), I see that the extra function evaluation
comes when optimize returns

list(minimum = val, objective = f(val, ...))

f(val, ...) is calculated, but f(val, ...) has already been calculated at some
point during the algorithm.

Would it be possible to let optimize() store its previous function evaluations
to avoid this unnecessary function evaluation? 

Kind regards,


More information about the R-devel mailing list