-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
47 lines (39 loc) · 1.38 KB
/
index.js
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
'use strict';
var goldenSectionMinimize = require('./src/golden-section-minimize');
var bracketMinimum = require('./src/bracket-minimum');
var bounds = [0, 0];
module.exports = function minimize (f, options, status) {
options = options || {};
var x0;
var tolerance = options.tolerance === undefined ? 1e-8 : options.tolerance;
var dx = options.initialIncrement === undefined ? 1 : options.initialIncrement;
var xMin = options.lowerBound === undefined ? -Infinity : options.lowerBound;
var xMax = options.upperBound === undefined ? Infinity : options.upperBound;
var maxIterations = options.maxIterations === undefined ? 100 : options.maxIterations;
if (status) {
status.iterations = 0;
status.argmin = NaN;
status.minimum = Infinity;
status.converged = false;
}
if (isFinite(xMax) && isFinite(xMin)) {
bounds[0] = xMin;
bounds[1] = xMax;
} else {
// Construct the best guess we can:
if (options.guess === undefined) {
if (xMin > -Infinity) {
x0 = xMax < Infinity ? 0.5 * (xMin + xMax) : xMin;
} else {
x0 = xMax < Infinity ? xMax : 0;
}
} else {
x0 = options.guess;
}
bracketMinimum(bounds, f, x0, dx, xMin, xMax, maxIterations);
if (isNaN(bounds[0]) || isNaN(bounds[1])) {
return NaN;
}
}
return goldenSectionMinimize(f, bounds[0], bounds[1], tolerance, maxIterations, status);
};