@ac-kit/math-analysis 0.1.11 → 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 +155 -0
- package/README.md +167 -10
- 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 +20 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +20 -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/_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 +19 -0
- package/dist/roots/_grown.js.map +1 -0
- package/dist/roots/_level-search.d.ts +154 -0
- package/dist/roots/_level-search.d.ts.map +1 -0
- package/dist/roots/_level-search.js +556 -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 +143 -0
- package/dist/roots/find-level-crossings.d.ts.map +1 -0
- package/dist/roots/find-level-crossings.js +162 -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/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 -5
|
@@ -0,0 +1,143 @@
|
|
|
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), whatever the bounds: epoch milliseconds included. A
|
|
113
|
+
* call to `f` the engine does not inline, as when one process searches several
|
|
114
|
+
* functions, boxes its argument and its result.
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* ```ts
|
|
118
|
+
* const crossings = findLevelCrossings(Math.sin, 0, 10, { maxRate: 1 });
|
|
119
|
+
* crossings.count; // 3
|
|
120
|
+
* crossings.startSide; // 1, since sin(0) = 0 counts as above
|
|
121
|
+
* // 1: just after 0, sin x < x, so a rate of 1 cannot rule out a dip.
|
|
122
|
+
* crossings.unresolvedCount;
|
|
123
|
+
* ```;
|
|
124
|
+
*
|
|
125
|
+
* @param f The function, finite wherever it is called.
|
|
126
|
+
* @param lower Start of the interval, finite.
|
|
127
|
+
* @param upper End of the interval, finite and `≥ lower`.
|
|
128
|
+
* @param options The rate bound, the level and how finely to search; see
|
|
129
|
+
* {@link LevelCrossingsOptions}.
|
|
130
|
+
* @param out A result to overwrite instead of allocating one; it must not be in
|
|
131
|
+
* use by an enclosing search (from inside `f`).
|
|
132
|
+
* @returns `out`, or a new {@link LevelCrossings}. Unspecified after a throw.
|
|
133
|
+
* @throws {RangeError} When the interval is not finite, inverted or wider than
|
|
134
|
+
* binary64 represents; its ends are not safe integers in integer mode;
|
|
135
|
+
* `level` is not finite; `maxRate` or `resolution` is not finite and
|
|
136
|
+
* positive; `tolerance` is negative or not finite; or `f` returns a value
|
|
137
|
+
* that is not finite.
|
|
138
|
+
* @throws Whatever `budget.spend` throws — `WorkBudgetExceededError` for a
|
|
139
|
+
* `FixedWorkBudget` — and the signal's reason once it is aborted.
|
|
140
|
+
* @see {@link findLevelBands} for the stretches between two levels.
|
|
141
|
+
*/
|
|
142
|
+
export declare function findLevelCrossings(f: Callable<[x: number], number>, lower: number, upper: number, options: LevelCrossingsOptions, out?: LevelCrossings): LevelCrossings;
|
|
143
|
+
//# 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,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAE7C,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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuEG;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,CAMhB"}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { LevelSearch_ } from "./_level-search.js";
|
|
2
|
+
/**
|
|
3
|
+
* Where a function crosses one level, as columns: what
|
|
4
|
+
* {@link findLevelCrossings} writes, and may write again into the same object
|
|
5
|
+
* without allocating once its columns are large enough.
|
|
6
|
+
*
|
|
7
|
+
* A point is **above** when `f ≥ level` and **below** when `f < level`, so a
|
|
8
|
+
* level reached exactly counts as above — the half-open convention of
|
|
9
|
+
* {@link findLevelBands}. Only the first {@link count} entries of {@link times}
|
|
10
|
+
* and {@link directions}, and the first {@link unresolvedCount} of the
|
|
11
|
+
* unresolved columns, are meaningful; the columns are reallocated when they
|
|
12
|
+
* grow, so read them again after each search rather than keeping them.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* const crossings = findLevelCrossings(Math.sin, 0, 10, { maxRate: 1 });
|
|
17
|
+
* for (let index = 0; index < crossings.count; index++) {
|
|
18
|
+
* console.log(crossings.times[index], crossings.directions[index]);
|
|
19
|
+
* }
|
|
20
|
+
* // 3.141592653589793 -1, 6.283185307179586 1, 9.42477796076938 -1
|
|
21
|
+
* ```;
|
|
22
|
+
*/
|
|
23
|
+
export class LevelCrossings {
|
|
24
|
+
/** @internal The search state and its buffers. */
|
|
25
|
+
search = new LevelSearch_();
|
|
26
|
+
/** `1` when `f(lower) ≥ level`, else `-1`. */
|
|
27
|
+
get startSide() {
|
|
28
|
+
return this.search.startSides[0] === 1 ? 1 : -1;
|
|
29
|
+
}
|
|
30
|
+
/** How many crossings were found. */
|
|
31
|
+
get count() {
|
|
32
|
+
return this.search.crossingCount;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Crossing times, non-decreasing. Real mode: a zero of `f − level` refined by
|
|
36
|
+
* {@link brent}. Integer mode: the first integer on the new side.
|
|
37
|
+
*/
|
|
38
|
+
get times() {
|
|
39
|
+
return this.search.crossingTimes;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Per crossing, the start of its closed enclosure `[earliest, latest]`, which
|
|
43
|
+
* holds the reported time and every zero of its cluster: `f` is certified on
|
|
44
|
+
* the old side between the previous item and `earliest`, and on the new side
|
|
45
|
+
* between `latest` and the next. Narrower than `resolution` where `f` crosses
|
|
46
|
+
* at a slope near `maxRate`; wider where it crosses slowly, since a bound
|
|
47
|
+
* well above the slope cannot rule out a brief return near the crossing.
|
|
48
|
+
*/
|
|
49
|
+
get earliestTimes() {
|
|
50
|
+
return this.search.crossingEarliest;
|
|
51
|
+
}
|
|
52
|
+
/** Per crossing, the end of its enclosure; see {@link earliestTimes}. */
|
|
53
|
+
get latestTimes() {
|
|
54
|
+
return this.search.crossingLatest;
|
|
55
|
+
}
|
|
56
|
+
/** Per crossing, `1` going from below to above, `-1` the other way. */
|
|
57
|
+
get directions() {
|
|
58
|
+
return this.search.crossingDirections;
|
|
59
|
+
}
|
|
60
|
+
/** How many unresolved intervals were found. */
|
|
61
|
+
get unresolvedCount() {
|
|
62
|
+
return this.search.unresolvedCount;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Starts of the closed unresolved intervals, ascending: stretches away from
|
|
66
|
+
* any crossing where `f` may touch or briefly cross the level and come back,
|
|
67
|
+
* which `maxRate` and `resolution` cannot decide, merged when less than
|
|
68
|
+
* `resolution` apart. Never guessed: the side of `f` outside them and the
|
|
69
|
+
* crossings' enclosures is certified.
|
|
70
|
+
*/
|
|
71
|
+
get unresolvedStarts() {
|
|
72
|
+
return this.search.unresolvedStarts;
|
|
73
|
+
}
|
|
74
|
+
/** Ends of the closed unresolved intervals. */
|
|
75
|
+
get unresolvedEnds() {
|
|
76
|
+
return this.search.unresolvedEnds;
|
|
77
|
+
}
|
|
78
|
+
/** How many times the last search called `f`. */
|
|
79
|
+
get evaluations() {
|
|
80
|
+
return this.search.evaluations;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Every time a function crosses a level in `[lower, upper]`, **certified**:
|
|
85
|
+
* given a true bound on `|f′|`, no crossing is missed.
|
|
86
|
+
*
|
|
87
|
+
* Sampling at a fixed step misses any pair of crossings closer than the step,
|
|
88
|
+
* and nothing tells it so. A rate bound closes that gap: between two samples
|
|
89
|
+
* `a` and `b` with `|f(a) − level| + |f(b) − level| > maxRate · (b − a)`, the
|
|
90
|
+
* two cones of slope `maxRate` cover the interval, so `f` cannot reach the
|
|
91
|
+
* level in it. Intervals that pass are skipped whole; the rest are bisected
|
|
92
|
+
* down to `resolution`. Each sign change left is refined by {@link brent} (or,
|
|
93
|
+
* with `integer`, to the first integer on the new side by `@ac-kit/algo`'s
|
|
94
|
+
* `binarySearch`), and each stretch without one that still cannot be certified
|
|
95
|
+
* — a tangent touch, or a brief excursion and return — is reported as
|
|
96
|
+
* **unresolved**, never guessed either way.
|
|
97
|
+
*
|
|
98
|
+
* **The guarantee.** When `maxRate` truly bounds `|f′|`, every zero of `f −
|
|
99
|
+
* level` lies in a reported crossing's enclosure or in an unresolved interval,
|
|
100
|
+
* and the side of `f` is certified everywhere else. A crossing stands for an
|
|
101
|
+
* odd cluster: zeros that no sample separates (closer than `resolution`, or
|
|
102
|
+
* hidden near a slow crossing) count once, and its enclosure says where all of
|
|
103
|
+
* them lie. An even cluster away from any crossing — a touch, or a dip and
|
|
104
|
+
* return — is an unresolved interval. When `maxRate` is too small, or `f`
|
|
105
|
+
* jumps: a sign change between samples is still found and refined (to the jump,
|
|
106
|
+
* for a discontinuity), but a crossing and its return inside an interval the
|
|
107
|
+
* false bound certified are missed. Every reported crossing is a real sign
|
|
108
|
+
* change of `f` in either case, and directions always alternate, starting from
|
|
109
|
+
* {@link LevelCrossings.startSide}. A start exactly on the level, as `sin` at
|
|
110
|
+
* `0`, is above but cannot be told from a dip that follows at once unless
|
|
111
|
+
* `maxRate` exceeds the slope there, and is then reported unresolved.
|
|
112
|
+
*
|
|
113
|
+
* **Termination.** At most `2 + 2 · (upper − lower) / resolution` evaluations
|
|
114
|
+
* isolate the crossings (`4 ·`, with `resolution` at least `2`, in integer
|
|
115
|
+
* mode), since the bisection never splits an interval narrower than
|
|
116
|
+
* `resolution`. Far fewer in practice: a stretch where `|f − level| ≥ c` is
|
|
117
|
+
* certified in pieces `2c / maxRate` wide, so the cost scales as `(upper −
|
|
118
|
+
* lower) · maxRate / c`, plus `log₂(spacing / resolution)` bisections per
|
|
119
|
+
* crossing. Each crossing then costs Brent's own bound (at most about N²
|
|
120
|
+
* evaluations, N being bisection's, over a bracket narrower than `resolution`)
|
|
121
|
+
* or `⌈log₂ resolution⌉` in integer mode.
|
|
122
|
+
*
|
|
123
|
+
* O(depth) memory: the subdivision runs on an explicit stack. With `out`
|
|
124
|
+
* reused, nothing is allocated once its columns have grown to the result (`f`'s
|
|
125
|
+
* own allocations aside), whatever the bounds: epoch milliseconds included. A
|
|
126
|
+
* call to `f` the engine does not inline, as when one process searches several
|
|
127
|
+
* functions, boxes its argument and its result.
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```ts
|
|
131
|
+
* const crossings = findLevelCrossings(Math.sin, 0, 10, { maxRate: 1 });
|
|
132
|
+
* crossings.count; // 3
|
|
133
|
+
* crossings.startSide; // 1, since sin(0) = 0 counts as above
|
|
134
|
+
* // 1: just after 0, sin x < x, so a rate of 1 cannot rule out a dip.
|
|
135
|
+
* crossings.unresolvedCount;
|
|
136
|
+
* ```;
|
|
137
|
+
*
|
|
138
|
+
* @param f The function, finite wherever it is called.
|
|
139
|
+
* @param lower Start of the interval, finite.
|
|
140
|
+
* @param upper End of the interval, finite and `≥ lower`.
|
|
141
|
+
* @param options The rate bound, the level and how finely to search; see
|
|
142
|
+
* {@link LevelCrossingsOptions}.
|
|
143
|
+
* @param out A result to overwrite instead of allocating one; it must not be in
|
|
144
|
+
* use by an enclosing search (from inside `f`).
|
|
145
|
+
* @returns `out`, or a new {@link LevelCrossings}. Unspecified after a throw.
|
|
146
|
+
* @throws {RangeError} When the interval is not finite, inverted or wider than
|
|
147
|
+
* binary64 represents; its ends are not safe integers in integer mode;
|
|
148
|
+
* `level` is not finite; `maxRate` or `resolution` is not finite and
|
|
149
|
+
* positive; `tolerance` is negative or not finite; or `f` returns a value
|
|
150
|
+
* that is not finite.
|
|
151
|
+
* @throws Whatever `budget.spend` throws — `WorkBudgetExceededError` for a
|
|
152
|
+
* `FixedWorkBudget` — and the signal's reason once it is aborted.
|
|
153
|
+
* @see {@link findLevelBands} for the stretches between two levels.
|
|
154
|
+
*/
|
|
155
|
+
export function findLevelCrossings(f, lower, upper, options, out = new LevelCrossings()) {
|
|
156
|
+
const { search } = out;
|
|
157
|
+
search.intervalLower = lower;
|
|
158
|
+
search.intervalUpper = upper;
|
|
159
|
+
search.run("findLevelCrossings", f, options, true);
|
|
160
|
+
return out;
|
|
161
|
+
}
|
|
162
|
+
//# 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":"AAEA,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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuEG;AACH,MAAM,UAAU,kBAAkB,CACjC,CAAgC,EAChC,KAAa,EACb,KAAa,EACb,OAA8B,EAC9B,GAAG,GAAmB,IAAI,cAAc,EAAE;IAE1C,MAAM,EAAE,MAAM,EAAE,GAAG,GAAG,CAAC;IACvB,MAAM,CAAC,aAAa,GAAG,KAAK,CAAC;IAC7B,MAAM,CAAC,aAAa,GAAG,KAAK,CAAC;IAC7B,MAAM,CAAC,GAAG,CAAC,oBAAoB,EAAE,CAAC,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;IACnD,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":""}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { ConvergenceOptions } from "../convergence-options.js";
|
|
2
|
+
/**
|
|
3
|
+
* A root of `f` by Newton–Raphson iteration from `x0`: `x ← x − f(x) / f′(x)`,
|
|
4
|
+
* quadratically convergent near a simple root. Prefer {@link brent} when a
|
|
5
|
+
* bracket is known; Newton's method is the one to reach for when only a good
|
|
6
|
+
* starting point and the derivative are.
|
|
7
|
+
*
|
|
8
|
+
* Stops when the step is below `tolerance · max(|x|, 1)` (`4ε` by default), or
|
|
9
|
+
* when `f(x)` is exactly zero. A vanishing derivative, an iterate that is not
|
|
10
|
+
* finite (the iteration diverged or left the function's domain) and a step that
|
|
11
|
+
* grows for ten iterations running (it is moving away from every root) throw a
|
|
12
|
+
* `RangeError` instead of looping. Two calls per iteration.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* newtonRaphson((x) => x * x - 2, (x) => 2 * x, 1); // 1.4142135623730951
|
|
17
|
+
* ```;
|
|
18
|
+
*
|
|
19
|
+
* @param f The function.
|
|
20
|
+
* @param df Its derivative.
|
|
21
|
+
* @param x0 The starting point.
|
|
22
|
+
* @param options `tolerance` on the relative step, `maxIterations` (100 by
|
|
23
|
+
* default) and `signal`.
|
|
24
|
+
* @returns The root.
|
|
25
|
+
* @throws {RangeError} When the derivative vanishes, an iterate is not finite
|
|
26
|
+
* or the steps diverge.
|
|
27
|
+
* @throws {ConvergenceError} When `maxIterations` runs out first.
|
|
28
|
+
* @throws The signal's reason, once it is aborted.
|
|
29
|
+
*/
|
|
30
|
+
export declare function newtonRaphson(f: (x: number) => number, df: (x: number) => number, x0: number, options?: ConvergenceOptions): number;
|
|
31
|
+
//# sourceMappingURL=newton-raphson.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"newton-raphson.d.ts","sourceRoot":"","sources":["../../src/roots/newton-raphson.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAEpE;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,aAAa,CAC5B,CAAC,EAAE,CAAC,CAAC,EAAE,MAAM,KAAK,MAAM,EACxB,EAAE,EAAE,CAAC,CAAC,EAAE,MAAM,KAAK,MAAM,EACzB,EAAE,EAAE,MAAM,EACV,OAAO,CAAC,EAAE,kBAAkB,GAC1B,MAAM,CA8CR"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { describeValue } from "@ac-kit/core";
|
|
2
|
+
import { ConvergenceError } from "../convergence-error.js";
|
|
3
|
+
/**
|
|
4
|
+
* A root of `f` by Newton–Raphson iteration from `x0`: `x ← x − f(x) / f′(x)`,
|
|
5
|
+
* quadratically convergent near a simple root. Prefer {@link brent} when a
|
|
6
|
+
* bracket is known; Newton's method is the one to reach for when only a good
|
|
7
|
+
* starting point and the derivative are.
|
|
8
|
+
*
|
|
9
|
+
* Stops when the step is below `tolerance · max(|x|, 1)` (`4ε` by default), or
|
|
10
|
+
* when `f(x)` is exactly zero. A vanishing derivative, an iterate that is not
|
|
11
|
+
* finite (the iteration diverged or left the function's domain) and a step that
|
|
12
|
+
* grows for ten iterations running (it is moving away from every root) throw a
|
|
13
|
+
* `RangeError` instead of looping. Two calls per iteration.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* newtonRaphson((x) => x * x - 2, (x) => 2 * x, 1); // 1.4142135623730951
|
|
18
|
+
* ```;
|
|
19
|
+
*
|
|
20
|
+
* @param f The function.
|
|
21
|
+
* @param df Its derivative.
|
|
22
|
+
* @param x0 The starting point.
|
|
23
|
+
* @param options `tolerance` on the relative step, `maxIterations` (100 by
|
|
24
|
+
* default) and `signal`.
|
|
25
|
+
* @returns The root.
|
|
26
|
+
* @throws {RangeError} When the derivative vanishes, an iterate is not finite
|
|
27
|
+
* or the steps diverge.
|
|
28
|
+
* @throws {ConvergenceError} When `maxIterations` runs out first.
|
|
29
|
+
* @throws The signal's reason, once it is aborted.
|
|
30
|
+
*/
|
|
31
|
+
export function newtonRaphson(f, df, x0, options) {
|
|
32
|
+
const tolerance = options?.tolerance ?? 4 * Number.EPSILON;
|
|
33
|
+
const maxIterations = options?.maxIterations ?? 100;
|
|
34
|
+
if (!(tolerance >= 0)) {
|
|
35
|
+
throw new RangeError(`newtonRaphson: tolerance must be non-negative, got ${describeValue(tolerance)}`);
|
|
36
|
+
}
|
|
37
|
+
const signal = options?.signal;
|
|
38
|
+
let x = x0;
|
|
39
|
+
let previousStep = Infinity;
|
|
40
|
+
let growing = 0;
|
|
41
|
+
for (let iteration = 0; iteration < maxIterations; iteration++) {
|
|
42
|
+
signal?.throwIfAborted();
|
|
43
|
+
const value = f(x);
|
|
44
|
+
if (value === 0) {
|
|
45
|
+
return x;
|
|
46
|
+
}
|
|
47
|
+
const slope = df(x);
|
|
48
|
+
if (slope === 0 || slope !== slope) {
|
|
49
|
+
throw new RangeError(`newtonRaphson: the derivative is ${slope} at x = ${x}`);
|
|
50
|
+
}
|
|
51
|
+
const step = value / slope;
|
|
52
|
+
const next = x - step;
|
|
53
|
+
if (next - next !== 0) {
|
|
54
|
+
throw new RangeError(`newtonRaphson: the iteration diverged (x = ${next} after ${iteration + 1} steps)`);
|
|
55
|
+
}
|
|
56
|
+
const size = Math.abs(step);
|
|
57
|
+
if (size <= tolerance * Math.max(Math.abs(next), 1)) {
|
|
58
|
+
return next;
|
|
59
|
+
}
|
|
60
|
+
growing = size > previousStep ? growing + 1 : 0;
|
|
61
|
+
if (growing >= 10) {
|
|
62
|
+
throw new RangeError(`newtonRaphson: the steps grew ten times running, at x = ${next}`);
|
|
63
|
+
}
|
|
64
|
+
previousStep = size;
|
|
65
|
+
x = next;
|
|
66
|
+
}
|
|
67
|
+
throw new ConvergenceError("newtonRaphson", maxIterations);
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=newton-raphson.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"newton-raphson.js","sourceRoot":"","sources":["../../src/roots/newton-raphson.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAE7C,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAG3D;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,UAAU,aAAa,CAC5B,CAAwB,EACxB,EAAyB,EACzB,EAAU,EACV,OAA4B;IAE5B,MAAM,SAAS,GAAG,OAAO,EAAE,SAAS,IAAI,CAAC,GAAG,MAAM,CAAC,OAAO,CAAC;IAC3D,MAAM,aAAa,GAAG,OAAO,EAAE,aAAa,IAAI,GAAG,CAAC;IACpD,IAAI,CAAC,CAAC,SAAS,IAAI,CAAC,CAAC,EAAE,CAAC;QACvB,MAAM,IAAI,UAAU,CACnB,sDAAsD,aAAa,CAAC,SAAS,CAAC,EAAE,CAChF,CAAC;IACH,CAAC;IACD,MAAM,MAAM,GAAG,OAAO,EAAE,MAAM,CAAC;IAE/B,IAAI,CAAC,GAAG,EAAE,CAAC;IACX,IAAI,YAAY,GAAG,QAAQ,CAAC;IAC5B,IAAI,OAAO,GAAG,CAAC,CAAC;IAChB,KAAK,IAAI,SAAS,GAAG,CAAC,EAAE,SAAS,GAAG,aAAa,EAAE,SAAS,EAAE,EAAE,CAAC;QAChE,MAAM,EAAE,cAAc,EAAE,CAAC;QACzB,MAAM,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;QACnB,IAAI,KAAK,KAAK,CAAC,EAAE,CAAC;YACjB,OAAO,CAAC,CAAC;QACV,CAAC;QACD,MAAM,KAAK,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC;QACpB,IAAI,KAAK,KAAK,CAAC,IAAI,KAAK,KAAK,KAAK,EAAE,CAAC;YACpC,MAAM,IAAI,UAAU,CACnB,oCAAoC,KAAK,WAAW,CAAC,EAAE,CACvD,CAAC;QACH,CAAC;QACD,MAAM,IAAI,GAAG,KAAK,GAAG,KAAK,CAAC;QAC3B,MAAM,IAAI,GAAG,CAAC,GAAG,IAAI,CAAC;QACtB,IAAI,IAAI,GAAG,IAAI,KAAK,CAAC,EAAE,CAAC;YACvB,MAAM,IAAI,UAAU,CACnB,8CAA8C,IAAI,UAAU,SAAS,GAAG,CAAC,SAAS,CAClF,CAAC;QACH,CAAC;QACD,MAAM,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QAC5B,IAAI,IAAI,IAAI,SAAS,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC;YACrD,OAAO,IAAI,CAAC;QACb,CAAC;QACD,OAAO,GAAG,IAAI,GAAG,YAAY,CAAC,CAAC,CAAC,OAAO,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QAChD,IAAI,OAAO,IAAI,EAAE,EAAE,CAAC;YACnB,MAAM,IAAI,UAAU,CACnB,2DAA2D,IAAI,EAAE,CACjE,CAAC;QACH,CAAC;QACD,YAAY,GAAG,IAAI,CAAC;QACpB,CAAC,GAAG,IAAI,CAAC;IACV,CAAC;IACD,MAAM,IAAI,gBAAgB,CAAC,eAAe,EAAE,aAAa,CAAC,CAAC;AAC5D,CAAC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { ConvergenceOptions } from "../convergence-options.js";
|
|
2
|
+
import type { ConvergenceResult } from "../convergence-result.js";
|
|
3
|
+
/** The options of {@link sumSeries}. */
|
|
4
|
+
export type SumSeriesOptions = ConvergenceOptions & {
|
|
5
|
+
/** The index of the first term; `0` by default. */
|
|
6
|
+
start?: number;
|
|
7
|
+
/**
|
|
8
|
+
* How many consecutive terms must each be within the tolerance before the sum
|
|
9
|
+
* stops; `2` by default, so a single term that happens to vanish (an
|
|
10
|
+
* alternating series' zero) does not end it early.
|
|
11
|
+
*/
|
|
12
|
+
consecutive?: number;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* The sum `Σ term(k)` for `k = start, start + 1, …`, stopped once the terms no
|
|
16
|
+
* longer change it: when `consecutive` terms in a row are each at most
|
|
17
|
+
* `tolerance · |sum|` (`ε` by default, so the sum is as converged as binary64
|
|
18
|
+
* can show), or exactly zero. The partial sums are exactly rounded
|
|
19
|
+
* (`@ac-kit/core`'s `PreciseSum`), so cancellation in an alternating series
|
|
20
|
+
* costs no accuracy of its own.
|
|
21
|
+
*
|
|
22
|
+
* The stopping test sees the terms, not the tail: it is right for a series
|
|
23
|
+
* whose terms decrease at least geometrically (a power or Taylor series inside
|
|
24
|
+
* its radius), and can stop far too early on a slowly convergent one such as `Σ
|
|
25
|
+
* 1/k²`, whose tail after `k` terms is about `1/k`; accelerate those first.
|
|
26
|
+
* Stops after `maxIterations` terms (`100000` by default) with `converged:
|
|
27
|
+
* false`; a non-finite term or sum stops it with `converged: false` too.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* sumSeries((k) => 1 / gamma(k + 1)).value; // e, 2.718281828459045
|
|
32
|
+
* sumSeries((k) => (-1) ** k / (2 * k + 1), { tolerance: 1e-4 }).value; // π/4 to about 1e-4
|
|
33
|
+
* ```;
|
|
34
|
+
*
|
|
35
|
+
* @param term The `k`-th term.
|
|
36
|
+
* @param options Tolerance, iteration limit, first index and signal.
|
|
37
|
+
* @returns The sum, the number of terms added, and whether it converged.
|
|
38
|
+
* @throws {RangeError} When a tolerance is negative or a count not a positive
|
|
39
|
+
* integer.
|
|
40
|
+
* @throws The signal's reason, once it is aborted.
|
|
41
|
+
*/
|
|
42
|
+
export declare function sumSeries(term: (k: number) => number, options?: SumSeriesOptions): ConvergenceResult<number>;
|
|
43
|
+
//# sourceMappingURL=sum-series.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sum-series.d.ts","sourceRoot":"","sources":["../../src/series/sum-series.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AACpE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAElE,wCAAwC;AACxC,MAAM,MAAM,gBAAgB,GAAG,kBAAkB,GAAG;IACnD,mDAAmD;IACnD,KAAK,CAAC,EAAE,MAAM,CAAC;IACf;;;;OAIG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,SAAS,CACxB,IAAI,EAAE,CAAC,CAAC,EAAE,MAAM,KAAK,MAAM,EAC3B,OAAO,CAAC,EAAE,gBAAgB,GACxB,iBAAiB,CAAC,MAAM,CAAC,CA8C3B"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { describeValue, PreciseSum } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* The sum `Σ term(k)` for `k = start, start + 1, …`, stopped once the terms no
|
|
4
|
+
* longer change it: when `consecutive` terms in a row are each at most
|
|
5
|
+
* `tolerance · |sum|` (`ε` by default, so the sum is as converged as binary64
|
|
6
|
+
* can show), or exactly zero. The partial sums are exactly rounded
|
|
7
|
+
* (`@ac-kit/core`'s `PreciseSum`), so cancellation in an alternating series
|
|
8
|
+
* costs no accuracy of its own.
|
|
9
|
+
*
|
|
10
|
+
* The stopping test sees the terms, not the tail: it is right for a series
|
|
11
|
+
* whose terms decrease at least geometrically (a power or Taylor series inside
|
|
12
|
+
* its radius), and can stop far too early on a slowly convergent one such as `Σ
|
|
13
|
+
* 1/k²`, whose tail after `k` terms is about `1/k`; accelerate those first.
|
|
14
|
+
* Stops after `maxIterations` terms (`100000` by default) with `converged:
|
|
15
|
+
* false`; a non-finite term or sum stops it with `converged: false` too.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* sumSeries((k) => 1 / gamma(k + 1)).value; // e, 2.718281828459045
|
|
20
|
+
* sumSeries((k) => (-1) ** k / (2 * k + 1), { tolerance: 1e-4 }).value; // π/4 to about 1e-4
|
|
21
|
+
* ```;
|
|
22
|
+
*
|
|
23
|
+
* @param term The `k`-th term.
|
|
24
|
+
* @param options Tolerance, iteration limit, first index and signal.
|
|
25
|
+
* @returns The sum, the number of terms added, and whether it converged.
|
|
26
|
+
* @throws {RangeError} When a tolerance is negative or a count not a positive
|
|
27
|
+
* integer.
|
|
28
|
+
* @throws The signal's reason, once it is aborted.
|
|
29
|
+
*/
|
|
30
|
+
export function sumSeries(term, options) {
|
|
31
|
+
const tolerance = options?.tolerance ?? Number.EPSILON;
|
|
32
|
+
const maxIterations = options?.maxIterations ?? 100000;
|
|
33
|
+
const start = options?.start ?? 0;
|
|
34
|
+
const consecutive = options?.consecutive ?? 2;
|
|
35
|
+
if (!(tolerance >= 0)) {
|
|
36
|
+
throw new RangeError(`sumSeries: tolerance must be non-negative, got ${describeValue(tolerance)}`);
|
|
37
|
+
}
|
|
38
|
+
if (!Number.isSafeInteger(consecutive) || consecutive < 1) {
|
|
39
|
+
throw new RangeError(`sumSeries: consecutive must be a positive integer, got ${describeValue(consecutive)}`);
|
|
40
|
+
}
|
|
41
|
+
if (!Number.isSafeInteger(start)) {
|
|
42
|
+
throw new RangeError(`sumSeries: start must be an integer, got ${describeValue(start)}`);
|
|
43
|
+
}
|
|
44
|
+
const signal = options?.signal;
|
|
45
|
+
const sum = new PreciseSum();
|
|
46
|
+
let small = 0;
|
|
47
|
+
let iterations = 0;
|
|
48
|
+
for (let k = start; iterations < maxIterations; k++) {
|
|
49
|
+
if ((iterations & 1023) === 0) {
|
|
50
|
+
signal?.throwIfAborted();
|
|
51
|
+
}
|
|
52
|
+
const value = term(k);
|
|
53
|
+
iterations++;
|
|
54
|
+
if (value - value !== 0) {
|
|
55
|
+
sum.add(value);
|
|
56
|
+
return { value: sum.value, iterations, converged: false };
|
|
57
|
+
}
|
|
58
|
+
sum.add(value);
|
|
59
|
+
const total = sum.value;
|
|
60
|
+
if (total - total !== 0) {
|
|
61
|
+
return { value: total, iterations, converged: false };
|
|
62
|
+
}
|
|
63
|
+
small = Math.abs(value) <= tolerance * Math.abs(total) ? small + 1 : 0;
|
|
64
|
+
if (small >= consecutive) {
|
|
65
|
+
return { value: total, iterations, converged: true };
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return { value: sum.value, iterations, converged: false };
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=sum-series.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sum-series.js","sourceRoot":"","sources":["../../src/series/sum-series.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAiBzD;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,UAAU,SAAS,CACxB,IAA2B,EAC3B,OAA0B;IAE1B,MAAM,SAAS,GAAG,OAAO,EAAE,SAAS,IAAI,MAAM,CAAC,OAAO,CAAC;IACvD,MAAM,aAAa,GAAG,OAAO,EAAE,aAAa,IAAI,MAAO,CAAC;IACxD,MAAM,KAAK,GAAG,OAAO,EAAE,KAAK,IAAI,CAAC,CAAC;IAClC,MAAM,WAAW,GAAG,OAAO,EAAE,WAAW,IAAI,CAAC,CAAC;IAC9C,IAAI,CAAC,CAAC,SAAS,IAAI,CAAC,CAAC,EAAE,CAAC;QACvB,MAAM,IAAI,UAAU,CACnB,kDAAkD,aAAa,CAAC,SAAS,CAAC,EAAE,CAC5E,CAAC;IACH,CAAC;IACD,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,WAAW,CAAC,IAAI,WAAW,GAAG,CAAC,EAAE,CAAC;QAC3D,MAAM,IAAI,UAAU,CACnB,0DAA0D,aAAa,CAAC,WAAW,CAAC,EAAE,CACtF,CAAC;IACH,CAAC;IACD,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,EAAE,CAAC;QAClC,MAAM,IAAI,UAAU,CACnB,4CAA4C,aAAa,CAAC,KAAK,CAAC,EAAE,CAClE,CAAC;IACH,CAAC;IACD,MAAM,MAAM,GAAG,OAAO,EAAE,MAAM,CAAC;IAE/B,MAAM,GAAG,GAAG,IAAI,UAAU,EAAE,CAAC;IAC7B,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,IAAI,UAAU,GAAG,CAAC,CAAC;IACnB,KAAK,IAAI,CAAC,GAAG,KAAK,EAAE,UAAU,GAAG,aAAa,EAAE,CAAC,EAAE,EAAE,CAAC;QACrD,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;YAC/B,MAAM,EAAE,cAAc,EAAE,CAAC;QAC1B,CAAC;QACD,MAAM,KAAK,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;QACtB,UAAU,EAAE,CAAC;QACb,IAAI,KAAK,GAAG,KAAK,KAAK,CAAC,EAAE,CAAC;YACzB,GAAG,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;YACf,OAAO,EAAE,KAAK,EAAE,GAAG,CAAC,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC;QAC3D,CAAC;QACD,GAAG,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QACf,MAAM,KAAK,GAAG,GAAG,CAAC,KAAK,CAAC;QACxB,IAAI,KAAK,GAAG,KAAK,KAAK,CAAC,EAAE,CAAC;YACzB,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC;QACvD,CAAC;QACD,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,SAAS,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QACvE,IAAI,KAAK,IAAI,WAAW,EAAE,CAAC;YAC1B,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC;QACtD,CAAC;IACF,CAAC;IACD,OAAO,EAAE,KAAK,EAAE,GAAG,CAAC,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC;AAC3D,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ac-kit/math-analysis",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.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,18 @@
|
|
|
44
44
|
"prepack": "yarn build"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"@ac-kit/
|
|
48
|
-
"@ac-kit/
|
|
47
|
+
"@ac-kit/algo": "^0.7.0",
|
|
48
|
+
"@ac-kit/core": "^0.14.0",
|
|
49
|
+
"@ac-kit/math-linear": "^0.5.0",
|
|
50
|
+
"@ac-kit/math-scalar": "^0.5.0",
|
|
49
51
|
"@types/web": "^0.0.367"
|
|
50
52
|
},
|
|
51
53
|
"devDependencies": {
|
|
52
|
-
"@ac-kit/
|
|
54
|
+
"@ac-kit/math-random": "0.4.2",
|
|
55
|
+
"@ac-kit/test-util": "0.5.1",
|
|
53
56
|
"@ac-kit/tsconfig": "0.1.0",
|
|
54
57
|
"@ac-kit/typedoc-config": "0.1.0",
|
|
55
|
-
"@ac-kit/vitest-config": "0.
|
|
58
|
+
"@ac-kit/vitest-config": "0.3.0",
|
|
56
59
|
"@typescript/native": "typescript@7.0.2",
|
|
57
60
|
"rimraf": "6.1.4",
|
|
58
61
|
"typedoc": "0.28.20",
|