@numero-uno-jpn/rstats 0.5.0 → 0.6.1

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.
Files changed (168) hide show
  1. package/CHANGELOG.md +101 -0
  2. package/README.ja.md +36 -20
  3. package/README.md +34 -18
  4. package/THIRD_PARTY_NOTICES.md +25 -5
  5. package/dist/chunks/chunk-2JCIB5H2.js +2 -0
  6. package/dist/chunks/chunk-7JVSDGQQ.js +2 -0
  7. package/dist/chunks/chunk-7NK4SA6B.js +2 -0
  8. package/dist/chunks/chunk-AJYQNFXK.js +2 -0
  9. package/dist/chunks/chunk-DRNMIEYH.js +2 -0
  10. package/dist/chunks/chunk-NAYDS7CL.js +2 -0
  11. package/dist/chunks/chunk-NOCEAM44.js +2 -0
  12. package/dist/chunks/chunk-QNAIK4PY.js +2 -0
  13. package/dist/chunks/chunk-TPC6MTYC.js +2 -0
  14. package/dist/chunks/chunk-UGUQYWRQ.js +2 -0
  15. package/dist/chunks/chunk-XM2TVD63.js +2 -0
  16. package/dist/chunks/chunk-Y4IZV7XK.js +2 -0
  17. package/dist/chunks/chunk-ZWJ42QN6.js +2 -0
  18. package/dist/descriptive/basic.d.ts +73 -0
  19. package/dist/descriptive/basic.js +256 -0
  20. package/dist/descriptive/cov.d.ts +39 -0
  21. package/dist/descriptive/cov.js +105 -0
  22. package/dist/descriptive/hist.d.ts +64 -0
  23. package/dist/descriptive/hist.js +304 -0
  24. package/dist/descriptive/moments.d.ts +63 -0
  25. package/dist/descriptive/moments.js +158 -0
  26. package/dist/descriptive/quantile.d.ts +50 -0
  27. package/dist/descriptive/quantile.js +131 -0
  28. package/dist/descriptive/rank.d.ts +23 -0
  29. package/dist/descriptive/rank.js +72 -0
  30. package/dist/descriptive/table.d.ts +54 -0
  31. package/dist/descriptive/table.js +160 -0
  32. package/dist/descriptive.js +2 -0
  33. package/dist/distributions/arcsine.d.ts +24 -0
  34. package/dist/distributions/arcsine.js +73 -0
  35. package/dist/distributions/invgamma.d.ts +29 -0
  36. package/dist/distributions/invgamma.js +85 -0
  37. package/dist/distributions/kumaraswamy.d.ts +17 -0
  38. package/dist/distributions/kumaraswamy.js +96 -0
  39. package/dist/distributions/laplace.d.ts +23 -0
  40. package/dist/distributions/laplace.js +69 -0
  41. package/dist/distributions/multinomial.d.ts +24 -0
  42. package/dist/distributions/multinomial.js +70 -0
  43. package/dist/distributions/nbeta.js +28 -10
  44. package/dist/distributions/nchisq.js +20 -3
  45. package/dist/distributions/pareto.d.ts +23 -0
  46. package/dist/distributions/pareto.js +76 -0
  47. package/dist/distributions/summary.d.ts +25 -0
  48. package/dist/distributions/summary.js +289 -0
  49. package/dist/distributions/triangular.d.ts +25 -0
  50. package/dist/distributions/triangular.js +87 -0
  51. package/dist/distributions.js +2 -0
  52. package/dist/entries/descriptive.d.ts +7 -0
  53. package/dist/entries/descriptive.js +8 -0
  54. package/dist/entries/distributions.d.ts +41 -0
  55. package/dist/entries/distributions.js +41 -0
  56. package/dist/entries/linalg.d.ts +11 -0
  57. package/dist/entries/linalg.js +11 -0
  58. package/dist/entries/numerics.d.ts +8 -0
  59. package/dist/entries/numerics.js +6 -0
  60. package/dist/entries/special.d.ts +9 -0
  61. package/dist/entries/special.js +9 -0
  62. package/dist/entries/tests.d.ts +21 -0
  63. package/dist/entries/tests.js +20 -0
  64. package/dist/htest/ansariTest.d.ts +13 -0
  65. package/dist/htest/ansariTest.js +80 -0
  66. package/dist/htest/aov.d.ts +85 -0
  67. package/dist/htest/aov.js +178 -0
  68. package/dist/htest/binomTest.d.ts +36 -0
  69. package/dist/htest/binomTest.js +121 -0
  70. package/dist/htest/cor.d.ts +14 -0
  71. package/dist/htest/cor.js +4 -3
  72. package/dist/htest/core/ansari.d.ts +18 -0
  73. package/dist/htest/core/ansari.js +121 -0
  74. package/dist/htest/core/kolmogorov.d.ts +26 -0
  75. package/dist/htest/core/kolmogorov.js +221 -0
  76. package/dist/htest/core/mantelhaen.d.ts +26 -0
  77. package/dist/htest/core/mantelhaen.js +145 -0
  78. package/dist/htest/core/shapiro.d.ts +24 -0
  79. package/dist/htest/core/shapiro.js +142 -0
  80. package/dist/htest/core/smirnov.d.ts +20 -0
  81. package/dist/htest/core/smirnov.js +75 -0
  82. package/dist/htest/fisherTest.js +134 -120
  83. package/dist/htest/groups.d.ts +20 -0
  84. package/dist/htest/groups.js +62 -0
  85. package/dist/htest/ksTest.d.ts +31 -0
  86. package/dist/htest/ksTest.js +106 -0
  87. package/dist/htest/mantelhaenTest.d.ts +26 -0
  88. package/dist/htest/mantelhaenTest.js +196 -0
  89. package/dist/htest/mcnemarTest.d.ts +26 -0
  90. package/dist/htest/mcnemarTest.js +99 -0
  91. package/dist/htest/onewayTest.d.ts +26 -0
  92. package/dist/htest/onewayTest.js +97 -0
  93. package/dist/htest/pairwise.d.ts +64 -0
  94. package/dist/htest/pairwise.js +152 -0
  95. package/dist/htest/poissonTest.d.ts +35 -0
  96. package/dist/htest/poissonTest.js +170 -0
  97. package/dist/htest/propTest.d.ts +37 -0
  98. package/dist/htest/propTest.js +164 -0
  99. package/dist/htest/rankTests.d.ts +52 -0
  100. package/dist/htest/rankTests.js +234 -0
  101. package/dist/htest/types.d.ts +2 -0
  102. package/dist/htest/types.js +6 -0
  103. package/dist/htest/varTest.d.ts +48 -0
  104. package/dist/htest/varTest.js +118 -0
  105. package/dist/htest/wilcoxTest.js +136 -174
  106. package/dist/index.d.ts +6 -50
  107. package/dist/index.js +8 -50
  108. package/dist/integrate/quadpack.d.ts +29 -0
  109. package/dist/integrate/quadpack.js +525 -0
  110. package/dist/internal/seq.d.ts +5 -0
  111. package/dist/internal/seq.js +14 -0
  112. package/dist/internal/uniroot.js +10 -71
  113. package/dist/linalg/lu.d.ts +45 -0
  114. package/dist/linalg/lu.js +88 -0
  115. package/dist/linalg/ops.d.ts +90 -0
  116. package/dist/linalg/ops.js +320 -0
  117. package/dist/linalg.js +2 -0
  118. package/dist/models/lm.d.ts +98 -0
  119. package/dist/models/lm.js +187 -0
  120. package/dist/numerics/deriv.d.ts +37 -0
  121. package/dist/numerics/deriv.js +150 -0
  122. package/dist/numerics/integrate.d.ts +91 -0
  123. package/dist/numerics/integrate.js +264 -0
  124. package/dist/numerics/interp.d.ts +103 -0
  125. package/dist/numerics/interp.js +581 -0
  126. package/dist/numerics/ode.d.ts +23 -0
  127. package/dist/numerics/ode.js +59 -0
  128. package/dist/numerics.js +2 -0
  129. package/dist/optim/localmin.d.ts +16 -0
  130. package/dist/optim/localmin.js +97 -0
  131. package/dist/random/beta.js +1 -1
  132. package/dist/random/binomial.js +1 -1
  133. package/dist/random/continuous.js +1 -1
  134. package/dist/random/extra.d.ts +33 -0
  135. package/dist/random/extra.js +62 -0
  136. package/dist/random/gamma.js +1 -1
  137. package/dist/random/hyper.js +1 -1
  138. package/dist/random/mt.js +0 -1
  139. package/dist/random/poisson.js +0 -1
  140. package/dist/random/rank.js +1 -1
  141. package/dist/random/rng.js +1 -1
  142. package/dist/random/sample.js +1 -1
  143. package/dist/rstats.js +2 -2
  144. package/dist/special/besselIK.d.ts +38 -0
  145. package/dist/special/besselIK.js +315 -0
  146. package/dist/special/besselJY.d.ts +32 -0
  147. package/dist/special/besselJY.js +508 -0
  148. package/dist/special/besselSeries.d.ts +8 -0
  149. package/dist/special/besselSeries.js +24 -0
  150. package/dist/special/beta.js +22 -5
  151. package/dist/special/choose.d.ts +75 -0
  152. package/dist/special/choose.js +233 -0
  153. package/dist/special/erf.d.ts +28 -2
  154. package/dist/special/erf.js +37 -10
  155. package/dist/special/extra.d.ts +47 -0
  156. package/dist/special/extra.js +220 -0
  157. package/dist/special/igam.d.ts +4 -0
  158. package/dist/special/igam.js +57 -2
  159. package/dist/special/incbet.js +77 -22
  160. package/dist/special/lgammaDiff.d.ts +7 -0
  161. package/dist/special/lgammaDiff.js +16 -3
  162. package/dist/special/psigamma.d.ts +48 -0
  163. package/dist/special/psigamma.js +318 -0
  164. package/dist/special/rCompatible.d.ts +8 -0
  165. package/dist/special/rCompatible.js +1 -0
  166. package/dist/special.js +2 -0
  167. package/dist/tests.js +2 -0
  168. package/package.json +26 -1
