@numero-uno-jpn/rstats 0.15.3 → 0.17.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.
Files changed (322) hide show
  1. package/CHANGELOG.md +294 -0
  2. package/README.ja.md +68 -35
  3. package/README.md +65 -30
  4. package/dist/chunks/chunk-2C33MXSW.js +3 -0
  5. package/dist/chunks/chunk-34S5SQY6.js +2 -0
  6. package/dist/chunks/chunk-4WE3OO62.js +2 -0
  7. package/dist/chunks/chunk-5AYHCITP.js +2 -0
  8. package/dist/chunks/chunk-6JJQL6HL.js +2 -0
  9. package/dist/chunks/chunk-C7V2FH6L.js +3 -0
  10. package/dist/chunks/chunk-CZI7KUO3.js +2 -0
  11. package/dist/chunks/chunk-DXI5N2ET.js +2 -0
  12. package/dist/chunks/chunk-FY3FVOBM.js +2 -0
  13. package/dist/chunks/chunk-H3WVPZDL.js +2 -0
  14. package/dist/chunks/chunk-LHUIJQHY.js +2 -0
  15. package/dist/chunks/chunk-LV3W2F7N.js +2 -0
  16. package/dist/chunks/chunk-M4ITG7YR.js +2 -0
  17. package/dist/chunks/chunk-NSYZTS26.js +2 -0
  18. package/dist/chunks/chunk-ONY5CISH.js +2 -0
  19. package/dist/chunks/chunk-RFE7MVP7.js +2 -0
  20. package/dist/chunks/chunk-SE7V2KLO.js +2 -0
  21. package/dist/chunks/chunk-THIGHOKK.js +2 -0
  22. package/dist/chunks/chunk-TNMJPQPV.js +2 -0
  23. package/dist/chunks/chunk-WM7VFXTV.js +2 -0
  24. package/dist/chunks/chunk-WMDYDTVQ.js +2 -0
  25. package/dist/chunks/chunk-XLSFJTTK.js +2 -0
  26. package/dist/chunks/chunk-Z37NELOS.js +2 -0
  27. package/dist/chunks/chunk-ZFQWB222.js +2 -0
  28. package/dist/data/grouping.js +26 -7
  29. package/dist/descriptive/basic.js +110 -8
  30. package/dist/descriptive/cov.js +23 -4
  31. package/dist/descriptive/dataFrames.d.ts +4 -2
  32. package/dist/descriptive/dataFrames.js +69 -23
  33. package/dist/descriptive/factanal.d.ts +2 -0
  34. package/dist/descriptive/factanal.js +26 -9
  35. package/dist/descriptive/hist.d.ts +8 -5
  36. package/dist/descriptive/hist.js +77 -31
  37. package/dist/descriptive/missing.js +3 -1
  38. package/dist/descriptive/moments.js +86 -19
  39. package/dist/descriptive/omega.d.ts +1 -1
  40. package/dist/descriptive/omega.js +74 -10
  41. package/dist/descriptive/pivot.d.ts +25 -10
  42. package/dist/descriptive/pivot.js +182 -42
  43. package/dist/descriptive/quantile.js +10 -1
  44. package/dist/descriptive/rank.js +8 -1
  45. package/dist/descriptive/table.d.ts +23 -3
  46. package/dist/descriptive/table.js +36 -21
  47. package/dist/descriptive.js +2 -2
  48. package/dist/distributions/arcsine.js +27 -2
  49. package/dist/distributions/beta.js +33 -4
  50. package/dist/distributions/binomial.js +46 -14
  51. package/dist/distributions/bvnorm.js +11 -2
  52. package/dist/distributions/cauchy.js +21 -4
  53. package/dist/distributions/chisq.js +16 -3
  54. package/dist/distributions/exponential.js +14 -0
  55. package/dist/distributions/f.js +41 -17
  56. package/dist/distributions/gamma.js +35 -3
  57. package/dist/distributions/geometric.js +58 -13
  58. package/dist/distributions/hypergeometric.d.ts +5 -1
  59. package/dist/distributions/hypergeometric.js +101 -6
  60. package/dist/distributions/invgamma.js +10 -1
  61. package/dist/distributions/kumaraswamy.js +9 -0
  62. package/dist/distributions/laplace.js +36 -6
  63. package/dist/distributions/logistic.js +13 -1
  64. package/dist/distributions/lognormal.js +16 -2
  65. package/dist/distributions/multinomial.js +58 -11
  66. package/dist/distributions/nbeta.js +62 -11
  67. package/dist/distributions/nchisq.js +30 -6
  68. package/dist/distributions/negbinomial.js +102 -17
  69. package/dist/distributions/normal.js +17 -4
  70. package/dist/distributions/nt.js +392 -17
  71. package/dist/distributions/pareto.js +12 -1
  72. package/dist/distributions/poisson.js +37 -3
  73. package/dist/distributions/signrank.js +16 -4
  74. package/dist/distributions/summary.d.ts +2 -1
  75. package/dist/distributions/summary.js +481 -61
  76. package/dist/distributions/t.js +9 -0
  77. package/dist/distributions/triangular.js +19 -5
  78. package/dist/distributions/tukey.js +11 -2
  79. package/dist/distributions/uniform.js +8 -0
  80. package/dist/distributions/weibull.js +18 -0
  81. package/dist/distributions/wilcox.d.ts +9 -3
  82. package/dist/distributions/wilcox.js +164 -26
  83. package/dist/distributions.js +2 -2
  84. package/dist/entries/descriptive.d.ts +3 -1
  85. package/dist/entries/descriptive.js +1 -1
  86. package/dist/entries/distributions.d.ts +1 -0
  87. package/dist/entries/distributions.js +2 -0
  88. package/dist/entries/linalg.d.ts +3 -1
  89. package/dist/entries/mixed.d.ts +3 -0
  90. package/dist/entries/mixed.js +2 -0
  91. package/dist/entries/numerics.d.ts +2 -1
  92. package/dist/entries/tests.d.ts +8 -5
  93. package/dist/entries/tests.js +3 -1
  94. package/dist/htest/agreement.d.ts +16 -2
  95. package/dist/htest/agreement.js +122 -21
  96. package/dist/htest/ansariTest.d.ts +8 -2
  97. package/dist/htest/ansariTest.js +116 -18
  98. package/dist/htest/aov.d.ts +29 -23
  99. package/dist/htest/aov.js +90 -47
  100. package/dist/htest/binomTest.d.ts +5 -0
  101. package/dist/htest/binomTest.js +47 -17
  102. package/dist/htest/brunnerMunzelTest.d.ts +4 -3
  103. package/dist/htest/brunnerMunzelTest.js +12 -5
  104. package/dist/htest/chisqTest.d.ts +5 -2
  105. package/dist/htest/chisqTest.js +151 -46
  106. package/dist/htest/cor.d.ts +9 -2
  107. package/dist/htest/cor.js +57 -26
  108. package/dist/htest/core/ansari.d.ts +9 -0
  109. package/dist/htest/core/ansari.js +21 -1
  110. package/dist/htest/core/kolmogorov.d.ts +25 -0
  111. package/dist/htest/core/kolmogorov.js +123 -0
  112. package/dist/htest/core/mantelhaen.js +6 -4
  113. package/dist/htest/core/shapiro.d.ts +1 -1
  114. package/dist/htest/core/shapiro.js +5 -1
  115. package/dist/htest/core/simulate.d.ts +6 -0
  116. package/dist/htest/core/simulate.js +20 -0
  117. package/dist/htest/dunnett.js +14 -5
  118. package/dist/htest/effectSize.d.ts +5 -0
  119. package/dist/htest/effectSize.js +117 -35
  120. package/dist/htest/fisherTest.d.ts +4 -3
  121. package/dist/htest/fisherTest.js +88 -24
  122. package/dist/htest/groups.d.ts +7 -0
  123. package/dist/htest/groups.js +21 -4
  124. package/dist/htest/ksTest.d.ts +7 -1
  125. package/dist/htest/ksTest.js +36 -6
  126. package/dist/htest/leveneTest.d.ts +2 -0
  127. package/dist/htest/leveneTest.js +28 -6
  128. package/dist/htest/mantelhaenTest.js +132 -25
  129. package/dist/htest/mcnemarTest.js +40 -11
  130. package/dist/htest/multipleComparisons.d.ts +24 -26
  131. package/dist/htest/multipleComparisons.js +70 -64
  132. package/dist/htest/normalityTests.js +54 -14
  133. package/dist/htest/onewayTest.js +106 -5
  134. package/dist/htest/pAdjust.d.ts +3 -2
  135. package/dist/htest/pAdjust.js +42 -6
  136. package/dist/htest/pairwise.d.ts +3 -1
  137. package/dist/htest/pairwise.js +46 -21
  138. package/dist/htest/poissonTest.d.ts +1 -1
  139. package/dist/htest/poissonTest.js +117 -59
  140. package/dist/htest/polycor.js +47 -17
  141. package/dist/htest/power.d.ts +15 -1
  142. package/dist/htest/power.js +128 -10
  143. package/dist/htest/propTest.d.ts +3 -0
  144. package/dist/htest/propTest.js +44 -6
  145. package/dist/htest/pwr.d.ts +38 -1
  146. package/dist/htest/pwr.js +159 -39
  147. package/dist/htest/rankTests.js +36 -6
  148. package/dist/htest/tTest.d.ts +2 -0
  149. package/dist/htest/tTest.js +53 -21
  150. package/dist/htest/testFormula.js +11 -11
  151. package/dist/htest/trendTests.d.ts +28 -4
  152. package/dist/htest/trendTests.js +124 -35
  153. package/dist/htest/types.d.ts +7 -0
  154. package/dist/htest/types.js +7 -0
  155. package/dist/htest/varTest.js +52 -14
  156. package/dist/htest/wilcoxTest.js +164 -29
  157. package/dist/integrate/quadpack.js +27 -5
  158. package/dist/internal/checkOptions.d.ts +11 -0
  159. package/dist/internal/checkOptions.js +31 -0
  160. package/dist/internal/covmat.d.ts +5 -0
  161. package/dist/internal/covmat.js +20 -0
  162. package/dist/internal/ddexp.d.ts +15 -1
  163. package/dist/internal/ddexp.js +59 -6
  164. package/dist/internal/discrete.d.ts +14 -0
  165. package/dist/internal/discrete.js +78 -5
  166. package/dist/internal/extremes.d.ts +9 -0
  167. package/dist/internal/extremes.js +24 -0
  168. package/dist/internal/fround.js +13 -4
  169. package/dist/internal/integer.d.ts +9 -0
  170. package/dist/internal/integer.js +12 -0
  171. package/dist/internal/invert.js +15 -2
  172. package/dist/internal/optionalArgs.d.ts +12 -0
  173. package/dist/internal/optionalArgs.js +46 -0
  174. package/dist/internal/scaledInverse.d.ts +10 -0
  175. package/dist/internal/scaledInverse.js +22 -0
  176. package/dist/internal/seq.js +15 -2
  177. package/dist/internal/summary.d.ts +90 -0
  178. package/dist/internal/summary.js +458 -39
  179. package/dist/internal/uniroot.d.ts +7 -0
  180. package/dist/internal/uniroot.js +28 -3
  181. package/dist/internal/warnings.d.ts +66 -0
  182. package/dist/internal/warnings.js +122 -0
  183. package/dist/linalg/basic.d.ts +3 -1
  184. package/dist/linalg/basic.js +58 -7
  185. package/dist/linalg/chol.js +10 -1
  186. package/dist/linalg/dot.d.ts +29 -0
  187. package/dist/linalg/dot.js +132 -0
  188. package/dist/linalg/eigen.d.ts +6 -1
  189. package/dist/linalg/eigen.js +75 -14
  190. package/dist/linalg/expm.d.ts +3 -2
  191. package/dist/linalg/expm.js +113 -15
  192. package/dist/linalg/kappa.js +64 -10
  193. package/dist/linalg/lu.d.ts +2 -1
  194. package/dist/linalg/lu.js +40 -5
  195. package/dist/linalg/matrix.d.ts +14 -0
  196. package/dist/linalg/matrix.js +24 -0
  197. package/dist/linalg/norm.js +4 -0
  198. package/dist/linalg/ops.d.ts +27 -2
  199. package/dist/linalg/ops.js +74 -26
  200. package/dist/linalg/qr.d.ts +2 -1
  201. package/dist/linalg/qr.js +105 -23
  202. package/dist/linalg/schur.js +11 -3
  203. package/dist/linalg/solve.d.ts +6 -1
  204. package/dist/linalg/solve.js +156 -26
  205. package/dist/linalg/svd.d.ts +9 -2
  206. package/dist/linalg/svd.js +33 -11
  207. package/dist/linalg.js +2 -2
  208. package/dist/mixed.js +4 -4
  209. package/dist/models/anovaSequential.d.ts +32 -2
  210. package/dist/models/anovaSequential.js +170 -28
  211. package/dist/models/aovEz.js +154 -39
  212. package/dist/models/aovStrata.js +68 -24
  213. package/dist/models/carAnova.d.ts +1 -1
  214. package/dist/models/carAnova.js +78 -33
  215. package/dist/models/coxph.d.ts +6 -0
  216. package/dist/models/coxph.js +166 -69
  217. package/dist/models/estimable.d.ts +15 -0
  218. package/dist/models/estimable.js +68 -0
  219. package/dist/models/formula.d.ts +1 -1
  220. package/dist/models/formula.js +26 -8
  221. package/dist/models/glm.d.ts +57 -7
  222. package/dist/models/glm.js +427 -74
  223. package/dist/models/glmAnova.js +90 -14
  224. package/dist/models/glmConfint.d.ts +1 -1
  225. package/dist/models/glmConfint.js +121 -29
  226. package/dist/models/glmFamily.d.ts +11 -6
  227. package/dist/models/glmFamily.js +247 -62
  228. package/dist/models/lm.d.ts +12 -1
  229. package/dist/models/lm.js +246 -37
  230. package/dist/models/lmer.d.ts +31 -7
  231. package/dist/models/lmer.js +129 -42
  232. package/dist/models/lmerAnova.d.ts +3 -3
  233. package/dist/models/lmerAnova.js +47 -9
  234. package/dist/models/lmerHypotheses.d.ts +6 -2
  235. package/dist/models/lmerHypotheses.js +14 -2
  236. package/dist/models/lmerTest.d.ts +13 -6
  237. package/dist/models/lmerTest.js +82 -29
  238. package/dist/models/mauchly.d.ts +6 -1
  239. package/dist/models/mauchly.js +41 -15
  240. package/dist/models/mixedStructure.js +6 -0
  241. package/dist/models/modelFrame.js +10 -4
  242. package/dist/models/modelMatrix.d.ts +3 -1
  243. package/dist/models/modelMatrix.js +47 -17
  244. package/dist/models/scaledTables.d.ts +8 -0
  245. package/dist/models/scaledTables.js +6 -0
  246. package/dist/models/survival.d.ts +12 -1
  247. package/dist/models/survival.js +75 -28
  248. package/dist/models/tiedTimes.d.ts +2 -0
  249. package/dist/models/tiedTimes.js +34 -0
  250. package/dist/numerics/deriv.d.ts +1 -1
  251. package/dist/numerics/deriv.js +72 -8
  252. package/dist/numerics/integrate.d.ts +9 -1
  253. package/dist/numerics/integrate.js +111 -16
  254. package/dist/numerics/interp.d.ts +40 -9
  255. package/dist/numerics/interp.js +375 -55
  256. package/dist/numerics/ode.js +59 -1
  257. package/dist/numerics.js +2 -2
  258. package/dist/optim/localmin.js +4 -0
  259. package/dist/optim/optim.d.ts +3 -1
  260. package/dist/optim/optim.js +97 -10
  261. package/dist/random/beta.js +58 -4
  262. package/dist/random/binomial.d.ts +14 -3
  263. package/dist/random/binomial.js +59 -24
  264. package/dist/random/continuous.js +50 -5
  265. package/dist/random/extra.js +55 -9
  266. package/dist/random/gamma.d.ts +4 -1
  267. package/dist/random/gamma.js +47 -8
  268. package/dist/random/hyper.js +9 -6
  269. package/dist/random/mt.js +17 -2
  270. package/dist/random/poisson.js +31 -3
  271. package/dist/random/r2dtable.d.ts +8 -1
  272. package/dist/random/r2dtable.js +24 -3
  273. package/dist/random/rank.d.ts +2 -0
  274. package/dist/random/rank.js +44 -4
  275. package/dist/random/rng.js +4 -0
  276. package/dist/random/sample.d.ts +12 -2
  277. package/dist/random/sample.js +63 -20
  278. package/dist/random/streams.js +4 -2
  279. package/dist/rstats.js +17 -14
  280. package/dist/special/besselIK.d.ts +12 -0
  281. package/dist/special/besselIK.js +175 -23
  282. package/dist/special/besselJY.d.ts +11 -1
  283. package/dist/special/besselJY.js +127 -19
  284. package/dist/special/besselSeries.d.ts +27 -0
  285. package/dist/special/besselSeries.js +45 -2
  286. package/dist/special/beta.js +4 -1
  287. package/dist/special/choose.js +24 -8
  288. package/dist/special/dd.js +122 -18
  289. package/dist/special/extra.js +14 -2
  290. package/dist/special/gamma.js +11 -2
  291. package/dist/special/incbet.js +30 -4
  292. package/dist/special/incbetTemme.d.ts +7 -0
  293. package/dist/special/incbetTemme.js +45 -12
  294. package/dist/special/loader.d.ts +1 -1
  295. package/dist/special/loader.js +117 -20
  296. package/dist/special/log1pmx.js +4 -0
  297. package/dist/special/psigamma.js +7 -0
  298. package/dist/special.js +2 -2
  299. package/dist/tests.js +15 -11
  300. package/llms.txt +15 -6
  301. package/package.json +19 -10
  302. package/dist/chunks/chunk-3THFTB36.js +0 -2
  303. package/dist/chunks/chunk-3TSWYE5L.js +0 -2
  304. package/dist/chunks/chunk-3WPU6LGK.js +0 -2
  305. package/dist/chunks/chunk-5ITR67MB.js +0 -2
  306. package/dist/chunks/chunk-5NRFU6F6.js +0 -2
  307. package/dist/chunks/chunk-E2LCSVQV.js +0 -2
  308. package/dist/chunks/chunk-FCNVTIPJ.js +0 -2
  309. package/dist/chunks/chunk-FKAKALOZ.js +0 -2
  310. package/dist/chunks/chunk-HD73UKPT.js +0 -2
  311. package/dist/chunks/chunk-IUULP3LE.js +0 -2
  312. package/dist/chunks/chunk-JQG3YQOC.js +0 -2
  313. package/dist/chunks/chunk-KT66SB7R.js +0 -2
  314. package/dist/chunks/chunk-MAUAOQG3.js +0 -2
  315. package/dist/chunks/chunk-MDYE7QP2.js +0 -2
  316. package/dist/chunks/chunk-NM3Z2UBQ.js +0 -2
  317. package/dist/chunks/chunk-PMJI5QO6.js +0 -3
  318. package/dist/chunks/chunk-UF7KOTHF.js +0 -3
  319. package/dist/chunks/chunk-UOE57JGT.js +0 -2
  320. package/dist/chunks/chunk-USPEBXSR.js +0 -2
  321. package/dist/chunks/chunk-VLUL2HFW.js +0 -3
  322. package/dist/chunks/chunk-ZIX7NYQF.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
