@numero-uno-jpn/rstats 0.15.2 → 0.16.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 +280 -0
- package/README.ja.md +40 -33
- package/README.md +36 -28
- package/dist/chunks/chunk-2D2IQWUQ.js +2 -0
- package/dist/chunks/chunk-5UQCRBAG.js +2 -0
- package/dist/chunks/chunk-6CSTWA5E.js +2 -0
- package/dist/chunks/chunk-72Y3HZID.js +2 -0
- package/dist/chunks/chunk-BG2KLEHM.js +2 -0
- package/dist/chunks/chunk-CMEKBNHQ.js +2 -0
- package/dist/chunks/chunk-FLJNSXSW.js +2 -0
- package/dist/chunks/chunk-GM53LWSR.js +2 -0
- package/dist/chunks/chunk-JBCA56ZS.js +3 -0
- package/dist/chunks/chunk-K3RJAD2T.js +2 -0
- package/dist/chunks/chunk-KAZRWIB5.js +2 -0
- package/dist/chunks/chunk-KBVVIMXX.js +3 -0
- package/dist/chunks/chunk-N3SIFCZX.js +2 -0
- package/dist/chunks/chunk-QNNL7AZ3.js +2 -0
- package/dist/chunks/chunk-SC6CV4Z3.js +2 -0
- package/dist/chunks/chunk-SYWOBOTR.js +2 -0
- package/dist/chunks/chunk-URIO5LY5.js +2 -0
- package/dist/chunks/chunk-VTUGG2SV.js +2 -0
- package/dist/chunks/chunk-W26LRYOJ.js +2 -0
- package/dist/chunks/chunk-XHETEYVC.js +2 -0
- package/dist/chunks/{chunk-PWVQVJTP.js → chunk-YRZWONZB.js} +1 -1
- package/dist/chunks/chunk-ZGABZMWX.js +2 -0
- package/dist/data/grouping.js +20 -5
- package/dist/descriptive/basic.js +90 -8
- package/dist/descriptive/cov.js +12 -4
- package/dist/descriptive/dataFrames.d.ts +4 -2
- package/dist/descriptive/dataFrames.js +56 -18
- package/dist/descriptive/factanal.js +16 -7
- package/dist/descriptive/hist.d.ts +8 -5
- package/dist/descriptive/hist.js +71 -31
- package/dist/descriptive/missing.js +3 -1
- package/dist/descriptive/moments.js +72 -19
- package/dist/descriptive/omega.d.ts +1 -1
- package/dist/descriptive/omega.js +68 -9
- package/dist/descriptive/pivot.d.ts +21 -9
- package/dist/descriptive/pivot.js +161 -35
- package/dist/descriptive/quantile.js +2 -1
- package/dist/descriptive/rank.js +4 -1
- package/dist/descriptive/table.d.ts +3 -2
- package/dist/descriptive/table.js +24 -9
- package/dist/descriptive.js +2 -2
- package/dist/distributions/arcsine.js +19 -2
- package/dist/distributions/beta.js +35 -5
- package/dist/distributions/binomial.js +37 -14
- package/dist/distributions/bvnorm.js +11 -2
- package/dist/distributions/cauchy.js +87 -8
- package/dist/distributions/chisq.js +7 -3
- package/dist/distributions/exponential.js +11 -1
- package/dist/distributions/f.js +73 -22
- package/dist/distributions/gamma.js +61 -6
- package/dist/distributions/geometric.js +53 -14
- package/dist/distributions/hypergeometric.d.ts +5 -1
- package/dist/distributions/hypergeometric.js +93 -6
- package/dist/distributions/invgamma.js +6 -1
- package/dist/distributions/kumaraswamy.js +22 -4
- package/dist/distributions/laplace.js +31 -7
- package/dist/distributions/logistic.js +11 -2
- package/dist/distributions/lognormal.js +20 -3
- package/dist/distributions/multinomial.js +54 -11
- package/dist/distributions/nbeta.js +128 -35
- package/dist/distributions/nchisq.js +85 -14
- package/dist/distributions/negbinomial.js +100 -18
- package/dist/distributions/normal.js +35 -5
- package/dist/distributions/nt.d.ts +28 -3
- package/dist/distributions/nt.js +536 -35
- package/dist/distributions/pareto.js +9 -1
- package/dist/distributions/poisson.js +28 -3
- package/dist/distributions/signrank.js +7 -4
- package/dist/distributions/summary.js +450 -60
- package/dist/distributions/t.js +55 -3
- package/dist/distributions/triangular.js +45 -13
- package/dist/distributions/tukey.js +21 -2
- package/dist/distributions/uniform.js +2 -0
- package/dist/distributions/weibull.js +33 -3
- package/dist/distributions/wilcox.d.ts +9 -3
- package/dist/distributions/wilcox.js +155 -26
- package/dist/distributions.js +2 -2
- package/dist/htest/agreement.d.ts +3 -2
- package/dist/htest/agreement.js +76 -14
- package/dist/htest/ansariTest.d.ts +6 -1
- package/dist/htest/ansariTest.js +75 -8
- package/dist/htest/aov.d.ts +1 -1
- package/dist/htest/aov.js +48 -34
- package/dist/htest/binomTest.d.ts +5 -0
- package/dist/htest/binomTest.js +41 -17
- package/dist/htest/brunnerMunzelTest.d.ts +4 -3
- package/dist/htest/brunnerMunzelTest.js +7 -5
- package/dist/htest/chisqTest.d.ts +2 -1
- package/dist/htest/chisqTest.js +130 -43
- package/dist/htest/cor.d.ts +2 -1
- package/dist/htest/cor.js +17 -19
- package/dist/htest/core/ansari.d.ts +9 -0
- package/dist/htest/core/ansari.js +21 -1
- package/dist/htest/core/kolmogorov.d.ts +25 -0
- package/dist/htest/core/kolmogorov.js +123 -0
- package/dist/htest/core/mantelhaen.js +6 -4
- package/dist/htest/core/shapiro.d.ts +1 -1
- package/dist/htest/core/shapiro.js +5 -1
- package/dist/htest/dunnett.js +14 -5
- package/dist/htest/effectSize.d.ts +2 -0
- package/dist/htest/effectSize.js +91 -29
- package/dist/htest/fisherTest.d.ts +2 -2
- package/dist/htest/fisherTest.js +36 -18
- package/dist/htest/groups.d.ts +7 -0
- package/dist/htest/groups.js +21 -4
- package/dist/htest/ksTest.d.ts +3 -1
- package/dist/htest/ksTest.js +23 -2
- package/dist/htest/leveneTest.js +19 -3
- package/dist/htest/mantelhaenTest.js +125 -25
- package/dist/htest/mcnemarTest.js +32 -11
- package/dist/htest/multipleComparisons.d.ts +8 -1
- package/dist/htest/multipleComparisons.js +44 -21
- package/dist/htest/normalityTests.js +44 -10
- package/dist/htest/onewayTest.js +96 -5
- package/dist/htest/pAdjust.d.ts +3 -2
- package/dist/htest/pAdjust.js +38 -6
- package/dist/htest/pairwise.d.ts +1 -1
- package/dist/htest/pairwise.js +17 -15
- package/dist/htest/poissonTest.d.ts +1 -1
- package/dist/htest/poissonTest.js +111 -59
- package/dist/htest/polycor.js +27 -7
- package/dist/htest/power.d.ts +11 -1
- package/dist/htest/power.js +112 -8
- package/dist/htest/propTest.js +21 -4
- package/dist/htest/pwr.js +121 -24
- package/dist/htest/rankTests.js +29 -5
- package/dist/htest/tTest.js +44 -21
- package/dist/htest/trendTests.d.ts +7 -3
- package/dist/htest/trendTests.js +114 -20
- package/dist/htest/types.d.ts +5 -0
- package/dist/htest/types.js +7 -0
- package/dist/htest/varTest.js +45 -14
- package/dist/htest/wilcoxTest.js +114 -21
- package/dist/integrate/quadpack.js +27 -5
- package/dist/internal/covmat.d.ts +5 -0
- package/dist/internal/covmat.js +20 -0
- package/dist/internal/ddexp.d.ts +23 -1
- package/dist/internal/ddexp.js +86 -6
- package/dist/internal/discrete.d.ts +14 -0
- package/dist/internal/discrete.js +78 -5
- package/dist/internal/extremes.d.ts +9 -0
- package/dist/internal/extremes.js +24 -0
- package/dist/internal/fround.js +13 -4
- package/dist/internal/integer.d.ts +9 -0
- package/dist/internal/integer.js +12 -0
- package/dist/internal/invert.d.ts +5 -0
- package/dist/internal/invert.js +25 -7
- package/dist/internal/noncentral.d.ts +7 -0
- package/dist/internal/noncentral.js +9 -0
- package/dist/internal/poissonMixture.js +29 -6
- package/dist/internal/scaledInverse.d.ts +10 -0
- package/dist/internal/scaledInverse.js +22 -0
- package/dist/internal/seq.js +15 -2
- package/dist/internal/summary.d.ts +90 -0
- package/dist/internal/summary.js +458 -39
- package/dist/internal/uniroot.d.ts +7 -0
- package/dist/internal/uniroot.js +28 -3
- package/dist/linalg/basic.d.ts +3 -1
- package/dist/linalg/basic.js +51 -7
- package/dist/linalg/chol.js +4 -1
- package/dist/linalg/dot.d.ts +29 -0
- package/dist/linalg/dot.js +132 -0
- package/dist/linalg/eigen.d.ts +6 -1
- package/dist/linalg/eigen.js +71 -14
- package/dist/linalg/expm.d.ts +3 -2
- package/dist/linalg/expm.js +109 -15
- package/dist/linalg/kappa.js +58 -10
- package/dist/linalg/lu.d.ts +2 -1
- package/dist/linalg/lu.js +35 -5
- package/dist/linalg/matrix.d.ts +14 -0
- package/dist/linalg/matrix.js +24 -0
- package/dist/linalg/ops.js +60 -13
- package/dist/linalg/qr.d.ts +2 -1
- package/dist/linalg/qr.js +91 -23
- package/dist/linalg/schur.js +11 -3
- package/dist/linalg/solve.d.ts +6 -1
- package/dist/linalg/solve.js +147 -26
- package/dist/linalg/svd.d.ts +9 -2
- package/dist/linalg/svd.js +29 -11
- package/dist/linalg.js +2 -2
- package/dist/mixed.js +4 -4
- package/dist/models/anovaSequential.d.ts +16 -1
- package/dist/models/anovaSequential.js +124 -25
- package/dist/models/aovEz.js +140 -30
- package/dist/models/aovStrata.js +55 -22
- package/dist/models/carAnova.d.ts +1 -1
- package/dist/models/carAnova.js +83 -32
- package/dist/models/coxph.js +148 -66
- package/dist/models/estimable.d.ts +15 -0
- package/dist/models/estimable.js +68 -0
- package/dist/models/formula.d.ts +1 -1
- package/dist/models/formula.js +26 -8
- package/dist/models/glm.d.ts +35 -5
- package/dist/models/glm.js +398 -55
- package/dist/models/glmAnova.js +84 -13
- package/dist/models/glmConfint.d.ts +1 -1
- package/dist/models/glmConfint.js +115 -27
- package/dist/models/glmFamily.d.ts +10 -0
- package/dist/models/glmFamily.js +244 -40
- package/dist/models/lm.d.ts +12 -1
- package/dist/models/lm.js +235 -36
- package/dist/models/lmer.d.ts +20 -1
- package/dist/models/lmer.js +109 -27
- package/dist/models/lmerAnova.js +37 -4
- package/dist/models/lmerHypotheses.d.ts +6 -2
- package/dist/models/lmerHypotheses.js +14 -2
- package/dist/models/lmerTest.d.ts +9 -2
- package/dist/models/lmerTest.js +64 -19
- package/dist/models/mauchly.d.ts +4 -1
- package/dist/models/mauchly.js +34 -13
- package/dist/models/mixedStructure.js +6 -0
- package/dist/models/modelFrame.js +8 -2
- package/dist/models/modelMatrix.d.ts +3 -1
- package/dist/models/modelMatrix.js +40 -15
- package/dist/models/scaledTables.d.ts +8 -0
- package/dist/models/scaledTables.js +6 -0
- package/dist/models/survival.d.ts +6 -1
- package/dist/models/survival.js +67 -27
- package/dist/models/tiedTimes.d.ts +2 -0
- package/dist/models/tiedTimes.js +34 -0
- package/dist/numerics/deriv.d.ts +1 -1
- package/dist/numerics/deriv.js +64 -8
- package/dist/numerics/integrate.d.ts +9 -1
- package/dist/numerics/integrate.js +103 -16
- package/dist/numerics/interp.d.ts +8 -2
- package/dist/numerics/interp.js +352 -50
- package/dist/numerics/ode.js +55 -1
- package/dist/numerics.js +2 -2
- package/dist/optim/localmin.js +4 -0
- package/dist/optim/optim.d.ts +3 -1
- package/dist/optim/optim.js +80 -8
- package/dist/random/beta.js +53 -4
- package/dist/random/binomial.d.ts +14 -3
- package/dist/random/binomial.js +59 -24
- package/dist/random/continuous.js +33 -5
- package/dist/random/extra.js +42 -9
- package/dist/random/gamma.d.ts +4 -1
- package/dist/random/gamma.js +36 -8
- package/dist/random/hyper.js +9 -6
- package/dist/random/mt.js +17 -2
- package/dist/random/poisson.js +26 -3
- package/dist/random/r2dtable.d.ts +8 -1
- package/dist/random/r2dtable.js +24 -3
- package/dist/random/rank.d.ts +2 -0
- package/dist/random/rank.js +44 -4
- package/dist/random/sample.d.ts +12 -2
- package/dist/random/sample.js +57 -20
- package/dist/random/streams.js +4 -2
- package/dist/rstats.js +14 -14
- package/dist/special/besselIK.d.ts +12 -0
- package/dist/special/besselIK.js +168 -23
- package/dist/special/besselJY.d.ts +11 -1
- package/dist/special/besselJY.js +127 -19
- package/dist/special/besselSeries.d.ts +27 -0
- package/dist/special/besselSeries.js +45 -2
- package/dist/special/beta.js +11 -1
- package/dist/special/choose.js +17 -8
- package/dist/special/dd.js +122 -18
- package/dist/special/extra.js +14 -2
- package/dist/special/gamma.js +15 -2
- package/dist/special/igam.js +33 -6
- package/dist/special/incbet.js +100 -10
- package/dist/special/incbetLargeA.js +7 -2
- package/dist/special/incbetPrefactor.d.ts +5 -0
- package/dist/special/incbetPrefactor.js +14 -3
- package/dist/special/incbetTemme.d.ts +7 -0
- package/dist/special/incbetTemme.js +45 -12
- package/dist/special/loader.d.ts +1 -1
- package/dist/special/loader.js +179 -33
- package/dist/special/log1pmx.js +4 -0
- package/dist/special.js +2 -2
- package/dist/tests.js +11 -11
- package/llms.txt +12 -6
- package/package.json +1 -1
- package/dist/chunks/chunk-2CUKVVRO.js +0 -2
- package/dist/chunks/chunk-3WQCKGKP.js +0 -2
- package/dist/chunks/chunk-4Z2XDWKV.js +0 -2
- package/dist/chunks/chunk-AJBLCNZ7.js +0 -3
- package/dist/chunks/chunk-D4F7GQAY.js +0 -2
- package/dist/chunks/chunk-EBQMGCQZ.js +0 -2
- package/dist/chunks/chunk-HBMHR5SS.js +0 -2
- package/dist/chunks/chunk-IBPWRQIF.js +0 -2
- package/dist/chunks/chunk-JLAH3X77.js +0 -2
- package/dist/chunks/chunk-LG74QY5U.js +0 -2
- package/dist/chunks/chunk-PEYGUK32.js +0 -2
- package/dist/chunks/chunk-Q4PGYESP.js +0 -2
- package/dist/chunks/chunk-RCAL4VHM.js +0 -3
- package/dist/chunks/chunk-SREPSVTT.js +0 -2
- package/dist/chunks/chunk-UFT662QZ.js +0 -2
- package/dist/chunks/chunk-VWJ74UHC.js +0 -2
- package/dist/chunks/chunk-XNPU54NH.js +0 -2
- package/dist/chunks/chunk-XPLDEK6D.js +0 -2
- package/dist/chunks/chunk-YGT4P7EU.js +0 -2
- package/dist/chunks/chunk-YVREYVI2.js +0 -2
|
@@ -17,6 +17,12 @@ export interface BesselOptions {
|
|
|
17
17
|
* @param options - `exponScaled` (see {@link BesselOptions}).
|
|
18
18
|
* @returns I_nu(x), or `exp(-x) I_nu(x)` when `exponScaled`.
|
|
19
19
|
*
|
|
20
|
+
* For orders above 1000, results far out of the range of doubles are returned at once (`Infinity` or 0, decided by
|
|
21
|
+
* bounds on their log); otherwise the time grows with the order (a recurrence of about `nu` steps): several seconds at
|
|
22
|
+
* `nu = 1e8`. Beyond 2^28 steps, and for `besselI` where its continued fraction does not converge (`x` above about
|
|
23
|
+
* 5e10), Debye's uniform expansion is used; a RangeError is thrown only where its exponent `nu eta - x` is formed from
|
|
24
|
+
* terms above 2^26 and the result is in range (near `x = 0.66 nu`, unscaled).
|
|
25
|
+
*
|
|
20
26
|
* @example
|
|
21
27
|
* ```ts
|
|
22
28
|
* besselI(1, 0); // 1.2660658777520086
|
|
@@ -35,6 +41,12 @@ export declare function besselI(x: number, nu: number, options?: BesselOptions):
|
|
|
35
41
|
* @param options - `exponScaled` (see {@link BesselOptions}).
|
|
36
42
|
* @returns K_nu(x), or `exp(x) K_nu(x)` when `exponScaled`.
|
|
37
43
|
*
|
|
44
|
+
* For orders above 1000, results far out of the range of doubles are returned at once (`Infinity` or 0, decided by
|
|
45
|
+
* bounds on their log); otherwise the time grows with the order (a recurrence of about `nu` steps): several seconds at
|
|
46
|
+
* `nu = 1e8`. Beyond 2^28 steps, and for `besselI` where its continued fraction does not converge (`x` above about
|
|
47
|
+
* 5e10), Debye's uniform expansion is used; a RangeError is thrown only where its exponent `nu eta - x` is formed from
|
|
48
|
+
* terms above 2^26 and the result is in range (near `x = 0.66 nu`, unscaled).
|
|
49
|
+
*
|
|
38
50
|
* @example
|
|
39
51
|
* ```ts
|
|
40
52
|
* besselK(1, 0); // 0.4210244382407083
|
package/dist/special/besselIK.js
CHANGED
|
@@ -10,9 +10,11 @@
|
|
|
10
10
|
// the end. The forward recurrence for K keeps a separate binary exponent, so neither overflows on the way.
|
|
11
11
|
// - Negative orders follow R's documentation: I_-v = I_v + (2/pi) sin(pi v) K_v (A&S 9.6.2) and K_-v = K_v.
|
|
12
12
|
import { lgamma1p, sinpi } from "./gamma.js";
|
|
13
|
-
import { seriesTimesPrefix } from "./besselSeries.js";
|
|
13
|
+
import { certainRange, LARGE_ORDER, logHalf, logISeriesBounds, seriesTimesPrefix, } from "./besselSeries.js";
|
|
14
14
|
const EPS = Number.EPSILON;
|
|
15
15
|
const MAX_ITER = 1_000_000;
|
|
16
|
+
/** Below this x, x^2/4 < 2^-2000: the power series are exact after their first term */
|
|
17
|
+
const TINY_X = 2 ** -1000;
|
|
16
18
|
/** Gamma(1 + v) - 1 for |v| <= 1/2 */
|
|
17
19
|
function tgamma1pm1(v) {
|
|
18
20
|
return Math.expm1(lgamma1p(v));
|
|
@@ -24,7 +26,7 @@ function tgamma1pm1(v) {
|
|
|
24
26
|
function temmeIK(u, x) {
|
|
25
27
|
const gp = tgamma1pm1(u);
|
|
26
28
|
const gm = tgamma1pm1(-u);
|
|
27
|
-
const a =
|
|
29
|
+
const a = logHalf(x);
|
|
28
30
|
const b = Math.exp(u * a);
|
|
29
31
|
const sigma = -a * u;
|
|
30
32
|
const c = Math.abs(u) < EPS ? 1 : sinpi(u) / (u * Math.PI);
|
|
@@ -51,8 +53,14 @@ function temmeIK(u, x) {
|
|
|
51
53
|
}
|
|
52
54
|
return [sum, (2 * sum1) / x];
|
|
53
55
|
}
|
|
54
|
-
/**
|
|
56
|
+
/**
|
|
57
|
+
* fv = I_(v+1) / I_v by continued fraction (A&S 9.1.73, modified Lentz), and whether it converged within MAX_ITER
|
|
58
|
+
* terms (for x above about 5e10 it does not, #492)
|
|
59
|
+
*/
|
|
55
60
|
function cf1IK(v, x) {
|
|
61
|
+
// b_1 = Infinity would make the Lentz steps Infinity * 0 = NaN; the rest of the fraction is below (1/b_1)^2
|
|
62
|
+
if (!Number.isFinite((2 * (v + 1)) / x))
|
|
63
|
+
return [x / 2 / (v + 1), true];
|
|
56
64
|
const tiny = Math.sqrt(Number.MIN_VALUE * 2 ** 52); // sqrt(DBL_MIN)
|
|
57
65
|
let C = tiny;
|
|
58
66
|
let f = tiny;
|
|
@@ -69,9 +77,9 @@ function cf1IK(v, x) {
|
|
|
69
77
|
const delta = C * D;
|
|
70
78
|
f *= delta;
|
|
71
79
|
if (Math.abs(delta - 1) <= 2 * EPS)
|
|
72
|
-
|
|
80
|
+
return [f, true];
|
|
73
81
|
}
|
|
74
|
-
return f;
|
|
82
|
+
return [f, false];
|
|
75
83
|
}
|
|
76
84
|
/**
|
|
77
85
|
* e^x K_u(x) and e^x K_(u+1)(x) for x > 2 by Steed's continued fraction (Thompson and Barnett, Computer Physics
|
|
@@ -126,18 +134,28 @@ function ldexp(m, e) {
|
|
|
126
134
|
}
|
|
127
135
|
return m * 2 ** e;
|
|
128
136
|
}
|
|
137
|
+
/**
|
|
138
|
+
* Bound on the time of the forward recurrence of kScaled, where the bounds of besselI and besselK do not decide: about
|
|
139
|
+
* 35 ns per step on the development machine
|
|
140
|
+
*/
|
|
141
|
+
const MAX_K_STEPS = 2 ** 28;
|
|
129
142
|
/**
|
|
130
143
|
* e^x K_v(x) as mantissa * 2^exp (v >= 0, x > 0), together with K_(v+1) for the Wronskian.
|
|
131
|
-
* Forward recurrence K_(u+k+1) = 2 (u + k) / x K_(u+k) + K_(u+k-1) from u = v - round(v)
|
|
144
|
+
* Forward recurrence K_(u+k+1) = 2 (u + k) / x K_(u+k) + K_(u+k-1) from u = v - round(v).
|
|
145
|
+
* name, nu: the caller and its order, for the RangeError beyond MAX_K_STEPS
|
|
132
146
|
*/
|
|
133
|
-
function kScaled(v, x) {
|
|
147
|
+
function kScaled(v, x, name, nu) {
|
|
134
148
|
const n = Math.round(v);
|
|
149
|
+
if (n > MAX_K_STEPS) {
|
|
150
|
+
throw new RangeError(`${name}: x = ${x}, nu = ${nu} needs a recurrence of about ${n} steps (at most 2^28)`);
|
|
151
|
+
}
|
|
135
152
|
const u = v - n;
|
|
136
153
|
let prev;
|
|
137
154
|
let current;
|
|
138
155
|
if (u === -0.5) {
|
|
139
|
-
// K_-1/2 = K_1/2 = sqrt(pi / (2x)) e^-x exactly (Temme's series is off by 2e-14 at x = 1e-300)
|
|
140
|
-
|
|
156
|
+
// K_-1/2 = K_1/2 = sqrt(pi / (2x)) e^-x exactly (Temme's series is off by 2e-14 at x = 1e-300); for tiny x without
|
|
157
|
+
// the quotient, which overflows or loses digits when 2x is subnormal
|
|
158
|
+
prev = current = x < TINY_X ? Math.sqrt(Math.PI / 2) / Math.sqrt(x) : Math.sqrt(Math.PI / (2 * x));
|
|
141
159
|
}
|
|
142
160
|
else if (x <= 2) {
|
|
143
161
|
const [ku, ku1] = temmeIK(u, x);
|
|
@@ -179,10 +197,53 @@ function iSmallZ(v, x) {
|
|
|
179
197
|
}
|
|
180
198
|
return seriesTimesPrefix(v, x, sum + term);
|
|
181
199
|
}
|
|
200
|
+
/**
|
|
201
|
+
* Bound on the size of the terms of the exponent in Debye's expansion: formed in double, the exponent has an absolute
|
|
202
|
+
* error of a few eps times this size, and the value as much relative error (1.5e-8 at the bound; as for the phase of
|
|
203
|
+
* besselJ, MAX_ASYM_PHASE)
|
|
204
|
+
*/
|
|
205
|
+
const MAX_DEBYE_EXP = 2 ** 26;
|
|
206
|
+
/**
|
|
207
|
+
* Debye's uniform expansion for large orders (DLMF 10.41.3, 10.41.4, with u1 to u4 of 10.41.10), v > 1000, z = x / v:
|
|
208
|
+
* I_v(x) = e^(v eta) / (sqrt(2 pi v) (1 + z^2)^(1/4)) sum u_k(t) / v^k and K_v(x) = sqrt(pi / (2v)) e^(-v eta) /
|
|
209
|
+
* (1 + z^2)^(1/4) sum (-1)^k u_k(t) / v^k, t = 1 / sqrt(1 + z^2). Used only where the recurrence of K would take more
|
|
210
|
+
* than MAX_K_STEPS steps or the continued fraction of I does not converge (#492); the omitted term u5 / v^5 is at
|
|
211
|
+
* most 2e-17 (abs(u5) <= 0.021 on [0, 1]). The exponent v eta - x = v / (s + z) - v log1p((1 + 1 / (s + z)) / z),
|
|
212
|
+
* s = sqrt(1 + z^2), is formed without forming v eta, which cancels against x (x = 1e308, v = 1e154: v eta - x = -0.5)
|
|
213
|
+
*/
|
|
214
|
+
function debyeIK(v, x, want, what) {
|
|
215
|
+
const z = x / v;
|
|
216
|
+
const s = z > 1 ? z * Math.sqrt(1 + 1 / (z * z)) : Math.sqrt(1 + z * z);
|
|
217
|
+
const t = 1 / s;
|
|
218
|
+
const t2 = t * t;
|
|
219
|
+
const u1 = (t * (3 - 5 * t2)) / 24;
|
|
220
|
+
const u2 = (t2 * (81 + t2 * (-462 + t2 * 385))) / 1152;
|
|
221
|
+
const u3 = (t * t2 * (30375 + t2 * (-369603 + t2 * (765765 - t2 * 425425)))) / 414720;
|
|
222
|
+
const u4 = (t2 * t2 * (4465125 + t2 * (-94121676 + t2 * (349922430 + t2 * (-446185740 + t2 * 185910725))))) / 39813120;
|
|
223
|
+
const w = 1 / v;
|
|
224
|
+
const sgn = want === "I" ? 1 : -1;
|
|
225
|
+
const sum = 1 + w * (sgn * u1 + w * (u2 + w * (sgn * u3 + w * u4)));
|
|
226
|
+
const p = s + z;
|
|
227
|
+
const a = v / p;
|
|
228
|
+
const b = v * Math.log1p((1 + 1 / p) / z);
|
|
229
|
+
// (2 pi v and v s overflow for the largest v and x: the square roots apart)
|
|
230
|
+
const root = Math.sqrt(v) * Math.sqrt(s);
|
|
231
|
+
const size = Math.abs(a) + Math.abs(b);
|
|
232
|
+
if (want === "I")
|
|
233
|
+
return { m: sum / (Math.sqrt(2 * Math.PI) * root), e2: 0, ex: 1, debye: { y: a - b, size, what } };
|
|
234
|
+
return { m: (sum * Math.sqrt(Math.PI / 2)) / root, e2: 0, ex: -1, debye: { y: b - a, size, what } };
|
|
235
|
+
}
|
|
182
236
|
/** m * 2^e2 * e^y, with y split into exact steps of 700 (e^(x/k) would turn the rounding of x/k into an error of x ε) */
|
|
183
237
|
function mulExp(m, e2, y) {
|
|
184
238
|
if (m === 0 || !Number.isFinite(m))
|
|
185
239
|
return m;
|
|
240
|
+
// a result far out of range is 0 or Infinity at once: the steps of 700 below took |y| / 700 turns (besselI(1e12, 0)
|
|
241
|
+
// did not return), and none at all ended once 700 was below the spacing of y (y = -2e308)
|
|
242
|
+
const log2 = Math.log2(Math.abs(m)) + e2 + y * Math.LOG2E;
|
|
243
|
+
if (log2 < -1100)
|
|
244
|
+
return m * 0;
|
|
245
|
+
if (log2 > 1100)
|
|
246
|
+
return m * Infinity;
|
|
186
247
|
let v = m;
|
|
187
248
|
let e = e2;
|
|
188
249
|
const step = y > 0 ? 700 : -700;
|
|
@@ -199,13 +260,31 @@ function mulExp(m, e2, y) {
|
|
|
199
260
|
/** The value e^(want x) * (unscaled value): want = 0 for the unscaled value, -1 for e^-x I, 1 for e^x K */
|
|
200
261
|
function resolve(r, x, want) {
|
|
201
262
|
const ex = r.ex + want;
|
|
263
|
+
if (r.debye) {
|
|
264
|
+
const { y, size, what } = r.debye;
|
|
265
|
+
const value = mulExp(r.m, r.e2, ex * x + y);
|
|
266
|
+
// a result far out of range is 0 or Infinity whatever the error of its exponent
|
|
267
|
+
const total = size + Math.abs(ex) * x;
|
|
268
|
+
if (value !== 0 && Number.isFinite(value) && total > MAX_DEBYE_EXP) {
|
|
269
|
+
throw new RangeError(`${what} has an exponent in Debye's expansion formed from terms of about ${total.toPrecision(3)}, ` +
|
|
270
|
+
`which double precision does not resolve (at most 2^26)`);
|
|
271
|
+
}
|
|
272
|
+
return value;
|
|
273
|
+
}
|
|
202
274
|
return ex === 0 ? ldexp(r.m, r.e2) : mulExp(r.m, r.e2, ex * x);
|
|
203
275
|
}
|
|
204
|
-
/** I_v(x) for v >= 0, x > 0, as I_v = m 2^e2 e^(ex x) */
|
|
205
|
-
function iPos(v, x) {
|
|
206
|
-
if (v === 0.5)
|
|
276
|
+
/** I_v(x) for v >= 0, x > 0, as I_v = m 2^e2 e^(ex x) (name, nu: as for kScaled) */
|
|
277
|
+
function iPos(v, x, name, nu) {
|
|
278
|
+
if (v === 0.5) {
|
|
279
|
+
// e^-x I_1/2 = sqrt(2 / (pi x)) (1 - e^-2x) / 2; for tiny x it is sqrt(2x / pi), formed without 2 / (pi x), which
|
|
280
|
+
// overflows (besselI(1e-310, 0.5) was Infinity)
|
|
281
|
+
if (x < TINY_X)
|
|
282
|
+
return { m: Math.sqrt(2 / Math.PI) * Math.sqrt(x), e2: 0, ex: 1 };
|
|
207
283
|
return { m: Math.sqrt(2 / (x * Math.PI)) * (-Math.expm1(-2 * x) / 2), e2: 0, ex: 1 };
|
|
208
|
-
|
|
284
|
+
}
|
|
285
|
+
// For tiny x the series is exact after one term at any v; the Wronskian below needs K_(v+1), which overflows there
|
|
286
|
+
// (besselI(1e-310, 0) was NaN)
|
|
287
|
+
if (x < TINY_X || (v > 0 && x / v < 0.25))
|
|
209
288
|
return { m: iSmallZ(v, x), e2: 0, ex: 0 };
|
|
210
289
|
let lim = (4 * v * v + 10) / (8 * x);
|
|
211
290
|
lim *= lim;
|
|
@@ -224,15 +303,23 @@ function iPos(v, x) {
|
|
|
224
303
|
num *= mu - 25;
|
|
225
304
|
denom *= ez * 3;
|
|
226
305
|
s -= num / denom;
|
|
227
|
-
|
|
306
|
+
// (2 x pi overflows for x above 2.8e307: besselI(1e308, 0, scaled) was 0; the square roots apart there)
|
|
307
|
+
const r = Number.isFinite(2 * x * Math.PI) ? Math.sqrt(2 * x * Math.PI) : Math.sqrt(2 * Math.PI) * Math.sqrt(x);
|
|
308
|
+
return { m: s / r, e2: 0, ex: 1 };
|
|
228
309
|
}
|
|
310
|
+
// Debye where the recurrence of K would be too long or the continued fraction does not converge (#492: it gave
|
|
311
|
+
// besselI(1e12, 2e5, scaled) 25% low without notice)
|
|
312
|
+
if (Math.round(v) > MAX_K_STEPS)
|
|
313
|
+
return debyeIK(v, x, "I", `${name}: x = ${x}, nu = ${nu}`);
|
|
314
|
+
const [fv, converged] = cf1IK(v, x);
|
|
315
|
+
if (!converged && v > LARGE_ORDER)
|
|
316
|
+
return debyeIK(v, x, "I", `${name}: x = ${x}, nu = ${nu}`);
|
|
229
317
|
// Wronskian I_v K_(v+1) + I_(v+1) K_v = 1/x with fv = I_(v+1) / I_v; K is scaled by e^x, so I comes out scaled by e^-x
|
|
230
|
-
const { k, k1, exp2 } = kScaled(v, x);
|
|
231
|
-
const fv = cf1IK(v, x);
|
|
318
|
+
const { k, k1, exp2 } = kScaled(v, x, name, nu);
|
|
232
319
|
return { m: 1 / (x * (k * fv + k1)), e2: -exp2, ex: 1 };
|
|
233
320
|
}
|
|
234
|
-
/** K_v(x) for v >= 0, x > 0, as K_v = m 2^e2 e^(ex x) */
|
|
235
|
-
function kPos(v, x) {
|
|
321
|
+
/** K_v(x) for v >= 0, x > 0, as K_v = m 2^e2 e^(ex x) (name, nu: as for kScaled) */
|
|
322
|
+
function kPos(v, x, name, nu) {
|
|
236
323
|
// A&S 9.7.2, four terms, where the first omitted term is below eps times the sum (as Boost limits the expansion
|
|
237
324
|
// of I, but measured on the term itself). For v <= 4.5 the remainder is bounded by the first omitted term
|
|
238
325
|
// (DLMF 10.40(ii), l >= v - 1/2); beyond, a small t4 needs x well above v^2, where the terms fall off fast.
|
|
@@ -245,11 +332,31 @@ function kPos(v, x) {
|
|
|
245
332
|
const t4 = (t3 * (mu - 49)) / (4 * ez);
|
|
246
333
|
const s = 1 + t1 + t2 + t3;
|
|
247
334
|
if (Math.abs(t4) < EPS * Math.abs(s)) {
|
|
248
|
-
|
|
335
|
+
// (2 x overflows for x above 9e307: besselK(1e308, 0, scaled) was 0)
|
|
336
|
+
const m = Number.isFinite(2 * x) ? (s * Math.sqrt(Math.PI)) / Math.sqrt(2 * x) : (s * Math.sqrt(Math.PI / 2)) / Math.sqrt(x);
|
|
337
|
+
return { m, e2: 0, ex: -1 };
|
|
249
338
|
}
|
|
250
|
-
|
|
339
|
+
if (Math.round(v) > MAX_K_STEPS)
|
|
340
|
+
return debyeIK(v, x, "K", `${name}: x = ${x}, nu = ${nu}`);
|
|
341
|
+
const { k, exp2 } = kScaled(v, x, name, nu);
|
|
251
342
|
return { m: k, e2: exp2, ex: -1 };
|
|
252
343
|
}
|
|
344
|
+
/**
|
|
345
|
+
* Bounds on log K_v(x) for v >= 1. The Wronskian I_v K_(v+1) + I_(v+1) K_v = 1/x, with I decreasing and K increasing in
|
|
346
|
+
* the order, gives 1 / (2x I_(v-1)(x)) <= K_v(x) <= 1 / (x I_(v+1)(x)), and the series bounds of I go in
|
|
347
|
+
*/
|
|
348
|
+
function logKBounds(v, x) {
|
|
349
|
+
const below = logISeriesBounds(v - 1, x);
|
|
350
|
+
const above = logISeriesBounds(v + 1, x);
|
|
351
|
+
const lx = Math.log(x);
|
|
352
|
+
return { lo: -below.hi - lx - Math.LN2, hi: -above.lo - lx, size: below.size + above.size + Math.abs(lx) + 1 };
|
|
353
|
+
}
|
|
354
|
+
/** The bounds of log(y e^s) */
|
|
355
|
+
function shifted(b, s) {
|
|
356
|
+
return { lo: b.lo + s, hi: b.hi + s, size: b.size + Math.abs(s) };
|
|
357
|
+
}
|
|
358
|
+
/** log(1 - 1/e): what is left of a term after taking off another at most 1/e of its size */
|
|
359
|
+
const LOG_LEFT = Math.log1p(-Math.exp(-1));
|
|
253
360
|
/**
|
|
254
361
|
* Modified Bessel function of the first kind. Equivalent to R's
|
|
255
362
|
* `besselI(x, nu, expon.scaled = FALSE)`. Takes single numbers.
|
|
@@ -260,6 +367,12 @@ function kPos(v, x) {
|
|
|
260
367
|
* @param options - `exponScaled` (see {@link BesselOptions}).
|
|
261
368
|
* @returns I_nu(x), or `exp(-x) I_nu(x)` when `exponScaled`.
|
|
262
369
|
*
|
|
370
|
+
* For orders above 1000, results far out of the range of doubles are returned at once (`Infinity` or 0, decided by
|
|
371
|
+
* bounds on their log); otherwise the time grows with the order (a recurrence of about `nu` steps): several seconds at
|
|
372
|
+
* `nu = 1e8`. Beyond 2^28 steps, and for `besselI` where its continued fraction does not converge (`x` above about
|
|
373
|
+
* 5e10), Debye's uniform expansion is used; a RangeError is thrown only where its exponent `nu eta - x` is formed from
|
|
374
|
+
* terms above 2^26 and the result is in range (near `x = 0.66 nu`, unscaled).
|
|
375
|
+
*
|
|
263
376
|
* @example
|
|
264
377
|
* ```ts
|
|
265
378
|
* besselI(1, 0); // 1.2660658777520086
|
|
@@ -282,11 +395,31 @@ export function besselI(x, nu, options = {}) {
|
|
|
282
395
|
if (x === Infinity)
|
|
283
396
|
return scaled ? 0 : Infinity;
|
|
284
397
|
const want = scaled ? -1 : 0;
|
|
285
|
-
|
|
398
|
+
if (v > LARGE_ORDER) {
|
|
399
|
+
// The result is far out of range (besselI(1e12, 1e12) is Infinity), decided before a recurrence of v steps
|
|
400
|
+
const iB = shifted(logISeriesBounds(v, x), want * x);
|
|
401
|
+
const ri = certainRange(iB);
|
|
402
|
+
if (!negNonInt) {
|
|
403
|
+
if (ri !== 0)
|
|
404
|
+
return ri > 0 ? Infinity : 0;
|
|
405
|
+
}
|
|
406
|
+
else {
|
|
407
|
+
// I_-v = I_v + (2/pi) sin(pi v) K_v: one term decides when it exceeds the other by a factor e
|
|
408
|
+
const s = sinpi(v);
|
|
409
|
+
const kB = shifted(logKBounds(v, x), want * x + Math.log((2 / Math.PI) * Math.abs(s)));
|
|
410
|
+
if (ri === -1 && certainRange(kB) === -1)
|
|
411
|
+
return 0;
|
|
412
|
+
if (kB.lo - 1 > iB.hi && certainRange(shifted(kB, LOG_LEFT)) === 1)
|
|
413
|
+
return s > 0 ? Infinity : -Infinity;
|
|
414
|
+
if (iB.lo - 1 > kB.hi && certainRange(shifted(iB, LOG_LEFT)) === 1)
|
|
415
|
+
return Infinity;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
const i = resolve(iPos(v, x, "besselI", nu), x, want);
|
|
286
419
|
if (!negNonInt)
|
|
287
420
|
return i;
|
|
288
421
|
// A&S 9.6.2: I_-v = I_v + (2/pi) sin(pi v) K_v, each term in the requested scaling
|
|
289
|
-
const k = kPos(v, x);
|
|
422
|
+
const k = kPos(v, x, "besselI", nu);
|
|
290
423
|
return i + resolve({ ...k, m: (2 / Math.PI) * sinpi(v) * k.m }, x, want);
|
|
291
424
|
}
|
|
292
425
|
/**
|
|
@@ -298,6 +431,12 @@ export function besselI(x, nu, options = {}) {
|
|
|
298
431
|
* @param options - `exponScaled` (see {@link BesselOptions}).
|
|
299
432
|
* @returns K_nu(x), or `exp(x) K_nu(x)` when `exponScaled`.
|
|
300
433
|
*
|
|
434
|
+
* For orders above 1000, results far out of the range of doubles are returned at once (`Infinity` or 0, decided by
|
|
435
|
+
* bounds on their log); otherwise the time grows with the order (a recurrence of about `nu` steps): several seconds at
|
|
436
|
+
* `nu = 1e8`. Beyond 2^28 steps, and for `besselI` where its continued fraction does not converge (`x` above about
|
|
437
|
+
* 5e10), Debye's uniform expansion is used; a RangeError is thrown only where its exponent `nu eta - x` is formed from
|
|
438
|
+
* terms above 2^26 and the result is in range (near `x = 0.66 nu`, unscaled).
|
|
439
|
+
*
|
|
301
440
|
* @example
|
|
302
441
|
* ```ts
|
|
303
442
|
* besselK(1, 0); // 0.4210244382407083
|
|
@@ -315,5 +454,11 @@ export function besselK(x, nu, options = {}) {
|
|
|
315
454
|
return Infinity;
|
|
316
455
|
if (x === Infinity)
|
|
317
456
|
return 0;
|
|
318
|
-
|
|
457
|
+
if (v > LARGE_ORDER) {
|
|
458
|
+
// besselK(1, 1e12) is Infinity, decided before a recurrence of v steps
|
|
459
|
+
const r = certainRange(shifted(logKBounds(v, x), scaled ? x : 0));
|
|
460
|
+
if (r !== 0)
|
|
461
|
+
return r > 0 ? Infinity : 0;
|
|
462
|
+
}
|
|
463
|
+
return resolve(kPos(v, x, "besselK", nu), x, scaled ? 1 : 0);
|
|
319
464
|
}
|
|
@@ -7,7 +7,11 @@
|
|
|
7
7
|
* @returns J_nu(x).
|
|
8
8
|
*
|
|
9
9
|
* For `x` much larger than `nu` but below the range of the asymptotic expansion (`x < nu / 0.011`), the time grows
|
|
10
|
-
* with `x` (a continued fraction of about `x` terms): about 15 ms at `x = 5e4`, `nu = 1000
|
|
10
|
+
* with `x` (a continued fraction of about `x - nu` terms): about 15 ms at `x = 5e4`, `nu = 1000`, 1.6 s at `x = 1e7`,
|
|
11
|
+
* `nu = 2e5`. Near the turning point (`x` a little below `nu`) a recurrence takes about `nu` steps. Beyond 2^25 terms
|
|
12
|
+
* of the continued fraction or 2^31 steps of the recurrence (2^28 for a negative non-integer order, which needs Y) a
|
|
13
|
+
* RangeError is thrown before the computation. So it is in the asymptotic expansion (`x` above about `90 nu`) where
|
|
14
|
+
* the phase correction `nu^2 / (2x)` exceeds 2^26, which double precision does not resolve.
|
|
11
15
|
*
|
|
12
16
|
* @example
|
|
13
17
|
* ```ts
|
|
@@ -26,6 +30,12 @@ export declare function besselJ(x: number, nu: number): number;
|
|
|
26
30
|
* (A&S 9.1.2, as in R).
|
|
27
31
|
* @returns Y_nu(x).
|
|
28
32
|
*
|
|
33
|
+
* For orders above 1000, results far out of the range of doubles are returned at once (`Infinity` or 0, decided by
|
|
34
|
+
* bounds on their log); otherwise the time grows with the order (a recurrence of about `nu` steps): about 2.4 s for
|
|
35
|
+
* `besselY(0.9e8, 1e8)`. Beyond 2^28 steps, or 2^25 terms of the continued fraction for `x` above `nu` (as for
|
|
36
|
+
* {@link besselJ}), a RangeError is thrown before the computation, and likewise for a phase correction `nu^2 / (2x)`
|
|
37
|
+
* beyond 2^26 in the asymptotic expansion for large `x`.
|
|
38
|
+
*
|
|
29
39
|
* @example
|
|
30
40
|
* ```ts
|
|
31
41
|
* besselY(1, 0); // 0.08825696421567694
|
package/dist/special/besselJY.js
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
// - Negative orders follow R's documentation (A&S 9.1.2):
|
|
11
11
|
// J_-v = cos(pi v) J_v - sin(pi v) Y_v, Y_-v = sin(pi v) J_v + cos(pi v) Y_v.
|
|
12
12
|
import { gamma, lgamma, lgamma1p, lgammaSign, sinpi } from "./gamma.js";
|
|
13
|
-
import { seriesTimesPrefix } from "./besselSeries.js";
|
|
13
|
+
import { certainRange, LARGE_ORDER, logHalf, logISeriesBounds, powHalf, seriesTimesPrefix } from "./besselSeries.js";
|
|
14
14
|
import { ddDiv, ddMul, ddSub, twoSum } from "./dd.js";
|
|
15
15
|
const EPS = Number.EPSILON;
|
|
16
16
|
/** Multiply by 2^e without overflow in the intermediate power */
|
|
@@ -76,13 +76,13 @@ function jSmallZ(v, x) {
|
|
|
76
76
|
* the reflection formula still multiplies it by a small factor)
|
|
77
77
|
*/
|
|
78
78
|
function ySmallZ(v, x) {
|
|
79
|
-
let p =
|
|
79
|
+
let p = logHalf(x);
|
|
80
80
|
let e2 = 0;
|
|
81
81
|
const needLogs = v >= 170 || 709.78 / v < Math.abs(p);
|
|
82
82
|
let prefix;
|
|
83
83
|
if (!needLogs) {
|
|
84
84
|
let gam = gamma(v);
|
|
85
|
-
p =
|
|
85
|
+
p = powHalf(x, v);
|
|
86
86
|
if (DBL_MAX * p < gam) {
|
|
87
87
|
gam *= 2 ** -1000;
|
|
88
88
|
e2 = 1000;
|
|
@@ -160,8 +160,8 @@ function temmeJY(u, x) {
|
|
|
160
160
|
const gm = tgamma1pm1(-u);
|
|
161
161
|
const spv = sinpi(u);
|
|
162
162
|
const spv2 = sinpi(u / 2);
|
|
163
|
-
const xp =
|
|
164
|
-
const a =
|
|
163
|
+
const xp = powHalf(x, u);
|
|
164
|
+
const a = logHalf(x);
|
|
165
165
|
const sigma = -a * u;
|
|
166
166
|
const d = Math.abs(sigma) < EPS ? 1 : Math.sinh(sigma) / sigma;
|
|
167
167
|
const e = Math.abs(u) < EPS ? (u * Math.PI * Math.PI) / 2 : (2 * spv2 * spv2) / u;
|
|
@@ -226,6 +226,10 @@ function cf1JYDD(v, x) {
|
|
|
226
226
|
function cf1JY(v, x) {
|
|
227
227
|
if (x > 30)
|
|
228
228
|
return cf1JYDD(v, x);
|
|
229
|
+
// b_1 = Infinity (x below 1e-308) made every Lentz step Infinity * 0 = NaN, so the loop ran to its 1e8 limit; the
|
|
230
|
+
// rest of the fraction is below (1/b_1)^2
|
|
231
|
+
if (!Number.isFinite((2 * (v + 1)) / x))
|
|
232
|
+
return { fv: x / 2 / (v + 1), sign: 1 };
|
|
229
233
|
let C = TINY;
|
|
230
234
|
let f = TINY;
|
|
231
235
|
let D = 0;
|
|
@@ -311,7 +315,17 @@ function asymAmplitude(v, x) {
|
|
|
311
315
|
s += (mu - 1) / (2 * txq);
|
|
312
316
|
s += (3 * (mu - 1) * (mu - 9)) / (txq * txq * 8);
|
|
313
317
|
s += (15 * (mu - 1) * (mu - 9) * (mu - 25)) / (txq * txq * txq * 8 * 6);
|
|
314
|
-
|
|
318
|
+
if (!Number.isFinite(s)) {
|
|
319
|
+
// 4 v^2 or (2x)^6 overflows (besselJ(1e200, 1e50) was NaN): the terms as products of
|
|
320
|
+
// (mu - c) / (2x)^2 = r^2 - c / (4 x^2), r = v / x
|
|
321
|
+
const r = v / x;
|
|
322
|
+
const c = (k) => r * r - k / (4 * x * x);
|
|
323
|
+
const c1 = c(1);
|
|
324
|
+
const c9 = c(9);
|
|
325
|
+
s = 1 + c1 / 2 + (3 * c1 * c9) / 8 + (15 * c1 * c9 * c(25)) / 48;
|
|
326
|
+
}
|
|
327
|
+
// pi x overflows for x above 5.7e307 (besselJ(1e308, 0) was -0): the square roots apart
|
|
328
|
+
return Number.isFinite(Math.PI * x) ? Math.sqrt((s * 2) / (Math.PI * x)) : Math.sqrt((s * 2) / Math.PI) / Math.sqrt(x);
|
|
315
329
|
}
|
|
316
330
|
/** Phase of J and Y for large x less (x - pi (v/2 + 1/4)) (A&S 9.2.29) */
|
|
317
331
|
function asymPhaseMx(v, x) {
|
|
@@ -326,30 +340,111 @@ function asymPhaseMx(v, x) {
|
|
|
326
340
|
s += ((mu - 1) * (mu * mu - 114 * mu + 1073)) / (5 * denom);
|
|
327
341
|
denom *= denomMult;
|
|
328
342
|
s += ((mu - 1) * (5 * mu * mu * mu - 1535 * mu * mu + 54703 * mu - 375733)) / (14 * denom);
|
|
343
|
+
if (!Number.isFinite(s)) {
|
|
344
|
+
// The powers overflow: (mu - 1) / (4x) = v r - 1 / (4x) times polynomials in rho = mu / (4x)^2 = r^2 / 4
|
|
345
|
+
const r = v / x;
|
|
346
|
+
const a1 = v * r - 1 / (4 * x);
|
|
347
|
+
const rho = (r * r) / 4;
|
|
348
|
+
const q = 1 / (16 * x * x); // 1 / (4x)^2, 0 where it underflows
|
|
349
|
+
s = a1 / 2;
|
|
350
|
+
s += (a1 * (rho - 25 * q)) / 6;
|
|
351
|
+
s += (a1 * (rho * rho - 114 * rho * q + 1073 * q * q)) / 5;
|
|
352
|
+
s += (a1 * (5 * rho * rho * rho - 1535 * rho * rho * q + 54703 * rho * q * q - 375733 * q * q * q)) / 14;
|
|
353
|
+
}
|
|
329
354
|
return s;
|
|
330
355
|
}
|
|
356
|
+
/**
|
|
357
|
+
* v / 2 + 1/4 modulo 2 for the phase pi (v/2 + 1/4): for v >= 2^52 the 1/4 rounds away in v / 2 + 0.25 (besselJ(1e40,
|
|
358
|
+
* 1e17) was 8.3 relative off), and v (an integer there) is reduced modulo 4 first, exactly. Below, v / 2 + 0.25 as
|
|
359
|
+
* before (the bits of ordinary orders)
|
|
360
|
+
*/
|
|
361
|
+
function quarterPhase(v) {
|
|
362
|
+
return v >= 2 ** 52 ? (v % 4) / 2 + 0.25 : v / 2 + 0.25;
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* Bound on the phase correction of the asymptotic expansion, about nu^2 / (2x): formed in double, its absolute error
|
|
366
|
+
* is a few eps times itself, and the relative error of J and Y about 1e-16 times it (1.3e-9 at 5e7; no digit is left
|
|
367
|
+
* beyond about 1e13)
|
|
368
|
+
*/
|
|
369
|
+
const MAX_ASYM_PHASE = 2 ** 26;
|
|
331
370
|
/** J_v and Y_v for large x (A&S 9.2.19), with the phase factored by addition formulas */
|
|
332
|
-
function asymJY(v, x) {
|
|
371
|
+
function asymJY(v, x, name, nu) {
|
|
372
|
+
const corr = (v * (v / x)) / 2;
|
|
373
|
+
if (corr > MAX_ASYM_PHASE) {
|
|
374
|
+
throw new RangeError(`${name}: x = ${x}, nu = ${nu} has a phase correction nu^2 / (2x) of about ${corr.toPrecision(3)}, ` +
|
|
375
|
+
"which double precision does not resolve (at most 2^26)");
|
|
376
|
+
}
|
|
333
377
|
const ampl = asymAmplitude(v, x);
|
|
334
378
|
const phase = asymPhaseMx(v, x);
|
|
335
379
|
const cx = Math.cos(x);
|
|
336
380
|
const sx = Math.sin(x);
|
|
337
|
-
const ci = cospi(v
|
|
338
|
-
const si = sinpi(v
|
|
381
|
+
const ci = cospi(quarterPhase(v));
|
|
382
|
+
const si = sinpi(quarterPhase(v));
|
|
339
383
|
const sp = Math.sin(phase);
|
|
340
384
|
const cp = Math.cos(phase);
|
|
341
385
|
const y = (sp * (cx * ci + sx * si) + cp * (sx * ci - cx * si)) * ampl;
|
|
342
386
|
const j = (cp * (cx * ci + sx * si) - sp * (sx * ci - cx * si)) * ampl;
|
|
343
387
|
return { j, y };
|
|
344
388
|
}
|
|
389
|
+
/**
|
|
390
|
+
* Margin for the reflection formulas, which multiply Y_v by sin(pi v) or cos(pi v): for 1000 < v < 2^53 that is not
|
|
391
|
+
* an integer (or a half-integer, for the cosine), the factor is at least sin(pi 2^-43) = 3.6e-13 = e^-28.7
|
|
392
|
+
*/
|
|
393
|
+
const REFLECT_MARGIN = 30;
|
|
394
|
+
/**
|
|
395
|
+
* J and Y for v > LARGE_ORDER when the results are certainly out of range: J rounds to 0 by abs(J_v(x)) <=
|
|
396
|
+
* (x/2)^v / Gamma(v + 1), and Y overflows by abs(Y_v(x)) >= Gamma(v) (2/x)^v / pi with Y_v(x) < 0, for x <= v - 1.
|
|
397
|
+
* The latter holds as d/dx [x^v Y_v(x)] = x^v Y_(v-1)(x) < 0 below the first zero of Y_(v-1), which is beyond v - 1,
|
|
398
|
+
* and x^v Y_v(x) tends to -Gamma(v) 2^v / pi as x -> 0. Undefined when the bounds do not decide (the recurrences then
|
|
399
|
+
* take about v steps)
|
|
400
|
+
*/
|
|
401
|
+
function largeOrder(v, x, needY) {
|
|
402
|
+
const jB = logISeriesBounds(v, x);
|
|
403
|
+
if (certainRange({ lo: -Infinity, hi: jB.lo, size: jB.size }) !== -1)
|
|
404
|
+
return undefined;
|
|
405
|
+
if (!needY)
|
|
406
|
+
return { j: 0, y: { m: NaN, e2: 0 } };
|
|
407
|
+
if (!(x <= v - 1))
|
|
408
|
+
return undefined;
|
|
409
|
+
const a = v * (Math.LN2 - Math.log(x));
|
|
410
|
+
const g = lgamma(v);
|
|
411
|
+
const lo = g + a - Math.log(Math.PI) - REFLECT_MARGIN;
|
|
412
|
+
if (certainRange({ lo, hi: Infinity, size: Math.abs(a) + Math.abs(g) + 2 }) !== 1)
|
|
413
|
+
return undefined;
|
|
414
|
+
return { j: 0, y: { m: -1, e2: Infinity } };
|
|
415
|
+
}
|
|
416
|
+
/**
|
|
417
|
+
* Bounds on the time where the bounds of largeOrder do not decide (on the development machine): the continued fraction
|
|
418
|
+
* cf1JY takes about x - v terms where it oscillates (x > v; about 0.16 to 0.4 us each in double-double), the backward
|
|
419
|
+
* recurrence for J about v steps (about 5 ns each) and the forward recurrence for Y about v steps (about 45 ns each)
|
|
420
|
+
*/
|
|
421
|
+
const MAX_CF1_TERMS = 2 ** 25;
|
|
422
|
+
const MAX_J_STEPS = 2 ** 31;
|
|
423
|
+
const MAX_Y_STEPS = 2 ** 28;
|
|
424
|
+
/** RangeError before a continued fraction or a recurrence beyond the bounds above */
|
|
425
|
+
function checkTime(name, x, nu, cf1Terms, steps, maxSteps) {
|
|
426
|
+
if (cf1Terms > MAX_CF1_TERMS) {
|
|
427
|
+
throw new RangeError(`${name}: x = ${x}, nu = ${nu} needs a continued fraction of about ${Math.round(cf1Terms)} terms (at most 2^25)`);
|
|
428
|
+
}
|
|
429
|
+
if (steps > maxSteps) {
|
|
430
|
+
const bound = maxSteps === MAX_J_STEPS ? "2^31" : "2^28";
|
|
431
|
+
throw new RangeError(`${name}: x = ${x}, nu = ${nu} needs a recurrence of about ${steps} steps (at most ${bound})`);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
345
434
|
/**
|
|
346
435
|
* J_v(x) and Y_v(x) for v >= 0, x > 0 (Boost's bessel_jy without the reflection). Y is returned as y / yScale.
|
|
347
|
-
* needY: whether Y is wanted (J alone allows a cheaper series)
|
|
436
|
+
* needY: whether Y is wanted (J alone allows a cheaper series). name, nu: the caller and its order, for the bounds on
|
|
437
|
+
* the time
|
|
348
438
|
*/
|
|
349
|
-
function besselJYPos(v, x, needY) {
|
|
439
|
+
function besselJYPos(v, x, needY, name, nu) {
|
|
350
440
|
const n = Math.round(v);
|
|
351
441
|
const u = v - n; // -1/2 < u <= 1/2
|
|
352
|
-
const W = 2 / (x * Math.PI); // Wronskian
|
|
442
|
+
const W = Number.isFinite(x * Math.PI) ? 2 / (x * Math.PI) : 2 / Math.PI / x; // Wronskian
|
|
443
|
+
if (v > LARGE_ORDER) {
|
|
444
|
+
const big = largeOrder(v, x, needY);
|
|
445
|
+
if (big !== undefined)
|
|
446
|
+
return big;
|
|
447
|
+
}
|
|
353
448
|
if (!needY && (x < 5 || v > (x * x) / 4))
|
|
354
449
|
return { j: jSmallZ(v, x), y: { m: NaN, e2: 0 } };
|
|
355
450
|
if (x < 1 && u !== 0 && Math.log(EPS / 2) > v * Math.log(((x / 2) * (x / 2)) / v)) {
|
|
@@ -357,13 +452,13 @@ function besselJYPos(v, x, needY) {
|
|
|
357
452
|
}
|
|
358
453
|
if (Math.max(v, 1) < x * 0.011048543456039806) {
|
|
359
454
|
// sqrt(forth root of epsilon)
|
|
360
|
-
const a = asymJY(v, x);
|
|
455
|
+
const a = asymJY(v, x, name, nu);
|
|
361
456
|
return { j: a.j, y: { m: a.y, e2: 0 } };
|
|
362
457
|
}
|
|
363
458
|
if (x > 8) {
|
|
364
459
|
const pq = hankelPQ(v, x);
|
|
365
460
|
if (pq !== undefined) {
|
|
366
|
-
const modV = (v
|
|
461
|
+
const modV = quarterPhase(v) % 2;
|
|
367
462
|
const sx = Math.sin(x);
|
|
368
463
|
const cx = Math.cos(x);
|
|
369
464
|
const sv = sinpi(modV);
|
|
@@ -375,6 +470,7 @@ function besselJYPos(v, x, needY) {
|
|
|
375
470
|
}
|
|
376
471
|
}
|
|
377
472
|
if (x <= 2) {
|
|
473
|
+
checkTime(name, x, nu, 0, n, MAX_Y_STEPS);
|
|
378
474
|
const [yu, yu1] = temmeJY(u, x);
|
|
379
475
|
const r = yForward(u, x, n, yu, yu1);
|
|
380
476
|
const { fv } = cf1JY(v, x);
|
|
@@ -383,8 +479,10 @@ function besselJYPos(v, x, needY) {
|
|
|
383
479
|
return { j, y: { m: r.y, e2: r.e2 } };
|
|
384
480
|
}
|
|
385
481
|
// x > 2: backward recurrence for J from CF1, then CF2 for the normalisation and Y_u
|
|
482
|
+
const direct = x >= v && v > 2;
|
|
483
|
+
checkTime(name, x, nu, x - v, direct ? 0 : n, needY ? MAX_Y_STEPS : MAX_J_STEPS);
|
|
386
484
|
const { fv, sign: s } = cf1JY(v, x);
|
|
387
|
-
if (
|
|
485
|
+
if (direct) {
|
|
388
486
|
// Beyond the turning point CF2 converges at order v itself, so Steed's method is applied there directly. Boost
|
|
389
487
|
// goes down to u = v - round(v) and back up by recurrence, whose error grows with the number of steps
|
|
390
488
|
// (1.2e-13 relative to the amplitude for Y at v = 575, x = 2211)
|
|
@@ -440,7 +538,11 @@ function besselJYPos(v, x, needY) {
|
|
|
440
538
|
* @returns J_nu(x).
|
|
441
539
|
*
|
|
442
540
|
* For `x` much larger than `nu` but below the range of the asymptotic expansion (`x < nu / 0.011`), the time grows
|
|
443
|
-
* with `x` (a continued fraction of about `x` terms): about 15 ms at `x = 5e4`, `nu = 1000
|
|
541
|
+
* with `x` (a continued fraction of about `x - nu` terms): about 15 ms at `x = 5e4`, `nu = 1000`, 1.6 s at `x = 1e7`,
|
|
542
|
+
* `nu = 2e5`. Near the turning point (`x` a little below `nu`) a recurrence takes about `nu` steps. Beyond 2^25 terms
|
|
543
|
+
* of the continued fraction or 2^31 steps of the recurrence (2^28 for a negative non-integer order, which needs Y) a
|
|
544
|
+
* RangeError is thrown before the computation. So it is in the asymptotic expansion (`x` above about `90 nu`) where
|
|
545
|
+
* the phase correction `nu^2 / (2x)` exceeds 2^26, which double precision does not resolve.
|
|
444
546
|
*
|
|
445
547
|
* @example
|
|
446
548
|
* ```ts
|
|
@@ -466,10 +568,10 @@ export function besselJ(x, nu) {
|
|
|
466
568
|
if (x === Infinity)
|
|
467
569
|
return 0;
|
|
468
570
|
if (!reflect) {
|
|
469
|
-
const j = besselJYPos(v, x, false).j;
|
|
571
|
+
const j = besselJYPos(v, x, false, "besselJ", nu).j;
|
|
470
572
|
return nu < 0 && v % 2 === 1 ? -j : j; // J_-n = (-1)^n J_n
|
|
471
573
|
}
|
|
472
|
-
const r = besselJYPos(v, x, true);
|
|
574
|
+
const r = besselJYPos(v, x, true, "besselJ", nu);
|
|
473
575
|
return cospi(v) * r.j - ldexp(sinpi(v) * r.y.m, r.y.e2);
|
|
474
576
|
}
|
|
475
577
|
/**
|
|
@@ -480,6 +582,12 @@ export function besselJ(x, nu) {
|
|
|
480
582
|
* (A&S 9.1.2, as in R).
|
|
481
583
|
* @returns Y_nu(x).
|
|
482
584
|
*
|
|
585
|
+
* For orders above 1000, results far out of the range of doubles are returned at once (`Infinity` or 0, decided by
|
|
586
|
+
* bounds on their log); otherwise the time grows with the order (a recurrence of about `nu` steps): about 2.4 s for
|
|
587
|
+
* `besselY(0.9e8, 1e8)`. Beyond 2^28 steps, or 2^25 terms of the continued fraction for `x` above `nu` (as for
|
|
588
|
+
* {@link besselJ}), a RangeError is thrown before the computation, and likewise for a phase correction `nu^2 / (2x)`
|
|
589
|
+
* beyond 2^26 in the asymptotic expansion for large `x`.
|
|
590
|
+
*
|
|
483
591
|
* @example
|
|
484
592
|
* ```ts
|
|
485
593
|
* besselY(1, 0); // 0.08825696421567694
|
|
@@ -503,7 +611,7 @@ export function besselY(x, nu) {
|
|
|
503
611
|
}
|
|
504
612
|
if (x === Infinity)
|
|
505
613
|
return 0;
|
|
506
|
-
const r = besselJYPos(v, x, true);
|
|
614
|
+
const r = besselJYPos(v, x, true, "besselY", nu);
|
|
507
615
|
if (!reflect)
|
|
508
616
|
return ldexp(r.y.m, r.y.e2);
|
|
509
617
|
const cp = cospi(v);
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/** log(x/2), also for subnormal x (log(Number.MIN_VALUE / 2) was -Infinity) */
|
|
2
|
+
export declare function logHalf(x: number): number;
|
|
3
|
+
/** (x/2)^v, also for subnormal x */
|
|
4
|
+
export declare function powHalf(x: number, v: number): number;
|
|
1
5
|
/**
|
|
2
6
|
* (x/2)^v / Gamma(v + 1) * sum, the leading factor of the power series of I_v and J_v times the series sum.
|
|
3
7
|
* For v >= 10 the factor is Loader's Poisson term dpois(v; x/2) e^(x/2), which is accurate where pow / gamma loses
|
|
@@ -6,3 +10,26 @@
|
|
|
6
10
|
* factor (the log of a value near 1e-308 carries an absolute error of 700 ε, which a subnormal result can afford)
|
|
7
11
|
*/
|
|
8
12
|
export declare function seriesTimesPrefix(v: number, x: number, sum: number): number;
|
|
13
|
+
/**
|
|
14
|
+
* Large orders. The recurrences in besselIK.ts and besselJY.ts take about v steps, which does not end for v = 1e12
|
|
15
|
+
* (besselK(1, 1e12)), while the result is far beyond the range of doubles. Above this order, bounds on the log of the
|
|
16
|
+
* result decide such cases first
|
|
17
|
+
*/
|
|
18
|
+
export declare const LARGE_ORDER = 1000;
|
|
19
|
+
/** Bounds lo <= log y <= hi, with the sum of the sizes of the terms they were formed from (for the rounding margin) */
|
|
20
|
+
export interface LogBounds {
|
|
21
|
+
lo: number;
|
|
22
|
+
hi: number;
|
|
23
|
+
size: number;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Bounds on log I_v(x) (v >= 0) from its power series (x/2)^v / Gamma(v + 1) sum (x^2/4)^k / (k! (v+1)_k): the terms
|
|
27
|
+
* are positive, and the sum is at most exp(x^2 / (4 (v + 1))). The lower bound is also the upper bound of
|
|
28
|
+
* log abs(J_v(x)) (DLMF 10.14.4)
|
|
29
|
+
*/
|
|
30
|
+
export declare function logISeriesBounds(v: number, x: number): LogBounds;
|
|
31
|
+
/**
|
|
32
|
+
* 1 if a value whose log lies in [lo, hi] certainly overflows, -1 if it certainly rounds to 0, 0 otherwise. The bounds
|
|
33
|
+
* must clear the limits by a margin above the rounding of their terms
|
|
34
|
+
*/
|
|
35
|
+
export declare function certainRange(b: LogBounds): 1 | -1 | 0;
|