@danielsimonjr/mathts-functions 0.62.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.
Files changed (43) hide show
  1. package/dist/algebra/derivative.d.ts.map +1 -1
  2. package/dist/algebra/sparse/csLu.d.ts.map +1 -1
  3. package/dist/arithmetic/pow.d.ts.map +1 -1
  4. package/dist/arithmetic/subtract.d.ts.map +1 -1
  5. package/dist/bitwise/rightLogShift.d.ts.map +1 -1
  6. package/dist/cas/layer3.d.ts +50 -0
  7. package/dist/cas/layer3.d.ts.map +1 -0
  8. package/dist/cas/rat.d.ts +17 -0
  9. package/dist/cas/rat.d.ts.map +1 -0
  10. package/dist/cas/rational-integrate.d.ts +3 -10
  11. package/dist/cas/rational-integrate.d.ts.map +1 -1
  12. package/dist/cas-integration.d.ts +3 -3
  13. package/dist/cas-integration.d.ts.map +1 -1
  14. package/dist/core/create.d.ts.map +1 -1
  15. package/dist/core/function/typed.d.ts.map +1 -1
  16. package/dist/factories/index.d.ts +5 -4
  17. package/dist/factories/index.d.ts.map +1 -1
  18. package/dist/geometry/intersect.d.ts +0 -4
  19. package/dist/geometry/intersect.d.ts.map +1 -1
  20. package/dist/graph/optimization.d.ts.map +1 -1
  21. package/dist/index.d.ts +1 -0
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +1234 -359
  24. package/dist/matrix/concat.d.ts.map +1 -1
  25. package/dist/matrix/matrixFromColumns.d.ts +7 -1
  26. package/dist/matrix/matrixFromColumns.d.ts.map +1 -1
  27. package/dist/matrix/matrixFromRows.d.ts +7 -1
  28. package/dist/matrix/matrixFromRows.d.ts.map +1 -1
  29. package/dist/probability/permutations.d.ts.map +1 -1
  30. package/dist/special/spheroidal.d.ts +47 -0
  31. package/dist/special/spheroidal.d.ts.map +1 -0
  32. package/dist/type/complex/Complex.d.ts.map +1 -1
  33. package/dist/type/matrix/utils/matAlgo03xDSf.d.ts.map +1 -1
  34. package/dist/type/matrix/utils/matAlgo11xS0s.d.ts.map +1 -1
  35. package/dist/type/unit/physicalConstants.d.ts +1 -0
  36. package/dist/type/unit/physicalConstants.d.ts.map +1 -1
  37. package/dist/typed/graph.d.ts.map +1 -1
  38. package/dist/typed/numeric.d.ts.map +1 -1
  39. package/dist/typed/polynomial-ideal.d.ts.map +1 -1
  40. package/dist/utils/bignumber/bitwise.d.ts +22 -0
  41. package/dist/utils/bignumber/bitwise.d.ts.map +1 -1
  42. package/dist/wasm/special/scalars.d.ts.map +1 -1
  43. package/package.json +76 -76
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
  }
@@ -9609,13 +9609,23 @@ function normalize(p) {
9609
9609
  if (existing) existing.coeff += t.coeff;
9610
9610
  else byKey.set(key2, { coeff: t.coeff, powers: [...t.powers] });
9611
9611
  }
9612
- return [...byKey.values()].filter((t) => Math.abs(t.coeff) > EPS).sort((a, b) => cmpPowers(b.powers, a.powers));
9612
+ const out = [];
9613
+ for (const t of byKey.values()) {
9614
+ if (Math.abs(t.coeff) > EPS) {
9615
+ out.push(t);
9616
+ }
9617
+ }
9618
+ return out.sort((a, b) => cmpPowers(b.powers, a.powers));
9613
9619
  }
9614
9620
  function polyAdd(a, b) {
9615
9621
  return normalize([...a, ...b]);
9616
9622
  }
9617
9623
  function polyNeg(a) {
9618
- return a.map((t) => ({ coeff: -t.coeff, powers: [...t.powers] }));
9624
+ const out = [];
9625
+ for (let i = 0; i < a.length; i++) {
9626
+ out.push({ coeff: -a[i].coeff, powers: [...a[i].powers] });
9627
+ }
9628
+ return out;
9619
9629
  }