@@ -0,0 +1,38 @@
1
+ /** Options for {@link besselI} and {@link besselK}. */
2
+ export interface BesselOptions {
3
+ /** If `true`, return `exp(-x) I(x, nu)` (for `besselI`) or `exp(x) K(x, nu)` (for `besselK`). Default `false`. */
4
+ exponScaled?: boolean;
5
+ }
6
+ /**
7
+ * Modified Bessel function of the first kind. Equivalent to R's
8
+ * `besselI(x, nu, expon.scaled = FALSE)` for single numbers.
9
+ *
10
+ * @param x - Argument, `x >= 0` (`NaN` for negative `x`).
11
+ * @param nu - Order, any real number. For `nu < 0`, `I(x, -nu) = I(x, nu) + 2/pi sin(pi nu) K(x, nu)`
12
+ * (A&S 9.6.2, as in R).
13
+ * @param options - `exponScaled` (see {@link BesselOptions}).
14
+ * @returns I_nu(x), or `exp(-x) I_nu(x)` when `exponScaled`.
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * besselI(1, 0); // 1.2660658777520086
19
+ * besselI(1000, 2, { exponScaled: true }); // 0.012592018595377398
20
+ * ```
21
+ */
22
+ export declare function besselI(x: number, nu: number, options?: BesselOptions): number;
23
+ /**
24
+ * Modified Bessel function of the second kind. Equivalent to R's
25
+ * `besselK(x, nu, expon.scaled = FALSE)` for single numbers.
26
+ *
27
+ * @param x - Argument, `x >= 0` (`NaN` for negative `x`).
28
+ * @param nu - Order, any real number (`K(x, -nu) = K(x, nu)`).
29
+ * @param options - `exponScaled` (see {@link BesselOptions}).
30
+ * @returns K_nu(x), or `exp(x) K_nu(x)` when `exponScaled`.
31
+ *
32
+ * @example
33
+ * ```ts
34
+ * besselK(1, 0); // 0.4210244382407083
35
+ * besselK(1000, 2, { exponScaled: true }); // 0.03970761786238014
36
+ * ```
37
+ */
38
+ export declare function besselK(x: number, nu: number, options?: BesselOptions): number;
@@ -0,0 +1,315 @@
1
+ // Modified Bessel functions I and K of real order: besselI, besselK.
2
+ //
3
+ // Ported from Boost.Math include/boost/math/special_functions/detail/bessel_ik.hpp, bessel.hpp and
4
+ // detail/bessel_jy_asym.hpp (Boost.Math commit 0a0fb4e).
5
+ // Copyright (c) 2006 Xiaogang Zhang, (c) 2007 John Maddock, (c) 2024 Matt Borland.
6
+ // Boost Software License 1.0. See THIRD_PARTY_NOTICES.md.
7
+ //
8
+ // Changes from Boost
9
+ // - Everything is computed scaled, e^-x I_v(x) and e^x K_v(x) (R's expon.scaled), and unscaled values are formed at
10
+ // the end. The forward recurrence for K keeps a separate binary exponent, so neither overflows on the way.
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
+ import { lgamma1p, sinpi } from "./gamma.js";
13
+ import { seriesTimesPrefix } from "./besselSeries.js";
14
+ const EPS = Number.EPSILON;
15
+ const MAX_ITER = 1_000_000;
16
+ /** Gamma(1 + v) - 1 for |v| <= 1/2 */
17
+ function tgamma1pm1(v) {
18
+ return Math.expm1(lgamma1p(v));
19
+ }
20
+ /**
21
+ * K_u(x) and K_(u+1)(x) by Temme's series (|u| <= 1/2, 0 < x <= 2).
22
+ * Temme, Journal of Computational Physics, vol 21, 343 (1976)
23
+ */
24
+ function temmeIK(u, x) {
25
+ const gp = tgamma1pm1(u);
26
+ const gm = tgamma1pm1(-u);
27
+ const a = Math.log(x / 2);
28
+ const b = Math.exp(u * a);
29
+ const sigma = -a * u;
30
+ const c = Math.abs(u) < EPS ? 1 : sinpi(u) / (u * Math.PI);
31
+ const d = Math.abs(sigma) < EPS ? 1 : Math.sinh(sigma) / sigma;
32
+ const gamma1 = Math.abs(u) < EPS ? -0.5772156649015329 : (0.5 / u) * (gp - gm) * c;
33
+ const gamma2 = ((2 + gp + gm) * c) / 2;
34
+ let p = (gp + 1) / (2 * b);
35
+ let q = ((1 + gm) * b) / 2;
36
+ let f = (Math.cosh(sigma) * gamma1 + d * -a * gamma2) / c;
37
+ let h = p;
38
+ let coef = 1;
39
+ let sum = coef * f;
40
+ let sum1 = coef * h;
41
+ for (let k = 1; k < MAX_ITER; k++) {
42
+ f = (k * f + p + q) / (k * k - u * u);
43
+ p /= k - u;
44
+ q /= k + u;
45
+ h = p - k * f;
46
+ coef *= (x * x) / (4 * k);
47
+ sum += coef * f;
48
+ sum1 += coef * h;
49
+ if (Math.abs(coef * f) < Math.abs(sum) * EPS)
50
+ break;
51
+ }
52
+ return [sum, (2 * sum1) / x];
53
+ }
54
+ /** fv = I_(v+1) / I_v by continued fraction (A&S 9.1.73, modified Lentz) */
55
+ function cf1IK(v, x) {
56
+ const tiny = Math.sqrt(Number.MIN_VALUE * 2 ** 52); // sqrt(DBL_MIN)
57
+ let C = tiny;
58
+ let f = tiny;
59
+ let D = 0;
60
+ for (let k = 1; k < MAX_ITER; k++) {
61
+ const b = (2 * (v + k)) / x;
62
+ C = b + 1 / C;
63
+ D = b + D;
64
+ if (C === 0)
65
+ C = tiny;
66
+ if (D === 0)
67
+ D = tiny;
68
+ D = 1 / D;
69
+ const delta = C * D;
70
+ f *= delta;
71
+ if (Math.abs(delta - 1) <= 2 * EPS)
72
+ break;
73
+ }
74
+ return f;
75
+ }
76
+ /**
77
+ * 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
78
+ * Communications, vol 47, 245 (1987)). Boost multiplies by e^-x at the end; here the scaled values are returned
79
+ */
80
+ function cf2IKScaled(u, x) {
81
+ let a = u * u - 0.25;
82
+ let b = 2 * (x + 1);
83
+ let D = 1 / b;
84
+ let f = D;
85
+ let delta = D;
86
+ let prev = 0;
87
+ let current = 1;
88
+ let C = -a;
89
+ let Q = C;
90
+ let S = 1 + Q * delta;
91
+ for (let k = 2; k < MAX_ITER; k++) {
92
+ a -= 2 * (k - 1);
93
+ b += 2;
94
+ D = 1 / (b + a * D);
95
+ delta *= b * D - 1;
96
+ f += delta;
97
+ const q = (prev - (b - 2) * current) / a;
98
+ prev = current;
99
+ current = q;
100
+ C *= -a / k;
101
+ Q += C * q;
102
+ S += Q * delta;
103
+ if (q < EPS) {
104
+ C *= q;
105
+ prev /= q;
106
+ current /= q;
107
+ }
108
+ if (Math.abs(Q * delta) < Math.abs(S) * EPS)
109
+ break;
110
+ }
111
+ const ratio = (0.5 + u + x + (u * u - 0.25) * f) / x;
112
+ const ku = Math.sqrt(Math.PI / (2 * x)) / S;
113
+ return [ku, ku * ratio];
114
+ }
115
+ /** Multiply by 2^e without overflow in the intermediate power */
116
+ function ldexp(m, e) {
117
+ if (!Number.isFinite(e))
118
+ return e > 0 ? m * Infinity : m * 0;
119
+ while (e > 1000) {
120
+ m *= 2 ** 1000;
121
+ e -= 1000;
122
+ }
123
+ while (e < -1000) {
124
+ m *= 2 ** -1000;
125
+ e += 1000;
126
+ }
127
+ return m * 2 ** e;
128
+ }
129
+ /**
130
+ * 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)
132
+ */
133
+ function kScaled(v, x) {
134
+ const n = Math.round(v);
135
+ const u = v - n;
136
+ let prev;
137
+ let current;
138
+ 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
+ prev = current = Math.sqrt(Math.PI / (2 * x));
141
+ }
142
+ else if (x <= 2) {
143
+ const [ku, ku1] = temmeIK(u, x);
144
+ const ex = Math.exp(x);
145
+ prev = ku * ex;
146
+ current = ku1 * ex;
147
+ }
148
+ else {
149
+ [prev, current] = cf2IKScaled(u, x);
150
+ }
151
+ let exp2 = 0;
152
+ for (let k = 1; k <= n; k++) {
153
+ const fact = (2 * (u + k)) / x;
154
+ // rescale by a power of two (exact); the factor itself can be huge for tiny x, so look at the product
155
+ const lg = Math.log2(Math.abs(current)) + Math.log2(fact);
156
+ if (lg > 900 && Number.isFinite(lg)) {
157
+ const s = Math.ceil(lg - 800);
158
+ prev = ldexp(prev, -s);
159
+ current = ldexp(current, -s);
160
+ exp2 += s;
161
+ }
162
+ const next = fact * current + prev;
163
+ prev = current;
164
+ current = next;
165
+ }
166
+ return { k: prev, k1: current, exp2 };
167
+ }
168
+ /** I_v(x) by its power series for x small compared to v: (x/2)^v / Gamma(v + 1) * sum (x^2/4)^k / (k! (v+1)_k) */
169
+ function iSmallZ(v, x) {
170
+ const mult = (x * x) / 4;
171
+ let term = 1;
172
+ let sum = 0;
173
+ for (let k = 1; k < MAX_ITER; k++) {
174
+ sum += term;
175
+ term *= mult / k;
176
+ term /= k + v;
177
+ if (Math.abs(term) <= EPS * Math.abs(sum))
178
+ break;
179
+ }
180
+ return seriesTimesPrefix(v, x, sum + term);
181
+ }
182
+ /** 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
+ function mulExp(m, e2, y) {
184
+ if (m === 0 || !Number.isFinite(m))
185
+ return m;
186
+ let v = m;
187
+ let e = e2;
188
+ const step = y > 0 ? 700 : -700;
189
+ const estep = Math.exp(step);
190
+ while (Math.abs(y) > 700) {
191
+ y -= step; // exact
192
+ v *= estep;
193
+ const k = Math.floor(Math.log2(Math.abs(v)));
194
+ v = ldexp(v, -k);
195
+ e += k;
196
+ }
197
+ return ldexp(v * Math.exp(y), e);
198
+ }
199
+ /** The value e^(want x) * (unscaled value): want = 0 for the unscaled value, -1 for e^-x I, 1 for e^x K */
200
+ function resolve(r, x, want) {
201
+ const ex = r.ex + want;
202
+ return ex === 0 ? ldexp(r.m, r.e2) : mulExp(r.m, r.e2, ex * x);
203
+ }
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)
207
+ return { m: Math.sqrt(2 / (x * Math.PI)) * (-Math.expm1(-2 * x) / 2), e2: 0, ex: 1 };
208
+ if (v > 0 && x / v < 0.25)
209
+ return { m: iSmallZ(v, x), e2: 0, ex: 0 };
210
+ let lim = (4 * v * v + 10) / (8 * x);
211
+ lim *= lim;
212
+ lim *= lim;
213
+ lim /= 24;
214
+ if (lim < EPS * 10 && x > 100) {
215
+ // A&S 9.7.1, three terms (Boost's asymptotic_bessel_i_large_x)
216
+ const mu = 4 * v * v;
217
+ const ez = 8 * x;
218
+ let num = mu - 1;
219
+ let denom = ez;
220
+ let s = 1 - num / denom;
221
+ num *= mu - 9;
222
+ denom *= ez * 2;
223
+ s += num / denom;
224
+ num *= mu - 25;
225
+ denom *= ez * 3;
226
+ s -= num / denom;
227
+ return { m: s / Math.sqrt(2 * x * Math.PI), e2: 0, ex: 1 };
228
+ }
229
+ // 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);
232
+ return { m: 1 / (x * (k * fv + k1)), e2: -exp2, ex: 1 };
233
+ }
234
+ /** K_v(x) for v >= 0, x > 0, as K_v = m 2^e2 e^(ex x) */
235
+ function kPos(v, x) {
236
+ // A&S 9.7.2, four terms, where the first omitted term is below eps times the sum (as Boost limits the expansion
237
+ // of I, but measured on the term itself). For v <= 4.5 the remainder is bounded by the first omitted term
238
+ // (DLMF 10.40(ii), l >= v - 1/2); beyond, a small t4 needs x well above v^2, where the terms fall off fast.
239
+ // Testing only mu - 25 used to enter the expansion for v near 2.5 at any x
240
+ const mu = 4 * v * v;
241
+ const ez = 8 * x;
242
+ const t1 = (mu - 1) / ez;
243
+ const t2 = (t1 * (mu - 9)) / (2 * ez);
244
+ const t3 = (t2 * (mu - 25)) / (3 * ez);
245
+ const t4 = (t3 * (mu - 49)) / (4 * ez);
246
+ const s = 1 + t1 + t2 + t3;
247
+ if (Math.abs(t4) < EPS * Math.abs(s)) {
248
+ return { m: (s * Math.sqrt(Math.PI)) / Math.sqrt(2 * x), e2: 0, ex: -1 };
249
+ }
250
+ const { k, exp2 } = kScaled(v, x);
251
+ return { m: k, e2: exp2, ex: -1 };
252
+ }
253
+ /**
254
+ * Modified Bessel function of the first kind. Equivalent to R's
255
+ * `besselI(x, nu, expon.scaled = FALSE)` for single numbers.
256
+ *
257
+ * @param x - Argument, `x >= 0` (`NaN` for negative `x`).
258
+ * @param nu - Order, any real number. For `nu < 0`, `I(x, -nu) = I(x, nu) + 2/pi sin(pi nu) K(x, nu)`
259
+ * (A&S 9.6.2, as in R).
260
+ * @param options - `exponScaled` (see {@link BesselOptions}).
261
+ * @returns I_nu(x), or `exp(-x) I_nu(x)` when `exponScaled`.
262
+ *
263
+ * @example
264
+ * ```ts
265
+ * besselI(1, 0); // 1.2660658777520086
266
+ * besselI(1000, 2, { exponScaled: true }); // 0.012592018595377398
267
+ * ```
268
+ */
269
+ export function besselI(x, nu, options = {}) {
270
+ const scaled = options.exponScaled ?? false;
271
+ if (Number.isNaN(x) || Number.isNaN(nu) || x < 0 || !Number.isFinite(nu))
272
+ return NaN;
273
+ const v = Math.abs(nu);
274
+ const negNonInt = nu < 0 && nu !== Math.floor(nu);
275
+ if (x === 0) {
276
+ if (negNonInt)
277
+ return sinpi(v) > 0 ? Infinity : -Infinity;
278
+ return nu === 0 ? 1 : 0;
279
+ }
280
+ if (x === Infinity)
281
+ return scaled ? 0 : Infinity;
282
+ const want = scaled ? -1 : 0;
283
+ const i = resolve(iPos(v, x), x, want);
284
+ if (!negNonInt)
285
+ return i;
286
+ // A&S 9.6.2: I_-v = I_v + (2/pi) sin(pi v) K_v, each term in the requested scaling
287
+ const k = kPos(v, x);
288
+ return i + resolve({ ...k, m: (2 / Math.PI) * sinpi(v) * k.m }, x, want);
289
+ }
290
+ /**
291
+ * Modified Bessel function of the second kind. Equivalent to R's
292
+ * `besselK(x, nu, expon.scaled = FALSE)` for single numbers.
293
+ *
294
+ * @param x - Argument, `x >= 0` (`NaN` for negative `x`).
295
+ * @param nu - Order, any real number (`K(x, -nu) = K(x, nu)`).
296
+ * @param options - `exponScaled` (see {@link BesselOptions}).
297
+ * @returns K_nu(x), or `exp(x) K_nu(x)` when `exponScaled`.
298
+ *
299
+ * @example
300
+ * ```ts
301
+ * besselK(1, 0); // 0.4210244382407083
302
+ * besselK(1000, 2, { exponScaled: true }); // 0.03970761786238014
303
+ * ```
304
+ */
305
+ export function besselK(x, nu, options = {}) {
306
+ const scaled = options.exponScaled ?? false;
307
+ if (Number.isNaN(x) || Number.isNaN(nu) || x < 0 || !Number.isFinite(nu))
308
+ return NaN;
309
+ const v = Math.abs(nu);
310
+ if (x === 0)
311
+ return Infinity;
312
+ if (x === Infinity)
313
+ return 0;
314
+ return resolve(kPos(v, x), x, scaled ? 1 : 0);
315
+ }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Bessel function of the first kind. Equivalent to R's `besselJ(x, nu)` for single numbers.
3
+ *
4
+ * @param x - Argument, `x >= 0` (`NaN` for negative `x`).
5
+ * @param nu - Order, any real number. For `nu < 0`, `J(x, -nu) = cos(pi nu) J(x, nu) - sin(pi nu) Y(x, nu)`
6
+ * (A&S 9.1.2, as in R).
7
+ * @returns J_nu(x).
8
+ *
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`.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * besselJ(1, 0); // 0.7651976865579666
15
+ * besselJ(10, 2.5); // 0.1966584835818184
16
+ * ```
17
+ */
18
+ export declare function besselJ(x: number, nu: number): number;
19
+ /**
20
+ * Bessel function of the second kind. Equivalent to R's `besselY(x, nu)` for single numbers.
21
+ *
22
+ * @param x - Argument, `x >= 0` (`NaN` for negative `x`; `-Infinity` at 0).
23
+ * @param nu - Order, any real number. For `nu < 0`, `Y(x, -nu) = sin(pi nu) J(x, nu) + cos(pi nu) Y(x, nu)`
24
+ * (A&S 9.1.2, as in R).
25
+ * @returns Y_nu(x).
26
+ *
27
+ * @example
28
+ * ```ts
29
+ * besselY(1, 0); // 0.08825696421567694
30
+ * ```
31
+ */
32
+ export declare function besselY(x: number, nu: number): number;