@danielsimonjr/mathts-functions 0.35.0 → 0.36.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
@@ -9930,7 +9930,143 @@ function substitute(expr, vars) {
9930
9930
  }
9931
9931
  return result;
9932
9932
  }
9933
+ function isNearInt(c) {
9934
+ return Math.abs(c - Math.round(c)) < 1e-7;
9935
+ }
9936
+ function polyToDense(p) {
9937
+ let maxPow = 0;
9938
+ for (const t of p) maxPow = Math.max(maxPow, t.powers[0] ?? 0);
9939
+ const dense = new Array(maxPow + 1).fill(0);
9940
+ for (const t of p) dense[t.powers[0]] += t.coeff;
9941
+ return trimPoly(dense);
9942
+ }
9943
+ function denseToPoly(coeffs) {
9944
+ return coeffs.map((coeff, i) => ({ coeff, powers: [i] })).filter((t) => Math.abs(t.coeff) > 1e-10);
9945
+ }
9946
+ function divisorsOf(n) {
9947
+ const m = Math.abs(Math.round(n)) || 1;
9948
+ const divs = [];
9949
+ for (let i = 1; i <= m; i++) if (m % i === 0) divs.push(i);
9950
+ return divs;
9951
+ }
9952
+ function ratReduce(r) {
9953
+ let { n, d } = r;
9954
+ if (d < 0) {
9955
+ n = -n;
9956
+ d = -d;
9957
+ }
9958
+ const g = gcdNum(Math.abs(n), Math.abs(d)) || 1;
9959
+ return { n: n / g, d: d / g };
9960
+ }
9961
+ function ratPolyval(coeffs, r) {
9962
+ let acc = { n: 0, d: 1 };
9963
+ for (let i = coeffs.length - 1; i >= 0; i--) {
9964
+ acc = ratReduce({
9965
+ n: acc.n * r.n + coeffs[i] * acc.d * r.d,
9966
+ d: acc.d * r.d
9967
+ });
9968
+ }
9969
+ return acc;
9970
+ }
9971
+ function findRationalLinearFactors(poly) {
9972
+ let cur = trimPoly(poly.map((c) => Math.round(c)));
9973
+ const roots = [];
9974
+ let repeated = false;
9975
+ const hasRoot = (r) => roots.some((x) => x.n === r.n && x.d === r.d);
9976
+ for (; ; ) {
9977
+ const deg = cur.length - 1;
9978
+ if (deg < 1) break;
9979
+ if (cur[0] === 0) {
9980
+ const r = { n: 0, d: 1 };
9981
+ if (hasRoot(r)) repeated = true;
9982
+ roots.push(r);
9983
+ cur = trimPoly(cur.slice(1));
9984
+ continue;
9985
+ }
9986
+ const ps = divisorsOf(cur[0]);
9987
+ const qs = divisorsOf(cur[cur.length - 1]);
9988
+ let found = null;
9989
+ let quotient = null;
9990
+ outer: for (const q of qs) {
9991
+ for (const pAbs of ps) {
9992
+ for (const sign3 of [1, -1]) {
9993
+ const candidate = ratReduce({ n: sign3 * pAbs, d: q });
9994
+ const divisor = trimPoly([-candidate.n, candidate.d]);
9995
+ const rem = polynomialRemainder(cur, divisor);
9996
+ if (rem.length === 1 && Math.abs(rem[0]) < 1e-6) {
9997
+ found = candidate;
9998
+ quotient = polynomialQuotient(cur, divisor).map((c) => Math.round(c * 1e6) / 1e6);
9999
+ break outer;
10000
+ }
10001
+ }
10002
+ }
10003
+ }
10004
+ if (!found || !quotient) break;
10005
+ if (hasRoot(found)) repeated = true;
10006
+ roots.push(found);
10007
+ cur = trimPoly(quotient);
10008
+ }
10009
+ return { roots, remainder: cur, repeated };
10010
+ }
10011
+ function formatLinearFactor(r, varName) {
10012
+ const base = r.d === 1 ? varName : `${r.d}*${varName}`;
10013
+ if (r.n === 0) return `(${base})`;
10014
+ return r.n > 0 ? `(${base} - ${r.n})` : `(${base} + ${-r.n})`;
10015
+ }
10016
+ function formatPartialTerm(a, root2, varName) {
10017
+ const rootExpr = root2.d === 1 ? root2.n === 0 ? varName : root2.n > 0 ? `${varName} - ${root2.n}` : `${varName} + ${-root2.n}` : `${varName} - ${root2.n}/${root2.d}`;
10018
+ const denomExpr = a.d === 1 ? `(${rootExpr})` : `(${a.d}*(${rootExpr}))`;
10019
+ if (a.n === 1) return `1/${denomExpr}`;
10020
+ if (a.n === -1) return `-1/${denomExpr}`;
10021
+ return `${a.n}/${denomExpr}`;
10022
+ }
10023
+ function splitTopLevelSum(expr) {
10024
+ const s = expr.replace(/\s+/g, "");
10025
+ const terms = [];
10026
+ let depth = 0;
10027
+ let start = 0;
10028
+ for (let i = 0; i < s.length; i++) {
10029
+ const c = s[i];
10030
+ if (c === "(") depth++;
10031
+ else if (c === ")") depth--;
10032
+ else if (depth === 0 && (c === "+" || c === "-") && i > start) {
10033
+ const prev = s[i - 1];
10034
+ if (!/[*/^(+-]/.test(prev)) {
10035
+ terms.push(s.slice(start, i));
10036
+ start = i;
10037
+ }
10038
+ }
10039
+ }
10040
+ terms.push(s.slice(start));
10041
+ return terms.filter((t) => t !== "");
10042
+ }
10043
+ function splitProductChain(term) {
10044
+ const nums = [];
10045
+ const dens = [];
10046
+ let depth = 0;
10047
+ let start = 0;
10048
+ let op = "*";
10049
+ for (let i = 0; i <= term.length; i++) {
10050
+ const c = term[i];
10051
+ if (c === "(") depth++;
10052
+ else if (c === ")") depth--;
10053
+ if (depth === 0 && (c === "*" || c === "/" || i === term.length)) {
10054
+ const piece = term.slice(start, i).trim();
10055
+ if (piece !== "") (op === "*" ? nums : dens).push(piece);
10056
+ if (i < term.length) op = c;
10057
+ start = i + 1;
10058
+ }
10059
+ }
10060
+ return { nums, dens };
10061
+ }
9933
10062
  function expand(expr) {
10063
+ const univariateVars = variables(expr);
10064
+ if (univariateVars.length === 1) {
10065
+ try {
10066
+ return polyToString(polyFromExpression(expr, univariateVars), univariateVars);
10067
+ } catch {
10068
+ }
10069
+ }
9934
10070
  let result = expr;
9935
10071
  result = result.replace(/\(([^()]+)\)\^2/g, "($1)*($1)");
9936
10072
  const splitTerms = (factor2) => factor2.replace(/\s*-\s*/g, " + -").split(/\s*\+\s*/).map((t) => t.trim()).filter((t) => t !== "");
@@ -9951,6 +10087,27 @@ function expand(expr) {
9951
10087
  return result;
9952
10088
  }
9953
10089
  function factor(expr) {
10090
+ const univariateVars = variables(expr);
10091
+ if (univariateVars.length === 1) {
10092
+ try {
10093
+ const v = univariateVars[0];
10094
+ const dense = polyToDense(polyFromExpression(expr, [v]));
10095
+ if (degree(dense) >= 2 && dense.every(isNearInt)) {
10096
+ const intDense = dense.map((c) => Math.round(c));
10097
+ const { roots, remainder } = findRationalLinearFactors(intDense);
10098
+ if (roots.length > 0) {
10099
+ const factors = roots.map((r) => formatLinearFactor(r, v));
10100
+ if (degree(remainder) >= 1) {
10101
+ factors.push(`(${polyToString(denseToPoly(remainder), [v])})`);
10102
+ } else if (remainder[0] !== 1) {
10103
+ factors.unshift(String(remainder[0]));
10104
+ }
10105
+ return factors.join("*");
10106
+ }
10107
+ }
10108
+ } catch {
10109
+ }
10110
+ }
9954
10111
  const normalized = expr.replace(/\s*-\s*/g, " + -");
9955
10112
  const terms = normalized.split(/\s*\+\s*/).filter((t) => t.trim() !== "");
9956
10113
  if (terms.length < 2) return expr;
@@ -10053,6 +10210,25 @@ function cancel(expr) {
10053
10210
  return expr;
10054
10211
  }
10055
10212
  function together(expr) {
10213
+ const univariateVars = variables(expr);
10214
+ if (univariateVars.length === 1) {
10215
+ try {
10216
+ const v = univariateVars[0];
10217
+ const terms = splitTopLevelSum(expr).map(splitProductChain);
10218
+ const denParts = terms.flatMap((t) => t.dens);
10219
+ if (denParts.length > 0) {
10220
+ const denominatorStr = denParts.map((d) => `(${d})`).join("*");
10221
+ const numeratorExpr = terms.map((t, i) => {
10222
+ const otherDens = terms.flatMap((t2, j) => j === i ? [] : t2.dens);
10223
+ const parts = [...t.nums, ...otherDens].map((p) => `(${p})`);
10224
+ return parts.length ? parts.join("*") : "1";
10225
+ }).join(" + ");
10226
+ const numeratorStr = polyToString(polyFromExpression(numeratorExpr, [v]), [v]);
10227
+ return `(${numeratorStr})/(${denominatorStr})`;
10228
+ }
10229
+ } catch {
10230
+ }
10231
+ }
10056
10232
  const match = expr.match(/^\s*(-?\d+)\s*\/\s*(-?\d+)\s*\+\s*(-?\d+)\s*\/\s*(-?\d+)\s*$/);
10057
10233
  if (match) {
10058
10234
  const a = parseInt(match[1], 10);
@@ -10067,6 +10243,52 @@ function together(expr) {
10067
10243
  return expr;
10068
10244
  }
10069
10245
  function apart(expr) {
10246
+ const univariateVars = variables(expr);
10247
+ if (univariateVars.length === 1) {
10248
+ try {
10249
+ const v = univariateVars[0];
10250
+ const topTerms = splitTopLevelSum(expr);
10251
+ if (topTerms.length === 1) {
10252
+ const { nums, dens } = splitProductChain(topTerms[0]);
10253
+ if (dens.length > 0) {
10254
+ const numStr = nums.length ? nums.join("*") : "1";
10255
+ const denStr = dens.join("*");
10256
+ const N = polyToDense(polyFromExpression(numStr, [v]));
10257
+ const D = polyToDense(polyFromExpression(denStr, [v]));
10258
+ if (degree(D) >= 1 && N.every(isNearInt) && D.every(isNearInt)) {
10259
+ let Nint = N.map((c) => Math.round(c));
10260
+ const Dint = D.map((c) => Math.round(c));
10261
+ let quotientPrefix = "";
10262
+ if (degree(Nint) >= degree(Dint)) {
10263
+ const q = polynomialQuotient(Nint, Dint);
10264
+ const r = polynomialRemainder(Nint, Dint);
10265
+ if (degree(r) === 0 && r[0] === 0) {
10266
+ return polyToString(denseToPoly(q), [v]);
10267
+ }
10268
+ quotientPrefix = `${polyToString(denseToPoly(q), [v])} + `;
10269
+ Nint = r.map((c) => Math.round(c));
10270
+ }
10271
+ const { roots, remainder, repeated } = findRationalLinearFactors(Dint);
10272
+ if (!repeated && degree(remainder) === 0 && roots.length === degree(Dint)) {
10273
+ const Dprime = polyder(Dint);
10274
+ const termStrs = roots.map((root2) => {
10275
+ const nAtRoot = ratPolyval(Nint, root2);
10276
+ const dAtRoot = ratPolyval(Dprime, root2);
10277
+ if (dAtRoot.n === 0) throw new Error("apart: derivative vanished at root");
10278
+ const a = ratReduce({
10279
+ n: nAtRoot.n * dAtRoot.d,
10280
+ d: nAtRoot.d * dAtRoot.n
10281
+ });
10282
+ return formatPartialTerm(a, root2, v);
10283
+ });
10284
+ return (quotientPrefix + termStrs.join(" + ")).replace(/\+ -/g, "- ");
10285
+ }
10286
+ }
10287
+ }
10288
+ }
10289
+ } catch {
10290
+ }
10291
+ }
10070
10292
  const match = expr.match(/^\s*(-?\d+)\s*\/\s*(-?\d+)\s*$/);
10071
10293
  if (match) {
10072
10294
  const num2 = parseInt(match[1], 10);
@@ -12731,15 +12953,15 @@ function solveODESystem(f, y0, tspan, opts) {
12731
12953
  function stiffODESolver(f, y0, tspan) {
12732
12954
  return rosenbrockSolve((t, y) => f(t, y), tspan, y0, { tol: 1e-7 });
12733
12955
  }
12734
- function solveBVP(f, bc, mesh) {
12735
- const n = 2;
12956
+ function solveBVP(f, bc, mesh, y0Guess = [0, 0]) {
12957
+ const n = y0Guess.length;
12736
12958
  const tspan = [mesh[0], mesh[mesh.length - 1]];
12737
12959
  function shoot(guess) {
12738
12960
  const sol = solveODESystem(f, guess, tspan, { dt: (tspan[1] - tspan[0]) / 50 });
12739
12961
  const yf = sol.y[sol.y.length - 1];
12740
12962
  return bc(guess, yf);
12741
12963
  }
12742
- const s = new Array(n).fill(0);
12964
+ const s = y0Guess.slice();
12743
12965
  const delta = 1e-7;
12744
12966
  for (let iter = 0; iter < 50; iter++) {
12745
12967
  const r = shoot(s);
@@ -16365,7 +16587,7 @@ function _median(arr10) {
16365
16587
  const m = n >> 1;
16366
16588
  return n % 2 === 1 ? s[m] : (s[m - 1] + s[m]) / 2;
16367
16589
  }
16368
- function leveneTest(groups, center2 = "median") {
16590
+ function leveneTest(groups, center3 = "median") {
16369
16591
  const k = groups.length;
16370
16592
  if (k < 2) throw new Error("leveneTest: need at least 2 groups");
16371
16593
  let N = 0;
@@ -16374,7 +16596,7 @@ function leveneTest(groups, center2 = "median") {
16374
16596
  N += g.length;
16375
16597
  }
16376
16598
  const z = groups.map((g) => {
16377
- const c = center2 === "median" ? _median(g) : _mean(g);
16599
+ const c = center3 === "median" ? _median(g) : _mean(g);
16378
16600
  return g.map((x) => Math.abs(x - c));
16379
16601
  });
16380
16602
  const allZ = [];
@@ -23175,7 +23397,7 @@ var dependencies92 = ["typed", "matrix", "flatten", "size"];
23175
23397
  var createMatrixFromColumns = /* @__PURE__ */ factory(
23176
23398
  name92,
23177
23399
  dependencies92,
23178
- ({ typed: typed3, matrix: matrix2, flatten: flatten4, size: size2 }) => {
23400
+ ({ typed: typed3, matrix: matrix2, flatten: flatten5, size: size2 }) => {
23179
23401
  return typed3(name92, {
23180
23402
  // Single variadic handler for arrays, matrices, and mixed types
23181
23403
  "...": function(arr10) {
@@ -23212,7 +23434,7 @@ var createMatrixFromColumns = /* @__PURE__ */ factory(
23212
23434
  "The vectors had different length: " + (N | 0) + " \u2260 " + (colLength | 0)
23213
23435
  );
23214
23436
  }
23215
- const f = flatten4(col);
23437
+ const f = flatten5(col);
23216
23438
  for (let i = 0; i < N; i++) {
23217
23439
  result[i].push(f[i]);
23218
23440
  }
@@ -23244,7 +23466,7 @@ var dependencies93 = ["typed", "matrix", "flatten", "size"];
23244
23466
  var createMatrixFromRows = /* @__PURE__ */ factory(
23245
23467
  name93,
23246
23468
  dependencies93,
23247
- ({ typed: typed3, matrix: matrix2, flatten: flatten4, size: size2 }) => {
23469
+ ({ typed: typed3, matrix: matrix2, flatten: flatten5, size: size2 }) => {
23248
23470
  return typed3(name93, {
23249
23471
  // Single variadic handler for arrays, matrices, and mixed types
23250
23472
  "...": function(arr10) {
@@ -23278,7 +23500,7 @@ var createMatrixFromRows = /* @__PURE__ */ factory(
23278
23500
  "The vectors had different length: " + (N | 0) + " \u2260 " + (rowLength | 0)
23279
23501
  );
23280
23502
  }
23281
- result.push(flatten4(row2));
23503
+ result.push(flatten5(row2));
23282
23504
  }
23283
23505
  return result;
23284
23506
  }
@@ -32299,13 +32521,13 @@ var createUtil = /* @__PURE__ */ factory(
32299
32521
  }
32300
32522
  return merged;
32301
32523
  }
32302
- function flatten4(node, context = defaultContext) {
32524
+ function flatten5(node, context = defaultContext) {
32303
32525
  if (!node.args || node.args.length === 0) {
32304
32526
  return;
32305
32527
  }
32306
32528
  node.args = allChildren(node, context);
32307
32529
  for (let i = 0; i < node.args.length; i++) {
32308
- flatten4(node.args[i], context);
32530
+ flatten5(node.args[i], context);
32309
32531
  }
32310
32532
  }
32311
32533
  function allChildren(node, context) {
@@ -32385,7 +32607,7 @@ var createUtil = /* @__PURE__ */ factory(
32385
32607
  isCommutative,
32386
32608
  isAssociative,
32387
32609
  mergeContext,
32388
- flatten: flatten4,
32610
+ flatten: flatten5,
32389
32611
  allChildren,
32390
32612
  unflattenr,
32391
32613
  unflattenl,
@@ -35420,7 +35642,7 @@ var createIntersect = /* @__PURE__ */ factory(
35420
35642
  subtract: subtract3,
35421
35643
  smaller: smaller2,
35422
35644
  equalScalar: equalScalar3,
35423
- flatten: flatten4,
35645
+ flatten: flatten5,
35424
35646
  isZero: isZero2,
35425
35647
  isNumeric: isNumeric2
35426
35648
  }) => {
@@ -35512,7 +35734,7 @@ var createIntersect = /* @__PURE__ */ factory(
35512
35734
  if (arr10.length === 1 && Array.isArray(arr10[0])) return arr10[0];
35513
35735
  if (arr10.length > 1 && Array.isArray(arr10[0])) {
35514
35736
  if (arr10.every((el) => Array.isArray(el) && el.length === 1))
35515
- return flatten4(arr10);
35737
+ return flatten5(arr10);
35516
35738
  }
35517
35739
  return arr10;
35518
35740
  }
@@ -37310,7 +37532,7 @@ function flattenToFloat648(matrix2, rows, cols) {
37310
37532
  function createComplexEigs({
37311
37533
  addScalar: addScalar2,
37312
37534
  subtract: subtract3,
37313
- flatten: flatten4,
37535
+ flatten: flatten5,
37314
37536
  multiply: multiply2,
37315
37537
  multiplyScalar: multiplyScalar2,
37316
37538
  divideScalar: divideScalar2,
@@ -37639,7 +37861,7 @@ function createComplexEigs({
37639
37861
  solutions = solutions.map(
37640
37862
  (v) => multiply2(correction, v)
37641
37863
  );
37642
- vectors.push(...solutions.map((v) => ({ value: lambda, vector: flatten4(v) })));
37864
+ vectors.push(...solutions.map((v) => ({ value: lambda, vector: flatten5(v) })));
37643
37865
  }
37644
37866
  return vectors;
37645
37867
  }
@@ -38184,7 +38406,7 @@ var createEigs = /* @__PURE__ */ factory(
38184
38406
  add: add3,
38185
38407
  larger: larger2,
38186
38408
  column: _column,
38187
- flatten: flatten4,
38409
+ flatten: flatten5,
38188
38410
  number: number2,
38189
38411
  complex: complex3,
38190
38412
  sqrt: sqrt2,
@@ -38220,7 +38442,7 @@ var createEigs = /* @__PURE__ */ factory(
38220
38442
  subtract: subtract3,
38221
38443
  multiply: multiply2,
38222
38444
  multiplyScalar: multiplyScalar2,
38223
- flatten: flatten4,
38445
+ flatten: flatten5,
38224
38446
  divideScalar: divideScalar2,
38225
38447
  sqrt: sqrt2,
38226
38448
  abs: abs2,
@@ -38868,7 +39090,7 @@ var createSimplify = /* @__PURE__ */ factory(
38868
39090
  isCommutative,
38869
39091
  isAssociative,
38870
39092
  mergeContext,
38871
- flatten: flatten4,
39093
+ flatten: flatten5,
38872
39094
  unflattenr,
38873
39095
  unflattenl,
38874
39096
  createMakeNodeFunction,
@@ -39180,7 +39402,7 @@ var createSimplify = /* @__PURE__ */ factory(
39180
39402
  );
39181
39403
  const expandsym = _getExpandPlaceholderSymbol();
39182
39404
  const expandedL = makeNode([newRule.l, expandsym]);
39183
- flatten4(expandedL, context);
39405
+ flatten5(expandedL, context);
39184
39406
  unflattenr(expandedL, context);
39185
39407
  newRule.expanded = {
39186
39408
  l: expandedL,
@@ -39267,7 +39489,7 @@ var createSimplify = /* @__PURE__ */ factory(
39267
39489
  if (debug)
39268
39490
  rulestr = builtRules[i].name;
39269
39491
  } else {
39270
- flatten4(res, options.context);
39492
+ flatten5(res, options.context);
39271
39493
  res = applyRule(res, builtRules[i], options.context);
39272
39494
  if (debug) {
39273
39495
  rulestr = `${builtRules[i].l.toString()} -> ${builtRules[i].r.toString()}`;
@@ -45474,7 +45696,7 @@ function hessian(f, x, h = 1e-4) {
45474
45696
  }
45475
45697
 
45476
45698
  // src/index.ts
45477
- import { svd as svd3 } from "@danielsimonjr/mathts-matrix";
45699
+ import { svd as svd4 } from "@danielsimonjr/mathts-matrix";
45478
45700
 
45479
45701
  // src/linalg-svd-extra.ts
45480
45702
  import { svd as svd2 } from "@danielsimonjr/mathts-matrix";
@@ -46776,6 +46998,87 @@ function minimizeScalar(f, opts) {
46776
46998
  return { x, fval: fx };
46777
46999
  }
46778
47000
 
47001
+ // src/numeric/interpn.ts
47002
+ function flattenND(values, shape) {
47003
+ const total = shape.reduce((a, b) => a * b, 1);
47004
+ const flat = new Float64Array(total);
47005
+ let pos = 0;
47006
+ function walk(v, depth) {
47007
+ if (depth === shape.length) {
47008
+ if (typeof v !== "number") {
47009
+ throw new Error(
47010
+ `interpn: values is nested deeper than grids.length (${shape.length} dimensions)`
47011
+ );
47012
+ }
47013
+ flat[pos++] = v;
47014
+ return;
47015
+ }
47016
+ if (!Array.isArray(v) || v.length !== shape[depth]) {
47017
+ const gotLen = Array.isArray(v) ? v.length : "scalar";
47018
+ throw new Error(
47019
+ `interpn: values shape mismatch at dimension ${depth} (expected length ${shape[depth]}, got ${gotLen})`
47020
+ );
47021
+ }
47022
+ for (const item of v) walk(item, depth + 1);
47023
+ }
47024
+ walk(values, 0);
47025
+ return flat;
47026
+ }
47027
+ function locateOnAxis(grid, x) {
47028
+ const n = grid.length;
47029
+ if (x < grid[0] || x > grid[n - 1]) {
47030
+ throw new Error(`interpn: query value ${x} is out of bounds [${grid[0]}, ${grid[n - 1]}]`);
47031
+ }
47032
+ if (n === 1) return { i: 0, t: 0 };
47033
+ let lo = 0;
47034
+ let hi = n - 1;
47035
+ while (hi - lo > 1) {
47036
+ const mid = lo + hi >> 1;
47037
+ if (grid[mid] <= x) lo = mid;
47038
+ else hi = mid;
47039
+ }
47040
+ const denom = grid[lo + 1] - grid[lo];
47041
+ const t = denom === 0 ? 0 : (x - grid[lo]) / denom;
47042
+ return { i: lo, t };
47043
+ }
47044
+ function interpn(grids, values, query) {
47045
+ if (grids.length === 0) throw new Error("interpn: at least one grid axis is required");
47046
+ for (const grid of grids) {
47047
+ if (grid.length < 2) throw new Error("interpn: each grid axis needs at least 2 points");
47048
+ for (let i = 1; i < grid.length; i++) {
47049
+ if (!(grid[i] > grid[i - 1])) {
47050
+ throw new Error("interpn: each grid axis must be strictly increasing");
47051
+ }
47052
+ }
47053
+ }
47054
+ const dims = grids.length;
47055
+ const shape = grids.map((g) => g.length);
47056
+ const flat = flattenND(values, shape);
47057
+ const strides = new Array(dims);
47058
+ strides[dims - 1] = 1;
47059
+ for (let d = dims - 2; d >= 0; d--) strides[d] = strides[d + 1] * shape[d + 1];
47060
+ const numCorners = 1 << dims;
47061
+ return query.map((q) => {
47062
+ if (q.length !== dims) {
47063
+ throw new Error(`interpn: query point has ${q.length} coordinates, expected ${dims}`);
47064
+ }
47065
+ const locs = grids.map((g, d) => locateOnAxis(g, q[d]));
47066
+ let result = 0;
47067
+ for (let corner = 0; corner < numCorners; corner++) {
47068
+ let weight = 1;
47069
+ let flatIndex = 0;
47070
+ for (let d = 0; d < dims; d++) {
47071
+ const bit = corner >> d & 1;
47072
+ const { i, t } = locs[d];
47073
+ weight *= bit ? t : 1 - t;
47074
+ flatIndex += (i + bit) * strides[d];
47075
+ }
47076
+ if (weight !== 0) result += weight * flat[flatIndex];
47077
+ }
47078
+ return result;
47079
+ });
47080
+ }
47081
+
46779
47082
  // src/timeseries-extra.ts
46780
47083
  var arr4 = (x) => Array.isArray(x) ? x : Array.from(x);
46781
47084
  var mean4 = (x) => mean(x);
@@ -49239,9 +49542,9 @@ function waverec(coeffs, wavelet = "haar") {
49239
49542
  function rickerWavelet(points, scale3) {
49240
49543
  const amplitude = 2 / (Math.sqrt(3 * scale3) * Math.pow(Math.PI, 0.25));
49241
49544
  const out = new Array(points);
49242
- const center2 = (points - 1) / 2;
49545
+ const center3 = (points - 1) / 2;
49243
49546
  for (let i = 0; i < points; i++) {
49244
- const t = (i - center2) / scale3;
49547
+ const t = (i - center3) / scale3;
49245
49548
  const t2 = t * t;
49246
49549
  out[i] = amplitude * (1 - t2) * Math.exp(-t2 / 2);
49247
49550
  }
@@ -49250,9 +49553,9 @@ function rickerWavelet(points, scale3) {
49250
49553
  function morletWavelet(points, scale3) {
49251
49554
  const norm4 = 1 / (Math.sqrt(scale3) * Math.pow(Math.PI, 0.25));
49252
49555
  const out = new Array(points);
49253
- const center2 = (points - 1) / 2;
49556
+ const center3 = (points - 1) / 2;
49254
49557
  for (let i = 0; i < points; i++) {
49255
- const t = (i - center2) / scale3;
49558
+ const t = (i - center3) / scale3;
49256
49559
  out[i] = norm4 * Math.cos(5 * t) * Math.exp(-(t * t) / 2);
49257
49560
  }
49258
49561
  return out;
@@ -49532,6 +49835,170 @@ function quaternionToRotationMatrix(q) {
49532
49835
  ];
49533
49836
  }
49534
49837
 
49838
+ // src/geometry/geometry-extra.ts
49839
+ import { svd as svd3 } from "@danielsimonjr/mathts-matrix";
49840
+ function dot42(a, b) {
49841
+ return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
49842
+ }
49843
+ function quaternionInverse(q) {
49844
+ const magSq = dot42(q, q);
49845
+ if (magSq === 0) throw new Error("quaternionInverse: zero-magnitude quaternion");
49846
+ return quaternionConjugate(q).map((v) => v / magSq);
49847
+ }
49848
+ function quaternionSlerp(q1, q2, t) {
49849
+ const a = q1;
49850
+ let b = q2;
49851
+ let d = dot42(a, b);
49852
+ if (d < 0) {
49853
+ d = -d;
49854
+ b = b.map((v) => -v);
49855
+ }
49856
+ const DOT_THRESHOLD = 0.9995;
49857
+ if (d > DOT_THRESHOLD) {
49858
+ const lerp = a.map((v, i) => v + t * (b[i] - v));
49859
+ const m = Math.sqrt(dot42(lerp, lerp)) || 1;
49860
+ return lerp.map((v) => v / m);
49861
+ }
49862
+ const clamped = d > 1 ? 1 : d < -1 ? -1 : d;
49863
+ const theta0 = Math.acos(clamped);
49864
+ const theta = theta0 * t;
49865
+ const sinTheta0 = Math.sin(theta0);
49866
+ const sinTheta = Math.sin(theta);
49867
+ const s0 = Math.cos(theta) - d * sinTheta / sinTheta0;
49868
+ const s1 = sinTheta / sinTheta0;
49869
+ return a.map((v, i) => s0 * v + s1 * b[i]);
49870
+ }
49871
+ function quaternionToEuler(q) {
49872
+ const [w, x, y, z] = q;
49873
+ const roll = Math.atan2(2 * (w * x + y * z), 1 - 2 * (x * x + y * y));
49874
+ const sinp = 2 * (w * y - z * x);
49875
+ const pitch = Math.asin(sinp > 1 ? 1 : sinp < -1 ? -1 : sinp);
49876
+ const yaw = Math.atan2(2 * (w * z + x * y), 1 - 2 * (y * y + z * z));
49877
+ return [roll, pitch, yaw];
49878
+ }
49879
+ function boundingBox(points) {
49880
+ if (points.length === 0) throw new Error("boundingBox: requires at least one point");
49881
+ const dims = points[0].length;
49882
+ const min2 = points[0].slice();
49883
+ const max2 = points[0].slice();
49884
+ for (let i = 1; i < points.length; i++) {
49885
+ const p = points[i];
49886
+ for (let d = 0; d < dims; d++) {
49887
+ if (p[d] < min2[d]) min2[d] = p[d];
49888
+ if (p[d] > max2[d]) max2[d] = p[d];
49889
+ }
49890
+ }
49891
+ return { min: min2, max: max2 };
49892
+ }
49893
+ function columnMean(A) {
49894
+ const n = A.length;
49895
+ const d = A[0]?.length ?? 0;
49896
+ const mean7 = new Array(d).fill(0);
49897
+ for (const row2 of A) {
49898
+ for (let j = 0; j < d; j++) mean7[j] += row2[j] / n;
49899
+ }
49900
+ return mean7;
49901
+ }
49902
+ function center2(A, mean7) {
49903
+ return A.map((row2) => row2.map((v, j) => v - mean7[j]));
49904
+ }
49905
+ function frobeniusNorm(A) {
49906
+ let sum3 = 0;
49907
+ for (const row2 of A) for (const v of row2) sum3 += v * v;
49908
+ return Math.sqrt(sum3);
49909
+ }
49910
+ function matTranspose(A) {
49911
+ const rows = A.length;
49912
+ const cols = A[0]?.length ?? 0;
49913
+ const T = Array.from({ length: cols }, () => new Array(rows).fill(0));
49914
+ for (let i = 0; i < rows; i++) for (let j = 0; j < cols; j++) T[j][i] = A[i][j];
49915
+ return T;
49916
+ }
49917
+ function matMul2(A, B) {
49918
+ const rows = A.length;
49919
+ const inner = B.length;
49920
+ const cols = B[0]?.length ?? 0;
49921
+ const C = Array.from({ length: rows }, () => new Array(cols).fill(0));
49922
+ for (let i = 0; i < rows; i++) {
49923
+ for (let k = 0; k < inner; k++) {
49924
+ const a = A[i][k];
49925
+ if (a === 0) continue;
49926
+ for (let j = 0; j < cols; j++) C[i][j] += a * B[k][j];
49927
+ }
49928
+ }
49929
+ return C;
49930
+ }
49931
+ function procrustes(A, B) {
49932
+ if (A.length !== B.length || A.length === 0) {
49933
+ throw new Error("procrustes: A and B must have the same non-zero number of rows");
49934
+ }
49935
+ const A0raw = center2(A, columnMean(A));
49936
+ const B0raw = center2(B, columnMean(B));
49937
+ const nA = frobeniusNorm(A0raw) || 1;
49938
+ const nB = frobeniusNorm(B0raw) || 1;
49939
+ const A0 = A0raw.map((row2) => row2.map((v) => v / nA));
49940
+ const B0 = B0raw.map((row2) => row2.map((v) => v / nB));
49941
+ const M = matMul2(matTranspose(A0), B0);
49942
+ const { U, S, V } = svd3(M);
49943
+ const R = matMul2(V, matTranspose(U));
49944
+ const scale3 = S.reduce((s, v) => s + v, 0);
49945
+ const transformed = matMul2(B0, R).map((row2) => row2.map((v) => v * scale3));
49946
+ let disparity = 0;
49947
+ for (let i = 0; i < A0.length; i++) {
49948
+ for (let j = 0; j < A0[i].length; j++) {
49949
+ const diff2 = A0[i][j] - transformed[i][j];
49950
+ disparity += diff2 * diff2;
49951
+ }
49952
+ }
49953
+ return { R, scale: scale3, disparity };
49954
+ }
49955
+ function euclideanDist(a, b) {
49956
+ let sum3 = 0;
49957
+ for (let i = 0; i < a.length; i++) {
49958
+ const d = a[i] - b[i];
49959
+ sum3 += d * d;
49960
+ }
49961
+ return Math.sqrt(sum3);
49962
+ }
49963
+ function kdTreeKNN(points, query, k) {
49964
+ const ranked = points.map((p, i) => ({ i, d: euclideanDist(p, query) }));
49965
+ ranked.sort((a, b) => a.d - b.d);
49966
+ return ranked.slice(0, Math.max(0, Math.min(k, ranked.length))).map((e) => e.i);
49967
+ }
49968
+ function kdTreeRadius(points, query, r) {
49969
+ const result = [];
49970
+ for (let i = 0; i < points.length; i++) {
49971
+ if (euclideanDist(points[i], query) <= r) result.push(i);
49972
+ }
49973
+ return result;
49974
+ }
49975
+ function flatten4(arr10) {
49976
+ const result = [];
49977
+ const stack = [...arr10];
49978
+ while (stack.length > 0) {
49979
+ const item = stack.shift();
49980
+ if (Array.isArray(item)) {
49981
+ stack.unshift(...item);
49982
+ } else {
49983
+ result.push(item);
49984
+ }
49985
+ }
49986
+ return result;
49987
+ }
49988
+ function setIsSuperset(a, b) {
49989
+ return setIsSubset(b, a);
49990
+ }
49991
+ function setEqual(a, b) {
49992
+ return setIsSubset(a, b) && setIsSubset(b, a);
49993
+ }
49994
+ function setDisjoint(a, b) {
49995
+ const flatA = flatten4(a);
49996
+ for (const elem of flatA) {
49997
+ if (setMultiplicity(elem, b) > 0) return false;
49998
+ }
49999
+ return true;
50000
+ }
50001
+
49535
50002
  // src/stats/inference-extra2.ts
49536
50003
  var TWO_PI = 2 * Math.PI;
49537
50004
  function noncentralChi2CDF(x, df, nc) {
@@ -49807,6 +50274,475 @@ function rootsLegendre(n) {
49807
50274
  weights.reverse();
49808
50275
  return { nodes, weights };
49809
50276
  }
50277
+
50278
+ // src/graph/traversal-centrality.ts
50279
+ function bfs(adj, start) {
50280
+ const n = adj.length;
50281
+ if (start < 0 || start >= n) {
50282
+ throw new Error("bfs: start out of bounds");
50283
+ }
50284
+ const visited = new Array(n).fill(false);
50285
+ const order = [];
50286
+ const queue = [start];
50287
+ visited[start] = true;
50288
+ while (queue.length > 0) {
50289
+ const u = queue.shift();
50290
+ order.push(u);
50291
+ for (let v = 0; v < n; v++) {
50292
+ if (!visited[v] && Number.isFinite(adj[u][v]) && adj[u][v] !== 0) {
50293
+ visited[v] = true;
50294
+ queue.push(v);
50295
+ }
50296
+ }
50297
+ }
50298
+ return order;
50299
+ }
50300
+ function dfs(adj, start) {
50301
+ const n = adj.length;
50302
+ if (start < 0 || start >= n) {
50303
+ throw new Error("dfs: start out of bounds");
50304
+ }
50305
+ const visited = new Array(n).fill(false);
50306
+ const order = [];
50307
+ function visit(u) {
50308
+ visited[u] = true;
50309
+ order.push(u);
50310
+ for (let v = 0; v < n; v++) {
50311
+ if (!visited[v] && Number.isFinite(adj[u][v]) && adj[u][v] !== 0) {
50312
+ visit(v);
50313
+ }
50314
+ }
50315
+ }
50316
+ visit(start);
50317
+ return order;
50318
+ }
50319
+ function _hasWeightedEdge(w) {
50320
+ return Number.isFinite(w) && w !== 0;
50321
+ }
50322
+ function floydWarshall(adj) {
50323
+ const n = adj.length;
50324
+ const dist = Array.from({ length: n }, () => new Array(n).fill(Infinity));
50325
+ for (let i = 0; i < n; i++) {
50326
+ dist[i][i] = 0;
50327
+ for (let j = 0; j < n; j++) {
50328
+ if (i !== j && _hasWeightedEdge(adj[i][j])) {
50329
+ dist[i][j] = adj[i][j];
50330
+ }
50331
+ }
50332
+ }
50333
+ for (let k = 0; k < n; k++) {
50334
+ for (let i = 0; i < n; i++) {
50335
+ if (dist[i][k] === Infinity) continue;
50336
+ for (let j = 0; j < n; j++) {
50337
+ const throughK = dist[i][k] + dist[k][j];
50338
+ if (throughK < dist[i][j]) {
50339
+ dist[i][j] = throughK;
50340
+ }
50341
+ }
50342
+ }
50343
+ }
50344
+ return dist;
50345
+ }
50346
+ function bellmanFord(adj, source) {
50347
+ const n = adj.length;
50348
+ if (source < 0 || source >= n) {
50349
+ throw new Error("bellmanFord: source out of bounds");
50350
+ }
50351
+ const dist = new Array(n).fill(Infinity);
50352
+ dist[source] = 0;
50353
+ const edges = [];
50354
+ for (let u = 0; u < n; u++) {
50355
+ for (let v = 0; v < n; v++) {
50356
+ if (u !== v && _hasWeightedEdge(adj[u][v])) {
50357
+ edges.push([u, v, adj[u][v]]);
50358
+ }
50359
+ }
50360
+ }
50361
+ for (let i = 0; i < n - 1; i++) {
50362
+ let changed = false;
50363
+ for (const [u, v, w] of edges) {
50364
+ if (dist[u] !== Infinity && dist[u] + w < dist[v]) {
50365
+ dist[v] = dist[u] + w;
50366
+ changed = true;
50367
+ }
50368
+ }
50369
+ if (!changed) break;
50370
+ }
50371
+ let hasNegativeCycle = false;
50372
+ for (const [u, v, w] of edges) {
50373
+ if (dist[u] !== Infinity && dist[u] + w < dist[v]) {
50374
+ hasNegativeCycle = true;
50375
+ break;
50376
+ }
50377
+ }
50378
+ return { dist, hasNegativeCycle };
50379
+ }
50380
+ function closenessCentrality(adj) {
50381
+ const n = adj.length;
50382
+ const dist = floydWarshall(adj);
50383
+ const result = new Array(n).fill(0);
50384
+ for (let u = 0; u < n; u++) {
50385
+ let sum3 = 0;
50386
+ let reachable = 0;
50387
+ for (let v = 0; v < n; v++) {
50388
+ if (v === u) continue;
50389
+ if (dist[v][u] < Infinity) {
50390
+ sum3 += dist[v][u];
50391
+ reachable++;
50392
+ }
50393
+ }
50394
+ if (reachable === 0 || sum3 === 0) {
50395
+ result[u] = 0;
50396
+ continue;
50397
+ }
50398
+ const r = reachable + 1;
50399
+ result[u] = (r - 1 === n - 1 ? 1 : (r - 1) / (n - 1)) * ((r - 1) / sum3);
50400
+ }
50401
+ return result;
50402
+ }
50403
+ function harmonicCentrality(adj) {
50404
+ const n = adj.length;
50405
+ const dist = floydWarshall(adj);
50406
+ const result = new Array(n).fill(0);
50407
+ for (let u = 0; u < n; u++) {
50408
+ let sum3 = 0;
50409
+ for (let v = 0; v < n; v++) {
50410
+ if (v === u) continue;
50411
+ if (dist[v][u] > 0 && dist[v][u] < Infinity) {
50412
+ sum3 += 1 / dist[v][u];
50413
+ }
50414
+ }
50415
+ result[u] = sum3;
50416
+ }
50417
+ return result;
50418
+ }
50419
+
50420
+ // src/graph/optimization.ts
50421
+ function _bfsResidual(residual, source, sink) {
50422
+ const n = residual.length;
50423
+ const parent = new Array(n).fill(-1);
50424
+ const visited = new Array(n).fill(false);
50425
+ visited[source] = true;
50426
+ const queue = [source];
50427
+ while (queue.length > 0) {
50428
+ const u = queue.shift();
50429
+ if (u === sink) return parent;
50430
+ for (let v = 0; v < n; v++) {
50431
+ if (!visited[v] && residual[u][v] > 0) {
50432
+ visited[v] = true;
50433
+ parent[v] = u;
50434
+ queue.push(v);
50435
+ }
50436
+ }
50437
+ }
50438
+ return visited[sink] ? parent : null;
50439
+ }
50440
+ function maxFlow(capacity, source, sink) {
50441
+ const n = capacity.length;
50442
+ if (source < 0 || source >= n || sink < 0 || sink >= n) {
50443
+ throw new Error("maxFlow: source or sink out of bounds");
50444
+ }
50445
+ const residual = capacity.map((row2) => row2.slice());
50446
+ const flow = Array.from({ length: n }, () => new Array(n).fill(0));
50447
+ let total = 0;
50448
+ if (source === sink) {
50449
+ return { maxFlow: 0, flow };
50450
+ }
50451
+ for (; ; ) {
50452
+ const parent = _bfsResidual(residual, source, sink);
50453
+ if (parent === null) break;
50454
+ let pathFlow = Infinity;
50455
+ for (let v = sink; v !== source; v = parent[v]) {
50456
+ const u = parent[v];
50457
+ pathFlow = Math.min(pathFlow, residual[u][v]);
50458
+ }
50459
+ for (let v = sink; v !== source; v = parent[v]) {
50460
+ const u = parent[v];
50461
+ residual[u][v] -= pathFlow;
50462
+ residual[v][u] += pathFlow;
50463
+ flow[u][v] += pathFlow;
50464
+ flow[v][u] -= pathFlow;
50465
+ }
50466
+ total += pathFlow;
50467
+ }
50468
+ return { maxFlow: total, flow };
50469
+ }
50470
+ function minCut(capacity, source, sink) {
50471
+ const n = capacity.length;
50472
+ if (source < 0 || source >= n || sink < 0 || sink >= n) {
50473
+ throw new Error("minCut: source or sink out of bounds");
50474
+ }
50475
+ const { maxFlow: value, flow } = maxFlow(capacity, source, sink);
50476
+ const residual = Array.from(
50477
+ { length: n },
50478
+ (_, i) => capacity[i].map((c, j) => c - flow[i][j])
50479
+ );
50480
+ const visited = new Array(n).fill(false);
50481
+ visited[source] = true;
50482
+ const queue = [source];
50483
+ while (queue.length > 0) {
50484
+ const u = queue.shift();
50485
+ for (let v = 0; v < n; v++) {
50486
+ if (!visited[v] && residual[u][v] > 0) {
50487
+ visited[v] = true;
50488
+ queue.push(v);
50489
+ }
50490
+ }
50491
+ }
50492
+ const s = [];
50493
+ const t = [];
50494
+ for (let i = 0; i < n; i++) {
50495
+ (visited[i] ? s : t).push(i);
50496
+ }
50497
+ return { value, partition: [s, t] };
50498
+ }
50499
+ function astar(adj, start, goal, heuristic) {
50500
+ const n = adj.length;
50501
+ if (start < 0 || start >= n || goal < 0 || goal >= n) {
50502
+ throw new Error("astar: start or goal out of bounds");
50503
+ }
50504
+ if (start === goal) {
50505
+ return { path: [start], cost: 0 };
50506
+ }
50507
+ const gScore = new Array(n).fill(Infinity);
50508
+ gScore[start] = 0;
50509
+ const cameFrom = new Array(n).fill(-1);
50510
+ const closed = new Array(n).fill(false);
50511
+ const open = [{ node: start, fScore: heuristic(start) }];
50512
+ while (open.length > 0) {
50513
+ let bestIdx = 0;
50514
+ for (let i = 1; i < open.length; i++) {
50515
+ if (open[i].fScore < open[bestIdx].fScore) bestIdx = i;
50516
+ }
50517
+ const { node: u } = open[bestIdx];
50518
+ open.splice(bestIdx, 1);
50519
+ if (closed[u]) continue;
50520
+ closed[u] = true;
50521
+ if (u === goal) break;
50522
+ for (let v = 0; v < n; v++) {
50523
+ if (closed[v] || !Number.isFinite(adj[u][v]) || adj[u][v] === 0) continue;
50524
+ const tentative = gScore[u] + adj[u][v];
50525
+ if (tentative < gScore[v]) {
50526
+ gScore[v] = tentative;
50527
+ cameFrom[v] = u;
50528
+ open.push({ node: v, fScore: tentative + heuristic(v) });
50529
+ }
50530
+ }
50531
+ }
50532
+ if (gScore[goal] === Infinity) {
50533
+ return { path: [], cost: Infinity };
50534
+ }
50535
+ const path = [];
50536
+ for (let v = goal; v !== -1; v = cameFrom[v]) {
50537
+ path.unshift(v);
50538
+ if (v === start) break;
50539
+ }
50540
+ return { path, cost: gScore[goal] };
50541
+ }
50542
+ function hungarian(cost) {
50543
+ const n = cost.length;
50544
+ if (n === 0) {
50545
+ return { assignment: [], cost: 0 };
50546
+ }
50547
+ for (const row2 of cost) {
50548
+ if (row2.length !== n) {
50549
+ throw new Error("hungarian: cost matrix must be square");
50550
+ }
50551
+ }
50552
+ const INF = Infinity;
50553
+ const u = new Array(n + 1).fill(0);
50554
+ const v = new Array(n + 1).fill(0);
50555
+ const a = new Array(n + 1).fill(0);
50556
+ const p = new Array(n + 1).fill(0);
50557
+ for (let i = 1; i <= n; i++) {
50558
+ a[0] = i;
50559
+ let j0 = 0;
50560
+ const minv = new Array(n + 1).fill(INF);
50561
+ const used = new Array(n + 1).fill(false);
50562
+ do {
50563
+ used[j0] = true;
50564
+ const i0 = a[j0];
50565
+ let delta = INF;
50566
+ let j1 = -1;
50567
+ for (let j = 1; j <= n; j++) {
50568
+ if (used[j]) continue;
50569
+ const cur = cost[i0 - 1][j - 1] - u[i0] - v[j];
50570
+ if (cur < minv[j]) {
50571
+ minv[j] = cur;
50572
+ p[j] = j0;
50573
+ }
50574
+ if (minv[j] < delta) {
50575
+ delta = minv[j];
50576
+ j1 = j;
50577
+ }
50578
+ }
50579
+ for (let j = 0; j <= n; j++) {
50580
+ if (used[j]) {
50581
+ u[a[j]] += delta;
50582
+ v[j] -= delta;
50583
+ } else {
50584
+ minv[j] -= delta;
50585
+ }
50586
+ }
50587
+ j0 = j1;
50588
+ } while (a[j0] !== 0);
50589
+ while (j0 !== 0) {
50590
+ const j1 = p[j0];
50591
+ a[j0] = a[j1];
50592
+ j0 = j1;
50593
+ }
50594
+ }
50595
+ const assignment = new Array(n).fill(-1);
50596
+ let totalCost = 0;
50597
+ for (let j = 1; j <= n; j++) {
50598
+ const i = a[j];
50599
+ if (i > 0) {
50600
+ assignment[i - 1] = j - 1;
50601
+ totalCost += cost[i - 1][j - 1];
50602
+ }
50603
+ }
50604
+ return { assignment, cost: totalCost };
50605
+ }
50606
+
50607
+ // src/numeric/interval.ts
50608
+ var EPS3 = Number.EPSILON;
50609
+ function outward(lo, hi) {
50610
+ const loOut = lo - Math.abs(lo) * EPS3 - Number.MIN_VALUE;
50611
+ const hiOut = hi + Math.abs(hi) * EPS3 + Number.MIN_VALUE;
50612
+ return new Interval(loOut, hiOut);
50613
+ }
50614
+ var Interval = class {
50615
+ lo;
50616
+ hi;
50617
+ constructor(lo, hi) {
50618
+ if (lo > hi) {
50619
+ throw new Error(`Interval: lo (${lo}) must be <= hi (${hi})`);
50620
+ }
50621
+ this.lo = lo;
50622
+ this.hi = hi;
50623
+ }
50624
+ /** `[this.lo + b.lo, this.hi + b.hi]`, outward-rounded. */
50625
+ add(b) {
50626
+ return outward(this.lo + b.lo, this.hi + b.hi);
50627
+ }
50628
+ /** `[this.lo - b.hi, this.hi - b.lo]`, outward-rounded. */
50629
+ sub(b) {
50630
+ return outward(this.lo - b.hi, this.hi - b.lo);
50631
+ }
50632
+ /**
50633
+ * Interval product: the min/max of all four endpoint products
50634
+ * (`lo*lo, lo*hi, hi*lo, hi*hi`), outward-rounded. Correct for any
50635
+ * combination of signs.
50636
+ */
50637
+ mul(b) {
50638
+ const p1 = this.lo * b.lo;
50639
+ const p2 = this.lo * b.hi;
50640
+ const p3 = this.hi * b.lo;
50641
+ const p4 = this.hi * b.hi;
50642
+ return outward(Math.min(p1, p2, p3, p4), Math.max(p1, p2, p3, p4));
50643
+ }
50644
+ /**
50645
+ * Interval quotient. Throws if `b` contains 0 (division would be
50646
+ * unbounded). Otherwise the min/max of the four endpoint quotients,
50647
+ * outward-rounded.
50648
+ */
50649
+ div(b) {
50650
+ if (b.contains(0)) {
50651
+ throw new Error("Interval.div: divisor interval contains zero");
50652
+ }
50653
+ const q1 = this.lo / b.lo;
50654
+ const q2 = this.lo / b.hi;
50655
+ const q3 = this.hi / b.lo;
50656
+ const q4 = this.hi / b.hi;
50657
+ return outward(Math.min(q1, q2, q3, q4), Math.max(q1, q2, q3, q4));
50658
+ }
50659
+ /** `[-this.hi, -this.lo]`, outward-rounded. */
50660
+ neg() {
50661
+ return outward(-this.hi, -this.lo);
50662
+ }
50663
+ /** `hi - lo`. */
50664
+ width() {
50665
+ return this.hi - this.lo;
50666
+ }
50667
+ /** `(lo + hi) / 2`. */
50668
+ mid() {
50669
+ return (this.lo + this.hi) / 2;
50670
+ }
50671
+ /** Whether the closed interval `[lo, hi]` contains the real number `x`. */
50672
+ contains(x) {
50673
+ return x >= this.lo && x <= this.hi;
50674
+ }
50675
+ /**
50676
+ * Square root, monotonic-increasing over `[0, +Infinity)`. Throws if the
50677
+ * interval contains negative values (real square root is undefined there).
50678
+ */
50679
+ sqrt() {
50680
+ if (this.lo < 0) {
50681
+ throw new Error("Interval.sqrt: interval contains negative values");
50682
+ }
50683
+ return outward(Math.sqrt(this.lo), Math.sqrt(this.hi));
50684
+ }
50685
+ /** Exponential, monotonic-increasing over all reals. */
50686
+ exp() {
50687
+ return outward(Math.exp(this.lo), Math.exp(this.hi));
50688
+ }
50689
+ /**
50690
+ * Natural log, monotonic-increasing over `(0, +Infinity)`. Throws if the
50691
+ * interval is not strictly positive.
50692
+ */
50693
+ log() {
50694
+ if (this.lo <= 0) {
50695
+ throw new Error("Interval.log: interval must be strictly positive");
50696
+ }
50697
+ return outward(Math.log(this.lo), Math.log(this.hi));
50698
+ }
50699
+ /**
50700
+ * Integer power `x^n`, monotonic-aware:
50701
+ * - `n` odd: `x^n` is monotonic-increasing over all reals, so the result is
50702
+ * `[lo^n, hi^n]`.
50703
+ * - `n` even, interval entirely non-negative: monotonic-increasing, so
50704
+ * `[lo^n, hi^n]`.
50705
+ * - `n` even, interval entirely non-positive: monotonic-decreasing (in
50706
+ * magnitude, toward zero), so `[hi^n, lo^n]`.
50707
+ * - `n` even, interval spans zero: the minimum is `0` (attained at `x=0`)
50708
+ * and the maximum is `max(|lo|, |hi|)^n`.
50709
+ * - `n` negative: computed as the reciprocal of `pow(-n)`; throws if that
50710
+ * positive-power interval contains zero (division by zero).
50711
+ * - `n = 0`: `[1, 1]` for every interval.
50712
+ *
50713
+ * Throws if `n` is not an integer.
50714
+ */
50715
+ pow(n) {
50716
+ if (!Number.isInteger(n)) {
50717
+ throw new Error("Interval.pow: exponent must be an integer");
50718
+ }
50719
+ if (n === 0) {
50720
+ return outward(1, 1);
50721
+ }
50722
+ if (n < 0) {
50723
+ const positivePow = this.pow(-n);
50724
+ if (positivePow.contains(0)) {
50725
+ throw new Error("Interval.pow: cannot invert an interval containing zero");
50726
+ }
50727
+ return outward(1 / positivePow.hi, 1 / positivePow.lo);
50728
+ }
50729
+ const isOdd = n % 2 !== 0;
50730
+ if (isOdd) {
50731
+ return outward(this.lo ** n, this.hi ** n);
50732
+ }
50733
+ if (this.lo >= 0) {
50734
+ return outward(this.lo ** n, this.hi ** n);
50735
+ }
50736
+ if (this.hi <= 0) {
50737
+ return outward(this.hi ** n, this.lo ** n);
50738
+ }
50739
+ const maxAbs = Math.max(Math.abs(this.lo), Math.abs(this.hi));
50740
+ return outward(0, maxAbs ** n);
50741
+ }
50742
+ };
50743
+ function interval(lo, hi) {
50744
+ return new Interval(lo, hi);
50745
+ }
49810
50746
  export {
49811
50747
  ARRAY_WORKER_THRESHOLD,
49812
50748
  CAS_BATCH_THRESHOLD,
@@ -49818,6 +50754,7 @@ export {
49818
50754
  GPU_ELEMENTWISE_OPS,
49819
50755
  GPU_MIN_ELEMENTS2 as GPU_MIN_ELEMENTS,
49820
50756
  GPU_REDUCE_OPS,
50757
+ Interval,
49821
50758
  WASM_INTERP_THRESHOLD,
49822
50759
  abs,
49823
50760
  acf,
@@ -49848,6 +50785,7 @@ export {
49848
50785
  asin,
49849
50786
  asinh,
49850
50787
  assume,
50788
+ astar,
49851
50789
  asymptotic,
49852
50790
  atan,
49853
50791
  atan2,
@@ -49859,6 +50797,7 @@ export {
49859
50797
  bartlettPSD,
49860
50798
  bartlettTest,
49861
50799
  bellNumbers,
50800
+ bellmanFord,
49862
50801
  bernoulli,
49863
50802
  bernoulliPMF,
49864
50803
  besselI,
@@ -49878,6 +50817,7 @@ export {
49878
50817
  betweennessCentrality,
49879
50818
  bezierCurve,
49880
50819
  bfgs,
50820
+ bfs,
49881
50821
  bicgstab,
49882
50822
  bigint,
49883
50823
  bignumber,
@@ -49895,6 +50835,7 @@ export {
49895
50835
  boltzmann,
49896
50836
  boolean,
49897
50837
  bootstrapCI,
50838
+ boundingBox,
49898
50839
  bspline,
49899
50840
  butter,
49900
50841
  buttord,
@@ -49941,6 +50882,7 @@ export {
49941
50882
  classicalElectronRadius,
49942
50883
  clearAssumptions,
49943
50884
  clone3 as clone,
50885
+ closenessCentrality,
49944
50886
  cochranQ,
49945
50887
  coefficientList,
49946
50888
  coherence,
@@ -50026,6 +50968,7 @@ export {
50026
50968
  det,
50027
50969
  detrend,
50028
50970
  deuteronMass,
50971
+ dfs,
50029
50972
  diag,
50030
50973
  diff,
50031
50974
  differences,
@@ -50192,6 +51135,7 @@ export {
50192
51135
  fix,
50193
51136
  flatten3 as flatten,
50194
51137
  floor,
51138
+ floydWarshall,
50195
51139
  forEach,
50196
51140
  format4 as format,
50197
51141
  fourier,
@@ -50246,6 +51190,7 @@ export {
50246
51190
  groupDelay,
50247
51191
  gumbelDist,
50248
51192
  halley,
51193
+ harmonicCentrality,
50249
51194
  harmonicNumber,
50250
51195
  hartreeEnergy,
50251
51196
  hasNumericValue,
@@ -50261,6 +51206,7 @@ export {
50261
51206
  histogram,
50262
51207
  hmean,
50263
51208
  hotellingT2,
51209
+ hungarian,
50264
51210
  hyp0f1,
50265
51211
  hyp1f1,
50266
51212
  hyp2f1,
@@ -50281,10 +51227,12 @@ export {
50281
51227
  initializeStatistics,
50282
51228
  integerDigits,
50283
51229
  integrate,
51230
+ interpn,
50284
51231
  interpolate,
50285
51232
  intersect,
50286
51233
  intersectLines2D,
50287
51234
  intersectSegments2D,
51235
+ interval,
50288
51236
  inv,
50289
51237
  invFourier,
50290
51238
  invGaussDist,
@@ -50318,7 +51266,9 @@ export {
50318
51266
  josephson,
50319
51267
  jsDivergence,
50320
51268
  kdTree,
51269
+ kdTreeKNN,
50321
51270
  kdTreeNearest,
51271
+ kdTreeRadius,
50322
51272
  kendallTau,
50323
51273
  kendallTauTest,
50324
51274
  kendalltau,
@@ -50410,6 +51360,7 @@ export {
50410
51360
  matrixSqrtm,
50411
51361
  matvec,
50412
51362
  max,
51363
+ maxFlow,
50413
51364
  maxSelect,
50414
51365
  maximize,
50415
51366
  mcnemar,
@@ -50419,6 +51370,7 @@ export {
50419
51370
  median,
50420
51371
  medianSelect,
50421
51372
  min,
51373
+ minCut,
50422
51374
  minSelect,
50423
51375
  minimalPolynomial,
50424
51376
  minimize,
@@ -50562,6 +51514,7 @@ export {
50562
51514
  primitiveRoot,
50563
51515
  principalComponentAnalysis,
50564
51516
  print,
51517
+ procrustes,
50565
51518
  prod,
50566
51519
  projectVector,
50567
51520
  proportionCI,
@@ -50575,9 +51528,12 @@ export {
50575
51528
  quantumOfCirculation,
50576
51529
  quaternionConjugate,
50577
51530
  quaternionFromAxisAngle,
51531
+ quaternionInverse,
50578
51532
  quaternionMultiply,
50579
51533
  quaternionNormalize,
50580
51534
  quaternionRotate,
51535
+ quaternionSlerp,
51536
+ quaternionToEuler,
50581
51537
  quaternionToRotationMatrix,
50582
51538
  quickSelect,
50583
51539
  qz,
@@ -50634,9 +51590,12 @@ export {
50634
51590
  seriesCoefficient,
50635
51591
  setCartesian,
50636
51592
  setDifference,
51593
+ setDisjoint,
50637
51594
  setDistinct,
51595
+ setEqual,
50638
51596
  setIntersect,
50639
51597
  setIsSubset,
51598
+ setIsSuperset,
50640
51599
  setMultiplicity,
50641
51600
  setPowerset,
50642
51601
  setSize,
@@ -50707,7 +51666,7 @@ export {
50707
51666
  subtractScalar,
50708
51667
  sum,
50709
51668
  summation,
50710
- svd3 as svd,
51669
+ svd4 as svd,
50711
51670
  sylvester,
50712
51671
  symbolicEqual,
50713
51672
  symbolicIntegral,