@ac-kit/math-analysis 0.1.10 → 0.2.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 +112 -0
- package/README.md +82 -8
- package/dist/_incomplete-beta.d.ts.map +1 -1
- package/dist/_incomplete-beta.js +3 -2
- package/dist/_incomplete-beta.js.map +1 -1
- package/dist/_incomplete-gamma.d.ts.map +1 -1
- package/dist/_incomplete-gamma.js +3 -2
- package/dist/_incomplete-gamma.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/inverse-regularized-incomplete-beta.d.ts.map +1 -1
- package/dist/inverse-regularized-incomplete-beta.js +2 -1
- package/dist/inverse-regularized-incomplete-beta.js.map +1 -1
- package/dist/inverse-regularized-incomplete-gamma.d.ts.map +1 -1
- package/dist/inverse-regularized-incomplete-gamma.js +2 -1
- package/dist/inverse-regularized-incomplete-gamma.js.map +1 -1
- package/dist/regularized-incomplete-beta.d.ts.map +1 -1
- package/dist/regularized-incomplete-beta.js +2 -1
- package/dist/regularized-incomplete-beta.js.map +1 -1
- package/dist/roots/_brent-kernel.d.ts +38 -0
- package/dist/roots/_brent-kernel.d.ts.map +1 -0
- package/dist/roots/_brent-kernel.js +112 -0
- package/dist/roots/_brent-kernel.js.map +1 -0
- package/dist/roots/_grown.d.ts +7 -0
- package/dist/roots/_grown.d.ts.map +1 -0
- package/dist/roots/_grown.js +20 -0
- package/dist/roots/_grown.js.map +1 -0
- package/dist/roots/_level-search.d.ts +137 -0
- package/dist/roots/_level-search.d.ts.map +1 -0
- package/dist/roots/_level-search.js +532 -0
- package/dist/roots/_level-search.js.map +1 -0
- package/dist/roots/brent.d.ts +59 -0
- package/dist/roots/brent.d.ts.map +1 -0
- package/dist/roots/brent.js +115 -0
- package/dist/roots/brent.js.map +1 -0
- package/dist/roots/find-level-bands.d.ts +148 -0
- package/dist/roots/find-level-bands.d.ts.map +1 -0
- package/dist/roots/find-level-bands.js +351 -0
- package/dist/roots/find-level-bands.js.map +1 -0
- package/dist/roots/find-level-crossings.d.ts +141 -0
- package/dist/roots/find-level-crossings.d.ts.map +1 -0
- package/dist/roots/find-level-crossings.js +168 -0
- package/dist/roots/find-level-crossings.js.map +1 -0
- package/dist/roots/level-search-options.d.ts +56 -0
- package/dist/roots/level-search-options.d.ts.map +1 -0
- package/dist/roots/level-search-options.js +2 -0
- package/dist/roots/level-search-options.js.map +1 -0
- package/dist/trapezoidal-rule.js +2 -2
- package/dist/trapezoidal-rule.js.map +1 -1
- package/package.json +7 -5
|
@@ -0,0 +1,532 @@
|
|
|
1
|
+
import { binarySearch, bisectRightNumbers } from "@ac-kit/algo";
|
|
2
|
+
import { describeValue, } from "@ac-kit/core";
|
|
3
|
+
import { brentKernel_ } from "./_brent-kernel.js";
|
|
4
|
+
import { grownFloat64_, grownInt32_, grownInt8_ } from "./_grown.js";
|
|
5
|
+
/** {@link LevelSearch_.setLevels}'s index for a `-∞` level: always below f. */
|
|
6
|
+
export const LEVEL_BELOW_ALL_ = -1;
|
|
7
|
+
/** {@link LevelSearch_.setLevels}'s index for a `+∞` level: always above f. */
|
|
8
|
+
export const LEVEL_ABOVE_ALL_ = -2;
|
|
9
|
+
const CERTIFIED_ = 0;
|
|
10
|
+
const UNCERTIFIED_ = 1;
|
|
11
|
+
const SIGN_CHANGE_ = 2;
|
|
12
|
+
// What a node needs after its analysis.
|
|
13
|
+
const SETTLED_ = 0;
|
|
14
|
+
const SPLIT_ = 1;
|
|
15
|
+
const LEAF_ = 2;
|
|
16
|
+
// What a level last emitted, so a run of uncertain cores joins it.
|
|
17
|
+
const NOTHING_ = 0;
|
|
18
|
+
const CROSSING_ = 1;
|
|
19
|
+
const UNRESOLVED_ = 2;
|
|
20
|
+
// An unresolved entry absorbed into the crossing after it.
|
|
21
|
+
const ABSORBED_ = -1;
|
|
22
|
+
// a, b, f(a), f(b), and the parent's hull: what lies outside it is certified.
|
|
23
|
+
const NODE_STRIDE_ = 6;
|
|
24
|
+
const DEFAULT_RELATIVE_RESOLUTION_ = 2 ** -20;
|
|
25
|
+
// Covers the rounding of the envelope and core arithmetic, a few ulps each.
|
|
26
|
+
const MARGIN_ULPS_ = 4 * Number.EPSILON;
|
|
27
|
+
function noFunction_() {
|
|
28
|
+
return Number.NaN;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The certified multi-level crossing search behind `findLevelCrossings` and
|
|
32
|
+
* `findLevelBands`, with every buffer it writes kept for the next run.
|
|
33
|
+
*
|
|
34
|
+
* One subdivision serves every level at once, so an expensive `f` is evaluated
|
|
35
|
+
* once per point however many levels are asked for. A node `[a, b]` with values
|
|
36
|
+
* `fa`, `fb` confines `f` to the Lipschitz envelope `[(fa + fb − L·w) / 2, (fa +
|
|
37
|
+
* fb + L·w) / 2]`; a level outside it is certified for the node. For a level
|
|
38
|
+
* inside it, the cones of slope `L` from both ends certify all but the core `[a +
|
|
39
|
+
* |fa − ℓ| / L, b − |fb − ℓ| / L]`. A node with a level left uncertain — a
|
|
40
|
+
* core, or a sign change — is bisected while it is at least `resolution` wide,
|
|
41
|
+
* and is otherwise a leaf, where a level with a sign change gets one crossing,
|
|
42
|
+
* enclosed by its core, and a level without one but with a core gets that core
|
|
43
|
+
* as unresolved.
|
|
44
|
+
*
|
|
45
|
+
* Cores less than `resolution` apart join, level by level: a run of them (a
|
|
46
|
+
* tangent leaves one) is one unresolved interval, and a run touching a crossing
|
|
47
|
+
* widens that crossing's enclosure instead — it can only hide a sub-resolution
|
|
48
|
+
* return of that crossing, so the odd cluster keeps one net crossing and states
|
|
49
|
+
* where all of it lies.
|
|
50
|
+
*
|
|
51
|
+
* Each child's cores are clipped to its parent's hull of cores, outside which
|
|
52
|
+
* the parent already certified every level, so a child never doubts what its
|
|
53
|
+
* parent settled. The split nodes at depth k are at most 2ᵏ, each `(upper −
|
|
54
|
+
* lower) / 2ᵏ` wide and at least `resolution` wide, which bounds the splits by
|
|
55
|
+
* `2 · (upper − lower) / resolution` (`4 ·` in integer mode, for rounding).
|
|
56
|
+
*
|
|
57
|
+
* Numbers move between the methods through fields and typed arrays, never as
|
|
58
|
+
* arguments or return values: a double crossing a call the engine does not
|
|
59
|
+
* inline is boxed, and a search must allocate nothing.
|
|
60
|
+
*/
|
|
61
|
+
export class LevelSearch_ {
|
|
62
|
+
/** The finite levels, ascending and distinct. */
|
|
63
|
+
levels = new Float64Array(4);
|
|
64
|
+
levelCount = 0;
|
|
65
|
+
/** The interval to search, set before {@link run}. */
|
|
66
|
+
intervalLower = 0;
|
|
67
|
+
intervalUpper = 0;
|
|
68
|
+
/** Per level, `1` when `f(lower) ≥ level`, else `-1`. */
|
|
69
|
+
startSides = new Int8Array(4);
|
|
70
|
+
crossingCount = 0;
|
|
71
|
+
crossingTimes = new Float64Array(16);
|
|
72
|
+
crossingEarliest = new Float64Array(16);
|
|
73
|
+
crossingLatest = new Float64Array(16);
|
|
74
|
+
crossingDirections = new Int8Array(16);
|
|
75
|
+
crossingLevels = new Int32Array(16);
|
|
76
|
+
unresolvedCount = 0;
|
|
77
|
+
unresolvedStarts = new Float64Array(8);
|
|
78
|
+
unresolvedEnds = new Float64Array(8);
|
|
79
|
+
unresolvedLevels = new Int32Array(8);
|
|
80
|
+
/** Calls to `f` made by the last run. */
|
|
81
|
+
evaluations = 0;
|
|
82
|
+
// BrentState_, also the evaluation slot `sample_` reads and writes.
|
|
83
|
+
lower = 0;
|
|
84
|
+
upper = 0;
|
|
85
|
+
lowerValue = 0;
|
|
86
|
+
upperValue = 0;
|
|
87
|
+
tolerance = 0;
|
|
88
|
+
x = 0;
|
|
89
|
+
value = 0;
|
|
90
|
+
root = 0;
|
|
91
|
+
lastKinds_ = new Int8Array(4);
|
|
92
|
+
lastIndices_ = new Int32Array(4);
|
|
93
|
+
stack_ = new Float64Array(16 * NODE_STRIDE_);
|
|
94
|
+
f_ = noFunction_;
|
|
95
|
+
budget_;
|
|
96
|
+
signal_;
|
|
97
|
+
routine_ = "";
|
|
98
|
+
maxRate_ = 1;
|
|
99
|
+
resolution_ = 1;
|
|
100
|
+
integer_ = false;
|
|
101
|
+
level_ = 0;
|
|
102
|
+
oldSideAbove_ = false;
|
|
103
|
+
// The node being analysed, and what its analysis leaves.
|
|
104
|
+
nodeA_ = 0;
|
|
105
|
+
nodeB_ = 0;
|
|
106
|
+
nodeFa_ = 0;
|
|
107
|
+
nodeFb_ = 0;
|
|
108
|
+
clipStart_ = 0;
|
|
109
|
+
clipEnd_ = 0;
|
|
110
|
+
margin_ = 0;
|
|
111
|
+
first_ = 0;
|
|
112
|
+
end_ = 0;
|
|
113
|
+
middle_ = 0;
|
|
114
|
+
hullStart_ = 0;
|
|
115
|
+
hullEnd_ = 0;
|
|
116
|
+
coreStart_ = 0;
|
|
117
|
+
coreEnd_ = 0;
|
|
118
|
+
onOldSide_;
|
|
119
|
+
constructor() {
|
|
120
|
+
this.onOldSide_ = (candidate) => {
|
|
121
|
+
this.x = candidate;
|
|
122
|
+
this.evaluate();
|
|
123
|
+
return this.value >= 0 === this.oldSideAbove_;
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Takes the distinct finite values among the first `count` of `values` as the
|
|
128
|
+
* levels, and writes to `indices` the level index of each value, or
|
|
129
|
+
* {@link LEVEL_BELOW_ALL_} / {@link LEVEL_ABOVE_ALL_} for `∓∞`.
|
|
130
|
+
*/
|
|
131
|
+
setLevels(values, count, indices) {
|
|
132
|
+
if (this.levels.length < count) {
|
|
133
|
+
this.levels = grownFloat64_(this.levels, count);
|
|
134
|
+
this.startSides = grownInt8_(this.startSides, count);
|
|
135
|
+
this.lastKinds_ = grownInt8_(this.lastKinds_, count);
|
|
136
|
+
this.lastIndices_ = grownInt32_(this.lastIndices_, count);
|
|
137
|
+
}
|
|
138
|
+
const levels = this.levels;
|
|
139
|
+
let levelCount = 0;
|
|
140
|
+
for (let index = 0; index < count; index++) {
|
|
141
|
+
const value = values[index];
|
|
142
|
+
if (!Number.isFinite(value)) {
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
const position = bisectRightNumbers(levels, value, 0, levelCount);
|
|
146
|
+
if (position > 0 && levels[position - 1] === value) {
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
levels.copyWithin(position + 1, position, levelCount);
|
|
150
|
+
levels[position] = value;
|
|
151
|
+
levelCount++;
|
|
152
|
+
}
|
|
153
|
+
this.levelCount = levelCount;
|
|
154
|
+
for (let index = 0; index < count; index++) {
|
|
155
|
+
const value = values[index];
|
|
156
|
+
if (value === Number.NEGATIVE_INFINITY) {
|
|
157
|
+
indices[index] = LEVEL_BELOW_ALL_;
|
|
158
|
+
}
|
|
159
|
+
else if (value === Number.POSITIVE_INFINITY) {
|
|
160
|
+
indices[index] = LEVEL_ABOVE_ALL_;
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
indices[index] = bisectRightNumbers(levels, value, 0, levelCount) - 1;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
/** {@link BrentState_.evaluate}: `f(x) − level` into `value`. */
|
|
168
|
+
evaluate() {
|
|
169
|
+
this.sample_();
|
|
170
|
+
this.value -= this.level_;
|
|
171
|
+
}
|
|
172
|
+
/** Searches `[intervalLower, intervalUpper]` for the levels set. */
|
|
173
|
+
run(routine, f, options) {
|
|
174
|
+
const lower = this.intervalLower;
|
|
175
|
+
const upper = this.intervalUpper;
|
|
176
|
+
const { maxRate, budget, signal } = options;
|
|
177
|
+
const integer = options.integer ?? false;
|
|
178
|
+
const tolerance = options.tolerance ?? 0;
|
|
179
|
+
if (!Number.isFinite(lower) || !Number.isFinite(upper) || lower > upper) {
|
|
180
|
+
throw new RangeError(`${routine}: expected a finite interval with lower ≤ upper, got [${describeValue(lower)}, ${describeValue(upper)}]`);
|
|
181
|
+
}
|
|
182
|
+
if (!Number.isFinite(upper - lower)) {
|
|
183
|
+
throw new RangeError(`${routine}: the interval [${lower}, ${upper}] is wider than binary64 can represent`);
|
|
184
|
+
}
|
|
185
|
+
if (integer &&
|
|
186
|
+
(!Number.isSafeInteger(lower) || !Number.isSafeInteger(upper))) {
|
|
187
|
+
throw new RangeError(`${routine}: integer mode needs safe-integer ends, got [${lower}, ${upper}]`);
|
|
188
|
+
}
|
|
189
|
+
if (!Number.isFinite(maxRate) || !(maxRate > 0)) {
|
|
190
|
+
throw new RangeError(`${routine}: maxRate must be finite and positive, got ${describeValue(maxRate)}`);
|
|
191
|
+
}
|
|
192
|
+
const resolution = options.resolution ??
|
|
193
|
+
(integer
|
|
194
|
+
? 1
|
|
195
|
+
: Math.max((upper - lower) * DEFAULT_RELATIVE_RESOLUTION_, Number.MIN_VALUE));
|
|
196
|
+
if (!Number.isFinite(resolution) || !(resolution > 0)) {
|
|
197
|
+
throw new RangeError(`${routine}: resolution must be finite and positive, got ${describeValue(resolution)}`);
|
|
198
|
+
}
|
|
199
|
+
if (!Number.isFinite(tolerance) || tolerance < 0) {
|
|
200
|
+
throw new RangeError(`${routine}: tolerance must be finite and non-negative, got ${describeValue(tolerance)}`);
|
|
201
|
+
}
|
|
202
|
+
this.routine_ = routine;
|
|
203
|
+
this.maxRate_ = maxRate;
|
|
204
|
+
this.resolution_ = resolution;
|
|
205
|
+
this.integer_ = integer;
|
|
206
|
+
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
|
+
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
|
+
const count = this.levelCount;
|
|
226
|
+
const levels = this.levels;
|
|
227
|
+
const lower = this.intervalLower;
|
|
228
|
+
const upper = this.intervalUpper;
|
|
229
|
+
this.x = lower;
|
|
230
|
+
this.sample_();
|
|
231
|
+
const lowerValue = this.value;
|
|
232
|
+
for (let index = 0; index < count; index++) {
|
|
233
|
+
this.startSides[index] = lowerValue >= levels[index] ? 1 : -1;
|
|
234
|
+
}
|
|
235
|
+
if (lower === upper || count === 0) {
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
this.x = upper;
|
|
239
|
+
this.sample_();
|
|
240
|
+
this.reserve_(NODE_STRIDE_);
|
|
241
|
+
let stack = this.stack_;
|
|
242
|
+
stack[0] = lower;
|
|
243
|
+
stack[1] = upper;
|
|
244
|
+
stack[2] = lowerValue;
|
|
245
|
+
stack[3] = this.value;
|
|
246
|
+
stack[4] = lower;
|
|
247
|
+
stack[5] = upper;
|
|
248
|
+
let top = NODE_STRIDE_;
|
|
249
|
+
while (top > 0) {
|
|
250
|
+
top -= NODE_STRIDE_;
|
|
251
|
+
stack = this.stack_;
|
|
252
|
+
this.nodeA_ = stack[top];
|
|
253
|
+
this.nodeB_ = stack[top + 1];
|
|
254
|
+
this.nodeFa_ = stack[top + 2];
|
|
255
|
+
this.nodeFb_ = stack[top + 3];
|
|
256
|
+
this.clipStart_ = stack[top + 4];
|
|
257
|
+
this.clipEnd_ = stack[top + 5];
|
|
258
|
+
const decision = this.analyse_();
|
|
259
|
+
if (decision === LEAF_) {
|
|
260
|
+
this.leaf_();
|
|
261
|
+
}
|
|
262
|
+
else if (decision === SPLIT_) {
|
|
263
|
+
const middle = this.middle_;
|
|
264
|
+
this.x = middle;
|
|
265
|
+
this.sample_();
|
|
266
|
+
this.reserve_(top + 2 * NODE_STRIDE_);
|
|
267
|
+
stack = this.stack_;
|
|
268
|
+
// The right child below the left, so the left is taken first and
|
|
269
|
+
// everything is emitted in order.
|
|
270
|
+
stack[top] = middle;
|
|
271
|
+
stack[top + 1] = this.nodeB_;
|
|
272
|
+
stack[top + 2] = this.value;
|
|
273
|
+
stack[top + 3] = this.nodeFb_;
|
|
274
|
+
stack[top + 4] = this.hullStart_;
|
|
275
|
+
stack[top + 5] = this.hullEnd_;
|
|
276
|
+
stack[top + 6] = this.nodeA_;
|
|
277
|
+
stack[top + 7] = middle;
|
|
278
|
+
stack[top + 8] = this.nodeFa_;
|
|
279
|
+
stack[top + 9] = this.value;
|
|
280
|
+
stack[top + 10] = this.hullStart_;
|
|
281
|
+
stack[top + 11] = this.hullEnd_;
|
|
282
|
+
top += 2 * NODE_STRIDE_;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Finds the node's candidate levels, `first_` to `end_`, and decides whether
|
|
288
|
+
* it is settled, split at `middle_` with the children clipped to
|
|
289
|
+
* `hullStart_`/`hullEnd_`, or a leaf.
|
|
290
|
+
*/
|
|
291
|
+
analyse_() {
|
|
292
|
+
const a = this.nodeA_;
|
|
293
|
+
const b = this.nodeB_;
|
|
294
|
+
const fa = this.nodeFa_;
|
|
295
|
+
const fb = this.nodeFb_;
|
|
296
|
+
const levels = this.levels;
|
|
297
|
+
const count = this.levelCount;
|
|
298
|
+
const width = b - a;
|
|
299
|
+
const reach = this.maxRate_ * width;
|
|
300
|
+
const margin = MARGIN_ULPS_ * (Math.abs(fa) + Math.abs(fb) + reach);
|
|
301
|
+
this.margin_ = margin;
|
|
302
|
+
let first = bisectRightNumbers(levels, 0.5 * (fa + fb - reach) - margin, 0, count);
|
|
303
|
+
if (first > 0) {
|
|
304
|
+
first--;
|
|
305
|
+
}
|
|
306
|
+
const end = bisectRightNumbers(levels, 0.5 * (fa + fb + reach) + margin, first, count);
|
|
307
|
+
this.first_ = first;
|
|
308
|
+
this.end_ = end;
|
|
309
|
+
let uncertain = false;
|
|
310
|
+
let hullStart = Number.POSITIVE_INFINITY;
|
|
311
|
+
let hullEnd = Number.NEGATIVE_INFINITY;
|
|
312
|
+
for (let index = first; index < end; index++) {
|
|
313
|
+
if (this.classify_(index) === CERTIFIED_) {
|
|
314
|
+
continue;
|
|
315
|
+
}
|
|
316
|
+
uncertain = true;
|
|
317
|
+
if (this.coreStart_ <= this.coreEnd_) {
|
|
318
|
+
hullStart = Math.min(hullStart, this.coreStart_);
|
|
319
|
+
hullEnd = Math.max(hullEnd, this.coreEnd_);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
if (!uncertain) {
|
|
323
|
+
return SETTLED_;
|
|
324
|
+
}
|
|
325
|
+
// Integer mode splits at an interior integer, so needs a width of 2.
|
|
326
|
+
const integer = this.integer_;
|
|
327
|
+
if (width < (integer ? Math.max(this.resolution_, 2) : this.resolution_)) {
|
|
328
|
+
return LEAF_;
|
|
329
|
+
}
|
|
330
|
+
const middle = integer ? a + Math.floor(width / 2) : 0.5 * a + 0.5 * b;
|
|
331
|
+
if (!(middle > a && middle < b)) {
|
|
332
|
+
return LEAF_;
|
|
333
|
+
}
|
|
334
|
+
// Only sign changes with no core: maxRate does not hold here, so the
|
|
335
|
+
// hull certifies nothing and the parent's clip stays.
|
|
336
|
+
if (hullStart > hullEnd) {
|
|
337
|
+
hullStart = this.clipStart_;
|
|
338
|
+
hullEnd = this.clipEnd_;
|
|
339
|
+
}
|
|
340
|
+
this.middle_ = middle;
|
|
341
|
+
this.hullStart_ = hullStart;
|
|
342
|
+
this.hullEnd_ = hullEnd;
|
|
343
|
+
return SPLIT_;
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Classifies level `index` on the node and leaves its core, clipped to the
|
|
347
|
+
* parent's hull, in `coreStart_`/`coreEnd_` (empty when start > end).
|
|
348
|
+
*/
|
|
349
|
+
classify_(index) {
|
|
350
|
+
const level = this.levels[index];
|
|
351
|
+
const a = this.nodeA_;
|
|
352
|
+
const b = this.nodeB_;
|
|
353
|
+
const ga = this.nodeFa_ - level;
|
|
354
|
+
const gb = this.nodeFb_ - level;
|
|
355
|
+
const margin = this.margin_;
|
|
356
|
+
const maxRate = this.maxRate_;
|
|
357
|
+
let start = Math.max(this.clipStart_, a + Math.max(0, Math.abs(ga) - margin) / maxRate);
|
|
358
|
+
let end = Math.min(this.clipEnd_, b - Math.max(0, Math.abs(gb) - margin) / maxRate);
|
|
359
|
+
if (this.integer_) {
|
|
360
|
+
start = Math.max(a + 1, Math.ceil(start));
|
|
361
|
+
end = Math.min(b - 1, Math.floor(end));
|
|
362
|
+
}
|
|
363
|
+
this.coreStart_ = start;
|
|
364
|
+
this.coreEnd_ = end;
|
|
365
|
+
if (ga >= 0 !== gb >= 0) {
|
|
366
|
+
return SIGN_CHANGE_;
|
|
367
|
+
}
|
|
368
|
+
return start <= end ? UNCERTIFIED_ : CERTIFIED_;
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* Emits the leaf's crossings and unresolved cores. Every net crossing in a
|
|
372
|
+
* leaf goes the same way, from fa's side to fb's, so the levels are visited
|
|
373
|
+
* in the order f passes them.
|
|
374
|
+
*/
|
|
375
|
+
leaf_() {
|
|
376
|
+
const first = this.first_;
|
|
377
|
+
const end = this.end_;
|
|
378
|
+
const rising = this.nodeFb_ > this.nodeFa_;
|
|
379
|
+
for (let step = 0; step < end - first; step++) {
|
|
380
|
+
const index = rising ? first + step : end - 1 - step;
|
|
381
|
+
const kind = this.classify_(index);
|
|
382
|
+
if (kind === SIGN_CHANGE_) {
|
|
383
|
+
this.refine_(index);
|
|
384
|
+
}
|
|
385
|
+
else if (kind === UNCERTIFIED_) {
|
|
386
|
+
this.addUncertain_(index);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
/** Refines the crossing of level `levelIndex` in the leaf, and emits it. */
|
|
391
|
+
refine_(levelIndex) {
|
|
392
|
+
const level = this.levels[levelIndex];
|
|
393
|
+
const a = this.nodeA_;
|
|
394
|
+
const b = this.nodeB_;
|
|
395
|
+
const fa = this.nodeFa_;
|
|
396
|
+
const fb = this.nodeFb_;
|
|
397
|
+
// Where every zero of the leaf lies when maxRate holds; all of the leaf
|
|
398
|
+
// when it does not (an empty core with a sign change).
|
|
399
|
+
let earliest = this.coreStart_;
|
|
400
|
+
let latest = this.coreEnd_;
|
|
401
|
+
this.level_ = level;
|
|
402
|
+
let time;
|
|
403
|
+
if (this.integer_) {
|
|
404
|
+
this.oldSideAbove_ = fa >= level;
|
|
405
|
+
time = binarySearch(a + 1, b, this.onOldSide_);
|
|
406
|
+
if (earliest > latest) {
|
|
407
|
+
earliest = a + 1;
|
|
408
|
+
latest = b;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
else {
|
|
412
|
+
this.lower = a;
|
|
413
|
+
this.upper = b;
|
|
414
|
+
this.lowerValue = fa - level;
|
|
415
|
+
this.upperValue = fb - level;
|
|
416
|
+
brentKernel_(this, Number.POSITIVE_INFINITY, this.routine_);
|
|
417
|
+
time = this.root;
|
|
418
|
+
if (earliest > latest) {
|
|
419
|
+
earliest = a;
|
|
420
|
+
latest = b;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
const count = this.crossingCount;
|
|
424
|
+
// Several levels crossed in one leaf: keep their times in level order.
|
|
425
|
+
if (count > 0 && time < this.crossingTimes[count - 1]) {
|
|
426
|
+
time = this.crossingTimes[count - 1];
|
|
427
|
+
}
|
|
428
|
+
earliest = Math.min(earliest, time);
|
|
429
|
+
latest = Math.max(latest, time);
|
|
430
|
+
if (this.lastKinds_[levelIndex] === UNRESOLVED_) {
|
|
431
|
+
const absorbed = this.lastIndices_[levelIndex];
|
|
432
|
+
if (earliest - this.unresolvedEnds[absorbed] <= this.resolution_) {
|
|
433
|
+
earliest = Math.min(earliest, this.unresolvedStarts[absorbed]);
|
|
434
|
+
this.unresolvedLevels[absorbed] = ABSORBED_;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
if (count === this.crossingTimes.length) {
|
|
438
|
+
this.crossingTimes = grownFloat64_(this.crossingTimes, count + 1);
|
|
439
|
+
this.crossingEarliest = grownFloat64_(this.crossingEarliest, count + 1);
|
|
440
|
+
this.crossingLatest = grownFloat64_(this.crossingLatest, count + 1);
|
|
441
|
+
this.crossingDirections = grownInt8_(this.crossingDirections, count + 1);
|
|
442
|
+
this.crossingLevels = grownInt32_(this.crossingLevels, count + 1);
|
|
443
|
+
}
|
|
444
|
+
this.crossingTimes[count] = time;
|
|
445
|
+
this.crossingEarliest[count] = earliest;
|
|
446
|
+
this.crossingLatest[count] = latest;
|
|
447
|
+
this.crossingDirections[count] = fb >= level ? 1 : -1;
|
|
448
|
+
this.crossingLevels[count] = levelIndex;
|
|
449
|
+
this.crossingCount = count + 1;
|
|
450
|
+
this.lastKinds_[levelIndex] = CROSSING_;
|
|
451
|
+
this.lastIndices_[levelIndex] = count;
|
|
452
|
+
}
|
|
453
|
+
/** The core of a same-side level joins its last crossing or unresolved run. */
|
|
454
|
+
addUncertain_(levelIndex) {
|
|
455
|
+
const start = this.coreStart_;
|
|
456
|
+
const end = this.coreEnd_;
|
|
457
|
+
const lastKind = this.lastKinds_[levelIndex];
|
|
458
|
+
const last = this.lastIndices_[levelIndex];
|
|
459
|
+
if (lastKind === CROSSING_ &&
|
|
460
|
+
start - this.crossingLatest[last] <= this.resolution_) {
|
|
461
|
+
this.crossingLatest[last] = Math.max(this.crossingLatest[last], end);
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
if (lastKind === UNRESOLVED_ &&
|
|
465
|
+
start - this.unresolvedEnds[last] <= this.resolution_) {
|
|
466
|
+
this.unresolvedEnds[last] = Math.max(this.unresolvedEnds[last], end);
|
|
467
|
+
return;
|
|
468
|
+
}
|
|
469
|
+
const index = this.unresolvedCount;
|
|
470
|
+
if (index === this.unresolvedStarts.length) {
|
|
471
|
+
this.unresolvedStarts = grownFloat64_(this.unresolvedStarts, index + 1);
|
|
472
|
+
this.unresolvedEnds = grownFloat64_(this.unresolvedEnds, index + 1);
|
|
473
|
+
this.unresolvedLevels = grownInt32_(this.unresolvedLevels, index + 1);
|
|
474
|
+
}
|
|
475
|
+
this.unresolvedStarts[index] = start;
|
|
476
|
+
this.unresolvedEnds[index] = end;
|
|
477
|
+
this.unresolvedLevels[index] = levelIndex;
|
|
478
|
+
this.unresolvedCount = index + 1;
|
|
479
|
+
this.lastKinds_[levelIndex] = UNRESOLVED_;
|
|
480
|
+
this.lastIndices_[levelIndex] = index;
|
|
481
|
+
}
|
|
482
|
+
/**
|
|
483
|
+
* Drops the absorbed entries and sorts by start: entries come in leaf order,
|
|
484
|
+
* so only those of one leaf, for different levels, can be out of order.
|
|
485
|
+
*/
|
|
486
|
+
finishUnresolved_() {
|
|
487
|
+
const starts = this.unresolvedStarts;
|
|
488
|
+
const ends = this.unresolvedEnds;
|
|
489
|
+
const levels = this.unresolvedLevels;
|
|
490
|
+
let kept = 0;
|
|
491
|
+
for (let index = 0; index < this.unresolvedCount; index++) {
|
|
492
|
+
const level = levels[index];
|
|
493
|
+
if (level === ABSORBED_) {
|
|
494
|
+
continue;
|
|
495
|
+
}
|
|
496
|
+
const start = starts[index];
|
|
497
|
+
const end = ends[index];
|
|
498
|
+
let target = kept;
|
|
499
|
+
while (target > 0 &&
|
|
500
|
+
(starts[target - 1] > start ||
|
|
501
|
+
(starts[target - 1] === start && ends[target - 1] > end))) {
|
|
502
|
+
starts[target] = starts[target - 1];
|
|
503
|
+
ends[target] = ends[target - 1];
|
|
504
|
+
levels[target] = levels[target - 1];
|
|
505
|
+
target--;
|
|
506
|
+
}
|
|
507
|
+
starts[target] = start;
|
|
508
|
+
ends[target] = end;
|
|
509
|
+
levels[target] = level;
|
|
510
|
+
kept++;
|
|
511
|
+
}
|
|
512
|
+
this.unresolvedCount = kept;
|
|
513
|
+
}
|
|
514
|
+
reserve_(length) {
|
|
515
|
+
if (length > this.stack_.length) {
|
|
516
|
+
this.stack_ = grownFloat64_(this.stack_, length);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
/** `f(x)` into `value`, checked, counted and charged. */
|
|
520
|
+
sample_() {
|
|
521
|
+
this.signal_?.throwIfAborted();
|
|
522
|
+
this.budget_?.spend(1);
|
|
523
|
+
this.evaluations++;
|
|
524
|
+
const { x } = this;
|
|
525
|
+
const value = this.f_(x);
|
|
526
|
+
if (!Number.isFinite(value)) {
|
|
527
|
+
throw new RangeError(`${this.routine_}: f(${x}) is ${describeValue(value)}, not a finite number`);
|
|
528
|
+
}
|
|
529
|
+
this.value = value;
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
//# sourceMappingURL=_level-search.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_level-search.js","sourceRoot":"","sources":["../../src/roots/_level-search.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAChE,OAAO,EAEN,aAAa,GAGb,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAoB,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACpE,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAGrE,+EAA+E;AAC/E,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,CAAC,CAAC;AACnC,+EAA+E;AAC/E,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,CAAC,CAAC;AAEnC,MAAM,UAAU,GAAG,CAAC,CAAC;AACrB,MAAM,YAAY,GAAG,CAAC,CAAC;AACvB,MAAM,YAAY,GAAG,CAAC,CAAC;AACvB,wCAAwC;AACxC,MAAM,QAAQ,GAAG,CAAC,CAAC;AACnB,MAAM,MAAM,GAAG,CAAC,CAAC;AACjB,MAAM,KAAK,GAAG,CAAC,CAAC;AAChB,mEAAmE;AACnE,MAAM,QAAQ,GAAG,CAAC,CAAC;AACnB,MAAM,SAAS,GAAG,CAAC,CAAC;AACpB,MAAM,WAAW,GAAG,CAAC,CAAC;AACtB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C;YACD,MAAM,CAAC,MAAM,CAAC,GAAG,KAAK,CAAC;YACvB,IAAI,CAAC,MAAM,CAAC,GAAG,GAAG,CAAC;YACnB,MAAM,CAAC,MAAM,CAAC,GAAG,KAAK,CAAC;YACvB,IAAI,EAAE,CAAC;QACR,CAAC;QACD,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC;IAC7B,CAAC;IAEO,QAAQ,CAAC,MAAc;QAC9B,IAAI,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;YACjC,IAAI,CAAC,MAAM,GAAG,aAAa,CAAC,IAAI,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAClD,CAAC;IACF,CAAC;IAED,yDAAyD;IACjD,OAAO;QACd,IAAI,CAAC,OAAO,EAAE,cAAc,EAAE,CAAC;QAC/B,IAAI,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC;QACvB,IAAI,CAAC,WAAW,EAAE,CAAC;QACnB,MAAM,EAAE,CAAC,EAAE,GAAG,IAAI,CAAC;QACnB,MAAM,KAAK,GAAG,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;QACzB,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;YAC7B,MAAM,IAAI,UAAU,CACnB,GAAG,IAAI,CAAC,QAAQ,OAAO,CAAC,QAAQ,aAAa,CAAC,KAAK,CAAC,uBAAuB,CAC3E,CAAC;QACH,CAAC;QACD,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACpB,CAAC;CACD"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { type Callable } from "@ac-kit/core";
|
|
2
|
+
import type { ConvergenceOptions } from "../convergence-options.js";
|
|
3
|
+
/**
|
|
4
|
+
* Finds where a function crosses zero inside a bracket, with Brent's 1973
|
|
5
|
+
* algorithm: bisection's guarantee at the speed of interpolation.
|
|
6
|
+
*
|
|
7
|
+
* Each step tries inverse quadratic interpolation through the last three points
|
|
8
|
+
* (or the secant through two), and falls back to bisection whenever the
|
|
9
|
+
* interpolated point would leave the bracket or shrink it too slowly. The root
|
|
10
|
+
* therefore stays bracketed throughout, the convergence is superlinear (order
|
|
11
|
+
* about 1.84) on a smooth simple root, and it is never much slower than
|
|
12
|
+
* bisection on anything else. It is the algorithm behind MATLAB's `fzero` and
|
|
13
|
+
* `scipy.optimize.brentq`, and the root finder to reach for here.
|
|
14
|
+
*
|
|
15
|
+
* **Tolerance.** Brent's: iteration stops once the bracket `[b, c]` satisfies
|
|
16
|
+
* `|c − b| / 2 ≤ 2ε|b| + tolerance`, ε being `Number.EPSILON`, and returns the
|
|
17
|
+
* end `b` with the smaller `|f|`. The root is then within `4ε|b| + 2 ·
|
|
18
|
+
* tolerance` of the result; Brent states the bound as `6ε|x| + 2t`. The
|
|
19
|
+
* relative part is fixed, so `tolerance: 0`, the default, asks for the bracket
|
|
20
|
+
* binary64 can resolve: a couple of ulps of the root, or the smallest subnormal
|
|
21
|
+
* at a root at zero.
|
|
22
|
+
*
|
|
23
|
+
* **Termination.** Guaranteed without a cap: Brent proves that it never takes
|
|
24
|
+
* more than about N² evaluations, N being the number bisection needs for the
|
|
25
|
+
* same bracket and tolerance; on a smooth simple root it takes far fewer than
|
|
26
|
+
* N. `maxIterations` only adds a cap of the caller's own.
|
|
27
|
+
*
|
|
28
|
+
* Accepts the bracket in either order. `f` may return ±`Infinity` (only its
|
|
29
|
+
* sign is used near a pole); `NaN` has no sign and throws.
|
|
30
|
+
*
|
|
31
|
+
* O(1) memory; one evaluation per iteration, two for the bracket.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* brent((x) => x * x - 2, 0, 2); // 1.414213562373095, an ulp from √2
|
|
36
|
+
* brent(Math.cos, 0, 3); // 1.5707963267948966
|
|
37
|
+
* brent((x) => x * x - 2, 2, 3); // throws RangeError — no sign change
|
|
38
|
+
* ```;
|
|
39
|
+
*
|
|
40
|
+
* @param f The function whose zero is sought, continuous on the bracket for the
|
|
41
|
+
* result to be a root rather than a jump.
|
|
42
|
+
* @param lower One end of the bracket, finite.
|
|
43
|
+
* @param upper The other end, finite.
|
|
44
|
+
* @param options `tolerance`: the absolute part `t` of the stopping test, `≥
|
|
45
|
+
* 0`, default `0`. `maxIterations`: evaluations after the two at the ends, a
|
|
46
|
+
* non-negative integer or `Infinity`, the default. `signal`: checked before
|
|
47
|
+
* every evaluation.
|
|
48
|
+
* @returns A point within `4ε|x| + 2 · tolerance` of a zero of `f`, or an end
|
|
49
|
+
* of the bracket where `f` is exactly zero.
|
|
50
|
+
* @throws {RangeError} When an end is not finite, `tolerance` is negative or
|
|
51
|
+
* not finite, `maxIterations` is not a non-negative integer or `Infinity`,
|
|
52
|
+
* `f` returns `NaN`, or `f(lower)` and `f(upper)` are both non-zero with the
|
|
53
|
+
* same sign — nothing guarantees a root between them.
|
|
54
|
+
* @throws {ConvergenceError} When `maxIterations` runs out first.
|
|
55
|
+
* @throws The signal's reason, once it is aborted.
|
|
56
|
+
* @see [Root finding](https://anthochamp.pages.grenadine.io/node-essentials/topics/math/calculus/root-finding/)
|
|
57
|
+
*/
|
|
58
|
+
export declare function brent(f: Callable<[x: number], number>, lower: number, upper: number, options?: ConvergenceOptions): number;
|
|
59
|
+
//# sourceMappingURL=brent.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"brent.d.ts","sourceRoot":"","sources":["../../src/roots/brent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,QAAQ,EAAiB,MAAM,cAAc,CAAC;AAE5D,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AA6BpE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsDG;AACH,wBAAgB,KAAK,CACpB,CAAC,EAAE,QAAQ,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,EAChC,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,EACb,OAAO,CAAC,EAAE,kBAAkB,GAC1B,MAAM,CA4CR"}
|