@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,175 @@
|
|
|
1
|
+
import { describeValue } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* The 15-point Kronrod nodes on `[0, 1)` (the rule is symmetric, the last one
|
|
4
|
+
* is the centre) and weights, and the weights of the 7-point Gauss rule on
|
|
5
|
+
* every other node (QUADPACK `qk15`, Piessens et al. 1983), each the binary64
|
|
6
|
+
* nearest to the published 33-digit value.
|
|
7
|
+
*/
|
|
8
|
+
const KRONROD_NODES_ = Float64Array.of(0.9914553711208126, 0.9491079123427585, 0.8648644233597691, 0.7415311855993945, 0.5860872354676911, 0.4058451513773972, 0.20778495500789848, 0);
|
|
9
|
+
const KRONROD_WEIGHTS_ = Float64Array.of(0.022935322010529224, 0.06309209262997856, 0.10479001032225019, 0.14065325971552592, 0.1690047266392679, 0.19035057806478542, 0.20443294007529889, 0.20948214108472782);
|
|
10
|
+
const GAUSS_WEIGHTS_ = Float64Array.of(0.1294849661688697, 0.27970539148927664, 0.3818300505051189, 0.4179591836734694);
|
|
11
|
+
const EPSILON_ = Number.EPSILON;
|
|
12
|
+
const UNDERFLOW_ = Number.MIN_VALUE * 2 ** 52;
|
|
13
|
+
/**
|
|
14
|
+
* Gauss–Kronrod 7–15 on one interval: value in `PANEL_[0]`, error in
|
|
15
|
+
* `PANEL_[1]`.
|
|
16
|
+
*/
|
|
17
|
+
const PANEL_ = new Float64Array(2);
|
|
18
|
+
/** The integrand at the 14 off-centre nodes of the panel being estimated. */
|
|
19
|
+
const VALUES_ = new Float64Array(14);
|
|
20
|
+
function kronrod15_(f, lower, upper) {
|
|
21
|
+
const center = lower / 2 + upper / 2;
|
|
22
|
+
const halfLength = upper / 2 - lower / 2;
|
|
23
|
+
const absHalfLength = Math.abs(halfLength);
|
|
24
|
+
const centerValue = f(center);
|
|
25
|
+
let kronrod = centerValue * KRONROD_WEIGHTS_[7];
|
|
26
|
+
let gauss = centerValue * GAUSS_WEIGHTS_[3];
|
|
27
|
+
let absolute = Math.abs(kronrod);
|
|
28
|
+
const values = VALUES_;
|
|
29
|
+
for (let index = 0; index < 7; index++) {
|
|
30
|
+
const offset = halfLength * KRONROD_NODES_[index];
|
|
31
|
+
const left = f(center - offset);
|
|
32
|
+
const right = f(center + offset);
|
|
33
|
+
values[2 * index] = left;
|
|
34
|
+
values[2 * index + 1] = right;
|
|
35
|
+
const pair = left + right;
|
|
36
|
+
kronrod += KRONROD_WEIGHTS_[index] * pair;
|
|
37
|
+
absolute += KRONROD_WEIGHTS_[index] * (Math.abs(left) + Math.abs(right));
|
|
38
|
+
if (index % 2 === 1) {
|
|
39
|
+
gauss += GAUSS_WEIGHTS_[(index - 1) / 2] * pair;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
const mean = kronrod / 2;
|
|
43
|
+
let deviation = KRONROD_WEIGHTS_[7] * Math.abs(centerValue - mean);
|
|
44
|
+
for (let index = 0; index < 7; index++) {
|
|
45
|
+
deviation +=
|
|
46
|
+
KRONROD_WEIGHTS_[index] *
|
|
47
|
+
(Math.abs(values[2 * index] - mean) +
|
|
48
|
+
Math.abs(values[2 * index + 1] - mean));
|
|
49
|
+
}
|
|
50
|
+
const value = kronrod * halfLength;
|
|
51
|
+
const resultAbs = absolute * absHalfLength;
|
|
52
|
+
const resultAsc = deviation * absHalfLength;
|
|
53
|
+
let error = Math.abs((kronrod - gauss) * halfLength);
|
|
54
|
+
if (resultAsc !== 0 && error !== 0) {
|
|
55
|
+
error = resultAsc * Math.min(1, ((200 * error) / resultAsc) ** 1.5);
|
|
56
|
+
}
|
|
57
|
+
if (resultAbs > UNDERFLOW_ / (50 * EPSILON_)) {
|
|
58
|
+
error = Math.max(50 * EPSILON_ * resultAbs, error);
|
|
59
|
+
}
|
|
60
|
+
PANEL_[0] = value;
|
|
61
|
+
PANEL_[1] = error;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Adaptive Gauss–Kronrod quadrature: the integral of `f` over `[lower, upper]`,
|
|
65
|
+
* the interval bisected where the estimated error is largest until the total
|
|
66
|
+
* meets the tolerance — QUADPACK's `qag` with the 15-point Kronrod rule
|
|
67
|
+
* (Piessens et al. 1983), the strategy behind SciPy's `quad` without its
|
|
68
|
+
* extrapolation.
|
|
69
|
+
*
|
|
70
|
+
* Each panel's error is the Kronrod–Gauss difference with QUADPACK's scaling,
|
|
71
|
+
* which is pessimistic for smooth integrands and alert to a kink, a peak or an
|
|
72
|
+
* endpoint singularity: evaluations go where they are needed, so an integrand
|
|
73
|
+
* smooth almost everywhere costs a few panels instead of a fine uniform grid.
|
|
74
|
+
* Stops when the summed error is below `max(tolerance, relativeTolerance ·
|
|
75
|
+
* |integral|)` (both `1.5e-8` by default) or after `maxIterations` bisections
|
|
76
|
+
* (50 by default), reporting `converged: false` then. 15 calls to `f` per
|
|
77
|
+
* panel.
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```ts
|
|
81
|
+
* adaptiveQuadrature(Math.sqrt, 0, 1).value; // 2/3 to 1e-10 or better, despite √x's endpoint
|
|
82
|
+
* adaptiveQuadrature((x) => Math.abs(x - 0.3), 0, 1).value; // 0.29, to about 1e-10
|
|
83
|
+
* ```;
|
|
84
|
+
*
|
|
85
|
+
* @param f The integrand.
|
|
86
|
+
* @param lower Lower limit of integration, finite.
|
|
87
|
+
* @param upper Upper limit of integration, finite.
|
|
88
|
+
* @param options Tolerances, iteration limit and abort signal.
|
|
89
|
+
* @returns The integral, its error estimate, and whether the tolerance was met.
|
|
90
|
+
* @throws {RangeError} When a limit is not finite, a tolerance is negative or
|
|
91
|
+
* `maxIterations` is not a non-negative integer.
|
|
92
|
+
* @throws The signal's reason, once it is aborted.
|
|
93
|
+
* @see {@link gaussLegendre} for a fixed rule on a smooth integrand.
|
|
94
|
+
*/
|
|
95
|
+
export function adaptiveQuadrature(f, lower, upper, options) {
|
|
96
|
+
if (lower - lower !== 0 || upper - upper !== 0) {
|
|
97
|
+
throw new RangeError(`adaptiveQuadrature: limits must be finite, got [${lower}, ${upper}]`);
|
|
98
|
+
}
|
|
99
|
+
const absoluteTolerance = options?.tolerance ?? 1.5e-8;
|
|
100
|
+
const relativeTolerance = options?.relativeTolerance ?? 1.5e-8;
|
|
101
|
+
const maxIterations = options?.maxIterations ?? 50;
|
|
102
|
+
if (!(absoluteTolerance >= 0) || !(relativeTolerance >= 0)) {
|
|
103
|
+
throw new RangeError(`adaptiveQuadrature: tolerances must be non-negative, got ${describeValue(absoluteTolerance)} and ${describeValue(relativeTolerance)}`);
|
|
104
|
+
}
|
|
105
|
+
if (!Number.isSafeInteger(maxIterations) || maxIterations < 0) {
|
|
106
|
+
throw new RangeError(`adaptiveQuadrature: maxIterations must be a non-negative integer, got ${describeValue(maxIterations)}`);
|
|
107
|
+
}
|
|
108
|
+
const signal = options?.signal;
|
|
109
|
+
const capacity = maxIterations + 1;
|
|
110
|
+
const lowers = new Float64Array(capacity);
|
|
111
|
+
const uppers = new Float64Array(capacity);
|
|
112
|
+
const values = new Float64Array(capacity);
|
|
113
|
+
const errors = new Float64Array(capacity);
|
|
114
|
+
kronrod15_(f, lower, upper);
|
|
115
|
+
lowers[0] = lower;
|
|
116
|
+
uppers[0] = upper;
|
|
117
|
+
values[0] = PANEL_[0];
|
|
118
|
+
errors[0] = PANEL_[1];
|
|
119
|
+
let count = 1;
|
|
120
|
+
let total = values[0];
|
|
121
|
+
let totalError = errors[0];
|
|
122
|
+
let iterations = 0;
|
|
123
|
+
while (totalError >
|
|
124
|
+
Math.max(absoluteTolerance, relativeTolerance * Math.abs(total)) &&
|
|
125
|
+
iterations < maxIterations) {
|
|
126
|
+
signal?.throwIfAborted();
|
|
127
|
+
let worst = 0;
|
|
128
|
+
for (let index = 1; index < count; index++) {
|
|
129
|
+
if (errors[index] > errors[worst]) {
|
|
130
|
+
worst = index;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
const a = lowers[worst];
|
|
134
|
+
const b = uppers[worst];
|
|
135
|
+
const middle = a / 2 + b / 2;
|
|
136
|
+
if (middle <= Math.min(a, b) || middle >= Math.max(a, b)) {
|
|
137
|
+
// The interval cannot be split further in binary64.
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
kronrod15_(f, a, middle);
|
|
141
|
+
const leftValue = PANEL_[0];
|
|
142
|
+
const leftError = PANEL_[1];
|
|
143
|
+
kronrod15_(f, middle, b);
|
|
144
|
+
const rightValue = PANEL_[0];
|
|
145
|
+
const rightError = PANEL_[1];
|
|
146
|
+
total += leftValue + rightValue - values[worst];
|
|
147
|
+
totalError += leftError + rightError - errors[worst];
|
|
148
|
+
uppers[worst] = middle;
|
|
149
|
+
values[worst] = leftValue;
|
|
150
|
+
errors[worst] = leftError;
|
|
151
|
+
lowers[count] = middle;
|
|
152
|
+
uppers[count] = b;
|
|
153
|
+
values[count] = rightValue;
|
|
154
|
+
errors[count] = rightError;
|
|
155
|
+
count++;
|
|
156
|
+
iterations++;
|
|
157
|
+
}
|
|
158
|
+
// Re-add the panels from scratch: the running sums drift by a rounding
|
|
159
|
+
// per update.
|
|
160
|
+
total = 0;
|
|
161
|
+
totalError = 0;
|
|
162
|
+
for (let index = 0; index < count; index++) {
|
|
163
|
+
total += values[index];
|
|
164
|
+
totalError += errors[index];
|
|
165
|
+
}
|
|
166
|
+
return {
|
|
167
|
+
value: total,
|
|
168
|
+
error: totalError,
|
|
169
|
+
iterations,
|
|
170
|
+
converged: totalError <=
|
|
171
|
+
Math.max(absoluteTolerance, relativeTolerance * Math.abs(total)),
|
|
172
|
+
evaluations: 15 * (2 * iterations + 1),
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
//# sourceMappingURL=adaptive-quadrature.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adaptive-quadrature.js","sourceRoot":"","sources":["../../src/quadrature/adaptive-quadrature.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAK7C;;;;;GAKG;AACH,MAAM,cAAc,GAAG,YAAY,CAAC,EAAE,CACrC,kBAAkB,EAClB,kBAAkB,EAClB,kBAAkB,EAClB,kBAAkB,EAClB,kBAAkB,EAClB,kBAAkB,EAClB,mBAAmB,EACnB,CAAC,CACD,CAAC;AACF,MAAM,gBAAgB,GAAG,YAAY,CAAC,EAAE,CACvC,oBAAoB,EACpB,mBAAmB,EACnB,mBAAmB,EACnB,mBAAmB,EACnB,kBAAkB,EAClB,mBAAmB,EACnB,mBAAmB,EACnB,mBAAmB,CACnB,CAAC;AACF,MAAM,cAAc,GAAG,YAAY,CAAC,EAAE,CACrC,kBAAkB,EAClB,mBAAmB,EACnB,kBAAkB,EAClB,kBAAkB,CAClB,CAAC;AAEF,MAAM,QAAQ,GAAG,MAAM,CAAC,OAAO,CAAC;AAChC,MAAM,UAAU,GAAG,MAAM,CAAC,SAAS,GAAG,CAAC,IAAI,EAAE,CAAC;AAqB9C;;;GAGG;AACH,MAAM,MAA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AG,UAAU,CAAC;QAC3B,KAAK,EAAE,CAAC;QACR,UAAU,EAAE,CAAC;IACd,CAAC;IAED,uEAAuE;IACvE,cAAc;IACd,KAAK,GAAG,CAAC,CAAC;IACV,UAAU,GAAG,CAAC,CAAC;IACf,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC;QAC5C,KAAK,IAAI,MAAM,CAAC,KAAK,CAAE,CAAC;QACxB,UAAU,IAAI,MAAM,CAAC,KAAK,CAAE,CAAC;IAC9B,CAAC;IACD,OAAO;QACN,KAAK,EAAE,KAAK;QACZ,KAAK,EAAE,UAAU;QACjB,UAAU;QACV,SAAS,EACR,UAAU;YACV,IAAI,CAAC,GAAG,CAAC,iBAAiB,EAAE,iBAAiB,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QACjE,WAAW,EAAE,EAAE,GAAG,CAAC,CAAC,GAAG,UAAU,GAAG,CAAC,CAAC;KACtC,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gauss–Legendre quadrature: the integral of `f` over `[lower, upper]` from
|
|
3
|
+
* `nodes` evaluations at the roots of the Legendre polynomial `Pₙ`, mapped onto
|
|
4
|
+
* the interval. Exact for every polynomial of degree up to `2n − 1`, and
|
|
5
|
+
* converging exponentially for an integrand analytic around the interval — the
|
|
6
|
+
* most accurate fixed rule per evaluation for a smooth `f`.
|
|
7
|
+
*
|
|
8
|
+
* Each rule is built once (O(n²)) and cached. The integrand is never evaluated
|
|
9
|
+
* at the endpoints, so an integrable singularity there does not stop it, though
|
|
10
|
+
* convergence slows. O(n) calls to `f`.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* gaussLegendre((x) => x ** 9, 0, 1, 5); // 0.1 — degree 9 = 2·5 − 1
|
|
15
|
+
* gaussLegendre(Math.exp, 0, 1, 8); // e − 1 to within an ulp or two
|
|
16
|
+
* ```;
|
|
17
|
+
*
|
|
18
|
+
* @param f The integrand.
|
|
19
|
+
* @param lower Lower limit of integration.
|
|
20
|
+
* @param upper Upper limit of integration.
|
|
21
|
+
* @param nodes Number of points, an integer from 1 to 1024; 20 by default.
|
|
22
|
+
* @returns The approximated integral.
|
|
23
|
+
* @throws {RangeError} When `nodes` is not an integer in `[1, 1024]`.
|
|
24
|
+
* @see {@link adaptiveQuadrature} when the integrand is not smooth everywhere.
|
|
25
|
+
*/
|
|
26
|
+
export declare function gaussLegendre(f: (x: number) => number, lower: number, upper: number, nodes?: number): number;
|
|
27
|
+
//# sourceMappingURL=gauss-legendre.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gauss-legendre.d.ts","sourceRoot":"","sources":["../../src/quadrature/gauss-legendre.ts"],"names":[],"mappings":"AA6EA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,aAAa,CAC5B,CAAC,EAAE,CAAC,CAAC,EAAE,MAAM,KAAK,MAAM,EACxB,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,EACb,KAAK,SAAK,GACR,MAAM,CAmBR"}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { describeValue } from "@ac-kit/core";
|
|
2
|
+
/** The largest rule built. */
|
|
3
|
+
const MAX_NODES_ = 1024;
|
|
4
|
+
/** The rules built so far, by size. */
|
|
5
|
+
const RULES_ = new Map();
|
|
6
|
+
/**
|
|
7
|
+
* `Pₙ(x)` and `Pₙ₋₁(x)` by the three-term recurrence, written into `LEGENDRE_`
|
|
8
|
+
* (two numbers, no boxing across the call).
|
|
9
|
+
*/
|
|
10
|
+
const LEGENDRE_ = new Float64Array(2);
|
|
11
|
+
function legendrePair_(n, x) {
|
|
12
|
+
let previous = 1;
|
|
13
|
+
let current = x;
|
|
14
|
+
for (let degree = 2; degree <= n; degree++) {
|
|
15
|
+
const next = ((2 * degree - 1) * x * current - (degree - 1) * previous) / degree;
|
|
16
|
+
previous = current;
|
|
17
|
+
current = next;
|
|
18
|
+
}
|
|
19
|
+
LEGENDRE_[0] = current;
|
|
20
|
+
LEGENDRE_[1] = previous;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The `n`-point Gauss–Legendre rule on `[−1, 1]`: the non-negative half of its
|
|
24
|
+
* nodes (the rule is symmetric; an odd `n` has a node at 0) and their weights.
|
|
25
|
+
* Each node is Tricomi's asymptotic guess polished by Newton's method on `Pₙ`,
|
|
26
|
+
* each weight `2 / ((1 − x²) Pₙ′(x)²)`.
|
|
27
|
+
*/
|
|
28
|
+
function rule_(n) {
|
|
29
|
+
const cached = RULES_.get(n);
|
|
30
|
+
if (cached !== undefined) {
|
|
31
|
+
return cached;
|
|
32
|
+
}
|
|
33
|
+
const half = Math.ceil(n / 2);
|
|
34
|
+
const nodes = new Float64Array(half);
|
|
35
|
+
const weights = new Float64Array(half);
|
|
36
|
+
if (n === 1) {
|
|
37
|
+
nodes[0] = 0;
|
|
38
|
+
weights[0] = 2;
|
|
39
|
+
}
|
|
40
|
+
for (let index = 0; index < half && n > 1; index++) {
|
|
41
|
+
// Roots from the largest down; for an odd n the last one is 0.
|
|
42
|
+
const rank = index + 1;
|
|
43
|
+
let x = (1 - (n - 1) / (8 * n * n * n)) *
|
|
44
|
+
Math.cos((Math.PI * (4 * rank - 1)) / (4 * n + 2));
|
|
45
|
+
if (n % 2 === 1 && rank === half) {
|
|
46
|
+
x = 0;
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
for (let iteration = 0; iteration < 100; iteration++) {
|
|
50
|
+
legendrePair_(n, x);
|
|
51
|
+
const derivative = (n * (x * LEGENDRE_[0] - LEGENDRE_[1])) / (x * x - 1);
|
|
52
|
+
const delta = LEGENDRE_[0] / derivative;
|
|
53
|
+
x -= delta;
|
|
54
|
+
if (Math.abs(delta) <= 1e-16 * Math.abs(x)) {
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
legendrePair_(n, x);
|
|
60
|
+
const derivative = (n * (x * LEGENDRE_[0] - LEGENDRE_[1])) / (x * x - 1);
|
|
61
|
+
nodes[index] = x;
|
|
62
|
+
weights[index] = 2 / ((1 - x * x) * derivative * derivative);
|
|
63
|
+
}
|
|
64
|
+
const built = { nodes, weights };
|
|
65
|
+
RULES_.set(n, built);
|
|
66
|
+
return built;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Gauss–Legendre quadrature: the integral of `f` over `[lower, upper]` from
|
|
70
|
+
* `nodes` evaluations at the roots of the Legendre polynomial `Pₙ`, mapped onto
|
|
71
|
+
* the interval. Exact for every polynomial of degree up to `2n − 1`, and
|
|
72
|
+
* converging exponentially for an integrand analytic around the interval — the
|
|
73
|
+
* most accurate fixed rule per evaluation for a smooth `f`.
|
|
74
|
+
*
|
|
75
|
+
* Each rule is built once (O(n²)) and cached. The integrand is never evaluated
|
|
76
|
+
* at the endpoints, so an integrable singularity there does not stop it, though
|
|
77
|
+
* convergence slows. O(n) calls to `f`.
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```ts
|
|
81
|
+
* gaussLegendre((x) => x ** 9, 0, 1, 5); // 0.1 — degree 9 = 2·5 − 1
|
|
82
|
+
* gaussLegendre(Math.exp, 0, 1, 8); // e − 1 to within an ulp or two
|
|
83
|
+
* ```;
|
|
84
|
+
*
|
|
85
|
+
* @param f The integrand.
|
|
86
|
+
* @param lower Lower limit of integration.
|
|
87
|
+
* @param upper Upper limit of integration.
|
|
88
|
+
* @param nodes Number of points, an integer from 1 to 1024; 20 by default.
|
|
89
|
+
* @returns The approximated integral.
|
|
90
|
+
* @throws {RangeError} When `nodes` is not an integer in `[1, 1024]`.
|
|
91
|
+
* @see {@link adaptiveQuadrature} when the integrand is not smooth everywhere.
|
|
92
|
+
*/
|
|
93
|
+
export function gaussLegendre(f, lower, upper, nodes = 20) {
|
|
94
|
+
if (!Number.isSafeInteger(nodes) || nodes < 1 || nodes > MAX_NODES_) {
|
|
95
|
+
throw new RangeError(`gaussLegendre: nodes must be an integer in [1, ${MAX_NODES_}], got ${describeValue(nodes)}`);
|
|
96
|
+
}
|
|
97
|
+
const rule = rule_(nodes);
|
|
98
|
+
const center = lower / 2 + upper / 2;
|
|
99
|
+
const radius = upper / 2 - lower / 2;
|
|
100
|
+
let total = 0;
|
|
101
|
+
for (let index = 0; index < rule.nodes.length; index++) {
|
|
102
|
+
const offset = radius * rule.nodes[index];
|
|
103
|
+
const weight = rule.weights[index];
|
|
104
|
+
total +=
|
|
105
|
+
offset === 0
|
|
106
|
+
? weight * f(center)
|
|
107
|
+
: weight * (f(center - offset) + f(center + offset));
|
|
108
|
+
}
|
|
109
|
+
return total * radius;
|
|
110
|
+
}
|
|
111
|
+
//# sourceMappingURL=gauss-legendre.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gauss-legendre.js","sourceRoot":"","sources":["../../src/quadrature/gauss-legendre.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAE7C,8BAA8B;AAC9B,MAAM,UAAU,GAAG,IAAI,CAAC;AAIxB,uCAAuC;AACvC,MAAM,MAAM,GAAG,IAAI,GAAG,EAAiB,CAAC;AAExC;;;GAGG;AACH,MAAM,SAAS,GAAG,IAAI,YAAY,CAAC,CAAC,CAAC,CAAC;AAEtC,SAAS,aAAa,CAAC,CAAS,EAAE,CAAS;IAC1C,IAAI,QAAQ,GAAG,CAAC,CAAC;IACjB,IAAI,OAAO,GAAG,CAAC,CAAC;IAChB,KAAK,IAAI,MAAM,GAAG,CAAC,EAAE,MAAM,IAAI,CAAC,EAAE,MAAM,EAAE,EAAE,CAAC;QAC5C,MAAM,IAAI,GACT,CAAC,CAAC,CAAC,GAAG,MAAM,GAAG,CAAC,CAAC,GAAG,CAAC,GAAG,OAAO,GAAG,CAAC,MAAM,GAAG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
|
|
2
|
+
* Composite Simpson's rule: the integral of `f` over `[lower, upper]` from
|
|
3
|
+
* `intervals` equal panels, each pair of them fitted with a parabola through
|
|
4
|
+
* its three points — weights `1, 4, 2, 4, …, 4, 1` times `h / 3`.
|
|
5
|
+
*
|
|
6
|
+
* The error is `O(h⁴)` for an integrand with a continuous fourth derivative,
|
|
7
|
+
* against the trapezoidal rule's `O(h²)`: the usual default when `f` is smooth
|
|
8
|
+
* and can be sampled anywhere. The terms are accumulated with `@ac-kit/core`'s
|
|
9
|
+
* exactly rounded `PreciseSum`, so adding panels does not add rounding error.
|
|
10
|
+
* O(`intervals`) calls to `f`, O(1) memory.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* simpsonsRule((x) => x ** 3, 0, 1, 2); // 0.25 — exact for cubics
|
|
15
|
+
* simpsonsRule(Math.sin, 0, Math.PI, 100); // 2, to about 1e-8
|
|
16
|
+
* ```;
|
|
17
|
+
*
|
|
18
|
+
* @param f The integrand.
|
|
19
|
+
* @param lower Lower limit of integration.
|
|
20
|
+
* @param upper Upper limit of integration.
|
|
21
|
+
* @param intervals Number of panels; a positive even integer, 1000 by default.
|
|
22
|
+
* @returns The approximated integral.
|
|
23
|
+
* @throws {RangeError} When `intervals` is not a positive even integer.
|
|
24
|
+
* @see {@link trapezoidalRule}, {@link gaussLegendre}, {@link adaptiveQuadrature}
|
|
25
|
+
*/
|
|
26
|
+
export declare function simpsonsRule(f: (x: number) => number, lower: number, upper: number, intervals?: number): number;
|
|
27
|
+
//# sourceMappingURL=simpsons-rule.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"simpsons-rule.d.ts","sourceRoot":"","sources":["../../src/quadrature/simpsons-rule.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,YAAY,CAC3B,CAAC,EAAE,CAAC,CAAC,EAAE,MAAM,KAAK,MAAM,EACxB,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,EACb,SAAS,SAAO,GACd,MAAM,CAqBR"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { describeValue, PreciseSum } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* Composite Simpson's rule: the integral of `f` over `[lower, upper]` from
|
|
4
|
+
* `intervals` equal panels, each pair of them fitted with a parabola through
|
|
5
|
+
* its three points — weights `1, 4, 2, 4, …, 4, 1` times `h / 3`.
|
|
6
|
+
*
|
|
7
|
+
* The error is `O(h⁴)` for an integrand with a continuous fourth derivative,
|
|
8
|
+
* against the trapezoidal rule's `O(h²)`: the usual default when `f` is smooth
|
|
9
|
+
* and can be sampled anywhere. The terms are accumulated with `@ac-kit/core`'s
|
|
10
|
+
* exactly rounded `PreciseSum`, so adding panels does not add rounding error.
|
|
11
|
+
* O(`intervals`) calls to `f`, O(1) memory.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* simpsonsRule((x) => x ** 3, 0, 1, 2); // 0.25 — exact for cubics
|
|
16
|
+
* simpsonsRule(Math.sin, 0, Math.PI, 100); // 2, to about 1e-8
|
|
17
|
+
* ```;
|
|
18
|
+
*
|
|
19
|
+
* @param f The integrand.
|
|
20
|
+
* @param lower Lower limit of integration.
|
|
21
|
+
* @param upper Upper limit of integration.
|
|
22
|
+
* @param intervals Number of panels; a positive even integer, 1000 by default.
|
|
23
|
+
* @returns The approximated integral.
|
|
24
|
+
* @throws {RangeError} When `intervals` is not a positive even integer.
|
|
25
|
+
* @see {@link trapezoidalRule}, {@link gaussLegendre}, {@link adaptiveQuadrature}
|
|
26
|
+
*/
|
|
27
|
+
export function simpsonsRule(f, lower, upper, intervals = 1000) {
|
|
28
|
+
if (!Number.isSafeInteger(intervals) ||
|
|
29
|
+
intervals < 2 ||
|
|
30
|
+
intervals % 2 !== 0) {
|
|
31
|
+
throw new RangeError(`simpsonsRule: intervals must be a positive even integer, got ${describeValue(intervals)}`);
|
|
32
|
+
}
|
|
33
|
+
const step = (upper - lower) / intervals;
|
|
34
|
+
const total = new PreciseSum();
|
|
35
|
+
total.add(f(lower));
|
|
36
|
+
total.add(f(upper));
|
|
37
|
+
for (let index = 1; index < intervals; index++) {
|
|
38
|
+
const value = f(lower + index * step);
|
|
39
|
+
// 4 and 2 are exact multipliers.
|
|
40
|
+
total.add(index % 2 === 1 ? 4 * value : 2 * value);
|
|
41
|
+
}
|
|
42
|
+
return (total.value * step) / 3;
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=simpsons-rule.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"simpsons-rule.js","sourceRoot":"","sources":["../../src/quadrature/simpsons-rule.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAEzD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,UAAU,YAAY,CAC3B,CAAwB,EACxB,KAAa,EACb,KAAa,EACb,SAAS,GAAG,IAAI;IAEhB,IACC,CAAC,MAAM,CAAC,aAAa,CAAC,SAAS,CAAC;QAChC,SAAS,GAAG,CAAC;QACb,SAAS,GAAG,CAAC,KAAK,CAAC,EAClB,CAAC;QACF,MAAM,IAAI,UAAU,CACnB,gEAAgE,aAAa,CAAC,SAAS,CAAC,EAAE,CAC1F,CAAC;IACH,CAAC;IAED,MAAM,IAAI,GAAG,CAAC,KAAK,GAAG,KAAK,CAAC,GAAG,SAAS,CAAC;IACzC,MAAM,KAAK,GAAG,IAAI,UAAU,EAAE,CAAC;IAC/B,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;IACpB,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;IACpB,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,SAAS,EAAE,KAAK,EAAE,EAAE,CAAC;QAChD,MAAM,KAAK,GAAG,CAAC,CAAC,KAAK,GAAG,KAAK,GAAG,IAAI,CAAC,CAAC;QACtC,iCAAiC;QACjC,KAAK,CAAC,GAAG,CAAC,KAAK,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC;IACpD,CAAC;IACD,OAAO,CAAC,KAAK,CAAC,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC;AACjC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_grown.d.ts","sourceRoot":"","sources":["../../src/roots/_grown.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"_grown.d.ts","sourceRoot":"","sources":["../../src/roots/_grown.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,wBAAgB,aAAa,CAC5B,KAAK,EAAE,YAAY,EACnB,MAAM,EAAE,MAAM,GACZ,YAAY,CAAC,WAAW,CAAC,CAI3B;AAED,gDAAgD;AAChD,wBAAgB,UAAU,CACzB,KAAK,EAAE,SAAS,EAChB,MAAM,EAAE,MAAM,GACZ,SAAS,CAAC,WAAW,CAAC,CAIxB;AAED,iDAAiD;AACjD,wBAAgB,WAAW,CAC1B,KAAK,EAAE,UAAU,EACjB,MAAM,EAAE,MAAM,GACZ,UAAU,CAAC,WAAW,CAAC,CAIzB"}
|
package/dist/roots/_grown.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// Generic, so bound for `@ac-kit/core`: math-analysis's TODO.md records it.
|
|
2
1
|
/** A copy of `array` at least `length` long, doubling to amortise growth. */
|
|
3
2
|
export function grownFloat64_(array, length) {
|
|
4
3
|
const grown = new Float64Array(Math.max(length, 2 * array.length));
|
package/dist/roots/_grown.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_grown.js","sourceRoot":"","sources":["../../src/roots/_grown.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"_grown.js","sourceRoot":"","sources":["../../src/roots/_grown.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,MAAM,UAAU,aAAa,CAC5B,KAAmB,EACnB,MAAc;IAEd,MAAM,KAAK,GAAG,IAAI,YAAY,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;IACnE,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IACjB,OAAO,KAAK,CAAC;AACd,CAAC;AAED,gDAAgD;AAChD,MAAM,UAAU,UAAU,CACzB,KAAgB,EAChB,MAAc;IAEd,MAAM,KAAK,GAAG,IAAI,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;IAChE,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IACjB,OAAO,KAAK,CAAC;AACd,CAAC;AAED,iDAAiD;AACjD,MAAM,UAAU,WAAW,CAC1B,KAAiB,EACjB,MAAc;IAEd,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;IACjE,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IACjB,OAAO,KAAK,CAAC;AACd,CAAC"}
|
|
@@ -5,6 +5,10 @@ import type { LevelSearchOptions } from "./level-search-options.js";
|
|
|
5
5
|
export declare const LEVEL_BELOW_ALL_ = -1;
|
|
6
6
|
/** {@link LevelSearch_.setLevels}'s index for a `+∞` level: always above f. */
|
|
7
7
|
export declare const LEVEL_ABOVE_ALL_ = -2;
|
|
8
|
+
/** What {@link LevelSearch_.run} reads: the options, and a level for one. */
|
|
9
|
+
type LevelSearchRunOptions_ = LevelSearchOptions & {
|
|
10
|
+
readonly level?: number;
|
|
11
|
+
};
|
|
8
12
|
/**
|
|
9
13
|
* The certified multi-level crossing search behind `findLevelCrossings` and
|
|
10
14
|
* `findLevelBands`, with every buffer it writes kept for the next run.
|
|
@@ -35,6 +39,12 @@ export declare const LEVEL_ABOVE_ALL_ = -2;
|
|
|
35
39
|
* Numbers move between the methods through fields and typed arrays, never as
|
|
36
40
|
* arguments or return values: a double crossing a call the engine does not
|
|
37
41
|
* inline is boxed, and a search must allocate nothing.
|
|
42
|
+
*
|
|
43
|
+
* Every number a search reads once — the options, the bounds, the level of
|
|
44
|
+
* `findLevelCrossings` — is read in {@link search_}, never in {@link run}: code
|
|
45
|
+
* run once a search reaches the optimising tier thousands of searches after the
|
|
46
|
+
* subdivision loop does, and until then boxes each double it loads, which
|
|
47
|
+
* bounds that are not small integers, such as epoch milliseconds, always are.
|
|
38
48
|
*/
|
|
39
49
|
export declare class LevelSearch_ implements BrentState_ {
|
|
40
50
|
/** The finite levels, ascending and distinct. */
|
|
@@ -69,6 +79,8 @@ export declare class LevelSearch_ implements BrentState_ {
|
|
|
69
79
|
private lastIndices_;
|
|
70
80
|
private stack_;
|
|
71
81
|
private f_;
|
|
82
|
+
private options_;
|
|
83
|
+
private levelFromOptions_;
|
|
72
84
|
private budget_;
|
|
73
85
|
private signal_;
|
|
74
86
|
private routine_;
|
|
@@ -101,8 +113,12 @@ export declare class LevelSearch_ implements BrentState_ {
|
|
|
101
113
|
setLevels(values: Float64Array, count: number, indices: Int32Array): void;
|
|
102
114
|
/** {@link BrentState_.evaluate}: `f(x) − level` into `value`. */
|
|
103
115
|
evaluate(): void;
|
|
104
|
-
/**
|
|
105
|
-
|
|
116
|
+
/**
|
|
117
|
+
* Searches `[intervalLower, intervalUpper]` for the levels set, or with
|
|
118
|
+
* `levelFromOptions` for the one level `options.level` (default `0`).
|
|
119
|
+
*/
|
|
120
|
+
run(routine: string, f: Callable<[x: number], number>, options: LevelSearchRunOptions_, levelFromOptions: boolean): void;
|
|
121
|
+
/** Reads and checks what {@link run} was given, then searches. */
|
|
106
122
|
private search_;
|
|
107
123
|
/**
|
|
108
124
|
* Finds the node's candidate levels, `first_` to `end_`, and decides whether
|
|
@@ -134,4 +150,5 @@ export declare class LevelSearch_ implements BrentState_ {
|
|
|
134
150
|
/** `f(x)` into `value`, checked, counted and charged. */
|
|
135
151
|
private sample_;
|
|
136
152
|
}
|
|
153
|
+
export {};
|
|
137
154
|
//# sourceMappingURL=_level-search.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_level-search.d.ts","sourceRoot":"","sources":["../../src/roots/_level-search.ts"],"names":[],"mappings":"AACA,OAAO,EACN,KAAK,QAAQ,EAIb,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,KAAK,WAAW,EAAgB,MAAM,oBAAoB,CAAC;AAEpE,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAEpE,+EAA+E;AAC/E,eAAO,MAAM,gBAAgB,KAAK,CAAC;AACnC,+EAA+E;AAC/E,eAAO,MAAM,gBAAgB,KAAK,CAAC;AAyBnC
|
|
1
|
+
{"version":3,"file":"_level-search.d.ts","sourceRoot":"","sources":["../../src/roots/_level-search.ts"],"names":[],"mappings":"AACA,OAAO,EACN,KAAK,QAAQ,EAIb,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,KAAK,WAAW,EAAgB,MAAM,oBAAoB,CAAC;AAEpE,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAEpE,+EAA+E;AAC/E,eAAO,MAAM,gBAAgB,KAAK,CAAC;AACnC,+EAA+E;AAC/E,eAAO,MAAM,gBAAgB,KAAK,CAAC;AAyBnC,6EAA6E;AAC7E,KAAK,sBAAsB,GAAG,kBAAkB,GAAG;IAAE,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAI/E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,qBA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O,CAAC,IAAI,CAAK;IACjB,OAAO,CAAC,OAAO,CAAK;IACpB,OAAO,CAAC,UAAU,CAAK;IACvB,OAAO,CAAC,QAAQ,CAAK;IACrB,OAAO,CAAC,UAAU,CAAK;IACvB,OAAO,CAAC,QAAQ,CAAK;IACrB,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAiC;IAE5D,cAMC;IAED;;;;OAIG;IACH,SAAS,CAAC,MAAM,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,UAAU,GAAG,IAAI,CAiCxE;IAED,iEAAiE;IACjE,QAAQ,IAAI,IAAI,CAGf;IAED;;;OAGG;IACH,GAAG,CACF,OAAO,EAAE,MAAM,EACf,CAAC,EAAE,QAAQ,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,EAChC,OAAO,EAAE,sBAAsB,EAC/B,gBAAgB,EAAE,OAAO,GACvB,IAAI,CAkBN;IAED,kEAAkE;IAClE,OAAO,CAAC,OAAO;IA+Hf;;;;OAIG;IACH,OAAO,CAAC,QAAQ;IAkEhB;;;OAGG;IACH,OAAO,CAAC,SAAS;IA4BjB;;;;OAIG;IACH,OAAO,CAAC,KAAK;IAeb,4EAA4E;IAC5E,OAAO,CAAC,OAAO;IA+Df,+EAA+E;IAC/E,OAAO,CAAC,aAAa;IAiCrB;;;OAGG;IACH,OAAO,CAAC,iBAAiB;IA+BzB,OAAO,CAAC,QAAQ;IAMhB,yDAAyD;IACzD,OAAO,CAAC,OAAO;CAaf"}
|
|
@@ -27,6 +27,7 @@ const MARGIN_ULPS_ = 4 * Number.EPSILON;
|
|
|
27
27
|
function noFunction_() {
|
|
28
28
|
return Number.NaN;
|
|
29
29
|
}
|
|
30
|
+
const NO_OPTIONS_ = { maxRate: 1 };
|
|
30
31
|
/**
|
|
31
32
|
* The certified multi-level crossing search behind `findLevelCrossings` and
|
|
32
33
|
* `findLevelBands`, with every buffer it writes kept for the next run.
|
|
@@ -57,6 +58,12 @@ function noFunction_() {
|
|
|
57
58
|
* Numbers move between the methods through fields and typed arrays, never as
|
|
58
59
|
* arguments or return values: a double crossing a call the engine does not
|
|
59
60
|
* inline is boxed, and a search must allocate nothing.
|
|
61
|
+
*
|
|
62
|
+
* Every number a search reads once — the options, the bounds, the level of
|
|
63
|
+
* `findLevelCrossings` — is read in {@link search_}, never in {@link run}: code
|
|
64
|
+
* run once a search reaches the optimising tier thousands of searches after the
|
|
65
|
+
* subdivision loop does, and until then boxes each double it loads, which
|
|
66
|
+
* bounds that are not small integers, such as epoch milliseconds, always are.
|
|
60
67
|
*/
|
|
61
68
|
export class LevelSearch_ {
|
|
62
69
|
/** The finite levels, ascending and distinct. */
|
|
@@ -92,6 +99,8 @@ export class LevelSearch_ {
|
|
|
92
99
|
lastIndices_ = new Int32Array(4);
|
|
93
100
|
stack_ = new Float64Array(16 * NODE_STRIDE_);
|
|
94
101
|
f_ = noFunction_;
|
|
102
|
+
options_ = NO_OPTIONS_;
|
|
103
|
+
levelFromOptions_ = false;
|
|
95
104
|
budget_;
|
|
96
105
|
signal_;
|
|
97
106
|
routine_ = "";
|
|
@@ -169,11 +178,45 @@ export class LevelSearch_ {
|
|
|
169
178
|
this.sample_();
|
|
170
179
|
this.value -= this.level_;
|
|
171
180
|
}
|
|
172
|
-
/**
|
|
173
|
-
|
|
181
|
+
/**
|
|
182
|
+
* Searches `[intervalLower, intervalUpper]` for the levels set, or with
|
|
183
|
+
* `levelFromOptions` for the one level `options.level` (default `0`).
|
|
184
|
+
*/
|
|
185
|
+
run(routine, f, options, levelFromOptions) {
|
|
186
|
+
this.routine_ = routine;
|
|
187
|
+
this.f_ = f;
|
|
188
|
+
this.options_ = options;
|
|
189
|
+
this.levelFromOptions_ = levelFromOptions;
|
|
190
|
+
this.budget_ = options.budget;
|
|
191
|
+
this.signal_ = options.signal;
|
|
192
|
+
this.crossingCount = 0;
|
|
193
|
+
this.unresolvedCount = 0;
|
|
194
|
+
this.evaluations = 0;
|
|
195
|
+
try {
|
|
196
|
+
this.search_();
|
|
197
|
+
}
|
|
198
|
+
finally {
|
|
199
|
+
this.f_ = noFunction_;
|
|
200
|
+
this.options_ = NO_OPTIONS_;
|
|
201
|
+
this.budget_ = undefined;
|
|
202
|
+
this.signal_ = undefined;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
/** Reads and checks what {@link run} was given, then searches. */
|
|
206
|
+
search_() {
|
|
207
|
+
const routine = this.routine_;
|
|
208
|
+
const options = this.options_;
|
|
209
|
+
if (this.levelFromOptions_) {
|
|
210
|
+
const level = options.level ?? 0;
|
|
211
|
+
if (!Number.isFinite(level)) {
|
|
212
|
+
throw new RangeError(`${routine}: level must be finite, got ${describeValue(level)}`);
|
|
213
|
+
}
|
|
214
|
+
this.levels[0] = level;
|
|
215
|
+
this.levelCount = 1;
|
|
216
|
+
}
|
|
174
217
|
const lower = this.intervalLower;
|
|
175
218
|
const upper = this.intervalUpper;
|
|
176
|
-
const { maxRate
|
|
219
|
+
const { maxRate } = options;
|
|
177
220
|
const integer = options.integer ?? false;
|
|
178
221
|
const tolerance = options.tolerance ?? 0;
|
|
179
222
|
if (!Number.isFinite(lower) || !Number.isFinite(upper) || lower > upper) {
|
|
@@ -199,33 +242,13 @@ export class LevelSearch_ {
|
|
|
199
242
|
if (!Number.isFinite(tolerance) || tolerance < 0) {
|
|
200
243
|
throw new RangeError(`${routine}: tolerance must be finite and non-negative, got ${describeValue(tolerance)}`);
|
|
201
244
|
}
|
|
202
|
-
this.routine_ = routine;
|
|
203
245
|
this.maxRate_ = maxRate;
|
|
204
246
|
this.resolution_ = resolution;
|
|
205
247
|
this.integer_ = integer;
|
|
206
248
|
this.tolerance = tolerance;
|
|
207
|
-
this.f_ = f;
|
|
208
|
-
this.budget_ = budget;
|
|
209
|
-
this.signal_ = signal;
|
|
210
|
-
this.crossingCount = 0;
|
|
211
|
-
this.unresolvedCount = 0;
|
|
212
|
-
this.evaluations = 0;
|
|
213
249
|
this.lastKinds_.fill(NOTHING_);
|
|
214
|
-
try {
|
|
215
|
-
this.search_();
|
|
216
|
-
}
|
|
217
|
-
finally {
|
|
218
|
-
this.f_ = noFunction_;
|
|
219
|
-
this.budget_ = undefined;
|
|
220
|
-
this.signal_ = undefined;
|
|
221
|
-
}
|
|
222
|
-
this.finishUnresolved_();
|
|
223
|
-
}
|
|
224
|
-
search_() {
|
|
225
250
|
const count = this.levelCount;
|
|
226
251
|
const levels = this.levels;
|
|
227
|
-
const lower = this.intervalLower;
|
|
228
|
-
const upper = this.intervalUpper;
|
|
229
252
|
this.x = lower;
|
|
230
253
|
this.sample_();
|
|
231
254
|
const lowerValue = this.value;
|
|
@@ -282,6 +305,7 @@ export class LevelSearch_ {
|
|
|
282
305
|
top += 2 * NODE_STRIDE_;
|
|
283
306
|
}
|
|
284
307
|
}
|
|
308
|
+
this.finishUnresolved_();
|
|
285
309
|
}
|
|
286
310
|
/**
|
|
287
311
|
* Finds the node's candidate levels, `first_` to `end_`, and decides whether
|