@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,141 @@
|
|
|
1
|
+
import { type Callable } from "@ac-kit/core";
|
|
2
|
+
import { LevelSearch_ } from "./_level-search.js";
|
|
3
|
+
import type { LevelSearchOptions } from "./level-search-options.js";
|
|
4
|
+
/** {@link LevelSearchOptions}, plus the level whose crossings are sought. */
|
|
5
|
+
export type LevelCrossingsOptions = LevelSearchOptions & {
|
|
6
|
+
/** The level `f` is compared with, finite. Defaults to `0`. */
|
|
7
|
+
level?: number;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Where a function crosses one level, as columns: what
|
|
11
|
+
* {@link findLevelCrossings} writes, and may write again into the same object
|
|
12
|
+
* without allocating once its columns are large enough.
|
|
13
|
+
*
|
|
14
|
+
* A point is **above** when `f ≥ level` and **below** when `f < level`, so a
|
|
15
|
+
* level reached exactly counts as above — the half-open convention of
|
|
16
|
+
* {@link findLevelBands}. Only the first {@link count} entries of {@link times}
|
|
17
|
+
* and {@link directions}, and the first {@link unresolvedCount} of the
|
|
18
|
+
* unresolved columns, are meaningful; the columns are reallocated when they
|
|
19
|
+
* grow, so read them again after each search rather than keeping them.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* const crossings = findLevelCrossings(Math.sin, 0, 10, { maxRate: 1 });
|
|
24
|
+
* for (let index = 0; index < crossings.count; index++) {
|
|
25
|
+
* console.log(crossings.times[index], crossings.directions[index]);
|
|
26
|
+
* }
|
|
27
|
+
* // 3.141592653589793 -1, 6.283185307179586 1, 9.42477796076938 -1
|
|
28
|
+
* ```;
|
|
29
|
+
*/
|
|
30
|
+
export declare class LevelCrossings {
|
|
31
|
+
/** @internal The search state and its buffers. */
|
|
32
|
+
readonly search: LevelSearch_;
|
|
33
|
+
/** `1` when `f(lower) ≥ level`, else `-1`. */
|
|
34
|
+
get startSide(): 1 | -1;
|
|
35
|
+
/** How many crossings were found. */
|
|
36
|
+
get count(): number;
|
|
37
|
+
/**
|
|
38
|
+
* Crossing times, non-decreasing. Real mode: a zero of `f − level` refined by
|
|
39
|
+
* {@link brent}. Integer mode: the first integer on the new side.
|
|
40
|
+
*/
|
|
41
|
+
get times(): Float64Array;
|
|
42
|
+
/**
|
|
43
|
+
* Per crossing, the start of its closed enclosure `[earliest, latest]`, which
|
|
44
|
+
* holds the reported time and every zero of its cluster: `f` is certified on
|
|
45
|
+
* the old side between the previous item and `earliest`, and on the new side
|
|
46
|
+
* between `latest` and the next. Narrower than `resolution` where `f` crosses
|
|
47
|
+
* at a slope near `maxRate`; wider where it crosses slowly, since a bound
|
|
48
|
+
* well above the slope cannot rule out a brief return near the crossing.
|
|
49
|
+
*/
|
|
50
|
+
get earliestTimes(): Float64Array;
|
|
51
|
+
/** Per crossing, the end of its enclosure; see {@link earliestTimes}. */
|
|
52
|
+
get latestTimes(): Float64Array;
|
|
53
|
+
/** Per crossing, `1` going from below to above, `-1` the other way. */
|
|
54
|
+
get directions(): Int8Array;
|
|
55
|
+
/** How many unresolved intervals were found. */
|
|
56
|
+
get unresolvedCount(): number;
|
|
57
|
+
/**
|
|
58
|
+
* Starts of the closed unresolved intervals, ascending: stretches away from
|
|
59
|
+
* any crossing where `f` may touch or briefly cross the level and come back,
|
|
60
|
+
* which `maxRate` and `resolution` cannot decide, merged when less than
|
|
61
|
+
* `resolution` apart. Never guessed: the side of `f` outside them and the
|
|
62
|
+
* crossings' enclosures is certified.
|
|
63
|
+
*/
|
|
64
|
+
get unresolvedStarts(): Float64Array;
|
|
65
|
+
/** Ends of the closed unresolved intervals. */
|
|
66
|
+
get unresolvedEnds(): Float64Array;
|
|
67
|
+
/** How many times the last search called `f`. */
|
|
68
|
+
get evaluations(): number;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Every time a function crosses a level in `[lower, upper]`, **certified**:
|
|
72
|
+
* given a true bound on `|f′|`, no crossing is missed.
|
|
73
|
+
*
|
|
74
|
+
* Sampling at a fixed step misses any pair of crossings closer than the step,
|
|
75
|
+
* and nothing tells it so. A rate bound closes that gap: between two samples
|
|
76
|
+
* `a` and `b` with `|f(a) − level| + |f(b) − level| > maxRate · (b − a)`, the
|
|
77
|
+
* two cones of slope `maxRate` cover the interval, so `f` cannot reach the
|
|
78
|
+
* level in it. Intervals that pass are skipped whole; the rest are bisected
|
|
79
|
+
* down to `resolution`. Each sign change left is refined by {@link brent} (or,
|
|
80
|
+
* with `integer`, to the first integer on the new side by `@ac-kit/algo`'s
|
|
81
|
+
* `binarySearch`), and each stretch without one that still cannot be certified
|
|
82
|
+
* — a tangent touch, or a brief excursion and return — is reported as
|
|
83
|
+
* **unresolved**, never guessed either way.
|
|
84
|
+
*
|
|
85
|
+
* **The guarantee.** When `maxRate` truly bounds `|f′|`, every zero of `f −
|
|
86
|
+
* level` lies in a reported crossing's enclosure or in an unresolved interval,
|
|
87
|
+
* and the side of `f` is certified everywhere else. A crossing stands for an
|
|
88
|
+
* odd cluster: zeros that no sample separates (closer than `resolution`, or
|
|
89
|
+
* hidden near a slow crossing) count once, and its enclosure says where all of
|
|
90
|
+
* them lie. An even cluster away from any crossing — a touch, or a dip and
|
|
91
|
+
* return — is an unresolved interval. When `maxRate` is too small, or `f`
|
|
92
|
+
* jumps: a sign change between samples is still found and refined (to the jump,
|
|
93
|
+
* for a discontinuity), but a crossing and its return inside an interval the
|
|
94
|
+
* false bound certified are missed. Every reported crossing is a real sign
|
|
95
|
+
* change of `f` in either case, and directions always alternate, starting from
|
|
96
|
+
* {@link LevelCrossings.startSide}. A start exactly on the level, as `sin` at
|
|
97
|
+
* `0`, is above but cannot be told from a dip that follows at once unless
|
|
98
|
+
* `maxRate` exceeds the slope there, and is then reported unresolved.
|
|
99
|
+
*
|
|
100
|
+
* **Termination.** At most `2 + 2 · (upper − lower) / resolution` evaluations
|
|
101
|
+
* isolate the crossings (`4 ·`, with `resolution` at least `2`, in integer
|
|
102
|
+
* mode), since the bisection never splits an interval narrower than
|
|
103
|
+
* `resolution`. Far fewer in practice: a stretch where `|f − level| ≥ c` is
|
|
104
|
+
* certified in pieces `2c / maxRate` wide, so the cost scales as `(upper −
|
|
105
|
+
* lower) · maxRate / c`, plus `log₂(spacing / resolution)` bisections per
|
|
106
|
+
* crossing. Each crossing then costs Brent's own bound (at most about N²
|
|
107
|
+
* evaluations, N being bisection's, over a bracket narrower than `resolution`)
|
|
108
|
+
* or `⌈log₂ resolution⌉` in integer mode.
|
|
109
|
+
*
|
|
110
|
+
* O(depth) memory: the subdivision runs on an explicit stack. With `out`
|
|
111
|
+
* reused, nothing is allocated once its columns have grown to the result (`f`'s
|
|
112
|
+
* own allocations aside).
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* ```ts
|
|
116
|
+
* const crossings = findLevelCrossings(Math.sin, 0, 10, { maxRate: 1 });
|
|
117
|
+
* crossings.count; // 3
|
|
118
|
+
* crossings.startSide; // 1, since sin(0) = 0 counts as above
|
|
119
|
+
* // 1: just after 0, sin x < x, so a rate of 1 cannot rule out a dip.
|
|
120
|
+
* crossings.unresolvedCount;
|
|
121
|
+
* ```;
|
|
122
|
+
*
|
|
123
|
+
* @param f The function, finite wherever it is called.
|
|
124
|
+
* @param lower Start of the interval, finite.
|
|
125
|
+
* @param upper End of the interval, finite and `≥ lower`.
|
|
126
|
+
* @param options The rate bound, the level and how finely to search; see
|
|
127
|
+
* {@link LevelCrossingsOptions}.
|
|
128
|
+
* @param out A result to overwrite instead of allocating one; it must not be in
|
|
129
|
+
* use by an enclosing search (from inside `f`).
|
|
130
|
+
* @returns `out`, or a new {@link LevelCrossings}. Unspecified after a throw.
|
|
131
|
+
* @throws {RangeError} When the interval is not finite, inverted or wider than
|
|
132
|
+
* binary64 represents; its ends are not safe integers in integer mode;
|
|
133
|
+
* `level` is not finite; `maxRate` or `resolution` is not finite and
|
|
134
|
+
* positive; `tolerance` is negative or not finite; or `f` returns a value
|
|
135
|
+
* that is not finite.
|
|
136
|
+
* @throws Whatever `budget.spend` throws — `WorkBudgetExceededError` for a
|
|
137
|
+
* `FixedWorkBudget` — and the signal's reason once it is aborted.
|
|
138
|
+
* @see {@link findLevelBands} for the stretches between two levels.
|
|
139
|
+
*/
|
|
140
|
+
export declare function findLevelCrossings(f: Callable<[x: number], number>, lower: number, upper: number, options: LevelCrossingsOptions, out?: LevelCrossings): LevelCrossings;
|
|
141
|
+
//# sourceMappingURL=find-level-crossings.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"find-level-crossings.d.ts","sourceRoot":"","sources":["../../src/roots/find-level-crossings.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,QAAQ,EAAiB,MAAM,cAAc,CAAC;AAE5D,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAEpE,6EAA6E;AAC7E,MAAM,MAAM,qBAAqB,GAAG,kBAAkB,GAAG;IACxD,+DAA+D;IAC/D,KAAK,CAAC,EAAE,MAAM,CAAC;CACf,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,qBAAa,cAAc;IAC1B,kDAAkD;IAClD,QAAQ,CAAC,MAAM,eAAsB;IAErC,8CAA8C;IAC9C,IAAI,SAAS,IAAI,CAAC,GAAG,CAAC,CAAC,CAEtB;IAED,qCAAqC;IACrC,IAAI,KAAK,IAAI,MAAM,CAElB;IAED;;;OAGG;IACH,IAAI,KAAK,IAAI,YAAY,CAExB;IAED;;;;;;;OAOG;IACH,IAAI,aAAa,IAAI,YAAY,CAEhC;IAED,yEAAyE;IACzE,IAAI,WAAW,IAAI,YAAY,CAE9B;IAED,uEAAuE;IACvE,IAAI,UAAU,IAAI,SAAS,CAE1B;IAED,gDAAgD;IAChD,IAAI,eAAe,IAAI,MAAM,CAE5B;IAED;;;;;;OAMG;IACH,IAAI,gBAAgB,IAAI,YAAY,CAEnC;IAED,+CAA+C;IAC/C,IAAI,cAAc,IAAI,YAAY,CAEjC;IAED,iDAAiD;IACjD,IAAI,WAAW,IAAI,MAAM,CAExB;CACD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqEG;AACH,wBAAgB,kBAAkB,CACjC,CAAC,EAAE,QAAQ,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,EAChC,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,qBAAqB,EAC9B,GAAG,GAAE,cAAqC,GACxC,cAAc,CAehB"}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { describeValue } from "@ac-kit/core";
|
|
2
|
+
import { LevelSearch_ } from "./_level-search.js";
|
|
3
|
+
/**
|
|
4
|
+
* Where a function crosses one level, as columns: what
|
|
5
|
+
* {@link findLevelCrossings} writes, and may write again into the same object
|
|
6
|
+
* without allocating once its columns are large enough.
|
|
7
|
+
*
|
|
8
|
+
* A point is **above** when `f ≥ level` and **below** when `f < level`, so a
|
|
9
|
+
* level reached exactly counts as above — the half-open convention of
|
|
10
|
+
* {@link findLevelBands}. Only the first {@link count} entries of {@link times}
|
|
11
|
+
* and {@link directions}, and the first {@link unresolvedCount} of the
|
|
12
|
+
* unresolved columns, are meaningful; the columns are reallocated when they
|
|
13
|
+
* grow, so read them again after each search rather than keeping them.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* const crossings = findLevelCrossings(Math.sin, 0, 10, { maxRate: 1 });
|
|
18
|
+
* for (let index = 0; index < crossings.count; index++) {
|
|
19
|
+
* console.log(crossings.times[index], crossings.directions[index]);
|
|
20
|
+
* }
|
|
21
|
+
* // 3.141592653589793 -1, 6.283185307179586 1, 9.42477796076938 -1
|
|
22
|
+
* ```;
|
|
23
|
+
*/
|
|
24
|
+
export class LevelCrossings {
|
|
25
|
+
/** @internal The search state and its buffers. */
|
|
26
|
+
search = new LevelSearch_();
|
|
27
|
+
/** `1` when `f(lower) ≥ level`, else `-1`. */
|
|
28
|
+
get startSide() {
|
|
29
|
+
return this.search.startSides[0] === 1 ? 1 : -1;
|
|
30
|
+
}
|
|
31
|
+
/** How many crossings were found. */
|
|
32
|
+
get count() {
|
|
33
|
+
return this.search.crossingCount;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Crossing times, non-decreasing. Real mode: a zero of `f − level` refined by
|
|
37
|
+
* {@link brent}. Integer mode: the first integer on the new side.
|
|
38
|
+
*/
|
|
39
|
+
get times() {
|
|
40
|
+
return this.search.crossingTimes;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Per crossing, the start of its closed enclosure `[earliest, latest]`, which
|
|
44
|
+
* holds the reported time and every zero of its cluster: `f` is certified on
|
|
45
|
+
* the old side between the previous item and `earliest`, and on the new side
|
|
46
|
+
* between `latest` and the next. Narrower than `resolution` where `f` crosses
|
|
47
|
+
* at a slope near `maxRate`; wider where it crosses slowly, since a bound
|
|
48
|
+
* well above the slope cannot rule out a brief return near the crossing.
|
|
49
|
+
*/
|
|
50
|
+
get earliestTimes() {
|
|
51
|
+
return this.search.crossingEarliest;
|
|
52
|
+
}
|
|
53
|
+
/** Per crossing, the end of its enclosure; see {@link earliestTimes}. */
|
|
54
|
+
get latestTimes() {
|
|
55
|
+
return this.search.crossingLatest;
|
|
56
|
+
}
|
|
57
|
+
/** Per crossing, `1` going from below to above, `-1` the other way. */
|
|
58
|
+
get directions() {
|
|
59
|
+
return this.search.crossingDirections;
|
|
60
|
+
}
|
|
61
|
+
/** How many unresolved intervals were found. */
|
|
62
|
+
get unresolvedCount() {
|
|
63
|
+
return this.search.unresolvedCount;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Starts of the closed unresolved intervals, ascending: stretches away from
|
|
67
|
+
* any crossing where `f` may touch or briefly cross the level and come back,
|
|
68
|
+
* which `maxRate` and `resolution` cannot decide, merged when less than
|
|
69
|
+
* `resolution` apart. Never guessed: the side of `f` outside them and the
|
|
70
|
+
* crossings' enclosures is certified.
|
|
71
|
+
*/
|
|
72
|
+
get unresolvedStarts() {
|
|
73
|
+
return this.search.unresolvedStarts;
|
|
74
|
+
}
|
|
75
|
+
/** Ends of the closed unresolved intervals. */
|
|
76
|
+
get unresolvedEnds() {
|
|
77
|
+
return this.search.unresolvedEnds;
|
|
78
|
+
}
|
|
79
|
+
/** How many times the last search called `f`. */
|
|
80
|
+
get evaluations() {
|
|
81
|
+
return this.search.evaluations;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Every time a function crosses a level in `[lower, upper]`, **certified**:
|
|
86
|
+
* given a true bound on `|f′|`, no crossing is missed.
|
|
87
|
+
*
|
|
88
|
+
* Sampling at a fixed step misses any pair of crossings closer than the step,
|
|
89
|
+
* and nothing tells it so. A rate bound closes that gap: between two samples
|
|
90
|
+
* `a` and `b` with `|f(a) − level| + |f(b) − level| > maxRate · (b − a)`, the
|
|
91
|
+
* two cones of slope `maxRate` cover the interval, so `f` cannot reach the
|
|
92
|
+
* level in it. Intervals that pass are skipped whole; the rest are bisected
|
|
93
|
+
* down to `resolution`. Each sign change left is refined by {@link brent} (or,
|
|
94
|
+
* with `integer`, to the first integer on the new side by `@ac-kit/algo`'s
|
|
95
|
+
* `binarySearch`), and each stretch without one that still cannot be certified
|
|
96
|
+
* — a tangent touch, or a brief excursion and return — is reported as
|
|
97
|
+
* **unresolved**, never guessed either way.
|
|
98
|
+
*
|
|
99
|
+
* **The guarantee.** When `maxRate` truly bounds `|f′|`, every zero of `f −
|
|
100
|
+
* level` lies in a reported crossing's enclosure or in an unresolved interval,
|
|
101
|
+
* and the side of `f` is certified everywhere else. A crossing stands for an
|
|
102
|
+
* odd cluster: zeros that no sample separates (closer than `resolution`, or
|
|
103
|
+
* hidden near a slow crossing) count once, and its enclosure says where all of
|
|
104
|
+
* them lie. An even cluster away from any crossing — a touch, or a dip and
|
|
105
|
+
* return — is an unresolved interval. When `maxRate` is too small, or `f`
|
|
106
|
+
* jumps: a sign change between samples is still found and refined (to the jump,
|
|
107
|
+
* for a discontinuity), but a crossing and its return inside an interval the
|
|
108
|
+
* false bound certified are missed. Every reported crossing is a real sign
|
|
109
|
+
* change of `f` in either case, and directions always alternate, starting from
|
|
110
|
+
* {@link LevelCrossings.startSide}. A start exactly on the level, as `sin` at
|
|
111
|
+
* `0`, is above but cannot be told from a dip that follows at once unless
|
|
112
|
+
* `maxRate` exceeds the slope there, and is then reported unresolved.
|
|
113
|
+
*
|
|
114
|
+
* **Termination.** At most `2 + 2 · (upper − lower) / resolution` evaluations
|
|
115
|
+
* isolate the crossings (`4 ·`, with `resolution` at least `2`, in integer
|
|
116
|
+
* mode), since the bisection never splits an interval narrower than
|
|
117
|
+
* `resolution`. Far fewer in practice: a stretch where `|f − level| ≥ c` is
|
|
118
|
+
* certified in pieces `2c / maxRate` wide, so the cost scales as `(upper −
|
|
119
|
+
* lower) · maxRate / c`, plus `log₂(spacing / resolution)` bisections per
|
|
120
|
+
* crossing. Each crossing then costs Brent's own bound (at most about N²
|
|
121
|
+
* evaluations, N being bisection's, over a bracket narrower than `resolution`)
|
|
122
|
+
* or `⌈log₂ resolution⌉` in integer mode.
|
|
123
|
+
*
|
|
124
|
+
* O(depth) memory: the subdivision runs on an explicit stack. With `out`
|
|
125
|
+
* reused, nothing is allocated once its columns have grown to the result (`f`'s
|
|
126
|
+
* own allocations aside).
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```ts
|
|
130
|
+
* const crossings = findLevelCrossings(Math.sin, 0, 10, { maxRate: 1 });
|
|
131
|
+
* crossings.count; // 3
|
|
132
|
+
* crossings.startSide; // 1, since sin(0) = 0 counts as above
|
|
133
|
+
* // 1: just after 0, sin x < x, so a rate of 1 cannot rule out a dip.
|
|
134
|
+
* crossings.unresolvedCount;
|
|
135
|
+
* ```;
|
|
136
|
+
*
|
|
137
|
+
* @param f The function, finite wherever it is called.
|
|
138
|
+
* @param lower Start of the interval, finite.
|
|
139
|
+
* @param upper End of the interval, finite and `≥ lower`.
|
|
140
|
+
* @param options The rate bound, the level and how finely to search; see
|
|
141
|
+
* {@link LevelCrossingsOptions}.
|
|
142
|
+
* @param out A result to overwrite instead of allocating one; it must not be in
|
|
143
|
+
* use by an enclosing search (from inside `f`).
|
|
144
|
+
* @returns `out`, or a new {@link LevelCrossings}. Unspecified after a throw.
|
|
145
|
+
* @throws {RangeError} When the interval is not finite, inverted or wider than
|
|
146
|
+
* binary64 represents; its ends are not safe integers in integer mode;
|
|
147
|
+
* `level` is not finite; `maxRate` or `resolution` is not finite and
|
|
148
|
+
* positive; `tolerance` is negative or not finite; or `f` returns a value
|
|
149
|
+
* that is not finite.
|
|
150
|
+
* @throws Whatever `budget.spend` throws — `WorkBudgetExceededError` for a
|
|
151
|
+
* `FixedWorkBudget` — and the signal's reason once it is aborted.
|
|
152
|
+
* @see {@link findLevelBands} for the stretches between two levels.
|
|
153
|
+
*/
|
|
154
|
+
export function findLevelCrossings(f, lower, upper, options, out = new LevelCrossings()) {
|
|
155
|
+
const level = options.level ?? 0;
|
|
156
|
+
if (!Number.isFinite(level)) {
|
|
157
|
+
throw new RangeError(`findLevelCrossings: level must be finite, got ${describeValue(level)}`);
|
|
158
|
+
}
|
|
159
|
+
const { search } = out;
|
|
160
|
+
// Written in place: a level passed on as an argument could be boxed.
|
|
161
|
+
search.levels[0] = level;
|
|
162
|
+
search.levelCount = 1;
|
|
163
|
+
search.intervalLower = lower;
|
|
164
|
+
search.intervalUpper = upper;
|
|
165
|
+
search.run("findLevelCrossings", f, options);
|
|
166
|
+
return out;
|
|
167
|
+
}
|
|
168
|
+
//# sourceMappingURL=find-level-crossings.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"find-level-crossings.js","sourceRoot":"","sources":["../../src/roots/find-level-crossings.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiB,aAAa,EAAE,MAAM,cAAc,CAAC;AAE5D,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AASlD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,OAAO,cAAc;IAC1B,kDAAkD;IACzC,MAAM,GAAG,IAAI,YAAY,EAAE,CAAC;IAErC,8CAA8C;IAC9C,IAAI,SAAS;QACZ,OAAO,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IACjD,CAAC;IAED,qCAAqC;IACrC,IAAI,KAAK;QACR,OAAO,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC;IAClC,CAAC;IAED;;;OAGG;IACH,IAAI,KAAK;QACR,OAAO,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC;IAClC,CAAC;IAED;;;;;;;OAOG;IACH,IAAI,aAAa;QAChB,OAAO,IAAI,CAAC,MAAM,CAAC,gBAAgB,CAAC;IACrC,CAAC;IAED,yEAAyE;IACzE,IAAI,WAAW;QACd,OAAO,IAAI,CAAC,MAAM,CAAC,cAAc,CAAC;IACnC,CAAC;IAED,uEAAuE;IACvE,IAAI,UAAU;QACb,OAAO,IAAI,CAAC,MAAM,CAAC,kBAAkB,CAAC;IACvC,CAAC;IAED,gDAAgD;IAChD,IAAI,eAAe;QAClB,OAAO,IAAI,CAAC,MAAM,CAAC,eAAe,CAAC;IACpC,CAAC;IAED;;;;;;OAMG;IACH,IAAI,gBAAgB;QACnB,OAAO,IAAI,CAAC,MAAM,CAAC,gBAAgB,CAAC;IACrC,CAAC;IAED,+CAA+C;IAC/C,IAAI,cAAc;QACjB,OAAO,IAAI,CAAC,MAAM,CAAC,cAAc,CAAC;IACnC,CAAC;IAED,iDAAiD;IACjD,IAAI,WAAW;QACd,OAAO,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC;IAChC,CAAC;CACD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqEG;AACH,MAAM,UAAU,kBAAkB,CACjC,CAAgC,EAChC,KAAa,EACb,KAAa,EACb,OAA8B,EAC9B,GAAG,GAAmB,IAAI,cAAc,EAAE;IAE1C,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,IAAI,CAAC,CAAC;IACjC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;QAC7B,MAAM,IAAI,UAAU,CACnB,iDAAiD,aAAa,CAAC,KAAK,CAAC,EAAE,CACvE,CAAC;IACH,CAAC;IACD,MAAM,EAAE,MAAM,EAAE,GAAG,GAAG,CAAC;IACvB,qEAAqE;IACrE,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC;IACzB,MAAM,CAAC,UAAU,GAAG,CAAC,CAAC;IACtB,MAAM,CAAC,aAAa,GAAG,KAAK,CAAC;IAC7B,MAAM,CAAC,aAAa,GAAG,KAAK,CAAC;IAC7B,MAAM,CAAC,GAAG,CAAC,oBAAoB,EAAE,CAAC,EAAE,OAAO,CAAC,CAAC;IAC7C,OAAO,GAAG,CAAC;AACZ,CAAC"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { WorkBudget } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* How {@link findLevelCrossings} and {@link findLevelBands} search an interval:
|
|
4
|
+
* the rate bound that certifies it, how finely an undecided stretch is
|
|
5
|
+
* examined, and how crossings are refined.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* // A sampled altitude in degrees over epoch milliseconds, which never
|
|
10
|
+
* // changes faster than 400° a day: crossings to the millisecond, touches
|
|
11
|
+
* // examined down to a minute.
|
|
12
|
+
* const options: LevelSearchOptions = {
|
|
13
|
+
* maxRate: 400 / 86_400_000,
|
|
14
|
+
* resolution: 60_000,
|
|
15
|
+
* integer: true,
|
|
16
|
+
* };
|
|
17
|
+
* ```;
|
|
18
|
+
*/
|
|
19
|
+
export type LevelSearchOptions = {
|
|
20
|
+
/**
|
|
21
|
+
* An upper bound on `|f′|` over the interval — a Lipschitz constant: `|f(x) −
|
|
22
|
+
* f(y)| ≤ maxRate · |x − y|`. Finite and positive. Every guarantee rests on
|
|
23
|
+
* it; a bound that is too small (or a discontinuity, which no finite bound
|
|
24
|
+
* covers) can hide a pair of crossings inside an interval it certified.
|
|
25
|
+
*/
|
|
26
|
+
maxRate: number;
|
|
27
|
+
/**
|
|
28
|
+
* The width below which an interval that cannot be certified is no longer
|
|
29
|
+
* split. Two crossings closer together than this can merge into their net
|
|
30
|
+
* effect, and an uncertain stretch with no net change is reported as
|
|
31
|
+
* unresolved; a tangent touch leaves one about `2 · √(resolution · maxRate /
|
|
32
|
+
* |f″|)` wide, the stretch where `f` stays within `maxRate · resolution` of
|
|
33
|
+
* the level. Finite and positive. Defaults to `(upper − lower) / 2²⁰` in real
|
|
34
|
+
* mode and to `1` in integer mode, where any value up to `2` examines every
|
|
35
|
+
* integer and leaves nothing unresolved.
|
|
36
|
+
*/
|
|
37
|
+
resolution?: number;
|
|
38
|
+
/**
|
|
39
|
+
* The absolute tolerance of the {@link brent} refinement of each crossing,
|
|
40
|
+
* finite and non-negative; `0`, the default, refines to what binary64 can
|
|
41
|
+
* resolve. Ignored in integer mode, which is exact.
|
|
42
|
+
*/
|
|
43
|
+
tolerance?: number;
|
|
44
|
+
/**
|
|
45
|
+
* Treats the domain as the integers in `[lower, upper]`, both then safe
|
|
46
|
+
* integers: `f` is only called at integers, and a crossing is the first
|
|
47
|
+
* integer on the new side, found by binary search. Suited to epoch
|
|
48
|
+
* milliseconds, sample indices and any time already quantised.
|
|
49
|
+
*/
|
|
50
|
+
integer?: boolean;
|
|
51
|
+
/** Charged one unit per evaluation of `f`; what it throws propagates. */
|
|
52
|
+
budget?: WorkBudget;
|
|
53
|
+
/** Checked before every evaluation of `f`; aborting throws its reason. */
|
|
54
|
+
signal?: AbortSignal | null;
|
|
55
|
+
};
|
|
56
|
+
//# sourceMappingURL=level-search-options.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"level-search-options.d.ts","sourceRoot":"","sources":["../../src/roots/level-search-options.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE/C;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,MAAM,kBAAkB,GAAG;IAChC;;;;;OAKG;IACH,OAAO,EAAE,MAAM,CAAC;IAEhB;;;;;;;;;OASG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB;;;;OAIG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB;;;;;OAKG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;IAElB,yEAAyE;IACzE,MAAM,CAAC,EAAE,UAAU,CAAC;IAEpB,0EAA0E;IAC1E,MAAM,CAAC,EAAE,WAAW,GAAG,IAAI,CAAC;CAC5B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"level-search-options.js","sourceRoot":"","sources":["../../src/roots/level-search-options.ts"],"names":[],"mappings":""}
|
package/dist/trapezoidal-rule.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { PreciseSum } from "@ac-kit/core";
|
|
1
|
+
import { describeValue, PreciseSum } from "@ac-kit/core";
|
|
2
2
|
/**
|
|
3
3
|
* Approximates the area under a curve by slicing it into equal-width trapezoids
|
|
4
4
|
* and adding them up.
|
|
@@ -34,7 +34,7 @@ import { PreciseSum } from "@ac-kit/core";
|
|
|
34
34
|
*/
|
|
35
35
|
export function trapezoidalRule(f, lower, upper, intervals = 1000) {
|
|
36
36
|
if (!Number.isInteger(intervals) || intervals < 1) {
|
|
37
|
-
throw new RangeError(`trapezoidalRule: intervals must be a positive integer, got ${intervals}`);
|
|
37
|
+
throw new RangeError(`trapezoidalRule: intervals must be a positive integer, got ${describeValue(intervals)}`);
|
|
38
38
|
}
|
|
39
39
|
const step = (upper - lower) / intervals;
|
|
40
40
|
// Halving is exact, so splitting the endpoint term lets every panel go
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"trapezoidal-rule.js","sourceRoot":"","sources":["../src/trapezoidal-rule.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"trapezoidal-rule.js","sourceRoot":"","sources":["../src/trapezoidal-rule.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAEzD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,MAAM,UAAU,eAAe,CAC9B,CAAwB,EACxB,KAAa,EACb,KAAa,EACb,SAAS,GAAG,IAAI;IAEhB,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,SAAS,CAAC,IAAI,SAAS,GAAG,CAAC,EAAE,CAAC;QACnD,MAAM,IAAI,UAAU,CACnB,8DAA8D,aAAa,CAAC,SAAS,CAAC,EAAE,CACxF,CAAC;IACH,CAAC;IAED,MAAM,IAAI,GAAG,CAAC,KAAK,GAAG,KAAK,CAAC,GAAG,SAAS,CAAC;IACzC,uEAAuE;IACvE,yEAAyE;IACzE,MAAM,KAAK,GAAG,IAAI,UAAU,EAAE,CAAC;IAC/B,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC;IACxB,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC;IACxB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,SAAS,EAAE,CAAC,EAAE,EAAE,CAAC;QACpC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC;IAChC,CAAC;IACD,OAAO,KAAK,CAAC,KAAK,GAAG,IAAI,CAAC;AAC3B,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ac-kit/math-analysis",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Numerical analysis: roots, integrals, derivatives and differential equations approximated in a finite number of floating-point steps.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"numerical-analysis",
|
|
@@ -44,15 +44,17 @@
|
|
|
44
44
|
"prepack": "yarn build"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"@ac-kit/
|
|
48
|
-
"@ac-kit/
|
|
47
|
+
"@ac-kit/algo": "^0.6.0",
|
|
48
|
+
"@ac-kit/core": "^0.13.0",
|
|
49
|
+
"@ac-kit/math-scalar": "^0.4.0",
|
|
49
50
|
"@types/web": "^0.0.367"
|
|
50
51
|
},
|
|
51
52
|
"devDependencies": {
|
|
52
|
-
"@ac-kit/
|
|
53
|
+
"@ac-kit/math-random": "0.4.1",
|
|
54
|
+
"@ac-kit/test-util": "0.5.0",
|
|
53
55
|
"@ac-kit/tsconfig": "0.1.0",
|
|
54
56
|
"@ac-kit/typedoc-config": "0.1.0",
|
|
55
|
-
"@ac-kit/vitest-config": "0.
|
|
57
|
+
"@ac-kit/vitest-config": "0.2.0",
|
|
56
58
|
"@typescript/native": "typescript@7.0.2",
|
|
57
59
|
"rimraf": "6.1.4",
|
|
58
60
|
"typedoc": "0.28.20",
|