@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/LICENSE
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-2026 Anthony Champagne and contributors
|
|
4
|
+
|
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
|
7
|
+
|
|
8
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+
list of conditions and the following disclaimer.
|
|
10
|
+
|
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
+
this list of conditions and the following disclaimer in the documentation
|
|
13
|
+
and/or other materials provided with the distribution.
|
|
14
|
+
|
|
15
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
16
|
+
contributors may be used to endorse or promote products derived from
|
|
17
|
+
this software without specific prior written permission.
|
|
18
|
+
|
|
19
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
20
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
21
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
22
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
23
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
24
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
25
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
26
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
27
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
28
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# @ac-kit/math-analysis
|
|
2
|
+
|
|
3
|
+
Numerical analysis: approximating the operations of continuous mathematics —
|
|
4
|
+
roots, integrals, derivatives, differential equations — with a finite number of
|
|
5
|
+
floating-point steps.
|
|
6
|
+
|
|
7
|
+
## Scope
|
|
8
|
+
|
|
9
|
+
Every function here takes a callable `f: (x: number) => number` and returns an
|
|
10
|
+
approximation with a stated error behaviour. That is the distinguishing trait:
|
|
11
|
+
the input is a _function_, not a value.
|
|
12
|
+
|
|
13
|
+
Operations on values live in `@ac-kit/math-scalar`; operations on sampled
|
|
14
|
+
sequences live in `@ac-kit/math-signal`.
|
|
15
|
+
|
|
16
|
+
## Contents
|
|
17
|
+
|
|
18
|
+
| Symbol | Description | Convergence |
|
|
19
|
+
| --------------------------------------------- | ------------------------------------------------------------ | ---------------------- |
|
|
20
|
+
| `bisect(f, lower, upper, options)` | Root of a continuous function within a sign-changing bracket | linear, `(b − a) / 2ⁿ` |
|
|
21
|
+
| `trapezoidalRule(f, lower, upper, intervals)` | Definite integral by piecewise linear approximation | `O(h²)` |
|
|
22
|
+
| `continuedFractionEval(fraction, options)` | Value of a continued fraction, by modified Lentz | forward, no truncation guess |
|
|
23
|
+
|
|
24
|
+
### Special functions
|
|
25
|
+
|
|
26
|
+
| Symbol | Description |
|
|
27
|
+
| --- | --- |
|
|
28
|
+
| `gamma(x)`, `logGamma(x)` | The gamma function and its logarithm |
|
|
29
|
+
| `beta(a, b)`, `logBeta(a, b)` | `Γ(a)Γ(b) / Γ(a + b)`, and its logarithm |
|
|
30
|
+
| `erf(x)`, `erfc(x)` | Error function and its complement |
|
|
31
|
+
| `regularizedIncompleteGamma(a, x)` | `P(a, x)` — lower tail of a `Gamma(a, 1)` |
|
|
32
|
+
| `regularizedIncompleteGammaUpper(a, x)` | `Q(a, x)` — upper tail, computed directly |
|
|
33
|
+
| `inverseRegularizedIncompleteGamma(a, p)` | The `x` with `P(a, x) = p` |
|
|
34
|
+
| `regularizedIncompleteBeta(a, b, x)` | `Iₓ(a, b)` — lower tail of a `Beta(a, b)` |
|
|
35
|
+
| `inverseRegularizedIncompleteBeta(a, b, p)` | The `x` with `Iₓ(a, b) = p` |
|
|
36
|
+
|
|
37
|
+
Each name says "regularized" rather than leaving it to be inferred: libraries
|
|
38
|
+
disagree over whether the bare name means the ratio in `[0, 1]` or the
|
|
39
|
+
unnormalised integral, and the two differ by a factor no caller should have to
|
|
40
|
+
guess at. Parameters lead and the variable trails throughout, so the gamma and
|
|
41
|
+
beta families agree on argument order.
|
|
42
|
+
|
|
43
|
+
**Reach for the upper form when the answer is small.** `Q(a, x)` and `erfc(x)`
|
|
44
|
+
are not conveniences over `1 − P(a, x)` and `1 − erf(x)`: that subtraction has
|
|
45
|
+
already discarded the whole answer by the time it is worth asking for. `erf(10)`
|
|
46
|
+
is exactly `1` in binary64, while `erfc(10)` is `2.09e-45` and correct.
|
|
47
|
+
|
|
48
|
+
## Reliability over speed
|
|
49
|
+
|
|
50
|
+
`bisect` converges linearly, which is slow next to Newton's quadratic rate. It
|
|
51
|
+
is nonetheless the right default: given a bracket where `f` changes sign, the
|
|
52
|
+
intermediate value theorem guarantees a root inside, and bisection cannot fail
|
|
53
|
+
to find it. Newton's method diverges from a poor starting point, stalls at
|
|
54
|
+
inflection points, and needs a derivative. Faster methods belong here too, but
|
|
55
|
+
as explicit choices, not as the default.
|
|
56
|
+
|
|
57
|
+
## Usage
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
import { bisect, trapezoidalRule } from "@ac-kit/math-analysis";
|
|
61
|
+
|
|
62
|
+
bisect((x) => x * x - 2, 0, 2); // ≈ 1.4142135623…
|
|
63
|
+
trapezoidalRule(Math.sin, 0, Math.PI); // ≈ 2
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
See the
|
|
67
|
+
[generated API docs](https://anthochamp.github.io/node-essentials/api/math-analysis/)
|
|
68
|
+
for the full reference.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The factor `xᵃ(1 − x)ᵇ / B(a, b)` scaling the continued fraction.
|
|
3
|
+
*
|
|
4
|
+
* Substituting the Lanczos form of every gamma in `B(a, b)` regroups the
|
|
5
|
+
* exponent into `a·ln(x·t_c/t_a) + b·ln(y·t_c/t_b)`, whose two bases sit near 1
|
|
6
|
+
* wherever `x` is near `a/(a + b)` — the region the fraction is used in. The
|
|
7
|
+
* literal `a·ln x + b·ln y − ln B(a, b)` instead builds three terms of order 45
|
|
8
|
+
* at `a = 30, b = 40` and cancels them down to 2, spending the digits that
|
|
9
|
+
* cancellation costs.
|
|
10
|
+
*
|
|
11
|
+
* The two pieces are exponentiated separately and multiplied, never added, so
|
|
12
|
+
* the sum's last bit never becomes the answer's error.
|
|
13
|
+
*
|
|
14
|
+
* @param a First shape parameter, positive.
|
|
15
|
+
* @param b Second shape parameter, positive.
|
|
16
|
+
* @param x Point of evaluation, in `(0, 1)`.
|
|
17
|
+
* @param y `1 − x`, passed in because the caller already holds an accurate one.
|
|
18
|
+
* @returns `xᵃ(1 − x)ᵇ / B(a, b)`.
|
|
19
|
+
*/
|
|
20
|
+
export declare function betaPowerTerms_(a: number, b: number, x: number, y: number): number;
|
|
21
|
+
//# sourceMappingURL=_beta-power-terms.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_beta-power-terms.d.ts","sourceRoot":"","sources":["../src/_beta-power-terms.ts"],"names":[],"mappings":"AAKA;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,eAAe,CAC9B,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,GACP,MAAM,CAwBR"}
|
|
@@ -0,0 +1,43 @@
|
|
|
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
|
+
/**
|
|
5
|
+
* The factor `xᵃ(1 − x)ᵇ / B(a, b)` scaling the continued fraction.
|
|
6
|
+
*
|
|
7
|
+
* Substituting the Lanczos form of every gamma in `B(a, b)` regroups the
|
|
8
|
+
* exponent into `a·ln(x·t_c/t_a) + b·ln(y·t_c/t_b)`, whose two bases sit near 1
|
|
9
|
+
* wherever `x` is near `a/(a + b)` — the region the fraction is used in. The
|
|
10
|
+
* literal `a·ln x + b·ln y − ln B(a, b)` instead builds three terms of order 45
|
|
11
|
+
* at `a = 30, b = 40` and cancels them down to 2, spending the digits that
|
|
12
|
+
* cancellation costs.
|
|
13
|
+
*
|
|
14
|
+
* The two pieces are exponentiated separately and multiplied, never added, so
|
|
15
|
+
* the sum's last bit never becomes the answer's error.
|
|
16
|
+
*
|
|
17
|
+
* @param a First shape parameter, positive.
|
|
18
|
+
* @param b Second shape parameter, positive.
|
|
19
|
+
* @param x Point of evaluation, in `(0, 1)`.
|
|
20
|
+
* @param y `1 − x`, passed in because the caller already holds an accurate one.
|
|
21
|
+
* @returns `xᵃ(1 − x)ᵇ / B(a, b)`.
|
|
22
|
+
*/
|
|
23
|
+
export function betaPowerTerms_(a, b, x, y) {
|
|
24
|
+
const shiftedA = a + LANCZOS_G_ - 0.5;
|
|
25
|
+
const shiftedB = b + LANCZOS_G_ - 0.5;
|
|
26
|
+
const shiftedSum = a + b + LANCZOS_G_ - 0.5;
|
|
27
|
+
const lead = (Math.sqrt((shiftedA * shiftedB) / shiftedSum) *
|
|
28
|
+
Math.exp(LANCZOS_G_ - 0.5) *
|
|
29
|
+
lanczosSeries_(a + b - 1)) /
|
|
30
|
+
(SQRT_TWO_PI * lanczosSeries_(a - 1) * lanczosSeries_(b - 1));
|
|
31
|
+
// `x·t_c/tₐ − 1` and `y·t_c/t_b − 1`, formed without the subtraction of
|
|
32
|
+
// near-equal numbers that the ratios themselves would need. Where a ratio
|
|
33
|
+
// falls well below 1 the delta sits next to −1, and `log1p` would have to
|
|
34
|
+
// rebuild the ratio from it; the ratio is taken directly there instead.
|
|
35
|
+
const ratioA = (x * shiftedSum) / shiftedA;
|
|
36
|
+
const ratioB = (y * shiftedSum) / shiftedB;
|
|
37
|
+
const deltaA = (x * b - y * shiftedA) / shiftedA;
|
|
38
|
+
const deltaB = (y * a - x * shiftedB) / shiftedB;
|
|
39
|
+
const first = a * (ratioA > 0.5 ? Math.log1p(deltaA) : Math.log(ratioA));
|
|
40
|
+
const second = b * (ratioB > 0.5 ? Math.log1p(deltaB) : Math.log(ratioB));
|
|
41
|
+
return lead * expSplit_(first, second);
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=_beta-power-terms.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_beta-power-terms.js","sourceRoot":"","sources":["../src/_beta-power-terms.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAElD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB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CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC;IACzE,MAAM,MAAM,GAAG,CAAC,GAAG,CAAC,MAAM,GAAG,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC;IAE1E,OAAO,IAAI,GAAG,SAAS,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;AACxC,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `e^(first + second)`, computed without ever forming the sum.
|
|
3
|
+
*
|
|
4
|
+
* The sum is the problem: an exponent of magnitude `E` can only be held to
|
|
5
|
+
* `E·ε` absolutely in binary64, and `exp` turns that straight into `E·ε` of
|
|
6
|
+
* relative error in the result — `7e-14` by `E = 700`. Exponentiating the two
|
|
7
|
+
* pieces separately and multiplying costs one ulp each instead, regardless of
|
|
8
|
+
* how large they are.
|
|
9
|
+
*
|
|
10
|
+
* When a piece alone would leave `exp`'s usable range, both are halved until
|
|
11
|
+
* they fit and the result is squared back up. Halving is exact, and each
|
|
12
|
+
* squaring adds one ulp, so the cost is `log₂` of the overshoot rather than the
|
|
13
|
+
* full `E·ε`.
|
|
14
|
+
*
|
|
15
|
+
* @param first One addend of the exponent.
|
|
16
|
+
* @param second The other addend.
|
|
17
|
+
* @returns `e^(first + second)`.
|
|
18
|
+
*/
|
|
19
|
+
export declare function expSplit_(first: number, second: number): number;
|
|
20
|
+
//# sourceMappingURL=_exp-split.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_exp-split.d.ts","sourceRoot":"","sources":["../src/_exp-split.ts"],"names":[],"mappings":"AAUA;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAoB/D"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Largest magnitude to hand `Math.exp` directly. Below binary64's `-745`
|
|
3
|
+
* underflow edge with room to spare, so no intermediate lands in the subnormals
|
|
4
|
+
* where it would shed bits.
|
|
5
|
+
*/
|
|
6
|
+
const SAFE_EXPONENT_ = 700;
|
|
7
|
+
/** Beyond this the result has saturated whatever the split, so stop splitting. */
|
|
8
|
+
const MAX_SCALE_ = 1024;
|
|
9
|
+
/**
|
|
10
|
+
* `e^(first + second)`, computed without ever forming the sum.
|
|
11
|
+
*
|
|
12
|
+
* The sum is the problem: an exponent of magnitude `E` can only be held to
|
|
13
|
+
* `E·ε` absolutely in binary64, and `exp` turns that straight into `E·ε` of
|
|
14
|
+
* relative error in the result — `7e-14` by `E = 700`. Exponentiating the two
|
|
15
|
+
* pieces separately and multiplying costs one ulp each instead, regardless of
|
|
16
|
+
* how large they are.
|
|
17
|
+
*
|
|
18
|
+
* When a piece alone would leave `exp`'s usable range, both are halved until
|
|
19
|
+
* they fit and the result is squared back up. Halving is exact, and each
|
|
20
|
+
* squaring adds one ulp, so the cost is `log₂` of the overshoot rather than the
|
|
21
|
+
* full `E·ε`.
|
|
22
|
+
*
|
|
23
|
+
* @param first One addend of the exponent.
|
|
24
|
+
* @param second The other addend.
|
|
25
|
+
* @returns `e^(first + second)`.
|
|
26
|
+
*/
|
|
27
|
+
export function expSplit_(first, second) {
|
|
28
|
+
const magnitude = Math.max(Math.abs(first), Math.abs(second));
|
|
29
|
+
if (!Number.isFinite(magnitude) || magnitude >= SAFE_EXPONENT_ * MAX_SCALE_) {
|
|
30
|
+
return Math.exp(first + second);
|
|
31
|
+
}
|
|
32
|
+
let scale = 1;
|
|
33
|
+
while (magnitude / scale >= SAFE_EXPONENT_) {
|
|
34
|
+
scale *= 2;
|
|
35
|
+
}
|
|
36
|
+
// Dividing by a power of two is exact, so the split introduces no error of
|
|
37
|
+
// its own.
|
|
38
|
+
let value = Math.exp(first / scale) * Math.exp(second / scale);
|
|
39
|
+
for (let remaining = scale; remaining > 1; remaining >>= 1) {
|
|
40
|
+
value *= value;
|
|
41
|
+
}
|
|
42
|
+
return value;
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=_exp-split.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_exp-split.js","sourceRoot":"","sources":["../src/_exp-split.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,cAAc,GAAG,GAAG,CAAC;AAE3B,kFAAkF;AAClF,MAAM,UAAU,GAAG,IAAI,CAAC;AAExB;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,SAAS,CAAC,KAAa,EAAE,MAAc;IACtD,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC;IAE9D,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,SAAS,CAAC,IAAI,SAAS,IAAI,cAAc,GAAG,UAAU,EAAE,CAAC;QAC7E,OAAO,IAAI,CAAC,GAAG,CAAC,KAAK,GAAG,MAAM,CAAC,CAAC;IACjC,CAAC;IAED,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,OAAO,SAAS,GAAG,KAAK,IAAI,cAAc,EAAE,CAAC;QAC5C,KAAK,IAAI,CAAC,CAAC;IACZ,CAAC;IAED,2EAA2E;IAC3E,WAAW;IACX,IAAI,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,GAAG,KAAK,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,GAAG,KAAK,CAAC,CAAC;IAC/D,KAAK,IAAI,SAAS,GAAG,KAAK,EAAE,SAAS,GAAG,CAAC,EAAE,SAAS,KAAK,CAAC,EAAE,CAAC;QAC5D,KAAK,IAAI,KAAK,CAAC;IAChB,CAAC;IAED,OAAO,KAAK,CAAC;AACd,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ConvergenceOptions } from "./convergence-options.js";
|
|
2
|
+
import type { ConvergenceResult } from "./convergence-result.js";
|
|
3
|
+
/**
|
|
4
|
+
* Rejects shape parameters the beta function is not defined for.
|
|
5
|
+
*
|
|
6
|
+
* @param routine Name of the calling export, for the message.
|
|
7
|
+
* @param a First shape parameter, which must be positive.
|
|
8
|
+
* @param b Second shape parameter, which must be positive.
|
|
9
|
+
* @throws {RangeError} When either parameter is non-positive.
|
|
10
|
+
*/
|
|
11
|
+
export declare function assertBetaShape_(routine: string, a: number, b: number): void;
|
|
12
|
+
/**
|
|
13
|
+
* The continued fraction `K` behind the regularized incomplete beta, in the
|
|
14
|
+
* form `Iₓ(a, b) = xᵃ(1−x)ᵇ / (a · B(a, b) · K)`.
|
|
15
|
+
*
|
|
16
|
+
* Converges quickly only for `x < (a + 1) / (a + b + 2)`; the caller reaches
|
|
17
|
+
* the other half of the domain through the symmetry `Iₓ(a, b) = 1 − I₁₋ₓ(b, a)`
|
|
18
|
+
* rather than by pushing this past its useful range.
|
|
19
|
+
*
|
|
20
|
+
* @param a First shape parameter, positive.
|
|
21
|
+
* @param b Second shape parameter, positive.
|
|
22
|
+
* @param x Point of evaluation, in `(0, 1)`.
|
|
23
|
+
* @param options Termination criteria.
|
|
24
|
+
* @returns `K`, the term count and whether the tolerance was met.
|
|
25
|
+
*/
|
|
26
|
+
export declare function betaContinuedFraction_(a: number, b: number, x: number, options?: ConvergenceOptions): ConvergenceResult<number>;
|
|
27
|
+
//# sourceMappingURL=_incomplete-beta.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_incomplete-beta.d.ts","sourceRoot":"","sources":["../src/_incomplete-beta.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AACnE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAUjE;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,IAAI,CAO5E;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,sBAAsB,CACrC,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,EACT,OAAO,CAAC,EAAE,kBAAkB,GAC1B,iBAAiB,CAAC,MAAM,CAAC,CAmB3B"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { continuedFractionEval } from "./continued-fraction-eval.js";
|
|
2
|
+
/**
|
|
3
|
+
* Scales with the shapes for the same reason the incomplete gamma's does: the
|
|
4
|
+
* fraction's convergence slows as the parameters grow.
|
|
5
|
+
*/
|
|
6
|
+
function maxIterations_(a, b) {
|
|
7
|
+
return Math.max(1000, Math.ceil(20 * Math.sqrt(a + b)));
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Rejects shape parameters the beta function is not defined for.
|
|
11
|
+
*
|
|
12
|
+
* @param routine Name of the calling export, for the message.
|
|
13
|
+
* @param a First shape parameter, which must be positive.
|
|
14
|
+
* @param b Second shape parameter, which must be positive.
|
|
15
|
+
* @throws {RangeError} When either parameter is non-positive.
|
|
16
|
+
*/
|
|
17
|
+
export function assertBetaShape_(routine, a, b) {
|
|
18
|
+
if (!(a > 0)) {
|
|
19
|
+
throw new RangeError(`${routine}: a must be positive, got ${a}`);
|
|
20
|
+
}
|
|
21
|
+
if (!(b > 0)) {
|
|
22
|
+
throw new RangeError(`${routine}: b must be positive, got ${b}`);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* The continued fraction `K` behind the regularized incomplete beta, in the
|
|
27
|
+
* form `Iₓ(a, b) = xᵃ(1−x)ᵇ / (a · B(a, b) · K)`.
|
|
28
|
+
*
|
|
29
|
+
* Converges quickly only for `x < (a + 1) / (a + b + 2)`; the caller reaches
|
|
30
|
+
* the other half of the domain through the symmetry `Iₓ(a, b) = 1 − I₁₋ₓ(b, a)`
|
|
31
|
+
* rather than by pushing this past its useful range.
|
|
32
|
+
*
|
|
33
|
+
* @param a First shape parameter, positive.
|
|
34
|
+
* @param b Second shape parameter, positive.
|
|
35
|
+
* @param x Point of evaluation, in `(0, 1)`.
|
|
36
|
+
* @param options Termination criteria.
|
|
37
|
+
* @returns `K`, the term count and whether the tolerance was met.
|
|
38
|
+
*/
|
|
39
|
+
export function betaContinuedFraction_(a, b, x, options) {
|
|
40
|
+
return continuedFractionEval({
|
|
41
|
+
numerator: (index) => {
|
|
42
|
+
const step = Math.floor(index / 2);
|
|
43
|
+
return index % 2 === 0
|
|
44
|
+
? (step * (b - step) * x) / ((a + 2 * step - 1) * (a + 2 * step))
|
|
45
|
+
: (-(a + step) * (a + b + step) * x) /
|
|
46
|
+
((a + 2 * step) * (a + 2 * step + 1));
|
|
47
|
+
},
|
|
48
|
+
denominator: () => 1,
|
|
49
|
+
}, {
|
|
50
|
+
tolerance: options?.tolerance ?? Number.EPSILON,
|
|
51
|
+
maxIterations: options?.maxIterations ?? maxIterations_(a, b),
|
|
52
|
+
signal: options?.signal,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=_incomplete-beta.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_incomplete-beta.js","sourceRoot":"","sources":["../src/_incomplete-beta.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AAIrE;;;GAGG;AACH,SAAS,cAAc,CAAC,CAAS,EAAE,CAAS;IAC3C,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,EAAE,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;AACzD,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,gBAAgB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AO,EAAE,aAAa,IAAI,cAAc,CAAC,CAAC,EAAE,CAAC,CAAC;QAC7D,MAAM,EAAE,OAAO,EAAE,MAAM;KACvB,CACD,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { ConvergenceOptions } from "./convergence-options.js";
|
|
2
|
+
import type { ConvergenceResult } from "./convergence-result.js";
|
|
3
|
+
/**
|
|
4
|
+
* Whether the continued fraction, rather than the series, is the expansion to
|
|
5
|
+
* run at `(a, x)`.
|
|
6
|
+
*
|
|
7
|
+
* Not the textbook `x ≥ a + 1`. That rule picks the expansion by convergence
|
|
8
|
+
* speed alone and ignores which of `P` and `Q` the caller is after: at `a =
|
|
9
|
+
* 0.05, x = 1` it routes to the series, leaving the upper form to reach a tail
|
|
10
|
+
* of `0.003` through `1 − 0.997` and throwing away two and a half bits.
|
|
11
|
+
* Measured, that cost 327 ulps where this rule costs a handful.
|
|
12
|
+
*
|
|
13
|
+
* `x ≥ 1 ∧ x ≥ a` is Cephes' condition, and it splits the domain by which tail
|
|
14
|
+
* is small: below it `x` sits left of the mode, so `P` is the small one and `Q`
|
|
15
|
+
* is safely `1 − P`; above it the roles swap and the fraction computes the
|
|
16
|
+
* small tail directly.
|
|
17
|
+
*/
|
|
18
|
+
export declare function preferContinuedFraction_(a: number, x: number): boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Rejects the arguments neither expansion is defined for, naming the public
|
|
21
|
+
* routine rather than this module.
|
|
22
|
+
*
|
|
23
|
+
* @param routine Name of the calling export, for the message.
|
|
24
|
+
* @param a Shape parameter, which must be positive.
|
|
25
|
+
* @param x Integration limit, which must be non-negative.
|
|
26
|
+
* @throws {RangeError} When either argument is outside its domain.
|
|
27
|
+
*/
|
|
28
|
+
export declare function assertIncompleteGammaDomain_(routine: string, a: number, x: number): void;
|
|
29
|
+
/**
|
|
30
|
+
* The factor `e^(−x) · xᵃ / Γ(a)` both expansions are scaled by, and the
|
|
31
|
+
* density `Γ(a, x)` differentiates to once divided by `x`.
|
|
32
|
+
*
|
|
33
|
+
* Not computed as `exp(a·ln x − ln Γ(a))`. That form is accurate to one ulp
|
|
34
|
+
* _relatively_ in each log, but `ln Γ(100)` is 359, so one ulp of it is `4e-14`
|
|
35
|
+
* _absolutely_ — and `exp` turns an absolute error in its argument into a
|
|
36
|
+
* relative error in its result. Measured, that put `P(100, 90)` `5e-14` out
|
|
37
|
+
* with everything else exact.
|
|
38
|
+
*
|
|
39
|
+
* Substituting the Lanczos form of `Γ(a)` gives the exponent as a sum of two
|
|
40
|
+
* pieces, in whichever of two algebraically identical splits keeps both small:
|
|
41
|
+
* near `x ≈ a` that is `a·log1pmx(d) − (g − ½)·d`, and far from it `a·ln(1 + d)
|
|
42
|
+
* \+ (agh − x)`, where `agh = a + g − ½` and `d = (x − agh)/agh`.
|
|
43
|
+
*
|
|
44
|
+
* The two pieces are then exponentiated **separately and multiplied**, never
|
|
45
|
+
* added: their sum is a number of order `ln Γ(a)` whose own last bit is the
|
|
46
|
+
* error this exists to avoid. Adding them is the fallback only when one piece
|
|
47
|
+
* alone would overflow `exp`, which is where the result saturates anyway.
|
|
48
|
+
*/
|
|
49
|
+
export declare function incompleteGammaScale_(a: number, x: number): number;
|
|
50
|
+
/**
|
|
51
|
+
* The lower regularized incomplete gamma `P(a, x)` by its power series.
|
|
52
|
+
*
|
|
53
|
+
* Every term is positive, so there is no cancellation to compensate for and a
|
|
54
|
+
* naive running total is already exact to within the terms' own rounding.
|
|
55
|
+
* Measured against a 60-digit reference, the sum is exact to the last bit and
|
|
56
|
+
* Neumaier compensation changes nothing: the error that used to show up here
|
|
57
|
+
* was {@link incompleteGammaScale_}'s, not this loop's.
|
|
58
|
+
*
|
|
59
|
+
* Converges in O(√a) terms for `x < a + 1`, and unusably slowly above that —
|
|
60
|
+
* use {@link gammaUpperContinuedFraction_} there.
|
|
61
|
+
*
|
|
62
|
+
* @param a Shape parameter, positive.
|
|
63
|
+
* @param x Upper limit of integration, positive.
|
|
64
|
+
* @param options Termination criteria.
|
|
65
|
+
* @returns `P(a, x)`, the term count and whether the tolerance was met.
|
|
66
|
+
*/
|
|
67
|
+
export declare function gammaLowerSeries_(a: number, x: number, options?: ConvergenceOptions): ConvergenceResult<number>;
|
|
68
|
+
/**
|
|
69
|
+
* The upper regularized incomplete gamma `Q(a, x)` by Legendre's continued
|
|
70
|
+
* fraction, evaluated with {@link continuedFractionEval}.
|
|
71
|
+
*
|
|
72
|
+
* Converges quickly for `x ≥ a + 1`, which is exactly where
|
|
73
|
+
* {@link gammaLowerSeries_} stops being usable.
|
|
74
|
+
*
|
|
75
|
+
* @param a Shape parameter, positive.
|
|
76
|
+
* @param x Lower limit of integration, positive.
|
|
77
|
+
* @param options Termination criteria.
|
|
78
|
+
* @returns `Q(a, x)`, the term count and whether the tolerance was met.
|
|
79
|
+
*/
|
|
80
|
+
export declare function gammaUpperContinuedFraction_(a: number, x: number, options?: ConvergenceOptions): ConvergenceResult<number>;
|
|
81
|
+
//# sourceMappingURL=_incomplete-gamma.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_incomplete-gamma.d.ts","sourceRoot":"","sources":["../src/_incomplete-gamma.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AACnE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAejE;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,wBAAwB,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,OAAO,CAEtE;AAED;;;;;;;;GAQG;AACH,wBAAgB,4BAA4B,CAC3C,OAAO,EAAE,MAAM,EACf,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,GACP,IAAI,CAON;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,qBAAqB,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAgBlE;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,iBAAiB,CAChC,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,EACT,OAAO,CAAC,EAAE,kBAAkB,GAC1B,iBAAiB,CAAC,MAAM,CAAC,CA8B3B;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,4BAA4B,CAC3C,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,EACT,OAAO,CAAC,EAAE,kBAAkB,GAC1B,iBAAiB,CAAC,MAAM,CAAC,CAkB3B"}
|
|
@@ -0,0 +1,155 @@
|
|
|
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 { log1pmx_ } from "./_log1pmx.js";
|
|
5
|
+
import { continuedFractionEval } from "./continued-fraction-eval.js";
|
|
6
|
+
/**
|
|
7
|
+
* Default iteration cap, which has to scale with the shape parameter.
|
|
8
|
+
*
|
|
9
|
+
* The series' successive terms shrink by `x / (a + n)`, so the count needed to
|
|
10
|
+
* reach `ε` grows like `√(72a)` — about 1900 at `a = 50000`, where a flat cap
|
|
11
|
+
* of a thousand reported a false non-convergence. Twenty times `√a` clears that
|
|
12
|
+
* with margin and costs nothing at small `a`, where the loop exits on tolerance
|
|
13
|
+
* long before.
|
|
14
|
+
*/
|
|
15
|
+
function maxIterations_(a) {
|
|
16
|
+
return Math.max(1000, Math.ceil(20 * Math.sqrt(a)));
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Whether the continued fraction, rather than the series, is the expansion to
|
|
20
|
+
* run at `(a, x)`.
|
|
21
|
+
*
|
|
22
|
+
* Not the textbook `x ≥ a + 1`. That rule picks the expansion by convergence
|
|
23
|
+
* speed alone and ignores which of `P` and `Q` the caller is after: at `a =
|
|
24
|
+
* 0.05, x = 1` it routes to the series, leaving the upper form to reach a tail
|
|
25
|
+
* of `0.003` through `1 − 0.997` and throwing away two and a half bits.
|
|
26
|
+
* Measured, that cost 327 ulps where this rule costs a handful.
|
|
27
|
+
*
|
|
28
|
+
* `x ≥ 1 ∧ x ≥ a` is Cephes' condition, and it splits the domain by which tail
|
|
29
|
+
* is small: below it `x` sits left of the mode, so `P` is the small one and `Q`
|
|
30
|
+
* is safely `1 − P`; above it the roles swap and the fraction computes the
|
|
31
|
+
* small tail directly.
|
|
32
|
+
*/
|
|
33
|
+
export function preferContinuedFraction_(a, x) {
|
|
34
|
+
return x >= 1 && x >= a;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Rejects the arguments neither expansion is defined for, naming the public
|
|
38
|
+
* routine rather than this module.
|
|
39
|
+
*
|
|
40
|
+
* @param routine Name of the calling export, for the message.
|
|
41
|
+
* @param a Shape parameter, which must be positive.
|
|
42
|
+
* @param x Integration limit, which must be non-negative.
|
|
43
|
+
* @throws {RangeError} When either argument is outside its domain.
|
|
44
|
+
*/
|
|
45
|
+
export function assertIncompleteGammaDomain_(routine, a, x) {
|
|
46
|
+
if (!(a > 0)) {
|
|
47
|
+
throw new RangeError(`${routine}: a must be positive, got ${a}`);
|
|
48
|
+
}
|
|
49
|
+
if (!(x >= 0)) {
|
|
50
|
+
throw new RangeError(`${routine}: x must be non-negative, got ${x}`);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* The factor `e^(−x) · xᵃ / Γ(a)` both expansions are scaled by, and the
|
|
55
|
+
* density `Γ(a, x)` differentiates to once divided by `x`.
|
|
56
|
+
*
|
|
57
|
+
* Not computed as `exp(a·ln x − ln Γ(a))`. That form is accurate to one ulp
|
|
58
|
+
* _relatively_ in each log, but `ln Γ(100)` is 359, so one ulp of it is `4e-14`
|
|
59
|
+
* _absolutely_ — and `exp` turns an absolute error in its argument into a
|
|
60
|
+
* relative error in its result. Measured, that put `P(100, 90)` `5e-14` out
|
|
61
|
+
* with everything else exact.
|
|
62
|
+
*
|
|
63
|
+
* Substituting the Lanczos form of `Γ(a)` gives the exponent as a sum of two
|
|
64
|
+
* pieces, in whichever of two algebraically identical splits keeps both small:
|
|
65
|
+
* near `x ≈ a` that is `a·log1pmx(d) − (g − ½)·d`, and far from it `a·ln(1 + d)
|
|
66
|
+
* \+ (agh − x)`, where `agh = a + g − ½` and `d = (x − agh)/agh`.
|
|
67
|
+
*
|
|
68
|
+
* The two pieces are then exponentiated **separately and multiplied**, never
|
|
69
|
+
* added: their sum is a number of order `ln Γ(a)` whose own last bit is the
|
|
70
|
+
* error this exists to avoid. Adding them is the fallback only when one piece
|
|
71
|
+
* alone would overflow `exp`, which is where the result saturates anyway.
|
|
72
|
+
*/
|
|
73
|
+
export function incompleteGammaScale_(a, x) {
|
|
74
|
+
const agh = a + LANCZOS_G_ - 0.5;
|
|
75
|
+
const d = (x - agh) / agh;
|
|
76
|
+
const [first, second] = Math.abs(d) <= 0.5
|
|
77
|
+
? [a * log1pmx_(d), -(LANCZOS_G_ - 0.5) * d]
|
|
78
|
+
: // `log1p(d)` would have to rebuild `x/agh` from a `d` sitting next to
|
|
79
|
+
// −1, losing every digit the ratio has; taking the ratio directly
|
|
80
|
+
// costs nothing here because the two are far apart.
|
|
81
|
+
[a * Math.log(x / agh), agh - x];
|
|
82
|
+
return ((expSplit_(first, second) * Math.sqrt(agh)) /
|
|
83
|
+
(SQRT_TWO_PI * lanczosSeries_(a - 1)));
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* The lower regularized incomplete gamma `P(a, x)` by its power series.
|
|
87
|
+
*
|
|
88
|
+
* Every term is positive, so there is no cancellation to compensate for and a
|
|
89
|
+
* naive running total is already exact to within the terms' own rounding.
|
|
90
|
+
* Measured against a 60-digit reference, the sum is exact to the last bit and
|
|
91
|
+
* Neumaier compensation changes nothing: the error that used to show up here
|
|
92
|
+
* was {@link incompleteGammaScale_}'s, not this loop's.
|
|
93
|
+
*
|
|
94
|
+
* Converges in O(√a) terms for `x < a + 1`, and unusably slowly above that —
|
|
95
|
+
* use {@link gammaUpperContinuedFraction_} there.
|
|
96
|
+
*
|
|
97
|
+
* @param a Shape parameter, positive.
|
|
98
|
+
* @param x Upper limit of integration, positive.
|
|
99
|
+
* @param options Termination criteria.
|
|
100
|
+
* @returns `P(a, x)`, the term count and whether the tolerance was met.
|
|
101
|
+
*/
|
|
102
|
+
export function gammaLowerSeries_(a, x, options) {
|
|
103
|
+
const tolerance = options?.tolerance ?? Number.EPSILON;
|
|
104
|
+
const maxIterations = options?.maxIterations ?? maxIterations_(a);
|
|
105
|
+
const { signal } = options ?? {};
|
|
106
|
+
let term = 1 / a;
|
|
107
|
+
let total = term;
|
|
108
|
+
let denominator = a;
|
|
109
|
+
for (let index = 1; index <= maxIterations; index++) {
|
|
110
|
+
signal?.throwIfAborted();
|
|
111
|
+
denominator += 1;
|
|
112
|
+
term *= x / denominator;
|
|
113
|
+
total += term;
|
|
114
|
+
if (Math.abs(term) < Math.abs(total) * tolerance) {
|
|
115
|
+
return {
|
|
116
|
+
value: total * incompleteGammaScale_(a, x),
|
|
117
|
+
iterations: index,
|
|
118
|
+
converged: true,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return {
|
|
123
|
+
value: total * incompleteGammaScale_(a, x),
|
|
124
|
+
iterations: maxIterations,
|
|
125
|
+
converged: false,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* The upper regularized incomplete gamma `Q(a, x)` by Legendre's continued
|
|
130
|
+
* fraction, evaluated with {@link continuedFractionEval}.
|
|
131
|
+
*
|
|
132
|
+
* Converges quickly for `x ≥ a + 1`, which is exactly where
|
|
133
|
+
* {@link gammaLowerSeries_} stops being usable.
|
|
134
|
+
*
|
|
135
|
+
* @param a Shape parameter, positive.
|
|
136
|
+
* @param x Lower limit of integration, positive.
|
|
137
|
+
* @param options Termination criteria.
|
|
138
|
+
* @returns `Q(a, x)`, the term count and whether the tolerance was met.
|
|
139
|
+
*/
|
|
140
|
+
export function gammaUpperContinuedFraction_(a, x, options) {
|
|
141
|
+
const fraction = continuedFractionEval({
|
|
142
|
+
numerator: (index) => (index === 1 ? 1 : -(index - 1) * (index - 1 - a)),
|
|
143
|
+
denominator: (index) => (index === 0 ? 0 : x + 2 * index - 1 - a),
|
|
144
|
+
}, {
|
|
145
|
+
tolerance: options?.tolerance ?? Number.EPSILON,
|
|
146
|
+
maxIterations: options?.maxIterations ?? maxIterations_(a),
|
|
147
|
+
signal: options?.signal,
|
|
148
|
+
});
|
|
149
|
+
return {
|
|
150
|
+
value: fraction.value * incompleteGammaScale_(a, x),
|
|
151
|
+
iterations: fraction.iterations,
|
|
152
|
+
converged: fraction.converged,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
//# sourceMappingURL=_incomplete-gamma.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_incomplete-gamma.js","sourceRoot":"","sources":["../src/_incomplete-gamma.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,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AAIrE;;;;;;;;GAQG;AACH,SAAS,cAAc,CAAC,CAAS;IAChC,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,EAAE,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACrD,CAAC;AAED;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,wBAAwB,CAAC,CAAS,EAAE,CAAS;IAC5D,OAAO,CAAC,IAAI,CAAC,IAAI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CAAC,CAAC,KAAK,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,GAAG,CAAC,KAAK,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;QACxE,WAAW,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,KAAK,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK,GAAG,CAAC,GAAG,CAAC,CAAC;KACjE,EACD;QACC,SAAS,EAAE,OAAO,EAAE,SAAS,IAAI,MAAM,CAAC,OAAO;QAC/C,aAAa,EAAE,OAAO,EAAE,aAAa,IAAI,cAAc,CAAC,CAAC,CAAC;QAC1D,MAAM,EAAE,OAAO,EAAE,MAAM;KACvB,CACD,CAAC;IAEF,OAAO;QACN,KAAK,EAAE,QAAQ,CAAC,KAAK,GAAG,qBAAqB,CAAC,CAAC,EAAE,CAAC,CAAC;QACnD,UAAU,EAAE,QAAQ,CAAC,UAAU;QAC/B,SAAS,EAAE,QAAQ,CAAC,SAAS;KAC7B,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
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 declare const LANCZOS_G_: number;
|
|
6
|
+
/**
|
|
7
|
+
* The Lanczos series `Aɡ(z)`, the slowly-varying factor both `gamma` and
|
|
8
|
+
* `logGamma` multiply their Stirling-shaped leading term by.
|
|
9
|
+
*
|
|
10
|
+
* The coefficients alternate in sign, which looks like a case for compensated
|
|
11
|
+
* summation. Measured: it is not. Neumaier compensation moves the series by a
|
|
12
|
+
* few ulps and leaves Γ a wash — the error that dominates is the fit's, and no
|
|
13
|
+
* better sum can reach it.
|
|
14
|
+
*
|
|
15
|
+
* O(1) — fifteen divisions, fixed by the coefficient set.
|
|
16
|
+
*
|
|
17
|
+
* @param z The shifted argument `x − 1`.
|
|
18
|
+
* @returns `Aɡ(z)`.
|
|
19
|
+
*/
|
|
20
|
+
export declare function lanczosSeries_(z: number): number;
|
|
21
|
+
//# sourceMappingURL=_lanczos.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_lanczos.d.ts","sourceRoot":"","sources":["../src/_lanczos.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,eAAO,MAAM,UAAU,QAAY,CAAC;AAwBpC;;;;;;;;;;;;;GAaG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAMhD"}
|