@antv/layout 1.0.0-alpha.4 → 1.0.0-alpha.6

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.
@@ -0,0 +1,28 @@
1
+ interface INode {
2
+ id: string;
3
+ x: number;
4
+ y: number;
5
+ vx: number;
6
+ vy: number;
7
+ cluster: any;
8
+ }
9
+ export default function forceInBox(): {
10
+ (alpha: number): any | undefined;
11
+ initialize(_: any): void;
12
+ template: (x: any) => string | any;
13
+ groupBy: (x: any) => ((d: INode) => any) | any;
14
+ enableGrouping: (x: any) => boolean | any;
15
+ strength: (x: any) => number | any;
16
+ centerX: (_: any) => number | any;
17
+ centerY: (_: any) => number | any;
18
+ nodes: (_: any) => INode[] | any;
19
+ links: (_: any) => any[] | any;
20
+ forceNodeSize: (_: any) => ((d: any) => number) | any;
21
+ nodeSize: (_: any) => ((d: any) => number) | any;
22
+ forceCharge: (_: any) => ((d: any) => number) | any;
23
+ forceLinkDistance: (_: any) => ((d: any) => number) | any;
24
+ forceLinkStrength: (_: any) => ((d: any) => number) | any;
25
+ offset: (_: any) => number[] | any;
26
+ getFocis: () => any;
27
+ };
28
+ export {};
@@ -0,0 +1,41 @@
1
+ import { Graph, Node, LayoutMapping, D3ForceLayoutOptions, SyncLayout } from "../types";
2
+ /**
3
+ * Layout the nodes' positions with d3's basic classic force
4
+ *
5
+ * @example
6
+ * // Assign layout options when initialization.
7
+ * const layout = new D3ForceLayout({ center: [100, 100] });
8
+ * const positions = layout.execute(graph); // { nodes: [], edges: [] }
9
+ *
10
+ * // Or use different options later.
11
+ * const layout = new D3ForceLayout({ center: [100, 100] });
12
+ * const positions = layout.execute(graph, { center: [100, 100] }); // { nodes: [], edges: [] }
13
+ *
14
+ * // If you want to assign the positions directly to the nodes, use assign method.
15
+ * layout.assign(graph, { center: [100, 100] });
16
+ */
17
+ export declare class D3ForceLayout implements SyncLayout<D3ForceLayoutOptions> {
18
+ options: D3ForceLayoutOptions;
19
+ id: string;
20
+ constructor(options?: D3ForceLayoutOptions);
21
+ /**
22
+ * Return the positions of nodes and edges(if needed).
23
+ */
24
+ execute(graph: Graph, options?: D3ForceLayoutOptions): LayoutMapping;
25
+ /**
26
+ * To directly assign the positions to the nodes.
27
+ */
28
+ assign(graph: Graph, options?: D3ForceLayoutOptions): void;
29
+ /** The sign of running */
30
+ private running;
31
+ private genericForceLayout;
32
+ /**
33
+ * Prevent overlappings.
34
+ * @param {object} simulation force simulation of d3
35
+ */
36
+ overlapProcess(simulation: any, options: {
37
+ nodeSize: number | number[] | ((d?: Node) => number) | undefined;
38
+ nodeSpacing: number | number[] | ((d?: Node) => number) | undefined;
39
+ collideStrength: number;
40
+ }): void;
41
+ }
@@ -0,0 +1,7 @@
1
+ import { Point } from "../types";
2
+ import { CalcGraph } from "./types";
3
+ export declare function forceNBody(calcGraph: CalcGraph, factor: number, coulombDisScale2: number, accMap: {
4
+ [id: string]: Point;
5
+ }): {
6
+ [id: string]: Point;
7
+ };
@@ -0,0 +1,125 @@
1
+ import { Graph, LayoutMapping, ForceLayoutOptions, SyncLayout, Point } from "../types";
2
+ import { CalcGraph, FormatedOptions } from "./types";
3
+ /**
4
+ * Layout with faster force
5
+ *
6
+ * @example
7
+ * // Assign layout options when initialization.
8
+ * const layout = new ForceLayout({ center: [100, 100] });
9
+ * const positions = layout.execute(graph); // { nodes: [], edges: [] }
10
+ *
11
+ * // Or use different options later.
12
+ * const layout = new ForceLayout({ center: [100, 100] });
13
+ * const positions = layout.execute(graph, { center: [100, 100] }); // { nodes: [], edges: [] }
14
+ *
15
+ * // If you want to assign the positions directly to the nodes, use assign method.
16
+ * layout.assign(graph, { center: [100, 100] });
17
+ */
18
+ export declare class ForceLayout implements SyncLayout<ForceLayoutOptions> {
19
+ options: ForceLayoutOptions;
20
+ id: string;
21
+ /**
22
+ * time interval for layout force animations
23
+ */
24
+ private timeInterval;
25
+ /**
26
+ * compare with minMovement to end the nodes' movement
27
+ */
28
+ private judgingDistance;
29
+ constructor(options?: ForceLayoutOptions);
30
+ /**
31
+ * Return the positions of nodes and edges(if needed).
32
+ */
33
+ execute(graph: Graph, options?: ForceLayoutOptions): LayoutMapping;
34
+ /**
35
+ * To directly assign the positions to the nodes.
36
+ */
37
+ assign(graph: Graph, options?: ForceLayoutOptions): void;
38
+ private genericForceLayout;
39
+ /**
40
+ * Format merged layout options.
41
+ * @param options merged layout options
42
+ * @param graph original graph
43
+ * @returns
44
+ */
45
+ private formatOptions;
46
+ /**
47
+ * Format centripetalOption in the option.
48
+ * @param options merged layout options
49
+ * @param calcGraph calculation graph
50
+ */
51
+ private formatCentripetal;
52
+ /**
53
+ * One iteration.
54
+ * @param calcGraph calculation graph
55
+ * @param graph origin graph
56
+ * @param iter current iteration index
57
+ * @param velMap nodes' velocity map
58
+ * @param options formatted layout options
59
+ * @returns
60
+ */
61
+ private runOneStep;
62
+ /**
63
+ * Calculate graph energy for monitoring convergence.
64
+ * @param accMap acceleration map
65
+ * @param nodes calculation nodes
66
+ * @returns energy
67
+ */
68
+ private calTotalEnergy;
69
+ /**
70
+ * Calculate the repulsive forces according to coulombs law.
71
+ * @param calcGraph calculation graph
72
+ * @param accMap acceleration map
73
+ * @param options formatted layout options
74
+ */
75
+ calRepulsive(calcGraph: CalcGraph, accMap: {
76
+ [id: string]: Point;
77
+ }, options: FormatedOptions): void;
78
+ /**
79
+ * Calculate the attractive forces according to hooks law.
80
+ * @param calcGraph calculation graph
81
+ * @param accMap acceleration map
82
+ */
83
+ calAttractive(calcGraph: CalcGraph, accMap: {
84
+ [id: string]: Point;
85
+ }): void;
86
+ /**
87
+ * Calculate the gravity forces toward center.
88
+ * @param calcGraph calculation graph
89
+ * @param graph origin graph
90
+ * @param accMap acceleration map
91
+ * @param options formatted layout options
92
+ */
93
+ calGravity(calcGraph: CalcGraph, graph: Graph, accMap: {
94
+ [id: string]: Point;
95
+ }, options: FormatedOptions): void;
96
+ /**
97
+ * Update the velocities for nodes.
98
+ * @param calcGraph calculation graph
99
+ * @param accMap acceleration map
100
+ * @param velMap velocity map
101
+ * @param options formatted layout options
102
+ * @returns
103
+ */
104
+ updateVelocity(calcGraph: CalcGraph, accMap: {
105
+ [id: string]: Point;
106
+ }, velMap: {
107
+ [id: string]: Point;
108
+ }, options: FormatedOptions): void;
109
+ /**
110
+ * Update nodes' positions.
111
+ * @param graph origin graph
112
+ * @param calcGraph calculatition graph
113
+ * @param velMap velocity map
114
+ * @param options formatted layou options
115
+ * @returns
116
+ */
117
+ updatePosition(graph: Graph, calcGraph: CalcGraph, velMap: {
118
+ [id: string]: Point;
119
+ }, options: FormatedOptions): void;
120
+ /**
121
+ * Stop the animation for no-silence force.
122
+ * TODO: confirm the controller and worker's controller
123
+ */
124
+ stop(): void;
125
+ }
@@ -0,0 +1,42 @@
1
+ import { Graph as IGraph, Node as INode, Edge as IEdge } from "@antv/graphlib";
2
+ import { CentripetalOptions, Edge, Node, EdgeData, ForceLayoutOptions, NodeData, PointTuple } from "../types";
3
+ export interface CalcNodeData extends NodeData {
4
+ x: number;
5
+ y: number;
6
+ mass: number;
7
+ nodeStrength: number;
8
+ }
9
+ export type CalcNode = INode<CalcNodeData>;
10
+ export interface CalcEdgeData extends EdgeData {
11
+ linkDistance?: number;
12
+ edgeStrength?: number;
13
+ }
14
+ export type CalcEdge = IEdge<CalcEdgeData>;
15
+ export type CalcGraph = IGraph<CalcNodeData, CalcEdgeData>;
16
+ interface FormatCentripetalOptions extends CentripetalOptions {
17
+ leaf: (node: Node, nodes: Node[], edges: Edge[]) => number;
18
+ /** Force strength for single nodes. */
19
+ single: (node: Node) => number;
20
+ /** Force strength for other nodes. */
21
+ others: (node: Node) => number;
22
+ }
23
+ export interface FormatedOptions extends ForceLayoutOptions {
24
+ width: number;
25
+ height: number;
26
+ center: PointTuple;
27
+ minMovement: number;
28
+ maxIteration: number;
29
+ factor: number;
30
+ interval: number;
31
+ damping: number;
32
+ maxSpeed: number;
33
+ coulombDisScale: number;
34
+ centripetalOptions: FormatCentripetalOptions;
35
+ nodeSize: (d?: Node) => number;
36
+ getMass: (d?: Node) => number;
37
+ nodeStrength: (d?: Node) => number;
38
+ edgeStrength: (d?: Edge) => number;
39
+ linkDistance: (edge?: Edge, source?: any, target?: any) => number;
40
+ clusterNodeStrength: (node?: Node) => number;
41
+ }
42
+ export {};
package/lib/index.d.ts CHANGED
@@ -6,3 +6,5 @@ export * from "./random";
6
6
  export * from "./mds";
7
7
  export * from "./concentric";
8
8
  export * from "./radial";
