@datagrok/bio 2.11.2 → 2.11.5
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/CHANGELOG.md +1 -0
- package/dist/196.js +1 -1
- package/dist/196.js.map +1 -1
- package/dist/361.js +1 -1
- package/dist/361.js.map +1 -1
- package/dist/381.js +1 -1
- package/dist/381.js.map +1 -1
- package/dist/770.js +1 -1
- package/dist/770.js.map +1 -1
- package/dist/79.js.map +1 -1
- package/dist/868.js +1 -1
- package/dist/868.js.map +1 -1
- package/dist/package-test.js +1 -1
- package/dist/package-test.js.map +1 -1
- package/dist/package.js +1 -1
- package/dist/package.js.map +1 -1
- package/package.json +3 -3
- package/src/analysis/sequence-space.ts +34 -12
- package/src/demo/bio01b-hierarchical-clustering-and-activity-cliffs.ts +2 -1
- package/src/package.ts +39 -34
- package/src/tests/activity-cliffs-tests.ts +5 -3
- package/src/tests/activity-cliffs-utils.ts +5 -2
- package/src/utils/helm-to-molfile.ts +37 -12
- package/src/utils/monomer-lib.ts +4 -9
- package/src/utils/poly-tool/{enumerator-tools.ts → transformation.ts} +25 -127
- package/src/utils/poly-tool/ui.ts +125 -0
package/CHANGELOG.md
CHANGED
package/dist/196.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
/*! For license information please see 196.js.LICENSE.txt */
|
|
2
|
-
var bio;(()=>{"use strict";var t,e,r={3196:(t,e,r)=>{var s=r(7659),n=r(3659);function o(t){return Math.random()*t}function i(t){return Math.floor(o(t))}function h(t=!1,e="Assertion error."){if(!t)throw new Error(e)}function a(t,e,r=1){const s=t.length;h(s==e.length,"Vector lengths do not match.");const o=new n.OW(s);for(let s=0;s<t.length;++s)o[s]=t[s]+r*e[s];return o}function l(t,e,r=1){const s=function(t,e,r=0){return new Array(t).fill(r).map((()=>new n.OW(e).fill(r)))}(t,e);for(let n=0;n<t;++n)for(let t=0;t<e;++t)s[n][t]=o(r);return s}function u(t,e){const r=function(t){let e=0;for(let r=0;r<t.length;++r)e+=t[r];return e}(function(t){const e=t.length,r=new n.OW(e);for(let e=0;e<t.length;++e)r[e]=t[e]*t[e];return r}(a(t,e,-1)));return Math.sqrt(r)}function c(t){return(e,r)=>t*e+r-Math.floor((e+2)*(e+1)/2)}class f{constructor(t=!0,e=!0){const s=navigator.hardwareConcurrency;this._workerCount=t?Math.max(s-2,1):1,this._workers=new Array(this._workerCount).fill(null).map((()=>new Worker(new URL(r.p+r.u(868),r.b)))),this._terminateOnComplete=e}async calc(t,e,r=!0,s){return new Promise((async(n,o)=>{try{const o=t.length,i=new Array(this._workerCount),h=o*(o-1)/2;this._workerCount=Math.min(this._workerCount,h);const a=h/this._workerCount,l=new Float32Array(h);let u=0,c=1,f=0,m=Number.MIN_VALUE;for(let r=0;r<this._workerCount;r++){const n=Math.floor(r*a),g=r===this._workerCount-1?h:Math.floor((r+1)*a),p=u,d=c;r!==this._workerCount-1&&(u=o-2-Math.floor(Math.sqrt(-8*g+4*o*(o-1)-7)/2-.5),c=g-o*u+Math.floor((u+1)*(u+2)/2)),this._workers[r].postMessage({values:t,fnName:e,startRow:p,startCol:d,chunckSize:g-n,opts:s}),i[r]=new Promise(((t,e)=>{this._workers[r].onmessage=({data:{error:s,distanceMatrixData:o,min:i,max:h}})=>{this._terminateOnComplete&&this._workers[r].terminate(),s?e(s):(l.set(o,n),i<f&&(f=i),h>m&&(m=h),t())}}))}await Promise.all(i),r&&l.forEach(((t,e)=>{l[e]=(t-f)/(m-f)})),n(l)}catch(t){o(t)}}))}terminate(){this._workers.forEach((t=>t.terminate()))}}class m{constructor(t){this.steps=t?.steps??0,this.cycles=t?.cycles??1e6,this.cutoff=t?.cutoff??0,this.lambda=t?.lambda??2,this.dlambda=t?.dlambda??.01,this.lambda2=this.lambda/2,this.dlambda2=this.dlambda/2,this.epsilon=t?.epsilon??1e-10,this.distanceFunction=t?.distance??u,this.distance=new Float32Array,this.distanceFunctionName=t?.distanceFunctionName}async initDistance(t){this.dmIndexFunct=c(t.length);const e=new f(!0,!1);this.distance=await e.calc(t,this.distanceFunctionName),e.terminate()}calcDistance(t,e,r){return this.distance[this.dmIndexFunct(e,r)]}async embed(t){const e=t.length,r=l(e,m.dimension,40);let s=this.lambda2;0===this.steps&&(this.steps=t.length-1),await this.initDistance(t);for(let n=0;n<this.cycles;++n){for(let n=0;n<this.steps;++n){const n=i(e);let o=i(e);for(;n==o;)o=i(e);const h=r[n],l=r[o],c=this.calcDistance(t,n,o),f=u(h,l);if(0==this.cutoff||c<=this.cutoff||f<c){const t=s*(c-f)/(f+this.epsilon),e=a(h,l,-1);r[n]=a(h,e,t),r[o]=a(l,e,-t)}}if(s-=this.dlambda2,s<=0)break}return r}}m.dimension=2;class g extends m{async embed(t){const e=t.length,r=l(e,g.dimension,40);let s=this.lambda;await this.initDistance(t);for(let n=0;n<this.cycles;++n){const n=i(e),o=r[n];for(let i=0;i<e;++i){if(n==i)continue;const e=r[i],h=this.calcDistance(t,n,i),l=u(o,e);if(0==this.cutoff||h<=this.cutoff||l<h){const t=s*(h-l)/(l+this.epsilon),n=a(o,e,-1);r[i]=a(e,n,-t)}}if(s-=this.dlambda,s<=0)break}return r}}class p extends m{constructor(t){super(t),this.cycles=t?.cycles??1e3,this.steps=t?.steps??1e5,this.radiusPercent=t?.radiusPercent??1,this.maxDistance=t?.maxDistance??null,this.maxDistanceSteps=t?.maxDistanceSteps??null}async embed(t){const e=t.length,r=l(e,p.dimension,40);if(await this.initDistance(t),null===this.maxDistanceSteps&&(this.maxDistanceSteps=e*Math.floor((e-1)/2)),null===this.maxDistance){this.maxDistance=-1e37;for(let r=0;r<this.maxDistanceSteps;r++){const r=i(e);let s=i(e);for(;r==s;)s=i(e);const n=this.calcDistance(t,r,s);n>this.maxDistance&&(this.maxDistance=n)}}let s=this.lambda;const n=0==this.radiusPercent?this.maxDistance:this.maxDistance*this.radiusPercent;for(let o=0;o<this.cycles;++o){for(let o=0;o<this.steps;++o){const o=i(e);let h=i(e);for(;o==h;)h=i(e);const l=r[o],c=r[h],f=this.calcDistance(t,o,h),m=u(l,c);if(f<=n||m<f){const t=.5*s*(f-m)/(m+this.epsilon),e=a(l,c,-1);r[o]=a(l,e,t),r[h]=a(c,e,-t)}}if(s-=(this.lambda-this.dlambda)/(this.cycles-1),s<this.dlambda)break}return r}}var d,w,y,b,M,x,E=r(1811),S=r(3979),v=r(9657);!function(t){t.Levenshtein="Levenshtein",t.JaroWinkler="Jaro-Winkler",t.Manhattan="Manhattan"}(d||(d={})),function(t){t.Euclidean="Euclidean"}(w||(w={})),function(t){t.Tanimoto="Tanimoto",t.Dice="Dice",t.Asymmetric="Asymmetric",t.BraunBlanquet="Braun-Blanquet",t.Cosine="Cosine",t.Kulczynski="Kulczynski",t.McConnaughey="Mc-Connaughey",t.RogotGoldberg="Rogot-Goldberg",t.Russel="Russel",t.Sokal="Sokal",t.Hamming="Hamming",t.Euclidean="Euclidean"}(y||(y={})),function(t){t.TanimotoIntArray="TanimotoIntArray"}(b||(b={})),function(t){t.Vector="Vector",t.String="String",t.BitArray="BitArray",t.MacroMolecule="MacroMolecule",t.Number="Number",t.IntArray="IntArray"}(M||(M={})),function(t){t.NumericDistance="NumericDistance"}(x||(x={}));var N=r(489);function k(t,e){const r=t.trueCount()+e.trueCount();if(0==r)return 1;const s=t.andWithCountBits(e,!0);return s/(r-s)}function C(t){return 0===t?3402823e32:1/t-1}y.Tanimoto,y.Dice,y.Asymmetric,y.BraunBlanquet,y.Cosine,y.Kulczynski,y.McConnaughey,y.RogotGoldberg,y.Russel,y.Sokal,y.Hamming,y.Euclidean,y.Tanimoto,y.Dice,y.Asymmetric,y.BraunBlanquet,y.Cosine,y.Kulczynski,y.McConnaughey,y.RogotGoldberg,y.Russel,y.Sokal,y.Hamming,y.Euclidean,y.Tanimoto,y.Dice,y.Cosine,y.Tanimoto,y.Asymmetric,y.Cosine,y.Sokal,N.U.HAMMING,N.U.LEVENSHTEIN,N.U.MONOMER_CHEMICAL_DISTANCE;const R={[w.Euclidean]:u},A={[d.Levenshtein]:E.T,[d.JaroWinkler]:S.H$,[d.Manhattan]:function(t,e){if(t.length!==e.length)return 1;{let r=0;for(let s=1;s<t.length;s++)r+=t[s]==e[s]?0:1;return r/t.length}}},I={[y.Tanimoto]:function(t,e){return C(k(t,e))},[y.Dice]:function(t,e){return C(function(t,e){const r=t.trueCount()+e.trueCount();return 0==r?0:2*t.andWithCountBits(e,!0)/r}(t,e))},[y.Asymmetric]:function(t,e){return C(function(t,e){const r=Math.min(t.trueCount(),e.trueCount());return 0==r?0:t.andWithCountBits(e,!0)/r}(t,e))},[y.BraunBlanquet]:function(t,e){return C(function(t,e){const r=Math.max(t.trueCount(),e.trueCount());return 0==r?0:t.andWithCountBits(e,!0)/r}(t,e))},[y.Cosine]:function(t,e){return C(function(t,e){const r=t.trueCount()*e.trueCount();return 0==r?0:t.andWithCountBits(e,!0)/Math.sqrt(r)}(t,e))},[y.Kulczynski]:function(t,e){return C(function(t,e){const r=t.trueCount()+e.trueCount(),s=t.trueCount()*e.trueCount();return 0==s?0:t.andWithCountBits(e,!0)*r/(2*s)}(t,e))},[y.McConnaughey]:function(t,e){return C(function(t,e){const r=t.trueCount()+e.trueCount(),s=t.trueCount()*e.trueCount();return 0==s?0:(t.andWithCountBits(e,!0)*r-s)/s}(t,e))},[y.RogotGoldberg]:function(t,e){return C(function(t,e){const r=t.andWithCountBits(e,!0),s=t.countBits(!0)+e.countBits(!0),n=t.length,o=n-s+r;return r==n||o==n?1:r/s+o/(2*n-s)}(t,e))},[y.Russel]:function(t,e){return C(function(t,e){return 0==t.length?0:t.andWithCountBits(e,!0)/t.length}(t,e))},[y.Sokal]:function(t,e){return C(function(t,e){const r=t.trueCount()+e.trueCount(),s=t.andWithCountBits(e,!0);return s/(2*r-3*s)}(t,e))},[y.Hamming]:function(t,e){return t.trueCount()+e.trueCount()-2*t.andWithCountBits(e,!0)},[y.Euclidean]:function(t,e){return Math.sqrt(t.trueCount()+e.trueCount()-2*t.andWithCountBits(e,!0))}},F={[b.TanimotoIntArray]:function(t,e){return C(k(new v.Z(t,32*t.length),new v.Z(e,32*e.length)))}},z={[x.NumericDistance]:function(t,e){return Math.abs(t-e)}},T={[M.Vector]:{[w.Euclidean]:R[w.Euclidean]},[M.String]:{[d.Levenshtein]:A[d.Levenshtein],[d.JaroWinkler]:A[d.JaroWinkler],[d.Manhattan]:A[d.Manhattan]},[M.BitArray]:{[y.Tanimoto]:I[y.Tanimoto],[y.Dice]:I[y.Dice],[y.Asymmetric]:I[y.Asymmetric],[y.BraunBlanquet]:I[y.BraunBlanquet],[y.Cosine]:I[y.Cosine],[y.Kulczynski]:I[y.Kulczynski],[y.McConnaughey]:I[y.McConnaughey],[y.RogotGoldberg]:I[y.RogotGoldberg],[y.Russel]:I[y.Russel],[y.Sokal]:I[y.Sokal]},[M.MacroMolecule]:{[N.U.HAMMING]:N.o[N.U.HAMMING],[N.U.LEVENSHTEIN]:N.o[N.U.LEVENSHTEIN],[N.U.NEEDLEMANN_WUNSCH]:N.o[N.U.NEEDLEMANN_WUNSCH],[N.U.MONOMER_CHEMICAL_DISTANCE]:N.o[N.U.MONOMER_CHEMICAL_DISTANCE]},[M.Number]:{[x.NumericDistance]:z[x.NumericDistance]},[M.IntArray]:{[b.TanimotoIntArray]:F[b.TanimotoIntArray]}},_=Object.keys(T).reduce(((t,e)=>{for(const r of Object.keys(T[e]))t[r]=e;return t}),{});class D{constructor(t){this.method=t,this.dataType=_[t]}getMeasure(t){const e=T;if(!e.hasOwnProperty(this.dataType)||!e[this.dataType].hasOwnProperty(this.method))throw new Error(`Unknown measure ${this.method} for data type ${this.dataType}`);return r=this.method,_[r]==M.MacroMolecule.toString()?e[this.dataType][this.method](t):e[this.dataType][this.method];var r}static getMetricByDataType(t){return Object.keys(T[t])}static get availableMeasures(){return Object.keys(T)}}function P(t,e){return Math.floor(e()*t)}function V(t){return t()}function O(t){const e=[];for(let r=0;r<t;r++)e.push(void 0);return e}function j(t){return O(t).map(((t,e)=>e))}function q(t,e){return O(t).map((()=>e))}function L(t){return q(t,0)}function $(t){return function(t){return t.reduce(((t,e)=>t+e))}(t)/t.length}function W(t){let e=0;for(let r=0;r<t.length;r++)e=t[r]>e?t[r]:e;return e}function B(t,e,r){const s=L(t);for(let n=0;n<t;n++){let t=!0;for(;t;){const o=P(e,r);let i=!1;for(let t=0;t<n;t++)if(o===s[t]){i=!0;break}i||(t=!1),s[n]=o}}return s}function U(t,e,r){const s=[];let n=0,o=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[o]),o+=1;s.push(e),n+=1}return s}function G(t,e){const r=r=>O(t).map((()=>q(e,r))),s=[];return s.push(r(-1)),s.push(r(1/0)),s.push(r(0)),s}function Y(t,e,r){const s=L(t);for(let n=0;n<t;n++){let t=!0,o=0;for(;t;){o=P(e,r);let i=!1;for(let t=0;t<n;t++)if(o===s[t]){i=!0;break}i||(t=!1)}s[n]=o}return s}function H(t,e,r,s,n){e=Math.floor(e);const o=t[0][e],i=t[1][e];if(t[2][e],r>=i[0])return 0;for(let t=0;t<o.length;t++)if(s===o[t])return 0;return K(t,e,r,s,n)}function K(t,e,r,s,n){const o=t[0][e],i=t[1][e],h=t[2][e];if(r>=i[0])return 0;i[0]=r,o[0]=s,h[0]=n;let a=0,l=0;for(;;){const e=2*a+1,s=e+1,n=t[0][0].length;if(e>=n)break;if(s>=n){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],o[a]=o[l],h[a]=h[l],a=l}return i[a]=r,o[a]=s,h[a]=n,1}function J(t,e,r,s,n){const o=G(e,s);for(let s=0;s<e;s++)for(let e=0;e<r;e++){if(t[0][s][e]<0)continue;const r=t[0][s][e],i=t[2][s][e],h=V(n);H(o,s,h,r,i),H(o,r,h,s,i),t[2][s][e]=0}return o}function X(t){const e=t[0],r=t[1];for(let t=0;t<e.length;t++){const s=e[t],n=r[t];for(let t=0;t<s.length-1;t++){const e=s.length-t-1,r=n.length-t-1,o=s[0];s[0]=s[e],s[e]=o;const i=n[0];n[0]=n[r],n[r]=i,Q(n,s,r,0)}}return{indices:e,weights:r}}function Q(t,e,r,s){for(;2*s+1<r;){const n=2*s+1,o=n+1;let i=s;if(t[i]<t[n]&&(i=n),o<r&&t[i]<t[o]&&(i=o),i===s)break;{const r=t[s];t[s]=t[i],t[i]=r;const n=e[s];e[s]=e[i],e[i]=n,s=i}}}function Z(t,e){const r=t[0][e],s=t[1][e],n=t[2][e];let o=1/0,i=-1;for(let t=0;t>r.length;t++)1===n[t]&&s[t]<o&&(o=s[t],i=t);return i>=0?(n[i]=0,Math.floor(r[i])):-1}class tt{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 n=t[s],o=e[s];this.checkDims(n,o);const i=this.makeKey(n,o);this.entries.set(i,{value:r[s],row:n,col:o})}}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){const s=this.makeKey(t,e);return this.entries.has(s)?this.entries.get(s).value:r}getAll(t=!0){const e=new Array(this.entries.size).fill(null);let r=0;return this.entries.forEach((t=>{e[r++]=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){const e=new Float32Array(this.entries.size);let r=0;this.entries.forEach((s=>{e[r++]=t(s.value,s.row,s.col)}));const s=[this.nRows,this.nCols];return new tt(this.getRows(),this.getCols(),e,s)}toArray(){const t=O(this.nRows).map((()=>L(this.nCols)));return this.entries.forEach((e=>{t[e.row][e.col]=e.value})),t}}function et(t){const e=t.getRows(),r=t.getCols(),s=t.getValues(),n=r.length,o=new Int32Array(n),i=new Int32Array(n),h=new Float32Array(n);o.set(e),i.set(r),h.set(s);const a=[t.nCols,t.nRows];return new tt(i,o,h,a)}function rt(t,e){return lt(t,e,((t,e)=>t*e))}function st(t,e){return lt(t,e,((t,e)=>t+e))}function nt(t,e){return lt(t,e,((t,e)=>t-e))}function ot(t,e){return t.map((t=>t*e))}function it(t){const e=new Set,r=t.getValues(),s=t.getRows(),n=t.getCols();for(let t=0;t<r.length;t++)0===r[t]&&e.add(t);const o=(t,r)=>!e.has(r),i=r.filter(o),h=s.filter(o),a=n.filter(o);return new tt(h,a,i,t.getDims())}function ht(t,e="l2"){const r=at[e],s=new Map;t.forEach(((t,e,r)=>{const n=s.get(e)||[];n.push(r),s.set(e,n)}));const n=new tt([],[],[],t.getDims());for(let e of s.keys()){const o=s.get(e).sort(),i=r(o.map((r=>t.get(e,r))));for(let t=0;t<i.length;t++)n.set(e,o[t],i[t])}return n}const at={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 lt(t,e,r){const s=new Set,n=[],o=[],i=[],h=(s,h)=>{n.push(s),o.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],n=`${e}:${r}`;s.add(n),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],n=`${e}:${r}`;s.has(n)||h(e,r)}const g=[t.nRows,t.nCols];return new tt(n,o,i,g)}function ut(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=[],n=[];let o=-1;for(let t=0;t<e.length;t++){const{row:i,col:h,value:a}=e[t];i!==o&&(o=i,n.push(t)),r.push(h),s.push(a)}return{indices:r,values:s,indptr:n}}class ct{constructor(t,e,r,s){this.hyperplanes=t,this.offsets=e,this.children=r,this.indices=s}}function ft(t,e,r,s){const n=Math.max(10,e),o=j(r).map(((e,r)=>function(t,e=30,r,s){return mt(t,j(t.length),e,r,s)}(t,n,r,s))),i=o.map((t=>function(t,e){const r=pt(t),s=dt(t),n=j(r).map((()=>t.hyperplane?1:0)),o=L(r),i=j(r).map((()=>[-1,-1])),h=j(s).map((()=>j(e).map((()=>-1))));return gt(t,n,o,i,h,0,0),new ct(n,o,i,h)}(t,n)));return i}function mt(t,e,r=30,s,n){if(e.length>r){const o=function(t,e,r){let s=P(e.length,r),n=P(e.length,r);n+=s===n?1:0,n%=e.length;const o=e[s],i=e[n];let h=0,a=0;a=t[o]-t[i],h-=a*(t[o]+t[i])/2;let l=0,u=0;const c=L(e.length);for(let s=0;s<e.length;s++){let n=h;n+=a*t[e[s]],0===n?(c[s]=P(2,r),0===c[s]?l+=1:u+=1):n>0?(c[s]=0,l+=1):(c[s]=1,u+=1)}const f=L(l),m=L(u);l=0,u=0;for(let t=0;t<c.length;t++)0===c[t]?(f[l]=e[t],l+=1):(m[u]=e[t],u+=1);return{indicesLeft:f,indicesRight:m,hyperplane:a,offset:h}}(t,e,n),{indicesLeft:i,indicesRight:h,hyperplane:a,offset:l}=o;return{leftChild:mt(t,i,r,s+1,n),rightChild:mt(t,h,r,s+1,n),isLeaf:!1,hyperplane:a,offset:l}}return{indices:e,isLeaf:!0}}function gt(t,e,r,s,n,o,i){if(t.isLeaf)return s[o][0]=-i,n[i].splice(0,t.indices.length,...t.indices),{nodeNum:o,leafNum:i+=1};{e[o]=t.hyperplane,r[o]=t.offset,s[o][0]=o+1;const h=o;let a=gt(t.leftChild,e,r,s,n,o+1,i);return o=a.nodeNum,i=a.leafNum,s[h][1]=o+1,a=gt(t.rightChild,e,r,s,n,o+1,i),{nodeNum:a.nodeNum,leafNum:a.leafNum}}}function pt(t){return t.isLeaf?1:1+pt(t.leftChild)+pt(t.rightChild)}function dt(t){return t.isLeaf?1:dt(t.leftChild)+dt(t.rightChild)}function wt(t,e,r,s){let n=e;return n+=t*r,0===n?P(2,s):n>0?0:1}function yt(t,e,r){let s=0;for(;e.children[s][0]>0;)s=0===wt(e.hyperplanes[s],e.offsets[s],t,r)?e.children[s][0]:e.children[s][1];const n=-1*e.children[s][0];return e.indices[n]}const bt=Object.prototype.toString;function Mt(t){return bt.call(t).endsWith("Array]")}function xt(t,e,r){let s=0;const n=r(e);for(let e=0;e<t.x.length;e++)s+=Math.abs(t.y[e]-n(t.x[e]));return s}const Et=Object.prototype.toString;function St(t){return Et.call(t).endsWith("Array]")}const vt=Object.prototype.toString;function Nt(t){return vt.call(t).endsWith("Array]")}const kt=Object.prototype.toString;const Ct=Object.prototype.toString;function Rt(t){var e,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!Nt(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(!Nt(r.output))throw new TypeError("output option must be an array if specified");e=r.output}else e=new Array(t.length);var s=function(t){var e,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(e=t,!Ct.call(e).endsWith("Array]"))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");var s=r.fromIndex,n=void 0===s?0:s,o=r.toIndex,i=void 0===o?t.length:o;if(n<0||n>=t.length||!Number.isInteger(n))throw new Error("fromIndex must be a positive integer smaller than length");if(i<=n||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[n],a=n+1;a<i;a++)t[a]<h&&(h=t[a]);return h}(t),n=function(t){var e,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(e=t,!kt.call(e).endsWith("Array]"))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");var s=r.fromIndex,n=void 0===s?0:s,o=r.toIndex,i=void 0===o?t.length:o;if(n<0||n>=t.length||!Number.isInteger(n))throw new Error("fromIndex must be a positive integer smaller than length");if(i<=n||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[n],a=n+1;a<i;a++)t[a]>h&&(h=t[a]);return h}(t);if(s===n)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");var o=r.min,i=void 0===o?r.autoMinMax?s:0:o,h=r.max,a=void 0===h?r.autoMinMax?n:1:h;if(i>=a)throw new RangeError("min option must be smaller than max option");for(var l=(a-i)/(n-s),u=0;u<t.length;u++)e[u]=(t[u]-s)*l+i;return e}const At=" ".repeat(2),It=" ".repeat(4);function Ft(t,e={}){const{maxRows:r=15,maxColumns:s=10,maxNumSize:n=8,padMinus:o="auto"}=e;return`${t.constructor.name} {\n${At}[\n${It}${function(t,e,r,s,n){const{rows:o,columns:i}=t,h=Math.min(o,e),a=Math.min(i,r),l=[];if("auto"===n){n=!1;t:for(let e=0;e<h;e++)for(let r=0;r<a;r++)if(t.get(e,r)<0){n=!0;break t}}for(let e=0;e<h;e++){let r=[];for(let o=0;o<a;o++)r.push(zt(t.get(e,o),s,n));l.push(`${r.join(" ")}`)}return a!==i&&(l[l.length-1]+=` ... ${i-r} more columns`),h!==o&&l.push(`... ${o-e} more rows`),l.join(`\n${It}`)}(t,r,s,n,o)}\n${At}]\n${At}rows: ${t.rows}\n${At}columns: ${t.columns}\n}`}function zt(t,e,r){return(t>=0&&r?` ${Tt(t,e-1)}`:Tt(t,e)).padEnd(e)}function Tt(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 n=t.toExponential(e);return n.length>e&&(n=t.toExponential(Math.max(0,e-(n.length-e)))),n.slice(0)}function _t(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 Dt(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 Pt(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 Vt(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 Ot(t,e,r,s,n){if(5!==arguments.length)throw new RangeError("expected 4 arguments");if(qt("startRow",e),qt("endRow",r),qt("startColumn",s),qt("endColumn",n),e>r||s>n||e<0||e>=t.rows||r<0||r>=t.rows||s<0||s>=t.columns||n<0||n>=t.columns)throw new RangeError("Submatrix indices are out of range")}function jt(t,e=0){let r=[];for(let s=0;s<t;s++)r.push(e);return r}function qt(t,e){if("number"!=typeof e)throw new TypeError(`${t} must be a number`)}function Lt(t){if(t.isEmpty())throw new Error("Empty matrix has no elements to index")}class $t{static from1DArray(t,e,r){if(t*e!==r.length)throw new RangeError("data length does not match given dimensions");let s=new Bt(t,e);for(let n=0;n<t;n++)for(let t=0;t<e;t++)s.set(n,t,r[n*e+t]);return s}static rowVector(t){let e=new Bt(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 Bt(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 Bt(t,e)}static ones(t,e){return new Bt(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 n=new Bt(t,e);for(let r=0;r<t;r++)for(let t=0;t<e;t++)n.set(r,t,s());return n}static randInt(t,e,r={}){if("object"!=typeof r)throw new TypeError("options must be an object");const{min:s=0,max:n=1e3,random:o=Math.random}=r;if(!Number.isInteger(s))throw new TypeError("min must be an integer");if(!Number.isInteger(n))throw new TypeError("max must be an integer");if(s>=n)throw new RangeError("min must be smaller than max");let i=n-s,h=new Bt(t,e);for(let r=0;r<t;r++)for(let t=0;t<e;t++){let e=s+Math.round(o()*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),n=this.zeros(t,e);for(let t=0;t<s;t++)n.set(t,t,r);return n}static diag(t,e,r){let s=t.length;void 0===e&&(e=s),void 0===r&&(r=e);let n=Math.min(s,e,r),o=this.zeros(e,r);for(let e=0;e<n;e++)o.set(e,e,t[e]);return o}static min(t,e){t=this.checkMatrix(t),e=this.checkMatrix(e);let r=t.rows,s=t.columns,n=new Bt(r,s);for(let o=0;o<r;o++)for(let r=0;r<s;r++)n.set(o,r,Math.min(t.get(o,r),e.get(o,r)));return n}static max(t,e){t=this.checkMatrix(t),e=this.checkMatrix(e);let r=t.rows,s=t.columns,n=new this(r,s);for(let o=0;o<r;o++)for(let r=0;r<s;r++)n.set(o,r,Math.max(t.get(o,r),e.get(o,r)));return n}static checkMatrix(t){return $t.isMatrix(t)?t:new Bt(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,s=!0,n=!1;for(;t<this.rows&&s;){for(e=0,n=!1;e<this.columns&&!1===n;)0===this.get(t,e)?e++:1===this.get(t,e)&&e>r?(n=!0,r=e):(s=!1,n=!0);t++}return s}isReducedEchelonForm(){let t=0,e=0,r=-1,s=!0,n=!1;for(;t<this.rows&&s;){for(e=0,n=!1;e<this.columns&&!1===n;)0===this.get(t,e)?e++:1===this.get(t,e)&&e>r?(n=!0,r=e):(s=!1,n=!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 n=e;n<t.rows;n++)t.get(n,r)>t.get(s,r)&&(s=n);if(0===t.get(s,r))r++;else{t.swapRows(e,s);let n=t.get(e,r);for(let s=r;s<t.columns;s++)t.set(e,s,t.get(e,s)/n);for(let s=e+1;s<t.rows;s++){let n=t.get(s,r)/t.get(e,r);t.set(s,r,0);for(let o=r+1;o<t.columns;o++)t.set(s,o,t.get(s,o)-t.get(e,o)*n)}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 n=0,o=!1;for(;n<r&&!1===o;)1===t.get(s,n)?o=!0:n++;for(let r=0;r<s;r++){let o=t.get(r,n);for(let i=n;i<e;i++){let e=t.get(r,i)-o*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 Bt(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){_t(this,t);let e=[];for(let r=0;r<this.columns;r++)e.push(this.get(t,r));return e}getRowVector(t){return Bt.rowVector(this.getRow(t))}setRow(t,e){_t(this,t),e=Pt(this,e);for(let r=0;r<this.columns;r++)this.set(t,r,e[r]);return this}swapRows(t,e){_t(this,t),_t(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){Dt(this,t);let e=[];for(let r=0;r<this.rows;r++)e.push(this.get(r,t));return e}getColumnVector(t){return Bt.columnVector(this.getColumn(t))}setColumn(t,e){Dt(this,t),e=Vt(this,e);for(let r=0;r<this.rows;r++)this.set(r,t,e[r]);return this}swapColumns(t,e){Dt(this,t),Dt(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=Pt(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=Pt(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=Pt(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=Pt(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=Vt(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=Vt(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=Vt(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=Vt(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){_t(this,t);for(let r=0;r<this.columns;r++)this.set(t,r,this.get(t,r)*e);return this}mulColumn(t,e){Dt(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(){Lt(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(){Lt(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(_t(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){_t(this,t),Lt(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(_t(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){_t(this,t),Lt(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(Dt(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){Dt(this,t),Lt(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(Dt(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){Dt(this,t),Lt(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"){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){$t.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=Bt.checkMatrix(t);let e=this.rows,r=this.columns,s=t.columns,n=new Bt(e,s),o=new Float64Array(r);for(let i=0;i<s;i++){for(let e=0;e<r;e++)o[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)*o[s];n.set(t,i,e)}}return n}strassen2x2(t){t=Bt.checkMatrix(t);let e=new Bt(2,2);const r=this.get(0,0),s=t.get(0,0),n=this.get(0,1),o=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*(o-l),m=a*(h-s),g=(r+n)*l,p=u+m-g+(n-a)*(h+l),d=f+g,w=c+m,y=u-c+f+(i-r)*(s+o);return e.set(0,0,p),e.set(0,1,d),e.set(1,0,w),e.set(1,1,y),e}strassen3x3(t){t=Bt.checkMatrix(t);let e=new Bt(3,3);const r=this.get(0,0),s=this.get(0,1),n=this.get(0,2),o=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),p=t.get(1,1),d=t.get(1,2),w=t.get(2,0),y=t.get(2,1),b=t.get(2,2),M=(r-o)*(-f+p),x=(-r+o+i)*(c-f+p),E=(o+i)*(-c+f),S=r*c,v=(-r+a+l)*(c-m+d),N=(-r+a)*(m-d),k=(a+l)*(-c+m),C=(-n+l+u)*(p+w-y),R=(n-u)*(p-y),A=n*w,I=(l+u)*(-w+y),F=(-n+i+h)*(d+w-b),z=(n-h)*(d-b),T=(i+h)*(-w+b),_=S+A+s*g,D=(r+s+n-o-i-l-u)*p+x+E+S+C+A+I,P=S+v+k+(r+s+n-i-h-a-l)*d+A+F+T,V=M+i*(-c+f+g-p-d-w+b)+x+S+A+F+z,O=M+x+E+S+h*y,j=A+F+z+T+o*m,q=S+v+N+l*(-c+m+g-p-d-w+y)+C+R+A,L=C+R+A+I+a*f,$=S+v+N+k+u*b;return e.set(0,0,_),e.set(0,1,D),e.set(0,2,P),e.set(1,0,V),e.set(1,1,O),e.set(1,2,j),e.set(2,0,q),e.set(2,1,L),e.set(2,2,$),e}mmulStrassen(t){t=Bt.checkMatrix(t);let e=this.clone(),r=e.rows,s=e.columns,n=t.rows,o=t.columns;function i(t,e,r){let s=t.rows,n=t.columns;if(s===e&&n===r)return t;{let s=$t.zeros(e,r);return s=s.setSubMatrix(t,0,0),s}}s!==n&&console.warn(`Multiplying ${r} x ${s} and ${n} x ${o} matrix: dimensions do not match.`);let h=Math.max(r,n),a=Math.max(s,o);return e=i(e,h,a),function t(e,r,s,n){if(s<=512||n<=512)return e.mmul(r);s%2==1&&n%2==1?(e=i(e,s+1,n+1),r=i(r,s+1,n+1)):s%2==1?(e=i(e,s+1,n),r=i(r,s+1,n)):n%2==1&&(e=i(e,s,n+1),r=i(r,s,n+1));let o=parseInt(e.rows/2,10),h=parseInt(e.columns/2,10),a=e.subMatrix(0,o-1,0,h-1),l=r.subMatrix(0,o-1,0,h-1),u=e.subMatrix(0,o-1,h,e.columns-1),c=r.subMatrix(0,o-1,h,r.columns-1),f=e.subMatrix(o,e.rows-1,0,h-1),m=r.subMatrix(o,r.rows-1,0,h-1),g=e.subMatrix(o,e.rows-1,h,e.columns-1),p=r.subMatrix(o,r.rows-1,h,r.columns-1),d=t($t.add(a,g),$t.add(l,p),o,h),w=t($t.add(f,g),l,o,h),y=t(a,$t.sub(c,p),o,h),b=t(g,$t.sub(m,l),o,h),M=t($t.add(a,u),p,o,h),x=t($t.sub(f,a),$t.add(l,c),o,h),E=t($t.sub(u,g),$t.add(m,p),o,h),S=$t.add(d,b);S.sub(M),S.add(E);let v=$t.add(y,M),N=$t.add(w,b),k=$t.sub(d,w);k.add(y),k.add(x);let C=$t.zeros(2*S.rows,2*S.columns);return C=C.setSubMatrix(S,0,0),C=C.setSubMatrix(v,S.rows,0),C=C.setSubMatrix(N,0,S.columns),C=C.setSubMatrix(k,S.rows,S.columns),C.subMatrix(0,s-1,0,n-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 Bt(this.rows,this.columns);for(let t=0;t<this.rows;t++){const n=this.getRow(t);n.length>0&&Rt(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 Bt(this.rows,this.columns);for(let t=0;t<this.columns;t++){const n=this.getColumn(t);n.length&&Rt(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=Bt.checkMatrix(t);let e=this.rows,r=this.columns,s=t.rows,n=t.columns,o=new Bt(e*s,r*n);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<n;h++)o.set(s*i+r,n*e+h,this.get(i,e)*t.get(r,h));return o}kroneckerSum(t){if(t=Bt.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(Bt.eye(r,r)),n=Bt.eye(e,e).kroneckerProduct(t);return s.add(n)}transpose(){let t=new Bt(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=Wt){for(let e=0;e<this.rows;e++)this.setRow(e,this.getRow(e).sort(t));return this}sortColumns(t=Wt){for(let e=0;e<this.columns;e++)this.setColumn(e,this.getColumn(e).sort(t));return this}subMatrix(t,e,r,s){Ot(this,t,e,r,s);let n=new Bt(e-t+1,s-r+1);for(let o=t;o<=e;o++)for(let e=r;e<=s;e++)n.set(o-t,e-r,this.get(o,e));return n}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 Bt(t.length,r-e+1);for(let n=0;n<t.length;n++)for(let o=e;o<=r;o++){if(t[n]<0||t[n]>=this.rows)throw new RangeError(`Row index out of range: ${t[n]}`);s.set(n,o-e,this.get(t[n],o))}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 Bt(r-e+1,t.length);for(let n=0;n<t.length;n++)for(let o=e;o<=r;o++){if(t[n]<0||t[n]>=this.columns)throw new RangeError(`Column index out of range: ${t[n]}`);s.set(o-e,n,this.get(o,t[n]))}return s}setSubMatrix(t,e,r){if((t=Bt.checkMatrix(t)).isEmpty())return this;Ot(this,e,e+t.rows-1,r,r+t.columns-1);for(let s=0;s<t.rows;s++)for(let n=0;n<t.columns;n++)this.set(e+s,r+n,t.get(s,n));return this}selection(t,e){!function(t,e){if(!St(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(!St(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 Bt(t.length,e.length);for(let s=0;s<t.length;s++){let n=t[s];for(let t=0;t<e.length;t++){let o=e[t];r.set(s,t,this.get(n,o))}}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 Bt(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=jt(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=jt(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=jt(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=jt(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:s=this.mean(t)}=e;if("boolean"!=typeof r)throw new TypeError("unbiased must be a boolean");switch(t){case"row":if(!St(s))throw new TypeError("mean must be an array");return function(t,e,r){const s=t.rows,n=t.columns,o=[];for(let i=0;i<s;i++){let s=0,h=0,a=0;for(let e=0;e<n;e++)a=t.get(i,e)-r[i],s+=a,h+=a*a;e?o.push((h-s*s/n)/(n-1)):o.push((h-s*s/n)/n)}return o}(this,r,s);case"column":if(!St(s))throw new TypeError("mean must be an array");return function(t,e,r){const s=t.rows,n=t.columns,o=[];for(let i=0;i<n;i++){let n=0,h=0,a=0;for(let e=0;e<s;e++)a=t.get(e,i)-r[i],n+=a,h+=a*a;e?o.push((h-n*n/s)/(s-1)):o.push((h-n*n/s)/s)}return o}(this,r,s);case void 0:if("number"!=typeof s)throw new TypeError("mean must be a number");return function(t,e,r){const s=t.rows,n=t.columns,o=s*n;let i=0,h=0,a=0;for(let e=0;e<s;e++)for(let s=0;s<n;s++)a=t.get(e,s)-r,i+=a,h+=a*a;return e?(h-i*i/o)/(o-1):(h-i*i/o)/o}(this,r,s);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(!St(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(!St(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+=Math.pow(t.get(r,e),2)/(t.columns-1);e.push(Math.sqrt(s))}return e}(this);else if(!St(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+=Math.pow(t.get(e,r),2)/(t.rows-1);e.push(Math.sqrt(s))}return e}(this);else if(!St(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 n=0;n<t.rows;n++)r+=Math.pow(t.get(n,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 Ft(this,t)}}function Wt(t,e){return t-e}$t.prototype.klass="Matrix","undefined"!=typeof Symbol&&($t.prototype[Symbol.for("nodejs.util.inspect.custom")]=function(){return Ft(this)}),$t.random=$t.rand,$t.randomInt=$t.randInt,$t.diagonal=$t.diag,$t.prototype.diagonal=$t.prototype.diag,$t.identity=$t.eye,$t.prototype.negate=$t.prototype.neg,$t.prototype.tensorProduct=$t.prototype.kroneckerProduct;class Bt extends $t{constructor(t,e){if(super(),Bt.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(!St(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(!r[s].every((t=>"number"==typeof t)))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 _t(this,t),this.data.splice(t,1),this.rows-=1,this}addRow(t,e){return void 0===e&&(e=t,t=this.rows),_t(this,t,!0),e=Float64Array.from(Pt(this,e)),this.data.splice(t,0,e),this.rows+=1,this}removeColumn(t){Dt(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),Dt(this,t,!0),e=Vt(this,e);for(let r=0;r<this.rows;r++){const s=new Float64Array(this.columns+1);let n=0;for(;n<t;n++)s[n]=this.data[r][n];for(s[n++]=e[r];n<this.columns+1;n++)s[n]=this.data[r][n-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,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}}($t,Bt);class Ut extends $t{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 Gt{constructor(t){let e,r,s,n,o,i,h,a,l,u=(t=Ut.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),o=0,s=0;s<l;s++)o+=u.get(e,s)*a[s];a[e]-=o,u.set(e,r,a[e])}for(n=r,e=r+1;e<c;e++)Math.abs(a[e])>Math.abs(a[n])&&(n=e);if(n!==r){for(s=0;s<f;s++)i=u.get(n,s),u.set(n,s,u.get(r,s)),u.set(r,s,i);h=m[n],m[n]=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=Bt.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,n,o=t.columns,i=t.subMatrixRow(this.pivotVector,0,o-1),h=e.columns;for(n=0;n<h;n++)for(r=n+1;r<h;r++)for(s=0;s<o;s++)i.set(r,s,i.get(r,s)-i.get(n,s)*e.get(r,n));for(n=h-1;n>=0;n--){for(s=0;s<o;s++)i.set(n,s,i.get(n,s)/e.get(n,n));for(r=0;r<n;r++)for(s=0;s<o;s++)i.set(r,s,i.get(r,s)-i.get(n,s)*e.get(r,n))}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 Bt(e,r);for(let n=0;n<e;n++)for(let e=0;e<r;e++)n>e?s.set(n,e,t.get(n,e)):n===e?s.set(n,e,1):s.set(n,e,0);return s}get upperTriangularMatrix(){let t=this.LU,e=t.rows,r=t.columns,s=new Bt(e,r);for(let n=0;n<e;n++)for(let e=0;e<r;e++)n<=e?s.set(n,e,t.get(n,e)):s.set(n,e,0);return s}get pivotPermutationVector(){return Array.from(this.pivotVector)}}function Yt(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 Ht{constructor(t){let e,r,s,n,o=(t=Ut.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=Yt(t,o.get(e,s));if(0!==t){for(o.get(s,s)<0&&(t=-t),e=s;e<i;e++)o.set(e,s,o.get(e,s)/t);for(o.set(s,s,o.get(s,s)+1),r=s+1;r<h;r++){for(n=0,e=s;e<i;e++)n+=o.get(e,s)*o.get(e,r);for(n=-n/o.get(s,s),e=s;e<i;e++)o.set(e,r,o.get(e,r)+n*o.get(e,s))}}a[s]=-t}this.QR=o,this.Rdiag=a}solve(t){t=Bt.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,n,o,i,h=t.columns,a=t.clone(),l=e.columns;for(o=0;o<l;o++)for(n=0;n<h;n++){for(i=0,s=o;s<r;s++)i+=e.get(s,o)*a.get(s,n);for(i=-i/e.get(o,o),s=o;s<r;s++)a.set(s,n,a.get(s,n)+i*e.get(s,o))}for(o=l-1;o>=0;o--){for(n=0;n<h;n++)a.set(o,n,a.get(o,n)/this.Rdiag[o]);for(s=0;s<o;s++)for(n=0;n<h;n++)a.set(s,n,a.get(s,n)-a.get(o,n)*e.get(s,o))}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,n=new Bt(s,s);for(t=0;t<s;t++)for(e=0;e<s;e++)t<e?n.set(t,e,r.get(t,e)):t===e?n.set(t,e,this.Rdiag[t]):n.set(t,e,0);return n}get orthogonalMatrix(){let t,e,r,s,n=this.QR,o=n.rows,i=n.columns,h=new Bt(o,i);for(r=i-1;r>=0;r--){for(t=0;t<o;t++)h.set(t,r,0);for(h.set(r,r,1),e=r;e<i;e++)if(0!==n.get(r,r)){for(s=0,t=r;t<o;t++)s+=n.get(t,r)*h.get(t,e);for(s=-s/n.get(r,r),t=r;t<o;t++)h.set(t,e,h.get(t,e)+s*n.get(t,r))}}return h}}class Kt{constructor(t,e={}){if((t=Ut.checkMatrix(t)).isEmpty())throw new Error("Matrix must be non-empty");let r=t.rows,s=t.columns;const{computeLeftSingularVectors:n=!0,computeRightSingularVectors:o=!0,autoTranspose:i=!1}=e;let h,a=Boolean(n),l=Boolean(o),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 Bt(r,c),p=new Bt(s,s),d=new Float64Array(s),w=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]=Yt(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 n=t;n<r;n++)s+=h.get(n,t)*h.get(n,e);s=-s/h.get(t,t);for(let n=t;n<r;n++)h.set(n,e,h.get(n,e)+s*h.get(n,t))}d[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){d[t]=0;for(let e=t+1;e<s;e++)d[t]=Yt(d[t],d[e]);if(0!==d[t]){d[t+1]<0&&(d[t]=0-d[t]);for(let e=t+1;e<s;e++)d[e]/=d[t];d[t+1]+=1}if(d[t]=-d[t],t+1<r&&0!==d[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<s;r++)w[e]+=d[r]*h.get(e,r);for(let e=t+1;e<s;e++){let s=-d[e]/d[t+1];for(let n=t+1;n<r;n++)h.set(n,e,h.get(n,e)+s*w[n])}}if(l)for(let e=t+1;e<s;e++)p.set(e,t,d[e])}}let E=Math.min(s,r+1);if(b<s&&(m[b]=h.get(b,b)),r<E&&(m[E-1]=0),M+1<E&&(d[M]=h.get(M,E-1)),d[E-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 n=t;n<r;n++)s+=g.get(n,t)*g.get(n,e);s=-s/g.get(t,t);for(let n=t;n<r;n++)g.set(n,e,g.get(n,e)+s*g.get(n,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!==d[t])for(let e=t+1;e<s;e++){let r=0;for(let n=t+1;n<s;n++)r+=p.get(n,t)*p.get(n,e);r=-r/p.get(t+1,t);for(let n=t+1;n<s;n++)p.set(n,e,p.get(n,e)+r*p.get(n,t))}for(let e=0;e<s;e++)p.set(e,t,0);p.set(t,t,1)}let S=E-1,v=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(m[t]+Math.abs(m[t+1]));if(Math.abs(d[t])<=e||Number.isNaN(d[t])){d[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(d[r]):0)+(r!==t+1?Math.abs(d[r-1]):0);if(Math.abs(m[r])<=N*e){m[r]=0;break}}r===t?e=3:r===E-1?e=1:(e=2,t=r)}switch(t++,e){case 1:{let e=d[E-2];d[E-2]=0;for(let r=E-2;r>=t;r--){let n=Yt(m[r],e),o=m[r]/n,i=e/n;if(m[r]=n,r!==t&&(e=-i*d[r-1],d[r-1]=o*d[r-1]),l)for(let t=0;t<s;t++)n=o*p.get(t,r)+i*p.get(t,E-1),p.set(t,E-1,-i*p.get(t,r)+o*p.get(t,E-1)),p.set(t,r,n)}break}case 2:{let e=d[t-1];d[t-1]=0;for(let s=t;s<E;s++){let n=Yt(m[s],e),o=m[s]/n,i=e/n;if(m[s]=n,e=-i*d[s],d[s]=o*d[s],a)for(let e=0;e<r;e++)n=o*g.get(e,s)+i*g.get(e,t-1),g.set(e,t-1,-i*g.get(e,s)+o*g.get(e,t-1)),g.set(e,s,n)}break}case 3:{const e=Math.max(Math.abs(m[E-1]),Math.abs(m[E-2]),Math.abs(d[E-2]),Math.abs(m[t]),Math.abs(d[t])),n=m[E-1]/e,o=m[E-2]/e,i=d[E-2]/e,h=m[t]/e,u=d[t]/e,c=((o+n)*(o-n)+i*i)/2,f=n*i*(n*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=(h+n)*(h-n)+w,b=h*u;for(let e=t;e<E-1;e++){let n=Yt(y,b);0===n&&(n=Number.MIN_VALUE);let o=y/n,i=b/n;if(e!==t&&(d[e-1]=n),y=o*m[e]+i*d[e],d[e]=o*d[e]-i*m[e],b=i*m[e+1],m[e+1]=o*m[e+1],l)for(let t=0;t<s;t++)n=o*p.get(t,e)+i*p.get(t,e+1),p.set(t,e+1,-i*p.get(t,e)+o*p.get(t,e+1)),p.set(t,e,n);if(n=Yt(y,b),0===n&&(n=Number.MIN_VALUE),o=y/n,i=b/n,m[e]=n,y=o*d[e]+i*m[e+1],m[e+1]=-i*d[e]+o*m[e+1],b=i*d[e+1],d[e+1]=o*d[e+1],a&&e<r-1)for(let t=0;t<r;t++)n=o*g.get(t,e)+i*g.get(t,e+1),g.set(t,e+1,-i*g.get(t,e)+o*g.get(t,e+1)),g.set(t,e,n)}d[E-2]=y,v+=1;break}case 4:if(m[t]<=0&&(m[t]=m[t]<0?-m[t]:0,l))for(let e=0;e<=S;e++)p.set(e,t,-p.get(e,t));for(;t<S&&!(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=p.get(r,t+1),p.set(r,t+1,p.get(r,t)),p.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++}v=0,E--}}if(u){let t=p;p=g,g=t}this.m=r,this.n=s,this.s=m,this.U=g,this.V=p}solve(t){let e=t,r=this.threshold,s=this.s.length,n=Bt.zeros(s,s);for(let t=0;t<s;t++)Math.abs(this.s[t])<=r?n.set(t,t,0):n.set(t,t,1/this.s[t]);let o=this.U,i=this.rightSingularVectors,h=i.mmul(n),a=i.rows,l=o.rows,u=Bt.zeros(a,l);for(let t=0;t<a;t++)for(let e=0;e<l;e++){let r=0;for(let n=0;n<s;n++)r+=h.get(t,n)*o.get(e,n);u.set(t,e,r)}return u.mmul(e)}solveForDiagonal(t){return this.solve(Bt.diag(t))}inverse(){let t=this.V,e=this.threshold,r=t.rows,s=t.columns,n=new Bt(r,this.s.length);for(let o=0;o<r;o++)for(let r=0;r<s;r++)Math.abs(this.s[r])>e&&n.set(o,r,t.get(o,r)/this.s[r]);let o=this.U,i=o.rows,h=o.columns,a=new Bt(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+=n.get(t,s)*o.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,n=r.length;s<n;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 Bt.diag(this.s)}}function Jt(t,e,r,s,n){let o=r*s*s,i=Bt.eye(e.length,e.length,o);const h=n(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,n){const o=r.length,i=t.x.length;let h=new Array(o);for(let a=0;a<o;a++){h[a]=new Array(i);let o=r.slice();o[a]+=s;let l=n(o);for(let r=0;r<i;r++)h[a][r]=e[r]-l(t.x[r])}return new Bt(h)}(t,a,e,s,n),u=function(t,e){const r=t.x.length;let s=new Array(r);for(let n=0;n<r;n++)s[n]=[t.y[n]-e[n]];return new Bt(s)}(t,a),c=function(t,e=!1){return t=Ut.checkMatrix(t),e?new Kt(t).inverse():function(t,e,r=!1){return t=Ut.checkMatrix(t),e=Ut.checkMatrix(e),r?new Kt(t).solve(e):t.isSquare()?new Gt(t).solve(e):new Ht(t).solve(e)}(t,Bt.eye(t.rows))}(i.add(l.mmul(l.transpose())));return(e=(e=new Bt([e])).sub(c.mmul(l).mmul(u).mul(s).transpose())).to1DArray()}const Xt=1e-5,Qt=.001;class Zt{get neighbors(){return this.nNeighbors}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=te,this.isInitialized=!1,this.rpForest=[],this.embedding=[],this.optimizationState=new ee;const e=e=>{void 0!==t[e]&&(this[e]=t[e])};e("distanceFn"),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{initFromTree:t,initFromRandom:e}=(r=this.distanceFn,{initFromRandom:function(t,e,s,n,o){for(let i=0;i<s.length;i++){const h=B(t,e.length,o);for(let t=0;t<h.length;t++)h[t]<0||H(n,i,r(e[h[t]],s[i]),h[t],1)}},initFromTree:function(t,e,s,n,o){for(let i=0;i<s.length;i++){const h=yt(s[i],t,o);for(let t=0;t<h.length;t++){if(h[t]<0)return;H(n,i,r(e[h[t]],s[i]),h[t],1)}}}});var r;this.initFromTree=t,this.initFromRandom=e,this.search=function(t){return function(e,r,s,n){const{indices:o,indptr:i}=ut(r);for(let r=0;r<n.length;r++){const h=new Set(s[0][r]);for(;;){const a=Z(s,r);if(-1===a)break;const l=o.slice(i[a],i[a+1]);for(const o of l)o===a||-1===o||h.has(o)||(K(s,r,t(e[o],n[r]),o,1),h.add(o))}}return s}}(this.distanceFn)}makeSearchGraph(t){const e=this.knnIndices,r=this.knnDistances,s=[t.length,t.length],n=new tt([],[],[],s);for(let t=0;t<e.length;t++){const s=e[t],o=r[t];for(let e=0;e<s.length;e++){const r=s[e],i=o[e];i>0&&n.set(t,r,i)}}return lt(n,et(n),((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,n,o,i){const h=G(r.length,s);if(n(s,e,r,h,i),t)for(let s of t)o(s,e,r,h,i);return h}(this.rpForest,e,t,r,this.initFromRandom,this.initFromTree,this.random),n=this.search(e,this.searchGraph,s,t);let{indices:o,weights:i}=X(n);o=o.map((t=>t.slice(0,this.nNeighbors))),i=i.map((t=>t.slice(0,this.nNeighbors)));const h=Math.max(0,this.localConnectivity-1),{sigmas:a,rhos:l}=this.smoothKNNDistance(i,this.nNeighbors,h),{rows:u,cols:c,vals:f}=this.computeMembershipStrengths(o,i,a,l),m=[t.length,e.length];let g=new tt(u,c,f,m);const p=ut(ht(g,"l1")),d=t.length,w=function(t,e,r){const s=L(t.length).map((t=>L(r[0].length)));for(let n=0;n<t.length;n++)for(let o=0;o<t[0].length;o++)for(let i=0;i<r[0].length;i++){const h=t[n][o];s[n][i]+=e[n][o]*r[h][i]}return s}(U(p.indices,d,this.nNeighbors),U(p.values,d,this.nNeighbors),this.embedding),y=this.nEpochs?this.nEpochs/3:g.nRows<=1e4?100:30,b=g.getValues().reduce(((t,e)=>e>t?e:t),0);g=g.map((t=>t<b/y?0:t)),g=it(g);const M=this.makeEpochsPerSample(g.getValues(),y),x=g.getRows(),E=g.getCols();return this.assignOptimizationStateParameters({headEmbedding:w,tailEmbedding:this.embedding,head:x,tail:E,currentEpoch:0,nEpochs:y,nVertices:g.getDims()[1],epochsPerSample:M}),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:e,nNeighbors:r}=this,s=function(t,e){return function(r,s,n,o=10,i=50,h=.001,a=.5,l=!0){const u=r.length,c=G(r.length,n);for(let s=0;s<r.length;s++){const o=Y(n,r.length,e);for(let e=0;e<o.length;e++){const n=t(r[s],r[o[e]]);H(c,s,n,o[e],1),H(c,o[e],n,s,1)}}if(l)for(let e=0;e<s.length;e++)for(let n=0;n<s[e].length&&!(s[e][n]<0);n++)for(let o=n+1;o<s[e].length&&!(s[e][o]<0);o++){const i=t(r[s[e][n]],r[s[e][o]]);H(c,s[e][n],i,s[e][o],1),H(c,s[e][o],i,s[e][n],1)}for(let s=0;s<o;s++){const s=J(c,u,n,i,e);let o=0;for(let n=0;n<u;n++)for(let h=0;h<i;h++){let l=Math.floor(s[0][n][h]);if(!(l<0||V(e)<a))for(let e=0;e<i;e++){const i=Math.floor(s[0][n][e]),a=s[2][n][h],u=s[2][n][e];if(i<0||!a&&!u)continue;const f=t(r[l],r[i]);o+=H(c,l,f,i,1),o+=H(c,i,f,l,1)}}if(o<=h*n*r.length)break}return X(c)}}(e,this.random),n=5+Math.floor(.5==(o=t.length**.5/20)?0:Math.round(o));var o;const i=Math.max(5,Math.floor(Math.round((t=>Math.log(t)/Math.log(2))(t.length))));this.rpForest=ft(t,r,n,this.random);const h=function(t){if(t.length>0){const e=[];for(let r of t)e.push(...r.indices);return e}return[[-1]]}(this.rpForest),{indices:a,weights:l}=s(t,h,r,i);return{knnIndices:a,knnDistances:l}}fuzzySimplicialSet(t,e,r=1){const{knnIndices:s=[],knnDistances:n=[],localConnectivity:o}=this,{sigmas:i,rhos:h}=this.smoothKNNDistance(n,e,o),{rows:a,cols:l,vals:u}=this.computeMembershipStrengths(s,n,i,h),c=[t.length,t.length],f=new tt(a,l,u,c),m=et(f),g=rt(f,m),p=nt(st(f,m),g);return st(ot(p,r),ot(g,1-r))}categoricalSimplicialSetIntersection(t,e,r,s=1){let n=function(t,e,r=1,s=5){return t.map(((t,n,o)=>-1===e[n]||-1===e[o]?t*Math.exp(-r):e[n]!==e[o]?t*Math.exp(-s):t))}(t,e,s,r);return n=it(n),function(t){const e=et(t=ht(t,"max"));return it(t=st(t,nt(e,rt(e,t))))}(n)}smoothKNNDistance(t,e,r=1,s=64,n=1){const o=Math.log(e)/Math.log(2)*n,i=L(t.length),h=L(t.length);for(let e=0;e<t.length;e++){let n=0,a=1/0,l=1;const u=t[e],c=u.filter((t=>t>0));if(c.length>=r){let t=Math.floor(r),s=r-t;t>0?(i[e]=c[t-1],s>Xt&&(i[e]+=s*(c[t]-c[t-1]))):i[e]=s*c[0]}else c.length>0&&(i[e]=W(c));for(let r=0;r<s;r++){let r=0;for(let s=1;s<t[e].length;s++){const n=t[e][s]-i[e];r+=n>0?Math.exp(-n/l):1}if(Math.abs(r-o)<Xt)break;r>o?(a=l,l=(n+a)/2):(n=l,a===1/0?l*=2:l=(n+a)/2)}if(h[e]=l,i[e]>0){const t=$(u);h[e]<Qt*t&&(h[e]=Qt*t)}else{const r=$(t.map($));h[e]<Qt*r&&(h[e]=Qt*r)}}return{sigmas:h,rhos:i}}computeMembershipStrengths(t,e,r,s){const n=t.length,o=t[0].length,i=L(n*o),h=L(n*o),a=L(n*o);for(let l=0;l<n;l++)for(let n=0;n<o;n++){let u=0;-1!==t[l][n]&&(u=t[l][n]===l?0:e[l][n]-s[l]<=0?1:Math.exp(-(e[l][n]-s[l])/r[l]),i[l*o+n]=l,h[l*o+n]=t[l][n],a[l*o+n]=u)}return{rows:i,cols:h,vals:a}}initializeSimplicialSetEmbedding(){const t=this.getNEpochs(),{nComponents:e}=this,r=this.graph.getValues();let s=0;for(let t=0;t<r.length;t++){const e=r[t];s<r[t]&&(s=e)}const n=this.graph.map((e=>e<s/t?0:e));this.embedding=L(n.nRows).map((()=>L(e).map((()=>20*V(this.random)-10))));const o=[],i=[],h=[],a=n.getAll();for(let t=0;t<a.length;t++){const e=a[t];e.value&&(o.push(e.value),h.push(e.row),i.push(e.col))}return{head:i,tail:h,epochsPerSample:this.makeEpochsPerSample(o,t)}}makeEpochsPerSample(t,e){const r=q(t.length,-1),s=W(t),n=t.map((t=>t/s*e));return n.forEach(((t,s)=>{t>0&&(r[s]=e/n[s])})),r}assignOptimizationStateParameters(t){Object.assign(this.optimizationState,t)}prepareForOptimizationLoop(){const{repulsionStrength:t,learningRate:e,negativeSampleRate:r}=this,{epochsPerSample:s,headEmbedding:n,tailEmbedding:o}=this.optimizationState,i=n[0].length,h=n.length===o.length,a=s.map((t=>t/r)),l=[...a],u=[...s];this.assignOptimizationStateParameters({epochOfNextSample:u,epochOfNextNegativeSample:l,epochsPerNegativeSample:a,moveOther:h,initialAlpha:e,alpha:e,gamma:t,dim:i})}initializeOptimization(){const t=this.embedding,e=this.embedding,{head:r,tail:s,epochsPerSample:n}=this.optimizationState,o=this.getNEpochs(),i=this.graph.nCols,{a:h,b:a}=function(t,e){const r=function(t,e,r){return O(300).map(((t,r)=>0+r*((e-0)/299)))}(0,3*t).map((t=>t<e?1:t)),s=L(r.length).map(((s,n)=>r[n]>=e?Math.exp(-(r[n]-e)/t):s)),n={x:r,y:s},{parameterValues:o}=function(t,e,r={}){let{maxIterations:s=100,gradientDifference:n=.1,damping:o=0,errorTolerance:i=.01,minValues:h,maxValues:a,initialValues:l}=r;if(o<=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(!Mt(t.x)||t.x.length<2||!Mt(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(!Mt(u))throw new Error("initialValues must be an array");let f,m=xt(t,u,e),g=m<=i;for(f=0;f<s&&!g;f++){u=Jt(t,u,o,n,e);for(let t=0;t<c;t++)u[t]=Math.min(Math.max(h[t],u[t]),a[t]);if(m=xt(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}),[i,h]=o;return{a:i,b:h}}(this.spread,this.minDist);this.assignOptimizationStateParameters({headEmbedding:t,tailEmbedding:e,head:r,tail:s,epochsPerSample:n,a:h,b:a,nEpochs:o,nVertices:i})}optimizeLayoutStep(t){const{optimizationState:e}=this,{head:r,tail:s,headEmbedding:n,tailEmbedding:o,epochsPerSample:i,epochOfNextSample:h,epochOfNextNegativeSample:a,epochsPerNegativeSample:l,moveOther:u,initialAlpha:c,alpha:f,gamma:m,a:g,b:p,dim:d,nEpochs:w,nVertices:y}=e;for(let e=0;e<i.length;e++){if(h[e]>t)continue;const c=r[e],w=s[e],b=n[c],M=o[w],x=se(b,M);let E=0;x>0&&(E=-2*g*p*Math.pow(x,p-1),E/=g*Math.pow(x,p)+1);for(let t=0;t<d;t++){const e=re(E*(b[t]-M[t]),4);b[t]+=e*f,u&&(M[t]+=-e*f)}h[e]+=i[e];const S=Math.floor((t-a[e])/l[e]);for(let t=0;t<S;t++){const t=P(y,this.random),e=o[t],r=se(b,e);let s=0;if(r>0)s=2*m*p,s/=(.001+r)*(g*Math.pow(r,p)+1);else if(c===t)continue;for(let t=0;t<d;t++){let r=4;s>0&&(r=re(s*(b[t]-e[t]),4)),b[t]+=r*f}}a[e]+=S*l[e]}return e.alpha=c*(1-t/w),e.currentEpoch+=1,n}optimizeLayoutAsync(t=(()=>!0)){return new Promise(((e,r)=>{const s=async()=>{try{const{nEpochs:r,currentEpoch:n}=this.optimizationState;this.embedding=this.optimizeLayoutStep(n);const o=this.optimizationState.currentEpoch,i=!1===t(o),h=o===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:n}=this.optimizationState;r=this.optimizeLayoutStep(n);const o=this.optimizationState.currentEpoch,i=!1===t(o);e=o===s||i}return r}getNEpochs(){const t=this.graph;if(this.nEpochs>0)return this.nEpochs;if(!t)return 200;const e=t.nRows;return e<=2500?500:e<=5e3?400:e<=7500?300:200}}function te(t,e){return Math.abs(t-e)}class ee{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 re(t,e){return t>e?e:t<-e?-e:t}function se(t,e){let r=0;for(let s=0;s<t.length;s++)r+=Math.pow(t[s]-e[s],2);return r}var ne,oe=r(6814);class ie{get data(){return this._data}get size(){return this._size}constructor(t,e){if(null==e){if(null==t)throw new Error("Arguments error: data or size is required.");if(this._data=t,this._size=(1+Math.sqrt(1+8*this._data.length))/2,this._size!=Math.floor(this._size))throw new Error(`Invalid data length ${this._data.length} leads to non integer size ${this._size}`)}else{this._size=e;const r=e*(e-1)/2;if(t){if(t.length!=r)throw new Error(`Invalid data length. Observations size ${e} requires data length ${r}.`);this._data=t}else this._data=new Float32Array(r)}}_linearizeIJ(t,e){if(!(t<e))throw new Error("i must be less than j");return this._size*t+e-Math.floor((t+2)*(t+1)/2)}get(t,e){return t==e?0:t<e?this._data[this._linearizeIJ(t,e)]:this._data[this._linearizeIJ(e,t)]}set(t,e,r){this._data[this._linearizeIJ(t,e)]=r}static calc(t,e){const r=t.length,s=new ie(void 0,r);for(let n=0;n<r;n++)for(let o=n+1;o<r;o++)s.set(n,o,(0,oe.k)(t[n])||(0,oe.k)(t[o])?1:e(t[n],t[o]));return s}square(){for(let t=0;t<this._data.length;t++)this._data[t]=this._data[t]**2}add(t){if(this._size!==t._size)throw new Error(`Matrices must have the same size. This size: ${this._size}, other size: ${t._size}`);for(let e=0;e<this._data.length;e++)this._data[e]+=t._data[e]}sqrt(){for(let t=0;t<this._data.length;t++)this._data[t]=Math.sqrt(this._data[t])}normalize(){let t=0,e=this._data[0];for(let r=0;r<this._data.length;r++)this._data[r]<t&&(t=this._data[r]),this._data[r]>e&&(e=this._data[r]);const r=e-t;for(let s=0;s<this._data.length;s++)this._data[s]=0===r?this._data[s]-t:(this._data[s]-t)/(e-t)}}class he{constructor(){this._workerCount=Math.max(navigator.hardwareConcurrency-2,1)}async calc(t,e,s,n={}){const o=t.length*(t.length-1)/2,i=Math.floor(o/this._workerCount),h=await this.getMinimalThreshold(t,e,n);s<h&&(console.log(`using threshold ${h}`),s=h),n.threshold=s;const a=new Array(this._workerCount),l=new Array(this._workerCount).fill(null).map((()=>new Worker(new URL(r.p+r.u(381),r.b))));for(let r=0;r<this._workerCount;r++)a[r]=new Promise(((h,a)=>{const u=r*i,c=r===this._workerCount-1?o:(r+1)*i;c<=u&&h({i:new Int32Array(0),j:new Int32Array(0),distance:new Float32Array(0),idx:r}),l[r].postMessage({values:t,startIdx:u,endIdx:c,threshold:s,fnName:e,opts:n}),l[r].onmessage=({data:{error:t,i:e,j:s,distance:n}})=>{t?(l[r].terminate(),a(t)):(l[r].terminate(),h({i:e,j:s,distance:n,idx:r}))}}));const u=await Promise.all(a),c=u.reduce(((t,e)=>t+e.i.length),0),f=new Int32Array(c),m=new Int32Array(c),g=new Float32Array(c);let p=0;for(const t of u)f.set(t.i,p),m.set(t.j,p),g.set(t.distance,p),p+=t.i.length;return{i:f,j:m,distance:g}}async getMinimalThreshold(t,e,s={}){const n=new Array(this._workerCount).fill(null).map((()=>new Worker(new URL(r.p+r.u(770),r.b)))),o=t.slice();for(let t=o.length-1;t>0;t--){const e=Math.floor(Math.random()*(t+1));[o[t],o[e]]=[o[e],o[t]]}try{const r=t.length*(t.length-1)/2,i=Math.floor(r/this._workerCount),h=Math.floor(Math.min(r/1e3,1e6)/this._workerCount),a=new Array(this._workerCount);for(let t=0;t<this._workerCount;t++)a[t]=new Promise(((a,l)=>{const u=t*i,c=t===this._workerCount-1?r:(t+1)*i;n[t].postMessage({values:o,startIdx:u,endIdx:c,sampleLength:h,fnName:e,opts:s}),n[t].onmessage=({data:{error:e,distance:r}})=>{n[t].terminate(),e?l(e):a({distance:r})}}));const l=await Promise.all(a),u=l.reduce(((t,e)=>t+e.distance.length),0),c=new Float32Array(u);let f=0;for(const t of l)c.set(t.distance,f),f+=t.distance.length;c.sort();let m=1-c[Math.floor(7e7/r*c.length)];return m=Math.min(Math.max(m,.3),.9),m}catch(t){return n?.forEach((t=>t?.terminate())),console.error(t),.5}}static calcSync(t,e,r,s){const n=[],o=[],i=[];let h=0,a=0,l=0;const u=t.length*(t.length-1)/2;for(;h<u;){const u=(0,oe.k)(t[a])||(0,oe.k)(t[l])?1:r(t[a],t[l]);(Object.values(y).some((t=>t===e))?1/(1+u):1-u)>=s&&(n.push(a),o.push(l),i.push(u)),h++,l++,l===t.length&&(a++,l=a+1)}return{i:new Int32Array(n),j:new Int32Array(o),distance:new Float32Array(i)}}}!function(t){t.UMAP="UMAP",t.T_SNE="t-SNE"}(ne||(ne={}));class ae{constructor(t){this.data=t.data}}const le={UMAP:class extends ae{constructor(t){super(t),h("distanceFname"in t),h("distanceFn"in t),this.distanceFnArgs=t?.distanceFnArgs,this.distanceFn=t.distanceFn,this.usingSparseMatrix=!!t.usingSparseMatrix||!!t.sparseMatrix,this.sparseMatrixThreshold=t.sparseMatrixThreshold??.8,this.transferedSparseMatrix=t.sparseMatrix,this.progressFunc=t.progressFunc,this.distanceFname=t.distanceFname,this.dmIndexFunc=c(this.data.length),this.vectors=new Array(this.data.length).fill(0).map(((t,e)=>e)),this.usingDistanceMatrix=!(!t.preCalculateDistanceMatrix&&this.data.length>2e4||this.usingSparseMatrix),this.usingDistanceMatrix?t.distanceFn=this._encodedDistanceMatrix.bind(this):this.usingSparseMatrix?t.distanceFn=this._encodedSparseMatrix.bind(this):t.distanceFn=this._encodedDistance.bind(this),this.data.length<15&&(t.nNeighbors=this.data.length-1),this.reducer=new Zt(t)}_encodedDistanceMatrix(t,e){return t===e?0:t>e?this.distanceMatrix[this.dmIndexFunc(e,t)]:this.distanceMatrix[this.dmIndexFunc(t,e)]}_encodedSparseMatrix(t,e){return this.sparseMatrix.get(t)?.get(e)??this.sparseMatrix.get(e)?.get(t)??1}_encodedDistance(t,e){return this.distanceFn(this.data[t],this.data[e])}async transform(t){if(this.usingDistanceMatrix)this.distanceMatrix=t?await(async()=>{const t=new f(!0,!1);try{const e=await t.calc(this.data,this.distanceFname,!0,this.distanceFnArgs);return t.terminate(),e}catch(e){throw t.terminate(),e}})():(()=>ie.calc(this.data,((t,e)=>this.distanceFn(t,e))).data)();else if(this.usingSparseMatrix){console.time("sparse matrix");let t=this.transferedSparseMatrix??await(new he).calc(this.data,this.distanceFname,this.sparseMatrixThreshold,this.distanceFnArgs);console.timeEnd("sparse matrix");const e=function(t,e,r,s,n){function o(t,e,r,s){if(r>t[t.length-1])return;let n=t.length-2;for(n=t.length-2;n>=0&&!(r>t[n]);n--);t.splice(t.length-1,1),t.splice(n+1,0,r),e.splice(e.length-1,1),e.splice(n+1,0,s)}console.time("knnGraph");const i=new Array(n).fill(null).map((()=>new Array(s).fill(1))),h=new Array(n).fill(null).map((()=>new Array(s).fill(1)));for(let s=0;s<t.length;s++)o(h[t[s]],i[t[s]],r[s],e[s]),o(h[e[s]],i[e[s]],r[s],t[s]);return console.timeEnd("knnGraph"),{knnIndexes:i,knnDistances:h}}(t.i,t.j,t.distance,this.reducer.neighbors,this.data.length);this.reducer.setPrecomputedKNN(e.knnIndexes,e.knnDistances),console.time("sparse matrix to map"),this.sparseMatrix=new Map;for(let e=0;e<t.i.length;++e){const r=t.i[e],s=t.j[e],n=t.distance[e];this.sparseMatrix.has(r)||this.sparseMatrix.set(r,new Map),this.sparseMatrix.get(r).set(s,n)}console.timeEnd("sparse matrix to map"),t.distance=null,t.i=null,t.j=null,t=null,await new Promise((t=>{setTimeout((()=>{t()}),500)}))}const e=await this.reducer.fitAsync(this.vectors,(t=>{this.progressFunc&&this.progressFunc(t,this.reducer.getNEpochs(),this.reducer.getEmbedding())}));return{embedding:(r=e,new Array(r.length).fill(0).map(((t,e)=>n.OW.from(r[e])))),...this.distanceMatrix?{distance:this.distanceMatrix}:{}};var r}},"t-SNE":class extends ae{constructor(t){super(t),this.reducer=new s.Z(t),this.iterations=t?.iterations??100,this.distanceFname=t.distanceFname,this.distanceFn=t.distanceFn}async transform(t){const e=t?await(async()=>{const t=new f(!0,!1);try{const e=await t.calc(this.data,this.distanceFname);return t.terminate(),e}catch(e){throw t.terminate(),e}})():(()=>{const t=ie.calc(this.data,((t,e)=>this.distanceFn(t,e)));return t.normalize(),t.data})(),r=function(t,e){const r=c(e);function s(t,e){const s=Number(t),n=Number(e);return r(s,n)}const n={get:(t,r,n)=>"length"===r?e:new Proxy(t,function(t){return{get:(e,r,n)=>t===r?0:e[Number(t)>Number(r)?s(r,t):s(t,r)]}}(r))};return new Proxy(t,n)}(e,this.data.length);this.reducer.initDataDist(r);for(let t=0;t<this.iterations;++t)this.reducer.step();return{distance:e,embedding:this.reducer.getSolution()}}},SPE:class extends ae{constructor(t){super(t),this.reducer=new m(t)}async transform(){const t=await this.reducer.embed(this.data);return{distance:this.reducer.distance,embedding:t}}},pSPE:class extends ae{constructor(t){super(t),this.reducer=new g(t)}async transform(){const t=await this.reducer.embed(this.data);return{distance:this.reducer.distance,embedding:t}}},OriginalSPE:class extends ae{constructor(t){super(t),this.reducer=new p(t)}async transform(){const t=await this.reducer.embed(this.data);return{distance:this.reducer.distance,embedding:t}}}};class ue{constructor(t,e,r,s){const n=new D(r).getMeasure(s?.distanceFnArgs);let o={};if("BitArray"==_[r])for(let e=0;e<t.length;++e)t[e]=new v.Z(t[e]._data,t[e]._length);o="UMAP"==e?{data:t,distanceFn:n,distanceFname:r,nEpochs:s?.cycles,...s}:"t-SNE"==e?{data:t,distanceFn:n,distanceFname:r,iterations:s?.cycles??void 0,...s}:{data:t,distance:n,distanceFunctionName:r,...s},this.reducer=new le[e](o)}async transform(t=!1,e){if(void 0===this.reducer)throw new Error("Reducer was not defined.");let{embedding:r,distance:s}=await this.reducer.transform(e);var o;return t&&(o=r,r=new Array(o[0].length).fill(0).map(((t,e)=>new n.OW(o.length).fill(0).map(((t,r)=>o[r][e]))))),{distance:s,embedding:r}}static availableMetricsByType(t){return Object.keys(T[t])}static get availableMethods(){return Object.keys(le)}static get availableMetrics(){let t=[];return Object.values(T).forEach((e=>{const r=Object.values(e);t=[...t,...r]})),t}}async function ce(t,e,r){t%5==0&&self.postMessage({epochNum:t,epochsLength:e,embedding:r})}self.onmessage=async({data:{columnData:t,method:e,measure:r,options:s,parallelDistanceWorkers:n}})=>{let o;try{o=await async function(t,e,r,s,n){const o=new ue(t,e,r,{...s,progressFunc:ce});return await o.transform(!0,n)}(t,e,r,s,n)}catch(t){o={error:t}}self.postMessage({error:o.error,distance:o.distance,embedding:o.embedding})}},7659:(t,e,r)=>{e.Z=void 0;var s=r(6215);Object.defineProperty(e,"Z",{enumerable:!0,get:function(){return s.TSNE}})},6215:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.TSNE=void 0,e.TSNE=class{constructor(t){this.returnV=!1,this.vValue=0,this.iter=0,t=t||{},this.perplexity=this.getopt(t,"perplexity",30),this.dim=this.getopt(t,"dim",2),this.epsilon=this.getopt(t,"epsilon",10)}assert(t,e){if(!t)throw e||"Assertion failed"}getopt(t,e,r){return t.hasOwnProperty(e)?t[e]:r}gaussRandom(){if(this.returnV)return this.returnV=!1,this.vValue;const t=2*Math.random()-1,e=2*Math.random()-1,r=t*t+e*e;if(0===r||r>1)return this.gaussRandom();const s=Math.sqrt(-2*Math.log(r)/r);return this.vValue=e*s,this.returnV=!0,t*s}randn(t,e){return t+this.gaussRandom()*e}zeros(t){if(void 0===t||isNaN(t))return[];if("undefined"==typeof ArrayBuffer){const e=new Array(t);for(let r=0;r<t;r++)e[r]=0;return e}return new Float64Array(t)}randn2d(t,e,r){const s=void 0!==r,n=[];for(let o=0;o<t;o++){const t=[];for(let n=0;n<e;n++)s?t.push(r):t.push(this.randn(0,1e-4));n.push(t)}return n}L2(t,e){const r=t.length;let s=0;for(let n=0;n<r;n++){const r=t[n],o=e[n];s+=(r-o)*(r-o)}return s}xtod(t){const e=t.length,r=this.zeros(e*e);for(let s=0;s<e;s++)for(let n=s+1;n<e;n++){const o=this.L2(t[s],t[n]);r[s*e+n]=o,r[n*e+s]=o}return r}d2p(t,e,r){const s=Math.sqrt(t.length),n=Math.floor(s);this.assert(n===s,"D should have square number of elements.");const o=Math.log(e),i=this.zeros(n*n),h=this.zeros(n);for(let e=0;e<n;e++){let s=-1/0,a=1/0,l=1,u=!1;const c=50;let f=0;for(;!u;){let i=0;for(let r=0;r<n;r++){let s=Math.exp(-t[e*n+r]*l);e===r&&(s=0),h[r]=s,i+=s}let m=0;for(let t=0;t<n;t++){let e;e=0===i?0:h[t]/i,h[t]=e,e>1e-7&&(m-=e*Math.log(e))}m>o?(s=l,a===1/0?l*=2:l=(l+a)/2):(a=l,s===-1/0?l/=2:l=(l+s)/2),f++,Math.abs(m-o)<r&&(u=!0),f>=c&&(u=!0)}for(let t=0;t<n;t++)i[e*n+t]=h[t]}const a=this.zeros(n*n),l=2*n;for(let t=0;t<n;t++)for(let e=0;e<n;e++)a[t*n+e]=Math.max((i[t*n+e]+i[e*n+t])/l,1e-100);return a}sign(t){return t>0?1:t<0?-1:0}initDataRaw(t){const e=t.length,r=t[0].length;this.assert(e>0," X is empty? You must have some data!"),this.assert(r>0," X[0] is empty? Where is the data?");const s=this.xtod(t);this.P=this.d2p(s,this.perplexity,1e-4),this.N=e,this.initSolution()}initDataDist(t){const e=t.length;this.assert(e>0," X is empty? You must have some data!");const r=this.zeros(e*e);for(let s=0;s<e;s++)for(let n=s+1;n<e;n++){const o=t[s][n];r[s*e+n]=o,r[n*e+s]=o}this.P=this.d2p(r,this.perplexity,1e-4),this.N=e,this.initSolution()}initSolution(){this.Y=this.randn2d(this.N,this.dim),this.gains=this.randn2d(this.N,this.dim,1),this.ystep=this.randn2d(this.N,this.dim,0),this.iter=0}getSolution(){return this.Y}step(){this.iter+=1;const t=this.N,e=this.costGrad(this.Y),r=e.cost,s=e.grad,n=this.zeros(this.dim);for(let e=0;e<t;e++)for(let t=0;t<this.dim;t++){const r=s[e][t],o=this.ystep[e][t],i=this.gains[e][t];let h=this.sign(r)===this.sign(o)?.8*i:i+.2;h<.01&&(h=.01),this.gains[e][t]=h;const a=(this.iter<250?.5:.8)*o-this.epsilon*h*s[e][t];this.ystep[e][t]=a,this.Y[e][t]+=a,n[t]+=this.Y[e][t]}for(let e=0;e<t;e++)for(let r=0;r<this.dim;r++)this.Y[e][r]-=n[r]/t;return r}debugGrad(){const t=this.N,e=this.costGrad(this.Y),r=(e.cost,e.grad),s=1e-5;for(let e=0;e<t;e++)for(let t=0;t<this.dim;t++){const n=this.Y[e][t];this.Y[e][t]=n+s;const o=this.costGrad(this.Y);this.Y[e][t]=n-s;const i=this.costGrad(this.Y),h=r[e][t],a=(o.cost-i.cost)/(2*s);console.log(e+","+t+": gradcheck analytic: "+h+" vs. numerical: "+a),this.Y[e][t]=n}}costGrad(t){const e=this.N,r=this.dim,s=this.P,n=this.iter<100?4:1,o=this.zeros(e*e);let i=0;for(let s=0;s<e;s++)for(let n=s+1;n<e;n++){let h=0;for(let e=0;e<r;e++){const r=t[s][e]-t[n][e];h+=r*r}const a=1/(1+h);o[s*e+n]=a,o[n*e+s]=a,i+=2*a}const h=e*e,a=this.zeros(h);for(let t=0;t<h;t++)a[t]=Math.max(o[t]/i,1e-100);let l=0;const u=[];for(let i=0;i<e;i++){const h=new Array(r);for(let t=0;t<r;t++)h[t]=0;for(let u=0;u<e;u++){l+=-s[i*e+u]*Math.log(a[i*e+u]);const c=4*(n*s[i*e+u]-a[i*e+u])*o[i*e+u];for(let e=0;e<r;e++)h[e]+=c*(t[i][e]-t[u][e])}u.push(h)}return{cost:l,grad:u}}}}},s={};function n(t){var e=s[t];if(void 0!==e)return e.exports;var o=s[t]={exports:{}};return r[t](o,o.exports,n),o.exports}n.m=r,n.x=()=>{var t=n.O(void 0,[1,172],(()=>n(3196)));return n.O(t)},t=[],n.O=(e,r,s,o)=>{if(!r){var i=1/0;for(u=0;u<t.length;u++){for(var[r,s,o]=t[u],h=!0,a=0;a<r.length;a++)(!1&o||i>=o)&&Object.keys(n.O).every((t=>n.O[t](r[a])))?r.splice(a--,1):(h=!1,o<i&&(i=o));if(h){t.splice(u--,1);var l=s();void 0!==l&&(e=l)}}return e}o=o||0;for(var u=t.length;u>0&&t[u-1][2]>o;u--)t[u]=t[u-1];t[u]=[r,s,o]},n.d=(t,e)=>{for(var r in e)n.o(e,r)&&!n.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:e[r]})},n.f={},n.e=t=>Promise.all(Object.keys(n.f).reduce(((e,r)=>(n.f[r](t,e),e)),[])),n.u=t=>t+".js",n.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||new Function("return this")()}catch(t){if("object"==typeof window)return window}}(),n.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),(()=>{var t;n.g.importScripts&&(t=n.g.location+"");var e=n.g.document;if(!t&&e&&(e.currentScript&&(t=e.currentScript.src),!t)){var r=e.getElementsByTagName("script");if(r.length)for(var s=r.length-1;s>-1&&!t;)t=r[s--].src}if(!t)throw new Error("Automatic publicPath is not supported in this browser");t=t.replace(/#.*$/,"").replace(/\?.*$/,"").replace(/\/[^\/]+$/,"/"),n.p=t})(),(()=>{n.b=self.location+"";var t={196:1};n.f.i=(e,r)=>{t[e]||importScripts(n.p+n.u(e))};var e=self.webpackChunkbio=self.webpackChunkbio||[],r=e.push.bind(e);e.push=e=>{var[s,o,i]=e;for(var h in o)n.o(o,h)&&(n.m[h]=o[h]);for(i&&i(n);s.length;)t[s.pop()]=1;r(e)}})(),e=n.x,n.x=()=>Promise.all([n.e(1),n.e(172)]).then(e);var o=n.x();bio=o})();
|
|
2
|
+
var bio;(()=>{"use strict";var t,e,r={3196:(t,e,r)=>{var s=r(7659),n=r(3659);function o(t){return Math.random()*t}function i(t){return Math.floor(o(t))}function h(t=!1,e="Assertion error."){if(!t)throw new Error(e)}function a(t,e,r=1){const s=t.length;h(s==e.length,"Vector lengths do not match.");const o=new n.OW(s);for(let s=0;s<t.length;++s)o[s]=t[s]+r*e[s];return o}function l(t,e,r=1){const s=function(t,e,r=0){return new Array(t).fill(r).map((()=>new n.OW(e).fill(r)))}(t,e);for(let n=0;n<t;++n)for(let t=0;t<e;++t)s[n][t]=o(r);return s}function u(t,e){const r=function(t){let e=0;for(let r=0;r<t.length;++r)e+=t[r];return e}(function(t){const e=t.length,r=new n.OW(e);for(let e=0;e<t.length;++e)r[e]=t[e]*t[e];return r}(a(t,e,-1)));return Math.sqrt(r)}function c(t){return(e,r)=>t*e+r-Math.floor((e+2)*(e+1)/2)}class f{constructor(t=!0,e=!0){const s=navigator.hardwareConcurrency;this._workerCount=t?Math.max(s-2,1):1,this._workers=new Array(this._workerCount).fill(null).map((()=>new Worker(new URL(r.p+r.u(868),r.b)))),this._terminateOnComplete=e}async calc(t,e,r=!0,s){return new Promise((async(n,o)=>{try{const o=t.length,i=new Array(this._workerCount),h=o*(o-1)/2;this._workerCount=Math.min(this._workerCount,h);const a=h/this._workerCount,l=new Float32Array(h);let u=0,c=1,f=0,m=Number.MIN_VALUE;for(let r=0;r<this._workerCount;r++){const n=Math.floor(r*a),g=r===this._workerCount-1?h:Math.floor((r+1)*a),p=u,d=c;r!==this._workerCount-1&&(u=o-2-Math.floor(Math.sqrt(-8*g+4*o*(o-1)-7)/2-.5),c=g-o*u+Math.floor((u+1)*(u+2)/2)),this._workers[r].postMessage({values:t,fnName:e,startRow:p,startCol:d,chunckSize:g-n,opts:s}),i[r]=new Promise(((t,e)=>{this._workers[r].onmessage=({data:{error:s,distanceMatrixData:o,min:i,max:h}})=>{this._terminateOnComplete&&this._workers[r].terminate(),s?e(s):(l.set(o,n),i<f&&(f=i),h>m&&(m=h),t())}}))}await Promise.all(i),r&&l.forEach(((t,e)=>{l[e]=(t-f)/(m-f)})),n(l)}catch(t){o(t)}}))}terminate(){this._workers.forEach((t=>t.terminate()))}}class m{constructor(t){this.steps=t?.steps??0,this.cycles=t?.cycles??1e6,this.cutoff=t?.cutoff??0,this.lambda=t?.lambda??2,this.dlambda=t?.dlambda??.01,this.lambda2=this.lambda/2,this.dlambda2=this.dlambda/2,this.epsilon=t?.epsilon??1e-10,this.distanceFunction=t?.distance??u,this.distance=new Float32Array,this.distanceFunctionName=t?.distanceFunctionName}async initDistance(t){this.dmIndexFunct=c(t.length);const e=new f(!0,!1);this.distance=await e.calc(t,this.distanceFunctionName),e.terminate()}calcDistance(t,e,r){return this.distance[this.dmIndexFunct(e,r)]}async embed(t){const e=t.length,r=l(e,m.dimension,40);let s=this.lambda2;0===this.steps&&(this.steps=t.length-1),await this.initDistance(t);for(let n=0;n<this.cycles;++n){for(let n=0;n<this.steps;++n){const n=i(e);let o=i(e);for(;n==o;)o=i(e);const h=r[n],l=r[o],c=this.calcDistance(t,n,o),f=u(h,l);if(0==this.cutoff||c<=this.cutoff||f<c){const t=s*(c-f)/(f+this.epsilon),e=a(h,l,-1);r[n]=a(h,e,t),r[o]=a(l,e,-t)}}if(s-=this.dlambda2,s<=0)break}return r}}m.dimension=2;class g extends m{async embed(t){const e=t.length,r=l(e,g.dimension,40);let s=this.lambda;await this.initDistance(t);for(let n=0;n<this.cycles;++n){const n=i(e),o=r[n];for(let i=0;i<e;++i){if(n==i)continue;const e=r[i],h=this.calcDistance(t,n,i),l=u(o,e);if(0==this.cutoff||h<=this.cutoff||l<h){const t=s*(h-l)/(l+this.epsilon),n=a(o,e,-1);r[i]=a(e,n,-t)}}if(s-=this.dlambda,s<=0)break}return r}}class p extends m{constructor(t){super(t),this.cycles=t?.cycles??1e3,this.steps=t?.steps??1e5,this.radiusPercent=t?.radiusPercent??1,this.maxDistance=t?.maxDistance??null,this.maxDistanceSteps=t?.maxDistanceSteps??null}async embed(t){const e=t.length,r=l(e,p.dimension,40);if(await this.initDistance(t),null===this.maxDistanceSteps&&(this.maxDistanceSteps=e*Math.floor((e-1)/2)),null===this.maxDistance){this.maxDistance=-1e37;for(let r=0;r<this.maxDistanceSteps;r++){const r=i(e);let s=i(e);for(;r==s;)s=i(e);const n=this.calcDistance(t,r,s);n>this.maxDistance&&(this.maxDistance=n)}}let s=this.lambda;const n=0==this.radiusPercent?this.maxDistance:this.maxDistance*this.radiusPercent;for(let o=0;o<this.cycles;++o){for(let o=0;o<this.steps;++o){const o=i(e);let h=i(e);for(;o==h;)h=i(e);const l=r[o],c=r[h],f=this.calcDistance(t,o,h),m=u(l,c);if(f<=n||m<f){const t=.5*s*(f-m)/(m+this.epsilon),e=a(l,c,-1);r[o]=a(l,e,t),r[h]=a(c,e,-t)}}if(s-=(this.lambda-this.dlambda)/(this.cycles-1),s<this.dlambda)break}return r}}var d,w,y,b,M,x,E=r(1811),S=r(3979),v=r(9657);!function(t){t.Levenshtein="Levenshtein",t.JaroWinkler="Jaro-Winkler",t.Manhattan="Manhattan"}(d||(d={})),function(t){t.Euclidean="Euclidean"}(w||(w={})),function(t){t.Tanimoto="Tanimoto",t.Dice="Dice",t.Asymmetric="Asymmetric",t.BraunBlanquet="Braun-Blanquet",t.Cosine="Cosine",t.Kulczynski="Kulczynski",t.McConnaughey="Mc-Connaughey",t.RogotGoldberg="Rogot-Goldberg",t.Russel="Russel",t.Sokal="Sokal",t.Hamming="Hamming",t.Euclidean="Euclidean"}(y||(y={})),function(t){t.TanimotoIntArray="TanimotoIntArray"}(b||(b={})),function(t){t.Vector="Vector",t.String="String",t.BitArray="BitArray",t.MacroMolecule="MacroMolecule",t.Number="Number",t.IntArray="IntArray"}(M||(M={})),function(t){t.NumericDistance="NumericDistance"}(x||(x={}));var N=r(489);function k(t,e){const r=t.trueCount()+e.trueCount();if(0==r)return 1;const s=t.andWithCountBits(e,!0);return s/(r-s)}function C(t){return 0===t?3402823e32:1/t-1}y.Tanimoto,y.Dice,y.Asymmetric,y.BraunBlanquet,y.Cosine,y.Kulczynski,y.McConnaughey,y.RogotGoldberg,y.Russel,y.Sokal,y.Hamming,y.Euclidean,y.Tanimoto,y.Dice,y.Asymmetric,y.BraunBlanquet,y.Cosine,y.Kulczynski,y.McConnaughey,y.RogotGoldberg,y.Russel,y.Sokal,y.Hamming,y.Euclidean,y.Tanimoto,y.Dice,y.Cosine,y.Tanimoto,y.Asymmetric,y.Cosine,y.Sokal,N.U.HAMMING,N.U.LEVENSHTEIN,N.U.MONOMER_CHEMICAL_DISTANCE;const A={[w.Euclidean]:u},R={[d.Levenshtein]:E.T,[d.JaroWinkler]:S.H$,[d.Manhattan]:function(t,e){if(t.length!==e.length)return 1;{let r=0;for(let s=1;s<t.length;s++)r+=t[s]==e[s]?0:1;return r/t.length}}},I={[y.Tanimoto]:function(t,e){return C(k(t,e))},[y.Dice]:function(t,e){return C(function(t,e){const r=t.trueCount()+e.trueCount();return 0==r?0:2*t.andWithCountBits(e,!0)/r}(t,e))},[y.Asymmetric]:function(t,e){return C(function(t,e){const r=Math.min(t.trueCount(),e.trueCount());return 0==r?0:t.andWithCountBits(e,!0)/r}(t,e))},[y.BraunBlanquet]:function(t,e){return C(function(t,e){const r=Math.max(t.trueCount(),e.trueCount());return 0==r?0:t.andWithCountBits(e,!0)/r}(t,e))},[y.Cosine]:function(t,e){return C(function(t,e){const r=t.trueCount()*e.trueCount();return 0==r?0:t.andWithCountBits(e,!0)/Math.sqrt(r)}(t,e))},[y.Kulczynski]:function(t,e){return C(function(t,e){const r=t.trueCount()+e.trueCount(),s=t.trueCount()*e.trueCount();return 0==s?0:t.andWithCountBits(e,!0)*r/(2*s)}(t,e))},[y.McConnaughey]:function(t,e){return C(function(t,e){const r=t.trueCount()+e.trueCount(),s=t.trueCount()*e.trueCount();return 0==s?0:(t.andWithCountBits(e,!0)*r-s)/s}(t,e))},[y.RogotGoldberg]:function(t,e){return C(function(t,e){const r=t.andWithCountBits(e,!0),s=t.countBits(!0)+e.countBits(!0),n=t.length,o=n-s+r;return r==n||o==n?1:r/s+o/(2*n-s)}(t,e))},[y.Russel]:function(t,e){return C(function(t,e){return 0==t.length?0:t.andWithCountBits(e,!0)/t.length}(t,e))},[y.Sokal]:function(t,e){return C(function(t,e){const r=t.trueCount()+e.trueCount(),s=t.andWithCountBits(e,!0);return s/(2*r-3*s)}(t,e))},[y.Hamming]:function(t,e){return t.trueCount()+e.trueCount()-2*t.andWithCountBits(e,!0)},[y.Euclidean]:function(t,e){return Math.sqrt(t.trueCount()+e.trueCount()-2*t.andWithCountBits(e,!0))}},F={[b.TanimotoIntArray]:function(t,e){return C(k(new v.Z(t,32*t.length),new v.Z(e,32*e.length)))}},z={[x.NumericDistance]:function(t,e){return Math.abs(t-e)}},T={[M.Vector]:{[w.Euclidean]:A[w.Euclidean]},[M.String]:{[d.Levenshtein]:R[d.Levenshtein],[d.JaroWinkler]:R[d.JaroWinkler],[d.Manhattan]:R[d.Manhattan]},[M.BitArray]:{[y.Tanimoto]:I[y.Tanimoto],[y.Dice]:I[y.Dice],[y.Asymmetric]:I[y.Asymmetric],[y.BraunBlanquet]:I[y.BraunBlanquet],[y.Cosine]:I[y.Cosine],[y.Kulczynski]:I[y.Kulczynski],[y.McConnaughey]:I[y.McConnaughey],[y.RogotGoldberg]:I[y.RogotGoldberg],[y.Russel]:I[y.Russel],[y.Sokal]:I[y.Sokal]},[M.MacroMolecule]:{[N.U.HAMMING]:N.o[N.U.HAMMING],[N.U.LEVENSHTEIN]:N.o[N.U.LEVENSHTEIN],[N.U.NEEDLEMANN_WUNSCH]:N.o[N.U.NEEDLEMANN_WUNSCH],[N.U.MONOMER_CHEMICAL_DISTANCE]:N.o[N.U.MONOMER_CHEMICAL_DISTANCE]},[M.Number]:{[x.NumericDistance]:z[x.NumericDistance]},[M.IntArray]:{[b.TanimotoIntArray]:F[b.TanimotoIntArray]}},_=Object.keys(T).reduce(((t,e)=>{for(const r of Object.keys(T[e]))t[r]=e;return t}),{});function D(t){return"BitArray"==_[t]}class P{constructor(t){this.method=t,this.dataType=_[t]}getMeasure(t){const e=T;if(!e.hasOwnProperty(this.dataType)||!e[this.dataType].hasOwnProperty(this.method))throw new Error(`Unknown measure ${this.method} for data type ${this.dataType}`);return r=this.method,_[r]==M.MacroMolecule.toString()?e[this.dataType][this.method](t):e[this.dataType][this.method];var r}static getMetricByDataType(t){return Object.keys(T[t])}static get availableMeasures(){return Object.keys(T)}}function V(t,e){return Math.floor(e()*t)}function O(t){return t()}function j(t){const e=[];for(let r=0;r<t;r++)e.push(void 0);return e}function q(t){return j(t).map(((t,e)=>e))}function L(t,e){return j(t).map((()=>e))}function $(t){return L(t,0)}function W(t){return function(t){return t.reduce(((t,e)=>t+e))}(t)/t.length}function B(t){let e=0;for(let r=0;r<t.length;r++)e=t[r]>e?t[r]:e;return e}function U(t,e,r){const s=$(t);for(let n=0;n<t;n++){let t=!0;for(;t;){const o=V(e,r);let i=!1;for(let t=0;t<n;t++)if(o===s[t]){i=!0;break}i||(t=!1),s[n]=o}}return s}function G(t,e,r){const s=[];let n=0,o=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[o]),o+=1;s.push(e),n+=1}return s}function Y(t,e){const r=r=>j(t).map((()=>L(e,r))),s=[];return s.push(r(-1)),s.push(r(1/0)),s.push(r(0)),s}function K(t,e,r){const s=$(t);for(let n=0;n<t;n++){let t=!0,o=0;for(;t;){o=V(e,r);let i=!1;for(let t=0;t<n;t++)if(o===s[t]){i=!0;break}i||(t=!1)}s[n]=o}return s}function H(t,e,r,s,n){e=Math.floor(e);const o=t[0][e],i=t[1][e];if(t[2][e],r>=i[0])return 0;for(let t=0;t<o.length;t++)if(s===o[t])return 0;return J(t,e,r,s,n)}function J(t,e,r,s,n){const o=t[0][e],i=t[1][e],h=t[2][e];if(r>=i[0])return 0;i[0]=r,o[0]=s,h[0]=n;let a=0,l=0;for(;;){const e=2*a+1,s=e+1,n=t[0][0].length;if(e>=n)break;if(s>=n){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],o[a]=o[l],h[a]=h[l],a=l}return i[a]=r,o[a]=s,h[a]=n,1}function X(t,e,r,s,n){const o=Y(e,s);for(let s=0;s<e;s++)for(let e=0;e<r;e++){if(t[0][s][e]<0)continue;const r=t[0][s][e],i=t[2][s][e],h=O(n);H(o,s,h,r,i),H(o,r,h,s,i),t[2][s][e]=0}return o}function Q(t){const e=t[0],r=t[1];for(let t=0;t<e.length;t++){const s=e[t],n=r[t];for(let t=0;t<s.length-1;t++){const e=s.length-t-1,r=n.length-t-1,o=s[0];s[0]=s[e],s[e]=o;const i=n[0];n[0]=n[r],n[r]=i,Z(n,s,r,0)}}return{indices:e,weights:r}}function Z(t,e,r,s){for(;2*s+1<r;){const n=2*s+1,o=n+1;let i=s;if(t[i]<t[n]&&(i=n),o<r&&t[i]<t[o]&&(i=o),i===s)break;{const r=t[s];t[s]=t[i],t[i]=r;const n=e[s];e[s]=e[i],e[i]=n,s=i}}}function tt(t,e){const r=t[0][e],s=t[1][e],n=t[2][e];let o=1/0,i=-1;for(let t=0;t>r.length;t++)1===n[t]&&s[t]<o&&(o=s[t],i=t);return i>=0?(n[i]=0,Math.floor(r[i])):-1}class et{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 n=t[s],o=e[s];this.checkDims(n,o);const i=this.makeKey(n,o);this.entries.set(i,{value:r[s],row:n,col:o})}}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){const s=this.makeKey(t,e);return this.entries.has(s)?this.entries.get(s).value:r}getAll(t=!0){const e=new Array(this.entries.size).fill(null);let r=0;return this.entries.forEach((t=>{e[r++]=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){const e=new Float32Array(this.entries.size);let r=0;this.entries.forEach((s=>{e[r++]=t(s.value,s.row,s.col)}));const s=[this.nRows,this.nCols];return new et(this.getRows(),this.getCols(),e,s)}toArray(){const t=j(this.nRows).map((()=>$(this.nCols)));return this.entries.forEach((e=>{t[e.row][e.col]=e.value})),t}}function rt(t){const e=t.getRows(),r=t.getCols(),s=t.getValues(),n=r.length,o=new Int32Array(n),i=new Int32Array(n),h=new Float32Array(n);o.set(e),i.set(r),h.set(s);const a=[t.nCols,t.nRows];return new et(i,o,h,a)}function st(t,e){return ut(t,e,((t,e)=>t*e))}function nt(t,e){return ut(t,e,((t,e)=>t+e))}function ot(t,e){return ut(t,e,((t,e)=>t-e))}function it(t,e){return t.map((t=>t*e))}function ht(t){const e=new Set,r=t.getValues(),s=t.getRows(),n=t.getCols();for(let t=0;t<r.length;t++)0===r[t]&&e.add(t);const o=(t,r)=>!e.has(r),i=r.filter(o),h=s.filter(o),a=n.filter(o);return new et(h,a,i,t.getDims())}function at(t,e="l2"){const r=lt[e],s=new Map;t.forEach(((t,e,r)=>{const n=s.get(e)||[];n.push(r),s.set(e,n)}));const n=new et([],[],[],t.getDims());for(let e of s.keys()){const o=s.get(e).sort(),i=r(o.map((r=>t.get(e,r))));for(let t=0;t<i.length;t++)n.set(e,o[t],i[t])}return n}const lt={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 ut(t,e,r){const s=new Set,n=[],o=[],i=[],h=(s,h)=>{n.push(s),o.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],n=`${e}:${r}`;s.add(n),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],n=`${e}:${r}`;s.has(n)||h(e,r)}const g=[t.nRows,t.nCols];return new et(n,o,i,g)}function ct(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=[],n=[];let o=-1;for(let t=0;t<e.length;t++){const{row:i,col:h,value:a}=e[t];i!==o&&(o=i,n.push(t)),r.push(h),s.push(a)}return{indices:r,values:s,indptr:n}}class ft{constructor(t,e,r,s){this.hyperplanes=t,this.offsets=e,this.children=r,this.indices=s}}function mt(t,e,r,s){const n=Math.max(10,e),o=q(r).map(((e,r)=>function(t,e=30,r,s){return gt(t,q(t.length),e,r,s)}(t,n,r,s))),i=o.map((t=>function(t,e){const r=dt(t),s=wt(t),n=q(r).map((()=>t.hyperplane?1:0)),o=$(r),i=q(r).map((()=>[-1,-1])),h=q(s).map((()=>q(e).map((()=>-1))));return pt(t,n,o,i,h,0,0),new ft(n,o,i,h)}(t,n)));return i}function gt(t,e,r=30,s,n){if(e.length>r){const o=function(t,e,r){let s=V(e.length,r),n=V(e.length,r);n+=s===n?1:0,n%=e.length;const o=e[s],i=e[n];let h=0,a=0;a=t[o]-t[i],h-=a*(t[o]+t[i])/2;let l=0,u=0;const c=$(e.length);for(let s=0;s<e.length;s++){let n=h;n+=a*t[e[s]],0===n?(c[s]=V(2,r),0===c[s]?l+=1:u+=1):n>0?(c[s]=0,l+=1):(c[s]=1,u+=1)}const f=$(l),m=$(u);l=0,u=0;for(let t=0;t<c.length;t++)0===c[t]?(f[l]=e[t],l+=1):(m[u]=e[t],u+=1);return{indicesLeft:f,indicesRight:m,hyperplane:a,offset:h}}(t,e,n),{indicesLeft:i,indicesRight:h,hyperplane:a,offset:l}=o;return{leftChild:gt(t,i,r,s+1,n),rightChild:gt(t,h,r,s+1,n),isLeaf:!1,hyperplane:a,offset:l}}return{indices:e,isLeaf:!0}}function pt(t,e,r,s,n,o,i){if(t.isLeaf)return s[o][0]=-i,n[i].splice(0,t.indices.length,...t.indices),{nodeNum:o,leafNum:i+=1};{e[o]=t.hyperplane,r[o]=t.offset,s[o][0]=o+1;const h=o;let a=pt(t.leftChild,e,r,s,n,o+1,i);return o=a.nodeNum,i=a.leafNum,s[h][1]=o+1,a=pt(t.rightChild,e,r,s,n,o+1,i),{nodeNum:a.nodeNum,leafNum:a.leafNum}}}function dt(t){return t.isLeaf?1:1+dt(t.leftChild)+dt(t.rightChild)}function wt(t){return t.isLeaf?1:wt(t.leftChild)+wt(t.rightChild)}function yt(t,e,r,s){let n=e;return n+=t*r,0===n?V(2,s):n>0?0:1}function bt(t,e,r){let s=0;for(;e.children[s][0]>0;)s=0===yt(e.hyperplanes[s],e.offsets[s],t,r)?e.children[s][0]:e.children[s][1];const n=-1*e.children[s][0];return e.indices[n]}const Mt=Object.prototype.toString;function xt(t){return Mt.call(t).endsWith("Array]")}function Et(t,e,r){let s=0;const n=r(e);for(let e=0;e<t.x.length;e++)s+=Math.abs(t.y[e]-n(t.x[e]));return s}const St=Object.prototype.toString;function vt(t){return St.call(t).endsWith("Array]")}const Nt=Object.prototype.toString;function kt(t){return Nt.call(t).endsWith("Array]")}const Ct=Object.prototype.toString;const At=Object.prototype.toString;function Rt(t){var e,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!kt(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(!kt(r.output))throw new TypeError("output option must be an array if specified");e=r.output}else e=new Array(t.length);var s=function(t){var e,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(e=t,!At.call(e).endsWith("Array]"))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");var s=r.fromIndex,n=void 0===s?0:s,o=r.toIndex,i=void 0===o?t.length:o;if(n<0||n>=t.length||!Number.isInteger(n))throw new Error("fromIndex must be a positive integer smaller than length");if(i<=n||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[n],a=n+1;a<i;a++)t[a]<h&&(h=t[a]);return h}(t),n=function(t){var e,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(e=t,!Ct.call(e).endsWith("Array]"))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");var s=r.fromIndex,n=void 0===s?0:s,o=r.toIndex,i=void 0===o?t.length:o;if(n<0||n>=t.length||!Number.isInteger(n))throw new Error("fromIndex must be a positive integer smaller than length");if(i<=n||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[n],a=n+1;a<i;a++)t[a]>h&&(h=t[a]);return h}(t);if(s===n)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");var o=r.min,i=void 0===o?r.autoMinMax?s:0:o,h=r.max,a=void 0===h?r.autoMinMax?n:1:h;if(i>=a)throw new RangeError("min option must be smaller than max option");for(var l=(a-i)/(n-s),u=0;u<t.length;u++)e[u]=(t[u]-s)*l+i;return e}const It=" ".repeat(2),Ft=" ".repeat(4);function zt(t,e={}){const{maxRows:r=15,maxColumns:s=10,maxNumSize:n=8,padMinus:o="auto"}=e;return`${t.constructor.name} {\n${It}[\n${Ft}${function(t,e,r,s,n){const{rows:o,columns:i}=t,h=Math.min(o,e),a=Math.min(i,r),l=[];if("auto"===n){n=!1;t:for(let e=0;e<h;e++)for(let r=0;r<a;r++)if(t.get(e,r)<0){n=!0;break t}}for(let e=0;e<h;e++){let r=[];for(let o=0;o<a;o++)r.push(Tt(t.get(e,o),s,n));l.push(`${r.join(" ")}`)}return a!==i&&(l[l.length-1]+=` ... ${i-r} more columns`),h!==o&&l.push(`... ${o-e} more rows`),l.join(`\n${Ft}`)}(t,r,s,n,o)}\n${It}]\n${It}rows: ${t.rows}\n${It}columns: ${t.columns}\n}`}function Tt(t,e,r){return(t>=0&&r?` ${_t(t,e-1)}`:_t(t,e)).padEnd(e)}function _t(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 n=t.toExponential(e);return n.length>e&&(n=t.toExponential(Math.max(0,e-(n.length-e)))),n.slice(0)}function Dt(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 Pt(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 Vt(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 Ot(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 jt(t,e,r,s,n){if(5!==arguments.length)throw new RangeError("expected 4 arguments");if(Lt("startRow",e),Lt("endRow",r),Lt("startColumn",s),Lt("endColumn",n),e>r||s>n||e<0||e>=t.rows||r<0||r>=t.rows||s<0||s>=t.columns||n<0||n>=t.columns)throw new RangeError("Submatrix indices are out of range")}function qt(t,e=0){let r=[];for(let s=0;s<t;s++)r.push(e);return r}function Lt(t,e){if("number"!=typeof e)throw new TypeError(`${t} must be a number`)}function $t(t){if(t.isEmpty())throw new Error("Empty matrix has no elements to index")}class Wt{static from1DArray(t,e,r){if(t*e!==r.length)throw new RangeError("data length does not match given dimensions");let s=new Ut(t,e);for(let n=0;n<t;n++)for(let t=0;t<e;t++)s.set(n,t,r[n*e+t]);return s}static rowVector(t){let e=new Ut(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 Ut(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 Ut(t,e)}static ones(t,e){return new Ut(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 n=new Ut(t,e);for(let r=0;r<t;r++)for(let t=0;t<e;t++)n.set(r,t,s());return n}static randInt(t,e,r={}){if("object"!=typeof r)throw new TypeError("options must be an object");const{min:s=0,max:n=1e3,random:o=Math.random}=r;if(!Number.isInteger(s))throw new TypeError("min must be an integer");if(!Number.isInteger(n))throw new TypeError("max must be an integer");if(s>=n)throw new RangeError("min must be smaller than max");let i=n-s,h=new Ut(t,e);for(let r=0;r<t;r++)for(let t=0;t<e;t++){let e=s+Math.round(o()*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),n=this.zeros(t,e);for(let t=0;t<s;t++)n.set(t,t,r);return n}static diag(t,e,r){let s=t.length;void 0===e&&(e=s),void 0===r&&(r=e);let n=Math.min(s,e,r),o=this.zeros(e,r);for(let e=0;e<n;e++)o.set(e,e,t[e]);return o}static min(t,e){t=this.checkMatrix(t),e=this.checkMatrix(e);let r=t.rows,s=t.columns,n=new Ut(r,s);for(let o=0;o<r;o++)for(let r=0;r<s;r++)n.set(o,r,Math.min(t.get(o,r),e.get(o,r)));return n}static max(t,e){t=this.checkMatrix(t),e=this.checkMatrix(e);let r=t.rows,s=t.columns,n=new this(r,s);for(let o=0;o<r;o++)for(let r=0;r<s;r++)n.set(o,r,Math.max(t.get(o,r),e.get(o,r)));return n}static checkMatrix(t){return Wt.isMatrix(t)?t:new Ut(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,s=!0,n=!1;for(;t<this.rows&&s;){for(e=0,n=!1;e<this.columns&&!1===n;)0===this.get(t,e)?e++:1===this.get(t,e)&&e>r?(n=!0,r=e):(s=!1,n=!0);t++}return s}isReducedEchelonForm(){let t=0,e=0,r=-1,s=!0,n=!1;for(;t<this.rows&&s;){for(e=0,n=!1;e<this.columns&&!1===n;)0===this.get(t,e)?e++:1===this.get(t,e)&&e>r?(n=!0,r=e):(s=!1,n=!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 n=e;n<t.rows;n++)t.get(n,r)>t.get(s,r)&&(s=n);if(0===t.get(s,r))r++;else{t.swapRows(e,s);let n=t.get(e,r);for(let s=r;s<t.columns;s++)t.set(e,s,t.get(e,s)/n);for(let s=e+1;s<t.rows;s++){let n=t.get(s,r)/t.get(e,r);t.set(s,r,0);for(let o=r+1;o<t.columns;o++)t.set(s,o,t.get(s,o)-t.get(e,o)*n)}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 n=0,o=!1;for(;n<r&&!1===o;)1===t.get(s,n)?o=!0:n++;for(let r=0;r<s;r++){let o=t.get(r,n);for(let i=n;i<e;i++){let e=t.get(r,i)-o*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 Ut(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){Dt(this,t);let e=[];for(let r=0;r<this.columns;r++)e.push(this.get(t,r));return e}getRowVector(t){return Ut.rowVector(this.getRow(t))}setRow(t,e){Dt(this,t),e=Vt(this,e);for(let r=0;r<this.columns;r++)this.set(t,r,e[r]);return this}swapRows(t,e){Dt(this,t),Dt(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){Pt(this,t);let e=[];for(let r=0;r<this.rows;r++)e.push(this.get(r,t));return e}getColumnVector(t){return Ut.columnVector(this.getColumn(t))}setColumn(t,e){Pt(this,t),e=Ot(this,e);for(let r=0;r<this.rows;r++)this.set(r,t,e[r]);return this}swapColumns(t,e){Pt(this,t),Pt(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=Vt(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=Vt(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=Vt(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=Vt(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=Ot(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=Ot(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=Ot(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=Ot(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){Dt(this,t);for(let r=0;r<this.columns;r++)this.set(t,r,this.get(t,r)*e);return this}mulColumn(t,e){Pt(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(){$t(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(){$t(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(Dt(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){Dt(this,t),$t(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(Dt(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){Dt(this,t),$t(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(Pt(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){Pt(this,t),$t(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(Pt(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){Pt(this,t),$t(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"){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){Wt.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=Ut.checkMatrix(t);let e=this.rows,r=this.columns,s=t.columns,n=new Ut(e,s),o=new Float64Array(r);for(let i=0;i<s;i++){for(let e=0;e<r;e++)o[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)*o[s];n.set(t,i,e)}}return n}strassen2x2(t){t=Ut.checkMatrix(t);let e=new Ut(2,2);const r=this.get(0,0),s=t.get(0,0),n=this.get(0,1),o=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*(o-l),m=a*(h-s),g=(r+n)*l,p=u+m-g+(n-a)*(h+l),d=f+g,w=c+m,y=u-c+f+(i-r)*(s+o);return e.set(0,0,p),e.set(0,1,d),e.set(1,0,w),e.set(1,1,y),e}strassen3x3(t){t=Ut.checkMatrix(t);let e=new Ut(3,3);const r=this.get(0,0),s=this.get(0,1),n=this.get(0,2),o=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),p=t.get(1,1),d=t.get(1,2),w=t.get(2,0),y=t.get(2,1),b=t.get(2,2),M=(r-o)*(-f+p),x=(-r+o+i)*(c-f+p),E=(o+i)*(-c+f),S=r*c,v=(-r+a+l)*(c-m+d),N=(-r+a)*(m-d),k=(a+l)*(-c+m),C=(-n+l+u)*(p+w-y),A=(n-u)*(p-y),R=n*w,I=(l+u)*(-w+y),F=(-n+i+h)*(d+w-b),z=(n-h)*(d-b),T=(i+h)*(-w+b),_=S+R+s*g,D=(r+s+n-o-i-l-u)*p+x+E+S+C+R+I,P=S+v+k+(r+s+n-i-h-a-l)*d+R+F+T,V=M+i*(-c+f+g-p-d-w+b)+x+S+R+F+z,O=M+x+E+S+h*y,j=R+F+z+T+o*m,q=S+v+N+l*(-c+m+g-p-d-w+y)+C+A+R,L=C+A+R+I+a*f,$=S+v+N+k+u*b;return e.set(0,0,_),e.set(0,1,D),e.set(0,2,P),e.set(1,0,V),e.set(1,1,O),e.set(1,2,j),e.set(2,0,q),e.set(2,1,L),e.set(2,2,$),e}mmulStrassen(t){t=Ut.checkMatrix(t);let e=this.clone(),r=e.rows,s=e.columns,n=t.rows,o=t.columns;function i(t,e,r){let s=t.rows,n=t.columns;if(s===e&&n===r)return t;{let s=Wt.zeros(e,r);return s=s.setSubMatrix(t,0,0),s}}s!==n&&console.warn(`Multiplying ${r} x ${s} and ${n} x ${o} matrix: dimensions do not match.`);let h=Math.max(r,n),a=Math.max(s,o);return e=i(e,h,a),function t(e,r,s,n){if(s<=512||n<=512)return e.mmul(r);s%2==1&&n%2==1?(e=i(e,s+1,n+1),r=i(r,s+1,n+1)):s%2==1?(e=i(e,s+1,n),r=i(r,s+1,n)):n%2==1&&(e=i(e,s,n+1),r=i(r,s,n+1));let o=parseInt(e.rows/2,10),h=parseInt(e.columns/2,10),a=e.subMatrix(0,o-1,0,h-1),l=r.subMatrix(0,o-1,0,h-1),u=e.subMatrix(0,o-1,h,e.columns-1),c=r.subMatrix(0,o-1,h,r.columns-1),f=e.subMatrix(o,e.rows-1,0,h-1),m=r.subMatrix(o,r.rows-1,0,h-1),g=e.subMatrix(o,e.rows-1,h,e.columns-1),p=r.subMatrix(o,r.rows-1,h,r.columns-1),d=t(Wt.add(a,g),Wt.add(l,p),o,h),w=t(Wt.add(f,g),l,o,h),y=t(a,Wt.sub(c,p),o,h),b=t(g,Wt.sub(m,l),o,h),M=t(Wt.add(a,u),p,o,h),x=t(Wt.sub(f,a),Wt.add(l,c),o,h),E=t(Wt.sub(u,g),Wt.add(m,p),o,h),S=Wt.add(d,b);S.sub(M),S.add(E);let v=Wt.add(y,M),N=Wt.add(w,b),k=Wt.sub(d,w);k.add(y),k.add(x);let C=Wt.zeros(2*S.rows,2*S.columns);return C=C.setSubMatrix(S,0,0),C=C.setSubMatrix(v,S.rows,0),C=C.setSubMatrix(N,0,S.columns),C=C.setSubMatrix(k,S.rows,S.columns),C.subMatrix(0,s-1,0,n-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 Ut(this.rows,this.columns);for(let t=0;t<this.rows;t++){const n=this.getRow(t);n.length>0&&Rt(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 Ut(this.rows,this.columns);for(let t=0;t<this.columns;t++){const n=this.getColumn(t);n.length&&Rt(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=Ut.checkMatrix(t);let e=this.rows,r=this.columns,s=t.rows,n=t.columns,o=new Ut(e*s,r*n);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<n;h++)o.set(s*i+r,n*e+h,this.get(i,e)*t.get(r,h));return o}kroneckerSum(t){if(t=Ut.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(Ut.eye(r,r)),n=Ut.eye(e,e).kroneckerProduct(t);return s.add(n)}transpose(){let t=new Ut(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=Bt){for(let e=0;e<this.rows;e++)this.setRow(e,this.getRow(e).sort(t));return this}sortColumns(t=Bt){for(let e=0;e<this.columns;e++)this.setColumn(e,this.getColumn(e).sort(t));return this}subMatrix(t,e,r,s){jt(this,t,e,r,s);let n=new Ut(e-t+1,s-r+1);for(let o=t;o<=e;o++)for(let e=r;e<=s;e++)n.set(o-t,e-r,this.get(o,e));return n}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 Ut(t.length,r-e+1);for(let n=0;n<t.length;n++)for(let o=e;o<=r;o++){if(t[n]<0||t[n]>=this.rows)throw new RangeError(`Row index out of range: ${t[n]}`);s.set(n,o-e,this.get(t[n],o))}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 Ut(r-e+1,t.length);for(let n=0;n<t.length;n++)for(let o=e;o<=r;o++){if(t[n]<0||t[n]>=this.columns)throw new RangeError(`Column index out of range: ${t[n]}`);s.set(o-e,n,this.get(o,t[n]))}return s}setSubMatrix(t,e,r){if((t=Ut.checkMatrix(t)).isEmpty())return this;jt(this,e,e+t.rows-1,r,r+t.columns-1);for(let s=0;s<t.rows;s++)for(let n=0;n<t.columns;n++)this.set(e+s,r+n,t.get(s,n));return this}selection(t,e){!function(t,e){if(!vt(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(!vt(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 Ut(t.length,e.length);for(let s=0;s<t.length;s++){let n=t[s];for(let t=0;t<e.length;t++){let o=e[t];r.set(s,t,this.get(n,o))}}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 Ut(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=qt(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=qt(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=qt(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=qt(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:s=this.mean(t)}=e;if("boolean"!=typeof r)throw new TypeError("unbiased must be a boolean");switch(t){case"row":if(!vt(s))throw new TypeError("mean must be an array");return function(t,e,r){const s=t.rows,n=t.columns,o=[];for(let i=0;i<s;i++){let s=0,h=0,a=0;for(let e=0;e<n;e++)a=t.get(i,e)-r[i],s+=a,h+=a*a;e?o.push((h-s*s/n)/(n-1)):o.push((h-s*s/n)/n)}return o}(this,r,s);case"column":if(!vt(s))throw new TypeError("mean must be an array");return function(t,e,r){const s=t.rows,n=t.columns,o=[];for(let i=0;i<n;i++){let n=0,h=0,a=0;for(let e=0;e<s;e++)a=t.get(e,i)-r[i],n+=a,h+=a*a;e?o.push((h-n*n/s)/(s-1)):o.push((h-n*n/s)/s)}return o}(this,r,s);case void 0:if("number"!=typeof s)throw new TypeError("mean must be a number");return function(t,e,r){const s=t.rows,n=t.columns,o=s*n;let i=0,h=0,a=0;for(let e=0;e<s;e++)for(let s=0;s<n;s++)a=t.get(e,s)-r,i+=a,h+=a*a;return e?(h-i*i/o)/(o-1):(h-i*i/o)/o}(this,r,s);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(!vt(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(!vt(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+=Math.pow(t.get(r,e),2)/(t.columns-1);e.push(Math.sqrt(s))}return e}(this);else if(!vt(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+=Math.pow(t.get(e,r),2)/(t.rows-1);e.push(Math.sqrt(s))}return e}(this);else if(!vt(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 n=0;n<t.rows;n++)r+=Math.pow(t.get(n,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 zt(this,t)}}function Bt(t,e){return t-e}Wt.prototype.klass="Matrix","undefined"!=typeof Symbol&&(Wt.prototype[Symbol.for("nodejs.util.inspect.custom")]=function(){return zt(this)}),Wt.random=Wt.rand,Wt.randomInt=Wt.randInt,Wt.diagonal=Wt.diag,Wt.prototype.diagonal=Wt.prototype.diag,Wt.identity=Wt.eye,Wt.prototype.negate=Wt.prototype.neg,Wt.prototype.tensorProduct=Wt.prototype.kroneckerProduct;class Ut extends Wt{constructor(t,e){if(super(),Ut.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(!vt(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(!r[s].every((t=>"number"==typeof t)))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 Dt(this,t),this.data.splice(t,1),this.rows-=1,this}addRow(t,e){return void 0===e&&(e=t,t=this.rows),Dt(this,t,!0),e=Float64Array.from(Vt(this,e)),this.data.splice(t,0,e),this.rows+=1,this}removeColumn(t){Pt(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),Pt(this,t,!0),e=Ot(this,e);for(let r=0;r<this.rows;r++){const s=new Float64Array(this.columns+1);let n=0;for(;n<t;n++)s[n]=this.data[r][n];for(s[n++]=e[r];n<this.columns+1;n++)s[n]=this.data[r][n-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,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}}(Wt,Ut);class Gt extends Wt{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 Yt{constructor(t){let e,r,s,n,o,i,h,a,l,u=(t=Gt.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),o=0,s=0;s<l;s++)o+=u.get(e,s)*a[s];a[e]-=o,u.set(e,r,a[e])}for(n=r,e=r+1;e<c;e++)Math.abs(a[e])>Math.abs(a[n])&&(n=e);if(n!==r){for(s=0;s<f;s++)i=u.get(n,s),u.set(n,s,u.get(r,s)),u.set(r,s,i);h=m[n],m[n]=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=Ut.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,n,o=t.columns,i=t.subMatrixRow(this.pivotVector,0,o-1),h=e.columns;for(n=0;n<h;n++)for(r=n+1;r<h;r++)for(s=0;s<o;s++)i.set(r,s,i.get(r,s)-i.get(n,s)*e.get(r,n));for(n=h-1;n>=0;n--){for(s=0;s<o;s++)i.set(n,s,i.get(n,s)/e.get(n,n));for(r=0;r<n;r++)for(s=0;s<o;s++)i.set(r,s,i.get(r,s)-i.get(n,s)*e.get(r,n))}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 Ut(e,r);for(let n=0;n<e;n++)for(let e=0;e<r;e++)n>e?s.set(n,e,t.get(n,e)):n===e?s.set(n,e,1):s.set(n,e,0);return s}get upperTriangularMatrix(){let t=this.LU,e=t.rows,r=t.columns,s=new Ut(e,r);for(let n=0;n<e;n++)for(let e=0;e<r;e++)n<=e?s.set(n,e,t.get(n,e)):s.set(n,e,0);return s}get pivotPermutationVector(){return Array.from(this.pivotVector)}}function Kt(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 Ht{constructor(t){let e,r,s,n,o=(t=Gt.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=Kt(t,o.get(e,s));if(0!==t){for(o.get(s,s)<0&&(t=-t),e=s;e<i;e++)o.set(e,s,o.get(e,s)/t);for(o.set(s,s,o.get(s,s)+1),r=s+1;r<h;r++){for(n=0,e=s;e<i;e++)n+=o.get(e,s)*o.get(e,r);for(n=-n/o.get(s,s),e=s;e<i;e++)o.set(e,r,o.get(e,r)+n*o.get(e,s))}}a[s]=-t}this.QR=o,this.Rdiag=a}solve(t){t=Ut.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,n,o,i,h=t.columns,a=t.clone(),l=e.columns;for(o=0;o<l;o++)for(n=0;n<h;n++){for(i=0,s=o;s<r;s++)i+=e.get(s,o)*a.get(s,n);for(i=-i/e.get(o,o),s=o;s<r;s++)a.set(s,n,a.get(s,n)+i*e.get(s,o))}for(o=l-1;o>=0;o--){for(n=0;n<h;n++)a.set(o,n,a.get(o,n)/this.Rdiag[o]);for(s=0;s<o;s++)for(n=0;n<h;n++)a.set(s,n,a.get(s,n)-a.get(o,n)*e.get(s,o))}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,n=new Ut(s,s);for(t=0;t<s;t++)for(e=0;e<s;e++)t<e?n.set(t,e,r.get(t,e)):t===e?n.set(t,e,this.Rdiag[t]):n.set(t,e,0);return n}get orthogonalMatrix(){let t,e,r,s,n=this.QR,o=n.rows,i=n.columns,h=new Ut(o,i);for(r=i-1;r>=0;r--){for(t=0;t<o;t++)h.set(t,r,0);for(h.set(r,r,1),e=r;e<i;e++)if(0!==n.get(r,r)){for(s=0,t=r;t<o;t++)s+=n.get(t,r)*h.get(t,e);for(s=-s/n.get(r,r),t=r;t<o;t++)h.set(t,e,h.get(t,e)+s*n.get(t,r))}}return h}}class Jt{constructor(t,e={}){if((t=Gt.checkMatrix(t)).isEmpty())throw new Error("Matrix must be non-empty");let r=t.rows,s=t.columns;const{computeLeftSingularVectors:n=!0,computeRightSingularVectors:o=!0,autoTranspose:i=!1}=e;let h,a=Boolean(n),l=Boolean(o),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 Ut(r,c),p=new Ut(s,s),d=new Float64Array(s),w=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]=Kt(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 n=t;n<r;n++)s+=h.get(n,t)*h.get(n,e);s=-s/h.get(t,t);for(let n=t;n<r;n++)h.set(n,e,h.get(n,e)+s*h.get(n,t))}d[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){d[t]=0;for(let e=t+1;e<s;e++)d[t]=Kt(d[t],d[e]);if(0!==d[t]){d[t+1]<0&&(d[t]=0-d[t]);for(let e=t+1;e<s;e++)d[e]/=d[t];d[t+1]+=1}if(d[t]=-d[t],t+1<r&&0!==d[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<s;r++)w[e]+=d[r]*h.get(e,r);for(let e=t+1;e<s;e++){let s=-d[e]/d[t+1];for(let n=t+1;n<r;n++)h.set(n,e,h.get(n,e)+s*w[n])}}if(l)for(let e=t+1;e<s;e++)p.set(e,t,d[e])}}let E=Math.min(s,r+1);if(b<s&&(m[b]=h.get(b,b)),r<E&&(m[E-1]=0),M+1<E&&(d[M]=h.get(M,E-1)),d[E-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 n=t;n<r;n++)s+=g.get(n,t)*g.get(n,e);s=-s/g.get(t,t);for(let n=t;n<r;n++)g.set(n,e,g.get(n,e)+s*g.get(n,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!==d[t])for(let e=t+1;e<s;e++){let r=0;for(let n=t+1;n<s;n++)r+=p.get(n,t)*p.get(n,e);r=-r/p.get(t+1,t);for(let n=t+1;n<s;n++)p.set(n,e,p.get(n,e)+r*p.get(n,t))}for(let e=0;e<s;e++)p.set(e,t,0);p.set(t,t,1)}let S=E-1,v=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(m[t]+Math.abs(m[t+1]));if(Math.abs(d[t])<=e||Number.isNaN(d[t])){d[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(d[r]):0)+(r!==t+1?Math.abs(d[r-1]):0);if(Math.abs(m[r])<=N*e){m[r]=0;break}}r===t?e=3:r===E-1?e=1:(e=2,t=r)}switch(t++,e){case 1:{let e=d[E-2];d[E-2]=0;for(let r=E-2;r>=t;r--){let n=Kt(m[r],e),o=m[r]/n,i=e/n;if(m[r]=n,r!==t&&(e=-i*d[r-1],d[r-1]=o*d[r-1]),l)for(let t=0;t<s;t++)n=o*p.get(t,r)+i*p.get(t,E-1),p.set(t,E-1,-i*p.get(t,r)+o*p.get(t,E-1)),p.set(t,r,n)}break}case 2:{let e=d[t-1];d[t-1]=0;for(let s=t;s<E;s++){let n=Kt(m[s],e),o=m[s]/n,i=e/n;if(m[s]=n,e=-i*d[s],d[s]=o*d[s],a)for(let e=0;e<r;e++)n=o*g.get(e,s)+i*g.get(e,t-1),g.set(e,t-1,-i*g.get(e,s)+o*g.get(e,t-1)),g.set(e,s,n)}break}case 3:{const e=Math.max(Math.abs(m[E-1]),Math.abs(m[E-2]),Math.abs(d[E-2]),Math.abs(m[t]),Math.abs(d[t])),n=m[E-1]/e,o=m[E-2]/e,i=d[E-2]/e,h=m[t]/e,u=d[t]/e,c=((o+n)*(o-n)+i*i)/2,f=n*i*(n*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=(h+n)*(h-n)+w,b=h*u;for(let e=t;e<E-1;e++){let n=Kt(y,b);0===n&&(n=Number.MIN_VALUE);let o=y/n,i=b/n;if(e!==t&&(d[e-1]=n),y=o*m[e]+i*d[e],d[e]=o*d[e]-i*m[e],b=i*m[e+1],m[e+1]=o*m[e+1],l)for(let t=0;t<s;t++)n=o*p.get(t,e)+i*p.get(t,e+1),p.set(t,e+1,-i*p.get(t,e)+o*p.get(t,e+1)),p.set(t,e,n);if(n=Kt(y,b),0===n&&(n=Number.MIN_VALUE),o=y/n,i=b/n,m[e]=n,y=o*d[e]+i*m[e+1],m[e+1]=-i*d[e]+o*m[e+1],b=i*d[e+1],d[e+1]=o*d[e+1],a&&e<r-1)for(let t=0;t<r;t++)n=o*g.get(t,e)+i*g.get(t,e+1),g.set(t,e+1,-i*g.get(t,e)+o*g.get(t,e+1)),g.set(t,e,n)}d[E-2]=y,v+=1;break}case 4:if(m[t]<=0&&(m[t]=m[t]<0?-m[t]:0,l))for(let e=0;e<=S;e++)p.set(e,t,-p.get(e,t));for(;t<S&&!(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=p.get(r,t+1),p.set(r,t+1,p.get(r,t)),p.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++}v=0,E--}}if(u){let t=p;p=g,g=t}this.m=r,this.n=s,this.s=m,this.U=g,this.V=p}solve(t){let e=t,r=this.threshold,s=this.s.length,n=Ut.zeros(s,s);for(let t=0;t<s;t++)Math.abs(this.s[t])<=r?n.set(t,t,0):n.set(t,t,1/this.s[t]);let o=this.U,i=this.rightSingularVectors,h=i.mmul(n),a=i.rows,l=o.rows,u=Ut.zeros(a,l);for(let t=0;t<a;t++)for(let e=0;e<l;e++){let r=0;for(let n=0;n<s;n++)r+=h.get(t,n)*o.get(e,n);u.set(t,e,r)}return u.mmul(e)}solveForDiagonal(t){return this.solve(Ut.diag(t))}inverse(){let t=this.V,e=this.threshold,r=t.rows,s=t.columns,n=new Ut(r,this.s.length);for(let o=0;o<r;o++)for(let r=0;r<s;r++)Math.abs(this.s[r])>e&&n.set(o,r,t.get(o,r)/this.s[r]);let o=this.U,i=o.rows,h=o.columns,a=new Ut(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+=n.get(t,s)*o.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,n=r.length;s<n;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 Ut.diag(this.s)}}function Xt(t,e,r,s,n){let o=r*s*s,i=Ut.eye(e.length,e.length,o);const h=n(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,n){const o=r.length,i=t.x.length;let h=new Array(o);for(let a=0;a<o;a++){h[a]=new Array(i);let o=r.slice();o[a]+=s;let l=n(o);for(let r=0;r<i;r++)h[a][r]=e[r]-l(t.x[r])}return new Ut(h)}(t,a,e,s,n),u=function(t,e){const r=t.x.length;let s=new Array(r);for(let n=0;n<r;n++)s[n]=[t.y[n]-e[n]];return new Ut(s)}(t,a),c=function(t,e=!1){return t=Gt.checkMatrix(t),e?new Jt(t).inverse():function(t,e,r=!1){return t=Gt.checkMatrix(t),e=Gt.checkMatrix(e),r?new Jt(t).solve(e):t.isSquare()?new Yt(t).solve(e):new Ht(t).solve(e)}(t,Ut.eye(t.rows))}(i.add(l.mmul(l.transpose())));return(e=(e=new Ut([e])).sub(c.mmul(l).mmul(u).mul(s).transpose())).to1DArray()}const Qt=1e-5,Zt=.001;class te{get neighbors(){return this.nNeighbors}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=ee,this.isInitialized=!1,this.rpForest=[],this.embedding=[],this.optimizationState=new re;const e=e=>{void 0!==t[e]&&(this[e]=t[e])};e("distanceFn"),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{initFromTree:t,initFromRandom:e}=(r=this.distanceFn,{initFromRandom:function(t,e,s,n,o){for(let i=0;i<s.length;i++){const h=U(t,e.length,o);for(let t=0;t<h.length;t++)h[t]<0||H(n,i,r(e[h[t]],s[i]),h[t],1)}},initFromTree:function(t,e,s,n,o){for(let i=0;i<s.length;i++){const h=bt(s[i],t,o);for(let t=0;t<h.length;t++){if(h[t]<0)return;H(n,i,r(e[h[t]],s[i]),h[t],1)}}}});var r;this.initFromTree=t,this.initFromRandom=e,this.search=function(t){return function(e,r,s,n){const{indices:o,indptr:i}=ct(r);for(let r=0;r<n.length;r++){const h=new Set(s[0][r]);for(;;){const a=tt(s,r);if(-1===a)break;const l=o.slice(i[a],i[a+1]);for(const o of l)o===a||-1===o||h.has(o)||(J(s,r,t(e[o],n[r]),o,1),h.add(o))}}return s}}(this.distanceFn)}makeSearchGraph(t){const e=this.knnIndices,r=this.knnDistances,s=[t.length,t.length],n=new et([],[],[],s);for(let t=0;t<e.length;t++){const s=e[t],o=r[t];for(let e=0;e<s.length;e++){const r=s[e],i=o[e];i>0&&n.set(t,r,i)}}return ut(n,rt(n),((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,n,o,i){const h=Y(r.length,s);if(n(s,e,r,h,i),t)for(let s of t)o(s,e,r,h,i);return h}(this.rpForest,e,t,r,this.initFromRandom,this.initFromTree,this.random),n=this.search(e,this.searchGraph,s,t);let{indices:o,weights:i}=Q(n);o=o.map((t=>t.slice(0,this.nNeighbors))),i=i.map((t=>t.slice(0,this.nNeighbors)));const h=Math.max(0,this.localConnectivity-1),{sigmas:a,rhos:l}=this.smoothKNNDistance(i,this.nNeighbors,h),{rows:u,cols:c,vals:f}=this.computeMembershipStrengths(o,i,a,l),m=[t.length,e.length];let g=new et(u,c,f,m);const p=ct(at(g,"l1")),d=t.length,w=function(t,e,r){const s=$(t.length).map((t=>$(r[0].length)));for(let n=0;n<t.length;n++)for(let o=0;o<t[0].length;o++)for(let i=0;i<r[0].length;i++){const h=t[n][o];s[n][i]+=e[n][o]*r[h][i]}return s}(G(p.indices,d,this.nNeighbors),G(p.values,d,this.nNeighbors),this.embedding),y=this.nEpochs?this.nEpochs/3:g.nRows<=1e4?100:30,b=g.getValues().reduce(((t,e)=>e>t?e:t),0);g=g.map((t=>t<b/y?0:t)),g=ht(g);const M=this.makeEpochsPerSample(g.getValues(),y),x=g.getRows(),E=g.getCols();return this.assignOptimizationStateParameters({headEmbedding:w,tailEmbedding:this.embedding,head:x,tail:E,currentEpoch:0,nEpochs:y,nVertices:g.getDims()[1],epochsPerSample:M}),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:e,nNeighbors:r}=this,s=function(t,e){return function(r,s,n,o=10,i=50,h=.001,a=.5,l=!0){const u=r.length,c=Y(r.length,n);for(let s=0;s<r.length;s++){const o=K(n,r.length,e);for(let e=0;e<o.length;e++){const n=t(r[s],r[o[e]]);H(c,s,n,o[e],1),H(c,o[e],n,s,1)}}if(l)for(let e=0;e<s.length;e++)for(let n=0;n<s[e].length&&!(s[e][n]<0);n++)for(let o=n+1;o<s[e].length&&!(s[e][o]<0);o++){const i=t(r[s[e][n]],r[s[e][o]]);H(c,s[e][n],i,s[e][o],1),H(c,s[e][o],i,s[e][n],1)}for(let s=0;s<o;s++){const s=X(c,u,n,i,e);let o=0;for(let n=0;n<u;n++)for(let h=0;h<i;h++){let l=Math.floor(s[0][n][h]);if(!(l<0||O(e)<a))for(let e=0;e<i;e++){const i=Math.floor(s[0][n][e]),a=s[2][n][h],u=s[2][n][e];if(i<0||!a&&!u)continue;const f=t(r[l],r[i]);o+=H(c,l,f,i,1),o+=H(c,i,f,l,1)}}if(o<=h*n*r.length)break}return Q(c)}}(e,this.random),n=5+Math.floor(.5==(o=t.length**.5/20)?0:Math.round(o));var o;const i=Math.max(5,Math.floor(Math.round((t=>Math.log(t)/Math.log(2))(t.length))));this.rpForest=mt(t,r,n,this.random);const h=function(t){if(t.length>0){const e=[];for(let r of t)e.push(...r.indices);return e}return[[-1]]}(this.rpForest),{indices:a,weights:l}=s(t,h,r,i);return{knnIndices:a,knnDistances:l}}fuzzySimplicialSet(t,e,r=1){const{knnIndices:s=[],knnDistances:n=[],localConnectivity:o}=this,{sigmas:i,rhos:h}=this.smoothKNNDistance(n,e,o),{rows:a,cols:l,vals:u}=this.computeMembershipStrengths(s,n,i,h),c=[t.length,t.length],f=new et(a,l,u,c),m=rt(f),g=st(f,m),p=ot(nt(f,m),g);return nt(it(p,r),it(g,1-r))}categoricalSimplicialSetIntersection(t,e,r,s=1){let n=function(t,e,r=1,s=5){return t.map(((t,n,o)=>-1===e[n]||-1===e[o]?t*Math.exp(-r):e[n]!==e[o]?t*Math.exp(-s):t))}(t,e,s,r);return n=ht(n),function(t){const e=rt(t=at(t,"max"));return ht(t=nt(t,ot(e,st(e,t))))}(n)}smoothKNNDistance(t,e,r=1,s=64,n=1){const o=Math.log(e)/Math.log(2)*n,i=$(t.length),h=$(t.length);for(let e=0;e<t.length;e++){let n=0,a=1/0,l=1;const u=t[e],c=u.filter((t=>t>0));if(c.length>=r){let t=Math.floor(r),s=r-t;t>0?(i[e]=c[t-1],s>Qt&&(i[e]+=s*(c[t]-c[t-1]))):i[e]=s*c[0]}else c.length>0&&(i[e]=B(c));for(let r=0;r<s;r++){let r=0;for(let s=1;s<t[e].length;s++){const n=t[e][s]-i[e];r+=n>0?Math.exp(-n/l):1}if(Math.abs(r-o)<Qt)break;r>o?(a=l,l=(n+a)/2):(n=l,a===1/0?l*=2:l=(n+a)/2)}if(h[e]=l,i[e]>0){const t=W(u);h[e]<Zt*t&&(h[e]=Zt*t)}else{const r=W(t.map(W));h[e]<Zt*r&&(h[e]=Zt*r)}}return{sigmas:h,rhos:i}}computeMembershipStrengths(t,e,r,s){const n=t.length,o=t[0].length,i=$(n*o),h=$(n*o),a=$(n*o);for(let l=0;l<n;l++)for(let n=0;n<o;n++){let u=0;-1!==t[l][n]&&(u=t[l][n]===l?0:e[l][n]-s[l]<=0?1:Math.exp(-(e[l][n]-s[l])/r[l]),i[l*o+n]=l,h[l*o+n]=t[l][n],a[l*o+n]=u)}return{rows:i,cols:h,vals:a}}initializeSimplicialSetEmbedding(){const t=this.getNEpochs(),{nComponents:e}=this,r=this.graph.getValues();let s=0;for(let t=0;t<r.length;t++){const e=r[t];s<r[t]&&(s=e)}const n=this.graph.map((e=>e<s/t?0:e));this.embedding=$(n.nRows).map((()=>$(e).map((()=>20*O(this.random)-10))));const o=[],i=[],h=[],a=n.getAll();for(let t=0;t<a.length;t++){const e=a[t];e.value&&(o.push(e.value),h.push(e.row),i.push(e.col))}return{head:i,tail:h,epochsPerSample:this.makeEpochsPerSample(o,t)}}makeEpochsPerSample(t,e){const r=L(t.length,-1),s=B(t),n=t.map((t=>t/s*e));return n.forEach(((t,s)=>{t>0&&(r[s]=e/n[s])})),r}assignOptimizationStateParameters(t){Object.assign(this.optimizationState,t)}prepareForOptimizationLoop(){const{repulsionStrength:t,learningRate:e,negativeSampleRate:r}=this,{epochsPerSample:s,headEmbedding:n,tailEmbedding:o}=this.optimizationState,i=n[0].length,h=n.length===o.length,a=s.map((t=>t/r)),l=[...a],u=[...s];this.assignOptimizationStateParameters({epochOfNextSample:u,epochOfNextNegativeSample:l,epochsPerNegativeSample:a,moveOther:h,initialAlpha:e,alpha:e,gamma:t,dim:i})}initializeOptimization(){const t=this.embedding,e=this.embedding,{head:r,tail:s,epochsPerSample:n}=this.optimizationState,o=this.getNEpochs(),i=this.graph.nCols,{a:h,b:a}=function(t,e){const r=function(t,e,r){return j(300).map(((t,r)=>0+r*((e-0)/299)))}(0,3*t).map((t=>t<e?1:t)),s=$(r.length).map(((s,n)=>r[n]>=e?Math.exp(-(r[n]-e)/t):s)),n={x:r,y:s},{parameterValues:o}=function(t,e,r={}){let{maxIterations:s=100,gradientDifference:n=.1,damping:o=0,errorTolerance:i=.01,minValues:h,maxValues:a,initialValues:l}=r;if(o<=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(!xt(t.x)||t.x.length<2||!xt(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(!xt(u))throw new Error("initialValues must be an array");let f,m=Et(t,u,e),g=m<=i;for(f=0;f<s&&!g;f++){u=Xt(t,u,o,n,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<=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}),[i,h]=o;return{a:i,b:h}}(this.spread,this.minDist);this.assignOptimizationStateParameters({headEmbedding:t,tailEmbedding:e,head:r,tail:s,epochsPerSample:n,a:h,b:a,nEpochs:o,nVertices:i})}optimizeLayoutStep(t){const{optimizationState:e}=this,{head:r,tail:s,headEmbedding:n,tailEmbedding:o,epochsPerSample:i,epochOfNextSample:h,epochOfNextNegativeSample:a,epochsPerNegativeSample:l,moveOther:u,initialAlpha:c,alpha:f,gamma:m,a:g,b:p,dim:d,nEpochs:w,nVertices:y}=e;for(let e=0;e<i.length;e++){if(h[e]>t)continue;const c=r[e],w=s[e],b=n[c],M=o[w],x=ne(b,M);let E=0;x>0&&(E=-2*g*p*Math.pow(x,p-1),E/=g*Math.pow(x,p)+1);for(let t=0;t<d;t++){const e=se(E*(b[t]-M[t]),4);b[t]+=e*f,u&&(M[t]+=-e*f)}h[e]+=i[e];const S=Math.floor((t-a[e])/l[e]);for(let t=0;t<S;t++){const t=V(y,this.random),e=o[t],r=ne(b,e);let s=0;if(r>0)s=2*m*p,s/=(.001+r)*(g*Math.pow(r,p)+1);else if(c===t)continue;for(let t=0;t<d;t++){let r=4;s>0&&(r=se(s*(b[t]-e[t]),4)),b[t]+=r*f}}a[e]+=S*l[e]}return e.alpha=c*(1-t/w),e.currentEpoch+=1,n}optimizeLayoutAsync(t=(()=>!0)){return new Promise(((e,r)=>{const s=async()=>{try{const{nEpochs:r,currentEpoch:n}=this.optimizationState;this.embedding=this.optimizeLayoutStep(n);const o=this.optimizationState.currentEpoch,i=!1===t(o),h=o===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:n}=this.optimizationState;r=this.optimizeLayoutStep(n);const o=this.optimizationState.currentEpoch,i=!1===t(o);e=o===s||i}return r}getNEpochs(){const t=this.graph;if(this.nEpochs>0)return this.nEpochs;if(!t)return 200;const e=t.nRows;return e<=2500?500:e<=5e3?400:e<=7500?300:200}}function ee(t,e){return Math.abs(t-e)}class re{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 se(t,e){return t>e?e:t<-e?-e:t}function ne(t,e){let r=0;for(let s=0;s<t.length;s++)r+=Math.pow(t[s]-e[s],2);return r}var oe,ie=r(6814);class he{get data(){return this._data}get size(){return this._size}constructor(t,e){if(null==e){if(null==t)throw new Error("Arguments error: data or size is required.");if(this._data=t,this._size=(1+Math.sqrt(1+8*this._data.length))/2,this._size!=Math.floor(this._size))throw new Error(`Invalid data length ${this._data.length} leads to non integer size ${this._size}`)}else{this._size=e;const r=e*(e-1)/2;if(t){if(t.length!=r)throw new Error(`Invalid data length. Observations size ${e} requires data length ${r}.`);this._data=t}else this._data=new Float32Array(r)}}_linearizeIJ(t,e){if(!(t<e))throw new Error("i must be less than j");return this._size*t+e-Math.floor((t+2)*(t+1)/2)}get(t,e){return t==e?0:t<e?this._data[this._linearizeIJ(t,e)]:this._data[this._linearizeIJ(e,t)]}set(t,e,r){this._data[this._linearizeIJ(t,e)]=r}static calc(t,e){const r=t.length,s=new he(void 0,r);for(let n=0;n<r;n++)for(let o=n+1;o<r;o++)s.set(n,o,(0,ie.k)(t[n])||(0,ie.k)(t[o])?1:e(t[n],t[o]));return s}square(){for(let t=0;t<this._data.length;t++)this._data[t]=this._data[t]**2}add(t){if(this._size!==t._size)throw new Error(`Matrices must have the same size. This size: ${this._size}, other size: ${t._size}`);for(let e=0;e<this._data.length;e++)this._data[e]+=t._data[e]}sqrt(){for(let t=0;t<this._data.length;t++)this._data[t]=Math.sqrt(this._data[t])}normalize(){let t=0,e=this._data[0];for(let r=0;r<this._data.length;r++)this._data[r]<t&&(t=this._data[r]),this._data[r]>e&&(e=this._data[r]);const r=e-t;for(let s=0;s<this._data.length;s++)this._data[s]=0===r?this._data[s]-t:(this._data[s]-t)/(e-t)}}class ae{constructor(){this._workerCount=Math.max(navigator.hardwareConcurrency-2,1)}async calc(t,e,s,n={}){const o=t.length*(t.length-1)/2,i=Math.floor(o/this._workerCount),h=await this.getMinimalThreshold(t,e,n);s<h&&(console.log(`using threshold ${h}`),s=h),n.threshold=s;const a=new Array(this._workerCount),l=new Array(this._workerCount).fill(null).map((()=>new Worker(new URL(r.p+r.u(381),r.b))));for(let r=0;r<this._workerCount;r++)a[r]=new Promise(((h,a)=>{const u=r*i,c=r===this._workerCount-1?o:(r+1)*i;c<=u&&h({i:new Int32Array(0),j:new Int32Array(0),distance:new Float32Array(0),idx:r}),l[r].postMessage({values:t,startIdx:u,endIdx:c,threshold:s,fnName:e,opts:n}),l[r].onmessage=({data:{error:t,i:e,j:s,distance:n}})=>{t?(l[r].terminate(),a(t)):(l[r].terminate(),h({i:e,j:s,distance:n,idx:r}))}}));const u=await Promise.all(a),c=u.reduce(((t,e)=>t+e.i.length),0),f=new Int32Array(c),m=new Int32Array(c),g=new Float32Array(c);let p=0;for(const t of u)f.set(t.i,p),m.set(t.j,p),g.set(t.distance,p),p+=t.i.length;return{i:f,j:m,distance:g}}async getMinimalThreshold(t,e,s={}){const n=new Array(this._workerCount).fill(null).map((()=>new Worker(new URL(r.p+r.u(770),r.b)))),o=t.slice();for(let t=o.length-1;t>0;t--){const e=Math.floor(Math.random()*(t+1));[o[t],o[e]]=[o[e],o[t]]}try{const r=t.length*(t.length-1)/2,i=Math.floor(r/this._workerCount),h=Math.floor(Math.min(r/1e3,1e6)/this._workerCount),a=new Array(this._workerCount);for(let t=0;t<this._workerCount;t++)a[t]=new Promise(((a,l)=>{const u=t*i,c=t===this._workerCount-1?r:(t+1)*i;n[t].postMessage({values:o,startIdx:u,endIdx:c,sampleLength:h,fnName:e,opts:s}),n[t].onmessage=({data:{error:e,distance:r}})=>{n[t].terminate(),e?l(e):a({distance:r})}}));const l=await Promise.all(a),u=l.reduce(((t,e)=>t+e.distance.length),0),c=new Float32Array(u);let f=0;for(const t of l)c.set(t.distance,f),f+=t.distance.length;c.sort();let m=1-c[Math.floor(7e7/r*c.length)];return m=Math.min(Math.max(m,.3),.9),m}catch(t){return n?.forEach((t=>t?.terminate())),console.error(t),.5}}static calcSync(t,e,r,s){const n=[],o=[],i=[];let h=0,a=0,l=0;const u=t.length*(t.length-1)/2;for(;h<u;){const u=(0,ie.k)(t[a])||(0,ie.k)(t[l])?1:r(t[a],t[l]);(Object.values(y).some((t=>t===e))?1/(1+u):1-u)>=s&&(n.push(a),o.push(l),i.push(u)),h++,l++,l===t.length&&(a++,l=a+1)}return{i:new Int32Array(n),j:new Int32Array(o),distance:new Float32Array(i)}}}!function(t){t.UMAP="UMAP",t.T_SNE="t-SNE"}(oe||(oe={}));class le{constructor(t){this.data=t.data}}const ue={UMAP:class extends le{constructor(t){super(t),h("distanceFname"in t),h("distanceFn"in t),this.distanceFnArgs=t?.distanceFnArgs,this.distanceFn=t.distanceFn,this.usingSparseMatrix=!!t.usingSparseMatrix||!!t.sparseMatrix,this.sparseMatrixThreshold=t.sparseMatrixThreshold??.8,this.transferedSparseMatrix=t.sparseMatrix,this.progressFunc=t.progressFunc,this.distanceFname=t.distanceFname,this.dmIndexFunc=c(this.data.length),this.vectors=new Array(this.data.length).fill(0).map(((t,e)=>e)),this.usingDistanceMatrix=!(!t.preCalculateDistanceMatrix&&this.data.length>2e4||this.usingSparseMatrix),this.usingDistanceMatrix?t.distanceFn=this._encodedDistanceMatrix.bind(this):this.usingSparseMatrix?t.distanceFn=this._encodedSparseMatrix.bind(this):t.distanceFn=this._encodedDistance.bind(this),this.data.length<15&&(t.nNeighbors=this.data.length-1),this.reducer=new te(t)}_encodedDistanceMatrix(t,e){return t===e?0:t>e?this.distanceMatrix[this.dmIndexFunc(e,t)]:this.distanceMatrix[this.dmIndexFunc(t,e)]}_encodedSparseMatrix(t,e){return this.sparseMatrix.get(t)?.get(e)??this.sparseMatrix.get(e)?.get(t)??1}_encodedDistance(t,e){return this.distanceFn(this.data[t],this.data[e])}async transform(t){if(this.usingDistanceMatrix){if(this.distanceMatrix=t?await(async()=>{const t=new f(!0,!1);try{const e=await t.calc(this.data,this.distanceFname,!1,this.distanceFnArgs);return t.terminate(),e}catch(e){throw t.terminate(),e}})():(()=>he.calc(this.data,((t,e)=>this.distanceFn(t,e))).data)(),this.distanceMatrix&&!D(this.distanceFname)){const t=function(t,e,r){function s(t,e,r,s){if(r>t[t.length-1])return;let n=t.length-2;for(n=t.length-2;n>=0&&!(r>t[n]);n--);t.splice(t.length-1,1),t.splice(n+1,0,r),e.splice(e.length-1,1),e.splice(n+1,0,s)}console.time("knnGraph");const n=new Array(r).fill(null).map((()=>new Array(e).fill(1))),o=new Array(r).fill(null).map((()=>new Array(e).fill(1)));let i=0,h=1;for(let e=0;e<t.length;e++)s(o[i],n[i],t[e],h),s(o[h],n[h],t[e],i),h++,h>=r&&(i++,h=i+1);return console.timeEnd("knnGraph"),{knnIndexes:n,knnDistances:o}}(this.distanceMatrix,this.reducer.neighbors,this.data.length);this.reducer.setPrecomputedKNN(t.knnIndexes,t.knnDistances)}}else if(this.usingSparseMatrix){console.time("sparse matrix");let t=this.transferedSparseMatrix??await(new ae).calc(this.data,this.distanceFname,this.sparseMatrixThreshold,this.distanceFnArgs);if(console.timeEnd("sparse matrix"),!D(this.distanceFname)){const e=function(t,e,r,s,n){function o(t,e,r,s){if(r>t[t.length-1])return;let n=t.length-2;for(n=t.length-2;n>=0&&!(r>t[n]);n--);t.splice(t.length-1,1),t.splice(n+1,0,r),e.splice(e.length-1,1),e.splice(n+1,0,s)}console.time("knnGraph");const i=new Array(n).fill(null).map((()=>new Array(s).fill(1))),h=new Array(n).fill(null).map((()=>new Array(s).fill(1)));for(let s=0;s<t.length;s++)o(h[t[s]],i[t[s]],r[s],e[s]),o(h[e[s]],i[e[s]],r[s],t[s]);return console.timeEnd("knnGraph"),{knnIndexes:i,knnDistances:h}}(t.i,t.j,t.distance,this.reducer.neighbors,this.data.length);this.reducer.setPrecomputedKNN(e.knnIndexes,e.knnDistances)}console.time("sparse matrix to map"),this.sparseMatrix=new Map;for(let e=0;e<t.i.length;++e){const r=t.i[e],s=t.j[e],n=t.distance[e];this.sparseMatrix.has(r)||this.sparseMatrix.set(r,new Map),this.sparseMatrix.get(r).set(s,n)}console.timeEnd("sparse matrix to map"),t.distance=null,t.i=null,t.j=null,t=null,await new Promise((t=>{setTimeout((()=>{t()}),500)}))}const e=await this.reducer.fitAsync(this.vectors,(t=>{this.progressFunc&&this.progressFunc(t,this.reducer.getNEpochs(),this.reducer.getEmbedding())}));return{embedding:(r=e,new Array(r.length).fill(0).map(((t,e)=>n.OW.from(r[e])))),...this.distanceMatrix?{distance:this.distanceMatrix}:{}};var r}},"t-SNE":class extends le{constructor(t){super(t),this.reducer=new s.Z(t),this.iterations=t?.iterations??100,this.distanceFname=t.distanceFname,this.distanceFn=t.distanceFn}async transform(t){const e=t?await(async()=>{const t=new f(!0,!1);try{const e=await t.calc(this.data,this.distanceFname);return t.terminate(),e}catch(e){throw t.terminate(),e}})():(()=>{const t=he.calc(this.data,((t,e)=>this.distanceFn(t,e)));return t.normalize(),t.data})(),r=function(t,e){const r=c(e);function s(t,e){const s=Number(t),n=Number(e);return r(s,n)}const n={get:(t,r,n)=>"length"===r?e:new Proxy(t,function(t){return{get:(e,r,n)=>t===r?0:e[Number(t)>Number(r)?s(r,t):s(t,r)]}}(r))};return new Proxy(t,n)}(e,this.data.length);this.reducer.initDataDist(r);for(let t=0;t<this.iterations;++t)this.reducer.step();return{distance:e,embedding:this.reducer.getSolution()}}},SPE:class extends le{constructor(t){super(t),this.reducer=new m(t)}async transform(){const t=await this.reducer.embed(this.data);return{distance:this.reducer.distance,embedding:t}}},pSPE:class extends le{constructor(t){super(t),this.reducer=new g(t)}async transform(){const t=await this.reducer.embed(this.data);return{distance:this.reducer.distance,embedding:t}}},OriginalSPE:class extends le{constructor(t){super(t),this.reducer=new p(t)}async transform(){const t=await this.reducer.embed(this.data);return{distance:this.reducer.distance,embedding:t}}}};class ce{constructor(t,e,r,s){const n=new P(r).getMeasure(s?.distanceFnArgs);let o={};if(D(r))for(let e=0;e<t.length;++e)t[e]=new v.Z(t[e]._data,t[e]._length);o="UMAP"==e?{data:t,distanceFn:n,distanceFname:r,nEpochs:s?.cycles,...s}:"t-SNE"==e?{data:t,distanceFn:n,distanceFname:r,iterations:s?.cycles??void 0,...s}:{data:t,distance:n,distanceFunctionName:r,...s},this.reducer=new ue[e](o)}async transform(t=!1,e){if(void 0===this.reducer)throw new Error("Reducer was not defined.");let{embedding:r,distance:s}=await this.reducer.transform(e);var o;return t&&(o=r,r=new Array(o[0].length).fill(0).map(((t,e)=>new n.OW(o.length).fill(0).map(((t,r)=>o[r][e]))))),{distance:s,embedding:r}}static availableMetricsByType(t){return Object.keys(T[t])}static get availableMethods(){return Object.keys(ue)}static get availableMetrics(){let t=[];return Object.values(T).forEach((e=>{const r=Object.values(e);t=[...t,...r]})),t}}async function fe(t,e,r){t%5==0&&self.postMessage({epochNum:t,epochsLength:e,embedding:r})}self.onmessage=async({data:{columnData:t,method:e,measure:r,options:s,parallelDistanceWorkers:n}})=>{let o;try{o=await async function(t,e,r,s,n){const o=new ce(t,e,r,{...s,progressFunc:fe});return await o.transform(!0,n)}(t,e,r,s,n)}catch(t){o={error:t}}self.postMessage({error:o.error,distance:o.distance,embedding:o.embedding})}},7659:(t,e,r)=>{e.Z=void 0;var s=r(6215);Object.defineProperty(e,"Z",{enumerable:!0,get:function(){return s.TSNE}})},6215:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.TSNE=void 0,e.TSNE=class{constructor(t){this.returnV=!1,this.vValue=0,this.iter=0,t=t||{},this.perplexity=this.getopt(t,"perplexity",30),this.dim=this.getopt(t,"dim",2),this.epsilon=this.getopt(t,"epsilon",10)}assert(t,e){if(!t)throw e||"Assertion failed"}getopt(t,e,r){return t.hasOwnProperty(e)?t[e]:r}gaussRandom(){if(this.returnV)return this.returnV=!1,this.vValue;const t=2*Math.random()-1,e=2*Math.random()-1,r=t*t+e*e;if(0===r||r>1)return this.gaussRandom();const s=Math.sqrt(-2*Math.log(r)/r);return this.vValue=e*s,this.returnV=!0,t*s}randn(t,e){return t+this.gaussRandom()*e}zeros(t){if(void 0===t||isNaN(t))return[];if("undefined"==typeof ArrayBuffer){const e=new Array(t);for(let r=0;r<t;r++)e[r]=0;return e}return new Float64Array(t)}randn2d(t,e,r){const s=void 0!==r,n=[];for(let o=0;o<t;o++){const t=[];for(let n=0;n<e;n++)s?t.push(r):t.push(this.randn(0,1e-4));n.push(t)}return n}L2(t,e){const r=t.length;let s=0;for(let n=0;n<r;n++){const r=t[n],o=e[n];s+=(r-o)*(r-o)}return s}xtod(t){const e=t.length,r=this.zeros(e*e);for(let s=0;s<e;s++)for(let n=s+1;n<e;n++){const o=this.L2(t[s],t[n]);r[s*e+n]=o,r[n*e+s]=o}return r}d2p(t,e,r){const s=Math.sqrt(t.length),n=Math.floor(s);this.assert(n===s,"D should have square number of elements.");const o=Math.log(e),i=this.zeros(n*n),h=this.zeros(n);for(let e=0;e<n;e++){let s=-1/0,a=1/0,l=1,u=!1;const c=50;let f=0;for(;!u;){let i=0;for(let r=0;r<n;r++){let s=Math.exp(-t[e*n+r]*l);e===r&&(s=0),h[r]=s,i+=s}let m=0;for(let t=0;t<n;t++){let e;e=0===i?0:h[t]/i,h[t]=e,e>1e-7&&(m-=e*Math.log(e))}m>o?(s=l,a===1/0?l*=2:l=(l+a)/2):(a=l,s===-1/0?l/=2:l=(l+s)/2),f++,Math.abs(m-o)<r&&(u=!0),f>=c&&(u=!0)}for(let t=0;t<n;t++)i[e*n+t]=h[t]}const a=this.zeros(n*n),l=2*n;for(let t=0;t<n;t++)for(let e=0;e<n;e++)a[t*n+e]=Math.max((i[t*n+e]+i[e*n+t])/l,1e-100);return a}sign(t){return t>0?1:t<0?-1:0}initDataRaw(t){const e=t.length,r=t[0].length;this.assert(e>0," X is empty? You must have some data!"),this.assert(r>0," X[0] is empty? Where is the data?");const s=this.xtod(t);this.P=this.d2p(s,this.perplexity,1e-4),this.N=e,this.initSolution()}initDataDist(t){const e=t.length;this.assert(e>0," X is empty? You must have some data!");const r=this.zeros(e*e);for(let s=0;s<e;s++)for(let n=s+1;n<e;n++){const o=t[s][n];r[s*e+n]=o,r[n*e+s]=o}this.P=this.d2p(r,this.perplexity,1e-4),this.N=e,this.initSolution()}initSolution(){this.Y=this.randn2d(this.N,this.dim),this.gains=this.randn2d(this.N,this.dim,1),this.ystep=this.randn2d(this.N,this.dim,0),this.iter=0}getSolution(){return this.Y}step(){this.iter+=1;const t=this.N,e=this.costGrad(this.Y),r=e.cost,s=e.grad,n=this.zeros(this.dim);for(let e=0;e<t;e++)for(let t=0;t<this.dim;t++){const r=s[e][t],o=this.ystep[e][t],i=this.gains[e][t];let h=this.sign(r)===this.sign(o)?.8*i:i+.2;h<.01&&(h=.01),this.gains[e][t]=h;const a=(this.iter<250?.5:.8)*o-this.epsilon*h*s[e][t];this.ystep[e][t]=a,this.Y[e][t]+=a,n[t]+=this.Y[e][t]}for(let e=0;e<t;e++)for(let r=0;r<this.dim;r++)this.Y[e][r]-=n[r]/t;return r}debugGrad(){const t=this.N,e=this.costGrad(this.Y),r=(e.cost,e.grad),s=1e-5;for(let e=0;e<t;e++)for(let t=0;t<this.dim;t++){const n=this.Y[e][t];this.Y[e][t]=n+s;const o=this.costGrad(this.Y);this.Y[e][t]=n-s;const i=this.costGrad(this.Y),h=r[e][t],a=(o.cost-i.cost)/(2*s);console.log(e+","+t+": gradcheck analytic: "+h+" vs. numerical: "+a),this.Y[e][t]=n}}costGrad(t){const e=this.N,r=this.dim,s=this.P,n=this.iter<100?4:1,o=this.zeros(e*e);let i=0;for(let s=0;s<e;s++)for(let n=s+1;n<e;n++){let h=0;for(let e=0;e<r;e++){const r=t[s][e]-t[n][e];h+=r*r}const a=1/(1+h);o[s*e+n]=a,o[n*e+s]=a,i+=2*a}const h=e*e,a=this.zeros(h);for(let t=0;t<h;t++)a[t]=Math.max(o[t]/i,1e-100);let l=0;const u=[];for(let i=0;i<e;i++){const h=new Array(r);for(let t=0;t<r;t++)h[t]=0;for(let u=0;u<e;u++){l+=-s[i*e+u]*Math.log(a[i*e+u]);const c=4*(n*s[i*e+u]-a[i*e+u])*o[i*e+u];for(let e=0;e<r;e++)h[e]+=c*(t[i][e]-t[u][e])}u.push(h)}return{cost:l,grad:u}}}}},s={};function n(t){var e=s[t];if(void 0!==e)return e.exports;var o=s[t]={exports:{}};return r[t](o,o.exports,n),o.exports}n.m=r,n.x=()=>{var t=n.O(void 0,[1,172],(()=>n(3196)));return n.O(t)},t=[],n.O=(e,r,s,o)=>{if(!r){var i=1/0;for(u=0;u<t.length;u++){for(var[r,s,o]=t[u],h=!0,a=0;a<r.length;a++)(!1&o||i>=o)&&Object.keys(n.O).every((t=>n.O[t](r[a])))?r.splice(a--,1):(h=!1,o<i&&(i=o));if(h){t.splice(u--,1);var l=s();void 0!==l&&(e=l)}}return e}o=o||0;for(var u=t.length;u>0&&t[u-1][2]>o;u--)t[u]=t[u-1];t[u]=[r,s,o]},n.d=(t,e)=>{for(var r in e)n.o(e,r)&&!n.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:e[r]})},n.f={},n.e=t=>Promise.all(Object.keys(n.f).reduce(((e,r)=>(n.f[r](t,e),e)),[])),n.u=t=>t+".js",n.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||new Function("return this")()}catch(t){if("object"==typeof window)return window}}(),n.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),(()=>{var t;n.g.importScripts&&(t=n.g.location+"");var e=n.g.document;if(!t&&e&&(e.currentScript&&(t=e.currentScript.src),!t)){var r=e.getElementsByTagName("script");if(r.length)for(var s=r.length-1;s>-1&&!t;)t=r[s--].src}if(!t)throw new Error("Automatic publicPath is not supported in this browser");t=t.replace(/#.*$/,"").replace(/\?.*$/,"").replace(/\/[^\/]+$/,"/"),n.p=t})(),(()=>{n.b=self.location+"";var t={196:1};n.f.i=(e,r)=>{t[e]||importScripts(n.p+n.u(e))};var e=self.webpackChunkbio=self.webpackChunkbio||[],r=e.push.bind(e);e.push=e=>{var[s,o,i]=e;for(var h in o)n.o(o,h)&&(n.m[h]=o[h]);for(i&&i(n);s.length;)t[s.pop()]=1;r(e)}})(),e=n.x,n.x=()=>Promise.all([n.e(1),n.e(172)]).then(e);var o=n.x();bio=o})();
|
|
3
3
|
//# sourceMappingURL=196.js.map
|