@danielsimonjr/mathts-functions 0.65.0 → 0.67.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -24,6 +24,10 @@ __export(typed_exports, {
24
24
  CENTRALITY_WORKER_THRESHOLD: () => CENTRALITY_WORKER_THRESHOLD,
25
25
  DIST_WORKER_THRESHOLD: () => DIST_WORKER_THRESHOLD,
26
26
  GAUSS_WORKER_THRESHOLD: () => GAUSS_WORKER_THRESHOLD,
27
+ GL4ConvergenceError: () => GL4ConvergenceError,
28
+ GL4_A: () => GL4_A,
29
+ GL4_B: () => GL4_B,
30
+ GL4_C: () => GL4_C,
27
31
  GPU_ELEMENTWISE_OPS: () => GPU_ELEMENTWISE_OPS,
28
32
  GPU_MIN_ELEMENTS: () => GPU_MIN_ELEMENTS2,
29
33
  GPU_REDUCE_OPS: () => GPU_REDUCE_OPS,
@@ -206,6 +210,7 @@ __export(typed_exports, {
206
210
  gammaPDF: () => gammaPDF,
207
211
  gammainc: () => gammainc,
208
212
  gammaincp: () => gammaincp,
213
+ gaussLegendre4: () => gaussLegendre4,
209
214
  gaussQuad: () => gaussQuad,
210
215
  gcd: () => gcd,
211
216
  geometricPMF: () => geometricPMF,
@@ -397,6 +402,7 @@ __export(typed_exports, {
397
402
  random: () => random,
398
403
  randomInt: () => randomInt,
399
404
  rank: () => rank,
405
+ rationalNullspace: () => rationalNullspace,
400
406
  rayleighDist: () => rayleighDist,
401
407
  rbfInterpolate: () => rbfInterpolate,
402
408
  re: () => re,
@@ -1683,6 +1689,8 @@ var pow = mathTyped2("pow", {
1683
1689
  // Dual numbers (forward-mode AD): constant or variable exponent.
1684
1690
  "Dual, number": (a, b) => a.powConst(b),
1685
1691
  "Dual, Dual": (a, b) => a.pow(b),
1692
+ // Dimensional power, including physics-mode `e^2` (elementary charge squared).
1693
+ "Unit, number": (a, b) => a.pow(b),
1686
1694
  // Matrix power A^n for a square 2-D array and non-negative integer n, via binary
1687
1695
  // exponentiation on the native DenseMatrix backend (accelerated matmul, B2). Element-
1688
1696
  // wise power is `dotPow`; negative/fractional matrix powers need eigendecomposition —
@@ -11119,8 +11127,8 @@ function integerPolynomialGCD(a, b) {
11119
11127
  return g.map((c) => Number(c) / lead);
11120
11128
  }
11121
11129
  function polynomialLCM(a, b) {
11122
- const gcd2 = polynomialGCD(a, b);
11123
- const [quotient] = polyDivMod(polymul(a, b), gcd2);
11130
+ const gcd3 = polynomialGCD(a, b);
11131
+ const [quotient] = polyDivMod(polymul(a, b), gcd3);
11124
11132
  const leading = quotient[quotient.length - 1];
11125
11133
  if (leading !== 0 && leading !== 1) {
11126
11134
  return quotient.map((c) => c / leading);
@@ -14116,6 +14124,126 @@ function quad(f, a, b, opts) {
14116
14124
  return panel(a, b, 0);
14117
14125
  }
14118
14126
 
14127
+ // src/numeric/rational-nullspace.ts
14128
+ import { Fraction as Fraction2, isFraction as isFraction2 } from "@danielsimonjr/mathts-core";
14129
+ function gcd2(a, b) {
14130
+ let x = a < 0n ? -a : a;
14131
+ let y = b < 0n ? -b : b;
14132
+ while (y !== 0n) {
14133
+ const t = y;
14134
+ y = x % y;
14135
+ x = t;
14136
+ }
14137
+ return x;
14138
+ }
14139
+ function lcm2(a, b) {
14140
+ if (a === 0n || b === 0n) return 0n;
14141
+ return a / gcd2(a, b) * b;
14142
+ }
14143
+ function entryToFraction(value, row2, col) {
14144
+ if (isFraction2(value)) return value;
14145
+ if (typeof value !== "number" || !Number.isFinite(value)) {
14146
+ throw new TypeError(
14147
+ `rationalNullspace: entry [${row2}, ${col}] must be a finite number or a Fraction`
14148
+ );
14149
+ }
14150
+ return Number.isInteger(value) ? new Fraction2(value, 1) : Fraction2.fromNumber(value);
14151
+ }
14152
+ function integerize(vec) {
14153
+ let scale4 = 1n;
14154
+ for (const f of vec) {
14155
+ if (f.numerator !== 0n) scale4 = lcm2(scale4, f.denominator);
14156
+ }
14157
+ const ints = vec.map((f) => f.numerator * scale4 / f.denominator);
14158
+ let content2 = 0n;
14159
+ for (const x of ints) {
14160
+ if (x !== 0n) content2 = gcd2(content2, x);
14161
+ }
14162
+ if (content2 === 0n) content2 = 1n;
14163
+ const reduced = ints.map((x) => x / content2);
14164
+ const first = reduced.find((x) => x !== 0n) ?? 0n;
14165
+ const sign3 = first < 0n ? -1n : 1n;
14166
+ return reduced.map((x) => new Fraction2(sign3 * x, 1n));
14167
+ }
14168
+ function rref(matrix2) {
14169
+ const rows = matrix2.length;
14170
+ const cols = rows === 0 ? 0 : matrix2[0].length;
14171
+ const pivotCols = [];
14172
+ let r = 0;
14173
+ for (let c = 0; c < cols && r < rows; c++) {
14174
+ let pivotRow = -1;
14175
+ for (let i = r; i < rows; i++) {
14176
+ if (matrix2[i][c].numerator !== 0n) {
14177
+ pivotRow = i;
14178
+ break;
14179
+ }
14180
+ }
14181
+ if (pivotRow === -1) continue;
14182
+ if (pivotRow !== r) {
14183
+ const swap = matrix2[r];
14184
+ matrix2[r] = matrix2[pivotRow];
14185
+ matrix2[pivotRow] = swap;
14186
+ }
14187
+ const pivot = matrix2[r][c];
14188
+ for (let j = 0; j < cols; j++) matrix2[r][j] = matrix2[r][j].div(pivot);
14189
+ for (let i = 0; i < rows; i++) {
14190
+ if (i === r || matrix2[i][c].numerator === 0n) continue;
14191
+ const factor2 = matrix2[i][c];
14192
+ for (let j = 0; j < cols; j++) {
14193
+ matrix2[i][j] = matrix2[i][j].sub(factor2.mul(matrix2[r][j]));
14194
+ }
14195
+ }
14196
+ pivotCols.push(c);
14197
+ r++;
14198
+ }
14199
+ return pivotCols;
14200
+ }
14201
+ function rationalNullspace(matrix2, options) {
14202
+ if (matrix2.length === 0) {
14203
+ const cols2 = options?.columns ?? 0;
14204
+ if (!Number.isInteger(cols2) || cols2 < 0) {
14205
+ throw new TypeError("rationalNullspace: columns must be a non-negative integer");
14206
+ }
14207
+ const basis2 = [];
14208
+ for (let free = 0; free < cols2; free++) {
14209
+ const vec = [];
14210
+ for (let j = 0; j < cols2; j++) vec.push(new Fraction2(j === free ? 1n : 0n, 1n));
14211
+ basis2.push(vec);
14212
+ }
14213
+ return { basis: basis2, rank: 0 };
14214
+ }
14215
+ const cols = matrix2[0].length;
14216
+ if (options?.columns !== void 0 && options.columns !== cols) {
14217
+ throw new TypeError(
14218
+ `rationalNullspace: columns ${options.columns} does not match row length ${cols}`
14219
+ );
14220
+ }
14221
+ for (let i = 0; i < matrix2.length; i++) {
14222
+ if (!Array.isArray(matrix2[i]) || matrix2[i].length !== cols) {
14223
+ throw new TypeError(
14224
+ `rationalNullspace: row ${i} has length ${matrix2[i]?.length ?? "n/a"}; expected ${cols}`
14225
+ );
14226
+ }
14227
+ }
14228
+ const reduced = matrix2.map((row2, i) => row2.map((value, j) => entryToFraction(value, i, j)));
14229
+ const pivotCols = rref(reduced);
14230
+ const pivotSet = new Set(pivotCols);
14231
+ const basis = [];
14232
+ const zero = new Fraction2(0n, 1n);
14233
+ const one = new Fraction2(1n, 1n);
14234
+ for (let free = 0; free < cols; free++) {
14235
+ if (pivotSet.has(free)) continue;
14236
+ const vec = new Array(cols);
14237
+ for (let j = 0; j < cols; j++) vec[j] = zero;
14238
+ vec[free] = one;
14239
+ for (let i = 0; i < pivotCols.length; i++) {
14240
+ vec[pivotCols[i]] = zero.sub(reduced[i][free]);
14241
+ }
14242
+ basis.push(integerize(vec));
14243
+ }
14244
+ return { basis, rank: pivotCols.length };
14245
+ }
14246
+
14119
14247
  // src/numeric/solveParabolicPDE.ts
14120
14248
  function asSpaceFn(c, dflt) {
14121
14249
  if (c === void 0) return () => dflt;
@@ -14681,6 +14809,129 @@ function solveDDE(f, tspan, history, delays, options = {}) {
14681
14809
  };
14682
14810
  }
14683
14811
 
14812
+ // src/numeric/gauss-legendre4.ts
14813
+ var SQRT3_OVER_6 = Math.sqrt(3) / 6;
14814
+ var GL4_C = [0.5 - SQRT3_OVER_6, 0.5 + SQRT3_OVER_6];
14815
+ var GL4_A = [
14816
+ [0.25, 0.25 - SQRT3_OVER_6],
14817
+ [0.25 + SQRT3_OVER_6, 0.25]
14818
+ ];
14819
+ var GL4_B = [0.5, 0.5];
14820
+ var GL4ConvergenceError = class extends Error {
14821
+ constructor(message = "Picard iteration did not converge") {
14822
+ super(message);
14823
+ this.name = "GL4ConvergenceError";
14824
+ }
14825
+ };
14826
+ function assertFiniteVector(label, v) {
14827
+ for (let i = 0; i < v.length; i++) {
14828
+ if (!Number.isFinite(v[i])) {
14829
+ throw new TypeError(`${label}[${i}] must be a finite number`);
14830
+ }
14831
+ }
14832
+ }
14833
+ function gaussLegendre4(f, y0, tspan, options) {
14834
+ if (y0.length === 0) {
14835
+ throw new TypeError("y0 must be a non-empty vector");
14836
+ }
14837
+ assertFiniteVector("y0", y0);
14838
+ const t0 = tspan[0];
14839
+ const t1 = tspan[1];
14840
+ if (!Number.isFinite(t0) || !Number.isFinite(t1)) {
14841
+ throw new TypeError("tspan entries must be finite numbers");
14842
+ }
14843
+ const picardTol = options?.picardTol ?? 1e-12;
14844
+ const picardMaxIter = options?.picardMaxIter ?? 64;
14845
+ if (!(picardTol > 0) || !Number.isFinite(picardTol)) {
14846
+ throw new TypeError("picardTol must be a positive finite number");
14847
+ }
14848
+ if (!Number.isInteger(picardMaxIter) || picardMaxIter < 1) {
14849
+ throw new TypeError("picardMaxIter must be a positive integer");
14850
+ }
14851
+ if (t0 === t1) {
14852
+ return { t: [t0], y: [y0.slice()], iterations: [] };
14853
+ }
14854
+ const span = t1 - t0;
14855
+ let nSteps;
14856
+ if (options?.steps !== void 0) {
14857
+ nSteps = options.steps;
14858
+ if (!Number.isInteger(nSteps) || nSteps < 1) {
14859
+ throw new TypeError("steps must be a positive integer");
14860
+ }
14861
+ } else if (options?.h !== void 0) {
14862
+ const hAbs = options.h;
14863
+ if (!(hAbs > 0) || !Number.isFinite(hAbs)) {
14864
+ throw new TypeError("h must be a positive finite number");
14865
+ }
14866
+ nSteps = Math.max(1, Math.ceil(Math.abs(span) / hAbs));
14867
+ } else {
14868
+ nSteps = 100;
14869
+ }
14870
+ const h = span / nSteps;
14871
+ const n = y0.length;
14872
+ const c0 = GL4_C[0];
14873
+ const c1 = GL4_C[1];
14874
+ const a00 = GL4_A[0][0];
14875
+ const a01 = GL4_A[0][1];
14876
+ const a10 = GL4_A[1][0];
14877
+ const a11 = GL4_A[1][1];
14878
+ const b0 = GL4_B[0];
14879
+ const b1 = GL4_B[1];
14880
+ const t = [t0];
14881
+ const y = [y0.slice()];
14882
+ const iterations = [];
14883
+ let tn = t0;
14884
+ let yn = y0.slice();
14885
+ for (let step = 0; step < nSteps; step++) {
14886
+ let Y0 = yn.slice();
14887
+ let Y1 = yn.slice();
14888
+ let converged = false;
14889
+ let used = 0;
14890
+ const next = new Array(n);
14891
+ for (let iter = 0; iter < picardMaxIter; iter++) {
14892
+ used = iter + 1;
14893
+ const k0 = f(tn + c0 * h, Y0);
14894
+ const k1 = f(tn + c1 * h, Y1);
14895
+ if (k0.length !== n || k1.length !== n) {
14896
+ throw new TypeError(`f must return a vector of length ${n}`);
14897
+ }
14898
+ assertFiniteVector("f(t, y)", k0);
14899
+ assertFiniteVector("f(t, y)", k1);
14900
+ let maxDiff = 0;
14901
+ const nY0 = new Array(n);
14902
+ const nY1 = new Array(n);
14903
+ for (let i = 0; i < n; i++) {
14904
+ nY0[i] = yn[i] + h * (a00 * k0[i] + a01 * k1[i]);
14905
+ nY1[i] = yn[i] + h * (a10 * k0[i] + a11 * k1[i]);
14906
+ const d0 = Math.abs(nY0[i] - Y0[i]);
14907
+ const d1 = Math.abs(nY1[i] - Y1[i]);
14908
+ if (d0 > maxDiff) maxDiff = d0;
14909
+ if (d1 > maxDiff) maxDiff = d1;
14910
+ }
14911
+ Y0 = nY0;
14912
+ Y1 = nY1;
14913
+ if (maxDiff <= picardTol) {
14914
+ for (let i = 0; i < n; i++) {
14915
+ next[i] = yn[i] + h * (b0 * k0[i] + b1 * k1[i]);
14916
+ }
14917
+ converged = true;
14918
+ break;
14919
+ }
14920
+ }
14921
+ if (!converged) {
14922
+ throw new GL4ConvergenceError(
14923
+ `Picard iteration did not converge within ${picardMaxIter} iterations (step h=${h})`
14924
+ );
14925
+ }
14926
+ tn += h;
14927
+ yn = next;
14928
+ t.push(tn);
14929
+ y.push(yn.slice());
14930
+ iterations.push(used);
14931
+ }
14932
+ return { t, y, iterations };
14933
+ }
14934
+
14684
14935
  // src/typed/numeric.ts
14685
14936
  function findRoot(f, a, b, opts) {
14686
14937
  const tol = opts?.tol ?? 1e-12;
@@ -19545,12 +19796,12 @@ function rowReduce(A, tol = 1e-10) {
19545
19796
  return M;
19546
19797
  }
19547
19798
  function matrixRank(A, tol = 1e-10) {
19548
- const rref = rowReduce(A, tol);
19799
+ const rref2 = rowReduce(A, tol);
19549
19800
  let r = 0;
19550
- for (let i = 0; i < rref.length; i++) {
19801
+ for (let i = 0; i < rref2.length; i++) {
19551
19802
  let nonzero = false;
19552
- for (let j = 0; j < rref[i].length; j++) {
19553
- if (Math.abs(rref[i][j]) > tol) {
19803
+ for (let j = 0; j < rref2[i].length; j++) {
19804
+ if (Math.abs(rref2[i][j]) > tol) {
19554
19805
  nonzero = true;
19555
19806
  break;
19556
19807
  }
@@ -19890,11 +20141,11 @@ async function jordanForm(A) {
19890
20141
  return { J, P: P2 };
19891
20142
  }
19892
20143
  function matrixRankInternal(A, tol) {
19893
- const rref = rowReduce(A, tol);
20144
+ const rref2 = rowReduce(A, tol);
19894
20145
  let r = 0;
19895
- for (let i = 0; i < rref.length; i++) {
19896
- for (let j = 0; j < rref[i].length; j++) {
19897
- if (Math.abs(rref[i][j]) > tol) {
20146
+ for (let i = 0; i < rref2.length; i++) {
20147
+ for (let j = 0; j < rref2[i].length; j++) {
20148
+ if (Math.abs(rref2[i][j]) > tol) {
19898
20149
  r++;
19899
20150
  break;
19900
20151
  }
@@ -19954,11 +20205,11 @@ async function buildJordanBlocks(A, lambda, algebraicMult, geoMult, n, tol) {
19954
20205
  return blocks;
19955
20206
  }
19956
20207
  function findKernelVector(A, n, tol) {
19957
- const rref = rowReduce(A, tol);
20208
+ const rref2 = rowReduce(A, tol);
19958
20209
  const pivotCols = /* @__PURE__ */ new Set();
19959
20210
  for (let i = 0; i < n; i++) {
19960
20211
  for (let j = 0; j < n; j++) {
19961
- if (Math.abs(rref[i][j]) > tol) {
20212
+ if (Math.abs(rref2[i][j]) > tol) {
19962
20213
  pivotCols.add(j);
19963
20214
  break;
19964
20215
  }
@@ -19980,8 +20231,8 @@ function findKernelVector(A, n, tol) {
19980
20231
  v[freeCol] = 1;
19981
20232
  for (let i = 0; i < n; i++) {
19982
20233
  for (let j = 0; j < n; j++) {
19983
- if (Math.abs(rref[i][j]) > tol) {
19984
- v[j] = -rref[i][freeCol];
20234
+ if (Math.abs(rref2[i][j]) > tol) {
20235
+ v[j] = -rref2[i][freeCol];
19985
20236
  break;
19986
20237
  }
19987
20238
  }
@@ -20114,7 +20365,7 @@ import { enableGpu, disableGpu, isGpuEnabled as isGpuEnabled3, GPU_MIN_ELEMENTS
20114
20365
 
20115
20366
  // src/typed/relational.ts
20116
20367
  import naturalSort from "javascript-natural-sort";
20117
- import { mathTyped as mathTyped14, Complex as Complex6, BigNumber as BigNumber6, Fraction as Fraction2 } from "@danielsimonjr/mathts-core";
20368
+ import { mathTyped as mathTyped14, Complex as Complex6, BigNumber as BigNumber6, Fraction as Fraction3 } from "@danielsimonjr/mathts-core";
20118
20369
  var REL_TOL = 1e-12;
20119
20370
  var ABS_TOL = 1e-15;
20120
20371
  function numbersNearlyEqual(x, y) {
@@ -20138,7 +20389,7 @@ function typeNameOf(x) {
20138
20389
  if (typeof x === "string") return "string";
20139
20390
  if (x instanceof Complex6) return "Complex";
20140
20391
  if (x instanceof BigNumber6) return "BigNumber";
20141
- if (x instanceof Fraction2) return "Fraction";
20392
+ if (x instanceof Fraction3) return "Fraction";
20142
20393
  if (Array.isArray(x)) return "Array";
20143
20394
  if (typeof x === "object") return "Object";
20144
20395
  return typeof x;
@@ -20276,7 +20527,7 @@ function _numericCompare(x, y) {
20276
20527
  let ny;
20277
20528
  if (x instanceof BigNumber6) {
20278
20529
  nx = x.toNumber();
20279
- } else if (x instanceof Fraction2) {
20530
+ } else if (x instanceof Fraction3) {
20280
20531
  nx = x.valueOf();
20281
20532
  } else if (typeof x === "bigint") {
20282
20533
  nx = Number(x);
@@ -20285,7 +20536,7 @@ function _numericCompare(x, y) {
20285
20536
  }
20286
20537
  if (y instanceof BigNumber6) {
20287
20538
  ny = y.toNumber();
20288
- } else if (y instanceof Fraction2) {
20539
+ } else if (y instanceof Fraction3) {
20289
20540
  ny = y.valueOf();
20290
20541
  } else if (typeof y === "bigint") {
20291
20542
  ny = Number(y);
@@ -20705,7 +20956,7 @@ import {
20705
20956
  } from "@danielsimonjr/mathts-expression";
20706
20957
  import {
20707
20958
  Complex as Complex9,
20708
- Fraction as Fraction4,
20959
+ Fraction as Fraction5,
20709
20960
  I,
20710
20961
  PHI,
20711
20962
  SQRT2,
@@ -20713,11 +20964,12 @@ import {
20713
20964
  LN2 as LN22,
20714
20965
  LN10,
20715
20966
  LOG2E,
20716
- LOG10E
20967
+ LOG10E,
20968
+ Unit as Unit2
20717
20969
  } from "@danielsimonjr/mathts-core";
20718
20970
 
20719
20971
  // src/factories/scope.ts
20720
- import { mathTyped as mathTyped18, Complex as Complex7, Fraction as Fraction3, BigNumber as BigNumber8 } from "@danielsimonjr/mathts-core";
20972
+ import { mathTyped as mathTyped18, Complex as Complex7, Fraction as Fraction4, BigNumber as BigNumber8 } from "@danielsimonjr/mathts-core";
20721
20973
 
20722
20974
  // src/typed/typed-bridge.ts
20723
20975
  import { registerNativeTypes } from "@danielsimonjr/mathts-core";
@@ -20832,7 +21084,7 @@ var factoryScope = {
20832
21084
  // Class constructors (uppercase) — factories use `new BigNumber(...)` etc.
20833
21085
  Complex: Complex7,
20834
21086
  BigNumber: BigNumber8,
20835
- Fraction: Fraction3,
21087
+ Fraction: Fraction4,
20836
21088
  // Matrix bridge classes
20837
21089
  DenseMatrix: MathJSDenseMatrix,
20838
21090
  SparseMatrix: MathJSSparseMatrix,
@@ -20846,7 +21098,7 @@ var factoryScope = {
20846
21098
  if (typeof x === "number") return BigNumber8.fromNumber(x);
20847
21099
  return BigNumber8.fromNumber(Number(x.valueOf()));
20848
21100
  },
20849
- fraction: (n, d) => new Fraction3(n, d ?? 1),
21101
+ fraction: (n, d) => new Fraction4(n, d ?? 1),
20850
21102
  number: Number,
20851
21103
  // Matrix helper factories
20852
21104
  createDenseMatrix: (data) => new MathJSDenseMatrix(data),
@@ -21372,7 +21624,7 @@ var dependencies10 = ["typed", "BigNumber", "complex", "Fraction"];
21372
21624
  var createSign = /* @__PURE__ */ factory(
21373
21625
  name10,
21374
21626
  dependencies10,
21375
- ({ typed: typed3, BigNumber: BigNumber9, complex: complex3, Fraction: Fraction5 }) => {
21627
+ ({ typed: typed3, BigNumber: BigNumber9, complex: complex3, Fraction: Fraction6 }) => {
21376
21628
  return typed3(name10, {
21377
21629
  number: signNumber,
21378
21630
  Complex: function(x) {
@@ -21385,7 +21637,7 @@ var createSign = /* @__PURE__ */ factory(
21385
21637
  return x > 0n ? 1n : x < 0n ? -1n : 0n;
21386
21638
  },
21387
21639
  Fraction: function(x) {
21388
- return x.n === 0n ? new Fraction5(0) : new Fraction5(x.s);
21640
+ return x.n === 0n ? new Fraction6(0) : new Fraction6(x.s);
21389
21641
  },
21390
21642
  // deep map collection, skip zeros since sign(0) = 0
21391
21643
  "Array | Matrix": typed3.referToSelf(
@@ -26861,7 +27113,7 @@ var createCbrt = /* @__PURE__ */ factory(
26861
27113
  matrix: matrix2,
26862
27114
  Complex: Complex13,
26863
27115
  BigNumber: BigNumber9,
26864
- Fraction: Fraction5
27116
+ Fraction: Fraction6
26865
27117
  }) => {
26866
27118
  return typed3(name106, {
26867
27119
  number: cbrtNumber,
@@ -26919,7 +27171,7 @@ var createCbrt = /* @__PURE__ */ factory(
26919
27171
  if (isBigNumber(value)) {
26920
27172
  return new BigNumber9(1).div(3);
26921
27173
  } else if (isFraction(value)) {
26922
- return new Fraction5(1, 3);
27174
+ return new Fraction6(1, 3);
26923
27175
  } else {
26924
27176
  return 1 / 3;
26925
27177
  }
@@ -27360,7 +27612,7 @@ var dependencies112 = ["typed", "config", "matrix", "BigNumber", "?Fraction"];
27360
27612
  var createXgcd = /* @__PURE__ */ factory(
27361
27613
  name112,
27362
27614
  dependencies112,
27363
- ({ typed: typed3, config: config2, matrix: matrix2, BigNumber: BigNumber9, Fraction: Fraction5 }) => {
27615
+ ({ typed: typed3, config: config2, matrix: matrix2, BigNumber: BigNumber9, Fraction: Fraction6 }) => {
27364
27616
  const typedSignatures = {
27365
27617
  "number, number": function(a, b) {
27366
27618
  const res = xgcdNumber(a, b);
@@ -27368,7 +27620,7 @@ var createXgcd = /* @__PURE__ */ factory(
27368
27620
  },
27369
27621
  "BigNumber, BigNumber": _xgcdBigNumber
27370
27622
  };
27371
- if (Fraction5) {
27623
+ if (Fraction6) {
27372
27624
  typedSignatures["Fraction, Fraction"] = _xgcdFraction;
27373
27625
  }
27374
27626
  return typed3(name112, typedSignatures);
@@ -27406,14 +27658,14 @@ var createXgcd = /* @__PURE__ */ factory(
27406
27658
  return config2.matrix === "Array" ? res : matrix2(res);
27407
27659
  }
27408
27660
  function _xgcdFraction(a, b) {
27409
- if (!Fraction5) {
27661
+ if (!Fraction6) {
27410
27662
  throw new Error("Fraction is not available");
27411
27663
  }
27412
27664
  let t;
27413
27665
  let q;
27414
27666
  let r;
27415
- const zero = new Fraction5(0);
27416
- const one = new Fraction5(1);
27667
+ const zero = new Fraction6(0);
27668
+ const one = new Fraction6(1);
27417
27669
  let x = zero;
27418
27670
  let lastx = one;
27419
27671
  let y = one;
@@ -28147,7 +28399,7 @@ var createBernoulli = /* @__PURE__ */ factory(
28147
28399
  isInteger: _isInteger,
28148
28400
  number: number2,
28149
28401
  BigNumber: BigNumber9,
28150
- Fraction: Fraction5
28402
+ Fraction: Fraction6
28151
28403
  }) => {
28152
28404
  const numberCache = [void 0];
28153
28405
  const fractionCache = [void 0];
@@ -28164,7 +28416,7 @@ var createBernoulli = /* @__PURE__ */ factory(
28164
28416
  ),
28165
28417
  "bigint | Fraction": (index) => _bernoulli(
28166
28418
  number2(index),
28167
- (n) => new Fraction5(n),
28419
+ (n) => new Fraction6(n),
28168
28420
  fractionCache,
28169
28421
  (a, b) => a.add(b),
28170
28422
  (a, b) => a.mul(b),
@@ -31009,7 +31261,7 @@ var createCompare = /* @__PURE__ */ factory(
31009
31261
  equalScalar: equalScalar3,
31010
31262
  matrix: matrix2,
31011
31263
  BigNumber: BigNumber9,
31012
- Fraction: Fraction5,
31264
+ Fraction: Fraction6,
31013
31265
  DenseMatrix: DenseMatrix7,
31014
31266
  concat: concat3
31015
31267
  }) => {
@@ -31037,7 +31289,7 @@ var createCompare = /* @__PURE__ */ factory(
31037
31289
  return x === y ? 0n : x > y ? 1n : -1n;
31038
31290
  },
31039
31291
  "Fraction, Fraction": function(x, y) {
31040
- return new Fraction5(x.compare(y));
31292
+ return new Fraction6(x.compare(y));
31041
31293
  },
31042
31294
  "Complex, Complex": function() {
31043
31295
  throw new TypeError("No ordering relation is defined for complex numbers");
@@ -31713,8 +31965,8 @@ var createInvmod = /* @__PURE__ */ factory(
31713
31965
  if (equal2(b, 0)) throw new Error("Divisor must be non zero");
31714
31966
  const res = xgcd2(a, b);
31715
31967
  const resVal = res.valueOf();
31716
- const [gcd2, invValue] = resVal;
31717
- if (!equal2(gcd2, 1)) return NaN;
31968
+ const [gcd3, invValue] = resVal;
31969
+ if (!equal2(gcd3, 1)) return NaN;
31718
31970
  let inv3 = mod3(invValue, b);
31719
31971
  if (smaller2(inv3, 0)) inv3 = add5(inv3, b);
31720
31972
  return inv3;
@@ -32055,9 +32307,9 @@ var createPinv = /* @__PURE__ */ factory(
32055
32307
  return M;
32056
32308
  }
32057
32309
  function _rankFact(mat, rows, cols) {
32058
- const rref = _rref(mat, rows, cols);
32059
- const C = mat.map((row2) => row2.filter((_, j) => j < rows && !_isZero(dot9(rref[j], rref[j]))));
32060
- const F = rref.filter((_, i) => !_isZero(dot9(rref[i], rref[i])));
32310
+ const rref2 = _rref(mat, rows, cols);
32311
+ const C = mat.map((row2) => row2.filter((_, j) => j < rows && !_isZero(dot9(rref2[j], rref2[j]))));
32312
+ const F = rref2.filter((_, i) => !_isZero(dot9(rref2[i], rref2[i])));
32061
32313
  return { C, F };
32062
32314
  }
32063
32315
  function _isZero(x) {
@@ -35173,33 +35425,33 @@ var dependencies203 = ["typed", "Unit"];
35173
35425
  var createCreateUnit = /* @__PURE__ */ factory(
35174
35426
  name203,
35175
35427
  dependencies203,
35176
- ({ typed: typed3, Unit: Unit2 }) => {
35428
+ ({ typed: typed3, Unit: Unit3 }) => {
35177
35429
  return typed3(name203, {
35178
35430
  // General function signature. First parameter is an object where each property is the definition of a new unit. The object keys are the unit names and the values are the definitions. The values can be objects, strings, or Units. If a property is an empty object or an empty string, a new base unit is created. The second parameter is the options.
35179
35431
  "Object, Object": function(obj, options) {
35180
- return Unit2.createUnit(obj, options);
35432
+ return Unit3.createUnit(obj, options);
35181
35433
  },
35182
35434
  // Same as above but without the options.
35183
35435
  Object: function(obj) {
35184
- return Unit2.createUnit(obj, {});
35436
+ return Unit3.createUnit(obj, {});
35185
35437
  },
35186
35438
  // Shortcut method for creating one unit.
35187
35439
  "string, Unit | string | Object, Object": function(name254, def, options) {
35188
35440
  const obj = {};
35189
35441
  obj[name254] = def;
35190
- return Unit2.createUnit(obj, options);
35442
+ return Unit3.createUnit(obj, options);
35191
35443
  },
35192
35444
  // Same as above but without the options.
35193
35445
  "string, Unit | string | Object": function(name254, def) {
35194
35446
  const obj = {};
35195
35447
  obj[name254] = def;
35196
- return Unit2.createUnit(obj, {});
35448
+ return Unit3.createUnit(obj, {});
35197
35449
  },
35198
35450
  // Without a definition, creates a base unit.
35199
35451
  string: function(name254) {
35200
35452
  const obj = {};
35201
35453
  obj[name254] = {};
35202
- return Unit2.createUnit(obj, {});
35454
+ return Unit3.createUnit(obj, {});
35203
35455
  }
35204
35456
  });
35205
35457
  }
@@ -35211,22 +35463,22 @@ var dependencies204 = ["typed", "Unit"];
35211
35463
  var createUnitFunction = /* @__PURE__ */ factory(
35212
35464
  name204,
35213
35465
  dependencies204,
35214
- ({ typed: typed3, Unit: Unit2 }) => {
35466
+ ({ typed: typed3, Unit: Unit3 }) => {
35215
35467
  return typed3(name204, {
35216
35468
  Unit: function(x) {
35217
35469
  return x.clone();
35218
35470
  },
35219
35471
  string: function(x) {
35220
- if (Unit2.isValuelessUnit(x)) {
35221
- return new Unit2(null, x);
35472
+ if (Unit3.isValuelessUnit(x)) {
35473
+ return new Unit3(null, x);
35222
35474
  }
35223
- return Unit2.parse(x, { allowNoUnits: true });
35475
+ return Unit3.parse(x, { allowNoUnits: true });
35224
35476
  },
35225
35477
  "number | BigNumber | Fraction | Complex, string | Unit": function(value, unit2) {
35226
- return new Unit2(value, unit2);
35478
+ return new Unit3(value, unit2);
35227
35479
  },
35228
35480
  "number | BigNumber | Fraction": function(value) {
35229
- return new Unit2(value);
35481
+ return new Unit3(value);
35230
35482
  },
35231
35483
  "Array | Matrix": typed3.referToSelf(
35232
35484
  (self) => (x) => deepMap(x, self)
@@ -42614,9 +42866,9 @@ function unitFactory(name254, valueStr, unitStr) {
42614
42866
  return factory(
42615
42867
  name254,
42616
42868
  dependencies254,
42617
- ({ config: config2, Unit: Unit2, BigNumber: BigNumber9 }) => {
42869
+ ({ config: config2, Unit: Unit3, BigNumber: BigNumber9 }) => {
42618
42870
  const value = config2.number === "BigNumber" ? new BigNumber9(valueStr) : parseFloat(valueStr);
42619
- const unit2 = new Unit2(value, unitStr);
42871
+ const unit2 = new Unit3(value, unitStr);
42620
42872
  unit2.fixPrefix = true;
42621
42873
  return unit2;
42622
42874
  }
@@ -43559,18 +43811,81 @@ var mathScope = {
43559
43811
  }
43560
43812
  }
43561
43813
  var parse = factoryScope.parse;
43562
- var evaluate = createEvaluate(parse, mathScope);
43563
- function compileExpr(expr) {
43564
- return _compileExpression(parse, mathScope, expr);
43814
+ var _evaluate = createEvaluate(parse, mathScope);
43815
+ var ELEMENTARY_CHARGE_COULOMB = 1602176634e-28;
43816
+ function elementaryChargeUnit() {
43817
+ return new Unit2(ELEMENTARY_CHARGE_COULOMB, "C");
43818
+ }
43819
+ function isMapScope(scope) {
43820
+ const candidate = scope;
43821
+ return typeof candidate.has === "function" && typeof candidate.get === "function" && typeof candidate.set === "function";
43822
+ }
43823
+ function physicsBinding(charge) {
43824
+ return charge === "scalar" ? ELEMENTARY_CHARGE_COULOMB : elementaryChargeUnit();
43825
+ }
43826
+ function physicsScope(scope, options) {
43827
+ const charge = physicsBinding(options?.charge);
43828
+ if (scope == null) return { e: charge };
43829
+ if (isMapScope(scope)) {
43830
+ if (scope.has("e")) return scope;
43831
+ return {
43832
+ has(key2) {
43833
+ return key2 === "e" || scope.has(key2);
43834
+ },
43835
+ get(key2) {
43836
+ return key2 === "e" ? charge : scope.get(key2);
43837
+ },
43838
+ set(key2, value) {
43839
+ scope.set(key2, value);
43840
+ }
43841
+ };
43842
+ }
43843
+ if (Object.prototype.hasOwnProperty.call(scope, "e")) return scope;
43844
+ return { e: charge, ...scope };
43565
43845
  }
43566
- function parser() {
43846
+ function assertPhysicsOptions(options) {
43847
+ if (options?.charge !== void 0 && options.charge !== "unit" && options.charge !== "scalar") {
43848
+ throw new TypeError('charge must be "unit" or "scalar"');
43849
+ }
43850
+ if (options?.physics !== true) {
43851
+ if (options?.charge !== void 0) {
43852
+ throw new TypeError("charge requires { physics: true }");
43853
+ }
43854
+ return void 0;
43855
+ }
43856
+ return { charge: options.charge ?? "unit" };
43857
+ }
43858
+ function evaluate(exprOrExprs, scope, options) {
43859
+ const physics = assertPhysicsOptions(options);
43860
+ const bound = physics === void 0 ? scope : physicsScope(scope, physics);
43861
+ const inner = options?.unsafe === true ? { unsafe: true } : void 0;
43862
+ if (Array.isArray(exprOrExprs)) return _evaluate(exprOrExprs, bound, inner);
43863
+ return _evaluate(exprOrExprs, bound, inner);
43864
+ }
43865
+ function compileExpr(expr, options) {
43866
+ const compiled = _compileExpression(
43867
+ parse,
43868
+ mathScope,
43869
+ expr,
43870
+ options?.unsafe === true ? { unsafe: true } : void 0
43871
+ );
43872
+ const physics = assertPhysicsOptions(options);
43873
+ if (physics === void 0) return compiled;
43874
+ return {
43875
+ evaluate(scope) {
43876
+ return compiled.evaluate(physicsScope(scope, physics));
43877
+ }
43878
+ };
43879
+ }
43880
+ function parser(options) {
43567
43881
  const scope = {};
43882
+ const callOptions = options?.physics === true || options?.unsafe === true || options?.charge !== void 0 ? { physics: options?.physics, unsafe: options?.unsafe, charge: options?.charge } : void 0;
43568
43883
  return {
43569
43884
  /** The live scope object — assignments during `evaluate` land here. */
43570
43885
  scope,
43571
43886
  /** Evaluate an expression against the retained scope. */
43572
43887
  evaluate(expr) {
43573
- return evaluate(expr, scope);
43888
+ return evaluate(expr, scope, callOptions);
43574
43889
  },
43575
43890
  /** Read a retained variable. */
43576
43891
  get(name254) {
@@ -43605,7 +43920,7 @@ function reviver(_key, value) {
43605
43920
  case "Complex":
43606
43921
  return Complex9.fromJSON(tagged);
43607
43922
  case "Fraction":
43608
- return Fraction4.fromJSON(tagged);
43923
+ return Fraction5.fromJSON(tagged);
43609
43924
  case "number":
43610
43925
  if (tagged.value === "Infinity") return Infinity;
43611
43926
  if (tagged.value === "-Infinity") return -Infinity;
@@ -45144,6 +45459,147 @@ function config(options) {
45144
45459
  return { ...shared };
45145
45460
  }
45146
45461
 
45462
+ // src/numeric/propagate-uncertainty.ts
45463
+ function asFiniteNumber(label, value) {
45464
+ if (typeof value !== "number" || !Number.isFinite(value)) {
45465
+ throw new TypeError(`${label} is not a finite number`);
45466
+ }
45467
+ return value;
45468
+ }
45469
+ function partialDerivative2(expr, name254, values) {
45470
+ const node = derivative2(expr, name254);
45471
+ return asFiniteNumber(`partial of "${expr}" with respect to "${name254}"`, node.evaluate(values));
45472
+ }
45473
+ function centralPartial(f, name254, point, relativeStep) {
45474
+ const x = point[name254];
45475
+ const h = relativeStep * Math.max(Math.abs(x), 1e-30);
45476
+ const plus = asFiniteNumber(
45477
+ `partial of function with respect to "${name254}"`,
45478
+ f({ ...point, [name254]: x + h })
45479
+ );
45480
+ const minus = asFiniteNumber(
45481
+ `partial of function with respect to "${name254}"`,
45482
+ f({ ...point, [name254]: x - h })
45483
+ );
45484
+ return (plus - minus) / (2 * h);
45485
+ }
45486
+ function curvatureRatio(evaluateAt, name254, point, partial, offset, value) {
45487
+ if (!Number.isFinite(offset)) {
45488
+ throw new TypeError(`curvatureOffsets.${name254} is not a finite number`);
45489
+ }
45490
+ const linear = partial * offset;
45491
+ if (linear === 0) return null;
45492
+ let up;
45493
+ let down;
45494
+ try {
45495
+ up = evaluateAt({ ...point, [name254]: point[name254] + offset });
45496
+ down = evaluateAt({ ...point, [name254]: point[name254] - offset });
45497
+ } catch {
45498
+ return null;
45499
+ }
45500
+ if (!Number.isFinite(up) || !Number.isFinite(down)) return null;
45501
+ return Math.abs(up + down - 2 * value) / 2 / Math.abs(linear);
45502
+ }
45503
+ function propagateUncertainty(expr, values, sigmas, options) {
45504
+ const isFunction = typeof expr === "function";
45505
+ if (!isFunction && (typeof expr !== "string" || expr.length === 0)) {
45506
+ throw new TypeError("expr must be a non-empty string or a function");
45507
+ }
45508
+ if (options?.relativeStep !== void 0 && !isFunction) {
45509
+ throw new TypeError(
45510
+ "relativeStep applies only to a function; a string uses the symbolic derivative"
45511
+ );
45512
+ }
45513
+ const relativeStep = options?.relativeStep ?? 1e-6;
45514
+ if (isFunction && (!(relativeStep > 0) || !Number.isFinite(relativeStep))) {
45515
+ throw new TypeError("relativeStep must be a positive finite number");
45516
+ }
45517
+ if (values === null || typeof values !== "object") {
45518
+ throw new TypeError("values must be an object of finite numbers");
45519
+ }
45520
+ const names = Object.keys(values);
45521
+ const point = {};
45522
+ for (const name254 of names) {
45523
+ point[name254] = asFiniteNumber(`values.${name254}`, values[name254]);
45524
+ }
45525
+ const covariance = options?.covariance;
45526
+ if (covariance !== void 0 && sigmas !== void 0) {
45527
+ throw new TypeError("pass either sigmas or covariance, not both");
45528
+ }
45529
+ if (sigmas !== void 0 && (sigmas === null || typeof sigmas !== "object")) {
45530
+ throw new TypeError("sigmas must be an object of finite numbers");
45531
+ }
45532
+ const partials = {};
45533
+ const gradient2 = [];
45534
+ const call = isFunction ? expr : void 0;
45535
+ for (const name254 of names) {
45536
+ const d = call ? centralPartial(call, name254, point, relativeStep) : partialDerivative2(expr, name254, point);
45537
+ partials[name254] = d;
45538
+ gradient2.push(d);
45539
+ }
45540
+ const value = asFiniteNumber("value", call ? call(point) : evaluate(expr, point));
45541
+ let variance2;
45542
+ if (covariance !== void 0) {
45543
+ const n = names.length;
45544
+ if (covariance.length !== n) {
45545
+ throw new TypeError(`covariance must be ${n}\xD7${n} to match values`);
45546
+ }
45547
+ variance2 = 0;
45548
+ for (let i = 0; i < n; i++) {
45549
+ const row2 = covariance[i];
45550
+ if (row2 === void 0 || row2.length !== n) {
45551
+ throw new TypeError(`covariance must be ${n}\xD7${n} to match values`);
45552
+ }
45553
+ let acc = 0;
45554
+ for (let j = 0; j < n; j++) {
45555
+ const sij = asFiniteNumber(`covariance[${i}][${j}]`, row2[j]);
45556
+ acc += sij * gradient2[j];
45557
+ }
45558
+ variance2 += gradient2[i] * acc;
45559
+ }
45560
+ } else {
45561
+ variance2 = 0;
45562
+ if (sigmas !== void 0) {
45563
+ for (const key2 of Object.keys(sigmas)) {
45564
+ if (!Object.prototype.hasOwnProperty.call(point, key2)) {
45565
+ throw new TypeError(`sigmas.${key2} has no matching value`);
45566
+ }
45567
+ }
45568
+ for (let i = 0; i < names.length; i++) {
45569
+ const name254 = names[i];
45570
+ if (!Object.prototype.hasOwnProperty.call(sigmas, name254)) continue;
45571
+ const sigma2 = asFiniteNumber(`sigmas.${name254}`, sigmas[name254]);
45572
+ if (sigma2 < 0) {
45573
+ throw new RangeError(`sigmas.${name254} must be >= 0`);
45574
+ }
45575
+ variance2 += (gradient2[i] * sigma2) ** 2;
45576
+ }
45577
+ }
45578
+ }
45579
+ if (variance2 < 0) {
45580
+ if (variance2 > -1e-12) variance2 = 0;
45581
+ else throw new RangeError("covariance produced a negative variance");
45582
+ }
45583
+ const sigma = Math.sqrt(variance2);
45584
+ const offsets = options?.curvatureOffsets;
45585
+ if (offsets === void 0) return { value, sigma, partials };
45586
+ if (offsets === null || typeof offsets !== "object") {
45587
+ throw new TypeError("curvatureOffsets must be an object of finite numbers");
45588
+ }
45589
+ const evaluateAt = (at) => {
45590
+ if (call) return call(at);
45591
+ return asFiniteNumber("curvature probe", evaluate(expr, at));
45592
+ };
45593
+ const curvature = {};
45594
+ for (const name254 of Object.keys(offsets)) {
45595
+ if (!Object.prototype.hasOwnProperty.call(point, name254)) {
45596
+ throw new TypeError(`curvatureOffsets.${name254} has no matching value`);
45597
+ }
45598
+ curvature[name254] = curvatureRatio(evaluateAt, name254, point, partials[name254], offsets[name254], value);
45599
+ }
45600
+ return { value, sigma, partials, curvature };
45601
+ }
45602
+
45147
45603
  // src/help.ts
45148
45604
  import { createHelpClass, embeddedDocs } from "@danielsimonjr/mathts-expression";
45149
45605
  var savedConfig = {};
@@ -49457,8 +49913,8 @@ function intToRatPoly(p) {
49457
49913
  }
49458
49914
  function ratPolyToInt(p) {
49459
49915
  if (p.length === 0) return { poly: [], den: 1n };
49460
- let lcm2 = 1n;
49461
- const gcd2 = (a, b) => {
49916
+ let lcm3 = 1n;
49917
+ const gcd3 = (a, b) => {
49462
49918
  let x = a < 0n ? -a : a;
49463
49919
  let y = b < 0n ? -b : b;
49464
49920
  while (y !== 0n) {
@@ -49469,10 +49925,10 @@ function ratPolyToInt(p) {
49469
49925
  return x;
49470
49926
  };
49471
49927
  for (const r of p) {
49472
- const g = gcd2(lcm2, r.den);
49473
- lcm2 = lcm2 / g * r.den;
49928
+ const g = gcd3(lcm3, r.den);
49929
+ lcm3 = lcm3 / g * r.den;
49474
49930
  }
49475
- return { poly: trim(p.map((r) => r.num * lcm2 / r.den)), den: lcm2 };
49931
+ return { poly: trim(p.map((r) => r.num * lcm3 / r.den)), den: lcm3 };
49476
49932
  }
49477
49933
  function rpAdd(a, b) {
49478
49934
  const n = Math.max(a.length, b.length);
@@ -55276,6 +55732,10 @@ export {
55276
55732
  DIST_WORKER_THRESHOLD,
55277
55733
  EARTH_RADIUS_KM,
55278
55734
  GAUSS_WORKER_THRESHOLD,
55735
+ GL4ConvergenceError,
55736
+ GL4_A,
55737
+ GL4_B,
55738
+ GL4_C,
55279
55739
  GPU_ELEMENTWISE_OPS,
55280
55740
  GPU_MIN_ELEMENTS2 as GPU_MIN_ELEMENTS,
55281
55741
  GPU_REDUCE_OPS,
@@ -55698,6 +56158,7 @@ export {
55698
56158
  gammainc,
55699
56159
  gammaincp,
55700
56160
  gasConstant,
56161
+ gaussLegendre4,
55701
56162
  gaussQuad,
55702
56163
  gaussianKDE,
55703
56164
  gaussianProcessRegression,
@@ -56040,6 +56501,7 @@ export {
56040
56501
  permutationTest,
56041
56502
  permutations,
56042
56503
  permutationsGen,
56504
+ physicsScope,
56043
56505
  pickRandom,
56044
56506
  piecewise,
56045
56507
  pinv,
@@ -56079,6 +56541,7 @@ export {
56079
56541
  procrustes,
56080
56542
  prod,
56081
56543
  projectVector,
56544
+ propagateUncertainty,
56082
56545
  proportionCI,
56083
56546
  proportionZTest,
56084
56547
  protonMass,
@@ -56107,6 +56570,7 @@ export {
56107
56570
  range,
56108
56571
  rank,
56109
56572
  rankdata,
56573
+ rationalNullspace,
56110
56574
  rationalize,
56111
56575
  rayPlaneIntersect,
56112
56576
  rayTriangleIntersect,