@danielsimonjr/mathts-functions 0.28.0 → 0.30.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.d.ts +18 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +926 -101
- package/dist/linalg-svd-extra.d.ts +21 -0
- package/dist/linalg-svd-extra.d.ts.map +1 -0
- package/dist/numeric/adaptive-quad.d.ts +56 -0
- package/dist/numeric/adaptive-quad.d.ts.map +1 -0
- package/dist/numeric/bfgs.d.ts +70 -0
- package/dist/numeric/bfgs.d.ts.map +1 -0
- package/dist/numeric/fsolve.d.ts +45 -0
- package/dist/numeric/fsolve.d.ts.map +1 -0
- package/dist/numeric/minimize-scalar.d.ts +56 -0
- package/dist/numeric/minimize-scalar.d.ts.map +1 -0
- package/dist/numeric/nnls.d.ts +72 -0
- package/dist/numeric/nnls.d.ts.map +1 -0
- package/dist/numeric/numeric-jacobian.d.ts +28 -0
- package/dist/numeric/numeric-jacobian.d.ts.map +1 -0
- package/dist/numeric/open-root-finders.d.ts +95 -0
- package/dist/numeric/open-root-finders.d.ts.map +1 -0
- package/dist/typed/cas.d.ts +13 -4
- package/dist/typed/cas.d.ts.map +1 -1
- package/dist/typed/numeric.d.ts +35 -10
- package/dist/typed/numeric.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -5510,15 +5510,15 @@ function modNumber(x, y) {
|
|
|
5510
5510
|
return y === 0 ? x : x - y * Math.floor(x / y);
|
|
5511
5511
|
}
|
|
5512
5512
|
modNumber.signature = n2;
|
|
5513
|
-
function nthRootNumber(a,
|
|
5514
|
-
const inv2 =
|
|
5513
|
+
function nthRootNumber(a, root2 = 2) {
|
|
5514
|
+
const inv2 = root2 < 0;
|
|
5515
5515
|
if (inv2) {
|
|
5516
|
-
|
|
5516
|
+
root2 = -root2;
|
|
5517
5517
|
}
|
|
5518
|
-
if (
|
|
5518
|
+
if (root2 === 0) {
|
|
5519
5519
|
throw new Error("Root must be non-zero");
|
|
5520
5520
|
}
|
|
5521
|
-
if (a < 0 && Math.abs(
|
|
5521
|
+
if (a < 0 && Math.abs(root2) % 2 !== 1) {
|
|
5522
5522
|
throw new Error("Root must be odd when a is negative.");
|
|
5523
5523
|
}
|
|
5524
5524
|
if (a === 0) {
|
|
@@ -5527,7 +5527,7 @@ function nthRootNumber(a, root = 2) {
|
|
|
5527
5527
|
if (!isFinite(a)) {
|
|
5528
5528
|
return inv2 ? 0 : a;
|
|
5529
5529
|
}
|
|
5530
|
-
let x = Math.pow(Math.abs(a), 1 /
|
|
5530
|
+
let x = Math.pow(Math.abs(a), 1 / root2);
|
|
5531
5531
|
x = a < 0 ? -x : x;
|
|
5532
5532
|
return inv2 ? 1 / x : x;
|
|
5533
5533
|
}
|
|
@@ -8381,18 +8381,18 @@ import { computePool as computePool7 } from "@danielsimonjr/mathts-parallel";
|
|
|
8381
8381
|
var GEOMETRY_WASM_THRESHOLD = 32;
|
|
8382
8382
|
var HULL3D_WASM_THRESHOLD = 1024;
|
|
8383
8383
|
function angle2D(v1, v2) {
|
|
8384
|
-
const
|
|
8384
|
+
const dot4 = v1[0] * v2[0] + v1[1] * v2[1];
|
|
8385
8385
|
const mag1 = Math.sqrt(v1[0] ** 2 + v1[1] ** 2);
|
|
8386
8386
|
const mag2 = Math.sqrt(v2[0] ** 2 + v2[1] ** 2);
|
|
8387
8387
|
if (mag1 === 0 || mag2 === 0) return NaN;
|
|
8388
|
-
return Math.acos(Math.max(-1, Math.min(1,
|
|
8388
|
+
return Math.acos(Math.max(-1, Math.min(1, dot4 / (mag1 * mag2))));
|
|
8389
8389
|
}
|
|
8390
8390
|
function angle3D(v1, v2) {
|
|
8391
|
-
const
|
|
8391
|
+
const dot4 = v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2];
|
|
8392
8392
|
const mag1 = Math.sqrt(v1[0] ** 2 + v1[1] ** 2 + v1[2] ** 2);
|
|
8393
8393
|
const mag2 = Math.sqrt(v2[0] ** 2 + v2[1] ** 2 + v2[2] ** 2);
|
|
8394
8394
|
if (mag1 === 0 || mag2 === 0) return NaN;
|
|
8395
|
-
return Math.acos(Math.max(-1, Math.min(1,
|
|
8395
|
+
return Math.acos(Math.max(-1, Math.min(1, dot4 / (mag1 * mag2))));
|
|
8396
8396
|
}
|
|
8397
8397
|
function cross3D(a, b) {
|
|
8398
8398
|
return [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
|
|
@@ -8497,14 +8497,14 @@ function reflectVector(v, normal) {
|
|
|
8497
8497
|
const mag = Math.sqrt(normal.reduce((s, c) => s + c * c, 0));
|
|
8498
8498
|
if (mag === 0) return v.slice();
|
|
8499
8499
|
const n = normal.map((c) => c / mag);
|
|
8500
|
-
const
|
|
8501
|
-
return v.map((c, i) => c - 2 *
|
|
8500
|
+
const dot4 = v.reduce((s, c, i) => s + c * n[i], 0);
|
|
8501
|
+
return v.map((c, i) => c - 2 * dot4 * n[i]);
|
|
8502
8502
|
}
|
|
8503
8503
|
function projectVector(v, onto) {
|
|
8504
|
-
const
|
|
8504
|
+
const dot4 = v.reduce((s, c, i) => s + c * onto[i], 0);
|
|
8505
8505
|
const magSq = onto.reduce((s, c) => s + c * c, 0);
|
|
8506
8506
|
if (magSq === 0) return onto.map(() => 0);
|
|
8507
|
-
const scale =
|
|
8507
|
+
const scale = dot4 / magSq;
|
|
8508
8508
|
return onto.map((c) => c * scale);
|
|
8509
8509
|
}
|
|
8510
8510
|
function distance2D(a, b) {
|
|
@@ -8852,9 +8852,9 @@ function kdTree(points) {
|
|
|
8852
8852
|
}
|
|
8853
8853
|
return build(indexed, 0);
|
|
8854
8854
|
}
|
|
8855
|
-
function kdTreeNearest(
|
|
8856
|
-
if (!
|
|
8857
|
-
let best = { point:
|
|
8855
|
+
function kdTreeNearest(root2, target) {
|
|
8856
|
+
if (!root2) return null;
|
|
8857
|
+
let best = { point: root2.point, index: root2.index, distance: distanceNDSq(root2.point, target) };
|
|
8858
8858
|
function search(node) {
|
|
8859
8859
|
if (!node) return;
|
|
8860
8860
|
const d = distanceNDSq(node.point, target);
|
|
@@ -8869,7 +8869,7 @@ function kdTreeNearest(root, target) {
|
|
|
8869
8869
|
search(second);
|
|
8870
8870
|
}
|
|
8871
8871
|
}
|
|
8872
|
-
search(
|
|
8872
|
+
search(root2);
|
|
8873
8873
|
best.distance = Math.sqrt(best.distance);
|
|
8874
8874
|
return best;
|
|
8875
8875
|
}
|
|
@@ -8921,8 +8921,8 @@ function nearestNeighbor(points, query) {
|
|
|
8921
8921
|
}
|
|
8922
8922
|
}
|
|
8923
8923
|
}
|
|
8924
|
-
const
|
|
8925
|
-
return kdTreeNearest(
|
|
8924
|
+
const root2 = kdTree(points);
|
|
8925
|
+
return kdTreeNearest(root2, query);
|
|
8926
8926
|
}
|
|
8927
8927
|
function convexHull3D(points) {
|
|
8928
8928
|
const n = points.length;
|
|
@@ -11843,6 +11843,81 @@ var createSolveODE = /* @__PURE__ */ factory(
|
|
|
11843
11843
|
}
|
|
11844
11844
|
);
|
|
11845
11845
|
|
|
11846
|
+
// src/numeric/adaptive-quad.ts
|
|
11847
|
+
var XGK = [
|
|
11848
|
+
0.991455371120813,
|
|
11849
|
+
0.949107912342759,
|
|
11850
|
+
0.864864423359769,
|
|
11851
|
+
0.741531185599394,
|
|
11852
|
+
0.586087235467691,
|
|
11853
|
+
0.405845151377397,
|
|
11854
|
+
0.207784955007898,
|
|
11855
|
+
0
|
|
11856
|
+
];
|
|
11857
|
+
var WGK = [
|
|
11858
|
+
0.022935322010529,
|
|
11859
|
+
0.063092092629979,
|
|
11860
|
+
0.10479001032225,
|
|
11861
|
+
0.140653259715525,
|
|
11862
|
+
0.169004726639267,
|
|
11863
|
+
0.190350578064785,
|
|
11864
|
+
0.204432940075298,
|
|
11865
|
+
0.209482141084728
|
|
11866
|
+
];
|
|
11867
|
+
var WG = [0.12948496616887, 0.27970539148928, 0.38183005050512, 0.41795918367347];
|
|
11868
|
+
var KRONROD_NODES = [];
|
|
11869
|
+
var KRONROD_WEIGHTS = [];
|
|
11870
|
+
var GAUSS_WEIGHTS = [];
|
|
11871
|
+
for (let i = 0; i < 7; i++) {
|
|
11872
|
+
KRONROD_NODES.push(-XGK[i]);
|
|
11873
|
+
KRONROD_WEIGHTS.push(WGK[i]);
|
|
11874
|
+
GAUSS_WEIGHTS.push(0);
|
|
11875
|
+
}
|
|
11876
|
+
KRONROD_NODES.push(0);
|
|
11877
|
+
KRONROD_WEIGHTS.push(WGK[7]);
|
|
11878
|
+
GAUSS_WEIGHTS.push(0);
|
|
11879
|
+
for (let i = 6; i >= 0; i--) {
|
|
11880
|
+
KRONROD_NODES.push(XGK[i]);
|
|
11881
|
+
KRONROD_WEIGHTS.push(WGK[i]);
|
|
11882
|
+
GAUSS_WEIGHTS.push(0);
|
|
11883
|
+
}
|
|
11884
|
+
GAUSS_WEIGHTS[1] = WG[0];
|
|
11885
|
+
GAUSS_WEIGHTS[3] = WG[1];
|
|
11886
|
+
GAUSS_WEIGHTS[5] = WG[2];
|
|
11887
|
+
GAUSS_WEIGHTS[7] = WG[3];
|
|
11888
|
+
GAUSS_WEIGHTS[9] = WG[2];
|
|
11889
|
+
GAUSS_WEIGHTS[11] = WG[1];
|
|
11890
|
+
GAUSS_WEIGHTS[13] = WG[0];
|
|
11891
|
+
function gaussKronrod15(f, a, b) {
|
|
11892
|
+
const halfWidth = (b - a) / 2;
|
|
11893
|
+
const mid = (a + b) / 2;
|
|
11894
|
+
let k = 0;
|
|
11895
|
+
let g = 0;
|
|
11896
|
+
for (let i = 0; i < 15; i++) {
|
|
11897
|
+
const fx = f(halfWidth * KRONROD_NODES[i] + mid);
|
|
11898
|
+
k += KRONROD_WEIGHTS[i] * fx;
|
|
11899
|
+
g += GAUSS_WEIGHTS[i] * fx;
|
|
11900
|
+
}
|
|
11901
|
+
return { k: halfWidth * k, g: halfWidth * g };
|
|
11902
|
+
}
|
|
11903
|
+
var ABS_FLOOR = 1e-14;
|
|
11904
|
+
function quad(f, a, b, opts) {
|
|
11905
|
+
const tol = opts?.tol ?? 1e-10;
|
|
11906
|
+
const maxDepth = opts?.maxDepth ?? 50;
|
|
11907
|
+
function panel(a2, b2, depth) {
|
|
11908
|
+
const { k, g } = gaussKronrod15(f, a2, b2);
|
|
11909
|
+
const panelError = Math.abs(k - g);
|
|
11910
|
+
if (depth >= maxDepth || panelError <= Math.max(tol * Math.abs(k), ABS_FLOOR)) {
|
|
11911
|
+
return { value: k, error: panelError };
|
|
11912
|
+
}
|
|
11913
|
+
const mid = (a2 + b2) / 2;
|
|
11914
|
+
const left = panel(a2, mid, depth + 1);
|
|
11915
|
+
const right = panel(mid, b2, depth + 1);
|
|
11916
|
+
return { value: left.value + right.value, error: left.error + right.error };
|
|
11917
|
+
}
|
|
11918
|
+
return panel(a, b, 0);
|
|
11919
|
+
}
|
|
11920
|
+
|
|
11846
11921
|
// src/typed/numeric.ts
|
|
11847
11922
|
var NUMERIC_WASM_THRESHOLD = 16;
|
|
11848
11923
|
function findRoot(f, a, b, opts) {
|
|
@@ -12072,37 +12147,7 @@ function leastSquares(A, b) {
|
|
|
12072
12147
|
return linsolve(AtA, Atb);
|
|
12073
12148
|
}
|
|
12074
12149
|
function nintegrate(f, a, b, opts) {
|
|
12075
|
-
|
|
12076
|
-
const maxDepth = opts?.maxDepth ?? 30;
|
|
12077
|
-
function adaptiveQuad(a2, b2, depth) {
|
|
12078
|
-
const mid = (a2 + b2) / 2;
|
|
12079
|
-
const whole = gaussLegendre5(f, a2, b2);
|
|
12080
|
-
const left = gaussLegendre5(f, a2, mid);
|
|
12081
|
-
const right = gaussLegendre5(f, mid, b2);
|
|
12082
|
-
const refined = left + right;
|
|
12083
|
-
if (depth >= maxDepth || Math.abs(refined - whole) < 15 * tol) {
|
|
12084
|
-
return refined + (refined - whole) / 15;
|
|
12085
|
-
}
|
|
12086
|
-
return adaptiveQuad(a2, mid, depth + 1) + adaptiveQuad(mid, b2, depth + 1);
|
|
12087
|
-
}
|
|
12088
|
-
return adaptiveQuad(a, b, 0);
|
|
12089
|
-
}
|
|
12090
|
-
function gaussLegendre5(f, a, b) {
|
|
12091
|
-
const nodes = [-0.906179845938664, -0.5384693101056831, 0, 0.5384693101056831, 0.906179845938664];
|
|
12092
|
-
const weights = [
|
|
12093
|
-
0.2369268850561891,
|
|
12094
|
-
0.4786286704993665,
|
|
12095
|
-
0.5688888888888889,
|
|
12096
|
-
0.4786286704993665,
|
|
12097
|
-
0.2369268850561891
|
|
12098
|
-
];
|
|
12099
|
-
const hw = (b - a) / 2;
|
|
12100
|
-
const mid = (a + b) / 2;
|
|
12101
|
-
let sum3 = 0;
|
|
12102
|
-
for (let i = 0; i < 5; i++) {
|
|
12103
|
-
sum3 += weights[i] * f(hw * nodes[i] + mid);
|
|
12104
|
-
}
|
|
12105
|
-
return hw * sum3;
|
|
12150
|
+
return quad(f, a, b, opts).value;
|
|
12106
12151
|
}
|
|
12107
12152
|
function simpsons(f, a, b, n = 100) {
|
|
12108
12153
|
if (n % 2 !== 0) n += 1;
|
|
@@ -13063,7 +13108,7 @@ function quadprog(H, f, A, b) {
|
|
|
13063
13108
|
}
|
|
13064
13109
|
return x;
|
|
13065
13110
|
}
|
|
13066
|
-
function
|
|
13111
|
+
function linprogOnePhaseLegacy(c, A, b) {
|
|
13067
13112
|
const m = A.length;
|
|
13068
13113
|
const n = c.length;
|
|
13069
13114
|
const tableau = [];
|
|
@@ -13134,6 +13179,285 @@ function linprog(c, A, b) {
|
|
|
13134
13179
|
}
|
|
13135
13180
|
return x;
|
|
13136
13181
|
}
|
|
13182
|
+
var LINPROG_TOL = 1e-9;
|
|
13183
|
+
function linprogSolveTwoPhase(costWork, Aeq, beq, hasSlack, slackCol) {
|
|
13184
|
+
const m = Aeq.length;
|
|
13185
|
+
const nw = costWork.length;
|
|
13186
|
+
if (m === 0) {
|
|
13187
|
+
for (let j = 0; j < nw; j++) {
|
|
13188
|
+
if (costWork[j] < -LINPROG_TOL) return { xWork: [], status: "unbounded" };
|
|
13189
|
+
}
|
|
13190
|
+
return { xWork: new Array(nw).fill(0), status: "optimal" };
|
|
13191
|
+
}
|
|
13192
|
+
const rows = [];
|
|
13193
|
+
const rhs = [];
|
|
13194
|
+
const flipped = [];
|
|
13195
|
+
for (let i2 = 0; i2 < m; i2++) {
|
|
13196
|
+
let row2 = [...Aeq[i2]];
|
|
13197
|
+
let b = beq[i2];
|
|
13198
|
+
let f = false;
|
|
13199
|
+
if (b < 0) {
|
|
13200
|
+
row2 = row2.map((v) => -v);
|
|
13201
|
+
b = -b;
|
|
13202
|
+
f = true;
|
|
13203
|
+
}
|
|
13204
|
+
rows.push(row2);
|
|
13205
|
+
rhs.push(b);
|
|
13206
|
+
flipped.push(f);
|
|
13207
|
+
}
|
|
13208
|
+
const basis = new Array(m).fill(-1);
|
|
13209
|
+
const artificialCols = [];
|
|
13210
|
+
let nextCol = nw;
|
|
13211
|
+
for (let i2 = 0; i2 < m; i2++) {
|
|
13212
|
+
if (hasSlack[i2] && !flipped[i2]) {
|
|
13213
|
+
basis[i2] = slackCol[i2];
|
|
13214
|
+
} else {
|
|
13215
|
+
const col = nextCol++;
|
|
13216
|
+
artificialCols.push(col);
|
|
13217
|
+
basis[i2] = col;
|
|
13218
|
+
}
|
|
13219
|
+
}
|
|
13220
|
+
const totalCols = nextCol;
|
|
13221
|
+
const isArtificial = new Array(totalCols).fill(false);
|
|
13222
|
+
for (const col of artificialCols) isArtificial[col] = true;
|
|
13223
|
+
const width = totalCols + 1;
|
|
13224
|
+
const tableau = [];
|
|
13225
|
+
for (let i2 = 0; i2 < m; i2++) {
|
|
13226
|
+
const row2 = new Array(width).fill(0);
|
|
13227
|
+
for (let j = 0; j < nw; j++) row2[j] = rows[i2][j];
|
|
13228
|
+
if (isArtificial[basis[i2]]) row2[basis[i2]] = 1;
|
|
13229
|
+
row2[totalCols] = rhs[i2];
|
|
13230
|
+
tableau.push(row2);
|
|
13231
|
+
}
|
|
13232
|
+
const pivot = (pr, pc, objRow) => {
|
|
13233
|
+
const pivotVal = tableau[pr][pc];
|
|
13234
|
+
for (let j = 0; j < width; j++) tableau[pr][j] /= pivotVal;
|
|
13235
|
+
for (let i2 = 0; i2 < tableau.length; i2++) {
|
|
13236
|
+
if (i2 === pr) continue;
|
|
13237
|
+
const factor3 = tableau[i2][pc];
|
|
13238
|
+
if (factor3 === 0) continue;
|
|
13239
|
+
for (let j = 0; j < width; j++) tableau[i2][j] -= factor3 * tableau[pr][j];
|
|
13240
|
+
}
|
|
13241
|
+
const factor2 = objRow[pc];
|
|
13242
|
+
if (factor2 !== 0) {
|
|
13243
|
+
for (let j = 0; j < width; j++) objRow[j] -= factor2 * tableau[pr][j];
|
|
13244
|
+
}
|
|
13245
|
+
};
|
|
13246
|
+
const buildObjRow = (costFull) => {
|
|
13247
|
+
const row2 = new Array(width).fill(0);
|
|
13248
|
+
for (let j = 0; j < totalCols; j++) row2[j] = costFull[j];
|
|
13249
|
+
for (let i2 = 0; i2 < tableau.length; i2++) {
|
|
13250
|
+
const cb = costFull[basis[i2]];
|
|
13251
|
+
if (cb === 0) continue;
|
|
13252
|
+
for (let j = 0; j < width; j++) row2[j] -= cb * tableau[i2][j];
|
|
13253
|
+
}
|
|
13254
|
+
return row2;
|
|
13255
|
+
};
|
|
13256
|
+
const runSimplex = (objRow, excluded, maxIter = 2e3) => {
|
|
13257
|
+
for (let iter = 0; iter < maxIter; iter++) {
|
|
13258
|
+
let pivotCol = -1;
|
|
13259
|
+
let minVal = -LINPROG_TOL;
|
|
13260
|
+
for (let j = 0; j < totalCols; j++) {
|
|
13261
|
+
if (excluded[j]) continue;
|
|
13262
|
+
if (objRow[j] < minVal) {
|
|
13263
|
+
minVal = objRow[j];
|
|
13264
|
+
pivotCol = j;
|
|
13265
|
+
}
|
|
13266
|
+
}
|
|
13267
|
+
if (pivotCol === -1) return "optimal";
|
|
13268
|
+
let pivotRow = -1;
|
|
13269
|
+
let minRatio = Infinity;
|
|
13270
|
+
for (let i2 = 0; i2 < tableau.length; i2++) {
|
|
13271
|
+
if (tableau[i2][pivotCol] > LINPROG_TOL) {
|
|
13272
|
+
const ratio = tableau[i2][totalCols] / tableau[i2][pivotCol];
|
|
13273
|
+
if (ratio < minRatio - 1e-12) {
|
|
13274
|
+
minRatio = ratio;
|
|
13275
|
+
pivotRow = i2;
|
|
13276
|
+
}
|
|
13277
|
+
}
|
|
13278
|
+
}
|
|
13279
|
+
if (pivotRow === -1) return "unbounded";
|
|
13280
|
+
basis[pivotRow] = pivotCol;
|
|
13281
|
+
pivot(pivotRow, pivotCol, objRow);
|
|
13282
|
+
}
|
|
13283
|
+
return "optimal";
|
|
13284
|
+
};
|
|
13285
|
+
const noExclusions = new Array(totalCols).fill(false);
|
|
13286
|
+
const cost1 = new Array(totalCols).fill(0);
|
|
13287
|
+
for (const col of artificialCols) cost1[col] = 1;
|
|
13288
|
+
const objRow1 = buildObjRow(cost1);
|
|
13289
|
+
const phase1Status = runSimplex(objRow1, noExclusions);
|
|
13290
|
+
if (phase1Status === "unbounded") return { xWork: [], status: "infeasible" };
|
|
13291
|
+
let phase1Obj = 0;
|
|
13292
|
+
for (let i2 = 0; i2 < tableau.length; i2++) phase1Obj += cost1[basis[i2]] * tableau[i2][totalCols];
|
|
13293
|
+
if (phase1Obj > 1e-7) return { xWork: [], status: "infeasible" };
|
|
13294
|
+
let i = 0;
|
|
13295
|
+
while (i < tableau.length) {
|
|
13296
|
+
if (!isArtificial[basis[i]]) {
|
|
13297
|
+
i++;
|
|
13298
|
+
continue;
|
|
13299
|
+
}
|
|
13300
|
+
let pc = -1;
|
|
13301
|
+
for (let j = 0; j < nw; j++) {
|
|
13302
|
+
if (Math.abs(tableau[i][j]) > 1e-8) {
|
|
13303
|
+
pc = j;
|
|
13304
|
+
break;
|
|
13305
|
+
}
|
|
13306
|
+
}
|
|
13307
|
+
if (pc === -1) {
|
|
13308
|
+
tableau.splice(i, 1);
|
|
13309
|
+
basis.splice(i, 1);
|
|
13310
|
+
continue;
|
|
13311
|
+
}
|
|
13312
|
+
basis[i] = pc;
|
|
13313
|
+
pivot(i, pc, new Array(width).fill(0));
|
|
13314
|
+
i++;
|
|
13315
|
+
}
|
|
13316
|
+
const costFull2 = new Array(totalCols).fill(0);
|
|
13317
|
+
for (let j = 0; j < nw; j++) costFull2[j] = costWork[j];
|
|
13318
|
+
const objRow2 = buildObjRow(costFull2);
|
|
13319
|
+
const excludeArtificial = isArtificial.slice();
|
|
13320
|
+
const phase2Status = runSimplex(objRow2, excludeArtificial);
|
|
13321
|
+
if (phase2Status === "unbounded") return { xWork: [], status: "unbounded" };
|
|
13322
|
+
const xWork = new Array(nw).fill(0);
|
|
13323
|
+
for (let r = 0; r < tableau.length; r++) {
|
|
13324
|
+
if (basis[r] < nw) xWork[basis[r]] = Math.max(0, tableau[r][totalCols]);
|
|
13325
|
+
}
|
|
13326
|
+
return { xWork, status: "optimal" };
|
|
13327
|
+
}
|
|
13328
|
+
function linprogTwoPhase(c, opts) {
|
|
13329
|
+
const n = c.length;
|
|
13330
|
+
const boundsResolved = [];
|
|
13331
|
+
for (let j = 0; j < n; j++) {
|
|
13332
|
+
const b = opts.bounds?.[j];
|
|
13333
|
+
boundsResolved.push(b ? [b[0], b[1]] : [0, null]);
|
|
13334
|
+
}
|
|
13335
|
+
let col = 0;
|
|
13336
|
+
const expansions = [];
|
|
13337
|
+
for (let j = 0; j < n; j++) {
|
|
13338
|
+
const low = boundsResolved[j][0];
|
|
13339
|
+
if (low === null) {
|
|
13340
|
+
const colPos = col++;
|
|
13341
|
+
const colNeg = col++;
|
|
13342
|
+
expansions.push({ kind: "split", colPos, colNeg });
|
|
13343
|
+
} else {
|
|
13344
|
+
const c0 = col++;
|
|
13345
|
+
expansions.push({ kind: "shift", col: c0, shift: low });
|
|
13346
|
+
}
|
|
13347
|
+
}
|
|
13348
|
+
const nx = col;
|
|
13349
|
+
const expandRow = (row2) => {
|
|
13350
|
+
const out = new Array(nx).fill(0);
|
|
13351
|
+
for (let j = 0; j < n; j++) {
|
|
13352
|
+
const coeff = row2[j] ?? 0;
|
|
13353
|
+
if (coeff === 0) continue;
|
|
13354
|
+
const e = expansions[j];
|
|
13355
|
+
if (e.kind === "shift") out[e.col] += coeff;
|
|
13356
|
+
else {
|
|
13357
|
+
out[e.colPos] += coeff;
|
|
13358
|
+
out[e.colNeg] -= coeff;
|
|
13359
|
+
}
|
|
13360
|
+
}
|
|
13361
|
+
return out;
|
|
13362
|
+
};
|
|
13363
|
+
const shiftedRhs = (row2, rhsVal) => {
|
|
13364
|
+
let r = rhsVal;
|
|
13365
|
+
for (let j = 0; j < n; j++) {
|
|
13366
|
+
const e = expansions[j];
|
|
13367
|
+
if (e.kind === "shift" && e.shift !== 0) r -= (row2[j] ?? 0) * e.shift;
|
|
13368
|
+
}
|
|
13369
|
+
return r;
|
|
13370
|
+
};
|
|
13371
|
+
let constant = 0;
|
|
13372
|
+
const cWork = new Array(nx).fill(0);
|
|
13373
|
+
for (let j = 0; j < n; j++) {
|
|
13374
|
+
const e = expansions[j];
|
|
13375
|
+
if (e.kind === "shift") {
|
|
13376
|
+
cWork[e.col] = c[j];
|
|
13377
|
+
constant += c[j] * e.shift;
|
|
13378
|
+
} else {
|
|
13379
|
+
cWork[e.colPos] = c[j];
|
|
13380
|
+
cWork[e.colNeg] = -c[j];
|
|
13381
|
+
}
|
|
13382
|
+
}
|
|
13383
|
+
const ubRows = [];
|
|
13384
|
+
const ubRhs = [];
|
|
13385
|
+
const A_ub = opts.A_ub ?? [];
|
|
13386
|
+
const b_ub = opts.b_ub ?? [];
|
|
13387
|
+
for (let i = 0; i < A_ub.length; i++) {
|
|
13388
|
+
ubRows.push(expandRow(A_ub[i]));
|
|
13389
|
+
ubRhs.push(shiftedRhs(A_ub[i], b_ub[i]));
|
|
13390
|
+
}
|
|
13391
|
+
for (let j = 0; j < n; j++) {
|
|
13392
|
+
const high = boundsResolved[j][1];
|
|
13393
|
+
if (high === null) continue;
|
|
13394
|
+
const e = expansions[j];
|
|
13395
|
+
const row2 = new Array(nx).fill(0);
|
|
13396
|
+
let rhsVal;
|
|
13397
|
+
if (e.kind === "shift") {
|
|
13398
|
+
row2[e.col] = 1;
|
|
13399
|
+
rhsVal = high - e.shift;
|
|
13400
|
+
} else {
|
|
13401
|
+
row2[e.colPos] = 1;
|
|
13402
|
+
row2[e.colNeg] = -1;
|
|
13403
|
+
rhsVal = high;
|
|
13404
|
+
}
|
|
13405
|
+
ubRows.push(row2);
|
|
13406
|
+
ubRhs.push(rhsVal);
|
|
13407
|
+
}
|
|
13408
|
+
const A_eq = opts.A_eq ?? [];
|
|
13409
|
+
const b_eq = opts.b_eq ?? [];
|
|
13410
|
+
const eqRows = [];
|
|
13411
|
+
const eqRhs = [];
|
|
13412
|
+
for (let i = 0; i < A_eq.length; i++) {
|
|
13413
|
+
eqRows.push(expandRow(A_eq[i]));
|
|
13414
|
+
eqRhs.push(shiftedRhs(A_eq[i], b_eq[i]));
|
|
13415
|
+
}
|
|
13416
|
+
const mUb = ubRows.length;
|
|
13417
|
+
const mEq = eqRows.length;
|
|
13418
|
+
const nw = nx + mUb;
|
|
13419
|
+
const Aeq = [];
|
|
13420
|
+
const beq = [];
|
|
13421
|
+
const hasSlack = [];
|
|
13422
|
+
const slackCol = [];
|
|
13423
|
+
for (let i = 0; i < mUb; i++) {
|
|
13424
|
+
const row2 = new Array(nw).fill(0);
|
|
13425
|
+
for (let j = 0; j < nx; j++) row2[j] = ubRows[i][j];
|
|
13426
|
+
row2[nx + i] = 1;
|
|
13427
|
+
Aeq.push(row2);
|
|
13428
|
+
beq.push(ubRhs[i]);
|
|
13429
|
+
hasSlack.push(true);
|
|
13430
|
+
slackCol.push(nx + i);
|
|
13431
|
+
}
|
|
13432
|
+
for (let i = 0; i < mEq; i++) {
|
|
13433
|
+
const row2 = new Array(nw).fill(0);
|
|
13434
|
+
for (let j = 0; j < nx; j++) row2[j] = eqRows[i][j];
|
|
13435
|
+
Aeq.push(row2);
|
|
13436
|
+
beq.push(eqRhs[i]);
|
|
13437
|
+
hasSlack.push(false);
|
|
13438
|
+
slackCol.push(-1);
|
|
13439
|
+
}
|
|
13440
|
+
const costWorkFull = new Array(nw).fill(0);
|
|
13441
|
+
for (let j = 0; j < nx; j++) costWorkFull[j] = cWork[j];
|
|
13442
|
+
const raw = linprogSolveTwoPhase(costWorkFull, Aeq, beq, hasSlack, slackCol);
|
|
13443
|
+
if (raw.status !== "optimal") {
|
|
13444
|
+
return { x: new Array(n).fill(NaN), fun: NaN, success: false, status: raw.status };
|
|
13445
|
+
}
|
|
13446
|
+
const x = new Array(n).fill(0);
|
|
13447
|
+
for (let j = 0; j < n; j++) {
|
|
13448
|
+
const e = expansions[j];
|
|
13449
|
+
x[j] = e.kind === "shift" ? raw.xWork[e.col] + e.shift : raw.xWork[e.colPos] - raw.xWork[e.colNeg];
|
|
13450
|
+
}
|
|
13451
|
+
let fun = constant;
|
|
13452
|
+
for (let j = 0; j < nx; j++) fun += cWork[j] * raw.xWork[j];
|
|
13453
|
+
return { x, fun, success: true, status: "optimal" };
|
|
13454
|
+
}
|
|
13455
|
+
function linprog(c, arg22, arg3) {
|
|
13456
|
+
if (Array.isArray(arg22)) {
|
|
13457
|
+
return linprogOnePhaseLegacy(c, arg22, arg3);
|
|
13458
|
+
}
|
|
13459
|
+
return linprogTwoPhase(c, arg22);
|
|
13460
|
+
}
|
|
13137
13461
|
function solvePDE(pde, domain, bc) {
|
|
13138
13462
|
const { alpha } = pde;
|
|
13139
13463
|
const { L, nx, nt, T } = domain;
|
|
@@ -14073,9 +14397,9 @@ function _aggregateEigenvector(vectors, aggregation) {
|
|
|
14073
14397
|
const count2 = vectors.length;
|
|
14074
14398
|
const ref = vectors[0];
|
|
14075
14399
|
const aligned = vectors.map((v) => {
|
|
14076
|
-
let
|
|
14077
|
-
for (let i = 0; i < n; i++)
|
|
14078
|
-
if (
|
|
14400
|
+
let dot4 = 0;
|
|
14401
|
+
for (let i = 0; i < n; i++) dot4 += v[i] * ref[i];
|
|
14402
|
+
if (dot4 < 0) {
|
|
14079
14403
|
const flipped = new Float64Array(n);
|
|
14080
14404
|
for (let i = 0; i < n; i++) flipped[i] = -v[i];
|
|
14081
14405
|
return flipped;
|
|
@@ -15137,9 +15461,9 @@ function multivariateNormal(mean6, cov2) {
|
|
|
15137
15461
|
for (let j = 0; j < i; j++) s -= L[i][j] * z[j];
|
|
15138
15462
|
z[i] = s / L[i][i];
|
|
15139
15463
|
}
|
|
15140
|
-
let
|
|
15141
|
-
for (let i = 0; i < k; i++)
|
|
15142
|
-
return Math.exp(-0.5 * (k * Math.log(2 * Math.PI) + logDet +
|
|
15464
|
+
let quad2 = 0;
|
|
15465
|
+
for (let i = 0; i < k; i++) quad2 += z[i] * z[i];
|
|
15466
|
+
return Math.exp(-0.5 * (k * Math.log(2 * Math.PI) + logDet + quad2));
|
|
15143
15467
|
}
|
|
15144
15468
|
};
|
|
15145
15469
|
}
|
|
@@ -15926,9 +16250,9 @@ function principalComponentAnalysis(data, k) {
|
|
|
15926
16250
|
const explained = eigenvalues.map((ev) => totalVar > 0 ? Math.abs(ev) / totalVar : 0);
|
|
15927
16251
|
const scores = centered.map((row2) => {
|
|
15928
16252
|
return eigenvectors.map((ev) => {
|
|
15929
|
-
let
|
|
15930
|
-
for (let j = 0; j < p; j++)
|
|
15931
|
-
return
|
|
16253
|
+
let dot4 = 0;
|
|
16254
|
+
for (let j = 0; j < p; j++) dot4 += row2[j] * ev[j];
|
|
16255
|
+
return dot4;
|
|
15932
16256
|
});
|
|
15933
16257
|
});
|
|
15934
16258
|
return { components: eigenvectors, explained, scores };
|
|
@@ -16539,30 +16863,30 @@ function hessenbergForm(A) {
|
|
|
16539
16863
|
if (normV < 1e-30) continue;
|
|
16540
16864
|
const beta2 = 2 / normV;
|
|
16541
16865
|
for (let j = k; j < n; j++) {
|
|
16542
|
-
let
|
|
16866
|
+
let dot4 = 0;
|
|
16543
16867
|
for (let i = 0; i < len; i++) {
|
|
16544
|
-
|
|
16868
|
+
dot4 += v[i] * H[k + 1 + i][j];
|
|
16545
16869
|
}
|
|
16546
16870
|
for (let i = 0; i < len; i++) {
|
|
16547
|
-
H[k + 1 + i][j] -= beta2 * v[i] *
|
|
16871
|
+
H[k + 1 + i][j] -= beta2 * v[i] * dot4;
|
|
16548
16872
|
}
|
|
16549
16873
|
}
|
|
16550
16874
|
for (let i = 0; i < n; i++) {
|
|
16551
|
-
let
|
|
16875
|
+
let dot4 = 0;
|
|
16552
16876
|
for (let j = 0; j < len; j++) {
|
|
16553
|
-
|
|
16877
|
+
dot4 += H[i][k + 1 + j] * v[j];
|
|
16554
16878
|
}
|
|
16555
16879
|
for (let j = 0; j < len; j++) {
|
|
16556
|
-
H[i][k + 1 + j] -= beta2 *
|
|
16880
|
+
H[i][k + 1 + j] -= beta2 * dot4 * v[j];
|
|
16557
16881
|
}
|
|
16558
16882
|
}
|
|
16559
16883
|
for (let i = 0; i < n; i++) {
|
|
16560
|
-
let
|
|
16884
|
+
let dot4 = 0;
|
|
16561
16885
|
for (let j = 0; j < len; j++) {
|
|
16562
|
-
|
|
16886
|
+
dot4 += Q2[i][k + 1 + j] * v[j];
|
|
16563
16887
|
}
|
|
16564
16888
|
for (let j = 0; j < len; j++) {
|
|
16565
|
-
Q2[i][k + 1 + j] -= beta2 *
|
|
16889
|
+
Q2[i][k + 1 + j] -= beta2 * dot4 * v[j];
|
|
16566
16890
|
}
|
|
16567
16891
|
}
|
|
16568
16892
|
}
|
|
@@ -25038,10 +25362,10 @@ var createNthRoots = /* @__PURE__ */ factory(
|
|
|
25038
25362
|
return new Complex12(0, -val);
|
|
25039
25363
|
}
|
|
25040
25364
|
];
|
|
25041
|
-
function _nthComplexRoots(a,
|
|
25042
|
-
if (
|
|
25043
|
-
if (
|
|
25044
|
-
if (
|
|
25365
|
+
function _nthComplexRoots(a, root2) {
|
|
25366
|
+
if (root2 < 0) throw new Error("Root must be greater than zero");
|
|
25367
|
+
if (root2 === 0) throw new Error("Root must be non-zero");
|
|
25368
|
+
if (root2 % 1 !== 0) throw new Error("Root must be an integer");
|
|
25045
25369
|
if (a === 0 || a.abs() === 0) return [new Complex12(0, 0)];
|
|
25046
25370
|
const aIsNumeric = typeof a === "number";
|
|
25047
25371
|
let offset = NaN;
|
|
@@ -25057,14 +25381,14 @@ var createNthRoots = /* @__PURE__ */ factory(
|
|
|
25057
25381
|
const arg3 = a.arg();
|
|
25058
25382
|
const abs2 = a.abs();
|
|
25059
25383
|
const roots = [];
|
|
25060
|
-
const r = Math.pow(abs2, 1 /
|
|
25061
|
-
for (let k = 0; k <
|
|
25062
|
-
const halfPiFactor = (offset + 4 * k) /
|
|
25384
|
+
const r = Math.pow(abs2, 1 / root2);
|
|
25385
|
+
for (let k = 0; k < root2; k++) {
|
|
25386
|
+
const halfPiFactor = (offset + 4 * k) / root2;
|
|
25063
25387
|
if (halfPiFactor === Math.round(halfPiFactor)) {
|
|
25064
25388
|
roots.push(_calculateExactResult[halfPiFactor % 4](r));
|
|
25065
25389
|
continue;
|
|
25066
25390
|
}
|
|
25067
|
-
roots.push(new Complex12({ r, phi: (arg3 + 2 * Math.PI * k) /
|
|
25391
|
+
roots.push(new Complex12({ r, phi: (arg3 + 2 * Math.PI * k) / root2 }));
|
|
25068
25392
|
}
|
|
25069
25393
|
return roots;
|
|
25070
25394
|
}
|
|
@@ -28117,19 +28441,19 @@ var createNthRoot = /* @__PURE__ */ factory(
|
|
|
28117
28441
|
sS: false
|
|
28118
28442
|
})
|
|
28119
28443
|
);
|
|
28120
|
-
function _bigNthRoot(a,
|
|
28444
|
+
function _bigNthRoot(a, root2) {
|
|
28121
28445
|
const precision = BigNumber9.precision;
|
|
28122
28446
|
const Big = BigNumber9.clone({ precision: precision + 2 });
|
|
28123
28447
|
const zero = new BigNumber9(0);
|
|
28124
28448
|
const one = new Big(1);
|
|
28125
|
-
const inv2 =
|
|
28449
|
+
const inv2 = root2.isNegative();
|
|
28126
28450
|
if (inv2) {
|
|
28127
|
-
|
|
28451
|
+
root2 = root2.neg();
|
|
28128
28452
|
}
|
|
28129
|
-
if (
|
|
28453
|
+
if (root2.isZero()) {
|
|
28130
28454
|
throw new Error("Root must be non-zero");
|
|
28131
28455
|
}
|
|
28132
|
-
if (a.isNegative() && !
|
|
28456
|
+
if (a.isNegative() && !root2.abs().mod(2).equals(1)) {
|
|
28133
28457
|
throw new Error("Root must be odd when a is negative.");
|
|
28134
28458
|
}
|
|
28135
28459
|
if (a.isZero()) {
|
|
@@ -28138,7 +28462,7 @@ var createNthRoot = /* @__PURE__ */ factory(
|
|
|
28138
28462
|
if (!a.isFinite()) {
|
|
28139
28463
|
return inv2 ? zero : a;
|
|
28140
28464
|
}
|
|
28141
|
-
let x = a.abs().pow(one.div(
|
|
28465
|
+
let x = a.abs().pow(one.div(root2));
|
|
28142
28466
|
x = a.isNeg() ? x.neg() : x;
|
|
28143
28467
|
return new BigNumber9((inv2 ? one.div(x) : x).toPrecision(precision));
|
|
28144
28468
|
}
|
|
@@ -29874,7 +30198,7 @@ var createPinv = /* @__PURE__ */ factory(
|
|
|
29874
30198
|
deepEqual: deepEqual3,
|
|
29875
30199
|
equal: equal2,
|
|
29876
30200
|
dotDivide: dotDivide2,
|
|
29877
|
-
dot:
|
|
30201
|
+
dot: dot4,
|
|
29878
30202
|
ctranspose: ctranspose2,
|
|
29879
30203
|
divideScalar: divideScalar2,
|
|
29880
30204
|
multiply: multiply2,
|
|
@@ -29890,7 +30214,7 @@ var createPinv = /* @__PURE__ */ factory(
|
|
|
29890
30214
|
if (size2[0] === 1) {
|
|
29891
30215
|
return inv2(x);
|
|
29892
30216
|
} else {
|
|
29893
|
-
return dotDivide2(ctranspose2(x),
|
|
30217
|
+
return dotDivide2(ctranspose2(x), dot4(x, x));
|
|
29894
30218
|
}
|
|
29895
30219
|
case 2: {
|
|
29896
30220
|
if (_isZeros(x)) return ctranspose2(x);
|
|
@@ -29969,8 +30293,8 @@ var createPinv = /* @__PURE__ */ factory(
|
|
|
29969
30293
|
}
|
|
29970
30294
|
function _rankFact(mat, rows, cols) {
|
|
29971
30295
|
const rref = _rref(mat, rows, cols);
|
|
29972
|
-
const C = mat.map((row2) => row2.filter((_, j) => j < rows && !_isZero(
|
|
29973
|
-
const F = rref.filter((_, i) => !_isZero(
|
|
30296
|
+
const C = mat.map((row2) => row2.filter((_, j) => j < rows && !_isZero(dot4(rref[j], rref[j]))));
|
|
30297
|
+
const F = rref.filter((_, i) => !_isZero(dot4(rref[i], rref[i])));
|
|
29974
30298
|
return { C, F };
|
|
29975
30299
|
}
|
|
29976
30300
|
function _isZero(x) {
|
|
@@ -36933,7 +37257,7 @@ function createComplexEigs({
|
|
|
36933
37257
|
larger: larger2,
|
|
36934
37258
|
smaller: smaller2,
|
|
36935
37259
|
matrixFromColumns: _matrixFromColumns,
|
|
36936
|
-
dot:
|
|
37260
|
+
dot: dot4
|
|
36937
37261
|
}) {
|
|
36938
37262
|
function complexEigs(arr7, N, prec, type, findVectors = true) {
|
|
36939
37263
|
if (!findVectors && type === "number" && N * N >= WASM_EIGS_THRESHOLD) {
|
|
@@ -37374,7 +37698,7 @@ function createComplexEigs({
|
|
|
37374
37698
|
v = subtract2(
|
|
37375
37699
|
v,
|
|
37376
37700
|
multiply2(
|
|
37377
|
-
divideScalar2(
|
|
37701
|
+
divideScalar2(dot4(w, v), dot4(w, w)),
|
|
37378
37702
|
w
|
|
37379
37703
|
)
|
|
37380
37704
|
);
|
|
@@ -37382,7 +37706,7 @@ function createComplexEigs({
|
|
|
37382
37706
|
return v;
|
|
37383
37707
|
}
|
|
37384
37708
|
function norm4(v) {
|
|
37385
|
-
return abs2(sqrt2(
|
|
37709
|
+
return abs2(sqrt2(dot4(v, v)));
|
|
37386
37710
|
}
|
|
37387
37711
|
function normalize2(v, type) {
|
|
37388
37712
|
const big = type === "BigNumber";
|
|
@@ -37802,7 +38126,7 @@ var createEigs = /* @__PURE__ */ factory(
|
|
|
37802
38126
|
re: re3,
|
|
37803
38127
|
smaller: smaller2,
|
|
37804
38128
|
matrixFromColumns: matrixFromColumns2,
|
|
37805
|
-
dot:
|
|
38129
|
+
dot: dot4
|
|
37806
38130
|
}) => {
|
|
37807
38131
|
const doRealSymmetric = createRealSymmetric({
|
|
37808
38132
|
config: config2,
|
|
@@ -37841,7 +38165,7 @@ var createEigs = /* @__PURE__ */ factory(
|
|
|
37841
38165
|
larger: larger2,
|
|
37842
38166
|
smaller: smaller2,
|
|
37843
38167
|
matrixFromColumns: matrixFromColumns2,
|
|
37844
|
-
dot:
|
|
38168
|
+
dot: dot4
|
|
37845
38169
|
});
|
|
37846
38170
|
return typed3("eigs", {
|
|
37847
38171
|
// The conversion to matrix in the first two implementations,
|
|
@@ -42149,6 +42473,31 @@ function reviver(_key, value) {
|
|
|
42149
42473
|
// src/typed/cas.ts
|
|
42150
42474
|
import { computePool as computePool12 } from "@danielsimonjr/mathts-parallel";
|
|
42151
42475
|
import { Complex as Complex10 } from "@danielsimonjr/mathts-core";
|
|
42476
|
+
|
|
42477
|
+
// src/numeric/numeric-jacobian.ts
|
|
42478
|
+
function numericJacobian(f, x0, opts = {}) {
|
|
42479
|
+
const n = x0.length;
|
|
42480
|
+
const base = Array.from(x0);
|
|
42481
|
+
const f0 = f(base);
|
|
42482
|
+
const m = f0.length;
|
|
42483
|
+
const cbrtEps = Math.cbrt(222e-18);
|
|
42484
|
+
const J = Array.from({ length: m }, () => new Array(n).fill(0));
|
|
42485
|
+
for (let j = 0; j < n; j++) {
|
|
42486
|
+
const h = opts.h ?? Math.max(1, Math.abs(base[j])) * cbrtEps;
|
|
42487
|
+
const xPlus = base.slice();
|
|
42488
|
+
xPlus[j] += h;
|
|
42489
|
+
const fPlus = f(xPlus);
|
|
42490
|
+
const xMinus = base.slice();
|
|
42491
|
+
xMinus[j] -= h;
|
|
42492
|
+
const fMinus = f(xMinus);
|
|
42493
|
+
for (let i = 0; i < m; i++) {
|
|
42494
|
+
J[i][j] = (fPlus[i] - fMinus[i]) / (2 * h);
|
|
42495
|
+
}
|
|
42496
|
+
}
|
|
42497
|
+
return J;
|
|
42498
|
+
}
|
|
42499
|
+
|
|
42500
|
+
// src/typed/cas.ts
|
|
42152
42501
|
function _evalNumeric(exprStr) {
|
|
42153
42502
|
let pos = 0;
|
|
42154
42503
|
const s = exprStr.replace(/\s+/g, "");
|
|
@@ -42409,6 +42758,9 @@ function gradientSymbolic(expr, vars, scope) {
|
|
|
42409
42758
|
return vars.map((v) => partialDerivative(expr, v, scope));
|
|
42410
42759
|
}
|
|
42411
42760
|
function jacobian(exprs, vars, scope) {
|
|
42761
|
+
if (typeof exprs === "function") {
|
|
42762
|
+
return numericJacobian(exprs, vars);
|
|
42763
|
+
}
|
|
42412
42764
|
return exprs.map((expr) => vars.map((v) => partialDerivative(expr, v, scope)));
|
|
42413
42765
|
}
|
|
42414
42766
|
function laplacian(expr, vars, scope) {
|
|
@@ -43149,8 +43501,8 @@ function toRadicals(expr) {
|
|
|
43149
43501
|
const a_lin = f1 - f0;
|
|
43150
43502
|
if (Math.abs(a_lin * 2 + b_lin - f2) < 1e-8 && Math.abs(-a_lin + b_lin - fm1) < 1e-8) {
|
|
43151
43503
|
if (Math.abs(a_lin) < 1e-12) return [];
|
|
43152
|
-
const
|
|
43153
|
-
return [formatCoeff(
|
|
43504
|
+
const root2 = -b_lin / a_lin;
|
|
43505
|
+
return [formatCoeff(root2)];
|
|
43154
43506
|
}
|
|
43155
43507
|
const c = f0;
|
|
43156
43508
|
const a_plus_b = f1 - c;
|
|
@@ -44789,6 +45141,219 @@ function hessian(f, x, h = 1e-4) {
|
|
|
44789
45141
|
return H;
|
|
44790
45142
|
}
|
|
44791
45143
|
|
|
45144
|
+
// src/index.ts
|
|
45145
|
+
import { svd as svd3 } from "@danielsimonjr/mathts-matrix";
|
|
45146
|
+
|
|
45147
|
+
// src/linalg-svd-extra.ts
|
|
45148
|
+
import { svd as svd2 } from "@danielsimonjr/mathts-matrix";
|
|
45149
|
+
function orth(A, opts = {}) {
|
|
45150
|
+
const { U, S } = svd2(A);
|
|
45151
|
+
const m = A.length;
|
|
45152
|
+
const n = A[0]?.length ?? 0;
|
|
45153
|
+
const s0 = S[0] ?? 0;
|
|
45154
|
+
const tol = opts.tol ?? Math.max(m, n) * s0 * 222e-18;
|
|
45155
|
+
const rank2 = S.filter((s) => s > tol).length;
|
|
45156
|
+
return U.map((row2) => row2.slice(0, rank2));
|
|
45157
|
+
}
|
|
45158
|
+
|
|
45159
|
+
// src/numeric/open-root-finders.ts
|
|
45160
|
+
var EPS_CBRT = Math.cbrt(2220446049250313e-31);
|
|
45161
|
+
function centralDiff1(f, x) {
|
|
45162
|
+
const h = Math.max(1, Math.abs(x)) * EPS_CBRT;
|
|
45163
|
+
return (f(x + h) - f(x - h)) / (2 * h);
|
|
45164
|
+
}
|
|
45165
|
+
function centralDiff2(f, x) {
|
|
45166
|
+
const h = Math.max(1, Math.abs(x)) * EPS_CBRT;
|
|
45167
|
+
return (f(x + h) - 2 * f(x) + f(x - h)) / (h * h);
|
|
45168
|
+
}
|
|
45169
|
+
function newton(f, x0, opts) {
|
|
45170
|
+
const fprime = opts?.fprime;
|
|
45171
|
+
const tol = opts?.tol ?? 1e-12;
|
|
45172
|
+
const maxIter = opts?.maxIter ?? 100;
|
|
45173
|
+
let x = x0;
|
|
45174
|
+
for (let iter = 0; iter < maxIter; iter++) {
|
|
45175
|
+
const fx = f(x);
|
|
45176
|
+
if (Math.abs(fx) < tol) return x;
|
|
45177
|
+
const dfx = fprime ? fprime(x) : centralDiff1(f, x);
|
|
45178
|
+
if (Math.abs(dfx) < 1e-300) {
|
|
45179
|
+
throw new Error("newton: derivative near zero \u2014 cannot continue");
|
|
45180
|
+
}
|
|
45181
|
+
const dx = fx / dfx;
|
|
45182
|
+
const xNext = x - dx;
|
|
45183
|
+
if (Math.abs(dx) < tol) return xNext;
|
|
45184
|
+
x = xNext;
|
|
45185
|
+
}
|
|
45186
|
+
throw new Error(`newton: failed to converge within ${maxIter} iterations`);
|
|
45187
|
+
}
|
|
45188
|
+
function secant(f, x0, x1, opts) {
|
|
45189
|
+
const tol = opts?.tol ?? 1e-12;
|
|
45190
|
+
const maxIter = opts?.maxIter ?? 100;
|
|
45191
|
+
let xPrev = x0;
|
|
45192
|
+
let x = x1;
|
|
45193
|
+
let fPrev = f(xPrev);
|
|
45194
|
+
for (let iter = 0; iter < maxIter; iter++) {
|
|
45195
|
+
const fx = f(x);
|
|
45196
|
+
if (Math.abs(fx) < tol) return x;
|
|
45197
|
+
const denom = fx - fPrev;
|
|
45198
|
+
if (Math.abs(denom) < 1e-300) {
|
|
45199
|
+
throw new Error("secant: f(x_k) - f(x_{k-1}) near zero \u2014 cannot continue");
|
|
45200
|
+
}
|
|
45201
|
+
const dx = fx * (x - xPrev) / denom;
|
|
45202
|
+
const xNext = x - dx;
|
|
45203
|
+
if (Math.abs(dx) < tol) return xNext;
|
|
45204
|
+
xPrev = x;
|
|
45205
|
+
fPrev = fx;
|
|
45206
|
+
x = xNext;
|
|
45207
|
+
}
|
|
45208
|
+
throw new Error(`secant: failed to converge within ${maxIter} iterations`);
|
|
45209
|
+
}
|
|
45210
|
+
function halley(f, x0, opts) {
|
|
45211
|
+
const fprime = opts?.fprime;
|
|
45212
|
+
const fprime2 = opts?.fprime2;
|
|
45213
|
+
const tol = opts?.tol ?? 1e-12;
|
|
45214
|
+
const maxIter = opts?.maxIter ?? 100;
|
|
45215
|
+
let x = x0;
|
|
45216
|
+
for (let iter = 0; iter < maxIter; iter++) {
|
|
45217
|
+
const fx = f(x);
|
|
45218
|
+
if (Math.abs(fx) < tol) return x;
|
|
45219
|
+
const dfx = fprime ? fprime(x) : centralDiff1(f, x);
|
|
45220
|
+
const d2fx = fprime2 ? fprime2(x) : centralDiff2(f, x);
|
|
45221
|
+
const denom = 2 * dfx * dfx - fx * d2fx;
|
|
45222
|
+
if (Math.abs(denom) < 1e-300) {
|
|
45223
|
+
throw new Error("halley: denominator near zero \u2014 cannot continue");
|
|
45224
|
+
}
|
|
45225
|
+
const dx = 2 * fx * dfx / denom;
|
|
45226
|
+
const xNext = x - dx;
|
|
45227
|
+
if (Math.abs(dx) < tol) return xNext;
|
|
45228
|
+
x = xNext;
|
|
45229
|
+
}
|
|
45230
|
+
throw new Error(`halley: failed to converge within ${maxIter} iterations`);
|
|
45231
|
+
}
|
|
45232
|
+
|
|
45233
|
+
// src/numeric/fsolve.ts
|
|
45234
|
+
function maxNorm(v) {
|
|
45235
|
+
let m = 0;
|
|
45236
|
+
for (const vi of v) m = Math.max(m, Math.abs(vi));
|
|
45237
|
+
return m;
|
|
45238
|
+
}
|
|
45239
|
+
function norm24(v) {
|
|
45240
|
+
let s = 0;
|
|
45241
|
+
for (const vi of v) s += vi * vi;
|
|
45242
|
+
return Math.sqrt(s);
|
|
45243
|
+
}
|
|
45244
|
+
function fsolve(F, x0, opts = {}) {
|
|
45245
|
+
const tol = opts.tol ?? 1e-10;
|
|
45246
|
+
const maxIter = opts.maxIter ?? 100;
|
|
45247
|
+
let x = Array.from(x0);
|
|
45248
|
+
let Fx = F(x);
|
|
45249
|
+
for (let iter = 0; iter < maxIter; iter++) {
|
|
45250
|
+
if (maxNorm(Fx) < tol) return x;
|
|
45251
|
+
const J = numericJacobian(F, x);
|
|
45252
|
+
const rhs = Fx.map((v) => -v);
|
|
45253
|
+
let delta;
|
|
45254
|
+
try {
|
|
45255
|
+
delta = linsolve(J, rhs);
|
|
45256
|
+
} catch (err) {
|
|
45257
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
45258
|
+
throw new Error(`fsolve: singular Jacobian at iteration ${iter} (${msg})`);
|
|
45259
|
+
}
|
|
45260
|
+
const residualNorm2 = norm24(Fx);
|
|
45261
|
+
let xNext = x.map((xi, i) => xi + delta[i]);
|
|
45262
|
+
let FxNext = F(xNext);
|
|
45263
|
+
if (norm24(FxNext) >= residualNorm2) {
|
|
45264
|
+
let trialLambda = 1;
|
|
45265
|
+
for (let halving = 0; halving < 20; halving++) {
|
|
45266
|
+
trialLambda /= 2;
|
|
45267
|
+
const xTrial = x.map((xi, i) => xi + trialLambda * delta[i]);
|
|
45268
|
+
const FxTrial = F(xTrial);
|
|
45269
|
+
if (norm24(FxTrial) < residualNorm2) {
|
|
45270
|
+
xNext = xTrial;
|
|
45271
|
+
FxNext = FxTrial;
|
|
45272
|
+
break;
|
|
45273
|
+
}
|
|
45274
|
+
}
|
|
45275
|
+
}
|
|
45276
|
+
x = xNext;
|
|
45277
|
+
Fx = FxNext;
|
|
45278
|
+
}
|
|
45279
|
+
if (maxNorm(Fx) < tol) return x;
|
|
45280
|
+
throw new Error(`fsolve: failed to converge within ${maxIter} iterations`);
|
|
45281
|
+
}
|
|
45282
|
+
var root = fsolve;
|
|
45283
|
+
|
|
45284
|
+
// src/numeric/minimize-scalar.ts
|
|
45285
|
+
var GOLD = (3 - Math.sqrt(5)) / 2;
|
|
45286
|
+
var EPS2 = 2220446049250313e-31;
|
|
45287
|
+
function minimizeScalar(f, opts) {
|
|
45288
|
+
const [a0, b0] = opts?.bracket ?? [-10, 10];
|
|
45289
|
+
const tol = opts?.tol ?? 1e-8;
|
|
45290
|
+
const maxIter = opts?.maxIter ?? 100;
|
|
45291
|
+
let a = Math.min(a0, b0);
|
|
45292
|
+
let b = Math.max(a0, b0);
|
|
45293
|
+
let x = a + GOLD * (b - a);
|
|
45294
|
+
let w = x;
|
|
45295
|
+
let v = x;
|
|
45296
|
+
let fx = f(x);
|
|
45297
|
+
let fw = fx;
|
|
45298
|
+
let fv = fx;
|
|
45299
|
+
let e = 0;
|
|
45300
|
+
let d = 0;
|
|
45301
|
+
for (let iter = 0; iter < maxIter; iter++) {
|
|
45302
|
+
const xm = 0.5 * (a + b);
|
|
45303
|
+
const tol1 = tol * Math.abs(x) + EPS2;
|
|
45304
|
+
const tol2 = 2 * tol1;
|
|
45305
|
+
if (Math.abs(x - xm) <= tol2 - 0.5 * (b - a)) break;
|
|
45306
|
+
let useGolden = true;
|
|
45307
|
+
if (Math.abs(e) > tol1) {
|
|
45308
|
+
const r = (x - w) * (fx - fv);
|
|
45309
|
+
let q = (x - v) * (fx - fw);
|
|
45310
|
+
let p = (x - v) * q - (x - w) * r;
|
|
45311
|
+
q = 2 * (q - r);
|
|
45312
|
+
if (q > 0) p = -p;
|
|
45313
|
+
q = Math.abs(q);
|
|
45314
|
+
const eTemp = e;
|
|
45315
|
+
e = d;
|
|
45316
|
+
if (Math.abs(p) < Math.abs(0.5 * q * eTemp) && p > q * (a - x) && p < q * (b - x)) {
|
|
45317
|
+
d = p / q;
|
|
45318
|
+
const u2 = x + d;
|
|
45319
|
+
if (u2 - a < tol2 || b - u2 < tol2) {
|
|
45320
|
+
d = xm - x >= 0 ? tol1 : -tol1;
|
|
45321
|
+
}
|
|
45322
|
+
useGolden = false;
|
|
45323
|
+
}
|
|
45324
|
+
}
|
|
45325
|
+
if (useGolden) {
|
|
45326
|
+
e = x >= xm ? a - x : b - x;
|
|
45327
|
+
d = GOLD * e;
|
|
45328
|
+
}
|
|
45329
|
+
const u = Math.abs(d) >= tol1 ? x + d : x + (d >= 0 ? tol1 : -tol1);
|
|
45330
|
+
const fu = f(u);
|
|
45331
|
+
if (fu <= fx) {
|
|
45332
|
+
if (u >= x) a = x;
|
|
45333
|
+
else b = x;
|
|
45334
|
+
v = w;
|
|
45335
|
+
fv = fw;
|
|
45336
|
+
w = x;
|
|
45337
|
+
fw = fx;
|
|
45338
|
+
x = u;
|
|
45339
|
+
fx = fu;
|
|
45340
|
+
} else {
|
|
45341
|
+
if (u < x) a = u;
|
|
45342
|
+
else b = u;
|
|
45343
|
+
if (fu <= fw || w === x) {
|
|
45344
|
+
v = w;
|
|
45345
|
+
fv = fw;
|
|
45346
|
+
w = u;
|
|
45347
|
+
fw = fu;
|
|
45348
|
+
} else if (fu <= fv || v === x || v === w) {
|
|
45349
|
+
v = u;
|
|
45350
|
+
fv = fu;
|
|
45351
|
+
}
|
|
45352
|
+
}
|
|
45353
|
+
}
|
|
45354
|
+
return { x, fval: fx };
|
|
45355
|
+
}
|
|
45356
|
+
|
|
44792
45357
|
// src/timeseries-extra.ts
|
|
44793
45358
|
var arr4 = (x) => Array.isArray(x) ? x : Array.from(x);
|
|
44794
45359
|
var mean4 = (x) => mean(x);
|
|
@@ -44987,7 +45552,7 @@ function levenbergMarquardt(residual, x0, opts = {}) {
|
|
|
44987
45552
|
let x = Array.from(x0);
|
|
44988
45553
|
let lambda = 1e-3;
|
|
44989
45554
|
const h = 1e-7;
|
|
44990
|
-
const
|
|
45555
|
+
const norm25 = (v) => v.reduce((s, c) => s + c * c, 0);
|
|
44991
45556
|
const jacobian2 = (xc, r0) => {
|
|
44992
45557
|
const m = r0.length;
|
|
44993
45558
|
const J = Array.from({ length: m }, () => new Array(n).fill(0));
|
|
@@ -45000,7 +45565,7 @@ function levenbergMarquardt(residual, x0, opts = {}) {
|
|
|
45000
45565
|
return J;
|
|
45001
45566
|
};
|
|
45002
45567
|
let r = residual(x);
|
|
45003
|
-
let cost =
|
|
45568
|
+
let cost = norm25(r);
|
|
45004
45569
|
let iter = 0;
|
|
45005
45570
|
for (; iter < maxIter; iter++) {
|
|
45006
45571
|
const J = jacobian2(x, r);
|
|
@@ -45031,7 +45596,7 @@ function levenbergMarquardt(residual, x0, opts = {}) {
|
|
|
45031
45596
|
}
|
|
45032
45597
|
const xNew = x.map((v, i) => v + delta[i]);
|
|
45033
45598
|
const rNew = residual(xNew);
|
|
45034
|
-
const costNew =
|
|
45599
|
+
const costNew = norm25(rNew);
|
|
45035
45600
|
if (costNew < cost) {
|
|
45036
45601
|
x = xNew;
|
|
45037
45602
|
r = rNew;
|
|
@@ -45127,6 +45692,253 @@ function spectralClustering(adjacency, k, opts = {}) {
|
|
|
45127
45692
|
return kmeans(embed, k, opts).labels;
|
|
45128
45693
|
}
|
|
45129
45694
|
|
|
45695
|
+
// src/numeric/bfgs.ts
|
|
45696
|
+
var CBRT_EPS = Math.cbrt(2220446049250313e-31);
|
|
45697
|
+
function numericGradient(f, x) {
|
|
45698
|
+
const n = x.length;
|
|
45699
|
+
const g = new Array(n);
|
|
45700
|
+
for (let i = 0; i < n; i++) {
|
|
45701
|
+
const h = Math.max(1, Math.abs(x[i])) * CBRT_EPS;
|
|
45702
|
+
const xp = x.slice();
|
|
45703
|
+
const xm = x.slice();
|
|
45704
|
+
xp[i] += h;
|
|
45705
|
+
xm[i] -= h;
|
|
45706
|
+
g[i] = (f(xp) - f(xm)) / (2 * h);
|
|
45707
|
+
}
|
|
45708
|
+
return g;
|
|
45709
|
+
}
|
|
45710
|
+
function clipToBounds(x, bounds) {
|
|
45711
|
+
if (!bounds) return x;
|
|
45712
|
+
return x.map((v, i) => {
|
|
45713
|
+
const b = bounds[i];
|
|
45714
|
+
if (!b) return v;
|
|
45715
|
+
const [lo, hi] = b;
|
|
45716
|
+
return Math.min(hi, Math.max(lo, v));
|
|
45717
|
+
});
|
|
45718
|
+
}
|
|
45719
|
+
function dot2(a, b) {
|
|
45720
|
+
let s = 0;
|
|
45721
|
+
for (let i = 0; i < a.length; i++) s += a[i] * b[i];
|
|
45722
|
+
return s;
|
|
45723
|
+
}
|
|
45724
|
+
function matVec(H, v) {
|
|
45725
|
+
const n = v.length;
|
|
45726
|
+
const out = new Array(n);
|
|
45727
|
+
for (let i = 0; i < n; i++) {
|
|
45728
|
+
let s = 0;
|
|
45729
|
+
for (let j = 0; j < n; j++) s += H[i][j] * v[j];
|
|
45730
|
+
out[i] = s;
|
|
45731
|
+
}
|
|
45732
|
+
return out;
|
|
45733
|
+
}
|
|
45734
|
+
function bfgs(f, x0, opts = {}) {
|
|
45735
|
+
const { grad, bounds, tol = 1e-8, maxIter = 500 } = opts;
|
|
45736
|
+
const gradOf = grad ?? ((x2) => numericGradient(f, x2));
|
|
45737
|
+
const n = x0.length;
|
|
45738
|
+
const c1 = 1e-4;
|
|
45739
|
+
let x = clipToBounds(x0.slice(), bounds);
|
|
45740
|
+
let fx = f(x);
|
|
45741
|
+
let g = gradOf(x);
|
|
45742
|
+
let H = Array.from(
|
|
45743
|
+
{ length: n },
|
|
45744
|
+
(_, i) => Array.from({ length: n }, (_2, j) => i === j ? 1 : 0)
|
|
45745
|
+
);
|
|
45746
|
+
const infNorm = (v) => v.reduce((m, c) => Math.max(m, Math.abs(c)), 0);
|
|
45747
|
+
let iter = 0;
|
|
45748
|
+
let converged = infNorm(g) < tol;
|
|
45749
|
+
while (!converged && iter < maxIter) {
|
|
45750
|
+
const d = matVec(H, g).map((v) => -v);
|
|
45751
|
+
const gd = dot2(g, d);
|
|
45752
|
+
const dir = gd < 0 ? d : g.map((v) => -v);
|
|
45753
|
+
const dirSlope = gd < 0 ? gd : -dot2(g, g);
|
|
45754
|
+
let alpha = 1;
|
|
45755
|
+
let xNew = clipToBounds(
|
|
45756
|
+
x.map((v, i) => v + alpha * dir[i]),
|
|
45757
|
+
bounds
|
|
45758
|
+
);
|
|
45759
|
+
let fNew = f(xNew);
|
|
45760
|
+
let halvings = 0;
|
|
45761
|
+
while (fNew > fx + c1 * alpha * dirSlope && halvings < 50) {
|
|
45762
|
+
alpha *= 0.5;
|
|
45763
|
+
xNew = clipToBounds(
|
|
45764
|
+
x.map((v, i) => v + alpha * dir[i]),
|
|
45765
|
+
bounds
|
|
45766
|
+
);
|
|
45767
|
+
fNew = f(xNew);
|
|
45768
|
+
halvings++;
|
|
45769
|
+
}
|
|
45770
|
+
const gNew = gradOf(xNew);
|
|
45771
|
+
const s = xNew.map((v, i) => v - x[i]);
|
|
45772
|
+
const y = gNew.map((v, i) => v - g[i]);
|
|
45773
|
+
const sy = dot2(s, y);
|
|
45774
|
+
if (sy > 1e-12) {
|
|
45775
|
+
const rho = 1 / sy;
|
|
45776
|
+
const Hy = matVec(H, y);
|
|
45777
|
+
const yHy = dot2(y, Hy);
|
|
45778
|
+
const HNew = Array.from({ length: n }, () => new Array(n).fill(0));
|
|
45779
|
+
for (let i = 0; i < n; i++) {
|
|
45780
|
+
for (let j = 0; j < n; j++) {
|
|
45781
|
+
HNew[i][j] = H[i][j] - rho * (s[i] * Hy[j] + Hy[i] * s[j]) + rho * rho * yHy * s[i] * s[j] + rho * s[i] * s[j];
|
|
45782
|
+
}
|
|
45783
|
+
}
|
|
45784
|
+
H = HNew;
|
|
45785
|
+
}
|
|
45786
|
+
x = xNew;
|
|
45787
|
+
fx = fNew;
|
|
45788
|
+
g = gNew;
|
|
45789
|
+
iter++;
|
|
45790
|
+
converged = infNorm(g) < tol;
|
|
45791
|
+
}
|
|
45792
|
+
return { x, fval: fx, iterations: iter, converged };
|
|
45793
|
+
}
|
|
45794
|
+
|
|
45795
|
+
// src/numeric/nnls.ts
|
|
45796
|
+
function matVec2(A, x) {
|
|
45797
|
+
const m = A.length;
|
|
45798
|
+
const result = new Array(m);
|
|
45799
|
+
for (let i = 0; i < m; i++) {
|
|
45800
|
+
const row2 = A[i];
|
|
45801
|
+
let s = 0;
|
|
45802
|
+
for (let j = 0; j < row2.length; j++) s += row2[j] * x[j];
|
|
45803
|
+
result[i] = s;
|
|
45804
|
+
}
|
|
45805
|
+
return result;
|
|
45806
|
+
}
|
|
45807
|
+
function residualNorm(A, x, b) {
|
|
45808
|
+
const Ax = matVec2(A, x);
|
|
45809
|
+
let s = 0;
|
|
45810
|
+
for (let i = 0; i < b.length; i++) {
|
|
45811
|
+
const d = Ax[i] - b[i];
|
|
45812
|
+
s += d * d;
|
|
45813
|
+
}
|
|
45814
|
+
return Math.sqrt(s);
|
|
45815
|
+
}
|
|
45816
|
+
function nnls(A, b, opts = {}) {
|
|
45817
|
+
const m = A.length;
|
|
45818
|
+
const n = A[0]?.length ?? 0;
|
|
45819
|
+
const tol = opts.tol ?? 1e-10;
|
|
45820
|
+
const maxIter = opts.maxIter ?? 3 * n;
|
|
45821
|
+
let x = new Array(n).fill(0);
|
|
45822
|
+
const passive = /* @__PURE__ */ new Set();
|
|
45823
|
+
const gradient2 = (xv) => {
|
|
45824
|
+
const Ax = matVec2(A, xv);
|
|
45825
|
+
const w = new Array(n).fill(0);
|
|
45826
|
+
for (let j = 0; j < n; j++) {
|
|
45827
|
+
let s = 0;
|
|
45828
|
+
for (let i = 0; i < m; i++) s += A[i][j] * (b[i] - Ax[i]);
|
|
45829
|
+
w[j] = s;
|
|
45830
|
+
}
|
|
45831
|
+
return w;
|
|
45832
|
+
};
|
|
45833
|
+
const solvePassive = (P2) => {
|
|
45834
|
+
const z = new Array(n).fill(0);
|
|
45835
|
+
if (P2.length === 0) return z;
|
|
45836
|
+
const Asub = A.map((row2) => P2.map((j) => row2[j]));
|
|
45837
|
+
let zp;
|
|
45838
|
+
try {
|
|
45839
|
+
zp = leastSquares(Asub, b);
|
|
45840
|
+
} catch {
|
|
45841
|
+
return x.slice();
|
|
45842
|
+
}
|
|
45843
|
+
for (let k = 0; k < P2.length; k++) z[P2[k]] = zp[k];
|
|
45844
|
+
return z;
|
|
45845
|
+
};
|
|
45846
|
+
let outerIter = 0;
|
|
45847
|
+
while (outerIter < maxIter) {
|
|
45848
|
+
const w = gradient2(x);
|
|
45849
|
+
let maxW = -Infinity;
|
|
45850
|
+
let maxJ = -1;
|
|
45851
|
+
for (let j = 0; j < n; j++) {
|
|
45852
|
+
if (!passive.has(j) && w[j] > maxW) {
|
|
45853
|
+
maxW = w[j];
|
|
45854
|
+
maxJ = j;
|
|
45855
|
+
}
|
|
45856
|
+
}
|
|
45857
|
+
if (maxJ === -1 || maxW <= tol) break;
|
|
45858
|
+
passive.add(maxJ);
|
|
45859
|
+
let z = solvePassive(Array.from(passive));
|
|
45860
|
+
let innerIter = 0;
|
|
45861
|
+
while (Array.from(passive).some((j) => z[j] <= tol) && innerIter < maxIter) {
|
|
45862
|
+
innerIter++;
|
|
45863
|
+
let alpha = Infinity;
|
|
45864
|
+
for (const j of passive) {
|
|
45865
|
+
if (z[j] <= tol) {
|
|
45866
|
+
const denom = x[j] - z[j];
|
|
45867
|
+
if (denom > 0) alpha = Math.min(alpha, x[j] / denom);
|
|
45868
|
+
}
|
|
45869
|
+
}
|
|
45870
|
+
if (!isFinite(alpha)) alpha = 0;
|
|
45871
|
+
for (let j = 0; j < n; j++) x[j] = x[j] + alpha * (z[j] - x[j]);
|
|
45872
|
+
for (const j of Array.from(passive)) {
|
|
45873
|
+
if (Math.abs(x[j]) < tol) passive.delete(j);
|
|
45874
|
+
}
|
|
45875
|
+
z = solvePassive(Array.from(passive));
|
|
45876
|
+
}
|
|
45877
|
+
x = z;
|
|
45878
|
+
outerIter++;
|
|
45879
|
+
}
|
|
45880
|
+
return { x, residual: residualNorm(A, x, b) };
|
|
45881
|
+
}
|
|
45882
|
+
function clip(v, lower, upper) {
|
|
45883
|
+
return v.map((vi, i) => Math.min(Math.max(vi, lower[i]), upper[i]));
|
|
45884
|
+
}
|
|
45885
|
+
function lsqBounded(A, b, lower, upper, opts = {}) {
|
|
45886
|
+
const m = A.length;
|
|
45887
|
+
const n = A[0]?.length ?? 0;
|
|
45888
|
+
const tol = opts.tol ?? 1e-10;
|
|
45889
|
+
const maxIter = opts.maxIter ?? Math.max(200, 20 * n);
|
|
45890
|
+
let x = clip(new Array(n).fill(0), lower, upper);
|
|
45891
|
+
const gradient2 = (xv) => {
|
|
45892
|
+
const Ax = matVec2(A, xv);
|
|
45893
|
+
const r = new Array(m);
|
|
45894
|
+
for (let i = 0; i < m; i++) r[i] = Ax[i] - b[i];
|
|
45895
|
+
const g = new Array(n).fill(0);
|
|
45896
|
+
for (let j = 0; j < n; j++) {
|
|
45897
|
+
let s = 0;
|
|
45898
|
+
for (let i = 0; i < m; i++) s += A[i][j] * r[i];
|
|
45899
|
+
g[j] = s;
|
|
45900
|
+
}
|
|
45901
|
+
return g;
|
|
45902
|
+
};
|
|
45903
|
+
const objective = (xv) => {
|
|
45904
|
+
const r = residualNorm(A, xv, b);
|
|
45905
|
+
return 0.5 * r * r;
|
|
45906
|
+
};
|
|
45907
|
+
for (let iter = 0; iter < maxIter; iter++) {
|
|
45908
|
+
const g = gradient2(x);
|
|
45909
|
+
const projFull = clip(
|
|
45910
|
+
x.map((xi, i) => xi - g[i]),
|
|
45911
|
+
lower,
|
|
45912
|
+
upper
|
|
45913
|
+
);
|
|
45914
|
+
let pgNorm = 0;
|
|
45915
|
+
for (let i = 0; i < n; i++) pgNorm += (x[i] - projFull[i]) ** 2;
|
|
45916
|
+
pgNorm = Math.sqrt(pgNorm);
|
|
45917
|
+
if (pgNorm < tol) break;
|
|
45918
|
+
const fx = objective(x);
|
|
45919
|
+
let alpha = 1;
|
|
45920
|
+
let xNext = clip(
|
|
45921
|
+
x.map((xi, i) => xi - alpha * g[i]),
|
|
45922
|
+
lower,
|
|
45923
|
+
upper
|
|
45924
|
+
);
|
|
45925
|
+
let fNext = objective(xNext);
|
|
45926
|
+
let backtrack = 0;
|
|
45927
|
+
while (fNext > fx && backtrack < 50) {
|
|
45928
|
+
alpha /= 2;
|
|
45929
|
+
xNext = clip(
|
|
45930
|
+
x.map((xi, i) => xi - alpha * g[i]),
|
|
45931
|
+
lower,
|
|
45932
|
+
upper
|
|
45933
|
+
);
|
|
45934
|
+
fNext = objective(xNext);
|
|
45935
|
+
backtrack++;
|
|
45936
|
+
}
|
|
45937
|
+
x = xNext;
|
|
45938
|
+
}
|
|
45939
|
+
return { x, residual: residualNorm(A, x, b) };
|
|
45940
|
+
}
|
|
45941
|
+
|
|
45130
45942
|
// src/cas-integration.ts
|
|
45131
45943
|
var parse2 = parse;
|
|
45132
45944
|
var evaluate2 = evaluate;
|
|
@@ -45385,15 +46197,15 @@ function haversine(lat1, lon1, lat2, lon2, radius = EARTH_RADIUS_KM) {
|
|
|
45385
46197
|
const a = Math.sin(dPhi / 2) ** 2 + Math.cos(phi1) * Math.cos(phi2) * Math.sin(dLambda / 2) ** 2;
|
|
45386
46198
|
return radius * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
|
45387
46199
|
}
|
|
45388
|
-
var
|
|
45389
|
-
var norm3 = (a) => Math.sqrt(
|
|
46200
|
+
var dot3 = (a, b) => a.reduce((s, v, i) => s + v * b[i], 0);
|
|
46201
|
+
var norm3 = (a) => Math.sqrt(dot3(a, a));
|
|
45390
46202
|
function slerp(v0, v1, t) {
|
|
45391
46203
|
const n0 = norm3(v0);
|
|
45392
46204
|
const n16 = norm3(v1);
|
|
45393
46205
|
if (n0 === 0 || n16 === 0) throw new Error("slerp: input vectors must be non-zero");
|
|
45394
46206
|
const u0 = v0.map((v) => v / n0);
|
|
45395
46207
|
const u1 = v1.map((v) => v / n16);
|
|
45396
|
-
let cos2 =
|
|
46208
|
+
let cos2 = dot3(u0, u1);
|
|
45397
46209
|
cos2 = cos2 > 1 ? 1 : cos2 < -1 ? -1 : cos2;
|
|
45398
46210
|
const omega = Math.acos(cos2);
|
|
45399
46211
|
const mag = n0 + (n16 - n0) * t;
|
|
@@ -45509,6 +46321,7 @@ export {
|
|
|
45509
46321
|
betainc,
|
|
45510
46322
|
betweennessCentrality,
|
|
45511
46323
|
bezierCurve,
|
|
46324
|
+
bfgs,
|
|
45512
46325
|
bigint,
|
|
45513
46326
|
bignumber,
|
|
45514
46327
|
bin,
|
|
@@ -45794,6 +46607,7 @@ export {
|
|
|
45794
46607
|
fresnelC,
|
|
45795
46608
|
fresnelS,
|
|
45796
46609
|
friedmanTest,
|
|
46610
|
+
fsolve,
|
|
45797
46611
|
fsum,
|
|
45798
46612
|
fullSimplify,
|
|
45799
46613
|
functionExpand,
|
|
@@ -45833,6 +46647,7 @@ export {
|
|
|
45833
46647
|
groebnerBasis,
|
|
45834
46648
|
groupDelay,
|
|
45835
46649
|
gumbelDist,
|
|
46650
|
+
halley,
|
|
45836
46651
|
harmonicNumber,
|
|
45837
46652
|
hartreeEnergy,
|
|
45838
46653
|
hasNumericValue,
|
|
@@ -45951,6 +46766,7 @@ export {
|
|
|
45951
46766
|
lowpassFilter,
|
|
45952
46767
|
lsolve,
|
|
45953
46768
|
lsolveAll,
|
|
46769
|
+
lsqBounded,
|
|
45954
46770
|
lucas,
|
|
45955
46771
|
lucasL,
|
|
45956
46772
|
lup,
|
|
@@ -45988,6 +46804,7 @@ export {
|
|
|
45988
46804
|
minSelect,
|
|
45989
46805
|
minimalPolynomial,
|
|
45990
46806
|
minimize,
|
|
46807
|
+
minimizeScalar,
|
|
45991
46808
|
minimumSpanningTree,
|
|
45992
46809
|
minkowskiDistance,
|
|
45993
46810
|
mod,
|
|
@@ -46010,9 +46827,11 @@ export {
|
|
|
46010
46827
|
negativeBinomialDist,
|
|
46011
46828
|
nelderMead,
|
|
46012
46829
|
neutronMass,
|
|
46830
|
+
newton,
|
|
46013
46831
|
newtonInterp,
|
|
46014
46832
|
nextPrime,
|
|
46015
46833
|
nintegrate,
|
|
46834
|
+
nnls,
|
|
46016
46835
|
nodeOperations,
|
|
46017
46836
|
noncentralChi2PDF,
|
|
46018
46837
|
norm,
|
|
@@ -46031,11 +46850,13 @@ export {
|
|
|
46031
46850
|
nullspace,
|
|
46032
46851
|
number,
|
|
46033
46852
|
numeric,
|
|
46853
|
+
numericJacobian,
|
|
46034
46854
|
oct,
|
|
46035
46855
|
odeAdaptiveStep,
|
|
46036
46856
|
odeGeneral,
|
|
46037
46857
|
ones,
|
|
46038
46858
|
or,
|
|
46859
|
+
orth,
|
|
46039
46860
|
outer,
|
|
46040
46861
|
padeApproximant,
|
|
46041
46862
|
pageRank,
|
|
@@ -46117,6 +46938,7 @@ export {
|
|
|
46117
46938
|
protonMass,
|
|
46118
46939
|
ptp,
|
|
46119
46940
|
qr,
|
|
46941
|
+
quad,
|
|
46120
46942
|
quadprog,
|
|
46121
46943
|
quantileSeq,
|
|
46122
46944
|
quantumOfCirculation,
|
|
@@ -46154,6 +46976,7 @@ export {
|
|
|
46154
46976
|
rightLogShift,
|
|
46155
46977
|
risingFactorial,
|
|
46156
46978
|
romberg,
|
|
46979
|
+
root,
|
|
46157
46980
|
rotate,
|
|
46158
46981
|
rotateVector2D,
|
|
46159
46982
|
rotateVector3D,
|
|
@@ -46165,6 +46988,7 @@ export {
|
|
|
46165
46988
|
sackurTetrode,
|
|
46166
46989
|
schur,
|
|
46167
46990
|
sec,
|
|
46991
|
+
secant,
|
|
46168
46992
|
sech,
|
|
46169
46993
|
secondRadiation,
|
|
46170
46994
|
seedProbabilityRng,
|
|
@@ -46241,6 +47065,7 @@ export {
|
|
|
46241
47065
|
subtractScalar,
|
|
46242
47066
|
sum,
|
|
46243
47067
|
summation,
|
|
47068
|
+
svd3 as svd,
|
|
46244
47069
|
sylvester,
|
|
46245
47070
|
symbolicEqual,
|
|
46246
47071
|
symbolicIntegral,
|