1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// "Whatever you do, work at it with all your heart, as working for the Lord,
// not for human masters, since you know that you will receive an inheritance
// from the Lord as a reward. It is the Lord Christ you are serving."
// (Col 3:23-24)
use Float;
/// # One Dimensional Optimization
///
/// ## Description:
///
/// The function ‘optimize’ searches the interval from ‘lower’ to
/// ‘upper’ for a minimum or maximum of the function ‘f’ with respect
/// to its first argument.
///
/// ‘optimise’ is an alias for ‘optimize’.
///
/// ## Usage:
///
/// ```r
/// optimize(f, interval, ..., lower = min(interval), upper = max(interval),
/// maximum = FALSE,
/// tol = .Machine$double.eps^0.25)
/// optimise(f, interval, ..., lower = min(interval), upper = max(interval),
/// maximum = FALSE,
/// tol = .Machine$double.eps^0.25)
/// ```
///
/// ## Arguments:
///
/// * f: the function to be optimized. The function is either
/// minimized or maximized over its first argument depending on
/// the value of ‘maximum’.
/// * interval: a vector containing the end-points of the interval to be
/// searched for the minimum.
/// * ...: additional named or unnamed arguments to be passed to ‘f’.
/// * lower: the lower end point of the interval to be searched.
/// * upper: the upper end point of the interval to be searched.
/// * maximum: logical. Should we maximize or minimize (the default)?
/// * tol: the desired accuracy.
///
/// ## Details:
///
/// Note that arguments after ‘...’ must be matched exactly.
///
/// The method used is a combination of golden section search and
/// successive parabolic interpolation, and was designed for use with
/// continuous functions. Convergence is never much slower than that
/// for a Fibonacci search. If ‘f’ has a continuous second derivative
/// which is positive at the minimum (which is not at ‘lower’ or
/// ‘upper’), then convergence is superlinear, and usually of the
/// order of about 1.324.
///
/// The function ‘f’ is never evaluated at two points closer together
/// than eps * |x_0| + (tol/3), where eps is approximately
/// ‘sqrt(.Machine$double.eps)’ and x_0 is the final abscissa
/// ‘optimize()$minimum’.
/// If ‘f’ is a unimodal function and the computed values of ‘f’ are
/// always unimodal when separated by at least eps * |x| + (tol/3),
/// then x_0 approximates the abscissa of the global minimum of ‘f’ on
/// the interval ‘lower,upper’ with an error less than eps * |x_0|+
/// tol.
/// If ‘f’ is not unimodal, then ‘optimize()’ may approximate a local,
/// but perhaps non-global, minimum to the same accuracy.
///
/// The first evaluation of ‘f’ is always at x_1 = a + (1-phi)(b-a)
/// where ‘(a,b) = (lower, upper)’ and phi = (sqrt(5) - 1)/2 =
/// 0.61803.. is the golden section ratio. Almost always, the second
/// evaluation is at x_2 = a + phi(b-a). Note that a local minimum
/// inside \[x_1,x_2\] will be found as solution, even when ‘f’ is
/// constant in there, see the last example.
///
/// ‘f’ will be called as ‘f(x, ...)’ for a numeric value of x.
///
/// The argument passed to ‘f’ has special semantics and used to be
/// shared between calls. The function should not copy it.
///
/// ## Value:
///
/// A list with components ‘minimum’ (or ‘maximum’) and ‘objective’
/// which give the location of the minimum (or maximum) and the value
/// of the function at that point.
///
/// ## Source:
///
/// A C translation of Fortran code
/// <https://www.netlib.org/fmm/fmin.f> (author(s) unstated) based on
/// the Algol 60 procedure ‘localmin’ given in the reference.
///
/// ## References:
///
/// Brent, R. (1973) _Algorithms for Minimization without
/// Derivatives._ Englewood Cliffs N.J.: Prentice-Hall.
///
/// ## See Also:
///
/// ‘nlm’, ‘uniroot’.
///
/// ## Examples:
///
/// ```r
/// require(graphics)
///
/// f <- function (x, a) (x - a)^2
/// xmin <- optimize(f, c(0, 1), tol = 0.0001, a = 1/3)
/// xmin
///
/// ## See where the function is evaluated:
/// optimize(function(x) x^2*(print(x)-1), lower = 0, upper = 10)
///
/// ## "wrong" solution with unlucky interval and piecewise constant f():
/// f <- function(x) ifelse(x > -1, ifelse(x < 4, exp(-1/abs(x - 1)), 10), 10)
/// fp <- function(x) { print(x); f(x) }
///
/// plot(f, -2,5, ylim = 0:1, col = 2)
/// optimize(fp, c(-4, 20))# doesn't see the minimum
/// optimize(fp, c(-7, 20))# ok
/// ```