@elarsaks/umap-wasm 0.4.2 → 0.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/umap.d.ts CHANGED
@@ -14,6 +14,7 @@ export interface UMAPParameters {
14
14
  useWasmDistance?: boolean;
15
15
  useWasmNNDescent?: boolean;
16
16
  useWasmOptimizer?: boolean;
17
+ wasmBatchSize?: number;
17
18
  learningRate?: number;
18
19
  localConnectivity?: number;
19
20
  minDist?: number;
@@ -54,6 +55,7 @@ export declare class UMAP {
54
55
  private useWasmMatrix;
55
56
  private useWasmOptimizer;
56
57
  private useWasmTree;
58
+ private wasmBatchSize;
57
59
  private knnIndices?;
58
60
  private knnDistances?;
59
61
  private graph;
@@ -95,6 +97,9 @@ export declare class UMAP {
95
97
  private optimizeLayoutStep;
96
98
  private optimizeLayoutAsync;
97
99
  private optimizeLayout;
100
+ private optimizeLayoutBatchWasm;
101
+ private materializeEmbeddingFromWasm;
102
+ private advanceRngState;
98
103
  private getNEpochs;
99
104
  }
100
105
  export declare function euclidean(x: Vector, y: Vector): number;
package/dist/umap.js CHANGED
@@ -30,6 +30,7 @@ export class UMAP {
30
30
  this.useWasmMatrix = false;
31
31
  this.useWasmOptimizer = false;
32
32
  this.useWasmTree = false;
33
+ this.wasmBatchSize = 10;
33
34
  this.isInitialized = false;
34
35
  this.rpForest = [];
35
36
  this.embedding = [];
@@ -46,6 +47,7 @@ export class UMAP {
46
47
  setParam('useWasmMatrix');
47
48
  setParam('useWasmTree');
48
49
  setParam('useWasmOptimizer');
50
+ setParam('wasmBatchSize');
49
51
  setParam('learningRate');
50
52
  setParam('localConnectivity');
51
53
  setParam('minDist');
@@ -220,6 +222,9 @@ export class UMAP {
220
222
  return this.optimizationState.currentEpoch;
221
223
  }
222
224
  getEmbedding() {
225
+ if (this.useWasmOptimizer && this.wasmOptimizerState) {
226
+ return this.materializeEmbeddingFromWasm();
227
+ }
223
228
  return this.embedding;
224
229
  }
225
230
  nearestNeighbors(X) {
@@ -480,20 +485,10 @@ export class UMAP {
480
485
  }
481
486
  optimizeLayoutStep(n) {
482
487
  if (this.useWasmOptimizer && this.wasmOptimizerState) {
483
- const flatEmbedding = wasmBridge.optimizeLayoutStepWasm(this.wasmOptimizerState, this.rngState);
484
- const A = BigInt('6364136223846793005');
485
- const C = BigInt('1442695040888963407');
486
- this.rngState = (this.rngState * A + C) & BigInt('0xFFFFFFFFFFFFFFFF');
487
- const { dim } = this.optimizationState;
488
- const { headEmbedding } = this.optimizationState;
489
- const nPoints = headEmbedding.length;
490
- for (let i = 0; i < nPoints; i++) {
491
- for (let j = 0; j < dim; j++) {
492
- headEmbedding[i][j] = flatEmbedding[i * dim + j];
493
- }
494
- }
488
+ wasmBridge.optimizeLayoutStepInPlaceWasm(this.wasmOptimizerState, this.rngState);
489
+ this.advanceRngState(1);
495
490
  this.optimizationState.currentEpoch += 1;
496
- return headEmbedding;
491
+ return this.materializeEmbeddingFromWasm();
497
492
  }
498
493
  const { optimizationState } = this;
499
494
  const { head, tail, headEmbedding, tailEmbedding, epochsPerSample, epochOfNextSample, epochOfNextNegativeSample, epochsPerNegativeSample, moveOther, initialAlpha, alpha, gamma, a, b, dim, nEpochs, nVertices, } = optimizationState;
@@ -552,6 +547,34 @@ export class UMAP {
552
547
  return new Promise((resolve, reject) => {
553
548
  const step = async () => {
554
549
  try {
550
+ if (this.useWasmOptimizer && this.wasmOptimizerState) {
551
+ const { nEpochs, currentEpoch } = this.optimizationState;
552
+ if (currentEpoch >= nEpochs) {
553
+ this.embedding = this.materializeEmbeddingFromWasm();
554
+ return resolve(true);
555
+ }
556
+ const batchSize = Math.max(1, this.wasmBatchSize);
557
+ const steps = Math.min(batchSize, nEpochs - currentEpoch);
558
+ const startEpoch = currentEpoch;
559
+ const advanced = this.optimizeLayoutBatchWasm(steps);
560
+ let shouldStop = false;
561
+ for (let e = startEpoch + 1; e <= startEpoch + advanced; e++) {
562
+ if (epochCallback(e) === false) {
563
+ shouldStop = true;
564
+ break;
565
+ }
566
+ }
567
+ const epochCompleted = this.optimizationState.currentEpoch;
568
+ const isFinished = epochCompleted === nEpochs;
569
+ if (!shouldStop && !isFinished) {
570
+ setTimeout(() => step(), 0);
571
+ }
572
+ else {
573
+ this.embedding = this.materializeEmbeddingFromWasm();
574
+ return resolve(isFinished);
575
+ }
576
+ return;
577
+ }
555
578
  const { nEpochs, currentEpoch } = this.optimizationState;
556
579
  this.embedding = this.optimizeLayoutStep(currentEpoch);
557
580
  const epochCompleted = this.optimizationState.currentEpoch;
@@ -574,6 +597,26 @@ export class UMAP {
574
597
  optimizeLayout(epochCallback = () => true) {
575
598
  let isFinished = false;
576
599
  let embedding = [];
600
+ if (this.useWasmOptimizer && this.wasmOptimizerState) {
601
+ while (!isFinished) {
602
+ const { nEpochs, currentEpoch } = this.optimizationState;
603
+ const batchSize = Math.max(1, this.wasmBatchSize);
604
+ const steps = Math.min(batchSize, nEpochs - currentEpoch);
605
+ const startEpoch = currentEpoch;
606
+ const advanced = this.optimizeLayoutBatchWasm(steps);
607
+ let shouldStop = false;
608
+ for (let e = startEpoch + 1; e <= startEpoch + advanced; e++) {
609
+ if (epochCallback(e) === false) {
610
+ shouldStop = true;
611
+ break;
612
+ }
613
+ }
614
+ const epochCompleted = this.optimizationState.currentEpoch;
615
+ isFinished = epochCompleted === nEpochs || shouldStop;
616
+ }
617
+ embedding = this.materializeEmbeddingFromWasm();
618
+ return embedding;
619
+ }
577
620
  while (!isFinished) {
578
621
  const { nEpochs, currentEpoch } = this.optimizationState;
579
622
  embedding = this.optimizeLayoutStep(currentEpoch);
@@ -583,6 +626,45 @@ export class UMAP {
583
626
  }
584
627
  return embedding;
585
628
  }
629
+ optimizeLayoutBatchWasm(steps) {
630
+ if (!this.wasmOptimizerState) {
631
+ throw new Error('WASM optimizer state is not initialized.');
632
+ }
633
+ const remaining = this.optimizationState.nEpochs - this.optimizationState.currentEpoch;
634
+ const actualSteps = Math.min(steps, remaining);
635
+ if (actualSteps <= 0) {
636
+ return 0;
637
+ }
638
+ wasmBridge.optimizeLayoutBatchInPlaceWasm(this.wasmOptimizerState, this.rngState, actualSteps);
639
+ this.advanceRngState(actualSteps);
640
+ this.optimizationState.currentEpoch += actualSteps;
641
+ return actualSteps;
642
+ }
643
+ materializeEmbeddingFromWasm() {
644
+ if (!this.wasmOptimizerState) {
645
+ return this.embedding;
646
+ }
647
+ const { dim, nVertices } = this.optimizationState;
648
+ const flat = wasmBridge.getOptimizerEmbeddingView(this.wasmOptimizerState);
649
+ const embedding = new Array(nVertices);
650
+ for (let i = 0; i < nVertices; i++) {
651
+ const row = new Array(dim);
652
+ const base = i * dim;
653
+ for (let j = 0; j < dim; j++) {
654
+ row[j] = flat[base + j];
655
+ }
656
+ embedding[i] = row;
657
+ }
658
+ this.embedding = embedding;
659
+ return embedding;
660
+ }
661
+ advanceRngState(steps) {
662
+ const A = BigInt('6364136223846793005');
663
+ const C = BigInt('1442695040888963407');
664
+ for (let i = 0; i < steps; i++) {
665
+ this.rngState = (this.rngState * A + C) & BigInt('0xFFFFFFFFFFFFFFFF');
666
+ }
667
+ }
586
668
  getNEpochs() {
587
669
  const graph = this.graph;
588
670
  if (this.nEpochs > 0) {
@@ -60,8 +60,13 @@ export interface WasmOptimizerState {
60
60
  head_embedding: Float64Array;
61
61
  current_epoch: number;
62
62
  n_epochs: number;
63
+ head_embedding_ptr(): number;
64
+ head_embedding_len(): number;
63
65
  free(): void;
64
66
  }
65
67
  export declare function createOptimizerState(head: number[], tail: number[], headEmbedding: number[][], tailEmbedding: number[][], epochsPerSample: number[], epochsPerNegativeSample: number[], moveOther: boolean, initialAlpha: number, gamma: number, a: number, b: number, dim: number, nEpochs: number, nVertices: number): WasmOptimizerState;
66
68
  export declare function optimizeLayoutStepWasm(state: WasmOptimizerState, rngSeed: bigint): Float64Array;
69
+ export declare function optimizeLayoutStepInPlaceWasm(state: WasmOptimizerState, rngSeed: bigint): void;
67
70
  export declare function optimizeLayoutBatchWasm(state: WasmOptimizerState, rngSeed: bigint, nSteps: number): Float64Array;
71
+ export declare function optimizeLayoutBatchInPlaceWasm(state: WasmOptimizerState, rngSeed: bigint, nSteps: number): void;
72
+ export declare function getOptimizerEmbeddingView(state: WasmOptimizerState): Float64Array;
@@ -1,5 +1,6 @@
1
1
  let wasmReady = null;
2
2
  let wasmModule = null;
3
+ let wasmExports = null;
3
4
  export async function initWasm() {
4
5
  if (wasmReady)
5
6
  return wasmReady;
@@ -23,7 +24,7 @@ export async function initWasm() {
23
24
  }
24
25
  }
25
26
  if (typeof mod.default === 'function') {
26
- await mod.default();
27
+ wasmExports = await mod.default();
27
28
  }
28
29
  wasmModule = mod;
29
30
  return mod;
@@ -31,6 +32,7 @@ export async function initWasm() {
31
32
  catch (err) {
32
33
  wasmReady = null;
33
34
  wasmModule = null;
35
+ wasmExports = null;
34
36
  throw new Error(`Failed to load WASM module: ${err}`);
35
37
  }
36
38
  })();
@@ -264,8 +266,31 @@ export function optimizeLayoutStepWasm(state, rngSeed) {
264
266
  throw new Error('WASM module not initialized');
265
267
  return wasmModule.optimize_layout_step(state, rngSeed);
266
268
  }
269
+ export function optimizeLayoutStepInPlaceWasm(state, rngSeed) {
270
+ if (!wasmModule)
271
+ throw new Error('WASM module not initialized');
272
+ wasmModule.optimize_layout_step_in_place(state, rngSeed);
273
+ }
267
274
  export function optimizeLayoutBatchWasm(state, rngSeed, nSteps) {
268
275
  if (!wasmModule)
269
276
  throw new Error('WASM module not initialized');
270
277
  return wasmModule.optimize_layout_batch(state, rngSeed, nSteps);
271
278
  }
279
+ export function optimizeLayoutBatchInPlaceWasm(state, rngSeed, nSteps) {
280
+ if (!wasmModule)
281
+ throw new Error('WASM module not initialized');
282
+ wasmModule.optimize_layout_batch_in_place(state, rngSeed, nSteps);
283
+ }
284
+ export function getOptimizerEmbeddingView(state) {
285
+ if (!wasmModule)
286
+ throw new Error('WASM module not initialized');
287
+ const ptr = state.head_embedding_ptr();
288
+ const len = state.head_embedding_len();
289
+ const memory = wasmExports?.memory ??
290
+ wasmModule?.memory ??
291
+ wasmModule?.__wasm?.memory;
292
+ if (!memory?.buffer) {
293
+ throw new Error('WASM memory is not available');
294
+ }
295
+ return new Float64Array(memory.buffer, ptr, len);
296
+ }
package/lib/umap-js.js CHANGED
@@ -6474,6 +6474,7 @@ function getCSR(x) {
6474
6474
  ;// ./src/wasmBridge.ts
6475
6475
  let wasmReady = null;
6476
6476
  let wasmModule = null;
6477
+ let wasmExports = null;
6477
6478
  async function initWasm() {
6478
6479
  if (wasmReady)
6479
6480
  return wasmReady;
@@ -6497,7 +6498,7 @@ async function initWasm() {
6497
6498
  }
6498
6499
  }
6499
6500
  if (typeof mod.default === 'function') {
6500
- await mod.default();
6501
+ wasmExports = await mod.default();
6501
6502
  }
6502
6503
  wasmModule = mod;
6503
6504
  return mod;
@@ -6505,6 +6506,7 @@ async function initWasm() {
6505
6506
  catch (err) {
6506
6507
  wasmReady = null;
6507
6508
  wasmModule = null;
6509
+ wasmExports = null;
6508
6510
  throw new Error(`Failed to load WASM module: ${err}`);
6509
6511
  }
6510
6512
  })();
@@ -6738,11 +6740,34 @@ function optimizeLayoutStepWasm(state, rngSeed) {
6738
6740
  throw new Error('WASM module not initialized');
6739
6741
  return wasmModule.optimize_layout_step(state, rngSeed);
6740
6742
  }
6743
+ function optimizeLayoutStepInPlaceWasm(state, rngSeed) {
6744
+ if (!wasmModule)
6745
+ throw new Error('WASM module not initialized');
6746
+ wasmModule.optimize_layout_step_in_place(state, rngSeed);
6747
+ }
6741
6748
  function optimizeLayoutBatchWasm(state, rngSeed, nSteps) {
6742
6749
  if (!wasmModule)
6743
6750
  throw new Error('WASM module not initialized');
6744
6751
  return wasmModule.optimize_layout_batch(state, rngSeed, nSteps);
6745
6752
  }
6753
+ function optimizeLayoutBatchInPlaceWasm(state, rngSeed, nSteps) {
6754
+ if (!wasmModule)
6755
+ throw new Error('WASM module not initialized');
6756
+ wasmModule.optimize_layout_batch_in_place(state, rngSeed, nSteps);
6757
+ }
6758
+ function getOptimizerEmbeddingView(state) {
6759
+ if (!wasmModule)
6760
+ throw new Error('WASM module not initialized');
6761
+ const ptr = state.head_embedding_ptr();
6762
+ const len = state.head_embedding_len();
6763
+ const memory = wasmExports?.memory ??
6764
+ wasmModule?.memory ??
6765
+ wasmModule?.__wasm?.memory;
6766
+ if (!memory?.buffer) {
6767
+ throw new Error('WASM memory is not available');
6768
+ }
6769
+ return new Float64Array(memory.buffer, ptr, len);
6770
+ }
6746
6771
 
6747
6772
  ;// ./src/tree.ts
6748
6773
 
@@ -7426,6 +7451,7 @@ class UMAP {
7426
7451
  this.useWasmMatrix = false;
7427
7452
  this.useWasmOptimizer = false;
7428
7453
  this.useWasmTree = false;
7454
+ this.wasmBatchSize = 10;
7429
7455
  this.isInitialized = false;
7430
7456
  this.rpForest = [];
7431
7457
  this.embedding = [];
@@ -7442,6 +7468,7 @@ class UMAP {
7442
7468
  setParam('useWasmMatrix');
7443
7469
  setParam('useWasmTree');
7444
7470
  setParam('useWasmOptimizer');
7471
+ setParam('wasmBatchSize');
7445
7472
  setParam('learningRate');
7446
7473
  setParam('localConnectivity');
7447
7474
  setParam('minDist');
@@ -7616,6 +7643,9 @@ class UMAP {
7616
7643
  return this.optimizationState.currentEpoch;
7617
7644
  }
7618
7645
  getEmbedding() {
7646
+ if (this.useWasmOptimizer && this.wasmOptimizerState) {
7647
+ return this.materializeEmbeddingFromWasm();
7648
+ }
7619
7649
  return this.embedding;
7620
7650
  }
7621
7651
  nearestNeighbors(X) {
@@ -7876,20 +7906,10 @@ class UMAP {
7876
7906
  }
7877
7907
  optimizeLayoutStep(n) {
7878
7908
  if (this.useWasmOptimizer && this.wasmOptimizerState) {
7879
- const flatEmbedding = optimizeLayoutStepWasm(this.wasmOptimizerState, this.rngState);
7880
- const A = BigInt('6364136223846793005');
7881
- const C = BigInt('1442695040888963407');
7882
- this.rngState = (this.rngState * A + C) & BigInt('0xFFFFFFFFFFFFFFFF');
7883
- const { dim } = this.optimizationState;
7884
- const { headEmbedding } = this.optimizationState;
7885
- const nPoints = headEmbedding.length;
7886
- for (let i = 0; i < nPoints; i++) {
7887
- for (let j = 0; j < dim; j++) {
7888
- headEmbedding[i][j] = flatEmbedding[i * dim + j];
7889
- }
7890
- }
7909
+ optimizeLayoutStepInPlaceWasm(this.wasmOptimizerState, this.rngState);
7910
+ this.advanceRngState(1);
7891
7911
  this.optimizationState.currentEpoch += 1;
7892
- return headEmbedding;
7912
+ return this.materializeEmbeddingFromWasm();
7893
7913
  }
7894
7914
  const { optimizationState } = this;
7895
7915
  const { head, tail, headEmbedding, tailEmbedding, epochsPerSample, epochOfNextSample, epochOfNextNegativeSample, epochsPerNegativeSample, moveOther, initialAlpha, alpha, gamma, a, b, dim, nEpochs, nVertices, } = optimizationState;
@@ -7948,6 +7968,34 @@ class UMAP {
7948
7968
  return new Promise((resolve, reject) => {
7949
7969
  const step = async () => {
7950
7970
  try {
7971
+ if (this.useWasmOptimizer && this.wasmOptimizerState) {
7972
+ const { nEpochs, currentEpoch } = this.optimizationState;
7973
+ if (currentEpoch >= nEpochs) {
7974
+ this.embedding = this.materializeEmbeddingFromWasm();
7975
+ return resolve(true);
7976
+ }
7977
+ const batchSize = Math.max(1, this.wasmBatchSize);
7978
+ const steps = Math.min(batchSize, nEpochs - currentEpoch);
7979
+ const startEpoch = currentEpoch;
7980
+ const advanced = this.optimizeLayoutBatchWasm(steps);
7981
+ let shouldStop = false;
7982
+ for (let e = startEpoch + 1; e <= startEpoch + advanced; e++) {
7983
+ if (epochCallback(e) === false) {
7984
+ shouldStop = true;
7985
+ break;
7986
+ }
7987
+ }
7988
+ const epochCompleted = this.optimizationState.currentEpoch;
7989
+ const isFinished = epochCompleted === nEpochs;
7990
+ if (!shouldStop && !isFinished) {
7991
+ setTimeout(() => step(), 0);
7992
+ }
7993
+ else {
7994
+ this.embedding = this.materializeEmbeddingFromWasm();
7995
+ return resolve(isFinished);
7996
+ }
7997
+ return;
7998
+ }
7951
7999
  const { nEpochs, currentEpoch } = this.optimizationState;
7952
8000
  this.embedding = this.optimizeLayoutStep(currentEpoch);
7953
8001
  const epochCompleted = this.optimizationState.currentEpoch;
@@ -7970,6 +8018,26 @@ class UMAP {
7970
8018
  optimizeLayout(epochCallback = () => true) {
7971
8019
  let isFinished = false;
7972
8020
  let embedding = [];
8021
+ if (this.useWasmOptimizer && this.wasmOptimizerState) {
8022
+ while (!isFinished) {
8023
+ const { nEpochs, currentEpoch } = this.optimizationState;
8024
+ const batchSize = Math.max(1, this.wasmBatchSize);
8025
+ const steps = Math.min(batchSize, nEpochs - currentEpoch);
8026
+ const startEpoch = currentEpoch;
8027
+ const advanced = this.optimizeLayoutBatchWasm(steps);
8028
+ let shouldStop = false;
8029
+ for (let e = startEpoch + 1; e <= startEpoch + advanced; e++) {
8030
+ if (epochCallback(e) === false) {
8031
+ shouldStop = true;
8032
+ break;
8033
+ }
8034
+ }
8035
+ const epochCompleted = this.optimizationState.currentEpoch;
8036
+ isFinished = epochCompleted === nEpochs || shouldStop;
8037
+ }
8038
+ embedding = this.materializeEmbeddingFromWasm();
8039
+ return embedding;
8040
+ }
7973
8041
  while (!isFinished) {
7974
8042
  const { nEpochs, currentEpoch } = this.optimizationState;
7975
8043
  embedding = this.optimizeLayoutStep(currentEpoch);
@@ -7979,6 +8047,45 @@ class UMAP {
7979
8047
  }
7980
8048
  return embedding;
7981
8049
  }
8050
+ optimizeLayoutBatchWasm(steps) {
8051
+ if (!this.wasmOptimizerState) {
8052
+ throw new Error('WASM optimizer state is not initialized.');
8053
+ }
8054
+ const remaining = this.optimizationState.nEpochs - this.optimizationState.currentEpoch;
8055
+ const actualSteps = Math.min(steps, remaining);
8056
+ if (actualSteps <= 0) {
8057
+ return 0;
8058
+ }
8059
+ optimizeLayoutBatchInPlaceWasm(this.wasmOptimizerState, this.rngState, actualSteps);
8060
+ this.advanceRngState(actualSteps);
8061
+ this.optimizationState.currentEpoch += actualSteps;
8062
+ return actualSteps;
8063
+ }
8064
+ materializeEmbeddingFromWasm() {
8065
+ if (!this.wasmOptimizerState) {
8066
+ return this.embedding;
8067
+ }
8068
+ const { dim, nVertices } = this.optimizationState;
8069
+ const flat = getOptimizerEmbeddingView(this.wasmOptimizerState);
8070
+ const embedding = new Array(nVertices);
8071
+ for (let i = 0; i < nVertices; i++) {
8072
+ const row = new Array(dim);
8073
+ const base = i * dim;
8074
+ for (let j = 0; j < dim; j++) {
8075
+ row[j] = flat[base + j];
8076
+ }
8077
+ embedding[i] = row;
8078
+ }
8079
+ this.embedding = embedding;
8080
+ return embedding;
8081
+ }
8082
+ advanceRngState(steps) {
8083
+ const A = BigInt('6364136223846793005');
8084
+ const C = BigInt('1442695040888963407');
8085
+ for (let i = 0; i < steps; i++) {
8086
+ this.rngState = (this.rngState * A + C) & BigInt('0xFFFFFFFFFFFFFFFF');
8087
+ }
8088
+ }
7982
8089
  getNEpochs() {
7983
8090
  const graph = this.graph;
7984
8091
  if (this.nEpochs > 0) {
@@ -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={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),z=(o-u)*(w-y),I=o*d,F=(l+u)*(-d+y),T=(-o+i+h)*(p+d-b),C=(o-h)*(p-b),D=(i+h)*(-d+b),V=v+I+s*g,q=(r+s+o-n-i-l-u)*w+x+S+v+k+I+F,W=v+A+R+(r+s+o-i-h-a-l)*p+I+T+D,P=M+i*(-c+f+g-w-p-d+b)+x+v+I+T+C,O=M+x+S+v+h*y,j=I+T+C+D+n*m,L=v+A+N+l*(-c+m+g-w-p-d+y)+k+z+I,_=k+z+I+F+a*f,$=v+A+N+R+u*b;return e.set(0,0,V),e.set(0,1,q),e.set(0,2,W),e.set(1,0,P),e.set(1,1,O),e.set(1,2,j),e.set(2,0,L),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 z 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 I{constructor(t){let e,r,s,o,n,i,h,a,l,u=(t=z.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 F(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 T{constructor(t){let e,r,s,o,n=(t=z.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=F(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 C{constructor(t,e={}){if((t=z.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]=F(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]=F(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=F(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=F(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=F(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=F(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=z.checkMatrix(t),e=z.checkMatrix(e),r?new C(t).solve(e):t.isSquare()?new I(t).solve(e):new T(t).solve(e)}function V(t,e){let r=[];for(let s=0;s<t;s++)s!==e&&r.push(s);return r}function q(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 W{constructor(t,e={}){const{assumeSymmetric:r=!1}=e;if(!(t=z.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=F(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=F(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,z=0,I=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--,I=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,z=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(z=A>=0?A+z:A-z,r[M-1]=f+z,r[M]=r[M-1],0!==z&&(r[M]=f-c/z),e[M-1]=0,e[M]=0,f=o.get(M,M-1),k=Math.abs(f)+Math.abs(z),A=f/k,N=z/k,R=Math.sqrt(A*A+N*N),A/=R,N/=R,i=M-1;i<t;i++)z=o.get(M-1,i),o.set(M-1,i,N*z+A*o.get(M,i)),o.set(M,i,N*o.get(M,i)-A*z);for(n=0;n<=M;n++)z=o.get(n,M-1),o.set(n,M-1,N*z+A*o.get(n,M)),o.set(n,M,N*o.get(n,M)-A*z);for(n=0;n<=x;n++)z=s.get(n,M-1),s.set(n,M-1,N*z+A*s.get(n,M)),s.set(n,M,N*s.get(n,M)-A*z)}else r[M-1]=f+A,r[M]=f+A,e[M-1]=z,e[M]=-z;M-=2,I=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===I){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===I&&(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(I+=1,l=M-2;l>=a&&(z=o.get(l,l),R=f-z,k=m-z,A=(R*k-c)/o.get(l+1,l)+o.get(l,l+1),N=o.get(l+1,l+1)-z-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(z)+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,z=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*z)),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+=z*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+=z*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)z=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-z*R)/N,o.set(n,M,u),o.set(n+1,M,Math.abs(f)>Math.abs(z)?(-R-c*u)/f:(-k-m*u)/z)),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=P(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)z=c,R=g,k=w;else if(a=n,0===e[n]?(b=P(-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(z))),b=P(f*R-z*g+N*w,f*k-z*w-N*g,p,d),o.set(n,M-1,b[0]),o.set(n,M,b[1]),Math.abs(f)>Math.abs(z)+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=P(-R-m*o.get(n,M-1),-k-m*o.get(n,M),z,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(z=0,h=0;h<=Math.min(i,x);h++)z+=s.get(n,h)*o.get(h,i);s.set(n,i,z)}}}(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 P(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 O{constructor(t){if(!(t=z.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=z.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 j{constructor(t,e={}){t=z.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):z.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=O,e.oN=O,e.Hc=A,e.cg=W,e.hj=W,e.LU=I,e.Tb=I,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=j,e.ks=j,e.QR=T,e.jp=T,e.mk=C,e.W2=C,e.l=v,e.KY=k,e.dv=z,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 I(e).determinant}throw Error("determinant can only be calculated for a square matrix")},e.DI=function(t,e=!1){return t=z.checkMatrix(t),e?new C(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(V(o,e)).transpose(),a=new C(h).solve(i),l=E.sub(i,h.mmul(a)).abs().max();n.setRow(e,q(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 C(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 z(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:()=>C,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 z(t,e,(t,e)=>t*e)}function E(t,e){return z(t,e,(t,e)=>t+e)}function v(t,e){return z(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 z(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 I(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 F=null,T=null;async function C(){return F||(F=(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 try{const e=["..","wasm","pkg","web","umap_wasm_core.js"].join("/");t=await r(433)(e)}catch(e){const r=`${"undefined"!=typeof window&&window.location?window.location.origin:""}/wasm/pkg/web/umap_wasm_core.js`;t=await new Function("p","return import(p)")(r)}return"function"==typeof t.default&&await t.default(),T=t,t}catch(t){throw F=null,T=null,new Error(`Failed to load WASM module: ${t}`)}})(),F)}function D(){return null!==T}function V(t,e){if(!T)throw new Error("WASM module not initialized");const r=new Float64Array(t),s=new Float64Array(e);return T.euclidean(r,s)}function q(t,e,r,s,o){if(!T)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 T.build_rp_tree(n,e,r,s,BigInt(o))}function W(t,e,r,s,o){if(!T)throw new Error("WASM module not initialized");const n=new Int32Array(t),i=new Int32Array(e),h=new Float64Array(r);return new T.WasmSparseMatrix(n,i,h,s,o)}function P(t){if(!T)throw new Error("WASM module not initialized");return T.sparse_transpose(t)}function O(t,e){if(!T)throw new Error("WASM module not initialized");return T.sparse_add(t,e)}function j(t,e){if(!T)throw new Error("WASM module not initialized");return T.sparse_subtract(t,e)}function L(t,e){if(!T)throw new Error("WASM module not initialized");return T.sparse_pairwise_multiply(t,e)}function _(t,e){if(!T)throw new Error("WASM module not initialized");return T.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=q(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(!T)throw new Error("WASM module not initialized");const s=new Float64Array(e),o=T.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.useWasmOptimizer=!1,this.useWasmTree=!1,this.isInitialized=!1,this.rpForest=[],this.embedding=[],this.optimizationState=new lt,this.wasmOptimizerState=null,this.rngState=BigInt(Math.floor(4294967295*Math.random()));const e=e=>{void 0!==t[e]&&(this[e]=t[e])};e("distanceFn"),e("useWasmDistance"),e("useWasmNNDescent"),e("useWasmMatrix"),e("useWasmTree"),e("useWasmOptimizer"),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 V(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}=I(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 V(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 z(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=I(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(),z=y.getCols();return this.assignOptimizationStateParameters({headEmbedding:S,tailEmbedding:this.embedding,head:k,tail:z,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(!T)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=T.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=W(a,l,u,c[0],c[1]),e=P(t),s=L(t,e),o=O(t,e),n=$(O(_(j(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=W(a,l,u,c[0],c[1]),e=P(t),s=L(t,e),o=O(t,e),n=$(O(_(j(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];if(this.assignOptimizationStateParameters({epochOfNextSample:u,epochOfNextNegativeSample:l,epochsPerNegativeSample:a,moveOther:h,initialAlpha:e,alpha:e,gamma:t,dim:i}),this.useWasmOptimizer&&D()){const{head:r,tail:l,nEpochs:u,nVertices:c,a:f,b:m}=this.optimizationState;this.wasmOptimizerState=function(t,e,r,s,o,n,i,h,a,l,u,c,f,m){if(!T)throw new Error("WASM module not initialized");const g=new Float64Array(r.length*c),w=new Float64Array(s.length*c);for(let t=0;t<r.length;t++)for(let e=0;e<c;e++)g[t*c+e]=r[t][e];for(let t=0;t<s.length;t++)for(let e=0;e<c;e++)w[t*c+e]=s[t][e];const p=new Uint32Array(t),d=new Uint32Array(e),y=new Float64Array(o),b=new Float64Array(n);return new T.OptimizerState(p,d,g,w,y,b,i,h,a,l,u,c,f,m)}(r,l,o,n,s,a,h,e,t,f,m,i,u,c)}}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){if(this.useWasmOptimizer&&this.wasmOptimizerState){const t=function(t,e){if(!T)throw new Error("WASM module not initialized");return T.optimize_layout_step(t,e)}(this.wasmOptimizerState,this.rngState),e=BigInt("6364136223846793005"),r=BigInt("1442695040888963407");this.rngState=this.rngState*e+r&BigInt("0xFFFFFFFFFFFFFFFF");const{dim:s}=this.optimizationState,{headEmbedding:o}=this.optimizationState,n=o.length;for(let e=0;e<n;e++)for(let r=0;r<s;r++)o[e][r]=t[e*s+r];return this.optimizationState.currentEpoch+=1,o}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 i=" ".repeat(2),n=" ".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${i}[\n${n}${function(t,e,r,s,o){const{rows:i,columns:h}=t,l=Math.min(i,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 i=0;i<u;i++)r.push(a(t.get(e,i),s,o));c.push(`${r.join(" ")}`)}return u!==h&&(c[c.length-1]+=` ... ${h-r} more columns`),l!==i&&c.push(`... ${i-e} more rows`),c.join(`\n${n}`)}(t,r,s,o,h)}\n${i}]\n${i}rows: ${t.rows}\n${i}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:i=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 n=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(i()*n);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),i=this.zeros(e,r);for(let e=0;e<o;e++)i.set(e,e,t[e]);return i}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 i=0;i<r;i++)for(let r=0;r<s;r++)o.set(i,r,Math.min(t.get(i,r),e.get(i,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 i=0;i<r;i++)for(let r=0;r<s;r++)o.set(i,r,Math.max(t.get(i,r),e.get(i,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 i=r+1;i<t.columns;i++)t.set(s,i,t.get(s,i)-t.get(e,i)*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,i=!1;for(;o<r&&!1===i;)1===t.get(s,o)?i=!0:o++;for(let r=0;r<s;r++){let i=t.get(r,o);for(let n=o;n<e;n++){let e=t.get(r,n)-i*t.get(s,n);t.set(r,n,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),i=new Float64Array(r);for(let n=0;n<s;n++){for(let e=0;e<r;e++)i[e]=t.get(e,n);for(let t=0;t<e;t++){let e=0;for(let s=0;s<r;s++)e+=this.get(t,s)*i[s];o.set(t,n,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),i=t.get(0,1),n=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=(n+a)*s,f=r*(i-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+(n-r)*(s+i);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),i=this.get(1,0),n=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-i)*(-f+w),x=(-r+i+n)*(c-f+w),S=(i+n)*(-c+f),v=r*c,A=(-r+a+l)*(c-m+p),z=(-r+a)*(m-p),R=(a+l)*(-c+m),N=(-o+l+u)*(w+d-y),k=(o-u)*(w-y),I=o*d,F=(l+u)*(-d+y),T=(-o+n+h)*(p+d-b),C=(o-h)*(p-b),W=(n+h)*(-d+b),D=v+I+s*g,V=(r+s+o-i-n-l-u)*w+x+S+v+N+I+F,q=v+A+R+(r+s+o-n-h-a-l)*p+I+T+W,O=M+n*(-c+f+g-w-p-d+b)+x+v+I+T+C,P=M+x+S+v+h*y,_=I+T+C+W+i*m,j=v+A+z+l*(-c+m+g-w-p-d+y)+N+k+I,L=N+k+I+F+a*f,$=v+A+z+R+u*b;return e.set(0,0,D),e.set(0,1,V),e.set(0,2,q),e.set(1,0,O),e.set(1,1,P),e.set(1,2,_),e.set(2,0,j),e.set(2,1,L),e.set(2,2,$),e}mmulStrassen(t){t=E.checkMatrix(t);let e=this.clone(),r=e.rows,s=e.columns,o=t.rows,i=t.columns;function n(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 ${i} matrix: dimensions do not match.`);let h=Math.max(r,o),a=Math.max(s,i);return e=n(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=n(e,s+1,o+1),r=n(r,s+1,o+1)):s%2==1?(e=n(e,s+1,o),r=n(r,s+1,o)):o%2==1&&(e=n(e,s,o+1),r=n(r,s,o+1));let i=parseInt(e.rows/2,10),h=parseInt(e.columns/2,10),a=e.subMatrix(0,i-1,0,h-1),l=r.subMatrix(0,i-1,0,h-1),u=e.subMatrix(0,i-1,h,e.columns-1),c=r.subMatrix(0,i-1,h,r.columns-1),f=e.subMatrix(i,e.rows-1,0,h-1),m=r.subMatrix(i,r.rows-1,0,h-1),g=e.subMatrix(i,e.rows-1,h,e.columns-1),w=r.subMatrix(i,r.rows-1,h,r.columns-1),p=t(M.add(a,g),M.add(l,w),i,h),d=t(M.add(f,g),l,i,h),y=t(a,M.sub(c,w),i,h),b=t(g,M.sub(m,l),i,h),x=t(M.add(a,u),w,i,h),S=t(M.sub(f,a),M.add(l,c),i,h),E=t(M.sub(u,g),M.add(m,w),i,h),v=M.add(p,b);v.sub(x),v.add(E);let A=M.add(y,x),z=M.add(d,b),R=M.sub(p,d);R.add(y),R.add(S);let N=M.zeros(2*v.rows,2*v.columns);return N=N.setSubMatrix(v,0,0),N=N.setSubMatrix(A,v.rows,0),N=N.setSubMatrix(z,0,v.columns),N=N.setSubMatrix(R,v.rows,v.columns),N.subMatrix(0,s-1,0,o-1)}(e,t=n(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 i=this.getRow(t);i.length>0&&o(i,{min:e,max:r,output:i}),s.setRow(t,i)}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 i=this.getColumn(t);i.length&&o(i,{min:e,max:r,output:i}),s.setColumn(t,i)}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,i=new E(e*s,r*o);for(let n=0;n<e;n++)for(let e=0;e<r;e++)for(let r=0;r<s;r++)for(let h=0;h<o;h++)i.set(s*n+r,o*e+h,this.get(n,e)*t.get(r,h));return i}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 i=t;i<=e;i++)for(let e=r;e<=s;e++)o.set(i-t,e-r,this.get(i,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 i=e;i<=r;i++){if(t[o]<0||t[o]>=this.rows)throw new RangeError(`Row index out of range: ${t[o]}`);s.set(o,i-e,this.get(t[o],i))}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 i=e;i<=r;i++){if(t[o]<0||t[o]>=this.columns)throw new RangeError(`Column index out of range: ${t[o]}`);s.set(i-e,o,this.get(i,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 i=e[t];r.set(s,t,this.get(o,i))}}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,i=[];for(let n=0;n<s;n++){let s=0,h=0,a=0;for(let e=0;e<o;e++)a=t.get(n,e)-r[n],s+=a,h+=a*a;e?i.push((h-s*s/o)/(o-1)):i.push((h-s*s/o)/o)}return i}(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,i=[];for(let n=0;n<o;n++){let o=0,h=0,a=0;for(let e=0;e<s;e++)a=t.get(e,n)-r[n],o+=a,h+=a*a;e?i.push((h-o*o/s)/(s-1)):i.push((h-o*o/s)/s)}return i}(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,i=s*o;let n=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,n+=a,h+=a*a;return e?(h-n*n/i)/(i-1):(h-n*n/i)/i}(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,i=0,n=0;n<e;n++)s.set(o,i,t[n]),++o>=r&&(o=++i);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,i=0,n=0;n<e;n++)s.set(o,i,t[n]),++o>=r&&(o=1+ ++i);return s}}A.prototype.klassSubType="DistanceMatrix";class z extends M{constructor(t,e,r){super(),this.matrix=t,this.rows=e,this.columns=r}}class R extends z{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 N 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 k 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 I{constructor(t){let e,r,s,o,i,n,h,a,l,u=(t=k.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),i=0,s=0;s<l;s++)i+=u.get(e,s)*a[s];a[e]-=i,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++)n=u.get(o,s),u.set(o,s,u.get(r,s)),u.set(r,s,n);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,i=t.columns,n=t.subMatrixRow(this.pivotVector,0,i-1),h=e.columns;for(o=0;o<h;o++)for(r=o+1;r<h;r++)for(s=0;s<i;s++)n.set(r,s,n.get(r,s)-n.get(o,s)*e.get(r,o));for(o=h-1;o>=0;o--){for(s=0;s<i;s++)n.set(o,s,n.get(o,s)/e.get(o,o));for(r=0;r<o;r++)for(s=0;s<i;s++)n.set(r,s,n.get(r,s)-n.get(o,s)*e.get(r,o))}return n}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 F(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 T{constructor(t){let e,r,s,o,i=(t=k.checkMatrix(t)).clone(),n=t.rows,h=t.columns,a=new Float64Array(h);for(s=0;s<h;s++){let t=0;for(e=s;e<n;e++)t=F(t,i.get(e,s));if(0!==t){for(i.get(s,s)<0&&(t=-t),e=s;e<n;e++)i.set(e,s,i.get(e,s)/t);for(i.set(s,s,i.get(s,s)+1),r=s+1;r<h;r++){for(o=0,e=s;e<n;e++)o+=i.get(e,s)*i.get(e,r);for(o=-o/i.get(s,s),e=s;e<n;e++)i.set(e,r,i.get(e,r)+o*i.get(e,s))}}a[s]=-t}this.QR=i,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,i,n,h=t.columns,a=t.clone(),l=e.columns;for(i=0;i<l;i++)for(o=0;o<h;o++){for(n=0,s=i;s<r;s++)n+=e.get(s,i)*a.get(s,o);for(n=-n/e.get(i,i),s=i;s<r;s++)a.set(s,o,a.get(s,o)+n*e.get(s,i))}for(i=l-1;i>=0;i--){for(o=0;o<h;o++)a.set(i,o,a.get(i,o)/this.Rdiag[i]);for(s=0;s<i;s++)for(o=0;o<h;o++)a.set(s,o,a.get(s,o)-a.get(i,o)*e.get(s,i))}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,i=o.rows,n=o.columns,h=new E(i,n);for(r=n-1;r>=0;r--){for(t=0;t<i;t++)h.set(t,r,0);for(h.set(r,r,1),e=r;e<n;e++)if(0!==o.get(r,r)){for(s=0,t=r;t<i;t++)s+=o.get(t,r)*h.get(t,e);for(s=-s/o.get(r,r),t=r;t<i;t++)h.set(t,e,h.get(t,e)+s*o.get(t,r))}}return h}}class C{constructor(t,e={}){if((t=k.checkMatrix(t)).isEmpty())throw new Error("Matrix must be non-empty");let r=t.rows,s=t.columns;const{computeLeftSingularVectors:o=!0,computeRightSingularVectors:i=!0,autoTranspose:n=!1}=e;let h,a=Boolean(o),l=Boolean(i),u=!1;if(r<s)if(n){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]=F(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]=F(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=F(m[r],e),i=m[r]/o,n=e/o;if(m[r]=o,r!==t&&(e=-n*p[r-1],p[r-1]=i*p[r-1]),l)for(let t=0;t<s;t++)o=i*w.get(t,r)+n*w.get(t,S-1),w.set(t,S-1,-n*w.get(t,r)+i*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=F(m[s],e),i=m[s]/o,n=e/o;if(m[s]=o,e=-n*p[s],p[s]=i*p[s],a)for(let e=0;e<r;e++)o=i*g.get(e,s)+n*g.get(e,t-1),g.set(e,t-1,-n*g.get(e,s)+i*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,i=m[S-2]/e,n=p[S-2]/e,h=m[t]/e,u=p[t]/e,c=((i+o)*(i-o)+n*n)/2,f=o*n*(o*n);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=F(y,b);0===o&&(o=Number.MIN_VALUE);let i=y/o,n=b/o;if(e!==t&&(p[e-1]=o),y=i*m[e]+n*p[e],p[e]=i*p[e]-n*m[e],b=n*m[e+1],m[e+1]=i*m[e+1],l)for(let t=0;t<s;t++)o=i*w.get(t,e)+n*w.get(t,e+1),w.set(t,e+1,-n*w.get(t,e)+i*w.get(t,e+1)),w.set(t,e,o);if(o=F(y,b),0===o&&(o=Number.MIN_VALUE),i=y/o,n=b/o,m[e]=o,y=i*p[e]+n*m[e+1],m[e+1]=-n*p[e]+i*m[e+1],b=n*p[e+1],p[e+1]=i*p[e+1],a&&e<r-1)for(let t=0;t<r;t++)o=i*g.get(t,e)+n*g.get(t,e+1),g.set(t,e+1,-n*g.get(t,e)+i*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 i=this.U,n=this.rightSingularVectors,h=n.mmul(o),a=n.rows,l=i.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)*i.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 i=0;i<r;i++)for(let r=0;r<s;r++)Math.abs(this.s[r])>e&&o.set(i,r,t.get(i,r)/this.s[r]);let i=this.U,n=i.rows,h=i.columns,a=new E(r,n);for(let t=0;t<r;t++)for(let e=0;e<n;e++){let r=0;for(let s=0;s<h;s++)r+=o.get(t,s)*i.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 W(t,e,r=!1){return t=k.checkMatrix(t),e=k.checkMatrix(e),r?new C(t).solve(e):t.isSquare()?new I(t).solve(e):new T(t).solve(e)}function D(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 q{constructor(t,e={}){const{assumeSymmetric:r=!1}=e;if(!(t=k.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,i=t.columns,n=new E(i,i),h=new Float64Array(i),a=new Float64Array(i),l=t,u=!1;if(u=!!r||t.isSymmetric(),u){for(s=0;s<i;s++)for(o=0;o<i;o++)n.set(s,o,l.get(s,o));!function(t,e,r,s){let o,i,n,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,n=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,n+=r[l]*r[l];for(o=r[h-1],i=Math.sqrt(n),o>0&&(i=-i),e[h]=c*i,n-=o*i,r[h-1]=o-i,a=0;a<h;a++)e[a]=0;for(a=0;a<h;a++){for(o=r[a],s.set(a,h,o),i=e[a]+s.get(a,a)*o,l=a+1;l<=h-1;l++)i+=s.get(l,a)*r[l],e[l]+=s.get(l,a)*o;e[a]=i}for(o=0,a=0;a<h;a++)e[a]/=n,o+=e[a]*r[a];for(u=o/(n+n),a=0;a<h;a++)e[a]-=u*r[a];for(a=0;a<h;a++){for(o=r[a],i=e[a],l=a;l<=h-1;l++)s.set(l,a,s.get(l,a)-(o*e[l]+i*r[l]));r[a]=s.get(h-1,a),s.set(h,a,0)}}r[h]=n}for(h=0;h<t-1;h++){if(s.set(t-1,h,s.get(h,h)),s.set(h,h,1),n=r[h+1],0!==n){for(l=0;l<=h;l++)r[l]=s.get(l,h+1)/n;for(a=0;a<=h;a++){for(i=0,l=0;l<=h;l++)i+=s.get(l,h+1)*s.get(l,a);for(l=0;l<=h;l++)s.set(l,a,s.get(l,a)-i*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}(i,a,h,n),function(t,e,r,s){let o,i,n,h,a,l,u,c,f,m,g,w,p,d,y,b;for(n=1;n<t;n++)e[n-1]=e[n];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=F(c,1),c<0&&(f=-f),r[l]=e[l]/(c+f),r[l+1]=e[l]*(c+f),m=r[l+1],i=o-r[l],n=l+2;n<t;n++)r[n]-=i;for(M+=i,c=r[u],g=1,w=g,p=g,d=e[l+1],y=0,b=0,n=u-1;n>=l;n--)for(p=w,w=g,b=y,o=g*e[n],i=g*c,f=F(c,e[n]),e[n+1]=y*f,y=e[n]/f,g=c/f,c=g*r[n]-y*o,r[n+1]=i+y*(g*o+y*r[n]),a=0;a<t;a++)i=s.get(a,n+1),s.set(a,n+1,y*s.get(a,n)+g*i),s.set(a,n,g*s.get(a,n)-y*i);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(n=0;n<t-1;n++){for(a=n,c=r[n],h=n+1;h<t;h++)r[h]<c&&(a=h,c=r[h]);if(a!==n)for(r[a]=r[n],r[n]=c,h=0;h<t;h++)c=s.get(h,n),s.set(h,n,s.get(h,a)),s.set(h,a,c)}}(i,a,h,n)}else{let t=new E(i,i),e=new Float64Array(i);for(o=0;o<i;o++)for(s=0;s<i;s++)t.set(s,o,l.get(s,o));!function(t,e,r,s){let o,i,n,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(n=0,h=c;h>=l;h--)r[h]=e.get(h,l-1)/u,n+=r[h]*r[h];for(i=Math.sqrt(n),r[l]>0&&(i=-i),n-=r[l]*i,r[l]=r[l]-i,a=l;a<t;a++){for(o=0,h=c;h>=l;h--)o+=r[h]*e.get(h,a);for(o/=n,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/=n,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*i)}}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(i=0,h=l;h<=c;h++)i+=r[h]*s.get(h,a);for(i=i/r[l]/e.get(l,l-1),h=l;h<=c;h++)s.set(h,a,s.get(h,a)+i*r[h])}}}(i,t,e,n),function(t,e,r,s,o){let i,n,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,z=0,R=0,N=0,k=0,I=0;for(i=0;i<t;i++)for((i<0||i>x)&&(r[i]=o.get(i,i),e[i]=0),n=Math.max(i-1,0);n<t;n++)v+=Math.abs(o.get(i,n));for(;M>=0;){for(a=M;a>0&&(N=Math.abs(o.get(a-1,a-1))+Math.abs(o.get(a,a)),0===N&&(N=v),!(Math.abs(o.get(a,a-1))<S*N));)a--;if(a===M)o.set(M,M,o.get(M,M)+E),r[M]=o.get(M,M),e[M]=0,M--,I=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,z=A*A+c,k=Math.sqrt(Math.abs(z)),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),z>=0){for(k=A>=0?A+k:A-k,r[M-1]=f+k,r[M]=r[M-1],0!==k&&(r[M]=f-c/k),e[M-1]=0,e[M]=0,f=o.get(M,M-1),N=Math.abs(f)+Math.abs(k),A=f/N,z=k/N,R=Math.sqrt(A*A+z*z),A/=R,z/=R,n=M-1;n<t;n++)k=o.get(M-1,n),o.set(M-1,n,z*k+A*o.get(M,n)),o.set(M,n,z*o.get(M,n)-A*k);for(i=0;i<=M;i++)k=o.get(i,M-1),o.set(i,M-1,z*k+A*o.get(i,M)),o.set(i,M,z*o.get(i,M)-A*k);for(i=0;i<=x;i++)k=s.get(i,M-1),s.set(i,M-1,z*k+A*s.get(i,M)),s.set(i,M,z*s.get(i,M)-A*k)}else r[M-1]=f+A,r[M]=f+A,e[M-1]=k,e[M]=-k;M-=2,I=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===I){for(E+=f,i=0;i<=M;i++)o.set(i,i,o.get(i,i)-f);N=Math.abs(o.get(M,M-1))+Math.abs(o.get(M-1,M-2)),f=m=.75*N,c=-.4375*N*N}if(30===I&&(N=(m-f)/2,N=N*N+c,N>0)){for(N=Math.sqrt(N),m<f&&(N=-N),N=f-c/((m-f)/2+N),i=0;i<=M;i++)o.set(i,i,o.get(i,i)-N);E+=N,f=m=c=.964}for(I+=1,l=M-2;l>=a&&(k=o.get(l,l),R=f-k,N=m-k,A=(R*N-c)/o.get(l+1,l)+o.get(l,l+1),z=o.get(l+1,l+1)-k-R-N,R=o.get(l+2,l+1),N=Math.abs(A)+Math.abs(z)+Math.abs(R),A/=N,z/=N,R/=N,l!==a)&&!(Math.abs(o.get(l,l-1))*(Math.abs(z)+Math.abs(R))<S*(Math.abs(A)*(Math.abs(o.get(l-1,l-1))+Math.abs(k)+Math.abs(o.get(l+1,l+1)))));)l--;for(i=l+2;i<=M;i++)o.set(i,i-2,0),i>l+2&&o.set(i,i-3,0);for(h=l;h<=M-1&&(y=h!==M-1,h!==l&&(A=o.get(h,h-1),z=o.get(h+1,h-1),R=y?o.get(h+2,h-1):0,f=Math.abs(A)+Math.abs(z)+Math.abs(R),0!==f&&(A/=f,z/=f,R/=f)),0!==f);h++)if(N=Math.sqrt(A*A+z*z+R*R),A<0&&(N=-N),0!==N){for(h!==l?o.set(h,h-1,-N*f):a!==l&&o.set(h,h-1,-o.get(h,h-1)),A+=N,f=A/N,m=z/N,k=R/N,z/=A,R/=A,n=h;n<t;n++)A=o.get(h,n)+z*o.get(h+1,n),y&&(A+=R*o.get(h+2,n),o.set(h+2,n,o.get(h+2,n)-A*k)),o.set(h,n,o.get(h,n)-A*f),o.set(h+1,n,o.get(h+1,n)-A*m);for(i=0;i<=Math.min(M,h+3);i++)A=f*o.get(i,h)+m*o.get(i,h+1),y&&(A+=k*o.get(i,h+2),o.set(i,h+2,o.get(i,h+2)-A*R)),o.set(i,h,o.get(i,h)-A),o.set(i,h+1,o.get(i,h+1)-A*z);for(i=0;i<=x;i++)A=f*s.get(i,h)+m*s.get(i,h+1),y&&(A+=k*s.get(i,h+2),s.set(i,h+2,s.get(i,h+2)-A*R)),s.set(i,h,s.get(i,h)-A),s.set(i,h+1,s.get(i,h+1)-A*z)}}}if(0!==v){for(M=t-1;M>=0;M--)if(A=r[M],z=e[M],0===z)for(a=M,o.set(M,M,1),i=M-1;i>=0;i--){for(c=o.get(i,i)-A,R=0,n=a;n<=M;n++)R+=o.get(i,n)*o.get(n,M);if(e[i]<0)k=c,N=R;else if(a=i,0===e[i]?o.set(i,M,0!==c?-R/c:-R/(S*v)):(f=o.get(i,i+1),m=o.get(i+1,i),z=(r[i]-A)*(r[i]-A)+e[i]*e[i],u=(f*N-k*R)/z,o.set(i,M,u),o.set(i+1,M,Math.abs(f)>Math.abs(k)?(-R-c*u)/f:(-N-m*u)/k)),u=Math.abs(o.get(i,M)),S*u*u>1)for(n=i;n<=M;n++)o.set(n,M,o.get(n,M)/u)}else if(z<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,z/o.get(M,M-1)),o.set(M-1,M,-(o.get(M,M)-A)/o.get(M,M-1))):(b=O(0,-o.get(M-1,M),o.get(M-1,M-1)-A,z),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),i=M-2;i>=0;i--){for(g=0,w=0,n=a;n<=M;n++)g+=o.get(i,n)*o.get(n,M-1),w+=o.get(i,n)*o.get(n,M);if(c=o.get(i,i)-A,e[i]<0)k=c,R=g,N=w;else if(a=i,0===e[i]?(b=O(-g,-w,c,z),o.set(i,M-1,b[0]),o.set(i,M,b[1])):(f=o.get(i,i+1),m=o.get(i+1,i),p=(r[i]-A)*(r[i]-A)+e[i]*e[i]-z*z,d=2*(r[i]-A)*z,0===p&&0===d&&(p=S*v*(Math.abs(c)+Math.abs(z)+Math.abs(f)+Math.abs(m)+Math.abs(k))),b=O(f*R-k*g+z*w,f*N-k*w-z*g,p,d),o.set(i,M-1,b[0]),o.set(i,M,b[1]),Math.abs(f)>Math.abs(k)+Math.abs(z)?(o.set(i+1,M-1,(-g-c*o.get(i,M-1)+z*o.get(i,M))/f),o.set(i+1,M,(-w-c*o.get(i,M)-z*o.get(i,M-1))/f)):(b=O(-R-m*o.get(i,M-1),-N-m*o.get(i,M),k,z),o.set(i+1,M-1,b[0]),o.set(i+1,M,b[1]))),u=Math.max(Math.abs(o.get(i,M-1)),Math.abs(o.get(i,M))),S*u*u>1)for(n=i;n<=M;n++)o.set(n,M-1,o.get(n,M-1)/u),o.set(n,M,o.get(n,M)/u)}for(i=0;i<t;i++)if(i<0||i>x)for(n=i;n<t;n++)s.set(i,n,o.get(i,n));for(n=t-1;n>=0;n--)for(i=0;i<=x;i++){for(k=0,h=0;h<=Math.min(n,x);h++)k+=s.get(i,h)*o.get(h,n);s.set(i,n,k)}}}(i,a,h,n,t)}this.n=i,this.e=a,this.d=h,this.V=n}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,i=new E(r,r);for(t=0;t<r;t++){for(e=0;e<r;e++)i.set(t,e,0);i.set(t,t,o[t]),s[t]>0?i.set(t,t+1,s[t]):s[t]<0&&i.set(t,t-1,s[t])}return i}}function O(t,e,r,s){let o,i;return Math.abs(r)>Math.abs(s)?(o=s/r,i=r+o*s,[(t+o*e)/i,(e-o*t)/i]):(o=r/s,i=s+o*r,[(o*t+e)/i,(o*e-t)/i])}class P{constructor(t){if(!(t=k.checkMatrix(t)).isSymmetric())throw new Error("Matrix is not symmetric");let e,r,s,o=t,i=o.rows,n=new E(i,i),h=!0;for(r=0;r<i;r++){let t=0;for(s=0;s<r;s++){let i=0;for(e=0;e<s;e++)i+=n.get(s,e)*n.get(r,e);i=(o.get(r,s)-i)/n.get(s,s),n.set(r,s,i),t+=i*i}for(t=o.get(r,r)-t,h&&=t>0,n.set(r,r,Math.sqrt(Math.max(t,0))),s=r+1;s<i;s++)n.set(r,s,0)}this.L=n,this.positiveDefinite=h}isPositiveDefinite(){return this.positiveDefinite}solve(t){t=k.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,i,n=t.columns,h=t.clone();for(i=0;i<r;i++)for(o=0;o<n;o++){for(s=0;s<i;s++)h.set(i,o,h.get(i,o)-h.get(s,o)*e.get(i,s));h.set(i,o,h.get(i,o)/e.get(i,i))}for(i=r-1;i>=0;i--)for(o=0;o<n;o++){for(s=i+1;s<r;s++)h.set(i,o,h.get(i,o)-h.get(s,o)*e.get(s,i));h.set(i,o,h.get(i,o)/e.get(i,i))}return h}get lowerTriangularMatrix(){return this.L}}class _{constructor(t,e={}){t=k.checkMatrix(t);let{Y:r}=e;const{scaleScores:o=!1,maxIterations:i=1e3,terminationCriteria:n=1e-10}=e;let h;if(r){if(r=s.isAnyArray(r)&&"number"==typeof r[0]?E.columnVector(r):k.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<i&&f>n;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)),i=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=i,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=P,e.oN=P,e.Hc=A,e.cg=q,e.hj=q,e.LU=I,e.Tb=I,e.uq=E,e.Zm=class extends z{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 z{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 z{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 z{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 z{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 z{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 z{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 z{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=_,e.ks=_,e.QR=T,e.jp=T,e.mk=C,e.W2=C,e.l=v,e.KY=N,e.dv=k,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:i=!0,scale:n=!0}=r;i&&(t.center("column"),o||e.center("column")),n&&(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:i=!0}=r;i&&(t=t.center("column"),o||(e=e.center("column")));const n=t.transpose().mmul(e);for(let e=0;e<n.rows;e++)for(let r=0;r<n.columns;r++)n.set(e,r,n.get(e,r)*(1/(t.rows-1)));return n},e.a4=function t(e){if((e=E.checkMatrix(e)).isSquare()){if(0===e.columns)return 1;let r,s,o,i;if(2===e.columns)return r=e.get(0,0),s=e.get(0,1),o=e.get(1,0),i=e.get(1,1),r*i-s*o;if(3===e.columns){let i,n,h;return i=new R(e,[1,2],[1,2]),n=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(i)-s*t(n)+o*t(h)}return new I(e).determinant}throw Error("determinant can only be calculated for a square matrix")},e.DI=function(t,e=!1){return t=k.checkMatrix(t),e?new C(t).inverse():W(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,i=new E(o,o);for(let e=0;e<o;e++){let n=E.columnVector(t.getRow(e)),h=t.subMatrixRow(D(o,e)).transpose(),a=new C(h).solve(n),l=E.sub(n,h.mmul(a)).abs().max();i.setRow(e,V(l,a,e,r,s))}return i},e.Zi=function(t,e=Number.EPSILON){if((t=E.checkMatrix(t)).isEmpty())return t.transpose();let r=new C(t,{autoTranspose:!0}),s=r.leftSingularVectors,o=r.rightSingularVectors,i=r.diagonal;for(let t=0;t<i.length;t++)Math.abs(i[t])>e?i[t]=1/i[t]:i[t]=0;return o.mmul(E.diag(i).mmul(s.transpose()))},e.kH=W,e.LV=function(t,e){if(s.isAnyArray(t))return t[0]&&s.isAnyArray(t[0])?new k(t):new N(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,i=e.toIndex,n=void 0===i?t.length:i;if(o<0||o>=t.length||!Number.isInteger(o))throw new Error("fromIndex must be a positive integer smaller than length");if(n<=o||n>t.length||!Number.isInteger(n))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<n;a++)t[a]<h&&(h=t[a]);return h}(t),i=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,i=e.toIndex,n=void 0===i?t.length:i;if(o<0||o>=t.length||!Number.isInteger(o))throw new Error("fromIndex must be a positive integer smaller than length");if(n<=o||n>t.length||!Number.isInteger(n))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<n;a++)t[a]>h&&(h=t[a]);return h}(t);if(o===i)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");var n=r.min,h=void 0===n?r.autoMinMax?o:0:n,a=r.max,l=void 0===a?r.autoMinMax?i:1:a;if(h>=l)throw new RangeError("min option must be smaller than max option");for(var u=(l-h)/(i-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 i=e[s]={exports:{}};return t[s](i,i.exports,r),i.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 i(t){return o(t).map((t,e)=>e)}function n(t,e){return o(t).map(()=>e)}function h(t){return n(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 i=0;i<e;i++){let e=!0;for(;e;){const n=t(r,s);let h=!1;for(let t=0;t<i;t++)if(n===o[t]){h=!0;break}h||(e=!1),o[i]=n}}return o}function c(t,e,r){const s=[];let o=0,i=0;if(t.length!==e*r)throw new Error("Array dimensions must match input length.");for(let n=0;n<e;n++){const e=[];for(let s=0;s<r;s++)e.push(t[i]),i+=1;s.push(e),o+=1}return s}function f(t,e){const r=r=>o(t).map(()=>n(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 i=0;i<e;i++){let e=!0,n=0;for(;e;){n=t(r,s);let h=!1;for(let t=0;t<i;t++)if(n===o[t]){h=!0;break}h||(e=!1)}o[i]=n}return o}function g(t,e,r,s,o){e=Math.floor(e);const i=t[0][e],n=t[1][e];if(t[2][e],r>=n[0])return 0;for(let t=0;t<i.length;t++)if(s===i[t])return 0;return w(t,e,r,s,o)}function w(t,e,r,s,o){const i=t[0][e],n=t[1][e],h=t[2][e];if(r>=n[0])return 0;n[0]=r,i[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(!(n[e]>r))break;l=e}else if(n[e]>=n[s]){if(!(r<n[e]))break;l=e}else{if(!(r<n[s]))break;l=s}n[a]=n[l],i[a]=i[l],h[a]=h[l],a=l}return n[a]=r,i[a]=s,h[a]=o,1}function p(t,r,s,o,i){const n=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(i);g(n,o,a,s,h),g(n,s,a,o,h),t[2][o][r]=0}return n}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,i=s[0];s[0]=s[e],s[e]=i;const n=o[0];o[0]=o[r],o[r]=n,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,i=o+1;let n=s;if(t[n]<t[o]&&(n=o),i<r&&t[n]<t[i]&&(n=i),n===s)break;{const r=t[s];t[s]=t[n],t[n]=r;const o=e[s];e[s]=e[n],e[n]=o,s=n}}}function b(t,e){const r=t[0][e],s=t[1][e],o=t[2][e];let i=1/0,n=-1;for(let t=0;t>r.length;t++)1===o[t]&&s[t]<i&&(i=s[t],n=t);return n>=0?(o[n]=0,Math.floor(r[n])):-1}r.r(s),r.d(s,{UMAP:()=>at,initWasm:()=>W,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],i=e[s];this.checkDims(o,i);const n=this.makeKey(o,i);this.entries.set(n,{value:r[s],row:o,col:i})}}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,i)=>{e.push(o),r.push(i),s.push(t)});const o=[t.nCols,t.nRows];return new M(r,e,s,o)}function S(t,e){return k(t,e,(t,e)=>t*e)}function E(t,e){return k(t,e,(t,e)=>t+e)}function v(t,e){return k(t,e,(t,e)=>t-e)}function A(t,e){return t.map(t=>t*e)}function z(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 i=(t,r)=>!e.has(r),n=r.filter(i),h=s.filter(i),a=o.filter(i);return new M(h,a,n,t.getDims())}function R(t,e="l2"){const r=N[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 i=s.get(e).sort(),n=r(i.map(r=>t.get(e,r)));for(let t=0;t<n.length;t++)o.set(e,i[t],n[t])}return o}const N={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 k(t,e,r){const s=new Set,o=[],i=[],n=[],h=(s,h)=>{o.push(s),i.push(h);const a=r(t.get(s,h),e.get(s,h));n.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,i,n,g)}function I(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 i=-1;for(let t=0;t<e.length;t++){const{row:n,col:h,value:a}=e[t];n!==i&&(i=n,o.push(t)),r.push(h),s.push(a)}return{indices:r,values:s,indptr:o}}let F=null,T=null,C=null;async function W(){return F||(F=(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 try{const e=["..","wasm","pkg","web","umap_wasm_core.js"].join("/");t=await r(433)(e)}catch(e){const r=`${"undefined"!=typeof window&&window.location?window.location.origin:""}/wasm/pkg/web/umap_wasm_core.js`;t=await new Function("p","return import(p)")(r)}return"function"==typeof t.default&&(C=await t.default()),T=t,t}catch(t){throw F=null,T=null,C=null,new Error(`Failed to load WASM module: ${t}`)}})(),F)}function D(){return null!==T}function V(t,e){if(!T)throw new Error("WASM module not initialized");const r=new Float64Array(t),s=new Float64Array(e);return T.euclidean(r,s)}function q(t,e,r,s,o){if(!T)throw new Error("WASM module not initialized");const i=new Float64Array(e*r);for(let s=0;s<e;s++)for(let e=0;e<r;e++)i[s*r+e]=t[s][e];return T.build_rp_tree(i,e,r,s,BigInt(o))}function O(t,e,r,s,o){if(!T)throw new Error("WASM module not initialized");const i=new Int32Array(t),n=new Int32Array(e),h=new Float64Array(r);return new T.WasmSparseMatrix(i,n,h,s,o)}function P(t){if(!T)throw new Error("WASM module not initialized");return T.sparse_transpose(t)}function _(t,e){if(!T)throw new Error("WASM module not initialized");return T.sparse_add(t,e)}function j(t,e){if(!T)throw new Error("WASM module not initialized");return T.sparse_subtract(t,e)}function L(t,e){if(!T)throw new Error("WASM module not initialized");return T.sparse_pairwise_multiply(t,e)}function $(t,e){if(!T)throw new Error("WASM module not initialized");return T.sparse_multiply_scalar(t,e)}function U(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 B{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()),i=t.dim(),n=t.n_nodes(),h=[];for(let t=0;t<n;t++)h.push(e.slice(t*i,(t+1)*i));const a=[];for(let t=0;t<n;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 B(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 K(t,e,r,s,o=!1){const n=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,i=t[0].length,n=[];for(let h=0;h<r;h++){const r=q(t,o,i,e,Math.floor(4294967295*s()));n.push(B.fromWasm(r))}return n}(t,n,r,s)}const a=i(r).map((e,r)=>function(t,e=30,r,s){return G(t,i(t.length),e,r,s)}(t,n,r,s)),l=a.map(t=>function(t,e){const r=Y(t),s=X(t),o=i(r).map(()=>h(t.hyperplane?t.hyperplane.length:0)),n=h(r),a=i(r).map(()=>[-1,-1]),l=i(s).map(()=>i(e).map(()=>-1));return Q(t,o,n,a,l,0,0),new B(o,n,a,l)}(t,n));return l}function G(e,r,s=30,o,i){if(r.length>s){const n=function(e,r,s){const o=e[0].length;let i=t(r.length,s),n=t(r.length,s);n+=i===n?1:0,n%=r.length;const a=r[i],l=r[n];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 i=0;i<r.length;i++){let n=u;for(let t=0;t<o;t++)n+=c[t]*e[r[i]][t];0===n?(g[i]=t(2,s),0===g[i]?f+=1:m+=1):n>0?(g[i]=0,f+=1):(g[i]=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,i),{indicesLeft:a,indicesRight:l,hyperplane:u,offset:c}=n;return{leftChild:G(e,a,s,o+1,i),rightChild:G(e,l,s,o+1,i),isLeaf:!1,hyperplane:u,offset:c}}return{indices:r,isLeaf:!0}}function Q(t,e,r,s,o,i,n){if(t.isLeaf)return s[i][0]=-n,o[n].splice(0,t.indices.length,...t.indices),{nodeNum:i,leafNum:n+=1};{e[i]=t.hyperplane,r[i]=t.offset,s[i][0]=i+1;const h=i;let a=Q(t.leftChild,e,r,s,o,i+1,n);return i=a.nodeNum,n=a.leafNum,s[h][1]=i+1,a=Q(t.rightChild,e,r,s,o,i+1,n),{nodeNum:a.nodeNum,leafNum:a.leafNum}}}function Y(t){return t.isLeaf?1:1+Y(t.leftChild)+Y(t.rightChild)}function X(t){return t.isLeaf?1:X(t.leftChild)+X(t.rightChild)}function J(e,r,s,o){let i=r;for(let t=0;t<s.length;t++)i+=e[t]*s[t];return 0===i?t(2,o):i>0?0:1}function H(t,e,r){const s=e.getWasmTree();if(s&&D())return function(t,e,r){if(!T)throw new Error("WASM module not initialized");const s=new Float64Array(e),o=T.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===J(e.hyperplanes[o],e.offsets[o],t,r)?e.children[o][0]:e.children[o][1];const i=-1*e.children[o][0];return e.indices[i]}const Z=Object.prototype.toString;function tt(t){return Z.call(t).endsWith("Array]")}function et(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 rt=r(673);rt.y3,rt.jy,rt.oN,rt.Hc,rt.cg,rt.hj,rt.LU,rt.Tb;const st=rt.uq,ot=(rt.Zm,rt.Dq,rt.__,rt.q0,rt.lh,rt.pI,rt.zC,rt.zg,rt.g6,rt.OL,rt.ks,rt.QR,rt.jp,rt.mk,rt.W2,rt.l,rt.KY,rt.dv,rt.BR,rt.Wu,rt.uq,rt.uq,rt.a4,rt.DI);function it(t,e,r,s,o){let i=r*s*s,n=st.eye(e.length,e.length,i);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 i=r.length,n=t.x.length;let h=new Array(i);for(let a=0;a<i;a++){h[a]=new Array(n);let i=r.slice();i[a]+=s;let l=o(i);for(let r=0;r<n;r++)h[a][r]=e[r]-l(t.x[r])}return new st(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 st(s)}(t,a),c=ot(n.add(l.mmul(l.transpose())));return(e=(e=new st([e])).sub(c.mmul(l).mmul(u).mul(s).transpose())).to1DArray()}rt.Jo,rt.Zi,rt.kH,rt.LV;const nt=1e-5,ht=.001;class at{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=lt,this.useWasmDistance=!1,this.useWasmNNDescent=!1,this.useWasmMatrix=!1,this.useWasmOptimizer=!1,this.useWasmTree=!1,this.wasmBatchSize=10,this.isInitialized=!1,this.rpForest=[],this.embedding=[],this.optimizationState=new ut,this.wasmOptimizerState=null,this.rngState=BigInt(Math.floor(4294967295*Math.random()));const e=e=>{void 0!==t[e]&&(this[e]=t[e])};e("distanceFn"),e("useWasmDistance"),e("useWasmNNDescent"),e("useWasmMatrix"),e("useWasmTree"),e("useWasmOptimizer"),e("wasmBatchSize"),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 V(t,e)}return this.distanceFn(t,e)},{initFromTree:e,initFromRandom:r}=(s=t,{initFromRandom:function(t,e,r,o,i){for(let n=0;n<r.length;n++){const h=u(t,e.length,i);for(let t=0;t<h.length;t++)h[t]<0||g(o,n,s(e[h[t]],r[n]),h[t],1)}},initFromTree:function(t,e,r,o,i){for(let n=0;n<r.length;n++){const h=H(r[n],t,i);for(let t=0;t<h.length;t++){if(h[t]<0)return;g(o,n,s(e[h[t]],r[n]),h[t],1)}}}});var s;this.initFromTree=e,this.initFromRandom=r,this.search=function(t){return function(e,r,s,o){const{indices:i,indptr:n}=I(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=i.slice(n[a],n[a+1]);for(const i of l)i===a||-1===i||h.has(i)||(w(s,r,t(e[i],o[r]),i,1),h.add(i))}}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 V(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],i=r[t];for(let e=0;e<s.length;e++){const r=s[e],n=i[e];n>0&&o.set(t,r,n)}}return k(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,i,n){const h=f(r.length,s);if(o(s,e,r,h,n),t)for(let s of t)i(s,e,r,h,n);return h}(this.rpForest,e,t,r,this.initFromRandom,this.initFromTree,this.random),o=this.search(e,this.searchGraph,s,t);let{indices:i,weights:n}=d(o);i=i.map(t=>t.slice(0,this.nNeighbors)),n=n.map(t=>t.slice(0,this.nNeighbors));const a=Math.max(0,this.localConnectivity-1),{sigmas:l,rhos:u}=this.smoothKNNDistance(n,this.nNeighbors,a),{rows:m,cols:g,vals:w}=this.computeMembershipStrengths(i,n,l,u),p=[t.length,e.length];let y=new M(m,g,w,p);const b=I(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 i=0;i<t[0].length;i++)for(let n=0;n<r[0].length;n++){const h=t[o][i];s[o][n]+=e[o][i]*r[h][n]}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=z(y);const A=this.makeEpochsPerSample(y.getValues(),E),N=y.getRows(),k=y.getCols();return this.assignOptimizationStateParameters({headEmbedding:S,tailEmbedding:this.embedding,head:N,tail:k,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.useWasmOptimizer&&this.wasmOptimizerState?this.materializeEmbeddingFromWasm():this.embedding}nearestNeighbors(t){const{distanceFn:r,nNeighbors:s}=this,o=function(t,r,s=!1){return function(o,i,n,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,i=.001,n=.5,h=!0,a="euclidean",l=42){if(!T)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=T.nn_descent(f,u,c,w,m,g,r,s,o,i,n,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 i=0;i<r;i++)e.push(p[t*r+i]),s.push(p[M+t*r+i]),o.push(p[x+t*r+i]);d.push(e),y.push(s),b.push(o)}return[d,y,b]}(o,i,n,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,n);for(let e=0;e<o.length;e++){const s=m(n,o.length,r);for(let r=0;r<s.length;r++){const i=t(o[e],o[s[r]]);g(y,e,i,s[r],1),g(y,s[r],i,e,1)}}if(c)for(let e=0;e<i.length;e++)for(let r=0;r<i[e].length&&!(i[e][r]<0);r++)for(let s=r+1;s<i[e].length&&!(i[e][s]<0);s++){const n=t(o[i[e][r]],o[i[e][s]]);g(y,i[e][r],n,i[e][s],1),g(y,i[e][s],n,i[e][r],1)}for(let s=0;s<h;s++){const s=p(y,w,n,a,r);let i=0;for(let n=0;n<w;n++)for(let h=0;h<a;h++){let l=Math.floor(s[0][n][h]);if(!(l<0||e(r)<u))for(let e=0;e<a;e++){const r=Math.floor(s[0][n][e]),a=s[2][n][h],u=s[2][n][e];if(r<0||!a&&!u)continue;const c=t(o[l],o[r]);i+=g(y,l,c,r,1),i+=g(y,r,c,l,1)}}if(i<=l*n*o.length)break}return d(y)}}(r,this.random,this.useWasmNNDescent),i=5+Math.floor(.5==(n=t.length**.5/20)?0:Math.round(n));var n;const h=Math.max(5,Math.floor(Math.round((t=>Math.log(t)/Math.log(2))(t.length))));this.rpForest=K(t,s,i,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:i}=this,{sigmas:n,rhos:h}=this.smoothKNNDistance(o,e,i),{rows:a,cols:l,vals:u}=this.computeMembershipStrengths(s,o,n,h),c=[t.length,t.length];if(this.useWasmMatrix&&D()){const t=O(a,l,u,c[0],c[1]),e=P(t),s=L(t,e),o=_(t,e),i=U(_($(j(o,s),r),$(s,1-r))),n=i.map(t=>t.row),h=i.map(t=>t.col),f=i.map(t=>t.value);return new M(n,h,f,c)}if(this.useWasmMatrix&&D()){const t=O(a,l,u,c[0],c[1]),e=P(t),s=L(t,e),o=_(t,e),i=U(_($(j(o,s),r),$(s,1-r))),n=i.map(t=>t.row),h=i.map(t=>t.col),f=i.map(t=>t.value);return new M(n,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,i)=>-1===e[o]||-1===e[i]?t*Math.exp(-r):e[o]!==e[i]?t*Math.exp(-s):t)}(t,e,s,r);return o=z(o),function(t){const e=x(t=R(t,"max"));return z(t=E(t,v(e,S(e,t))))}(o)}smoothKNNDistance(t,e,r=1,s=64,o=1){const i=Math.log(e)/Math.log(2)*o,n=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?(n[e]=m[t-1],s>nt&&(n[e]+=s*(m[t]-m[t-1]))):n[e]=s*m[0]}else m.length>0&&(n[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]-n[e];r+=o>0?Math.exp(-o/c):1}if(Math.abs(r-i)<nt)break;r>i?(h=c,c=(o+h)/2):(o=c,h===1/0?c*=2:c=(o+h)/2)}if(u[e]=c,n[e]>0){const t=a(f);u[e]<ht*t&&(u[e]=ht*t)}else{const r=a(t.map(a));u[e]<ht*r&&(u[e]=ht*r)}}return{sigmas:u,rhos:n}}computeMembershipStrengths(t,e,r,s){const o=t.length,i=t[0].length,n=h(o*i),a=h(o*i),l=h(o*i);for(let h=0;h<o;h++)for(let o=0;o<i;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]),n[h*i+o]=h,a[h*i+o]=t[h][o],l[h*i+o]=u)}return{rows:n,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 i=this.graph.map(e=>e<o/t?0:e);this.embedding=h(i.nRows).map(()=>h(r).map(()=>20*e(this.random)-10));const n=[],a=[],l=[],u=i.getAll();for(let t=0;t<u.length;t++){const e=u[t];e.value&&(n.push(e.value),l.push(e.row),a.push(e.col))}return{head:a,tail:l,epochsPerSample:this.makeEpochsPerSample(n,t)}}makeEpochsPerSample(t,e){const r=n(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:i}=this.optimizationState,n=o[0].length,h=o.length===i.length,a=s.map(t=>t/r),l=[...a],u=[...s];if(this.assignOptimizationStateParameters({epochOfNextSample:u,epochOfNextNegativeSample:l,epochsPerNegativeSample:a,moveOther:h,initialAlpha:e,alpha:e,gamma:t,dim:n}),this.useWasmOptimizer&&D()){const{head:r,tail:l,nEpochs:u,nVertices:c,a:f,b:m}=this.optimizationState;this.wasmOptimizerState=function(t,e,r,s,o,i,n,h,a,l,u,c,f,m){if(!T)throw new Error("WASM module not initialized");const g=new Float64Array(r.length*c),w=new Float64Array(s.length*c);for(let t=0;t<r.length;t++)for(let e=0;e<c;e++)g[t*c+e]=r[t][e];for(let t=0;t<s.length;t++)for(let e=0;e<c;e++)w[t*c+e]=s[t][e];const p=new Uint32Array(t),d=new Uint32Array(e),y=new Float64Array(o),b=new Float64Array(i);return new T.OptimizerState(p,d,g,w,y,b,n,h,a,l,u,c,f,m)}(r,l,o,i,s,a,h,e,t,f,m,n,u,c)}}initializeOptimization(){const t=this.embedding,e=this.embedding,{head:r,tail:s,epochsPerSample:i}=this.optimizationState,n=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),i={x:r,y:s},{parameterValues:n}=function(t,e,r={}){let{maxIterations:s=100,gradientDifference:o=.1,damping:i=0,errorTolerance:n=.01,minValues:h,maxValues:a,initialValues:l}=r;if(i<=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(!tt(t.x)||t.x.length<2||!tt(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(!tt(u))throw new Error("initialValues must be an array");let f,m=et(t,u,e),g=m<=n;for(f=0;f<s&&!g;f++){u=it(t,u,i,o,e);for(let t=0;t<c;t++)u[t]=Math.min(Math.max(h[t],u[t]),a[t]);if(m=et(t,u,e),isNaN(m))break;g=m<=n}return{parameterValues:u,parameterError:m,iterations:f}}(i,([t,e])=>r=>1/(1+t*r**(2*e)),{damping:1.5,initialValues:[.5,.5],gradientDifference:.1,maxIterations:100,errorTolerance:.01}),[a,l]=n;return{a,b:l}}(this.spread,this.minDist);this.assignOptimizationStateParameters({headEmbedding:t,tailEmbedding:e,head:r,tail:s,epochsPerSample:i,a:l,b:u,nEpochs:n,nVertices:a})}optimizeLayoutStep(e){if(this.useWasmOptimizer&&this.wasmOptimizerState)return function(t,e){if(!T)throw new Error("WASM module not initialized");T.optimize_layout_step_in_place(t,e)}(this.wasmOptimizerState,this.rngState),this.advanceRngState(1),this.optimizationState.currentEpoch+=1,this.materializeEmbeddingFromWasm();const{optimizationState:r}=this,{head:s,tail:o,headEmbedding:i,tailEmbedding:n,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=i[f],x=n[y],S=ft(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=ct(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=n[e],s=ft(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=ct(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,i}optimizeLayoutAsync(t=()=>!0){return new Promise((e,r)=>{const s=async()=>{try{if(this.useWasmOptimizer&&this.wasmOptimizerState){const{nEpochs:r,currentEpoch:o}=this.optimizationState;if(o>=r)return this.embedding=this.materializeEmbeddingFromWasm(),e(!0);const i=Math.max(1,this.wasmBatchSize),n=Math.min(i,r-o),h=o,a=this.optimizeLayoutBatchWasm(n);let l=!1;for(let e=h+1;e<=h+a;e++)if(!1===t(e)){l=!0;break}const u=this.optimizationState.currentEpoch===r;return l||u?(this.embedding=this.materializeEmbeddingFromWasm(),e(u)):void setTimeout(()=>s(),0)}const{nEpochs:r,currentEpoch:o}=this.optimizationState;this.embedding=this.optimizeLayoutStep(o);const i=this.optimizationState.currentEpoch,n=!1===t(i),h=i===r;if(n||h)return e(h);setTimeout(()=>s(),0)}catch(t){r(t)}};setTimeout(()=>s(),0)})}optimizeLayout(t=()=>!0){let e=!1,r=[];if(this.useWasmOptimizer&&this.wasmOptimizerState){for(;!e;){const{nEpochs:r,currentEpoch:s}=this.optimizationState,o=Math.max(1,this.wasmBatchSize),i=Math.min(o,r-s),n=s,h=this.optimizeLayoutBatchWasm(i);let a=!1;for(let e=n+1;e<=n+h;e++)if(!1===t(e)){a=!0;break}e=this.optimizationState.currentEpoch===r||a}return r=this.materializeEmbeddingFromWasm(),r}for(;!e;){const{nEpochs:s,currentEpoch:o}=this.optimizationState;r=this.optimizeLayoutStep(o);const i=this.optimizationState.currentEpoch,n=!1===t(i);e=i===s||n}return r}optimizeLayoutBatchWasm(t){if(!this.wasmOptimizerState)throw new Error("WASM optimizer state is not initialized.");const e=this.optimizationState.nEpochs-this.optimizationState.currentEpoch,r=Math.min(t,e);return r<=0?0:(function(t,e,r){if(!T)throw new Error("WASM module not initialized");T.optimize_layout_batch_in_place(t,e,r)}(this.wasmOptimizerState,this.rngState,r),this.advanceRngState(r),this.optimizationState.currentEpoch+=r,r)}materializeEmbeddingFromWasm(){if(!this.wasmOptimizerState)return this.embedding;const{dim:t,nVertices:e}=this.optimizationState,r=function(t){if(!T)throw new Error("WASM module not initialized");const e=t.head_embedding_ptr(),r=t.head_embedding_len(),s=C?.memory??T?.memory??T?.__wasm?.memory;if(!s?.buffer)throw new Error("WASM memory is not available");return new Float64Array(s.buffer,e,r)}(this.wasmOptimizerState),s=new Array(e);for(let o=0;o<e;o++){const e=new Array(t),i=o*t;for(let s=0;s<t;s++)e[s]=r[i+s];s[o]=e}return this.embedding=s,s}advanceRngState(t){const e=BigInt("6364136223846793005"),r=BigInt("1442695040888963407");for(let s=0;s<t;s++)this.rngState=this.rngState*e+r&BigInt("0xFFFFFFFFFFFFFFFF")}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 lt(t,e){let r=0;for(let s=0;s<t.length;s++)r+=(t[s]-e[s])**2;return Math.sqrt(r)}class ut{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 ct(t,e){return t>e?e:t<-e?-e:t}function ft(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.4.2",
3
+ "version": "0.4.4",
4
4
  "description": "JavaScript implementation of UMAP",
5
5
  "author": {
6
6
  "name": "Elar Saks",
@@ -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/web && wasm-pack build --target nodejs --out-dir pkg/node && rm -f pkg/web/.gitignore pkg/node/.gitignore"
39
+ "build:wasm": "cd wasm && RUSTFLAGS='-C target-feature=+simd128' wasm-pack build --target web --out-dir pkg/web --release && wasm-opt -O4 -o pkg/web/umap_wasm_core_bg.wasm pkg/web/umap_wasm_core_bg.wasm && RUSTFLAGS='-C target-feature=+simd128' wasm-pack build --target nodejs --out-dir pkg/node --release && wasm-opt -O4 -o pkg/node/umap_wasm_core_bg.wasm pkg/node/umap_wasm_core_bg.wasm && rm -f pkg/web/.gitignore pkg/node/.gitignore"
40
40
  },
41
41
  "devDependencies": {
42
42
  "@playwright/test": "^1.57.0",
@@ -34,6 +34,14 @@ export class FlatTree {
34
34
  export class OptimizerState {
35
35
  free(): void;
36
36
  [Symbol.dispose](): void;
37
+ /**
38
+ * Get the length of the embedding buffer.
39
+ */
40
+ head_embedding_len(): number;
41
+ /**
42
+ * Get a pointer to the embedding buffer (for zero-copy views).
43
+ */
44
+ head_embedding_ptr(): number;
37
45
  /**
38
46
  * Create a new optimizer state with the given parameters.
39
47
  */
@@ -216,6 +224,11 @@ export function nn_descent(data_flat: Float64Array, n_samples: number, dim: numb
216
224
  */
217
225
  export function optimize_layout_batch(state: OptimizerState, rng_seed: bigint, n_steps: number): Float64Array;
218
226
 
227
+ /**
228
+ * Perform multiple optimization steps in place without cloning the embedding.
229
+ */
230
+ export function optimize_layout_batch_in_place(state: OptimizerState, rng_seed: bigint, n_steps: number): void;
231
+
219
232
  /**
220
233
  * Perform a single optimization step for UMAP layout.
221
234
  *
@@ -228,6 +241,11 @@ export function optimize_layout_batch(state: OptimizerState, rng_seed: bigint, n
228
241
  */
229
242
  export function optimize_layout_step(state: OptimizerState, rng_seed: bigint): Float64Array;
230
243
 
244
+ /**
245
+ * Perform a single optimization step in place without cloning the embedding.
246
+ */
247
+ export function optimize_layout_step_in_place(state: OptimizerState, rng_seed: bigint): void;
248
+
231
249
  /**
232
250
  * Search a flattened tree to find the leaf containing the query point.
233
251
  *
@@ -261,6 +261,22 @@ class OptimizerState {
261
261
  wasm.__wbindgen_free(ret[0], ret[1] * 8, 8);
262
262
  return v1;
263
263
  }
264
+ /**
265
+ * Get the length of the embedding buffer.
266
+ * @returns {number}
267
+ */
268
+ head_embedding_len() {
269
+ const ret = wasm.optimizerstate_head_embedding_len(this.__wbg_ptr);
270
+ return ret >>> 0;
271
+ }
272
+ /**
273
+ * Get a pointer to the embedding buffer (for zero-copy views).
274
+ * @returns {number}
275
+ */
276
+ head_embedding_ptr() {
277
+ const ret = wasm.optimizerstate_head_embedding_ptr(this.__wbg_ptr);
278
+ return ret >>> 0;
279
+ }
264
280
  /**
265
281
  * Create a new optimizer state with the given parameters.
266
282
  * @param {Uint32Array} head
@@ -658,6 +674,18 @@ function optimize_layout_batch(state, rng_seed, n_steps) {
658
674
  }
659
675
  exports.optimize_layout_batch = optimize_layout_batch;
660
676
 
677
+ /**
678
+ * Perform multiple optimization steps in place without cloning the embedding.
679
+ * @param {OptimizerState} state
680
+ * @param {bigint} rng_seed
681
+ * @param {number} n_steps
682
+ */
683
+ function optimize_layout_batch_in_place(state, rng_seed, n_steps) {
684
+ _assertClass(state, OptimizerState);
685
+ wasm.optimize_layout_batch_in_place(state.__wbg_ptr, rng_seed, n_steps);
686
+ }
687
+ exports.optimize_layout_batch_in_place = optimize_layout_batch_in_place;
688
+
661
689
  /**
662
690
  * Perform a single optimization step for UMAP layout.
663
691
  *
@@ -680,6 +708,17 @@ function optimize_layout_step(state, rng_seed) {
680
708
  }
681
709
  exports.optimize_layout_step = optimize_layout_step;
682
710
 
711
+ /**
712
+ * Perform a single optimization step in place without cloning the embedding.
713
+ * @param {OptimizerState} state
714
+ * @param {bigint} rng_seed
715
+ */
716
+ function optimize_layout_step_in_place(state, rng_seed) {
717
+ _assertClass(state, OptimizerState);
718
+ wasm.optimize_layout_step_in_place(state.__wbg_ptr, rng_seed);
719
+ }
720
+ exports.optimize_layout_step_in_place = optimize_layout_step_in_place;
721
+
683
722
  /**
684
723
  * Search a flattened tree to find the leaf containing the query point.
685
724
  *
@@ -15,9 +15,13 @@ export const flattree_n_nodes: (a: number) => number;
15
15
  export const flattree_offsets: (a: number) => any;
16
16
  export const nn_descent: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number, l: number, m: number, n: number, o: number, p: number, q: bigint) => [number, number, number, number];
17
17
  export const optimize_layout_batch: (a: number, b: bigint, c: number) => [number, number];
18
+ export const optimize_layout_batch_in_place: (a: number, b: bigint, c: number) => void;
18
19
  export const optimize_layout_step: (a: number, b: bigint) => [number, number];
20
+ export const optimize_layout_step_in_place: (a: number, b: bigint) => void;
19
21
  export const optimizerstate_current_epoch: (a: number) => number;
20
22
  export const optimizerstate_head_embedding: (a: number) => [number, number];
23
+ export const optimizerstate_head_embedding_len: (a: number) => number;
24
+ export const optimizerstate_head_embedding_ptr: (a: number) => number;
21
25
  export const optimizerstate_new: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number, l: number, m: number, n: number, o: number, p: number, q: number, r: number, s: number, t: number) => number;
22
26
  export const search_flat_tree: (a: number, b: number, c: number, d: bigint) => [number, number];
23
27
  export const sparse_add: (a: number, b: number) => number;
@@ -34,6 +34,14 @@ export class FlatTree {
34
34
  export class OptimizerState {
35
35
  free(): void;
36
36
  [Symbol.dispose](): void;
37
+ /**
38
+ * Get the length of the embedding buffer.
39
+ */
40
+ head_embedding_len(): number;
41
+ /**
42
+ * Get a pointer to the embedding buffer (for zero-copy views).
43
+ */
44
+ head_embedding_ptr(): number;
37
45
  /**
38
46
  * Create a new optimizer state with the given parameters.
39
47
  */
@@ -216,6 +224,11 @@ export function nn_descent(data_flat: Float64Array, n_samples: number, dim: numb
216
224
  */
217
225
  export function optimize_layout_batch(state: OptimizerState, rng_seed: bigint, n_steps: number): Float64Array;
218
226
 
227
+ /**
228
+ * Perform multiple optimization steps in place without cloning the embedding.
229
+ */
230
+ export function optimize_layout_batch_in_place(state: OptimizerState, rng_seed: bigint, n_steps: number): void;
231
+
219
232
  /**
220
233
  * Perform a single optimization step for UMAP layout.
221
234
  *
@@ -228,6 +241,11 @@ export function optimize_layout_batch(state: OptimizerState, rng_seed: bigint, n
228
241
  */
229
242
  export function optimize_layout_step(state: OptimizerState, rng_seed: bigint): Float64Array;
230
243
 
244
+ /**
245
+ * Perform a single optimization step in place without cloning the embedding.
246
+ */
247
+ export function optimize_layout_step_in_place(state: OptimizerState, rng_seed: bigint): void;
248
+
231
249
  /**
232
250
  * Search a flattened tree to find the leaf containing the query point.
233
251
  *
@@ -316,9 +334,13 @@ export interface InitOutput {
316
334
  readonly flattree_offsets: (a: number) => any;
317
335
  readonly nn_descent: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number, l: number, m: number, n: number, o: number, p: number, q: bigint) => [number, number, number, number];
318
336
  readonly optimize_layout_batch: (a: number, b: bigint, c: number) => [number, number];
337
+ readonly optimize_layout_batch_in_place: (a: number, b: bigint, c: number) => void;
319
338
  readonly optimize_layout_step: (a: number, b: bigint) => [number, number];
339
+ readonly optimize_layout_step_in_place: (a: number, b: bigint) => void;
320
340
  readonly optimizerstate_current_epoch: (a: number) => number;
321
341
  readonly optimizerstate_head_embedding: (a: number) => [number, number];
342
+ readonly optimizerstate_head_embedding_len: (a: number) => number;
343
+ readonly optimizerstate_head_embedding_ptr: (a: number) => number;
322
344
  readonly optimizerstate_new: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number, l: number, m: number, n: number, o: number, p: number, q: number, r: number, s: number, t: number) => number;
323
345
  readonly search_flat_tree: (a: number, b: number, c: number, d: bigint) => [number, number];
324
346
  readonly sparse_add: (a: number, b: number) => number;
@@ -266,6 +266,22 @@ export class OptimizerState {
266
266
  wasm.__wbindgen_free(ret[0], ret[1] * 8, 8);
267
267
  return v1;
268
268
  }
269
+ /**
270
+ * Get the length of the embedding buffer.
271
+ * @returns {number}
272
+ */
273
+ head_embedding_len() {
274
+ const ret = wasm.optimizerstate_head_embedding_len(this.__wbg_ptr);
275
+ return ret >>> 0;
276
+ }
277
+ /**
278
+ * Get a pointer to the embedding buffer (for zero-copy views).
279
+ * @returns {number}
280
+ */
281
+ head_embedding_ptr() {
282
+ const ret = wasm.optimizerstate_head_embedding_ptr(this.__wbg_ptr);
283
+ return ret >>> 0;
284
+ }
269
285
  /**
270
286
  * Create a new optimizer state with the given parameters.
271
287
  * @param {Uint32Array} head
@@ -656,6 +672,17 @@ export function optimize_layout_batch(state, rng_seed, n_steps) {
656
672
  return v1;
657
673
  }
658
674
 
675
+ /**
676
+ * Perform multiple optimization steps in place without cloning the embedding.
677
+ * @param {OptimizerState} state
678
+ * @param {bigint} rng_seed
679
+ * @param {number} n_steps
680
+ */
681
+ export function optimize_layout_batch_in_place(state, rng_seed, n_steps) {
682
+ _assertClass(state, OptimizerState);
683
+ wasm.optimize_layout_batch_in_place(state.__wbg_ptr, rng_seed, n_steps);
684
+ }
685
+
659
686
  /**
660
687
  * Perform a single optimization step for UMAP layout.
661
688
  *
@@ -677,6 +704,16 @@ export function optimize_layout_step(state, rng_seed) {
677
704
  return v1;
678
705
  }
679
706
 
707
+ /**
708
+ * Perform a single optimization step in place without cloning the embedding.
709
+ * @param {OptimizerState} state
710
+ * @param {bigint} rng_seed
711
+ */
712
+ export function optimize_layout_step_in_place(state, rng_seed) {
713
+ _assertClass(state, OptimizerState);
714
+ wasm.optimize_layout_step_in_place(state.__wbg_ptr, rng_seed);
715
+ }
716
+
680
717
  /**
681
718
  * Search a flattened tree to find the leaf containing the query point.
682
719
  *
Binary file
@@ -15,9 +15,13 @@ export const flattree_n_nodes: (a: number) => number;
15
15
  export const flattree_offsets: (a: number) => any;
16
16
  export const nn_descent: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number, l: number, m: number, n: number, o: number, p: number, q: bigint) => [number, number, number, number];
17
17
  export const optimize_layout_batch: (a: number, b: bigint, c: number) => [number, number];
18
+ export const optimize_layout_batch_in_place: (a: number, b: bigint, c: number) => void;
18
19
  export const optimize_layout_step: (a: number, b: bigint) => [number, number];
20
+ export const optimize_layout_step_in_place: (a: number, b: bigint) => void;
19
21
  export const optimizerstate_current_epoch: (a: number) => number;
20
22
  export const optimizerstate_head_embedding: (a: number) => [number, number];
23
+ export const optimizerstate_head_embedding_len: (a: number) => number;
24
+ export const optimizerstate_head_embedding_ptr: (a: number) => number;
21
25
  export const optimizerstate_new: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number, l: number, m: number, n: number, o: number, p: number, q: number, r: number, s: number, t: number) => number;
22
26
  export const search_flat_tree: (a: number, b: number, c: number, d: bigint) => [number, number];
23
27
  export const sparse_add: (a: number, b: number) => number;