@@ -10,9 +10,16 @@
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
+ // Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
15
+ import { checkOptions } from "../internal/checkOptions.js";
16
+ const BESSEL_I_KEYS = ["exponScaled"];
17
+ const BESSEL_K_KEYS = ["exponScaled"];
18
+ // Generated by `node scripts/option-keys.mjs --write` (end)
14
19
  const EPS = Number.EPSILON;
15
20
  const MAX_ITER = 1_000_000;
21
+ /** Below this x, x^2/4 < 2^-2000: the power series are exact after their first term */
22
+ const TINY_X = 2 ** -1000;
16
23
  /** Gamma(1 + v) - 1 for |v| <= 1/2 */
17
24
  function tgamma1pm1(v) {
18
25
  return Math.expm1(lgamma1p(v));
@@ -24,7 +31,7 @@ function tgamma1pm1(v) {
24
31
  function temmeIK(u, x) {
25
32
  const gp = tgamma1pm1(u);
26
33
  const gm = tgamma1pm1(-u);
27
- const a = Math.log(x / 2);
34
+ const a = logHalf(x);
28
35
  const b = Math.exp(u * a);
29
36
  const sigma = -a * u;
30
37
  const c = Math.abs(u) < EPS ? 1 : sinpi(u) / (u * Math.PI);
@@ -51,8 +58,14 @@ function temmeIK(u, x) {
51
58
  }
52
59
  return [sum, (2 * sum1) / x];
53
60
  }
54
- /** fv = I_(v+1) / I_v by continued fraction (A&S 9.1.73, modified Lentz) */
61
+ /**
62
+ * fv = I_(v+1) / I_v by continued fraction (A&S 9.1.73, modified Lentz), and whether it converged within MAX_ITER
63
+ * terms (for x above about 5e10 it does not, #492)
64
+ */
55
65
  function cf1IK(v, x) {
66
+ // b_1 = Infinity would make the Lentz steps Infinity * 0 = NaN; the rest of the fraction is below (1/b_1)^2
67
+ if (!Number.isFinite((2 * (v + 1)) / x))
68
+ return [x / 2 / (v + 1), true];
56
69
  const tiny = Math.sqrt(Number.MIN_VALUE * 2 ** 52); // sqrt(DBL_MIN)
57
70
  let C = tiny;
58
71
  let f = tiny;
@@ -69,9 +82,9 @@ function cf1IK(v, x) {
69
82
  const delta = C * D;
70
83
  f *= delta;
71
84
  if (Math.abs(delta - 1) <= 2 * EPS)
72
- break;
85
+ return [f, true];
73
86
  }
74
- return f;
87
+ return [f, false];
75
88
  }
76
89
  /**
77
90
  * 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 +139,28 @@ function ldexp(m, e) {
126
139
  }
127
140
  return m * 2 ** e;
128
141
  }
142
+ /**
143
+ * Bound on the time of the forward recurrence of kScaled, where the bounds of besselI and besselK do not decide: about
144
+ * 35 ns per step on the development machine
145
+ */
146
+ const MAX_K_STEPS = 2 ** 28;
129
147
  /**
130
148
  * 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)
149
+ * Forward recurrence K_(u+k+1) = 2 (u + k) / x K_(u+k) + K_(u+k-1) from u = v - round(v).
150
+ * name, nu: the caller and its order, for the RangeError beyond MAX_K_STEPS
132
151
  */
133
- function kScaled(v, x) {
152
+ function kScaled(v, x, name, nu) {
134
153
  const n = Math.round(v);
154
+ if (n > MAX_K_STEPS) {
155
+ throw new RangeError(`${name}: x = ${x}, nu = ${nu} needs a recurrence of about ${n} steps (at most 2^28)`);
156
+ }
135
157
  const u = v - n;
136
158
  let prev;
137
159
  let current;
138
160
  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));
161
+ // 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
162
+ // the quotient, which overflows or loses digits when 2x is subnormal
163
+ prev = current = x < TINY_X ? Math.sqrt(Math.PI / 2) / Math.sqrt(x) : Math.sqrt(Math.PI / (2 * x));
141
164
  }
142
165
  else if (x <= 2) {
143
166
  const [ku, ku1] = temmeIK(u, x);
@@ -179,10 +202,53 @@ function iSmallZ(v, x) {
179
202
  }
180
203
  return seriesTimesPrefix(v, x, sum + term);
181
204
  }
205
+ /**
206
+ * Bound on the size of the terms of the exponent in Debye's expansion: formed in double, the exponent has an absolute
207
+ * 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
208
+ * besselJ, MAX_ASYM_PHASE)
209
+ */
210
+ const MAX_DEBYE_EXP = 2 ** 26;
211
+ /**
212
+ * 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:
213
+ * 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) /
214
+ * (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
215
+ * than MAX_K_STEPS steps or the continued fraction of I does not converge (#492); the omitted term u5 / v^5 is at
216
+ * 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),
217
+ * s = sqrt(1 + z^2), is formed without forming v eta, which cancels against x (x = 1e308, v = 1e154: v eta - x = -0.5)
218
+ */
219
+ function debyeIK(v, x, want, what) {
220
+ const z = x / v;
221
+ const s = z > 1 ? z * Math.sqrt(1 + 1 / (z * z)) : Math.sqrt(1 + z * z);
222
+ const t = 1 / s;
223
+ const t2 = t * t;
224
+ const u1 = (t * (3 - 5 * t2)) / 24;
225
+ const u2 = (t2 * (81 + t2 * (-462 + t2 * 385))) / 1152;
226
+ const u3 = (t * t2 * (30375 + t2 * (-369603 + t2 * (765765 - t2 * 425425)))) / 414720;
227
+ const u4 = (t2 * t2 * (4465125 + t2 * (-94121676 + t2 * (349922430 + t2 * (-446185740 + t2 * 185910725))))) / 39813120;
228
+ const w = 1 / v;
229
+ const sgn = want === "I" ? 1 : -1;
230
+ const sum = 1 + w * (sgn * u1 + w * (u2 + w * (sgn * u3 + w * u4)));
231
+ const p = s + z;
232
+ const a = v / p;
233
+ const b = v * Math.log1p((1 + 1 / p) / z);
234
+ // (2 pi v and v s overflow for the largest v and x: the square roots apart)
235
+ const root = Math.sqrt(v) * Math.sqrt(s);
236
+ const size = Math.abs(a) + Math.abs(b);
237
+ if (want === "I")
238
+ return { m: sum / (Math.sqrt(2 * Math.PI) * root), e2: 0, ex: 1, debye: { y: a - b, size, what } };
239
+ return { m: (sum * Math.sqrt(Math.PI / 2)) / root, e2: 0, ex: -1, debye: { y: b - a, size, what } };
240
+ }
182
241
  /** 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
242
  function mulExp(m, e2, y) {
184
243
  if (m === 0 || !Number.isFinite(m))
185
244
  return m;
245
+ // a result far out of range is 0 or Infinity at once: the steps of 700 below took |y| / 700 turns (besselI(1e12, 0)
246
+ // did not return), and none at all ended once 700 was below the spacing of y (y = -2e308)
247
+ const log2 = Math.log2(Math.abs(m)) + e2 + y * Math.LOG2E;
248
+ if (log2 < -1100)
249
+ return m * 0;
250
+ if (log2 > 1100)
251
+ return m * Infinity;
186
252
  let v = m;
187
253
  let e = e2;
188
254
  const step = y > 0 ? 700 : -700;
@@ -199,13 +265,31 @@ function mulExp(m, e2, y) {
199
265
  /** The value e^(want x) * (unscaled value): want = 0 for the unscaled value, -1 for e^-x I, 1 for e^x K */
200
266
  function resolve(r, x, want) {
201
267
  const ex = r.ex + want;
268
+ if (r.debye) {
269
+ const { y, size, what } = r.debye;
270
+ const value = mulExp(r.m, r.e2, ex * x + y);
271
+ // a result far out of range is 0 or Infinity whatever the error of its exponent
272
+ const total = size + Math.abs(ex) * x;
273
+ if (value !== 0 && Number.isFinite(value) && total > MAX_DEBYE_EXP) {
274
+ throw new RangeError(`${what} has an exponent in Debye's expansion formed from terms of about ${total.toPrecision(3)}, ` +
275
+ `which double precision does not resolve (at most 2^26)`);
276
+ }
277
+ return value;
278
+ }
202
279
  return ex === 0 ? ldexp(r.m, r.e2) : mulExp(r.m, r.e2, ex * x);
203
280
  }
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)
281
+ /** I_v(x) for v >= 0, x > 0, as I_v = m 2^e2 e^(ex x) (name, nu: as for kScaled) */
282
+ function iPos(v, x, name, nu) {
283
+ if (v === 0.5) {
284
+ // 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
285
+ // overflows (besselI(1e-310, 0.5) was Infinity)
286
+ if (x < TINY_X)
287
+ return { m: Math.sqrt(2 / Math.PI) * Math.sqrt(x), e2: 0, ex: 1 };
207
288
  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)
289
+ }
290
+ // For tiny x the series is exact after one term at any v; the Wronskian below needs K_(v+1), which overflows there
291
+ // (besselI(1e-310, 0) was NaN)
292
+ if (x < TINY_X || (v > 0 && x / v < 0.25))
209
293
  return { m: iSmallZ(v, x), e2: 0, ex: 0 };
