@danielsimonjr/mathts-functions 0.63.0 → 0.64.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/dist/index.js CHANGED
@@ -4366,7 +4366,7 @@ function dwt(x, wavelet = "haar") {
4366
4366
  try {
4367
4367
  return dwtPeriodization(x, wavelet);
4368
4368
  } catch (err) {
4369
- throw new Error(`dwt: ${err instanceof Error ? err.message : String(err)}`);
4369
+ throw new Error(`dwt: ${err instanceof Error ? err.message : String(err)}`, { cause: err });
4370
4370
  }
4371
4371
  }
4372
4372
  }
@@ -6548,7 +6548,7 @@ function betaincScalar(a, b, x) {
6548
6548
  }
6549
6549
  const lnBeta = _lgamma(a) + _lgamma(b) - _lgamma(a + b);
6550
6550
  const front = Math.exp(Math.log(x) * a + Math.log(1 - x) * b - lnBeta) / a;
6551
- let f = 1;
6551
+ let f;
6552
6552
  let c = 1;
6553
6553
  let d = 1 - (a + b) * x / (a + 1);
6554
6554
  if (Math.abs(d) < 1e-30) d = 1e-30;
@@ -6806,9 +6806,9 @@ function _carlsonRJ(x, y, z, p) {
6806
6806
  const sy = Math.sqrt(yy);
6807
6807
  const sz = Math.sqrt(zz);
6808
6808
  const lam = sx * sy + sy * sz + sz * sx;
6809
- const alpha = pp * (sx + sy + sz) + sx * sy * sz;
6810
- const beta2 = pp * (pp + lam) * (pp + lam);
6811
- sum3 += fac * _carlsonRC(alpha * alpha, beta2);
6809
+ const alpha2 = pp * (sx + sy + sz) + sx * sy * sz;
6810
+ const beta3 = pp * (pp + lam) * (pp + lam);
6811
+ sum3 += fac * _carlsonRC(alpha2 * alpha2, beta3);
6812
6812
  fac /= 4;
6813
6813
  xx = (xx + lam) / 4;
6814
6814
  yy = (yy + lam) / 4;
@@ -8420,19 +8420,19 @@ var jsDivergence = mathTyped11("jsDivergence", {
8420
8420
  return jsd;
8421
8421
  }
8422
8422
  });
