@ac-kit/math-analysis 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +89 -0
- package/README.md +85 -2
- package/dist/differentiation/finite-difference.d.ts +41 -0
- package/dist/differentiation/finite-difference.d.ts.map +1 -0
- package/dist/differentiation/finite-difference.js +53 -0
- package/dist/differentiation/finite-difference.js.map +1 -0
- package/dist/differentiation/gradient.d.ts +73 -0
- package/dist/differentiation/gradient.d.ts.map +1 -0
- package/dist/differentiation/gradient.js +167 -0
- package/dist/differentiation/gradient.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -1
- package/dist/interpolation/barycentric-interpolator.d.ts +29 -0
- package/dist/interpolation/barycentric-interpolator.d.ts.map +1 -0
- package/dist/interpolation/barycentric-interpolator.js +94 -0
- package/dist/interpolation/barycentric-interpolator.js.map +1 -0
- package/dist/legendre/_legendre-kernel.d.ts +22 -0
- package/dist/legendre/_legendre-kernel.d.ts.map +1 -0
- package/dist/legendre/_legendre-kernel.js +232 -0
- package/dist/legendre/_legendre-kernel.js.map +1 -0
- package/dist/legendre/legendre-fully-normalized.d.ts +52 -0
- package/dist/legendre/legendre-fully-normalized.d.ts.map +1 -0
- package/dist/legendre/legendre-fully-normalized.js +62 -0
- package/dist/legendre/legendre-fully-normalized.js.map +1 -0
- package/dist/legendre/legendre-index.d.ts +27 -0
- package/dist/legendre/legendre-index.d.ts.map +1 -0
- package/dist/legendre/legendre-index.js +31 -0
- package/dist/legendre/legendre-index.js.map +1 -0
- package/dist/legendre/legendre-schmidt.d.ts +37 -0
- package/dist/legendre/legendre-schmidt.d.ts.map +1 -0
- package/dist/legendre/legendre-schmidt.js +47 -0
- package/dist/legendre/legendre-schmidt.js.map +1 -0
- package/dist/ode/runge-kutta4.d.ts +46 -0
- package/dist/ode/runge-kutta4.d.ts.map +1 -0
- package/dist/ode/runge-kutta4.js +85 -0
- package/dist/ode/runge-kutta4.js.map +1 -0
- package/dist/optimize/_more-thuente.d.ts +95 -0
- package/dist/optimize/_more-thuente.d.ts.map +1 -0
- package/dist/optimize/_more-thuente.js +333 -0
- package/dist/optimize/_more-thuente.js.map +1 -0
- package/dist/optimize/_wolfe-line-search.d.ts +85 -0
- package/dist/optimize/_wolfe-line-search.d.ts.map +1 -0
- package/dist/optimize/_wolfe-line-search.js +193 -0
- package/dist/optimize/_wolfe-line-search.js.map +1 -0
- package/dist/optimize/bfgs.d.ts +262 -0
- package/dist/optimize/bfgs.d.ts.map +1 -0
- package/dist/optimize/bfgs.js +418 -0
- package/dist/optimize/bfgs.js.map +1 -0
- package/dist/optimize/lbfgs.d.ts +199 -0
- package/dist/optimize/lbfgs.d.ts.map +1 -0
- package/dist/optimize/lbfgs.js +383 -0
- package/dist/optimize/lbfgs.js.map +1 -0
- package/dist/optimize/levenberg-marquardt.d.ts +78 -0
- package/dist/optimize/levenberg-marquardt.d.ts.map +1 -0
- package/dist/optimize/levenberg-marquardt.js +204 -0
- package/dist/optimize/levenberg-marquardt.js.map +1 -0
- package/dist/optimize/nelder-mead.d.ts +68 -0
- package/dist/optimize/nelder-mead.d.ts.map +1 -0
- package/dist/optimize/nelder-mead.js +198 -0
- package/dist/optimize/nelder-mead.js.map +1 -0
- package/dist/quadrature/adaptive-quadrature.d.ts +53 -0
- package/dist/quadrature/adaptive-quadrature.d.ts.map +1 -0
- package/dist/quadrature/adaptive-quadrature.js +175 -0
- package/dist/quadrature/adaptive-quadrature.js.map +1 -0
- package/dist/quadrature/gauss-legendre.d.ts +27 -0
- package/dist/quadrature/gauss-legendre.d.ts.map +1 -0
- package/dist/quadrature/gauss-legendre.js +111 -0
- package/dist/quadrature/gauss-legendre.js.map +1 -0
- package/dist/quadrature/simpsons-rule.d.ts +27 -0
- package/dist/quadrature/simpsons-rule.d.ts.map +1 -0
- package/dist/quadrature/simpsons-rule.js +44 -0
- package/dist/quadrature/simpsons-rule.js.map +1 -0
- package/dist/roots/_grown.d.ts.map +1 -1
- package/dist/roots/_grown.js +0 -1
- package/dist/roots/_grown.js.map +1 -1
- package/dist/roots/_level-search.d.ts +19 -2
- package/dist/roots/_level-search.d.ts.map +1 -1
- package/dist/roots/_level-search.js +47 -23
- package/dist/roots/_level-search.js.map +1 -1
- package/dist/roots/find-level-bands.js +1 -1
- package/dist/roots/find-level-bands.js.map +1 -1
- package/dist/roots/find-level-crossings.d.ts +4 -2
- package/dist/roots/find-level-crossings.d.ts.map +1 -1
- package/dist/roots/find-level-crossings.js +4 -10
- package/dist/roots/find-level-crossings.js.map +1 -1
- package/dist/roots/newton-raphson.d.ts +31 -0
- package/dist/roots/newton-raphson.d.ts.map +1 -0
- package/dist/roots/newton-raphson.js +69 -0
- package/dist/roots/newton-raphson.js.map +1 -0
- package/dist/series/sum-series.d.ts +43 -0
- package/dist/series/sum-series.d.ts.map +1 -0
- package/dist/series/sum-series.js +70 -0
- package/dist/series/sum-series.js.map +1 -0
- package/package.json +8 -7
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { type Callable, type WorkBudget } from "@ac-kit/core";
|
|
2
|
+
import { MoreThuente_ } from "./_more-thuente.js";
|
|
3
|
+
/** The objective of the gradient minimisers: `f(point)`, `∇f` into `gradient`. */
|
|
4
|
+
export type GradientObjective_ = Callable<[
|
|
5
|
+
point: Float64Array,
|
|
6
|
+
gradient: Float64Array
|
|
7
|
+
], number>;
|
|
8
|
+
/** The default `gradientTolerance` of the gradient minimisers. */
|
|
9
|
+
export declare const DEFAULT_GRADIENT_TOLERANCE_ = 1e-8;
|
|
10
|
+
/** The default `maxIterations` of the gradient minimisers. */
|
|
11
|
+
export declare const DEFAULT_MAX_ITERATIONS_ = 1000;
|
|
12
|
+
/** The line-search options shared by the gradient minimisers. */
|
|
13
|
+
export type LineSearchOptions_ = {
|
|
14
|
+
readonly sufficientDecrease?: number;
|
|
15
|
+
readonly curvature?: number;
|
|
16
|
+
readonly maxLineSearchEvaluations?: number;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* `value ?? fallback`, checked finite and non-negative.
|
|
20
|
+
*
|
|
21
|
+
* @throws {RangeError} Naming `owner` and `name` otherwise.
|
|
22
|
+
*/
|
|
23
|
+
export declare function nonNegativeOption_(value: number | undefined, fallback: number, owner: string, name: string): number;
|
|
24
|
+
/**
|
|
25
|
+
* The line search of the gradient minimisers, over `dimension` variables: the
|
|
26
|
+
* Moré–Thuente search ({@link MoreThuente_}) driven along a direction, with
|
|
27
|
+
* Hager and Zhang's approximate Wolfe acceptance where `f` no longer resolves a
|
|
28
|
+
* decrease, step halving on non-finite trials, and a fall-back on the lowest
|
|
29
|
+
* point evaluated. It also owns the run's objective, budget, signal and
|
|
30
|
+
* evaluation count, so that every evaluation of a minimiser goes through
|
|
31
|
+
* {@link evaluate}.
|
|
32
|
+
*
|
|
33
|
+
* Numbers pass through fields, never boxed arguments or results.
|
|
34
|
+
*/
|
|
35
|
+
export declare class WolfeLineSearch_ {
|
|
36
|
+
/** The step-length engine; its `stp` is the first trial step of a search. */
|
|
37
|
+
readonly engine: MoreThuente_;
|
|
38
|
+
/** The last point evaluated; after a successful search, the accepted one. */
|
|
39
|
+
readonly trial: Float64Array;
|
|
40
|
+
/** The gradient at {@link trial}. */
|
|
41
|
+
readonly trialGradient: Float64Array;
|
|
42
|
+
/** Evaluations one search may use. */
|
|
43
|
+
readonly maxEvaluations: number;
|
|
44
|
+
/** The objective of the current run. */
|
|
45
|
+
objective: GradientObjective_;
|
|
46
|
+
/** Charged one unit per evaluation. */
|
|
47
|
+
budget: WorkBudget | undefined;
|
|
48
|
+
/** Checked before every evaluation. */
|
|
49
|
+
signal: AbortSignal | null | undefined;
|
|
50
|
+
/** Evaluations since the run began. */
|
|
51
|
+
evaluations: number;
|
|
52
|
+
/** `f` at the last evaluation; after a successful search, at {@link trial}. */
|
|
53
|
+
value: number;
|
|
54
|
+
/** `f` at the search's start, set by the caller before {@link search}. */
|
|
55
|
+
startValue: number;
|
|
56
|
+
/** `φ′(0) < 0` along the direction, set by the caller before {@link search}. */
|
|
57
|
+
slope: number;
|
|
58
|
+
private readonly best_;
|
|
59
|
+
private readonly bestGradient_;
|
|
60
|
+
private bestValue_;
|
|
61
|
+
/**
|
|
62
|
+
* @throws {RangeError} When `maxLineSearchEvaluations` is not a positive
|
|
63
|
+
* integer, or not `0 < sufficientDecrease < curvature < 1` (naming
|
|
64
|
+
* `owner`).
|
|
65
|
+
*/
|
|
66
|
+
constructor(dimension: number, options: LineSearchOptions_ | undefined, owner: string);
|
|
67
|
+
/** Starts a run of `objective`. */
|
|
68
|
+
begin(objective: GradientObjective_, budget: WorkBudget | undefined, signal: AbortSignal | null | undefined): void;
|
|
69
|
+
/** Ends a run, dropping the references it held. */
|
|
70
|
+
end(): void;
|
|
71
|
+
/** One evaluation at `at`, the gradient into `into`, `f` into `value`. */
|
|
72
|
+
evaluate(at: Float64Array, into: Float64Array): void;
|
|
73
|
+
/** Whether {@link value} and every entry of `gradient` are finite. */
|
|
74
|
+
finiteAt(gradient: Float64Array): boolean;
|
|
75
|
+
/**
|
|
76
|
+
* Searches along `direction` from `point` (value {@link startValue}, slope
|
|
77
|
+
* {@link slope} `< 0`) from the step in `engine.stp`. On success the accepted
|
|
78
|
+
* point is in {@link trial}, {@link trialGradient} and {@link value}.
|
|
79
|
+
*
|
|
80
|
+
* @returns Whether a step lowering `f` (or meeting the approximate Wolfe
|
|
81
|
+
* conditions) was found.
|
|
82
|
+
*/
|
|
83
|
+
search(point: Float64Array, direction: Float64Array): boolean;
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=_wolfe-line-search.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_wolfe-line-search.d.ts","sourceRoot":"","sources":["../../src/optimize/_wolfe-line-search.ts"],"names":[],"mappings":"AAAA,OAAO,EAEN,KAAK,QAAQ,EAEb,KAAK,UAAU,EACf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E,KAAK,SAAO;IAEnB,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAe;IACrC,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAe;IAC7C,OAAO,CAAC,UAAU,CAAO;IAEzB;;;;OAIG;IACH,YACC,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,kBAAkB,GAAG,SAAS,EACvC,KAAK,EAAE,MAAM,EAwBb;IAED,mCAAmC;IAC5B,KAAK,CACX,SAAS,EAAE,kBAAkB,EAC7B,MAAM,EAAE,UAAU,GAAG,SAAS,EAC9B,MAAM,EAAE,WAAW,GAAG,IAAI,GAAG,SAAS,GACpC,IAAI,CAKN;IAED,mDAAmD;IAC5C,GAAG,IAAI,IAAI,CAIjB;IAED,0EAA0E;IACnE,QAAQ,CAAC,EAAE,EAAE,YAAY,EAAE,IAAI,EAAE,YAAY,GAAG,IAAI,CAK1D;IAED,sEAAsE;IAC/D,QAAQ,CAAC,QAAQ,EAAE,YAAY,GAAG,OAAO,CAc/C;IAED;;;;;;;OAOG;IACI,MAAM,CAAC,KAAK,EAAE,YAAY,EAAE,SAAS,EAAE,YAAY,GAAG,OAAO,CAwEnE;CACD"}
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { assertInteger, describeValue, } from "@ac-kit/core";
|
|
2
|
+
import { MoreThuente_, SEARCH_CONTINUE_, SEARCH_CONVERGED_, } from "./_more-thuente.js";
|
|
3
|
+
/**
|
|
4
|
+
* A change of `f` within this many of its ulps is rounding, not decrease: the
|
|
5
|
+
* line search then judges the step by its slope alone.
|
|
6
|
+
*/
|
|
7
|
+
const NOISE_ULPS_ = 16;
|
|
8
|
+
const DEFAULT_MAX_LINE_SEARCH_EVALUATIONS_ = 20;
|
|
9
|
+
/** The default `gradientTolerance` of the gradient minimisers. */
|
|
10
|
+
export const DEFAULT_GRADIENT_TOLERANCE_ = 1e-8;
|
|
11
|
+
/** The default `maxIterations` of the gradient minimisers. */
|
|
12
|
+
export const DEFAULT_MAX_ITERATIONS_ = 1000;
|
|
13
|
+
function noObjective_() {
|
|
14
|
+
return Number.NaN;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* `value ?? fallback`, checked finite and non-negative.
|
|
18
|
+
*
|
|
19
|
+
* @throws {RangeError} Naming `owner` and `name` otherwise.
|
|
20
|
+
*/
|
|
21
|
+
export function nonNegativeOption_(value, fallback, owner, name) {
|
|
22
|
+
const resolved = value ?? fallback;
|
|
23
|
+
if (!(resolved >= 0 && resolved < Number.POSITIVE_INFINITY)) {
|
|
24
|
+
throw new RangeError(`${owner}: ${name} must be finite and non-negative, got ${describeValue(resolved)}`);
|
|
25
|
+
}
|
|
26
|
+
return resolved;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The line search of the gradient minimisers, over `dimension` variables: the
|
|
30
|
+
* Moré–Thuente search ({@link MoreThuente_}) driven along a direction, with
|
|
31
|
+
* Hager and Zhang's approximate Wolfe acceptance where `f` no longer resolves a
|
|
32
|
+
* decrease, step halving on non-finite trials, and a fall-back on the lowest
|
|
33
|
+
* point evaluated. It also owns the run's objective, budget, signal and
|
|
34
|
+
* evaluation count, so that every evaluation of a minimiser goes through
|
|
35
|
+
* {@link evaluate}.
|
|
36
|
+
*
|
|
37
|
+
* Numbers pass through fields, never boxed arguments or results.
|
|
38
|
+
*/
|
|
39
|
+
export class WolfeLineSearch_ {
|
|
40
|
+
/** The step-length engine; its `stp` is the first trial step of a search. */
|
|
41
|
+
engine = new MoreThuente_();
|
|
42
|
+
/** The last point evaluated; after a successful search, the accepted one. */
|
|
43
|
+
trial;
|
|
44
|
+
/** The gradient at {@link trial}. */
|
|
45
|
+
trialGradient;
|
|
46
|
+
/** Evaluations one search may use. */
|
|
47
|
+
maxEvaluations;
|
|
48
|
+
/** The objective of the current run. */
|
|
49
|
+
objective = noObjective_;
|
|
50
|
+
/** Charged one unit per evaluation. */
|
|
51
|
+
budget = undefined;
|
|
52
|
+
/** Checked before every evaluation. */
|
|
53
|
+
signal = undefined;
|
|
54
|
+
/** Evaluations since the run began. */
|
|
55
|
+
evaluations = 0;
|
|
56
|
+
/** `f` at the last evaluation; after a successful search, at {@link trial}. */
|
|
57
|
+
value = 0.5;
|
|
58
|
+
/** `f` at the search's start, set by the caller before {@link search}. */
|
|
59
|
+
startValue = 0.5;
|
|
60
|
+
/** `φ′(0) < 0` along the direction, set by the caller before {@link search}. */
|
|
61
|
+
slope = 0.5;
|
|
62
|
+
best_;
|
|
63
|
+
bestGradient_;
|
|
64
|
+
bestValue_ = 0.5;
|
|
65
|
+
/**
|
|
66
|
+
* @throws {RangeError} When `maxLineSearchEvaluations` is not a positive
|
|
67
|
+
* integer, or not `0 < sufficientDecrease < curvature < 1` (naming
|
|
68
|
+
* `owner`).
|
|
69
|
+
*/
|
|
70
|
+
constructor(dimension, options, owner) {
|
|
71
|
+
this.maxEvaluations =
|
|
72
|
+
options?.maxLineSearchEvaluations ?? DEFAULT_MAX_LINE_SEARCH_EVALUATIONS_;
|
|
73
|
+
assertInteger(this.maxEvaluations, "maxLineSearchEvaluations", 1);
|
|
74
|
+
const sufficientDecrease = options?.sufficientDecrease ?? 1e-4;
|
|
75
|
+
const curvature = options?.curvature ?? 0.9;
|
|
76
|
+
if (!(sufficientDecrease > 0 &&
|
|
77
|
+
sufficientDecrease < curvature &&
|
|
78
|
+
curvature < 1)) {
|
|
79
|
+
throw new RangeError(`${owner}: needs 0 < sufficientDecrease < curvature < 1, got ${describeValue(sufficientDecrease)} and ${describeValue(curvature)}`);
|
|
80
|
+
}
|
|
81
|
+
this.engine.ftol = sufficientDecrease;
|
|
82
|
+
this.engine.gtol = curvature;
|
|
83
|
+
this.trial = new Float64Array(dimension);
|
|
84
|
+
this.trialGradient = new Float64Array(dimension);
|
|
85
|
+
this.best_ = new Float64Array(dimension);
|
|
86
|
+
this.bestGradient_ = new Float64Array(dimension);
|
|
87
|
+
}
|
|
88
|
+
/** Starts a run of `objective`. */
|
|
89
|
+
begin(objective, budget, signal) {
|
|
90
|
+
this.objective = objective;
|
|
91
|
+
this.budget = budget;
|
|
92
|
+
this.signal = signal;
|
|
93
|
+
this.evaluations = 0;
|
|
94
|
+
}
|
|
95
|
+
/** Ends a run, dropping the references it held. */
|
|
96
|
+
end() {
|
|
97
|
+
this.objective = noObjective_;
|
|
98
|
+
this.budget = undefined;
|
|
99
|
+
this.signal = undefined;
|
|
100
|
+
}
|
|
101
|
+
/** One evaluation at `at`, the gradient into `into`, `f` into `value`. */
|
|
102
|
+
evaluate(at, into) {
|
|
103
|
+
this.signal?.throwIfAborted();
|
|
104
|
+
this.budget?.spend(1);
|
|
105
|
+
this.evaluations++;
|
|
106
|
+
this.value = this.objective(at, into);
|
|
107
|
+
}
|
|
108
|
+
/** Whether {@link value} and every entry of `gradient` are finite. */
|
|
109
|
+
finiteAt(gradient) {
|
|
110
|
+
const value = this.value;
|
|
111
|
+
if (value - value !== 0) {
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
for (let index = 0; index < gradient.length; index++) {
|
|
115
|
+
const entry = gradient[index];
|
|
116
|
+
if (entry - entry !== 0) {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Searches along `direction` from `point` (value {@link startValue}, slope
|
|
124
|
+
* {@link slope} `< 0`) from the step in `engine.stp`. On success the accepted
|
|
125
|
+
* point is in {@link trial}, {@link trialGradient} and {@link value}.
|
|
126
|
+
*
|
|
127
|
+
* @returns Whether a step lowering `f` (or meeting the approximate Wolfe
|
|
128
|
+
* conditions) was found.
|
|
129
|
+
*/
|
|
130
|
+
search(point, direction) {
|
|
131
|
+
const dimension = point.length;
|
|
132
|
+
const engine = this.engine;
|
|
133
|
+
const slope = this.slope;
|
|
134
|
+
const trial = this.trial;
|
|
135
|
+
const trialGradient = this.trialGradient;
|
|
136
|
+
const best = this.best_;
|
|
137
|
+
const bestGradient = this.bestGradient_;
|
|
138
|
+
const start = this.startValue;
|
|
139
|
+
const noise = NOISE_ULPS_ * Number.EPSILON * Math.abs(start);
|
|
140
|
+
engine.value = start;
|
|
141
|
+
engine.slope = slope;
|
|
142
|
+
engine.start();
|
|
143
|
+
this.bestValue_ = start;
|
|
144
|
+
for (let evaluation = 0; evaluation < this.maxEvaluations; evaluation++) {
|
|
145
|
+
const length = engine.stp;
|
|
146
|
+
for (let index = 0; index < dimension; index++) {
|
|
147
|
+
trial[index] = point[index] + length * direction[index];
|
|
148
|
+
}
|
|
149
|
+
this.evaluate(trial, trialGradient);
|
|
150
|
+
const value = this.value;
|
|
151
|
+
let trialSlope = 0;
|
|
152
|
+
for (let index = 0; index < dimension; index++) {
|
|
153
|
+
trialSlope += trialGradient[index] * direction[index];
|
|
154
|
+
}
|
|
155
|
+
if (value - value !== 0 || trialSlope - trialSlope !== 0) {
|
|
156
|
+
engine.retreat();
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
if (value < this.bestValue_) {
|
|
160
|
+
this.bestValue_ = value;
|
|
161
|
+
best.set(trial);
|
|
162
|
+
bestGradient.set(trialGradient);
|
|
163
|
+
}
|
|
164
|
+
// Hager and Zhang's approximate Wolfe conditions (SIAM J. Optim.
|
|
165
|
+
// 16(1), 2005) where `f` no longer resolves the decrease — within a
|
|
166
|
+
// few of its ulps of the start, as near a minimum — while the slope
|
|
167
|
+
// still does: `c₂·φ′(0) ≤ φ′(α) ≤ (2c₁ − 1)·φ′(0)`.
|
|
168
|
+
if (Math.abs(value - start) <= noise &&
|
|
169
|
+
trialSlope >= engine.gtol * slope &&
|
|
170
|
+
trialSlope <= (2 * engine.ftol - 1) * slope) {
|
|
171
|
+
return true;
|
|
172
|
+
}
|
|
173
|
+
engine.value = value;
|
|
174
|
+
engine.slope = trialSlope;
|
|
175
|
+
const code = engine.iterate();
|
|
176
|
+
if (code === SEARCH_CONVERGED_) {
|
|
177
|
+
return true;
|
|
178
|
+
}
|
|
179
|
+
if (code !== SEARCH_CONTINUE_) {
|
|
180
|
+
break;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
// Out of evaluations or stalled: the lowest point, if it is lower.
|
|
184
|
+
if (!(this.bestValue_ < start)) {
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
trial.set(best);
|
|
188
|
+
trialGradient.set(bestGradient);
|
|
189
|
+
this.value = this.bestValue_;
|
|
190
|
+
return true;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
//# sourceMappingURL=_wolfe-line-search.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_wolfe-line-search.js","sourceRoot":"","sources":["../../src/optimize/_wolfe-line-search.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,aAAa,EAEb,aAAa,GAEb,MAAM,cAAc,CAAC;AAEtB,OAAO,EACN,YAAY,EACZ,gBAAgB,EAChB,iBAAiB,GACjB,MAAM,oBAAoB,CAAC;AAQ5B;;;GAGG;AACH,MAAM,WAAW,GAAG,EAAE,CAAC;AAEvB,MAAM,oCAAoC,GAAG,EAAE,CAAC;AAEhD,kEAAkE;AAClE,MAAM,CAAC,MAAM,2BAA2B,GAAG,IAAI,CAAC;AAEhD,8DAA8D;AAC9D,MAAM,CAAC,MAAM,uBAAuB,GAAG,IAAI,CAAC;AAE5C,SAAS,YAAY;IACpB,OAAO,MAAM,CAAC,GAAG,CAAC;AACnB,CAAC;AASD;;;;GAIG;AACH,MAAM,UAAU,kBAAkB,CACjC,KAAyB,EACzB,QAAgB,EAChB,KAAa,EACb,IAAY;IAEZ,MAAM,QAAQ,GAAG,KAAK,IAAI,QAAQ,CAAC;IACnC,IAAI,CAAC,CAAC,QAAQ,IAAI,CAAC,IAAI,QAAQ,GAAG,MAAM,CAAC,iBAAiB,CAAC,EAAE,CAAC;QAC7D,MAAM,IAAI,UAAU,CACnB,GAAG,KAAK,KAAK,IAAI,yCAAyC,aAAa,CAAC,QAAQ,CAAC,EAAE,CACnF,CAAC;IACH,CAAC;IAC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AC1B,MAAM,IAAI,GAAG,MAAM,CAAC,OAAO,EAAE,CAAC;YAE9B,IAAI,IAAI,KAAK,iBAAiB,EAAE,CAAC;gBAChC,OAAO,IAAI,CAAC;YACb,CAAC;YACD,IAAI,IAAI,KAAK,gBAAgB,EAAE,CAAC;gBAC/B,MAAM;YACP,CAAC;QACF,CAAC;QAED,mEAAmE;QACnE,IAAI,CAAC,CAAC,IAAI,CAAC,UAAU,GAAG,KAAK,CAAC,EAAE,CAAC;YAChC,OAAO,KAAK,CAAC;QACd,CAAC;QACD,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QAChB,aAAa,CAAC,GAAG,CAAC,YAAY,CAAC,CAAC;QAChC,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,UAAU,CAAC;QAC7B,OAAO,IAAI,CAAC;IACb,CAAC;CACD"}
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import { type Callable, type WorkBudget } from "@ac-kit/core";
|
|
2
|
+
import { type Matrix } from "@ac-kit/math-linear";
|
|
3
|
+
/**
|
|
4
|
+
* The function {@link BfgsMinimizer} minimises: its value at `point`, and its
|
|
5
|
+
* gradient written into `gradient` (every entry, each call). Analytic
|
|
6
|
+
* gradients, or any the caller computes (automatic or finite differences).
|
|
7
|
+
*
|
|
8
|
+
* `point` is the minimiser's own array: read it, never write or keep it.
|
|
9
|
+
*/
|
|
10
|
+
export type BfgsObjective = Callable<[
|
|
11
|
+
point: Float64Array,
|
|
12
|
+
gradient: Float64Array
|
|
13
|
+
], number>;
|
|
14
|
+
/**
|
|
15
|
+
* Why a minimisation stopped. The first three are convergence; the others leave
|
|
16
|
+
* the best point found, whose quality the caller must judge.
|
|
17
|
+
*
|
|
18
|
+
* - `"gradient"`: `‖∇f‖∞ ≤ gradientTolerance`.
|
|
19
|
+
* - `"value"`: the decrease of the last step fell to `valueTolerance` relative.
|
|
20
|
+
* - `"step"`: the last step fell to `stepTolerance` relative.
|
|
21
|
+
* - `"max-iterations"`: `maxIterations` steps were taken first.
|
|
22
|
+
* - `"line-search"`: no step along the quasi-Newton direction, then along the
|
|
23
|
+
* steepest descent, lowered `f` — usually the precision floor of `f` near a
|
|
24
|
+
* minimum, sometimes a gradient inconsistent with `f`.
|
|
25
|
+
*/
|
|
26
|
+
export type BfgsStatus = "gradient" | "value" | "step" | "max-iterations" | "line-search";
|
|
27
|
+
/**
|
|
28
|
+
* The stopping rules and line-search constants of a {@link BfgsMinimizer}, fixed
|
|
29
|
+
* when it is made.
|
|
30
|
+
*/
|
|
31
|
+
export type BfgsOptions = {
|
|
32
|
+
/**
|
|
33
|
+
* Converged once `max |∂f/∂xᵢ| ≤ gradientTolerance` (an absolute test, in the
|
|
34
|
+
* units of `f` over those of `x`). Finite, non-negative; `1e-8` by default.
|
|
35
|
+
*/
|
|
36
|
+
readonly gradientTolerance?: number;
|
|
37
|
+
/**
|
|
38
|
+
* Converged once a step lowers `f` by at most `valueTolerance · max(|f|, |f
|
|
39
|
+
* after|, 1)` (SciPy's `ftol` rule). Finite, non-negative; `0`, the default,
|
|
40
|
+
* disables the test.
|
|
41
|
+
*/
|
|
42
|
+
readonly valueTolerance?: number;
|
|
43
|
+
/**
|
|
44
|
+
* Converged once a step moves no coordinate by more than `stepTolerance ·
|
|
45
|
+
* max(‖x‖∞, 1)`. Finite, non-negative; `0`, the default, disables the test.
|
|
46
|
+
*/
|
|
47
|
+
readonly stepTolerance?: number;
|
|
48
|
+
/**
|
|
49
|
+
* Steps (accepted line searches) at most; a positive integer, 1000 by
|
|
50
|
+
* default.
|
|
51
|
+
*/
|
|
52
|
+
readonly maxIterations?: number;
|
|
53
|
+
/**
|
|
54
|
+
* The sufficient-decrease constant `c₁` of the Wolfe conditions, `0 < c₁ <
|
|
55
|
+
* c₂`; `1e-4` by default.
|
|
56
|
+
*/
|
|
57
|
+
readonly sufficientDecrease?: number;
|
|
58
|
+
/**
|
|
59
|
+
* The curvature constant `c₂` of the strong Wolfe conditions, `c₁ < c₂ < 1`;
|
|
60
|
+
* `0.9` by default (Nocedal and Wright's choice for quasi-Newton methods).
|
|
61
|
+
* Smaller values search each line more exactly.
|
|
62
|
+
*/
|
|
63
|
+
readonly curvature?: number;
|
|
64
|
+
/**
|
|
65
|
+
* Evaluations one line search may use; a positive integer, 20 by default. A
|
|
66
|
+
* search that runs out keeps its best point if it lowered `f`.
|
|
67
|
+
*/
|
|
68
|
+
readonly maxLineSearchEvaluations?: number;
|
|
69
|
+
};
|
|
70
|
+
/** What one {@link BfgsMinimizer.minimize} call may also be given. */
|
|
71
|
+
export type BfgsRunOptions = {
|
|
72
|
+
/**
|
|
73
|
+
* The starting inverse Hessian approximation, `dimension × dimension`,
|
|
74
|
+
* symmetric positive definite (only finiteness is checked; a matrix that is
|
|
75
|
+
* not positive definite is replaced, as described on the class), copied.
|
|
76
|
+
* Absent: the identity, rescaled after the first step by `yᵀs / yᵀy` (Nocedal
|
|
77
|
+
* and Wright, eq. 6.20). A previous run's {@link BfgsMinimizer.inverseHessian}
|
|
78
|
+
* warm-starts a related problem.
|
|
79
|
+
*/
|
|
80
|
+
readonly initialInverseHessian?: Matrix | null;
|
|
81
|
+
/**
|
|
82
|
+
* Charged one unit per evaluation of the objective; what it throws
|
|
83
|
+
* propagates.
|
|
84
|
+
*/
|
|
85
|
+
readonly budget?: WorkBudget;
|
|
86
|
+
/** Checked before every evaluation; aborting throws its reason. */
|
|
87
|
+
readonly signal?: AbortSignal | null;
|
|
88
|
+
};
|
|
89
|
+
/**
|
|
90
|
+
* Minimises a smooth function of `dimension` variables by the BFGS quasi-Newton
|
|
91
|
+
* method (Broyden, Fletcher, Goldfarb and Shanno, 1970) with a line search
|
|
92
|
+
* satisfying the strong Wolfe conditions (Moré and Thuente 1994, MINPACK-2's
|
|
93
|
+
* `dcsrch`): the method of SciPy's `minimize(method="BFGS")` and Optim.jl's
|
|
94
|
+
* `BFGS`, for problems of up to a few hundred variables.
|
|
95
|
+
*
|
|
96
|
+
* Make one per dimension and options, then call {@link minimize} as often as
|
|
97
|
+
* needed: after the constructor nothing is allocated (when the objective
|
|
98
|
+
* allocates nothing itself). The result is left in fields, read-only for
|
|
99
|
+
* callers: {@link value}, {@link gradient}, {@link inverseHessian},
|
|
100
|
+
* {@link iterations}, {@link evaluations}, {@link status}.
|
|
101
|
+
*
|
|
102
|
+
* **Method.** Each step moves along `d = −H·∇f` by a step length satisfying
|
|
103
|
+
* `f(x + αd) ≤ f(x) + c₁·α·∇fᵀd` and `|∇f(x + αd)ᵀd| ≤ c₂·|∇fᵀd|`, then updates
|
|
104
|
+
* the inverse Hessian approximation `H ← (I − ρsyᵀ)H(I − ρysᵀ) + ρssᵀ`, `ρ =
|
|
105
|
+
* 1/yᵀs`, which the curvature condition keeps positive definite. The update is
|
|
106
|
+
* skipped when `yᵀs ≤ ε·‖s‖·‖y‖` (rounding has lost the curvature). When `d` is
|
|
107
|
+
* not a descent direction (`H` lost definiteness to rounding, or was given
|
|
108
|
+
* indefinite), or a line search fails, `H` is reset to a scaled identity and
|
|
109
|
+
* the step retried along the steepest descent; a second failure stops with
|
|
110
|
+
* `"line-search"`. The first line search starts at `α = min(1, 1/‖d‖)`, the
|
|
111
|
+
* others at `α = 1`, where superlinear convergence takes full steps. Near a
|
|
112
|
+
* minimum, where `f` changes by less than its own rounding, a step is also
|
|
113
|
+
* accepted on Hager and Zhang's approximate Wolfe conditions (its value within
|
|
114
|
+
* 16 ulps of the start, `c₂·φ′(0) ≤ φ′(α) ≤ (2c₁ − 1)·φ′(0)`), so the gradient
|
|
115
|
+
* keeps converging past the precision of `f`; the reported value may then sit a
|
|
116
|
+
* few ulps above the lowest seen.
|
|
117
|
+
*
|
|
118
|
+
* **Values.** The start must give a finite value and gradient (else a
|
|
119
|
+
* `RangeError`). A non-finite value or gradient at a trial point — outside the
|
|
120
|
+
* function's domain, an overflow — makes the line search halve its step and
|
|
121
|
+
* stay below it, so a domain boundary is approached, never crossed. On every
|
|
122
|
+
* exit the reported point is the last accepted one (the start if none was),
|
|
123
|
+
* whose value and gradient are reported with it.
|
|
124
|
+
*
|
|
125
|
+
* **Complexity.** O(dimension²) time per step plus the evaluations (usually one
|
|
126
|
+
* or two a step near the minimum), O(dimension²) memory.
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```ts
|
|
130
|
+
* // Rosenbrock's function, minimum 0 at (1, 1).
|
|
131
|
+
* const minimizer = new BfgsMinimizer(2);
|
|
132
|
+
* const point = minimizer.minimize(
|
|
133
|
+
* new Float64Array(2),
|
|
134
|
+
* (x, gradient) => {
|
|
135
|
+
* const a = 1 - x[0]!;
|
|
136
|
+
* const b = x[1]! - x[0]! * x[0]!;
|
|
137
|
+
* gradient[0] = -2 * a - 400 * x[0]! * b;
|
|
138
|
+
* gradient[1] = 200 * b;
|
|
139
|
+
* return a * a + 100 * b * b;
|
|
140
|
+
* },
|
|
141
|
+
* Float64Array.of(-1.2, 1),
|
|
142
|
+
* );
|
|
143
|
+
* // point ≈ [1, 1], minimizer.status === "gradient"
|
|
144
|
+
* ```;
|
|
145
|
+
*/
|
|
146
|
+
export declare class BfgsMinimizer {
|
|
147
|
+
/** Number of variables. */
|
|
148
|
+
readonly dimension: number;
|
|
149
|
+
/**
|
|
150
|
+
* The gradient at the returned point, written by {@link minimize}. The
|
|
151
|
+
* minimiser's own array: read it, never write it.
|
|
152
|
+
*/
|
|
153
|
+
readonly gradient: Float64Array;
|
|
154
|
+
/**
|
|
155
|
+
* The final inverse Hessian approximation, `dimension × dimension`, over the
|
|
156
|
+
* minimiser's own storage. Near a minimum it approximates `(∇²f)⁻¹`; a
|
|
157
|
+
* covariance from it is only as good as that approximation (exact Hessians
|
|
158
|
+
* give the covariance; this one is a by-product of the path taken).
|
|
159
|
+
*/
|
|
160
|
+
readonly inverseHessian: Matrix;
|
|
161
|
+
/** `f` at the returned point. Read-only for callers. */
|
|
162
|
+
value: number;
|
|
163
|
+
/** Accepted steps of the last run. Read-only for callers. */
|
|
164
|
+
iterations: number;
|
|
165
|
+
/** Evaluations of the objective in the last run. Read-only for callers. */
|
|
166
|
+
evaluations: number;
|
|
167
|
+
/** Why the last run stopped. Read-only for callers. */
|
|
168
|
+
status: BfgsStatus;
|
|
169
|
+
private readonly gradientTolerance_;
|
|
170
|
+
private readonly valueTolerance_;
|
|
171
|
+
private readonly stepTolerance_;
|
|
172
|
+
private readonly maxIterations_;
|
|
173
|
+
private readonly line_;
|
|
174
|
+
private readonly hessian_;
|
|
175
|
+
private readonly point_;
|
|
176
|
+
private readonly direction_;
|
|
177
|
+
private readonly step_;
|
|
178
|
+
private readonly change_;
|
|
179
|
+
private readonly product_;
|
|
180
|
+
/** Numbers passed between methods through fields, never boxed arguments. */
|
|
181
|
+
private slope_;
|
|
182
|
+
private scale_;
|
|
183
|
+
private curvature_;
|
|
184
|
+
/**
|
|
185
|
+
* @throws {RangeError} When `dimension` is not a non-negative safe integer
|
|
186
|
+
* (or its square is not), or an option is out of its range: tolerances
|
|
187
|
+
* negative or not finite, `maxIterations` or `maxLineSearchEvaluations` not
|
|
188
|
+
* a positive integer, not `0 < sufficientDecrease < curvature < 1`.
|
|
189
|
+
*/
|
|
190
|
+
constructor(dimension: number, options?: BfgsOptions);
|
|
191
|
+
/** Whether the last run stopped on one of the convergence tests. */
|
|
192
|
+
get converged(): boolean;
|
|
193
|
+
/**
|
|
194
|
+
* Minimises `objective` from `start`, writes the point reached into `out` and
|
|
195
|
+
* returns it; the other results are left in the minimiser's fields.
|
|
196
|
+
*
|
|
197
|
+
* @param out - The point reached, `dimension` numbers. May be `start`.
|
|
198
|
+
* @param objective - The function and its gradient; see
|
|
199
|
+
* {@link BfgsObjective}.
|
|
200
|
+
* @param start - The starting point, `dimension` numbers, only read.
|
|
201
|
+
* @param options - A starting inverse Hessian, a budget, a signal.
|
|
202
|
+
* @returns `out`.
|
|
203
|
+
* @throws {RangeError} When `out` or `start` does not hold `dimension`
|
|
204
|
+
* numbers, `initialInverseHessian` is not `dimension × dimension` and
|
|
205
|
+
* finite, or the objective's value or gradient at `start` is not finite.
|
|
206
|
+
* @throws The signal's reason once aborted, what the budget or the objective
|
|
207
|
+
* throws; the fields then hold the run so far.
|
|
208
|
+
*/
|
|
209
|
+
minimize(out: Float64Array, objective: BfgsObjective, start: Float64Array, options?: BfgsRunOptions): Float64Array;
|
|
210
|
+
/** The iterations, from a finite start in `point_`, `gradient`, `value`. */
|
|
211
|
+
private run_;
|
|
212
|
+
/** `direction_ ← −H·gradient`, and `slope_ ← gradientᵀ·direction_`. */
|
|
213
|
+
private computeDirection_;
|
|
214
|
+
/** `H ← scale_·I`. */
|
|
215
|
+
private resetHessian_;
|
|
216
|
+
/**
|
|
217
|
+
* The inverse BFGS update from `step_` (s) and `change_` (y), `curvature_ =
|
|
218
|
+
* yᵀs > 0`: with `Hy` and `yᵀHy`, `H += ((1 + ρ·yᵀHy)·ssᵀ − Hy·sᵀ −
|
|
219
|
+
* s·(Hy)ᵀ)·ρ`.
|
|
220
|
+
*/
|
|
221
|
+
private update_;
|
|
222
|
+
}
|
|
223
|
+
/** What {@link bfgs} returns: the fields of a {@link BfgsMinimizer} after a run. */
|
|
224
|
+
export type BfgsResult = {
|
|
225
|
+
/** The point reached. */
|
|
226
|
+
readonly point: Float64Array;
|
|
227
|
+
/** `f` there. */
|
|
228
|
+
readonly value: number;
|
|
229
|
+
/** `∇f` there. */
|
|
230
|
+
readonly gradient: Float64Array;
|
|
231
|
+
/** The final inverse Hessian approximation. */
|
|
232
|
+
readonly inverseHessian: Matrix;
|
|
233
|
+
/** Accepted steps. */
|
|
234
|
+
readonly iterations: number;
|
|
235
|
+
/** Evaluations of the objective. */
|
|
236
|
+
readonly evaluations: number;
|
|
237
|
+
/** Why it stopped. */
|
|
238
|
+
readonly status: BfgsStatus;
|
|
239
|
+
/** Whether `status` is one of the convergence tests. */
|
|
240
|
+
readonly converged: boolean;
|
|
241
|
+
};
|
|
242
|
+
/**
|
|
243
|
+
* Minimises `objective` from `start` with a new {@link BfgsMinimizer}: the
|
|
244
|
+
* one-shot form, which allocates the minimiser and its result. Reuse a
|
|
245
|
+
* minimiser for repeated fits.
|
|
246
|
+
*
|
|
247
|
+
* @example
|
|
248
|
+
* ```ts
|
|
249
|
+
* const result = bfgs(
|
|
250
|
+
* (x, gradient) => {
|
|
251
|
+
* gradient[0] = 2 * (x[0]! - 3);
|
|
252
|
+
* return (x[0]! - 3) ** 2;
|
|
253
|
+
* },
|
|
254
|
+
* Float64Array.of(0),
|
|
255
|
+
* );
|
|
256
|
+
* // result.point ≈ [3], result.converged === true
|
|
257
|
+
* ```;
|
|
258
|
+
*
|
|
259
|
+
* @throws {RangeError} As the constructor and {@link BfgsMinimizer.minimize}.
|
|
260
|
+
*/
|
|
261
|
+
export declare function bfgs(objective: BfgsObjective, start: Float64Array, options?: BfgsOptions & BfgsRunOptions): BfgsResult;
|
|
262
|
+
//# sourceMappingURL=bfgs.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bfgs.d.ts","sourceRoot":"","sources":["../../src/optimize/bfgs.ts"],"names":[],"mappings":"AAAA,OAAO,EAEN,KAAK,QAAQ,EAEb,KAAK,UAAU,EACf,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,KAAK,MAAM,EAAc,MAAM,qBAAqB,CAAC;AAS9D;;;;;;GAMG;AACH,MAAM,MAAM,aAAa,GAAG,QAAQ,CACnC;IAAC,KAAK,EAAE,YAAY;IAAE,QAAQ,EAAE,YAAY;CAAC,EAC7C,MAAM,CACN,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,UAAU,GACnB,UAAU,GACV,OAAO,GACP,MAAM,GACN,gBAAgB,GAChB,aAAa,CAAC;AAEjB;;;GAGG;AACH,MAAM,MAAM,WAAW,GAAG;IACzB;;;OAGG;IACH,QAAQ,CAAC,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAEpC;;;;OAIG;IACH,QAAQ,CAAC,cAAc,CAAC,EAAE,MAAM,CAAC;IAEjC;;;OAGG;IACH,QAAQ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