210
294
  let lim = (4 * v * v + 10) / (8 * x);
211
295
  lim *= lim;
@@ -224,15 +308,23 @@ function iPos(v, x) {
224
308
  num *= mu - 25;
225
309
  denom *= ez * 3;
226
310
  s -= num / denom;
227
- return { m: s / Math.sqrt(2 * x * Math.PI), e2: 0, ex: 1 };
311
+ // (2 x pi overflows for x above 2.8e307: besselI(1e308, 0, scaled) was 0; the square roots apart there)
312
+ const r = Number.isFinite(2 * x * Math.PI) ? Math.sqrt(2 * x * Math.PI) : Math.sqrt(2 * Math.PI) * Math.sqrt(x);
313
+ return { m: s / r, e2: 0, ex: 1 };
228
314
  }
315
+ // Debye where the recurrence of K would be too long or the continued fraction does not converge (#492: it gave
316
+ // besselI(1e12, 2e5, scaled) 25% low without notice)
317
+ if (Math.round(v) > MAX_K_STEPS)
318
+ return debyeIK(v, x, "I", `${name}: x = ${x}, nu = ${nu}`);
319
+ const [fv, converged] = cf1IK(v, x);
320
+ if (!converged && v > LARGE_ORDER)
321
+ return debyeIK(v, x, "I", `${name}: x = ${x}, nu = ${nu}`);
229
322
  // 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);
