@elarsaks/umap-wasm 0.1.5 → 0.3.1
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/README.md +11 -8
- package/dist/nn_descent.d.ts +1 -1
- package/dist/nn_descent.js +14 -1
- package/dist/umap.d.ts +2 -0
- package/dist/umap.js +3 -1
- package/dist/wasmBridge.d.ts +1 -0
- package/dist/wasmBridge.js +50 -4
- package/lib/umap-js.js +69 -8
- package/lib/umap-js.min.js +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# 🚧 UNDER DEVELOPMENT 🚧
|
|
2
|
+
|
|
1
3
|
# UMAP-WASM: WebAssembly-Accelerated UMAP for JavaScript
|
|
2
4
|
|
|
3
5
|
[](LICENSE)
|
|
@@ -30,7 +32,7 @@ This implementation builds upon the PAIR-code `umap-js` library with strategic W
|
|
|
30
32
|
- Distance computations (implemented in Rust: `distances.rs`)
|
|
31
33
|
- Nearest neighbour search (random projection trees) (implemented in Rust: `tree.rs`)
|
|
32
34
|
- Matrix operations in optimization loops (implemented in Rust: `matrix.rs`)
|
|
33
|
-
- Nearest‑neighbour graph refinement (NN‑Descent)
|
|
35
|
+
- Nearest‑neighbour graph refinement (NN‑Descent) (implemented in Rust: `nn_descent.rs`)
|
|
34
36
|
- Gradient‑descent layout optimisation *(TODO — optimizer currently runs in JS)*
|
|
35
37
|
|
|
36
38
|
### Key Features
|
|
@@ -163,6 +165,7 @@ The UMAP constructor accepts a `UMAPParameters` object with the following option
|
|
|
163
165
|
| `random` | `() => number` | `Math.random` | PRNG for reproducibility |
|
|
164
166
|
| `distanceFn` | `DistanceFn` | `euclidean` | Distance metric for input space |
|
|
165
167
|
| `useWasmDistance` | `boolean` | `false` | Whether to use Rust/WASM distance functions when available |
|
|
168
|
+
| `useWasmNNDescent` | `boolean` | `false` | Whether to use Rust/WASM NN-Descent implementation when available |
|
|
166
169
|
| `useWasmTree` | `boolean` | `false` | Whether to use Rust/WASM random projection tree construction when available |
|
|
167
170
|
| `useWasmMatrix` | `boolean` | `false` | Whether to use Rust/WASM sparse matrix operations when available |
|
|
168
171
|
|
|
@@ -170,13 +173,13 @@ The UMAP constructor accepts a `UMAPParameters` object with the following option
|
|
|
170
173
|
|
|
171
174
|
The project exposes configuration flags to selectively enable WASM-accelerated components. The table below maps the high-level operations to the available configuration flags and current implementation status.
|
|
172
175
|
|
|
173
|
-
| Component | Config Flag |
|
|
174
|
-
|
|
175
|
-
| Distance computations | `useWasmDistance` |
|
|
176
|
-
| Nearest neighbour search (
|
|
177
|
-
| Matrix operations
|
|
178
|
-
|
|
|
179
|
-
| Gradient‑descent layout optimisation | — | TODO
|
|
176
|
+
| Component | Config Flag | Notes |
|
|
177
|
+
|-----------|-------------|-------|
|
|
178
|
+
| Distance computations | `useWasmDistance` | WASM `euclidean` and `cosine` implementations. See `distances.rs`. |
|
|
179
|
+
| Nearest neighbour search (RP trees) | `useWasmTree` | WASM-accelerated random projection tree construction. See `tree.rs`. |
|
|
180
|
+
| Matrix operations | `useWasmMatrix` | Sparse-matrix operations (transpose, element-wise ops, CSR, normalization). See `matrix.rs`. |
|
|
181
|
+
| NN‑Descent graph refinement | `useWasmNNDescent` | **TODO** - Approximate nearest-neighbour graph construction/refinement. See `nn_descent.rs`. |
|
|
182
|
+
| Gradient‑descent layout optimisation | — | **TODO** — optimization loop currently runs in JS. |
|
|
180
183
|
|
|
181
184
|
|
|
182
185
|
### Example with Custom Parameters
|
package/dist/nn_descent.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import * as heap from './heap.js';
|
|
|
2
2
|
import * as matrix from './matrix.js';
|
|
3
3
|
import * as tree from './tree.js';
|
|
4
4
|
import { RandomFn, Vectors, DistanceFn } from './umap.js';
|
|
5
|
-
export declare function makeNNDescent(distanceFn: DistanceFn, random: RandomFn): (data: Vectors, leafArray: Vectors, nNeighbors: number, nIters?: number, maxCandidates?: number, delta?: number, rho?: number, rpTreeInit?: boolean) => {
|
|
5
|
+
export declare function makeNNDescent(distanceFn: DistanceFn, random: RandomFn, useWasm?: boolean): (data: Vectors, leafArray: Vectors, nNeighbors: number, nIters?: number, maxCandidates?: number, delta?: number, rho?: number, rpTreeInit?: boolean) => {
|
|
6
6
|
indices: number[][];
|
|
7
7
|
weights: number[][];
|
|
8
8
|
};
|
package/dist/nn_descent.js
CHANGED
|
@@ -2,8 +2,21 @@ import * as heap from './heap.js';
|
|
|
2
2
|
import * as matrix from './matrix.js';
|
|
3
3
|
import * as tree from './tree.js';
|
|
4
4
|
import * as utils from './utils.js';
|
|
5
|
-
|
|
5
|
+
import * as wasmBridge from './wasmBridge.js';
|
|
6
|
+
export function makeNNDescent(distanceFn, random, useWasm = false) {
|
|
6
7
|
return function nNDescent(data, leafArray, nNeighbors, nIters = 10, maxCandidates = 50, delta = 0.001, rho = 0.5, rpTreeInit = true) {
|
|
8
|
+
if (useWasm) {
|
|
9
|
+
if (!wasmBridge.isWasmAvailable()) {
|
|
10
|
+
throw new Error('WASM NN-Descent requested but WASM module is not available. Initialize WASM with initWasm() first.');
|
|
11
|
+
}
|
|
12
|
+
const distanceMetric = distanceFn.name === 'cosine' ? 'cosine' : 'euclidean';
|
|
13
|
+
const seed = Math.floor(random() * 0xFFFFFFFF);
|
|
14
|
+
const result = wasmBridge.nnDescentWasm(data, leafArray, nNeighbors, nIters, maxCandidates, delta, rho, rpTreeInit, distanceMetric, seed);
|
|
15
|
+
return {
|
|
16
|
+
indices: result[0],
|
|
17
|
+
weights: result[1],
|
|
18
|
+
};
|
|
19
|
+
}
|
|
7
20
|
const nVertices = data.length;
|
|
8
21
|
const currentGraph = heap.makeHeap(data.length, nNeighbors);
|
|
9
22
|
for (let i = 0; i < data.length; i++) {
|
package/dist/umap.d.ts
CHANGED
|
@@ -12,6 +12,7 @@ export declare const enum TargetMetric {
|
|
|
12
12
|
export interface UMAPParameters {
|
|
13
13
|
distanceFn?: DistanceFn;
|
|
14
14
|
useWasmDistance?: boolean;
|
|
15
|
+
useWasmNNDescent?: boolean;
|
|
15
16
|
learningRate?: number;
|
|
16
17
|
localConnectivity?: number;
|
|
17
18
|
minDist?: number;
|
|
@@ -48,6 +49,7 @@ export declare class UMAP {
|
|
|
48
49
|
private targetNNeighbors;
|
|
49
50
|
private distanceFn;
|
|
50
51
|
private useWasmDistance;
|
|
52
|
+
private useWasmNNDescent;
|
|
51
53
|
private useWasmMatrix;
|
|
52
54
|
private useWasmTree;
|
|
53
55
|
private knnIndices?;
|
package/dist/umap.js
CHANGED
|
@@ -26,6 +26,7 @@ export class UMAP {
|
|
|
26
26
|
this.targetNNeighbors = this.nNeighbors;
|
|
27
27
|
this.distanceFn = euclidean;
|
|
28
28
|
this.useWasmDistance = false;
|
|
29
|
+
this.useWasmNNDescent = false;
|
|
29
30
|
this.useWasmMatrix = false;
|
|
30
31
|
this.useWasmTree = false;
|
|
31
32
|
this.isInitialized = false;
|
|
@@ -38,6 +39,7 @@ export class UMAP {
|
|
|
38
39
|
};
|
|
39
40
|
setParam('distanceFn');
|
|
40
41
|
setParam('useWasmDistance');
|
|
42
|
+
setParam('useWasmNNDescent');
|
|
41
43
|
setParam('useWasmMatrix');
|
|
42
44
|
setParam('useWasmTree');
|
|
43
45
|
setParam('learningRate');
|
|
@@ -219,7 +221,7 @@ export class UMAP {
|
|
|
219
221
|
nearestNeighbors(X) {
|
|
220
222
|
const { distanceFn, nNeighbors } = this;
|
|
221
223
|
const log2 = (n) => Math.log(n) / Math.log(2);
|
|
222
|
-
const metricNNDescent = nnDescent.makeNNDescent(distanceFn, this.random);
|
|
224
|
+
const metricNNDescent = nnDescent.makeNNDescent(distanceFn, this.random, this.useWasmNNDescent);
|
|
223
225
|
const round = (n) => {
|
|
224
226
|
return n === 0.5 ? 0 : Math.round(n);
|
|
225
227
|
};
|
package/dist/wasmBridge.d.ts
CHANGED
|
@@ -55,3 +55,4 @@ export declare function wasmSparseMatrixGetAll(matrix: WasmSparseMatrix): {
|
|
|
55
55
|
row: number;
|
|
56
56
|
col: number;
|
|
57
57
|
}[];
|
|
58
|
+
export declare function nnDescentWasm(data: number[][], leafArray: number[][], nNeighbors: number, nIters?: number, maxCandidates?: number, delta?: number, rho?: number, rpTreeInit?: boolean, distanceMetric?: string, seed?: number): number[][][];
|
package/dist/wasmBridge.js
CHANGED
|
@@ -6,10 +6,16 @@ export async function initWasm() {
|
|
|
6
6
|
wasmReady = (async () => {
|
|
7
7
|
try {
|
|
8
8
|
const isNode = typeof process !== 'undefined' && process.versions != null && process.versions.node != null;
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
let mod;
|
|
10
|
+
if (isNode) {
|
|
11
|
+
const nodePath = ['..', 'wasm', 'pkg', 'node', 'umap_wasm_core.js'].join('/');
|
|
12
|
+
mod = await import(nodePath);
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
const origin = typeof window !== 'undefined' && window.location ? window.location.origin : '';
|
|
16
|
+
const wasmPath = `${origin}/wasm/pkg/web/umap_wasm_core.js`;
|
|
17
|
+
mod = await new Function('p', 'return import(p)')(wasmPath);
|
|
18
|
+
}
|
|
13
19
|
if (typeof mod.default === 'function') {
|
|
14
20
|
await mod.default();
|
|
15
21
|
}
|
|
@@ -186,3 +192,43 @@ export function wasmSparseMatrixGetAll(matrix) {
|
|
|
186
192
|
}
|
|
187
193
|
return entries;
|
|
188
194
|
}
|
|
195
|
+
export function nnDescentWasm(data, leafArray, nNeighbors, nIters = 10, maxCandidates = 50, delta = 0.001, rho = 0.5, rpTreeInit = true, distanceMetric = 'euclidean', seed = 42) {
|
|
196
|
+
if (!wasmModule)
|
|
197
|
+
throw new Error('WASM module not initialized');
|
|
198
|
+
const nSamples = data.length;
|
|
199
|
+
const dim = data[0].length;
|
|
200
|
+
const flatData = new Float64Array(nSamples * dim);
|
|
201
|
+
for (let i = 0; i < nSamples; i++) {
|
|
202
|
+
for (let j = 0; j < dim; j++) {
|
|
203
|
+
flatData[i * dim + j] = data[i][j];
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
const nLeaves = leafArray.length;
|
|
207
|
+
const leafSize = nLeaves > 0 ? leafArray[0].length : 0;
|
|
208
|
+
const flatLeafArray = new Int32Array(nLeaves * leafSize);
|
|
209
|
+
for (let i = 0; i < nLeaves; i++) {
|
|
210
|
+
for (let j = 0; j < leafSize; j++) {
|
|
211
|
+
flatLeafArray[i * leafSize + j] = leafArray[i][j];
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
const result = wasmModule.nn_descent(flatData, nSamples, dim, flatLeafArray, nLeaves, leafSize, nNeighbors, nIters, maxCandidates, delta, rho, rpTreeInit, distanceMetric, BigInt(seed));
|
|
215
|
+
const indices = [];
|
|
216
|
+
const distances = [];
|
|
217
|
+
const flags = [];
|
|
218
|
+
const offset1 = nSamples * nNeighbors;
|
|
219
|
+
const offset2 = 2 * nSamples * nNeighbors;
|
|
220
|
+
for (let i = 0; i < nSamples; i++) {
|
|
221
|
+
const rowIndices = [];
|
|
222
|
+
const rowDistances = [];
|
|
223
|
+
const rowFlags = [];
|
|
224
|
+
for (let j = 0; j < nNeighbors; j++) {
|
|
225
|
+
rowIndices.push(result[i * nNeighbors + j]);
|
|
226
|
+
rowDistances.push(result[offset1 + i * nNeighbors + j]);
|
|
227
|
+
rowFlags.push(result[offset2 + i * nNeighbors + j]);
|
|
228
|
+
}
|
|
229
|
+
indices.push(rowIndices);
|
|
230
|
+
distances.push(rowDistances);
|
|
231
|
+
flags.push(rowFlags);
|
|
232
|
+
}
|
|
233
|
+
return [indices, distances, flags];
|
|
234
|
+
}
|
package/lib/umap-js.js
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
return /******/ (() => { // webpackBootstrap
|
|
12
12
|
/******/ var __webpack_modules__ = ({
|
|
13
13
|
|
|
14
|
-
/***/
|
|
14
|
+
/***/ 433
|
|
15
15
|
(module) {
|
|
16
16
|
|
|
17
17
|
function webpackEmptyAsyncContext(req) {
|
|
@@ -25,7 +25,7 @@ function webpackEmptyAsyncContext(req) {
|
|
|
25
25
|
}
|
|
26
26
|
webpackEmptyAsyncContext.keys = () => ([]);
|
|
27
27
|
webpackEmptyAsyncContext.resolve = webpackEmptyAsyncContext;
|
|
28
|
-
webpackEmptyAsyncContext.id =
|
|
28
|
+
webpackEmptyAsyncContext.id = 433;
|
|
29
29
|
module.exports = webpackEmptyAsyncContext;
|
|
30
30
|
|
|
31
31
|
/***/ },
|
|
@@ -6480,10 +6480,16 @@ async function initWasm() {
|
|
|
6480
6480
|
wasmReady = (async () => {
|
|
6481
6481
|
try {
|
|
6482
6482
|
const isNode = typeof process !== 'undefined' && process.versions != null && process.versions.node != null;
|
|
6483
|
-
|
|
6484
|
-
|
|
6485
|
-
|
|
6486
|
-
|
|
6483
|
+
let mod;
|
|
6484
|
+
if (isNode) {
|
|
6485
|
+
const nodePath = ['..', 'wasm', 'pkg', 'node', 'umap_wasm_core.js'].join('/');
|
|
6486
|
+
mod = await __webpack_require__(433)(nodePath);
|
|
6487
|
+
}
|
|
6488
|
+
else {
|
|
6489
|
+
const origin = typeof window !== 'undefined' && window.location ? window.location.origin : '';
|
|
6490
|
+
const wasmPath = `${origin}/wasm/pkg/web/umap_wasm_core.js`;
|
|
6491
|
+
mod = await new Function('p', 'return import(p)')(wasmPath);
|
|
6492
|
+
}
|
|
6487
6493
|
if (typeof mod.default === 'function') {
|
|
6488
6494
|
await mod.default();
|
|
6489
6495
|
}
|
|
@@ -6660,6 +6666,46 @@ function wasmSparseMatrixGetAll(matrix) {
|
|
|
6660
6666
|
}
|
|
6661
6667
|
return entries;
|
|
6662
6668
|
}
|
|
6669
|
+
function nnDescentWasm(data, leafArray, nNeighbors, nIters = 10, maxCandidates = 50, delta = 0.001, rho = 0.5, rpTreeInit = true, distanceMetric = 'euclidean', seed = 42) {
|
|
6670
|
+
if (!wasmModule)
|
|
6671
|
+
throw new Error('WASM module not initialized');
|
|
6672
|
+
const nSamples = data.length;
|
|
6673
|
+
const dim = data[0].length;
|
|
6674
|
+
const flatData = new Float64Array(nSamples * dim);
|
|
6675
|
+
for (let i = 0; i < nSamples; i++) {
|
|
6676
|
+
for (let j = 0; j < dim; j++) {
|
|
6677
|
+
flatData[i * dim + j] = data[i][j];
|
|
6678
|
+
}
|
|
6679
|
+
}
|
|
6680
|
+
const nLeaves = leafArray.length;
|
|
6681
|
+
const leafSize = nLeaves > 0 ? leafArray[0].length : 0;
|
|
6682
|
+
const flatLeafArray = new Int32Array(nLeaves * leafSize);
|
|
6683
|
+
for (let i = 0; i < nLeaves; i++) {
|
|
6684
|
+
for (let j = 0; j < leafSize; j++) {
|
|
6685
|
+
flatLeafArray[i * leafSize + j] = leafArray[i][j];
|
|
6686
|
+
}
|
|
6687
|
+
}
|
|
6688
|
+
const result = wasmModule.nn_descent(flatData, nSamples, dim, flatLeafArray, nLeaves, leafSize, nNeighbors, nIters, maxCandidates, delta, rho, rpTreeInit, distanceMetric, BigInt(seed));
|
|
6689
|
+
const indices = [];
|
|
6690
|
+
const distances = [];
|
|
6691
|
+
const flags = [];
|
|
6692
|
+
const offset1 = nSamples * nNeighbors;
|
|
6693
|
+
const offset2 = 2 * nSamples * nNeighbors;
|
|
6694
|
+
for (let i = 0; i < nSamples; i++) {
|
|
6695
|
+
const rowIndices = [];
|
|
6696
|
+
const rowDistances = [];
|
|
6697
|
+
const rowFlags = [];
|
|
6698
|
+
for (let j = 0; j < nNeighbors; j++) {
|
|
6699
|
+
rowIndices.push(result[i * nNeighbors + j]);
|
|
6700
|
+
rowDistances.push(result[offset1 + i * nNeighbors + j]);
|
|
6701
|
+
rowFlags.push(result[offset2 + i * nNeighbors + j]);
|
|
6702
|
+
}
|
|
6703
|
+
indices.push(rowIndices);
|
|
6704
|
+
distances.push(rowDistances);
|
|
6705
|
+
flags.push(rowFlags);
|
|
6706
|
+
}
|
|
6707
|
+
return [indices, distances, flags];
|
|
6708
|
+
}
|
|
6663
6709
|
|
|
6664
6710
|
;// ./src/tree.ts
|
|
6665
6711
|
|
|
@@ -6893,8 +6939,21 @@ function searchFlatTree(point, tree, random) {
|
|
|
6893
6939
|
|
|
6894
6940
|
|
|
6895
6941
|
|
|
6896
|
-
|
|
6942
|
+
|
|
6943
|
+
function makeNNDescent(distanceFn, random, useWasm = false) {
|
|
6897
6944
|
return function nNDescent(data, leafArray, nNeighbors, nIters = 10, maxCandidates = 50, delta = 0.001, rho = 0.5, rpTreeInit = true) {
|
|
6945
|
+
if (useWasm) {
|
|
6946
|
+
if (!isWasmAvailable()) {
|
|
6947
|
+
throw new Error('WASM NN-Descent requested but WASM module is not available. Initialize WASM with initWasm() first.');
|
|
6948
|
+
}
|
|
6949
|
+
const distanceMetric = distanceFn.name === 'cosine' ? 'cosine' : 'euclidean';
|
|
6950
|
+
const seed = Math.floor(random() * 0xFFFFFFFF);
|
|
6951
|
+
const result = nnDescentWasm(data, leafArray, nNeighbors, nIters, maxCandidates, delta, rho, rpTreeInit, distanceMetric, seed);
|
|
6952
|
+
return {
|
|
6953
|
+
indices: result[0],
|
|
6954
|
+
weights: result[1],
|
|
6955
|
+
};
|
|
6956
|
+
}
|
|
6898
6957
|
const nVertices = data.length;
|
|
6899
6958
|
const currentGraph = makeHeap(data.length, nNeighbors);
|
|
6900
6959
|
for (let i = 0; i < data.length; i++) {
|
|
@@ -7326,6 +7385,7 @@ class UMAP {
|
|
|
7326
7385
|
this.targetNNeighbors = this.nNeighbors;
|
|
7327
7386
|
this.distanceFn = euclidean;
|
|
7328
7387
|
this.useWasmDistance = false;
|
|
7388
|
+
this.useWasmNNDescent = false;
|
|
7329
7389
|
this.useWasmMatrix = false;
|
|
7330
7390
|
this.useWasmTree = false;
|
|
7331
7391
|
this.isInitialized = false;
|
|
@@ -7338,6 +7398,7 @@ class UMAP {
|
|
|
7338
7398
|
};
|
|
7339
7399
|
setParam('distanceFn');
|
|
7340
7400
|
setParam('useWasmDistance');
|
|
7401
|
+
setParam('useWasmNNDescent');
|
|
7341
7402
|
setParam('useWasmMatrix');
|
|
7342
7403
|
setParam('useWasmTree');
|
|
7343
7404
|
setParam('learningRate');
|
|
@@ -7519,7 +7580,7 @@ class UMAP {
|
|
|
7519
7580
|
nearestNeighbors(X) {
|
|
7520
7581
|
const { distanceFn, nNeighbors } = this;
|
|
7521
7582
|
const log2 = (n) => Math.log(n) / Math.log(2);
|
|
7522
|
-
const metricNNDescent = makeNNDescent(distanceFn, this.random);
|
|
7583
|
+
const metricNNDescent = makeNNDescent(distanceFn, this.random, this.useWasmNNDescent);
|
|
7523
7584
|
const round = (n) => {
|
|
7524
7585
|
return n === 0.5 ? 0 : Math.round(n);
|
|
7525
7586
|
};
|
package/lib/umap-js.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.UMAP=e():t.UMAP=e()}(this,()=>(()=>{var t={659(t){function e(t){return Promise.resolve().then(()=>{var e=new Error("Cannot find module '"+t+"'");throw e.code="MODULE_NOT_FOUND",e})}e.keys=()=>[],e.resolve=e,e.id=659,t.exports=e},673(t,e,r){"use strict";var s=r(788),o=r(718);const n=" ".repeat(2),i=" ".repeat(4);function h(t,e={}){const{maxRows:r=15,maxColumns:s=10,maxNumSize:o=8,padMinus:h="auto"}=e;return`${t.constructor.name} {\n${n}[\n${i}${function(t,e,r,s,o){const{rows:n,columns:h}=t,l=Math.min(n,e),u=Math.min(h,r),c=[];if("auto"===o){o=!1;t:for(let e=0;e<l;e++)for(let r=0;r<u;r++)if(t.get(e,r)<0){o=!0;break t}}for(let e=0;e<l;e++){let r=[];for(let n=0;n<u;n++)r.push(a(t.get(e,n),s,o));c.push(`${r.join(" ")}`)}return u!==h&&(c[c.length-1]+=` ... ${h-r} more columns`),l!==n&&c.push(`... ${n-e} more rows`),c.join(`\n${i}`)}(t,r,s,o,h)}\n${n}]\n${n}rows: ${t.rows}\n${n}columns: ${t.columns}\n}`}function a(t,e,r){return(t>=0&&r?` ${l(t,e-1)}`:l(t,e)).padEnd(e)}function l(t,e){let r=t.toString();if(r.length<=e)return r;let s=t.toFixed(e);if(s.length>e&&(s=t.toFixed(Math.max(0,e-(s.length-e)))),s.length<=e&&!s.startsWith("0.000")&&!s.startsWith("-0.000"))return s;let o=t.toExponential(e);return o.length>e&&(o=t.toExponential(Math.max(0,e-(o.length-e)))),o.slice(0)}function u(t,e,r){let s=r?t.rows:t.rows-1;if(e<0||e>s)throw new RangeError("Row index out of range")}function c(t,e,r){let s=r?t.columns:t.columns-1;if(e<0||e>s)throw new RangeError("Column index out of range")}function f(t,e){if(e.to1DArray&&(e=e.to1DArray()),e.length!==t.columns)throw new RangeError("vector size must be the same as the number of columns");return e}function m(t,e){if(e.to1DArray&&(e=e.to1DArray()),e.length!==t.rows)throw new RangeError("vector size must be the same as the number of rows");return e}function g(t,e){if(!s.isAnyArray(e))throw new TypeError("row indices must be an array");for(let r=0;r<e.length;r++)if(e[r]<0||e[r]>=t.rows)throw new RangeError("row indices are out of range")}function w(t,e){if(!s.isAnyArray(e))throw new TypeError("column indices must be an array");for(let r=0;r<e.length;r++)if(e[r]<0||e[r]>=t.columns)throw new RangeError("column indices are out of range")}function p(t,e,r,s,o){if(5!==arguments.length)throw new RangeError("expected 4 arguments");if(y("startRow",e),y("endRow",r),y("startColumn",s),y("endColumn",o),e>r||s>o||e<0||e>=t.rows||r<0||r>=t.rows||s<0||s>=t.columns||o<0||o>=t.columns)throw new RangeError("Submatrix indices are out of range")}function d(t,e=0){let r=[];for(let s=0;s<t;s++)r.push(e);return r}function y(t,e){if("number"!=typeof e)throw new TypeError(`${t} must be a number`)}function b(t){if(t.isEmpty())throw new Error("Empty matrix has no elements to index")}class M{static from1DArray(t,e,r){if(t*e!==r.length)throw new RangeError("data length does not match given dimensions");let s=new E(t,e);for(let o=0;o<t;o++)for(let t=0;t<e;t++)s.set(o,t,r[o*e+t]);return s}static rowVector(t){let e=new E(1,t.length);for(let r=0;r<t.length;r++)e.set(0,r,t[r]);return e}static columnVector(t){let e=new E(t.length,1);for(let r=0;r<t.length;r++)e.set(r,0,t[r]);return e}static zeros(t,e){return new E(t,e)}static ones(t,e){return new E(t,e).fill(1)}static rand(t,e,r={}){if("object"!=typeof r)throw new TypeError("options must be an object");const{random:s=Math.random}=r;let o=new E(t,e);for(let r=0;r<t;r++)for(let t=0;t<e;t++)o.set(r,t,s());return o}static randInt(t,e,r={}){if("object"!=typeof r)throw new TypeError("options must be an object");const{min:s=0,max:o=1e3,random:n=Math.random}=r;if(!Number.isInteger(s))throw new TypeError("min must be an integer");if(!Number.isInteger(o))throw new TypeError("max must be an integer");if(s>=o)throw new RangeError("min must be smaller than max");let i=o-s,h=new E(t,e);for(let r=0;r<t;r++)for(let t=0;t<e;t++){let e=s+Math.round(n()*i);h.set(r,t,e)}return h}static eye(t,e,r){void 0===e&&(e=t),void 0===r&&(r=1);let s=Math.min(t,e),o=this.zeros(t,e);for(let t=0;t<s;t++)o.set(t,t,r);return o}static diag(t,e,r){let s=t.length;void 0===e&&(e=s),void 0===r&&(r=e);let o=Math.min(s,e,r),n=this.zeros(e,r);for(let e=0;e<o;e++)n.set(e,e,t[e]);return n}static min(t,e){t=this.checkMatrix(t),e=this.checkMatrix(e);let r=t.rows,s=t.columns,o=new E(r,s);for(let n=0;n<r;n++)for(let r=0;r<s;r++)o.set(n,r,Math.min(t.get(n,r),e.get(n,r)));return o}static max(t,e){t=this.checkMatrix(t),e=this.checkMatrix(e);let r=t.rows,s=t.columns,o=new this(r,s);for(let n=0;n<r;n++)for(let r=0;r<s;r++)o.set(n,r,Math.max(t.get(n,r),e.get(n,r)));return o}static checkMatrix(t){return M.isMatrix(t)?t:new E(t)}static isMatrix(t){return null!=t&&"Matrix"===t.klass}get size(){return this.rows*this.columns}apply(t){if("function"!=typeof t)throw new TypeError("callback must be a function");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)t.call(this,e,r);return this}to1DArray(){let t=[];for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)t.push(this.get(e,r));return t}to2DArray(){let t=[];for(let e=0;e<this.rows;e++){t.push([]);for(let r=0;r<this.columns;r++)t[e].push(this.get(e,r))}return t}toJSON(){return this.to2DArray()}isRowVector(){return 1===this.rows}isColumnVector(){return 1===this.columns}isVector(){return 1===this.rows||1===this.columns}isSquare(){return this.rows===this.columns}isEmpty(){return 0===this.rows||0===this.columns}isSymmetric(){if(this.isSquare()){for(let t=0;t<this.rows;t++)for(let e=0;e<=t;e++)if(this.get(t,e)!==this.get(e,t))return!1;return!0}return!1}isDistance(){if(!this.isSymmetric())return!1;for(let t=0;t<this.rows;t++)if(0!==this.get(t,t))return!1;return!0}isEchelonForm(){let t=0,e=0,r=-1,s=!0,o=!1;for(;t<this.rows&&s;){for(e=0,o=!1;e<this.columns&&!1===o;)0===this.get(t,e)?e++:1===this.get(t,e)&&e>r?(o=!0,r=e):(s=!1,o=!0);t++}return s}isReducedEchelonForm(){let t=0,e=0,r=-1,s=!0,o=!1;for(;t<this.rows&&s;){for(e=0,o=!1;e<this.columns&&!1===o;)0===this.get(t,e)?e++:1===this.get(t,e)&&e>r?(o=!0,r=e):(s=!1,o=!0);for(let r=e+1;r<this.rows;r++)0!==this.get(t,r)&&(s=!1);t++}return s}echelonForm(){let t=this.clone(),e=0,r=0;for(;e<t.rows&&r<t.columns;){let s=e;for(let o=e;o<t.rows;o++)t.get(o,r)>t.get(s,r)&&(s=o);if(0===t.get(s,r))r++;else{t.swapRows(e,s);let o=t.get(e,r);for(let s=r;s<t.columns;s++)t.set(e,s,t.get(e,s)/o);for(let s=e+1;s<t.rows;s++){let o=t.get(s,r)/t.get(e,r);t.set(s,r,0);for(let n=r+1;n<t.columns;n++)t.set(s,n,t.get(s,n)-t.get(e,n)*o)}e++,r++}}return t}reducedEchelonForm(){let t=this.echelonForm(),e=t.columns,r=t.rows,s=r-1;for(;s>=0;)if(0===t.maxRow(s))s--;else{let o=0,n=!1;for(;o<r&&!1===n;)1===t.get(s,o)?n=!0:o++;for(let r=0;r<s;r++){let n=t.get(r,o);for(let i=o;i<e;i++){let e=t.get(r,i)-n*t.get(s,i);t.set(r,i,e)}}s--}return t}set(){throw new Error("set method is unimplemented")}get(){throw new Error("get method is unimplemented")}repeat(t={}){if("object"!=typeof t)throw new TypeError("options must be an object");const{rows:e=1,columns:r=1}=t;if(!Number.isInteger(e)||e<=0)throw new TypeError("rows must be a positive integer");if(!Number.isInteger(r)||r<=0)throw new TypeError("columns must be a positive integer");let s=new E(this.rows*e,this.columns*r);for(let t=0;t<e;t++)for(let e=0;e<r;e++)s.setSubMatrix(this,this.rows*t,this.columns*e);return s}fill(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,t);return this}neg(){return this.mulS(-1)}getRow(t){u(this,t);let e=[];for(let r=0;r<this.columns;r++)e.push(this.get(t,r));return e}getRowVector(t){return E.rowVector(this.getRow(t))}setRow(t,e){u(this,t),e=f(this,e);for(let r=0;r<this.columns;r++)this.set(t,r,e[r]);return this}swapRows(t,e){u(this,t),u(this,e);for(let r=0;r<this.columns;r++){let s=this.get(t,r);this.set(t,r,this.get(e,r)),this.set(e,r,s)}return this}getColumn(t){c(this,t);let e=[];for(let r=0;r<this.rows;r++)e.push(this.get(r,t));return e}getColumnVector(t){return E.columnVector(this.getColumn(t))}setColumn(t,e){c(this,t),e=m(this,e);for(let r=0;r<this.rows;r++)this.set(r,t,e[r]);return this}swapColumns(t,e){c(this,t),c(this,e);for(let r=0;r<this.rows;r++){let s=this.get(r,t);this.set(r,t,this.get(r,e)),this.set(r,e,s)}return this}addRowVector(t){t=f(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)+t[r]);return this}subRowVector(t){t=f(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)-t[r]);return this}mulRowVector(t){t=f(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)*t[r]);return this}divRowVector(t){t=f(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)/t[r]);return this}addColumnVector(t){t=m(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)+t[e]);return this}subColumnVector(t){t=m(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)-t[e]);return this}mulColumnVector(t){t=m(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)*t[e]);return this}divColumnVector(t){t=m(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)/t[e]);return this}mulRow(t,e){u(this,t);for(let r=0;r<this.columns;r++)this.set(t,r,this.get(t,r)*e);return this}mulColumn(t,e){c(this,t);for(let r=0;r<this.rows;r++)this.set(r,t,this.get(r,t)*e);return this}max(t){if(this.isEmpty())return NaN;switch(t){case"row":{const t=new Array(this.rows).fill(Number.NEGATIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.get(e,r)>t[e]&&(t[e]=this.get(e,r));return t}case"column":{const t=new Array(this.columns).fill(Number.NEGATIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.get(e,r)>t[r]&&(t[r]=this.get(e,r));return t}case void 0:{let t=this.get(0,0);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.get(e,r)>t&&(t=this.get(e,r));return t}default:throw new Error(`invalid option: ${t}`)}}maxIndex(){b(this);let t=this.get(0,0),e=[0,0];for(let r=0;r<this.rows;r++)for(let s=0;s<this.columns;s++)this.get(r,s)>t&&(t=this.get(r,s),e[0]=r,e[1]=s);return e}min(t){if(this.isEmpty())return NaN;switch(t){case"row":{const t=new Array(this.rows).fill(Number.POSITIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.get(e,r)<t[e]&&(t[e]=this.get(e,r));return t}case"column":{const t=new Array(this.columns).fill(Number.POSITIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.get(e,r)<t[r]&&(t[r]=this.get(e,r));return t}case void 0:{let t=this.get(0,0);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.get(e,r)<t&&(t=this.get(e,r));return t}default:throw new Error(`invalid option: ${t}`)}}minIndex(){b(this);let t=this.get(0,0),e=[0,0];for(let r=0;r<this.rows;r++)for(let s=0;s<this.columns;s++)this.get(r,s)<t&&(t=this.get(r,s),e[0]=r,e[1]=s);return e}maxRow(t){if(u(this,t),this.isEmpty())return NaN;let e=this.get(t,0);for(let r=1;r<this.columns;r++)this.get(t,r)>e&&(e=this.get(t,r));return e}maxRowIndex(t){u(this,t),b(this);let e=this.get(t,0),r=[t,0];for(let s=1;s<this.columns;s++)this.get(t,s)>e&&(e=this.get(t,s),r[1]=s);return r}minRow(t){if(u(this,t),this.isEmpty())return NaN;let e=this.get(t,0);for(let r=1;r<this.columns;r++)this.get(t,r)<e&&(e=this.get(t,r));return e}minRowIndex(t){u(this,t),b(this);let e=this.get(t,0),r=[t,0];for(let s=1;s<this.columns;s++)this.get(t,s)<e&&(e=this.get(t,s),r[1]=s);return r}maxColumn(t){if(c(this,t),this.isEmpty())return NaN;let e=this.get(0,t);for(let r=1;r<this.rows;r++)this.get(r,t)>e&&(e=this.get(r,t));return e}maxColumnIndex(t){c(this,t),b(this);let e=this.get(0,t),r=[0,t];for(let s=1;s<this.rows;s++)this.get(s,t)>e&&(e=this.get(s,t),r[0]=s);return r}minColumn(t){if(c(this,t),this.isEmpty())return NaN;let e=this.get(0,t);for(let r=1;r<this.rows;r++)this.get(r,t)<e&&(e=this.get(r,t));return e}minColumnIndex(t){c(this,t),b(this);let e=this.get(0,t),r=[0,t];for(let s=1;s<this.rows;s++)this.get(s,t)<e&&(e=this.get(s,t),r[0]=s);return r}diag(){let t=Math.min(this.rows,this.columns),e=[];for(let r=0;r<t;r++)e.push(this.get(r,r));return e}norm(t="frobenius"){switch(t){case"max":return this.max();case"frobenius":return Math.sqrt(this.dot(this));default:throw new RangeError(`unknown norm type: ${t}`)}}cumulativeSum(){let t=0;for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)t+=this.get(e,r),this.set(e,r,t);return this}dot(t){M.isMatrix(t)&&(t=t.to1DArray());let e=this.to1DArray();if(e.length!==t.length)throw new RangeError("vectors do not have the same size");let r=0;for(let s=0;s<e.length;s++)r+=e[s]*t[s];return r}mmul(t){t=E.checkMatrix(t);let e=this.rows,r=this.columns,s=t.columns,o=new E(e,s),n=new Float64Array(r);for(let i=0;i<s;i++){for(let e=0;e<r;e++)n[e]=t.get(e,i);for(let t=0;t<e;t++){let e=0;for(let s=0;s<r;s++)e+=this.get(t,s)*n[s];o.set(t,i,e)}}return o}mpow(t){if(!this.isSquare())throw new RangeError("Matrix must be square");if(!Number.isInteger(t)||t<0)throw new RangeError("Exponent must be a non-negative integer");let e=E.eye(this.rows),r=this;for(let s=t;s>=1;s/=2)1&s&&(e=e.mmul(r)),r=r.mmul(r);return e}strassen2x2(t){t=E.checkMatrix(t);let e=new E(2,2);const r=this.get(0,0),s=t.get(0,0),o=this.get(0,1),n=t.get(0,1),i=this.get(1,0),h=t.get(1,0),a=this.get(1,1),l=t.get(1,1),u=(r+a)*(s+l),c=(i+a)*s,f=r*(n-l),m=a*(h-s),g=(r+o)*l,w=u+m-g+(o-a)*(h+l),p=f+g,d=c+m,y=u-c+f+(i-r)*(s+n);return e.set(0,0,w),e.set(0,1,p),e.set(1,0,d),e.set(1,1,y),e}strassen3x3(t){t=E.checkMatrix(t);let e=new E(3,3);const r=this.get(0,0),s=this.get(0,1),o=this.get(0,2),n=this.get(1,0),i=this.get(1,1),h=this.get(1,2),a=this.get(2,0),l=this.get(2,1),u=this.get(2,2),c=t.get(0,0),f=t.get(0,1),m=t.get(0,2),g=t.get(1,0),w=t.get(1,1),p=t.get(1,2),d=t.get(2,0),y=t.get(2,1),b=t.get(2,2),M=(r-n)*(-f+w),x=(-r+n+i)*(c-f+w),S=(n+i)*(-c+f),v=r*c,R=(-r+a+l)*(c-m+p),A=(-r+a)*(m-p),N=(a+l)*(-c+m),k=(-o+l+u)*(w+d-y),I=(o-u)*(w-y),z=o*d,T=(l+u)*(-d+y),C=(-o+i+h)*(p+d-b),F=(o-h)*(p-b),V=(i+h)*(-d+b),q=v+z+s*g,D=(r+s+o-n-i-l-u)*w+x+S+v+k+z+T,P=v+R+N+(r+s+o-i-h-a-l)*p+z+C+V,W=M+i*(-c+f+g-w-p-d+b)+x+v+z+C+F,j=M+x+S+v+h*y,L=z+C+F+V+n*m,O=v+R+A+l*(-c+m+g-w-p-d+y)+k+I+z,$=k+I+z+T+a*f,_=v+R+A+N+u*b;return e.set(0,0,q),e.set(0,1,D),e.set(0,2,P),e.set(1,0,W),e.set(1,1,j),e.set(1,2,L),e.set(2,0,O),e.set(2,1,$),e.set(2,2,_),e}mmulStrassen(t){t=E.checkMatrix(t);let e=this.clone(),r=e.rows,s=e.columns,o=t.rows,n=t.columns;function i(t,e,r){let s=t.rows,o=t.columns;if(s===e&&o===r)return t;{let s=M.zeros(e,r);return s=s.setSubMatrix(t,0,0),s}}s!==o&&console.warn(`Multiplying ${r} x ${s} and ${o} x ${n} matrix: dimensions do not match.`);let h=Math.max(r,o),a=Math.max(s,n);return e=i(e,h,a),function t(e,r,s,o){if(s<=512||o<=512)return e.mmul(r);s%2==1&&o%2==1?(e=i(e,s+1,o+1),r=i(r,s+1,o+1)):s%2==1?(e=i(e,s+1,o),r=i(r,s+1,o)):o%2==1&&(e=i(e,s,o+1),r=i(r,s,o+1));let n=parseInt(e.rows/2,10),h=parseInt(e.columns/2,10),a=e.subMatrix(0,n-1,0,h-1),l=r.subMatrix(0,n-1,0,h-1),u=e.subMatrix(0,n-1,h,e.columns-1),c=r.subMatrix(0,n-1,h,r.columns-1),f=e.subMatrix(n,e.rows-1,0,h-1),m=r.subMatrix(n,r.rows-1,0,h-1),g=e.subMatrix(n,e.rows-1,h,e.columns-1),w=r.subMatrix(n,r.rows-1,h,r.columns-1),p=t(M.add(a,g),M.add(l,w),n,h),d=t(M.add(f,g),l,n,h),y=t(a,M.sub(c,w),n,h),b=t(g,M.sub(m,l),n,h),x=t(M.add(a,u),w,n,h),S=t(M.sub(f,a),M.add(l,c),n,h),E=t(M.sub(u,g),M.add(m,w),n,h),v=M.add(p,b);v.sub(x),v.add(E);let R=M.add(y,x),A=M.add(d,b),N=M.sub(p,d);N.add(y),N.add(S);let k=M.zeros(2*v.rows,2*v.columns);return k=k.setSubMatrix(v,0,0),k=k.setSubMatrix(R,v.rows,0),k=k.setSubMatrix(A,0,v.columns),k=k.setSubMatrix(N,v.rows,v.columns),k.subMatrix(0,s-1,0,o-1)}(e,t=i(t,h,a),h,a)}scaleRows(t={}){if("object"!=typeof t)throw new TypeError("options must be an object");const{min:e=0,max:r=1}=t;if(!Number.isFinite(e))throw new TypeError("min must be a number");if(!Number.isFinite(r))throw new TypeError("max must be a number");if(e>=r)throw new RangeError("min must be smaller than max");let s=new E(this.rows,this.columns);for(let t=0;t<this.rows;t++){const n=this.getRow(t);n.length>0&&o(n,{min:e,max:r,output:n}),s.setRow(t,n)}return s}scaleColumns(t={}){if("object"!=typeof t)throw new TypeError("options must be an object");const{min:e=0,max:r=1}=t;if(!Number.isFinite(e))throw new TypeError("min must be a number");if(!Number.isFinite(r))throw new TypeError("max must be a number");if(e>=r)throw new RangeError("min must be smaller than max");let s=new E(this.rows,this.columns);for(let t=0;t<this.columns;t++){const n=this.getColumn(t);n.length&&o(n,{min:e,max:r,output:n}),s.setColumn(t,n)}return s}flipRows(){const t=Math.ceil(this.columns/2);for(let e=0;e<this.rows;e++)for(let r=0;r<t;r++){let t=this.get(e,r),s=this.get(e,this.columns-1-r);this.set(e,r,s),this.set(e,this.columns-1-r,t)}return this}flipColumns(){const t=Math.ceil(this.rows/2);for(let e=0;e<this.columns;e++)for(let r=0;r<t;r++){let t=this.get(r,e),s=this.get(this.rows-1-r,e);this.set(r,e,s),this.set(this.rows-1-r,e,t)}return this}kroneckerProduct(t){t=E.checkMatrix(t);let e=this.rows,r=this.columns,s=t.rows,o=t.columns,n=new E(e*s,r*o);for(let i=0;i<e;i++)for(let e=0;e<r;e++)for(let r=0;r<s;r++)for(let h=0;h<o;h++)n.set(s*i+r,o*e+h,this.get(i,e)*t.get(r,h));return n}kroneckerSum(t){if(t=E.checkMatrix(t),!this.isSquare()||!t.isSquare())throw new Error("Kronecker Sum needs two Square Matrices");let e=this.rows,r=t.rows,s=this.kroneckerProduct(E.eye(r,r)),o=E.eye(e,e).kroneckerProduct(t);return s.add(o)}transpose(){let t=new E(this.columns,this.rows);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)t.set(r,e,this.get(e,r));return t}sortRows(t=x){for(let e=0;e<this.rows;e++)this.setRow(e,this.getRow(e).sort(t));return this}sortColumns(t=x){for(let e=0;e<this.columns;e++)this.setColumn(e,this.getColumn(e).sort(t));return this}subMatrix(t,e,r,s){p(this,t,e,r,s);let o=new E(e-t+1,s-r+1);for(let n=t;n<=e;n++)for(let e=r;e<=s;e++)o.set(n-t,e-r,this.get(n,e));return o}subMatrixRow(t,e,r){if(void 0===e&&(e=0),void 0===r&&(r=this.columns-1),e>r||e<0||e>=this.columns||r<0||r>=this.columns)throw new RangeError("Argument out of range");let s=new E(t.length,r-e+1);for(let o=0;o<t.length;o++)for(let n=e;n<=r;n++){if(t[o]<0||t[o]>=this.rows)throw new RangeError(`Row index out of range: ${t[o]}`);s.set(o,n-e,this.get(t[o],n))}return s}subMatrixColumn(t,e,r){if(void 0===e&&(e=0),void 0===r&&(r=this.rows-1),e>r||e<0||e>=this.rows||r<0||r>=this.rows)throw new RangeError("Argument out of range");let s=new E(r-e+1,t.length);for(let o=0;o<t.length;o++)for(let n=e;n<=r;n++){if(t[o]<0||t[o]>=this.columns)throw new RangeError(`Column index out of range: ${t[o]}`);s.set(n-e,o,this.get(n,t[o]))}return s}setSubMatrix(t,e,r){if((t=E.checkMatrix(t)).isEmpty())return this;p(this,e,e+t.rows-1,r,r+t.columns-1);for(let s=0;s<t.rows;s++)for(let o=0;o<t.columns;o++)this.set(e+s,r+o,t.get(s,o));return this}selection(t,e){g(this,t),w(this,e);let r=new E(t.length,e.length);for(let s=0;s<t.length;s++){let o=t[s];for(let t=0;t<e.length;t++){let n=e[t];r.set(s,t,this.get(o,n))}}return r}trace(){let t=Math.min(this.rows,this.columns),e=0;for(let r=0;r<t;r++)e+=this.get(r,r);return e}clone(){return this.constructor.copy(this,new E(this.rows,this.columns))}static copy(t,e){for(const[r,s,o]of t.entries())e.set(r,s,o);return e}sum(t){switch(t){case"row":return function(t){let e=d(t.rows);for(let r=0;r<t.rows;++r)for(let s=0;s<t.columns;++s)e[r]+=t.get(r,s);return e}(this);case"column":return function(t){let e=d(t.columns);for(let r=0;r<t.rows;++r)for(let s=0;s<t.columns;++s)e[s]+=t.get(r,s);return e}(this);case void 0:return function(t){let e=0;for(let r=0;r<t.rows;r++)for(let s=0;s<t.columns;s++)e+=t.get(r,s);return e}(this);default:throw new Error(`invalid option: ${t}`)}}product(t){switch(t){case"row":return function(t){let e=d(t.rows,1);for(let r=0;r<t.rows;++r)for(let s=0;s<t.columns;++s)e[r]*=t.get(r,s);return e}(this);case"column":return function(t){let e=d(t.columns,1);for(let r=0;r<t.rows;++r)for(let s=0;s<t.columns;++s)e[s]*=t.get(r,s);return e}(this);case void 0:return function(t){let e=1;for(let r=0;r<t.rows;r++)for(let s=0;s<t.columns;s++)e*=t.get(r,s);return e}(this);default:throw new Error(`invalid option: ${t}`)}}mean(t){const e=this.sum(t);switch(t){case"row":for(let t=0;t<this.rows;t++)e[t]/=this.columns;return e;case"column":for(let t=0;t<this.columns;t++)e[t]/=this.rows;return e;case void 0:return e/this.size;default:throw new Error(`invalid option: ${t}`)}}variance(t,e={}){if("object"==typeof t&&(e=t,t=void 0),"object"!=typeof e)throw new TypeError("options must be an object");const{unbiased:r=!0,mean:o=this.mean(t)}=e;if("boolean"!=typeof r)throw new TypeError("unbiased must be a boolean");switch(t){case"row":if(!s.isAnyArray(o))throw new TypeError("mean must be an array");return function(t,e,r){const s=t.rows,o=t.columns,n=[];for(let i=0;i<s;i++){let s=0,h=0,a=0;for(let e=0;e<o;e++)a=t.get(i,e)-r[i],s+=a,h+=a*a;e?n.push((h-s*s/o)/(o-1)):n.push((h-s*s/o)/o)}return n}(this,r,o);case"column":if(!s.isAnyArray(o))throw new TypeError("mean must be an array");return function(t,e,r){const s=t.rows,o=t.columns,n=[];for(let i=0;i<o;i++){let o=0,h=0,a=0;for(let e=0;e<s;e++)a=t.get(e,i)-r[i],o+=a,h+=a*a;e?n.push((h-o*o/s)/(s-1)):n.push((h-o*o/s)/s)}return n}(this,r,o);case void 0:if("number"!=typeof o)throw new TypeError("mean must be a number");return function(t,e,r){const s=t.rows,o=t.columns,n=s*o;let i=0,h=0,a=0;for(let e=0;e<s;e++)for(let s=0;s<o;s++)a=t.get(e,s)-r,i+=a,h+=a*a;return e?(h-i*i/n)/(n-1):(h-i*i/n)/n}(this,r,o);default:throw new Error(`invalid option: ${t}`)}}standardDeviation(t,e){"object"==typeof t&&(e=t,t=void 0);const r=this.variance(t,e);if(void 0===t)return Math.sqrt(r);for(let t=0;t<r.length;t++)r[t]=Math.sqrt(r[t]);return r}center(t,e={}){if("object"==typeof t&&(e=t,t=void 0),"object"!=typeof e)throw new TypeError("options must be an object");const{center:r=this.mean(t)}=e;switch(t){case"row":if(!s.isAnyArray(r))throw new TypeError("center must be an array");return function(t,e){for(let r=0;r<t.rows;r++)for(let s=0;s<t.columns;s++)t.set(r,s,t.get(r,s)-e[r])}(this,r),this;case"column":if(!s.isAnyArray(r))throw new TypeError("center must be an array");return function(t,e){for(let r=0;r<t.rows;r++)for(let s=0;s<t.columns;s++)t.set(r,s,t.get(r,s)-e[s])}(this,r),this;case void 0:if("number"!=typeof r)throw new TypeError("center must be a number");return function(t,e){for(let r=0;r<t.rows;r++)for(let s=0;s<t.columns;s++)t.set(r,s,t.get(r,s)-e)}(this,r),this;default:throw new Error(`invalid option: ${t}`)}}scale(t,e={}){if("object"==typeof t&&(e=t,t=void 0),"object"!=typeof e)throw new TypeError("options must be an object");let r=e.scale;switch(t){case"row":if(void 0===r)r=function(t){const e=[];for(let r=0;r<t.rows;r++){let s=0;for(let e=0;e<t.columns;e++)s+=t.get(r,e)**2/(t.columns-1);e.push(Math.sqrt(s))}return e}(this);else if(!s.isAnyArray(r))throw new TypeError("scale must be an array");return function(t,e){for(let r=0;r<t.rows;r++)for(let s=0;s<t.columns;s++)t.set(r,s,t.get(r,s)/e[r])}(this,r),this;case"column":if(void 0===r)r=function(t){const e=[];for(let r=0;r<t.columns;r++){let s=0;for(let e=0;e<t.rows;e++)s+=t.get(e,r)**2/(t.rows-1);e.push(Math.sqrt(s))}return e}(this);else if(!s.isAnyArray(r))throw new TypeError("scale must be an array");return function(t,e){for(let r=0;r<t.rows;r++)for(let s=0;s<t.columns;s++)t.set(r,s,t.get(r,s)/e[s])}(this,r),this;case void 0:if(void 0===r)r=function(t){const e=t.size-1;let r=0;for(let s=0;s<t.columns;s++)for(let o=0;o<t.rows;o++)r+=t.get(o,s)**2/e;return Math.sqrt(r)}(this);else if("number"!=typeof r)throw new TypeError("scale must be a number");return function(t,e){for(let r=0;r<t.rows;r++)for(let s=0;s<t.columns;s++)t.set(r,s,t.get(r,s)/e)}(this,r),this;default:throw new Error(`invalid option: ${t}`)}}toString(t){return h(this,t)}[Symbol.iterator](){return this.entries()}*entries(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)yield[t,e,this.get(t,e)]}*values(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)yield this.get(t,e)}}function x(t,e){return t-e}function S(t){return t.every(t=>"number"==typeof t)}M.prototype.klass="Matrix","undefined"!=typeof Symbol&&(M.prototype[Symbol.for("nodejs.util.inspect.custom")]=function(){return h(this)}),M.random=M.rand,M.randomInt=M.randInt,M.diagonal=M.diag,M.prototype.diagonal=M.prototype.diag,M.identity=M.eye,M.prototype.negate=M.prototype.neg,M.prototype.tensorProduct=M.prototype.kroneckerProduct;class E extends M{data;#t(t,e){if(this.data=[],!(Number.isInteger(e)&&e>=0))throw new TypeError("nColumns must be a positive integer");for(let r=0;r<t;r++)this.data.push(new Float64Array(e));this.rows=t,this.columns=e}constructor(t,e){if(super(),E.isMatrix(t))this.#t(t.rows,t.columns),E.copy(t,this);else if(Number.isInteger(t)&&t>=0)this.#t(t,e);else{if(!s.isAnyArray(t))throw new TypeError("First argument must be a positive number or an array");{const r=t;if("number"!=typeof(e=(t=r.length)?r[0].length:0))throw new TypeError("Data must be a 2D array with at least one element");this.data=[];for(let s=0;s<t;s++){if(r[s].length!==e)throw new RangeError("Inconsistent array dimensions");if(!S(r[s]))throw new TypeError("Input data contains non-numeric values");this.data.push(Float64Array.from(r[s]))}this.rows=t,this.columns=e}}}set(t,e,r){return this.data[t][e]=r,this}get(t,e){return this.data[t][e]}removeRow(t){return u(this,t),this.data.splice(t,1),this.rows-=1,this}addRow(t,e){return void 0===e&&(e=t,t=this.rows),u(this,t,!0),e=Float64Array.from(f(this,e)),this.data.splice(t,0,e),this.rows+=1,this}removeColumn(t){c(this,t);for(let e=0;e<this.rows;e++){const r=new Float64Array(this.columns-1);for(let s=0;s<t;s++)r[s]=this.data[e][s];for(let s=t+1;s<this.columns;s++)r[s-1]=this.data[e][s];this.data[e]=r}return this.columns-=1,this}addColumn(t,e){void 0===e&&(e=t,t=this.columns),c(this,t,!0),e=m(this,e);for(let r=0;r<this.rows;r++){const s=new Float64Array(this.columns+1);let o=0;for(;o<t;o++)s[o]=this.data[r][o];for(s[o++]=e[r];o<this.columns+1;o++)s[o]=this.data[r][o-1];this.data[r]=s}return this.columns+=1,this}}!function(t,e){t.prototype.add=function(t){return"number"==typeof t?this.addS(t):this.addM(t)},t.prototype.addS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)+t);return this},t.prototype.addM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)+t.get(e,r));return this},t.add=function(t,r){return new e(t).add(r)},t.prototype.sub=function(t){return"number"==typeof t?this.subS(t):this.subM(t)},t.prototype.subS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)-t);return this},t.prototype.subM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)-t.get(e,r));return this},t.sub=function(t,r){return new e(t).sub(r)},t.prototype.subtract=t.prototype.sub,t.prototype.subtractS=t.prototype.subS,t.prototype.subtractM=t.prototype.subM,t.subtract=t.sub,t.prototype.mul=function(t){return"number"==typeof t?this.mulS(t):this.mulM(t)},t.prototype.mulS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)*t);return this},t.prototype.mulM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)*t.get(e,r));return this},t.mul=function(t,r){return new e(t).mul(r)},t.prototype.multiply=t.prototype.mul,t.prototype.multiplyS=t.prototype.mulS,t.prototype.multiplyM=t.prototype.mulM,t.multiply=t.mul,t.prototype.div=function(t){return"number"==typeof t?this.divS(t):this.divM(t)},t.prototype.divS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)/t);return this},t.prototype.divM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)/t.get(e,r));return this},t.div=function(t,r){return new e(t).div(r)},t.prototype.divide=t.prototype.div,t.prototype.divideS=t.prototype.divS,t.prototype.divideM=t.prototype.divM,t.divide=t.div,t.prototype.mod=function(t){return"number"==typeof t?this.modS(t):this.modM(t)},t.prototype.modS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)%t);return this},t.prototype.modM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)%t.get(e,r));return this},t.mod=function(t,r){return new e(t).mod(r)},t.prototype.modulus=t.prototype.mod,t.prototype.modulusS=t.prototype.modS,t.prototype.modulusM=t.prototype.modM,t.modulus=t.mod,t.prototype.and=function(t){return"number"==typeof t?this.andS(t):this.andM(t)},t.prototype.andS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)&t);return this},t.prototype.andM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)&t.get(e,r));return this},t.and=function(t,r){return new e(t).and(r)},t.prototype.or=function(t){return"number"==typeof t?this.orS(t):this.orM(t)},t.prototype.orS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)|t);return this},t.prototype.orM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)|t.get(e,r));return this},t.or=function(t,r){return new e(t).or(r)},t.prototype.xor=function(t){return"number"==typeof t?this.xorS(t):this.xorM(t)},t.prototype.xorS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)^t);return this},t.prototype.xorM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)^t.get(e,r));return this},t.xor=function(t,r){return new e(t).xor(r)},t.prototype.leftShift=function(t){return"number"==typeof t?this.leftShiftS(t):this.leftShiftM(t)},t.prototype.leftShiftS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)<<t);return this},t.prototype.leftShiftM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)<<t.get(e,r));return this},t.leftShift=function(t,r){return new e(t).leftShift(r)},t.prototype.signPropagatingRightShift=function(t){return"number"==typeof t?this.signPropagatingRightShiftS(t):this.signPropagatingRightShiftM(t)},t.prototype.signPropagatingRightShiftS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)>>t);return this},t.prototype.signPropagatingRightShiftM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)>>t.get(e,r));return this},t.signPropagatingRightShift=function(t,r){return new e(t).signPropagatingRightShift(r)},t.prototype.rightShift=function(t){return"number"==typeof t?this.rightShiftS(t):this.rightShiftM(t)},t.prototype.rightShiftS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)>>>t);return this},t.prototype.rightShiftM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)>>>t.get(e,r));return this},t.rightShift=function(t,r){return new e(t).rightShift(r)},t.prototype.zeroFillRightShift=t.prototype.rightShift,t.prototype.zeroFillRightShiftS=t.prototype.rightShiftS,t.prototype.zeroFillRightShiftM=t.prototype.rightShiftM,t.zeroFillRightShift=t.rightShift,t.prototype.not=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,~this.get(t,e));return this},t.not=function(t){return new e(t).not()},t.prototype.abs=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.abs(this.get(t,e)));return this},t.abs=function(t){return new e(t).abs()},t.prototype.acos=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.acos(this.get(t,e)));return this},t.acos=function(t){return new e(t).acos()},t.prototype.acosh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.acosh(this.get(t,e)));return this},t.acosh=function(t){return new e(t).acosh()},t.prototype.asin=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.asin(this.get(t,e)));return this},t.asin=function(t){return new e(t).asin()},t.prototype.asinh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.asinh(this.get(t,e)));return this},t.asinh=function(t){return new e(t).asinh()},t.prototype.atan=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.atan(this.get(t,e)));return this},t.atan=function(t){return new e(t).atan()},t.prototype.atanh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.atanh(this.get(t,e)));return this},t.atanh=function(t){return new e(t).atanh()},t.prototype.cbrt=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.cbrt(this.get(t,e)));return this},t.cbrt=function(t){return new e(t).cbrt()},t.prototype.ceil=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.ceil(this.get(t,e)));return this},t.ceil=function(t){return new e(t).ceil()},t.prototype.clz32=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.clz32(this.get(t,e)));return this},t.clz32=function(t){return new e(t).clz32()},t.prototype.cos=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.cos(this.get(t,e)));return this},t.cos=function(t){return new e(t).cos()},t.prototype.cosh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.cosh(this.get(t,e)));return this},t.cosh=function(t){return new e(t).cosh()},t.prototype.exp=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.exp(this.get(t,e)));return this},t.exp=function(t){return new e(t).exp()},t.prototype.expm1=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.expm1(this.get(t,e)));return this},t.expm1=function(t){return new e(t).expm1()},t.prototype.floor=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.floor(this.get(t,e)));return this},t.floor=function(t){return new e(t).floor()},t.prototype.fround=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.fround(this.get(t,e)));return this},t.fround=function(t){return new e(t).fround()},t.prototype.log=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.log(this.get(t,e)));return this},t.log=function(t){return new e(t).log()},t.prototype.log1p=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.log1p(this.get(t,e)));return this},t.log1p=function(t){return new e(t).log1p()},t.prototype.log10=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.log10(this.get(t,e)));return this},t.log10=function(t){return new e(t).log10()},t.prototype.log2=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.log2(this.get(t,e)));return this},t.log2=function(t){return new e(t).log2()},t.prototype.round=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.round(this.get(t,e)));return this},t.round=function(t){return new e(t).round()},t.prototype.sign=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.sign(this.get(t,e)));return this},t.sign=function(t){return new e(t).sign()},t.prototype.sin=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.sin(this.get(t,e)));return this},t.sin=function(t){return new e(t).sin()},t.prototype.sinh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.sinh(this.get(t,e)));return this},t.sinh=function(t){return new e(t).sinh()},t.prototype.sqrt=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.sqrt(this.get(t,e)));return this},t.sqrt=function(t){return new e(t).sqrt()},t.prototype.tan=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.tan(this.get(t,e)));return this},t.tan=function(t){return new e(t).tan()},t.prototype.tanh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.tanh(this.get(t,e)));return this},t.tanh=function(t){return new e(t).tanh()},t.prototype.trunc=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.trunc(this.get(t,e)));return this},t.trunc=function(t){return new e(t).trunc()},t.pow=function(t,r){return new e(t).pow(r)},t.prototype.pow=function(t){return"number"==typeof t?this.powS(t):this.powM(t)},t.prototype.powS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)**t);return this},t.prototype.powM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)**t.get(e,r));return this}}(M,E);class v extends M{#e;get size(){return this.#e.size}get rows(){return this.#e.rows}get columns(){return this.#e.columns}get diagonalSize(){return this.rows}static isSymmetricMatrix(t){return E.isMatrix(t)&&"SymmetricMatrix"===t.klassType}static zeros(t){return new this(t)}static ones(t){return new this(t).fill(1)}constructor(t){if(super(),E.isMatrix(t)){if(!t.isSymmetric())throw new TypeError("not symmetric data");this.#e=E.copy(t,new E(t.rows,t.rows))}else if(Number.isInteger(t)&&t>=0)this.#e=new E(t,t);else if(this.#e=new E(t),!this.isSymmetric())throw new TypeError("not symmetric data")}clone(){const t=new v(this.diagonalSize);for(const[e,r,s]of this.upperRightEntries())t.set(e,r,s);return t}toMatrix(){return new E(this)}get(t,e){return this.#e.get(t,e)}set(t,e,r){return this.#e.set(t,e,r),this.#e.set(e,t,r),this}removeCross(t){return this.#e.removeRow(t),this.#e.removeColumn(t),this}addCross(t,e){void 0===e&&(e=t,t=this.diagonalSize);const r=e.slice();return r.splice(t,1),this.#e.addRow(t,r),this.#e.addColumn(t,e),this}applyMask(t){if(t.length!==this.diagonalSize)throw new RangeError("Mask size do not match with matrix size");const e=[];for(const[r,s]of t.entries())s||e.push(r);e.reverse();for(const t of e)this.removeCross(t);return this}toCompact(){const{diagonalSize:t}=this,e=new Array(t*(t+1)/2);for(let r=0,s=0,o=0;o<e.length;o++)e[o]=this.get(s,r),++r>=t&&(r=++s);return e}static fromCompact(t){const e=t.length,r=(Math.sqrt(8*e+1)-1)/2;if(!Number.isInteger(r))throw new TypeError(`This array is not a compact representation of a Symmetric Matrix, ${JSON.stringify(t)}`);const s=new v(r);for(let o=0,n=0,i=0;i<e;i++)s.set(o,n,t[i]),++o>=r&&(o=++n);return s}*upperRightEntries(){for(let t=0,e=0;t<this.diagonalSize;void 0){const r=this.get(t,e);yield[t,e,r],++e>=this.diagonalSize&&(e=++t)}}*upperRightValues(){for(let t=0,e=0;t<this.diagonalSize;void 0){const r=this.get(t,e);yield r,++e>=this.diagonalSize&&(e=++t)}}}v.prototype.klassType="SymmetricMatrix";class R extends v{static isDistanceMatrix(t){return v.isSymmetricMatrix(t)&&"DistanceMatrix"===t.klassSubType}constructor(t){if(super(t),!this.isDistance())throw new TypeError("Provided arguments do no produce a distance matrix")}set(t,e,r){return t===e&&(r=0),super.set(t,e,r)}addCross(t,e){return void 0===e&&(e=t,t=this.diagonalSize),(e=e.slice())[t]=0,super.addCross(t,e)}toSymmetricMatrix(){return new v(this)}clone(){const t=new R(this.diagonalSize);for(const[e,r,s]of this.upperRightEntries())e!==r&&t.set(e,r,s);return t}toCompact(){const{diagonalSize:t}=this,e=new Array((t-1)*t/2);for(let r=1,s=0,o=0;o<e.length;o++)e[o]=this.get(s,r),++r>=t&&(r=1+ ++s);return e}static fromCompact(t){const e=t.length;if(0===e)return new this(0);const r=(Math.sqrt(8*e+1)+1)/2;if(!Number.isInteger(r))throw new TypeError(`This array is not a compact representation of a DistanceMatrix, ${JSON.stringify(t)}`);const s=new this(r);for(let o=1,n=0,i=0;i<e;i++)s.set(o,n,t[i]),++o>=r&&(o=1+ ++n);return s}}R.prototype.klassSubType="DistanceMatrix";class A extends M{constructor(t,e,r){super(),this.matrix=t,this.rows=e,this.columns=r}}class N extends A{constructor(t,e,r){g(t,e),w(t,r),super(t,e.length,r.length),this.rowIndices=e,this.columnIndices=r}set(t,e,r){return this.matrix.set(this.rowIndices[t],this.columnIndices[e],r),this}get(t,e){return this.matrix.get(this.rowIndices[t],this.columnIndices[e])}}class k extends M{constructor(t,e={}){const{rows:r=1}=e;if(t.length%r!==0)throw new Error("the data length is not divisible by the number of rows");super(),this.rows=r,this.columns=t.length/r,this.data=t}set(t,e,r){let s=this._calculateIndex(t,e);return this.data[s]=r,this}get(t,e){let r=this._calculateIndex(t,e);return this.data[r]}_calculateIndex(t,e){return t*this.columns+e}}class I extends M{constructor(t){super(),this.data=t,this.rows=t.length,this.columns=t[0].length}set(t,e,r){return this.data[t][e]=r,this}get(t,e){return this.data[t][e]}}class z{constructor(t){let e,r,s,o,n,i,h,a,l,u=(t=I.checkMatrix(t)).clone(),c=u.rows,f=u.columns,m=new Float64Array(c),g=1;for(e=0;e<c;e++)m[e]=e;for(a=new Float64Array(c),r=0;r<f;r++){for(e=0;e<c;e++)a[e]=u.get(e,r);for(e=0;e<c;e++){for(l=Math.min(e,r),n=0,s=0;s<l;s++)n+=u.get(e,s)*a[s];a[e]-=n,u.set(e,r,a[e])}for(o=r,e=r+1;e<c;e++)Math.abs(a[e])>Math.abs(a[o])&&(o=e);if(o!==r){for(s=0;s<f;s++)i=u.get(o,s),u.set(o,s,u.get(r,s)),u.set(r,s,i);h=m[o],m[o]=m[r],m[r]=h,g=-g}if(r<c&&0!==u.get(r,r))for(e=r+1;e<c;e++)u.set(e,r,u.get(e,r)/u.get(r,r))}this.LU=u,this.pivotVector=m,this.pivotSign=g}isSingular(){let t=this.LU,e=t.columns;for(let r=0;r<e;r++)if(0===t.get(r,r))return!0;return!1}solve(t){t=E.checkMatrix(t);let e=this.LU;if(e.rows!==t.rows)throw new Error("Invalid matrix dimensions");if(this.isSingular())throw new Error("LU matrix is singular");let r,s,o,n=t.columns,i=t.subMatrixRow(this.pivotVector,0,n-1),h=e.columns;for(o=0;o<h;o++)for(r=o+1;r<h;r++)for(s=0;s<n;s++)i.set(r,s,i.get(r,s)-i.get(o,s)*e.get(r,o));for(o=h-1;o>=0;o--){for(s=0;s<n;s++)i.set(o,s,i.get(o,s)/e.get(o,o));for(r=0;r<o;r++)for(s=0;s<n;s++)i.set(r,s,i.get(r,s)-i.get(o,s)*e.get(r,o))}return i}get determinant(){let t=this.LU;if(!t.isSquare())throw new Error("Matrix must be square");let e=this.pivotSign,r=t.columns;for(let s=0;s<r;s++)e*=t.get(s,s);return e}get lowerTriangularMatrix(){let t=this.LU,e=t.rows,r=t.columns,s=new E(e,r);for(let o=0;o<e;o++)for(let e=0;e<r;e++)o>e?s.set(o,e,t.get(o,e)):o===e?s.set(o,e,1):s.set(o,e,0);return s}get upperTriangularMatrix(){let t=this.LU,e=t.rows,r=t.columns,s=new E(e,r);for(let o=0;o<e;o++)for(let e=0;e<r;e++)o<=e?s.set(o,e,t.get(o,e)):s.set(o,e,0);return s}get pivotPermutationVector(){return Array.from(this.pivotVector)}}function T(t,e){let r=0;return Math.abs(t)>Math.abs(e)?(r=e/t,Math.abs(t)*Math.sqrt(1+r*r)):0!==e?(r=t/e,Math.abs(e)*Math.sqrt(1+r*r)):0}class C{constructor(t){let e,r,s,o,n=(t=I.checkMatrix(t)).clone(),i=t.rows,h=t.columns,a=new Float64Array(h);for(s=0;s<h;s++){let t=0;for(e=s;e<i;e++)t=T(t,n.get(e,s));if(0!==t){for(n.get(s,s)<0&&(t=-t),e=s;e<i;e++)n.set(e,s,n.get(e,s)/t);for(n.set(s,s,n.get(s,s)+1),r=s+1;r<h;r++){for(o=0,e=s;e<i;e++)o+=n.get(e,s)*n.get(e,r);for(o=-o/n.get(s,s),e=s;e<i;e++)n.set(e,r,n.get(e,r)+o*n.get(e,s))}}a[s]=-t}this.QR=n,this.Rdiag=a}solve(t){t=E.checkMatrix(t);let e=this.QR,r=e.rows;if(t.rows!==r)throw new Error("Matrix row dimensions must agree");if(!this.isFullRank())throw new Error("Matrix is rank deficient");let s,o,n,i,h=t.columns,a=t.clone(),l=e.columns;for(n=0;n<l;n++)for(o=0;o<h;o++){for(i=0,s=n;s<r;s++)i+=e.get(s,n)*a.get(s,o);for(i=-i/e.get(n,n),s=n;s<r;s++)a.set(s,o,a.get(s,o)+i*e.get(s,n))}for(n=l-1;n>=0;n--){for(o=0;o<h;o++)a.set(n,o,a.get(n,o)/this.Rdiag[n]);for(s=0;s<n;s++)for(o=0;o<h;o++)a.set(s,o,a.get(s,o)-a.get(n,o)*e.get(s,n))}return a.subMatrix(0,l-1,0,h-1)}isFullRank(){let t=this.QR.columns;for(let e=0;e<t;e++)if(0===this.Rdiag[e])return!1;return!0}get upperTriangularMatrix(){let t,e,r=this.QR,s=r.columns,o=new E(s,s);for(t=0;t<s;t++)for(e=0;e<s;e++)t<e?o.set(t,e,r.get(t,e)):t===e?o.set(t,e,this.Rdiag[t]):o.set(t,e,0);return o}get orthogonalMatrix(){let t,e,r,s,o=this.QR,n=o.rows,i=o.columns,h=new E(n,i);for(r=i-1;r>=0;r--){for(t=0;t<n;t++)h.set(t,r,0);for(h.set(r,r,1),e=r;e<i;e++)if(0!==o.get(r,r)){for(s=0,t=r;t<n;t++)s+=o.get(t,r)*h.get(t,e);for(s=-s/o.get(r,r),t=r;t<n;t++)h.set(t,e,h.get(t,e)+s*o.get(t,r))}}return h}}class F{constructor(t,e={}){if((t=I.checkMatrix(t)).isEmpty())throw new Error("Matrix must be non-empty");let r=t.rows,s=t.columns;const{computeLeftSingularVectors:o=!0,computeRightSingularVectors:n=!0,autoTranspose:i=!1}=e;let h,a=Boolean(o),l=Boolean(n),u=!1;if(r<s)if(i){h=t.transpose(),r=h.rows,s=h.columns,u=!0;let e=a;a=l,l=e}else h=t.clone(),console.warn("Computing SVD on a matrix with more columns than rows. Consider enabling autoTranspose");else h=t.clone();let c=Math.min(r,s),f=Math.min(r+1,s),m=new Float64Array(f),g=new E(r,c),w=new E(s,s),p=new Float64Array(s),d=new Float64Array(r),y=new Float64Array(f);for(let t=0;t<f;t++)y[t]=t;let b=Math.min(r-1,s),M=Math.max(0,Math.min(s-2,r)),x=Math.max(b,M);for(let t=0;t<x;t++){if(t<b){m[t]=0;for(let e=t;e<r;e++)m[t]=T(m[t],h.get(e,t));if(0!==m[t]){h.get(t,t)<0&&(m[t]=-m[t]);for(let e=t;e<r;e++)h.set(e,t,h.get(e,t)/m[t]);h.set(t,t,h.get(t,t)+1)}m[t]=-m[t]}for(let e=t+1;e<s;e++){if(t<b&&0!==m[t]){let s=0;for(let o=t;o<r;o++)s+=h.get(o,t)*h.get(o,e);s=-s/h.get(t,t);for(let o=t;o<r;o++)h.set(o,e,h.get(o,e)+s*h.get(o,t))}p[e]=h.get(t,e)}if(a&&t<b)for(let e=t;e<r;e++)g.set(e,t,h.get(e,t));if(t<M){p[t]=0;for(let e=t+1;e<s;e++)p[t]=T(p[t],p[e]);if(0!==p[t]){p[t+1]<0&&(p[t]=0-p[t]);for(let e=t+1;e<s;e++)p[e]/=p[t];p[t+1]+=1}if(p[t]=-p[t],t+1<r&&0!==p[t]){for(let e=t+1;e<r;e++)d[e]=0;for(let e=t+1;e<r;e++)for(let r=t+1;r<s;r++)d[e]+=p[r]*h.get(e,r);for(let e=t+1;e<s;e++){let s=-p[e]/p[t+1];for(let o=t+1;o<r;o++)h.set(o,e,h.get(o,e)+s*d[o])}}if(l)for(let e=t+1;e<s;e++)w.set(e,t,p[e])}}let S=Math.min(s,r+1);if(b<s&&(m[b]=h.get(b,b)),r<S&&(m[S-1]=0),M+1<S&&(p[M]=h.get(M,S-1)),p[S-1]=0,a){for(let t=b;t<c;t++){for(let e=0;e<r;e++)g.set(e,t,0);g.set(t,t,1)}for(let t=b-1;t>=0;t--)if(0!==m[t]){for(let e=t+1;e<c;e++){let s=0;for(let o=t;o<r;o++)s+=g.get(o,t)*g.get(o,e);s=-s/g.get(t,t);for(let o=t;o<r;o++)g.set(o,e,g.get(o,e)+s*g.get(o,t))}for(let e=t;e<r;e++)g.set(e,t,-g.get(e,t));g.set(t,t,1+g.get(t,t));for(let e=0;e<t-1;e++)g.set(e,t,0)}else{for(let e=0;e<r;e++)g.set(e,t,0);g.set(t,t,1)}}if(l)for(let t=s-1;t>=0;t--){if(t<M&&0!==p[t])for(let e=t+1;e<s;e++){let r=0;for(let o=t+1;o<s;o++)r+=w.get(o,t)*w.get(o,e);r=-r/w.get(t+1,t);for(let o=t+1;o<s;o++)w.set(o,e,w.get(o,e)+r*w.get(o,t))}for(let e=0;e<s;e++)w.set(e,t,0);w.set(t,t,1)}let v=S-1,R=Number.EPSILON;for(;S>0;){let t,e;for(t=S-2;t>=-1&&-1!==t;t--){const e=Number.MIN_VALUE+R*Math.abs(m[t]+Math.abs(m[t+1]));if(Math.abs(p[t])<=e||Number.isNaN(p[t])){p[t]=0;break}}if(t===S-2)e=4;else{let r;for(r=S-1;r>=t&&r!==t;r--){let e=(r!==S?Math.abs(p[r]):0)+(r!==t+1?Math.abs(p[r-1]):0);if(Math.abs(m[r])<=R*e){m[r]=0;break}}r===t?e=3:r===S-1?e=1:(e=2,t=r)}switch(t++,e){case 1:{let e=p[S-2];p[S-2]=0;for(let r=S-2;r>=t;r--){let o=T(m[r],e),n=m[r]/o,i=e/o;if(m[r]=o,r!==t&&(e=-i*p[r-1],p[r-1]=n*p[r-1]),l)for(let t=0;t<s;t++)o=n*w.get(t,r)+i*w.get(t,S-1),w.set(t,S-1,-i*w.get(t,r)+n*w.get(t,S-1)),w.set(t,r,o)}break}case 2:{let e=p[t-1];p[t-1]=0;for(let s=t;s<S;s++){let o=T(m[s],e),n=m[s]/o,i=e/o;if(m[s]=o,e=-i*p[s],p[s]=n*p[s],a)for(let e=0;e<r;e++)o=n*g.get(e,s)+i*g.get(e,t-1),g.set(e,t-1,-i*g.get(e,s)+n*g.get(e,t-1)),g.set(e,s,o)}break}case 3:{const e=Math.max(Math.abs(m[S-1]),Math.abs(m[S-2]),Math.abs(p[S-2]),Math.abs(m[t]),Math.abs(p[t])),o=m[S-1]/e,n=m[S-2]/e,i=p[S-2]/e,h=m[t]/e,u=p[t]/e,c=((n+o)*(n-o)+i*i)/2,f=o*i*(o*i);let d=0;0===c&&0===f||(d=c<0?0-Math.sqrt(c*c+f):Math.sqrt(c*c+f),d=f/(c+d));let y=(h+o)*(h-o)+d,b=h*u;for(let e=t;e<S-1;e++){let o=T(y,b);0===o&&(o=Number.MIN_VALUE);let n=y/o,i=b/o;if(e!==t&&(p[e-1]=o),y=n*m[e]+i*p[e],p[e]=n*p[e]-i*m[e],b=i*m[e+1],m[e+1]=n*m[e+1],l)for(let t=0;t<s;t++)o=n*w.get(t,e)+i*w.get(t,e+1),w.set(t,e+1,-i*w.get(t,e)+n*w.get(t,e+1)),w.set(t,e,o);if(o=T(y,b),0===o&&(o=Number.MIN_VALUE),n=y/o,i=b/o,m[e]=o,y=n*p[e]+i*m[e+1],m[e+1]=-i*p[e]+n*m[e+1],b=i*p[e+1],p[e+1]=n*p[e+1],a&&e<r-1)for(let t=0;t<r;t++)o=n*g.get(t,e)+i*g.get(t,e+1),g.set(t,e+1,-i*g.get(t,e)+n*g.get(t,e+1)),g.set(t,e,o)}p[S-2]=y;break}case 4:if(m[t]<=0&&(m[t]=m[t]<0?-m[t]:0,l))for(let e=0;e<=v;e++)w.set(e,t,-w.get(e,t));for(;t<v&&!(m[t]>=m[t+1]);){let e=m[t];if(m[t]=m[t+1],m[t+1]=e,l&&t<s-1)for(let r=0;r<s;r++)e=w.get(r,t+1),w.set(r,t+1,w.get(r,t)),w.set(r,t,e);if(a&&t<r-1)for(let s=0;s<r;s++)e=g.get(s,t+1),g.set(s,t+1,g.get(s,t)),g.set(s,t,e);t++}S--}}if(u){let t=w;w=g,g=t}this.m=r,this.n=s,this.s=m,this.U=g,this.V=w}solve(t){let e=t,r=this.threshold,s=this.s.length,o=E.zeros(s,s);for(let t=0;t<s;t++)Math.abs(this.s[t])<=r?o.set(t,t,0):o.set(t,t,1/this.s[t]);let n=this.U,i=this.rightSingularVectors,h=i.mmul(o),a=i.rows,l=n.rows,u=E.zeros(a,l);for(let t=0;t<a;t++)for(let e=0;e<l;e++){let r=0;for(let o=0;o<s;o++)r+=h.get(t,o)*n.get(e,o);u.set(t,e,r)}return u.mmul(e)}solveForDiagonal(t){return this.solve(E.diag(t))}inverse(){let t=this.V,e=this.threshold,r=t.rows,s=t.columns,o=new E(r,this.s.length);for(let n=0;n<r;n++)for(let r=0;r<s;r++)Math.abs(this.s[r])>e&&o.set(n,r,t.get(n,r)/this.s[r]);let n=this.U,i=n.rows,h=n.columns,a=new E(r,i);for(let t=0;t<r;t++)for(let e=0;e<i;e++){let r=0;for(let s=0;s<h;s++)r+=o.get(t,s)*n.get(e,s);a.set(t,e,r)}return a}get condition(){return this.s[0]/this.s[Math.min(this.m,this.n)-1]}get norm2(){return this.s[0]}get rank(){let t=Math.max(this.m,this.n)*this.s[0]*Number.EPSILON,e=0,r=this.s;for(let s=0,o=r.length;s<o;s++)r[s]>t&&e++;return e}get diagonal(){return Array.from(this.s)}get threshold(){return Number.EPSILON/2*Math.max(this.m,this.n)*this.s[0]}get leftSingularVectors(){return this.U}get rightSingularVectors(){return this.V}get diagonalMatrix(){return E.diag(this.s)}}function V(t,e,r=!1){return t=I.checkMatrix(t),e=I.checkMatrix(e),r?new F(t).solve(e):t.isSquare()?new z(t).solve(e):new C(t).solve(e)}function q(t,e){let r=[];for(let s=0;s<t;s++)s!==e&&r.push(s);return r}function D(t,e,r,s=1e-9,o=1e-9){if(t>o)return new Array(e.rows+1).fill(0);{let t=e.addRow(r,[0]);for(let e=0;e<t.rows;e++)Math.abs(t.get(e,0))<s&&t.set(e,0,0);return t.to1DArray()}}class P{constructor(t,e={}){const{assumeSymmetric:r=!1}=e;if(!(t=I.checkMatrix(t)).isSquare())throw new Error("Matrix is not a square matrix");if(t.isEmpty())throw new Error("Matrix must be non-empty");let s,o,n=t.columns,i=new E(n,n),h=new Float64Array(n),a=new Float64Array(n),l=t,u=!1;if(u=!!r||t.isSymmetric(),u){for(s=0;s<n;s++)for(o=0;o<n;o++)i.set(s,o,l.get(s,o));!function(t,e,r,s){let o,n,i,h,a,l,u,c;for(a=0;a<t;a++)r[a]=s.get(t-1,a);for(h=t-1;h>0;h--){for(c=0,i=0,l=0;l<h;l++)c+=Math.abs(r[l]);if(0===c)for(e[h]=r[h-1],a=0;a<h;a++)r[a]=s.get(h-1,a),s.set(h,a,0),s.set(a,h,0);else{for(l=0;l<h;l++)r[l]/=c,i+=r[l]*r[l];for(o=r[h-1],n=Math.sqrt(i),o>0&&(n=-n),e[h]=c*n,i-=o*n,r[h-1]=o-n,a=0;a<h;a++)e[a]=0;for(a=0;a<h;a++){for(o=r[a],s.set(a,h,o),n=e[a]+s.get(a,a)*o,l=a+1;l<=h-1;l++)n+=s.get(l,a)*r[l],e[l]+=s.get(l,a)*o;e[a]=n}for(o=0,a=0;a<h;a++)e[a]/=i,o+=e[a]*r[a];for(u=o/(i+i),a=0;a<h;a++)e[a]-=u*r[a];for(a=0;a<h;a++){for(o=r[a],n=e[a],l=a;l<=h-1;l++)s.set(l,a,s.get(l,a)-(o*e[l]+n*r[l]));r[a]=s.get(h-1,a),s.set(h,a,0)}}r[h]=i}for(h=0;h<t-1;h++){if(s.set(t-1,h,s.get(h,h)),s.set(h,h,1),i=r[h+1],0!==i){for(l=0;l<=h;l++)r[l]=s.get(l,h+1)/i;for(a=0;a<=h;a++){for(n=0,l=0;l<=h;l++)n+=s.get(l,h+1)*s.get(l,a);for(l=0;l<=h;l++)s.set(l,a,s.get(l,a)-n*r[l])}}for(l=0;l<=h;l++)s.set(l,h+1,0)}for(a=0;a<t;a++)r[a]=s.get(t-1,a),s.set(t-1,a,0);s.set(t-1,t-1,1),e[0]=0}(n,a,h,i),function(t,e,r,s){let o,n,i,h,a,l,u,c,f,m,g,w,p,d,y,b;for(i=1;i<t;i++)e[i-1]=e[i];e[t-1]=0;let M=0,x=0,S=Number.EPSILON;for(l=0;l<t;l++){for(x=Math.max(x,Math.abs(r[l])+Math.abs(e[l])),u=l;u<t&&!(Math.abs(e[u])<=S*x);)u++;if(u>l)do{for(o=r[l],c=(r[l+1]-o)/(2*e[l]),f=T(c,1),c<0&&(f=-f),r[l]=e[l]/(c+f),r[l+1]=e[l]*(c+f),m=r[l+1],n=o-r[l],i=l+2;i<t;i++)r[i]-=n;for(M+=n,c=r[u],g=1,w=g,p=g,d=e[l+1],y=0,b=0,i=u-1;i>=l;i--)for(p=w,w=g,b=y,o=g*e[i],n=g*c,f=T(c,e[i]),e[i+1]=y*f,y=e[i]/f,g=c/f,c=g*r[i]-y*o,r[i+1]=n+y*(g*o+y*r[i]),a=0;a<t;a++)n=s.get(a,i+1),s.set(a,i+1,y*s.get(a,i)+g*n),s.set(a,i,g*s.get(a,i)-y*n);c=-y*b*p*d*e[l]/m,e[l]=y*c,r[l]=g*c}while(Math.abs(e[l])>S*x);r[l]=r[l]+M,e[l]=0}for(i=0;i<t-1;i++){for(a=i,c=r[i],h=i+1;h<t;h++)r[h]<c&&(a=h,c=r[h]);if(a!==i)for(r[a]=r[i],r[i]=c,h=0;h<t;h++)c=s.get(h,i),s.set(h,i,s.get(h,a)),s.set(h,a,c)}}(n,a,h,i)}else{let t=new E(n,n),e=new Float64Array(n);for(o=0;o<n;o++)for(s=0;s<n;s++)t.set(s,o,l.get(s,o));!function(t,e,r,s){let o,n,i,h,a,l,u,c=t-1;for(l=1;l<=c-1;l++){for(u=0,h=l;h<=c;h++)u+=Math.abs(e.get(h,l-1));if(0!==u){for(i=0,h=c;h>=l;h--)r[h]=e.get(h,l-1)/u,i+=r[h]*r[h];for(n=Math.sqrt(i),r[l]>0&&(n=-n),i-=r[l]*n,r[l]=r[l]-n,a=l;a<t;a++){for(o=0,h=c;h>=l;h--)o+=r[h]*e.get(h,a);for(o/=i,h=l;h<=c;h++)e.set(h,a,e.get(h,a)-o*r[h])}for(h=0;h<=c;h++){for(o=0,a=c;a>=l;a--)o+=r[a]*e.get(h,a);for(o/=i,a=l;a<=c;a++)e.set(h,a,e.get(h,a)-o*r[a])}r[l]=u*r[l],e.set(l,l-1,u*n)}}for(h=0;h<t;h++)for(a=0;a<t;a++)s.set(h,a,h===a?1:0);for(l=c-1;l>=1;l--)if(0!==e.get(l,l-1)){for(h=l+1;h<=c;h++)r[h]=e.get(h,l-1);for(a=l;a<=c;a++){for(n=0,h=l;h<=c;h++)n+=r[h]*s.get(h,a);for(n=n/r[l]/e.get(l,l-1),h=l;h<=c;h++)s.set(h,a,s.get(h,a)+n*r[h])}}}(n,t,e,i),function(t,e,r,s,o){let n,i,h,a,l,u,c,f,m,g,w,p,d,y,b,M=t-1,x=t-1,S=Number.EPSILON,E=0,v=0,R=0,A=0,N=0,k=0,I=0,z=0;for(n=0;n<t;n++)for((n<0||n>x)&&(r[n]=o.get(n,n),e[n]=0),i=Math.max(n-1,0);i<t;i++)v+=Math.abs(o.get(n,i));for(;M>=0;){for(a=M;a>0&&(k=Math.abs(o.get(a-1,a-1))+Math.abs(o.get(a,a)),0===k&&(k=v),!(Math.abs(o.get(a,a-1))<S*k));)a--;if(a===M)o.set(M,M,o.get(M,M)+E),r[M]=o.get(M,M),e[M]=0,M--,z=0;else if(a===M-1){if(c=o.get(M,M-1)*o.get(M-1,M),R=(o.get(M-1,M-1)-o.get(M,M))/2,A=R*R+c,I=Math.sqrt(Math.abs(A)),o.set(M,M,o.get(M,M)+E),o.set(M-1,M-1,o.get(M-1,M-1)+E),f=o.get(M,M),A>=0){for(I=R>=0?R+I:R-I,r[M-1]=f+I,r[M]=r[M-1],0!==I&&(r[M]=f-c/I),e[M-1]=0,e[M]=0,f=o.get(M,M-1),k=Math.abs(f)+Math.abs(I),R=f/k,A=I/k,N=Math.sqrt(R*R+A*A),R/=N,A/=N,i=M-1;i<t;i++)I=o.get(M-1,i),o.set(M-1,i,A*I+R*o.get(M,i)),o.set(M,i,A*o.get(M,i)-R*I);for(n=0;n<=M;n++)I=o.get(n,M-1),o.set(n,M-1,A*I+R*o.get(n,M)),o.set(n,M,A*o.get(n,M)-R*I);for(n=0;n<=x;n++)I=s.get(n,M-1),s.set(n,M-1,A*I+R*s.get(n,M)),s.set(n,M,A*s.get(n,M)-R*I)}else r[M-1]=f+R,r[M]=f+R,e[M-1]=I,e[M]=-I;M-=2,z=0}else{if(f=o.get(M,M),m=0,c=0,a<M&&(m=o.get(M-1,M-1),c=o.get(M,M-1)*o.get(M-1,M)),10===z){for(E+=f,n=0;n<=M;n++)o.set(n,n,o.get(n,n)-f);k=Math.abs(o.get(M,M-1))+Math.abs(o.get(M-1,M-2)),f=m=.75*k,c=-.4375*k*k}if(30===z&&(k=(m-f)/2,k=k*k+c,k>0)){for(k=Math.sqrt(k),m<f&&(k=-k),k=f-c/((m-f)/2+k),n=0;n<=M;n++)o.set(n,n,o.get(n,n)-k);E+=k,f=m=c=.964}for(z+=1,l=M-2;l>=a&&(I=o.get(l,l),N=f-I,k=m-I,R=(N*k-c)/o.get(l+1,l)+o.get(l,l+1),A=o.get(l+1,l+1)-I-N-k,N=o.get(l+2,l+1),k=Math.abs(R)+Math.abs(A)+Math.abs(N),R/=k,A/=k,N/=k,l!==a)&&!(Math.abs(o.get(l,l-1))*(Math.abs(A)+Math.abs(N))<S*(Math.abs(R)*(Math.abs(o.get(l-1,l-1))+Math.abs(I)+Math.abs(o.get(l+1,l+1)))));)l--;for(n=l+2;n<=M;n++)o.set(n,n-2,0),n>l+2&&o.set(n,n-3,0);for(h=l;h<=M-1&&(y=h!==M-1,h!==l&&(R=o.get(h,h-1),A=o.get(h+1,h-1),N=y?o.get(h+2,h-1):0,f=Math.abs(R)+Math.abs(A)+Math.abs(N),0!==f&&(R/=f,A/=f,N/=f)),0!==f);h++)if(k=Math.sqrt(R*R+A*A+N*N),R<0&&(k=-k),0!==k){for(h!==l?o.set(h,h-1,-k*f):a!==l&&o.set(h,h-1,-o.get(h,h-1)),R+=k,f=R/k,m=A/k,I=N/k,A/=R,N/=R,i=h;i<t;i++)R=o.get(h,i)+A*o.get(h+1,i),y&&(R+=N*o.get(h+2,i),o.set(h+2,i,o.get(h+2,i)-R*I)),o.set(h,i,o.get(h,i)-R*f),o.set(h+1,i,o.get(h+1,i)-R*m);for(n=0;n<=Math.min(M,h+3);n++)R=f*o.get(n,h)+m*o.get(n,h+1),y&&(R+=I*o.get(n,h+2),o.set(n,h+2,o.get(n,h+2)-R*N)),o.set(n,h,o.get(n,h)-R),o.set(n,h+1,o.get(n,h+1)-R*A);for(n=0;n<=x;n++)R=f*s.get(n,h)+m*s.get(n,h+1),y&&(R+=I*s.get(n,h+2),s.set(n,h+2,s.get(n,h+2)-R*N)),s.set(n,h,s.get(n,h)-R),s.set(n,h+1,s.get(n,h+1)-R*A)}}}if(0!==v){for(M=t-1;M>=0;M--)if(R=r[M],A=e[M],0===A)for(a=M,o.set(M,M,1),n=M-1;n>=0;n--){for(c=o.get(n,n)-R,N=0,i=a;i<=M;i++)N+=o.get(n,i)*o.get(i,M);if(e[n]<0)I=c,k=N;else if(a=n,0===e[n]?o.set(n,M,0!==c?-N/c:-N/(S*v)):(f=o.get(n,n+1),m=o.get(n+1,n),A=(r[n]-R)*(r[n]-R)+e[n]*e[n],u=(f*k-I*N)/A,o.set(n,M,u),o.set(n+1,M,Math.abs(f)>Math.abs(I)?(-N-c*u)/f:(-k-m*u)/I)),u=Math.abs(o.get(n,M)),S*u*u>1)for(i=n;i<=M;i++)o.set(i,M,o.get(i,M)/u)}else if(A<0)for(a=M-1,Math.abs(o.get(M,M-1))>Math.abs(o.get(M-1,M))?(o.set(M-1,M-1,A/o.get(M,M-1)),o.set(M-1,M,-(o.get(M,M)-R)/o.get(M,M-1))):(b=W(0,-o.get(M-1,M),o.get(M-1,M-1)-R,A),o.set(M-1,M-1,b[0]),o.set(M-1,M,b[1])),o.set(M,M-1,0),o.set(M,M,1),n=M-2;n>=0;n--){for(g=0,w=0,i=a;i<=M;i++)g+=o.get(n,i)*o.get(i,M-1),w+=o.get(n,i)*o.get(i,M);if(c=o.get(n,n)-R,e[n]<0)I=c,N=g,k=w;else if(a=n,0===e[n]?(b=W(-g,-w,c,A),o.set(n,M-1,b[0]),o.set(n,M,b[1])):(f=o.get(n,n+1),m=o.get(n+1,n),p=(r[n]-R)*(r[n]-R)+e[n]*e[n]-A*A,d=2*(r[n]-R)*A,0===p&&0===d&&(p=S*v*(Math.abs(c)+Math.abs(A)+Math.abs(f)+Math.abs(m)+Math.abs(I))),b=W(f*N-I*g+A*w,f*k-I*w-A*g,p,d),o.set(n,M-1,b[0]),o.set(n,M,b[1]),Math.abs(f)>Math.abs(I)+Math.abs(A)?(o.set(n+1,M-1,(-g-c*o.get(n,M-1)+A*o.get(n,M))/f),o.set(n+1,M,(-w-c*o.get(n,M)-A*o.get(n,M-1))/f)):(b=W(-N-m*o.get(n,M-1),-k-m*o.get(n,M),I,A),o.set(n+1,M-1,b[0]),o.set(n+1,M,b[1]))),u=Math.max(Math.abs(o.get(n,M-1)),Math.abs(o.get(n,M))),S*u*u>1)for(i=n;i<=M;i++)o.set(i,M-1,o.get(i,M-1)/u),o.set(i,M,o.get(i,M)/u)}for(n=0;n<t;n++)if(n<0||n>x)for(i=n;i<t;i++)s.set(n,i,o.get(n,i));for(i=t-1;i>=0;i--)for(n=0;n<=x;n++){for(I=0,h=0;h<=Math.min(i,x);h++)I+=s.get(n,h)*o.get(h,i);s.set(n,i,I)}}}(n,a,h,i,t)}this.n=n,this.e=a,this.d=h,this.V=i}get realEigenvalues(){return Array.from(this.d)}get imaginaryEigenvalues(){return Array.from(this.e)}get eigenvectorMatrix(){return this.V}get diagonalMatrix(){let t,e,r=this.n,s=this.e,o=this.d,n=new E(r,r);for(t=0;t<r;t++){for(e=0;e<r;e++)n.set(t,e,0);n.set(t,t,o[t]),s[t]>0?n.set(t,t+1,s[t]):s[t]<0&&n.set(t,t-1,s[t])}return n}}function W(t,e,r,s){let o,n;return Math.abs(r)>Math.abs(s)?(o=s/r,n=r+o*s,[(t+o*e)/n,(e-o*t)/n]):(o=r/s,n=s+o*r,[(o*t+e)/n,(o*e-t)/n])}class j{constructor(t){if(!(t=I.checkMatrix(t)).isSymmetric())throw new Error("Matrix is not symmetric");let e,r,s,o=t,n=o.rows,i=new E(n,n),h=!0;for(r=0;r<n;r++){let t=0;for(s=0;s<r;s++){let n=0;for(e=0;e<s;e++)n+=i.get(s,e)*i.get(r,e);n=(o.get(r,s)-n)/i.get(s,s),i.set(r,s,n),t+=n*n}for(t=o.get(r,r)-t,h&&=t>0,i.set(r,r,Math.sqrt(Math.max(t,0))),s=r+1;s<n;s++)i.set(r,s,0)}this.L=i,this.positiveDefinite=h}isPositiveDefinite(){return this.positiveDefinite}solve(t){t=I.checkMatrix(t);let e=this.L,r=e.rows;if(t.rows!==r)throw new Error("Matrix dimensions do not match");if(!1===this.isPositiveDefinite())throw new Error("Matrix is not positive definite");let s,o,n,i=t.columns,h=t.clone();for(n=0;n<r;n++)for(o=0;o<i;o++){for(s=0;s<n;s++)h.set(n,o,h.get(n,o)-h.get(s,o)*e.get(n,s));h.set(n,o,h.get(n,o)/e.get(n,n))}for(n=r-1;n>=0;n--)for(o=0;o<i;o++){for(s=n+1;s<r;s++)h.set(n,o,h.get(n,o)-h.get(s,o)*e.get(s,n));h.set(n,o,h.get(n,o)/e.get(n,n))}return h}get lowerTriangularMatrix(){return this.L}}class L{constructor(t,e={}){t=I.checkMatrix(t);let{Y:r}=e;const{scaleScores:o=!1,maxIterations:n=1e3,terminationCriteria:i=1e-10}=e;let h;if(r){if(r=s.isAnyArray(r)&&"number"==typeof r[0]?E.columnVector(r):I.checkMatrix(r),r.rows!==t.rows)throw new Error("Y should have the same number of rows as X");h=r.getColumnVector(0)}else h=t.getColumnVector(0);let a,l,u,c,f=1;for(let e=0;e<n&&f>i;e++)u=t.transpose().mmul(h).div(h.transpose().mmul(h).get(0,0)),u=u.div(u.norm()),a=t.mmul(u).div(u.transpose().mmul(u).get(0,0)),e>0&&(f=a.clone().sub(c).pow(2).sum()),c=a.clone(),r?(l=r.transpose().mmul(a).div(a.transpose().mmul(a).get(0,0)),l=l.div(l.norm()),h=r.mmul(l).div(l.transpose().mmul(l).get(0,0))):h=a;if(r){let e=t.transpose().mmul(a).div(a.transpose().mmul(a).get(0,0));e=e.div(e.norm());let s=t.clone().sub(a.clone().mmul(e.transpose())),o=h.transpose().mmul(a).div(a.transpose().mmul(a).get(0,0)),n=r.clone().sub(a.clone().mulS(o.get(0,0)).mmul(l.transpose()));this.t=a,this.p=e.transpose(),this.w=u.transpose(),this.q=l,this.u=h,this.s=a.transpose().mmul(a),this.xResidual=s,this.yResidual=n,this.betas=o}else this.w=u.transpose(),this.s=a.transpose().mmul(a).sqrt(),this.t=o?a.clone().div(this.s.get(0,0)):a,this.xResidual=t.sub(a.mmul(u.transpose()))}}e.y3=M,e.jy=j,e.oN=j,e.Hc=R,e.cg=P,e.hj=P,e.LU=z,e.Tb=z,e.uq=E,e.Zm=class extends A{constructor(t,e){w(t,e),super(t,t.rows,e.length),this.columnIndices=e}set(t,e,r){return this.matrix.set(t,this.columnIndices[e],r),this}get(t,e){return this.matrix.get(t,this.columnIndices[e])}},e.Dq=class extends A{constructor(t,e){c(t,e),super(t,t.rows,1),this.column=e}set(t,e,r){return this.matrix.set(t,this.column,r),this}get(t){return this.matrix.get(t,this.column)}},e.__=class extends A{constructor(t){super(t,t.rows,t.columns)}set(t,e,r){return this.matrix.set(t,this.columns-e-1,r),this}get(t,e){return this.matrix.get(t,this.columns-e-1)}},e.q0=class extends A{constructor(t){super(t,t.rows,t.columns)}set(t,e,r){return this.matrix.set(this.rows-t-1,e,r),this}get(t,e){return this.matrix.get(this.rows-t-1,e)}},e.lh=class extends A{constructor(t,e){g(t,e),super(t,e.length,t.columns),this.rowIndices=e}set(t,e,r){return this.matrix.set(this.rowIndices[t],e,r),this}get(t,e){return this.matrix.get(this.rowIndices[t],e)}},e.pI=class extends A{constructor(t,e){u(t,e),super(t,1,t.columns),this.row=e}set(t,e,r){return this.matrix.set(this.row,e,r),this}get(t,e){return this.matrix.get(this.row,e)}},e.zC=N,e.zg=class extends A{constructor(t,e,r,s,o){p(t,e,r,s,o),super(t,r-e+1,o-s+1),this.startRow=e,this.startColumn=s}set(t,e,r){return this.matrix.set(this.startRow+t,this.startColumn+e,r),this}get(t,e){return this.matrix.get(this.startRow+t,this.startColumn+e)}},e.g6=class extends A{constructor(t){super(t,t.columns,t.rows)}set(t,e,r){return this.matrix.set(e,t,r),this}get(t,e){return this.matrix.get(e,t)}},e.OL=L,e.ks=L,e.QR=C,e.jp=C,e.mk=F,e.W2=F,e.l=v,e.KY=k,e.dv=I,e.BR=function(t,e=t,r={}){t=new E(t);let o=!1;if("object"!=typeof e||E.isMatrix(e)||s.isAnyArray(e)?e=new E(e):(r=e,e=t,o=!0),t.rows!==e.rows)throw new TypeError("Both matrices must have the same number of rows");const{center:n=!0,scale:i=!0}=r;n&&(t.center("column"),o||e.center("column")),i&&(t.scale("column"),o||e.scale("column"));const h=t.standardDeviation("column",{unbiased:!0}),a=o?h:e.standardDeviation("column",{unbiased:!0}),l=t.transpose().mmul(e);for(let e=0;e<l.rows;e++)for(let r=0;r<l.columns;r++)l.set(e,r,l.get(e,r)*(1/(h[e]*a[r]))*(1/(t.rows-1)));return l},e.Wu=function(t,e=t,r={}){t=new E(t);let o=!1;if("object"!=typeof e||E.isMatrix(e)||s.isAnyArray(e)?e=new E(e):(r=e,e=t,o=!0),t.rows!==e.rows)throw new TypeError("Both matrices must have the same number of rows");const{center:n=!0}=r;n&&(t=t.center("column"),o||(e=e.center("column")));const i=t.transpose().mmul(e);for(let e=0;e<i.rows;e++)for(let r=0;r<i.columns;r++)i.set(e,r,i.get(e,r)*(1/(t.rows-1)));return i},e.a4=function t(e){if((e=E.checkMatrix(e)).isSquare()){if(0===e.columns)return 1;let r,s,o,n;if(2===e.columns)return r=e.get(0,0),s=e.get(0,1),o=e.get(1,0),n=e.get(1,1),r*n-s*o;if(3===e.columns){let n,i,h;return n=new N(e,[1,2],[1,2]),i=new N(e,[1,2],[0,2]),h=new N(e,[1,2],[0,1]),r=e.get(0,0),s=e.get(0,1),o=e.get(0,2),r*t(n)-s*t(i)+o*t(h)}return new z(e).determinant}throw Error("determinant can only be calculated for a square matrix")},e.DI=function(t,e=!1){return t=I.checkMatrix(t),e?new F(t).inverse():V(t,E.eye(t.rows))},e.Jo=function(t,e={}){const{thresholdValue:r=1e-9,thresholdError:s=1e-9}=e;let o=(t=E.checkMatrix(t)).rows,n=new E(o,o);for(let e=0;e<o;e++){let i=E.columnVector(t.getRow(e)),h=t.subMatrixRow(q(o,e)).transpose(),a=new F(h).solve(i),l=E.sub(i,h.mmul(a)).abs().max();n.setRow(e,D(l,a,e,r,s))}return n},e.Zi=function(t,e=Number.EPSILON){if((t=E.checkMatrix(t)).isEmpty())return t.transpose();let r=new F(t,{autoTranspose:!0}),s=r.leftSingularVectors,o=r.rightSingularVectors,n=r.diagonal;for(let t=0;t<n.length;t++)Math.abs(n[t])>e?n[t]=1/n[t]:n[t]=0;return o.mmul(E.diag(n).mmul(s.transpose()))},e.kH=V,e.LV=function(t,e){if(s.isAnyArray(t))return t[0]&&s.isAnyArray(t[0])?new I(t):new k(t,e);throw new Error("the argument is not an array")}},718(t,e,r){"use strict";r.r(e),r.d(e,{default:()=>o});var s=r(788);function o(t){var e,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!(0,s.isAnyArray)(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");if(void 0!==r.output){if(!(0,s.isAnyArray)(r.output))throw new TypeError("output option must be an array if specified");e=r.output}else e=new Array(t.length);var o=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!(0,s.isAnyArray)(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");var r=e.fromIndex,o=void 0===r?0:r,n=e.toIndex,i=void 0===n?t.length:n;if(o<0||o>=t.length||!Number.isInteger(o))throw new Error("fromIndex must be a positive integer smaller than length");if(i<=o||i>t.length||!Number.isInteger(i))throw new Error("toIndex must be an integer greater than fromIndex and at most equal to length");for(var h=t[o],a=o+1;a<i;a++)t[a]<h&&(h=t[a]);return h}(t),n=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!(0,s.isAnyArray)(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");var r=e.fromIndex,o=void 0===r?0:r,n=e.toIndex,i=void 0===n?t.length:n;if(o<0||o>=t.length||!Number.isInteger(o))throw new Error("fromIndex must be a positive integer smaller than length");if(i<=o||i>t.length||!Number.isInteger(i))throw new Error("toIndex must be an integer greater than fromIndex and at most equal to length");for(var h=t[o],a=o+1;a<i;a++)t[a]>h&&(h=t[a]);return h}(t);if(o===n)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");var i=r.min,h=void 0===i?r.autoMinMax?o:0:i,a=r.max,l=void 0===a?r.autoMinMax?n:1:a;if(h>=l)throw new RangeError("min option must be smaller than max option");for(var u=(l-h)/(n-o),c=0;c<t.length;c++)e[c]=(t[c]-o)*u+h;return e}},788(t,e,r){"use strict";r.r(e),r.d(e,{isAnyArray:()=>o});const s=Object.prototype.toString;function o(t){const e=s.call(t);return e.endsWith("Array]")&&!e.includes("Big")}}},e={};function r(s){var o=e[s];if(void 0!==o)return o.exports;var n=e[s]={exports:{}};return t[s](n,n.exports,r),n.exports}r.d=(t,e)=>{for(var s in e)r.o(e,s)&&!r.o(t,s)&&Object.defineProperty(t,s,{enumerable:!0,get:e[s]})},r.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r.r=t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})};var s={};return(()=>{"use strict";function t(t,e){return Math.floor(e()*t)}function e(t){return t()}function o(t){const e=[];for(let r=0;r<t;r++)e.push(void 0);return e}function n(t){return o(t).map((t,e)=>e)}function i(t,e){return o(t).map(()=>e)}function h(t){return i(t,0)}function a(t){return function(t){return t.reduce((t,e)=>t+e)}(t)/t.length}function l(t){let e=0;for(let r=0;r<t.length;r++)e=t[r]>e?t[r]:e;return e}function u(e,r,s){const o=h(e);for(let n=0;n<e;n++){let e=!0;for(;e;){const i=t(r,s);let h=!1;for(let t=0;t<n;t++)if(i===o[t]){h=!0;break}h||(e=!1),o[n]=i}}return o}function c(t,e,r){const s=[];let o=0,n=0;if(t.length!==e*r)throw new Error("Array dimensions must match input length.");for(let i=0;i<e;i++){const e=[];for(let s=0;s<r;s++)e.push(t[n]),n+=1;s.push(e),o+=1}return s}function f(t,e){const r=r=>o(t).map(()=>i(e,r)),s=[];return s.push(r(-1)),s.push(r(1/0)),s.push(r(0)),s}function m(e,r,s){const o=h(e);for(let n=0;n<e;n++){let e=!0,i=0;for(;e;){i=t(r,s);let h=!1;for(let t=0;t<n;t++)if(i===o[t]){h=!0;break}h||(e=!1)}o[n]=i}return o}function g(t,e,r,s,o){e=Math.floor(e);const n=t[0][e],i=t[1][e];if(t[2][e],r>=i[0])return 0;for(let t=0;t<n.length;t++)if(s===n[t])return 0;return w(t,e,r,s,o)}function w(t,e,r,s,o){const n=t[0][e],i=t[1][e],h=t[2][e];if(r>=i[0])return 0;i[0]=r,n[0]=s,h[0]=o;let a=0,l=0;for(;;){const e=2*a+1,s=e+1,o=t[0][0].length;if(e>=o)break;if(s>=o){if(!(i[e]>r))break;l=e}else if(i[e]>=i[s]){if(!(r<i[e]))break;l=e}else{if(!(r<i[s]))break;l=s}i[a]=i[l],n[a]=n[l],h[a]=h[l],a=l}return i[a]=r,n[a]=s,h[a]=o,1}function p(t,r,s,o,n){const i=f(r,o);for(let o=0;o<r;o++)for(let r=0;r<s;r++){if(t[0][o][r]<0)continue;const s=t[0][o][r],h=t[2][o][r],a=e(n);g(i,o,a,s,h),g(i,s,a,o,h),t[2][o][r]=0}return i}function d(t){const e=t[0],r=t[1];for(let t=0;t<e.length;t++){const s=e[t],o=r[t];for(let t=0;t<s.length-1;t++){const e=s.length-t-1,r=o.length-t-1,n=s[0];s[0]=s[e],s[e]=n;const i=o[0];o[0]=o[r],o[r]=i,y(o,s,r,0)}}return{indices:e,weights:r}}function y(t,e,r,s){for(;2*s+1<r;){const o=2*s+1,n=o+1;let i=s;if(t[i]<t[o]&&(i=o),n<r&&t[i]<t[n]&&(i=n),i===s)break;{const r=t[s];t[s]=t[i],t[i]=r;const o=e[s];e[s]=e[i],e[i]=o,s=i}}}function b(t,e){const r=t[0][e],s=t[1][e],o=t[2][e];let n=1/0,i=-1;for(let t=0;t>r.length;t++)1===o[t]&&s[t]<n&&(n=s[t],i=t);return i>=0?(o[i]=0,Math.floor(r[i])):-1}r.r(s),r.d(s,{UMAP:()=>ht,initWasm:()=>F,isWasmAvailable:()=>V});class M{constructor(t,e,r,s){if(this.entries=new Map,this.nRows=0,this.nCols=0,t.length!==e.length||t.length!==r.length)throw new Error("rows, cols and values arrays must all have the same length");this.nRows=s[0],this.nCols=s[1];for(let s=0;s<r.length;s++){const o=t[s],n=e[s];this.checkDims(o,n);const i=this.makeKey(o,n);this.entries.set(i,{value:r[s],row:o,col:n})}}makeKey(t,e){return`${t}:${e}`}checkDims(t,e){if(!(t<this.nRows&&e<this.nCols))throw new Error("row and/or col specified outside of matrix dimensions")}set(t,e,r){this.checkDims(t,e);const s=this.makeKey(t,e);this.entries.has(s)?this.entries.get(s).value=r:this.entries.set(s,{value:r,row:t,col:e})}get(t,e,r=0){this.checkDims(t,e);const s=this.makeKey(t,e);return this.entries.has(s)?this.entries.get(s).value:r}getAll(t=!0){const e=[];return this.entries.forEach(t=>{e.push(t)}),t&&e.sort((t,e)=>t.row===e.row?t.col-e.col:t.row-e.row),e}getDims(){return[this.nRows,this.nCols]}getRows(){return Array.from(this.entries,([t,e])=>e.row)}getCols(){return Array.from(this.entries,([t,e])=>e.col)}getValues(){return Array.from(this.entries,([t,e])=>e.value)}forEach(t){this.entries.forEach(e=>t(e.value,e.row,e.col))}map(t){let e=[];this.entries.forEach(r=>{e.push(t(r.value,r.row,r.col))});const r=[this.nRows,this.nCols];return new M(this.getRows(),this.getCols(),e,r)}toArray(){const t=o(this.nRows).map(()=>h(this.nCols));return this.entries.forEach(e=>{t[e.row][e.col]=e.value}),t}}function x(t){const e=[],r=[],s=[];t.forEach((t,o,n)=>{e.push(o),r.push(n),s.push(t)});const o=[t.nCols,t.nRows];return new M(r,e,s,o)}function S(t,e){return I(t,e,(t,e)=>t*e)}function E(t,e){return I(t,e,(t,e)=>t+e)}function v(t,e){return I(t,e,(t,e)=>t-e)}function R(t,e){return t.map(t=>t*e)}function A(t){const e=new Set,r=t.getValues(),s=t.getRows(),o=t.getCols();for(let t=0;t<r.length;t++)0===r[t]&&e.add(t);const n=(t,r)=>!e.has(r),i=r.filter(n),h=s.filter(n),a=o.filter(n);return new M(h,a,i,t.getDims())}function N(t,e="l2"){const r=k[e],s=new Map;t.forEach((t,e,r)=>{const o=s.get(e)||[];o.push(r),s.set(e,o)});const o=new M([],[],[],t.getDims());for(let e of s.keys()){const n=s.get(e).sort(),i=r(n.map(r=>t.get(e,r)));for(let t=0;t<i.length;t++)o.set(e,n[t],i[t])}return o}const k={max:t=>{let e=-1/0;for(let r=0;r<t.length;r++)e=t[r]>e?t[r]:e;return t.map(t=>t/e)},l1:t=>{let e=0;for(let r=0;r<t.length;r++)e+=t[r];return t.map(t=>t/e)},l2:t=>{let e=0;for(let r=0;r<t.length;r++)e+=t[r]**2;return t.map(t=>Math.sqrt(t**2/e))}};function I(t,e,r){const s=new Set,o=[],n=[],i=[],h=(s,h)=>{o.push(s),n.push(h);const a=r(t.get(s,h),e.get(s,h));i.push(a)},a=t.getValues(),l=t.getRows(),u=t.getCols();for(let t=0;t<a.length;t++){const e=l[t],r=u[t],o=`${e}:${r}`;s.add(o),h(e,r)}const c=e.getValues(),f=e.getRows(),m=e.getCols();for(let t=0;t<c.length;t++){const e=f[t],r=m[t],o=`${e}:${r}`;s.has(o)||h(e,r)}const g=[t.nRows,t.nCols];return new M(o,n,i,g)}function z(t){const e=[];t.forEach((t,r,s)=>{e.push({value:t,row:r,col:s})}),e.sort((t,e)=>t.row===e.row?t.col-e.col:t.row-e.row);const r=[],s=[],o=[];let n=-1;for(let t=0;t<e.length;t++){const{row:i,col:h,value:a}=e[t];i!==n&&(n=i,o.push(t)),r.push(h),s.push(a)}return{indices:r,values:s,indptr:o}}let T=null,C=null;async function F(){return T||(T=(async()=>{try{const t="undefined"!=typeof process&&null!=process.versions&&null!=process.versions.node?"../wasm/pkg/node/umap_wasm_core.js":"../wasm/pkg/web/umap_wasm_core.js",e=await r(659)(t);return"function"==typeof e.default&&await e.default(),C=e,e}catch(t){throw T=null,C=null,new Error(`Failed to load WASM module: ${t}`)}})(),T)}function V(){return null!==C}function q(t,e){if(!C)throw new Error("WASM module not initialized");const r=new Float64Array(t),s=new Float64Array(e);return C.euclidean(r,s)}function D(t,e,r,s,o){if(!C)throw new Error("WASM module not initialized");const n=new Float64Array(e*r);for(let s=0;s<e;s++)for(let e=0;e<r;e++)n[s*r+e]=t[s][e];return C.build_rp_tree(n,e,r,s,BigInt(o))}function P(t,e,r,s,o){if(!C)throw new Error("WASM module not initialized");const n=new Int32Array(t),i=new Int32Array(e),h=new Float64Array(r);return new C.WasmSparseMatrix(n,i,h,s,o)}function W(t){if(!C)throw new Error("WASM module not initialized");return C.sparse_transpose(t)}function j(t,e){if(!C)throw new Error("WASM module not initialized");return C.sparse_add(t,e)}function L(t,e){if(!C)throw new Error("WASM module not initialized");return C.sparse_subtract(t,e)}function O(t,e){if(!C)throw new Error("WASM module not initialized");return C.sparse_pairwise_multiply(t,e)}function $(t,e){if(!C)throw new Error("WASM module not initialized");return C.sparse_multiply_scalar(t,e)}function _(t){const e=Array.from(t.get_all_ordered()),r=[];for(let t=0;t<e.length;t+=3)r.push({row:e[t],col:e[t+1],value:e[t+2]});return r}class U{constructor(t,e,r,s){this.hyperplanes=t,this.offsets=e,this.children=r,this.indices=s}static fromWasm(t){const e=function(t){const e=Array.from(t.hyperplanes()),r=Array.from(t.offsets()),s=Array.from(t.children()),o=Array.from(t.indices()),n=t.dim(),i=t.n_nodes(),h=[];for(let t=0;t<i;t++)h.push(e.slice(t*n,(t+1)*n));const a=[];for(let t=0;t<i;t++)a.push([s[2*t],s[2*t+1]]);let l=0;for(let t=0;t<s.length;t++){const e=s[t];if(e<=0){const t=-e;t>l&&(l=t)}}const u=l+1,c=u>0?Math.floor(o.length/u):0,f=[];for(let t=0;t<u;t++){const e=o.slice(t*c,(t+1)*c);for(;e.length<c;)e.push(-1);f.push(e)}return{hyperplanes:h,offsets:r,children:a,indices:f}}(t),r=new U(e.hyperplanes,e.offsets,e.children,e.indices);return r.wasmTree=t,r}getWasmTree(){return this.wasmTree}dispose(){this.wasmTree&&(this.wasmTree.free(),this.wasmTree=void 0)}}function B(t,e,r,s,o=!1){const i=Math.max(10,e);if(o){if(!V())throw new Error("WASM requested but not available");return function(t,e,r,s){const o=t.length,n=t[0].length,i=[];for(let h=0;h<r;h++){const r=D(t,o,n,e,Math.floor(4294967295*s()));i.push(U.fromWasm(r))}return i}(t,i,r,s)}const a=n(r).map((e,r)=>function(t,e=30,r,s){return K(t,n(t.length),e,r,s)}(t,i,r,s)),l=a.map(t=>function(t,e){const r=Q(t),s=Y(t),o=n(r).map(()=>h(t.hyperplane?t.hyperplane.length:0)),i=h(r),a=n(r).map(()=>[-1,-1]),l=n(s).map(()=>n(e).map(()=>-1));return G(t,o,i,a,l,0,0),new U(o,i,a,l)}(t,i));return l}function K(e,r,s=30,o,n){if(r.length>s){const i=function(e,r,s){const o=e[0].length;let n=t(r.length,s),i=t(r.length,s);i+=n===i?1:0,i%=r.length;const a=r[n],l=r[i];let u=0;const c=h(o);for(let t=0;t<c.length;t++)c[t]=e[a][t]-e[l][t],u-=c[t]*(e[a][t]+e[l][t])/2;let f=0,m=0;const g=h(r.length);for(let n=0;n<r.length;n++){let i=u;for(let t=0;t<o;t++)i+=c[t]*e[r[n]][t];0===i?(g[n]=t(2,s),0===g[n]?f+=1:m+=1):i>0?(g[n]=0,f+=1):(g[n]=1,m+=1)}const w=h(f),p=h(m);f=0,m=0;for(let t=0;t<g.length;t++)0===g[t]?(w[f]=r[t],f+=1):(p[m]=r[t],m+=1);return{indicesLeft:w,indicesRight:p,hyperplane:c,offset:u}}(e,r,n),{indicesLeft:a,indicesRight:l,hyperplane:u,offset:c}=i;return{leftChild:K(e,a,s,o+1,n),rightChild:K(e,l,s,o+1,n),isLeaf:!1,hyperplane:u,offset:c}}return{indices:r,isLeaf:!0}}function G(t,e,r,s,o,n,i){if(t.isLeaf)return s[n][0]=-i,o[i].splice(0,t.indices.length,...t.indices),{nodeNum:n,leafNum:i+=1};{e[n]=t.hyperplane,r[n]=t.offset,s[n][0]=n+1;const h=n;let a=G(t.leftChild,e,r,s,o,n+1,i);return n=a.nodeNum,i=a.leafNum,s[h][1]=n+1,a=G(t.rightChild,e,r,s,o,n+1,i),{nodeNum:a.nodeNum,leafNum:a.leafNum}}}function Q(t){return t.isLeaf?1:1+Q(t.leftChild)+Q(t.rightChild)}function Y(t){return t.isLeaf?1:Y(t.leftChild)+Y(t.rightChild)}function X(e,r,s,o){let n=r;for(let t=0;t<s.length;t++)n+=e[t]*s[t];return 0===n?t(2,o):n>0?0:1}function J(t,e,r){const s=e.getWasmTree();if(s&&V())return function(t,e,r){if(!C)throw new Error("WASM module not initialized");const s=new Float64Array(e),o=C.search_flat_tree(t,s,BigInt(r));return Array.from(o)}(s,t,Math.floor(4294967295*r()));let o=0;for(;e.children[o][0]>0;)o=0===X(e.hyperplanes[o],e.offsets[o],t,r)?e.children[o][0]:e.children[o][1];const n=-1*e.children[o][0];return e.indices[n]}const H=Object.prototype.toString;function Z(t){return H.call(t).endsWith("Array]")}function tt(t,e,r){let s=0;const o=r(e);for(let e=0;e<t.x.length;e++)s+=Math.abs(t.y[e]-o(t.x[e]));return s}var et=r(673);et.y3,et.jy,et.oN,et.Hc,et.cg,et.hj,et.LU,et.Tb;const rt=et.uq,st=(et.Zm,et.Dq,et.__,et.q0,et.lh,et.pI,et.zC,et.zg,et.g6,et.OL,et.ks,et.QR,et.jp,et.mk,et.W2,et.l,et.KY,et.dv,et.BR,et.Wu,et.uq,et.uq,et.a4,et.DI);function ot(t,e,r,s,o){let n=r*s*s,i=rt.eye(e.length,e.length,n);const h=o(e);let a=new Float64Array(t.x.length);for(let e=0;e<t.x.length;e++)a[e]=h(t.x[e]);let l=function(t,e,r,s,o){const n=r.length,i=t.x.length;let h=new Array(n);for(let a=0;a<n;a++){h[a]=new Array(i);let n=r.slice();n[a]+=s;let l=o(n);for(let r=0;r<i;r++)h[a][r]=e[r]-l(t.x[r])}return new rt(h)}(t,a,e,s,o),u=function(t,e){const r=t.x.length;let s=new Array(r);for(let o=0;o<r;o++)s[o]=[t.y[o]-e[o]];return new rt(s)}(t,a),c=st(i.add(l.mmul(l.transpose())));return(e=(e=new rt([e])).sub(c.mmul(l).mmul(u).mul(s).transpose())).to1DArray()}et.Jo,et.Zi,et.kH,et.LV;const nt=1e-5,it=.001;class ht{constructor(t={}){this.learningRate=1,this.localConnectivity=1,this.minDist=.1,this.nComponents=2,this.nEpochs=0,this.nNeighbors=15,this.negativeSampleRate=5,this.random=Math.random,this.repulsionStrength=1,this.setOpMixRatio=1,this.spread=1,this.transformQueueSize=4,this.targetMetric="categorical",this.targetWeight=.5,this.targetNNeighbors=this.nNeighbors,this.distanceFn=at,this.useWasmDistance=!1,this.useWasmMatrix=!1,this.useWasmTree=!1,this.isInitialized=!1,this.rpForest=[],this.embedding=[],this.optimizationState=new lt;const e=e=>{void 0!==t[e]&&(this[e]=t[e])};e("distanceFn"),e("useWasmDistance"),e("useWasmMatrix"),e("useWasmTree"),e("learningRate"),e("localConnectivity"),e("minDist"),e("nComponents"),e("nEpochs"),e("nNeighbors"),e("negativeSampleRate"),e("random"),e("repulsionStrength"),e("setOpMixRatio"),e("spread"),e("transformQueueSize")}fit(t){return this.initializeFit(t),this.optimizeLayout(),this.embedding}async fitAsync(t,e=()=>!0){return this.initializeFit(t),await this.optimizeLayoutAsync(e),this.embedding}setSupervisedProjection(t,e={}){this.Y=t,this.targetMetric=e.targetMetric||this.targetMetric,this.targetWeight=e.targetWeight||this.targetWeight,this.targetNNeighbors=e.targetNNeighbors||this.targetNNeighbors}setPrecomputedKNN(t,e){this.knnIndices=t,this.knnDistances=e}initializeFit(t){if(t.length<=this.nNeighbors)throw new Error(`Not enough data points (${t.length}) to create nNeighbors: ${this.nNeighbors}. Add more data points or adjust the configuration.`);if(this.X===t&&this.isInitialized)return this.getNEpochs();if(this.X=t,!this.knnIndices&&!this.knnDistances){const e=this.nearestNeighbors(t);this.knnIndices=e.knnIndices,this.knnDistances=e.knnDistances}this.graph=this.fuzzySimplicialSet(t,this.nNeighbors,this.setOpMixRatio),this.makeSearchFns(),this.searchGraph=this.makeSearchGraph(t),this.processGraphForSupervisedProjection();const{head:e,tail:r,epochsPerSample:s}=this.initializeSimplicialSetEmbedding();return this.optimizationState.head=e,this.optimizationState.tail=r,this.optimizationState.epochsPerSample=s,this.initializeOptimization(),this.prepareForOptimizationLoop(),this.isInitialized=!0,this.getNEpochs()}makeSearchFns(){const t=(t,e)=>{if(this.useWasmDistance){if(!V())throw new Error("WASM distance requested via `useWasmDistance: true` but the wasm module is not initialized or available. Call `await wasmBridge.initWasm()` before using UMAP with wasm distances or build the wasm package.");return q(t,e)}return this.distanceFn(t,e)},{initFromTree:e,initFromRandom:r}=(s=t,{initFromRandom:function(t,e,r,o,n){for(let i=0;i<r.length;i++){const h=u(t,e.length,n);for(let t=0;t<h.length;t++)h[t]<0||g(o,i,s(e[h[t]],r[i]),h[t],1)}},initFromTree:function(t,e,r,o,n){for(let i=0;i<r.length;i++){const h=J(r[i],t,n);for(let t=0;t<h.length;t++){if(h[t]<0)return;g(o,i,s(e[h[t]],r[i]),h[t],1)}}}});var s;this.initFromTree=e,this.initFromRandom=r,this.search=function(t){return function(e,r,s,o){const{indices:n,indptr:i}=z(r);for(let r=0;r<o.length;r++){const h=new Set(s[0][r]);for(;;){const a=b(s,r);if(-1===a)break;const l=n.slice(i[a],i[a+1]);for(const n of l)n===a||-1===n||h.has(n)||(w(s,r,t(e[n],o[r]),n,1),h.add(n))}}return s}}(t)}computeDistance(t,e){if(this.useWasmDistance){if(!V())throw new Error("WASM distance requested via `useWasmDistance: true` but the wasm module is not initialized or available. Call `await wasmBridge.initWasm()` before using UMAP with wasm distances or build the wasm package.");return q(t,e)}return this.distanceFn(t,e)}makeSearchGraph(t){const e=this.knnIndices,r=this.knnDistances,s=[t.length,t.length],o=new M([],[],[],s);for(let t=0;t<e.length;t++){const s=e[t],n=r[t];for(let e=0;e<s.length;e++){const r=s[e],i=n[e];i>0&&o.set(t,r,i)}}return I(o,x(o),(t,e)=>t>e?t:e)}transform(t){const e=this.X;if(void 0===e||0===e.length)throw new Error("No data has been fit.");let r=Math.floor(this.nNeighbors*this.transformQueueSize);r=Math.min(e.length,r);const s=function(t,e,r,s,o,n,i){const h=f(r.length,s);if(o(s,e,r,h,i),t)for(let s of t)n(s,e,r,h,i);return h}(this.rpForest,e,t,r,this.initFromRandom,this.initFromTree,this.random),o=this.search(e,this.searchGraph,s,t);let{indices:n,weights:i}=d(o);n=n.map(t=>t.slice(0,this.nNeighbors)),i=i.map(t=>t.slice(0,this.nNeighbors));const a=Math.max(0,this.localConnectivity-1),{sigmas:l,rhos:u}=this.smoothKNNDistance(i,this.nNeighbors,a),{rows:m,cols:g,vals:w}=this.computeMembershipStrengths(n,i,l,u),p=[t.length,e.length];let y=new M(m,g,w,p);const b=z(N(y,"l1")),x=t.length,S=function(t,e,r){const s=h(t.length).map(t=>h(r[0].length));for(let o=0;o<t.length;o++)for(let n=0;n<t[0].length;n++)for(let i=0;i<r[0].length;i++){const h=t[o][n];s[o][i]+=e[o][n]*r[h][i]}return s}(c(b.indices,x,this.nNeighbors),c(b.values,x,this.nNeighbors),this.embedding),E=this.nEpochs?this.nEpochs/3:y.nRows<=1e4?100:30,v=y.getValues().reduce((t,e)=>e>t?e:t,0);y=y.map(t=>t<v/E?0:t),y=A(y);const R=this.makeEpochsPerSample(y.getValues(),E),k=y.getRows(),I=y.getCols();return this.assignOptimizationStateParameters({headEmbedding:S,tailEmbedding:this.embedding,head:k,tail:I,currentEpoch:0,nEpochs:E,nVertices:y.getDims()[1],epochsPerSample:R}),this.prepareForOptimizationLoop(),this.optimizeLayout()}processGraphForSupervisedProjection(){const{Y:t,X:e}=this;if(t){if(t.length!==e.length)throw new Error("Length of X and y must be equal");if("categorical"===this.targetMetric){const e=this.targetWeight<1?1/(1-this.targetWeight)*2.5:1e12;this.graph=this.categoricalSimplicialSetIntersection(this.graph,t,e)}}}step(){const{currentEpoch:t}=this.optimizationState;return t<this.getNEpochs()&&this.optimizeLayoutStep(t),this.optimizationState.currentEpoch}getEmbedding(){return this.embedding}nearestNeighbors(t){const{distanceFn:r,nNeighbors:s}=this,o=function(t,r){return function(s,o,n,i=10,h=50,a=.001,l=.5,u=!0){const c=s.length,w=f(s.length,n);for(let e=0;e<s.length;e++){const o=m(n,s.length,r);for(let r=0;r<o.length;r++){const n=t(s[e],s[o[r]]);g(w,e,n,o[r],1),g(w,o[r],n,e,1)}}if(u)for(let e=0;e<o.length;e++)for(let r=0;r<o[e].length&&!(o[e][r]<0);r++)for(let n=r+1;n<o[e].length&&!(o[e][n]<0);n++){const i=t(s[o[e][r]],s[o[e][n]]);g(w,o[e][r],i,o[e][n],1),g(w,o[e][n],i,o[e][r],1)}for(let o=0;o<i;o++){const o=p(w,c,n,h,r);let i=0;for(let n=0;n<c;n++)for(let a=0;a<h;a++){let u=Math.floor(o[0][n][a]);if(!(u<0||e(r)<l))for(let e=0;e<h;e++){const r=Math.floor(o[0][n][e]),h=o[2][n][a],l=o[2][n][e];if(r<0||!h&&!l)continue;const c=t(s[u],s[r]);i+=g(w,u,c,r,1),i+=g(w,r,c,u,1)}}if(i<=a*n*s.length)break}return d(w)}}(r,this.random),n=5+Math.floor(.5==(i=t.length**.5/20)?0:Math.round(i));var i;const h=Math.max(5,Math.floor(Math.round((t=>Math.log(t)/Math.log(2))(t.length))));this.rpForest=B(t,s,n,this.random,this.useWasmTree);const a=function(t){if(t.length>0){const e=[];for(let r of t)e.push(...r.indices);return e}return[[-1]]}(this.rpForest),{indices:l,weights:u}=o(t,a,s,h);return{knnIndices:l,knnDistances:u}}fuzzySimplicialSet(t,e,r=1){const{knnIndices:s=[],knnDistances:o=[],localConnectivity:n}=this,{sigmas:i,rhos:h}=this.smoothKNNDistance(o,e,n),{rows:a,cols:l,vals:u}=this.computeMembershipStrengths(s,o,i,h),c=[t.length,t.length];if(this.useWasmMatrix&&V()){const t=P(a,l,u,c[0],c[1]),e=W(t),s=O(t,e),o=j(t,e),n=_(j($(L(o,s),r),$(s,1-r))),i=n.map(t=>t.row),h=n.map(t=>t.col),f=n.map(t=>t.value);return new M(i,h,f,c)}if(this.useWasmMatrix&&V()){const t=P(a,l,u,c[0],c[1]),e=W(t),s=O(t,e),o=j(t,e),n=_(j($(L(o,s),r),$(s,1-r))),i=n.map(t=>t.row),h=n.map(t=>t.col),f=n.map(t=>t.value);return new M(i,h,f,c)}const f=new M(a,l,u,c),m=x(f),g=S(f,m),w=v(E(f,m),g);return E(R(w,r),R(g,1-r))}categoricalSimplicialSetIntersection(t,e,r,s=1){let o=function(t,e,r=1,s=5){return t.map((t,o,n)=>-1===e[o]||-1===e[n]?t*Math.exp(-r):e[o]!==e[n]?t*Math.exp(-s):t)}(t,e,s,r);return o=A(o),function(t){const e=x(t=N(t,"max"));return A(t=E(t,v(e,S(e,t))))}(o)}smoothKNNDistance(t,e,r=1,s=64,o=1){const n=Math.log(e)/Math.log(2)*o,i=h(t.length),u=h(t.length);for(let e=0;e<t.length;e++){let o=0,h=1/0,c=1;const f=t[e],m=f.filter(t=>t>0);if(m.length>=r){let t=Math.floor(r),s=r-t;t>0?(i[e]=m[t-1],s>nt&&(i[e]+=s*(m[t]-m[t-1]))):i[e]=s*m[0]}else m.length>0&&(i[e]=l(m));for(let r=0;r<s;r++){let r=0;for(let s=1;s<t[e].length;s++){const o=t[e][s]-i[e];r+=o>0?Math.exp(-o/c):1}if(Math.abs(r-n)<nt)break;r>n?(h=c,c=(o+h)/2):(o=c,h===1/0?c*=2:c=(o+h)/2)}if(u[e]=c,i[e]>0){const t=a(f);u[e]<it*t&&(u[e]=it*t)}else{const r=a(t.map(a));u[e]<it*r&&(u[e]=it*r)}}return{sigmas:u,rhos:i}}computeMembershipStrengths(t,e,r,s){const o=t.length,n=t[0].length,i=h(o*n),a=h(o*n),l=h(o*n);for(let h=0;h<o;h++)for(let o=0;o<n;o++){let u=0;-1!==t[h][o]&&(u=t[h][o]===h?0:e[h][o]-s[h]<=0?1:Math.exp(-(e[h][o]-s[h])/r[h]),i[h*n+o]=h,a[h*n+o]=t[h][o],l[h*n+o]=u)}return{rows:i,cols:a,vals:l}}initializeSimplicialSetEmbedding(){const t=this.getNEpochs(),{nComponents:r}=this,s=this.graph.getValues();let o=0;for(let t=0;t<s.length;t++){const e=s[t];o<s[t]&&(o=e)}const n=this.graph.map(e=>e<o/t?0:e);this.embedding=h(n.nRows).map(()=>h(r).map(()=>20*e(this.random)-10));const i=[],a=[],l=[],u=n.getAll();for(let t=0;t<u.length;t++){const e=u[t];e.value&&(i.push(e.value),l.push(e.row),a.push(e.col))}return{head:a,tail:l,epochsPerSample:this.makeEpochsPerSample(i,t)}}makeEpochsPerSample(t,e){const r=i(t.length,-1),s=l(t),o=t.map(t=>t/s*e);return o.forEach((t,s)=>{t>0&&(r[s]=e/o[s])}),r}assignOptimizationStateParameters(t){Object.assign(this.optimizationState,t)}prepareForOptimizationLoop(){const{repulsionStrength:t,learningRate:e,negativeSampleRate:r}=this,{epochsPerSample:s,headEmbedding:o,tailEmbedding:n}=this.optimizationState,i=o[0].length,h=o.length===n.length,a=s.map(t=>t/r),l=[...a],u=[...s];this.assignOptimizationStateParameters({epochOfNextSample:u,epochOfNextNegativeSample:l,epochsPerNegativeSample:a,moveOther:h,initialAlpha:e,alpha:e,gamma:t,dim:i})}initializeOptimization(){const t=this.embedding,e=this.embedding,{head:r,tail:s,epochsPerSample:n}=this.optimizationState,i=this.getNEpochs(),a=this.graph.nCols,{a:l,b:u}=function(t,e){const r=function(t,e){return o(300).map((t,r)=>0+r*((e-0)/299))}(0,3*t).map(t=>t<e?1:t),s=h(r.length).map((s,o)=>r[o]>=e?Math.exp(-(r[o]-e)/t):s),n={x:r,y:s},{parameterValues:i}=function(t,e,r={}){let{maxIterations:s=100,gradientDifference:o=.1,damping:n=0,errorTolerance:i=.01,minValues:h,maxValues:a,initialValues:l}=r;if(n<=0)throw new Error("The damping option must be a positive number");if(!t.x||!t.y)throw new Error("The data parameter must have x and y elements");if(!Z(t.x)||t.x.length<2||!Z(t.y)||t.y.length<2)throw new Error("The data parameter elements must be an array with more than 2 points");if(t.x.length!==t.y.length)throw new Error("The data parameter elements must have the same size");let u=l||new Array(e.length).fill(1),c=u.length;if(a=a||new Array(c).fill(Number.MAX_SAFE_INTEGER),h=h||new Array(c).fill(Number.MIN_SAFE_INTEGER),a.length!==h.length)throw new Error("minValues and maxValues must be the same size");if(!Z(u))throw new Error("initialValues must be an array");let f,m=tt(t,u,e),g=m<=i;for(f=0;f<s&&!g;f++){u=ot(t,u,n,o,e);for(let t=0;t<c;t++)u[t]=Math.min(Math.max(h[t],u[t]),a[t]);if(m=tt(t,u,e),isNaN(m))break;g=m<=i}return{parameterValues:u,parameterError:m,iterations:f}}(n,([t,e])=>r=>1/(1+t*r**(2*e)),{damping:1.5,initialValues:[.5,.5],gradientDifference:.1,maxIterations:100,errorTolerance:.01}),[a,l]=i;return{a,b:l}}(this.spread,this.minDist);this.assignOptimizationStateParameters({headEmbedding:t,tailEmbedding:e,head:r,tail:s,epochsPerSample:n,a:l,b:u,nEpochs:i,nVertices:a})}optimizeLayoutStep(e){const{optimizationState:r}=this,{head:s,tail:o,headEmbedding:n,tailEmbedding:i,epochsPerSample:h,epochOfNextSample:a,epochOfNextNegativeSample:l,epochsPerNegativeSample:u,moveOther:c,initialAlpha:f,alpha:m,gamma:g,a:w,b:p,dim:d,nEpochs:y,nVertices:b}=r;for(let r=0;r<h.length;r++){if(a[r]>e)continue;const f=s[r],y=o[r],M=n[f],x=i[y],S=ct(M,x);let E=0;S>0&&(E=-2*w*p*Math.pow(S,p-1),E/=w*Math.pow(S,p)+1);for(let t=0;t<d;t++){const e=ut(E*(M[t]-x[t]),4);M[t]+=e*m,c&&(x[t]+=-e*m)}a[r]+=h[r];const v=Math.floor((e-l[r])/u[r]);for(let e=0;e<v;e++){const e=t(b,this.random),r=i[e],s=ct(M,r);let o=0;if(s>0)o=2*g*p,o/=(.001+s)*(w*Math.pow(s,p)+1);else if(f===e)continue;for(let t=0;t<d;t++){let e=4;o>0&&(e=ut(o*(M[t]-r[t]),4)),M[t]+=e*m}}l[r]+=v*u[r]}return r.alpha=f*(1-e/y),r.currentEpoch+=1,n}optimizeLayoutAsync(t=()=>!0){return new Promise((e,r)=>{const s=async()=>{try{const{nEpochs:r,currentEpoch:o}=this.optimizationState;this.embedding=this.optimizeLayoutStep(o);const n=this.optimizationState.currentEpoch,i=!1===t(n),h=n===r;if(i||h)return e(h);setTimeout(()=>s(),0)}catch(t){r(t)}};setTimeout(()=>s(),0)})}optimizeLayout(t=()=>!0){let e=!1,r=[];for(;!e;){const{nEpochs:s,currentEpoch:o}=this.optimizationState;r=this.optimizeLayoutStep(o);const n=this.optimizationState.currentEpoch,i=!1===t(n);e=n===s||i}return r}getNEpochs(){const t=this.graph;if(this.nEpochs>0)return this.nEpochs;const e=t.nRows;return e<=2500?500:e<=5e3?400:e<=7500?300:200}}function at(t,e){let r=0;for(let s=0;s<t.length;s++)r+=(t[s]-e[s])**2;return Math.sqrt(r)}class lt{constructor(){this.currentEpoch=0,this.headEmbedding=[],this.tailEmbedding=[],this.head=[],this.tail=[],this.epochsPerSample=[],this.epochOfNextSample=[],this.epochOfNextNegativeSample=[],this.epochsPerNegativeSample=[],this.moveOther=!0,this.initialAlpha=1,this.alpha=1,this.gamma=1,this.a=1.5769434603113077,this.b=.8950608779109733,this.dim=2,this.nEpochs=500,this.nVertices=0}}function ut(t,e){return t>e?e:t<-e?-e:t}function ct(t,e){let r=0;for(let s=0;s<t.length;s++)r+=Math.pow(t[s]-e[s],2);return r}})(),s})());
|
|
1
|
+
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.UMAP=e():t.UMAP=e()}(this,()=>(()=>{var t={433(t){function e(t){return Promise.resolve().then(()=>{var e=new Error("Cannot find module '"+t+"'");throw e.code="MODULE_NOT_FOUND",e})}e.keys=()=>[],e.resolve=e,e.id=433,t.exports=e},673(t,e,r){"use strict";var s=r(788),o=r(718);const n=" ".repeat(2),i=" ".repeat(4);function h(t,e={}){const{maxRows:r=15,maxColumns:s=10,maxNumSize:o=8,padMinus:h="auto"}=e;return`${t.constructor.name} {\n${n}[\n${i}${function(t,e,r,s,o){const{rows:n,columns:h}=t,l=Math.min(n,e),u=Math.min(h,r),c=[];if("auto"===o){o=!1;t:for(let e=0;e<l;e++)for(let r=0;r<u;r++)if(t.get(e,r)<0){o=!0;break t}}for(let e=0;e<l;e++){let r=[];for(let n=0;n<u;n++)r.push(a(t.get(e,n),s,o));c.push(`${r.join(" ")}`)}return u!==h&&(c[c.length-1]+=` ... ${h-r} more columns`),l!==n&&c.push(`... ${n-e} more rows`),c.join(`\n${i}`)}(t,r,s,o,h)}\n${n}]\n${n}rows: ${t.rows}\n${n}columns: ${t.columns}\n}`}function a(t,e,r){return(t>=0&&r?` ${l(t,e-1)}`:l(t,e)).padEnd(e)}function l(t,e){let r=t.toString();if(r.length<=e)return r;let s=t.toFixed(e);if(s.length>e&&(s=t.toFixed(Math.max(0,e-(s.length-e)))),s.length<=e&&!s.startsWith("0.000")&&!s.startsWith("-0.000"))return s;let o=t.toExponential(e);return o.length>e&&(o=t.toExponential(Math.max(0,e-(o.length-e)))),o.slice(0)}function u(t,e,r){let s=r?t.rows:t.rows-1;if(e<0||e>s)throw new RangeError("Row index out of range")}function c(t,e,r){let s=r?t.columns:t.columns-1;if(e<0||e>s)throw new RangeError("Column index out of range")}function f(t,e){if(e.to1DArray&&(e=e.to1DArray()),e.length!==t.columns)throw new RangeError("vector size must be the same as the number of columns");return e}function m(t,e){if(e.to1DArray&&(e=e.to1DArray()),e.length!==t.rows)throw new RangeError("vector size must be the same as the number of rows");return e}function g(t,e){if(!s.isAnyArray(e))throw new TypeError("row indices must be an array");for(let r=0;r<e.length;r++)if(e[r]<0||e[r]>=t.rows)throw new RangeError("row indices are out of range")}function w(t,e){if(!s.isAnyArray(e))throw new TypeError("column indices must be an array");for(let r=0;r<e.length;r++)if(e[r]<0||e[r]>=t.columns)throw new RangeError("column indices are out of range")}function p(t,e,r,s,o){if(5!==arguments.length)throw new RangeError("expected 4 arguments");if(y("startRow",e),y("endRow",r),y("startColumn",s),y("endColumn",o),e>r||s>o||e<0||e>=t.rows||r<0||r>=t.rows||s<0||s>=t.columns||o<0||o>=t.columns)throw new RangeError("Submatrix indices are out of range")}function d(t,e=0){let r=[];for(let s=0;s<t;s++)r.push(e);return r}function y(t,e){if("number"!=typeof e)throw new TypeError(`${t} must be a number`)}function b(t){if(t.isEmpty())throw new Error("Empty matrix has no elements to index")}class M{static from1DArray(t,e,r){if(t*e!==r.length)throw new RangeError("data length does not match given dimensions");let s=new E(t,e);for(let o=0;o<t;o++)for(let t=0;t<e;t++)s.set(o,t,r[o*e+t]);return s}static rowVector(t){let e=new E(1,t.length);for(let r=0;r<t.length;r++)e.set(0,r,t[r]);return e}static columnVector(t){let e=new E(t.length,1);for(let r=0;r<t.length;r++)e.set(r,0,t[r]);return e}static zeros(t,e){return new E(t,e)}static ones(t,e){return new E(t,e).fill(1)}static rand(t,e,r={}){if("object"!=typeof r)throw new TypeError("options must be an object");const{random:s=Math.random}=r;let o=new E(t,e);for(let r=0;r<t;r++)for(let t=0;t<e;t++)o.set(r,t,s());return o}static randInt(t,e,r={}){if("object"!=typeof r)throw new TypeError("options must be an object");const{min:s=0,max:o=1e3,random:n=Math.random}=r;if(!Number.isInteger(s))throw new TypeError("min must be an integer");if(!Number.isInteger(o))throw new TypeError("max must be an integer");if(s>=o)throw new RangeError("min must be smaller than max");let i=o-s,h=new E(t,e);for(let r=0;r<t;r++)for(let t=0;t<e;t++){let e=s+Math.round(n()*i);h.set(r,t,e)}return h}static eye(t,e,r){void 0===e&&(e=t),void 0===r&&(r=1);let s=Math.min(t,e),o=this.zeros(t,e);for(let t=0;t<s;t++)o.set(t,t,r);return o}static diag(t,e,r){let s=t.length;void 0===e&&(e=s),void 0===r&&(r=e);let o=Math.min(s,e,r),n=this.zeros(e,r);for(let e=0;e<o;e++)n.set(e,e,t[e]);return n}static min(t,e){t=this.checkMatrix(t),e=this.checkMatrix(e);let r=t.rows,s=t.columns,o=new E(r,s);for(let n=0;n<r;n++)for(let r=0;r<s;r++)o.set(n,r,Math.min(t.get(n,r),e.get(n,r)));return o}static max(t,e){t=this.checkMatrix(t),e=this.checkMatrix(e);let r=t.rows,s=t.columns,o=new this(r,s);for(let n=0;n<r;n++)for(let r=0;r<s;r++)o.set(n,r,Math.max(t.get(n,r),e.get(n,r)));return o}static checkMatrix(t){return M.isMatrix(t)?t:new E(t)}static isMatrix(t){return null!=t&&"Matrix"===t.klass}get size(){return this.rows*this.columns}apply(t){if("function"!=typeof t)throw new TypeError("callback must be a function");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)t.call(this,e,r);return this}to1DArray(){let t=[];for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)t.push(this.get(e,r));return t}to2DArray(){let t=[];for(let e=0;e<this.rows;e++){t.push([]);for(let r=0;r<this.columns;r++)t[e].push(this.get(e,r))}return t}toJSON(){return this.to2DArray()}isRowVector(){return 1===this.rows}isColumnVector(){return 1===this.columns}isVector(){return 1===this.rows||1===this.columns}isSquare(){return this.rows===this.columns}isEmpty(){return 0===this.rows||0===this.columns}isSymmetric(){if(this.isSquare()){for(let t=0;t<this.rows;t++)for(let e=0;e<=t;e++)if(this.get(t,e)!==this.get(e,t))return!1;return!0}return!1}isDistance(){if(!this.isSymmetric())return!1;for(let t=0;t<this.rows;t++)if(0!==this.get(t,t))return!1;return!0}isEchelonForm(){let t=0,e=0,r=-1,s=!0,o=!1;for(;t<this.rows&&s;){for(e=0,o=!1;e<this.columns&&!1===o;)0===this.get(t,e)?e++:1===this.get(t,e)&&e>r?(o=!0,r=e):(s=!1,o=!0);t++}return s}isReducedEchelonForm(){let t=0,e=0,r=-1,s=!0,o=!1;for(;t<this.rows&&s;){for(e=0,o=!1;e<this.columns&&!1===o;)0===this.get(t,e)?e++:1===this.get(t,e)&&e>r?(o=!0,r=e):(s=!1,o=!0);for(let r=e+1;r<this.rows;r++)0!==this.get(t,r)&&(s=!1);t++}return s}echelonForm(){let t=this.clone(),e=0,r=0;for(;e<t.rows&&r<t.columns;){let s=e;for(let o=e;o<t.rows;o++)t.get(o,r)>t.get(s,r)&&(s=o);if(0===t.get(s,r))r++;else{t.swapRows(e,s);let o=t.get(e,r);for(let s=r;s<t.columns;s++)t.set(e,s,t.get(e,s)/o);for(let s=e+1;s<t.rows;s++){let o=t.get(s,r)/t.get(e,r);t.set(s,r,0);for(let n=r+1;n<t.columns;n++)t.set(s,n,t.get(s,n)-t.get(e,n)*o)}e++,r++}}return t}reducedEchelonForm(){let t=this.echelonForm(),e=t.columns,r=t.rows,s=r-1;for(;s>=0;)if(0===t.maxRow(s))s--;else{let o=0,n=!1;for(;o<r&&!1===n;)1===t.get(s,o)?n=!0:o++;for(let r=0;r<s;r++){let n=t.get(r,o);for(let i=o;i<e;i++){let e=t.get(r,i)-n*t.get(s,i);t.set(r,i,e)}}s--}return t}set(){throw new Error("set method is unimplemented")}get(){throw new Error("get method is unimplemented")}repeat(t={}){if("object"!=typeof t)throw new TypeError("options must be an object");const{rows:e=1,columns:r=1}=t;if(!Number.isInteger(e)||e<=0)throw new TypeError("rows must be a positive integer");if(!Number.isInteger(r)||r<=0)throw new TypeError("columns must be a positive integer");let s=new E(this.rows*e,this.columns*r);for(let t=0;t<e;t++)for(let e=0;e<r;e++)s.setSubMatrix(this,this.rows*t,this.columns*e);return s}fill(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,t);return this}neg(){return this.mulS(-1)}getRow(t){u(this,t);let e=[];for(let r=0;r<this.columns;r++)e.push(this.get(t,r));return e}getRowVector(t){return E.rowVector(this.getRow(t))}setRow(t,e){u(this,t),e=f(this,e);for(let r=0;r<this.columns;r++)this.set(t,r,e[r]);return this}swapRows(t,e){u(this,t),u(this,e);for(let r=0;r<this.columns;r++){let s=this.get(t,r);this.set(t,r,this.get(e,r)),this.set(e,r,s)}return this}getColumn(t){c(this,t);let e=[];for(let r=0;r<this.rows;r++)e.push(this.get(r,t));return e}getColumnVector(t){return E.columnVector(this.getColumn(t))}setColumn(t,e){c(this,t),e=m(this,e);for(let r=0;r<this.rows;r++)this.set(r,t,e[r]);return this}swapColumns(t,e){c(this,t),c(this,e);for(let r=0;r<this.rows;r++){let s=this.get(r,t);this.set(r,t,this.get(r,e)),this.set(r,e,s)}return this}addRowVector(t){t=f(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)+t[r]);return this}subRowVector(t){t=f(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)-t[r]);return this}mulRowVector(t){t=f(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)*t[r]);return this}divRowVector(t){t=f(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)/t[r]);return this}addColumnVector(t){t=m(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)+t[e]);return this}subColumnVector(t){t=m(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)-t[e]);return this}mulColumnVector(t){t=m(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)*t[e]);return this}divColumnVector(t){t=m(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)/t[e]);return this}mulRow(t,e){u(this,t);for(let r=0;r<this.columns;r++)this.set(t,r,this.get(t,r)*e);return this}mulColumn(t,e){c(this,t);for(let r=0;r<this.rows;r++)this.set(r,t,this.get(r,t)*e);return this}max(t){if(this.isEmpty())return NaN;switch(t){case"row":{const t=new Array(this.rows).fill(Number.NEGATIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.get(e,r)>t[e]&&(t[e]=this.get(e,r));return t}case"column":{const t=new Array(this.columns).fill(Number.NEGATIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.get(e,r)>t[r]&&(t[r]=this.get(e,r));return t}case void 0:{let t=this.get(0,0);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.get(e,r)>t&&(t=this.get(e,r));return t}default:throw new Error(`invalid option: ${t}`)}}maxIndex(){b(this);let t=this.get(0,0),e=[0,0];for(let r=0;r<this.rows;r++)for(let s=0;s<this.columns;s++)this.get(r,s)>t&&(t=this.get(r,s),e[0]=r,e[1]=s);return e}min(t){if(this.isEmpty())return NaN;switch(t){case"row":{const t=new Array(this.rows).fill(Number.POSITIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.get(e,r)<t[e]&&(t[e]=this.get(e,r));return t}case"column":{const t=new Array(this.columns).fill(Number.POSITIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.get(e,r)<t[r]&&(t[r]=this.get(e,r));return t}case void 0:{let t=this.get(0,0);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.get(e,r)<t&&(t=this.get(e,r));return t}default:throw new Error(`invalid option: ${t}`)}}minIndex(){b(this);let t=this.get(0,0),e=[0,0];for(let r=0;r<this.rows;r++)for(let s=0;s<this.columns;s++)this.get(r,s)<t&&(t=this.get(r,s),e[0]=r,e[1]=s);return e}maxRow(t){if(u(this,t),this.isEmpty())return NaN;let e=this.get(t,0);for(let r=1;r<this.columns;r++)this.get(t,r)>e&&(e=this.get(t,r));return e}maxRowIndex(t){u(this,t),b(this);let e=this.get(t,0),r=[t,0];for(let s=1;s<this.columns;s++)this.get(t,s)>e&&(e=this.get(t,s),r[1]=s);return r}minRow(t){if(u(this,t),this.isEmpty())return NaN;let e=this.get(t,0);for(let r=1;r<this.columns;r++)this.get(t,r)<e&&(e=this.get(t,r));return e}minRowIndex(t){u(this,t),b(this);let e=this.get(t,0),r=[t,0];for(let s=1;s<this.columns;s++)this.get(t,s)<e&&(e=this.get(t,s),r[1]=s);return r}maxColumn(t){if(c(this,t),this.isEmpty())return NaN;let e=this.get(0,t);for(let r=1;r<this.rows;r++)this.get(r,t)>e&&(e=this.get(r,t));return e}maxColumnIndex(t){c(this,t),b(this);let e=this.get(0,t),r=[0,t];for(let s=1;s<this.rows;s++)this.get(s,t)>e&&(e=this.get(s,t),r[0]=s);return r}minColumn(t){if(c(this,t),this.isEmpty())return NaN;let e=this.get(0,t);for(let r=1;r<this.rows;r++)this.get(r,t)<e&&(e=this.get(r,t));return e}minColumnIndex(t){c(this,t),b(this);let e=this.get(0,t),r=[0,t];for(let s=1;s<this.rows;s++)this.get(s,t)<e&&(e=this.get(s,t),r[0]=s);return r}diag(){let t=Math.min(this.rows,this.columns),e=[];for(let r=0;r<t;r++)e.push(this.get(r,r));return e}norm(t="frobenius"){switch(t){case"max":return this.max();case"frobenius":return Math.sqrt(this.dot(this));default:throw new RangeError(`unknown norm type: ${t}`)}}cumulativeSum(){let t=0;for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)t+=this.get(e,r),this.set(e,r,t);return this}dot(t){M.isMatrix(t)&&(t=t.to1DArray());let e=this.to1DArray();if(e.length!==t.length)throw new RangeError("vectors do not have the same size");let r=0;for(let s=0;s<e.length;s++)r+=e[s]*t[s];return r}mmul(t){t=E.checkMatrix(t);let e=this.rows,r=this.columns,s=t.columns,o=new E(e,s),n=new Float64Array(r);for(let i=0;i<s;i++){for(let e=0;e<r;e++)n[e]=t.get(e,i);for(let t=0;t<e;t++){let e=0;for(let s=0;s<r;s++)e+=this.get(t,s)*n[s];o.set(t,i,e)}}return o}mpow(t){if(!this.isSquare())throw new RangeError("Matrix must be square");if(!Number.isInteger(t)||t<0)throw new RangeError("Exponent must be a non-negative integer");let e=E.eye(this.rows),r=this;for(let s=t;s>=1;s/=2)1&s&&(e=e.mmul(r)),r=r.mmul(r);return e}strassen2x2(t){t=E.checkMatrix(t);let e=new E(2,2);const r=this.get(0,0),s=t.get(0,0),o=this.get(0,1),n=t.get(0,1),i=this.get(1,0),h=t.get(1,0),a=this.get(1,1),l=t.get(1,1),u=(r+a)*(s+l),c=(i+a)*s,f=r*(n-l),m=a*(h-s),g=(r+o)*l,w=u+m-g+(o-a)*(h+l),p=f+g,d=c+m,y=u-c+f+(i-r)*(s+n);return e.set(0,0,w),e.set(0,1,p),e.set(1,0,d),e.set(1,1,y),e}strassen3x3(t){t=E.checkMatrix(t);let e=new E(3,3);const r=this.get(0,0),s=this.get(0,1),o=this.get(0,2),n=this.get(1,0),i=this.get(1,1),h=this.get(1,2),a=this.get(2,0),l=this.get(2,1),u=this.get(2,2),c=t.get(0,0),f=t.get(0,1),m=t.get(0,2),g=t.get(1,0),w=t.get(1,1),p=t.get(1,2),d=t.get(2,0),y=t.get(2,1),b=t.get(2,2),M=(r-n)*(-f+w),x=(-r+n+i)*(c-f+w),S=(n+i)*(-c+f),v=r*c,A=(-r+a+l)*(c-m+p),N=(-r+a)*(m-p),R=(a+l)*(-c+m),k=(-o+l+u)*(w+d-y),I=(o-u)*(w-y),z=o*d,T=(l+u)*(-d+y),C=(-o+i+h)*(p+d-b),F=(o-h)*(p-b),D=(i+h)*(-d+b),q=v+z+s*g,V=(r+s+o-n-i-l-u)*w+x+S+v+k+z+T,P=v+A+R+(r+s+o-i-h-a-l)*p+z+C+D,W=M+i*(-c+f+g-w-p-d+b)+x+v+z+C+F,j=M+x+S+v+h*y,L=z+C+F+D+n*m,O=v+A+N+l*(-c+m+g-w-p-d+y)+k+I+z,$=k+I+z+T+a*f,_=v+A+N+R+u*b;return e.set(0,0,q),e.set(0,1,V),e.set(0,2,P),e.set(1,0,W),e.set(1,1,j),e.set(1,2,L),e.set(2,0,O),e.set(2,1,$),e.set(2,2,_),e}mmulStrassen(t){t=E.checkMatrix(t);let e=this.clone(),r=e.rows,s=e.columns,o=t.rows,n=t.columns;function i(t,e,r){let s=t.rows,o=t.columns;if(s===e&&o===r)return t;{let s=M.zeros(e,r);return s=s.setSubMatrix(t,0,0),s}}s!==o&&console.warn(`Multiplying ${r} x ${s} and ${o} x ${n} matrix: dimensions do not match.`);let h=Math.max(r,o),a=Math.max(s,n);return e=i(e,h,a),function t(e,r,s,o){if(s<=512||o<=512)return e.mmul(r);s%2==1&&o%2==1?(e=i(e,s+1,o+1),r=i(r,s+1,o+1)):s%2==1?(e=i(e,s+1,o),r=i(r,s+1,o)):o%2==1&&(e=i(e,s,o+1),r=i(r,s,o+1));let n=parseInt(e.rows/2,10),h=parseInt(e.columns/2,10),a=e.subMatrix(0,n-1,0,h-1),l=r.subMatrix(0,n-1,0,h-1),u=e.subMatrix(0,n-1,h,e.columns-1),c=r.subMatrix(0,n-1,h,r.columns-1),f=e.subMatrix(n,e.rows-1,0,h-1),m=r.subMatrix(n,r.rows-1,0,h-1),g=e.subMatrix(n,e.rows-1,h,e.columns-1),w=r.subMatrix(n,r.rows-1,h,r.columns-1),p=t(M.add(a,g),M.add(l,w),n,h),d=t(M.add(f,g),l,n,h),y=t(a,M.sub(c,w),n,h),b=t(g,M.sub(m,l),n,h),x=t(M.add(a,u),w,n,h),S=t(M.sub(f,a),M.add(l,c),n,h),E=t(M.sub(u,g),M.add(m,w),n,h),v=M.add(p,b);v.sub(x),v.add(E);let A=M.add(y,x),N=M.add(d,b),R=M.sub(p,d);R.add(y),R.add(S);let k=M.zeros(2*v.rows,2*v.columns);return k=k.setSubMatrix(v,0,0),k=k.setSubMatrix(A,v.rows,0),k=k.setSubMatrix(N,0,v.columns),k=k.setSubMatrix(R,v.rows,v.columns),k.subMatrix(0,s-1,0,o-1)}(e,t=i(t,h,a),h,a)}scaleRows(t={}){if("object"!=typeof t)throw new TypeError("options must be an object");const{min:e=0,max:r=1}=t;if(!Number.isFinite(e))throw new TypeError("min must be a number");if(!Number.isFinite(r))throw new TypeError("max must be a number");if(e>=r)throw new RangeError("min must be smaller than max");let s=new E(this.rows,this.columns);for(let t=0;t<this.rows;t++){const n=this.getRow(t);n.length>0&&o(n,{min:e,max:r,output:n}),s.setRow(t,n)}return s}scaleColumns(t={}){if("object"!=typeof t)throw new TypeError("options must be an object");const{min:e=0,max:r=1}=t;if(!Number.isFinite(e))throw new TypeError("min must be a number");if(!Number.isFinite(r))throw new TypeError("max must be a number");if(e>=r)throw new RangeError("min must be smaller than max");let s=new E(this.rows,this.columns);for(let t=0;t<this.columns;t++){const n=this.getColumn(t);n.length&&o(n,{min:e,max:r,output:n}),s.setColumn(t,n)}return s}flipRows(){const t=Math.ceil(this.columns/2);for(let e=0;e<this.rows;e++)for(let r=0;r<t;r++){let t=this.get(e,r),s=this.get(e,this.columns-1-r);this.set(e,r,s),this.set(e,this.columns-1-r,t)}return this}flipColumns(){const t=Math.ceil(this.rows/2);for(let e=0;e<this.columns;e++)for(let r=0;r<t;r++){let t=this.get(r,e),s=this.get(this.rows-1-r,e);this.set(r,e,s),this.set(this.rows-1-r,e,t)}return this}kroneckerProduct(t){t=E.checkMatrix(t);let e=this.rows,r=this.columns,s=t.rows,o=t.columns,n=new E(e*s,r*o);for(let i=0;i<e;i++)for(let e=0;e<r;e++)for(let r=0;r<s;r++)for(let h=0;h<o;h++)n.set(s*i+r,o*e+h,this.get(i,e)*t.get(r,h));return n}kroneckerSum(t){if(t=E.checkMatrix(t),!this.isSquare()||!t.isSquare())throw new Error("Kronecker Sum needs two Square Matrices");let e=this.rows,r=t.rows,s=this.kroneckerProduct(E.eye(r,r)),o=E.eye(e,e).kroneckerProduct(t);return s.add(o)}transpose(){let t=new E(this.columns,this.rows);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)t.set(r,e,this.get(e,r));return t}sortRows(t=x){for(let e=0;e<this.rows;e++)this.setRow(e,this.getRow(e).sort(t));return this}sortColumns(t=x){for(let e=0;e<this.columns;e++)this.setColumn(e,this.getColumn(e).sort(t));return this}subMatrix(t,e,r,s){p(this,t,e,r,s);let o=new E(e-t+1,s-r+1);for(let n=t;n<=e;n++)for(let e=r;e<=s;e++)o.set(n-t,e-r,this.get(n,e));return o}subMatrixRow(t,e,r){if(void 0===e&&(e=0),void 0===r&&(r=this.columns-1),e>r||e<0||e>=this.columns||r<0||r>=this.columns)throw new RangeError("Argument out of range");let s=new E(t.length,r-e+1);for(let o=0;o<t.length;o++)for(let n=e;n<=r;n++){if(t[o]<0||t[o]>=this.rows)throw new RangeError(`Row index out of range: ${t[o]}`);s.set(o,n-e,this.get(t[o],n))}return s}subMatrixColumn(t,e,r){if(void 0===e&&(e=0),void 0===r&&(r=this.rows-1),e>r||e<0||e>=this.rows||r<0||r>=this.rows)throw new RangeError("Argument out of range");let s=new E(r-e+1,t.length);for(let o=0;o<t.length;o++)for(let n=e;n<=r;n++){if(t[o]<0||t[o]>=this.columns)throw new RangeError(`Column index out of range: ${t[o]}`);s.set(n-e,o,this.get(n,t[o]))}return s}setSubMatrix(t,e,r){if((t=E.checkMatrix(t)).isEmpty())return this;p(this,e,e+t.rows-1,r,r+t.columns-1);for(let s=0;s<t.rows;s++)for(let o=0;o<t.columns;o++)this.set(e+s,r+o,t.get(s,o));return this}selection(t,e){g(this,t),w(this,e);let r=new E(t.length,e.length);for(let s=0;s<t.length;s++){let o=t[s];for(let t=0;t<e.length;t++){let n=e[t];r.set(s,t,this.get(o,n))}}return r}trace(){let t=Math.min(this.rows,this.columns),e=0;for(let r=0;r<t;r++)e+=this.get(r,r);return e}clone(){return this.constructor.copy(this,new E(this.rows,this.columns))}static copy(t,e){for(const[r,s,o]of t.entries())e.set(r,s,o);return e}sum(t){switch(t){case"row":return function(t){let e=d(t.rows);for(let r=0;r<t.rows;++r)for(let s=0;s<t.columns;++s)e[r]+=t.get(r,s);return e}(this);case"column":return function(t){let e=d(t.columns);for(let r=0;r<t.rows;++r)for(let s=0;s<t.columns;++s)e[s]+=t.get(r,s);return e}(this);case void 0:return function(t){let e=0;for(let r=0;r<t.rows;r++)for(let s=0;s<t.columns;s++)e+=t.get(r,s);return e}(this);default:throw new Error(`invalid option: ${t}`)}}product(t){switch(t){case"row":return function(t){let e=d(t.rows,1);for(let r=0;r<t.rows;++r)for(let s=0;s<t.columns;++s)e[r]*=t.get(r,s);return e}(this);case"column":return function(t){let e=d(t.columns,1);for(let r=0;r<t.rows;++r)for(let s=0;s<t.columns;++s)e[s]*=t.get(r,s);return e}(this);case void 0:return function(t){let e=1;for(let r=0;r<t.rows;r++)for(let s=0;s<t.columns;s++)e*=t.get(r,s);return e}(this);default:throw new Error(`invalid option: ${t}`)}}mean(t){const e=this.sum(t);switch(t){case"row":for(let t=0;t<this.rows;t++)e[t]/=this.columns;return e;case"column":for(let t=0;t<this.columns;t++)e[t]/=this.rows;return e;case void 0:return e/this.size;default:throw new Error(`invalid option: ${t}`)}}variance(t,e={}){if("object"==typeof t&&(e=t,t=void 0),"object"!=typeof e)throw new TypeError("options must be an object");const{unbiased:r=!0,mean:o=this.mean(t)}=e;if("boolean"!=typeof r)throw new TypeError("unbiased must be a boolean");switch(t){case"row":if(!s.isAnyArray(o))throw new TypeError("mean must be an array");return function(t,e,r){const s=t.rows,o=t.columns,n=[];for(let i=0;i<s;i++){let s=0,h=0,a=0;for(let e=0;e<o;e++)a=t.get(i,e)-r[i],s+=a,h+=a*a;e?n.push((h-s*s/o)/(o-1)):n.push((h-s*s/o)/o)}return n}(this,r,o);case"column":if(!s.isAnyArray(o))throw new TypeError("mean must be an array");return function(t,e,r){const s=t.rows,o=t.columns,n=[];for(let i=0;i<o;i++){let o=0,h=0,a=0;for(let e=0;e<s;e++)a=t.get(e,i)-r[i],o+=a,h+=a*a;e?n.push((h-o*o/s)/(s-1)):n.push((h-o*o/s)/s)}return n}(this,r,o);case void 0:if("number"!=typeof o)throw new TypeError("mean must be a number");return function(t,e,r){const s=t.rows,o=t.columns,n=s*o;let i=0,h=0,a=0;for(let e=0;e<s;e++)for(let s=0;s<o;s++)a=t.get(e,s)-r,i+=a,h+=a*a;return e?(h-i*i/n)/(n-1):(h-i*i/n)/n}(this,r,o);default:throw new Error(`invalid option: ${t}`)}}standardDeviation(t,e){"object"==typeof t&&(e=t,t=void 0);const r=this.variance(t,e);if(void 0===t)return Math.sqrt(r);for(let t=0;t<r.length;t++)r[t]=Math.sqrt(r[t]);return r}center(t,e={}){if("object"==typeof t&&(e=t,t=void 0),"object"!=typeof e)throw new TypeError("options must be an object");const{center:r=this.mean(t)}=e;switch(t){case"row":if(!s.isAnyArray(r))throw new TypeError("center must be an array");return function(t,e){for(let r=0;r<t.rows;r++)for(let s=0;s<t.columns;s++)t.set(r,s,t.get(r,s)-e[r])}(this,r),this;case"column":if(!s.isAnyArray(r))throw new TypeError("center must be an array");return function(t,e){for(let r=0;r<t.rows;r++)for(let s=0;s<t.columns;s++)t.set(r,s,t.get(r,s)-e[s])}(this,r),this;case void 0:if("number"!=typeof r)throw new TypeError("center must be a number");return function(t,e){for(let r=0;r<t.rows;r++)for(let s=0;s<t.columns;s++)t.set(r,s,t.get(r,s)-e)}(this,r),this;default:throw new Error(`invalid option: ${t}`)}}scale(t,e={}){if("object"==typeof t&&(e=t,t=void 0),"object"!=typeof e)throw new TypeError("options must be an object");let r=e.scale;switch(t){case"row":if(void 0===r)r=function(t){const e=[];for(let r=0;r<t.rows;r++){let s=0;for(let e=0;e<t.columns;e++)s+=t.get(r,e)**2/(t.columns-1);e.push(Math.sqrt(s))}return e}(this);else if(!s.isAnyArray(r))throw new TypeError("scale must be an array");return function(t,e){for(let r=0;r<t.rows;r++)for(let s=0;s<t.columns;s++)t.set(r,s,t.get(r,s)/e[r])}(this,r),this;case"column":if(void 0===r)r=function(t){const e=[];for(let r=0;r<t.columns;r++){let s=0;for(let e=0;e<t.rows;e++)s+=t.get(e,r)**2/(t.rows-1);e.push(Math.sqrt(s))}return e}(this);else if(!s.isAnyArray(r))throw new TypeError("scale must be an array");return function(t,e){for(let r=0;r<t.rows;r++)for(let s=0;s<t.columns;s++)t.set(r,s,t.get(r,s)/e[s])}(this,r),this;case void 0:if(void 0===r)r=function(t){const e=t.size-1;let r=0;for(let s=0;s<t.columns;s++)for(let o=0;o<t.rows;o++)r+=t.get(o,s)**2/e;return Math.sqrt(r)}(this);else if("number"!=typeof r)throw new TypeError("scale must be a number");return function(t,e){for(let r=0;r<t.rows;r++)for(let s=0;s<t.columns;s++)t.set(r,s,t.get(r,s)/e)}(this,r),this;default:throw new Error(`invalid option: ${t}`)}}toString(t){return h(this,t)}[Symbol.iterator](){return this.entries()}*entries(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)yield[t,e,this.get(t,e)]}*values(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)yield this.get(t,e)}}function x(t,e){return t-e}function S(t){return t.every(t=>"number"==typeof t)}M.prototype.klass="Matrix","undefined"!=typeof Symbol&&(M.prototype[Symbol.for("nodejs.util.inspect.custom")]=function(){return h(this)}),M.random=M.rand,M.randomInt=M.randInt,M.diagonal=M.diag,M.prototype.diagonal=M.prototype.diag,M.identity=M.eye,M.prototype.negate=M.prototype.neg,M.prototype.tensorProduct=M.prototype.kroneckerProduct;class E extends M{data;#t(t,e){if(this.data=[],!(Number.isInteger(e)&&e>=0))throw new TypeError("nColumns must be a positive integer");for(let r=0;r<t;r++)this.data.push(new Float64Array(e));this.rows=t,this.columns=e}constructor(t,e){if(super(),E.isMatrix(t))this.#t(t.rows,t.columns),E.copy(t,this);else if(Number.isInteger(t)&&t>=0)this.#t(t,e);else{if(!s.isAnyArray(t))throw new TypeError("First argument must be a positive number or an array");{const r=t;if("number"!=typeof(e=(t=r.length)?r[0].length:0))throw new TypeError("Data must be a 2D array with at least one element");this.data=[];for(let s=0;s<t;s++){if(r[s].length!==e)throw new RangeError("Inconsistent array dimensions");if(!S(r[s]))throw new TypeError("Input data contains non-numeric values");this.data.push(Float64Array.from(r[s]))}this.rows=t,this.columns=e}}}set(t,e,r){return this.data[t][e]=r,this}get(t,e){return this.data[t][e]}removeRow(t){return u(this,t),this.data.splice(t,1),this.rows-=1,this}addRow(t,e){return void 0===e&&(e=t,t=this.rows),u(this,t,!0),e=Float64Array.from(f(this,e)),this.data.splice(t,0,e),this.rows+=1,this}removeColumn(t){c(this,t);for(let e=0;e<this.rows;e++){const r=new Float64Array(this.columns-1);for(let s=0;s<t;s++)r[s]=this.data[e][s];for(let s=t+1;s<this.columns;s++)r[s-1]=this.data[e][s];this.data[e]=r}return this.columns-=1,this}addColumn(t,e){void 0===e&&(e=t,t=this.columns),c(this,t,!0),e=m(this,e);for(let r=0;r<this.rows;r++){const s=new Float64Array(this.columns+1);let o=0;for(;o<t;o++)s[o]=this.data[r][o];for(s[o++]=e[r];o<this.columns+1;o++)s[o]=this.data[r][o-1];this.data[r]=s}return this.columns+=1,this}}!function(t,e){t.prototype.add=function(t){return"number"==typeof t?this.addS(t):this.addM(t)},t.prototype.addS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)+t);return this},t.prototype.addM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)+t.get(e,r));return this},t.add=function(t,r){return new e(t).add(r)},t.prototype.sub=function(t){return"number"==typeof t?this.subS(t):this.subM(t)},t.prototype.subS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)-t);return this},t.prototype.subM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)-t.get(e,r));return this},t.sub=function(t,r){return new e(t).sub(r)},t.prototype.subtract=t.prototype.sub,t.prototype.subtractS=t.prototype.subS,t.prototype.subtractM=t.prototype.subM,t.subtract=t.sub,t.prototype.mul=function(t){return"number"==typeof t?this.mulS(t):this.mulM(t)},t.prototype.mulS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)*t);return this},t.prototype.mulM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)*t.get(e,r));return this},t.mul=function(t,r){return new e(t).mul(r)},t.prototype.multiply=t.prototype.mul,t.prototype.multiplyS=t.prototype.mulS,t.prototype.multiplyM=t.prototype.mulM,t.multiply=t.mul,t.prototype.div=function(t){return"number"==typeof t?this.divS(t):this.divM(t)},t.prototype.divS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)/t);return this},t.prototype.divM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)/t.get(e,r));return this},t.div=function(t,r){return new e(t).div(r)},t.prototype.divide=t.prototype.div,t.prototype.divideS=t.prototype.divS,t.prototype.divideM=t.prototype.divM,t.divide=t.div,t.prototype.mod=function(t){return"number"==typeof t?this.modS(t):this.modM(t)},t.prototype.modS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)%t);return this},t.prototype.modM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)%t.get(e,r));return this},t.mod=function(t,r){return new e(t).mod(r)},t.prototype.modulus=t.prototype.mod,t.prototype.modulusS=t.prototype.modS,t.prototype.modulusM=t.prototype.modM,t.modulus=t.mod,t.prototype.and=function(t){return"number"==typeof t?this.andS(t):this.andM(t)},t.prototype.andS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)&t);return this},t.prototype.andM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)&t.get(e,r));return this},t.and=function(t,r){return new e(t).and(r)},t.prototype.or=function(t){return"number"==typeof t?this.orS(t):this.orM(t)},t.prototype.orS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)|t);return this},t.prototype.orM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)|t.get(e,r));return this},t.or=function(t,r){return new e(t).or(r)},t.prototype.xor=function(t){return"number"==typeof t?this.xorS(t):this.xorM(t)},t.prototype.xorS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)^t);return this},t.prototype.xorM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)^t.get(e,r));return this},t.xor=function(t,r){return new e(t).xor(r)},t.prototype.leftShift=function(t){return"number"==typeof t?this.leftShiftS(t):this.leftShiftM(t)},t.prototype.leftShiftS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)<<t);return this},t.prototype.leftShiftM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)<<t.get(e,r));return this},t.leftShift=function(t,r){return new e(t).leftShift(r)},t.prototype.signPropagatingRightShift=function(t){return"number"==typeof t?this.signPropagatingRightShiftS(t):this.signPropagatingRightShiftM(t)},t.prototype.signPropagatingRightShiftS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)>>t);return this},t.prototype.signPropagatingRightShiftM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)>>t.get(e,r));return this},t.signPropagatingRightShift=function(t,r){return new e(t).signPropagatingRightShift(r)},t.prototype.rightShift=function(t){return"number"==typeof t?this.rightShiftS(t):this.rightShiftM(t)},t.prototype.rightShiftS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)>>>t);return this},t.prototype.rightShiftM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)>>>t.get(e,r));return this},t.rightShift=function(t,r){return new e(t).rightShift(r)},t.prototype.zeroFillRightShift=t.prototype.rightShift,t.prototype.zeroFillRightShiftS=t.prototype.rightShiftS,t.prototype.zeroFillRightShiftM=t.prototype.rightShiftM,t.zeroFillRightShift=t.rightShift,t.prototype.not=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,~this.get(t,e));return this},t.not=function(t){return new e(t).not()},t.prototype.abs=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.abs(this.get(t,e)));return this},t.abs=function(t){return new e(t).abs()},t.prototype.acos=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.acos(this.get(t,e)));return this},t.acos=function(t){return new e(t).acos()},t.prototype.acosh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.acosh(this.get(t,e)));return this},t.acosh=function(t){return new e(t).acosh()},t.prototype.asin=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.asin(this.get(t,e)));return this},t.asin=function(t){return new e(t).asin()},t.prototype.asinh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.asinh(this.get(t,e)));return this},t.asinh=function(t){return new e(t).asinh()},t.prototype.atan=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.atan(this.get(t,e)));return this},t.atan=function(t){return new e(t).atan()},t.prototype.atanh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.atanh(this.get(t,e)));return this},t.atanh=function(t){return new e(t).atanh()},t.prototype.cbrt=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.cbrt(this.get(t,e)));return this},t.cbrt=function(t){return new e(t).cbrt()},t.prototype.ceil=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.ceil(this.get(t,e)));return this},t.ceil=function(t){return new e(t).ceil()},t.prototype.clz32=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.clz32(this.get(t,e)));return this},t.clz32=function(t){return new e(t).clz32()},t.prototype.cos=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.cos(this.get(t,e)));return this},t.cos=function(t){return new e(t).cos()},t.prototype.cosh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.cosh(this.get(t,e)));return this},t.cosh=function(t){return new e(t).cosh()},t.prototype.exp=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.exp(this.get(t,e)));return this},t.exp=function(t){return new e(t).exp()},t.prototype.expm1=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.expm1(this.get(t,e)));return this},t.expm1=function(t){return new e(t).expm1()},t.prototype.floor=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.floor(this.get(t,e)));return this},t.floor=function(t){return new e(t).floor()},t.prototype.fround=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.fround(this.get(t,e)));return this},t.fround=function(t){return new e(t).fround()},t.prototype.log=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.log(this.get(t,e)));return this},t.log=function(t){return new e(t).log()},t.prototype.log1p=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.log1p(this.get(t,e)));return this},t.log1p=function(t){return new e(t).log1p()},t.prototype.log10=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.log10(this.get(t,e)));return this},t.log10=function(t){return new e(t).log10()},t.prototype.log2=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.log2(this.get(t,e)));return this},t.log2=function(t){return new e(t).log2()},t.prototype.round=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.round(this.get(t,e)));return this},t.round=function(t){return new e(t).round()},t.prototype.sign=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.sign(this.get(t,e)));return this},t.sign=function(t){return new e(t).sign()},t.prototype.sin=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.sin(this.get(t,e)));return this},t.sin=function(t){return new e(t).sin()},t.prototype.sinh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.sinh(this.get(t,e)));return this},t.sinh=function(t){return new e(t).sinh()},t.prototype.sqrt=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.sqrt(this.get(t,e)));return this},t.sqrt=function(t){return new e(t).sqrt()},t.prototype.tan=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.tan(this.get(t,e)));return this},t.tan=function(t){return new e(t).tan()},t.prototype.tanh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.tanh(this.get(t,e)));return this},t.tanh=function(t){return new e(t).tanh()},t.prototype.trunc=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.trunc(this.get(t,e)));return this},t.trunc=function(t){return new e(t).trunc()},t.pow=function(t,r){return new e(t).pow(r)},t.prototype.pow=function(t){return"number"==typeof t?this.powS(t):this.powM(t)},t.prototype.powS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)**t);return this},t.prototype.powM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)**t.get(e,r));return this}}(M,E);class v extends M{#e;get size(){return this.#e.size}get rows(){return this.#e.rows}get columns(){return this.#e.columns}get diagonalSize(){return this.rows}static isSymmetricMatrix(t){return E.isMatrix(t)&&"SymmetricMatrix"===t.klassType}static zeros(t){return new this(t)}static ones(t){return new this(t).fill(1)}constructor(t){if(super(),E.isMatrix(t)){if(!t.isSymmetric())throw new TypeError("not symmetric data");this.#e=E.copy(t,new E(t.rows,t.rows))}else if(Number.isInteger(t)&&t>=0)this.#e=new E(t,t);else if(this.#e=new E(t),!this.isSymmetric())throw new TypeError("not symmetric data")}clone(){const t=new v(this.diagonalSize);for(const[e,r,s]of this.upperRightEntries())t.set(e,r,s);return t}toMatrix(){return new E(this)}get(t,e){return this.#e.get(t,e)}set(t,e,r){return this.#e.set(t,e,r),this.#e.set(e,t,r),this}removeCross(t){return this.#e.removeRow(t),this.#e.removeColumn(t),this}addCross(t,e){void 0===e&&(e=t,t=this.diagonalSize);const r=e.slice();return r.splice(t,1),this.#e.addRow(t,r),this.#e.addColumn(t,e),this}applyMask(t){if(t.length!==this.diagonalSize)throw new RangeError("Mask size do not match with matrix size");const e=[];for(const[r,s]of t.entries())s||e.push(r);e.reverse();for(const t of e)this.removeCross(t);return this}toCompact(){const{diagonalSize:t}=this,e=new Array(t*(t+1)/2);for(let r=0,s=0,o=0;o<e.length;o++)e[o]=this.get(s,r),++r>=t&&(r=++s);return e}static fromCompact(t){const e=t.length,r=(Math.sqrt(8*e+1)-1)/2;if(!Number.isInteger(r))throw new TypeError(`This array is not a compact representation of a Symmetric Matrix, ${JSON.stringify(t)}`);const s=new v(r);for(let o=0,n=0,i=0;i<e;i++)s.set(o,n,t[i]),++o>=r&&(o=++n);return s}*upperRightEntries(){for(let t=0,e=0;t<this.diagonalSize;void 0){const r=this.get(t,e);yield[t,e,r],++e>=this.diagonalSize&&(e=++t)}}*upperRightValues(){for(let t=0,e=0;t<this.diagonalSize;void 0){const r=this.get(t,e);yield r,++e>=this.diagonalSize&&(e=++t)}}}v.prototype.klassType="SymmetricMatrix";class A extends v{static isDistanceMatrix(t){return v.isSymmetricMatrix(t)&&"DistanceMatrix"===t.klassSubType}constructor(t){if(super(t),!this.isDistance())throw new TypeError("Provided arguments do no produce a distance matrix")}set(t,e,r){return t===e&&(r=0),super.set(t,e,r)}addCross(t,e){return void 0===e&&(e=t,t=this.diagonalSize),(e=e.slice())[t]=0,super.addCross(t,e)}toSymmetricMatrix(){return new v(this)}clone(){const t=new A(this.diagonalSize);for(const[e,r,s]of this.upperRightEntries())e!==r&&t.set(e,r,s);return t}toCompact(){const{diagonalSize:t}=this,e=new Array((t-1)*t/2);for(let r=1,s=0,o=0;o<e.length;o++)e[o]=this.get(s,r),++r>=t&&(r=1+ ++s);return e}static fromCompact(t){const e=t.length;if(0===e)return new this(0);const r=(Math.sqrt(8*e+1)+1)/2;if(!Number.isInteger(r))throw new TypeError(`This array is not a compact representation of a DistanceMatrix, ${JSON.stringify(t)}`);const s=new this(r);for(let o=1,n=0,i=0;i<e;i++)s.set(o,n,t[i]),++o>=r&&(o=1+ ++n);return s}}A.prototype.klassSubType="DistanceMatrix";class N extends M{constructor(t,e,r){super(),this.matrix=t,this.rows=e,this.columns=r}}class R extends N{constructor(t,e,r){g(t,e),w(t,r),super(t,e.length,r.length),this.rowIndices=e,this.columnIndices=r}set(t,e,r){return this.matrix.set(this.rowIndices[t],this.columnIndices[e],r),this}get(t,e){return this.matrix.get(this.rowIndices[t],this.columnIndices[e])}}class k extends M{constructor(t,e={}){const{rows:r=1}=e;if(t.length%r!==0)throw new Error("the data length is not divisible by the number of rows");super(),this.rows=r,this.columns=t.length/r,this.data=t}set(t,e,r){let s=this._calculateIndex(t,e);return this.data[s]=r,this}get(t,e){let r=this._calculateIndex(t,e);return this.data[r]}_calculateIndex(t,e){return t*this.columns+e}}class I extends M{constructor(t){super(),this.data=t,this.rows=t.length,this.columns=t[0].length}set(t,e,r){return this.data[t][e]=r,this}get(t,e){return this.data[t][e]}}class z{constructor(t){let e,r,s,o,n,i,h,a,l,u=(t=I.checkMatrix(t)).clone(),c=u.rows,f=u.columns,m=new Float64Array(c),g=1;for(e=0;e<c;e++)m[e]=e;for(a=new Float64Array(c),r=0;r<f;r++){for(e=0;e<c;e++)a[e]=u.get(e,r);for(e=0;e<c;e++){for(l=Math.min(e,r),n=0,s=0;s<l;s++)n+=u.get(e,s)*a[s];a[e]-=n,u.set(e,r,a[e])}for(o=r,e=r+1;e<c;e++)Math.abs(a[e])>Math.abs(a[o])&&(o=e);if(o!==r){for(s=0;s<f;s++)i=u.get(o,s),u.set(o,s,u.get(r,s)),u.set(r,s,i);h=m[o],m[o]=m[r],m[r]=h,g=-g}if(r<c&&0!==u.get(r,r))for(e=r+1;e<c;e++)u.set(e,r,u.get(e,r)/u.get(r,r))}this.LU=u,this.pivotVector=m,this.pivotSign=g}isSingular(){let t=this.LU,e=t.columns;for(let r=0;r<e;r++)if(0===t.get(r,r))return!0;return!1}solve(t){t=E.checkMatrix(t);let e=this.LU;if(e.rows!==t.rows)throw new Error("Invalid matrix dimensions");if(this.isSingular())throw new Error("LU matrix is singular");let r,s,o,n=t.columns,i=t.subMatrixRow(this.pivotVector,0,n-1),h=e.columns;for(o=0;o<h;o++)for(r=o+1;r<h;r++)for(s=0;s<n;s++)i.set(r,s,i.get(r,s)-i.get(o,s)*e.get(r,o));for(o=h-1;o>=0;o--){for(s=0;s<n;s++)i.set(o,s,i.get(o,s)/e.get(o,o));for(r=0;r<o;r++)for(s=0;s<n;s++)i.set(r,s,i.get(r,s)-i.get(o,s)*e.get(r,o))}return i}get determinant(){let t=this.LU;if(!t.isSquare())throw new Error("Matrix must be square");let e=this.pivotSign,r=t.columns;for(let s=0;s<r;s++)e*=t.get(s,s);return e}get lowerTriangularMatrix(){let t=this.LU,e=t.rows,r=t.columns,s=new E(e,r);for(let o=0;o<e;o++)for(let e=0;e<r;e++)o>e?s.set(o,e,t.get(o,e)):o===e?s.set(o,e,1):s.set(o,e,0);return s}get upperTriangularMatrix(){let t=this.LU,e=t.rows,r=t.columns,s=new E(e,r);for(let o=0;o<e;o++)for(let e=0;e<r;e++)o<=e?s.set(o,e,t.get(o,e)):s.set(o,e,0);return s}get pivotPermutationVector(){return Array.from(this.pivotVector)}}function T(t,e){let r=0;return Math.abs(t)>Math.abs(e)?(r=e/t,Math.abs(t)*Math.sqrt(1+r*r)):0!==e?(r=t/e,Math.abs(e)*Math.sqrt(1+r*r)):0}class C{constructor(t){let e,r,s,o,n=(t=I.checkMatrix(t)).clone(),i=t.rows,h=t.columns,a=new Float64Array(h);for(s=0;s<h;s++){let t=0;for(e=s;e<i;e++)t=T(t,n.get(e,s));if(0!==t){for(n.get(s,s)<0&&(t=-t),e=s;e<i;e++)n.set(e,s,n.get(e,s)/t);for(n.set(s,s,n.get(s,s)+1),r=s+1;r<h;r++){for(o=0,e=s;e<i;e++)o+=n.get(e,s)*n.get(e,r);for(o=-o/n.get(s,s),e=s;e<i;e++)n.set(e,r,n.get(e,r)+o*n.get(e,s))}}a[s]=-t}this.QR=n,this.Rdiag=a}solve(t){t=E.checkMatrix(t);let e=this.QR,r=e.rows;if(t.rows!==r)throw new Error("Matrix row dimensions must agree");if(!this.isFullRank())throw new Error("Matrix is rank deficient");let s,o,n,i,h=t.columns,a=t.clone(),l=e.columns;for(n=0;n<l;n++)for(o=0;o<h;o++){for(i=0,s=n;s<r;s++)i+=e.get(s,n)*a.get(s,o);for(i=-i/e.get(n,n),s=n;s<r;s++)a.set(s,o,a.get(s,o)+i*e.get(s,n))}for(n=l-1;n>=0;n--){for(o=0;o<h;o++)a.set(n,o,a.get(n,o)/this.Rdiag[n]);for(s=0;s<n;s++)for(o=0;o<h;o++)a.set(s,o,a.get(s,o)-a.get(n,o)*e.get(s,n))}return a.subMatrix(0,l-1,0,h-1)}isFullRank(){let t=this.QR.columns;for(let e=0;e<t;e++)if(0===this.Rdiag[e])return!1;return!0}get upperTriangularMatrix(){let t,e,r=this.QR,s=r.columns,o=new E(s,s);for(t=0;t<s;t++)for(e=0;e<s;e++)t<e?o.set(t,e,r.get(t,e)):t===e?o.set(t,e,this.Rdiag[t]):o.set(t,e,0);return o}get orthogonalMatrix(){let t,e,r,s,o=this.QR,n=o.rows,i=o.columns,h=new E(n,i);for(r=i-1;r>=0;r--){for(t=0;t<n;t++)h.set(t,r,0);for(h.set(r,r,1),e=r;e<i;e++)if(0!==o.get(r,r)){for(s=0,t=r;t<n;t++)s+=o.get(t,r)*h.get(t,e);for(s=-s/o.get(r,r),t=r;t<n;t++)h.set(t,e,h.get(t,e)+s*o.get(t,r))}}return h}}class F{constructor(t,e={}){if((t=I.checkMatrix(t)).isEmpty())throw new Error("Matrix must be non-empty");let r=t.rows,s=t.columns;const{computeLeftSingularVectors:o=!0,computeRightSingularVectors:n=!0,autoTranspose:i=!1}=e;let h,a=Boolean(o),l=Boolean(n),u=!1;if(r<s)if(i){h=t.transpose(),r=h.rows,s=h.columns,u=!0;let e=a;a=l,l=e}else h=t.clone(),console.warn("Computing SVD on a matrix with more columns than rows. Consider enabling autoTranspose");else h=t.clone();let c=Math.min(r,s),f=Math.min(r+1,s),m=new Float64Array(f),g=new E(r,c),w=new E(s,s),p=new Float64Array(s),d=new Float64Array(r),y=new Float64Array(f);for(let t=0;t<f;t++)y[t]=t;let b=Math.min(r-1,s),M=Math.max(0,Math.min(s-2,r)),x=Math.max(b,M);for(let t=0;t<x;t++){if(t<b){m[t]=0;for(let e=t;e<r;e++)m[t]=T(m[t],h.get(e,t));if(0!==m[t]){h.get(t,t)<0&&(m[t]=-m[t]);for(let e=t;e<r;e++)h.set(e,t,h.get(e,t)/m[t]);h.set(t,t,h.get(t,t)+1)}m[t]=-m[t]}for(let e=t+1;e<s;e++){if(t<b&&0!==m[t]){let s=0;for(let o=t;o<r;o++)s+=h.get(o,t)*h.get(o,e);s=-s/h.get(t,t);for(let o=t;o<r;o++)h.set(o,e,h.get(o,e)+s*h.get(o,t))}p[e]=h.get(t,e)}if(a&&t<b)for(let e=t;e<r;e++)g.set(e,t,h.get(e,t));if(t<M){p[t]=0;for(let e=t+1;e<s;e++)p[t]=T(p[t],p[e]);if(0!==p[t]){p[t+1]<0&&(p[t]=0-p[t]);for(let e=t+1;e<s;e++)p[e]/=p[t];p[t+1]+=1}if(p[t]=-p[t],t+1<r&&0!==p[t]){for(let e=t+1;e<r;e++)d[e]=0;for(let e=t+1;e<r;e++)for(let r=t+1;r<s;r++)d[e]+=p[r]*h.get(e,r);for(let e=t+1;e<s;e++){let s=-p[e]/p[t+1];for(let o=t+1;o<r;o++)h.set(o,e,h.get(o,e)+s*d[o])}}if(l)for(let e=t+1;e<s;e++)w.set(e,t,p[e])}}let S=Math.min(s,r+1);if(b<s&&(m[b]=h.get(b,b)),r<S&&(m[S-1]=0),M+1<S&&(p[M]=h.get(M,S-1)),p[S-1]=0,a){for(let t=b;t<c;t++){for(let e=0;e<r;e++)g.set(e,t,0);g.set(t,t,1)}for(let t=b-1;t>=0;t--)if(0!==m[t]){for(let e=t+1;e<c;e++){let s=0;for(let o=t;o<r;o++)s+=g.get(o,t)*g.get(o,e);s=-s/g.get(t,t);for(let o=t;o<r;o++)g.set(o,e,g.get(o,e)+s*g.get(o,t))}for(let e=t;e<r;e++)g.set(e,t,-g.get(e,t));g.set(t,t,1+g.get(t,t));for(let e=0;e<t-1;e++)g.set(e,t,0)}else{for(let e=0;e<r;e++)g.set(e,t,0);g.set(t,t,1)}}if(l)for(let t=s-1;t>=0;t--){if(t<M&&0!==p[t])for(let e=t+1;e<s;e++){let r=0;for(let o=t+1;o<s;o++)r+=w.get(o,t)*w.get(o,e);r=-r/w.get(t+1,t);for(let o=t+1;o<s;o++)w.set(o,e,w.get(o,e)+r*w.get(o,t))}for(let e=0;e<s;e++)w.set(e,t,0);w.set(t,t,1)}let v=S-1,A=Number.EPSILON;for(;S>0;){let t,e;for(t=S-2;t>=-1&&-1!==t;t--){const e=Number.MIN_VALUE+A*Math.abs(m[t]+Math.abs(m[t+1]));if(Math.abs(p[t])<=e||Number.isNaN(p[t])){p[t]=0;break}}if(t===S-2)e=4;else{let r;for(r=S-1;r>=t&&r!==t;r--){let e=(r!==S?Math.abs(p[r]):0)+(r!==t+1?Math.abs(p[r-1]):0);if(Math.abs(m[r])<=A*e){m[r]=0;break}}r===t?e=3:r===S-1?e=1:(e=2,t=r)}switch(t++,e){case 1:{let e=p[S-2];p[S-2]=0;for(let r=S-2;r>=t;r--){let o=T(m[r],e),n=m[r]/o,i=e/o;if(m[r]=o,r!==t&&(e=-i*p[r-1],p[r-1]=n*p[r-1]),l)for(let t=0;t<s;t++)o=n*w.get(t,r)+i*w.get(t,S-1),w.set(t,S-1,-i*w.get(t,r)+n*w.get(t,S-1)),w.set(t,r,o)}break}case 2:{let e=p[t-1];p[t-1]=0;for(let s=t;s<S;s++){let o=T(m[s],e),n=m[s]/o,i=e/o;if(m[s]=o,e=-i*p[s],p[s]=n*p[s],a)for(let e=0;e<r;e++)o=n*g.get(e,s)+i*g.get(e,t-1),g.set(e,t-1,-i*g.get(e,s)+n*g.get(e,t-1)),g.set(e,s,o)}break}case 3:{const e=Math.max(Math.abs(m[S-1]),Math.abs(m[S-2]),Math.abs(p[S-2]),Math.abs(m[t]),Math.abs(p[t])),o=m[S-1]/e,n=m[S-2]/e,i=p[S-2]/e,h=m[t]/e,u=p[t]/e,c=((n+o)*(n-o)+i*i)/2,f=o*i*(o*i);let d=0;0===c&&0===f||(d=c<0?0-Math.sqrt(c*c+f):Math.sqrt(c*c+f),d=f/(c+d));let y=(h+o)*(h-o)+d,b=h*u;for(let e=t;e<S-1;e++){let o=T(y,b);0===o&&(o=Number.MIN_VALUE);let n=y/o,i=b/o;if(e!==t&&(p[e-1]=o),y=n*m[e]+i*p[e],p[e]=n*p[e]-i*m[e],b=i*m[e+1],m[e+1]=n*m[e+1],l)for(let t=0;t<s;t++)o=n*w.get(t,e)+i*w.get(t,e+1),w.set(t,e+1,-i*w.get(t,e)+n*w.get(t,e+1)),w.set(t,e,o);if(o=T(y,b),0===o&&(o=Number.MIN_VALUE),n=y/o,i=b/o,m[e]=o,y=n*p[e]+i*m[e+1],m[e+1]=-i*p[e]+n*m[e+1],b=i*p[e+1],p[e+1]=n*p[e+1],a&&e<r-1)for(let t=0;t<r;t++)o=n*g.get(t,e)+i*g.get(t,e+1),g.set(t,e+1,-i*g.get(t,e)+n*g.get(t,e+1)),g.set(t,e,o)}p[S-2]=y;break}case 4:if(m[t]<=0&&(m[t]=m[t]<0?-m[t]:0,l))for(let e=0;e<=v;e++)w.set(e,t,-w.get(e,t));for(;t<v&&!(m[t]>=m[t+1]);){let e=m[t];if(m[t]=m[t+1],m[t+1]=e,l&&t<s-1)for(let r=0;r<s;r++)e=w.get(r,t+1),w.set(r,t+1,w.get(r,t)),w.set(r,t,e);if(a&&t<r-1)for(let s=0;s<r;s++)e=g.get(s,t+1),g.set(s,t+1,g.get(s,t)),g.set(s,t,e);t++}S--}}if(u){let t=w;w=g,g=t}this.m=r,this.n=s,this.s=m,this.U=g,this.V=w}solve(t){let e=t,r=this.threshold,s=this.s.length,o=E.zeros(s,s);for(let t=0;t<s;t++)Math.abs(this.s[t])<=r?o.set(t,t,0):o.set(t,t,1/this.s[t]);let n=this.U,i=this.rightSingularVectors,h=i.mmul(o),a=i.rows,l=n.rows,u=E.zeros(a,l);for(let t=0;t<a;t++)for(let e=0;e<l;e++){let r=0;for(let o=0;o<s;o++)r+=h.get(t,o)*n.get(e,o);u.set(t,e,r)}return u.mmul(e)}solveForDiagonal(t){return this.solve(E.diag(t))}inverse(){let t=this.V,e=this.threshold,r=t.rows,s=t.columns,o=new E(r,this.s.length);for(let n=0;n<r;n++)for(let r=0;r<s;r++)Math.abs(this.s[r])>e&&o.set(n,r,t.get(n,r)/this.s[r]);let n=this.U,i=n.rows,h=n.columns,a=new E(r,i);for(let t=0;t<r;t++)for(let e=0;e<i;e++){let r=0;for(let s=0;s<h;s++)r+=o.get(t,s)*n.get(e,s);a.set(t,e,r)}return a}get condition(){return this.s[0]/this.s[Math.min(this.m,this.n)-1]}get norm2(){return this.s[0]}get rank(){let t=Math.max(this.m,this.n)*this.s[0]*Number.EPSILON,e=0,r=this.s;for(let s=0,o=r.length;s<o;s++)r[s]>t&&e++;return e}get diagonal(){return Array.from(this.s)}get threshold(){return Number.EPSILON/2*Math.max(this.m,this.n)*this.s[0]}get leftSingularVectors(){return this.U}get rightSingularVectors(){return this.V}get diagonalMatrix(){return E.diag(this.s)}}function D(t,e,r=!1){return t=I.checkMatrix(t),e=I.checkMatrix(e),r?new F(t).solve(e):t.isSquare()?new z(t).solve(e):new C(t).solve(e)}function q(t,e){let r=[];for(let s=0;s<t;s++)s!==e&&r.push(s);return r}function V(t,e,r,s=1e-9,o=1e-9){if(t>o)return new Array(e.rows+1).fill(0);{let t=e.addRow(r,[0]);for(let e=0;e<t.rows;e++)Math.abs(t.get(e,0))<s&&t.set(e,0,0);return t.to1DArray()}}class P{constructor(t,e={}){const{assumeSymmetric:r=!1}=e;if(!(t=I.checkMatrix(t)).isSquare())throw new Error("Matrix is not a square matrix");if(t.isEmpty())throw new Error("Matrix must be non-empty");let s,o,n=t.columns,i=new E(n,n),h=new Float64Array(n),a=new Float64Array(n),l=t,u=!1;if(u=!!r||t.isSymmetric(),u){for(s=0;s<n;s++)for(o=0;o<n;o++)i.set(s,o,l.get(s,o));!function(t,e,r,s){let o,n,i,h,a,l,u,c;for(a=0;a<t;a++)r[a]=s.get(t-1,a);for(h=t-1;h>0;h--){for(c=0,i=0,l=0;l<h;l++)c+=Math.abs(r[l]);if(0===c)for(e[h]=r[h-1],a=0;a<h;a++)r[a]=s.get(h-1,a),s.set(h,a,0),s.set(a,h,0);else{for(l=0;l<h;l++)r[l]/=c,i+=r[l]*r[l];for(o=r[h-1],n=Math.sqrt(i),o>0&&(n=-n),e[h]=c*n,i-=o*n,r[h-1]=o-n,a=0;a<h;a++)e[a]=0;for(a=0;a<h;a++){for(o=r[a],s.set(a,h,o),n=e[a]+s.get(a,a)*o,l=a+1;l<=h-1;l++)n+=s.get(l,a)*r[l],e[l]+=s.get(l,a)*o;e[a]=n}for(o=0,a=0;a<h;a++)e[a]/=i,o+=e[a]*r[a];for(u=o/(i+i),a=0;a<h;a++)e[a]-=u*r[a];for(a=0;a<h;a++){for(o=r[a],n=e[a],l=a;l<=h-1;l++)s.set(l,a,s.get(l,a)-(o*e[l]+n*r[l]));r[a]=s.get(h-1,a),s.set(h,a,0)}}r[h]=i}for(h=0;h<t-1;h++){if(s.set(t-1,h,s.get(h,h)),s.set(h,h,1),i=r[h+1],0!==i){for(l=0;l<=h;l++)r[l]=s.get(l,h+1)/i;for(a=0;a<=h;a++){for(n=0,l=0;l<=h;l++)n+=s.get(l,h+1)*s.get(l,a);for(l=0;l<=h;l++)s.set(l,a,s.get(l,a)-n*r[l])}}for(l=0;l<=h;l++)s.set(l,h+1,0)}for(a=0;a<t;a++)r[a]=s.get(t-1,a),s.set(t-1,a,0);s.set(t-1,t-1,1),e[0]=0}(n,a,h,i),function(t,e,r,s){let o,n,i,h,a,l,u,c,f,m,g,w,p,d,y,b;for(i=1;i<t;i++)e[i-1]=e[i];e[t-1]=0;let M=0,x=0,S=Number.EPSILON;for(l=0;l<t;l++){for(x=Math.max(x,Math.abs(r[l])+Math.abs(e[l])),u=l;u<t&&!(Math.abs(e[u])<=S*x);)u++;if(u>l)do{for(o=r[l],c=(r[l+1]-o)/(2*e[l]),f=T(c,1),c<0&&(f=-f),r[l]=e[l]/(c+f),r[l+1]=e[l]*(c+f),m=r[l+1],n=o-r[l],i=l+2;i<t;i++)r[i]-=n;for(M+=n,c=r[u],g=1,w=g,p=g,d=e[l+1],y=0,b=0,i=u-1;i>=l;i--)for(p=w,w=g,b=y,o=g*e[i],n=g*c,f=T(c,e[i]),e[i+1]=y*f,y=e[i]/f,g=c/f,c=g*r[i]-y*o,r[i+1]=n+y*(g*o+y*r[i]),a=0;a<t;a++)n=s.get(a,i+1),s.set(a,i+1,y*s.get(a,i)+g*n),s.set(a,i,g*s.get(a,i)-y*n);c=-y*b*p*d*e[l]/m,e[l]=y*c,r[l]=g*c}while(Math.abs(e[l])>S*x);r[l]=r[l]+M,e[l]=0}for(i=0;i<t-1;i++){for(a=i,c=r[i],h=i+1;h<t;h++)r[h]<c&&(a=h,c=r[h]);if(a!==i)for(r[a]=r[i],r[i]=c,h=0;h<t;h++)c=s.get(h,i),s.set(h,i,s.get(h,a)),s.set(h,a,c)}}(n,a,h,i)}else{let t=new E(n,n),e=new Float64Array(n);for(o=0;o<n;o++)for(s=0;s<n;s++)t.set(s,o,l.get(s,o));!function(t,e,r,s){let o,n,i,h,a,l,u,c=t-1;for(l=1;l<=c-1;l++){for(u=0,h=l;h<=c;h++)u+=Math.abs(e.get(h,l-1));if(0!==u){for(i=0,h=c;h>=l;h--)r[h]=e.get(h,l-1)/u,i+=r[h]*r[h];for(n=Math.sqrt(i),r[l]>0&&(n=-n),i-=r[l]*n,r[l]=r[l]-n,a=l;a<t;a++){for(o=0,h=c;h>=l;h--)o+=r[h]*e.get(h,a);for(o/=i,h=l;h<=c;h++)e.set(h,a,e.get(h,a)-o*r[h])}for(h=0;h<=c;h++){for(o=0,a=c;a>=l;a--)o+=r[a]*e.get(h,a);for(o/=i,a=l;a<=c;a++)e.set(h,a,e.get(h,a)-o*r[a])}r[l]=u*r[l],e.set(l,l-1,u*n)}}for(h=0;h<t;h++)for(a=0;a<t;a++)s.set(h,a,h===a?1:0);for(l=c-1;l>=1;l--)if(0!==e.get(l,l-1)){for(h=l+1;h<=c;h++)r[h]=e.get(h,l-1);for(a=l;a<=c;a++){for(n=0,h=l;h<=c;h++)n+=r[h]*s.get(h,a);for(n=n/r[l]/e.get(l,l-1),h=l;h<=c;h++)s.set(h,a,s.get(h,a)+n*r[h])}}}(n,t,e,i),function(t,e,r,s,o){let n,i,h,a,l,u,c,f,m,g,w,p,d,y,b,M=t-1,x=t-1,S=Number.EPSILON,E=0,v=0,A=0,N=0,R=0,k=0,I=0,z=0;for(n=0;n<t;n++)for((n<0||n>x)&&(r[n]=o.get(n,n),e[n]=0),i=Math.max(n-1,0);i<t;i++)v+=Math.abs(o.get(n,i));for(;M>=0;){for(a=M;a>0&&(k=Math.abs(o.get(a-1,a-1))+Math.abs(o.get(a,a)),0===k&&(k=v),!(Math.abs(o.get(a,a-1))<S*k));)a--;if(a===M)o.set(M,M,o.get(M,M)+E),r[M]=o.get(M,M),e[M]=0,M--,z=0;else if(a===M-1){if(c=o.get(M,M-1)*o.get(M-1,M),A=(o.get(M-1,M-1)-o.get(M,M))/2,N=A*A+c,I=Math.sqrt(Math.abs(N)),o.set(M,M,o.get(M,M)+E),o.set(M-1,M-1,o.get(M-1,M-1)+E),f=o.get(M,M),N>=0){for(I=A>=0?A+I:A-I,r[M-1]=f+I,r[M]=r[M-1],0!==I&&(r[M]=f-c/I),e[M-1]=0,e[M]=0,f=o.get(M,M-1),k=Math.abs(f)+Math.abs(I),A=f/k,N=I/k,R=Math.sqrt(A*A+N*N),A/=R,N/=R,i=M-1;i<t;i++)I=o.get(M-1,i),o.set(M-1,i,N*I+A*o.get(M,i)),o.set(M,i,N*o.get(M,i)-A*I);for(n=0;n<=M;n++)I=o.get(n,M-1),o.set(n,M-1,N*I+A*o.get(n,M)),o.set(n,M,N*o.get(n,M)-A*I);for(n=0;n<=x;n++)I=s.get(n,M-1),s.set(n,M-1,N*I+A*s.get(n,M)),s.set(n,M,N*s.get(n,M)-A*I)}else r[M-1]=f+A,r[M]=f+A,e[M-1]=I,e[M]=-I;M-=2,z=0}else{if(f=o.get(M,M),m=0,c=0,a<M&&(m=o.get(M-1,M-1),c=o.get(M,M-1)*o.get(M-1,M)),10===z){for(E+=f,n=0;n<=M;n++)o.set(n,n,o.get(n,n)-f);k=Math.abs(o.get(M,M-1))+Math.abs(o.get(M-1,M-2)),f=m=.75*k,c=-.4375*k*k}if(30===z&&(k=(m-f)/2,k=k*k+c,k>0)){for(k=Math.sqrt(k),m<f&&(k=-k),k=f-c/((m-f)/2+k),n=0;n<=M;n++)o.set(n,n,o.get(n,n)-k);E+=k,f=m=c=.964}for(z+=1,l=M-2;l>=a&&(I=o.get(l,l),R=f-I,k=m-I,A=(R*k-c)/o.get(l+1,l)+o.get(l,l+1),N=o.get(l+1,l+1)-I-R-k,R=o.get(l+2,l+1),k=Math.abs(A)+Math.abs(N)+Math.abs(R),A/=k,N/=k,R/=k,l!==a)&&!(Math.abs(o.get(l,l-1))*(Math.abs(N)+Math.abs(R))<S*(Math.abs(A)*(Math.abs(o.get(l-1,l-1))+Math.abs(I)+Math.abs(o.get(l+1,l+1)))));)l--;for(n=l+2;n<=M;n++)o.set(n,n-2,0),n>l+2&&o.set(n,n-3,0);for(h=l;h<=M-1&&(y=h!==M-1,h!==l&&(A=o.get(h,h-1),N=o.get(h+1,h-1),R=y?o.get(h+2,h-1):0,f=Math.abs(A)+Math.abs(N)+Math.abs(R),0!==f&&(A/=f,N/=f,R/=f)),0!==f);h++)if(k=Math.sqrt(A*A+N*N+R*R),A<0&&(k=-k),0!==k){for(h!==l?o.set(h,h-1,-k*f):a!==l&&o.set(h,h-1,-o.get(h,h-1)),A+=k,f=A/k,m=N/k,I=R/k,N/=A,R/=A,i=h;i<t;i++)A=o.get(h,i)+N*o.get(h+1,i),y&&(A+=R*o.get(h+2,i),o.set(h+2,i,o.get(h+2,i)-A*I)),o.set(h,i,o.get(h,i)-A*f),o.set(h+1,i,o.get(h+1,i)-A*m);for(n=0;n<=Math.min(M,h+3);n++)A=f*o.get(n,h)+m*o.get(n,h+1),y&&(A+=I*o.get(n,h+2),o.set(n,h+2,o.get(n,h+2)-A*R)),o.set(n,h,o.get(n,h)-A),o.set(n,h+1,o.get(n,h+1)-A*N);for(n=0;n<=x;n++)A=f*s.get(n,h)+m*s.get(n,h+1),y&&(A+=I*s.get(n,h+2),s.set(n,h+2,s.get(n,h+2)-A*R)),s.set(n,h,s.get(n,h)-A),s.set(n,h+1,s.get(n,h+1)-A*N)}}}if(0!==v){for(M=t-1;M>=0;M--)if(A=r[M],N=e[M],0===N)for(a=M,o.set(M,M,1),n=M-1;n>=0;n--){for(c=o.get(n,n)-A,R=0,i=a;i<=M;i++)R+=o.get(n,i)*o.get(i,M);if(e[n]<0)I=c,k=R;else if(a=n,0===e[n]?o.set(n,M,0!==c?-R/c:-R/(S*v)):(f=o.get(n,n+1),m=o.get(n+1,n),N=(r[n]-A)*(r[n]-A)+e[n]*e[n],u=(f*k-I*R)/N,o.set(n,M,u),o.set(n+1,M,Math.abs(f)>Math.abs(I)?(-R-c*u)/f:(-k-m*u)/I)),u=Math.abs(o.get(n,M)),S*u*u>1)for(i=n;i<=M;i++)o.set(i,M,o.get(i,M)/u)}else if(N<0)for(a=M-1,Math.abs(o.get(M,M-1))>Math.abs(o.get(M-1,M))?(o.set(M-1,M-1,N/o.get(M,M-1)),o.set(M-1,M,-(o.get(M,M)-A)/o.get(M,M-1))):(b=W(0,-o.get(M-1,M),o.get(M-1,M-1)-A,N),o.set(M-1,M-1,b[0]),o.set(M-1,M,b[1])),o.set(M,M-1,0),o.set(M,M,1),n=M-2;n>=0;n--){for(g=0,w=0,i=a;i<=M;i++)g+=o.get(n,i)*o.get(i,M-1),w+=o.get(n,i)*o.get(i,M);if(c=o.get(n,n)-A,e[n]<0)I=c,R=g,k=w;else if(a=n,0===e[n]?(b=W(-g,-w,c,N),o.set(n,M-1,b[0]),o.set(n,M,b[1])):(f=o.get(n,n+1),m=o.get(n+1,n),p=(r[n]-A)*(r[n]-A)+e[n]*e[n]-N*N,d=2*(r[n]-A)*N,0===p&&0===d&&(p=S*v*(Math.abs(c)+Math.abs(N)+Math.abs(f)+Math.abs(m)+Math.abs(I))),b=W(f*R-I*g+N*w,f*k-I*w-N*g,p,d),o.set(n,M-1,b[0]),o.set(n,M,b[1]),Math.abs(f)>Math.abs(I)+Math.abs(N)?(o.set(n+1,M-1,(-g-c*o.get(n,M-1)+N*o.get(n,M))/f),o.set(n+1,M,(-w-c*o.get(n,M)-N*o.get(n,M-1))/f)):(b=W(-R-m*o.get(n,M-1),-k-m*o.get(n,M),I,N),o.set(n+1,M-1,b[0]),o.set(n+1,M,b[1]))),u=Math.max(Math.abs(o.get(n,M-1)),Math.abs(o.get(n,M))),S*u*u>1)for(i=n;i<=M;i++)o.set(i,M-1,o.get(i,M-1)/u),o.set(i,M,o.get(i,M)/u)}for(n=0;n<t;n++)if(n<0||n>x)for(i=n;i<t;i++)s.set(n,i,o.get(n,i));for(i=t-1;i>=0;i--)for(n=0;n<=x;n++){for(I=0,h=0;h<=Math.min(i,x);h++)I+=s.get(n,h)*o.get(h,i);s.set(n,i,I)}}}(n,a,h,i,t)}this.n=n,this.e=a,this.d=h,this.V=i}get realEigenvalues(){return Array.from(this.d)}get imaginaryEigenvalues(){return Array.from(this.e)}get eigenvectorMatrix(){return this.V}get diagonalMatrix(){let t,e,r=this.n,s=this.e,o=this.d,n=new E(r,r);for(t=0;t<r;t++){for(e=0;e<r;e++)n.set(t,e,0);n.set(t,t,o[t]),s[t]>0?n.set(t,t+1,s[t]):s[t]<0&&n.set(t,t-1,s[t])}return n}}function W(t,e,r,s){let o,n;return Math.abs(r)>Math.abs(s)?(o=s/r,n=r+o*s,[(t+o*e)/n,(e-o*t)/n]):(o=r/s,n=s+o*r,[(o*t+e)/n,(o*e-t)/n])}class j{constructor(t){if(!(t=I.checkMatrix(t)).isSymmetric())throw new Error("Matrix is not symmetric");let e,r,s,o=t,n=o.rows,i=new E(n,n),h=!0;for(r=0;r<n;r++){let t=0;for(s=0;s<r;s++){let n=0;for(e=0;e<s;e++)n+=i.get(s,e)*i.get(r,e);n=(o.get(r,s)-n)/i.get(s,s),i.set(r,s,n),t+=n*n}for(t=o.get(r,r)-t,h&&=t>0,i.set(r,r,Math.sqrt(Math.max(t,0))),s=r+1;s<n;s++)i.set(r,s,0)}this.L=i,this.positiveDefinite=h}isPositiveDefinite(){return this.positiveDefinite}solve(t){t=I.checkMatrix(t);let e=this.L,r=e.rows;if(t.rows!==r)throw new Error("Matrix dimensions do not match");if(!1===this.isPositiveDefinite())throw new Error("Matrix is not positive definite");let s,o,n,i=t.columns,h=t.clone();for(n=0;n<r;n++)for(o=0;o<i;o++){for(s=0;s<n;s++)h.set(n,o,h.get(n,o)-h.get(s,o)*e.get(n,s));h.set(n,o,h.get(n,o)/e.get(n,n))}for(n=r-1;n>=0;n--)for(o=0;o<i;o++){for(s=n+1;s<r;s++)h.set(n,o,h.get(n,o)-h.get(s,o)*e.get(s,n));h.set(n,o,h.get(n,o)/e.get(n,n))}return h}get lowerTriangularMatrix(){return this.L}}class L{constructor(t,e={}){t=I.checkMatrix(t);let{Y:r}=e;const{scaleScores:o=!1,maxIterations:n=1e3,terminationCriteria:i=1e-10}=e;let h;if(r){if(r=s.isAnyArray(r)&&"number"==typeof r[0]?E.columnVector(r):I.checkMatrix(r),r.rows!==t.rows)throw new Error("Y should have the same number of rows as X");h=r.getColumnVector(0)}else h=t.getColumnVector(0);let a,l,u,c,f=1;for(let e=0;e<n&&f>i;e++)u=t.transpose().mmul(h).div(h.transpose().mmul(h).get(0,0)),u=u.div(u.norm()),a=t.mmul(u).div(u.transpose().mmul(u).get(0,0)),e>0&&(f=a.clone().sub(c).pow(2).sum()),c=a.clone(),r?(l=r.transpose().mmul(a).div(a.transpose().mmul(a).get(0,0)),l=l.div(l.norm()),h=r.mmul(l).div(l.transpose().mmul(l).get(0,0))):h=a;if(r){let e=t.transpose().mmul(a).div(a.transpose().mmul(a).get(0,0));e=e.div(e.norm());let s=t.clone().sub(a.clone().mmul(e.transpose())),o=h.transpose().mmul(a).div(a.transpose().mmul(a).get(0,0)),n=r.clone().sub(a.clone().mulS(o.get(0,0)).mmul(l.transpose()));this.t=a,this.p=e.transpose(),this.w=u.transpose(),this.q=l,this.u=h,this.s=a.transpose().mmul(a),this.xResidual=s,this.yResidual=n,this.betas=o}else this.w=u.transpose(),this.s=a.transpose().mmul(a).sqrt(),this.t=o?a.clone().div(this.s.get(0,0)):a,this.xResidual=t.sub(a.mmul(u.transpose()))}}e.y3=M,e.jy=j,e.oN=j,e.Hc=A,e.cg=P,e.hj=P,e.LU=z,e.Tb=z,e.uq=E,e.Zm=class extends N{constructor(t,e){w(t,e),super(t,t.rows,e.length),this.columnIndices=e}set(t,e,r){return this.matrix.set(t,this.columnIndices[e],r),this}get(t,e){return this.matrix.get(t,this.columnIndices[e])}},e.Dq=class extends N{constructor(t,e){c(t,e),super(t,t.rows,1),this.column=e}set(t,e,r){return this.matrix.set(t,this.column,r),this}get(t){return this.matrix.get(t,this.column)}},e.__=class extends N{constructor(t){super(t,t.rows,t.columns)}set(t,e,r){return this.matrix.set(t,this.columns-e-1,r),this}get(t,e){return this.matrix.get(t,this.columns-e-1)}},e.q0=class extends N{constructor(t){super(t,t.rows,t.columns)}set(t,e,r){return this.matrix.set(this.rows-t-1,e,r),this}get(t,e){return this.matrix.get(this.rows-t-1,e)}},e.lh=class extends N{constructor(t,e){g(t,e),super(t,e.length,t.columns),this.rowIndices=e}set(t,e,r){return this.matrix.set(this.rowIndices[t],e,r),this}get(t,e){return this.matrix.get(this.rowIndices[t],e)}},e.pI=class extends N{constructor(t,e){u(t,e),super(t,1,t.columns),this.row=e}set(t,e,r){return this.matrix.set(this.row,e,r),this}get(t,e){return this.matrix.get(this.row,e)}},e.zC=R,e.zg=class extends N{constructor(t,e,r,s,o){p(t,e,r,s,o),super(t,r-e+1,o-s+1),this.startRow=e,this.startColumn=s}set(t,e,r){return this.matrix.set(this.startRow+t,this.startColumn+e,r),this}get(t,e){return this.matrix.get(this.startRow+t,this.startColumn+e)}},e.g6=class extends N{constructor(t){super(t,t.columns,t.rows)}set(t,e,r){return this.matrix.set(e,t,r),this}get(t,e){return this.matrix.get(e,t)}},e.OL=L,e.ks=L,e.QR=C,e.jp=C,e.mk=F,e.W2=F,e.l=v,e.KY=k,e.dv=I,e.BR=function(t,e=t,r={}){t=new E(t);let o=!1;if("object"!=typeof e||E.isMatrix(e)||s.isAnyArray(e)?e=new E(e):(r=e,e=t,o=!0),t.rows!==e.rows)throw new TypeError("Both matrices must have the same number of rows");const{center:n=!0,scale:i=!0}=r;n&&(t.center("column"),o||e.center("column")),i&&(t.scale("column"),o||e.scale("column"));const h=t.standardDeviation("column",{unbiased:!0}),a=o?h:e.standardDeviation("column",{unbiased:!0}),l=t.transpose().mmul(e);for(let e=0;e<l.rows;e++)for(let r=0;r<l.columns;r++)l.set(e,r,l.get(e,r)*(1/(h[e]*a[r]))*(1/(t.rows-1)));return l},e.Wu=function(t,e=t,r={}){t=new E(t);let o=!1;if("object"!=typeof e||E.isMatrix(e)||s.isAnyArray(e)?e=new E(e):(r=e,e=t,o=!0),t.rows!==e.rows)throw new TypeError("Both matrices must have the same number of rows");const{center:n=!0}=r;n&&(t=t.center("column"),o||(e=e.center("column")));const i=t.transpose().mmul(e);for(let e=0;e<i.rows;e++)for(let r=0;r<i.columns;r++)i.set(e,r,i.get(e,r)*(1/(t.rows-1)));return i},e.a4=function t(e){if((e=E.checkMatrix(e)).isSquare()){if(0===e.columns)return 1;let r,s,o,n;if(2===e.columns)return r=e.get(0,0),s=e.get(0,1),o=e.get(1,0),n=e.get(1,1),r*n-s*o;if(3===e.columns){let n,i,h;return n=new R(e,[1,2],[1,2]),i=new R(e,[1,2],[0,2]),h=new R(e,[1,2],[0,1]),r=e.get(0,0),s=e.get(0,1),o=e.get(0,2),r*t(n)-s*t(i)+o*t(h)}return new z(e).determinant}throw Error("determinant can only be calculated for a square matrix")},e.DI=function(t,e=!1){return t=I.checkMatrix(t),e?new F(t).inverse():D(t,E.eye(t.rows))},e.Jo=function(t,e={}){const{thresholdValue:r=1e-9,thresholdError:s=1e-9}=e;let o=(t=E.checkMatrix(t)).rows,n=new E(o,o);for(let e=0;e<o;e++){let i=E.columnVector(t.getRow(e)),h=t.subMatrixRow(q(o,e)).transpose(),a=new F(h).solve(i),l=E.sub(i,h.mmul(a)).abs().max();n.setRow(e,V(l,a,e,r,s))}return n},e.Zi=function(t,e=Number.EPSILON){if((t=E.checkMatrix(t)).isEmpty())return t.transpose();let r=new F(t,{autoTranspose:!0}),s=r.leftSingularVectors,o=r.rightSingularVectors,n=r.diagonal;for(let t=0;t<n.length;t++)Math.abs(n[t])>e?n[t]=1/n[t]:n[t]=0;return o.mmul(E.diag(n).mmul(s.transpose()))},e.kH=D,e.LV=function(t,e){if(s.isAnyArray(t))return t[0]&&s.isAnyArray(t[0])?new I(t):new k(t,e);throw new Error("the argument is not an array")}},718(t,e,r){"use strict";r.r(e),r.d(e,{default:()=>o});var s=r(788);function o(t){var e,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!(0,s.isAnyArray)(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");if(void 0!==r.output){if(!(0,s.isAnyArray)(r.output))throw new TypeError("output option must be an array if specified");e=r.output}else e=new Array(t.length);var o=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!(0,s.isAnyArray)(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");var r=e.fromIndex,o=void 0===r?0:r,n=e.toIndex,i=void 0===n?t.length:n;if(o<0||o>=t.length||!Number.isInteger(o))throw new Error("fromIndex must be a positive integer smaller than length");if(i<=o||i>t.length||!Number.isInteger(i))throw new Error("toIndex must be an integer greater than fromIndex and at most equal to length");for(var h=t[o],a=o+1;a<i;a++)t[a]<h&&(h=t[a]);return h}(t),n=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!(0,s.isAnyArray)(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");var r=e.fromIndex,o=void 0===r?0:r,n=e.toIndex,i=void 0===n?t.length:n;if(o<0||o>=t.length||!Number.isInteger(o))throw new Error("fromIndex must be a positive integer smaller than length");if(i<=o||i>t.length||!Number.isInteger(i))throw new Error("toIndex must be an integer greater than fromIndex and at most equal to length");for(var h=t[o],a=o+1;a<i;a++)t[a]>h&&(h=t[a]);return h}(t);if(o===n)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");var i=r.min,h=void 0===i?r.autoMinMax?o:0:i,a=r.max,l=void 0===a?r.autoMinMax?n:1:a;if(h>=l)throw new RangeError("min option must be smaller than max option");for(var u=(l-h)/(n-o),c=0;c<t.length;c++)e[c]=(t[c]-o)*u+h;return e}},788(t,e,r){"use strict";r.r(e),r.d(e,{isAnyArray:()=>o});const s=Object.prototype.toString;function o(t){const e=s.call(t);return e.endsWith("Array]")&&!e.includes("Big")}}},e={};function r(s){var o=e[s];if(void 0!==o)return o.exports;var n=e[s]={exports:{}};return t[s](n,n.exports,r),n.exports}r.d=(t,e)=>{for(var s in e)r.o(e,s)&&!r.o(t,s)&&Object.defineProperty(t,s,{enumerable:!0,get:e[s]})},r.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r.r=t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})};var s={};return(()=>{"use strict";function t(t,e){return Math.floor(e()*t)}function e(t){return t()}function o(t){const e=[];for(let r=0;r<t;r++)e.push(void 0);return e}function n(t){return o(t).map((t,e)=>e)}function i(t,e){return o(t).map(()=>e)}function h(t){return i(t,0)}function a(t){return function(t){return t.reduce((t,e)=>t+e)}(t)/t.length}function l(t){let e=0;for(let r=0;r<t.length;r++)e=t[r]>e?t[r]:e;return e}function u(e,r,s){const o=h(e);for(let n=0;n<e;n++){let e=!0;for(;e;){const i=t(r,s);let h=!1;for(let t=0;t<n;t++)if(i===o[t]){h=!0;break}h||(e=!1),o[n]=i}}return o}function c(t,e,r){const s=[];let o=0,n=0;if(t.length!==e*r)throw new Error("Array dimensions must match input length.");for(let i=0;i<e;i++){const e=[];for(let s=0;s<r;s++)e.push(t[n]),n+=1;s.push(e),o+=1}return s}function f(t,e){const r=r=>o(t).map(()=>i(e,r)),s=[];return s.push(r(-1)),s.push(r(1/0)),s.push(r(0)),s}function m(e,r,s){const o=h(e);for(let n=0;n<e;n++){let e=!0,i=0;for(;e;){i=t(r,s);let h=!1;for(let t=0;t<n;t++)if(i===o[t]){h=!0;break}h||(e=!1)}o[n]=i}return o}function g(t,e,r,s,o){e=Math.floor(e);const n=t[0][e],i=t[1][e];if(t[2][e],r>=i[0])return 0;for(let t=0;t<n.length;t++)if(s===n[t])return 0;return w(t,e,r,s,o)}function w(t,e,r,s,o){const n=t[0][e],i=t[1][e],h=t[2][e];if(r>=i[0])return 0;i[0]=r,n[0]=s,h[0]=o;let a=0,l=0;for(;;){const e=2*a+1,s=e+1,o=t[0][0].length;if(e>=o)break;if(s>=o){if(!(i[e]>r))break;l=e}else if(i[e]>=i[s]){if(!(r<i[e]))break;l=e}else{if(!(r<i[s]))break;l=s}i[a]=i[l],n[a]=n[l],h[a]=h[l],a=l}return i[a]=r,n[a]=s,h[a]=o,1}function p(t,r,s,o,n){const i=f(r,o);for(let o=0;o<r;o++)for(let r=0;r<s;r++){if(t[0][o][r]<0)continue;const s=t[0][o][r],h=t[2][o][r],a=e(n);g(i,o,a,s,h),g(i,s,a,o,h),t[2][o][r]=0}return i}function d(t){const e=t[0],r=t[1];for(let t=0;t<e.length;t++){const s=e[t],o=r[t];for(let t=0;t<s.length-1;t++){const e=s.length-t-1,r=o.length-t-1,n=s[0];s[0]=s[e],s[e]=n;const i=o[0];o[0]=o[r],o[r]=i,y(o,s,r,0)}}return{indices:e,weights:r}}function y(t,e,r,s){for(;2*s+1<r;){const o=2*s+1,n=o+1;let i=s;if(t[i]<t[o]&&(i=o),n<r&&t[i]<t[n]&&(i=n),i===s)break;{const r=t[s];t[s]=t[i],t[i]=r;const o=e[s];e[s]=e[i],e[i]=o,s=i}}}function b(t,e){const r=t[0][e],s=t[1][e],o=t[2][e];let n=1/0,i=-1;for(let t=0;t>r.length;t++)1===o[t]&&s[t]<n&&(n=s[t],i=t);return i>=0?(o[i]=0,Math.floor(r[i])):-1}r.r(s),r.d(s,{UMAP:()=>ht,initWasm:()=>F,isWasmAvailable:()=>D});class M{constructor(t,e,r,s){if(this.entries=new Map,this.nRows=0,this.nCols=0,t.length!==e.length||t.length!==r.length)throw new Error("rows, cols and values arrays must all have the same length");this.nRows=s[0],this.nCols=s[1];for(let s=0;s<r.length;s++){const o=t[s],n=e[s];this.checkDims(o,n);const i=this.makeKey(o,n);this.entries.set(i,{value:r[s],row:o,col:n})}}makeKey(t,e){return`${t}:${e}`}checkDims(t,e){if(!(t<this.nRows&&e<this.nCols))throw new Error("row and/or col specified outside of matrix dimensions")}set(t,e,r){this.checkDims(t,e);const s=this.makeKey(t,e);this.entries.has(s)?this.entries.get(s).value=r:this.entries.set(s,{value:r,row:t,col:e})}get(t,e,r=0){this.checkDims(t,e);const s=this.makeKey(t,e);return this.entries.has(s)?this.entries.get(s).value:r}getAll(t=!0){const e=[];return this.entries.forEach(t=>{e.push(t)}),t&&e.sort((t,e)=>t.row===e.row?t.col-e.col:t.row-e.row),e}getDims(){return[this.nRows,this.nCols]}getRows(){return Array.from(this.entries,([t,e])=>e.row)}getCols(){return Array.from(this.entries,([t,e])=>e.col)}getValues(){return Array.from(this.entries,([t,e])=>e.value)}forEach(t){this.entries.forEach(e=>t(e.value,e.row,e.col))}map(t){let e=[];this.entries.forEach(r=>{e.push(t(r.value,r.row,r.col))});const r=[this.nRows,this.nCols];return new M(this.getRows(),this.getCols(),e,r)}toArray(){const t=o(this.nRows).map(()=>h(this.nCols));return this.entries.forEach(e=>{t[e.row][e.col]=e.value}),t}}function x(t){const e=[],r=[],s=[];t.forEach((t,o,n)=>{e.push(o),r.push(n),s.push(t)});const o=[t.nCols,t.nRows];return new M(r,e,s,o)}function S(t,e){return I(t,e,(t,e)=>t*e)}function E(t,e){return I(t,e,(t,e)=>t+e)}function v(t,e){return I(t,e,(t,e)=>t-e)}function A(t,e){return t.map(t=>t*e)}function N(t){const e=new Set,r=t.getValues(),s=t.getRows(),o=t.getCols();for(let t=0;t<r.length;t++)0===r[t]&&e.add(t);const n=(t,r)=>!e.has(r),i=r.filter(n),h=s.filter(n),a=o.filter(n);return new M(h,a,i,t.getDims())}function R(t,e="l2"){const r=k[e],s=new Map;t.forEach((t,e,r)=>{const o=s.get(e)||[];o.push(r),s.set(e,o)});const o=new M([],[],[],t.getDims());for(let e of s.keys()){const n=s.get(e).sort(),i=r(n.map(r=>t.get(e,r)));for(let t=0;t<i.length;t++)o.set(e,n[t],i[t])}return o}const k={max:t=>{let e=-1/0;for(let r=0;r<t.length;r++)e=t[r]>e?t[r]:e;return t.map(t=>t/e)},l1:t=>{let e=0;for(let r=0;r<t.length;r++)e+=t[r];return t.map(t=>t/e)},l2:t=>{let e=0;for(let r=0;r<t.length;r++)e+=t[r]**2;return t.map(t=>Math.sqrt(t**2/e))}};function I(t,e,r){const s=new Set,o=[],n=[],i=[],h=(s,h)=>{o.push(s),n.push(h);const a=r(t.get(s,h),e.get(s,h));i.push(a)},a=t.getValues(),l=t.getRows(),u=t.getCols();for(let t=0;t<a.length;t++){const e=l[t],r=u[t],o=`${e}:${r}`;s.add(o),h(e,r)}const c=e.getValues(),f=e.getRows(),m=e.getCols();for(let t=0;t<c.length;t++){const e=f[t],r=m[t],o=`${e}:${r}`;s.has(o)||h(e,r)}const g=[t.nRows,t.nCols];return new M(o,n,i,g)}function z(t){const e=[];t.forEach((t,r,s)=>{e.push({value:t,row:r,col:s})}),e.sort((t,e)=>t.row===e.row?t.col-e.col:t.row-e.row);const r=[],s=[],o=[];let n=-1;for(let t=0;t<e.length;t++){const{row:i,col:h,value:a}=e[t];i!==n&&(n=i,o.push(t)),r.push(h),s.push(a)}return{indices:r,values:s,indptr:o}}let T=null,C=null;async function F(){return T||(T=(async()=>{try{let t;if("undefined"!=typeof process&&null!=process.versions&&null!=process.versions.node){const e=["..","wasm","pkg","node","umap_wasm_core.js"].join("/");t=await r(433)(e)}else{const e=`${"undefined"!=typeof window&&window.location?window.location.origin:""}/wasm/pkg/web/umap_wasm_core.js`;t=await new Function("p","return import(p)")(e)}return"function"==typeof t.default&&await t.default(),C=t,t}catch(t){throw T=null,C=null,new Error(`Failed to load WASM module: ${t}`)}})(),T)}function D(){return null!==C}function q(t,e){if(!C)throw new Error("WASM module not initialized");const r=new Float64Array(t),s=new Float64Array(e);return C.euclidean(r,s)}function V(t,e,r,s,o){if(!C)throw new Error("WASM module not initialized");const n=new Float64Array(e*r);for(let s=0;s<e;s++)for(let e=0;e<r;e++)n[s*r+e]=t[s][e];return C.build_rp_tree(n,e,r,s,BigInt(o))}function P(t,e,r,s,o){if(!C)throw new Error("WASM module not initialized");const n=new Int32Array(t),i=new Int32Array(e),h=new Float64Array(r);return new C.WasmSparseMatrix(n,i,h,s,o)}function W(t){if(!C)throw new Error("WASM module not initialized");return C.sparse_transpose(t)}function j(t,e){if(!C)throw new Error("WASM module not initialized");return C.sparse_add(t,e)}function L(t,e){if(!C)throw new Error("WASM module not initialized");return C.sparse_subtract(t,e)}function O(t,e){if(!C)throw new Error("WASM module not initialized");return C.sparse_pairwise_multiply(t,e)}function $(t,e){if(!C)throw new Error("WASM module not initialized");return C.sparse_multiply_scalar(t,e)}function _(t){const e=Array.from(t.get_all_ordered()),r=[];for(let t=0;t<e.length;t+=3)r.push({row:e[t],col:e[t+1],value:e[t+2]});return r}class U{constructor(t,e,r,s){this.hyperplanes=t,this.offsets=e,this.children=r,this.indices=s}static fromWasm(t){const e=function(t){const e=Array.from(t.hyperplanes()),r=Array.from(t.offsets()),s=Array.from(t.children()),o=Array.from(t.indices()),n=t.dim(),i=t.n_nodes(),h=[];for(let t=0;t<i;t++)h.push(e.slice(t*n,(t+1)*n));const a=[];for(let t=0;t<i;t++)a.push([s[2*t],s[2*t+1]]);let l=0;for(let t=0;t<s.length;t++){const e=s[t];if(e<=0){const t=-e;t>l&&(l=t)}}const u=l+1,c=u>0?Math.floor(o.length/u):0,f=[];for(let t=0;t<u;t++){const e=o.slice(t*c,(t+1)*c);for(;e.length<c;)e.push(-1);f.push(e)}return{hyperplanes:h,offsets:r,children:a,indices:f}}(t),r=new U(e.hyperplanes,e.offsets,e.children,e.indices);return r.wasmTree=t,r}getWasmTree(){return this.wasmTree}dispose(){this.wasmTree&&(this.wasmTree.free(),this.wasmTree=void 0)}}function B(t,e,r,s,o=!1){const i=Math.max(10,e);if(o){if(!D())throw new Error("WASM requested but not available");return function(t,e,r,s){const o=t.length,n=t[0].length,i=[];for(let h=0;h<r;h++){const r=V(t,o,n,e,Math.floor(4294967295*s()));i.push(U.fromWasm(r))}return i}(t,i,r,s)}const a=n(r).map((e,r)=>function(t,e=30,r,s){return K(t,n(t.length),e,r,s)}(t,i,r,s)),l=a.map(t=>function(t,e){const r=Q(t),s=Y(t),o=n(r).map(()=>h(t.hyperplane?t.hyperplane.length:0)),i=h(r),a=n(r).map(()=>[-1,-1]),l=n(s).map(()=>n(e).map(()=>-1));return G(t,o,i,a,l,0,0),new U(o,i,a,l)}(t,i));return l}function K(e,r,s=30,o,n){if(r.length>s){const i=function(e,r,s){const o=e[0].length;let n=t(r.length,s),i=t(r.length,s);i+=n===i?1:0,i%=r.length;const a=r[n],l=r[i];let u=0;const c=h(o);for(let t=0;t<c.length;t++)c[t]=e[a][t]-e[l][t],u-=c[t]*(e[a][t]+e[l][t])/2;let f=0,m=0;const g=h(r.length);for(let n=0;n<r.length;n++){let i=u;for(let t=0;t<o;t++)i+=c[t]*e[r[n]][t];0===i?(g[n]=t(2,s),0===g[n]?f+=1:m+=1):i>0?(g[n]=0,f+=1):(g[n]=1,m+=1)}const w=h(f),p=h(m);f=0,m=0;for(let t=0;t<g.length;t++)0===g[t]?(w[f]=r[t],f+=1):(p[m]=r[t],m+=1);return{indicesLeft:w,indicesRight:p,hyperplane:c,offset:u}}(e,r,n),{indicesLeft:a,indicesRight:l,hyperplane:u,offset:c}=i;return{leftChild:K(e,a,s,o+1,n),rightChild:K(e,l,s,o+1,n),isLeaf:!1,hyperplane:u,offset:c}}return{indices:r,isLeaf:!0}}function G(t,e,r,s,o,n,i){if(t.isLeaf)return s[n][0]=-i,o[i].splice(0,t.indices.length,...t.indices),{nodeNum:n,leafNum:i+=1};{e[n]=t.hyperplane,r[n]=t.offset,s[n][0]=n+1;const h=n;let a=G(t.leftChild,e,r,s,o,n+1,i);return n=a.nodeNum,i=a.leafNum,s[h][1]=n+1,a=G(t.rightChild,e,r,s,o,n+1,i),{nodeNum:a.nodeNum,leafNum:a.leafNum}}}function Q(t){return t.isLeaf?1:1+Q(t.leftChild)+Q(t.rightChild)}function Y(t){return t.isLeaf?1:Y(t.leftChild)+Y(t.rightChild)}function X(e,r,s,o){let n=r;for(let t=0;t<s.length;t++)n+=e[t]*s[t];return 0===n?t(2,o):n>0?0:1}function J(t,e,r){const s=e.getWasmTree();if(s&&D())return function(t,e,r){if(!C)throw new Error("WASM module not initialized");const s=new Float64Array(e),o=C.search_flat_tree(t,s,BigInt(r));return Array.from(o)}(s,t,Math.floor(4294967295*r()));let o=0;for(;e.children[o][0]>0;)o=0===X(e.hyperplanes[o],e.offsets[o],t,r)?e.children[o][0]:e.children[o][1];const n=-1*e.children[o][0];return e.indices[n]}const H=Object.prototype.toString;function Z(t){return H.call(t).endsWith("Array]")}function tt(t,e,r){let s=0;const o=r(e);for(let e=0;e<t.x.length;e++)s+=Math.abs(t.y[e]-o(t.x[e]));return s}var et=r(673);et.y3,et.jy,et.oN,et.Hc,et.cg,et.hj,et.LU,et.Tb;const rt=et.uq,st=(et.Zm,et.Dq,et.__,et.q0,et.lh,et.pI,et.zC,et.zg,et.g6,et.OL,et.ks,et.QR,et.jp,et.mk,et.W2,et.l,et.KY,et.dv,et.BR,et.Wu,et.uq,et.uq,et.a4,et.DI);function ot(t,e,r,s,o){let n=r*s*s,i=rt.eye(e.length,e.length,n);const h=o(e);let a=new Float64Array(t.x.length);for(let e=0;e<t.x.length;e++)a[e]=h(t.x[e]);let l=function(t,e,r,s,o){const n=r.length,i=t.x.length;let h=new Array(n);for(let a=0;a<n;a++){h[a]=new Array(i);let n=r.slice();n[a]+=s;let l=o(n);for(let r=0;r<i;r++)h[a][r]=e[r]-l(t.x[r])}return new rt(h)}(t,a,e,s,o),u=function(t,e){const r=t.x.length;let s=new Array(r);for(let o=0;o<r;o++)s[o]=[t.y[o]-e[o]];return new rt(s)}(t,a),c=st(i.add(l.mmul(l.transpose())));return(e=(e=new rt([e])).sub(c.mmul(l).mmul(u).mul(s).transpose())).to1DArray()}et.Jo,et.Zi,et.kH,et.LV;const nt=1e-5,it=.001;class ht{constructor(t={}){this.learningRate=1,this.localConnectivity=1,this.minDist=.1,this.nComponents=2,this.nEpochs=0,this.nNeighbors=15,this.negativeSampleRate=5,this.random=Math.random,this.repulsionStrength=1,this.setOpMixRatio=1,this.spread=1,this.transformQueueSize=4,this.targetMetric="categorical",this.targetWeight=.5,this.targetNNeighbors=this.nNeighbors,this.distanceFn=at,this.useWasmDistance=!1,this.useWasmNNDescent=!1,this.useWasmMatrix=!1,this.useWasmTree=!1,this.isInitialized=!1,this.rpForest=[],this.embedding=[],this.optimizationState=new lt;const e=e=>{void 0!==t[e]&&(this[e]=t[e])};e("distanceFn"),e("useWasmDistance"),e("useWasmNNDescent"),e("useWasmMatrix"),e("useWasmTree"),e("learningRate"),e("localConnectivity"),e("minDist"),e("nComponents"),e("nEpochs"),e("nNeighbors"),e("negativeSampleRate"),e("random"),e("repulsionStrength"),e("setOpMixRatio"),e("spread"),e("transformQueueSize")}fit(t){return this.initializeFit(t),this.optimizeLayout(),this.embedding}async fitAsync(t,e=()=>!0){return this.initializeFit(t),await this.optimizeLayoutAsync(e),this.embedding}setSupervisedProjection(t,e={}){this.Y=t,this.targetMetric=e.targetMetric||this.targetMetric,this.targetWeight=e.targetWeight||this.targetWeight,this.targetNNeighbors=e.targetNNeighbors||this.targetNNeighbors}setPrecomputedKNN(t,e){this.knnIndices=t,this.knnDistances=e}initializeFit(t){if(t.length<=this.nNeighbors)throw new Error(`Not enough data points (${t.length}) to create nNeighbors: ${this.nNeighbors}. Add more data points or adjust the configuration.`);if(this.X===t&&this.isInitialized)return this.getNEpochs();if(this.X=t,!this.knnIndices&&!this.knnDistances){const e=this.nearestNeighbors(t);this.knnIndices=e.knnIndices,this.knnDistances=e.knnDistances}this.graph=this.fuzzySimplicialSet(t,this.nNeighbors,this.setOpMixRatio),this.makeSearchFns(),this.searchGraph=this.makeSearchGraph(t),this.processGraphForSupervisedProjection();const{head:e,tail:r,epochsPerSample:s}=this.initializeSimplicialSetEmbedding();return this.optimizationState.head=e,this.optimizationState.tail=r,this.optimizationState.epochsPerSample=s,this.initializeOptimization(),this.prepareForOptimizationLoop(),this.isInitialized=!0,this.getNEpochs()}makeSearchFns(){const t=(t,e)=>{if(this.useWasmDistance){if(!D())throw new Error("WASM distance requested via `useWasmDistance: true` but the wasm module is not initialized or available. Call `await wasmBridge.initWasm()` before using UMAP with wasm distances or build the wasm package.");return q(t,e)}return this.distanceFn(t,e)},{initFromTree:e,initFromRandom:r}=(s=t,{initFromRandom:function(t,e,r,o,n){for(let i=0;i<r.length;i++){const h=u(t,e.length,n);for(let t=0;t<h.length;t++)h[t]<0||g(o,i,s(e[h[t]],r[i]),h[t],1)}},initFromTree:function(t,e,r,o,n){for(let i=0;i<r.length;i++){const h=J(r[i],t,n);for(let t=0;t<h.length;t++){if(h[t]<0)return;g(o,i,s(e[h[t]],r[i]),h[t],1)}}}});var s;this.initFromTree=e,this.initFromRandom=r,this.search=function(t){return function(e,r,s,o){const{indices:n,indptr:i}=z(r);for(let r=0;r<o.length;r++){const h=new Set(s[0][r]);for(;;){const a=b(s,r);if(-1===a)break;const l=n.slice(i[a],i[a+1]);for(const n of l)n===a||-1===n||h.has(n)||(w(s,r,t(e[n],o[r]),n,1),h.add(n))}}return s}}(t)}computeDistance(t,e){if(this.useWasmDistance){if(!D())throw new Error("WASM distance requested via `useWasmDistance: true` but the wasm module is not initialized or available. Call `await wasmBridge.initWasm()` before using UMAP with wasm distances or build the wasm package.");return q(t,e)}return this.distanceFn(t,e)}makeSearchGraph(t){const e=this.knnIndices,r=this.knnDistances,s=[t.length,t.length],o=new M([],[],[],s);for(let t=0;t<e.length;t++){const s=e[t],n=r[t];for(let e=0;e<s.length;e++){const r=s[e],i=n[e];i>0&&o.set(t,r,i)}}return I(o,x(o),(t,e)=>t>e?t:e)}transform(t){const e=this.X;if(void 0===e||0===e.length)throw new Error("No data has been fit.");let r=Math.floor(this.nNeighbors*this.transformQueueSize);r=Math.min(e.length,r);const s=function(t,e,r,s,o,n,i){const h=f(r.length,s);if(o(s,e,r,h,i),t)for(let s of t)n(s,e,r,h,i);return h}(this.rpForest,e,t,r,this.initFromRandom,this.initFromTree,this.random),o=this.search(e,this.searchGraph,s,t);let{indices:n,weights:i}=d(o);n=n.map(t=>t.slice(0,this.nNeighbors)),i=i.map(t=>t.slice(0,this.nNeighbors));const a=Math.max(0,this.localConnectivity-1),{sigmas:l,rhos:u}=this.smoothKNNDistance(i,this.nNeighbors,a),{rows:m,cols:g,vals:w}=this.computeMembershipStrengths(n,i,l,u),p=[t.length,e.length];let y=new M(m,g,w,p);const b=z(R(y,"l1")),x=t.length,S=function(t,e,r){const s=h(t.length).map(t=>h(r[0].length));for(let o=0;o<t.length;o++)for(let n=0;n<t[0].length;n++)for(let i=0;i<r[0].length;i++){const h=t[o][n];s[o][i]+=e[o][n]*r[h][i]}return s}(c(b.indices,x,this.nNeighbors),c(b.values,x,this.nNeighbors),this.embedding),E=this.nEpochs?this.nEpochs/3:y.nRows<=1e4?100:30,v=y.getValues().reduce((t,e)=>e>t?e:t,0);y=y.map(t=>t<v/E?0:t),y=N(y);const A=this.makeEpochsPerSample(y.getValues(),E),k=y.getRows(),I=y.getCols();return this.assignOptimizationStateParameters({headEmbedding:S,tailEmbedding:this.embedding,head:k,tail:I,currentEpoch:0,nEpochs:E,nVertices:y.getDims()[1],epochsPerSample:A}),this.prepareForOptimizationLoop(),this.optimizeLayout()}processGraphForSupervisedProjection(){const{Y:t,X:e}=this;if(t){if(t.length!==e.length)throw new Error("Length of X and y must be equal");if("categorical"===this.targetMetric){const e=this.targetWeight<1?1/(1-this.targetWeight)*2.5:1e12;this.graph=this.categoricalSimplicialSetIntersection(this.graph,t,e)}}}step(){const{currentEpoch:t}=this.optimizationState;return t<this.getNEpochs()&&this.optimizeLayoutStep(t),this.optimizationState.currentEpoch}getEmbedding(){return this.embedding}nearestNeighbors(t){const{distanceFn:r,nNeighbors:s}=this,o=function(t,r,s=!1){return function(o,n,i,h=10,a=50,l=.001,u=.5,c=!0){if(s){if(!D())throw new Error("WASM NN-Descent requested but WASM module is not available. Initialize WASM with initWasm() first.");const e=function(t,e,r,s=10,o=50,n=.001,i=.5,h=!0,a="euclidean",l=42){if(!C)throw new Error("WASM module not initialized");const u=t.length,c=t[0].length,f=new Float64Array(u*c);for(let e=0;e<u;e++)for(let r=0;r<c;r++)f[e*c+r]=t[e][r];const m=e.length,g=m>0?e[0].length:0,w=new Int32Array(m*g);for(let t=0;t<m;t++)for(let r=0;r<g;r++)w[t*g+r]=e[t][r];const p=C.nn_descent(f,u,c,w,m,g,r,s,o,n,i,h,a,BigInt(l)),d=[],y=[],b=[],M=u*r,x=2*u*r;for(let t=0;t<u;t++){const e=[],s=[],o=[];for(let n=0;n<r;n++)e.push(p[t*r+n]),s.push(p[M+t*r+n]),o.push(p[x+t*r+n]);d.push(e),y.push(s),b.push(o)}return[d,y,b]}(o,n,i,h,a,l,u,c,"cosine"===t.name?"cosine":"euclidean",Math.floor(4294967295*r()));return{indices:e[0],weights:e[1]}}const w=o.length,y=f(o.length,i);for(let e=0;e<o.length;e++){const s=m(i,o.length,r);for(let r=0;r<s.length;r++){const n=t(o[e],o[s[r]]);g(y,e,n,s[r],1),g(y,s[r],n,e,1)}}if(c)for(let e=0;e<n.length;e++)for(let r=0;r<n[e].length&&!(n[e][r]<0);r++)for(let s=r+1;s<n[e].length&&!(n[e][s]<0);s++){const i=t(o[n[e][r]],o[n[e][s]]);g(y,n[e][r],i,n[e][s],1),g(y,n[e][s],i,n[e][r],1)}for(let s=0;s<h;s++){const s=p(y,w,i,a,r);let n=0;for(let i=0;i<w;i++)for(let h=0;h<a;h++){let l=Math.floor(s[0][i][h]);if(!(l<0||e(r)<u))for(let e=0;e<a;e++){const r=Math.floor(s[0][i][e]),a=s[2][i][h],u=s[2][i][e];if(r<0||!a&&!u)continue;const c=t(o[l],o[r]);n+=g(y,l,c,r,1),n+=g(y,r,c,l,1)}}if(n<=l*i*o.length)break}return d(y)}}(r,this.random,this.useWasmNNDescent),n=5+Math.floor(.5==(i=t.length**.5/20)?0:Math.round(i));var i;const h=Math.max(5,Math.floor(Math.round((t=>Math.log(t)/Math.log(2))(t.length))));this.rpForest=B(t,s,n,this.random,this.useWasmTree);const a=function(t){if(t.length>0){const e=[];for(let r of t)e.push(...r.indices);return e}return[[-1]]}(this.rpForest),{indices:l,weights:u}=o(t,a,s,h);return{knnIndices:l,knnDistances:u}}fuzzySimplicialSet(t,e,r=1){const{knnIndices:s=[],knnDistances:o=[],localConnectivity:n}=this,{sigmas:i,rhos:h}=this.smoothKNNDistance(o,e,n),{rows:a,cols:l,vals:u}=this.computeMembershipStrengths(s,o,i,h),c=[t.length,t.length];if(this.useWasmMatrix&&D()){const t=P(a,l,u,c[0],c[1]),e=W(t),s=O(t,e),o=j(t,e),n=_(j($(L(o,s),r),$(s,1-r))),i=n.map(t=>t.row),h=n.map(t=>t.col),f=n.map(t=>t.value);return new M(i,h,f,c)}if(this.useWasmMatrix&&D()){const t=P(a,l,u,c[0],c[1]),e=W(t),s=O(t,e),o=j(t,e),n=_(j($(L(o,s),r),$(s,1-r))),i=n.map(t=>t.row),h=n.map(t=>t.col),f=n.map(t=>t.value);return new M(i,h,f,c)}const f=new M(a,l,u,c),m=x(f),g=S(f,m),w=v(E(f,m),g);return E(A(w,r),A(g,1-r))}categoricalSimplicialSetIntersection(t,e,r,s=1){let o=function(t,e,r=1,s=5){return t.map((t,o,n)=>-1===e[o]||-1===e[n]?t*Math.exp(-r):e[o]!==e[n]?t*Math.exp(-s):t)}(t,e,s,r);return o=N(o),function(t){const e=x(t=R(t,"max"));return N(t=E(t,v(e,S(e,t))))}(o)}smoothKNNDistance(t,e,r=1,s=64,o=1){const n=Math.log(e)/Math.log(2)*o,i=h(t.length),u=h(t.length);for(let e=0;e<t.length;e++){let o=0,h=1/0,c=1;const f=t[e],m=f.filter(t=>t>0);if(m.length>=r){let t=Math.floor(r),s=r-t;t>0?(i[e]=m[t-1],s>nt&&(i[e]+=s*(m[t]-m[t-1]))):i[e]=s*m[0]}else m.length>0&&(i[e]=l(m));for(let r=0;r<s;r++){let r=0;for(let s=1;s<t[e].length;s++){const o=t[e][s]-i[e];r+=o>0?Math.exp(-o/c):1}if(Math.abs(r-n)<nt)break;r>n?(h=c,c=(o+h)/2):(o=c,h===1/0?c*=2:c=(o+h)/2)}if(u[e]=c,i[e]>0){const t=a(f);u[e]<it*t&&(u[e]=it*t)}else{const r=a(t.map(a));u[e]<it*r&&(u[e]=it*r)}}return{sigmas:u,rhos:i}}computeMembershipStrengths(t,e,r,s){const o=t.length,n=t[0].length,i=h(o*n),a=h(o*n),l=h(o*n);for(let h=0;h<o;h++)for(let o=0;o<n;o++){let u=0;-1!==t[h][o]&&(u=t[h][o]===h?0:e[h][o]-s[h]<=0?1:Math.exp(-(e[h][o]-s[h])/r[h]),i[h*n+o]=h,a[h*n+o]=t[h][o],l[h*n+o]=u)}return{rows:i,cols:a,vals:l}}initializeSimplicialSetEmbedding(){const t=this.getNEpochs(),{nComponents:r}=this,s=this.graph.getValues();let o=0;for(let t=0;t<s.length;t++){const e=s[t];o<s[t]&&(o=e)}const n=this.graph.map(e=>e<o/t?0:e);this.embedding=h(n.nRows).map(()=>h(r).map(()=>20*e(this.random)-10));const i=[],a=[],l=[],u=n.getAll();for(let t=0;t<u.length;t++){const e=u[t];e.value&&(i.push(e.value),l.push(e.row),a.push(e.col))}return{head:a,tail:l,epochsPerSample:this.makeEpochsPerSample(i,t)}}makeEpochsPerSample(t,e){const r=i(t.length,-1),s=l(t),o=t.map(t=>t/s*e);return o.forEach((t,s)=>{t>0&&(r[s]=e/o[s])}),r}assignOptimizationStateParameters(t){Object.assign(this.optimizationState,t)}prepareForOptimizationLoop(){const{repulsionStrength:t,learningRate:e,negativeSampleRate:r}=this,{epochsPerSample:s,headEmbedding:o,tailEmbedding:n}=this.optimizationState,i=o[0].length,h=o.length===n.length,a=s.map(t=>t/r),l=[...a],u=[...s];this.assignOptimizationStateParameters({epochOfNextSample:u,epochOfNextNegativeSample:l,epochsPerNegativeSample:a,moveOther:h,initialAlpha:e,alpha:e,gamma:t,dim:i})}initializeOptimization(){const t=this.embedding,e=this.embedding,{head:r,tail:s,epochsPerSample:n}=this.optimizationState,i=this.getNEpochs(),a=this.graph.nCols,{a:l,b:u}=function(t,e){const r=function(t,e){return o(300).map((t,r)=>0+r*((e-0)/299))}(0,3*t).map(t=>t<e?1:t),s=h(r.length).map((s,o)=>r[o]>=e?Math.exp(-(r[o]-e)/t):s),n={x:r,y:s},{parameterValues:i}=function(t,e,r={}){let{maxIterations:s=100,gradientDifference:o=.1,damping:n=0,errorTolerance:i=.01,minValues:h,maxValues:a,initialValues:l}=r;if(n<=0)throw new Error("The damping option must be a positive number");if(!t.x||!t.y)throw new Error("The data parameter must have x and y elements");if(!Z(t.x)||t.x.length<2||!Z(t.y)||t.y.length<2)throw new Error("The data parameter elements must be an array with more than 2 points");if(t.x.length!==t.y.length)throw new Error("The data parameter elements must have the same size");let u=l||new Array(e.length).fill(1),c=u.length;if(a=a||new Array(c).fill(Number.MAX_SAFE_INTEGER),h=h||new Array(c).fill(Number.MIN_SAFE_INTEGER),a.length!==h.length)throw new Error("minValues and maxValues must be the same size");if(!Z(u))throw new Error("initialValues must be an array");let f,m=tt(t,u,e),g=m<=i;for(f=0;f<s&&!g;f++){u=ot(t,u,n,o,e);for(let t=0;t<c;t++)u[t]=Math.min(Math.max(h[t],u[t]),a[t]);if(m=tt(t,u,e),isNaN(m))break;g=m<=i}return{parameterValues:u,parameterError:m,iterations:f}}(n,([t,e])=>r=>1/(1+t*r**(2*e)),{damping:1.5,initialValues:[.5,.5],gradientDifference:.1,maxIterations:100,errorTolerance:.01}),[a,l]=i;return{a,b:l}}(this.spread,this.minDist);this.assignOptimizationStateParameters({headEmbedding:t,tailEmbedding:e,head:r,tail:s,epochsPerSample:n,a:l,b:u,nEpochs:i,nVertices:a})}optimizeLayoutStep(e){const{optimizationState:r}=this,{head:s,tail:o,headEmbedding:n,tailEmbedding:i,epochsPerSample:h,epochOfNextSample:a,epochOfNextNegativeSample:l,epochsPerNegativeSample:u,moveOther:c,initialAlpha:f,alpha:m,gamma:g,a:w,b:p,dim:d,nEpochs:y,nVertices:b}=r;for(let r=0;r<h.length;r++){if(a[r]>e)continue;const f=s[r],y=o[r],M=n[f],x=i[y],S=ct(M,x);let E=0;S>0&&(E=-2*w*p*Math.pow(S,p-1),E/=w*Math.pow(S,p)+1);for(let t=0;t<d;t++){const e=ut(E*(M[t]-x[t]),4);M[t]+=e*m,c&&(x[t]+=-e*m)}a[r]+=h[r];const v=Math.floor((e-l[r])/u[r]);for(let e=0;e<v;e++){const e=t(b,this.random),r=i[e],s=ct(M,r);let o=0;if(s>0)o=2*g*p,o/=(.001+s)*(w*Math.pow(s,p)+1);else if(f===e)continue;for(let t=0;t<d;t++){let e=4;o>0&&(e=ut(o*(M[t]-r[t]),4)),M[t]+=e*m}}l[r]+=v*u[r]}return r.alpha=f*(1-e/y),r.currentEpoch+=1,n}optimizeLayoutAsync(t=()=>!0){return new Promise((e,r)=>{const s=async()=>{try{const{nEpochs:r,currentEpoch:o}=this.optimizationState;this.embedding=this.optimizeLayoutStep(o);const n=this.optimizationState.currentEpoch,i=!1===t(n),h=n===r;if(i||h)return e(h);setTimeout(()=>s(),0)}catch(t){r(t)}};setTimeout(()=>s(),0)})}optimizeLayout(t=()=>!0){let e=!1,r=[];for(;!e;){const{nEpochs:s,currentEpoch:o}=this.optimizationState;r=this.optimizeLayoutStep(o);const n=this.optimizationState.currentEpoch,i=!1===t(n);e=n===s||i}return r}getNEpochs(){const t=this.graph;if(this.nEpochs>0)return this.nEpochs;const e=t.nRows;return e<=2500?500:e<=5e3?400:e<=7500?300:200}}function at(t,e){let r=0;for(let s=0;s<t.length;s++)r+=(t[s]-e[s])**2;return Math.sqrt(r)}class lt{constructor(){this.currentEpoch=0,this.headEmbedding=[],this.tailEmbedding=[],this.head=[],this.tail=[],this.epochsPerSample=[],this.epochOfNextSample=[],this.epochOfNextNegativeSample=[],this.epochsPerNegativeSample=[],this.moveOther=!0,this.initialAlpha=1,this.alpha=1,this.gamma=1,this.a=1.5769434603113077,this.b=.8950608779109733,this.dim=2,this.nEpochs=500,this.nVertices=0}}function ut(t,e){return t>e?e:t<-e?-e:t}function ct(t,e){let r=0;for(let s=0;s<t.length;s++)r+=Math.pow(t[s]-e[s],2);return r}})(),s})());
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@elarsaks/umap-wasm",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "JavaScript implementation of UMAP",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Andy Coenen",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"preview": "npx serve . -p 4173",
|
|
37
37
|
"bundle": "rm -rf lib && webpack --config ./webpack/lib.config.mjs && webpack --config ./webpack/lib.min.config.mjs",
|
|
38
38
|
"build": "rm -rf dist && tsc && yarn bundle",
|
|
39
|
-
"build:wasm": "cd wasm && wasm-pack build --target web --out-dir pkg",
|
|
39
|
+
"build:wasm": "cd wasm && wasm-pack build --target web --out-dir pkg/web && wasm-pack build --target nodejs --out-dir pkg/node",
|
|
40
40
|
"prepublishOnly": "yarn build:wasm && yarn build"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|