9620
9630
  function polySub(a, b) {
9621
9631
  return polyAdd(a, polyNeg(b));
@@ -9624,9 +9634,13 @@ function polyMul(a, b) {
9624
9634
  const out = [];
9625
9635
  for (const ta of a) {
9626
9636
  for (const tb of b) {
9637
+ const powers = new Array(ta.powers.length);
9638
+ for (let i = 0; i < ta.powers.length; i++) {
9639
+ powers[i] = ta.powers[i] + tb.powers[i];
9640
+ }
9627
9641
  out.push({
9628
9642
  coeff: ta.coeff * tb.coeff,
9629
- powers: ta.powers.map((p, i) => p + tb.powers[i])
9643
+ powers
9630
9644
  });
9631
9645
  }
9632
9646
  }
@@ -9639,12 +9653,24 @@ function polyPow(a, n, nVars) {
9639
9653
  }
9640
9654
  function polyFromExpression(expr, vars) {
9641
9655
  const nVars = vars.length;
9642
- const varIndex = new Map(vars.map((v, i) => [v, i]));
9656
+ const varIndex = /* @__PURE__ */ new Map();
9657
+ for (let i = 0; i < vars.length; i++) {
9658
+ varIndex.set(vars[i], i);
9659
+ }
9643
9660
  let pos = 0;
9644
9661
  const constPoly = (v) => normalize([{ coeff: v, powers: new Array(nVars).fill(0) }]);
9645
9662
  function constOf(p) {
9646
9663
  if (p.length === 0) return 0;
9647
- if (p.length === 1 && p[0].powers.every((x) => x === 0)) return p[0].coeff;
9664
+ if (p.length === 1) {
9665
+ let isConst2 = true;
9666
+ for (let i = 0; i < p[0].powers.length; i++) {
9667
+ if (p[0].powers[i] !== 0) {
9668
+ isConst2 = false;
9669
+ break;
9670
+ }
9671
+ }
9672
+ if (isConst2) return p[0].coeff;
9673
+ }
9648
9674
  return null;
9649
9675
  }
9650
9676
  const skipWs = () => {
@@ -9727,7 +9753,11 @@ function polyFromExpression(expr, vars) {
9727
9753
  if (denom === null || Math.abs(denom) <= EPS) {
9728
9754
  return fail("division only by a nonzero numeric constant");
9729
9755
  }
9730
- p = normalize(p.map((t) => ({ coeff: t.coeff / denom, powers: t.powers })));
9756
+ const nextP = [];
9757
+ for (let i = 0; i < p.length; i++) {
9758
+ nextP.push({ coeff: p[i].coeff / denom, powers: p[i].powers });
9759
+ }
9760
+ p = normalize(nextP);
9731
9761
  } else {
9732
9762
  break;
9733
9763
  }
@@ -9806,10 +9836,14 @@ function polyReduce(p, G) {
9806
9836
  const lt = work[0];
9807
9837
  const g = index.find(lt.powers);
9808
9838
  if (g) {
9839
+ const powers = new Array(lt.powers.length);
9840
+ for (let i = 0; i < lt.powers.length; i++) {
9841
+ powers[i] = lt.powers[i] - g[0].powers[i];
9842
+ }
9809
9843
  const factor2 = [
9810
9844
  {
9811
9845
  coeff: lt.coeff / g[0].coeff,
9812
- powers: lt.powers.map((x, i) => x - g[0].powers[i])
9846
+ powers
9813
9847
  }
9814
9848
  ];
9815
9849
  work = polySub(work, polyMul(factor2, g));
@@ -9821,21 +9855,42 @@ function polyReduce(p, G) {
9821
9855
  return normalize(rem);
9822
9856
  }
9823
9857
  function lcmPowers(a, b) {
9824
- return a.map((x, i) => Math.max(x, b[i]));
9858
+ const len = a.length;
9859
+ const out = new Array(len);
9860
+ for (let i = 0; i < len; i++) {
9861
+ out[i] = Math.max(a[i], b[i]);
9862
+ }
9863
+ return out;
9825
9864
  }
9826
9865
  function sPoly(f, g) {
9827
9866
  const l = lcmPowers(f[0].powers, g[0].powers);
9828
- const mf = [{ coeff: 1 / f[0].coeff, powers: l.map((x, i) => x - f[0].powers[i]) }];
9829
- const mg = [{ coeff: 1 / g[0].coeff, powers: l.map((x, i) => x - g[0].powers[i]) }];
9867
+ const mfPowers = new Array(l.length);
9868
+ const mgPowers = new Array(l.length);
9869
+ for (let i = 0; i < l.length; i++) {
9870
+ mfPowers[i] = l[i] - f[0].powers[i];
9871
+ mgPowers[i] = l[i] - g[0].powers[i];
9872
+ }
9873
+ const mf = [{ coeff: 1 / f[0].coeff, powers: mfPowers }];
9874
+ const mg = [{ coeff: 1 / g[0].coeff, powers: mgPowers }];
9830
9875
  return polySub(polyMul(mf, f), polyMul(mg, g));
9831
9876
  }
9832
9877
  function monic(p) {
9833
9878
  if (p.length === 0) return p;
9834
9879
  const c = p[0].coeff;
9835
- return p.map((t) => ({ coeff: t.coeff / c, powers: [...t.powers] }));
9880
+ const out = [];
9881
+ for (let i = 0; i < p.length; i++) {
9882
+ out.push({ coeff: p[i].coeff / c, powers: [...p[i].powers] });
9883
+ }
9884
+ return out;
9836
9885
  }
9837
9886
  function buchberger(input) {
9838
- let G = input.map(normalize).filter((p) => p.length > 0).map(monic);
9887
+ let G = [];
9888
+ for (let i = 0; i < input.length; i++) {
9889
+ const p = normalize(input[i]);
9890
+ if (p.length > 0) {
9891
+ G.push(monic(p));
9892
+ }
9893
+ }
9839
9894
  if (G.length === 0) return [];
9840
9895
  const pairs = [];
9841
9896
  for (let i = 0; i < G.length; i++) {
@@ -9857,19 +9912,31 @@ function buchberger(input) {
9857
9912
  for (let t = 0; t < k; t++) pairs.push([t, k]);
9858
9913
  }
9859
9914
  }
9860
- G = G.filter(
9861
- (g, i) => !G.some(
9862
- (h, j) => j !== i && h.length > 0 && divides(h[0].powers, g[0].powers) && (cmpPowers(h[0].powers, g[0].powers) !== 0 || j < i)
9863
- )
9864
- );
9865
- G = G.map(
9866
- (g, i) => monic(
9867
- polyReduce(
9868
- g,
9869
- G.filter((_, j) => j !== i)
9870
- )
9871
- )
9872
- ).filter((g) => g.length > 0);
9915
+ const minimalG = [];
9916
+ for (let i = 0; i < G.length; i++) {
9917
+ const g = G[i];
9918
+ let isDivisible = false;
9919
+ for (let j = 0; j < G.length; j++) {
9920
+ if (i === j) continue;
9921
+ const h = G[j];
9922
+ if (h.length > 0 && divides(h[0].powers, g[0].powers) && (cmpPowers(h[0].powers, g[0].powers) !== 0 || j < i)) {
9923
+ isDivisible = true;
9924
+ break;
9925
+ }
9926
+ }
9927
+ if (!isDivisible) minimalG.push(g);
9928
+ }
9929
+ G = minimalG;
9930
+ const reducedG = [];
9931
+ for (let i = 0; i < G.length; i++) {
9932
+ const others = [];
9933
+ for (let j = 0; j < G.length; j++) {
9934
+ if (i !== j) others.push(G[j]);
9935
+ }
9936
+ const reduced = monic(polyReduce(G[i], others));
9937
+ if (reduced.length > 0) reducedG.push(reduced);
9938
+ }
9939
+ G = reducedG;
9873
9940
  G.sort((a, b) => cmpPowers(b[0].powers, a[0].powers));
9874
9941
  return G;
9875
9942
  }
@@ -9879,10 +9946,18 @@ function polyToString(p, vars) {
9879
9946
  const r = Math.round(c);
9880
9947
  return Math.abs(c - r) < 1e-9 ? String(r) : String(Number(c.toPrecision(12)));
9881
9948
  };
9882
- const terms = [...p].reverse().map((t) => {
9883
- const varPart = t.powers.map((e, i) => e === 0 ? "" : e === 1 ? vars[i] : `${vars[i]}^${e}`).filter(Boolean).join("*");
9884
- return varPart ? `${fmt(t.coeff)}*${varPart}` : fmt(t.coeff);
9885
- });
9949
+ const terms = [];
9950
+ for (let i = p.length - 1; i >= 0; i--) {
9951
+ const t = p[i];
9952
+ const parts = [];
9953
+ for (let j = 0; j < t.powers.length; j++) {
9954
+ const e = t.powers[j];
9955
+ if (e === 1) parts.push(vars[j]);
9956
+ else if (e > 1) parts.push(`${vars[j]}^${e}`);
9957
+ }
9958
+ const varPart = parts.join("*");
9959
+ terms.push(varPart ? `${fmt(t.coeff)}*${varPart}` : fmt(t.coeff));
9960
+ }
9886
9961
  return terms.join(" + ").replace(/\+ -/g, "- ");
9887
9962
  }
9888
9963
 
@@ -12574,7 +12649,7 @@ function linearInterp(xs, ys, x) {
12574
12649
  if (xs.length !== ys.length || xs.length < 2) {
12575
12650
  throw new Error("linearInterp requires at least 2 data points with matching lengths");
12576
12651
  }
12577
- let i = 0;
12652
+ let i;
12578
12653
  if (x <= xs[0]) {
12579
12654
  i = 0;
12580
12655
  } else if (x >= xs[xs.length - 1]) {
@@ -12761,7 +12836,7 @@ function pchipInterp(xs, ys, x) {
12761
12836
  delta[n - 3]
12762
12837
  );
12763
12838
  }
12764
- let i = 0;
12839
+ let i;
12765
12840
  if (x <= xs[0]) {
12766
12841
  i = 0;
12767
12842
  } else if (x >= xs[n - 1]) {
@@ -13738,7 +13813,7 @@ function bdfSolve(f, tspan, y0raw, options = {}) {
13738
13813
  const F0 = _fArr(f, t, y);
13739
13814
  const J = _jacobianAt(f, t, y, F0, options);
13740
13815
  let stepAccepted = false;
13741
- let yNew = y;
13816
+ let yNew;
13742
13817
  let dVec = new Array(n).fill(0);
13743
13818
  let errorNorm = 0;
13744
13819
  let nIter = 1;
@@ -15142,7 +15217,7 @@ function minimize(f, x0, opts) {
15142
15217
  const maxIter = opts?.maxIter ?? 1e3;
15143
15218
  const step = opts?.step ?? 0.1;
15144
15219
  const n = x0.length;
15145
- const alpha = 1;
15220
+ const alpha2 = 1;
15146
15221
  const gamma2 = 2;
15147
15222
  const rho = 0.5;
15148
15223
  const sigma = 0.5;
@@ -15174,7 +15249,7 @@ function minimize(f, x0, opts) {
15174
15249
  }
15175
15250
  }
15176
15251
  for (let j = 0; j < n; j++) centroid2[j] /= n;
15177
- 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]));
15178
15253
  const fr = f(xr);
15179
15254
  if (fr < fValues[n - 1] && fr >= fValues[0]) {
15180
15255
  simplex[n] = xr;
@@ -15307,9 +15382,9 @@ function interpolate(xs, ys, method = "linear") {
15307
15382
  const n = xs.length - 1;
15308
15383
  const h = [];
15309
15384
  for (let i = 0; i < n; i++) h[i] = xs[i + 1] - xs[i];
15310
- const alpha = [0];
15385
+ const alpha2 = [0];
15311
15386
  for (let i = 1; i < n; i++) {
15312
- 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]);
15313
15388
  }
15314
15389
  const l = [1];
15315
15390
  const mu = [0];
@@ -15317,7 +15392,7 @@ function interpolate(xs, ys, method = "linear") {
15317
15392
  for (let i = 1; i < n; i++) {
15318
15393
  l[i] = 2 * (xs[i + 1] - xs[i - 1]) - h[i - 1] * mu[i - 1];
15319
15394
  mu[i] = h[i] / l[i];
15320
- 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];
15321
15396
  }
15322
15397
  const c = new Array(n + 1).fill(0);
15323
15398
  const bArr = new Array(n);
@@ -15333,7 +15408,6 @@ function interpolate(xs, ys, method = "linear") {
15333
15408
  if (x <= xs[0]) j = 0;
15334
15409
  else if (x >= xs[n]) j = n - 1;
15335
15410
  else {
15336
- j = 0;
15337
15411
  for (j = 0; j < n; j++) {
15338
15412
  if (x >= xs[j] && x <= xs[j + 1]) break;
15339
15413
  }
@@ -15345,7 +15419,7 @@ function interpolate(xs, ys, method = "linear") {
15345
15419
  case "linear":
15346
15420
  default:
15347
15421
  return (x) => {
15348
- let i = 0;
15422
+ let i;
15349
15423
  if (x <= xs[0]) i = 0;
15350
15424
  else if (x >= xs[xs.length - 1]) i = xs.length - 2;
15351
15425
  else {
@@ -15385,7 +15459,7 @@ function pchip(xs, ys, x) {
15385
15459
  m[0] = delta[0];
15386
15460
  m[n - 1] = delta[n - 2];
15387
15461
  }
15388
- let idx = 0;
15462
+ let idx;
15389
15463
  if (x <= xs[0]) idx = 0;
15390
15464
  else if (x >= xs[n - 1]) idx = n - 2;
15391
15465
  else {
@@ -15462,9 +15536,9 @@ function bspline(controlPoints, degree3, t) {
15462
15536
  for (let r = 1; r <= p; r++) {
15463
15537
  for (let j = p; j >= r; j--) {
15464
15538
  const idx = k - p + j;
15465
- 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]);
15466
15540
  for (let dd = 0; dd < dim; dd++) {
15467
- 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];
15468
15542
  }
15469
15543
  }
15470
15544
  }
@@ -16662,11 +16736,11 @@ function linprog(c, arg22, arg3) {
16662
16736
  return linprogTwoPhase(c, arg22);
16663
16737
  }
16664
16738
  function solvePDE(pde, domain, bc) {
16665
- const { alpha } = pde;
16739
+ const { alpha: alpha2 } = pde;
16666
16740
  const { L, nx, nt, T } = domain;
16667
16741
  const dx = L / (nx - 1);
16668
16742
  const dt = T / nt;
16669
- const r = alpha * dt / (dx * dx);
16743
+ const r = alpha2 * dt / (dx * dx);
16670
16744
  if (r > 0.5) {
16671
16745
  }
16672
16746
  const x = [];
@@ -17511,6 +17585,8 @@ function _betweennessOnce(adj, directed, normalise, seed) {
17511
17585
  if (denom > 0) {
17512
17586
  for (let i = 0; i < n; i++) cb[i] /= denom;
17513
17587
  }
17588
+ } else if (!directed) {
17589
+ for (let i = 0; i < n; i++) cb[i] /= 2;
17514
17590
  }
17515
17591
  return cb;
17516
17592
  }
@@ -17674,11 +17750,11 @@ function normalSampleRng(rng) {
17674
17750
  const u2 = rng();
17675
17751
  return Math.sqrt(-2 * Math.log(u1 < 1e-300 ? 1e-300 : u1)) * Math.cos(2 * Math.PI * u2);
17676
17752
  }
17677
- function gammaSampleRng(alpha, rng) {
17678
- if (alpha < 1) {
17679
- 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);
17680
17756
  }
17681
- const d = alpha - 1 / 3;
17757
+ const d = alpha2 - 1 / 3;
17682
17758
  const c = 1 / Math.sqrt(9 * d);
17683
17759
  for (; ; ) {
17684
17760
  let x;
@@ -17894,28 +17970,28 @@ function normalDist(mu = 0, sigma = 1) {
17894
17970
  })
17895
17971
  };
17896
17972
  }
17897
- function betaDist(alpha, beta_) {
17898
- if (alpha <= 0 || beta_ <= 0) throw new Error("betaDist: alpha and beta must be positive");
17899
- 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_);
17900
17976
  return {
17901
17977
  pdf: (x) => {
17902
17978
  if (x < 0 || x > 1) return 0;
17903
- if (x === 0 && alpha < 1) return Infinity;
17979
+ if (x === 0 && alpha2 < 1) return Infinity;
17904
17980
  if (x === 1 && beta_ < 1) return Infinity;
17905
- 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;
17906
17982
  },
17907
17983
  cdf: (x) => {
17908
17984
  if (x <= 0) return 0;
17909
17985
  if (x >= 1) return 1;
17910
- return _betainc(x, alpha, beta_);
17986
+ return _betainc(x, alpha2, beta_);
17911
17987
  },
17912
17988
  quantile: (p) => {
17913
17989
  if (p <= 0) return 0;
17914
17990
  if (p >= 1) return 1;
17915
- let x = alpha / (alpha + beta_);
17991
+ let x = alpha2 / (alpha2 + beta_);
17916
17992
  for (let i = 0; i < 100; i++) {
17917
- const fx = _betainc(x, alpha, beta_) - p;
17918
- 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;
17919
17995
  if (dx === 0) break;
17920
17996
  const step = fx / dx;
17921
17997
  x = Math.max(1e-15, Math.min(1 - 1e-15, x - step));
@@ -17923,17 +17999,17 @@ function betaDist(alpha, beta_) {
17923
17999
  }
17924
18000
  return x;
17925
18001
  },
17926
- mean: alpha / (alpha + beta_),
17927
- variance: alpha * beta_ / ((alpha + beta_) ** 2 * (alpha + beta_ + 1)),
18002
+ mean: alpha2 / (alpha2 + beta_),
18003
+ variance: alpha2 * beta_ / ((alpha2 + beta_) ** 2 * (alpha2 + beta_ + 1)),
17928
18004
  sample: () => {
17929
- const ga = _gammaRandom(alpha);
18005
+ const ga = _gammaRandom(alpha2);
17930
18006
  const gb = _gammaRandom(beta_);
17931
18007
  return ga / (ga + gb);
17932
18008
  },
17933
- sampleN: (n, opts) => _sampleNDispatch("beta", [alpha, beta_], n, opts, (count2, rng) => {
18009
+ sampleN: (n, opts) => _sampleNDispatch("beta", [alpha2, beta_], n, opts, (count2, rng) => {
17934
18010
  const out = new Float64Array(count2);
17935
18011
  for (let i = 0; i < count2; i++) {
17936
- const ga = _gammaRandomRng(alpha, rng);
18012
+ const ga = _gammaRandomRng(alpha2, rng);
17937
18013
  const gb = _gammaRandomRng(beta_, rng);
17938
18014
  out[i] = ga / (ga + gb);
17939
18015
  }
@@ -17941,14 +18017,14 @@ function betaDist(alpha, beta_) {
17941
18017
  })
17942
18018
  };
17943
18019
  }
17944
- function _gammaRandom(alpha) {
17945
- return gammaSampleRng(alpha, Math.random);
18020
+ function _gammaRandom(alpha2) {
18021
+ return gammaSampleRng(alpha2, Math.random);
17946
18022
  }
17947
18023
  function _normalRandom() {
17948
18024
  return normalSampleRng(Math.random);
17949
18025
  }
17950
- function _gammaRandomRng(alpha, rng) {
17951
- return gammaSampleRng(alpha, rng);
18026
+ function _gammaRandomRng(alpha2, rng) {
18027
+ return gammaSampleRng(alpha2, rng);
17952
18028
  }
17953
18029
  function binomialDist(n, p) {
17954
18030
  if (!Number.isInteger(n) || n < 0)
@@ -18098,7 +18174,7 @@ function fDist(d1, d2) {
18098
18174
  quantile: (p) => {
18099
18175
  if (p <= 0) return 0;
18100
18176
  if (p >= 1) return Infinity;
18101
- let x = d1 > 2 ? d2 * (d1 - 2) / (d1 * (d2 + 2)) : 1;
18177
+ let x;
18102
18178
  let lo = 0;
18103
18179
  let hi = 1e3;
18104
18180
  for (let i = 0; i < 100; i++) {
@@ -18563,18 +18639,18 @@ function discreteUniformDist(lo, hi) {
18563
18639
  })
18564
18640
  };
18565
18641
  }
18566
- function gumbelDist(mu, beta2) {
18567
- if (beta2 <= 0) throw new Error("gumbelDist: scale beta must be positive");
18568
- 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));
18569
18645
  return {
18570
18646
  pdf: (x) => {
18571
- const z = (x - mu) / beta2;
18572
- 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)));
18573
18649
  },
18574
- cdf: (x) => Math.exp(-Math.exp(-(x - mu) / beta2)),
18650
+ cdf: (x) => Math.exp(-Math.exp(-(x - mu) / beta3)),
18575
18651
  quantile,
18576
- mean: mu + beta2 * EULER_GAMMA,
18577
- variance: Math.PI * Math.PI / 6 * beta2 * beta2,
18652
+ mean: mu + beta3 * EULER_GAMMA,
18653
+ variance: Math.PI * Math.PI / 6 * beta3 * beta3,
18578
18654
  sample: () => _contSample(quantile, Math.random),
18579
18655
  sampleN: (n, opts) => _sampleNDispatch(null, [], n, opts, (count2, rng) => {
18580
18656
  const out = new Float64Array(count2);
@@ -19766,12 +19842,12 @@ function dagostinoTest(data) {
19766
19842
  if (n < 8) throw new Error("dagostinoTest: need at least 8 observations");
19767
19843
  const { skew, kurtPearson } = _skewKurt(data);
19768
19844
  const y = skew * Math.sqrt((n + 1) * (n + 3) / (6 * (n - 2)));
19769
- const beta2 = 3 * (n * n + 27 * n - 70) * (n + 1) * (n + 3) / ((n - 2) * (n + 5) * (n + 7) * (n + 9));
19770
- 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));
19771
19847
  const delta = 1 / Math.sqrt(0.5 * Math.log(w2));
19772
- const alpha = Math.sqrt(2 / (w2 - 1));
19848
+ const alpha2 = Math.sqrt(2 / (w2 - 1));
19773
19849
  const yy = y === 0 ? 1 : y;
19774
- 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));
19775
19851
  const E = 3 * (n - 1) / (n + 1);
19776
19852
  const varb2 = 24 * n * (n - 2) * (n - 3) / ((n + 1) * (n + 1) * (n + 3) * (n + 5));
19777
19853
  const xx = (kurtPearson - E) / Math.sqrt(varb2);
@@ -20157,14 +20233,14 @@ function hessenbergForm(A) {
20157
20233
  let normV = 0;
20158
20234
  for (let i = 0; i < len; i++) normV += v[i] * v[i];
20159
20235
  if (normV < 1e-30) continue;
20160
- const beta2 = 2 / normV;
20236
+ const beta3 = 2 / normV;
20161
20237
  for (let j = k; j < n; j++) {
20162
20238
  let dot9 = 0;
20163
20239
  for (let i = 0; i < len; i++) {
20164
20240
  dot9 += v[i] * H[k + 1 + i][j];
20165
20241
  }
20166
20242
  for (let i = 0; i < len; i++) {
20167
- H[k + 1 + i][j] -= beta2 * v[i] * dot9;
20243
+ H[k + 1 + i][j] -= beta3 * v[i] * dot9;
20168
20244
  }
20169
20245
  }
20170
20246
  for (let i = 0; i < n; i++) {
@@ -20173,7 +20249,7 @@ function hessenbergForm(A) {
20173
20249
  dot9 += H[i][k + 1 + j] * v[j];
20174
20250
  }
20175
20251
  for (let j = 0; j < len; j++) {
20176
- H[i][k + 1 + j] -= beta2 * dot9 * v[j];
20252
+ H[i][k + 1 + j] -= beta3 * dot9 * v[j];
20177
20253
  }
20178
20254
  }
20179
20255
  for (let i = 0; i < n; i++) {
@@ -20182,7 +20258,7 @@ function hessenbergForm(A) {
20182
20258
  dot9 += Q2[i][k + 1 + j] * v[j];
20183
20259
  }
20184
20260
  for (let j = 0; j < len; j++) {
20185
- Q2[i][k + 1 + j] -= beta2 * dot9 * v[j];
20261
+ Q2[i][k + 1 + j] -= beta3 * dot9 * v[j];
20186
20262
  }
20187
20263
  }
20188
20264
  }
@@ -21649,6 +21725,7 @@ __export(factories_exports, {
21649
21725
  solveODE: () => solveODE,
21650
21726
  sort: () => sort,
21651
21727
  sparse: () => sparse,
21728
+ spectralRadiance: () => spectralRadiance,
21652
21729
  speedOfLight: () => speedOfLight,
21653
21730
  splitUnit: () => splitUnit,
21654
21731
  sqrtm: () => sqrtm,
@@ -22381,6 +22458,36 @@ function rightArithShiftBigNumber(x, y) {
22381
22458
  }
22382
22459
  return x.div(new BigNumber9(2).pow(y)).floor();
22383
22460
  }
22461
+ function rightLogShiftBigNumber(x, y) {
22462
+ if (x.isFinite() && !x.isInteger() || y.isFinite() && !y.isInteger()) {
22463
+ throw new Error("Integers expected in function rightLogShift");
22464
+ }
22465
+ const BigNumber9 = x.constructor;
22466
+ if (x.isNaN() || y.isNaN() || y.isNegative() && !y.isZero()) {
22467
+ return new BigNumber9(NaN);
22468
+ }
22469
+ if (x.isZero() || y.isZero()) {
22470
+ return x;
22471
+ }
22472
+ if (!y.isFinite()) {
22473
+ if (x.isNegative() || !x.isFinite()) {
22474
+ return new BigNumber9(NaN);
22475
+ }
22476
+ return new BigNumber9(0);
22477
+ }
22478
+ let val = x;
22479
+ if (val.isNegative()) {
22480
+ const MASK = new BigNumber9(4294967296);
22481
+ val = val.mod(MASK);
22482
+ if (val.isNegative()) {
22483
+ val = val.plus(MASK);
22484
+ }
22485
+ }
22486
+ if (y.lt(55)) {
22487
+ return val.div(Math.pow(2, y.toNumber()) + "").floor();
22488
+ }
22489
+ return val.div(new BigNumber9(2).pow(y)).floor();
22490
+ }
22384
22491
 
22385
22492
  // src/bitwise/bitNot.ts
22386
22493
  var name15 = "bitNot";
@@ -24293,7 +24400,7 @@ var createIsPrime = /* @__PURE__ */ factory(
24293
24400
  d = d.div(2);
24294
24401
  r += 1;
24295
24402
  }
24296
- let bases = null;
24403
+ let bases;
24297
24404
  if (n.toBigInt() < 3317044064679887385961981n) {
24298
24405
  bases = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41].filter((x) => x < n.toNumber());
24299
24406
  } else {
@@ -25534,17 +25641,79 @@ var createMatrixFromColumns = /* @__PURE__ */ factory(
25534
25641
  (item) => typeof item.toArray === "function"
25535
25642
  );
25536
25643
  const hasArray = arr10.some((item) => Array.isArray(item));
25644
+ const isSparse = allMatrix && !hasArray && arr10.some(
25645
+ (item) => typeof item.storage === "function" && item.storage() === "sparse"
25646
+ );
25647
+ if (isSparse) {
25648
+ return _createSparseMatrixFromColumns(arr10);
25649
+ }
25537
25650
  const arrays = arr10.map(
25538
25651
  (item) => typeof item.toArray === "function" ? item.toArray() : item
25539
25652
  );
25540
25653
  const result = _createArray(arrays);
25541
25654
  if (allMatrix && !hasArray) {
25542
- return matrix2(result);
25655
+ return matrix2(result, "dense");
25543
25656
  }
25544
25657
  return result;
25545
25658
  }
25546
- // TODO implement this properly for SparseMatrix
25547
25659
  });
25660
+ function _createSparseMatrixFromColumns(arr10) {
25661
+ const M = arr10.length;
25662
+ const N2 = checkVectorTypeAndReturnLength(arr10[0]);
25663
+ const entries = [];
25664
+ for (let j = 0; j < M; j++) {
25665
+ const colVec = arr10[j];
25666
+ const colLength = checkVectorTypeAndReturnLength(colVec);
25667
+ if (colLength !== N2) {
25668
+ throw new TypeError(
25669
+ "The vectors had different length: " + (N2 | 0) + " \u2260 " + (colLength | 0)
25670
+ );
25671
+ }
25672
+ if (typeof colVec.forEach === "function") {
25673
+ const s = size2(colVec);
25674
+ const is1D = s.length === 1;
25675
+ const isCol = s.length === 2 && s[1] === 1;
25676
+ colVec.forEach((val, idx) => {
25677
+ let row2;
25678
+ if (is1D) {
25679
+ row2 = idx[0];
25680
+ } else if (isCol) {
25681
+ row2 = idx[0];
25682
+ } else {
25683
+ row2 = idx[1];
25684
+ }
25685
+ if (val !== 0) {
25686
+ entries.push({ row: row2, col: j, value: val });
25687
+ }
25688
+ });
25689
+ } else {
25690
+ const f = flatten5(colVec);
25691
+ for (let i = 0; i < N2; i++) {
25692
+ if (f[i] !== 0) {
25693
+ entries.push({ row: i, col: j, value: f[i] });
25694
+ }
25695
+ }
25696
+ }
25697
+ }
25698
+ entries.sort((a, b) => {
25699
+ if (a.col !== b.col) return a.col - b.col;
25700
+ return a.row - b.row;
25701
+ });
25702
+ const values = [];
25703
+ const index = [];
25704
+ const ptr = new Array(M + 1).fill(0);
25705
+ for (let i = 0; i < entries.length; i++) {
25706
+ ptr[entries[i].col + 1]++;
25707
+ }
25708
+ for (let j = 1; j <= M; j++) {
25709
+ ptr[j] += ptr[j - 1];
25710
+ }
25711
+ for (let i = 0; i < entries.length; i++) {
25712
+ values.push(entries[i].value);
25713
+ index.push(entries[i].row);
25714
+ }
25715
+ return matrix2({ values, index, ptr, size: [N2, M] }, "sparse");
25716
+ }
25548
25717
  function _createArray(arr10) {
25549
25718
  if (arr10.length === 0)
25550
25719
  throw new TypeError("At least one column is needed to construct a matrix.");
@@ -25603,17 +25772,79 @@ var createMatrixFromRows = /* @__PURE__ */ factory(
25603
25772
  (item) => typeof item.toArray === "function"
25604
25773
  );
25605
25774
  const hasArray = arr10.some((item) => Array.isArray(item));
25775
+ const isSparse = allMatrix && !hasArray && arr10.some(
25776
+ (item) => typeof item.storage === "function" && item.storage() === "sparse"
25777
+ );
25778
+ if (isSparse) {
25779
+ return _createSparseMatrixFromRows(arr10);
25780
+ }
25606
25781
  const arrays = arr10.map(
25607
25782
  (item) => typeof item.toArray === "function" ? item.toArray() : item
25608
25783
  );
25609
25784
  const result = _createArray(arrays);
25610
25785
  if (allMatrix && !hasArray) {
25611
- return matrix2(result);
25786
+ return matrix2(result, "dense");
25612
25787
  }
25613
25788
  return result;
25614
25789
  }
25615
- // TODO implement this properly for SparseMatrix
25616
25790
  });
25791
+ function _createSparseMatrixFromRows(arr10) {
25792
+ const N2 = arr10.length;
25793
+ const M = checkVectorTypeAndReturnLength(arr10[0]);
25794
+ const entries = [];
25795
+ for (let i = 0; i < N2; i++) {
25796
+ const rowVec = arr10[i];
25797
+ const rowLength = checkVectorTypeAndReturnLength(rowVec);
25798
+ if (rowLength !== M) {
25799
+ throw new TypeError(
25800
+ "The vectors had different length: " + (M | 0) + " \u2260 " + (rowLength | 0)
25801
+ );
25802
+ }
25803
+ if (typeof rowVec.forEach === "function") {
25804
+ const s = size2(rowVec);
25805
+ const is1D = s.length === 1;
25806
+ const isCol = s.length === 2 && s[1] === 1;
25807
+ rowVec.forEach((val, idx) => {
25808
+ let col;
25809
+ if (is1D) {
25810
+ col = idx[0];
25811
+ } else if (isCol) {
25812
+ col = idx[0];
25813
+ } else {
25814
+ col = idx[1];
25815
+ }
25816
+ if (val !== 0) {
25817
+ entries.push({ row: i, col, value: val });
25818
+ }
25819
+ });
25820
+ } else {
25821
+ const f = flatten5(rowVec);
25822
+ for (let j = 0; j < M; j++) {
25823
+ if (f[j] !== 0) {
25824
+ entries.push({ row: i, col: j, value: f[j] });
25825
+ }
25826
+ }
25827
+ }
25828
+ }
25829
+ entries.sort((a, b) => {
25830
+ if (a.col !== b.col) return a.col - b.col;
25831
+ return a.row - b.row;
25832
+ });
25833
+ const values = [];
25834
+ const index = [];
25835
+ const ptr = new Array(M + 1).fill(0);
25836
+ for (let i = 0; i < entries.length; i++) {
25837
+ ptr[entries[i].col + 1]++;
25838
+ }
25839
+ for (let j = 1; j <= M; j++) {
25840
+ ptr[j] += ptr[j - 1];
25841
+ }
25842
+ for (let i = 0; i < entries.length; i++) {
25843
+ values.push(entries[i].value);
25844
+ index.push(entries[i].row);
25845
+ }
25846
+ return matrix2({ values, index, ptr, size: [N2, M] }, "sparse");
25847
+ }
25617
25848
  function _createArray(arr10) {
25618
25849
  if (arr10.length === 0)
25619
25850
  throw new TypeError("At least one row is needed to construct a matrix.");
@@ -26476,7 +26707,7 @@ var createCsAmd = /* @__PURE__ */ factory(
26476
26707
  // src/algebra/sparse/csLeaf.ts
26477
26708
  function csLeaf(i, j, w, first, maxfirst, prevleaf, ancestor) {
26478
26709
  let s, sparent;
26479
- let jleaf = 0;
26710
+ let jleaf;
26480
26711
  let q;
26481
26712
  if (i <= j || w[first + j] <= w[maxfirst + i]) {
26482
26713
  return -1;
@@ -27828,6 +28059,10 @@ var createMatAlgo11xS0s = /* @__PURE__ */ factory(
27828
28059
  name108,
27829
28060
  dependencies108,
27830
28061
  ({ typed: typed3, equalScalar: equalScalar3 }) => {
28062
+ let _dtCache;
28063
+ let _cbCache;
28064
+ let _eqCache;
28065
+ let _cfCache;
27831
28066
  return function matAlgo11xS0s(s, b, callback, inverse) {
27832
28067
  const avalues = s._values;
27833
28068
  const aindex = s._index;
@@ -27845,10 +28080,19 @@ var createMatAlgo11xS0s = /* @__PURE__ */ factory(
27845
28080
  let cf = callback;
27846
28081
  if (typeof adt === "string") {
27847
28082
  dt = adt;
27848
- eq = typed3.find(equalScalar3, [dt, dt]);
28083
+ if (dt === _dtCache && callback === _cbCache) {
28084
+ eq = _eqCache;
28085
+ cf = _cfCache;
28086
+ } else {
28087
+ eq = typed3.find(equalScalar3, [dt, dt]) || equalScalar3;
28088
+ cf = typed3.find(callback, [dt, dt]) || callback;
28089
+ _dtCache = dt;
28090
+ _cbCache = callback;
28091
+ _eqCache = eq;
28092
+ _cfCache = cf;
28093
+ }
27849
28094
  zero = typed3.convert(0, dt);
27850
28095
  b = typed3.convert(b, dt);
27851
- cf = typed3.find(callback, [dt, dt]);
27852
28096
  }
27853
28097
  const cvalues = [];
27854
28098
  const cindex = [];
@@ -28318,45 +28562,52 @@ var createConcat = /* @__PURE__ */ factory(
28318
28562
  dependencies115,
28319
28563
  ({ typed: typed3, matrix: matrix2, isInteger: isInteger3 }) => {
28320
28564
  return typed3(name115, {
28321
- // TODO: change signature to '...Array | Matrix, dim?' when supported
28322
28565
  "...Array | Matrix | number | BigNumber": function(args) {
28323
- let i;
28324
28566
  const len = args.length;
28567
+ if (len === 0) {
28568
+ throw new SyntaxError("At least one matrix expected");
28569
+ }
28570
+ const lastArg = args[len - 1];
28571
+ const hasDim = isNumber(lastArg) || isBigNumber(lastArg);
28572
+ const dimIndex = hasDim ? len - 1 : len;
28325
28573
  let dim = -1;
28326
- let prevDim;
28574
+ if (hasDim) {
28575
+ dim = lastArg.valueOf();
28576
+ if (!isInteger3(dim)) {
28577
+ throw new TypeError("Integer number expected for dimension");
28578
+ }
28579
+ }
28327
28580
  let asMatrix = false;
28581
+ let prevDim = -1;
28328
28582
  const matrices = [];
28329
- for (i = 0; i < len; i++) {
28583
+ for (let i = 0; i < dimIndex; i++) {
28330
28584
  const arg3 = args[i];
28585
+ if (isNumber(arg3) || isBigNumber(arg3)) {
28586
+ throw new Error("Dimension must be specified as last argument");
28587
+ }
28331
28588
  if (isMatrix(arg3)) {
28332
28589
  asMatrix = true;
28333
28590
  }
28334
- if (isNumber(arg3) || isBigNumber(arg3)) {
28335
- if (i !== len - 1) {
28336
- throw new Error("Dimension must be specified as last argument");
28337
- }
28338
- prevDim = dim;
28339
- dim = arg3.valueOf();
28340
- if (!isInteger3(dim)) {
28341
- throw new TypeError("Integer number expected for dimension");
28342
- }
28343
- if (dim < 0 || i > 0 && dim > prevDim) {
28344
- throw new DimensionError(dim, dim < 0 ? 0 : prevDim, dim < 0 ? "<" : ">");
28345
- }
28346
- } else {
28347
- const m = clone(arg3).valueOf();
28348
- const size2 = arraySize(m);
28349
- matrices[i] = m;
28350
- prevDim = dim;
28351
- dim = size2.length - 1;
28352
- if (i > 0 && dim !== prevDim) {
28353
- throw new DimensionError(prevDim + 1, dim + 1);
28354
- }
28591
+ const m = clone(arg3).valueOf();
28592
+ const size2 = arraySize(m);
28593
+ matrices.push(m);
28594
+ const currentDim = size2.length - 1;
28595
+ if (i === 0) {
28596
+ prevDim = currentDim;
28597
+ } else if (currentDim !== prevDim) {
28598
+ throw new DimensionError(prevDim + 1, currentDim + 1);
28355
28599
  }
28356
28600
  }
28357
28601
  if (matrices.length === 0) {
28358
28602
  throw new SyntaxError("At least one matrix expected");
28359
28603
  }
28604
+ if (hasDim) {
28605
+ if (dim < 0 || dim > prevDim) {
28606
+ throw new DimensionError(dim, dim < 0 ? 0 : prevDim, dim < 0 ? "<" : ">");
28607
+ }
28608
+ } else {
28609
+ dim = prevDim;
28610
+ }
28360
28611
  let res = matrices.shift();
28361
28612
  while (matrices.length) {
28362
28613
  res = concat(res, matrices.shift(), dim);
@@ -29055,6 +29306,7 @@ var createMatAlgo03xDSf = /* @__PURE__ */ factory(
29055
29306
  name121,
29056
29307
  dependencies121,
29057
29308
  ({ typed: typed3 }) => {
29309
+ const signaturesCache = /* @__PURE__ */ new WeakMap();
29058
29310
  return function matAlgo03xDSf(denseMatrix, sparseMatrix, callback, inverse) {
29059
29311
  const adata = denseMatrix._data;
29060
29312
  const asize = denseMatrix._size;
@@ -29083,7 +29335,17 @@ var createMatAlgo03xDSf = /* @__PURE__ */ factory(
29083
29335
  if (typeof adt === "string" && adt === bdt && adt !== "mixed") {
29084
29336
  dt = adt;
29085
29337
  zero = typed3.convert(0, dt);
29086
- cf = typed3.find(callback, [dt, dt]);
29338
+ let callbacksForDt = signaturesCache.get(callback);
29339
+ if (!callbacksForDt) {
29340
+ callbacksForDt = /* @__PURE__ */ new Map();
29341
+ signaturesCache.set(callback, callbacksForDt);
29342
+ }
29343
+ let cachedCf = callbacksForDt.get(dt);
29344
+ if (!cachedCf) {
29345
+ cachedCf = typed3.find(callback, [dt, dt]);
29346
+ callbacksForDt.set(dt, cachedCf);
29347
+ }
29348
+ cf = cachedCf;
29087
29349
  }
29088
29350
  const cdata = [];
29089
29351
  for (let z = 0; z < rows; z++) {
@@ -31042,7 +31304,8 @@ var createPow = /* @__PURE__ */ factory(
31042
31304
  } catch (error) {
31043
31305
  if (error instanceof Error && error.message === "Cannot calculate inverse, determinant is zero") {
31044
31306
  throw new TypeError(
31045
- "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 }
31046
31309
  );
31047
31310
  }
31048
31311
  throw error;
@@ -31574,7 +31837,7 @@ var createRightLogShift = /* @__PURE__ */ factory(
31574
31837
  name153,
31575
31838
  {
31576
31839
  "number, number": rightLogShiftNumber,
31577
- // 'BigNumber, BigNumber': ..., // TODO: implement BigNumber support for rightLogShift
31840
+ "BigNumber, BigNumber": rightLogShiftBigNumber,
31578
31841
  "SparseMatrix, number | BigNumber": typed3.referToSelf(
31579
31842
  (self) => (x, y) => {
31580
31843
  if (equalScalar3(y, 0)) {
@@ -32910,14 +33173,14 @@ var createQr = /* @__PURE__ */ factory(
32910
33173
  for (i = k; i < rows; i++) {
32911
33174
  alphaSquared = addScalar2(alphaSquared, multiplyScalar2(Rdata[i][k], conj3(Rdata[i][k])));
32912
33175
  }
32913
- const alpha = multiplyScalar2(sgn, sqrt2(alphaSquared));
32914
- if (!isZero4(alpha)) {
32915
- const u1 = subtractScalar2(pivot, alpha);
33176
+ const alpha2 = multiplyScalar2(sgn, sqrt2(alphaSquared));
33177
+ if (!isZero4(alpha2)) {
33178
+ const u1 = subtractScalar2(pivot, alpha2);
32916
33179
  w[k] = 1;
32917
33180
  for (i = k + 1; i < rows; i++) {
32918
33181
  w[i] = divideScalar2(Rdata[i][k], u1);
32919
33182
  }
32920
- const tau2 = unaryMinus2(conj3(divideScalar2(u1, alpha)));
33183
+ const tau2 = unaryMinus2(conj3(divideScalar2(u1, alpha2)));
32921
33184
  let s;
32922
33185
  for (j = k; j < cols; j++) {
32923
33186
  s = 0;
@@ -34263,8 +34526,16 @@ var createPermutations = /* @__PURE__ */ factory(
34263
34526
  result = result.times(i);
34264
34527
  }
34265
34528
  return result;
34266
- }
34267
- // TODO: implement support for collection in permutations
34529
+ },
34530
+ "Array | Matrix": typed3.referToSelf(
34531
+ (self) => (n) => deepMap(n, self)
34532
+ ),
34533
+ "Array | Matrix, number | BigNumber": typed3.referToSelf(
34534
+ (self) => (n, k) => deepMap(n, (n_val) => self(n_val, k))
34535
+ ),
34536
+ "number | BigNumber, Array | Matrix": typed3.referToSelf(
34537
+ (self) => (n, k) => deepMap(k, (k_val) => self(n, k_val))
34538
+ )
34268
34539
  });
34269
34540
  }
34270
34541
  );
@@ -35493,7 +35764,16 @@ var createSubtract = /* @__PURE__ */ factory(
35493
35764
  // =========================================================================
35494
35765
  // EXISTING SIGNATURES - Keep after Node signatures
35495
35766
  // =========================================================================
35496
- "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
+ )
35497
35777
  },
35498
35778
  matrixAlgorithmSuite({
35499
35779
  elop: subtractScalar2,
@@ -37311,12 +37591,10 @@ var createCsLu = /* @__PURE__ */ factory(
37311
37591
  const size2 = m._size;
37312
37592
  const n = size2[1];
37313
37593
  let q;
37314
- let lnz = 100;
37315
- let unz = 100;
37594
+ let lnz;
37595
+ let unz;
37316
37596
  if (s) {
37317
37597
  q = s.q;
37318
- lnz = s.lnz || lnz;
37319
- unz = s.unz || unz;
37320
37598
  }
37321
37599
  const lvalues = [];
37322
37600
  const lindex = [];
@@ -37718,7 +37996,6 @@ var dependencies218 = [
37718
37996
  "abs",
37719
37997
  "add",
37720
37998
  "addScalar",
37721
- "matrix",
37722
37999
  "multiply",
37723
38000
  "multiplyScalar",
37724
38001
  "divideScalar",
@@ -37738,7 +38015,6 @@ var createIntersect = /* @__PURE__ */ factory(
37738
38015
  abs: abs2,
37739
38016
  add: add5,
37740
38017
  addScalar: addScalar2,
37741
- matrix: matrix2,
37742
38018
  multiply: multiply2,
37743
38019
  multiplyScalar: multiplyScalar2,
37744
38020
  divideScalar: divideScalar2,
@@ -37758,7 +38034,7 @@ var createIntersect = /* @__PURE__ */ factory(
37758
38034
  y.valueOf(),
37759
38035
  plane.valueOf()
37760
38036
  );
37761
- return arr10 === null ? null : matrix2(arr10);
38037
+ return arr10 === null ? null : x.create(arr10, x.datatype());
37762
38038
  },
37763
38039
  "Matrix, Matrix, Matrix, Matrix": function(w, x, y, z) {
37764
38040
  const arr10 = _AAAA(
@@ -37767,7 +38043,7 @@ var createIntersect = /* @__PURE__ */ factory(
37767
38043
  y.valueOf(),
37768
38044
  z.valueOf()
37769
38045
  );
37770
- return arr10 === null ? null : matrix2(arr10);
38046
+ return arr10 === null ? null : w.create(arr10, w.datatype());
37771
38047
  }
37772
38048
  });
37773
38049
  function _AAA(x, y, plane) {
@@ -38856,7 +39132,7 @@ var createSetPowerset = /* @__PURE__ */ factory(
38856
39132
  return result;
38857
39133
  }
38858
39134
  function _sort(array) {
38859
- let temp = [];
39135
+ let temp;
38860
39136
  for (let i = array.length - 1; i > 0; i--) {
38861
39137
  for (let j = 0; j < i; j++) {
38862
39138
  if (array[j].length > array[j + 1].length) {
@@ -41105,7 +41381,7 @@ var createMad = /* @__PURE__ */ factory(
41105
41381
  );
41106
41382
  } catch (err) {
41107
41383
  if (err instanceof TypeError && err.message.includes("median")) {
41108
- throw new TypeError(err.message.replace("median", "mad"));
41384
+ throw new TypeError(err.message.replace("median", "mad"), { cause: err });
41109
41385
  } else {
41110
41386
  throw improveErrorMessage(err, "mad", void 0);
41111
41387
  }
@@ -41149,7 +41425,7 @@ var createStd = /* @__PURE__ */ factory(
41149
41425
  }
41150
41426
  } catch (err) {
41151
41427
  if (err instanceof TypeError && err.message.includes(" variance")) {
41152
- throw new TypeError(err.message.replace(" variance", " std"));
41428
+ throw new TypeError(err.message.replace(" variance", " std"), { cause: err });
41153
41429
  } else {
41154
41430
  throw err;
41155
41431
  }
@@ -42067,19 +42343,30 @@ var createDerivative = /* @__PURE__ */ factory(
42067
42343
  SymbolNode
42068
42344
  }) => {
42069
42345
  function plainDerivative(expr, variable, options = { simplify: true }) {
42070
- const cache = /* @__PURE__ */ new Map();
42346
+ const order = options.order !== void 0 ? options.order : 1;
42347
+ if (!Number.isInteger(order) || order < 0) {
42348
+ throw new TypeError('Option "order" must be a non-negative integer');
42349
+ }
42071
42350
  const variableName = variable.name;
42072
- function isConstCached(node) {
42073
- const cached = cache.get(node);
42074
- if (cached !== void 0) {
42075
- return cached;
42351
+ let res = expr;
42352
+ for (let i = 0; i < order; i++) {
42353
+ let isConstCached2 = function(node) {
42354
+ const cached = cache.get(node);
42355
+ if (cached !== void 0) {
42356
+ return cached;
42357
+ }
42358
+ const r = _isConst(isConstCached2, node, variableName);
42359
+ cache.set(node, r);
42360
+ return r;
42361
+ };
42362
+ var isConstCached = isConstCached2;
42363
+ const cache = /* @__PURE__ */ new Map();
42364
+ res = _derivative(res, isConstCached2);
42365
+ if (options.simplify !== false && i < order - 1) {
42366
+ res = simplify2(res);
42076
42367
  }
42077
- const res2 = _isConst(isConstCached, node, variableName);
42078
- cache.set(node, res2);
42079
- return res2;
42080
42368
  }
42081
- const res = _derivative(expr, isConstCached);
42082
- return options.simplify ? simplify2(res) : res;
42369
+ return options.simplify !== false ? simplify2(res) : res;
42083
42370
  }
42084
42371
  function parseIdentifier(string2) {
42085
42372
  const symbol = parse3(string2);
@@ -42095,16 +42382,6 @@ var createDerivative = /* @__PURE__ */ factory(
42095
42382
  "Node, SymbolNode, Object": plainDerivative,
42096
42383
  "Node, string": (node, symbol) => plainDerivative(node, parseIdentifier(symbol)),
42097
42384
  "Node, string, Object": (node, symbol, options) => plainDerivative(node, parseIdentifier(symbol), options)
42098
- /* TODO: implement and test syntax with order of derivatives -> implement as an option {order: number}
42099
- 'Node, SymbolNode, ConstantNode': function (expr, variable, {order}) {
42100
- let res = expr
42101
- for (let i = 0; i < order; i++) {
42102
- <create caching isConst>
42103
- res = _derivative(res, isConst)
42104
- }
42105
- return res
42106
- }
42107
- */
42108
42385
  });
42109
42386
  derivative3._simplify = true;
42110
42387
  derivative3.toTex = function(deriv) {
@@ -42113,7 +42390,11 @@ var createDerivative = /* @__PURE__ */ factory(
42113
42390
  const _derivTex = typed3("_derivTex", {
42114
42391
  "Node, SymbolNode": function(expr, x) {
42115
42392
  if (isConstantNode(expr) && typeOf(expr.value) === "string") {
42116
- return _derivTex(parse3(expr.value).toString(), x.toString(), 1);
42393
+ return _derivTex(
42394
+ parse3(expr.value).toString(),
42395
+ x.toString(),
42396
+ 1
42397
+ );
42117
42398
  } else {
42118
42399
  return _derivTex(expr.toTex(), x.toString(), 1);
42119
42400
  }
@@ -42169,7 +42450,9 @@ var createDerivative = /* @__PURE__ */ factory(
42169
42450
  return createConstantNode2(1);
42170
42451
  },
42171
42452
  "ParenthesisNode, function": function(node, isConst2) {
42172
- return new ParenthesisNode(_derivative(node.content, isConst2));
42453
+ return new ParenthesisNode(
42454
+ _derivative(node.content, isConst2)
42455
+ );
42173
42456
  },
42174
42457
  "FunctionAssignmentNode, function": function(node, isConst2) {
42175
42458
  if (isConst2(node)) {
@@ -43784,7 +44067,7 @@ var createNuclearMagneton = /* @__PURE__ */ unitFactory(
43784
44067
  var createKlitzing = /* @__PURE__ */ unitFactory("klitzing", "25812.807459304513", "ohm");
43785
44068
  var createJosephson = /* @__PURE__ */ unitFactory(
43786
44069
  "josephson",
43787
- "4.835978484169836e14 Hz V",
44070
+ "4.835978484169836e14",
43788
44071
  "Hz V^-1"
43789
44072
  );
43790
44073
  var createBohrRadius = /* @__PURE__ */ unitFactory("bohrRadius", "5.29177210544e-11", "m");
@@ -43854,6 +44137,11 @@ var createFirstRadiation = /* @__PURE__ */ unitFactory(
43854
44137
  "3.7417718521927573e-16",
43855
44138
  "W m^2"
43856
44139
  );
44140
+ var createSpectralRadiance = /* @__PURE__ */ unitFactory(
44141
+ "spectralRadiance",
44142
+ "1.1910429723971881e-16",
44143
+ "W m^2 sr^-1"
44144
+ );
43857
44145
  var createLoschmidt = /* @__PURE__ */ unitFactory(
43858
44146
  "loschmidt",
43859
44147
  "2.686780111798444e25",
@@ -44704,6 +44992,9 @@ var stefanBoltzmann = createStefanBoltzmann(
44704
44992
  var firstRadiation = createFirstRadiation(
44705
44993
  factoryScope
44706
44994
  );
44995
+ var spectralRadiance = createSpectralRadiance(
44996
+ factoryScope
44997
+ );
44707
44998
  var secondRadiation = createSecondRadiation(
44708
44999
  factoryScope
44709
45000
  );
@@ -45731,16 +46022,16 @@ function groebnerBasis(polys, vars) {
45731
46022
  return basis.map((b) => polyToString(b, vars));
45732
46023
  }
45733
46024
  function minimalPolynomial(expr, varName) {
45734
- const alpha = evaluate(expr);
45735
- if (!isFinite(alpha)) return null;
46025
+ const alpha2 = evaluate(expr);
46026
+ if (!isFinite(alpha2)) return null;
45736
46027
  for (let den = 1; den <= 100; den++) {
45737
- const num2 = Math.round(alpha * den);
45738
- if (Math.abs(alpha - num2 / den) < 1e-12) {
46028
+ const num2 = Math.round(alpha2 * den);
46029
+ if (Math.abs(alpha2 - num2 / den) < 1e-12) {
45739
46030
  if (den === 1) return `${varName} - ${num2}`;
45740
46031
  return `${den}*${varName} - ${num2}`;
45741
46032
  }
45742
46033
  }
45743
- const powers = [1, alpha, alpha * alpha, alpha * alpha * alpha, alpha ** 4];
46034
+ const powers = [1, alpha2, alpha2 * alpha2, alpha2 * alpha2 * alpha2, alpha2 ** 4];
45744
46035
  for (let deg = 2; deg <= 6; deg++) {
45745
46036
  const maxCoeff = 20;
45746
46037
  const found = findIntegerRelation(powers.slice(0, deg + 1), maxCoeff);
@@ -46259,7 +46550,7 @@ function iltSimplifyNode(node) {
46259
46550
  return node;
46260
46551
  }
46261
46552
  function _inverseLaplaceNode(node, sVar, tVar) {
46262
- let simplified = node;
46553
+ let simplified;
46263
46554
  try {
46264
46555
  simplified = iltSimplifyNode(node);
46265
46556
  } catch {
@@ -47317,7 +47608,7 @@ function studentizedRangeQuantile(p, k, df) {
47317
47608
  }
47318
47609
  return (lo + hi) / 2;
47319
47610
  }
47320
- function tukeyHSD(groups, alpha = 0.05) {
47611
+ function tukeyHSD(groups, alpha2 = 0.05) {
47321
47612
  const gs = groups.map(arr2);
47322
47613
  const k = gs.length;
47323
47614
  if (k < 2) throw new Error("tukeyHSD: need at least 2 groups");
@@ -47328,7 +47619,7 @@ function tukeyHSD(groups, alpha = 0.05) {
47328
47619
  throw new Error("tukeyHSD: residual df must be > 0 (groups must hold more observations than groups)");
47329
47620
  const sse = gs.reduce((s, g) => s + sampleVar(g) * (g.length - 1), 0);
47330
47621
  const mse = sse / dfErr;
47331
- const qCrit = studentizedRangeQuantile(1 - alpha, k, dfErr);
47622
+ const qCrit = studentizedRangeQuantile(1 - alpha2, k, dfErr);
47332
47623
  const out = [];
47333
47624
  for (let i = 0; i < k; i++) {
47334
47625
  for (let j = i + 1; j < k; j++) {
@@ -47619,7 +47910,7 @@ function fsolve(F, x0, opts = {}) {
47619
47910
  delta = linsolve(J, rhs);
47620
47911
  } catch (err) {
47621
47912
  const msg = err instanceof Error ? err.message : String(err);
47622
- throw new Error(`fsolve: singular Jacobian at iteration ${iter} (${msg})`);
47913
+ throw new Error(`fsolve: singular Jacobian at iteration ${iter} (${msg})`, { cause: err });
47623
47914
  }
47624
47915
  const residualNorm2 = norm24(Fx);
47625
47916
  let xNext = x.map((xi, i) => xi + delta[i]);
@@ -47672,14 +47963,14 @@ function dot2(a, b) {
47672
47963
  function norm25(a) {
47673
47964
  return Math.sqrt(dot2(a, a));
47674
47965
  }
47675
- function axpy(alpha, x, y) {
47966
+ function axpy(alpha2, x, y) {
47676
47967
  const n = x.length;
47677
47968
  const out = new Array(n);
47678
- 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];
47679
47970
  return out;
47680
47971
  }
47681
- function scale(alpha, x) {
47682
- return x.map((v) => alpha * v);
47972
+ function scale(alpha2, x) {
47973
+ return x.map((v) => alpha2 * v);
47683
47974
  }
47684
47975
  function subtract2(a, b) {
47685
47976
  return a.map((v, i) => v - b[i]);
@@ -47843,17 +48134,17 @@ function cg(a, b, opts) {
47843
48134
  const Ap = matvec2(p);
47844
48135
  const pAp = dot2(p, Ap);
47845
48136
  if (Math.abs(pAp) < 1e-300) break;
47846
- const alpha = rzOld / pAp;
47847
- x = axpy(alpha, p, x);
47848
- r = axpy(-alpha, Ap, r);
48137
+ const alpha2 = rzOld / pAp;
48138
+ x = axpy(alpha2, p, x);
48139
+ r = axpy(-alpha2, Ap, r);
47849
48140
  residual = relativeResidualNorm(norm25(r), bNorm);
47850
48141
  if (residual < tol) {
47851
48142
  return { x, iterations, converged: true, residual };
47852
48143
  }
47853
48144
  z = applyM(r);
47854
48145
  const rzNew = dot2(r, z);
47855
- const beta2 = rzNew / rzOld;
47856
- p = add3(z, scale(beta2, p));
48146
+ const beta3 = rzNew / rzOld;
48147
+ p = add3(z, scale(beta3, p));
47857
48148
  rzOld = rzNew;
47858
48149
  }
47859
48150
  return { x, iterations, converged: residual < tol, residual };
@@ -47874,7 +48165,7 @@ function minres(a, b, opts) {
47874
48165
  return { x, iterations: 0, converged: residual0 < tol, residual: residual0 };
47875
48166
  }
47876
48167
  let oldb = 0;
47877
- let beta2 = beta1;
48168
+ let beta3 = beta1;
47878
48169
  let dbar = 0;
47879
48170
  let epsln = 0;
47880
48171
  let phibar = beta1;
@@ -47886,31 +48177,31 @@ function minres(a, b, opts) {
47886
48177
  let iterations = 0;
47887
48178
  for (let iter = 1; iter <= maxIter; iter++) {
47888
48179
  iterations = iter;
47889
- const v = scale(1 / beta2, y);
48180
+ const v = scale(1 / beta3, y);
47890
48181
  y = matvec2(v);
47891
- if (iter >= 2) y = axpy(-beta2 / oldb, r1, y);
48182
+ if (iter >= 2) y = axpy(-beta3 / oldb, r1, y);
47892
48183
  const alfa = dot2(v, y);
47893
- y = axpy(-alfa / beta2, r2, y);
48184
+ y = axpy(-alfa / beta3, r2, y);
47894
48185
  r1 = r2;
47895
48186
  r2 = y;
47896
48187
  y = applyM(r2);
47897
- oldb = beta2;
47898
- beta2 = Math.sqrt(Math.max(0, dot2(r2, y)));
48188
+ oldb = beta3;
48189
+ beta3 = Math.sqrt(Math.max(0, dot2(r2, y)));
47899
48190
  const oldeps = epsln;
47900
48191
  const delta = cs * dbar + sn * alfa;
47901
48192
  const gbar = sn * dbar - cs * alfa;
47902
- epsln = sn * beta2;
47903
- dbar = -cs * beta2;
47904
- 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);
47905
48196
  cs = gbar / gamma2;
47906
- sn = beta2 / gamma2;
48197
+ sn = beta3 / gamma2;
47907
48198
  const phi = cs * phibar;
47908
48199
  phibar = sn * phibar;
47909
48200
  const w1 = w2;
47910
48201
  w2 = w;
47911
48202
  w = scale(1 / gamma2, subtract2(subtract2(v, scale(oldeps, w1)), scale(delta, w2)));
47912
48203
  for (let i = 0; i < n; i++) x[i] += phi * w[i];
47913
- if (phibar / beta1 < tol || beta2 < 1e-300) break;
48204
+ if (phibar / beta1 < tol || beta3 < 1e-300) break;
47914
48205
  }
47915
48206
  const residual = relativeResidual(matvec2, x, b, bNorm);
47916
48207
  return { x, iterations, converged: residual < tol, residual };
@@ -47931,15 +48222,15 @@ function gmres(a, b, opts) {
47931
48222
  }
47932
48223
  while (totalIterations < maxIter) {
47933
48224
  const r0 = applyM(subtract2(b, matvec2(x)));
47934
- const beta2 = norm25(r0);
47935
- if (beta2 < 1e-300) {
48225
+ const beta3 = norm25(r0);
48226
+ if (beta3 < 1e-300) {
47936
48227
  return { x, iterations: totalIterations, converged: residual < tol, residual };
47937
48228
  }
47938
48229
  const m = Math.min(restart, maxIter - totalIterations);
47939
- const V = [scale(1 / beta2, r0)];
48230
+ const V = [scale(1 / beta3, r0)];
47940
48231
  const H = Array.from({ length: m + 1 }, () => new Array(m).fill(0));
47941
48232
  const g = new Array(m + 1).fill(0);
47942
- g[0] = beta2;
48233
+ g[0] = beta3;
47943
48234
  const cs = new Array(m).fill(0);
47944
48235
  const sn = new Array(m).fill(0);
47945
48236
  let k = 0;
@@ -48007,7 +48298,7 @@ function bicgstab(a, b, opts) {
48007
48298
  return { x, iterations: 0, converged: true, residual };
48008
48299
  }
48009
48300
  let rho = 1;
48010
- let alpha = 1;
48301
+ let alpha2 = 1;
48011
48302
  let omega = 1;
48012
48303
  let v = zeros2(n);
48013
48304
  let p = zeros2(n);
@@ -48019,19 +48310,19 @@ function bicgstab(a, b, opts) {
48019
48310
  if (iter === 1) {
48020
48311
  p = r.slice();
48021
48312
  } else {
48022
- const beta2 = rhoNew / rho * (alpha / omega);
48023
- 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))));
48024
48315
  }
48025
48316
  rho = rhoNew;
48026
48317
  const pHat = applyM(p);
48027
48318
  v = matvec2(pHat);
48028
48319
  const rHatV = dot2(rHat0, v);
48029
48320
  if (Math.abs(rHatV) < 1e-300) break;
48030
- alpha = rho / rHatV;
48031
- const s = axpy(-alpha, v, r);
48321
+ alpha2 = rho / rHatV;
48322
+ const s = axpy(-alpha2, v, r);
48032
48323
  const sNorm = relativeResidualNorm(norm25(s), bNorm);
48033
48324
  if (sNorm < tol) {
48034
- x = axpy(alpha, pHat, x);
48325
+ x = axpy(alpha2, pHat, x);
48035
48326
  residual = sNorm;
48036
48327
  return { x, iterations, converged: true, residual };
48037
48328
  }
@@ -48039,7 +48330,7 @@ function bicgstab(a, b, opts) {
48039
48330
  const t = matvec2(sHat);
48040
48331
  const tt = dot2(t, t);
48041
48332
  omega = tt > 1e-300 ? dot2(t, s) / tt : 0;
48042
- x = axpy(alpha, pHat, x);
48333
+ x = axpy(alpha2, pHat, x);
48043
48334
  x = axpy(omega, sHat, x);
48044
48335
  r = axpy(-omega, t, s);
48045
48336
  residual = relativeResidualNorm(norm25(r), bNorm);
@@ -48082,13 +48373,13 @@ function dot3(a, b) {
48082
48373
  function norm26(a) {
48083
48374
  return Math.sqrt(dot3(a, a));
48084
48375
  }
48085
- function axpy2(alpha, x, y) {
48376
+ function axpy2(alpha2, x, y) {
48086
48377
  const out = new Array(x.length);
48087
- 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];
48088
48379
  return out;
48089
48380
  }
48090
- function scale2(alpha, x) {
48091
- return x.map((v) => alpha * v);
48381
+ function scale2(alpha2, x) {
48382
+ return x.map((v) => alpha2 * v);
48092
48383
  }
48093
48384
  function startVector(n) {
48094
48385
  const v = new Array(n);
@@ -48104,19 +48395,19 @@ function lanczos(matvec2, n, m) {
48104
48395
  const vj = V[j];
48105
48396
  let w = matvec2(vj);
48106
48397
  if (j > 0) w = axpy2(-betas[j - 1], V[j - 1], w);
48107
- const alpha = dot3(w, vj);
48108
- alphas.push(alpha);
48109
- w = axpy2(-alpha, vj, w);
48398
+ const alpha2 = dot3(w, vj);
48399
+ alphas.push(alpha2);
48400
+ w = axpy2(-alpha2, vj, w);
48110
48401
  for (let pass = 0; pass < 2; pass++) {
48111
48402
  for (let i = 0; i <= j; i++) {
48112
48403
  const c = dot3(w, V[i]);
48113
48404
  w = axpy2(-c, V[i], w);
48114
48405
  }
48115
48406
  }
48116
- const beta2 = norm26(w);
48117
- if (j === m - 1 || beta2 < 1e-13) break;
48118
- betas.push(beta2);
48119
- 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));
48120
48411
  }
48121
48412
  return { V, alphas, betas };
48122
48413
  }
@@ -48420,7 +48711,7 @@ function ldl(A) {
48420
48711
  for (let i = 0; i < n; i++) [W[i][a], W[i][b]] = [W[i][b], W[i][a]];
48421
48712
  [L[a], L[b]] = [L[b], L[a]];
48422
48713
  };
48423
- const alpha = (1 + Math.sqrt(17)) / 8;
48714
+ const alpha2 = (1 + Math.sqrt(17)) / 8;
48424
48715
  let k = 0;
48425
48716
  while (k < n) {
48426
48717
  if (k === n - 1) {
@@ -48441,13 +48732,13 @@ function ldl(A) {
48441
48732
  }
48442
48733
  let use1x1 = true;
48443
48734
  let swapWith = -1;
48444
- if (lambda !== 0 && w1 < alpha * lambda) {
48735
+ if (lambda !== 0 && w1 < alpha2 * lambda) {
48445
48736
  let sigma = 0;
48446
48737
  for (let i = k; i < n; i++) {
48447
48738
  if (i !== r) sigma = Math.max(sigma, Math.abs(W[i][r]));
48448
48739
  }
48449
- if (w1 * sigma < alpha * lambda * lambda) {
48450
- if (Math.abs(W[r][r]) >= alpha * sigma) {
48740
+ if (w1 * sigma < alpha2 * lambda * lambda) {
48741
+ if (Math.abs(W[r][r]) >= alpha2 * sigma) {
48451
48742
  swapWith = r;
48452
48743
  } else {
48453
48744
  use1x1 = false;
@@ -49361,15 +49652,15 @@ function movingAverage(x, w) {
49361
49652
  }
49362
49653
  return out;
49363
49654
  }
49364
- function ewma(x, alpha) {
49655
+ function ewma(x, alpha2) {
49365
49656
  const a = arr4(x);
49366
- 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]");
49367
49658
  if (a.length === 0) throw new Error("ewma: input series is empty");
49368
49659
  const out = new Array(a.length);
49369
49660
  let prev = a[0];
49370
49661
  out[0] = prev;
49371
49662
  for (let i = 1; i < a.length; i++) {
49372
- prev = alpha * a[i] + (1 - alpha) * prev;
49663
+ prev = alpha2 * a[i] + (1 - alpha2) * prev;
49373
49664
  out[i] = prev;
49374
49665
  }
49375
49666
  return out;
@@ -49561,8 +49852,8 @@ var MACKINNON_C = {
49561
49852
  10: { betaInf: -2.5671, beta1: -1.438, beta2: -4.48 }
49562
49853
  };
49563
49854
  function adfCriticalValue(pct, n) {
49564
- const { betaInf, beta1, beta2 } = MACKINNON_C[pct];
49565
- return betaInf + beta1 / n + beta2 / (n * n);
49855
+ const { betaInf, beta1, beta2: beta22 } = MACKINNON_C[pct];
49856
+ return betaInf + beta1 / n + beta22 / (n * n);
49566
49857
  }
49567
49858
  function adfPValue(statistic, n) {
49568
49859
  const cv1 = adfCriticalValue(1, n);
@@ -49689,9 +49980,9 @@ function softThreshold(a, lambda) {
49689
49980
  if (a < -lambda) return a + lambda;
49690
49981
  return 0;
49691
49982
  }
49692
- function ridge(X, y, alpha, opts) {
49983
+ function ridge(X, y, alpha2, opts) {
49693
49984
  const { n, p } = validate2(X, y, "ridge");
49694
- 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");
49695
49986
  const useIntercept = opts?.intercept !== false;
49696
49987
  const { Xc, yc, xMean, yMean } = center(X, y, n, p, useIntercept);
49697
49988
  const XtX = Array.from({ length: p }, () => new Array(p).fill(0));
@@ -49703,7 +49994,7 @@ function ridge(X, y, alpha, opts) {
49703
49994
  XtX[j][k] = s;
49704
49995
  XtX[k][j] = s;
49705
49996
  }
49706
- XtX[j][j] += alpha;
49997
+ XtX[j][j] += alpha2;
49707
49998
  for (let i = 0; i < n; i++) Xty[j] += Xc[i][j] * yc[i];
49708
49999
  }
49709
50000
  const coefficients = linsolve(XtX, Xty);
@@ -49722,52 +50013,52 @@ function standardize(Xc, n, p) {
49722
50013
  return { Xs, scale: scale4 };
49723
50014
  }
49724
50015
  function coordinateDescent(Xs, yc, n, p, l1, l2, maxIter, tol) {
49725
- const beta2 = new Array(p).fill(0);
50016
+ const beta3 = new Array(p).fill(0);
49726
50017
  const z = new Array(p).fill(n);
49727
50018
  const residual = [...yc];
49728
50019
  for (let iter = 0; iter < maxIter; iter++) {
49729
50020
  let maxChange = 0;
49730
50021
  for (let j = 0; j < p; j++) {
49731
50022
  const col = Xs.map((row2) => row2[j]);
49732
- const oldBeta = beta2[j];
50023
+ const oldBeta = beta3[j];
49733
50024
  let rho = 0;
49734
50025
  for (let i = 0; i < n; i++) rho += col[i] * (residual[i] + col[i] * oldBeta);
49735
50026
  const newBeta = softThreshold(rho, l1) / (z[j] + l2);
49736
50027
  if (newBeta !== oldBeta) {
49737
50028
  const delta = newBeta - oldBeta;
49738
50029
  for (let i = 0; i < n; i++) residual[i] -= col[i] * delta;
49739
- beta2[j] = newBeta;
50030
+ beta3[j] = newBeta;
49740
50031
  maxChange = Math.max(maxChange, Math.abs(delta));
49741
50032
  }
49742
50033
  }
49743
50034
  if (maxChange < tol) break;
49744
50035
  }
49745
- return beta2;
50036
+ return beta3;
49746
50037
  }
49747
- function lasso(X, y, alpha, opts) {
50038
+ function lasso(X, y, alpha2, opts) {
49748
50039
  const { n, p } = validate2(X, y, "lasso");
49749
- 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");
49750
50041
  const useIntercept = opts?.intercept !== false;
49751
50042
  const maxIter = opts?.maxIter ?? 1e3;
49752
50043
  const tol = opts?.tol ?? 1e-7;
49753
50044
  const { Xc, yc, xMean, yMean } = center(X, y, n, p, useIntercept);
49754
50045
  const { Xs, scale: scale4 } = standardize(Xc, n, p);
49755
- const betaStd = coordinateDescent(Xs, yc, n, p, alpha, 0, maxIter, tol);
50046
+ const betaStd = coordinateDescent(Xs, yc, n, p, alpha2, 0, maxIter, tol);
49756
50047
  const coefficients = betaStd.map((b, j) => b / scale4[j]);
49757
50048
  const intercept = useIntercept ? yMean - dot4(xMean, coefficients) : 0;
49758
50049
  return { coefficients, intercept };
49759
50050
  }
49760
- function elasticNet(X, y, alpha, l1Ratio, opts) {
50051
+ function elasticNet(X, y, alpha2, l1Ratio, opts) {
49761
50052
  const { n, p } = validate2(X, y, "elasticNet");
49762
- 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");
49763
50054
  if (l1Ratio < 0 || l1Ratio > 1) throw new Error("elasticNet: l1Ratio must be in [0, 1]");
49764
50055
  const useIntercept = opts?.intercept !== false;
49765
50056
  const maxIter = opts?.maxIter ?? 1e3;
49766
50057
  const tol = opts?.tol ?? 1e-7;
49767
50058
  const { Xc, yc, xMean, yMean } = center(X, y, n, p, useIntercept);
49768
50059
  const { Xs, scale: scale4 } = standardize(Xc, n, p);
49769
- const l1 = alpha * l1Ratio;
49770
- const l2 = alpha * (1 - l1Ratio);
50060
+ const l1 = alpha2 * l1Ratio;
50061
+ const l2 = alpha2 * (1 - l1Ratio);
49771
50062
  const betaStd = coordinateDescent(Xs, yc, n, p, l1, l2, maxIter, tol);
49772
50063
  const coefficients = betaStd.map((b, j) => b / scale4[j]);
49773
50064
  const intercept = useIntercept ? yMean - dot4(xMean, coefficients) : 0;
@@ -49801,9 +50092,9 @@ function logisticRegression(X, y, opts) {
49801
50092
  const design = (rows) => useIntercept ? rows.map((row2) => [1, ...row2]) : rows.map((row2) => [...row2]);
49802
50093
  const D = design(X);
49803
50094
  const p = D[0].length;
49804
- let beta2 = new Array(p).fill(0);
50095
+ let beta3 = new Array(p).fill(0);
49805
50096
  for (let iter = 0; iter < maxIter; iter++) {
49806
- 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));
49807
50098
  const probs = eta.map(sigmoid2);
49808
50099
  const weights = probs.map((pi) => Math.max(pi * (1 - pi), 1e-9));
49809
50100
  const XtWX = Array.from({ length: p }, () => new Array(p).fill(0));
@@ -49841,14 +50132,14 @@ function logisticRegression(X, y, opts) {
49841
50132
  delta = delta.map((d) => d * scale4);
49842
50133
  stepNorm = MAX_STEP_NORM;
49843
50134
  }
49844
- beta2 = beta2.map((b, j) => b + delta[j]);
50135
+ beta3 = beta3.map((b, j) => b + delta[j]);
49845
50136
  if (stepNorm < tol) break;
49846
50137
  }
49847
- const intercept = useIntercept ? beta2[0] : 0;
49848
- const coefficients = useIntercept ? beta2.slice(1) : beta2.slice();
50138
+ const intercept = useIntercept ? beta3[0] : 0;
50139
+ const coefficients = useIntercept ? beta3.slice(1) : beta3.slice();
49849
50140
  const predictProba = (x) => {
49850
50141
  const Dx = design(x);
49851
- 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)));
49852
50143
  };
49853
50144
  const predict = (x) => predictProba(x).map((pi) => pi >= 0.5 ? 1 : 0);
49854
50145
  return { coefficients, intercept, predictProba, predict };
@@ -49859,7 +50150,7 @@ var _inv2 = inv;
49859
50150
  function nelderMead(f, x0, opts = {}) {
49860
50151
  const { maxIter = 2e3, tol = 1e-8, step = 0.05 } = opts;
49861
50152
  const n = x0.length;
49862
- const alpha = 1;
50153
+ const alpha2 = 1;
49863
50154
  const gamma2 = 2;
49864
50155
  const rho = 0.5;
49865
50156
  const sigma = 0.5;
@@ -49884,7 +50175,7 @@ function nelderMead(f, x0, opts = {}) {
49884
50175
  fv = order.map((i) => fv[i]);
49885
50176
  if (Math.abs(fv[n] - fv[0]) <= tol * (Math.abs(fv[0]) + tol)) break;
49886
50177
  const c = centroid2(simplex.slice(0, n));
49887
- const xr = add5(c, sub4(c, simplex[n]), alpha);
50178
+ const xr = add5(c, sub4(c, simplex[n]), alpha2);
49888
50179
  const fr = f(xr);
49889
50180
  if (fr < fv[0]) {
49890
50181
  const xe = add5(c, sub4(c, simplex[n]), gamma2);
@@ -50384,17 +50675,17 @@ function bfgs(f, x0, opts = {}) {
50384
50675
  const gd = dot5(g, d);
50385
50676
  const dir = gd < 0 ? d : g.map((v) => -v);
50386
50677
  const dirSlope = gd < 0 ? gd : -dot5(g, g);
50387
- let alpha = 1;
50678
+ let alpha2 = 1;
50388
50679
  let xNew = clipToBounds(
50389
- x.map((v, i) => v + alpha * dir[i]),
50680
+ x.map((v, i) => v + alpha2 * dir[i]),
50390
50681
  bounds
50391
50682
  );
50392
50683
  let fNew = f(xNew);
50393
50684
  let halvings = 0;
50394
- while (fNew > fx + c1 * alpha * dirSlope && halvings < 50) {
50395
- alpha *= 0.5;
50685
+ while (fNew > fx + c1 * alpha2 * dirSlope && halvings < 50) {
50686
+ alpha2 *= 0.5;
50396
50687
  xNew = clipToBounds(
50397
- x.map((v, i) => v + alpha * dir[i]),
50688
+ x.map((v, i) => v + alpha2 * dir[i]),
50398
50689
  bounds
50399
50690
  );
50400
50691
  fNew = f(xNew);
@@ -50493,15 +50784,15 @@ function nnls(A, b, opts = {}) {
50493
50784
  let innerIter = 0;
50494
50785
  while (Array.from(passive).some((j) => z[j] <= tol) && innerIter < maxIter) {
50495
50786
  innerIter++;
50496
- let alpha = Infinity;
50787
+ let alpha2 = Infinity;
50497
50788
  for (const j of passive) {
50498
50789
  if (z[j] <= tol) {
50499
50790
  const denom = x[j] - z[j];
50500
- if (denom > 0) alpha = Math.min(alpha, x[j] / denom);
50791
+ if (denom > 0) alpha2 = Math.min(alpha2, x[j] / denom);
50501
50792
  }
50502
50793
  }
50503
- if (!isFinite(alpha)) alpha = 0;
50504
- 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]);
50505
50796
  for (const j of Array.from(passive)) {
50506
50797
  if (Math.abs(x[j]) < tol) passive.delete(j);
50507
50798
  }
@@ -50549,18 +50840,18 @@ function lsqBounded(A, b, lower, upper, opts = {}) {
50549
50840
  pgNorm = Math.sqrt(pgNorm);
50550
50841
  if (pgNorm < tol) break;
50551
50842
  const fx = objective(x);
50552
- let alpha = 1;
50843
+ let alpha2 = 1;
50553
50844
  let xNext = clip(
50554
- x.map((xi, i) => xi - alpha * g[i]),
50845
+ x.map((xi, i) => xi - alpha2 * g[i]),
50555
50846
  lower,
50556
50847
  upper
50557
50848
  );
50558
50849
  let fNext = objective(xNext);
50559
50850
  let backtrack = 0;
50560
50851
  while (fNext > fx && backtrack < 50) {
50561
- alpha /= 2;
50852
+ alpha2 /= 2;
50562
50853
  xNext = clip(
50563
- x.map((xi, i) => xi - alpha * g[i]),
50854
+ x.map((xi, i) => xi - alpha2 * g[i]),
50564
50855
  lower,
50565
50856
  upper
50566
50857
  );
@@ -50572,7 +50863,10 @@ function lsqBounded(A, b, lower, upper, opts = {}) {
50572
50863
  return { x, residual: residualNorm(A, x, b) };
50573
50864
  }
50574
50865
 
50575
- // 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
50576
50870
  function ratNormalize(num2, den) {
50577
50871
  if (den === 0n) {
50578
50872
  throw new Error("Rat: zero denominator");
@@ -50608,6 +50902,378 @@ function ratFromBigint(n) {
50608
50902
  return { num: n, den: 1n };
50609
50903
  }
50610
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
50611
51277
  function surdFromRat(r) {
50612
51278
  return { a: r, b: RAT_ZERO };
50613
51279
  }
@@ -50829,7 +51495,7 @@ function shiftedColumn(poly, shift, size2) {
50829
51495
  }
50830
51496
  return col;
50831
51497
  }
50832
- function solveLinearSystemRat(matrix2, rhs) {
51498
+ function solveLinearSystemRat2(matrix2, rhs) {
50833
51499
  const n = rhs.length;
50834
51500
  const rows = matrix2.map((row2, r) => [...row2, rhs[r]]);
50835
51501
  for (let col = 0; col < n; col += 1) {
@@ -50906,7 +51572,7 @@ function partialFractions(remainder, factors) {
50906
51572
  rows.push(columns.map((col) => col[r]));
50907
51573
  }
50908
51574
  const rhs = new Array(n).fill(RAT_ZERO).map((_, idx2) => ratFromBigint(idx2 < trimmedRemainder.length ? trimmedRemainder[idx2] : 0n));
50909
- const solution = n === 0 ? [] : solveLinearSystemRat(rows, rhs);
51575
+ const solution = n === 0 ? [] : solveLinearSystemRat2(rows, rhs);
50910
51576
  const terms = [];
50911
51577
  let idx = 0;
50912
51578
  for (let fi = 0; fi < factors.length; fi += 1) {
@@ -50928,7 +51594,7 @@ function ratNeg(r) {
50928
51594
  function ratToStr(r) {
50929
51595
  return r.den === 1n ? `${r.num}` : `${r.num}/${r.den}`;
50930
51596
  }
50931
- function renderCoeffTimes(r, rest) {
51597
+ function renderCoeffTimes2(r, rest) {
50932
51598
  const neg2 = r.num < 0n;
50933
51599
  const absNum = neg2 ? -r.num : r.num;
50934
51600
  const sign3 = neg2 ? "-" : "";
@@ -50962,7 +51628,7 @@ function renderTwoXPlusB(b, v) {
50962
51628
  }
50963
51629
  return s;
50964
51630
  }
50965
- function joinTerms(parts) {
51631
+ function joinTerms2(parts) {
50966
51632
  if (parts.length === 0) {
50967
51633
  return "0";
50968
51634
  }
@@ -50978,10 +51644,10 @@ function integrateLinearTerm(factor2, k, numer, v) {
50978
51644
  }
50979
51645
  const xMinus = renderXMinus(a, v);
50980
51646
  if (k === 1) {
50981
- return renderCoeffTimes(A, `log(abs(${xMinus}))`);
51647
+ return renderCoeffTimes2(A, `log(abs(${xMinus}))`);
50982
51648
  }
50983
51649
  const coeff = ratNeg(ratDiv(A, ratFromBigint(BigInt(k - 1))));
50984
- return renderCoeffTimes(coeff, `(${xMinus})^(${-(k - 1)})`);
51650
+ return renderCoeffTimes2(coeff, `(${xMinus})^(${-(k - 1)})`);
50985
51651
  }
50986
51652
  function integrateInverseQuadraticPower(k, b, c) {
50987
51653
  if (k === 1) {
@@ -51013,10 +51679,10 @@ function integrateQuadraticTerm(factor2, k, numer, v) {
51013
51679
  if (D.num !== 0n) {
51014
51680
  const half = ratDiv(D, ratFromBigint(2n));
51015
51681
  if (k === 1) {
51016
- parts.push(renderCoeffTimes(half, `log(${qCore})`));
51682
+ parts.push(renderCoeffTimes2(half, `log(${qCore})`));
51017
51683
  } else {
51018
51684
  const coeff = ratNeg(ratDiv(half, ratFromBigint(BigInt(k - 1))));
51019
- parts.push(renderCoeffTimes(coeff, `(${qCore})^(${-(k - 1)})`));
51685
+ parts.push(renderCoeffTimes2(coeff, `(${qCore})^(${-(k - 1)})`));
51020
51686
  }
51021
51687
  }
51022
51688
  if (K.num !== 0n) {
@@ -51026,15 +51692,15 @@ function integrateQuadraticTerm(factor2, k, numer, v) {
51026
51692
  if (coeff.num === 0n) {
51027
51693
  continue;
51028
51694
  }
51029
- parts.push(renderCoeffTimes(coeff, `(${twoXb})/(${qCore})^(${t.power})`));
51695
+ parts.push(renderCoeffTimes2(coeff, `(${twoXb})/(${qCore})^(${t.power})`));
51030
51696
  }
51031
51697
  const atanCoeff = ratMul(ratMul(K, ik.atanCoeff), ratFromBigint(2n));
51032
51698
  if (atanCoeff.num !== 0n) {
51033
51699
  const d2s = ratToStr(d2);
51034
- parts.push(renderCoeffTimes(atanCoeff, `atan((${twoXb})/sqrt(${d2s}))/sqrt(${d2s})`));
51700
+ parts.push(renderCoeffTimes2(atanCoeff, `atan((${twoXb})/sqrt(${d2s}))/sqrt(${d2s})`));
51035
51701
  }
51036
51702
  }
51037
- return joinTerms(parts);
51703
+ return joinTerms2(parts);
51038
51704
  }
51039
51705
  function integrateQuadraticPosTerm(factor2, numer, v) {
51040
51706
  const c = factor2[0];
@@ -51064,7 +51730,7 @@ function integrateQuadraticPosTerm(factor2, numer, v) {
51064
51730
  };
51065
51731
  emit(A, r1);
51066
51732
  emit(B, r2);
51067
- return joinTerms(parts);
51733
+ return joinTerms2(parts);
51068
51734
  }
51069
51735
  function integratePFTerm(term, v) {
51070
51736
  const factor2 = trim(term.factor);
@@ -51092,7 +51758,13 @@ function integrateRationalFunction(expr, v) {
51092
51758
  }
51093
51759
  const factors = factorDenominator(rf.denom);
51094
51760
  if (factors === null) {
51095
- 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);
51096
51768
  }
51097
51769
  const { quotient, remainder } = polynomialPart(rf);
51098
51770
  const terms = partialFractions(remainder, factors);
@@ -51118,8 +51790,11 @@ function integrateRationalFunction(expr, v) {
51118
51790
  parts.push(s);
51119
51791
  }
51120
51792
  }
51121
- return joinTerms(parts);
51122
- } 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
+ }
51123
51798
  return null;
51124
51799
  }
51125
51800
  }
@@ -51193,6 +51868,18 @@ function integrateNode(raw, x) {
51193
51868
  const k = evalConst(args[0].toString()) / a;
51194
51869
  return `${num(k)} * log(abs(${args[1].toString()}))`;
51195
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
+ }
51196
51883
  }
51197
51884
  throw new NotIntegrable("general quotient");
51198
51885
  }
@@ -52046,7 +52733,7 @@ function fminbound(f, x1, x2) {
52046
52733
  let x = xf;
52047
52734
  let fx = f(x);
52048
52735
  let num2 = 1;
52049
- let fu = Infinity;
52736
+ let fu;
52050
52737
  let ffulc = fx;
52051
52738
  let fnfc = fx;
52052
52739
  let xm = 0.5 * (a + b);
@@ -52306,7 +52993,7 @@ function wate(freq, fx, wtx, lband, jtype) {
52306
52993
  if (fx[lband] >= 1e-4) return wtx[lband] / freq;
52307
52994
  return wtx[lband];
52308
52995
  }
52309
- 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) {
52310
52997
  const size2 = ngrid + 2 * nfcns + 8;
52311
52998
  const a = new Array(size2).fill(0);
52312
52999
  const p = new Array(size2).fill(0);
@@ -52630,7 +53317,7 @@ function remezCore(des, grid, edge, wt, ngrid, nbands, iext, alpha, nfcns, itrma
52630
53317
  xt = (xt - bb) / aa;
52631
53318
  ft = Math.acos(xt) / TWOPI;
52632
53319
  }
52633
- let assigned = false;
53320
+ let assigned;
52634
53321
  for (; ; ) {
52635
53322
  const xe = x[l];
52636
53323
  if (xt > xe) {
@@ -52661,14 +53348,14 @@ function remezCore(des, grid, edge, wt, ngrid, nbands, iext, alpha, nfcns, itrma
52661
53348
  if (nm1 >= 1) {
52662
53349
  for (let k = 1; k <= nm1; k++) dtemp += a[k + 1] * Math.cos(dnum * k);
52663
53350
  }
52664
- alpha[j] = 2 * dtemp + a[1];
53351
+ alpha2[j] = 2 * dtemp + a[1];
52665
53352
  }
52666
- for (j = 2; j <= nfcns; j++) alpha[j] *= 2 / cn;
52667
- alpha[1] /= cn;
53353
+ for (j = 2; j <= nfcns; j++) alpha2[j] *= 2 / cn;
53354
+ alpha2[1] /= cn;
52668
53355
  if (kkk !== 1) {
52669
- p[1] = 2 * alpha[nfcns] * bb + alpha[nm1];
52670
- p[2] = 2 * aa * alpha[nfcns];
52671
- 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];
52672
53359
  for (j = 2; j <= nm1; j++) {
52673
53360
  if (j >= nm1) {
52674
53361
  aa *= 0.5;
@@ -52686,14 +53373,14 @@ function remezCore(des, grid, edge, wt, ngrid, nbands, iext, alpha, nfcns, itrma
52686
53373
  for (let k = 3; k <= jp1; k++) p[k] += aa * a[k - 1];
52687
53374
  if (j !== nm1) {
52688
53375
  for (let k = 1; k <= j; k++) q[k] = -a[k];
52689
- q[1] += alpha[nfcns - 1 - j];
53376
+ q[1] += alpha2[nfcns - 1 - j];
52690
53377
  }
52691
53378
  }
52692
- for (j = 1; j <= nfcns; j++) alpha[j] = p[j];
53379
+ for (j = 1; j <= nfcns; j++) alpha2[j] = p[j];
52693
53380
  }
52694
53381
  if (nfcns <= 3) {
52695
- alpha[nfcns + 1] = 0;
52696
- alpha[nfcns + 2] = 0;
53382
+ alpha2[nfcns + 1] = 0;
53383
+ alpha2[nfcns + 2] = 0;
52697
53384
  }
52698
53385
  return { status: 0, dev, niter };
52699
53386
  }
@@ -52727,7 +53414,7 @@ function remezExchange(numtaps, bands, desired, weight, type = "bandpass", maxit
52727
53414
  const des = new Array(wrksize + 2).fill(0);
52728
53415
  const grid = new Array(wrksize + 2).fill(0);
52729
53416
  const wt = new Array(wrksize + 2).fill(0);
52730
- const alpha = new Array(dimsize + 3).fill(0);
53417
+ const alpha2 = new Array(dimsize + 3).fill(0);
52731
53418
  const iext = new Array(dimsize + 3).fill(0);
52732
53419
  let delf = gridDensity * nfcns;
52733
53420
  delf = 0.5 / delf;
@@ -52784,31 +53471,31 @@ function remezExchange(numtaps, bands, desired, weight, type = "bandpass", maxit
52784
53471
  const temp = (ngrid - 1) / nfcns;
52785
53472
  for (j = 1; j <= nfcns; j++) iext[j] = Math.floor((j - 1) * temp) + 1;
52786
53473
  iext[nfcns + 1] = ngrid;
52787
- 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);
52788
53475
  if (res.status < 0) throw new Error("remez: Remez exchange failed to converge");
52789
53476
  const h = new Array(numtaps + 1).fill(0);
52790
53477
  const nz = nfcns + 1;
52791
53478
  const nm1 = nfcns - 1;
52792
53479
  if (neg2 <= 0) {
52793
53480
  if (nodd !== 0) {
52794
- for (j = 1; j <= nm1; j++) h[j] = 0.5 * alpha[nz - j];
52795
- h[nfcns] = alpha[1];
53481
+ for (j = 1; j <= nm1; j++) h[j] = 0.5 * alpha2[nz - j];
53482
+ h[nfcns] = alpha2[1];
52796
53483
  } else {
52797
- h[1] = 0.25 * alpha[nfcns];
52798
- for (j = 2; j <= nm1; j++) h[j] = 0.25 * (alpha[nz - j] + alpha[nfcns + 2 - j]);
52799
- 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];
52800
53487
  }
52801
53488
  } else {
52802
53489
  if (nodd !== 0) {
52803
- h[1] = 0.25 * alpha[nfcns];
52804
- h[2] = 0.25 * alpha[nm1];
52805
- for (j = 3; j <= nm1; j++) h[j] = 0.25 * (alpha[nz - j] - alpha[nfcns + 3 - j]);
52806
- 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];
52807
53494
  h[nz] = 0;
52808
53495
  } else {
52809
- h[1] = 0.25 * alpha[nfcns];
52810
- for (j = 2; j <= nm1; j++) h[j] = 0.25 * (alpha[nz - j] - alpha[nfcns + 2 - j]);
52811
- 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];
52812
53499
  }
52813
53500
  }
52814
53501
  for (j = 1; j <= nfcns; j++) {
@@ -53040,7 +53727,7 @@ function idwt(approx, detail, wavelet = "haar") {
53040
53727
  try {
53041
53728
  return idwtPeriodization(approx, detail, wavelet);
53042
53729
  } catch (err) {
53043
- 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 });
53044
53731
  }
53045
53732
  }
53046
53733
  function wavedec(x, wavelet = "haar", level = 1) {
@@ -53770,10 +54457,10 @@ function circumradius(a, b, c) {
53770
54457
  const uy = (a2 * (c[0] - b[0]) + b2 * (a[0] - c[0]) + c2 * (b[0] - a[0])) / d;
53771
54458
  return Math.hypot(a[0] - ux, a[1] - uy);
53772
54459
  }
53773
- function alphaShape(points, alpha) {
53774
- 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");
53775
54462
  const { simplices: tris } = delaunay(points);
53776
- const threshold = 1 / alpha;
54463
+ const threshold = 1 / alpha2;
53777
54464
  const triangles = [];
53778
54465
  const edgeCount = /* @__PURE__ */ new Map();
53779
54466
  for (const t of tris) {
@@ -54262,34 +54949,34 @@ function polygamma(n, x) {
54262
54949
  function trigamma(x) {
54263
54950
  return polygamma(1, x);
54264
54951
  }
54265
- function jacobiP(n, alpha, beta2, x) {
54952
+ function jacobiP(n, alpha2, beta3, x) {
54266
54953
  if (!Number.isInteger(n) || n < 0) {
54267
54954
  throw new Error("jacobiP: n must be a nonnegative integer");
54268
54955
  }
54269
54956
  if (n === 0) return 1;
54270
54957
  let pPrev = 1;
54271
- let pCurr = alpha + 1 + (alpha + beta2 + 2) * (x - 1) / 2;
54958
+ let pCurr = alpha2 + 1 + (alpha2 + beta3 + 2) * (x - 1) / 2;
54272
54959
  if (n === 1) return pCurr;
54273
54960
  for (let k = 2; k <= n; k++) {
54274
- const a1 = 2 * k * (k + alpha + beta2) * (2 * k + alpha + beta2 - 2);
54275
- const a2 = (2 * k + alpha + beta2 - 1) * ((2 * k + alpha + beta2) * (2 * k + alpha + beta2 - 2) * x + alpha * alpha - beta2 * beta2);
54276
- 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);
54277
54964
  const pNext = (a2 * pCurr - a3 * pPrev) / a1;
54278
54965
  pPrev = pCurr;
54279
54966
  pCurr = pNext;
54280
54967
  }
54281
54968
  return pCurr;
54282
54969
  }
54283
- function gegenbauerC(n, alpha, x) {
54970
+ function gegenbauerC(n, alpha2, x) {
54284
54971
  if (!Number.isInteger(n) || n < 0) {
54285
54972
  throw new Error("gegenbauerC: n must be a nonnegative integer");
54286
54973
  }
54287
54974
  if (n === 0) return 1;
54288
54975
  let cPrev = 1;
54289
- let cCurr = 2 * alpha * x;
54976
+ let cCurr = 2 * alpha2 * x;
54290
54977
  if (n === 1) return cCurr;
54291
54978
  for (let k = 2; k <= n; k++) {
54292
- 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;
54293
54980
  cPrev = cCurr;
54294
54981
  cCurr = cNext;
54295
54982
  }
@@ -54318,7 +55005,7 @@ function rootsLegendre(n) {
54318
55005
  const weights = [];
54319
55006
  for (let i = 0; i < n; i++) {
54320
55007
  let x = Math.cos(Math.PI * (i + 0.75) / (n + 0.5));
54321
- let pn = 0;
55008
+ let pn;
54322
55009
  let dpn = 0;
54323
55010
  for (let iter = 0; iter < NEWTON_MAX_ITERATIONS; iter++) {
54324
55011
  const [pCurr, pPrev] = legendrePair(n, x);
@@ -54675,7 +55362,7 @@ function coulombFG(L, eta, rho) {
54675
55362
  }
54676
55363
 
54677
55364
  // src/special/mathieu.ts
54678
- import { eig as eig4 } from "@danielsimonjr/mathts-matrix";
55365
+ import { eig as eig5 } from "@danielsimonjr/mathts-matrix";
54679
55366
  var N = 50;
54680
55367
  function harmonicsFor(cls) {
54681
55368
  const h = new Int32Array(N);
@@ -54714,7 +55401,7 @@ function solveClass(cls, q) {
54714
55401
  const cached = modeCache.get(key2);
54715
55402
  if (cached) return cached;
54716
55403
  const harmonics = harmonicsFor(cls);
54717
- const { values, vectors } = eig4(buildMatrix(cls, q));
55404
+ const { values, vectors } = eig5(buildMatrix(cls, q));
54718
55405
  const order = values.map((_, i) => i).sort((i, j) => values[i].re - values[j].re);
54719
55406
  const modes = order.map((idx, m) => {
54720
55407
  const g = vectors[idx];
@@ -54774,6 +55461,184 @@ function mathieuSe(n, q, x) {
54774
55461
  return evalSeries(solveClass(cls, q)[m], x, Math.sin);
54775
55462
  }
54776
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
+
54777
55642
  // src/graph/traversal-centrality.ts
54778
55643
  function bfs(adj, start) {
54779
55644
  const n = adj.length;
@@ -54997,7 +55862,10 @@ function minCut(capacity, source, sink) {
54997
55862
  }
54998
55863
  function astar(adj, start, goal, heuristic) {
54999
55864
  const n = adj.length;
55000
- 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) {
55001
55869
  throw new Error("astar: start or goal out of bounds");
55002
55870
  }
55003
55871
  if (start === goal) {
@@ -55032,7 +55900,8 @@ function astar(adj, start, goal, heuristic) {
55032
55900
  return { path: [], cost: Infinity };
55033
55901
  }
55034
55902
  const path = [];
55035
- 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;
55036
55905
  path.unshift(v);
55037
55906
  if (v === start) break;
55038
55907
  }
@@ -55432,14 +56301,14 @@ function _solveLinearSystem(A, b) {
55432
56301
  }
55433
56302
  return M.map((row2) => row2[n]);
55434
56303
  }
55435
- function katzCentrality(adj, alpha, beta2 = 1) {
56304
+ function katzCentrality(adj, alpha2, beta3 = 1) {
55436
56305
  const n = adj.length;
55437
56306
  if (n === 0) return [];
55438
56307
  const M = Array.from(
55439
56308
  { length: n },
55440
- (_, 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])
55441
56310
  );
55442
- const b = new Array(n).fill(beta2);
56311
+ const b = new Array(n).fill(beta3);
55443
56312
  const x = _solveLinearSystem(M, b);
55444
56313
  let sum3 = 0;
55445
56314
  let sumSq = 0;
@@ -55571,7 +56440,7 @@ function glm(X, y, opts) {
55571
56440
  const tol = opts.tol ?? 1e-10;
55572
56441
  const maxIter = opts.maxIter ?? 100;
55573
56442
  let eta = family.mustart(y).map((mu2) => linkFns.linkFn(mu2));
55574
- let beta2 = new Array(p).fill(0);
56443
+ let beta3 = new Array(p).fill(0);
55575
56444
  let iterations = 0;
55576
56445
  for (let iter = 0; iter < maxIter; iter++) {
55577
56446
  iterations = iter + 1;
@@ -55608,7 +56477,7 @@ function glm(X, y, opts) {
55608
56477
  const newEta = design.map((row2) => row2.reduce((s, v, j) => s + v * newBeta[j], 0));
55609
56478
  let maxDiff = 0;
55610
56479
  for (let i = 0; i < n; i++) maxDiff = Math.max(maxDiff, Math.abs(newEta[i] - eta[i]));
55611
- beta2 = newBeta;
56480
+ beta3 = newBeta;
55612
56481
  eta = newEta;
55613
56482
  if (maxDiff < tol) break;
55614
56483
  }
@@ -55616,9 +56485,9 @@ function glm(X, y, opts) {
55616
56485
  const deviance = y.reduce((sum3, yi, i) => sum3 + family.deviance(yi, mu[i]), 0);
55617
56486
  const predict = (x) => {
55618
56487
  const Dx = useIntercept ? x.map((row2) => [1, ...row2]) : x.map((row2) => [...row2]);
55619
- 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)));
55620
56489
  };
55621
- return { coefficients: beta2, fittedValues: mu, deviance, iterations, predict };
56490
+ return { coefficients: beta3, fittedValues: mu, deviance, iterations, predict };
55622
56491
  }
55623
56492
 
55624
56493
  // src/stats/mvn.ts
@@ -55683,22 +56552,22 @@ function mvnSample(mean7, cov2, n, opts) {
55683
56552
  }
55684
56553
 
55685
56554
  // src/stats/power-analysis.ts
55686
- function powerAt(effectSize, nobs, alpha, alt) {
56555
+ function powerAt(effectSize, nobs, alpha2, alt) {
55687
56556
  const df = 2 * (nobs - 1);
55688
56557
  const nc = effectSize * Math.sqrt(nobs / 2);
55689
56558
  if (alt === "larger") {
55690
- const tCrit2 = studentTQuantile(1 - alpha, df);
56559
+ const tCrit2 = studentTQuantile(1 - alpha2, df);
55691
56560
  return 1 - noncentralTCDF(tCrit2, df, nc);
55692
56561
  }
55693
56562
  if (alt === "smaller") {
55694
- const tCrit2 = studentTQuantile(alpha, df);
56563
+ const tCrit2 = studentTQuantile(alpha2, df);
55695
56564
  return noncentralTCDF(tCrit2, df, nc);
55696
56565
  }
55697
- const tCrit = studentTQuantile(1 - alpha / 2, df);
56566
+ const tCrit = studentTQuantile(1 - alpha2 / 2, df);
55698
56567
  return 1 - noncentralTCDF(tCrit, df, nc) + noncentralTCDF(-tCrit, df, nc);
55699
56568
  }
55700
- function tTestPower(effectSize, nobsOrPower, alpha, opts = {}) {
55701
- 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)");
55702
56571
  const alt = opts.alternative ?? "two-sided";
55703
56572
  if (opts.solveFor === "nobs") {
55704
56573
  const targetPower = nobsOrPower;
@@ -55708,19 +56577,19 @@ function tTestPower(effectSize, nobsOrPower, alpha, opts = {}) {
55708
56577
  if (effectSize === 0) throw new Error("tTestPower: cannot solve for nobs when effectSize is 0");
55709
56578
  let lo = 2 + 1e-9;
55710
56579
  let hi = 1e7;
55711
- if (powerAt(effectSize, hi, alpha, alt) < targetPower) {
56580
+ if (powerAt(effectSize, hi, alpha2, alt) < targetPower) {
55712
56581
  throw new Error("tTestPower: target power unreachable even at nobs = 1e7");
55713
56582
  }
55714
56583
  for (let it = 0; it < 200; it++) {
55715
56584
  const mid = (lo + hi) / 2;
55716
- if (powerAt(effectSize, mid, alpha, alt) < targetPower) lo = mid;
56585
+ if (powerAt(effectSize, mid, alpha2, alt) < targetPower) lo = mid;
55717
56586
  else hi = mid;
55718
56587
  }
55719
56588
  return (lo + hi) / 2;
55720
56589
  }
55721
56590
  const nobs = nobsOrPower;
55722
56591
  if (!(nobs > 1)) throw new Error("tTestPower: nobs must be greater than 1");
55723
- return powerAt(effectSize, nobs, alpha, alt);
56592
+ return powerAt(effectSize, nobs, alpha2, alt);
55724
56593
  }
55725
56594
 
55726
56595
  // src/stats/gaussian-process.ts
@@ -55856,29 +56725,29 @@ function gaussianProcessRegression(X, y, options = {}) {
55856
56725
  var gpRegression = gaussianProcessRegression;
55857
56726
 
55858
56727
  // src/stats/multivariate-sampling.ts
55859
- function validateAlpha(alpha, fn) {
55860
- if (!Array.isArray(alpha) || alpha.length < 2) {
56728
+ function validateAlpha(alpha2, fn) {
56729
+ if (!Array.isArray(alpha2) || alpha2.length < 2) {
55861
56730
  throw new Error(`${fn}: alpha must be an array of at least 2 concentration parameters`);
55862
56731
  }
55863
- for (const a of alpha) {
56732
+ for (const a of alpha2) {
55864
56733
  if (!(a > 0) || !Number.isFinite(a)) {
55865
56734
  throw new Error(`${fn}: all concentration parameters must be positive and finite`);
55866
56735
  }
55867
56736
  }
55868
56737
  }
55869
- function dirichletSample(alpha, n = 1, opts) {
55870
- validateAlpha(alpha, "dirichletSample");
56738
+ function dirichletSample(alpha2, n = 1, opts) {
56739
+ validateAlpha(alpha2, "dirichletSample");
55871
56740
  if (!Number.isInteger(n) || n < 1) {
55872
56741
  throw new Error("dirichletSample: n must be a positive integer");
55873
56742
  }
55874
- const k = alpha.length;
56743
+ const k = alpha2.length;
55875
56744
  const rng = createRng(opts?.seed ?? null);
55876
56745
  const samples = new Array(n);
55877
56746
  for (let s = 0; s < n; s++) {
55878
56747
  const g = new Array(k);
55879
56748
  let total = 0;
55880
56749
  for (let i = 0; i < k; i++) {
55881
- const gi = gammaSampleRng(alpha[i], rng);
56750
+ const gi = gammaSampleRng(alpha2[i], rng);
55882
56751
  g[i] = gi;
55883
56752
  total += gi;
55884
56753
  }
@@ -55888,10 +56757,10 @@ function dirichletSample(alpha, n = 1, opts) {
55888
56757
  }
55889
56758
  return samples;
55890
56759
  }
55891
- function dirichletPdf(x, alpha) {
55892
- validateAlpha(alpha, "dirichletPdf");
55893
- if (x.length !== alpha.length) {
55894
- 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}`);
55895
56764
  }
55896
56765
  let sum3 = 0;
55897
56766
  for (const xi of x) {
@@ -55905,7 +56774,7 @@ function dirichletPdf(x, alpha) {
55905
56774
  }
55906
56775
  let a0 = 0;
55907
56776
  let logB = 0;
55908
- for (const a of alpha) {
56777
+ for (const a of alpha2) {
55909
56778
  logB += lgammaNumber(a);
55910
56779
  a0 += a;
55911
56780
  }
@@ -55913,7 +56782,7 @@ function dirichletPdf(x, alpha) {
55913
56782
  let logpdf = -logB;
55914
56783
  for (let i = 0; i < x.length; i++) {
55915
56784
  const xi = x[i];
55916
- const p = alpha[i] - 1;
56785
+ const p = alpha2[i] - 1;
55917
56786
  if (p !== 0) {
55918
56787
  logpdf += p * Math.log(xi);
55919
56788
  }
@@ -56359,6 +57228,7 @@ export {
56359
57228
  fallingFactorial,
56360
57229
  faraday,
56361
57230
  fermiCoupling,
57231
+ ferrersP,
56362
57232
  fft,
56363
57233
  fft2d,
56364
57234
  fftGpuDispatch,
@@ -56916,9 +57786,14 @@ export {
56916
57786
  spearman,
56917
57787
  spearmanr,
56918
57788
  spectralClustering,
57789
+ spectralRadiance,
56919
57790
  spectrogram,
56920
57791
  speedOfLight,
56921
57792
  sphericalVoronoi,
57793
+ spheroidalAngular,
57794
+ spheroidalCharacteristic,
57795
+ spheroidalLambda,
57796
+ spheroidalRadial,
56922
57797
  splitUnit,
56923
57798
  sqrt,
56924
57799
  sqrtm,