323
+ const { k, k1, exp2 } = kScaled(v, x, name, nu);
232
324
  return { m: 1 / (x * (k * fv + k1)), e2: -exp2, ex: 1 };
233
325
  }
234
- /** K_v(x) for v >= 0, x > 0, as K_v = m 2^e2 e^(ex x) */
235
- function kPos(v, x) {
326
+ /** K_v(x) for v >= 0, x > 0, as K_v = m 2^e2 e^(ex x) (name, nu: as for kScaled) */
327
+ function kPos(v, x, name, nu) {
236
328
  // A&S 9.7.2, four terms, where the first omitted term is below eps times the sum (as Boost limits the expansion
237
329
  // of I, but measured on the term itself). For v <= 4.5 the remainder is bounded by the first omitted term
238
330
  // (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 +337,31 @@ function kPos(v, x) {
245
337
  const t4 = (t3 * (mu - 49)) / (4 * ez);
246
338
  const s = 1 + t1 + t2 + t3;
247
339
  if (Math.abs(t4) < EPS * Math.abs(s)) {
248
- return { m: (s * Math.sqrt(Math.PI)) / Math.sqrt(2 * x), e2: 0, ex: -1 };
340
+ // (2 x overflows for x above 9e307: besselK(1e308, 0, scaled) was 0)
341
+ const m = Number.isFinite(2 * x) ? (s * Math.sqrt(Math.PI)) / Math.sqrt(2 * x) : (s * Math.sqrt(Math.PI / 2)) / Math.sqrt(x);
342
+ return { m, e2: 0, ex: -1 };
249
343
  }
250
- const { k, exp2 } = kScaled(v, x);
344
+ if (Math.round(v) > MAX_K_STEPS)
345
+ return debyeIK(v, x, "K", `${name}: x = ${x}, nu = ${nu}`);
346
+ const { k, exp2 } = kScaled(v, x, name, nu);
251
347
  return { m: k, e2: exp2, ex: -1 };
252
348
  }
349
+ /**
350
+ * 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
351
+ * 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
352
+ */
353
+ function logKBounds(v, x) {
354
+ const below = logISeriesBounds(v - 1, x);
355
+ const above = logISeriesBounds(v + 1, x);
356
+ const lx = Math.log(x);
357
+ return { lo: -below.hi - lx - Math.LN2, hi: -above.lo - lx, size: below.size + above.size + Math.abs(lx) + 1 };
358
+ }
359
+ /** The bounds of log(y e^s) */
360
+ function shifted(b, s) {
361
+ return { lo: b.lo + s, hi: b.hi + s, size: b.size + Math.abs(s) };
362
+ }
363
+ /** log(1 - 1/e): what is left of a term after taking off another at most 1/e of its size */
364
+ const LOG_LEFT = Math.log1p(-Math.exp(-1));
253
365
  /**
254
366
  * Modified Bessel function of the first kind. Equivalent to R's
255
367
  * `besselI(x, nu, expon.scaled = FALSE)`. Takes single numbers.
@@ -260,6 +372,12 @@ function kPos(v, x) {
260
372
  * @param options - `exponScaled` (see {@link BesselOptions}).
261
373
  * @returns I_nu(x), or `exp(-x) I_nu(x)` when `exponScaled`.
262
374
  *
375
+ * For orders above 1000, results far out of the range of doubles are returned at once (`Infinity` or 0, decided by
376
+ * bounds on their log); otherwise the time grows with the order (a recurrence of about `nu` steps): several seconds at
377
+ * `nu = 1e8`. Beyond 2^28 steps, and for `besselI` where its continued fraction does not converge (`x` above about
378
+ * 5e10), Debye's uniform expansion is used; a RangeError is thrown only where its exponent `nu eta - x` is formed from
379
+ * terms above 2^26 and the result is in range (near `x = 0.66 nu`, unscaled).
380
+ *
263
381
  * @example
264
382
  * ```ts
265
383
  * besselI(1, 0); // 1.2660658777520086
@@ -269,6 +387,7 @@ function kPos(v, x) {
269
387
  * @category Special functions
270
388
  */
271
389
  export function besselI(x, nu, options = {}) {
390
+ checkOptions(options, BESSEL_I_KEYS, "besselI"); // generated: option-keys.mjs
272
391
  const scaled = options.exponScaled ?? false;
273
392
  if (Number.isNaN(x) || Number.isNaN(nu) || x < 0 || !Number.isFinite(nu))
274
393
  return NaN;
@@ -282,11 +401,31 @@ export function besselI(x, nu, options = {}) {
282
401
  if (x === Infinity)
283
402
  return scaled ? 0 : Infinity;
284
403
  const want = scaled ? -1 : 0;
285
- const i = resolve(iPos(v, x), x, want);
404
+ if (v > LARGE_ORDER) {
405
+ // The result is far out of range (besselI(1e12, 1e12) is Infinity), decided before a recurrence of v steps
406
+ const iB = shifted(logISeriesBounds(v, x), want * x);
407
+ const ri = certainRange(iB);
408
+ if (!negNonInt) {
409
+ if (ri !== 0)
410
+ return ri > 0 ? Infinity : 0;
411
+ }
412
+ else {
413
+ // I_-v = I_v + (2/pi) sin(pi v) K_v: one term decides when it exceeds the other by a factor e
414
+ const s = sinpi(v);
415
+ const kB = shifted(logKBounds(v, x), want * x + Math.log((2 / Math.PI) * Math.abs(s)));
416
+ if (ri === -1 && certainRange(kB) === -1)
417
+ return 0;
418
+ if (kB.lo - 1 > iB.hi && certainRange(shifted(kB, LOG_LEFT)) === 1)
419
+ return s > 0 ? Infinity : -Infinity;
420
+ if (iB.lo - 1 > kB.hi && certainRange(shifted(iB, LOG_LEFT)) === 1)
421
+ return Infinity;
422
+ }
423
+ }
424
+ const i = resolve(iPos(v, x, "besselI", nu), x, want);
286
425
  if (!negNonInt)
287
426
  return i;
288
427
  // 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);
428
+ const k = kPos(v, x, "besselI", nu);
290
429
  return i + resolve({ ...k, m: (2 / Math.PI) * sinpi(v) * k.m }, x, want);
291
430
  }
292
431
  /**
@@ -298,6 +437,12 @@ export function besselI(x, nu, options = {}) {
298
437
  * @param options - `exponScaled` (see {@link BesselOptions}).
299
438
  * @returns K_nu(x), or `exp(x) K_nu(x)` when `exponScaled`.
300
439
  *
440
+ * For orders above 1000, results far out of the range of doubles are returned at once (`Infinity` or 0, decided by
441
+ * bounds on their log); otherwise the time grows with the order (a recurrence of about `nu` steps): several seconds at
442
+ * `nu = 1e8`. Beyond 2^28 steps, and for `besselI` where its continued fraction does not converge (`x` above about
443
+ * 5e10), Debye's uniform expansion is used; a RangeError is thrown only where its exponent `nu eta - x` is formed from
444
+ * terms above 2^26 and the result is in range (near `x = 0.66 nu`, unscaled).
445
+ *
301
446
  * @example
302
447
  * ```ts
303
448
  * besselK(1, 0); // 0.4210244382407083
@@ -307,6 +452,7 @@ export function besselI(x, nu, options = {}) {
307
452
  * @category Special functions
308
453
  */
309
454
  export function besselK(x, nu, options = {}) {
455
+ checkOptions(options, BESSEL_K_KEYS, "besselK"); // generated: option-keys.mjs
310
456
  const scaled = options.exponScaled ?? false;
311
457
  if (Number.isNaN(x) || Number.isNaN(nu) || x < 0 || !Number.isFinite(nu))
312
458
  return NaN;
@@ -315,5 +461,11 @@ export function besselK(x, nu, options = {}) {
315
461
  return Infinity;
316
462
  if (x === Infinity)
317
463
  return 0;
318
- return resolve(kPos(v, x), x, scaled ? 1 : 0);
464
+ if (v > LARGE_ORDER) {
465
+ // besselK(1, 1e12) is Infinity, decided before a recurrence of v steps
466
+ const r = certainRange(shifted(logKBounds(v, x), scaled ? x : 0));
467
+ if (r !== 0)
468
+ return r > 0 ? Infinity : 0;
469
+ }
470
+ return resolve(kPos(v, x, "besselK", nu), x, scaled ? 1 : 0);
319
471
  }
@@ -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
@@ -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 = Math.log(x / 2);
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 = Math.pow(x / 2, v);
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 = Math.pow(x / 2, u);
164
- const a = Math.log(x / 2);
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
- return Math.sqrt((s * 2) / (Math.PI * x));
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 / 2 + 0.25);
338
- const si = sinpi(v / 2 + 0.25);
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 / 2 + 0.25) % 2;
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 (x >= v && v > 2) {
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);