@ac-kit/math-analysis 0.1.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/LICENSE +28 -0
- package/README.md +68 -0
- package/dist/_beta-power-terms.d.ts +21 -0
- package/dist/_beta-power-terms.d.ts.map +1 -0
- package/dist/_beta-power-terms.js +43 -0
- package/dist/_beta-power-terms.js.map +1 -0
- package/dist/_exp-split.d.ts +20 -0
- package/dist/_exp-split.d.ts.map +1 -0
- package/dist/_exp-split.js +44 -0
- package/dist/_exp-split.js.map +1 -0
- package/dist/_incomplete-beta.d.ts +27 -0
- package/dist/_incomplete-beta.d.ts.map +1 -0
- package/dist/_incomplete-beta.js +55 -0
- package/dist/_incomplete-beta.js.map +1 -0
- package/dist/_incomplete-gamma.d.ts +81 -0
- package/dist/_incomplete-gamma.d.ts.map +1 -0
- package/dist/_incomplete-gamma.js +155 -0
- package/dist/_incomplete-gamma.js.map +1 -0
- package/dist/_lanczos.d.ts +21 -0
- package/dist/_lanczos.d.ts.map +1 -0
- package/dist/_lanczos.js +48 -0
- package/dist/_lanczos.js.map +1 -0
- package/dist/_log1pmx.d.ts +18 -0
- package/dist/_log1pmx.d.ts.map +1 -0
- package/dist/_log1pmx.js +36 -0
- package/dist/_log1pmx.js.map +1 -0
- package/dist/_normal-quantile-guess.d.ts +14 -0
- package/dist/_normal-quantile-guess.d.ts.map +1 -0
- package/dist/_normal-quantile-guess.js +19 -0
- package/dist/_normal-quantile-guess.js.map +1 -0
- package/dist/beta.d.ts +36 -0
- package/dist/beta.d.ts.map +1 -0
- package/dist/beta.js +77 -0
- package/dist/beta.js.map +1 -0
- package/dist/bisect.d.ts +40 -0
- package/dist/bisect.d.ts.map +1 -0
- package/dist/bisect.js +72 -0
- package/dist/bisect.js.map +1 -0
- package/dist/continued-fraction-eval.d.ts +68 -0
- package/dist/continued-fraction-eval.d.ts.map +1 -0
- package/dist/continued-fraction-eval.js +86 -0
- package/dist/continued-fraction-eval.js.map +1 -0
- package/dist/convergence-error.d.ts +31 -0
- package/dist/convergence-error.d.ts.map +1 -0
- package/dist/convergence-error.js +34 -0
- package/dist/convergence-error.js.map +1 -0
- package/dist/convergence-options.d.ts +38 -0
- package/dist/convergence-options.d.ts.map +1 -0
- package/dist/convergence-options.js +2 -0
- package/dist/convergence-options.js.map +1 -0
- package/dist/convergence-result.d.ts +33 -0
- package/dist/convergence-result.d.ts.map +1 -0
- package/dist/convergence-result.js +2 -0
- package/dist/convergence-result.js.map +1 -0
- package/dist/erf.d.ts +28 -0
- package/dist/erf.d.ts.map +1 -0
- package/dist/erf.js +43 -0
- package/dist/erf.js.map +1 -0
- package/dist/erfc.d.ts +31 -0
- package/dist/erfc.d.ts.map +1 -0
- package/dist/erfc.js +48 -0
- package/dist/erfc.js.map +1 -0
- package/dist/gamma.d.ts +36 -0
- package/dist/gamma.d.ts.map +1 -0
- package/dist/gamma.js +59 -0
- package/dist/gamma.js.map +1 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/inverse-regularized-incomplete-beta.d.ts +34 -0
- package/dist/inverse-regularized-incomplete-beta.d.ts.map +1 -0
- package/dist/inverse-regularized-incomplete-beta.js +136 -0
- package/dist/inverse-regularized-incomplete-beta.js.map +1 -0
- package/dist/inverse-regularized-incomplete-gamma.d.ts +36 -0
- package/dist/inverse-regularized-incomplete-gamma.d.ts.map +1 -0
- package/dist/inverse-regularized-incomplete-gamma.js +149 -0
- package/dist/inverse-regularized-incomplete-gamma.js.map +1 -0
- package/dist/log-beta.d.ts +29 -0
- package/dist/log-beta.d.ts.map +1 -0
- package/dist/log-beta.js +36 -0
- package/dist/log-beta.js.map +1 -0
- package/dist/log-gamma.d.ts +29 -0
- package/dist/log-gamma.d.ts.map +1 -0
- package/dist/log-gamma.js +39 -0
- package/dist/log-gamma.js.map +1 -0
- package/dist/regularized-incomplete-beta.d.ts +41 -0
- package/dist/regularized-incomplete-beta.d.ts.map +1 -0
- package/dist/regularized-incomplete-beta.js +68 -0
- package/dist/regularized-incomplete-beta.js.map +1 -0
- package/dist/regularized-incomplete-gamma-upper.d.ts +38 -0
- package/dist/regularized-incomplete-gamma-upper.d.ts.map +1 -0
- package/dist/regularized-incomplete-gamma-upper.js +52 -0
- package/dist/regularized-incomplete-gamma-upper.js.map +1 -0
- package/dist/regularized-incomplete-gamma.d.ts +44 -0
- package/dist/regularized-incomplete-gamma.d.ts.map +1 -0
- package/dist/regularized-incomplete-gamma.js +58 -0
- package/dist/regularized-incomplete-gamma.js.map +1 -0
- package/dist/trapezoidal-rule.d.ts +35 -0
- package/dist/trapezoidal-rule.d.ts.map +1 -0
- package/dist/trapezoidal-rule.js +50 -0
- package/dist/trapezoidal-rule.js.map +1 -0
- package/package.json +64 -0
package/dist/_lanczos.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lanczos parameter `g`, which is meaningless apart from the coefficient set
|
|
3
|
+
* below — the two are fitted together and cannot be mixed with another pair.
|
|
4
|
+
*/
|
|
5
|
+
export const LANCZOS_G_ = 607 / 128;
|
|
6
|
+
/**
|
|
7
|
+
* Coefficients of the Lanczos rational series for `g = 607/128`, `n = 15`.
|
|
8
|
+
*
|
|
9
|
+
* Chosen over the `g = 7`, `n = 9` set that circulates widely, because that
|
|
10
|
+
* set's _fit_ error grows with the argument — measured against a 60-digit
|
|
11
|
+
* reference it is 2e-16 at `z = 0` but 6.5e-14 at `z = 99` and 1.5e-13 at `z =
|
|
12
|
+
* 499`, which became the error floor of every incomplete gamma and beta built
|
|
13
|
+
* on it. This set holds under 2e-15 across the same range, and under 1.3e-18
|
|
14
|
+
* for `z ≤ 19`.
|
|
15
|
+
*
|
|
16
|
+
* Written as the binary64 values the published decimals round to, rather than
|
|
17
|
+
* the decimals themselves — the stored value is identical either way, and this
|
|
18
|
+
* form does not read as though it carries digits it cannot.
|
|
19
|
+
*/
|
|
20
|
+
const LANCZOS_COEFFICIENTS_ = [
|
|
21
|
+
0.9999999999999971, 57.15623566586292, -59.59796035547549, 14.136097974741746,
|
|
22
|
+
-0.4919138160976202, 0.00003399464998481189, 0.00004652362892704858,
|
|
23
|
+
-0.00009837447530487956, 0.0001580887032249125, -0.00021026444172410488,
|
|
24
|
+
0.00021743961811521265, -0.0001643181065367639, 0.00008441822398385275,
|
|
25
|
+
-0.000026190838401581408, 0.0000036899182659531625,
|
|
26
|
+
];
|
|
27
|
+
/**
|
|
28
|
+
* The Lanczos series `Aɡ(z)`, the slowly-varying factor both `gamma` and
|
|
29
|
+
* `logGamma` multiply their Stirling-shaped leading term by.
|
|
30
|
+
*
|
|
31
|
+
* The coefficients alternate in sign, which looks like a case for compensated
|
|
32
|
+
* summation. Measured: it is not. Neumaier compensation moves the series by a
|
|
33
|
+
* few ulps and leaves Γ a wash — the error that dominates is the fit's, and no
|
|
34
|
+
* better sum can reach it.
|
|
35
|
+
*
|
|
36
|
+
* O(1) — fifteen divisions, fixed by the coefficient set.
|
|
37
|
+
*
|
|
38
|
+
* @param z The shifted argument `x − 1`.
|
|
39
|
+
* @returns `Aɡ(z)`.
|
|
40
|
+
*/
|
|
41
|
+
export function lanczosSeries_(z) {
|
|
42
|
+
let result = LANCZOS_COEFFICIENTS_[0];
|
|
43
|
+
for (let index = 1; index < LANCZOS_COEFFICIENTS_.length; index++) {
|
|
44
|
+
result += LANCZOS_COEFFICIENTS_[index] / (z + index);
|
|
45
|
+
}
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=_lanczos.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_lanczos.js","sourceRoot":"","sources":["../src/_lanczos.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,GAAG,GAAG,GAAG,CAAC;AAEpC;;;;;;;;;;;;;GAaG;AACH,MAAM,qBAAqB,GAAG;IAC7B,kBAAkB,EAAE,iBAAiB,EAAE,CAAC,iBAAiB,EAAE,kBAAkB;IAC7E,CAAC,kBAAkB,EAAE,sBAAsB,EAAE,sBAAsB;IACnE,CAAC,sBAAsB,EAAE,qBAAqB,EAAE,CAAC,sBAAsB;IACvE,sBAAsB,EAAE,CAAC,qBAAqB,EAAE,sBAAsB;IACtE,CAAC,uBAAuB,EAAE,wBAAwB;CACzC,CAAC;AAEX;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,cAAc,CAAC,CAAS;IACvC,IAAI,MAAM,GAAG,qBAAqB,CAAC,CAAC,CAAC,CAAC;IACtC,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,qBAAqB,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;QACnE,MAAM,IAAI,qBAAqB,CAAC,KAAK,CAAE,GAAG,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC;IACvD,CAAC;IACD,OAAO,MAAM,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `ln(1 + x) − x`, computed without the cancellation the literal expression
|
|
3
|
+
* suffers.
|
|
4
|
+
*
|
|
5
|
+
* The two terms agree to leading order — `ln(1 + x) ≈ x − x²/2` — so
|
|
6
|
+
* subtracting them directly loses every digit down to the `x²/2` that survives.
|
|
7
|
+
* The error of the naive form is about `2ε/|x|`, which is 2e-14 by `x = 0.01`
|
|
8
|
+
* and unbounded as `x → 0`.
|
|
9
|
+
*
|
|
10
|
+
* O(1) amortised: the alternating series is used only for `|x| ≤ 0.5`, where it
|
|
11
|
+
* needs at most a few dozen terms, and the direct subtraction takes over above
|
|
12
|
+
* that, where it is accurate because the terms no longer agree.
|
|
13
|
+
*
|
|
14
|
+
* @param x Any real greater than `-1`.
|
|
15
|
+
* @returns `ln(1 + x) − x`, always non-positive.
|
|
16
|
+
*/
|
|
17
|
+
export declare function log1pmx_(x: number): number;
|
|
18
|
+
//# sourceMappingURL=_log1pmx.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_log1pmx.d.ts","sourceRoot":"","sources":["../src/_log1pmx.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAuB1C"}
|
package/dist/_log1pmx.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `ln(1 + x) − x`, computed without the cancellation the literal expression
|
|
3
|
+
* suffers.
|
|
4
|
+
*
|
|
5
|
+
* The two terms agree to leading order — `ln(1 + x) ≈ x − x²/2` — so
|
|
6
|
+
* subtracting them directly loses every digit down to the `x²/2` that survives.
|
|
7
|
+
* The error of the naive form is about `2ε/|x|`, which is 2e-14 by `x = 0.01`
|
|
8
|
+
* and unbounded as `x → 0`.
|
|
9
|
+
*
|
|
10
|
+
* O(1) amortised: the alternating series is used only for `|x| ≤ 0.5`, where it
|
|
11
|
+
* needs at most a few dozen terms, and the direct subtraction takes over above
|
|
12
|
+
* that, where it is accurate because the terms no longer agree.
|
|
13
|
+
*
|
|
14
|
+
* @param x Any real greater than `-1`.
|
|
15
|
+
* @returns `ln(1 + x) − x`, always non-positive.
|
|
16
|
+
*/
|
|
17
|
+
export function log1pmx_(x) {
|
|
18
|
+
if (Math.abs(x) > 0.5) {
|
|
19
|
+
return Math.log1p(x) - x;
|
|
20
|
+
}
|
|
21
|
+
// −x²/2 + x³/3 − x⁴/4 + …, which has no cancellation to lose.
|
|
22
|
+
let total = 0;
|
|
23
|
+
let power = x * x;
|
|
24
|
+
let sign = -1;
|
|
25
|
+
for (let index = 2; index <= 80; index++) {
|
|
26
|
+
const term = (sign * power) / index;
|
|
27
|
+
total += term;
|
|
28
|
+
if (Math.abs(term) <= Math.abs(total) * Number.EPSILON) {
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
power *= x;
|
|
32
|
+
sign = -sign;
|
|
33
|
+
}
|
|
34
|
+
return total;
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=_log1pmx.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_log1pmx.js","sourceRoot":"","sources":["../src/_log1pmx.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,QAAQ,CAAC,CAAS;IACjC,IAAI,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,EAAE,CAAC;QACvB,OAAO,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;IAC1B,CAAC;IAED,8DAA8D;IAC9D,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,IAAI,KAAK,GAAG,CAAC,GAAG,CAAC,CAAC;IAClB,IAAI,IAAI,GAAG,CAAC,CAAC,CAAC;IAEd,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,IAAI,EAAE,EAAE,KAAK,EAAE,EAAE,CAAC;QAC1C,MAAM,IAAI,GAAG,CAAC,IAAI,GAAG,KAAK,CAAC,GAAG,KAAK,CAAC;QACpC,KAAK,IAAI,IAAI,CAAC;QAEd,IAAI,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,MAAM,CAAC,OAAO,EAAE,CAAC;YACxD,MAAM;QACP,CAAC;QAED,KAAK,IAAI,CAAC,CAAC;QACX,IAAI,GAAG,CAAC,IAAI,CAAC;IACd,CAAC;IAED,OAAO,KAAK,CAAC;AACd,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A rough standard normal quantile, accurate to about four decimal places.
|
|
3
|
+
*
|
|
4
|
+
* Abramowitz and Stegun 26.2.23. Deliberately not the accurate approximation
|
|
5
|
+
* `@ac-kit/math-stats` exposes as `normalQuantile` — that package sits above
|
|
6
|
+
* this one, and nothing here needs more than a starting point: this feeds a
|
|
7
|
+
* Halley iteration that triples its own correct digits every step, so four
|
|
8
|
+
* decimals in reaches full precision in three.
|
|
9
|
+
*
|
|
10
|
+
* @param probability Probability in `(0, 1)`.
|
|
11
|
+
* @returns The approximate `z` with `Φ(z) = probability`.
|
|
12
|
+
*/
|
|
13
|
+
export declare function normalQuantileGuess_(probability: number): number;
|
|
14
|
+
//# sourceMappingURL=_normal-quantile-guess.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_normal-quantile-guess.d.ts","sourceRoot":"","sources":["../src/_normal-quantile-guess.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,wBAAgB,oBAAoB,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CAOhE"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A rough standard normal quantile, accurate to about four decimal places.
|
|
3
|
+
*
|
|
4
|
+
* Abramowitz and Stegun 26.2.23. Deliberately not the accurate approximation
|
|
5
|
+
* `@ac-kit/math-stats` exposes as `normalQuantile` — that package sits above
|
|
6
|
+
* this one, and nothing here needs more than a starting point: this feeds a
|
|
7
|
+
* Halley iteration that triples its own correct digits every step, so four
|
|
8
|
+
* decimals in reaches full precision in three.
|
|
9
|
+
*
|
|
10
|
+
* @param probability Probability in `(0, 1)`.
|
|
11
|
+
* @returns The approximate `z` with `Φ(z) = probability`.
|
|
12
|
+
*/
|
|
13
|
+
export function normalQuantileGuess_(probability) {
|
|
14
|
+
const tail = probability < 0.5 ? probability : 1 - probability;
|
|
15
|
+
const t = Math.sqrt(-2 * Math.log(tail));
|
|
16
|
+
const magnitude = t - (2.30753 + 0.27061 * t) / (1 + t * (0.99229 + 0.04481 * t));
|
|
17
|
+
return probability < 0.5 ? -magnitude : magnitude;
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=_normal-quantile-guess.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_normal-quantile-guess.js","sourceRoot":"","sources":["../src/_normal-quantile-guess.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,oBAAoB,CAAC,WAAmB;IACvD,MAAM,IAAI,GAAG,WAAW,GAAG,GAAG,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,GAAG,WAAW,CAAC;IAC/D,MAAM,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC;IACzC,MAAM,SAAS,GACd,CAAC,GAAG,CAAC,OAAO,GAAG,OAAO,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,OAAO,GAAG,OAAO,GAAG,CAAC,CAAC,CAAC,CAAC;IAEjE,OAAO,WAAW,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC;AACnD,CAAC"}
|
package/dist/beta.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The beta function `B(a, b) = Γ(a)Γ(b) / Γ(a + b)`.
|
|
3
|
+
*
|
|
4
|
+
* The ratio that normalizes a Beta distribution, and through it Student's _t_,
|
|
5
|
+
* the _F_ distribution and the binomial. Written out by hand it overflows for
|
|
6
|
+
* shapes far smaller than the ones whose ratio is still perfectly
|
|
7
|
+
* representable, which is the whole reason it is a function rather than an
|
|
8
|
+
* expression.
|
|
9
|
+
*
|
|
10
|
+
* `B` decays fast: it underflows to zero from around `a + b ≈ 1500`. Reach for
|
|
11
|
+
* {@link logBeta} at that point, or any time you would immediately take a
|
|
12
|
+
* logarithm of the result.
|
|
13
|
+
*
|
|
14
|
+
* Relative error is a few ulps while all three gammas are representable. Past
|
|
15
|
+
* that it is bounded below by one ulp of each half-exponent — `B(500, 500)` has
|
|
16
|
+
* a logarithm of `−695`, so no binary64 route gets nearer than about `4e-14`.
|
|
17
|
+
*
|
|
18
|
+
* O(1) time, O(1) memory.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* beta(2, 3); // 0.08333333333333336 — 1/12
|
|
23
|
+
* beta(0.5, 0.5); // 3.1415926535897905 — π
|
|
24
|
+
* beta(500, 500); // 1.4799015991255068e-302
|
|
25
|
+
* beta(2000, 2000); // 0 — underflowed; use logBeta
|
|
26
|
+
* ```;
|
|
27
|
+
*
|
|
28
|
+
* @param a First shape parameter, positive.
|
|
29
|
+
* @param b Second shape parameter, positive.
|
|
30
|
+
* @returns `B(a, b)`, or `NaN` when either parameter is non-positive, where the
|
|
31
|
+
* defining ratio has no real value.
|
|
32
|
+
* @see {@link logBeta} for shapes whose `B` underflows.
|
|
33
|
+
* @see [The gamma and beta functions](https://anthochamp.github.io/node-essentials/topics/math/functions/gamma/)
|
|
34
|
+
*/
|
|
35
|
+
export declare function beta(a: number, b: number): number;
|
|
36
|
+
//# sourceMappingURL=beta.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"beta.d.ts","sourceRoot":"","sources":["../src/beta.ts"],"names":[],"mappings":"AAsCA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,wBAAgB,IAAI,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAajD"}
|
package/dist/beta.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { SQRT_TWO_PI } from "@ac-kit/math-scalar";
|
|
2
|
+
import { expSplit_ } from "./_exp-split.js";
|
|
3
|
+
import { LANCZOS_G_, lanczosSeries_ } from "./_lanczos.js";
|
|
4
|
+
import { gamma } from "./gamma.js";
|
|
5
|
+
/**
|
|
6
|
+
* `B(a, b)` from the Lanczos form of all three gammas, with the terms regrouped
|
|
7
|
+
* so nothing of order `ln Γ` is ever built.
|
|
8
|
+
*
|
|
9
|
+
* `Γ(a)Γ(b)/Γ(a+b)` collapses to
|
|
10
|
+
* `√(2π)·e^(½−g)·(Aₐ·A_b/A_c)·(tₐ/t_c)^(a−½)·(t_b/t_c)^(b−½)·t_c^(−½)`, where
|
|
11
|
+
* the `e^(−t)` factors cancel to the constant `e^(½−g)` exactly. The two
|
|
12
|
+
* remaining powers are exponentiated separately, so each carries one ulp of its
|
|
13
|
+
* own half-sized exponent rather than one ulp of their sum.
|
|
14
|
+
*
|
|
15
|
+
* Taken only past `a + b ≳ 171`, where the defining ratio has lost a gamma to
|
|
16
|
+
* overflow; below that the ratio is both cheaper and more accurate. Closing the
|
|
17
|
+
* residual `4e-14` at extreme shapes needs a double-double exponent, which
|
|
18
|
+
* `@ac-kit/math-scalar` has planned but not built.
|
|
19
|
+
*/
|
|
20
|
+
function betaLanczos_(a, b) {
|
|
21
|
+
const shiftedSum = a + b + LANCZOS_G_ - 0.5;
|
|
22
|
+
const lead = (SQRT_TWO_PI *
|
|
23
|
+
Math.exp(0.5 - LANCZOS_G_) *
|
|
24
|
+
lanczosSeries_(a - 1) *
|
|
25
|
+
lanczosSeries_(b - 1)) /
|
|
26
|
+
(lanczosSeries_(a + b - 1) * Math.sqrt(shiftedSum));
|
|
27
|
+
// `tₐ/t_c = 1 − b/t_c` and `t_b/t_c = 1 − a/t_c`, since `t_c − tₐ = b`.
|
|
28
|
+
const first = (a - 0.5) * Math.log1p(-b / shiftedSum);
|
|
29
|
+
const second = (b - 0.5) * Math.log1p(-a / shiftedSum);
|
|
30
|
+
return lead * expSplit_(first, second);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* The beta function `B(a, b) = Γ(a)Γ(b) / Γ(a + b)`.
|
|
34
|
+
*
|
|
35
|
+
* The ratio that normalizes a Beta distribution, and through it Student's _t_,
|
|
36
|
+
* the _F_ distribution and the binomial. Written out by hand it overflows for
|
|
37
|
+
* shapes far smaller than the ones whose ratio is still perfectly
|
|
38
|
+
* representable, which is the whole reason it is a function rather than an
|
|
39
|
+
* expression.
|
|
40
|
+
*
|
|
41
|
+
* `B` decays fast: it underflows to zero from around `a + b ≈ 1500`. Reach for
|
|
42
|
+
* {@link logBeta} at that point, or any time you would immediately take a
|
|
43
|
+
* logarithm of the result.
|
|
44
|
+
*
|
|
45
|
+
* Relative error is a few ulps while all three gammas are representable. Past
|
|
46
|
+
* that it is bounded below by one ulp of each half-exponent — `B(500, 500)` has
|
|
47
|
+
* a logarithm of `−695`, so no binary64 route gets nearer than about `4e-14`.
|
|
48
|
+
*
|
|
49
|
+
* O(1) time, O(1) memory.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```ts
|
|
53
|
+
* beta(2, 3); // 0.08333333333333336 — 1/12
|
|
54
|
+
* beta(0.5, 0.5); // 3.1415926535897905 — π
|
|
55
|
+
* beta(500, 500); // 1.4799015991255068e-302
|
|
56
|
+
* beta(2000, 2000); // 0 — underflowed; use logBeta
|
|
57
|
+
* ```;
|
|
58
|
+
*
|
|
59
|
+
* @param a First shape parameter, positive.
|
|
60
|
+
* @param b Second shape parameter, positive.
|
|
61
|
+
* @returns `B(a, b)`, or `NaN` when either parameter is non-positive, where the
|
|
62
|
+
* defining ratio has no real value.
|
|
63
|
+
* @see {@link logBeta} for shapes whose `B` underflows.
|
|
64
|
+
* @see [The gamma and beta functions](https://anthochamp.github.io/node-essentials/topics/math/functions/gamma/)
|
|
65
|
+
*/
|
|
66
|
+
export function beta(a, b) {
|
|
67
|
+
if (!(a > 0) || !(b > 0)) {
|
|
68
|
+
return Number.NaN;
|
|
69
|
+
}
|
|
70
|
+
const product = gamma(a) * gamma(b);
|
|
71
|
+
const total = gamma(a + b);
|
|
72
|
+
if (Number.isFinite(product) && Number.isFinite(total) && total !== 0) {
|
|
73
|
+
return product / total;
|
|
74
|
+
}
|
|
75
|
+
return betaLanczos_(a, b);
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=beta.js.map
|
package/dist/beta.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"beta.js","sourceRoot":"","sources":["../src/beta.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAElD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAC3D,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC;;;;;;;;;;;;;;GAcG;AACH,SAAS,YAAY,CAAC,CAAS,EAAE,CAAS;IACzC,MAAM,UAAU,GAAG,CAAC,GAAG,CAAC,GAAG,UAAU,GAAG,GAAG,CAAC;IAE5C,MAAM,IAAI,GACT,CAAC,WAAW;QACX,IAAI,CAAC,GAAG,CAAC,GAAG,GAAG,UAAU,CAAC;QAC1B,cAAc,CAAC,CAAC,GAAG,CAAC,CAAC;QACrB,cAAc,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;QACvB,CAAC,cAAc,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC;IAErD,wEAAwE;IACxE,MAAM,KAAK,GAAG,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,CAAC;IACtD,MAAM,MAAM,GAAG,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,CAAC;IAEvD,OAAO,IAAI,GAAG,SAAS,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;AACxC,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,MAAM,UAAU,IAAI,CAAC,CAAS,EAAE,CAAS;IACxC,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC;QAC1B,OAAO,MAAM,CAAC,GAAG,CAAC;IACnB,CAAC;IAED,MAAM,OAAO,GAAG,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;IACpC,MAAM,KAAK,GAAG,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;IAE3B,IAAI,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,KAAK,KAAK,CAAC,EAAE,CAAC;QACvE,OAAO,OAAO,GAAG,KAAK,CAAC;IACxB,CAAC;IAED,OAAO,YAAY,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AAC3B,CAAC"}
|
package/dist/bisect.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { ConvergenceOptions } from "./convergence-options.js";
|
|
2
|
+
/**
|
|
3
|
+
* Finds where a function crosses zero, by repeatedly halving an interval known
|
|
4
|
+
* to contain the crossing.
|
|
5
|
+
*
|
|
6
|
+
* You give it two points where `f` has opposite signs. Somewhere between them
|
|
7
|
+
* the function must pass through zero, so the answer is somewhere in that
|
|
8
|
+
* interval; bisection tries the midpoint, keeps whichever half still straddles
|
|
9
|
+
* zero, and repeats. Slow but incapable of failing — unlike Newton's method,
|
|
10
|
+
* which needs a derivative and can wander off from a bad start with no
|
|
11
|
+
* warning.
|
|
12
|
+
*
|
|
13
|
+
* Use it when reliability matters more than speed, when `f` is expensive to
|
|
14
|
+
* differentiate, or when you have no idea what the function looks like between
|
|
15
|
+
* the endpoints.
|
|
16
|
+
*
|
|
17
|
+
* The error after `n` steps is at most `(upper − lower) / 2ⁿ`, so each step
|
|
18
|
+
* buys one bit. Reaching `1e-12` from a bracket of width 2 takes about 41.
|
|
19
|
+
*
|
|
20
|
+
* O(log₂((upper − lower) / tolerance)) calls to `f`, capped at `maxIterations`;
|
|
21
|
+
* O(1) memory.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* bisect((x) => x * x - 2, 0, 2); // 1.414213562372879, against √2 = 1.4142135623730951
|
|
26
|
+
* bisect((x) => x * x - 2, 2, 3); // throws RangeError — no sign change
|
|
27
|
+
* ```;
|
|
28
|
+
*
|
|
29
|
+
* @param f The continuous function whose root is sought.
|
|
30
|
+
* @param lower Lower end of the bracket.
|
|
31
|
+
* @param upper Upper end of the bracket.
|
|
32
|
+
* @param options Termination criteria. `tolerance` is the absolute bracket
|
|
33
|
+
* width to stop at, defaulting to `1e-12`; `maxIterations` defaults to 200.
|
|
34
|
+
* @returns An approximation of the root.
|
|
35
|
+
* @throws {RangeError} When `f(lower)` and `f(upper)` have the same sign, so
|
|
36
|
+
* nothing guarantees a root between them.
|
|
37
|
+
* @see [Root finding](https://anthochamp.github.io/node-essentials/topics/math/calculus/root-finding/)
|
|
38
|
+
*/
|
|
39
|
+
export declare function bisect(f: (x: number) => number, lower: number, upper: number, options?: ConvergenceOptions): number;
|
|
40
|
+
//# sourceMappingURL=bisect.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bisect.d.ts","sourceRoot":"","sources":["../src/bisect.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAEnE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,wBAAgB,MAAM,CACrB,CAAC,EAAE,CAAC,CAAC,EAAE,MAAM,KAAK,MAAM,EACxB,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,EACb,OAAO,CAAC,EAAE,kBAAkB,GAC1B,MAAM,CAuCR"}
|
package/dist/bisect.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Finds where a function crosses zero, by repeatedly halving an interval known
|
|
3
|
+
* to contain the crossing.
|
|
4
|
+
*
|
|
5
|
+
* You give it two points where `f` has opposite signs. Somewhere between them
|
|
6
|
+
* the function must pass through zero, so the answer is somewhere in that
|
|
7
|
+
* interval; bisection tries the midpoint, keeps whichever half still straddles
|
|
8
|
+
* zero, and repeats. Slow but incapable of failing — unlike Newton's method,
|
|
9
|
+
* which needs a derivative and can wander off from a bad start with no
|
|
10
|
+
* warning.
|
|
11
|
+
*
|
|
12
|
+
* Use it when reliability matters more than speed, when `f` is expensive to
|
|
13
|
+
* differentiate, or when you have no idea what the function looks like between
|
|
14
|
+
* the endpoints.
|
|
15
|
+
*
|
|
16
|
+
* The error after `n` steps is at most `(upper − lower) / 2ⁿ`, so each step
|
|
17
|
+
* buys one bit. Reaching `1e-12` from a bracket of width 2 takes about 41.
|
|
18
|
+
*
|
|
19
|
+
* O(log₂((upper − lower) / tolerance)) calls to `f`, capped at `maxIterations`;
|
|
20
|
+
* O(1) memory.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* bisect((x) => x * x - 2, 0, 2); // 1.414213562372879, against √2 = 1.4142135623730951
|
|
25
|
+
* bisect((x) => x * x - 2, 2, 3); // throws RangeError — no sign change
|
|
26
|
+
* ```;
|
|
27
|
+
*
|
|
28
|
+
* @param f The continuous function whose root is sought.
|
|
29
|
+
* @param lower Lower end of the bracket.
|
|
30
|
+
* @param upper Upper end of the bracket.
|
|
31
|
+
* @param options Termination criteria. `tolerance` is the absolute bracket
|
|
32
|
+
* width to stop at, defaulting to `1e-12`; `maxIterations` defaults to 200.
|
|
33
|
+
* @returns An approximation of the root.
|
|
34
|
+
* @throws {RangeError} When `f(lower)` and `f(upper)` have the same sign, so
|
|
35
|
+
* nothing guarantees a root between them.
|
|
36
|
+
* @see [Root finding](https://anthochamp.github.io/node-essentials/topics/math/calculus/root-finding/)
|
|
37
|
+
*/
|
|
38
|
+
export function bisect(f, lower, upper, options) {
|
|
39
|
+
const tolerance = options?.tolerance ?? 1e-12;
|
|
40
|
+
const maxIterations = options?.maxIterations ?? 200;
|
|
41
|
+
const { signal } = options ?? {};
|
|
42
|
+
let a = lower;
|
|
43
|
+
let b = upper;
|
|
44
|
+
let fa = f(a);
|
|
45
|
+
const fb = f(b);
|
|
46
|
+
if (fa === 0) {
|
|
47
|
+
return a;
|
|
48
|
+
}
|
|
49
|
+
if (fb === 0) {
|
|
50
|
+
return b;
|
|
51
|
+
}
|
|
52
|
+
if (fa * fb > 0) {
|
|
53
|
+
throw new RangeError("bisect: the bracket endpoints must have opposite signs");
|
|
54
|
+
}
|
|
55
|
+
for (let i = 0; i < maxIterations && b - a > tolerance; i++) {
|
|
56
|
+
signal?.throwIfAborted();
|
|
57
|
+
const midpoint = a + (b - a) / 2;
|
|
58
|
+
const fm = f(midpoint);
|
|
59
|
+
if (fm === 0) {
|
|
60
|
+
return midpoint;
|
|
61
|
+
}
|
|
62
|
+
if (fa * fm < 0) {
|
|
63
|
+
b = midpoint;
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
a = midpoint;
|
|
67
|
+
fa = fm;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return a + (b - a) / 2;
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=bisect.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bisect.js","sourceRoot":"","sources":["../src/bisect.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,MAAM,UAAU,MAAM,CACrB,CAAwB,EACxB,KAAa,EACb,KAAa,EACb,OAA4B;IAE5B,MAAM,SAAS,GAAG,OAAO,EAAE,SAAS,IAAI,KAAK,CAAC;IAC9C,MAAM,aAAa,GAAG,OAAO,EAAE,aAAa,IAAI,GAAG,CAAC;IACpD,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,IAAI,EAAE,CAAC;IAEjC,IAAI,CAAC,GAAG,KAAK,CAAC;IACd,IAAI,CAAC,GAAG,KAAK,CAAC;IACd,IAAI,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;IACd,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;IAEhB,IAAI,EAAE,KAAK,CAAC,EAAE,CAAC;QACd,OAAO,CAAC,CAAC;IACV,CAAC;IACD,IAAI,EAAE,KAAK,CAAC,EAAE,CAAC;QACd,OAAO,CAAC,CAAC;IACV,CAAC;IACD,IAAI,EAAE,GAAG,EAAE,GAAG,CAAC,EAAE,CAAC;QACjB,MAAM,IAAI,UAAU,CACnB,wDAAwD,CACxD,CAAC;IACH,CAAC;IAED,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,aAAa,IAAI,CAAC,GAAG,CAAC,GAAG,SAAS,EAAE,CAAC,EAAE,EAAE,CAAC;QAC7D,MAAM,EAAE,cAAc,EAAE,CAAC;QAEzB,MAAM,QAAQ,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC;QACjC,MAAM,EAAE,GAAG,CAAC,CAAC,QAAQ,CAAC,CAAC;QACvB,IAAI,EAAE,KAAK,CAAC,EAAE,CAAC;YACd,OAAO,QAAQ,CAAC;QACjB,CAAC;QACD,IAAI,EAAE,GAAG,EAAE,GAAG,CAAC,EAAE,CAAC;YACjB,CAAC,GAAG,QAAQ,CAAC;QACd,CAAC;aAAM,CAAC;YACP,CAAC,GAAG,QAAQ,CAAC;YACb,EAAE,GAAG,EAAE,CAAC;QACT,CAAC;IACF,CAAC;IAED,OAAO,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC;AACxB,CAAC"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { ConvergenceOptions } from "./convergence-options.js";
|
|
2
|
+
import type { ConvergenceResult } from "./convergence-result.js";
|
|
3
|
+
/**
|
|
4
|
+
* The coefficients of `b₀ + a₁/(b₁ + a₂/(b₂ + a₃/(b₃ + ⋯)))`, supplied as
|
|
5
|
+
* functions of the term index so an unbounded fraction needs no array.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* // Every coefficient is 1, for any depth.
|
|
10
|
+
* const goldenRatio: ContinuedFraction = {
|
|
11
|
+
* numerator: () => 1,
|
|
12
|
+
* denominator: () => 1,
|
|
13
|
+
* };
|
|
14
|
+
* ```;
|
|
15
|
+
*
|
|
16
|
+
* @see [Continued fractions](https://anthochamp.github.io/node-essentials/topics/math/calculus/series/)
|
|
17
|
+
*/
|
|
18
|
+
export type ContinuedFraction = {
|
|
19
|
+
/** The partial numerator `aⱼ`, called with `index ≥ 1`. */
|
|
20
|
+
numerator: (index: number) => number;
|
|
21
|
+
/**
|
|
22
|
+
* The partial denominator `bⱼ`, called with `index ≥ 0`. `b₀` is the leading
|
|
23
|
+
* term, outside the fraction proper.
|
|
24
|
+
*/
|
|
25
|
+
denominator: (index: number) => number;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Evaluates a continued fraction — an expression of the form `b₀ + a₁/(b₁ +
|
|
29
|
+
* a₂/(b₂ + ⋯))` that in principle never ends.
|
|
30
|
+
*
|
|
31
|
+
* Many functions have a continued fraction that converges far faster than their
|
|
32
|
+
* power series, especially away from the origin, which is why the incomplete
|
|
33
|
+
* gamma and beta in this package are built on one. This evaluates such a
|
|
34
|
+
* fraction without having to decide up front how deep to go: the running value
|
|
35
|
+
* is multiplied by a factor tending to 1, and the iteration stops when that
|
|
36
|
+
* factor is within `tolerance` of 1.
|
|
37
|
+
*
|
|
38
|
+
* It reports rather than throws. A fraction that exhausts `maxIterations` still
|
|
39
|
+
* returns its best estimate, with `converged: false` — check it before trusting
|
|
40
|
+
* the value.
|
|
41
|
+
*
|
|
42
|
+
* O(`maxIterations`) calls to each of {@link ContinuedFraction.numerator} and
|
|
43
|
+
* {@link ContinuedFraction.denominator}, and the same count of divisions. O(1)
|
|
44
|
+
* memory: the coefficients are functions of the index, so an unbounded fraction
|
|
45
|
+
* needs no array.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* // 1 + 1/(1 + 1/(1 + ⋯)) is the golden ratio.
|
|
50
|
+
* const phi = continuedFractionEval({
|
|
51
|
+
* numerator: () => 1,
|
|
52
|
+
* denominator: () => 1,
|
|
53
|
+
* });
|
|
54
|
+
* phi.value; // 1.618033988749895
|
|
55
|
+
* phi.iterations; // 38
|
|
56
|
+
* phi.converged; // true
|
|
57
|
+
* ```;
|
|
58
|
+
*
|
|
59
|
+
* @param fraction The partial numerators and denominators.
|
|
60
|
+
* @param options Termination criteria. `tolerance` is compared against the
|
|
61
|
+
* distance from 1 of the multiplicative update, so it is relative; it
|
|
62
|
+
* defaults to `Number.EPSILON` and `maxIterations` to 300.
|
|
63
|
+
* @returns The fraction's value, the iteration count, and whether the tolerance
|
|
64
|
+
* was met.
|
|
65
|
+
* @see [Continued fractions](https://anthochamp.github.io/node-essentials/topics/math/calculus/series/)
|
|
66
|
+
*/
|
|
67
|
+
export declare function continuedFractionEval(fraction: ContinuedFraction, options?: ConvergenceOptions): ConvergenceResult<number>;
|
|
68
|
+
//# sourceMappingURL=continued-fraction-eval.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"continued-fraction-eval.d.ts","sourceRoot":"","sources":["../src/continued-fraction-eval.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AACnE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAWjE;;;;;;;;;;;;;;GAcG;AACH,MAAM,MAAM,iBAAiB,GAAG;IAC/B,2DAA2D;IAC3D,SAAS,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,CAAC;IAErC;;;OAGG;IACH,WAAW,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,CAAC;CACvC,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,wBAAgB,qBAAqB,CACpC,QAAQ,EAAE,iBAAiB,EAC3B,OAAO,CAAC,EAAE,kBAAkB,GAC1B,iBAAiB,CAAC,MAAM,CAAC,CA8C3B"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Smallest magnitude a vanishing partial is rescued to.
|
|
3
|
+
*
|
|
4
|
+
* Lentz's method divides by its running denominator, so a zero there is fatal.
|
|
5
|
+
* Substituting a value this small perturbs the result by less than the working
|
|
6
|
+
* precision — the standard choice of `smallest normal / ε`.
|
|
7
|
+
*/
|
|
8
|
+
const TINY_ = 2 ** -1022 / Number.EPSILON;
|
|
9
|
+
/**
|
|
10
|
+
* Evaluates a continued fraction — an expression of the form `b₀ + a₁/(b₁ +
|
|
11
|
+
* a₂/(b₂ + ⋯))` that in principle never ends.
|
|
12
|
+
*
|
|
13
|
+
* Many functions have a continued fraction that converges far faster than their
|
|
14
|
+
* power series, especially away from the origin, which is why the incomplete
|
|
15
|
+
* gamma and beta in this package are built on one. This evaluates such a
|
|
16
|
+
* fraction without having to decide up front how deep to go: the running value
|
|
17
|
+
* is multiplied by a factor tending to 1, and the iteration stops when that
|
|
18
|
+
* factor is within `tolerance` of 1.
|
|
19
|
+
*
|
|
20
|
+
* It reports rather than throws. A fraction that exhausts `maxIterations` still
|
|
21
|
+
* returns its best estimate, with `converged: false` — check it before trusting
|
|
22
|
+
* the value.
|
|
23
|
+
*
|
|
24
|
+
* O(`maxIterations`) calls to each of {@link ContinuedFraction.numerator} and
|
|
25
|
+
* {@link ContinuedFraction.denominator}, and the same count of divisions. O(1)
|
|
26
|
+
* memory: the coefficients are functions of the index, so an unbounded fraction
|
|
27
|
+
* needs no array.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* // 1 + 1/(1 + 1/(1 + ⋯)) is the golden ratio.
|
|
32
|
+
* const phi = continuedFractionEval({
|
|
33
|
+
* numerator: () => 1,
|
|
34
|
+
* denominator: () => 1,
|
|
35
|
+
* });
|
|
36
|
+
* phi.value; // 1.618033988749895
|
|
37
|
+
* phi.iterations; // 38
|
|
38
|
+
* phi.converged; // true
|
|
39
|
+
* ```;
|
|
40
|
+
*
|
|
41
|
+
* @param fraction The partial numerators and denominators.
|
|
42
|
+
* @param options Termination criteria. `tolerance` is compared against the
|
|
43
|
+
* distance from 1 of the multiplicative update, so it is relative; it
|
|
44
|
+
* defaults to `Number.EPSILON` and `maxIterations` to 300.
|
|
45
|
+
* @returns The fraction's value, the iteration count, and whether the tolerance
|
|
46
|
+
* was met.
|
|
47
|
+
* @see [Continued fractions](https://anthochamp.github.io/node-essentials/topics/math/calculus/series/)
|
|
48
|
+
*/
|
|
49
|
+
export function continuedFractionEval(fraction, options) {
|
|
50
|
+
// The modified Lentz algorithm, which works forwards from `b₀`. The backward
|
|
51
|
+
// evaluation it replaces has to pick a depth up front and re-run the whole
|
|
52
|
+
// fraction when the pick was too shallow.
|
|
53
|
+
const tolerance = options?.tolerance ?? Number.EPSILON;
|
|
54
|
+
const maxIterations = options?.maxIterations ?? 300;
|
|
55
|
+
const { signal } = options ?? {};
|
|
56
|
+
const { numerator, denominator } = fraction;
|
|
57
|
+
let value = denominator(0);
|
|
58
|
+
if (value === 0) {
|
|
59
|
+
value = TINY_;
|
|
60
|
+
}
|
|
61
|
+
// `c` and `d` carry the forward recurrence's numerator and denominator
|
|
62
|
+
// ratios; their product is the update applied to `value` each step.
|
|
63
|
+
let c = value;
|
|
64
|
+
let d = 0;
|
|
65
|
+
for (let index = 1; index <= maxIterations; index++) {
|
|
66
|
+
signal?.throwIfAborted();
|
|
67
|
+
const a = numerator(index);
|
|
68
|
+
const b = denominator(index);
|
|
69
|
+
d = b + a * d;
|
|
70
|
+
if (d === 0) {
|
|
71
|
+
d = TINY_;
|
|
72
|
+
}
|
|
73
|
+
c = b + a / c;
|
|
74
|
+
if (c === 0) {
|
|
75
|
+
c = TINY_;
|
|
76
|
+
}
|
|
77
|
+
d = 1 / d;
|
|
78
|
+
const update = c * d;
|
|
79
|
+
value *= update;
|
|
80
|
+
if (Math.abs(update - 1) < tolerance) {
|
|
81
|
+
return { value, iterations: index, converged: true };
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return { value, iterations: maxIterations, converged: false };
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=continued-fraction-eval.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"continued-fraction-eval.js","sourceRoot":"","sources":["../src/continued-fraction-eval.ts"],"names":[],"mappings":"AAGA;;;;;;GAMG;AACH,MAAM,KAAK,GAAG,CAAC,IAAI,CAAC,IAAI,GAAG,MAAM,CAAC,OAAO,CAAC;AA4B1C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,MAAM,UAAU,qBAAqB,CACpC,QAA2B,EAC3B,OAA4B;IAE5B,6EAA6E;IAC7E,2EAA2E;IAC3E,0CAA0C;IAC1C,MAAM,SAAS,GAAG,OAAO,EAAE,SAAS,IAAI,MAAM,CAAC,OAAO,CAAC;IACvD,MAAM,aAAa,GAAG,OAAO,EAAE,aAAa,IAAI,GAAG,CAAC;IACpD,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,IAAI,EAAE,CAAC;IACjC,MAAM,EAAE,SAAS,EAAE,WAAW,EAAE,GAAG,QAAQ,CAAC;IAE5C,IAAI,KAAK,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC;IAC3B,IAAI,KAAK,KAAK,CAAC,EAAE,CAAC;QACjB,KAAK,GAAG,KAAK,CAAC;IACf,CAAC;IAED,uEAAuE;IACvE,oEAAoE;IACpE,IAAI,CAAC,GAAG,KAAK,CAAC;IACd,IAAI,CAAC,GAAG,CAAC,CAAC;IAEV,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,IAAI,aAAa,EAAE,KAAK,EAAE,EAAE,CAAC;QACrD,MAAM,EAAE,cAAc,EAAE,CAAC;QAEzB,MAAM,CAAC,GAAG,SAAS,CAAC,KAAK,CAAC,CAAC;QAC3B,MAAM,CAAC,GAAG,WAAW,CAAC,KAAK,CAAC,CAAC;QAE7B,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACd,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;YACb,CAAC,GAAG,KAAK,CAAC;QACX,CAAC;QAED,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACd,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;YACb,CAAC,GAAG,KAAK,CAAC;QACX,CAAC;QAED,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAEV,MAAM,MAAM,GAAG,CAAC,GAAG,CAAC,CAAC;QACrB,KAAK,IAAI,MAAM,CAAC;QAEhB,IAAI,IAAI,CAAC,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,SAAS,EAAE,CAAC;YACtC,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,KAAK,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC;QACtD,CAAC;IACF,CAAC;IAED,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,aAAa,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC;AAC/D,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An iterative routine hit its iteration limit before meeting its tolerance.
|
|
3
|
+
*
|
|
4
|
+
* Thrown only by routines that return a bare value, where a caller has no way
|
|
5
|
+
* to notice the shortfall. Routines returning a {@link ConvergenceResult} report
|
|
6
|
+
* it in `converged` instead and never throw this.
|
|
7
|
+
*
|
|
8
|
+
* Catching it is rarely the right response: it almost always means the input
|
|
9
|
+
* was outside the regime the routine covers, or `maxIterations` was set too low
|
|
10
|
+
* for the accuracy asked of it. Raise one, or loosen the other.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* regularizedIncompleteGamma(3, 20, { maxIterations: 2 });
|
|
15
|
+
* // ConvergenceError: regularizedIncompleteGamma: no convergence after 2 iterations
|
|
16
|
+
* ```;
|
|
17
|
+
*
|
|
18
|
+
* @see [Convergence and iteration limits](https://anthochamp.github.io/node-essentials/topics/math/calculus/convergence/)
|
|
19
|
+
*/
|
|
20
|
+
export declare class ConvergenceError extends Error {
|
|
21
|
+
/** Name of the routine that gave up, so the message names a call site. */
|
|
22
|
+
readonly routine: string;
|
|
23
|
+
/** Iterations that ran before the limit was reached. */
|
|
24
|
+
readonly iterations: number;
|
|
25
|
+
constructor(
|
|
26
|
+
/** Name of the routine that gave up, so the message names a call site. */
|
|
27
|
+
routine: string,
|
|
28
|
+
/** Iterations that ran before the limit was reached. */
|
|
29
|
+
iterations: number);
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=convergence-error.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"convergence-error.d.ts","sourceRoot":"","sources":["../src/convergence-error.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,gBAAiB,SAAQ,KAAK;IAEzC,0EAA0E;IAC1E,QAAQ,CAAC,OAAO,EAAE,MAAM;IAExB,wDAAwD;IACxD,QAAQ,CAAC,UAAU,EAAE,MAAM;IAL5B;IACC,0EAA0E;IACjE,OAAO,EAAE,MAAM;IAExB,wDAAwD;IAC/C,UAAU,EAAE,MAAM,EAI3B;CACD"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An iterative routine hit its iteration limit before meeting its tolerance.
|
|
3
|
+
*
|
|
4
|
+
* Thrown only by routines that return a bare value, where a caller has no way
|
|
5
|
+
* to notice the shortfall. Routines returning a {@link ConvergenceResult} report
|
|
6
|
+
* it in `converged` instead and never throw this.
|
|
7
|
+
*
|
|
8
|
+
* Catching it is rarely the right response: it almost always means the input
|
|
9
|
+
* was outside the regime the routine covers, or `maxIterations` was set too low
|
|
10
|
+
* for the accuracy asked of it. Raise one, or loosen the other.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* regularizedIncompleteGamma(3, 20, { maxIterations: 2 });
|
|
15
|
+
* // ConvergenceError: regularizedIncompleteGamma: no convergence after 2 iterations
|
|
16
|
+
* ```;
|
|
17
|
+
*
|
|
18
|
+
* @see [Convergence and iteration limits](https://anthochamp.github.io/node-essentials/topics/math/calculus/convergence/)
|
|
19
|
+
*/
|
|
20
|
+
export class ConvergenceError extends Error {
|
|
21
|
+
routine;
|
|
22
|
+
iterations;
|
|
23
|
+
constructor(
|
|
24
|
+
/** Name of the routine that gave up, so the message names a call site. */
|
|
25
|
+
routine,
|
|
26
|
+
/** Iterations that ran before the limit was reached. */
|
|
27
|
+
iterations) {
|
|
28
|
+
super(`${routine}: no convergence after ${iterations} iterations`);
|
|
29
|
+
this.routine = routine;
|
|
30
|
+
this.iterations = iterations;
|
|
31
|
+
this.name = "ConvergenceError";
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=convergence-error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"convergence-error.js","sourceRoot":"","sources":["../src/convergence-error.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,OAAO,gBAAiB,SAAQ,KAAK;IAGhC,OAAO;IAGP,UAAU;IALpB;IACC,0EAA0E;IACjE,OAAe;IAExB,wDAAwD;IAC/C,UAAkB;QAE3B,KAAK,CAAC,GAAG,OAAO,0BAA0B,UAAU,aAAa,CAAC,CAAC;uBAL1D,OAAO;0BAGP,UAAU;QAGnB,IAAI,CAAC,IAAI,GAAG,kBAAkB,CAAC;IAChC,CAAC;CACD"}
|