8423
- function betaPDFScalar(x, alpha, beta_) {
8424
- if (alpha <= 0 || beta_ <= 0) return NaN;
8423
+ function betaPDFScalar(x, alpha2, beta_) {
8424
+ if (alpha2 <= 0 || beta_ <= 0) return NaN;
8425
8425
  if (x <= 0 || x >= 1) return 0;
8426
- const logB = _lgammaD(alpha) + _lgammaD(beta_) - _lgammaD(alpha + beta_);
8427
- return Math.exp((alpha - 1) * Math.log(x) + (beta_ - 1) * Math.log(1 - x) - logB);
8426
+ const logB = _lgammaD(alpha2) + _lgammaD(beta_) - _lgammaD(alpha2 + beta_);
8427
+ return Math.exp((alpha2 - 1) * Math.log(x) + (beta_ - 1) * Math.log(1 - x) - logB);
8428
8428
  }
8429
8429
  var betaPDF = mathTyped11("betaPDF", {
8430
- "number, number, number": (x, alpha, beta_) => betaPDFScalar(x, alpha, beta_),
8431
- "Float64Array, number, number": (x, alpha, beta_) => {
8430
+ "number, number, number": (x, alpha2, beta_) => betaPDFScalar(x, alpha2, beta_),
8431
+ "Float64Array, number, number": (x, alpha2, beta_) => {
8432
8432
  const out = new Float64Array(x.length);
8433
- const lgAlpha = _lgammaD(alpha);
8433
+ const lgAlpha = _lgammaD(alpha2);
8434
8434
  const lgBeta = _lgammaD(beta_);
8435
- const lgAlphaBeta = _lgammaD(alpha + beta_);
8435
+ const lgAlphaBeta = _lgammaD(alpha2 + beta_);
8436
8436
  const logB = lgAlpha + lgBeta - lgAlphaBeta;
8437
8437
  if (x.length >= WASM_SPECIAL_THRESHOLD) {
8438
8438
  for (let i = 0; i < x.length; i++) {
@@ -8440,13 +8440,13 @@ var betaPDF = mathTyped11("betaPDF", {
8440
8440
  if (xi <= 0 || xi >= 1) {
8441
8441
  out[i] = 0;
8442
8442
  } else {
8443
- out[i] = Math.exp((alpha - 1) * Math.log(xi) + (beta_ - 1) * Math.log(1 - xi) - logB);
8443
+ out[i] = Math.exp((alpha2 - 1) * Math.log(xi) + (beta_ - 1) * Math.log(1 - xi) - logB);
8444
8444
  }
8445
8445
  }
8446
8446
  return Promise.resolve(out);
8447
8447
  }
8448
8448
  for (let i = 0; i < x.length; i++) {
8449
- out[i] = betaPDFScalar(x[i], alpha, beta_);
8449
+ out[i] = betaPDFScalar(x[i], alpha2, beta_);
8450
8450
  }
8451
8451
  return Promise.resolve(out);
8452
8452
  }
@@ -12649,7 +12649,7 @@ function linearInterp(xs, ys, x) {
12649
12649
  if (xs.length !== ys.length || xs.length < 2) {
12650
12650
  throw new Error("linearInterp requires at least 2 data points with matching lengths");
12651
12651
  }
12652
- let i = 0;
12652
+ let i;
12653
12653
  if (x <= xs[0]) {
12654
12654
  i = 0;
12655
12655
  } else if (x >= xs[xs.length - 1]) {
@@ -12836,7 +12836,7 @@ function pchipInterp(xs, ys, x) {
12836
12836
  delta[n - 3]
12837
12837
  );
12838
12838
  }
12839
- let i = 0;
12839
+ let i;
12840
12840
  if (x <= xs[0]) {
12841
12841
  i = 0;
12842
12842
  } else if (x >= xs[n - 1]) {
@@ -13813,7 +13813,7 @@ function bdfSolve(f, tspan, y0raw, options = {}) {
13813
13813
  const F0 = _fArr(f, t, y);
13814
13814
  const J = _jacobianAt(f, t, y, F0, options);
13815
13815
  let stepAccepted = false;
13816
- let yNew = y;
13816
+ let yNew;
13817
13817
  let dVec = new Array(n).fill(0);
13818
13818
  let errorNorm = 0;
13819
13819
  let nIter = 1;
@@ -15217,7 +15217,7 @@ function minimize(f, x0, opts) {
15217
15217
  const maxIter = opts?.maxIter ?? 1e3;
15218
15218
  const step = opts?.step ?? 0.1;
15219
15219
  const n = x0.length;
15220
- const alpha = 1;
15220
+ const alpha2 = 1;
15221
15221
  const gamma2 = 2;
15222
15222
  const rho = 0.5;
15223
15223
  const sigma = 0.5;
@@ -15249,7 +15249,7 @@ function minimize(f, x0, opts) {
15249
15249
  }
15250
15250
  }
15251
15251
  for (let j = 0; j < n; j++) centroid2[j] /= n;
15252
- const xr = centroid2.map((c, j) => c + alpha * (c - simplex[n][j]));
15252
+ const xr = centroid2.map((c, j) => c + alpha2 * (c - simplex[n][j]));
15253
15253
  const fr = f(xr);
15254
15254
  if (fr < fValues[n - 1] && fr >= fValues[0]) {
15255
15255
  simplex[n] = xr;
@@ -15382,9 +15382,9 @@ function interpolate(xs, ys, method = "linear") {
15382
15382
  const n = xs.length - 1;
15383
15383
  const h = [];
15384
15384
  for (let i = 0; i < n; i++) h[i] = xs[i + 1] - xs[i];
15385
- const alpha = [0];
15385
+ const alpha2 = [0];
15386
15386
  for (let i = 1; i < n; i++) {
15387
- alpha[i] = 3 / h[i] * (ys[i + 1] - ys[i]) - 3 / h[i - 1] * (ys[i] - ys[i - 1]);
15387
+ alpha2[i] = 3 / h[i] * (ys[i + 1] - ys[i]) - 3 / h[i - 1] * (ys[i] - ys[i - 1]);
15388
15388
  }
15389
15389
  const l = [1];
15390
15390
  const mu = [0];
@@ -15392,7 +15392,7 @@ function interpolate(xs, ys, method = "linear") {
15392
15392
  for (let i = 1; i < n; i++) {
15393
15393
  l[i] = 2 * (xs[i + 1] - xs[i - 1]) - h[i - 1] * mu[i - 1];
15394
15394
  mu[i] = h[i] / l[i];
15395
- z[i] = (alpha[i] - h[i - 1] * z[i - 1]) / l[i];
15395
+ z[i] = (alpha2[i] - h[i - 1] * z[i - 1]) / l[i];
15396
15396
  }
15397
15397
  const c = new Array(n + 1).fill(0);
15398
15398
  const bArr = new Array(n);
@@ -15408,7 +15408,6 @@ function interpolate(xs, ys, method = "linear") {
15408
15408
  if (x <= xs[0]) j = 0;
15409
15409
  else if (x >= xs[n]) j = n - 1;
15410
15410
  else {
15411
- j = 0;
15412
15411
  for (j = 0; j < n; j++) {
15413
15412
  if (x >= xs[j] && x <= xs[j + 1]) break;
15414
15413
  }
@@ -15420,7 +15419,7 @@ function interpolate(xs, ys, method = "linear") {
15420
15419
  case "linear":
15421
15420
  default:
15422
15421
  return (x) => {
15423
- let i = 0;
15422
+ let i;
15424
15423
  if (x <= xs[0]) i = 0;
15425
15424
  else if (x >= xs[xs.length - 1]) i = xs.length - 2;
15426
15425
  else {
@@ -15460,7 +15459,7 @@ function pchip(xs, ys, x) {
15460
15459
  m[0] = delta[0];
15461
15460
  m[n - 1] = delta[n - 2];
15462
15461
  }
15463
- let idx = 0;
15462
+ let idx;
15464
15463
  if (x <= xs[0]) idx = 0;
15465
15464
  else if (x >= xs[n - 1]) idx = n - 2;
15466
15465
  else {
@@ -15537,9 +15536,9 @@ function bspline(controlPoints, degree3, t) {
15537
15536
  for (let r = 1; r <= p; r++) {
15538
15537
  for (let j = p; j >= r; j--) {
15539
15538
  const idx = k - p + j;
15540
- const alpha = (tClamped - knots[idx]) / (knots[idx + p - r + 1] - knots[idx]);
15539
+ const alpha2 = (tClamped - knots[idx]) / (knots[idx + p - r + 1] - knots[idx]);
15541
15540
  for (let dd = 0; dd < dim; dd++) {
15542
- d[j][dd] = (1 - alpha) * d[j - 1][dd] + alpha * d[j][dd];
15541
+ d[j][dd] = (1 - alpha2) * d[j - 1][dd] + alpha2 * d[j][dd];
15543
15542
  }
15544
15543
  }
15545
15544
  }
@@ -16737,11 +16736,11 @@ function linprog(c, arg22, arg3) {
16737
16736
  return linprogTwoPhase(c, arg22);
16738
16737
  }
16739
16738
  function solvePDE(pde, domain, bc) {
16740
- const { alpha } = pde;
16739
+ const { alpha: alpha2 } = pde;
16741
16740
  const { L, nx, nt, T } = domain;
16742
16741
  const dx = L / (nx - 1);
16743
16742
  const dt = T / nt;
16744
- const r = alpha * dt / (dx * dx);
16743
+ const r = alpha2 * dt / (dx * dx);
16745
16744
  if (r > 0.5) {
16746
16745
  }
16747
16746
  const x = [];
@@ -17751,11 +17750,11 @@ function normalSampleRng(rng) {
17751
17750
  const u2 = rng();
17752
17751
  return Math.sqrt(-2 * Math.log(u1 < 1e-300 ? 1e-300 : u1)) * Math.cos(2 * Math.PI * u2);
17753
17752
  }
17754
- function gammaSampleRng(alpha, rng) {
17755
- if (alpha < 1) {
17756
- return gammaSampleRng(alpha + 1, rng) * Math.pow(rng(), 1 / alpha);
17753
+ function gammaSampleRng(alpha2, rng) {
17754
+ if (alpha2 < 1) {
17755
+ return gammaSampleRng(alpha2 + 1, rng) * Math.pow(rng(), 1 / alpha2);
17757
17756
  }
17758
- const d = alpha - 1 / 3;
17757
+ const d = alpha2 - 1 / 3;
17759
17758
  const c = 1 / Math.sqrt(9 * d);
17760
17759
  for (; ; ) {
17761
17760
  let x;
@@ -17971,28 +17970,28 @@ function normalDist(mu = 0, sigma = 1) {
17971
17970
  })
17972
17971
  };
17973
17972
  }
17974
- function betaDist(alpha, beta_) {
17975
- if (alpha <= 0 || beta_ <= 0) throw new Error("betaDist: alpha and beta must be positive");
17976
- const B = _gamma(alpha) * _gamma(beta_) / _gamma(alpha + beta_);
17973
+ function betaDist(alpha2, beta_) {
17974
+ if (alpha2 <= 0 || beta_ <= 0) throw new Error("betaDist: alpha and beta must be positive");
17975
+ const B = _gamma(alpha2) * _gamma(beta_) / _gamma(alpha2 + beta_);
17977
17976
  return {
17978
17977
  pdf: (x) => {
17979
17978
  if (x < 0 || x > 1) return 0;
17980
- if (x === 0 && alpha < 1) return Infinity;
17979
+ if (x === 0 && alpha2 < 1) return Infinity;
17981
17980
  if (x === 1 && beta_ < 1) return Infinity;
17982
- return Math.pow(x, alpha - 1) * Math.pow(1 - x, beta_ - 1) / B;
17981
+ return Math.pow(x, alpha2 - 1) * Math.pow(1 - x, beta_ - 1) / B;
17983
17982
  },
17984
17983
  cdf: (x) => {
17985
17984
  if (x <= 0) return 0;
17986
17985
  if (x >= 1) return 1;
17987
- return _betainc(x, alpha, beta_);
17986
+ return _betainc(x, alpha2, beta_);
17988
17987
  },
17989
17988
  quantile: (p) => {
17990
17989
  if (p <= 0) return 0;
17991
17990
  if (p >= 1) return 1;
17992
- let x = alpha / (alpha + beta_);
17991
+ let x = alpha2 / (alpha2 + beta_);
17993
17992
  for (let i = 0; i < 100; i++) {
17994
- const fx = _betainc(x, alpha, beta_) - p;
17995
- const dx = Math.pow(x, alpha - 1) * Math.pow(1 - x, beta_ - 1) / B;
17993
+ const fx = _betainc(x, alpha2, beta_) - p;
17994
+ const dx = Math.pow(x, alpha2 - 1) * Math.pow(1 - x, beta_ - 1) / B;
17996
17995
  if (dx === 0) break;
17997
17996
  const step = fx / dx;
17998
17997
  x = Math.max(1e-15, Math.min(1 - 1e-15, x - step));
@@ -18000,17 +17999,17 @@ function betaDist(alpha, beta_) {
18000
17999
  }
18001
18000
  return x;
18002
18001
  },
18003
- mean: alpha / (alpha + beta_),
18004
- variance: alpha * beta_ / ((alpha + beta_) ** 2 * (alpha + beta_ + 1)),
18002
+ mean: alpha2 / (alpha2 + beta_),
18003
+ variance: alpha2 * beta_ / ((alpha2 + beta_) ** 2 * (alpha2 + beta_ + 1)),
18005
18004
  sample: () => {
18006
- const ga = _gammaRandom(alpha);
18005
+ const ga = _gammaRandom(alpha2);
18007
18006
  const gb = _gammaRandom(beta_);
18008
18007
  return ga / (ga + gb);
18009
18008
  },
18010
- sampleN: (n, opts) => _sampleNDispatch("beta", [alpha, beta_], n, opts, (count2, rng) => {
18009
+ sampleN: (n, opts) => _sampleNDispatch("beta", [alpha2, beta_], n, opts, (count2, rng) => {
18011
18010
  const out = new Float64Array(count2);
18012
18011
  for (let i = 0; i < count2; i++) {
18013
- const ga = _gammaRandomRng(alpha, rng);
18012
+ const ga = _gammaRandomRng(alpha2, rng);
18014
18013
  const gb = _gammaRandomRng(beta_, rng);
18015
18014
  out[i] = ga / (ga + gb);
18016
18015
  }
@@ -18018,14 +18017,14 @@ function betaDist(alpha, beta_) {
18018
18017
  })
18019
18018
  };
18020
18019
  }
18021
- function _gammaRandom(alpha) {
18022
- return gammaSampleRng(alpha, Math.random);
18020
+ function _gammaRandom(alpha2) {
18021
+ return gammaSampleRng(alpha2, Math.random);
18023
18022
  }
18024
18023
  function _normalRandom() {
18025
18024
  return normalSampleRng(Math.random);
18026
18025
  }
18027
- function _gammaRandomRng(alpha, rng) {
18028
- return gammaSampleRng(alpha, rng);
18026
+ function _gammaRandomRng(alpha2, rng) {
18027
+ return gammaSampleRng(alpha2, rng);
18029
18028
  }
18030
18029
  function binomialDist(n, p) {
18031
18030
  if (!Number.isInteger(n) || n < 0)
@@ -18175,7 +18174,7 @@ function fDist(d1, d2) {
18175
18174
  quantile: (p) => {
18176
18175
  if (p <= 0) return 0;
18177
18176
  if (p >= 1) return Infinity;
18178
- let x = d1 > 2 ? d2 * (d1 - 2) / (d1 * (d2 + 2)) : 1;
18177
+ let x;
18179
18178
  let lo = 0;
18180
18179
  let hi = 1e3;
18181
18180
  for (let i = 0; i < 100; i++) {
@@ -18640,18 +18639,18 @@ function discreteUniformDist(lo, hi) {
18640
18639
  })
18641
18640
  };
18642
18641
  }
18643
- function gumbelDist(mu, beta2) {
18644
- if (beta2 <= 0) throw new Error("gumbelDist: scale beta must be positive");
18645
- const quantile = (p) => p <= 0 ? -Infinity : p >= 1 ? Infinity : mu - beta2 * Math.log(-Math.log(p));
18642
+ function gumbelDist(mu, beta3) {
18643
+ if (beta3 <= 0) throw new Error("gumbelDist: scale beta must be positive");
18644
+ const quantile = (p) => p <= 0 ? -Infinity : p >= 1 ? Infinity : mu - beta3 * Math.log(-Math.log(p));
18646
18645
  return {
18647
18646
  pdf: (x) => {
18648
- const z = (x - mu) / beta2;
18649
- return 1 / beta2 * Math.exp(-(z + Math.exp(-z)));
18647
+ const z = (x - mu) / beta3;
18648
+ return 1 / beta3 * Math.exp(-(z + Math.exp(-z)));
18650
18649
  },
18651
- cdf: (x) => Math.exp(-Math.exp(-(x - mu) / beta2)),
18650
+ cdf: (x) => Math.exp(-Math.exp(-(x - mu) / beta3)),
18652
18651
  quantile,
18653
- mean: mu + beta2 * EULER_GAMMA,
18654
- variance: Math.PI * Math.PI / 6 * beta2 * beta2,
18652
+ mean: mu + beta3 * EULER_GAMMA,
18653
+ variance: Math.PI * Math.PI / 6 * beta3 * beta3,
18655
18654
  sample: () => _contSample(quantile, Math.random),
18656
18655
  sampleN: (n, opts) => _sampleNDispatch(null, [], n, opts, (count2, rng) => {
18657
18656
  const out = new Float64Array(count2);
@@ -19843,12 +19842,12 @@ function dagostinoTest(data) {
19843
19842
  if (n < 8) throw new Error("dagostinoTest: need at least 8 observations");
19844
19843
  const { skew, kurtPearson } = _skewKurt(data);
19845
19844
  const y = skew * Math.sqrt((n + 1) * (n + 3) / (6 * (n - 2)));
19846
- const beta2 = 3 * (n * n + 27 * n - 70) * (n + 1) * (n + 3) / ((n - 2) * (n + 5) * (n + 7) * (n + 9));
19847
- const w2 = -1 + Math.sqrt(2 * (beta2 - 1));
19845
+ const beta22 = 3 * (n * n + 27 * n - 70) * (n + 1) * (n + 3) / ((n - 2) * (n + 5) * (n + 7) * (n + 9));
19846
+ const w2 = -1 + Math.sqrt(2 * (beta22 - 1));
19848
19847
  const delta = 1 / Math.sqrt(0.5 * Math.log(w2));
19849
- const alpha = Math.sqrt(2 / (w2 - 1));
19848
+ const alpha2 = Math.sqrt(2 / (w2 - 1));
19850
19849
  const yy = y === 0 ? 1 : y;
19851
- const z1 = delta * Math.log(yy / alpha + Math.sqrt(yy / alpha * (yy / alpha) + 1));
19850
+ const z1 = delta * Math.log(yy / alpha2 + Math.sqrt(yy / alpha2 * (yy / alpha2) + 1));
19852
19851
  const E = 3 * (n - 1) / (n + 1);
19853
19852
  const varb2 = 24 * n * (n - 2) * (n - 3) / ((n + 1) * (n + 1) * (n + 3) * (n + 5));
19854
19853
  const xx = (kurtPearson - E) / Math.sqrt(varb2);
@@ -20234,14 +20233,14 @@ function hessenbergForm(A) {
20234
20233
  let normV = 0;
20235
20234
  for (let i = 0; i < len; i++) normV += v[i] * v[i];
20236
20235
  if (normV < 1e-30) continue;
20237
- const beta2 = 2 / normV;
20236
+ const beta3 = 2 / normV;
20238
20237
  for (let j = k; j < n; j++) {
20239
20238
  let dot9 = 0;
20240
20239
  for (let i = 0; i < len; i++) {
20241
20240
  dot9 += v[i] * H[k + 1 + i][j];
20242
20241
  }
20243
20242
  for (let i = 0; i < len; i++) {
20244
- H[k + 1 + i][j] -= beta2 * v[i] * dot9;
20243
+ H[k + 1 + i][j] -= beta3 * v[i] * dot9;
20245
20244
  }
20246
20245
  }
20247
20246
  for (let i = 0; i < n; i++) {
@@ -20250,7 +20249,7 @@ function hessenbergForm(A) {
20250
20249
  dot9 += H[i][k + 1 + j] * v[j];
20251
20250
  }
20252
20251
  for (let j = 0; j < len; j++) {
20253
- H[i][k + 1 + j] -= beta2 * dot9 * v[j];
20252
+ H[i][k + 1 + j] -= beta3 * dot9 * v[j];
20254
20253
  }
20255
20254
  }
20256
20255
  for (let i = 0; i < n; i++) {
@@ -20259,7 +20258,7 @@ function hessenbergForm(A) {
20259
20258
  dot9 += Q2[i][k + 1 + j] * v[j];
20260
20259
  }
20261
20260
  for (let j = 0; j < len; j++) {
20262
- Q2[i][k + 1 + j] -= beta2 * dot9 * v[j];
20261
+ Q2[i][k + 1 + j] -= beta3 * dot9 * v[j];
20263
20262
  }
20264
20263
  }
20265
20264
  }
@@ -21726,6 +21725,7 @@ __export(factories_exports, {
21726
21725
  solveODE: () => solveODE,
21727
21726
  sort: () => sort,
21728
21727
  sparse: () => sparse,
21728
+ spectralRadiance: () => spectralRadiance,
21729
21729
  speedOfLight: () => speedOfLight,
21730
21730
  splitUnit: () => splitUnit,
21731
21731
  sqrtm: () => sqrtm,
@@ -24400,7 +24400,7 @@ var createIsPrime = /* @__PURE__ */ factory(
24400
24400
  d = d.div(2);
24401
24401
  r += 1;
24402
24402
  }
24403
- let bases = null;
24403
+ let bases;
24404
24404
  if (n.toBigInt() < 3317044064679887385961981n) {
24405
24405
  bases = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41].filter((x) => x < n.toNumber());
24406
24406
  } else {
@@ -26707,7 +26707,7 @@ var createCsAmd = /* @__PURE__ */ factory(
26707
26707
  // src/algebra/sparse/csLeaf.ts
26708
26708
  function csLeaf(i, j, w, first, maxfirst, prevleaf, ancestor) {
26709
26709
  let s, sparent;
26710
- let jleaf = 0;
26710
+ let jleaf;
26711
26711
  let q;
26712
26712
  if (i <= j || w[first + j] <= w[maxfirst + i]) {
26713
26713
  return -1;
@@ -31304,7 +31304,8 @@ var createPow = /* @__PURE__ */ factory(
31304
31304
  } catch (error) {
31305
31305
  if (error instanceof Error && error.message === "Cannot calculate inverse, determinant is zero") {
31306
31306
  throw new TypeError(
31307
- "For A^b, when A is not invertible, b must be a positive integer (value is " + y + ")"
31307
+ "For A^b, when A is not invertible, b must be a positive integer (value is " + y + ")",
31308
+ { cause: error }
31308
31309
  );
31309
31310
  }
31310
31311
  throw error;
@@ -33172,14 +33173,14 @@ var createQr = /* @__PURE__ */ factory(
33172
33173
  for (i = k; i < rows; i++) {
33173
33174
  alphaSquared = addScalar2(alphaSquared, multiplyScalar2(Rdata[i][k], conj3(Rdata[i][k])));
33174
33175
  }
33175
- const alpha = multiplyScalar2(sgn, sqrt2(alphaSquared));
33176
- if (!isZero4(alpha)) {
33177
- const u1 = subtractScalar2(pivot, alpha);
33176
+ const alpha2 = multiplyScalar2(sgn, sqrt2(alphaSquared));
33177
+ if (!isZero4(alpha2)) {
33178
+ const u1 = subtractScalar2(pivot, alpha2);
33178
33179
  w[k] = 1;
33179
33180
  for (i = k + 1; i < rows; i++) {
33180
33181
  w[i] = divideScalar2(Rdata[i][k], u1);
33181
33182
  }
33182
- const tau2 = unaryMinus2(conj3(divideScalar2(u1, alpha)));
33183
+ const tau2 = unaryMinus2(conj3(divideScalar2(u1, alpha2)));
33183
33184
  let s;
33184
33185
  for (j = k; j < cols; j++) {
33185
33186
  s = 0;
@@ -35763,7 +35764,16 @@ var createSubtract = /* @__PURE__ */ factory(
35763
35764
  // =========================================================================
35764
35765
  // EXISTING SIGNATURES - Keep after Node signatures
35765
35766
  // =========================================================================
35766
- "any, any": subtractScalar2
35767
+ "any, any": subtractScalar2,
35768
+ "any, any, ...any": typed3.referToSelf(
35769
+ (self) => (x, y, ...rest) => {
35770
+ let res = self(x, y);
35771
+ for (let i = 0; i < rest.length; i++) {
35772
+ res = self(res, rest[i]);
35773
+ }
35774
+ return res;
35775
+ }
35776
+ )
35767
35777
  },
35768
35778
  matrixAlgorithmSuite({
35769
35779
  elop: subtractScalar2,
@@ -37581,12 +37591,10 @@ var createCsLu = /* @__PURE__ */ factory(
37581
37591
  const size2 = m._size;
37582
37592
  const n = size2[1];
37583
37593
  let q;
37584
- let lnz = 100;
37585
- let unz = 100;
37594
+ let lnz;
37595
+ let unz;
37586
37596
  if (s) {
37587
37597
  q = s.q;
37588
- lnz = s.lnz || lnz;
37589
- unz = s.unz || unz;
37590
37598
  }
37591
37599
  const lvalues = [];
37592
37600
  const lindex = [];
@@ -39124,7 +39132,7 @@ var createSetPowerset = /* @__PURE__ */ factory(
39124
39132
  return result;
39125
39133
  }
39126
39134
  function _sort(array) {
39127
- let temp = [];
39135
+ let temp;
39128
39136
  for (let i = array.length - 1; i > 0; i--) {
39129
39137
  for (let j = 0; j < i; j++) {
39130
39138
  if (array[j].length > array[j + 1].length) {
@@ -41373,7 +41381,7 @@ var createMad = /* @__PURE__ */ factory(
41373
41381
  );
41374
41382
  } catch (err) {
41375
41383
  if (err instanceof TypeError && err.message.includes("median")) {
41376
- throw new TypeError(err.message.replace("median", "mad"));
41384
+ throw new TypeError(err.message.replace("median", "mad"), { cause: err });
41377
41385
  } else {
41378
41386
  throw improveErrorMessage(err, "mad", void 0);
41379
41387
  }
@@ -41417,7 +41425,7 @@ var createStd = /* @__PURE__ */ factory(
41417
41425
  }
41418
41426
  } catch (err) {
41419
41427
  if (err instanceof TypeError && err.message.includes(" variance")) {
41420
- throw new TypeError(err.message.replace(" variance", " std"));
41428
+ throw new TypeError(err.message.replace(" variance", " std"), { cause: err });
41421
41429
  } else {
41422
41430
  throw err;
41423
41431
  }
@@ -44129,6 +44137,11 @@ var createFirstRadiation = /* @__PURE__ */ unitFactory(
44129
44137
  "3.7417718521927573e-16",
44130
44138
  "W m^2"
44131
44139
  );
44140
+ var createSpectralRadiance = /* @__PURE__ */ unitFactory(
44141
+ "spectralRadiance",
44142
+ "1.1910429723971881e-16",
44143
+ "W m^2 sr^-1"
44144
+ );
44132
44145
  var createLoschmidt = /* @__PURE__ */ unitFactory(
44133
44146
  "loschmidt",
44134
44147
  "2.686780111798444e25",
@@ -44979,6 +44992,9 @@ var stefanBoltzmann = createStefanBoltzmann(
44979
44992
  var firstRadiation = createFirstRadiation(
44980
44993
  factoryScope
44981
44994
  );
44995
+ var spectralRadiance = createSpectralRadiance(
44996
+ factoryScope
44997
+ );
44982
44998
  var secondRadiation = createSecondRadiation(
44983
44999
  factoryScope
44984
45000
  );
@@ -46006,16 +46022,16 @@ function groebnerBasis(polys, vars) {
46006
46022
  return basis.map((b) => polyToString(b, vars));
46007
46023
  }
46008
46024
  function minimalPolynomial(expr, varName) {
46009
- const alpha = evaluate(expr);
46010
- if (!isFinite(alpha)) return null;
46025
+ const alpha2 = evaluate(expr);
46026
+ if (!isFinite(alpha2)) return null;
46011
46027
  for (let den = 1; den <= 100; den++) {
46012
- const num2 = Math.round(alpha * den);
46013
- if (Math.abs(alpha - num2 / den) < 1e-12) {
46028
+ const num2 = Math.round(alpha2 * den);
46029
+ if (Math.abs(alpha2 - num2 / den) < 1e-12) {
46014
46030
  if (den === 1) return `${varName} - ${num2}`;
46015
46031
  return `${den}*${varName} - ${num2}`;
46016
46032
  }
46017
46033
  }
46018
- const powers = [1, alpha, alpha * alpha, alpha * alpha * alpha, alpha ** 4];
46034
+ const powers = [1, alpha2, alpha2 * alpha2, alpha2 * alpha2 * alpha2, alpha2 ** 4];
46019
46035
  for (let deg = 2; deg <= 6; deg++) {
46020
46036
  const maxCoeff = 20;
46021
46037
  const found = findIntegerRelation(powers.slice(0, deg + 1), maxCoeff);
@@ -46534,7 +46550,7 @@ function iltSimplifyNode(node) {
46534
46550
  return node;
46535
46551
  }
46536
46552
  function _inverseLaplaceNode(node, sVar, tVar) {
46537
- let simplified = node;
46553
+ let simplified;
46538
46554
  try {
46539
46555
  simplified = iltSimplifyNode(node);
46540
46556
  } catch {
@@ -47592,7 +47608,7 @@ function studentizedRangeQuantile(p, k, df) {
47592
47608
  }
47593
47609
  return (lo + hi) / 2;
47594
47610
  }
47595
- function tukeyHSD(groups, alpha = 0.05) {
47611
+ function tukeyHSD(groups, alpha2 = 0.05) {
47596
47612
  const gs = groups.map(arr2);
47597
47613
  const k = gs.length;
47598
47614
  if (k < 2) throw new Error("tukeyHSD: need at least 2 groups");
@@ -47603,7 +47619,7 @@ function tukeyHSD(groups, alpha = 0.05) {
47603
47619
  throw new Error("tukeyHSD: residual df must be > 0 (groups must hold more observations than groups)");
47604
47620
  const sse = gs.reduce((s, g) => s + sampleVar(g) * (g.length - 1), 0);
47605
47621
  const mse = sse / dfErr;
47606
- const qCrit = studentizedRangeQuantile(1 - alpha, k, dfErr);
47622
+ const qCrit = studentizedRangeQuantile(1 - alpha2, k, dfErr);
47607
47623
  const out = [];
47608
47624
  for (let i = 0; i < k; i++) {
47609
47625
  for (let j = i + 1; j < k; j++) {
@@ -47894,7 +47910,7 @@ function fsolve(F, x0, opts = {}) {
47894
47910
  delta = linsolve(J, rhs);
47895
47911
  } catch (err) {
47896
47912
  const msg = err instanceof Error ? err.message : String(err);
47897
- throw new Error(`fsolve: singular Jacobian at iteration ${iter} (${msg})`);
47913
+ throw new Error(`fsolve: singular Jacobian at iteration ${iter} (${msg})`, { cause: err });
47898
47914
  }
47899
47915
  const residualNorm2 = norm24(Fx);
47900
47916
  let xNext = x.map((xi, i) => xi + delta[i]);
@@ -47947,14 +47963,14 @@ function dot2(a, b) {
47947
47963
  function norm25(a) {
47948
47964
  return Math.sqrt(dot2(a, a));
47949
47965
  }
47950
- function axpy(alpha, x, y) {
47966
+ function axpy(alpha2, x, y) {
47951
47967
  const n = x.length;
47952
47968
  const out = new Array(n);
47953
- for (let i = 0; i < n; i++) out[i] = alpha * x[i] + y[i];
47969
+ for (let i = 0; i < n; i++) out[i] = alpha2 * x[i] + y[i];
47954
47970
  return out;
47955
47971
  }
47956
- function scale(alpha, x) {
47957
- return x.map((v) => alpha * v);
47972
+ function scale(alpha2, x) {
47973
+ return x.map((v) => alpha2 * v);
47958
47974
  }
47959
47975
  function subtract2(a, b) {
47960
47976
  return a.map((v, i) => v - b[i]);
@@ -48118,17 +48134,17 @@ function cg(a, b, opts) {
48118
48134
  const Ap = matvec2(p);
48119
48135
  const pAp = dot2(p, Ap);
48120
48136
  if (Math.abs(pAp) < 1e-300) break;
48121
- const alpha = rzOld / pAp;
48122
- x = axpy(alpha, p, x);
48123
- r = axpy(-alpha, Ap, r);
48137
+ const alpha2 = rzOld / pAp;
48138
+ x = axpy(alpha2, p, x);
48139
+ r = axpy(-alpha2, Ap, r);
48124
48140
  residual = relativeResidualNorm(norm25(r), bNorm);
48125
48141
  if (residual < tol) {
48126
48142
  return { x, iterations, converged: true, residual };
48127
48143
  }
48128
48144
  z = applyM(r);
48129
48145
  const rzNew = dot2(r, z);
48130
- const beta2 = rzNew / rzOld;
48131
- p = add3(z, scale(beta2, p));
48146
+ const beta3 = rzNew / rzOld;
48147
+ p = add3(z, scale(beta3, p));
48132
48148
  rzOld = rzNew;
48133
48149
  }
48134
48150
  return { x, iterations, converged: residual < tol, residual };
@@ -48149,7 +48165,7 @@ function minres(a, b, opts) {
48149
48165
  return { x, iterations: 0, converged: residual0 < tol, residual: residual0 };
48150
48166
  }
48151
48167
  let oldb = 0;
48152
- let beta2 = beta1;
48168
+ let beta3 = beta1;
48153
48169
  let dbar = 0;
48154
48170
  let epsln = 0;
48155
48171
  let phibar = beta1;
@@ -48161,31 +48177,31 @@ function minres(a, b, opts) {
48161
48177
  let iterations = 0;
48162
48178
  for (let iter = 1; iter <= maxIter; iter++) {
48163
48179
  iterations = iter;
48164
- const v = scale(1 / beta2, y);
48180
+ const v = scale(1 / beta3, y);
48165
48181
  y = matvec2(v);
48166
- if (iter >= 2) y = axpy(-beta2 / oldb, r1, y);
48182
+ if (iter >= 2) y = axpy(-beta3 / oldb, r1, y);
48167
48183
  const alfa = dot2(v, y);
48168
- y = axpy(-alfa / beta2, r2, y);
48184
+ y = axpy(-alfa / beta3, r2, y);
48169
48185
  r1 = r2;
48170
48186
  r2 = y;
48171
48187
  y = applyM(r2);
48172
- oldb = beta2;
48173
- beta2 = Math.sqrt(Math.max(0, dot2(r2, y)));
48188
+ oldb = beta3;
48189
+ beta3 = Math.sqrt(Math.max(0, dot2(r2, y)));
48174
48190
  const oldeps = epsln;
48175
48191
  const delta = cs * dbar + sn * alfa;
48176
48192
  const gbar = sn * dbar - cs * alfa;
48177
- epsln = sn * beta2;
48178
- dbar = -cs * beta2;
48179
- const gamma2 = Math.max(Math.sqrt(gbar * gbar + beta2 * beta2), 1e-300);
48193
+ epsln = sn * beta3;
48194
+ dbar = -cs * beta3;
48195
+ const gamma2 = Math.max(Math.sqrt(gbar * gbar + beta3 * beta3), 1e-300);
48180
48196
  cs = gbar / gamma2;
48181
- sn = beta2 / gamma2;
48197
+ sn = beta3 / gamma2;
48182
48198
  const phi = cs * phibar;
48183
48199
  phibar = sn * phibar;
48184
48200
  const w1 = w2;
48185
48201
  w2 = w;
48186
48202
  w = scale(1 / gamma2, subtract2(subtract2(v, scale(oldeps, w1)), scale(delta, w2)));
48187
48203
  for (let i = 0; i < n; i++) x[i] += phi * w[i];
48188
- if (phibar / beta1 < tol || beta2 < 1e-300) break;
48204
+ if (phibar / beta1 < tol || beta3 < 1e-300) break;
48189
48205
  }
48190
48206
  const residual = relativeResidual(matvec2, x, b, bNorm);
48191
48207
  return { x, iterations, converged: residual < tol, residual };
@@ -48206,15 +48222,15 @@ function gmres(a, b, opts) {
48206
48222
  }
48207
48223
  while (totalIterations < maxIter) {
48208
48224
  const r0 = applyM(subtract2(b, matvec2(x)));
48209
- const beta2 = norm25(r0);
48210
- if (beta2 < 1e-300) {
48225
+ const beta3 = norm25(r0);
48226
+ if (beta3 < 1e-300) {
48211
48227
  return { x, iterations: totalIterations, converged: residual < tol, residual };
48212
48228
  }
48213
48229
  const m = Math.min(restart, maxIter - totalIterations);
48214
- const V = [scale(1 / beta2, r0)];
48230
+ const V = [scale(1 / beta3, r0)];
48215
48231
  const H = Array.from({ length: m + 1 }, () => new Array(m).fill(0));
48216
48232
  const g = new Array(m + 1).fill(0);
48217
- g[0] = beta2;
48233
+ g[0] = beta3;
48218
48234
  const cs = new Array(m).fill(0);
48219
48235
  const sn = new Array(m).fill(0);
48220
48236
  let k = 0;
@@ -48282,7 +48298,7 @@ function bicgstab(a, b, opts) {
48282
48298
  return { x, iterations: 0, converged: true, residual };
48283
48299
  }
48284
48300
  let rho = 1;
48285
- let alpha = 1;
48301
+ let alpha2 = 1;
48286
48302
  let omega = 1;
48287
48303
  let v = zeros2(n);
48288
48304
  let p = zeros2(n);
@@ -48294,19 +48310,19 @@ function bicgstab(a, b, opts) {
48294
48310
  if (iter === 1) {
48295
48311
  p = r.slice();
48296
48312
  } else {
48297
- const beta2 = rhoNew / rho * (alpha / omega);
48298
- p = add3(r, scale(beta2, subtract2(p, scale(omega, v))));
48313
+ const beta3 = rhoNew / rho * (alpha2 / omega);
48314
+ p = add3(r, scale(beta3, subtract2(p, scale(omega, v))));
48299
48315
  }
48300
48316
  rho = rhoNew;
48301
48317
  const pHat = applyM(p);
48302
48318
  v = matvec2(pHat);
48303
48319
  const rHatV = dot2(rHat0, v);
48304
48320
  if (Math.abs(rHatV) < 1e-300) break;
48305
- alpha = rho / rHatV;
48306
- const s = axpy(-alpha, v, r);
48321
+ alpha2 = rho / rHatV;
48322
+ const s = axpy(-alpha2, v, r);
48307
48323
  const sNorm = relativeResidualNorm(norm25(s), bNorm);
48308
48324
  if (sNorm < tol) {
48309
- x = axpy(alpha, pHat, x);
48325
+ x = axpy(alpha2, pHat, x);
48310
48326
  residual = sNorm;
48311
48327
  return { x, iterations, converged: true, residual };
48312
48328
  }
@@ -48314,7 +48330,7 @@ function bicgstab(a, b, opts) {
48314
48330
  const t = matvec2(sHat);
48315
48331
  const tt = dot2(t, t);
48316
48332
  omega = tt > 1e-300 ? dot2(t, s) / tt : 0;
48317
- x = axpy(alpha, pHat, x);
48333
+ x = axpy(alpha2, pHat, x);
48318
48334
  x = axpy(omega, sHat, x);
48319
48335
  r = axpy(-omega, t, s);
48320
48336
  residual = relativeResidualNorm(norm25(r), bNorm);
@@ -48357,13 +48373,13 @@ function dot3(a, b) {
48357
48373
  function norm26(a) {
48358
48374
  return Math.sqrt(dot3(a, a));
48359
48375
  }
48360
- function axpy2(alpha, x, y) {
48376
+ function axpy2(alpha2, x, y) {
48361
48377
  const out = new Array(x.length);
48362
- for (let i = 0; i < x.length; i++) out[i] = alpha * x[i] + y[i];
48378
+ for (let i = 0; i < x.length; i++) out[i] = alpha2 * x[i] + y[i];
48363
48379
  return out;
48364
48380
  }
48365
- function scale2(alpha, x) {
48366
- return x.map((v) => alpha * v);
48381
+ function scale2(alpha2, x) {
48382
+ return x.map((v) => alpha2 * v);
48367
48383
  }
48368
48384
  function startVector(n) {
48369
48385
  const v = new Array(n);
@@ -48379,19 +48395,19 @@ function lanczos(matvec2, n, m) {
48379
48395
  const vj = V[j];
48380
48396
  let w = matvec2(vj);
48381
48397
  if (j > 0) w = axpy2(-betas[j - 1], V[j - 1], w);
48382
- const alpha = dot3(w, vj);
48383
- alphas.push(alpha);
48384
- w = axpy2(-alpha, vj, w);
48398
+ const alpha2 = dot3(w, vj);
48399
+ alphas.push(alpha2);
48400
+ w = axpy2(-alpha2, vj, w);
48385
48401
  for (let pass = 0; pass < 2; pass++) {
48386
48402
  for (let i = 0; i <= j; i++) {
48387
48403
  const c = dot3(w, V[i]);
48388
48404
  w = axpy2(-c, V[i], w);
48389
48405
  }
48390
48406
  }
48391
- const beta2 = norm26(w);
48392
- if (j === m - 1 || beta2 < 1e-13) break;
48393
- betas.push(beta2);
48394
- V.push(scale2(1 / beta2, w));
48407
+ const beta3 = norm26(w);
48408
+ if (j === m - 1 || beta3 < 1e-13) break;
48409
+ betas.push(beta3);
48410
+ V.push(scale2(1 / beta3, w));
48395
48411
  }
48396
48412
  return { V, alphas, betas };
48397
48413
  }
@@ -48695,7 +48711,7 @@ function ldl(A) {
48695
48711
  for (let i = 0; i < n; i++) [W[i][a], W[i][b]] = [W[i][b], W[i][a]];
48696
48712
  [L[a], L[b]] = [L[b], L[a]];
48697
48713
  };
48698
- const alpha = (1 + Math.sqrt(17)) / 8;
48714
+ const alpha2 = (1 + Math.sqrt(17)) / 8;
48699
48715
  let k = 0;
48700
48716
  while (k < n) {
48701
48717
  if (k === n - 1) {
@@ -48716,13 +48732,13 @@ function ldl(A) {
48716
48732
  }
48717
48733
  let use1x1 = true;
48718
48734
  let swapWith = -1;
48719
- if (lambda !== 0 && w1 < alpha * lambda) {
48735
+ if (lambda !== 0 && w1 < alpha2 * lambda) {
48720
48736
  let sigma = 0;
48721
48737
  for (let i = k; i < n; i++) {
48722
48738
  if (i !== r) sigma = Math.max(sigma, Math.abs(W[i][r]));
48723
48739
  }
48724
- if (w1 * sigma < alpha * lambda * lambda) {
48725
- if (Math.abs(W[r][r]) >= alpha * sigma) {
48740
+ if (w1 * sigma < alpha2 * lambda * lambda) {
48741
+ if (Math.abs(W[r][r]) >= alpha2 * sigma) {
48726
48742
  swapWith = r;
48727
48743
  } else {
48728
48744
  use1x1 = false;
@@ -49636,15 +49652,15 @@ function movingAverage(x, w) {
49636
49652
  }
49637
49653
  return out;
49638
49654
  }
49639
- function ewma(x, alpha) {
49655
+ function ewma(x, alpha2) {
49640
49656
  const a = arr4(x);
49641
- if (alpha <= 0 || alpha > 1) throw new Error("ewma: alpha must be in (0, 1]");
49657
+ if (alpha2 <= 0 || alpha2 > 1) throw new Error("ewma: alpha must be in (0, 1]");
49642
49658
  if (a.length === 0) throw new Error("ewma: input series is empty");
49643
49659
  const out = new Array(a.length);
49644
49660
  let prev = a[0];
49645
49661
  out[0] = prev;
49646
49662
  for (let i = 1; i < a.length; i++) {
49647
- prev = alpha * a[i] + (1 - alpha) * prev;
49663
+ prev = alpha2 * a[i] + (1 - alpha2) * prev;
49648
49664
  out[i] = prev;
49649
49665
  }
49650
49666
  return out;
@@ -49836,8 +49852,8 @@ var MACKINNON_C = {
49836
49852
  10: { betaInf: -2.5671, beta1: -1.438, beta2: -4.48 }
49837
49853
  };
49838
49854
  function adfCriticalValue(pct, n) {
49839
- const { betaInf, beta1, beta2 } = MACKINNON_C[pct];
49840
- return betaInf + beta1 / n + beta2 / (n * n);
49855
+ const { betaInf, beta1, beta2: beta22 } = MACKINNON_C[pct];
49856
+ return betaInf + beta1 / n + beta22 / (n * n);
49841
49857
  }
49842
49858
  function adfPValue(statistic, n) {
49843
49859
  const cv1 = adfCriticalValue(1, n);
@@ -49964,9 +49980,9 @@ function softThreshold(a, lambda) {
49964
49980
  if (a < -lambda) return a + lambda;
49965
49981
  return 0;
49966
49982
  }
49967
- function ridge(X, y, alpha, opts) {
49983
+ function ridge(X, y, alpha2, opts) {
49968
49984
  const { n, p } = validate2(X, y, "ridge");
49969
- if (alpha < 0) throw new Error("ridge: alpha must be non-negative");
49985
+ if (alpha2 < 0) throw new Error("ridge: alpha must be non-negative");
49970
49986
  const useIntercept = opts?.intercept !== false;
49971
49987
  const { Xc, yc, xMean, yMean } = center(X, y, n, p, useIntercept);
49972
49988
  const XtX = Array.from({ length: p }, () => new Array(p).fill(0));
@@ -49978,7 +49994,7 @@ function ridge(X, y, alpha, opts) {
49978
49994
  XtX[j][k] = s;
49979
49995
  XtX[k][j] = s;
49980
49996
  }
49981
- XtX[j][j] += alpha;
49997
+ XtX[j][j] += alpha2;
49982
49998
  for (let i = 0; i < n; i++) Xty[j] += Xc[i][j] * yc[i];
49983
49999
  }
49984
50000
  const coefficients = linsolve(XtX, Xty);
@@ -49997,52 +50013,52 @@ function standardize(Xc, n, p) {
49997
50013
  return { Xs, scale: scale4 };
49998
50014
  }
49999
50015
  function coordinateDescent(Xs, yc, n, p, l1, l2, maxIter, tol) {
50000
- const beta2 = new Array(p).fill(0);
50016
+ const beta3 = new Array(p).fill(0);
50001
50017
  const z = new Array(p).fill(n);
50002
50018
  const residual = [...yc];
50003
50019
  for (let iter = 0; iter < maxIter; iter++) {
50004
50020
  let maxChange = 0;
50005
50021
  for (let j = 0; j < p; j++) {
50006
50022
  const col = Xs.map((row2) => row2[j]);
50007
- const oldBeta = beta2[j];
50023
+ const oldBeta = beta3[j];
50008
50024
  let rho = 0;
50009
50025
  for (let i = 0; i < n; i++) rho += col[i] * (residual[i] + col[i] * oldBeta);
50010
50026
  const newBeta = softThreshold(rho, l1) / (z[j] + l2);
50011
50027
  if (newBeta !== oldBeta) {
50012
50028
  const delta = newBeta - oldBeta;
50013
50029
  for (let i = 0; i < n; i++) residual[i] -= col[i] * delta;
50014
- beta2[j] = newBeta;
50030
+ beta3[j] = newBeta;
50015
50031
  maxChange = Math.max(maxChange, Math.abs(delta));
50016
50032
  }
50017
50033
  }
50018
50034
  if (maxChange < tol) break;
50019
50035
  }
50020
- return beta2;
50036
+ return beta3;
50021
50037
  }
50022
- function lasso(X, y, alpha, opts) {
50038
+ function lasso(X, y, alpha2, opts) {
50023
50039
  const { n, p } = validate2(X, y, "lasso");
50024
- if (alpha < 0) throw new Error("lasso: alpha must be non-negative");
50040
+ if (alpha2 < 0) throw new Error("lasso: alpha must be non-negative");
50025
50041
  const useIntercept = opts?.intercept !== false;
50026
50042
  const maxIter = opts?.maxIter ?? 1e3;
50027
50043
  const tol = opts?.tol ?? 1e-7;
50028
50044
  const { Xc, yc, xMean, yMean } = center(X, y, n, p, useIntercept);
50029
50045
  const { Xs, scale: scale4 } = standardize(Xc, n, p);
50030
- const betaStd = coordinateDescent(Xs, yc, n, p, alpha, 0, maxIter, tol);
50046
+ const betaStd = coordinateDescent(Xs, yc, n, p, alpha2, 0, maxIter, tol);
50031
50047
  const coefficients = betaStd.map((b, j) => b / scale4[j]);
50032
50048
  const intercept = useIntercept ? yMean - dot4(xMean, coefficients) : 0;
50033
50049
  return { coefficients, intercept };
50034
50050
  }
50035
- function elasticNet(X, y, alpha, l1Ratio, opts) {
50051
+ function elasticNet(X, y, alpha2, l1Ratio, opts) {
50036
50052
  const { n, p } = validate2(X, y, "elasticNet");
50037
- if (alpha < 0) throw new Error("elasticNet: alpha must be non-negative");
50053
+ if (alpha2 < 0) throw new Error("elasticNet: alpha must be non-negative");
50038
50054
  if (l1Ratio < 0 || l1Ratio > 1) throw new Error("elasticNet: l1Ratio must be in [0, 1]");
50039
50055
  const useIntercept = opts?.intercept !== false;
50040
50056
  const maxIter = opts?.maxIter ?? 1e3;
50041
50057
  const tol = opts?.tol ?? 1e-7;
50042
50058
  const { Xc, yc, xMean, yMean } = center(X, y, n, p, useIntercept);
50043
50059
  const { Xs, scale: scale4 } = standardize(Xc, n, p);
50044
- const l1 = alpha * l1Ratio;
50045
- const l2 = alpha * (1 - l1Ratio);
50060
+ const l1 = alpha2 * l1Ratio;
50061
+ const l2 = alpha2 * (1 - l1Ratio);
50046
50062
  const betaStd = coordinateDescent(Xs, yc, n, p, l1, l2, maxIter, tol);
50047
50063
  const coefficients = betaStd.map((b, j) => b / scale4[j]);
50048
50064
  const intercept = useIntercept ? yMean - dot4(xMean, coefficients) : 0;
@@ -50076,9 +50092,9 @@ function logisticRegression(X, y, opts) {
50076
50092
  const design = (rows) => useIntercept ? rows.map((row2) => [1, ...row2]) : rows.map((row2) => [...row2]);
50077
50093
  const D = design(X);
50078
50094
  const p = D[0].length;
50079
- let beta2 = new Array(p).fill(0);
50095
+ let beta3 = new Array(p).fill(0);
50080
50096
  for (let iter = 0; iter < maxIter; iter++) {
50081
- const eta = D.map((row2) => row2.reduce((s, v, j) => s + v * beta2[j], 0));
50097
+ const eta = D.map((row2) => row2.reduce((s, v, j) => s + v * beta3[j], 0));
50082
50098
  const probs = eta.map(sigmoid2);
50083
50099
  const weights = probs.map((pi) => Math.max(pi * (1 - pi), 1e-9));
50084
50100
  const XtWX = Array.from({ length: p }, () => new Array(p).fill(0));
@@ -50116,14 +50132,14 @@ function logisticRegression(X, y, opts) {
50116
50132
  delta = delta.map((d) => d * scale4);
50117
50133
  stepNorm = MAX_STEP_NORM;
50118
50134
  }
50119
- beta2 = beta2.map((b, j) => b + delta[j]);
50135
+ beta3 = beta3.map((b, j) => b + delta[j]);
50120
50136
  if (stepNorm < tol) break;
50121
50137
  }
50122
- const intercept = useIntercept ? beta2[0] : 0;
50123
- const coefficients = useIntercept ? beta2.slice(1) : beta2.slice();
50138
+ const intercept = useIntercept ? beta3[0] : 0;
50139
+ const coefficients = useIntercept ? beta3.slice(1) : beta3.slice();
50124
50140
  const predictProba = (x) => {
50125
50141
  const Dx = design(x);
50126
- return Dx.map((row2) => sigmoid2(row2.reduce((s, v, j) => s + v * beta2[j], 0)));
50142
+ return Dx.map((row2) => sigmoid2(row2.reduce((s, v, j) => s + v * beta3[j], 0)));
50127
50143
  };
50128
50144
  const predict = (x) => predictProba(x).map((pi) => pi >= 0.5 ? 1 : 0);
50129
50145
  return { coefficients, intercept, predictProba, predict };
@@ -50134,7 +50150,7 @@ var _inv2 = inv;
50134
50150
  function nelderMead(f, x0, opts = {}) {
50135
50151
  const { maxIter = 2e3, tol = 1e-8, step = 0.05 } = opts;
50136
50152
  const n = x0.length;
50137
- const alpha = 1;
50153
+ const alpha2 = 1;
50138
50154
  const gamma2 = 2;
50139
50155
  const rho = 0.5;
50140
50156
  const sigma = 0.5;
@@ -50159,7 +50175,7 @@ function nelderMead(f, x0, opts = {}) {
50159
50175
  fv = order.map((i) => fv[i]);
50160
50176
  if (Math.abs(fv[n] - fv[0]) <= tol * (Math.abs(fv[0]) + tol)) break;
50161
50177
  const c = centroid2(simplex.slice(0, n));
50162
- const xr = add5(c, sub4(c, simplex[n]), alpha);
50178
+ const xr = add5(c, sub4(c, simplex[n]), alpha2);
50163
50179
  const fr = f(xr);
50164
50180
  if (fr < fv[0]) {
50165
50181
  const xe = add5(c, sub4(c, simplex[n]), gamma2);
@@ -50659,17 +50675,17 @@ function bfgs(f, x0, opts = {}) {
50659
50675
  const gd = dot5(g, d);
50660
50676
  const dir = gd < 0 ? d : g.map((v) => -v);
50661
50677
  const dirSlope = gd < 0 ? gd : -dot5(g, g);
50662
- let alpha = 1;
50678
+ let alpha2 = 1;
50663
50679
  let xNew = clipToBounds(
50664
- x.map((v, i) => v + alpha * dir[i]),
50680
+ x.map((v, i) => v + alpha2 * dir[i]),
50665
50681
  bounds
50666
50682
  );
50667
50683
  let fNew = f(xNew);
50668
50684
  let halvings = 0;
50669
- while (fNew > fx + c1 * alpha * dirSlope && halvings < 50) {
50670
- alpha *= 0.5;
50685
+ while (fNew > fx + c1 * alpha2 * dirSlope && halvings < 50) {
50686
+ alpha2 *= 0.5;
50671
50687
  xNew = clipToBounds(
50672
- x.map((v, i) => v + alpha * dir[i]),
50688
+ x.map((v, i) => v + alpha2 * dir[i]),
50673
50689
  bounds
50674
50690
  );
50675
50691
  fNew = f(xNew);
@@ -50768,15 +50784,15 @@ function nnls(A, b, opts = {}) {
50768
50784
  let innerIter = 0;
50769
50785
  while (Array.from(passive).some((j) => z[j] <= tol) && innerIter < maxIter) {
50770
50786
  innerIter++;
50771
- let alpha = Infinity;
50787
+ let alpha2 = Infinity;
50772
50788
  for (const j of passive) {
50773
50789
  if (z[j] <= tol) {
50774
50790
  const denom = x[j] - z[j];
50775
- if (denom > 0) alpha = Math.min(alpha, x[j] / denom);
50791
+ if (denom > 0) alpha2 = Math.min(alpha2, x[j] / denom);
50776
50792
  }
50777
50793
  }
50778
- if (!isFinite(alpha)) alpha = 0;
50779
- for (let j = 0; j < n; j++) x[j] = x[j] + alpha * (z[j] - x[j]);
50794
+ if (!isFinite(alpha2)) alpha2 = 0;
50795
+ for (let j = 0; j < n; j++) x[j] = x[j] + alpha2 * (z[j] - x[j]);
50780
50796
  for (const j of Array.from(passive)) {
50781
50797
  if (Math.abs(x[j]) < tol) passive.delete(j);
50782
50798
  }
@@ -50824,18 +50840,18 @@ function lsqBounded(A, b, lower, upper, opts = {}) {
50824
50840
  pgNorm = Math.sqrt(pgNorm);
50825
50841
  if (pgNorm < tol) break;
50826
50842
  const fx = objective(x);
50827
- let alpha = 1;
50843
+ let alpha2 = 1;
50828
50844
  let xNext = clip(
50829
- x.map((xi, i) => xi - alpha * g[i]),
50845
+ x.map((xi, i) => xi - alpha2 * g[i]),
50830
50846
  lower,
50831
50847
  upper
50832
50848
  );
50833
50849
  let fNext = objective(xNext);
50834
50850
  let backtrack = 0;
50835
50851
  while (fNext > fx && backtrack < 50) {
50836
- alpha /= 2;
50852
+ alpha2 /= 2;
50837
50853
  xNext = clip(
50838
- x.map((xi, i) => xi - alpha * g[i]),
50854
+ x.map((xi, i) => xi - alpha2 * g[i]),
50839
50855
  lower,
50840
50856
  upper
50841
50857
  );
@@ -50847,7 +50863,10 @@ function lsqBounded(A, b, lower, upper, opts = {}) {
50847
50863
  return { x, residual: residualNorm(A, x, b) };
50848
50864
  }
50849
50865
 
50850
- // src/cas/rational-integrate.ts
50866
+ // src/cas/layer3.ts
50867
+ import { eig as eig4 } from "@danielsimonjr/mathts-matrix";
50868
+
50869
+ // src/cas/rat.ts
50851
50870
  function ratNormalize(num2, den) {
50852
50871
  if (den === 0n) {
50853
50872
  throw new Error("Rat: zero denominator");
@@ -50883,6 +50902,378 @@ function ratFromBigint(n) {
50883
50902
  return { num: n, den: 1n };
50884
50903
  }
50885
50904
  var RAT_ZERO = { num: 0n, den: 1n };
50905
+
50906
+ // src/cas/layer3.ts
50907
+ function ratEq(a, b) {
50908
+ return a.num === b.num && a.den === b.den;
50909
+ }
50910
+ function joinTerms(parts) {
50911
+ if (parts.length === 0) return "0";
50912
+ return parts.join(" + ").replace(/\+ -/g, "- ");
50913
+ }
50914
+ function renderCoeffTimes(r, rest) {
50915
+ const neg2 = r.num < 0n;
50916
+ const absNum = neg2 ? -r.num : r.num;
50917
+ const sign3 = neg2 ? "-" : "";
50918
+ if (absNum === r.den) return `${sign3}${rest}`;
50919
+ const denPart = r.den === 1n ? "" : `/${r.den}`;
50920
+ return `${sign3}${absNum}${denPart}*${rest}`;
50921
+ }
50922
+ function renderPoly(p, v) {
50923
+ const t = trim(p);
50924
+ if (t.length === 0) return "0";
50925
+ const parts = [];
50926
+ for (let i = t.length - 1; i >= 0; i -= 1) {
50927
+ const c = t[i];
50928
+ if (c === 0n) continue;
50929
+ const sign3 = c < 0n ? "-" : parts.length === 0 ? "" : "+";
50930
+ const abs2 = c < 0n ? -c : c;
50931
+ let term;
50932
+ if (i === 0) term = `${abs2}`;
50933
+ else if (i === 1) term = abs2 === 1n ? v : `${abs2}*${v}`;
50934
+ else term = abs2 === 1n ? `${v}^${i}` : `${abs2}*${v}^${i}`;
50935
+ parts.push(parts.length === 0 && sign3 === "" ? term : `${sign3} ${term}`);
50936
+ }
50937
+ return parts.join(" ").replace(/^\+ /, "");
50938
+ }
50939
+ function detZ(matrix2) {
50940
+ const n = matrix2.length;
50941
+ if (n === 0) return 1n;
50942
+ const a = matrix2.map((row2) => row2.slice());
50943
+ let sign3 = 1n;
50944
+ let prev = 1n;
50945
+ for (let k = 0; k < n; k += 1) {
50946
+ let pivot = k;
50947
+ while (pivot < n && a[pivot][k] === 0n) pivot += 1;
50948
+ if (pivot === n) return 0n;
50949
+ if (pivot !== k) {
50950
+ [a[k], a[pivot]] = [a[pivot], a[k]];
50951
+ sign3 = -sign3;
50952
+ }
50953
+ if (k === n - 1) break;
50954
+ for (let i = k + 1; i < n; i += 1) {
50955
+ for (let j = k + 1; j < n; j += 1) {
50956
+ a[i][j] = (a[i][j] * a[k][k] - a[i][k] * a[k][j]) / prev;
50957
+ }
50958
+ }
50959
+ prev = a[k][k];
50960
+ }
50961
+ return sign3 * a[n - 1][n - 1];
50962
+ }
50963
+ function resultantZ(a, b) {
50964
+ const ta = trim(a);
50965
+ const tb = trim(b);
50966
+ const m = ta.length - 1;
50967
+ const n = tb.length - 1;
50968
+ if (m < 0 || n < 0) return 0n;
50969
+ if (m === 0 && n === 0) return 1n;
50970
+ if (m === 0) return ta[0] ** BigInt(n);
50971
+ if (n === 0) return tb[0] ** BigInt(m);
50972
+ const dim = m + n;
50973
+ const s = Array.from({ length: dim }, () => Array(dim).fill(0n));
50974
+ for (let i = 0; i < n; i += 1) {
50975
+ for (let j = 0; j <= m; j += 1) s[i][i + j] = ta[m - j];
50976
+ }
50977
+ for (let i = 0; i < m; i += 1) {
50978
+ for (let j = 0; j <= n; j += 1) s[n + i][i + j] = tb[n - j];
50979
+ }
50980
+ return detZ(s);
50981
+ }
50982
+ function rothsteinResultant(A, B) {
50983
+ const Bp = derivative(B);
50984
+ const n = Math.max(degree(B), 0) + Math.max(degree(Bp), 0) + 2;
50985
+ const samples = [];
50986
+ for (let k = 0; k <= n; k += 1) {
50987
+ const p = sub(A, scalarMul(Bp, BigInt(k)));
50988
+ samples.push(resultantZ(p, B));
50989
+ }
50990
+ return interpolateAtIntegers(samples);
50991
+ }
50992
+ function interpolateAtIntegers(values) {
50993
+ const n = values.length - 1;
50994
+ const size2 = n + 1;
50995
+ const matrix2 = [];
50996
+ const rhs = [];
50997
+ for (let k = 0; k < size2; k += 1) {
50998
+ const row2 = [];
50999
+ let pk = 1n;
51000
+ for (let j = 0; j < size2; j += 1) {
51001
+ row2.push(ratFromBigint(pk));
51002
+ pk *= BigInt(k);
51003
+ }
51004
+ matrix2.push(row2);
51005
+ rhs.push(ratFromBigint(values[k]));
51006
+ }
51007
+ const coeffs = solveLinearSystemRat(matrix2, rhs);
51008
+ return trim(
51009
+ coeffs.map((r) => {
51010
+ if (r.den !== 1n) {
51011
+ throw new Error("rothsteinResultant: non-integer interpolated coefficient");
51012
+ }
51013
+ return r.num;
51014
+ })
51015
+ );
51016
+ }
51017
+ function solveLinearSystemRat(matrix2, rhs) {
51018
+ const n = rhs.length;
51019
+ const a = matrix2.map((row2, i) => [...row2, rhs[i]]);
51020
+ for (let col = 0; col < n; col += 1) {
51021
+ let pivot = col;
51022
+ while (pivot < n && a[pivot][col].num === 0n) pivot += 1;
51023
+ if (pivot === n) throw new Error("singular Rat system");
51024
+ if (pivot !== col) [a[col], a[pivot]] = [a[pivot], a[col]];
51025
+ const pv = a[col][col];
51026
+ for (let j = col; j <= n; j += 1) a[col][j] = ratDiv(a[col][j], pv);
51027
+ for (let i = 0; i < n; i += 1) {
51028
+ if (i === col) continue;
51029
+ const f = a[i][col];
51030
+ if (f.num === 0n) continue;
51031
+ for (let j = col; j <= n; j += 1) a[i][j] = ratSub(a[i][j], ratMul(f, a[col][j]));
51032
+ }
51033
+ }
51034
+ return a.map((row2) => row2[n]);
51035
+ }
51036
+ function intToRatPoly(p) {
51037
+ return trim(p).map(ratFromBigint);
51038
+ }
51039
+ function ratPolyToInt(p) {
51040
+ if (p.length === 0) return { poly: [], den: 1n };
51041
+ let lcm2 = 1n;
51042
+ const gcd2 = (a, b) => {
51043
+ let x = a < 0n ? -a : a;
51044
+ let y = b < 0n ? -b : b;
51045
+ while (y !== 0n) {
51046
+ const t = x % y;
51047
+ x = y;
51048
+ y = t;
51049
+ }
51050
+ return x;
51051
+ };
51052
+ for (const r of p) {
51053
+ const g = gcd2(lcm2, r.den);
51054
+ lcm2 = lcm2 / g * r.den;
51055
+ }
51056
+ return { poly: trim(p.map((r) => r.num * lcm2 / r.den)), den: lcm2 };
51057
+ }
51058
+ function rpAdd(a, b) {
51059
+ const n = Math.max(a.length, b.length);
51060
+ const out = [];
51061
+ for (let i = 0; i < n; i += 1) {
51062
+ out.push(ratAdd(a[i] ?? RAT_ZERO, b[i] ?? RAT_ZERO));
51063
+ }
51064
+ let m = out.length;
51065
+ while (m > 0 && out[m - 1].num === 0n) m -= 1;
51066
+ return out.slice(0, m);
51067
+ }
51068
+ function rpScale(p, s) {
51069
+ if (s.num === 0n) return [];
51070
+ return p.map((c) => ratMul(c, s));
51071
+ }
51072
+ function rpMul(a, b) {
51073
+ if (a.length === 0 || b.length === 0) return [];
51074
+ const out = Array.from({ length: a.length + b.length - 1 }, () => RAT_ZERO);
51075
+ for (let i = 0; i < a.length; i += 1) {
51076
+ if (a[i].num === 0n) continue;
51077
+ for (let j = 0; j < b.length; j += 1) {
51078
+ out[i + j] = ratAdd(out[i + j], ratMul(a[i], b[j]));
51079
+ }
51080
+ }
51081
+ let m = out.length;
51082
+ while (m > 0 && out[m - 1].num === 0n) m -= 1;
51083
+ return out.slice(0, m);
51084
+ }
51085
+ function rpDerivative(p) {
51086
+ if (p.length <= 1) return [];
51087
+ const out = [];
51088
+ for (let i = 1; i < p.length; i += 1) {
51089
+ out.push(ratMul(p[i], ratFromBigint(BigInt(i))));
51090
+ }
51091
+ return out;
51092
+ }
51093
+ function hermiteReduce(R, Q2, v) {
51094
+ const Q1 = polyGcdZ(Q2, derivative(Q2));
51095
+ if (degree(Q1) <= 0) {
51096
+ return { rational: "0", squareFreeNumer: trim(R), squareFreeDenom: trim(Q2) };
51097
+ }
51098
+ const Q22 = exactDivide(Q2, Q1);
51099
+ if (Q22 === null) {
51100
+ throw new Error("hermiteReduce: Q1 does not divide Q");
51101
+ }
51102
+ const Q1p = derivative(Q1);
51103
+ const Wnum = mul(Q22, Q1p);
51104
+ const Wdiv = exactDivide(Wnum, Q1);
51105
+ let W;
51106
+ if (Wdiv !== null) {
51107
+ W = intToRatPoly(Wdiv);
51108
+ } else {
51109
+ W = ratPolyExactQuotient(intToRatPoly(Wnum), intToRatPoly(Q1));
51110
+ }
51111
+ const d1 = degree(Q1);
51112
+ const d2 = degree(Q22);
51113
+ const nP = Math.max(d1, 0);
51114
+ const nS = Math.max(d2, 0);
51115
+ const n = nP + nS;
51116
+ const q2 = intToRatPoly(Q22);
51117
+ const q1 = intToRatPoly(Q1);
51118
+ const r = intToRatPoly(R);
51119
+ const matrix2 = Array.from({ length: n }, () => Array(n).fill(RAT_ZERO));
51120
+ const rhs = Array.from({ length: n }, () => RAT_ZERO);
51121
+ for (let i = 0; i < r.length && i < n; i += 1) rhs[i] = r[i];
51122
+ for (let k = 0; k < nP; k += 1) {
51123
+ const Pk = Array.from({ length: k + 1 }, () => RAT_ZERO);
51124
+ Pk[k] = { num: 1n, den: 1n };
51125
+ const Pp = rpDerivative(Pk);
51126
+ const term = rpAdd(rpMul(q2, Pp), rpScale(rpMul(W, Pk), ratFromBigint(-1n)));
51127
+ for (let i = 0; i < term.length && i < n; i += 1) matrix2[i][k] = term[i];
51128
+ }
51129
+ for (let k = 0; k < nS; k += 1) {
51130
+ const Sk = Array.from({ length: k + 1 }, () => RAT_ZERO);
51131
+ Sk[k] = { num: 1n, den: 1n };
51132
+ const term = rpMul(q1, Sk);
51133
+ for (let i = 0; i < term.length && i < n; i += 1) matrix2[i][nP + k] = term[i];
51134
+ }
51135
+ const sol = solveLinearSystemRat(matrix2, rhs);
51136
+ const P2 = sol.slice(0, nP);
51137
+ const S = sol.slice(nP);
51138
+ const Pint = ratPolyToInt(P2);
51139
+ const Sint = ratPolyToInt(S);
51140
+ const q1Scaled = Pint.den === 1n ? Q1 : scalarMul(Q1, Pint.den);
51141
+ const rational = degree(Pint.poly) < 0 ? "0" : `(${renderPoly(Pint.poly, v)})/(${renderPoly(q1Scaled, v)})`;
51142
+ const q2Scaled = Sint.den === 1n ? Q22 : scalarMul(Q22, Sint.den);
51143
+ return { rational, squareFreeNumer: Sint.poly, squareFreeDenom: trim(q2Scaled) };
51144
+ }
51145
+ function ratPolyExactQuotient(a, b) {
51146
+ const db = b.length - 1;
51147
+ if (db < 0) throw new Error("ratPolyExactQuotient: zero divisor");
51148
+ const lb = b[db];
51149
+ let rem = a.slice();
51150
+ const q = [];
51151
+ while (rem.length - 1 >= db) {
51152
+ const coeff = ratDiv(rem[rem.length - 1], lb);
51153
+ const shift = rem.length - 1 - db;
51154
+ while (q.length <= shift) q.push(RAT_ZERO);
51155
+ q[shift] = coeff;
51156
+ for (let i = 0; i <= db; i += 1) {
51157
+ rem[shift + i] = ratSub(rem[shift + i] ?? RAT_ZERO, ratMul(coeff, b[i]));
51158
+ }
51159
+ let m = rem.length;
51160
+ while (m > 0 && rem[m - 1].num === 0n) m -= 1;
51161
+ rem = rem.slice(0, m);
51162
+ }
51163
+ if (rem.some((c) => c.num !== 0n)) {
51164
+ throw new Error("ratPolyExactQuotient: nonzero remainder");
51165
+ }
51166
+ return q;
51167
+ }
51168
+ function evalPolyC(p, re3, im3) {
51169
+ let r = 0;
51170
+ let i = 0;
51171
+ for (let k = p.length - 1; k >= 0; k -= 1) {
51172
+ const c = Number(p[k]);
51173
+ const nr = r * re3 - i * im3 + c;
51174
+ const ni = r * im3 + i * re3;
51175
+ r = nr;
51176
+ i = ni;
51177
+ }
51178
+ return { re: r, im: i };
51179
+ }
51180
+ function companionRoots(B) {
51181
+ const t = trim(B);
51182
+ const n = t.length - 1;
51183
+ if (n <= 0) return [];
51184
+ const lead = Number(t[n]);
51185
+ if (n === 1) {
51186
+ return [{ re: -Number(t[0]) / lead, im: 0 }];
51187
+ }
51188
+ const m = Array.from({ length: n }, () => Array(n).fill(0));
51189
+ for (let i = 1; i < n; i += 1) m[i][i - 1] = 1;
51190
+ for (let j = 0; j < n; j += 1) m[0][j] = -Number(t[n - 1 - j]) / lead;
51191
+ for (let i = 0; i < n; i += 1) m[i].fill(0);
51192
+ for (let i = 0; i < n - 1; i += 1) m[i][i + 1] = 1;
51193
+ for (let j = 0; j < n; j += 1) m[n - 1][j] = -Number(t[j]) / lead;
51194
+ const { values } = eig4(m, { computeVectors: false });
51195
+ return values;
51196
+ }
51197
+ function residueIntegral(A, B, v) {
51198
+ const roots = companionRoots(B);
51199
+ const Bp = derivative(B);
51200
+ const used = new Array(roots.length).fill(false);
51201
+ const parts = [];
51202
+ const eps = 1e-10;
51203
+ for (let i = 0; i < roots.length; i += 1) {
51204
+ if (used[i]) continue;
51205
+ const r = roots[i];
51206
+ const Ar = evalPolyC(A, r.re, r.im);
51207
+ const Bpr = evalPolyC(Bp, r.re, r.im);
51208
+ const den = Bpr.re * Bpr.re + Bpr.im * Bpr.im;
51209
+ if (den < 1e-30) continue;
51210
+ const resRe = (Ar.re * Bpr.re + Ar.im * Bpr.im) / den;
51211
+ const resIm = (Ar.im * Bpr.re - Ar.re * Bpr.im) / den;
51212
+ if (Math.abs(r.im) < eps) {
51213
+ used[i] = true;
51214
+ if (Math.abs(resRe) < eps) continue;
51215
+ const rootStr = Math.abs(r.re) < eps ? v : `${v} - (${r.re})`;
51216
+ parts.push(`(${resRe})*log(abs(${rootStr}))`);
51217
+ continue;
51218
+ }
51219
+ let j = -1;
51220
+ for (let k = i + 1; k < roots.length; k += 1) {
51221
+ if (!used[k] && Math.abs(roots[k].re - r.re) < 1e-8 && Math.abs(roots[k].im + r.im) < 1e-8) {
51222
+ j = k;
51223
+ break;
51224
+ }
51225
+ }
51226
+ used[i] = true;
51227
+ if (j >= 0) used[j] = true;
51228
+ const a = r.re;
51229
+ const b = r.im;
51230
+ const quad2 = `(${v} - (${a}))^2 + (${b})^2`;
51231
+ if (Math.abs(resRe) > eps) {
51232
+ parts.push(`(${resRe})*log(${quad2})`);
51233
+ }
51234
+ if (Math.abs(resIm) > eps) {
51235
+ parts.push(`(${-2 * resIm})*atan2(${-b}, ${v} - (${a}))`);
51236
+ }
51237
+ }
51238
+ return joinTerms(parts);
51239
+ }
51240
+ function rothsteinTrager(A, B, v) {
51241
+ try {
51242
+ const R = rothsteinResultant(A, B);
51243
+ if (degree(R) < 0) return "0";
51244
+ const { factors } = factorUnivariateZ(R);
51245
+ const allLinear = factors.every((f) => trim(f.poly).length - 1 === 1);
51246
+ if (allLinear) {
51247
+ const Bp = derivative(B);
51248
+ const parts = [];
51249
+ for (const { poly } of factors) {
51250
+ const q = trim(poly);
51251
+ if (q.length !== 2) continue;
51252
+ const c = ratDiv(ratFromBigint(-q[0]), ratFromBigint(q[1]));
51253
+ if (ratEq(c, RAT_ZERO)) continue;
51254
+ const scaled = sub(scalarMul(A, c.den), scalarMul(Bp, c.num));
51255
+ const g = polyGcdZ(scaled, B);
51256
+ if (degree(g) < 1) continue;
51257
+ parts.push(renderCoeffTimes(c, `log(${renderPoly(g, v)})`));
51258
+ }
51259
+ return joinTerms(parts);
51260
+ }
51261
+ } catch {
51262
+ }
51263
+ return residueIntegral(A, B, v);
51264
+ }
51265
+ function integrateLayer3(R, Q2, v) {
51266
+ const { rational, squareFreeNumer, squareFreeDenom } = hermiteReduce(R, Q2, v);
51267
+ const parts = [];
51268
+ if (rational !== "0") parts.push(rational);
51269
+ if (degree(squareFreeDenom) >= 1 && degree(squareFreeNumer) >= 0) {
51270
+ const sf = rothsteinTrager(squareFreeNumer, squareFreeDenom, v);
51271
+ if (sf !== "0") parts.push(sf);
51272
+ }
51273
+ return joinTerms(parts);
51274
+ }
51275
+
51276
+ // src/cas/rational-integrate.ts
50886
51277
  function surdFromRat(r) {
50887
51278
  return { a: r, b: RAT_ZERO };
50888
51279
  }
@@ -51104,7 +51495,7 @@ function shiftedColumn(poly, shift, size2) {
51104
51495
  }
51105
51496
  return col;
51106
51497
  }
51107
- function solveLinearSystemRat(matrix2, rhs) {
51498
+ function solveLinearSystemRat2(matrix2, rhs) {
51108
51499
  const n = rhs.length;
51109
51500
  const rows = matrix2.map((row2, r) => [...row2, rhs[r]]);
51110
51501
  for (let col = 0; col < n; col += 1) {
@@ -51181,7 +51572,7 @@ function partialFractions(remainder, factors) {
51181
51572
  rows.push(columns.map((col) => col[r]));
51182
51573
  }
51183
51574
  const rhs = new Array(n).fill(RAT_ZERO).map((_, idx2) => ratFromBigint(idx2 < trimmedRemainder.length ? trimmedRemainder[idx2] : 0n));
51184
- const solution = n === 0 ? [] : solveLinearSystemRat(rows, rhs);
51575
+ const solution = n === 0 ? [] : solveLinearSystemRat2(rows, rhs);
51185
51576
  const terms = [];
51186
51577
  let idx = 0;
51187
51578
  for (let fi = 0; fi < factors.length; fi += 1) {
@@ -51203,7 +51594,7 @@ function ratNeg(r) {
51203
51594
  function ratToStr(r) {
51204
51595
  return r.den === 1n ? `${r.num}` : `${r.num}/${r.den}`;
51205
51596
  }
51206
- function renderCoeffTimes(r, rest) {
51597
+ function renderCoeffTimes2(r, rest) {
51207
51598
  const neg2 = r.num < 0n;
51208
51599
  const absNum = neg2 ? -r.num : r.num;
51209
51600
  const sign3 = neg2 ? "-" : "";
@@ -51237,7 +51628,7 @@ function renderTwoXPlusB(b, v) {
51237
51628
  }
51238
51629
  return s;
51239
51630
  }
51240
- function joinTerms(parts) {
51631
+ function joinTerms2(parts) {
51241
51632
  if (parts.length === 0) {
51242
51633
  return "0";
51243
51634
  }
@@ -51253,10 +51644,10 @@ function integrateLinearTerm(factor2, k, numer, v) {
51253
51644
  }
51254
51645
  const xMinus = renderXMinus(a, v);
51255
51646
  if (k === 1) {
51256
- return renderCoeffTimes(A, `log(abs(${xMinus}))`);
51647
+ return renderCoeffTimes2(A, `log(abs(${xMinus}))`);
51257
51648
  }
51258
51649
  const coeff = ratNeg(ratDiv(A, ratFromBigint(BigInt(k - 1))));
51259
- return renderCoeffTimes(coeff, `(${xMinus})^(${-(k - 1)})`);
51650
+ return renderCoeffTimes2(coeff, `(${xMinus})^(${-(k - 1)})`);
51260
51651
  }
51261
51652
  function integrateInverseQuadraticPower(k, b, c) {
51262
51653
  if (k === 1) {
@@ -51288,10 +51679,10 @@ function integrateQuadraticTerm(factor2, k, numer, v) {
51288
51679
  if (D.num !== 0n) {
51289
51680
  const half = ratDiv(D, ratFromBigint(2n));
51290
51681
  if (k === 1) {
51291
- parts.push(renderCoeffTimes(half, `log(${qCore})`));
51682
+ parts.push(renderCoeffTimes2(half, `log(${qCore})`));
51292
51683
  } else {
51293
51684
  const coeff = ratNeg(ratDiv(half, ratFromBigint(BigInt(k - 1))));
51294
- parts.push(renderCoeffTimes(coeff, `(${qCore})^(${-(k - 1)})`));
51685
+ parts.push(renderCoeffTimes2(coeff, `(${qCore})^(${-(k - 1)})`));
51295
51686
  }
51296
51687
  }
51297
51688
  if (K.num !== 0n) {
@@ -51301,15 +51692,15 @@ function integrateQuadraticTerm(factor2, k, numer, v) {
51301
51692
  if (coeff.num === 0n) {
51302
51693
  continue;
51303
51694
  }
51304
- parts.push(renderCoeffTimes(coeff, `(${twoXb})/(${qCore})^(${t.power})`));
51695
+ parts.push(renderCoeffTimes2(coeff, `(${twoXb})/(${qCore})^(${t.power})`));
51305
51696
  }
51306
51697
  const atanCoeff = ratMul(ratMul(K, ik.atanCoeff), ratFromBigint(2n));
51307
51698
  if (atanCoeff.num !== 0n) {
51308
51699
  const d2s = ratToStr(d2);
51309
- parts.push(renderCoeffTimes(atanCoeff, `atan((${twoXb})/sqrt(${d2s}))/sqrt(${d2s})`));
51700
+ parts.push(renderCoeffTimes2(atanCoeff, `atan((${twoXb})/sqrt(${d2s}))/sqrt(${d2s})`));
51310
51701
  }
51311
51702
  }
51312
- return joinTerms(parts);
51703
+ return joinTerms2(parts);
51313
51704
  }
51314
51705
  function integrateQuadraticPosTerm(factor2, numer, v) {
51315
51706
  const c = factor2[0];
@@ -51339,7 +51730,7 @@ function integrateQuadraticPosTerm(factor2, numer, v) {
51339
51730
  };
51340
51731
  emit(A, r1);
51341
51732
  emit(B, r2);
51342
- return joinTerms(parts);
51733
+ return joinTerms2(parts);
51343
51734
  }
51344
51735
  function integratePFTerm(term, v) {
51345
51736
  const factor2 = trim(term.factor);
@@ -51367,7 +51758,13 @@ function integrateRationalFunction(expr, v) {
51367
51758
  }
51368
51759
  const factors = factorDenominator(rf.denom);
51369
51760
  if (factors === null) {
51370
- return null;
51761
+ const { quotient: quotient2, remainder: remainder2 } = polynomialPart(rf);
51762
+ const parts2 = [];
51763
+ const polyPart2 = integratePolynomial(quotient2, v);
51764
+ if (polyPart2 !== "0") parts2.push(polyPart2);
51765
+ const l3 = integrateLayer3(remainder2, rf.denom, v);
51766
+ if (l3 !== "0") parts2.push(l3);
51767
+ return joinTerms2(parts2);
51371
51768
  }
51372
51769
  const { quotient, remainder } = polynomialPart(rf);
51373
51770
  const terms = partialFractions(remainder, factors);
@@ -51393,8 +51790,11 @@ function integrateRationalFunction(expr, v) {
51393
51790
  parts.push(s);
51394
51791
  }
51395
51792
  }
51396
- return joinTerms(parts);
51397
- } catch {
51793
+ return joinTerms2(parts);
51794
+ } catch (err) {
51795
+ if (typeof process !== "undefined" && process.env.MATHTS_DEBUG_L3) {
51796
+ console.error("integrateRationalFunction declined:", err);
51797
+ }
51398
51798
  return null;
51399
51799
  }
51400
51800
  }
@@ -51468,6 +51868,18 @@ function integrateNode(raw, x) {
51468
51868
  const k = evalConst(args[0].toString()) / a;
51469
51869
  return `${num(k)} * log(abs(${args[1].toString()}))`;
51470
51870
  }
51871
+ const den = unwrap(args[1]);
51872
+ if (den.type === "OperatorNode" && den.op === "*") {
51873
+ const df = (den.args ?? []).map(unwrap);
51874
+ const hasX = df.some((f) => f.type === "SymbolNode" && f.name === x);
51875
+ const logF = df.find(
51876
+ (f) => f.type === "FunctionNode" && (f.fn?.name === "log" || f.fn?.name === "ln") && unwrap((f.args ?? [])[0] ?? f).type === "SymbolNode" && unwrap((f.args ?? [])[0] ?? f).name === x
51877
+ );
51878
+ if (hasX && logF && df.length === 2) {
51879
+ const k = evalConst(args[0].toString());
51880
+ return k === 1 ? `log(log(${x}))` : `${num(k)} * log(log(${x}))`;
51881
+ }
51882
+ }
51471
51883
  }
51472
51884
  throw new NotIntegrable("general quotient");
51473
51885
  }
@@ -52321,7 +52733,7 @@ function fminbound(f, x1, x2) {
52321
52733
  let x = xf;
52322
52734
  let fx = f(x);
52323
52735
  let num2 = 1;
52324
- let fu = Infinity;
52736
+ let fu;
52325
52737
  let ffulc = fx;
52326
52738
  let fnfc = fx;
52327
52739
  let xm = 0.5 * (a + b);
@@ -52581,7 +52993,7 @@ function wate(freq, fx, wtx, lband, jtype) {
52581
52993
  if (fx[lband] >= 1e-4) return wtx[lband] / freq;
52582
52994
  return wtx[lband];
52583
52995
  }
52584
- function remezCore(des, grid, edge, wt, ngrid, nbands, iext, alpha, nfcns, itrmax) {
52996
+ function remezCore(des, grid, edge, wt, ngrid, nbands, iext, alpha2, nfcns, itrmax) {
52585
52997
  const size2 = ngrid + 2 * nfcns + 8;
52586
52998
  const a = new Array(size2).fill(0);
52587
52999
  const p = new Array(size2).fill(0);
@@ -52905,7 +53317,7 @@ function remezCore(des, grid, edge, wt, ngrid, nbands, iext, alpha, nfcns, itrma
52905
53317
  xt = (xt - bb) / aa;
52906
53318
  ft = Math.acos(xt) / TWOPI;
52907
53319
  }
52908
- let assigned = false;
53320
+ let assigned;
52909
53321
  for (; ; ) {
52910
53322
  const xe = x[l];
52911
53323
  if (xt > xe) {
@@ -52936,14 +53348,14 @@ function remezCore(des, grid, edge, wt, ngrid, nbands, iext, alpha, nfcns, itrma
52936
53348
  if (nm1 >= 1) {
52937
53349
  for (let k = 1; k <= nm1; k++) dtemp += a[k + 1] * Math.cos(dnum * k);
52938
53350
  }
52939
- alpha[j] = 2 * dtemp + a[1];
53351
+ alpha2[j] = 2 * dtemp + a[1];
52940
53352
  }
52941
- for (j = 2; j <= nfcns; j++) alpha[j] *= 2 / cn;
52942
- alpha[1] /= cn;
53353
+ for (j = 2; j <= nfcns; j++) alpha2[j] *= 2 / cn;
53354
+ alpha2[1] /= cn;
52943
53355
  if (kkk !== 1) {
52944
- p[1] = 2 * alpha[nfcns] * bb + alpha[nm1];
52945
- p[2] = 2 * aa * alpha[nfcns];
52946
- q[1] = alpha[nfcns - 2] - alpha[nfcns];
53356
+ p[1] = 2 * alpha2[nfcns] * bb + alpha2[nm1];
53357
+ p[2] = 2 * aa * alpha2[nfcns];
53358
+ q[1] = alpha2[nfcns - 2] - alpha2[nfcns];
52947
53359
  for (j = 2; j <= nm1; j++) {
52948
53360
  if (j >= nm1) {
52949
53361
  aa *= 0.5;
@@ -52961,14 +53373,14 @@ function remezCore(des, grid, edge, wt, ngrid, nbands, iext, alpha, nfcns, itrma
52961
53373
  for (let k = 3; k <= jp1; k++) p[k] += aa * a[k - 1];
52962
53374
  if (j !== nm1) {
52963
53375
  for (let k = 1; k <= j; k++) q[k] = -a[k];
52964
- q[1] += alpha[nfcns - 1 - j];
53376
+ q[1] += alpha2[nfcns - 1 - j];
52965
53377
  }
52966
53378
  }
52967
- for (j = 1; j <= nfcns; j++) alpha[j] = p[j];
53379
+ for (j = 1; j <= nfcns; j++) alpha2[j] = p[j];
52968
53380
  }
52969
53381
  if (nfcns <= 3) {
52970
- alpha[nfcns + 1] = 0;
52971
- alpha[nfcns + 2] = 0;
53382
+ alpha2[nfcns + 1] = 0;
53383
+ alpha2[nfcns + 2] = 0;
52972
53384
  }
52973
53385
  return { status: 0, dev, niter };
52974
53386
  }
@@ -53002,7 +53414,7 @@ function remezExchange(numtaps, bands, desired, weight, type = "bandpass", maxit
53002
53414
  const des = new Array(wrksize + 2).fill(0);
53003
53415
  const grid = new Array(wrksize + 2).fill(0);
53004
53416
  const wt = new Array(wrksize + 2).fill(0);
53005
- const alpha = new Array(dimsize + 3).fill(0);
53417
+ const alpha2 = new Array(dimsize + 3).fill(0);
53006
53418
  const iext = new Array(dimsize + 3).fill(0);
53007
53419
  let delf = gridDensity * nfcns;
53008
53420
  delf = 0.5 / delf;
@@ -53059,31 +53471,31 @@ function remezExchange(numtaps, bands, desired, weight, type = "bandpass", maxit
53059
53471
  const temp = (ngrid - 1) / nfcns;
53060
53472
  for (j = 1; j <= nfcns; j++) iext[j] = Math.floor((j - 1) * temp) + 1;
53061
53473
  iext[nfcns + 1] = ngrid;
53062
- const res = remezCore(des, grid, edge, wt, ngrid, numbands, iext, alpha, nfcns, maxiter);
53474
+ const res = remezCore(des, grid, edge, wt, ngrid, numbands, iext, alpha2, nfcns, maxiter);
53063
53475
  if (res.status < 0) throw new Error("remez: Remez exchange failed to converge");
53064
53476
  const h = new Array(numtaps + 1).fill(0);
53065
53477
  const nz = nfcns + 1;
53066
53478
  const nm1 = nfcns - 1;
53067
53479
  if (neg2 <= 0) {
53068
53480
  if (nodd !== 0) {
53069
- for (j = 1; j <= nm1; j++) h[j] = 0.5 * alpha[nz - j];
53070
- h[nfcns] = alpha[1];
53481
+ for (j = 1; j <= nm1; j++) h[j] = 0.5 * alpha2[nz - j];
53482
+ h[nfcns] = alpha2[1];
53071
53483
  } else {
53072
- h[1] = 0.25 * alpha[nfcns];
53073
- for (j = 2; j <= nm1; j++) h[j] = 0.25 * (alpha[nz - j] + alpha[nfcns + 2 - j]);
53074
- h[nfcns] = 0.5 * alpha[1] + 0.25 * alpha[2];
53484
+ h[1] = 0.25 * alpha2[nfcns];
53485
+ for (j = 2; j <= nm1; j++) h[j] = 0.25 * (alpha2[nz - j] + alpha2[nfcns + 2 - j]);
53486
+ h[nfcns] = 0.5 * alpha2[1] + 0.25 * alpha2[2];
53075
53487
  }
53076
53488
  } else {
53077
53489
  if (nodd !== 0) {
53078
- h[1] = 0.25 * alpha[nfcns];
53079
- h[2] = 0.25 * alpha[nm1];
53080
- for (j = 3; j <= nm1; j++) h[j] = 0.25 * (alpha[nz - j] - alpha[nfcns + 3 - j]);
53081
- h[nfcns] = 0.5 * alpha[1] - 0.25 * alpha[3];
53490
+ h[1] = 0.25 * alpha2[nfcns];
53491
+ h[2] = 0.25 * alpha2[nm1];
53492
+ for (j = 3; j <= nm1; j++) h[j] = 0.25 * (alpha2[nz - j] - alpha2[nfcns + 3 - j]);
53493
+ h[nfcns] = 0.5 * alpha2[1] - 0.25 * alpha2[3];
53082
53494
  h[nz] = 0;
53083
53495
  } else {
53084
- h[1] = 0.25 * alpha[nfcns];
53085
- for (j = 2; j <= nm1; j++) h[j] = 0.25 * (alpha[nz - j] - alpha[nfcns + 2 - j]);
53086
- h[nfcns] = 0.5 * alpha[1] - 0.25 * alpha[2];
53496
+ h[1] = 0.25 * alpha2[nfcns];
53497
+ for (j = 2; j <= nm1; j++) h[j] = 0.25 * (alpha2[nz - j] - alpha2[nfcns + 2 - j]);
53498
+ h[nfcns] = 0.5 * alpha2[1] - 0.25 * alpha2[2];
53087
53499
  }
53088
53500
  }
53089
53501
  for (j = 1; j <= nfcns; j++) {
@@ -53315,7 +53727,7 @@ function idwt(approx, detail, wavelet = "haar") {
53315
53727
  try {
53316
53728
  return idwtPeriodization(approx, detail, wavelet);
53317
53729
  } catch (err) {
53318
- throw new Error(`idwt: ${err instanceof Error ? err.message : String(err)}`);
53730
+ throw new Error(`idwt: ${err instanceof Error ? err.message : String(err)}`, { cause: err });
53319
53731
  }
53320
53732
  }
53321
53733
  function wavedec(x, wavelet = "haar", level = 1) {
@@ -54045,10 +54457,10 @@ function circumradius(a, b, c) {
54045
54457
  const uy = (a2 * (c[0] - b[0]) + b2 * (a[0] - c[0]) + c2 * (b[0] - a[0])) / d;
54046
54458
  return Math.hypot(a[0] - ux, a[1] - uy);
54047
54459
  }
54048
- function alphaShape(points, alpha) {
54049
- if (!(alpha > 0)) throw new Error("alphaShape: alpha must be positive");
54460
+ function alphaShape(points, alpha2) {
54461
+ if (!(alpha2 > 0)) throw new Error("alphaShape: alpha must be positive");
54050
54462
  const { simplices: tris } = delaunay(points);
54051
- const threshold = 1 / alpha;
54463
+ const threshold = 1 / alpha2;
54052
54464
  const triangles = [];
54053
54465
  const edgeCount = /* @__PURE__ */ new Map();
54054
54466
  for (const t of tris) {
@@ -54537,34 +54949,34 @@ function polygamma(n, x) {
54537
54949
  function trigamma(x) {
54538
54950
  return polygamma(1, x);
54539
54951
  }
54540
- function jacobiP(n, alpha, beta2, x) {
54952
+ function jacobiP(n, alpha2, beta3, x) {
54541
54953
  if (!Number.isInteger(n) || n < 0) {
54542
54954
  throw new Error("jacobiP: n must be a nonnegative integer");
54543
54955
  }
54544
54956
  if (n === 0) return 1;
54545
54957
  let pPrev = 1;
54546
- let pCurr = alpha + 1 + (alpha + beta2 + 2) * (x - 1) / 2;
54958
+ let pCurr = alpha2 + 1 + (alpha2 + beta3 + 2) * (x - 1) / 2;
54547
54959
  if (n === 1) return pCurr;
54548
54960
  for (let k = 2; k <= n; k++) {
54549
- const a1 = 2 * k * (k + alpha + beta2) * (2 * k + alpha + beta2 - 2);
54550
- const a2 = (2 * k + alpha + beta2 - 1) * ((2 * k + alpha + beta2) * (2 * k + alpha + beta2 - 2) * x + alpha * alpha - beta2 * beta2);
54551
- const a3 = 2 * (k + alpha - 1) * (k + beta2 - 1) * (2 * k + alpha + beta2);
54961
+ const a1 = 2 * k * (k + alpha2 + beta3) * (2 * k + alpha2 + beta3 - 2);
54962
+ const a2 = (2 * k + alpha2 + beta3 - 1) * ((2 * k + alpha2 + beta3) * (2 * k + alpha2 + beta3 - 2) * x + alpha2 * alpha2 - beta3 * beta3);
54963
+ const a3 = 2 * (k + alpha2 - 1) * (k + beta3 - 1) * (2 * k + alpha2 + beta3);
54552
54964
  const pNext = (a2 * pCurr - a3 * pPrev) / a1;
54553
54965
  pPrev = pCurr;
54554
54966
  pCurr = pNext;
54555
54967
  }
54556
54968
  return pCurr;
54557
54969
  }
54558
- function gegenbauerC(n, alpha, x) {
54970
+ function gegenbauerC(n, alpha2, x) {
54559
54971
  if (!Number.isInteger(n) || n < 0) {
54560
54972
  throw new Error("gegenbauerC: n must be a nonnegative integer");
54561
54973
  }
54562
54974
  if (n === 0) return 1;
54563
54975
  let cPrev = 1;
54564
- let cCurr = 2 * alpha * x;
54976
+ let cCurr = 2 * alpha2 * x;
54565
54977
  if (n === 1) return cCurr;
54566
54978
  for (let k = 2; k <= n; k++) {
54567
- const cNext = (2 * x * (k + alpha - 1) * cCurr - (k + 2 * alpha - 2) * cPrev) / k;
54979
+ const cNext = (2 * x * (k + alpha2 - 1) * cCurr - (k + 2 * alpha2 - 2) * cPrev) / k;
54568
54980
  cPrev = cCurr;
54569
54981
  cCurr = cNext;
54570
54982
  }
@@ -54593,7 +55005,7 @@ function rootsLegendre(n) {
54593
55005
  const weights = [];
54594
55006
  for (let i = 0; i < n; i++) {
54595
55007
  let x = Math.cos(Math.PI * (i + 0.75) / (n + 0.5));
54596
- let pn = 0;
55008
+ let pn;
54597
55009
  let dpn = 0;
54598
55010
  for (let iter = 0; iter < NEWTON_MAX_ITERATIONS; iter++) {
54599
55011
  const [pCurr, pPrev] = legendrePair(n, x);
@@ -54950,7 +55362,7 @@ function coulombFG(L, eta, rho) {
54950
55362
  }
54951
55363
 
54952
55364
  // src/special/mathieu.ts
54953
- import { eig as eig4 } from "@danielsimonjr/mathts-matrix";
55365
+ import { eig as eig5 } from "@danielsimonjr/mathts-matrix";
54954
55366
  var N = 50;
54955
55367
  function harmonicsFor(cls) {
54956
55368
  const h = new Int32Array(N);
@@ -54989,7 +55401,7 @@ function solveClass(cls, q) {
54989
55401
  const cached = modeCache.get(key2);
54990
55402
  if (cached) return cached;
54991
55403
  const harmonics = harmonicsFor(cls);
54992
- const { values, vectors } = eig4(buildMatrix(cls, q));
55404
+ const { values, vectors } = eig5(buildMatrix(cls, q));
54993
55405
  const order = values.map((_, i) => i).sort((i, j) => values[i].re - values[j].re);
54994
55406
  const modes = order.map((idx, m) => {
54995
55407
  const g = vectors[idx];
@@ -55049,6 +55461,184 @@ function mathieuSe(n, q, x) {
55049
55461
  return evalSeries(solveClass(cls, q)[m], x, Math.sin);
55050
55462
  }
55051
55463
 
55464
+ // src/special/spheroidal.ts
55465
+ import { eig as eig6 } from "@danielsimonjr/mathts-matrix";
55466
+ var KMAX = 48;
55467
+ function checkMN(m, n, name254) {
55468
+ if (!Number.isInteger(m) || m < 0) {
55469
+ throw new Error(`${name254}: m must be a nonnegative integer`);
55470
+ }
55471
+ if (!Number.isInteger(n) || n < m) {
55472
+ throw new Error(`${name254}: n must be an integer \u2265 m`);
55473
+ }
55474
+ }
55475
+ function ferrersP(n, m, x) {
55476
+ if (m < 0 || n < m) return 0;
55477
+ const s = Math.max(0, 1 - x * x);
55478
+ const somx2 = Math.sqrt(s);
55479
+ let pmm = 1;
55480
+ if (m > 0) {
55481
+ let fact = 1;
55482
+ for (let i = 1; i <= m; i += 1) {
55483
+ pmm *= -fact * somx2;
55484
+ fact += 2;
55485
+ }
55486
+ }
55487
+ if (n === m) return pmm;
55488
+ let pmmp1 = x * (2 * m + 1) * pmm;
55489
+ if (n === m + 1) return pmmp1;
55490
+ let pnn = pmmp1;
55491
+ for (let nn = m + 2; nn <= n; nn += 1) {
55492
+ pnn = ((2 * nn - 1) * x * pmmp1 - (nn + m - 1) * pmm) / (nn - m);
55493
+ pmm = pmmp1;
55494
+ pmmp1 = pnn;
55495
+ }
55496
+ return pnn;
55497
+ }
55498
+ function alpha(m, r, c2) {
55499
+ const ell = m + r;
55500
+ if (ell === 0 && m === 0) {
55501
+ return c2 / 3;
55502
+ }
55503
+ const den = (2 * ell - 1) * (2 * ell + 3);
55504
+ const extra = den === 0 ? 0 : c2 * (2 * ell * (ell + 1) - 2 * m * m - 1) / den;
55505
+ return ell * (ell + 1) + extra;
55506
+ }
55507
+ function beta2(m, r, c2) {
55508
+ const ell = m + r;
55509
+ const den = (2 * ell + 3) * (2 * ell + 5);
55510
+ if (den === 0) return 0;
55511
+ return c2 * (ell + m + 1) * (ell + m + 2) / den;
55512
+ }
55513
+ function gammaCoeff(m, r, c2) {
55514
+ const ell = m + r;
55515
+ const den = (2 * ell - 3) * (2 * ell - 1);
55516
+ if (den === 0) return 0;
55517
+ return c2 * (ell - m) * (ell - m - 1) / den;
55518
+ }
55519
+ function solveParity(m, r0, c) {
55520
+ const c2 = c * c;
55521
+ const rs = [];
55522
+ for (let r = r0; r < KMAX; r += 2) rs.push(r);
55523
+ const N2 = rs.length;
55524
+ const diag2 = new Float64Array(N2);
55525
+ const off = new Float64Array(N2 - 1);
55526
+ for (let i = 0; i < N2; i += 1) {
55527
+ diag2[i] = alpha(m, rs[i], c2);
55528
+ if (i < N2 - 1) {
55529
+ const b = beta2(m, rs[i], c2);
55530
+ const g = gammaCoeff(m, rs[i + 1], c2);
55531
+ off[i] = Math.sign(b) * Math.sqrt(Math.abs(b * g));
55532
+ }
55533
+ }
55534
+ const mat = Array.from({ length: N2 }, () => Array(N2).fill(0));
55535
+ for (let i = 0; i < N2; i += 1) {
55536
+ mat[i][i] = diag2[i];
55537
+ if (i < N2 - 1) {
55538
+ mat[i][i + 1] = off[i];
55539
+ mat[i + 1][i] = off[i];
55540
+ }
55541
+ }
55542
+ const { values, vectors } = eig6(mat);
55543
+ const order = values.map((v, i) => ({ lam: v.re, i })).sort((a, b) => a.lam - b.lam);
55544
+ return order.map(({ lam, i }) => {
55545
+ const coeffs = new Float64Array(N2);
55546
+ let nrm = 0;
55547
+ for (let k = 0; k < N2; k += 1) {
55548
+ coeffs[k] = vectors[k][i];
55549
+ nrm += coeffs[k] * coeffs[k];
55550
+ }
55551
+ const s = Math.sqrt(nrm) || 1;
55552
+ const sign3 = coeffs[0] < 0 ? -1 : 1;
55553
+ for (let k = 0; k < N2; k += 1) coeffs[k] = sign3 * coeffs[k] / s;
55554
+ return { lambda: lam, coeffs, r0 };
55555
+ });
55556
+ }
55557
+ function modeFor(m, n, c) {
55558
+ const N2 = n - m;
55559
+ const r0 = N2 % 2;
55560
+ const idx = Math.floor(N2 / 2);
55561
+ const modes = solveParity(m, r0, c);
55562
+ if (idx >= modes.length) {
55563
+ throw new Error(`spheroidal: expansion too short for n=${n}, m=${m}`);
55564
+ }
55565
+ return modes[idx];
55566
+ }
55567
+ function spheroidalLambda(m, n, c) {
55568
+ checkMN(m, n, "spheroidalLambda");
55569
+ if (c === 0) return n * (n + 1);
55570
+ return modeFor(m, n, c).lambda;
55571
+ }
55572
+ var spheroidalCharacteristic = spheroidalLambda;
55573
+ function spheroidalAngular(m, n, c, eta) {
55574
+ checkMN(m, n, "spheroidalAngular");
55575
+ if (!Number.isFinite(eta) || eta < -1 || eta > 1) {
55576
+ throw new Error("spheroidalAngular: eta must lie in [-1, 1]");
55577
+ }
55578
+ if (c === 0) return ferrersP(n, m, eta);
55579
+ const mode2 = modeFor(m, n, c);
55580
+ let s = 0;
55581
+ for (let i = 0; i < mode2.coeffs.length; i += 1) {
55582
+ const r = mode2.r0 + 2 * i;
55583
+ s += mode2.coeffs[i] * ferrersP(m + r, m, eta);
55584
+ }
55585
+ const ref = m === 0 ? 0 : 0.5;
55586
+ const pref = ferrersP(n, m, ref);
55587
+ let sref = 0;
55588
+ for (let i = 0; i < mode2.coeffs.length; i += 1) {
55589
+ const r = mode2.r0 + 2 * i;
55590
+ sref += mode2.coeffs[i] * ferrersP(m + r, m, ref);
55591
+ }
55592
+ if (Math.abs(sref) < 1e-18 || Math.abs(pref) < 1e-18) return s;
55593
+ return s * (pref / sref);
55594
+ }
55595
+ function spheroidalRadial(m, n, c, xi) {
55596
+ checkMN(m, n, "spheroidalRadial");
55597
+ if (!Number.isFinite(xi) || xi < 1) {
55598
+ throw new Error("spheroidalRadial: xi must be \u2265 1");
55599
+ }
55600
+ return spheroidalAngularContinued(m, n, c, xi);
55601
+ }
55602
+ function ferrersPContinued(n, m, x) {
55603
+ const somx2 = x * x >= 1 ? Math.sqrt(x * x - 1) : Math.sqrt(1 - x * x);
55604
+ let pmm = 1;
55605
+ if (m > 0) {
55606
+ let fact = 1;
55607
+ for (let i = 1; i <= m; i += 1) {
55608
+ pmm *= fact * somx2;
55609
+ fact += 2;
55610
+ }
55611
+ }
55612
+ if (n === m) return pmm;
55613
+ let pmmp1 = x * (2 * m + 1) * pmm;
55614
+ if (n === m + 1) return pmmp1;
55615
+ let pnn = pmmp1;
55616
+ for (let nn = m + 2; nn <= n; nn += 1) {
55617
+ pnn = ((2 * nn - 1) * x * pmmp1 - (nn + m - 1) * pmm) / (nn - m);
55618
+ pmm = pmmp1;
55619
+ pmmp1 = pnn;
55620
+ }
55621
+ return pnn;
55622
+ }
55623
+ function spheroidalAngularContinued(m, n, c, x) {
55624
+ if (c === 0) return ferrersPContinued(n, m, x);
55625
+ const mode2 = modeFor(m, n, c);
55626
+ let s = 0;
55627
+ for (let i = 0; i < mode2.coeffs.length; i += 1) {
55628
+ const r = mode2.r0 + 2 * i;
55629
+ s += mode2.coeffs[i] * ferrersPContinued(m + r, m, x);
55630
+ }
55631
+ const ref = 1.2;
55632
+ const pref = ferrersPContinued(n, m, ref);
55633
+ let sref = 0;
55634
+ for (let i = 0; i < mode2.coeffs.length; i += 1) {
55635
+ const r = mode2.r0 + 2 * i;
55636
+ sref += mode2.coeffs[i] * ferrersPContinued(m + r, m, ref);
55637
+ }
55638
+ if (Math.abs(sref) < 1e-18 || Math.abs(pref) < 1e-18) return s;
55639
+ return s * (pref / sref);
55640
+ }
55641
+
55052
55642
  // src/graph/traversal-centrality.ts
55053
55643
  function bfs(adj, start) {
55054
55644
  const n = adj.length;
@@ -55272,7 +55862,10 @@ function minCut(capacity, source, sink) {
55272
55862
  }
55273
55863
  function astar(adj, start, goal, heuristic) {
55274
55864
  const n = adj.length;
55275
- if (start < 0 || start >= n || goal < 0 || goal >= n) {
55865
+ if (!Array.isArray(adj) || !Number.isInteger(n) || n < 0) {
55866
+ throw new Error("astar: adjacency must be a square number[][]");
55867
+ }
55868
+ if (!Number.isInteger(start) || !Number.isInteger(goal) || start < 0 || start >= n || goal < 0 || goal >= n) {
55276
55869
  throw new Error("astar: start or goal out of bounds");
55277
55870
  }
55278
55871
  if (start === goal) {
@@ -55307,7 +55900,8 @@ function astar(adj, start, goal, heuristic) {
55307
55900
  return { path: [], cost: Infinity };
55308
55901
  }
55309
55902
  const path = [];
55310
- for (let v = goal; v !== -1; v = cameFrom[v]) {
55903
+ for (let v = goal, guard = 0; v !== -1 && guard <= n; v = cameFrom[v], guard += 1) {
55904
+ if (!Number.isInteger(v) || v < 0 || v >= n) break;
55311
55905
  path.unshift(v);
55312
55906
  if (v === start) break;
55313
55907
  }
@@ -55707,14 +56301,14 @@ function _solveLinearSystem(A, b) {
55707
56301
  }
55708
56302
  return M.map((row2) => row2[n]);
55709
56303
  }
55710
- function katzCentrality(adj, alpha, beta2 = 1) {
56304
+ function katzCentrality(adj, alpha2, beta3 = 1) {
55711
56305
  const n = adj.length;
55712
56306
  if (n === 0) return [];
55713
56307
  const M = Array.from(
55714
56308
  { length: n },
55715
- (_, i) => Array.from({ length: n }, (_2, j) => (i === j ? 1 : 0) - alpha * adj[j][i])
56309
+ (_, i) => Array.from({ length: n }, (_2, j) => (i === j ? 1 : 0) - alpha2 * adj[j][i])
55716
56310
  );
55717
- const b = new Array(n).fill(beta2);
56311
+ const b = new Array(n).fill(beta3);
55718
56312
  const x = _solveLinearSystem(M, b);
55719
56313
  let sum3 = 0;
55720
56314
  let sumSq = 0;
@@ -55846,7 +56440,7 @@ function glm(X, y, opts) {
55846
56440
  const tol = opts.tol ?? 1e-10;
55847
56441
  const maxIter = opts.maxIter ?? 100;
55848
56442
  let eta = family.mustart(y).map((mu2) => linkFns.linkFn(mu2));
55849
- let beta2 = new Array(p).fill(0);
56443
+ let beta3 = new Array(p).fill(0);
55850
56444
  let iterations = 0;
55851
56445
  for (let iter = 0; iter < maxIter; iter++) {
55852
56446
  iterations = iter + 1;
@@ -55883,7 +56477,7 @@ function glm(X, y, opts) {
55883
56477
  const newEta = design.map((row2) => row2.reduce((s, v, j) => s + v * newBeta[j], 0));
55884
56478
  let maxDiff = 0;
55885
56479
  for (let i = 0; i < n; i++) maxDiff = Math.max(maxDiff, Math.abs(newEta[i] - eta[i]));
55886
- beta2 = newBeta;
56480
+ beta3 = newBeta;
55887
56481
  eta = newEta;
55888
56482
  if (maxDiff < tol) break;
55889
56483
  }
@@ -55891,9 +56485,9 @@ function glm(X, y, opts) {
55891
56485
  const deviance = y.reduce((sum3, yi, i) => sum3 + family.deviance(yi, mu[i]), 0);
55892
56486
  const predict = (x) => {
55893
56487
  const Dx = useIntercept ? x.map((row2) => [1, ...row2]) : x.map((row2) => [...row2]);
55894
- return Dx.map((row2) => linkFns.linkInv(row2.reduce((s, v, j) => s + v * beta2[j], 0)));
56488
+ return Dx.map((row2) => linkFns.linkInv(row2.reduce((s, v, j) => s + v * beta3[j], 0)));
55895
56489
  };
55896
- return { coefficients: beta2, fittedValues: mu, deviance, iterations, predict };
56490
+ return { coefficients: beta3, fittedValues: mu, deviance, iterations, predict };
55897
56491
  }
55898
56492
 
55899
56493
  // src/stats/mvn.ts
@@ -55958,22 +56552,22 @@ function mvnSample(mean7, cov2, n, opts) {
55958
56552
  }
55959
56553
 
55960
56554
  // src/stats/power-analysis.ts
55961
- function powerAt(effectSize, nobs, alpha, alt) {
56555
+ function powerAt(effectSize, nobs, alpha2, alt) {
55962
56556
  const df = 2 * (nobs - 1);
55963
56557
  const nc = effectSize * Math.sqrt(nobs / 2);
55964
56558
  if (alt === "larger") {
55965
- const tCrit2 = studentTQuantile(1 - alpha, df);
56559
+ const tCrit2 = studentTQuantile(1 - alpha2, df);
55966
56560
  return 1 - noncentralTCDF(tCrit2, df, nc);
55967
56561
  }
55968
56562
  if (alt === "smaller") {
55969
- const tCrit2 = studentTQuantile(alpha, df);
56563
+ const tCrit2 = studentTQuantile(alpha2, df);
55970
56564
  return noncentralTCDF(tCrit2, df, nc);
55971
56565
  }
55972
- const tCrit = studentTQuantile(1 - alpha / 2, df);
56566
+ const tCrit = studentTQuantile(1 - alpha2 / 2, df);
55973
56567
  return 1 - noncentralTCDF(tCrit, df, nc) + noncentralTCDF(-tCrit, df, nc);
55974
56568
  }
55975
- function tTestPower(effectSize, nobsOrPower, alpha, opts = {}) {
55976
- if (!(alpha > 0 && alpha < 1)) throw new Error("tTestPower: alpha must be in (0, 1)");
56569
+ function tTestPower(effectSize, nobsOrPower, alpha2, opts = {}) {
56570
+ if (!(alpha2 > 0 && alpha2 < 1)) throw new Error("tTestPower: alpha must be in (0, 1)");
55977
56571
  const alt = opts.alternative ?? "two-sided";
55978
56572
  if (opts.solveFor === "nobs") {
55979
56573
  const targetPower = nobsOrPower;
@@ -55983,19 +56577,19 @@ function tTestPower(effectSize, nobsOrPower, alpha, opts = {}) {
55983
56577
  if (effectSize === 0) throw new Error("tTestPower: cannot solve for nobs when effectSize is 0");
55984
56578
  let lo = 2 + 1e-9;
55985
56579
  let hi = 1e7;
55986
- if (powerAt(effectSize, hi, alpha, alt) < targetPower) {
56580
+ if (powerAt(effectSize, hi, alpha2, alt) < targetPower) {
55987
56581
  throw new Error("tTestPower: target power unreachable even at nobs = 1e7");
55988
56582
  }
55989
56583
  for (let it = 0; it < 200; it++) {
55990
56584
  const mid = (lo + hi) / 2;
55991
- if (powerAt(effectSize, mid, alpha, alt) < targetPower) lo = mid;
56585
+ if (powerAt(effectSize, mid, alpha2, alt) < targetPower) lo = mid;
55992
56586
  else hi = mid;
55993
56587
  }
55994
56588
  return (lo + hi) / 2;
55995
56589
  }
55996
56590
  const nobs = nobsOrPower;
55997
56591
  if (!(nobs > 1)) throw new Error("tTestPower: nobs must be greater than 1");
55998
- return powerAt(effectSize, nobs, alpha, alt);
56592
+ return powerAt(effectSize, nobs, alpha2, alt);
55999
56593
  }
56000
56594
 
56001
56595
  // src/stats/gaussian-process.ts
@@ -56131,29 +56725,29 @@ function gaussianProcessRegression(X, y, options = {}) {
56131
56725
  var gpRegression = gaussianProcessRegression;
56132
56726
 
56133
56727
  // src/stats/multivariate-sampling.ts
56134
- function validateAlpha(alpha, fn) {
56135
- if (!Array.isArray(alpha) || alpha.length < 2) {
56728
+ function validateAlpha(alpha2, fn) {
56729
+ if (!Array.isArray(alpha2) || alpha2.length < 2) {
56136
56730
  throw new Error(`${fn}: alpha must be an array of at least 2 concentration parameters`);
56137
56731
  }
56138
- for (const a of alpha) {
56732
+ for (const a of alpha2) {
56139
56733
  if (!(a > 0) || !Number.isFinite(a)) {
56140
56734
  throw new Error(`${fn}: all concentration parameters must be positive and finite`);
56141
56735
  }
56142
56736
  }
56143
56737
  }
56144
- function dirichletSample(alpha, n = 1, opts) {
56145
- validateAlpha(alpha, "dirichletSample");
56738
+ function dirichletSample(alpha2, n = 1, opts) {
56739
+ validateAlpha(alpha2, "dirichletSample");
56146
56740
  if (!Number.isInteger(n) || n < 1) {
56147
56741
  throw new Error("dirichletSample: n must be a positive integer");
56148
56742
  }
56149
- const k = alpha.length;
56743
+ const k = alpha2.length;
56150
56744
  const rng = createRng(opts?.seed ?? null);
56151
56745
  const samples = new Array(n);
56152
56746
  for (let s = 0; s < n; s++) {
56153
56747
  const g = new Array(k);
56154
56748
  let total = 0;
56155
56749
  for (let i = 0; i < k; i++) {
56156
- const gi = gammaSampleRng(alpha[i], rng);
56750
+ const gi = gammaSampleRng(alpha2[i], rng);
56157
56751
  g[i] = gi;
56158
56752
  total += gi;
56159
56753
  }
@@ -56163,10 +56757,10 @@ function dirichletSample(alpha, n = 1, opts) {
56163
56757
  }
56164
56758
  return samples;
56165
56759
  }
56166
- function dirichletPdf(x, alpha) {
56167
- validateAlpha(alpha, "dirichletPdf");
56168
- if (x.length !== alpha.length) {
56169
- throw new Error(`dirichletPdf: x length ${x.length} must match alpha length ${alpha.length}`);
56760
+ function dirichletPdf(x, alpha2) {
56761
+ validateAlpha(alpha2, "dirichletPdf");
56762
+ if (x.length !== alpha2.length) {
56763
+ throw new Error(`dirichletPdf: x length ${x.length} must match alpha length ${alpha2.length}`);
56170
56764
  }
56171
56765
  let sum3 = 0;
56172
56766
  for (const xi of x) {
@@ -56180,7 +56774,7 @@ function dirichletPdf(x, alpha) {
56180
56774
  }
56181
56775
  let a0 = 0;
56182
56776
  let logB = 0;
56183
- for (const a of alpha) {
56777
+ for (const a of alpha2) {
56184
56778
  logB += lgammaNumber(a);
56185
56779
  a0 += a;
56186
56780
  }
@@ -56188,7 +56782,7 @@ function dirichletPdf(x, alpha) {
56188
56782
  let logpdf = -logB;
56189
56783
  for (let i = 0; i < x.length; i++) {
56190
56784
  const xi = x[i];
56191
- const p = alpha[i] - 1;
56785
+ const p = alpha2[i] - 1;
56192
56786
  if (p !== 0) {
56193
56787
  logpdf += p * Math.log(xi);
56194
56788
  }
@@ -56634,6 +57228,7 @@ export {
56634
57228
  fallingFactorial,
56635
57229
  faraday,
56636
57230
  fermiCoupling,
57231
+ ferrersP,
56637
57232
  fft,
56638
57233
  fft2d,
56639
57234
  fftGpuDispatch,
@@ -57191,9 +57786,14 @@ export {
57191
57786
  spearman,
57192
57787
  spearmanr,
57193
57788
  spectralClustering,
57789
+ spectralRadiance,
57194
57790
  spectrogram,
57195
57791
  speedOfLight,
57196
57792
  sphericalVoronoi,
57793
+ spheroidalAngular,
57794
+ spheroidalCharacteristic,
57795
+ spheroidalLambda,
57796
+ spheroidalRadial,
57197
57797
  splitUnit,
57198
57798
  sqrt,
57199
57799
  sqrtm,