9
+ export * from "./d3Force";
10
+ export * from "./force";
package/lib/types.d.ts CHANGED
@@ -83,7 +83,6 @@ export interface GridLayoutOptions extends CommonOptions {
83
83
  rows?: number;
84
84
  cols?: number;
85
85
  sortBy?: string;
86
- columns?: number | undefined;
87
86
  position?: ((node?: Node) => {
88
87
  row?: number;
89
88
  col?: number;
@@ -112,7 +111,7 @@ export interface ConcentricLayoutOptions extends CommonOptions {
112
111
  sortBy?: string;
113
112
  width?: number;
114
113
  height?: number;
115
- nodeSpacing: number | number[] | ((node?: Node) => number) | undefined;
114
+ nodeSpacing?: number | number[] | ((node?: Node) => number) | undefined;
116
115
  }
117
116
  export interface RadialLayoutOptions extends CommonOptions {
118
117
  center?: PointTuple;
@@ -152,13 +151,13 @@ export interface DagreLayoutOptions extends CommonOptions {
152
151
  }
153
152
  export interface D3ForceLayoutOptions extends CommonOptions {
154
153
  center?: PointTuple;
155
- linkDistance?: number | ((d?: Edge) => number) | undefined;
156
- edgeStrength?: number | ((d?: Edge) => number) | undefined;
157
- nodeStrength?: number | ((d?: Node) => number) | undefined;
154
+ linkDistance?: number | ((edge?: Edge) => number) | undefined;
155
+ edgeStrength?: number | ((edge?: Edge) => number) | undefined;
156
+ nodeStrength?: number | ((node: Node, i: number, nodes: Node[]) => number) | undefined;
158
157
  preventOverlap?: boolean;
159
158
  collideStrength?: number;
160
- nodeSize?: number | number[] | ((d?: Node) => number) | undefined;
161
- nodeSpacing?: number | number[] | ((d?: Node) => number) | undefined;
159
+ nodeSize?: number | number[] | ((node?: Node) => number) | undefined;
160
+ nodeSpacing?: number | number[] | ((node?: Node) => number) | undefined;
162
161
  alpha?: number;
163
162
  alphaDecay?: number;
164
163
  alphaMin?: number;
@@ -193,8 +192,8 @@ export interface ForceLayoutOptions extends CommonOptions {
193
192
  nodeStrength?: number | ((d?: Node) => number) | undefined;
194
193
  edgeStrength?: number | ((d?: Edge) => number) | undefined;
195
194
  preventOverlap?: boolean;
196
- nodeSize?: number | number[] | ((d?: Node) => number) | undefined;
197
- nodeSpacing?: number | number[] | ((d?: Node) => number) | undefined;
195
+ nodeSize?: number | number[] | ((d?: Node) => number);
196
+ nodeSpacing?: number | ((d?: Node) => number);
198
197
  minMovement?: number;
199
198
  maxIteration?: number;
200
199
  damping?: number;
@@ -202,6 +201,7 @@ export interface ForceLayoutOptions extends CommonOptions {
202
201
  coulombDisScale?: number;
203
202
  gravity?: number;
204
203
  factor?: number;
204
+ interval?: number;
205
205
  centripetalOptions?: CentripetalOptions;
206
206
  leafCluster?: boolean;
207
207
  clustering?: boolean;
@@ -211,12 +211,12 @@ export interface ForceLayoutOptions extends CommonOptions {
211
211
  distanceThresholdMode?: "mean" | "max" | "min";
212
212
  animate?: boolean;
213
213
  onTick?: (data: LayoutMapping) => void;
214
- getMass?: ((d?: Node) => number) | undefined;
214
+ getMass?: ((d: Node) => number) | undefined;
215
215
  getCenter?: ((d?: Node, degree?: number) => number[]) | undefined;
216
216
  monitor?: (params: {
217
217
  energy: number;
218
218
  nodes: Node[];
219
- edge: Edge[];
219
+ edges: Edge[];
220
220
  iterations: number;
221
221
  }) => void;
222
222
  }
@@ -1,6 +1,20 @@
1
+ import { Node } from "../types";
1
2
  export declare const isFunction: (val: unknown) => val is Function;
2
- export declare const getFunc: (value: number, defaultValue: number, func?: ((d?: any) => number) | undefined) => Function;
3
- export declare const getFuncByUnknownType: (defaultValue: number, value?: number | number[] | {
3
+ /**
4
+ * Format value with multiple types into a function returns number.
5
+ * @param defaultValue default value when value is invalid
6
+ * @param value value to be formatted
7
+ * @returns formatted result, a function returns number
8
+ */
9
+ export declare function formatNumberFn<T = unknown>(defaultValue: number, value: number | ((d?: T) => number) | undefined): (d?: T | undefined) => number;
10
+ /**
11
+ * Format size config with multiple types into a function returns number
12
+ * @param defaultValue default value when value is invalid
13
+ * @param value value to be formatted
14
+ * @param resultIsNumber whether returns number
15
+ * @returns formatted result, a function returns number
16
+ */
17
+ export declare function formatSizeFn<T extends Node>(defaultValue: number, value?: number | number[] | {
4
18
  width: number;
5
19
  height: number;
6
- } | ((d?: any) => number) | undefined, resultIsNumber?: boolean) => (d?: any) => number | number[];
20
+ } | ((d?: T) => number) | undefined, resultIsNumber?: boolean): (d: T) => number | number[];
package/lib/util/gpu.d.ts CHANGED
@@ -1,11 +1,4 @@
1
1
  import { OutNode, Edge } from "../types";
2
- /**
3
- * 将 number | Function 类型的参数转换为 return number 的 Function
4
- * @param {number | Function} value 需要被转换的值
5
- * @param {number} defaultV 返回函数的默认返回值
6
- * @return {Function} 转换后的函数
7
- */
8
- export declare const proccessToFunc: (value: number | Function | undefined, defaultV?: number) => (d?: any) => number;
9
2
  /**
10
3
  * 将节点和边数据转换为 GPU 可读的数组。并返回 maxEdgePerVetex,每个节点上最多的边数
11
4
  * @param {NodeConfig[]} nodes 需要被转换的值
@@ -1,8 +1,4 @@
1
- import type { Matrix, IndexMap, Edge, Node, OutNode, Degree } from "../types";
2
- export declare const getDegree: (n: number, nodeIdxMap: IndexMap, edges: Edge[] | null) => Degree[];
3
- export declare const getDegreeMap: (nodes: Node[], edges: Edge[] | null) => {
4
- [id: string]: Degree;
5
- };
1
+ import type { Matrix, Edge, Node, OutNode, Degree, Point } from "../types";
6
2
  export declare const floydWarshall: (adjMatrix: Matrix[]) => Matrix[];
7
3
  export declare const getAdjMatrix: (data: {
8
4
  nodes: Node[];
@@ -50,3 +46,10 @@ export declare const getCoreNodeAndRelativeLeafNodes: (type: "leaf" | "all", nod
50
46
  relativeLeafNodes: Node[];
51
47
  sameTypeLeafNodes: Node[];
52
48
  };
49
+ /**
50
+ * calculate the euclidean distance form p1 to p2
51
+ * @param p1
52
+ * @param p2
53
+ * @returns
54
+ */
55
+ export declare const getEuclideanDistance: (p1: Point, p2: Point) => number;
@@ -1,2 +1,9 @@
1
+ import { Node, Edge } from '../types';
1
2
  export declare const isObject: (val: unknown) => val is Record<any, any>;
2
3
  export declare const clone: <T>(target: T) => T;
4
+ /**
5
+ * Clone node or edge data and format it
6
+ * @param target
7
+ * @returns
8
+ */
9
+ export declare const cloneFormatData: <T extends Node | Edge>(target: T) => T;
package/lib/worker.d.ts CHANGED
@@ -1,3 +1,2 @@
1
1
  import type { Payload } from "./supervisor";
2
- import type { LayoutMapping } from "./types";
3
- export declare function calculateLayout(payload: Payload, transferables: Float32Array[]): (LayoutMapping | Float32Array[])[];
2
+ export declare function calculateLayout(payload: Payload, transferables: Float32Array[]): Promise<unknown>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@antv/layout",
3
- "version": "1.0.0-alpha.4",
3
+ "version": "1.0.0-alpha.6",
4
4
  "description": "graph layout algorithm",
5
5
  "main": "dist/index.min.js",
6
6
  "module": "esm/index.esm.js",
@@ -21,8 +21,10 @@
21
21
  "antv"
22
22
  ],
23
23
  "dependencies": {
24
- "@antv/graphlib": "^2.0.0-alpha.1",
24
+ "@antv/graphlib": "^2.0.0-alpha.2",
25
25
  "@naoak/workerize-transferable": "^0.1.0",
26
+ "d3-force": "^3.0.0",
27
+ "d3-quadtree": "^3.0.1",
26
28
  "eventemitter3": "^4.0.0",
27
29
  "ml-matrix": "^6.10.4"
28
30
  },
@@ -1,2 +0,0 @@
1
- (()=>{"use strict";var t={d:(e,r)=>{for(var o in r)t.o(r,o)&&!t.o(e,o)&&Object.defineProperty(e,o,{enumerable:!0,get:r[o]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)},e={};t.d(e,{calculateLayout:()=>yt});class r{nodeMap=new Map;edgeMap=new Map;inEdgesMap=new Map;outEdgesMap=new Map;treeIndices=new Map;changes=[];batchCount=0;onChanged=()=>{};constructor(t){t&&(t.nodes&&this.addNodes(t.nodes),t.edges&&this.addEdges(t.edges),t.tree&&this.addTree(t.tree),t.onChanged&&(this.onChanged=t.onChanged))}batch=t=>{this.batchCount+=1,t(),this.batchCount-=1,this.batchCount||this.commit()};commit(){const t=this.changes;this.changes=[],this.onChanged({graph:this,changes:t})}reduceChanges(t){let e=[];return t.forEach((t=>{switch(t.type){case"NodeRemoved":{let r=!1;e=e.filter((e=>{if("NodeAdded"===e.type){const o=e.value.id===t.value.id;return o&&(r=!0),!o}return"NodeDataUpdated"===e.type?e.id!==t.value.id:"TreeStructureChanged"!==e.type||e.nodeId!==t.value.id})),r||e.push(t);break}case"EdgeRemoved":{let r=!1;e=e.filter((e=>{if("EdgeAdded"===e.type){const o=e.value.id===t.value.id;return o&&(r=!0),!o}return"EdgeDataUpdated"!==e.type&&"EdgeUpdated"!==e.type||e.id!==t.value.id})),r||e.push(t);break}case"NodeDataUpdated":case"EdgeDataUpdated":case"EdgeUpdated":{const r=e.find((e=>e.type===t.type&&e.id===t.id&&e.propertyName===t.propertyName));r?r.newValue=t.newValue:e.push(t);break}case"TreeStructureDetached":e=e.filter((e=>"TreeStructureAttached"===e.type?e.treeKey!==t.treeKey:"TreeStructureChanged"!==e.type||e.treeKey!==t.treeKey)),e.push(t);break;case"TreeStructureChanged":{const r=e.find((e=>"TreeStructureChanged"===e.type&&e.treeKey===t.treeKey&&e.nodeId===t.nodeId));r?r.newParentId=t.newParentId:e.push(t);break}default:e.push(t)}})),e}checkNodeExistence(t){if(!this.hasNode(t))throw new Error("Node not found for id: "+t)}hasNode(t){return this.nodeMap.has(t)}areNeighbors(t,e){return this.checkNodeExistence(t),this.getNeighbors(e).some((e=>e.id===t))}getNode(t){return this.checkNodeExistence(t),this.nodeMap.get(t)}getRelatedEdges(t,e){this.checkNodeExistence(t);const r=this.inEdgesMap.get(t),o=this.outEdgesMap.get(t);if("in"===e)return Array.from(r);if("out"===e)return Array.from(o);const s=new Set([...r,...o]);return Array.from(s)}getDegree(t,e){return this.getRelatedEdges(t,e).length}getSuccessors(t){const e=this.getRelatedEdges(t,"out").map((t=>t.target));return Array.from(new Set(e)).map((t=>this.getNode(t)))}getPredecessors(t){const e=this.getRelatedEdges(t,"in").map((t=>t.source));return Array.from(new Set(e)).map((t=>this.getNode(t)))}getNeighbors(t){const e=this.getPredecessors(t),r=this.getSuccessors(t);return Array.from(new Set([...e,...r]))}doAddNode(t){if(this.hasNode(t.id))throw new Error("Node already exists: "+t.id);this.nodeMap.set(t.id,t),this.inEdgesMap.set(t.id,new Set),this.outEdgesMap.set(t.id,new Set),this.treeIndices.forEach((e=>{e.childrenMap.set(t.id,new Set)})),this.changes.push({type:"NodeAdded",value:t})}addNodes(t){this.batch((()=>{for(const e of t)this.doAddNode(e)}))}addNode(t){this.addNodes([t])}doRemoveNode(t){const e=this.getNode(t),r=this.inEdgesMap.get(t),o=this.outEdgesMap.get(t);r?.forEach((t=>this.doRemoveEdge(t.id))),o?.forEach((t=>this.doRemoveEdge(t.id))),this.nodeMap.delete(t),this.treeIndices.forEach((e=>{e.childrenMap.get(t)?.forEach((t=>{e.parentMap.delete(t.id)})),e.parentMap.delete(t),e.childrenMap.delete(t)})),this.changes.push({type:"NodeRemoved",value:e})}removeNodes(t){this.batch((()=>{t.forEach((t=>this.doRemoveNode(t)))}))}removeNode(t){this.removeNodes([t])}updateNodeData(t,e,r){const o=this.getNode(t);this.batch((()=>{const s=o.data[e],n=r;o.data[e]=n,this.changes.push({type:"NodeDataUpdated",id:t,propertyName:e,oldValue:s,newValue:n})}))}mergeNodeData(t,e){this.batch((()=>{Object.entries(e).forEach((([e,r])=>{this.updateNodeData(t,e,r)}))}))}checkEdgeExistence(t){if(!this.hasEdge(t))throw new Error("Edge not found for id: "+t)}hasEdge(t){return this.edgeMap.has(t)}getEdge(t){return this.checkEdgeExistence(t),this.edgeMap.get(t)}getEdgeDetail(t){const e=this.getEdge(t);return{edge:e,source:this.getNode(e.source),target:this.getNode(e.target)}}doAddEdge(t){if(this.hasEdge(t.id))throw new Error("Edge already exists: "+t.id);this.checkNodeExistence(t.source),this.checkNodeExistence(t.target),this.edgeMap.set(t.id,t);const e=this.inEdgesMap.get(t.target),r=this.outEdgesMap.get(t.source);e.add(t),r.add(t),this.changes.push({type:"EdgeAdded",value:t})}addEdges(t){this.batch((()=>{for(const e of t)this.doAddEdge(e)}))}addEdge(t){this.addEdges([t])}doRemoveEdge(t){const e=this.getEdge(t),r=this.outEdgesMap.get(e.source),o=this.inEdgesMap.get(e.target);r.delete(e),o.delete(e),this.edgeMap.delete(t),this.changes.push({type:"EdgeRemoved",value:e})}removeEdges(t){this.batch((()=>{t.forEach((t=>this.doRemoveEdge(t)))}))}removeEdge(t){this.removeEdges([t])}updateEdgeSource(t,e){const r=this.getEdge(t);this.checkNodeExistence(e);const o=r.source,s=e;this.outEdgesMap.get(o).delete(r),this.outEdgesMap.get(s).add(r),r.source=e,this.batch((()=>{this.changes.push({type:"EdgeUpdated",id:t,propertyName:"source",oldValue:o,newValue:s})}))}updateEdgeTarget(t,e){const r=this.getEdge(t);this.checkNodeExistence(e);const o=r.target,s=e;this.inEdgesMap.get(o).delete(r),this.inEdgesMap.get(s).add(r),r.target=e,this.batch((()=>{this.changes.push({type:"EdgeUpdated",id:t,propertyName:"target",oldValue:o,newValue:s})}))}updateEdgeData(t,e,r){const o=this.getEdge(t);this.batch((()=>{const s=o.data[e],n=r;o.data[e]=n,this.changes.push({type:"EdgeDataUpdated",id:t,propertyName:e,oldValue:s,newValue:n})}))}mergeEdgeData(t,e){this.batch((()=>{Object.entries(e).forEach((([e,r])=>{this.updateEdgeData(t,e,r)}))}))}checkTreeExistence(t){if(!this.treeIndices.has(t))throw new Error("Tree structure not found for treeKey: "+t)}attachTreeStructure(t){this.treeIndices.has(t)||(this.treeIndices.set(t,{parentMap:new Map,childrenMap:new Map}),this.batch((()=>{this.changes.push({type:"TreeStructureAttached",treeKey:t})})))}detachTreeStructure(t){this.checkTreeExistence(t),this.treeIndices.delete(t),this.batch((()=>{this.changes.push({type:"TreeStructureDetached",treeKey:t})}))}addTree(t,e){this.batch((()=>{this.attachTreeStructure(e);const r=[],o=Array.isArray(t)?t:[t];for(;o.length;){const t=o.shift();r.push(t),t.children&&o.push(...t.children)}this.addNodes(r),r.forEach((t=>{t.children?.forEach((r=>{this.setParent(r.id,t.id,e)}))}))}))}getRoots(t){return this.checkTreeExistence(t),this.getAllNodes().filter((e=>!this.getParent(e.id,t)))}getChildren(t,e){this.checkNodeExistence(t),this.checkTreeExistence(e);const r=this.treeIndices.get(e).childrenMap.get(t);return Array.from(r||[])}getParent(t,e){return this.checkNodeExistence(t),this.checkTreeExistence(e),this.treeIndices.get(e).parentMap.get(t)||null}setParent(t,e,r){this.checkTreeExistence(r);const o=this.treeIndices.get(r),s=this.getNode(t),n=o.parentMap.get(t),i=this.getNode(e);o.parentMap.set(t,i),n&&o.childrenMap.get(n.id)?.delete(s);let a=o.childrenMap.get(i.id);a||(a=new Set,o.childrenMap.set(i.id,a)),a.add(s),this.batch((()=>{this.changes.push({type:"TreeStructureChanged",treeKey:r,nodeId:t,oldParentId:n?.id,newParentId:i.id})}))}getAllNodes(){return Array.from(this.nodeMap.values())}getAllEdges(){return Array.from(this.edgeMap.values())}doBFS(t,e,r){for(;t.length;){const o=t.shift();r(o),e.add(o.id),this.getSuccessors(o.id).forEach((r=>{e.has(r.id)||(e.add(r.id),t.push(r))}))}}bfs(t,e){this.doBFS([this.getNode(t)],new Set,e)}doDFS(t,e,r){r(t),e.add(t.id),this.getSuccessors(t.id).forEach((t=>{e.has(t.id)||this.doDFS(t,e,r)}))}dfs(t,e){this.doDFS(this.getNode(t),new Set,e)}clone(){const t=this.getAllNodes().map((t=>({...t,data:{...t.data}}))),e=this.getAllEdges().map((t=>({...t,data:{...t.data}}))),o=new r({nodes:t,edges:e});return this.treeIndices.forEach((({parentMap:t,childrenMap:e},r)=>{const s=new Map;t.forEach(((t,e)=>{s.set(e,o.getNode(t.id))}));const n=new Map;e.forEach(((t,e)=>{n.set(e,new Set(Array.from(t).map((t=>o.getNode(t.id)))))})),o.treeIndices.set(r,{parentMap:s,childrenMap:n})})),o}toJSON(){return JSON.stringify({nodes:this.getAllNodes(),edges:this.getAllEdges()})}}var o=function(){return o=Object.assign||function(t){for(var e,r=1,o=arguments.length;r<o;r++)for(var s in e=arguments[r])Object.prototype.hasOwnProperty.call(e,s)&&(t[s]=e[s]);return t},o.apply(this,arguments)};function s(t,e){var r="function"==typeof Symbol&&t[Symbol.iterator];if(!r)return t;var o,s,n=r.call(t),i=[];try{for(;(void 0===e||e-- >0)&&!(o=n.next()).done;)i.push(o.value)}catch(t){s={error:t}}finally{try{o&&!o.done&&(r=n.return)&&r.call(n)}finally{if(s)throw s.error}}return i}function n(t,e,r){if(r||2===arguments.length)for(var o,s=0,n=e.length;s<n;s++)!o&&s in e||(o||(o=Array.prototype.slice.call(e,0,s)),o[s]=e[s]);return t.concat(o||Array.prototype.slice.call(e))}Object.create,Object.create;var i=function(t){return"string"==typeof t},a=/-(\w)/g,h=(function(t){return t.replace(a,(function(t,e){return e?e.toUpperCase():""}))},Object.create(null),Array.isArray),u=function(t){return"number"==typeof t},l=function(t){for(var e=[],r=t.length,o=0;o<r;o+=1){e[o]=[];for(var s=0;s<r;s+=1)o===s?e[o][s]=0:0!==t[o][s]&&t[o][s]?e[o][s]=t[o][s]:e[o][s]=1/0}for(var n=0;n<r;n+=1)for(o=0;o<r;o+=1)for(s=0;s<r;s+=1)e[o][s]>e[o][n]+e[n][s]&&(e[o][s]=e[o][n]+e[n][s]);return e},c=function(t,e){var r=t.nodes,o=t.edges,s=[],n={};if(!r)throw new Error("invalid nodes data!");return r&&r.forEach((function(t,e){n[t.id]=e,s.push([])})),null==o||o.forEach((function(t){var r=t.source,o=t.target,i=n[r],a=n[o];void 0!==i&&void 0!==a&&(s[i][a]=1,e||(s[a][i]=1))})),s},f=function(t){return null!==t&&"object"==typeof t},d=function(t){if(null===t)return t;if(t instanceof Date)return new Date(t.getTime());if(t instanceof Array){var e=[];return t.forEach((function(t){e.push(t)})),e.map((function(t){return d(t)}))}if("object"==typeof t&&Object.keys(t).length){var r=o({},t);return Object.keys(r).forEach((function(t){r[t]=d(r[t])})),r}return t},g=function(t){return"function"==typeof t},m=function(t,e,r){return void 0===r&&(r=!0),e||0===e?g(e)?e:u(e)?function(){return e}:h(e)?function(){if(r){var o=Math.max.apply(Math,n([],s(e),!1));return isNaN(o)?t:o}return e}:f(e)?function(){if(r){var o=Math.max(e.width,e.height);return isNaN(o)?t:o}return[e.width,e.height]}:function(){return t}:function(e){return e.size?h(e.size)?e.size[0]>e.size[1]?e.size[0]:e.size[1]:f(e.size)?e.size.width>e.size.height?e.size.width:e.size.height:e.size:t}},p={radius:null,startRadius:null,endRadius:null,startAngle:0,endAngle:2*Math.PI,clockwise:!0,divisions:1,ordering:null,angleRatio:1},w=function(){function t(t){void 0===t&&(t={}),this.options=t,this.id="circular",this.options=o(o({},p),t)}return t.prototype.execute=function(t,e){return this.genericCircularLayout(!1,t,e)},t.prototype.assign=function(t,e){var r=this;t.batch((function(){r.genericCircularLayout(!0,t,e)}))},t.prototype.genericCircularLayout=function(t,e,r){var n=o(o({},this.options),r),i=n.width,a=n.height,h=n.center,u=n.divisions,l=n.startAngle,c=void 0===l?0:l,f=n.endAngle,g=void 0===f?2*Math.PI:f,p=n.angleRatio,w=n.ordering,M=n.clockwise,b=n.nodeSpacing,E=n.nodeSize,x=n.layoutInvisibles,S=n.onLayoutEnd,N=e.getAllNodes(),R=e.getAllEdges();x||(N=N.filter((function(t){return t.data.visible||void 0===t.data.visible})),R=R.filter((function(t){return t.data.visible||void 0===t.data.visible})));var I=N.length;if(0===I)return null==S||S({nodes:[],edges:[]}),{nodes:[],edges:[]};var k=s(v(i,a,h),3),A=k[0],T=k[1],C=k[2];if(1===I){t&&e.mergeNodeData(N[0].id,{x:C[0],y:C[1]});var D={nodes:[o(o({},N[0]),{data:o(o({},N[0].data),{x:C[0],y:C[1]})})],edges:R};return null==S||S(D),D}var z=(g-c)/I,P=n.radius,V=n.startRadius,q=n.endRadius;if(b){var j=m(10,b),F=m(10,E),O=-1/0;N.forEach((function(t){var e=F(t);O<e&&(O=e)}));var L=0;N.forEach((function(t,e){L+=0===e?O||10:(j(t)||0)+(O||10)})),P=L/(2*Math.PI)}else P||V||q?!V&&q?V=q:V&&!q&&(q=V):P=Math.min(T,A)/2;var $=z*p,U=[];U="topology"===w?y(e,N):"topology-directed"===w?y(e,N,!0):"degree"===w?function(t,e){var r=[];return e.forEach((function(t,e){r.push(d(t))})),r.sort((function(e,r){return t.getDegree(e.id,"both")-t.getDegree(r.id,"both")})),r}(e,N):N.map((function(t){return d(t)}));for(var B=Math.ceil(I/u),K=0;K<I;++K){var W=P;W||null===V||null===q||(W=V+K*(q-V)/(I-1)),W||(W=10+100*K/(I-1));var J=c+K%B*$+2*Math.PI/u*Math.floor(K/B);M||(J=g-K%B*$-2*Math.PI/u*Math.floor(K/B)),U[K].data.x=C[0]+Math.cos(J)*W,U[K].data.y=C[1]+Math.sin(J)*W}t&&U.forEach((function(t){e.mergeNodeData(t.id,{x:t.data.x,y:t.data.y})}));var _={nodes:U,edges:R};return null==S||S(_),_},t}(),y=function(t,e,r){void 0===r&&(r=!1);var o=[d(e[0])],s={},n=e.length;s[e[0].id]=!0;var i=0;return e.forEach((function(a,h){if(0!==h)if(h!==n-1&&t.getDegree(a.id,"both")===t.getDegree(e[h+1].id,"both")&&!t.areNeighbors(o[i].id,a.id)||s[a.id]){for(var u=r?t.getSuccessors(o[i].id):t.getNeighbors(o[i].id),l=!1,c=0;c<u.length;c++){var f=u[c];if(t.getDegree(f.id)===t.getDegree(a.id)&&!s[f.id]){o.push(d(f)),s[f.id]=!0,l=!0;break}}for(var g=0;!l&&(s[e[g].id]||(o.push(d(e[g])),s[e[g].id]=!0,l=!0),++g!==n););}else o.push(d(a)),s[a.id]=!0,i++})),o},v=function(t,e,r){var o=t,s=e,n=r;return o||"undefined"==typeof window||(o=window.innerWidth),s||"undefined"==typeof window||(s=window.innerHeight),n||(n=[o/2,s/2]),[o,s,n]},M={nodeSize:30,minNodeSpacing:10,nodeSpacing:10,preventOverlap:!1,sweep:void 0,equidistant:!1,startAngle:1.5*Math.PI,clockwise:!0,maxLevelDiff:void 0,sortBy:"degree"},b=function(){function t(t){void 0===t&&(t={}),this.options=t,this.id="concentric",this.options=o(o({},M),t)}return t.prototype.execute=function(t,e){return this.genericConcentricLayout(!1,t,e)},t.prototype.assign=function(t,e){this.genericConcentricLayout(!0,t,e)},t.prototype.genericConcentricLayout=function(t,e,r){var s=o(o({},this.options),r),n=s.center,a=s.width,l=s.height,c=s.sortBy,m=s.maxLevelDiff,p=s.sweep,w=s.clockwise,y=s.equidistant,v=s.minNodeSpacing,M=void 0===v?10:v,b=s.preventOverlap,E=s.startAngle,x=void 0===E?1.5*Math.PI:E,S=s.nodeSize,N=s.nodeSpacing,R=s.layoutInvisibles,I=s.onLayoutEnd,k=e.getAllNodes(),A=e.getAllEdges();R||(k=k.filter((function(t){return t.data.visible||void 0===t.data.visible})),A=A.filter((function(t){return t.data.visible||void 0===t.data.visible})));var T=k.length;if(0===T){var C={nodes:[],edges:A};return null==I||I(C),C}var D=a||"undefined"==typeof window?a:window.innerWidth,z=l||"undefined"==typeof window?l:window.innerHeight,P=n||[D/2,z/2];if(1===T){t&&e.mergeNodeData(k[0].id,{x:P[0],y:P[1]});var V={nodes:[o(o({},k[0]),{data:o(o({},k[0].data),{x:P[0],y:P[1]})})],edges:A};return null==I||I(V),V}var q,j=[],F=0;q=h(S)?Math.max(S[0],S[1]):S,h(N)?F=Math.max(N[0],N[1]):u(N)&&(F=N),k.forEach((function(t){j.push(d(t));var e=q;h(t.data.size)?e=Math.max(t.data.size[0],t.data.size[1]):u(t.data.size)?e=t.data.size:f(t.data.size)&&(e=Math.max(t.data.size.width,t.data.size.height)),q=Math.max(q,e),g(N)&&(F=Math.max(N(t),F))}));var O={};j.forEach((function(t,e){O[t.id]=e}));var L=c;i(L)&&void 0!==j[0].data[L]||(L="degree"),"degree"===L?j.sort((function(t,r){return e.getDegree(r.id,"both")-e.getDegree(t.id,"both")})):j.sort((function(t,e){return e.data[L]-t.data[L]}));var $=j[0],U=m||$.data[L]/4,B=[{nodes:[]}],K=B[0];j.forEach((function(t){if(K.nodes.length>0){var e=Math.abs(K.nodes[0].data[L]-t.data[L]);U&&e>=U&&(K={nodes:[]},B.push(K))}K.nodes.push(t)}));var W=q+(F||M);if(!b){var J=B.length>0&&B[0].nodes.length>1,_=(Math.min(D,z)/2-W)/(B.length+(J?1:0));W=Math.min(W,_)}var G=0;if(B.forEach((function(t){var e=void 0===p?2*Math.PI-2*Math.PI/t.nodes.length:p;if(t.dTheta=e/Math.max(1,t.nodes.length-1),t.nodes.length>1&&b){var r=Math.cos(t.dTheta)-Math.cos(0),o=Math.sin(t.dTheta)-Math.sin(0),s=Math.sqrt(W*W/(r*r+o*o));G=Math.max(s,G)}t.r=G,G+=W})),y){for(var H=0,Y=0,Q=0;Q<B.length;Q++){var X=(B[Q].r||0)-Y;H=Math.max(H,X)}Y=0,B.forEach((function(t,e){0===e&&(Y=t.r||0),t.r=Y,Y+=H}))}B.forEach((function(t){var e=t.dTheta||0,r=t.r||0;t.nodes.forEach((function(t,o){var s=x+(w?1:-1)*e*o;t.data.x=P[0]+r*Math.cos(s),t.data.y=P[1]+r*Math.sin(s)}))})),t&&j.forEach((function(t){return e.mergeNodeData(t.id,{x:t.data.x,y:t.data.y})}));var Z={nodes:j,edges:A};return null==I||I(Z),Z},t}(),E={begin:[0,0],preventOverlap:!0,preventOverlapPadding:10,condense:!1,rows:void 0,cols:void 0,position:void 0,sortBy:"degree",nodeSize:30,width:300,height:300},x=function(){function t(t){void 0===t&&(t={}),this.options=t,this.id="grid",this.options=o(o({},E),t)}return t.prototype.execute=function(t,e){return this.genericGridLayout(!1,t,e)},t.prototype.assign=function(t,e){this.genericGridLayout(!0,t,e)},t.prototype.genericGridLayout=function(t,e,r){var s=o(o({},this.options),r),n=s.begin,a=void 0===n?[0,0]:n,l=s.condense,c=s.preventOverlapPadding,f=s.preventOverlap,g=s.columns,p=s.rows,w=s.cols,y=void 0===w?g:w,v=s.nodeSpacing,M=s.nodeSize,b=s.width,E=s.height,x=s.layoutInvisibles,k=s.onLayoutEnd,T=s.position,C=s.sortBy,D=e.getAllNodes(),z=e.getAllEdges();x||(D=D.filter((function(t){return t.data.visible||void 0===t.data.visible})),z=z.filter((function(t){return t.data.visible||void 0===t.data.visible})));var P=D.length;if(0===P){var V={nodes:[],edges:z};return null==k||k(V),V}if(1===P){t&&e.mergeNodeData(D[0].id,{x:a[0],y:a[1]});var q={nodes:[o(o({},D[0]),{data:o(o({},D[0].data),{x:a[0],y:a[1]})})],edges:z};return null==k||k(q),q}var j=D.map((function(t){return d(t)}));"id"===C||i(C)&&void 0!==j[0].data[C]||(C="degree"),"degree"===C?j.sort((function(t,r){return e.getDegree(r.id,"both")-e.getDegree(t.id,"both")})):"id"===C?j.sort((function(t,e){return u(e.id)&&u(t.id)?e.id-t.id:"".concat(e.id).localeCompare("".concat(t.id))})):j.sort((function(t,e){return e.data[C]-t.data[C]}));var F=b||"undefined"==typeof window?b:window.innerWidth,O=E||"undefined"==typeof window?E:window.innerHeight,L=P,$={rows:p,cols:y};if(null!=p&&null!=y)$.rows=p,$.cols=y;else if(null!=p&&null==y)$.rows=p,$.cols=Math.ceil(L/$.rows);else if(null==p&&null!=y)$.cols=y,$.rows=Math.ceil(L/$.cols);else{var U=Math.sqrt(L*O/F);$.rows=Math.round(U),$.cols=Math.round(F/O*U)}if($.rows=Math.max($.rows,1),$.cols=Math.max($.cols,1),$.cols*$.rows>L)((K=S($))-1)*(B=N($))>=L?S($,K-1):(B-1)*K>=L&&N($,B-1);else for(;$.cols*$.rows<L;){var B,K=S($);((B=N($))+1)*K>=L?N($,B+1):S($,K+1)}var W=l?0:F/$.cols,J=l?0:O/$.rows;if(f||v){var _=m(10,v),G=m(30,M,!1);j.forEach((function(t){t.data.x&&t.data.y||(t.data.x=0,t.data.y=0);var r,o,s=e.getNode(t.id),n=G(s)||30;h(n)?(r=n[0],o=n[1]):(r=n,o=n);var i=void 0!==_?_(t):c,a=r+i,u=o+i;W=Math.max(W,a),J=Math.max(J,u)}))}for(var H={},Y={row:0,col:0},Q={},X=0;X<j.length;X++){var Z=j[X],tt=void 0;if(T&&(tt=T(e.getNode(Z.id))),tt&&(void 0!==tt.row||void 0!==tt.col)){var et={row:tt.row,col:tt.col};if(void 0===et.col)for(et.col=0;R(H,et);)et.col++;else if(void 0===et.row)for(et.row=0;R(H,et);)et.row++;Q[Z.id]=et,I(H,et)}A(Z,a,W,J,Q,$,Y,H)}var rt={nodes:j,edges:z};return null==k||k(rt),t&&j.forEach((function(t){e.mergeNodeData(t.id,{x:t.data.x,y:t.data.y})})),rt},t}(),S=function(t,e){var r,o=t.rows||5,s=t.cols||5;return null==e?r=Math.min(o,s):Math.min(o,s)===t.rows?t.rows=e:t.cols=e,r},N=function(t,e){var r,o=t.rows||5,s=t.cols||5;return null==e?r=Math.max(o,s):Math.max(o,s)===t.rows?t.rows=e:t.cols=e,r},R=function(t,e){return t["c-".concat(e.row,"-").concat(e.col)]||!1},I=function(t,e){return t["c-".concat(e.row,"-").concat(e.col)]=!0},k=function(t,e){var r=t.cols||5;e.col++,e.col>=r&&(e.col=0,e.row++)},A=function(t,e,r,o,s,n,i,a){var h,u,l=s[t.id];if(l)h=l.col*r+r/2+e[0],u=l.row*o+o/2+e[1];else{for(;R(a,i);)k(n,i);h=i.col*r+r/2+e[0],u=i.row*o+o/2+e[1],I(a,i),k(n,i)}t.data.x=h,t.data.y=u};const T=Object.prototype.toString;function C(t){return T.call(t).endsWith("Array]")}function D(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!C(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,s=e.toIndex,n=void 0===s?t.length:s;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 i=t[o],a=o+1;a<n;a++)t[a]>i&&(i=t[a]);return i}function z(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!C(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,s=e.toIndex,n=void 0===s?t.length:s;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 i=t[o],a=o+1;a<n;a++)t[a]<i&&(i=t[a]);return i}function P(t){var e,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!C(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(!C(r.output))throw new TypeError("output option must be an array if specified");e=r.output}else e=new Array(t.length);var o=z(t),s=D(t);if(o===s)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");var n=r.min,i=void 0===n?r.autoMinMax?o:0:n,a=r.max,h=void 0===a?r.autoMinMax?s:1:a;if(i>=h)throw new RangeError("min option must be smaller than max option");for(var u=(h-i)/(s-o),l=0;l<t.length;l++)e[l]=(t[l]-o)*u+i;return e}const V=" ".repeat(2),q=" ".repeat(4);function j(t,e={}){const{maxRows:r=15,maxColumns:o=10,maxNumSize:s=8,padMinus:n="auto"}=e;return`${t.constructor.name} {\n${V}[\n${q}${function(t,e,r,o,s){const{rows:n,columns:i}=t,a=Math.min(n,e),h=Math.min(i,r),u=[];if("auto"===s){s=!1;t:for(let e=0;e<a;e++)for(let r=0;r<h;r++)if(t.get(e,r)<0){s=!0;break t}}for(let e=0;e<a;e++){let r=[];for(let n=0;n<h;n++)r.push(F(t.get(e,n),o,s));u.push(`${r.join(" ")}`)}return h!==i&&(u[u.length-1]+=` ... ${i-r} more columns`),a!==n&&u.push(`... ${n-e} more rows`),u.join(`\n${q}`)}(t,r,o,s,n)}\n${V}]\n${V}rows: ${t.rows}\n${V}columns: ${t.columns}\n}`}function F(t,e,r){return(t>=0&&r?` ${O(t,e-1)}`:O(t,e)).padEnd(e)}function O(t,e){let r=t.toString();if(r.length<=e)return r;let o=t.toFixed(e);if(o.length>e&&(o=t.toFixed(Math.max(0,e-(o.length-e)))),o.length<=e&&!o.startsWith("0.000")&&!o.startsWith("-0.000"))return o;let s=t.toExponential(e);return s.length>e&&(s=t.toExponential(Math.max(0,e-(s.length-e)))),s.slice(0)}function L(t,e,r){let o=r?t.rows:t.rows-1;if(e<0||e>o)throw new RangeError("Row index out of range")}function $(t,e,r){let o=r?t.columns:t.columns-1;if(e<0||e>o)throw new RangeError("Column index out of range")}function U(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 B(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 K(t,e,r,o,s){if(5!==arguments.length)throw new RangeError("expected 4 arguments");if(J("startRow",e),J("endRow",r),J("startColumn",o),J("endColumn",s),e>r||o>s||e<0||e>=t.rows||r<0||r>=t.rows||o<0||o>=t.columns||s<0||s>=t.columns)throw new RangeError("Submatrix indices are out of range")}function W(t,e=0){let r=[];for(let o=0;o<t;o++)r.push(e);return r}function J(t,e){if("number"!=typeof e)throw new TypeError(`${t} must be a number`)}function _(t){if(t.isEmpty())throw new Error("Empty matrix has no elements to index")}class G{static from1DArray(t,e,r){if(t*e!==r.length)throw new RangeError("data length does not match given dimensions");let o=new Y(t,e);for(let s=0;s<t;s++)for(let t=0;t<e;t++)o.set(s,t,r[s*e+t]);return o}static rowVector(t){let e=new Y(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 Y(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 Y(t,e)}static ones(t,e){return new Y(t,e).fill(1)}static rand(t,e,r={}){if("object"!=typeof r)throw new TypeError("options must be an object");const{random:o=Math.random}=r;let s=new Y(t,e);for(let r=0;r<t;r++)for(let t=0;t<e;t++)s.set(r,t,o());return s}static randInt(t,e,r={}){if("object"!=typeof r)throw new TypeError("options must be an object");const{min:o=0,max:s=1e3,random:n=Math.random}=r;if(!Number.isInteger(o))throw new TypeError("min must be an integer");if(!Number.isInteger(s))throw new TypeError("max must be an integer");if(o>=s)throw new RangeError("min must be smaller than max");let i=s-o,a=new Y(t,e);for(let r=0;r<t;r++)for(let t=0;t<e;t++){let e=o+Math.round(n()*i);a.set(r,t,e)}return a}static eye(t,e,r){void 0===e&&(e=t),void 0===r&&(r=1);let o=Math.min(t,e),s=this.zeros(t,e);for(let t=0;t<o;t++)s.set(t,t,r);return s}static diag(t,e,r){let o=t.length;void 0===e&&(e=o),void 0===r&&(r=e);let s=Math.min(o,e,r),n=this.zeros(e,r);for(let e=0;e<s;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,o=t.columns,s=new Y(r,o);for(let n=0;n<r;n++)for(let r=0;r<o;r++)s.set(n,r,Math.min(t.get(n,r),e.get(n,r)));return s}static max(t,e){t=this.checkMatrix(t),e=this.checkMatrix(e);let r=t.rows,o=t.columns,s=new this(r,o);for(let n=0;n<r;n++)for(let r=0;r<o;r++)s.set(n,r,Math.max(t.get(n,r),e.get(n,r)));return s}static checkMatrix(t){return G.isMatrix(t)?t:new Y(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}isEchelonForm(){let t=0,e=0,r=-1,o=!0,s=!1;for(;t<this.rows&&o;){for(e=0,s=!1;e<this.columns&&!1===s;)0===this.get(t,e)?e++:1===this.get(t,e)&&e>r?(s=!0,r=e):(o=!1,s=!0);t++}return o}isReducedEchelonForm(){let t=0,e=0,r=-1,o=!0,s=!1;for(;t<this.rows&&o;){for(e=0,s=!1;e<this.columns&&!1===s;)0===this.get(t,e)?e++:1===this.get(t,e)&&e>r?(s=!0,r=e):(o=!1,s=!0);for(let r=e+1;r<this.rows;r++)0!==this.get(t,r)&&(o=!1);t++}return o}echelonForm(){let t=this.clone(),e=0,r=0;for(;e<t.rows&&r<t.columns;){let o=e;for(let s=e;s<t.rows;s++)t.get(s,r)>t.get(o,r)&&(o=s);if(0===t.get(o,r))r++;else{t.swapRows(e,o);let s=t.get(e,r);for(let o=r;o<t.columns;o++)t.set(e,o,t.get(e,o)/s);for(let o=e+1;o<t.rows;o++){let s=t.get(o,r)/t.get(e,r);t.set(o,r,0);for(let n=r+1;n<t.columns;n++)t.set(o,n,t.get(o,n)-t.get(e,n)*s)}e++,r++}}return t}reducedEchelonForm(){let t=this.echelonForm(),e=t.columns,r=t.rows,o=r-1;for(;o>=0;)if(0===t.maxRow(o))o--;else{let s=0,n=!1;for(;s<r&&!1===n;)1===t.get(o,s)?n=!0:s++;for(let r=0;r<o;r++){let n=t.get(r,s);for(let i=s;i<e;i++){let e=t.get(r,i)-n*t.get(o,i);t.set(r,i,e)}}o--}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 o=new Y(this.rows*e,this.columns*r);for(let t=0;t<e;t++)for(let e=0;e<r;e++)o.setSubMatrix(this,this.rows*t,this.columns*e);return o}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){L(this,t);let e=[];for(let r=0;r<this.columns;r++)e.push(this.get(t,r));return e}getRowVector(t){return Y.rowVector(this.getRow(t))}setRow(t,e){L(this,t),e=U(this,e);for(let r=0;r<this.columns;r++)this.set(t,r,e[r]);return this}swapRows(t,e){L(this,t),L(this,e);for(let r=0;r<this.columns;r++){let o=this.get(t,r);this.set(t,r,this.get(e,r)),this.set(e,r,o)}return this}getColumn(t){$(this,t);let e=[];for(let r=0;r<this.rows;r++)e.push(this.get(r,t));return e}getColumnVector(t){return Y.columnVector(this.getColumn(t))}setColumn(t,e){$(this,t),e=B(this,e);for(let r=0;r<this.rows;r++)this.set(r,t,e[r]);return this}swapColumns(t,e){$(this,t),$(this,e);for(let r=0;r<this.rows;r++){let o=this.get(r,t);this.set(r,t,this.get(r,e)),this.set(r,e,o)}return this}addRowVector(t){t=U(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=U(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=U(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=U(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=B(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=B(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=B(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=B(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){L(this,t);for(let r=0;r<this.columns;r++)this.set(t,r,this.get(t,r)*e);return this}mulColumn(t,e){$(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(){_(this);let t=this.get(0,0),e=[0,0];for(let r=0;r<this.rows;r++)for(let o=0;o<this.columns;o++)this.get(r,o)>t&&(t=this.get(r,o),e[0]=r,e[1]=o);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(){_(this);let t=this.get(0,0),e=[0,0];for(let r=0;r<this.rows;r++)for(let o=0;o<this.columns;o++)this.get(r,o)<t&&(t=this.get(r,o),e[0]=r,e[1]=o);return e}maxRow(t){if(L(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){L(this,t),_(this);let e=this.get(t,0),r=[t,0];for(let o=1;o<this.columns;o++)this.get(t,o)>e&&(e=this.get(t,o),r[1]=o);return r}minRow(t){if(L(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){L(this,t),_(this);let e=this.get(t,0),r=[t,0];for(let o=1;o<this.columns;o++)this.get(t,o)<e&&(e=this.get(t,o),r[1]=o);return r}maxColumn(t){if($(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){$(this,t),_(this);let e=this.get(0,t),r=[0,t];for(let o=1;o<this.rows;o++)this.get(o,t)>e&&(e=this.get(o,t),r[0]=o);return r}minColumn(t){if($(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){$(this,t),_(this);let e=this.get(0,t),r=[0,t];for(let o=1;o<this.rows;o++)this.get(o,t)<e&&(e=this.get(o,t),r[0]=o);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"){let e=0;if("max"===t)return this.max();if("frobenius"===t){for(let t=0;t<this.rows;t++)for(let r=0;r<this.columns;r++)e+=this.get(t,r)*this.get(t,r);return Math.sqrt(e)}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){G.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 o=0;o<e.length;o++)r+=e[o]*t[o];return r}mmul(t){t=Y.checkMatrix(t);let e=this.rows,r=this.columns,o=t.columns,s=new Y(e,o),n=new Float64Array(r);for(let i=0;i<o;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 o=0;o<r;o++)e+=this.get(t,o)*n[o];s.set(t,i,e)}}return s}strassen2x2(t){t=Y.checkMatrix(t);let e=new Y(2,2);const r=this.get(0,0),o=t.get(0,0),s=this.get(0,1),n=t.get(0,1),i=this.get(1,0),a=t.get(1,0),h=this.get(1,1),u=t.get(1,1),l=(r+h)*(o+u),c=(i+h)*o,f=r*(n-u),d=h*(a-o),g=(r+s)*u,m=l+d-g+(s-h)*(a+u),p=f+g,w=c+d,y=l-c+f+(i-r)*(o+n);return e.set(0,0,m),e.set(0,1,p),e.set(1,0,w),e.set(1,1,y),e}strassen3x3(t){t=Y.checkMatrix(t);let e=new Y(3,3);const r=this.get(0,0),o=this.get(0,1),s=this.get(0,2),n=this.get(1,0),i=this.get(1,1),a=this.get(1,2),h=this.get(2,0),u=this.get(2,1),l=this.get(2,2),c=t.get(0,0),f=t.get(0,1),d=t.get(0,2),g=t.get(1,0),m=t.get(1,1),p=t.get(1,2),w=t.get(2,0),y=t.get(2,1),v=t.get(2,2),M=(r-n)*(-f+m),b=(-r+n+i)*(c-f+m),E=(n+i)*(-c+f),x=r*c,S=(-r+h+u)*(c-d+p),N=(-r+h)*(d-p),R=(h+u)*(-c+d),I=(-s+u+l)*(m+w-y),k=(s-l)*(m-y),A=s*w,T=(u+l)*(-w+y),C=(-s+i+a)*(p+w-v),D=(s-a)*(p-v),z=(i+a)*(-w+v),P=x+A+o*g,V=(r+o+s-n-i-u-l)*m+b+E+x+I+A+T,q=x+S+R+(r+o+s-i-a-h-u)*p+A+C+z,j=M+i*(-c+f+g-m-p-w+v)+b+x+A+C+D,F=M+b+E+x+a*y,O=A+C+D+z+n*d,L=x+S+N+u*(-c+d+g-m-p-w+y)+I+k+A,$=I+k+A+T+h*f,U=x+S+N+R+l*v;return e.set(0,0,P),e.set(0,1,V),e.set(0,2,q),e.set(1,0,j),e.set(1,1,F),e.set(1,2,O),e.set(2,0,L),e.set(2,1,$),e.set(2,2,U),e}mmulStrassen(t){t=Y.checkMatrix(t);let e=this.clone(),r=e.rows,o=e.columns,s=t.rows,n=t.columns;function i(t,e,r){let o=t.rows,s=t.columns;if(o===e&&s===r)return t;{let o=G.zeros(e,r);return o=o.setSubMatrix(t,0,0),o}}o!==s&&console.warn(`Multiplying ${r} x ${o} and ${s} x ${n} matrix: dimensions do not match.`);let a=Math.max(r,s),h=Math.max(o,n);return e=i(e,a,h),function t(e,r,o,s){if(o<=512||s<=512)return e.mmul(r);o%2==1&&s%2==1?(e=i(e,o+1,s+1),r=i(r,o+1,s+1)):o%2==1?(e=i(e,o+1,s),r=i(r,o+1,s)):s%2==1&&(e=i(e,o,s+1),r=i(r,o,s+1));let n=parseInt(e.rows/2,10),a=parseInt(e.columns/2,10),h=e.subMatrix(0,n-1,0,a-1),u=r.subMatrix(0,n-1,0,a-1),l=e.subMatrix(0,n-1,a,e.columns-1),c=r.subMatrix(0,n-1,a,r.columns-1),f=e.subMatrix(n,e.rows-1,0,a-1),d=r.subMatrix(n,r.rows-1,0,a-1),g=e.subMatrix(n,e.rows-1,a,e.columns-1),m=r.subMatrix(n,r.rows-1,a,r.columns-1),p=t(G.add(h,g),G.add(u,m),n,a),w=t(G.add(f,g),u,n,a),y=t(h,G.sub(c,m),n,a),v=t(g,G.sub(d,u),n,a),M=t(G.add(h,l),m,n,a),b=t(G.sub(f,h),G.add(u,c),n,a),E=t(G.sub(l,g),G.add(d,m),n,a),x=G.add(p,v);x.sub(M),x.add(E);let S=G.add(y,M),N=G.add(w,v),R=G.sub(p,w);R.add(y),R.add(b);let I=G.zeros(2*x.rows,2*x.columns);return I=I.setSubMatrix(x,0,0),I=I.setSubMatrix(S,x.rows,0),I=I.setSubMatrix(N,0,x.columns),I=I.setSubMatrix(R,x.rows,x.columns),I.subMatrix(0,o-1,0,s-1)}(e,t=i(t,a,h),a,h)}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 o=new Y(this.rows,this.columns);for(let t=0;t<this.rows;t++){const s=this.getRow(t);s.length>0&&P(s,{min:e,max:r,output:s}),o.setRow(t,s)}return o}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 o=new Y(this.rows,this.columns);for(let t=0;t<this.columns;t++){const s=this.getColumn(t);s.length&&P(s,{min:e,max:r,output:s}),o.setColumn(t,s)}return o}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),o=this.get(e,this.columns-1-r);this.set(e,r,o),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),o=this.get(this.rows-1-r,e);this.set(r,e,o),this.set(this.rows-1-r,e,t)}return this}kroneckerProduct(t){t=Y.checkMatrix(t);let e=this.rows,r=this.columns,o=t.rows,s=t.columns,n=new Y(e*o,r*s);for(let i=0;i<e;i++)for(let e=0;e<r;e++)for(let r=0;r<o;r++)for(let a=0;a<s;a++)n.set(o*i+r,s*e+a,this.get(i,e)*t.get(r,a));return n}kroneckerSum(t){if(t=Y.checkMatrix(t),!this.isSquare()||!t.isSquare())throw new Error("Kronecker Sum needs two Square Matrices");let e=this.rows,r=t.rows,o=this.kroneckerProduct(Y.eye(r,r)),s=Y.eye(e,e).kroneckerProduct(t);return o.add(s)}transpose(){let t=new Y(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=H){for(let e=0;e<this.rows;e++)this.setRow(e,this.getRow(e).sort(t));return this}sortColumns(t=H){for(let e=0;e<this.columns;e++)this.setColumn(e,this.getColumn(e).sort(t));return this}subMatrix(t,e,r,o){K(this,t,e,r,o);let s=new Y(e-t+1,o-r+1);for(let n=t;n<=e;n++)for(let e=r;e<=o;e++)s.set(n-t,e-r,this.get(n,e));return s}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 o=new Y(t.length,r-e+1);for(let s=0;s<t.length;s++)for(let n=e;n<=r;n++){if(t[s]<0||t[s]>=this.rows)throw new RangeError(`Row index out of range: ${t[s]}`);o.set(s,n-e,this.get(t[s],n))}return o}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 o=new Y(r-e+1,t.length);for(let s=0;s<t.length;s++)for(let n=e;n<=r;n++){if(t[s]<0||t[s]>=this.columns)throw new RangeError(`Column index out of range: ${t[s]}`);o.set(n-e,s,this.get(n,t[s]))}return o}setSubMatrix(t,e,r){if((t=Y.checkMatrix(t)).isEmpty())return this;K(this,e,e+t.rows-1,r,r+t.columns-1);for(let o=0;o<t.rows;o++)for(let s=0;s<t.columns;s++)this.set(e+o,r+s,t.get(o,s));return this}selection(t,e){!function(t,e){if(!C(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")}(this,t),function(t,e){if(!C(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")}(this,e);let r=new Y(t.length,e.length);for(let o=0;o<t.length;o++){let s=t[o];for(let t=0;t<e.length;t++){let n=e[t];r.set(o,t,this.get(s,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(){let t=new Y(this.rows,this.columns);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)t.set(e,r,this.get(e,r));return t}sum(t){switch(t){case"row":return function(t){let e=W(t.rows);for(let r=0;r<t.rows;++r)for(let o=0;o<t.columns;++o)e[r]+=t.get(r,o);return e}(this);case"column":return function(t){let e=W(t.columns);for(let r=0;r<t.rows;++r)for(let o=0;o<t.columns;++o)e[o]+=t.get(r,o);return e}(this);case void 0:return function(t){let e=0;for(let r=0;r<t.rows;r++)for(let o=0;o<t.columns;o++)e+=t.get(r,o);return e}(this);default:throw new Error(`invalid option: ${t}`)}}product(t){switch(t){case"row":return function(t){let e=W(t.rows,1);for(let r=0;r<t.rows;++r)for(let o=0;o<t.columns;++o)e[r]*=t.get(r,o);return e}(this);case"column":return function(t){let e=W(t.columns,1);for(let r=0;r<t.rows;++r)for(let o=0;o<t.columns;++o)e[o]*=t.get(r,o);return e}(this);case void 0:return function(t){let e=1;for(let r=0;r<t.rows;r++)for(let o=0;o<t.columns;o++)e*=t.get(r,o);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(!C(o))throw new TypeError("mean must be an array");return function(t,e,r){const o=t.rows,s=t.columns,n=[];for(let i=0;i<o;i++){let o=0,a=0,h=0;for(let e=0;e<s;e++)h=t.get(i,e)-r[i],o+=h,a+=h*h;e?n.push((a-o*o/s)/(s-1)):n.push((a-o*o/s)/s)}return n}(this,r,o);case"column":if(!C(o))throw new TypeError("mean must be an array");return function(t,e,r){const o=t.rows,s=t.columns,n=[];for(let i=0;i<s;i++){let s=0,a=0,h=0;for(let e=0;e<o;e++)h=t.get(e,i)-r[i],s+=h,a+=h*h;e?n.push((a-s*s/o)/(o-1)):n.push((a-s*s/o)/o)}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 o=t.rows,s=t.columns,n=o*s;let i=0,a=0,h=0;for(let e=0;e<o;e++)for(let o=0;o<s;o++)h=t.get(e,o)-r,i+=h,a+=h*h;return e?(a-i*i/n)/(n-1):(a-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(!C(r))throw new TypeError("center must be an array");return function(t,e){for(let r=0;r<t.rows;r++)for(let o=0;o<t.columns;o++)t.set(r,o,t.get(r,o)-e[r])}(this,r),this;case"column":if(!C(r))throw new TypeError("center must be an array");return function(t,e){for(let r=0;r<t.rows;r++)for(let o=0;o<t.columns;o++)t.set(r,o,t.get(r,o)-e[o])}(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 o=0;o<t.columns;o++)t.set(r,o,t.get(r,o)-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 o=0;for(let e=0;e<t.columns;e++)o+=Math.pow(t.get(r,e),2)/(t.columns-1);e.push(Math.sqrt(o))}return e}(this);else if(!C(r))throw new TypeError("scale must be an array");return function(t,e){for(let r=0;r<t.rows;r++)for(let o=0;o<t.columns;o++)t.set(r,o,t.get(r,o)/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 o=0;for(let e=0;e<t.rows;e++)o+=Math.pow(t.get(e,r),2)/(t.rows-1);e.push(Math.sqrt(o))}return e}(this);else if(!C(r))throw new TypeError("scale must be an array");return function(t,e){for(let r=0;r<t.rows;r++)for(let o=0;o<t.columns;o++)t.set(r,o,t.get(r,o)/e[o])}(this,r),this;case void 0:if(void 0===r)r=function(t){const e=t.size-1;let r=0;for(let o=0;o<t.columns;o++)for(let s=0;s<t.rows;s++)r+=Math.pow(t.get(s,o),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 o=0;o<t.columns;o++)t.set(r,o,t.get(r,o)/e)}(this,r),this;default:throw new Error(`invalid option: ${t}`)}}toString(t){return j(this,t)}}function H(t,e){return t-e}G.prototype.klass="Matrix","undefined"!=typeof Symbol&&(G.prototype[Symbol.for("nodejs.util.inspect.custom")]=function(){return j(this)}),G.random=G.rand,G.randomInt=G.randInt,G.diagonal=G.diag,G.prototype.diagonal=G.prototype.diag,G.identity=G.eye,G.prototype.negate=G.prototype.neg,G.prototype.tensorProduct=G.prototype.kroneckerProduct;class Y extends G{constructor(t,e){if(super(),Y.isMatrix(t))return t.clone();if(Number.isInteger(t)&&t>=0){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))}else{if(!C(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 o=0;o<t;o++){if(r[o].length!==e)throw new RangeError("Inconsistent array dimensions");if(!r[o].every((t=>"number"==typeof t)))throw new TypeError("Input data contains non-numeric values");this.data.push(Float64Array.from(r[o]))}}}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 L(this,t),this.data.splice(t,1),this.rows-=1,this}addRow(t,e){return void 0===e&&(e=t,t=this.rows),L(this,t,!0),e=Float64Array.from(U(this,e)),this.data.splice(t,0,e),this.rows+=1,this}removeColumn(t){$(this,t);for(let e=0;e<this.rows;e++){const r=new Float64Array(this.columns-1);for(let o=0;o<t;o++)r[o]=this.data[e][o];for(let o=t+1;o<this.columns;o++)r[o-1]=this.data[e][o];this.data[e]=r}return this.columns-=1,this}addColumn(t,e){void 0===e&&(e=t,t=this.columns),$(this,t,!0),e=B(this,e);for(let r=0;r<this.rows;r++){const o=new Float64Array(this.columns+1);let s=0;for(;s<t;s++)o[s]=this.data[r][s];for(o[s++]=e[r];s<this.columns+1;s++)o[s]=this.data[r][s-1];this.data[r]=o}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,Math.pow(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,Math.pow(this.get(e,r),t.get(e,r)));return this}}(G,Y);class Q extends G{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]}}function X(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 Z{constructor(t,e={}){if((t=Q.checkMatrix(t)).isEmpty())throw new Error("Matrix must be non-empty");let r=t.rows,o=t.columns;const{computeLeftSingularVectors:s=!0,computeRightSingularVectors:n=!0,autoTranspose:i=!1}=e;let a,h=Boolean(s),u=Boolean(n),l=!1;if(r<o)if(i){a=t.transpose(),r=a.rows,o=a.columns,l=!0;let e=h;h=u,u=e}else a=t.clone(),console.warn("Computing SVD on a matrix with more columns than rows. Consider enabling autoTranspose");else a=t.clone();let c=Math.min(r,o),f=Math.min(r+1,o),d=new Float64Array(f),g=new Y(r,c),m=new Y(o,o),p=new Float64Array(o),w=new Float64Array(r),y=new Float64Array(f);for(let t=0;t<f;t++)y[t]=t;let v=Math.min(r-1,o),M=Math.max(0,Math.min(o-2,r)),b=Math.max(v,M);for(let t=0;t<b;t++){if(t<v){d[t]=0;for(let e=t;e<r;e++)d[t]=X(d[t],a.get(e,t));if(0!==d[t]){a.get(t,t)<0&&(d[t]=-d[t]);for(let e=t;e<r;e++)a.set(e,t,a.get(e,t)/d[t]);a.set(t,t,a.get(t,t)+1)}d[t]=-d[t]}for(let e=t+1;e<o;e++){if(t<v&&0!==d[t]){let o=0;for(let s=t;s<r;s++)o+=a.get(s,t)*a.get(s,e);o=-o/a.get(t,t);for(let s=t;s<r;s++)a.set(s,e,a.get(s,e)+o*a.get(s,t))}p[e]=a.get(t,e)}if(h&&t<v)for(let e=t;e<r;e++)g.set(e,t,a.get(e,t));if(t<M){p[t]=0;for(let e=t+1;e<o;e++)p[t]=X(p[t],p[e]);if(0!==p[t]){p[t+1]<0&&(p[t]=0-p[t]);for(let e=t+1;e<o;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++)w[e]=0;for(let e=t+1;e<r;e++)for(let r=t+1;r<o;r++)w[e]+=p[r]*a.get(e,r);for(let e=t+1;e<o;e++){let o=-p[e]/p[t+1];for(let s=t+1;s<r;s++)a.set(s,e,a.get(s,e)+o*w[s])}}if(u)for(let e=t+1;e<o;e++)m.set(e,t,p[e])}}let E=Math.min(o,r+1);if(v<o&&(d[v]=a.get(v,v)),r<E&&(d[E-1]=0),M+1<E&&(p[M]=a.get(M,E-1)),p[E-1]=0,h){for(let t=v;t<c;t++){for(let e=0;e<r;e++)g.set(e,t,0);g.set(t,t,1)}for(let t=v-1;t>=0;t--)if(0!==d[t]){for(let e=t+1;e<c;e++){let o=0;for(let s=t;s<r;s++)o+=g.get(s,t)*g.get(s,e);o=-o/g.get(t,t);for(let s=t;s<r;s++)g.set(s,e,g.get(s,e)+o*g.get(s,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(u)for(let t=o-1;t>=0;t--){if(t<M&&0!==p[t])for(let e=t+1;e<o;e++){let r=0;for(let s=t+1;s<o;s++)r+=m.get(s,t)*m.get(s,e);r=-r/m.get(t+1,t);for(let s=t+1;s<o;s++)m.set(s,e,m.get(s,e)+r*m.get(s,t))}for(let e=0;e<o;e++)m.set(e,t,0);m.set(t,t,1)}let x=E-1,S=0,N=Number.EPSILON;for(;E>0;){let t,e;for(t=E-2;t>=-1&&-1!==t;t--){const e=Number.MIN_VALUE+N*Math.abs(d[t]+Math.abs(d[t+1]));if(Math.abs(p[t])<=e||Number.isNaN(p[t])){p[t]=0;break}}if(t===E-2)e=4;else{let r;for(r=E-1;r>=t&&r!==t;r--){let e=(r!==E?Math.abs(p[r]):0)+(r!==t+1?Math.abs(p[r-1]):0);if(Math.abs(d[r])<=N*e){d[r]=0;break}}r===t?e=3:r===E-1?e=1:(e=2,t=r)}switch(t++,e){case 1:{let e=p[E-2];p[E-2]=0;for(let r=E-2;r>=t;r--){let s=X(d[r],e),n=d[r]/s,i=e/s;if(d[r]=s,r!==t&&(e=-i*p[r-1],p[r-1]=n*p[r-1]),u)for(let t=0;t<o;t++)s=n*m.get(t,r)+i*m.get(t,E-1),m.set(t,E-1,-i*m.get(t,r)+n*m.get(t,E-1)),m.set(t,r,s)}break}case 2:{let e=p[t-1];p[t-1]=0;for(let o=t;o<E;o++){let s=X(d[o],e),n=d[o]/s,i=e/s;if(d[o]=s,e=-i*p[o],p[o]=n*p[o],h)for(let e=0;e<r;e++)s=n*g.get(e,o)+i*g.get(e,t-1),g.set(e,t-1,-i*g.get(e,o)+n*g.get(e,t-1)),g.set(e,o,s)}break}case 3:{const e=Math.max(Math.abs(d[E-1]),Math.abs(d[E-2]),Math.abs(p[E-2]),Math.abs(d[t]),Math.abs(p[t])),s=d[E-1]/e,n=d[E-2]/e,i=p[E-2]/e,a=d[t]/e,l=p[t]/e,c=((n+s)*(n-s)+i*i)/2,f=s*i*(s*i);let w=0;0===c&&0===f||(w=c<0?0-Math.sqrt(c*c+f):Math.sqrt(c*c+f),w=f/(c+w));let y=(a+s)*(a-s)+w,v=a*l;for(let e=t;e<E-1;e++){let s=X(y,v);0===s&&(s=Number.MIN_VALUE);let n=y/s,i=v/s;if(e!==t&&(p[e-1]=s),y=n*d[e]+i*p[e],p[e]=n*p[e]-i*d[e],v=i*d[e+1],d[e+1]=n*d[e+1],u)for(let t=0;t<o;t++)s=n*m.get(t,e)+i*m.get(t,e+1),m.set(t,e+1,-i*m.get(t,e)+n*m.get(t,e+1)),m.set(t,e,s);if(s=X(y,v),0===s&&(s=Number.MIN_VALUE),n=y/s,i=v/s,d[e]=s,y=n*p[e]+i*d[e+1],d[e+1]=-i*p[e]+n*d[e+1],v=i*p[e+1],p[e+1]=n*p[e+1],h&&e<r-1)for(let t=0;t<r;t++)s=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,s)}p[E-2]=y,S+=1;break}case 4:if(d[t]<=0&&(d[t]=d[t]<0?-d[t]:0,u))for(let e=0;e<=x;e++)m.set(e,t,-m.get(e,t));for(;t<x&&!(d[t]>=d[t+1]);){let e=d[t];if(d[t]=d[t+1],d[t+1]=e,u&&t<o-1)for(let r=0;r<o;r++)e=m.get(r,t+1),m.set(r,t+1,m.get(r,t)),m.set(r,t,e);if(h&&t<r-1)for(let o=0;o<r;o++)e=g.get(o,t+1),g.set(o,t+1,g.get(o,t)),g.set(o,t,e);t++}S=0,E--}}if(l){let t=m;m=g,g=t}this.m=r,this.n=o,this.s=d,this.U=g,this.V=m}solve(t){let e=t,r=this.threshold,o=this.s.length,s=Y.zeros(o,o);for(let t=0;t<o;t++)Math.abs(this.s[t])<=r?s.set(t,t,0):s.set(t,t,1/this.s[t]);let n=this.U,i=this.rightSingularVectors,a=i.mmul(s),h=i.rows,u=n.rows,l=Y.zeros(h,u);for(let t=0;t<h;t++)for(let e=0;e<u;e++){let r=0;for(let s=0;s<o;s++)r+=a.get(t,s)*n.get(e,s);l.set(t,e,r)}return l.mmul(e)}solveForDiagonal(t){return this.solve(Y.diag(t))}inverse(){let t=this.V,e=this.threshold,r=t.rows,o=t.columns,s=new Y(r,this.s.length);for(let n=0;n<r;n++)for(let r=0;r<o;r++)Math.abs(this.s[r])>e&&s.set(n,r,t.get(n,r)/this.s[r]);let n=this.U,i=n.rows,a=n.columns,h=new Y(r,i);for(let t=0;t<r;t++)for(let e=0;e<i;e++){let r=0;for(let o=0;o<a;o++)r+=s.get(t,o)*n.get(e,o);h.set(t,e,r)}return h}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 o=0,s=r.length;o<s;o++)r[o]>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 Y.diag(this.s)}}var tt={center:[0,0],linkDistance:50},et=function(){function t(t){void 0===t&&(t={}),this.options=t,this.id="mds",this.options=o(o({},tt),t)}return t.prototype.execute=function(t,e){return this.genericMDSLayout(!1,t,e)},t.prototype.assign=function(t,e){this.genericMDSLayout(!0,t,e)},t.prototype.genericMDSLayout=function(t,e,r){var s=o(o({},this.options),r),n=s.center,i=void 0===n?[0,0]:n,a=s.linkDistance,h=void 0===a?50:a,u=s.layoutInvisibles,f=s.onLayoutEnd,g=e.getAllNodes(),m=e.getAllEdges();if(u||(g=g.filter((function(t){return t.data.visible||void 0===t.data.visible})),m=m.filter((function(t){return t.data.visible||void 0===t.data.visible}))),!g||0===g.length){var p={nodes:[],edges:m};return null==f||f(p),p}if(1===g.length){t&&e.mergeNodeData(g[0].id,{x:i[0],y:i[1]});var w={nodes:[o(o({},g[0]),{data:o(o({},g[0].data),{x:i[0],y:i[1]})})],edges:m};return null==f||f(w),w}var y=c({nodes:g,edges:m},!1),v=l(y);rt(v);var M=function(t,e){var r=[];return t.forEach((function(t){var o=[];t.forEach((function(t){o.push(t*e)})),r.push(o)})),r}(v,h),b=ot(M),E=[];b.forEach((function(t,e){var r=d(g[e]);r.data.x=t[0]+i[0],r.data.y=t[1]+i[1],E.push(r)})),t&&E.forEach((function(t){return e.mergeNodeData(t.id,{x:t.data.x,y:t.data.y})}));var x={nodes:E,edges:m};return null==f||f(x),x},t}(),rt=function(t){var e=-999999;t.forEach((function(t){t.forEach((function(t){t!==1/0&&e<t&&(e=t)}))})),t.forEach((function(r,o){r.forEach((function(r,s){r===1/0&&(t[o][s]=e)}))}))},ot=function(t){var e=Y.mul(Y.pow(t,2),-.5),r=e.mean("row"),o=e.mean("column"),s=e.mean();e.add(s).subRowVector(r).subColumnVector(o);var n=new Z(e),i=Y.sqrt(n.diagonalMatrix).diagonal();return n.leftSingularVectors.toJSON().map((function(t){return Y.mul([t],[i]).toJSON()[0].splice(0,2)}))},st={iterations:10,height:10,width:10,speed:100,gravity:10,k:5},nt=function(t,e,r,o,s,n){e.forEach((function(i,a){r[a]={x:0,y:0},e.forEach((function(e,h){if(a!==h&&s[a]===s[h]){var u=i.x-e.x,l=i.y-e.y,c=Math.sqrt(u*u+l*l);if(0===c){c=1;var f=a>h?1:-1;u=.01*f,l=.01*f}if(c<n(t[a])/2+n(t[h])/2){var d=o*o/c;r[a].x+=u/c*d,r[a].y+=l/c*d}}}))}))},it=function(t,e,r,o,s,n,i,a){var h=n||i/10;return o&&e.forEach((function(e,r){var o=t[r].x-t[s].x,n=t[r].y-t[s].y,i=Math.sqrt(o*o+n*n),a=n/i,h=-o/i,u=Math.sqrt(e.x*e.x+e.y*e.y),l=Math.acos((a*e.x+h*e.y)/u);l>Math.PI/2&&(l-=Math.PI/2,a*=-1,h*=-1);var c=Math.cos(l)*u;e.x=a*c,e.y=h*c})),t.forEach((function(n,i){if(i!==s){var u=Math.sqrt(e[i].x*e[i].x+e[i].y*e[i].y);if(u>0&&i!==s){var l=Math.min(h*(r/800),u);if(n.x+=e[i].x/u*l,n.y+=e[i].y/u*l,o){var c=n.x-t[s].x,f=n.y-t[s].y,d=Math.sqrt(c*c+f*f);c=c/d*a[i],f=f/d*a[i],n.x=t[s].x+c,n.y=t[s].y+f}}}})),t},at={maxIteration:1e3,focusNode:null,unitRadius:null,linkDistance:50,preventOverlap:!1,nodeSize:void 0,nodeSpacing:void 0,strictRadial:!0,maxPreventOverlapIteration:200,sortBy:void 0,sortStrength:10},ht=function(){function t(t){void 0===t&&(t={}),this.options=t,this.id="radial",this.options=o(o({},at),t)}return t.prototype.execute=function(t,e){return this.genericRadialLayout(!1,t,e)},t.prototype.assign=function(t,e){this.genericRadialLayout(!0,t,e)},t.prototype.genericRadialLayout=function(t,e,r){var a=o(o({},this.options),r),h=a.width,u=a.height,f=a.center,d=a.focusNode,g=a.unitRadius,m=a.nodeSize,p=a.nodeSpacing,w=a.strictRadial,y=a.preventOverlap,v=a.maxPreventOverlapIteration,M=a.sortBy,b=a.linkDistance,E=void 0===b?50:b,x=a.sortStrength,S=void 0===x?10:x,N=a.maxIteration,R=void 0===N?1e3:N,I=a.layoutInvisibles,k=a.onLayoutEnd,A=e.getAllNodes(),T=e.getAllEdges();if(I||(A=A.filter((function(t){return t.data.visible||void 0===t.data.visible})),T=T.filter((function(t){return t.data.visible||void 0===t.data.visible}))),!A||0===A.length){var C={nodes:[],edges:T};return null==k||k(C),C}var D=h||"undefined"==typeof window?h:window.innerWidth,z=u||"undefined"==typeof window?u:window.innerHeight,P=f||[D/2,z/2];if(1===A.length){t&&e.mergeNodeData(A[0].id,{x:P[0],y:P[1]});var V={nodes:[o(o({},A[0]),{data:o(o({},A[0].data),{x:P[0],y:P[1]})})],edges:T};return null==k||k(V),V}var q=A[0];if(i(d)){for(var j=0;j<A.length;j++)if(A[j].id===d){q=A[j];break}}else q=d||A[0];var F=ft(A,q.id),O=c({nodes:A,edges:T},!1),L=l(O),$=gt(L,F);dt(L,F,$+1);var U=L[F],B=D-P[0]>P[0]?P[0]:D-P[0],K=z-P[1]>P[1]?P[1]:z-P[1];0===B&&(B=D/2),0===K&&(K=z/2);var W=Math.min(B,K),J=Math.max.apply(Math,n([],s(U),!1)),_=[],G=g||W/J;U.forEach((function(t,e){_[e]=t*G}));var H,Q=ut(A,L,E,_,G,M,S),X=lt(Q),tt=function(t,e,r){try{var o=Y.mul(Y.pow(e,2),-.5),s=o.mean("row"),n=o.mean("column"),i=o.mean();o.add(i).subRowVector(s).subColumnVector(n);var a=new Z(o),h=Y.sqrt(a.diagonalMatrix).diagonal();return a.leftSingularVectors.toJSON().map((function(e){return Y.mul([e],[h]).toJSON()[0].splice(0,t)}))}catch(t){for(var u=[],l=0;l<e.length;l++){var c=Math.random()*r,f=Math.random()*r;u.push([c,f])}return u}}(E,Q,E).map((function(t){var e=s(t,2),r=e[0],o=e[1];return{x:(isNaN(r)?Math.random()*E:r)-tt[F].x,y:(isNaN(o)?Math.random()*E:o)-tt[F].y}}));if(this.run(R,tt,X,Q,_,F),y){H=mt(m,p);var et={nodes:A,nodeSizeFunc:H,positions:tt,radii:_,height:z,width:D,strictRadial:Boolean(w),focusIdx:F,iterations:v||200,k:tt.length/4.5};tt=function(t,e){for(var r=o(o({},st),e),s=r.positions,n=r.iterations,i=r.width,a=r.k,h=r.speed,u=void 0===h?100:h,l=r.strictRadial,c=r.focusIdx,f=r.radii,d=void 0===f?[]:f,g=r.nodeSizeFunc,m=t.getAllNodes(),p=[],w=i/10,y=0;y<n;y++)s.forEach((function(t,e){p[e]={x:0,y:0}})),nt(m,s,p,a,d,g),it(s,p,u,l,c,w,i,d);return s}(e,et)}var rt=[];tt.forEach((function(t,e){rt.push(o(o({},A[e]),{data:o(o({},A[0].data),{x:t.x+P[0],y:t.y+P[1]})}))})),t&&rt.forEach((function(t){return e.mergeNodeData(t.id,{x:t.data.x,y:t.data.y})}));var ot={nodes:rt,edges:T};return null==k||k(ot),ot},t.prototype.run=function(t,e,r,o,s,n){for(var i=0;i<=t;i++){var a=i/t;this.oneIteration(a,e,s,o,r,n)}},t.prototype.oneIteration=function(t,e,r,o,s,n){var i=1-t;e.forEach((function(a,h){var u=ct(a,{x:0,y:0}),l=0===u?0:1/u;if(h!==n){var c=0,f=0,d=0;e.forEach((function(t,e){if(h!==e){var r=ct(a,t),n=0===r?0:1/r,i=o[e][h];d+=s[h][e],c+=s[h][e]*(t.x+i*(a.x-t.x)*n),f+=s[h][e]*(t.y+i*(a.y-t.y)*n)}}));var g=0===r[h]?0:1/r[h];d*=i,d+=t*g*g,c*=i,c+=t*g*a.x*l,a.x=c/d,f*=i,f+=t*g*a.y*l,a.y=f/d}}))},t}(),ut=function(t,e,r,o,s,n,a){if(!t)return[];var h=[];return e&&e.forEach((function(e,u){var l=[];e.forEach((function(e,h){if(u===h)l.push(0);else if(o[u]===o[h])if("data"===n)l.push(e*(Math.abs(u-h)*a)/(o[u]/s));else if(n){var c=t[u][n]||0,f=t[h][n]||0;i(c)&&(c=c.charCodeAt(0)),i(f)&&(f=f.charCodeAt(0)),l.push(e*(Math.abs(c-f)*a)/(o[u]/s))}else l.push(e*r/(o[u]/s));else{var d=(r+s)/2;l.push(e*d)}})),h.push(l)})),h},lt=function(t){for(var e=t.length,r=t[0].length,o=[],s=0;s<e;s++){for(var n=[],i=0;i<r;i++)0!==t[s][i]?n.push(1/(t[s][i]*t[s][i])):n.push(0);o.push(n)}return o},ct=function(t,e){return Math.sqrt((t.x-e.x)*(t.x-e.x)+(t.y-e.y)*(t.y-e.y))},ft=function(t,e){var r=-1;return t.forEach((function(t,o){t.id===e&&(r=o)})),Math.max(r,0)},dt=function(t,e,r){for(var o=t.length,s=0;s<o;s++)if(t[e][s]===1/0){t[e][s]=r,t[s][e]=r;for(var n=0;n<o;n++)t[s][n]!==1/0&&t[e][n]===1/0&&(t[e][n]=r+t[s][n],t[n][e]=r+t[s][n])}for(s=0;s<o;s++)if(s!==e)for(n=0;n<o;n++)if(t[s][n]===1/0){var i=Math.abs(t[e][s]-t[e][n]);i=0===i?1:i,t[s][n]=i}},gt=function(t,e){for(var r=0,o=0;o<t[e].length;o++)t[e][o]!==1/0&&(r=t[e][o]>r?t[e][o]:r);return r},mt=function(t,e){var r;return r=u(e)?function(){return e}:g(e)?e:function(){return 0},t?h(t)?function(e){return(t[0]>t[1]?t[0]:t[1])+r(e)}:function(e){return t+r(e)}:function(t){return t.data.bboxSize?Math.max(t.data.bboxSize[0],t.data.bboxSize[1])+r(t):t.data.size?h(t.data.size)?Math.max(t.data.size[0],t.data.size[1])+r(t):f(t.data.size)?(t.data.size.width>t.data.size.height?t.data.size.width:t.data.size.height)+r(t):t.data.size+r(t):10+r(t)}},pt={center:[0,0],width:300,height:300},wt={circular:w,concentric:b,mds:et,random:function(){function t(t){void 0===t&&(t={}),this.options=t,this.id="random",this.options=o(o({},pt),t)}return t.prototype.execute=function(t,e){return this.genericRandomLayout(!1,t,e)},t.prototype.assign=function(t,e){this.genericRandomLayout(!0,t,e)},t.prototype.genericRandomLayout=function(t,e,r){var s=o(o({},this.options),r),n=s.center,i=s.width,a=s.height,h=s.layoutInvisibles,u=s.onLayoutEnd,l=e.getAllNodes();h||(l=l.filter((function(t){return t.data.visible||void 0===t.data.visible})));var c=i||"undefined"==typeof window?i:window.innerWidth,f=a||"undefined"==typeof window?a:window.innerHeight,d=n||[c/2,f/2],g=[];l&&l.forEach((function(t){g.push({id:t.id,data:{x:.9*(Math.random()-.5)*c+d[0],y:.9*(Math.random()-.5)*f+d[1]}})})),t&&g.forEach((function(t){return e.mergeNodeData(t.id,{x:t.data.x,y:t.data.y})}));var m={nodes:g,edges:e.getAllEdges()};return null==u||u(m),m},t}(),grid:x,radial:ht};function yt(t,e){var o=t.layout,s=o.id,n=o.options,i=t.nodes,a=t.edges,h=new r({nodes:i,edges:a}),u=wt[s];if(!u)throw new Error("Unknown layout id: ".concat(s));return[new u(n).execute(h),e]}addEventListener("message",(function(t){var r,o=t.data,s=o.type,n=o.method,i=o.id,a=o.params;"RPC"===s&&n&&((r=e[n])?Promise.resolve().then((function(){return r.apply(e,a)})):Promise.reject("No such method")).then((function(t){postMessage({type:"RPC",id:i,result:t})})).catch((function(t){var e={message:t};t.stack&&(e.message=t.message,e.stack=t.stack,e.name=t.name),postMessage({type:"RPC",id:i,error:e})}))})),postMessage({type:"RPC",method:"ready"})})();
2
- //# sourceMappingURL=6215b31a5bc171c6c2a5.worker.js.map