@datagrok/bio 2.22.7 → 2.22.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/284.js CHANGED
@@ -1,3 +1,3 @@
1
1
  /*! For license information please see 284.js.LICENSE.txt */
2
- (self.webpackChunkbio=self.webpackChunkbio||[]).push([[284],{5540:(t,e,n)=>{"use strict";t=n.hmd(t);var r,s=(r="undefined"!=typeof document&&document.currentScript?document.currentScript.src:void 0,function(t={}){var e,n,s=void 0!==t?t:{};s.ready=new Promise((function(t,r){e=t,n=r}));var i,o=Object.assign({},s),a="object"==typeof window,u="function"==typeof importScripts,l=("object"==typeof process&&"object"==typeof process.versions&&process.versions.node,"");(a||u)&&(u?l=self.location.href:"undefined"!=typeof document&&document.currentScript&&(l=document.currentScript.src),r&&(l=r),l=0!==l.indexOf("blob:")?l.substr(0,l.replace(/[?#].*/,"").lastIndexOf("/")+1):"",u&&(i=t=>{var e=new XMLHttpRequest;return e.open("GET",t,!1),e.responseType="arraybuffer",e.send(null),new Uint8Array(e.response)})),s.print||console.log.bind(console);var h,c,f=s.printErr||console.warn.bind(console);Object.assign(s,o),o=null,s.arguments&&s.arguments,s.thisProgram&&s.thisProgram,s.quit&&s.quit,s.wasmBinary&&(h=s.wasmBinary),s.noExitRuntime,"object"!=typeof WebAssembly&&I("no native wasm support detected");var m,g,d,p=!1;function w(){var t=c.buffer;s.HEAP8=m=new Int8Array(t),s.HEAP16=new Int16Array(t),s.HEAP32=new Int32Array(t),s.HEAPU8=g=new Uint8Array(t),s.HEAPU16=new Uint16Array(t),s.HEAPU32=d=new Uint32Array(t),s.HEAPF32=new Float32Array(t),s.HEAPF64=new Float64Array(t)}var y,b,x=[],M=[],v=[],S=0,A=null,E=null;function I(t){s.onAbort&&s.onAbort(t),f(t="Aborted("+t+")"),p=!0,t+=". Build with -sASSERTIONS for more info.";var e=new WebAssembly.RuntimeError(t);throw n(e),e}function N(t){return t.startsWith("data:application/octet-stream;base64,")}function R(t){try{if(t==y&&h)return new Uint8Array(h);if(i)return i(t);throw"both async and sync fetching of the wasm failed"}catch(t){I(t)}}function k(t,e,n){return function(t){return h||!a&&!u||"function"!=typeof fetch?Promise.resolve().then((function(){return R(t)})):fetch(t,{credentials:"same-origin"}).then((function(e){if(!e.ok)throw"failed to load wasm binary file at '"+t+"'";return e.arrayBuffer()})).catch((function(){return R(t)}))}(t).then((function(t){return WebAssembly.instantiate(t,e)})).then((function(t){return t})).then(n,(function(t){f("failed to asynchronously prepare wasm: "+t),I(t)}))}function C(t){for(;t.length>0;)t.shift()(s)}function T(t){this.excPtr=t,this.ptr=t-24,this.set_type=function(t){d[this.ptr+4>>2]=t},this.get_type=function(){return d[this.ptr+4>>2]},this.set_destructor=function(t){d[this.ptr+8>>2]=t},this.get_destructor=function(){return d[this.ptr+8>>2]},this.set_caught=function(t){t=t?1:0,m[this.ptr+12|0]=t},this.get_caught=function(){return 0!=m[this.ptr+12|0]},this.set_rethrown=function(t){t=t?1:0,m[this.ptr+13|0]=t},this.get_rethrown=function(){return 0!=m[this.ptr+13|0]},this.init=function(t,e){this.set_adjusted_ptr(0),this.set_type(t),this.set_destructor(e)},this.set_adjusted_ptr=function(t){d[this.ptr+16>>2]=t},this.get_adjusted_ptr=function(){return d[this.ptr+16>>2]},this.get_exception_ptr=function(){if(G(this.get_type()))return d[this.excPtr>>2];var t=this.get_adjusted_ptr();return 0!==t?t:this.excPtr}}function P(t){var e=c.buffer;try{return c.grow(t-e.byteLength+65535>>>16),w(),1}catch(t){}}function D(t){return s["_"+t]}N(y="wasmDbscan.wasm")||(b=y,y=s.locateFile?s.locateFile(b,l):l+b);var U="undefined"!=typeof TextDecoder?new TextDecoder("utf8"):void 0;function O(t,e,n,r,s){var i={string:t=>{var e=0;return null!=t&&0!==t&&(e=function(t){var e=function(t){for(var e=0,n=0;n<t.length;++n){var r=t.charCodeAt(n);r<=127?e++:r<=2047?e+=2:r>=55296&&r<=57343?(e+=4,++n):e+=3}return e}(t)+1,n=$(e);return function(t,e,n){!function(t,e,n,r){if(!(r>0))return 0;for(var s=n+r-1,i=0;i<t.length;++i){var o=t.charCodeAt(i);if(o>=55296&&o<=57343&&(o=65536+((1023&o)<<10)|1023&t.charCodeAt(++i)),o<=127){if(n>=s)break;e[n++]=o}else if(o<=2047){if(n+1>=s)break;e[n++]=192|o>>6,e[n++]=128|63&o}else if(o<=65535){if(n+2>=s)break;e[n++]=224|o>>12,e[n++]=128|o>>6&63,e[n++]=128|63&o}else{if(n+3>=s)break;e[n++]=240|o>>18,e[n++]=128|o>>12&63,e[n++]=128|o>>6&63,e[n++]=128|63&o}}e[n]=0}(t,g,e,n)}(t,n,e),n}(t)),e},array:t=>{var e,n,r=$(t.length);return e=t,n=r,m.set(e,n),r}},o=D(t),a=[],u=0;if(r)for(var l=0;l<r.length;l++){var h=i[n[l]];h?(0===u&&(u=_()),a[l]=h(r[l])):a[l]=r[l]}var c=o.apply(null,a);return function(t){return 0!==u&&L(u),function(t){return"string"===e?(n=t)?function(t,e,n){for(var r=e+n,s=e;t[s]&&!(s>=r);)++s;if(s-e>16&&t.buffer&&U)return U.decode(t.subarray(e,s));for(var i="";e<s;){var o=t[e++];if(128&o){var a=63&t[e++];if(192!=(224&o)){var u=63&t[e++];if((o=224==(240&o)?(15&o)<<12|a<<6|u:(7&o)<<18|a<<12|u<<6|63&t[e++])<65536)i+=String.fromCharCode(o);else{var l=o-65536;i+=String.fromCharCode(55296|l>>10,56320|1023&l)}}else i+=String.fromCharCode((31&o)<<6|a)}else i+=String.fromCharCode(o)}return i}(g,n,r):"":"boolean"===e?Boolean(t):t;var n,r}(t)}(c)}var z,B={__cxa_throw:function(t,e,n){throw new T(t).init(e,n),t},abort:function(){I("")},emscripten_memcpy_big:function(t,e,n){g.copyWithin(t,e,e+n)},emscripten_resize_heap:function(t){var e,n=g.length,r=2147483648;if((t>>>=0)>r)return!1;for(var s=1;s<=4;s*=2){var i=n*(1+.2/s);if(i=Math.min(i,t+100663296),P(Math.min(r,(e=Math.max(t,i))+(65536-e%65536)%65536)))return!0}return!1}},_=(function(){var t,e,r,i,o={env:B,wasi_snapshot_preview1:B};function a(t,e){var n,r=t.exports;return s.asm=r,c=s.asm.memory,w(),s.asm.__indirect_function_table,n=s.asm.__wasm_call_ctors,M.unshift(n),function(){if(S--,s.monitorRunDependencies&&s.monitorRunDependencies(S),0==S&&(null!==A&&(clearInterval(A),A=null),E)){var t=E;E=null,t()}}(),r}if(S++,s.monitorRunDependencies&&s.monitorRunDependencies(S),s.instantiateWasm)try{return s.instantiateWasm(o,a)}catch(t){f("Module.instantiateWasm callback failed with error: "+t),n(t)}(t=h,e=y,r=o,i=function(t){a(t.instance)},t||"function"!=typeof WebAssembly.instantiateStreaming||N(e)||"function"!=typeof fetch?k(e,r,i):fetch(e,{credentials:"same-origin"}).then((function(t){return WebAssembly.instantiateStreaming(t,r).then(i,(function(t){return f("wasm streaming compile failed: "+t),f("falling back to ArrayBuffer instantiation"),k(e,r,i)}))}))).catch(n)}(),s._dbscan=function(){return(s._dbscan=s.asm.dbscan).apply(null,arguments)},s._malloc=function(){return(s._malloc=s.asm.malloc).apply(null,arguments)},s._free=function(){return(s._free=s.asm.free).apply(null,arguments)},function(){return(_=s.asm.stackSave).apply(null,arguments)}),L=function(){return(L=s.asm.stackRestore).apply(null,arguments)},$=function(){return($=s.asm.stackAlloc).apply(null,arguments)},G=function(){return(G=s.asm.__cxa_is_pointer_type).apply(null,arguments)};function F(){function t(){z||(z=!0,s.calledRun=!0,p||(C(M),e(s),s.onRuntimeInitialized&&s.onRuntimeInitialized(),function(){if(s.postRun)for("function"==typeof s.postRun&&(s.postRun=[s.postRun]);s.postRun.length;)t=s.postRun.shift(),v.unshift(t);var t;C(v)}()))}S>0||(function(){if(s.preRun)for("function"==typeof s.preRun&&(s.preRun=[s.preRun]);s.preRun.length;)t=s.preRun.shift(),x.unshift(t);var t;C(x)}(),S>0||(s.setStatus?(s.setStatus("Running..."),setTimeout((function(){setTimeout((function(){s.setStatus("")}),1),t()}),1)):t()))}if(s.ccall=O,s.cwrap=function(t,e,n,r){var s=!n||n.every((t=>"number"===t||"boolean"===t));return"string"!==e&&s&&!r?D(t):function(){return O(t,e,n,arguments)}},E=function t(){z||F(),z||(E=t)},s.preInit)for("function"==typeof s.preInit&&(s.preInit=[s.preInit]);s.preInit.length>0;)s.preInit.pop()();return F(),t.ready});"object"==typeof exports?t.exports=s:"function"==typeof define&&n.amdO?define([],(function(){return s})):"object"==typeof exports&&(exports.exportCppDbscanLib=s)},606:(t,e,n)=>{"use strict";n.d(e,{Qt:()=>s,gD:()=>r}),n(6066);const r=t=>null==t;function s(t,e,n,r){if(n>t[t.length-1])return;const s=t.findIndex((t=>n<t));t.pop(),t.splice(s,0,n),e.pop(),e.splice(s,0,r)}},5284:(t,e,n)=>{"use strict";class r extends Float32Array{}var s=n(8498),i=n(2787),o=n(944),a=n(6295),u=n(6863);const l={[s.Pn.Euclidean]:a.jL,[s.Pn.Manhattan]:a.Hf,[s.Pn.Cosine]:a.w_},h={[s.xL.Levenshtein]:i.I,[s.xL.JaroWinkler]:o.Vb,[s.xL.Manhattan]:function(t,e){if(t.length!==e.length)return 1;{let n=0;for(let r=1;r<t.length;r++)n+=t[r]==e[r]?0:1;return n/t.length}},[s.xL.Onehot]:function(t,e){return t===e?0:1}},c={[s.aT.Tanimoto]:a.tO,[s.aT.Dice]:a.zn,[s.aT.Asymmetric]:a.gt,[s.aT.BraunBlanquet]:a.ij,[s.aT.Cosine]:a.lO,[s.aT.Kulczynski]:a.rR,[s.aT.McConnaughey]:a.lP,[s.aT.RogotGoldberg]:a.TK,[s.aT.Russel]:a.yK,[s.aT.Sokal]:a.Ii,[s.aT.Hamming]:a.be,[s.aT.Euclidean]:a.Qj},f={[s.m6.TanimotoIntArray]:a.v_},m={[s.RR.Difference]:a.T6},g={[s.Wn.CommonItems]:a.Hr},d={[s.EG.Vector]:{[s.Pn.Euclidean]:l[s.Pn.Euclidean],[s.Pn.Manhattan]:l[s.Pn.Manhattan],[s.Pn.Cosine]:l[s.Pn.Cosine]},[s.EG.String]:{[s.xL.Levenshtein]:h[s.xL.Levenshtein],[s.xL.JaroWinkler]:h[s.xL.JaroWinkler],[s.xL.Manhattan]:h[s.xL.Manhattan],[s.xL.Onehot]:h[s.xL.Onehot]},[s.EG.BitArray]:{[s.aT.Tanimoto]:c[s.aT.Tanimoto],[s.aT.Dice]:c[s.aT.Dice],[s.aT.Asymmetric]:c[s.aT.Asymmetric],[s.aT.BraunBlanquet]:c[s.aT.BraunBlanquet],[s.aT.Cosine]:c[s.aT.Cosine],[s.aT.Kulczynski]:c[s.aT.Kulczynski],[s.aT.McConnaughey]:c[s.aT.McConnaughey],[s.aT.RogotGoldberg]:c[s.aT.RogotGoldberg],[s.aT.Russel]:c[s.aT.Russel],[s.aT.Sokal]:c[s.aT.Sokal]},[s.EG.MacroMolecule]:{[u.Z.HAMMING]:u.V[u.Z.HAMMING],[u.Z.LEVENSHTEIN]:u.V[u.Z.LEVENSHTEIN],[u.Z.NEEDLEMANN_WUNSCH]:u.V[u.Z.NEEDLEMANN_WUNSCH],[u.Z.MONOMER_CHEMICAL_DISTANCE]:u.V[u.Z.MONOMER_CHEMICAL_DISTANCE]},[s.EG.Number]:{[s.RR.Difference]:m[s.RR.Difference]},[s.EG.IntArray]:{[s.m6.TanimotoIntArray]:f[s.m6.TanimotoIntArray]},[s.EG.NumberArray]:{[s.Wn.CommonItems]:g[s.Wn.CommonItems]}},p=Object.keys(d).reduce(((t,e)=>{for(const n of Object.keys(d[e]))t[n]=e;return t}),{});class w{constructor(t){this.method=t,this.dataType=p[t]}metricNeedsArgs(t){return p[t]==s.EG.MacroMolecule.toString()||p[t]==s.EG.Number.toString()||function(t){return p[t]==s.EG.NumberArray.toString()}(t)}getMeasure(t){const e=d;if(!e.hasOwnProperty(this.dataType)||!e[this.dataType].hasOwnProperty(this.method))throw new Error(`Unknown measure ${this.method} for data type ${this.dataType}`);return this.metricNeedsArgs(this.method)?e[this.dataType][this.method](t):e[this.dataType][this.method]}static getMetricByDataType(t){return Object.keys(d[t])}static get availableMeasures(){return Object.keys(d)}}n(606);var y=n(6066);class b{constructor(t=!0,e=!0){const r=navigator.hardwareConcurrency;this._workerCount=t?Math.max(r-2,1):1,this._workers=new Array(this._workerCount).fill(null).map((()=>new Worker(new URL(n.p+n.u(793),n.b)))),this._terminateOnComplete=e}async calc(t,e,n=!0,r){return await this.calcMulti([t],[e],n,[r??{}],[1],y.G.MANHATTAN)}async calcMulti(t,e,n=!0,r=[{}],s=[1],i=y.G.MANHATTAN){if(t.length<1)throw new Error("values must contain at least one array");if(e.length!==t.length||r.length!==t.length||s.length!==t.length)throw new Error("values, fnNames, weights and opts must have the same length");return new Promise((async(o,a)=>{try{const a=t[0].length,u=new Array(this._workerCount),l=a*(a-1)/2;this._workerCount=Math.min(this._workerCount,l);const h=l/this._workerCount,c=new Float32Array(l);let f=0,m=1,g=0,d=Number.MIN_VALUE;for(let n=0;n<this._workerCount;n++){const o=Math.floor(n*h),p=n===this._workerCount-1?l:Math.floor((n+1)*h),w=f,y=m;n!==this._workerCount-1&&(f=a-2-Math.floor(Math.sqrt(-8*p+4*a*(a-1)-7)/2-.5),m=p-a*f+Math.floor((f+1)*(f+2)/2)),this._workers[n].postMessage({values:t,fnNames:e,startRow:w,startCol:y,chunckSize:p-o,opts:r,weights:s,aggregationMethod:i}),u[n]=new Promise(((t,e)=>{this._workers[n].onmessage=({data:{error:r,distanceMatrixData:s,min:i,max:a}})=>{this._terminateOnComplete&&setTimeout((()=>this._workers[n].terminate())),r?e(r):(c.set(s,o),i<g&&(g=i),a>d&&(d=a),t())}}))}await Promise.all(u),n&&c.forEach(((t,e)=>{c[e]=(t-g)/(d-g)})),o(c)}catch(t){a(t)}}))}terminate(){this._workers.forEach((t=>t.terminate()))}}function x(t,e){return Math.floor(e()*t)}function M(t){return t()}function v(t){const e=[];for(let n=0;n<t;n++)e.push(void 0);return e}function S(t){return v(t).map(((t,e)=>e))}function A(t,e){return v(t).map((()=>e))}function E(t){return A(t,0)}function I(t){return function(t){return t.reduce(((t,e)=>t+e))}(t)/t.length}function N(t){let e=0;for(let n=0;n<t.length;n++)e=t[n]>e?t[n]:e;return e}function R(t,e,n){const r=E(t);for(let s=0;s<t;s++){let t=!0;for(;t;){const i=x(e,n);let o=!1;for(let t=0;t<s;t++)if(i===r[t]){o=!0;break}o||(t=!1),r[s]=i}}return r}function k(t,e,n){const r=[];let s=0;if(t.length!==e*n)throw new Error("Array dimensions must match input length.");for(let i=0;i<e;i++){const e=[];for(let r=0;r<n;r++)e.push(t[s]),s+=1;r.push(e)}return r}function C(t,e){const n=n=>v(t).map((()=>A(e,n))),r=[];return r.push(n(-1)),r.push(n(1/0)),r.push(n(0)),r}function T(t,e,n){const r=E(t);for(let s=0;s<t;s++){let t=!0,i=0;for(;t;){i=x(e,n);let o=!1;for(let t=0;t<s;t++)if(i===r[t]){o=!0;break}o||(t=!1)}r[s]=i}return r}function P(t,e,n,r,s){e=Math.floor(e);const i=t[0][e];if(n>=t[1][e][0])return 0;for(let t=0;t<i.length;t++)if(r===i[t])return 0;return D(t,e,n,r,s)}function D(t,e,n,r,s){const i=t[0][e],o=t[1][e],a=t[2][e];if(n>=o[0])return 0;o[0]=n,i[0]=r,a[0]=s;let u=0,l=0;for(;;){const e=2*u+1,r=e+1,s=t[0][0].length;if(e>=s)break;if(r>=s){if(!(o[e]>n))break;l=e}else if(o[e]>=o[r]){if(!(n<o[e]))break;l=e}else{if(!(n<o[r]))break;l=r}o[u]=o[l],i[u]=i[l],a[u]=a[l],u=l}return o[u]=n,i[u]=r,a[u]=s,1}function U(t,e,n,r,s){const i=C(e,r);for(let r=0;r<e;r++)for(let e=0;e<n;e++){if(t[0][r][e]<0)continue;const n=t[0][r][e],o=t[2][r][e],a=M(s);P(i,r,a,n,o),P(i,n,a,r,o),t[2][r][e]=0}return i}function O(t){const e=t[0],n=t[1];for(let t=0;t<e.length;t++){const r=e[t],s=n[t];for(let t=0;t<r.length-1;t++){const e=r.length-t-1,n=s.length-t-1,i=r[0];r[0]=r[e],r[e]=i;const o=s[0];s[0]=s[n],s[n]=o,z(s,r,n,0)}}return{indices:e,weights:n}}function z(t,e,n,r){for(;2*r+1<n;){const s=2*r+1,i=s+1;let o=r;if(t[o]<t[s]&&(o=s),i<n&&t[o]<t[i]&&(o=i),o===r)break;{const n=t[r];t[r]=t[o],t[o]=n;const s=e[r];e[r]=e[o],e[o]=s,r=o}}}function B(t,e){const n=t[0][e],r=t[1][e],s=t[2][e];let i=1/0,o=-1;for(let t=0;t>n.length;t++)1===s[t]&&r[t]<i&&(i=r[t],o=t);return o>=0?(s[o]=0,Math.floor(n[o])):-1}class _{constructor(t,e,n,r){if(this.entries=new Map,this.nRows=0,this.nCols=0,t.length!==e.length||t.length!==n.length)throw new Error("rows, cols and values arrays must all have the same length");this.nRows=r[0],this.nCols=r[1];for(let r=0;r<n.length;r++){const s=t[r],i=e[r];this.checkDims(s,i);const o=this.makeKey(s,i);this.entries.set(o,{value:n[r],row:s,col:i})}}makeKey(t,e){return`${t}:${e}`}checkDims(t,e){if(!(t<this.nRows&&e<this.nCols))throw new Error("row and/or col specified outside of matrix dimensions")}set(t,e,n){this.checkDims(t,e);const r=this.makeKey(t,e);this.entries.has(r)?this.entries.get(r).value=n:this.entries.set(r,{value:n,row:t,col:e})}get(t,e,n=0){const r=this.makeKey(t,e);return this.entries.has(r)?this.entries.get(r).value:n}getAll(t=!0){const e=new Array(this.entries.size).fill(null);let n=0;return this.entries.forEach((t=>{e[n++]=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 n=0;this.entries.forEach((r=>{e[n++]=t(r.value,r.row,r.col)}));const r=[this.nRows,this.nCols];return new _(this.getRows(),this.getCols(),e,r)}toArray(){const t=v(this.nRows).map((()=>E(this.nCols)));return this.entries.forEach((e=>{t[e.row][e.col]=e.value})),t}}function L(t){const e=t.getRows(),n=t.getCols(),r=t.getValues(),s=n.length,i=new Int32Array(s),o=new Int32Array(s),a=new Float32Array(s);i.set(e),o.set(n),a.set(r);const u=[t.nCols,t.nRows];return new _(o,i,a,u)}function $(t,e){return K(t,e,((t,e)=>t*e))}function G(t,e){return K(t,e,((t,e)=>t+e))}function F(t,e){return K(t,e,((t,e)=>t-e))}function j(t,e){return t.map((t=>t*e))}function q(t){const e=new Set,n=t.getValues(),r=t.getRows(),s=t.getCols();for(let t=0;t<n.length;t++)0===n[t]&&e.add(t);const i=(t,n)=>!e.has(n),o=n.filter(i),a=r.filter(i),u=s.filter(i);return new _(a,u,o,t.getDims())}function V(t,e="l2"){const n=Y[e],r=new Map;t.forEach(((t,e,n)=>{const s=r.get(e)||[];s.push(n),r.set(e,s)}));const s=new _([],[],[],t.getDims());for(const e of r.keys()){const i=r.get(e).sort(),o=n(i.map((n=>t.get(e,n))));for(let t=0;t<o.length;t++)s.set(e,i[t],o[t])}return s}const Y={max:t=>{let e=-1/0;for(let n=0;n<t.length;n++)e=t[n]>e?t[n]:e;return t.map((t=>t/e))},l1:t=>{let e=0;for(let n=0;n<t.length;n++)e+=t[n];return t.map((t=>t/e))},l2:t=>{let e=0;for(let n=0;n<t.length;n++)e+=t[n]**2;return t.map((t=>Math.sqrt(t**2/e)))}};function K(t,e,n){const r=new Set,s=[],i=[],o=[],a=(r,a)=>{s.push(r),i.push(a);const u=n(t.get(r,a),e.get(r,a));o.push(u)},u=t.getValues(),l=t.getRows(),h=t.getCols();for(let t=0;t<u.length;t++){const e=l[t],n=h[t],s=`${e}:${n}`;r.add(s),a(e,n)}const c=e.getValues(),f=e.getRows(),m=e.getCols();for(let t=0;t<c.length;t++){const e=f[t],n=m[t],s=`${e}:${n}`;r.has(s)||a(e,n)}const g=[t.nRows,t.nCols];return new _(s,i,o,g)}function W(t){const e=[];t.forEach(((t,n,r)=>{e.push({value:t,row:n,col:r})})),e.sort(((t,e)=>t.row===e.row?t.col-e.col:t.row-e.row));const n=[],r=[],s=[];let i=-1;for(let t=0;t<e.length;t++){const{row:o,col:a,value:u}=e[t];o!==i&&(i=o,s.push(t)),n.push(a),r.push(u)}return{indices:n,values:r,indptr:s}}class H{constructor(t,e,n,r){this.hyperplanes=t,this.offsets=e,this.children=n,this.indices=r}}function Z(t,e,n,r){const s=Math.max(10,e),i=S(n).map(((e,n)=>function(t,e=30,n,r){return X(t,S(t.length),e,n,r)}(t,s,n,r))),o=i.map((t=>function(t,e){const n=J(t),r=tt(t),s=S(n).map((()=>t.hyperplane?1:0)),i=E(n),o=S(n).map((()=>[-1,-1])),a=S(r).map((()=>S(e).map((()=>-1))));return Q(t,s,i,o,a,0,0),new H(s,i,o,a)}(t,s)));return o}function X(t,e,n=30,r,s){if(e.length>n){const i=function(t,e,n){const r=x(e.length,n);let s=x(e.length,n);s+=r===s?1:0,s%=e.length;const i=e[r],o=e[s];let a=0,u=0;u=t[i]-t[o],a-=u*(t[i]+t[o])/2;let l=0,h=0;const c=E(e.length);for(let r=0;r<e.length;r++){let s=a;s+=u*t[e[r]],0===s?(c[r]=x(2,n),0===c[r]?l+=1:h+=1):s>0?(c[r]=0,l+=1):(c[r]=1,h+=1)}const f=E(l),m=E(h);l=0,h=0;for(let t=0;t<c.length;t++)0===c[t]?(f[l]=e[t],l+=1):(m[h]=e[t],h+=1);return{indicesLeft:f,indicesRight:m,hyperplane:u,offset:a}}(t,e,s),{indicesLeft:o,indicesRight:a,hyperplane:u,offset:l}=i;return{leftChild:X(t,o,n,r+1,s),rightChild:X(t,a,n,r+1,s),isLeaf:!1,hyperplane:u,offset:l}}return{indices:e,isLeaf:!0}}function Q(t,e,n,r,s,i,o){if(t.isLeaf)return r[i][0]=-o,s[o].splice(0,t.indices.length,...t.indices),{nodeNum:i,leafNum:o+=1};{e[i]=t.hyperplane,n[i]=t.offset,r[i][0]=i+1;const a=i;let u=Q(t.leftChild,e,n,r,s,i+1,o);return i=u.nodeNum,o=u.leafNum,r[a][1]=i+1,u=Q(t.rightChild,e,n,r,s,i+1,o),{nodeNum:u.nodeNum,leafNum:u.leafNum}}}function J(t){return t.isLeaf?1:1+J(t.leftChild)+J(t.rightChild)}function tt(t){return t.isLeaf?1:tt(t.leftChild)+tt(t.rightChild)}function et(t,e,n,r){let s=e;return s+=t*n,0===s?x(2,r):s>0?0:1}function nt(t,e,n){let r=0;for(;e.children[r][0]>0;)r=0===et(e.hyperplanes[r],e.offsets[r],t,n)?e.children[r][0]:e.children[r][1];const s=-1*e.children[r][0];return e.indices[s]}const rt=Object.prototype.toString;function st(t){return rt.call(t).endsWith("Array]")}function it(t,e,n){let r=0;const s=n(e);for(let e=0;e<t.x.length;e++)r+=Math.abs(t.y[e]-s(t.x[e]));return r}var ot=n(3054);ot.y3,ot.jy,ot.oN,ot.Hc,ot.cg,ot.hj,ot.LU,ot.Tb;const at=ot.uq,ut=(ot.Zm,ot.Dq,ot.__,ot.q0,ot.lh,ot.pI,ot.zC,ot.zg,ot.g6,ot.OL,ot.ks,ot.QR,ot.jp,ot.mk,ot.W2,ot.l,ot.KY,ot.dv,ot.BR,ot.Wu,ot.uq,ot.uq,ot.a4,ot.DI);function lt(t,e,n,r,s){let i=n*r*r,o=at.eye(e.length,e.length,i);const a=s(e);let u=new Float64Array(t.x.length);for(let e=0;e<t.x.length;e++)u[e]=a(t.x[e]);let l=function(t,e,n,r,s){const i=n.length,o=t.x.length;let a=new Array(i);for(let u=0;u<i;u++){a[u]=new Array(o);let i=n.slice();i[u]+=r;let l=s(i);for(let n=0;n<o;n++)a[u][n]=e[n]-l(t.x[n])}return new at(a)}(t,u,e,r,s),h=function(t,e){const n=t.x.length;let r=new Array(n);for(let s=0;s<n;s++)r[s]=[t.y[s]-e[s]];return new at(r)}(t,u),c=ut(o.add(l.mmul(l.transpose())));return(e=(e=new at([e])).sub(c.mmul(l).mmul(h).mul(r).transpose())).to1DArray()}ot.Jo,ot.Zi,ot.kH,ot.LV;const ht=1e-5,ct=.001;class ft{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=15,this.distanceFn=mt,this.isInitialized=!1,this.rpForest=[],this.embedding=[],this.optimizationState=new gt;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"),this.targetNNeighbors=t.nNeighbors||this.nNeighbors||this.targetNNeighbors}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:n,epochsPerSample:r}=this.initializeSimplicialSetEmbedding();return this.optimizationState.head=e,this.optimizationState.tail=n,this.optimizationState.epochsPerSample=r,this.initializeOptimization(),this.prepareForOptimizationLoop(),this.isInitialized=!0,this.getNEpochs()}makeSearchFns(){const{initFromTree:t,initFromRandom:e}=(n=this.distanceFn,{initFromRandom:function(t,e,r,s,i){for(let o=0;o<r.length;o++){const a=R(t,e.length,i);for(let t=0;t<a.length;t++)a[t]<0||P(s,o,n(e[a[t]],r[o]),a[t],1)}},initFromTree:function(t,e,r,s,i){for(let o=0;o<r.length;o++){const a=nt(r[o],t,i);for(let t=0;t<a.length;t++){if(a[t]<0)return;P(s,o,n(e[a[t]],r[o]),a[t],1)}}}});var n;this.initFromTree=t,this.initFromRandom=e,this.search=function(t){return function(e,n,r,s){const{indices:i,indptr:o}=W(n);for(let n=0;n<s.length;n++){const a=new Set(r[0][n]);for(;;){const u=B(r,n);if(-1===u)break;const l=i.slice(o[u],o[u+1]);for(const i of l)i===u||-1===i||a.has(i)||(D(r,n,t(e[i],s[n]),i,1),a.add(i))}}return r}}(this.distanceFn)}makeSearchGraph(t){const e=this.knnIndices,n=this.knnDistances,r=[t.length,t.length],s=new _([],[],[],r);for(let t=0;t<e.length;t++){const r=e[t],i=n[t];for(let e=0;e<r.length;e++){const n=r[e],o=i[e];o>0&&s.set(t,n,o)}}return K(s,L(s),((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 n=Math.floor(this.nNeighbors*this.transformQueueSize);n=Math.min(e.length,n);const r=function(t,e,n,r,s,i,o){const a=C(n.length,r);if(s(r,e,n,a,o),t)for(const r of t)i(r,e,n,a,o);return a}(this.rpForest,e,t,n,this.initFromRandom,this.initFromTree,this.random),s=this.search(e,this.searchGraph,r,t);let{indices:i,weights:o}=O(s);i=i.map((t=>t.slice(0,this.nNeighbors))),o=o.map((t=>t.slice(0,this.nNeighbors)));const a=Math.max(0,this.localConnectivity-1),{sigmas:u,rhos:l}=this.smoothKNNDistance(o,this.nNeighbors,a),{rows:h,cols:c,vals:f}=this.computeMembershipStrengths(i,o,u,l),m=[t.length,e.length];let g=new _(h,c,f,m);const d=W(V(g,"l1")),p=t.length,w=function(t,e,n){const r=E(t.length).map((t=>E(n[0].length)));for(let s=0;s<t.length;s++)for(let i=0;i<t[0].length;i++)for(let o=0;o<n[0].length;o++){const a=t[s][i];r[s][o]+=e[s][i]*n[a][o]}return r}(k(d.indices,p,this.nNeighbors),k(d.values,p,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=q(g);const x=this.makeEpochsPerSample(g.getValues(),y),M=g.getRows(),v=g.getCols();return this.assignOptimizationStateParameters({headEmbedding:w,tailEmbedding:this.embedding,head:M,tail:v,currentEpoch:0,nEpochs:y,nVertices:g.getDims()[1],epochsPerSample:x}),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:n}=this,r=function(t,e){return function(n,r,s,i=10,o=50,a=.001,u=.5,l=!0){const h=n.length,c=C(n.length,s);for(let r=0;r<n.length;r++){const i=T(s,n.length,e);for(let e=0;e<i.length;e++){const s=t(n[r],n[i[e]]);P(c,r,s,i[e],1),P(c,i[e],s,r,1)}}if(l)for(let e=0;e<r.length;e++)for(let s=0;s<r[e].length&&!(r[e][s]<0);s++)for(let i=s+1;i<r[e].length&&!(r[e][i]<0);i++){const o=t(n[r[e][s]],n[r[e][i]]);P(c,r[e][s],o,r[e][i],1),P(c,r[e][i],o,r[e][s],1)}for(let r=0;r<i;r++){const r=U(c,h,s,o,e);let i=0;for(let s=0;s<h;s++)for(let a=0;a<o;a++){const l=Math.floor(r[0][s][a]);if(!(l<0||M(e)<u))for(let e=0;e<o;e++){const o=Math.floor(r[0][s][e]),u=r[2][s][a],h=r[2][s][e];if(o<0||!u&&!h)continue;const f=t(n[l],n[o]);i+=P(c,l,f,o,1),i+=P(c,o,f,l,1)}}if(i<=a*s*n.length)break}return O(c)}}(e,this.random),s=5+Math.floor(.5==(i=t.length**.5/20)?0:Math.round(i));var i;const o=Math.max(5,Math.floor(Math.round((t=>Math.log(t)/Math.log(2))(t.length))));this.rpForest=Z(t,n,s,this.random);const a=function(t){if(t.length>0){const e=[];for(const n of t)e.push(...n.indices);return e}return[[-1]]}(this.rpForest),{indices:u,weights:l}=r(t,a,n,o);return{knnIndices:u,knnDistances:l}}fuzzySimplicialSet(t,e,n=1){const{knnIndices:r=[],knnDistances:s=[],localConnectivity:i}=this,{sigmas:o,rhos:a}=this.smoothKNNDistance(s,e,i),{rows:u,cols:l,vals:h}=this.computeMembershipStrengths(r,s,o,a),c=[t.length,t.length],f=new _(u,l,h,c),m=L(f),g=$(f,m),d=F(G(f,m),g);return G(j(d,n),j(g,1-n))}categoricalSimplicialSetIntersection(t,e,n,r=1){let s=function(t,e,n=1,r=5){return t.map(((t,s,i)=>-1===e[s]||-1===e[i]?t*Math.exp(-n):e[s]!==e[i]?t*Math.exp(-r):t))}(t,e,r,n);return s=q(s),function(t){const e=L(t=V(t,"max"));return q(t=G(t,F(e,$(e,t))))}(s)}smoothKNNDistance(t,e,n=1,r=64,s=1){const i=Math.log(e)/Math.log(2)*s,o=E(t.length),a=E(t.length);for(let e=0;e<t.length;e++){let s=0,u=1/0,l=1;const h=t[e],c=h.filter((t=>t>0));if(c.length>=n){const t=Math.floor(n),r=n-t;t>0?(o[e]=c[t-1],r>ht&&(o[e]+=r*(c[t]-c[t-1]))):o[e]=r*c[0]}else c.length>0&&(o[e]=N(c));for(let n=0;n<r;n++){let n=0;for(let r=1;r<t[e].length;r++){const s=t[e][r]-o[e];n+=s>0?Math.exp(-s/l):1}if(Math.abs(n-i)<ht)break;n>i?(u=l,l=(s+u)/2):(s=l,u===1/0?l*=2:l=(s+u)/2)}if(a[e]=l,o[e]>0){const t=I(h);a[e]<ct*t&&(a[e]=ct*t)}else{const n=I(t.map(I));a[e]<ct*n&&(a[e]=ct*n)}}return{sigmas:a,rhos:o}}computeMembershipStrengths(t,e,n,r){const s=t.length,i=t[0].length,o=E(s*i),a=E(s*i),u=E(s*i);for(let l=0;l<s;l++)for(let s=0;s<i;s++){let h=0;-1!==t[l][s]&&(h=t[l][s]===l?0:e[l][s]-r[l]<=0?1:Math.exp(-(e[l][s]-r[l])/n[l]),o[l*i+s]=l,a[l*i+s]=t[l][s],u[l*i+s]=h)}return{rows:o,cols:a,vals:u}}initializeSimplicialSetEmbedding(){const t=this.getNEpochs(),{nComponents:e}=this,n=this.graph.getValues();let r=0;for(let t=0;t<n.length;t++){const e=n[t];r<n[t]&&(r=e)}const s=this.graph.map((e=>e<r/t?0:e));this.embedding=E(s.nRows).map((()=>E(e).map((()=>20*M(this.random)-10))));const i=[],o=[],a=[],u=s.getAll();for(let t=0;t<u.length;t++){const e=u[t];e.value&&(i.push(e.value),a.push(e.row),o.push(e.col))}return{head:o,tail:a,epochsPerSample:this.makeEpochsPerSample(i,t)}}makeEpochsPerSample(t,e){const n=A(t.length,-1),r=N(t),s=t.map((t=>t/r*e));return s.forEach(((t,r)=>{t>0&&(n[r]=e/s[r])})),n}assignOptimizationStateParameters(t){Object.assign(this.optimizationState,t)}prepareForOptimizationLoop(){const{repulsionStrength:t,learningRate:e,negativeSampleRate:n}=this,{epochsPerSample:r,headEmbedding:s,tailEmbedding:i}=this.optimizationState,o=s[0].length,a=s.length===i.length,u=r.map((t=>t/n)),l=[...u],h=[...r];this.assignOptimizationStateParameters({epochOfNextSample:h,epochOfNextNegativeSample:l,epochsPerNegativeSample:u,moveOther:a,initialAlpha:e,alpha:e,gamma:t,dim:o})}initializeOptimization(){const t=this.embedding,e=this.embedding,{head:n,tail:r,epochsPerSample:s}=this.optimizationState,i=this.getNEpochs(),o=this.graph.nCols,{a,b:u}=function(t,e){const n=function(t,e){return v(300).map(((t,n)=>0+n*((e-0)/299)))}(0,3*t).map((t=>t<e?1:t)),r=E(n.length).map(((r,s)=>n[s]>=e?Math.exp(-(n[s]-e)/t):r)),s={x:n,y:r},{parameterValues:i}=function(t,e,n={}){let{maxIterations:r=100,gradientDifference:s=.1,damping:i=0,errorTolerance:o=.01,minValues:a,maxValues:u,initialValues:l}=n;if(i<=0)throw new Error("The damping option must be a positive number");if(!t.x||!t.y)throw new Error("The data parameter must have x and y elements");if(!st(t.x)||t.x.length<2||!st(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 h=l||new Array(e.length).fill(1),c=h.length;if(u=u||new Array(c).fill(Number.MAX_SAFE_INTEGER),a=a||new Array(c).fill(Number.MIN_SAFE_INTEGER),u.length!==a.length)throw new Error("minValues and maxValues must be the same size");if(!st(h))throw new Error("initialValues must be an array");let f,m=it(t,h,e),g=m<=o;for(f=0;f<r&&!g;f++){h=lt(t,h,i,s,e);for(let t=0;t<c;t++)h[t]=Math.min(Math.max(a[t],h[t]),u[t]);if(m=it(t,h,e),isNaN(m))break;g=m<=o}return{parameterValues:h,parameterError:m,iterations:f}}(s,(([t,e])=>n=>1/(1+t*n**(2*e))),{damping:1.5,initialValues:[.5,.5],gradientDifference:.1,maxIterations:100,errorTolerance:.01}),[o,a]=i;return{a:o,b:a}}(this.spread,this.minDist);this.assignOptimizationStateParameters({headEmbedding:t,tailEmbedding:e,head:n,tail:r,epochsPerSample:s,a,b:u,nEpochs:i,nVertices:o})}optimizeLayoutStep(t){const{optimizationState:e}=this,{head:n,tail:r,headEmbedding:s,tailEmbedding:i,epochsPerSample:o,epochOfNextSample:a,epochOfNextNegativeSample:u,epochsPerNegativeSample:l,moveOther:h,initialAlpha:c,alpha:f,gamma:m,a:g,b:d,dim:p,nEpochs:w,nVertices:y}=e;for(let e=0;e<o.length;e++){if(a[e]>t)continue;const c=n[e],w=r[e],b=s[c],M=i[w],v=pt(b,M);let S=0;v>0&&(S=-2*g*d*Math.pow(v,d-1),S/=g*Math.pow(v,d)+1);for(let t=0;t<p;t++){const e=dt(S*(b[t]-M[t]),4);b[t]+=e*f,h&&(M[t]+=-e*f)}a[e]+=o[e];const A=Math.floor((t-u[e])/l[e]);for(let t=0;t<A;t++){const t=x(y,this.random),e=i[t],n=pt(b,e);let r=0;if(n>0)r=2*m*d,r/=(.001+n)*(g*Math.pow(n,d)+1);else if(c===t)continue;for(let t=0;t<p;t++){let n=4;r>0&&(n=dt(r*(b[t]-e[t]),4)),b[t]+=n*f}}u[e]+=A*l[e]}return e.alpha=c*(1-t/w),e.currentEpoch+=1,s}optimizeLayoutAsync(t=()=>!0){return new Promise(((e,n)=>{const r=async()=>{try{const{nEpochs:n,currentEpoch:s}=this.optimizationState;this.embedding=this.optimizeLayoutStep(s);const i=this.optimizationState.currentEpoch,o=!1===t(i),a=i===n;if(o||a)return e(a);setTimeout((()=>r()),0)}catch(t){n(t)}};setTimeout((()=>r()),0)}))}optimizeLayout(t=()=>!0){let e=!1,n=[];for(;!e;){const{nEpochs:r,currentEpoch:s}=this.optimizationState;n=this.optimizeLayoutStep(s);const i=this.optimizationState.currentEpoch,o=!1===t(i);e=i===r||o}return n}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 mt(t,e){return Math.abs(t-e)}class gt{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 dt(t,e){return t>e?e:t<-e?-e:t}function pt(t,e){let n=0;for(let r=0;r<t.length;r++)n+=Math.pow(t[r]-e[r],2);return n}function wt(t=!1,e="Assertion error."){if(!t)throw new Error(e)}var yt=n(5731),bt=n(8774),xt=n(7391),Mt=n.n(xt);class vt{assert(t,e){if(!t)throw e||"Assertion failed"}getopt(t,e,n){return t.hasOwnProperty(e)?t[e]:n}gaussRandom(){if(this.returnV)return this.returnV=!1,this.vValue;const t=2*this.random()-1,e=2*this.random()-1,n=t*t+e*e;if(0===n||n>1)return this.gaussRandom();const r=Math.sqrt(-2*Math.log(n)/n);return this.vValue=e*r,this.returnV=!0,t*r}randn(t,e){return t+this.gaussRandom()*e}zeros(t){if(void 0===t||isNaN(t))return new Float32Array;if("undefined"==typeof ArrayBuffer){const e=new Float32Array(t);for(let n=0;n<t;n++)e[n]=0;return e}return new Float32Array(t)}randn2d(t,e,n){const r=void 0!==n,s=new Array(t).fill(null).map((()=>new Float32Array(e)));if(r)for(let r=0;r<t;r++)s[r]=new Float32Array(e).fill(n);else for(let n=0;n<t;n++)for(let t=0;t<e;t++)s[n][t]=this.randn(0,1e-4);return s}l2(t,e){const n=t.length;let r=0;for(let s=0;s<n;s++){const n=t[s],i=e[s];r+=(n-i)*(n-i)}return r}xtod(t){const e=t.length,n=this.zeros(e*e);for(let r=0;r<e;r++)for(let s=r+1;s<e;s++){const i=this.l2(t[r],t[s]);n[r*e+s]=i,n[s*e+r]=i}return n}getIterSize(t){return t<=2e3?100:t<=3e3?90:t<=5e3?80:70}d2p(t,e,n,r){const s=(o=r,(t,e)=>o*t+e-Math.floor((t+2)*(t+1)/2)),i=new Float32Array(r*r);var o;for(let e=0;e<r;e++)for(let n=e+1;n<r;n++)i[e*r+n]=i[n*r+e]=t[s(e,n)];const a=r,u=Math.log(e),l=this.zeros(a*a),h=this.zeros(a);for(let t=0;t<a;t++){let e=-1/0,s=1/0,o=1,c=!1;const f=Math.floor(this.getIterSize(r)/5);let m=0;for(;!c;){let r=0;for(let e=0;e<a;e++){const n=t===e?0:Math.exp(-i[t*a+e]*o);h[e]=n,r+=n}let l=0;for(let t=0;t<a;t++){let e;e=0===r?0:h[t]/r,h[t]=e,e>1e-7&&(l-=e*Math.log(e))}l>u?(e=o,s===1/0?o*=2:o=(o+s)/2):(s=o,e===-1/0?o/=2:o=(o+e)/2),m++,Math.abs(l-u)<n&&(c=!0),m>=f&&(c=!0)}for(let e=0;e<a;e++)l[t*a+e]=h[e]}const c=this.zeros(a*a),f=2*a;for(let t=0;t<a;t++)for(let e=0;e<a;e++)c[t*a+e]=Math.max((l[t*a+e]+l[e*a+t])/f,1e-100);return c}sign(t){return t>0?1:t<0?-1:0}constructor(t){this.iter=0,this.random=Math.random,this.returnV=!1,this.vValue=0,t=t||{},this.perplexity=this.getopt(t,"perplexity",30),this.dim=this.getopt(t,"dim",2),this.epsilon=this.getopt(t,"epsilon",10),this.random=this.getopt(t,"random",Math.random)}initDataRaw(t){const e=t.length,n=t[0].length;this.assert(e>0," X is empty? You must have some data!"),this.assert(n>0," X[0] is empty? Where is the data?");const r=this.xtod(t);this.P=this.d2p(r,this.perplexity,1e-4,n),this.N=e,this.initSolution()}initDataDist(t,e){const n=t.length;this.assert(n>0," X is empty? You must have some data!"),console.time("distances to matrix"),this.P=this.d2p(t,this.perplexity,1e-4,e),console.timeEnd("distances to matrix"),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),n=e.cost,r=e.grad,s=this.zeros(this.dim);for(let e=0;e<t;e++)for(let t=0;t<this.dim;t++){const n=r[e][t],i=this.ystep[e][t],o=this.gains[e][t];let a=this.sign(n)===this.sign(i)?.8*o:o+.2;a<.01&&(a=.01),this.gains[e][t]=a;const u=(this.iter<250?.5:.8)*i-this.epsilon*a*r[e][t];this.ystep[e][t]=u,this.Y[e][t]+=u,s[t]+=this.Y[e][t]}for(let e=0;e<t;e++)for(let n=0;n<this.dim;n++)this.Y[e][n]-=s[n]/t;return n}debugGrad(){const t=this.N,e=this.costGrad(this.Y).grad,n=1e-5;for(let r=0;r<t;r++)for(let t=0;t<this.dim;t++){const s=this.Y[r][t];this.Y[r][t]=s+n;const i=this.costGrad(this.Y);this.Y[r][t]=s-n;const o=this.costGrad(this.Y),a=e[r][t],u=(i.cost-o.cost)/(2*n);console.log(r+","+t+": gradcheck analytic: "+a+" vs. numerical: "+u),this.Y[r][t]=s}}costGrad(t){const e=this.N,n=this.dim,r=this.P,s=this.iter<100?4:1;this.quArr??(this.quArr=this.zeros(e*e));let i=0;for(let r=0;r<e;r++)for(let s=r+1;s<e;s++){const o=1/(1+new Array(n).reduce(((e,n,i)=>e+Math.pow(t[r][i]-t[s][i],2)),0));this.quArr[r*e+s]=o,this.quArr[s*e+r]=o,i+=2*o}let o=0;const a=new Array(e).fill(null).map((()=>new Float32Array(n).fill(0)));for(let u=0;u<e;u++)for(let l=u+1;l<e;l++){const h=Math.max(this.quArr[u*e+l]/i,1e-100);o+=-r[u*e+l]*Math.log(h)*2;const c=4*(s*r[u*e+l]-h)*this.quArr[u*e+l];for(let e=0;e<n;e++)a[u][e]+=c*(t[u][e]-t[l][e]),a[l][e]+=c*(t[l][e]-t[u][e])}return{cost:o,grad:a}}}n(5540);let St=null,At=null;function Et(){return t=this,e=void 0,r=function*(){if(!navigator.gpu)return console.error("WebGPU is not supported in this browser"),null;if(!St&&(St=yield navigator.gpu.requestAdapter({powerPreference:"high-performance"}),null==St))return null;let t=!1;if(At&&(At.lost.then((()=>{t=!0})),yield new Promise((t=>setTimeout(t,10)))),!At||t){const t=1e9,e=St.limits,n=e.maxBufferSize,r=e.maxStorageBufferBindingSize;try{return At=yield St.requestDevice({requiredLimits:{maxBufferSize:Math.min(n,t),maxStorageBufferBindingSize:Math.min(r,t)}}),At}catch(t){return console.error("Failed to create device with required limits",t),At=yield St.requestDevice(),At}}return At},new((n=void 0)||(n=Promise))((function(s,i){function o(t){try{u(r.next(t))}catch(t){i(t)}}function a(t){try{u(r.throw(t))}catch(t){i(t)}}function u(t){var e;t.done?s(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(o,a)}u((r=r.apply(t,e||[])).next())}));var t,e,n,r}var It,Nt;(Nt=It||(It={})).EUCLIDEAN="EUCLIDEAN",Nt.MANHATTAN="MANHATTAN";const Rt={[It.EUCLIDEAN]:function(t){return`\n var sum = 0.0;\n for (var i = 0u; i < ${t}; i = i + 1u) {\n sum = sum + distances[i] * distances[i] * computeInfo.weights[i] * computeInfo.weights[i];\n }\n return sqrt(sum);\n `},[It.MANHATTAN]:function(t){return`\n var sum = 0.0;\n for (var i = 0u; i < ${t}; i = i + 1u) {\n sum = sum + abs(distances[i]) * computeInfo.weights[i];\n }\n return sum;\n `}};var kt,Ct;(Ct=kt||(kt={})).HAMMING="Hamming",Ct.EUCLIDEAN="Euclidean",Ct.VECTOR_COSINE="Vector Cosine",Ct.MANHATTAN="Manhattan",Ct.TANIMOTO="Tanimoto",Ct.LEVENSTEIN="Levenshtein",Ct.NEEDLEMAN_WUNSCH="Needlemann-Wunsch",Ct.MONOMER_CHEMICAL_DISTANCE="Monomer chemical distance",Ct.SOKAL="Sokal",Ct.COSINE="Cosine",Ct.ASYMMETRIC="Asymmetric",Ct.Difference="Difference",Ct.OneHot="One-Hot";const Tt={[kt.HAMMING]:function(t,e){return`\n let aLength: u32 = computeInfo.entrySizes[${e}][aIndex];\n let bLength: u32 = computeInfo.entrySizes[${e}][bIndex];\n let maxLength: u32 = max(aLength, bLength);\n let minLength: u32 = min(aLength, bLength);\n let sizeDiff: u32 = maxLength - minLength;\n \n let maxIntDistance = ceil(maxDistance * f32(maxLength)) - f32(sizeDiff);\n\n var diff: f32 = 0.0;\n for (var i = 0u; i < ${t}; i = i + 1u) {\n diff = diff + f32(a[i] != b[i]);\n if (diff > maxIntDistance) {\n return 1.0;\n }\n }\n diff += f32(sizeDiff);\n return diff / ${t};\n `},[kt.EUCLIDEAN]:function(t,e){return`\n var dist: f32 = 0.0;\n for (var i = 0u; i < ${t}; i = i + 1u) {\n dist = dist + f32(a[i] - b[i]) * f32(a[i] - b[i]);\n }\n return sqrt(dist);\n `},[kt.MANHATTAN]:function(t,e){return`\n var dist: f32 = 0.0;\n for (var i = 0u; i < ${t}; i = i + 1u) {\n dist = dist + abs(f32(a[i] - b[i]));\n }\n return dist;\n `},[kt.VECTOR_COSINE]:function(t,e){return`\n var dist: f32 = 0.0;\n var productSum: f32 = 0.0;\n var aSquareSum: f32 = 0.0;\n var bSquareSum: f32 = 0.0;\n for (var i = 0u; i < ${t}; i = i + 1u) {\n productSum = productSum + f32(a[i] * b[i]);\n aSquareSum = aSquareSum + f32(a[i] * a[i]);\n bSquareSum = bSquareSum + f32(b[i] * b[i]);\n }\n var sim = productSum / (sqrt(aSquareSum) * sqrt(bSquareSum));\n return (1.0 - sim) / 2.0;\n `},[kt.TANIMOTO]:function(t,e){return`\n var onBitsa: u32 = 0u;\n var onBitsb: u32 = 0u;\n for (var i = 0u; i < ${t}u; i = i + 1u) {\n onBitsa = onBitsa + countOneBits(a[i]);\n onBitsb = onBitsb + countOneBits(b[i]);\n }\n\n if (onBitsa == 0u && onBitsb == 0u) {\n return 0.0;\n }\n\n let totalOnBits = onBitsa + onBitsb;\n var commonBits: u32 = 0u;\n for (var i = 0u; i < ${t}u; i = i + 1u) {\n commonBits = commonBits + countOneBits(a[i] & b[i]);\n }\n\n return 1.0 - f32(commonBits) / f32(totalOnBits - commonBits);\n `},[kt.LEVENSTEIN]:function(t,e){return`\n let aLength: u32 = computeInfo.entrySizes[${e}][aIndex];\n let bLength: u32 = computeInfo.entrySizes[${e}][bIndex];\n let maxLength: u32 = max(aLength, bLength);\n let minLength: u32 = min(aLength, bLength);\n\n let maxIntDistance = ceil(maxDistance * f32(maxLength));\n\n // we will store two arrays as matrix and swap the working indices per pass.\n // this way we can reduce memory usage per computation to just O(aLength)\n // the grid will have aLength + 1 columns and bLength + 1 rows\n // this will be guaranteed by iteration, but the array sizes must be known at compile time, so we will use a fixed size of maxArraySize\n var dynamicPassMat: array<array<f32, ${t+1}u>, 2>; // initialize to 0\n \n var prevIndex: u32 = 0;\n var curIndex: u32 = 1; // we will swap these indices per pass\n\n // initialize the first row\n for (var i = 0u; i <= aLength; i = i + 1u) {\n dynamicPassMat[prevIndex][i] = f32(i);\n }\n\n // iterate over the rows\n for (var i = 1u; i <= bLength; i = i + 1u) {\n dynamicPassMat[curIndex][0] = f32(i);\n var minEntry: f32 = f32(maxLength);\n let prevRow = &dynamicPassMat[prevIndex];\n let curRow = &dynamicPassMat[curIndex];\n let bMon = u32(b[i - 1]);\n for (var j = 1u; j <= aLength; j = j + 1u) {\n var cost: f32 = f32(a[j - 1] != bMon);\n var res: f32 = min(\n min(\n (*prevRow)[j] + 1.0, // deletion\n (*curRow)[j - 1] + 1.0, // insertion\n ),\n (*prevRow)[j - 1] + cost // substitution\n );\n (*curRow)[j] = res;\n if (res < minEntry) {\n minEntry = res;\n }\n }\n // swap the indices\n let temp: u32 = prevIndex;\n prevIndex = curIndex;\n curIndex = temp;\n if (minEntry > maxIntDistance) {\n return 1.0;\n }\n }\n\n return dynamicPassMat[prevIndex][aLength] / f32(maxLength);\n `},[kt.NEEDLEMAN_WUNSCH]:function(t,e){return`\n let aLength: u32 = computeInfo.entrySizes[${e}][aIndex];\n let bLength: u32 = computeInfo.entrySizes[${e}][bIndex];\n let maxLength: u32 = max(aLength, bLength);\n let minLength: u32 = min(aLength, bLength);\n \n let maxIntDistance = ceil(maxDistance * f32(maxLength));\n // we will store two arrays as matrix and swap the working indices per pass.\n // this way we can reduce memory usage per computation to just O(aLength)\n // the grid will have aLength + 1 columns and bLength + 1 rows\n // this will be guaranteed by iteration, but the array sizes must be known at compile time, so we will use a fixed size of maxArraySize\n var dynamicPassMat: array<array<f32, ${t+1}u>, 2>; // initialize to 0\n \n // we need to keep track of which operation led to the current cell\n // i.e. whether we came from the left, top or diagonal to assign gap open/gap extend penalty\n var verticalGaps: array<u32, ${t+1}u>;\n var horizontalGaps: array<u32, ${t+1}u>;\n\n let gapOpenPenalty: f32 = suppInfo.gapOpenPenalty${e};\n let gapExtensionPenalty: f32 = suppInfo.gapExtensionPenalty${e};\n var prevIndex: u32 = 0;\n var curIndex: u32 = 1; // we will swap these indices per pass\n // initialize the first row\n for (var i = 0u; i <= aLength; i = i + 1u) {\n dynamicPassMat[prevIndex][i] = gapExtensionPenalty + f32(i - 1) * gapExtensionPenalty; // accounting for the fact that left and right gaps are less costly\n dynamicPassMat[curIndex][i] = 0.0;\n }\n dynamicPassMat[0][0] = 0.0;\n\n let simMatrix = &suppInfo.similarityMatrix${e}; // using pointers make things faster\n // iterate over the rows\n for (var i = 1u; i <= bLength; i = i + 1u) {\n let prevRow = &dynamicPassMat[prevIndex];\n let curRow = &dynamicPassMat[curIndex];\n (*curRow)[0] = gapExtensionPenalty + f32(i - 1) * gapExtensionPenalty;\n var minEntry: f32 = f32(maxLength);\n let monB = u32(b[i - 1]);\n for (var j = 1u; j <= aLength; j = j + 1u) {\n let monA = u32(a[j - 1]);\n \n let cost: f32 = (*prevRow)[j - 1] + 1f - (*simMatrix)[monA][monB];\n var top = (*prevRow)[j]; // deletion\n if (verticalGaps[j] > 0 || i == 1 || i == bLength) {\n top = top + gapExtensionPenalty;\n } else {\n top = top + gapOpenPenalty;\n }\n var left = (*curRow)[j - 1]; // insertion\n if (horizontalGaps[j - 1] > 0 || j == 1 || j == aLength) {\n left = left + gapExtensionPenalty;\n } else {\n left = left + gapOpenPenalty;\n }\n var res: f32 = min(\n min(\n top, // deletion\n left, // insertion\n ),\n cost // substitution\n );\n (*curRow)[j] = res;\n if (res < minEntry) {\n minEntry = res;\n }\n // update the horizontal and vertical gaps\n if (res == cost) {\n verticalGaps[j] = 0;\n horizontalGaps[j] = 0;\n } else if (res == left) {\n verticalGaps[j] = 0;\n horizontalGaps[j] = 1;\n } else {\n verticalGaps[j] = 1;\n horizontalGaps[j] = 0;\n }\n }\n // swap the indices\n let temp: u32 = prevIndex;\n prevIndex = curIndex;\n curIndex = temp;\n if (minEntry > maxIntDistance) {\n return 1.0;\n }\n }\n return dynamicPassMat[prevIndex][aLength] / f32(minLength);\n\n `},[kt.MONOMER_CHEMICAL_DISTANCE]:function(t,e){return`\n let aLength: u32 = computeInfo.entrySizes[${e}][aIndex];\n let bLength: u32 = computeInfo.entrySizes[${e}][bIndex];\n let maxLength: u32 = max(aLength, bLength);\n let minLength: u32 = min(aLength, bLength);\n let sizeDiff: u32 = maxLength - minLength;\n \n let maxIntDistance = ceil(maxDistance * f32(maxLength)) - f32(sizeDiff);\n\n let simMatrix = &(suppInfo.similarityMatrix${e}); // using pointers make things faster\n var diff: f32 = 0.0;\n for (var i = 0u; i < ${t}; i = i + 1u) {\n diff = diff + 1.0 - (*simMatrix)[u32(a[i])][u32(b[i])];\n if (diff > maxIntDistance) {\n return 1.0;\n }\n }\n diff += f32(sizeDiff);\n return diff / ${t};\n `},[kt.SOKAL]:function(t,e){return`\n var onBitsa: u32 = 0u;\n var onBitsb: u32 = 0u;\n for (var i = 0u; i < ${t}u; i = i + 1u) {\n onBitsa = onBitsa + countOneBits(a[i]);\n onBitsb = onBitsb + countOneBits(b[i]);\n }\n let total = onBitsa + onBitsb;\n if (total == 0u) {\n return 1.0;\n }\n var commonBits: u32 = 0u;\n for (var i = 0u; i < ${t}u; i = i + 1u) {\n commonBits = commonBits + countOneBits(a[i] & b[i]);\n }\n return 1.0 - f32(commonBits) / f32(total * 2 - commonBits * 3);\n `},[kt.COSINE]:function(t,e){return`\n var onBitsa: u32 = 0u;\n var onBitsb: u32 = 0u;\n for (var i = 0u; i < ${t}u; i = i + 1u) {\n onBitsa = onBitsa + countOneBits(a[i]);\n onBitsb = onBitsb + countOneBits(b[i]);\n }\n let total = onBitsa * onBitsb; // p.s. here total is taken by multiplying\n if (total == 0u) {\n return 1.0;\n }\n var commonBits: u32 = 0u;\n for (var i = 0u; i < ${t}u; i = i + 1u) {\n commonBits = commonBits + countOneBits(a[i] & b[i]);\n }\n return 1.0 - f32(commonBits) / sqrt(f32(total));\n `},[kt.ASYMMETRIC]:function(t,e){return`\n var onBitsa: u32 = 0u;\n var onBitsb: u32 = 0u;\n for (var i = 0u; i < ${t}u; i = i + 1u) {\n onBitsa = onBitsa + countOneBits(a[i]);\n onBitsb = onBitsb + countOneBits(b[i]);\n }\n let min = min(onBitsa, onBitsb);\n if (min == 0u) {\n return 1.0;\n }\n var commonBits: u32 = 0u;\n for (var i = 0u; i < ${t}u; i = i + 1u) {\n commonBits = commonBits + countOneBits(a[i] & b[i]);\n }\n return 1.0 - f32(commonBits) / f32(min);\n `},[kt.Difference]:function(t,e){return`\n let range = suppInfo.range${e};\n return f32(abs(f32(a[0]) - f32(b[0])) / range);\n `},[kt.OneHot]:function(t,e){return`\n let aLength: u32 = computeInfo.entrySizes[${e}][aIndex];\n let bLength: u32 = computeInfo.entrySizes[${e}][bIndex];\n if (aLength != bLength) {\n return 1.0;\n }\n for (var i = 0u; i < aLength; i = i + 1u) {\n if(a[i] != b[i]) {\n return 1.0;\n }\n }\n return 0.0;\n `}},Pt={[kt.HAMMING]:t=>Math.ceil(t/30),[kt.EUCLIDEAN]:t=>Math.ceil(t/30),[kt.MANHATTAN]:t=>Math.ceil(t/30),[kt.TANIMOTO]:t=>Math.ceil(t/30),[kt.SOKAL]:t=>Math.ceil(t/30),[kt.COSINE]:t=>Math.ceil(t/30),[kt.ASYMMETRIC]:t=>Math.ceil(t/30),[kt.LEVENSTEIN]:t=>Math.ceil(t*t/60),[kt.NEEDLEMAN_WUNSCH]:t=>Math.ceil(t*t/60),[kt.MONOMER_CHEMICAL_DISTANCE]:t=>Math.ceil(t/25),[kt.Difference]:t=>1,[kt.OneHot]:t=>Math.ceil(t/40),[kt.VECTOR_COSINE]:t=>Math.ceil(t/30)},Dt={STRING:new Set([kt.HAMMING,kt.LEVENSTEIN,kt.NEEDLEMAN_WUNSCH,kt.MONOMER_CHEMICAL_DISTANCE,kt.OneHot]),UINT32ARRAY:new Set([kt.HAMMING,kt.EUCLIDEAN,kt.MANHATTAN,kt.MONOMER_CHEMICAL_DISTANCE,kt.LEVENSTEIN,kt.NEEDLEMAN_WUNSCH,kt.TANIMOTO,kt.COSINE,kt.VECTOR_COSINE,kt.SOKAL,kt.ASYMMETRIC,kt.OneHot,kt.Difference]),INT32ARRAY:new Set([kt.EUCLIDEAN,kt.MANHATTAN,kt.OneHot,kt.Difference,kt.VECTOR_COSINE]),FLOAT32ARRAY:new Set([kt.EUCLIDEAN,kt.MANHATTAN,kt.Difference,kt.VECTOR_COSINE]),NUMBER:new Set([kt.EUCLIDEAN,kt.MANHATTAN,kt.Difference]),BITARRAY:new Set([kt.TANIMOTO,kt.COSINE,kt.SOKAL,kt.ASYMMETRIC])};var Ut,Ot,zt;function Bt(t,e=15,n,r,s,i){return o=this,a=void 0,l=function*(){if(i.length!==t.length||i.length!==n.length||i.length!==s.length)throw new Error("Options, weigths and distance functions must be provided for each column");if(t.some((e=>e.length!==t[0].length)))throw new Error("All entry lists must be the same length");const o=Object.values(kt);if(n.some((t=>!o.includes(t))))throw new Error("Invalid distance metrics provided: "+n.join(", "));if(!Object.values(It).includes(r))throw new Error("Invalid aggregation function provided: "+r);const a=t.length;if(0===a)throw new Error("No columns provided. Please provide at least one column of data.");const u=yield Et();if(!u)return null;let l=!1;u.lost.then((()=>{l=!0}));const h=t[0].length,c=t.map(((t,e)=>function(t,e=kt.HAMMING,n,r={gapOpenPenalty:1,gapExtensionPenalty:.6}){var s,i;let o=null;const a=t.some((t=>"string"==typeof t))?(o="STRING",t.map((t=>new Uint32Array(t.split("").map((t=>t.charCodeAt(0))))))):t.some((t=>"number"==typeof t))?(o="NUMBER",t.map((t=>new Float32Array([t])))):"object"==typeof t[0]&&t.some((t=>"_data"in t&&"_length"in t))?(o="BITARRAY",t.map((t=>t._data))):t.some((t=>t instanceof Float32Array))?(o="FLOAT32ARRAY",t):t.some((t=>t instanceof Uint32Array))?(o="UINT32ARRAY",t):t.some((t=>t instanceof Int32Array))?(o="INT32ARRAY",t):void 0;if(!a||!o)throw new Error("Invalid entry type, could not determine entry type from input list");const u=a[0]instanceof Int32Array?"INT32ARRAY":a[0]instanceof Float32Array?"FLOAT32ARRAY":"UINT32ARRAY",l=new Uint32Array(a.map((t=>t.length)));if(!Dt[o]||!Dt[o].has(e))throw new Error(`Distance metric '${e}' not supported for entry type '${o}'`);const h=l.reduce(((t,e)=>Math.max(t,e)),0),c=Pt[e](h),f="INT32ARRAY"===u?Int32Array:"FLOAT32ARRAY"===u?Float32Array:Uint32Array,m=new f(a.length*h);a.forEach(((t,e)=>{m.set(t,e*h)}));let g="",d=0,p="FLOAT32ARRAY",w=null;if(e===kt.NEEDLEMAN_WUNSCH||e===kt.MONOMER_CHEMICAL_DISTANCE){let t=r.scoringMatrix&&r.alphabetIndexes?Object.keys(r.alphabetIndexes).reduce(((t,e)=>Math.max(t,e.charCodeAt(0))),0):-1;if(!r.alphabetIndexes||!r.scoringMatrix){for(let e=0;e<m.length;e++)m[e]>t&&(t=m[e]);r.scoringMatrix=new Array(t+1).fill(null).map((()=>new Array(t+1).fill(0))),r.alphabetIndexes={};for(let t=0;t<r.scoringMatrix.length;t++)r.scoringMatrix[t][t]=1,r.alphabetIndexes[String.fromCharCode(t)]=t}const e=(t+1)*(t+1),o=new Array(t+1).fill(null).map((()=>new Float32Array(t+1)));for(let e=0;e<t+1;e++)o[e][e]=1;const a=r.alphabetIndexes;for(const t of Object.keys(a))for(const e of Object.keys(a))t!==e&&(o[t.charCodeAt(0)][e.charCodeAt(0)]=r.scoringMatrix[a[t]][a[e]]);d=2+e,p="FLOAT32ARRAY",w=new Float32Array(d),w[0]=null!==(s=r.gapOpenPenalty)&&void 0!==s?s:1,w[1]=null!==(i=r.gapExtensionPenalty)&&void 0!==i?i:.6;let u=2;for(let t=0;t<o.length;t++)w.set(o[t],u),u+=o[t].length;g=`\n gapOpenPenalty${n}: f32,\n gapExtensionPenalty${n}: f32,\n similarityMatrix${n}: array<array<f32, ${t+1}>, ${t+1}>`}else if(e===kt.Difference){if(!r.range||"number"!=typeof r.range||r.range<=0){const t=m.reduce(((t,e)=>Math.min(t,e)),m[0]),e=m.reduce(((t,e)=>Math.max(t,e)),m[0]);r.range=e-t}r.range<=0&&(r.range=1),d=1,p="FLOAT32ARRAY",w=new Float32Array([r.range]),g=`\n range${n}: f32`}const y=m instanceof Int32Array?"i32":m instanceof Float32Array?"f32":"u32",b=`data${n}: array<array<${y}, ${h}>, ${a.length}>`;return{flatSourceArray:m,sourceArraySize:m.length,maxEntryLen:h,arraySizes:l,complexity:c,suppInfoBuffer:w,suppInfoSize:d,suppInfoType:p,suppInfoStructWgsl:g,entryType:o,dataTypeWGSL:y,dataStructWgsl:b,EncodedArrayConstructor:f}}(t,n[e],e,i[e])));1===a&&(r=It.MANHATTAN);let f=c.map((t=>t.suppInfoStructWgsl)).filter((t=>!!t&&""!=t)).join(",\n"),m=!1;f&&""!=f.trim()||(m=!0,f="\ndummy: f32\n");const g=c.reduce(((t,e)=>t+e.complexity),0),d=c.map((t=>t.dataStructWgsl)).filter((t=>!!t&&""!=t)).join(",\n"),p=new Uint32Array(a*h);c.forEach(((t,e)=>{p.set(t.arraySizes,e*h)}));const w=1e4,y=Math.ceil(1e4/g),b=Math.ceil(Math.sqrt(Math.ceil(100))),x=10*b,M=new Array(h).fill(null).map((()=>new Uint32Array(e))),v=new Array(h).fill(null).map((()=>new Float32Array(e))),S=u.createShaderModule({label:"KNN compute shader",code:`\n // this struct will contain all the info about the computation, startAtEndAt will contain the start and end of the knnDistances and knnIndexes.\n // array of sizes for each entries, and the main data as arrays of arrays called data0, data1 and so on. good thing is that because the first entry is vec4<u32>,\n // there will be no paddings, so no need to worry about padding data. also, arrays and matrices get stucked together, so no padding there as well.\n // what we need to worry about is the padding of overall struct. the size of overall struct will be factor of 16 bytes, so keep that in mind.\n struct ComputeInfo {\n // the x coordinate will contain the start index of the knnDistances and knnIndexes, while y will contain the end index \n // the z coordinate will contain the start of the pair comparisons, while w will contain the end of the pair comparisons\n // just keep in mind that this vec4 will be in first 16 bytes of corresponding buffer.\n startAtEndAt: vec4<u32>,\n // the ACTUALLY sizes of each entry\n entrySizes: array<array<u32, ${h}>, ${a}>,\n // the weights for each entry\n weights: array<f32, ${a}>,\n // the data for each entry\n ${d} // an example of the dataWgsl would be:\n //data0: array<array<u32,20>,100>,\n //data1: array<array<u32,20>,100>\n };\n\n struct SuppInfo {\n // struct containing all the supplementary info, like scoring matrix, alphabet indexes, range, etc.\n ${f}\n };\n \n @group(0) @binding(0) var<storage, read_write> knnIndexes: array<array<u32, ${e}>, 10000>;\n @group(0) @binding(1) var<storage, read_write> knnDistances: array<array<f32, ${e}>, 10000>; // each time just compute for a subset of the list\n @group(0) @binding(2) var<storage, read_write> computeInfo: ComputeInfo;\n @group(0) @binding(3) var<storage, read_write> suppInfo: SuppInfo;\n \n @compute @workgroup_size(10, 10) fn calcKNN(\n @builtin(global_invocation_id) id: vec3<u32>\n ) {\n ${m?"let otherDummy = suppInfo.dummy * 2;":""} // just to make sure that the suppInfo is not optimized out\n let col = id.x; //* 10 + localId.x;\n let row = id.y; //* 10 + localId.y;\n let graphIndex = row * ${x} + col;\n let index = graphIndex + computeInfo.startAtEndAt.x; // add the starting index of the knnDistances and knnIndexes\n \n if (index >= min(${h}u, computeInfo.startAtEndAt.y)) {return;}\n \n let pairComparisonStartAt = computeInfo.startAtEndAt.z;\n let pairComparisonEndAt = min(computeInfo.startAtEndAt.w, ${h}u);\n \n \n // only clear the knnDistances and knnIndexes if we are at the start of the pair comparison\n if (pairComparisonStartAt == 0u) {\n for (var i = 0u; i < ${e}; i = i + 1u) {\n knnDistances[graphIndex][i] = 99999.0;\n knnIndexes[graphIndex][i] = 0u;\n }\n }\n \n for (var i: u32 = pairComparisonStartAt; i < pairComparisonEndAt; i = i + 1u) {\n if (i == index) {continue;}\n let dist = combinedDistance(index, i);\n insertKnn(graphIndex, dist, i);\n }\n \n }\n // this will generate the distance script for each distance metric and then combine them into one\n ${_t(n,c.map((t=>t.maxEntryLen)),e,r)}\n \n fn insertKnn(knnIndex: u32, dist: f32, index: u32) {\n // small optimization, if the distance is larger than the last element in the knnDistances, we can skip\n if (dist >= knnDistances[knnIndex][${e} - 1u]) {return;}\n for (var i = 0u; i < ${e}; i = i + 1u) {\n if (dist < knnDistances[knnIndex][i]) {\n for (var j = ${e} - 1u; j > i; j = j - 1u) {\n knnDistances[knnIndex][j] = knnDistances[knnIndex][j - 1u];\n knnIndexes[knnIndex][j] = knnIndexes[knnIndex][j - 1u];\n }\n knnDistances[knnIndex][i] = dist;\n knnIndexes[knnIndex][i] = index;\n return;\n }\n }\n }\n `}),A=u.createComputePipeline({label:"hamming compute pipeline",layout:"auto",compute:{module:S,entryPoint:"calcKNN"}}),E=4+a*h+a+c.reduce(((t,e)=>t+e.sourceArraySize),0),I=c.reduce(((t,e)=>t+e.suppInfoSize),0),N=E*Uint32Array.BYTES_PER_ELEMENT;let R=N;const k=15&N;0!==k&&(R+=16-k);const C=u.createBuffer({label:"compute info buffer",size:R,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST,mappedAtCreation:!0}),T=C.getMappedRange();let P=0,D=w,U=0,O=y,z=0;new Uint32Array(T,z,4).set([P,D,U,O]),z+=4*Uint32Array.BYTES_PER_ELEMENT,new Uint32Array(T,z,p.length).set(p),z+=p.byteLength,new Float32Array(T,z,a).set(s),z+=a*Float32Array.BYTES_PER_ELEMENT;for(const t of c){const e=t.EncodedArrayConstructor,n=t.sourceArraySize;new e(T,z,n).set(t.flatSourceArray),z+=n*e.BYTES_PER_ELEMENT}C.unmap();const B=I*Uint32Array.BYTES_PER_ELEMENT;let _=B;const L=15&B;0!==L&&(_+=16-L),_=Math.max(_,16);const $=u.createBuffer({label:"supp info buffer",size:_,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST,mappedAtCreation:!0}),G=$.getMappedRange();let F=0;for(const t of c)t.suppInfoBuffer&&t.suppInfoBuffer.byteLength>0&&t.suppInfoSize>0&&(new("UINT32ARRAY"===t.suppInfoType?Uint32Array:Float32Array)(G,F,t.suppInfoBuffer.length).set(t.suppInfoBuffer),F+=t.suppInfoBuffer.byteLength);0===F&&new Uint32Array(G,0,4).set([1,1,1,1]),$.unmap();const j=e*Uint32Array.BYTES_PER_ELEMENT*w,q=u.createBuffer({label:"buffer distances",size:j,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC}),V=u.createBuffer({label:"buffer indexes",size:j,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC}),Y=e*Uint32Array.BYTES_PER_ELEMENT,K=u.createBindGroup({label:"bindGroup for knn buffer",layout:A.getBindGroupLayout(0),entries:[{binding:0,resource:{buffer:V}},{binding:1,resource:{buffer:q}},{binding:2,resource:{buffer:C}},{binding:3,resource:{buffer:$}}]}),W=u.createBuffer({label:"result buffer distances",size:q.size,usage:GPUBufferUsage.MAP_READ|GPUBufferUsage.COPY_DST}),H=u.createBuffer({label:"result buffer indexes",size:V.size,usage:GPUBufferUsage.MAP_READ|GPUBufferUsage.COPY_DST});for(let t=0;t<Math.ceil(h/w);t++){P=t*w,D=Math.min((t+1)*w,h);const n=Math.ceil(h/y);for(let t=0;t<n;t++){U=t*y,O=Math.min((t+1)*y,h),u.queue.writeBuffer(C,0,new Uint32Array([P,D,U,O]));const r=u.createCommandEncoder({label:"distance encoder"}),s=r.beginComputePass({label:"distance compute pass"});if(s.setPipeline(A),s.setBindGroup(0,K),s.dispatchWorkgroups(b,Math.ceil(w/b/100)),s.end(),t===n-1){r.copyBufferToBuffer(q,0,W,0,W.size),r.copyBufferToBuffer(V,0,H,0,H.size);const t=r.finish();u.queue.submit([t]),yield u.queue.onSubmittedWorkDone(),yield W.mapAsync(GPUMapMode.READ),yield H.mapAsync(GPUMapMode.READ);const n=H.getMappedRange(),s=W.getMappedRange();for(let t=0;t<D-P;t++){const r=new Uint32Array(n,t*Y,e),i=new Float32Array(s,t*Y,e);M[P+t].set(r),v[P+t].set(i)}H.unmap(),W.unmap()}else{const t=r.finish();u.queue.submit([t]),yield u.queue.onSubmittedWorkDone()}if(l)return null}}return q.destroy(),V.destroy(),C.destroy(),$.destroy(),W.destroy(),H.destroy(),l?null:{knnIndexes:M,knnDistances:v}},new((u=void 0)||(u=Promise))((function(t,e){function n(t){try{s(l.next(t))}catch(t){e(t)}}function r(t){try{s(l.throw(t))}catch(t){e(t)}}function s(e){var s;e.done?t(e.value):(s=e.value,s instanceof u?s:new u((function(t){t(s)}))).then(n,r)}s((l=l.apply(o,a||[])).next())}));var o,a,u,l}function _t(t,e,n,r){return t.map(((t,r)=>`\n fn distanceScript${r}(aIndex: u32, bIndex: u32) -> f32 {\n let a = computeInfo.data${r}[aIndex];\n let b = computeInfo.data${r}[bIndex];\n let maxDistance: f32 = knnDistances[aIndex - computeInfo.startAtEndAt.x][${n} - 1u];\n ${Tt[t](e[r],r)}\n }\n `)).join("\n")+"\n"+`\n fn combinedDistance(aIndex: u32, bIndex: u32) -> f32 {\n var distances: array<f32, ${t.length}>;\n ${t.map(((t,e)=>`distances[${e}] = distanceScript${e}(aIndex, bIndex);`)).join("\n")}\n ${Rt[r](t.length)}\n }\n \n `}!function(t){t.ADD="ADD",t.SUB="SUB",t.MULT="MULT"}(Ut||(Ut={})),function(t){t.SQUARE="SQUARE",t.INVERSE="INVERSE",t.TRANSPOSE="TRANSPOSE",t.NORM="NORM",t.COLUMN_NORM="COLUMN_NORM"}(Ot||(Ot={})),function(t){t.SCALARMULT="SCALARMULT",t.SCALARADD="SCALARADD",t.SCALARPOW="SCALARPOW"}(zt||(zt={}));function Lt(t,e,n,r,s,i,o,a,u){return l=this,h=void 0,f=function*(){const l=Math.ceil(o/100),h=Math.ceil(Math.sqrt(l)),c=10*h,f=n[n.length-1],m=i[i.length-1],g=a[a.length-1],d=`\n struct SparseKNNStorage1 {\n indexes:array<i32, ${f}>,\n knnDistances: array<f32, ${f}>,\n offsets: array<u32, ${o+1}>\n }\n\n struct SparseKNNStorage2 {\n indexes:array<i32, ${m}>,\n knnDistances: array<f32, ${m}>,\n offsets: array<u32, ${o+1}>\n }\n\n struct ResKnn {\n knnIndexes: array<i32, ${g}>,\n knnDistances: array<f32, ${g}>\n }\n\n @group(0) @binding(0) var<storage, read_write> source1: SparseKNNStorage1;\n @group(0) @binding(1) var<storage, read_write> source2: SparseKNNStorage2;\n @group(0) @binding(2) var<storage, read_write> result: ResKnn;\n @group(0) @binding(3) var<storage, read_write> unionMatrixOffsets: array<u32, ${o+1}>;\n @compute @workgroup_size(10, 10) fn pairwiseOp(\n @builtin(global_invocation_id) id: vec3<u32>,\n ) {\n\n let col: u32 = id.x;\n let row: u32 = id.y;\n let workingIndex: u32 = row * ${c} + col;\n\n if (workingIndex >= ${o}) {\n return;\n }\n var curUnionOffset: u32 = unionMatrixOffsets[workingIndex]; // offset at which to start writing in union matrix\n\n let start1 = source1.offsets[workingIndex];\n let end1 = source1.offsets[workingIndex + 1];\n let start2 = source2.offsets[workingIndex];\n let end2 = source2.offsets[workingIndex + 1];\n // TODO: sort a copy of these arrays, will make operation faster\n for (var i: u32 = start1; i < end1; i++) {\n let val1: f32 = source1.knnDistances[i];\n var val2: f32 = 0.0;\n let curIndex: i32 = source1.indexes[i];\n for (var j: u32 = start2; j < end2; j++) {\n if (source2.indexes[j] == curIndex) {\n val2 = source2.knnDistances[j];\n break;\n }\n }\n \n result.knnIndexes[curUnionOffset] = curIndex;\n result.knnDistances[curUnionOffset] = val1 ${u} val2;\n curUnionOffset += 1;\n }\n\n if (curUnionOffset >= unionMatrixOffsets[workingIndex + 1]) {\n // small optimization applicable only to our case, but saves a good amount of time\n return;\n }\n\n // do the same for source2 but skip the indexes where they are already present in source1\n for (var i: u32 = start2; i < end2; i++) {\n let val1: f32 = source2.knnDistances[i];\n let curIndex: i32 = source2.indexes[i];\n var found = false;\n for (var j: u32 = start1; j < end1; j++) {\n if (source1.indexes[j] == curIndex) {\n found = true;\n break;\n }\n }\n if (!found) {\n result.knnIndexes[curUnionOffset] = curIndex;\n result.knnDistances[curUnionOffset] = val1 ${u} 0.0;\n curUnionOffset += 1;\n }\n }\n }\n`;let p=4*(2*f+o+1);const w=15&p;0!==w&&(p+=16-w);let y=4*(2*m+o+1);const b=15&y;0!==b&&(y+=16-b);let x=2*g*4;const M=15&x;0!==M&&(x+=16-M);const v=yield Et();if(null==v)return;const S=v.createShaderModule({label:"pairwiseOpShader",code:d}),A=v.createComputePipeline({label:"pairwiseOpPipeline",layout:"auto",compute:{module:S,entryPoint:"pairwiseOp"}}),E=v.createBuffer({label:"source 1 buffer",size:p,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST,mappedAtCreation:!0}),I=E.getMappedRange();new Int32Array(I,0,f).set(t),new Float32Array(I,4*f,f).set(e),new Uint32Array(I,8*f,o+1).set(n),E.unmap();const N=v.createBuffer({label:"source 2 buffer",size:y,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST,mappedAtCreation:!0}),R=N.getMappedRange();new Int32Array(R,0,m).set(r),new Float32Array(R,4*m,m).set(s),new Uint32Array(R,8*m,o+1).set(i),N.unmap();const k=v.createBuffer({label:"res buffer",size:x,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST}),C=v.createBuffer({label:"union matrix offsets buffer",size:4*(o+1),usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST,mappedAtCreation:!0}),T=C.getMappedRange();new Uint32Array(T).set(a),C.unmap();const P=v.createBindGroup({label:"bindGroup for pairwise ops",layout:A.getBindGroupLayout(0),entries:[{binding:0,resource:{buffer:E}},{binding:1,resource:{buffer:N}},{binding:2,resource:{buffer:k}},{binding:3,resource:{buffer:C}}]}),D=v.createCommandEncoder({label:"matrix ops encoder"}),U=D.beginComputePass({label:"matrix ops compute pass"});U.setPipeline(A),U.setBindGroup(0,P),U.dispatchWorkgroups(h,h),U.end();const O=v.createBuffer({label:"result buffer",size:x,usage:GPUBufferUsage.MAP_READ|GPUBufferUsage.COPY_DST});D.copyBufferToBuffer(k,0,O,0,O.size);const z=D.finish();v.queue.submit([z]),yield v.queue.onSubmittedWorkDone(),yield O.mapAsync(GPUMapMode.READ);const B=O.getMappedRange(),_=new Int32Array(g),L=new Float32Array(g);return _.set(new Int32Array(B,0,g)),L.set(new Float32Array(B,4*g,g)),O.unmap(),O.destroy(),E.destroy(),N.destroy(),k.destroy(),C.destroy(),{resultKnnIndexes:_,resultKnnDistances:L}},new((c=void 0)||(c=Promise))((function(t,e){function n(t){try{s(f.next(t))}catch(t){e(t)}}function r(t){try{s(f.throw(t))}catch(t){e(t)}}function s(e){var s;e.done?t(e.value):(s=e.value,s instanceof c?s:new c((function(t){t(s)}))).then(n,r)}s((f=f.apply(l,h||[])).next())}));var l,h,c,f}var $t=n(788);function Gt(t,e,n,r){let s=0;const i=n(e);for(let e=0;e<t.x.length;e++)s+=Math.pow(t.y[e]-i(t.x[e]),2)/r[e];return s}function Ft(t,e,n,r,s,i,o){let a=n,u=at.eye(e.length,e.length,a);const l=s(e);let h=new Float64Array(t.x.length);for(let e=0;e<t.x.length;e++)h[e]=l(t.x[e]);let c=function(t,e,n,r,s,i){const o=n.length,a=t.x.length;let u=at.zeros(o,a),l=0;for(let h=0;h<o;h++){if(0===r[h])continue;let o=r[h],c=n.slice();c[h]+=o;let f=s(c);if(i){c=n.slice(),c[h]-=o,o*=2;let e=s(c);for(let n=0;n<a;n++)u.set(l,n,(e(t.x[n])-f(t.x[n]))/o)}else for(let n=0;n<a;n++)u.set(l,n,(e[n]-f(t.x[n]))/o);l++}return u}(t,h,e,r,s,i),f=function(t,e){const n=t.x.length;let r=new at(n,1);for(let s=0;s<n;s++)r.set(s,0,t.y[s]-e[s]);return r}(t,h),m=ut(u.add(c.mmul(c.transpose().scale("row",{scale:o})))),g=c.mmul(f.scale("row",{scale:o}));return{perturbations:m.mmul(g),jacobianWeightResidualError:g}}function jt(t,e){const n=function(t,e){const n=(e-0)/299;return Array.from({length:300},((t,e)=>0+n*e))}(0,3*t).map((t=>t<e?1:t)),r=new Array(n.length).fill(0).map(((r,s)=>n[s]>=e?Math.exp(-(n[s]-e)/t):r)),s={x:n,y:r},{parameterValues:i}=function(t,e,n){let{checkTimeout:r,minValues:s,maxValues:i,parameters:o,weightSquare:a,damping:u,dampingStepUp:l,dampingStepDown:h,maxIterations:c,errorTolerance:f,centralDifference:m,gradientDifference:g,improvementThreshold:d}=function(t,e,n){let{timeout:r,minValues:s,maxValues:i,initialValues:o,weights:a=1,damping:u=.01,dampingStepUp:l=11,dampingStepDown:h=9,maxIterations:c=100,errorTolerance:f=1e-7,centralDifference:m=!1,gradientDifference:g=.1,improvementThreshold:d=.001}=n;if(u<=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(!(0,$t.isAnyArray)(t.x)||t.x.length<2||!(0,$t.isAnyArray)(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");if(!(o&&o.length>0))throw new Error("The initialValues option is mandatory and must be an array");let p,w,y=o,b=t.y.length,x=y.length;if(i=i||new Array(x).fill(Number.MAX_SAFE_INTEGER),s=s||new Array(x).fill(Number.MIN_SAFE_INTEGER),i.length!==s.length)throw new Error("minValues and maxValues must be the same size");if("number"==typeof g)g=new Array(y.length).fill(g);else{if(!(0,$t.isAnyArray)(g))throw new Error("gradientDifference should be a number or array with length equal to the number of parameters");g.length!==x&&(g=new Array(x).fill(g[0]))}if("number"==typeof a){let t=1/a**2;p=()=>t}else{if(!(0,$t.isAnyArray)(a))throw new Error("weights should be a number or array with length equal to the number of data points");if(a.length<t.x.length){let t=1/a[0]**2;p=()=>t}else p=t=>1/a[t]**2}if(void 0!==r){if("number"!=typeof r)throw new Error("timeout should be a number");let t=Date.now()+1e3*r;w=()=>Date.now()>t}else w=()=>!1;let M=new Array(t.x.length);for(let t=0;t<b;t++)M[t]=p(t);return{checkTimeout:w,minValues:s,maxValues:i,parameters:y,weightSquare:M,damping:u,dampingStepUp:l,dampingStepDown:h,maxIterations:c,errorTolerance:f,centralDifference:m,gradientDifference:g,improvementThreshold:d}}(t,0,n),p=Gt(t,o,e,a),w=p,y=o.slice(),b=p<=f,x=0;for(;x<c&&!b;x++){let c=p,{perturbations:x,jacobianWeightResidualError:M}=Ft(t,o,u,g,e,m,a);for(let t=0;t<o.length;t++)o[t]=Math.min(Math.max(s[t],o[t]-x.get(t,0)),i[t]);if(p=Gt(t,o,e,a),isNaN(p))break;if(p<w-f&&(w=p,y=o.slice()),u=(c-p)/x.transpose().mmul(x.mul(u).add(M)).get(0,0)>d?Math.max(u/h,1e-7):Math.min(u*l,1e7),r())throw new Error(`The execution time is over to ${n.timeout} seconds`);b=p<=f}return{parameterValues:y,parameterError:w,iterations:x}}(s,(([t,e])=>n=>1/(1+t*Math.pow(n,2*e))),{damping:1.5,initialValues:[.5,.5],gradientDifference:.1,maxIterations:100,errorTolerance:.01}),[o,a]=i;return{a:o,b:a}}function qt(t){const e=new Uint32Array(t.length+1);let n=0;for(let r=0;r<t.length;r++)e[r]=n,n+=t[r];return e[t.length]=n,e}function Vt(t,e){for(let n=0;n<t.length;n++)t[n]=t[n]*e}function Yt(t,e,n=1){return r=this,s=void 0,o=function*(){const r=new Int32Array(t.length*t[0].length),s=new Float32Array(e.length*e[0].length);for(let n=0;n<t.length;n++)r.set(t[n],n*t[n].length),s.set(e[n],n*e[n].length);const i=yield function(t){return e=this,n=void 0,s=function*(){const e=t.length,n=Math.ceil(e/100),r=Math.ceil(Math.sqrt(n)),s=t[0].length,i=function(t,e,n,r=15){return`\n\n @group(0) @binding(0) var<storage, read_write> knnIndexes: array<array<i32, ${r}>, ${n}>;\n @group(0) @binding(1) var<storage, read_write> resTransposedSizes: array<atomic<u32>, ${n}>;\n // this array will store the union of knn and its transpose sizes per index. should be initialized to knnSize each.\n @group(0) @binding(2) var<storage, read_write> resUnionMatrixSizes: array<atomic<u32>, ${n}>;\n\n @compute @workgroup_size(${t}, ${t}) fn countTransposedCols(\n @builtin(global_invocation_id) id: vec3<u32>,\n ) {\n let col: u32 = id.x;\n let row: u32 = id.y;\n let workingIndex: u32 = row * ${e} + col;\n\n if (workingIndex >= ${n}) {\n return;\n }\n \n for (var i = 0u; i < ${r}u; i++) {\n let otherIndex: i32 = knnIndexes[workingIndex][i];\n if (otherIndex != -1 && otherIndex < ${n}) {\n atomicAdd(&resTransposedSizes[otherIndex], 1u);\n atomicAdd(&resUnionMatrixSizes[otherIndex], 1u);\n let otherIndexes = &knnIndexes[otherIndex];\n for(var j = 0u; j < ${r}; j++) {\n if ((*otherIndexes)[j] == i32(workingIndex)) {\n atomicSub(&resUnionMatrixSizes[workingIndex], 1u);\n // if same is found in other array, decrement by one;\n break;\n }\n }\n }\n\n }\n }\n `}(10,10*r,e,s),o=e*t[0].length,a=yield Et();if(null==a)return;const u=a.createShaderModule({label:"transposedSizesCalulation",code:i}),l=a.createComputePipeline({label:"transposedSizesPipeline",layout:"auto",compute:{module:u,entryPoint:"countTransposedCols"}}),h=a.createBuffer({label:"indexes buffer",size:4*o,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST,mappedAtCreation:!0}),c=h.getMappedRange(),f=new Int32Array(c);for(let e=0;e<t.length;e++)f.set(t[e],e*t[e].length);h.unmap();const m=a.createBuffer({label:"transpose sizes buffer",size:4*t.length,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST}),g=a.createBuffer({label:"union sizes buffer",size:4*t.length,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST,mappedAtCreation:!0}),d=g.getMappedRange();new Uint32Array(d).fill(s),g.unmap();const p=a.createBindGroup({label:"bindGroup for count ops",layout:l.getBindGroupLayout(0),entries:[{binding:0,resource:{buffer:h}},{binding:1,resource:{buffer:m}},{binding:2,resource:{buffer:g}}]}),w=a.createCommandEncoder({label:"matrix ops encoder"}),y=w.beginComputePass({label:"matrix ops compute pass"});y.setPipeline(l),y.setBindGroup(0,p),y.dispatchWorkgroups(r,r),y.end();const b=a.createBuffer({label:"result transpose sizes",size:m.size,usage:GPUBufferUsage.MAP_READ|GPUBufferUsage.COPY_DST}),x=a.createBuffer({label:"result union sizes",size:g.size,usage:GPUBufferUsage.MAP_READ|GPUBufferUsage.COPY_DST});w.copyBufferToBuffer(m,0,b,0,b.size),w.copyBufferToBuffer(g,0,x,0,x.size);const M=w.finish();a.queue.submit([M]),yield a.queue.onSubmittedWorkDone(),yield b.mapAsync(GPUMapMode.READ),yield x.mapAsync(GPUMapMode.READ);const v=b.getMappedRange(),S=x.getMappedRange(),A=new Uint32Array(t.length);A.set(new Uint32Array(v,0,t.length)),b.unmap();const E=new Uint32Array(t.length);E.set(new Uint32Array(S,0,t.length)),x.unmap();const I=E.reduce(((t,e)=>t+e),0);return x.destroy(),b.destroy(),m.destroy(),h.destroy(),g.destroy(),{resultTransposeSizesArray:A,resultUnionSizesArray:E,unionMatrixSize:I}},new((r=void 0)||(r=Promise))((function(t,i){function o(t){try{u(s.next(t))}catch(t){i(t)}}function a(t){try{u(s.throw(t))}catch(t){i(t)}}function u(e){var n;e.done?t(e.value):(n=e.value,n instanceof r?n:new r((function(t){t(n)}))).then(o,a)}u((s=s.apply(e,n||[])).next())}));var e,n,r,s}(t),o=t.length,a=yield function(t,e,n,r=15){return s=this,i=void 0,a=function*(){const s=t.length,i=r*s,o=qt(n.resultTransposeSizesArray),a=new Uint32Array(o.length);a.set(o);const u=new Int32Array(i).fill(0),l=new Float32Array(i).fill(0);for(let n=0;n<s;n++)for(let s=0;s<r;s++){const r=t[n][s],i=a[r];u[i]=n,l[i]=e[n][s],a[r]+=1}return{transposeKNNIndexes:u,transposeKNNDistances:l,transposeOffsets:o}},new((o=void 0)||(o=Promise))((function(t,e){function n(t){try{u(a.next(t))}catch(t){e(t)}}function r(t){try{u(a.throw(t))}catch(t){e(t)}}function u(e){var s;e.done?t(e.value):(s=e.value,s instanceof o?s:new o((function(t){t(s)}))).then(n,r)}u((a=a.apply(s,i||[])).next())}));var s,i,o,a}(t,e,i,e[0].length),u=qt(new Uint32Array(o).fill(t[0].length)),l=qt(i.resultUnionSizesArray),h=yield Lt(r,s,u,a.transposeKNNIndexes,a.transposeKNNDistances,a.transposeOffsets,o,l,"*"),c=yield Lt(r,s,u,a.transposeKNNIndexes,a.transposeKNNDistances,a.transposeOffsets,o,l,"+"),f=yield Lt(c.resultKnnIndexes,c.resultKnnDistances,l,h.resultKnnIndexes,h.resultKnnDistances,l,o,l,"-");return 1!==n?(Vt(f.resultKnnDistances,n),Vt(h.resultKnnDistances,1-n),{res:yield Lt(f.resultKnnIndexes,f.resultKnnDistances,l,h.resultKnnIndexes,h.resultKnnDistances,l,o,l,"+"),unionMatrixOffsets:l}):{res:f,unionMatrixOffsets:l,unionSizes:i.resultUnionSizesArray}},new((i=void 0)||(i=Promise))((function(t,e){function n(t){try{u(o.next(t))}catch(t){e(t)}}function a(t){try{u(o.throw(t))}catch(t){e(t)}}function u(e){var r;e.done?t(e.value):(r=e.value,r instanceof i?r:new i((function(t){t(r)}))).then(n,a)}u((o=o.apply(r,s||[])).next())}));var r,s,i,o}function Kt(t,e=15,n=1,r=64,s=1){return i=this,o=void 0,u=function*(){const i=t.length,o=Math.ceil(i/100),a=Math.ceil(Math.sqrt(o)),u=function(t=10,e,n,r=15,s=1,i=64,o=1){const a=n.length;let u=0;for(let t=0;t<a;t++){let e=0;for(let s=0;s<r;s++)e+=n[t][s];u+=e/r}return` \n var<private> SMOOTH_K_TOLERANCE: f32 = 1e-5;\n var<private> MIN_K_DIST_SCALE: f32 = 1e-3;\n var<private> nNeighbors: u32 = ${r};\n var<private> localConnectivity: f32 = ${s}; \n var<private> nIter: u32 = ${i};\n var<private> bandwidth: u32 = ${o};\n var<private> meanDistances: f32 = ${u/a};\n var<private> targetValue: f32 = ${Math.log(r)/Math.log(2)*o};\n @group(0) @binding(0) var<storage, read_write> sigmas: array<f32, ${a}>;\n @group(0) @binding(1) var<storage, read_write> rhos: array<f32, ${a}>;\n @group(0) @binding(2) var<storage, read> distances: array<array<f32, ${r}>, ${a}>;\n @compute @workgroup_size(${t}, ${t}) fn smoothKNNDistance(\n @builtin(global_invocation_id) id: vec3<u32>,\n ) {\n let col: u32 = id.x;\n let row: u32 = id.y;\n let workingIndex: u32 = row * ${e} + col;\n\n if (workingIndex >= ${a}) {\n return;\n }\n\n var lo: f32 = 0.0;\n var hi: f32 = 1.0e+30; // treated as Infinity\n var mid: f32 = 1.0;\n\n var nonZeroDists: array<f32, ${r}>;\n var nonZeroDistsSize: u32 = 0;\n for (var j = 0u; j < nNeighbors; j = j + 1u) {\n if (distances[workingIndex][j] > 0.0) {\n nonZeroDists[nonZeroDistsSize] = distances[workingIndex][j];\n nonZeroDistsSize = nonZeroDistsSize + 1u;\n }\n }\n\n if (f32(nonZeroDistsSize) >= localConnectivity) {\n let index: u32 = u32(floor(localConnectivity));\n let interpolation: f32 = localConnectivity - f32(index);\n if (index > 0) {\n rhos[workingIndex] = nonZeroDists[index - 1];\n if (interpolation > SMOOTH_K_TOLERANCE) {\n rhos[workingIndex] += interpolation * (nonZeroDists[index] - nonZeroDists[index - 1]);\n }\n }\n else {\n rhos[workingIndex] = interpolation * nonZeroDists[0];\n }\n }\n else if (nonZeroDistsSize > 0u) {\n var maxDist: f32 = 0.0;\n for (var j = 0u; j < nonZeroDistsSize; j = j + 1u) {\n maxDist = max(nonZeroDists[j], maxDist);\n }\n rhos[workingIndex] = maxDist;\n }\n\n for (var n = 0u; n < nIter; n = n + 1u) {\n var psum: f32 = 0.0;\n for (var j = 1u; j < ${r}; j = j + 1u) {\n let d: f32 = distances[workingIndex][j] - rhos[workingIndex];\n if (d > 0.0) {\n psum += exp(0.0 - (d / mid));\n }\n else {\n psum += 1.0;\n }\n }\n\n if (abs(psum - targetValue) < SMOOTH_K_TOLERANCE) {\n break;\n }\n\n if (psum > targetValue) {\n hi = mid;\n mid = (lo + hi) / 2.0;\n }\n else {\n lo = mid;\n if (hi == 1.0e+30) {\n mid *= 2.0;\n }\n else {\n mid = (lo + hi) / 2.0;\n }\n }\n }\n\n sigmas[workingIndex] = mid;\n\n if (rhos[workingIndex] > 0.0) {\n var sum: f32 = 0.0;\n for (var j = 0u; j < ${r}; j = j + 1u) {\n sum += distances[workingIndex][j];\n }\n let meanIthDistances: f32 = sum / ${r}.0;\n if (sigmas[workingIndex] < MIN_K_DIST_SCALE * meanIthDistances) {\n sigmas[workingIndex] = MIN_K_DIST_SCALE * meanIthDistances;\n }\n }\n else {\n if (sigmas[workingIndex] < MIN_K_DIST_SCALE * meanDistances) {\n sigmas[workingIndex] = MIN_K_DIST_SCALE * meanDistances;\n }\n }\n }\n `}(10,10*a,t,e,n,r,s),l=i*t[0].length,h=yield Et();if(null==h)return;const c=new Float32Array(i),f=new Float32Array(i),m=h.createShaderModule({label:"sigmaRhoCalulation",code:u}),g=h.createComputePipeline({label:"sigmaRhoPipeline",layout:"auto",compute:{module:m,entryPoint:"smoothKNNDistance"}}),d=h.createBuffer({label:"distance buffer",size:4*l,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST,mappedAtCreation:!0}),p=d.getMappedRange(),w=new Float32Array(p);for(let e=0;e<t.length;e++)w.set(t[e],e*t[e].length);d.unmap();const y=h.createBuffer({label:"buffer sigmas",size:4*i,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC}),b=h.createBuffer({label:"buffer Rhos",size:4*i,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC}),x=h.createBindGroup({label:"bindGroup for sigmarho buffers",layout:g.getBindGroupLayout(0),entries:[{binding:0,resource:{buffer:y}},{binding:1,resource:{buffer:b}},{binding:2,resource:{buffer:d}}]}),M=h.createCommandEncoder({label:"sigmarho encoder"}),v=M.beginComputePass({label:"sigmarho compute pass"});v.setPipeline(g),v.setBindGroup(0,x),v.dispatchWorkgroups(a,a),v.end();const S=h.createBuffer({label:"result buffer rhos",size:b.size,usage:GPUBufferUsage.MAP_READ|GPUBufferUsage.COPY_DST}),A=h.createBuffer({label:"result buffer sigmas",size:y.size,usage:GPUBufferUsage.MAP_READ|GPUBufferUsage.COPY_DST});M.copyBufferToBuffer(b,0,S,0,S.size),M.copyBufferToBuffer(y,0,A,0,A.size);const E=M.finish();return h.queue.submit([E]),yield h.queue.onSubmittedWorkDone(),yield S.mapAsync(GPUMapMode.READ),yield A.mapAsync(GPUMapMode.READ),f.set(new Float32Array(S.getMappedRange())),c.set(new Float32Array(A.getMappedRange())),S.unmap(),A.unmap(),d.destroy(),y.destroy(),b.destroy(),S.destroy(),A.destroy(),{resultRhos:f,resultSigmas:c}},new((a=void 0)||(a=Promise))((function(t,e){function n(t){try{s(u.next(t))}catch(t){e(t)}}function r(t){try{s(u.throw(t))}catch(t){e(t)}}function s(e){var s;e.done?t(e.value):(s=e.value,s instanceof a?s:new a((function(t){t(s)}))).then(n,r)}s((u=u.apply(i,o||[])).next())}));var i,o,a,u}var Wt=function(t,e,n,r){return new(n||(n=Promise))((function(s,i){function o(t){try{u(r.next(t))}catch(t){i(t)}}function a(t){try{u(r.throw(t))}catch(t){i(t)}}function u(t){var e;t.done?s(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(o,a)}u((r=r.apply(t,e||[])).next())}))};class Ht{constructor(){this.nComponents=2,this.gamma=1,this.alpha=1,this.entryLen=0,this.nNeighbours=15,this.spread=1,this.minDist=.1,this.negativeSampleRate=5,this.localConnectivity=1,this.setOpMixRatio=1}}class Zt{constructor(t,e){this.params=new Ht,this.knnIndexes=null,this.knnDistances=null,this.rhos=null,this.sigmas=null,this.membershipStrengths=null,this.params.entryLen=t,Object.assign(this.params,e)}setPrecomputedKNN(t,e){this.knnIndexes=t,this.knnDistances=e}initializeFit(){return Wt(this,void 0,void 0,(function*(){yield this.calcSmoothKNNDistances(),yield this.calcMembershipStrengths()}))}fit(){return Wt(this,void 0,void 0,(function*(){yield this.initializeFit();const t=yield this.initializeSimplicialSetEmbedding();if(!t)throw new Error("failed to compute umap simplistical set embeddings");const{head:e,tail:n,epochsPerSample:r,epochsPerNegativeSample:s,a:i,b:o,gamma:a,initialAlpha:u,nComponents:l,nEpochs:h,entryLen:c}=t,f=yield function(t,e,n,r,s,i,o,a,u,l,h,c){return f=this,m=void 0,d=function*(){const f=Math.ceil(t.length/100),m=Math.ceil(Math.sqrt(f)),g=10*m,d=yield Et();if(!d)return;const p=1e7,w=t.length,y=function(t,e,n,r,s,i,o,a,u,l,h,c,f=1e5){return`\n// first define some functions\n\nfn rDist(ar1: array<f32, ${a}>, ar2: array<f32, ${a}>) -> f32 {\n var res = 0.0;\n for (var i1 = 0u; i1 < ${a}; i1++) {\n let diff = ar1[i1] - ar2[i1];\n res += pow(diff, 2);\n }\n return res;\n}\n\nfn clip(x: f32, clipValue: f32) -> f32 {\n if (x > clipValue) {\n return clipValue;\n }\n else if (x < 0.0 - clipValue) {\n return 0.0 - clipValue;\n }\n return x;\n}\n\nvar<private> randomSeedInt: u32 = 0;\nvar<private> nVertices: u32 = ${l};\nvar<private> initialAlpha: f32 = ${r};\nvar<private> gamma: f32 = ${s};\nvar<private> a: f32 = ${i};\nvar<private> b: f32 = ${o};\nvar<private> dim: u32 = ${a};\nvar<private> nEpochs: f32 = ${u};\nvar<private> moveOther: u32 = 1;\n\nfn random() -> u32 {\n randomSeedInt = (randomSeedInt ^ 61) ^ (randomSeedInt >> 16);\n randomSeedInt *= 9;\n randomSeedInt = randomSeedInt ^ (randomSeedInt >> 4);\n randomSeedInt *= 0x27d4eb2d;\n randomSeedInt = randomSeedInt ^ (randomSeedInt >> 15);\n return randomSeedInt;\n}\n\nfn randInt(maxVal: u32) -> u32 {\n let nextRandomNum = random();\n return u32(floor((f32(nextRandomNum) * (f32(maxVal) / 4294967296.0))));\n}\n\nstruct MatrixStorage {\n head: array<u32, ${t}>,\n tail: array<u32, ${t}>,\n}\n\nstruct EpochStorage {\n epochsPerSample: array<f32, ${t}>,\n epochOfNextSample: array<f32, ${t}>,\n epochsPerNegativeSample: array<f32, ${t}>,\n epochOfNextNegativeSample: array<f32, ${t}>\n}\n\nstruct EmbeddingStorage {\n // embeddings will be in range of -10 to 10, as atomics only can store i32 or u32, we will store them as floor(f32 * 100_000)\n headEmbeddings: array<array<atomic<i32>, ${a}>, ${e}>,\n tailEmbeddings: array<array<atomic<i32>, ${a}>, ${e}>\n}\n\nstruct ComputeInfo {\n n: f32,\n alpha: f32,\n randomNumbers: array<u32, ${t}>,\n //tailOffsets: array<u32, ${e+1}>\n}\n @group(0) @binding(0) var<storage, read_write> matrixStorage: MatrixStorage;\n @group(0) @binding(1) var<storage, read_write> epochStorage: EpochStorage;\n @group(0) @binding(2) var<storage, read_write> embedStorage: EmbeddingStorage;\n @group(0) @binding(3) var<storage, read_write> computeInfo: ComputeInfo;\n @compute @workgroup_size(${h}, ${h}) fn optimizeStep(\n @builtin(global_invocation_id) id: vec3<u32>,\n ) {\n let divisionFactor: f32 = ${f};\n let col: u32 = id.x;\n let row: u32 = id.y;\n let threadIndex: u32 = row * ${c} + col;\n let clipValue: f32 = 4.0;\n\n if (threadIndex >= ${t}) {\n return;\n }\n\n let workingIndex = threadIndex;\n //let startAtOffset = computeInfo.tailOffsets[threadIndex];\n //let endAtOffset = computeInfo.tailOffsets[threadIndex + 1];\n //for (var workingIndex = startAtOffset; workingIndex < endAtOffset; workingIndex++) {\n randomSeedInt = computeInfo.randomNumbers[workingIndex] * u32(computeInfo.n);\n\n let epochOfNextSample = epochStorage.epochOfNextSample[workingIndex];\n if (epochOfNextSample > computeInfo.n) {\n return;\n }\n\n let j = matrixStorage.head[workingIndex];\n let k = matrixStorage.tail[workingIndex];\n\n // as said before, we will store embeddings as floor(f32 * 100_000)\n var current: array<f32, ${a}>;\n var other: array<f32, ${a}>;\n for (var i = 0u; i < ${a}u; i++) {\n current[i] = f32(atomicLoad(&embedStorage.headEmbeddings[j][i])) / divisionFactor;\n other[i] = f32(atomicLoad(&embedStorage.tailEmbeddings[k][i])) / divisionFactor;\n }\n\n let distSquared = rDist(current, other);\n \n var gradCoeff: f32 = 0.0;\n if (distSquared > 0.0) {\n gradCoeff = (0.0 - 2.0) * ${i} * ${o} * pow(distSquared, b - 1.0);\n gradCoeff /= (a * pow(distSquared, b) + 1.0);\n }\n\n var gradBuff: array<i32, ${a}>;\n for (var d = 0u; d < ${a}u; d++) {\n let gradD = clip((gradCoeff * (current[d] - other[d])), clipValue);\n let toAdd = gradD * computeInfo.alpha;\n gradBuff[d] = i32(floor(toAdd * divisionFactor));\n current[d] += toAdd;\n other[d] -= toAdd;\n }\n\n for (var d = 0u; d < ${a}u; d++) {\n atomicAdd(&embedStorage.headEmbeddings[j][d], gradBuff[d]);\n atomicSub(&embedStorage.tailEmbeddings[k][d], gradBuff[d]);\n }\n\n epochStorage.epochOfNextSample[workingIndex] += epochStorage.epochsPerSample[workingIndex];\n var nNegSamples: i32 = i32(floor((computeInfo.n - epochStorage.epochOfNextNegativeSample[workingIndex]) / epochStorage.epochsPerNegativeSample[workingIndex]));\n\n for (var p = 0i; p < nNegSamples; p++) {\n let k1 = randInt(nVertices);\n var other1: array<f32, ${a}>;\n for (var i = 0u; i < ${a}u; i++) {\n other1[i] = f32(atomicLoad(&embedStorage.tailEmbeddings[k1][i])) / divisionFactor;\n }\n\n let distSquared1 = rDist(current, other1);\n\n var gradCoeff1: f32 = 0.0;\n if (distSquared1 > 0.0) {\n gradCoeff1 = 2.0 * gamma * b;\n gradCoeff1 /= (0.001 + distSquared1) * (a * pow(distSquared1, b) + 1);\n }\n else if (j == k1) {\n continue;\n }\n\n var gradDBuff: array<i32, ${a}>;\n for (var d = 0u; d < ${a}u; d++) {\n var gradD: f32 = 4.0;\n if (gradCoeff1 > 0.0) {\n gradD = clip(gradCoeff1 * (current[d] - other1[d]), clipValue);\n }\n\n gradDBuff[d] = i32(floor(gradD * computeInfo.alpha * divisionFactor));\n \n }\n for (var d = 0u; d < ${a}u; d++) {\n atomicAdd(&embedStorage.headEmbeddings[j][d], gradDBuff[d]);\n }\n }\n epochStorage.epochOfNextNegativeSample[workingIndex] += f32(nNegSamples) * epochStorage.epochsPerNegativeSample[workingIndex];\n //}\n }\n`}(w,n,0,i,o,a,u,l,h,c,10,g,p);let b=2*w*4;const x=15&b;0!==x&&(b+=16-x);let M=4*w*4;const v=15&M;0!==v&&(M+=16-v);let S=2*n*2*4;const A=15&S;0!==A&&(S+=16-A);let E=4*(2+w);const I=15&E;0!==I&&(E+=16-I);const N=d.createShaderModule({label:"optimizeShader",code:y}),R=d.createComputePipeline({label:"optimizePipeline",layout:"auto",compute:{module:N,entryPoint:"optimizeStep"}}),k=d.createBuffer({label:"matrix storage buffer",size:b,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST,mappedAtCreation:!0}),C=k.getMappedRange();new Uint32Array(C,0,w).set(t),new Uint32Array(C,4*w,w).set(e),k.unmap();const T=d.createBuffer({label:"epoch storage buffer",size:M,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST,mappedAtCreation:!0}),P=T.getMappedRange(),D=new Float32Array(P);D.set(r,0),D.set(r,w),D.set(s,2*w),D.set(s,3*w),T.unmap();const U=new Int32Array(n*l).map((()=>Math.floor((2*Math.random()-1)*p)));new Int32Array(n*l).set(U);const O=d.createBuffer({label:"embedding storage buffer",size:S,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST,mappedAtCreation:!0}),z=O.getMappedRange(),B=new Int32Array(z);B.set(U,0),B.set(U,U.length),O.unmap();const _=d.createBuffer({label:"compute info buffer",size:E,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST,mappedAtCreation:!0}),L=new Uint32Array(w).map((()=>Math.floor(Math.random()*p))),$=_.getMappedRange();new Float32Array($,0,2).set([0,i]),new Uint32Array($,8,w).set(L),_.unmap();const G=d.createBindGroup({label:"bindGroup for optimize",layout:R.getBindGroupLayout(0),entries:[{binding:0,resource:{buffer:k}},{binding:1,resource:{buffer:T}},{binding:2,resource:{buffer:O}},{binding:3,resource:{buffer:_}}]});let F=i;const j=d.createBuffer({label:"result buffer",size:O.size,usage:GPUBufferUsage.MAP_READ|GPUBufferUsage.COPY_DST});for(let t=0;t<h;t++){d.queue.writeBuffer(_,0,new Float32Array([t,F]),0);const e=d.createCommandEncoder({label:"matrix ops encoder"}),n=e.beginComputePass({label:"matrix ops compute pass"});n.setPipeline(R),n.setBindGroup(0,G),n.dispatchWorkgroups(m,m),n.end(),t===h-1&&e.copyBufferToBuffer(O,0,j,0,j.size);const r=e.finish();d.queue.submit([r]),yield d.queue.onSubmittedWorkDone(),F=i*(1-t/h)}yield j.mapAsync(GPUMapMode.READ);const q=j.getMappedRange(),V=new Int32Array(q,0,U.length);U.set(V),j.unmap();const Y=new Array(l).fill(null).map((t=>new Float32Array(n)));for(let t=0;t<n;t++)for(let e=0;e<l;e++)Y[e][t]=U[t*l+e]/p;return k.destroy(),T.destroy(),O.destroy(),_.destroy(),j.destroy(),Y},new((g=void 0)||(g=Promise))((function(t,e){function n(t){try{s(d.next(t))}catch(t){e(t)}}function r(t){try{s(d.throw(t))}catch(t){e(t)}}function s(e){var s;e.done?t(e.value):(s=e.value,s instanceof g?s:new g((function(t){t(s)}))).then(n,r)}s((d=d.apply(f,m||[])).next())}));var f,m,g,d}(e,n,c,r,s,u,a,i,o,l,h,c);return f}))}calcSmoothKNNDistances(){return Wt(this,void 0,void 0,(function*(){if(!this.knnIndexes||!this.knnDistances)throw new Error("knn indexes and distances must be set before calling fit");const t=yield Kt(this.knnDistances,this.params.nNeighbours,this.params.localConnectivity,64);if(!t)throw new Error("failed to compute smooth knn distances");this.rhos=t.resultRhos,this.sigmas=t.resultSigmas}))}calcMembershipStrengths(){return Wt(this,void 0,void 0,(function*(){if(!(this.knnIndexes&&this.knnDistances&&this.rhos&&this.sigmas))throw new Error("knn indexes, distances, rhos, and sigmas must be set before calling fit");const t=yield function(t,e,n){return r=this,s=void 0,o=function*(){const r=t.length,s=Math.ceil(r/100),i=Math.ceil(Math.sqrt(s)),o=function(t=10,e,n,r,s){const i=n.length,o=r.length,a=s.length;return`\n struct MembershipStrengthsInfo {\n knnDistances: array<array<f32, ${n[0].length}>, ${i}>,\n sigmas: array<f32, ${o}>,\n rhos: array<f32, ${a}>\n };\n var<private> nNeighbors: u32 = ${n[0].length};\n @group(0) @binding(0) var<storage, read> membershipStrengthsInfo: MembershipStrengthsInfo;\n @group(0) @binding(1) var<storage, read_write> resultKnnDistances: array<array<f32, ${n[0].length}>, ${i}>;\n @compute @workgroup_size(${t}, ${t}) fn computeMembershipStrengths(\n @builtin(global_invocation_id) id: vec3<u32>,\n ) {\n let col: u32 = id.x;\n let row: u32 = id.y;\n let workingIndex: u32 = row * ${e} + col;\n\n if (workingIndex >= ${i}) {\n return;\n }\n\n let workingResKnnDistances = &resultKnnDistances[workingIndex];\n let knnDistances = &membershipStrengthsInfo.knnDistances[workingIndex];\n for (var j = 0u; j < nNeighbors; j = j + 1u) {\n var val: f32 = 0.0;\n\n if ((*knnDistances)[j] - membershipStrengthsInfo.rhos[workingIndex] <= 0.0) {\n val = 1.0;\n } else {\n val = exp(0.0 - ((knnDistances[j] - membershipStrengthsInfo.rhos[workingIndex]) / membershipStrengthsInfo.sigmas[workingIndex])); \n }\n\n (*workingResKnnDistances)[j] = val;\n }\n }\n `}(10,10*i,t,e,n),a=r*t[0].length,u=yield Et();if(null==u)return;const l=u.createShaderModule({label:"rowsColsValsCalulation",code:o}),h=u.createComputePipeline({label:"rowsColsValsPipeline",layout:"auto",compute:{module:l,entryPoint:"computeMembershipStrengths"}});let c=4*(n.length+e.length+a);const f=15&c;0!=f&&(c+=16-f);const m=u.createBuffer({label:"info buffer",size:c,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST,mappedAtCreation:!0}),g=m.getMappedRange(),d=new Float32Array(g);for(let e=0;e<t.length;e++)d.set(t[e],e*t[e].length);let p=t[0].length*t.length;d.set(e,p),p+=e.length,d.set(n,p),m.unmap();const w=u.createBuffer({label:"knn distance buffer",size:t[0].length*t.length*Float32Array.BYTES_PER_ELEMENT,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST}),y=u.createBindGroup({label:"bindGroup for membership strhegths buffers",layout:h.getBindGroupLayout(0),entries:[{binding:0,resource:{buffer:m}},{binding:1,resource:{buffer:w}}]}),b=u.createCommandEncoder({label:"membership strengths encoder"}),x=b.beginComputePass({label:"membership strengths compute pass"});x.setPipeline(h),x.setBindGroup(0,y),x.dispatchWorkgroups(i,i),x.end();const M=u.createBuffer({label:"result buffer distances",size:w.size,usage:GPUBufferUsage.MAP_READ|GPUBufferUsage.COPY_DST});b.copyBufferToBuffer(w,0,M,0,M.size);const v=b.finish();u.queue.submit([v]),yield u.queue.onSubmittedWorkDone(),yield M.mapAsync(GPUMapMode.READ);const S=M.getMappedRange(),A=t[0].length*Float32Array.BYTES_PER_ELEMENT,E=new Array(t.length).fill(null).map(((e,n)=>{const r=new Float32Array(S,A*n,t[0].length),s=new Float32Array(t[0].length);return s.set(r),s}));return M.unmap(),M.destroy(),w.destroy(),m.destroy(),E},new((i=void 0)||(i=Promise))((function(t,e){function n(t){try{u(o.next(t))}catch(t){e(t)}}function a(t){try{u(o.throw(t))}catch(t){e(t)}}function u(e){var r;e.done?t(e.value):(r=e.value,r instanceof i?r:new i((function(t){t(r)}))).then(n,a)}u((o=o.apply(r,s||[])).next())}));var r,s,i,o}(this.knnDistances,this.sigmas,this.rhos);if(!t)throw new Error("failed to compute membership strengths");this.membershipStrengths=t}))}fuzzySimplicialSet(){return Wt(this,void 0,void 0,(function*(){if(!(this.knnIndexes&&this.knnDistances&&this.rhos&&this.sigmas&&this.membershipStrengths))throw new Error("knn indexes, distances, rhos, sigmas, and membership strengths must be set before calling fit");const t=yield Yt(this.knnIndexes,this.membershipStrengths,this.params.setOpMixRatio);if(!t)throw new Error("failed to compute pairwise multiply with transpose");return t}))}initializeSimplicialSetEmbedding(){return Wt(this,void 0,void 0,(function*(){const t=yield this.fuzzySimplicialSet();if(!(t&&t.res&&t.unionMatrixOffsets&&t.unionSizes))throw new Error("failed to compute fuzzy simplicial set");const e=yield function(t,e,n,r,s=5){return i=this,o=void 0,u=function*(){if(!t.unionSizes)return;const i=function(t){return t<=2500?1e3:t<=5e3?800:t<=7500?650:400}(e),o=t.res.resultKnnDistances.reduce(((t,e)=>Math.max(t,e))),a=o/i,u=new Uint32Array(t.unionSizes.length);u.set(t.unionSizes);let l=0;for(let e=0;e<t.unionSizes.length;e++){for(let n=0;n<t.unionSizes[e];n++){const r=l+n;t.res.resultKnnDistances[r]<a&&(t.res.resultKnnDistances[r]=0,u[e]--)}l+=t.unionSizes[e]}const h=qt(u),c=h[h.length-1],f=new Float32Array(c),m=new Uint32Array(c),g=new Uint32Array(c);l=0;for(let e=0;e<t.unionSizes.length;e++)for(let n=0;n<t.unionSizes[e];n++){const r=t.unionMatrixOffsets[e]+n;0!=t.res.resultKnnDistances[r]&&(f[l]=t.res.resultKnnDistances[r],m[l]=t.res.resultKnnIndexes[r],g[l]=e,l+=1)}const d=new Float32Array(f.length).fill(-1),p=f.map((t=>t/o*i));p.forEach(((t,e)=>{t>0&&(d[e]=i/p[e])}));const{a:w,b:y}=jt(n,r),b=d.map((t=>t/s));return{epochsPerSample:d,epochsPerNegativeSample:b,a:w,b:y,head:m,tail:g,entryLen:e,nEpochs:i,nComponents:2,initialAlpha:1,gamma:1}},new((a=void 0)||(a=Promise))((function(t,e){function n(t){try{s(u.next(t))}catch(t){e(t)}}function r(t){try{s(u.throw(t))}catch(t){e(t)}}function s(e){var s;e.done?t(e.value):(s=e.value,s instanceof a?s:new a((function(t){t(s)}))).then(n,r)}s((u=u.apply(i,o||[])).next())}));var i,o,a,u}(t,this.params.entryLen,this.params.spread,this.params.minDist,this.params.negativeSampleRate);if(!e)throw new Error("failed to compute umap simplistical set embeddings");return e}))}}class Xt{constructor(t){this.data=t.data,this.weights=t.weights,this.aggregationMethod=t.aggregationMethod}}class Qt extends Xt{constructor(t){super(t);const e=t.randomSeed??Date(),n=Mt()(e);t.dim=2,t.random=n,this.reducer=new vt(t),this.iterations=t?.iterations??this.reducer.getIterSize(this.data[0].length),this.distanceFnames=t.distanceFnames,this.distanceFns=t.distanceFns,this.distanceFnArgs=t.distanceFnArgs,this.progressFunc=t.progressFunc}async transform(){if(this.data[0].length>1e4)throw new Error("Maximum number of samples for T-SNE is 10000");const t=new b(!0,!1);try{const e=await t.calcMulti(this.data,this.distanceFnames,!1,this.distanceFnArgs,this.weights,this.aggregationMethod);t.terminate(),this.reducer.initDataDist(e,this.data[0].length);for(let t=0;t<this.iterations;++t)this.reducer.step(),this.progressFunc&&this.progressFunc(t,this.iterations,[]);return this.reducer.getSolution()}catch(e){throw t.terminate(),e}}}class Jt extends Xt{constructor(t){const e=t.randomSeed??Date(),n=Mt()(e);super(t),this.useWebGPU=!1,wt("distanceFnames"in t),wt("distanceFns"in t),this.distanceFnArgs=t.distanceFnArgs,this.distanceFns=t.distanceFns,this.progressFunc=t.progressFunc,this.useWebGPU=t.useWebGPU??!1,this.distanceFnames=t.distanceFnames,t.nComponents=2,this.vectors=new Array(this.data[0].length).fill(0).map(((t,e)=>e)),this.data[0].length<=(t.nNeighbors??15)&&(t.nNeighbors=this.data[0].length-1),t.random=n,this.reducer=new ft(t)}async transform(t){let e;if(console.time("knn graph"),this.useWebGPU)try{e=await Bt(this.data,this.reducer.neighbors,this.distanceFnames,this.aggregationMethod,this.weights,this.distanceFnArgs)}catch(t){console.error(t)}if(e||(this.useWebGPU&&console.error("WEBGPU KNN failed, falling back to multithreaded CPU implementation"),e=await(new yt.p).multiColumnKNN(this.data,this.distanceFnames,this.reducer.neighbors,this.distanceFnArgs,this.weights,this.aggregationMethod)),console.timeEnd("knn graph"),!e)throw new Error("Failed to compute KNN graph");const n=this.data[0].length;for(let t=0;t<e.knnIndexes.length;++t)for(let r=0;r<e.knnIndexes[t].length;++r)e.knnDistances[t][r]<0&&(e.knnDistances[t][r]=0),(e.knnIndexes[t][r]<0||e.knnIndexes[t][r]>=n||e.knnDistances[t][r]>10)&&(e.knnIndexes[t][r]=t==r?r+1:r,e.knnDistances[t][r]=10);this.useWebGPU?(this.webGPUReducer=new Zt(this.vectors.length,{nComponents:this.reducer.nComponents,gamma:this.reducer.repulsionStrength,alpha:this.reducer.learningRate,nNeighbours:this.reducer.neighbors,spread:this.reducer.spread,minDist:this.reducer.minDist,negativeSampleRate:this.reducer.negativeSampleRate,localConnectivity:this.reducer.localConnectivity,setOpMixRatio:this.reducer.setOpMixRatio}),this.webGPUReducer.setPrecomputedKNN(e.knnIndexes,e.knnDistances)):this.reducer.setPrecomputedKNN(e.knnIndexes,e.knnDistances),await new Promise((t=>{setTimeout((()=>{t()}),300)}));let s=null;console.time("fit");try{if(this.useWebGPU&&this.webGPUReducer){if(s=await this.webGPUReducer.fit(),!s)throw new Error("Failed to compute embedding");s=new Array(s[0].length).fill(null).map(((t,e)=>new Float32Array(s.map((t=>t[e])))))}}catch(t){console.error(t)}return s||(this.useWebGPU&&(console.error("WEBGPU UMAP failed, falling back to CPU implementation"),this.reducer.setPrecomputedKNN(e.knnIndexes,e.knnDistances)),s=await this.reducer.fitAsync(this.vectors,(t=>{this.progressFunc&&this.progressFunc(t,this.reducer.getNEpochs(),this.reducer.getEmbedding())}))),console.timeEnd("fit"),i=s,new Array(i.length).fill(0).map(((t,e)=>r.from(i[e])));var i}}const te={UMAP:Jt,"t-SNE":Qt};class ee{constructor(t,e,n,r,s,i){const o=[];for(let e=0;e<n.length;++e){const r=new w(n[e]).getMeasure(i.distanceFnArgs[e]);o.push(r);let s=2048;for(let n=0;n<t[e].length;++n)if(t[e][n]&&t[e][n]._length){s=t[e][n]._length;break}if(a=n[e],"BitArray"==p[a])for(let n=0;n<t[e].length;++n)t[e][n]&&t[e][n]._data?t[e][n]=new bt.A(t[e][n]._data,t[e][n]._length):t[e][n]=new bt.A(s)}var a;"UMAP"==e?this.reducer=new Jt({data:t,distanceFnames:n,distanceFns:o,distanceFnArgs:i.distanceFnArgs,weights:r,aggregationMethod:s,...i}):"t-SNE"==e&&(this.reducer=new Qt({data:t,distanceFnames:n,distanceFns:o,distanceFnArgs:i.distanceFnArgs,weights:r,aggregationMethod:s,...i}))}async transform(t=!1){if(void 0===this.reducer)throw new Error("Reducer was not defined.");let e=await this.reducer.transform();return t&&(e=function(t){return new Array(t[0].length).fill(0).map(((e,n)=>new r(t.length).fill(0).map(((e,r)=>t[r][n]))))}(e)),e}static availableMetricsByType(t){return Object.keys(d[t])}static get availableMethods(){return Object.keys(te)}static get availableMetrics(){let t=[];return Object.values(d).forEach((e=>{const n=Object.values(e);t=[...t,...n]})),t}}async function ne(t,e,n){t%5==0&&self.postMessage({epochNum:t,epochsLength:e,embedding:n})}self.onmessage=async({data:{columnsData:t,method:e,distanceMetrics:n,options:r,weights:s,aggregationMethod:i}})=>{let o;try{const a=await async function(t,e,n,r,s,i){const o=new ee(t,e,n,r,s,{...i,progressFunc:ne});return await o.transform(!0)}(t,e,n,s,i,r);o={embedding:a}}catch(t){o={error:t}}self.postMessage({error:o.error,embedding:o.embedding})}},788:(t,e,n)=>{"use strict";n.r(e),n.d(e,{isAnyArray:()=>s});const r=Object.prototype.toString;function s(t){const e=r.call(t);return e.endsWith("Array]")&&!e.includes("Big")}},3718:(t,e,n)=>{"use strict";n.r(e),n.d(e,{default:()=>s});var r=n(788);function s(t){var e,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!(0,r.isAnyArray)(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");if(void 0!==n.output){if(!(0,r.isAnyArray)(n.output))throw new TypeError("output option must be an array if specified");e=n.output}else e=new Array(t.length);var s=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!(0,r.isAnyArray)(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");var n=e.fromIndex,s=void 0===n?0:n,i=e.toIndex,o=void 0===i?t.length:i;if(s<0||s>=t.length||!Number.isInteger(s))throw new Error("fromIndex must be a positive integer smaller than length");if(o<=s||o>t.length||!Number.isInteger(o))throw new Error("toIndex must be an integer greater than fromIndex and at most equal to length");for(var a=t[s],u=s+1;u<o;u++)t[u]<a&&(a=t[u]);return a}(t),i=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!(0,r.isAnyArray)(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");var n=e.fromIndex,s=void 0===n?0:n,i=e.toIndex,o=void 0===i?t.length:i;if(s<0||s>=t.length||!Number.isInteger(s))throw new Error("fromIndex must be a positive integer smaller than length");if(o<=s||o>t.length||!Number.isInteger(o))throw new Error("toIndex must be an integer greater than fromIndex and at most equal to length");for(var a=t[s],u=s+1;u<o;u++)t[u]>a&&(a=t[u]);return a}(t);if(s===i)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");var o=n.min,a=void 0===o?n.autoMinMax?s:0:o,u=n.max,l=void 0===u?n.autoMinMax?i:1:u;if(a>=l)throw new RangeError("min option must be smaller than max option");for(var h=(l-a)/(i-s),c=0;c<t.length;c++)e[c]=(t[c]-s)*h+a;return e}},3054:(t,e,n)=>{"use strict";var r=n(788),s=n(3718);const i=" ".repeat(2),o=" ".repeat(4);function a(t,e={}){const{maxRows:n=15,maxColumns:r=10,maxNumSize:s=8,padMinus:a="auto"}=e;return`${t.constructor.name} {\n${i}[\n${o}${function(t,e,n,r,s){const{rows:i,columns:a}=t,l=Math.min(i,e),h=Math.min(a,n),c=[];if("auto"===s){s=!1;t:for(let e=0;e<l;e++)for(let n=0;n<h;n++)if(t.get(e,n)<0){s=!0;break t}}for(let e=0;e<l;e++){let n=[];for(let i=0;i<h;i++)n.push(u(t.get(e,i),r,s));c.push(`${n.join(" ")}`)}return h!==a&&(c[c.length-1]+=` ... ${a-n} more columns`),l!==i&&c.push(`... ${i-e} more rows`),c.join(`\n${o}`)}(t,n,r,s,a)}\n${i}]\n${i}rows: ${t.rows}\n${i}columns: ${t.columns}\n}`}function u(t,e,n){return(t>=0&&n?` ${l(t,e-1)}`:l(t,e)).padEnd(e)}function l(t,e){let n=t.toString();if(n.length<=e)return n;let r=t.toFixed(e);if(r.length>e&&(r=t.toFixed(Math.max(0,e-(r.length-e)))),r.length<=e&&!r.startsWith("0.000")&&!r.startsWith("-0.000"))return r;let s=t.toExponential(e);return s.length>e&&(s=t.toExponential(Math.max(0,e-(s.length-e)))),s.slice(0)}function h(t,e,n){let r=n?t.rows:t.rows-1;if(e<0||e>r)throw new RangeError("Row index out of range")}function c(t,e,n){let r=n?t.columns:t.columns-1;if(e<0||e>r)throw new RangeError("Column index out of range")}function f(t,e){if(e.to1DArray&&(e=e.to1DArray()),e.length!==t.columns)throw new RangeError("vector size must be the same as the number of columns");return e}function m(t,e){if(e.to1DArray&&(e=e.to1DArray()),e.length!==t.rows)throw new RangeError("vector size must be the same as the number of rows");return e}function g(t,e){if(!r.isAnyArray(e))throw new TypeError("row indices must be an array");for(let n=0;n<e.length;n++)if(e[n]<0||e[n]>=t.rows)throw new RangeError("row indices are out of range")}function d(t,e){if(!r.isAnyArray(e))throw new TypeError("column indices must be an array");for(let n=0;n<e.length;n++)if(e[n]<0||e[n]>=t.columns)throw new RangeError("column indices are out of range")}function p(t,e,n,r,s){if(5!==arguments.length)throw new RangeError("expected 4 arguments");if(y("startRow",e),y("endRow",n),y("startColumn",r),y("endColumn",s),e>n||r>s||e<0||e>=t.rows||n<0||n>=t.rows||r<0||r>=t.columns||s<0||s>=t.columns)throw new RangeError("Submatrix indices are out of range")}function w(t,e=0){let n=[];for(let r=0;r<t;r++)n.push(e);return n}function y(t,e){if("number"!=typeof e)throw new TypeError(`${t} must be a number`)}function b(t){if(t.isEmpty())throw new Error("Empty matrix has no elements to index")}class x{static from1DArray(t,e,n){if(t*e!==n.length)throw new RangeError("data length does not match given dimensions");let r=new v(t,e);for(let s=0;s<t;s++)for(let t=0;t<e;t++)r.set(s,t,n[s*e+t]);return r}static rowVector(t){let e=new v(1,t.length);for(let n=0;n<t.length;n++)e.set(0,n,t[n]);return e}static columnVector(t){let e=new v(t.length,1);for(let n=0;n<t.length;n++)e.set(n,0,t[n]);return e}static zeros(t,e){return new v(t,e)}static ones(t,e){return new v(t,e).fill(1)}static rand(t,e,n={}){if("object"!=typeof n)throw new TypeError("options must be an object");const{random:r=Math.random}=n;let s=new v(t,e);for(let n=0;n<t;n++)for(let t=0;t<e;t++)s.set(n,t,r());return s}static randInt(t,e,n={}){if("object"!=typeof n)throw new TypeError("options must be an object");const{min:r=0,max:s=1e3,random:i=Math.random}=n;if(!Number.isInteger(r))throw new TypeError("min must be an integer");if(!Number.isInteger(s))throw new TypeError("max must be an integer");if(r>=s)throw new RangeError("min must be smaller than max");let o=s-r,a=new v(t,e);for(let n=0;n<t;n++)for(let t=0;t<e;t++){let e=r+Math.round(i()*o);a.set(n,t,e)}return a}static eye(t,e,n){void 0===e&&(e=t),void 0===n&&(n=1);let r=Math.min(t,e),s=this.zeros(t,e);for(let t=0;t<r;t++)s.set(t,t,n);return s}static diag(t,e,n){let r=t.length;void 0===e&&(e=r),void 0===n&&(n=e);let s=Math.min(r,e,n),i=this.zeros(e,n);for(let e=0;e<s;e++)i.set(e,e,t[e]);return i}static min(t,e){t=this.checkMatrix(t),e=this.checkMatrix(e);let n=t.rows,r=t.columns,s=new v(n,r);for(let i=0;i<n;i++)for(let n=0;n<r;n++)s.set(i,n,Math.min(t.get(i,n),e.get(i,n)));return s}static max(t,e){t=this.checkMatrix(t),e=this.checkMatrix(e);let n=t.rows,r=t.columns,s=new this(n,r);for(let i=0;i<n;i++)for(let n=0;n<r;n++)s.set(i,n,Math.max(t.get(i,n),e.get(i,n)));return s}static checkMatrix(t){return x.isMatrix(t)?t:new v(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 n=0;n<this.columns;n++)t.call(this,e,n);return this}to1DArray(){let t=[];for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)t.push(this.get(e,n));return t}to2DArray(){let t=[];for(let e=0;e<this.rows;e++){t.push([]);for(let n=0;n<this.columns;n++)t[e].push(this.get(e,n))}return t}toJSON(){return this.to2DArray()}isRowVector(){return 1===this.rows}isColumnVector(){return 1===this.columns}isVector(){return 1===this.rows||1===this.columns}isSquare(){return this.rows===this.columns}isEmpty(){return 0===this.rows||0===this.columns}isSymmetric(){if(this.isSquare()){for(let t=0;t<this.rows;t++)for(let e=0;e<=t;e++)if(this.get(t,e)!==this.get(e,t))return!1;return!0}return!1}isDistance(){if(!this.isSymmetric())return!1;for(let t=0;t<this.rows;t++)if(0!==this.get(t,t))return!1;return!0}isEchelonForm(){let t=0,e=0,n=-1,r=!0,s=!1;for(;t<this.rows&&r;){for(e=0,s=!1;e<this.columns&&!1===s;)0===this.get(t,e)?e++:1===this.get(t,e)&&e>n?(s=!0,n=e):(r=!1,s=!0);t++}return r}isReducedEchelonForm(){let t=0,e=0,n=-1,r=!0,s=!1;for(;t<this.rows&&r;){for(e=0,s=!1;e<this.columns&&!1===s;)0===this.get(t,e)?e++:1===this.get(t,e)&&e>n?(s=!0,n=e):(r=!1,s=!0);for(let n=e+1;n<this.rows;n++)0!==this.get(t,n)&&(r=!1);t++}return r}echelonForm(){let t=this.clone(),e=0,n=0;for(;e<t.rows&&n<t.columns;){let r=e;for(let s=e;s<t.rows;s++)t.get(s,n)>t.get(r,n)&&(r=s);if(0===t.get(r,n))n++;else{t.swapRows(e,r);let s=t.get(e,n);for(let r=n;r<t.columns;r++)t.set(e,r,t.get(e,r)/s);for(let r=e+1;r<t.rows;r++){let s=t.get(r,n)/t.get(e,n);t.set(r,n,0);for(let i=n+1;i<t.columns;i++)t.set(r,i,t.get(r,i)-t.get(e,i)*s)}e++,n++}}return t}reducedEchelonForm(){let t=this.echelonForm(),e=t.columns,n=t.rows,r=n-1;for(;r>=0;)if(0===t.maxRow(r))r--;else{let s=0,i=!1;for(;s<n&&!1===i;)1===t.get(r,s)?i=!0:s++;for(let n=0;n<r;n++){let i=t.get(n,s);for(let o=s;o<e;o++){let e=t.get(n,o)-i*t.get(r,o);t.set(n,o,e)}}r--}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:n=1}=t;if(!Number.isInteger(e)||e<=0)throw new TypeError("rows must be a positive integer");if(!Number.isInteger(n)||n<=0)throw new TypeError("columns must be a positive integer");let r=new v(this.rows*e,this.columns*n);for(let t=0;t<e;t++)for(let e=0;e<n;e++)r.setSubMatrix(this,this.rows*t,this.columns*e);return r}fill(t){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,t);return this}neg(){return this.mulS(-1)}getRow(t){h(this,t);let e=[];for(let n=0;n<this.columns;n++)e.push(this.get(t,n));return e}getRowVector(t){return v.rowVector(this.getRow(t))}setRow(t,e){h(this,t),e=f(this,e);for(let n=0;n<this.columns;n++)this.set(t,n,e[n]);return this}swapRows(t,e){h(this,t),h(this,e);for(let n=0;n<this.columns;n++){let r=this.get(t,n);this.set(t,n,this.get(e,n)),this.set(e,n,r)}return this}getColumn(t){c(this,t);let e=[];for(let n=0;n<this.rows;n++)e.push(this.get(n,t));return e}getColumnVector(t){return v.columnVector(this.getColumn(t))}setColumn(t,e){c(this,t),e=m(this,e);for(let n=0;n<this.rows;n++)this.set(n,t,e[n]);return this}swapColumns(t,e){c(this,t),c(this,e);for(let n=0;n<this.rows;n++){let r=this.get(n,t);this.set(n,t,this.get(n,e)),this.set(n,e,r)}return this}addRowVector(t){t=f(this,t);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)+t[n]);return this}subRowVector(t){t=f(this,t);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)-t[n]);return this}mulRowVector(t){t=f(this,t);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)*t[n]);return this}divRowVector(t){t=f(this,t);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)/t[n]);return this}addColumnVector(t){t=m(this,t);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)+t[e]);return this}subColumnVector(t){t=m(this,t);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)-t[e]);return this}mulColumnVector(t){t=m(this,t);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)*t[e]);return this}divColumnVector(t){t=m(this,t);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)/t[e]);return this}mulRow(t,e){h(this,t);for(let n=0;n<this.columns;n++)this.set(t,n,this.get(t,n)*e);return this}mulColumn(t,e){c(this,t);for(let n=0;n<this.rows;n++)this.set(n,t,this.get(n,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 n=0;n<this.columns;n++)this.get(e,n)>t[e]&&(t[e]=this.get(e,n));return t}case"column":{const t=new Array(this.columns).fill(Number.NEGATIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.get(e,n)>t[n]&&(t[n]=this.get(e,n));return t}case void 0:{let t=this.get(0,0);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.get(e,n)>t&&(t=this.get(e,n));return t}default:throw new Error(`invalid option: ${t}`)}}maxIndex(){b(this);let t=this.get(0,0),e=[0,0];for(let n=0;n<this.rows;n++)for(let r=0;r<this.columns;r++)this.get(n,r)>t&&(t=this.get(n,r),e[0]=n,e[1]=r);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 n=0;n<this.columns;n++)this.get(e,n)<t[e]&&(t[e]=this.get(e,n));return t}case"column":{const t=new Array(this.columns).fill(Number.POSITIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.get(e,n)<t[n]&&(t[n]=this.get(e,n));return t}case void 0:{let t=this.get(0,0);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.get(e,n)<t&&(t=this.get(e,n));return t}default:throw new Error(`invalid option: ${t}`)}}minIndex(){b(this);let t=this.get(0,0),e=[0,0];for(let n=0;n<this.rows;n++)for(let r=0;r<this.columns;r++)this.get(n,r)<t&&(t=this.get(n,r),e[0]=n,e[1]=r);return e}maxRow(t){if(h(this,t),this.isEmpty())return NaN;let e=this.get(t,0);for(let n=1;n<this.columns;n++)this.get(t,n)>e&&(e=this.get(t,n));return e}maxRowIndex(t){h(this,t),b(this);let e=this.get(t,0),n=[t,0];for(let r=1;r<this.columns;r++)this.get(t,r)>e&&(e=this.get(t,r),n[1]=r);return n}minRow(t){if(h(this,t),this.isEmpty())return NaN;let e=this.get(t,0);for(let n=1;n<this.columns;n++)this.get(t,n)<e&&(e=this.get(t,n));return e}minRowIndex(t){h(this,t),b(this);let e=this.get(t,0),n=[t,0];for(let r=1;r<this.columns;r++)this.get(t,r)<e&&(e=this.get(t,r),n[1]=r);return n}maxColumn(t){if(c(this,t),this.isEmpty())return NaN;let e=this.get(0,t);for(let n=1;n<this.rows;n++)this.get(n,t)>e&&(e=this.get(n,t));return e}maxColumnIndex(t){c(this,t),b(this);let e=this.get(0,t),n=[0,t];for(let r=1;r<this.rows;r++)this.get(r,t)>e&&(e=this.get(r,t),n[0]=r);return n}minColumn(t){if(c(this,t),this.isEmpty())return NaN;let e=this.get(0,t);for(let n=1;n<this.rows;n++)this.get(n,t)<e&&(e=this.get(n,t));return e}minColumnIndex(t){c(this,t),b(this);let e=this.get(0,t),n=[0,t];for(let r=1;r<this.rows;r++)this.get(r,t)<e&&(e=this.get(r,t),n[0]=r);return n}diag(){let t=Math.min(this.rows,this.columns),e=[];for(let n=0;n<t;n++)e.push(this.get(n,n));return e}norm(t="frobenius"){switch(t){case"max":return this.max();case"frobenius":return Math.sqrt(this.dot(this));default:throw new RangeError(`unknown norm type: ${t}`)}}cumulativeSum(){let t=0;for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)t+=this.get(e,n),this.set(e,n,t);return this}dot(t){x.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 n=0;for(let r=0;r<e.length;r++)n+=e[r]*t[r];return n}mmul(t){t=v.checkMatrix(t);let e=this.rows,n=this.columns,r=t.columns,s=new v(e,r),i=new Float64Array(n);for(let o=0;o<r;o++){for(let e=0;e<n;e++)i[e]=t.get(e,o);for(let t=0;t<e;t++){let e=0;for(let r=0;r<n;r++)e+=this.get(t,r)*i[r];s.set(t,o,e)}}return s}mpow(t){if(!this.isSquare())throw new RangeError("Matrix must be square");if(!Number.isInteger(t)||t<0)throw new RangeError("Exponent must be a non-negative integer");let e=v.eye(this.rows),n=this;for(let r=t;r>1;r/=2)1&r&&(e=e.mmul(n)),n=n.mmul(n);return e}strassen2x2(t){t=v.checkMatrix(t);let e=new v(2,2);const n=this.get(0,0),r=t.get(0,0),s=this.get(0,1),i=t.get(0,1),o=this.get(1,0),a=t.get(1,0),u=this.get(1,1),l=t.get(1,1),h=(n+u)*(r+l),c=(o+u)*r,f=n*(i-l),m=u*(a-r),g=(n+s)*l,d=h+m-g+(s-u)*(a+l),p=f+g,w=c+m,y=h-c+f+(o-n)*(r+i);return e.set(0,0,d),e.set(0,1,p),e.set(1,0,w),e.set(1,1,y),e}strassen3x3(t){t=v.checkMatrix(t);let e=new v(3,3);const n=this.get(0,0),r=this.get(0,1),s=this.get(0,2),i=this.get(1,0),o=this.get(1,1),a=this.get(1,2),u=this.get(2,0),l=this.get(2,1),h=this.get(2,2),c=t.get(0,0),f=t.get(0,1),m=t.get(0,2),g=t.get(1,0),d=t.get(1,1),p=t.get(1,2),w=t.get(2,0),y=t.get(2,1),b=t.get(2,2),x=(n-i)*(-f+d),M=(-n+i+o)*(c-f+d),S=(i+o)*(-c+f),A=n*c,E=(-n+u+l)*(c-m+p),I=(-n+u)*(m-p),N=(u+l)*(-c+m),R=(-s+l+h)*(d+w-y),k=(s-h)*(d-y),C=s*w,T=(l+h)*(-w+y),P=(-s+o+a)*(p+w-b),D=(s-a)*(p-b),U=(o+a)*(-w+b),O=A+C+r*g,z=(n+r+s-i-o-l-h)*d+M+S+A+R+C+T,B=A+E+N+(n+r+s-o-a-u-l)*p+C+P+U,_=x+o*(-c+f+g-d-p-w+b)+M+A+C+P+D,L=x+M+S+A+a*y,$=C+P+D+U+i*m,G=A+E+I+l*(-c+m+g-d-p-w+y)+R+k+C,F=R+k+C+T+u*f,j=A+E+I+N+h*b;return e.set(0,0,O),e.set(0,1,z),e.set(0,2,B),e.set(1,0,_),e.set(1,1,L),e.set(1,2,$),e.set(2,0,G),e.set(2,1,F),e.set(2,2,j),e}mmulStrassen(t){t=v.checkMatrix(t);let e=this.clone(),n=e.rows,r=e.columns,s=t.rows,i=t.columns;function o(t,e,n){let r=t.rows,s=t.columns;if(r===e&&s===n)return t;{let r=x.zeros(e,n);return r=r.setSubMatrix(t,0,0),r}}r!==s&&console.warn(`Multiplying ${n} x ${r} and ${s} x ${i} matrix: dimensions do not match.`);let a=Math.max(n,s),u=Math.max(r,i);return e=o(e,a,u),function t(e,n,r,s){if(r<=512||s<=512)return e.mmul(n);r%2==1&&s%2==1?(e=o(e,r+1,s+1),n=o(n,r+1,s+1)):r%2==1?(e=o(e,r+1,s),n=o(n,r+1,s)):s%2==1&&(e=o(e,r,s+1),n=o(n,r,s+1));let i=parseInt(e.rows/2,10),a=parseInt(e.columns/2,10),u=e.subMatrix(0,i-1,0,a-1),l=n.subMatrix(0,i-1,0,a-1),h=e.subMatrix(0,i-1,a,e.columns-1),c=n.subMatrix(0,i-1,a,n.columns-1),f=e.subMatrix(i,e.rows-1,0,a-1),m=n.subMatrix(i,n.rows-1,0,a-1),g=e.subMatrix(i,e.rows-1,a,e.columns-1),d=n.subMatrix(i,n.rows-1,a,n.columns-1),p=t(x.add(u,g),x.add(l,d),i,a),w=t(x.add(f,g),l,i,a),y=t(u,x.sub(c,d),i,a),b=t(g,x.sub(m,l),i,a),M=t(x.add(u,h),d,i,a),v=t(x.sub(f,u),x.add(l,c),i,a),S=t(x.sub(h,g),x.add(m,d),i,a),A=x.add(p,b);A.sub(M),A.add(S);let E=x.add(y,M),I=x.add(w,b),N=x.sub(p,w);N.add(y),N.add(v);let R=x.zeros(2*A.rows,2*A.columns);return R=R.setSubMatrix(A,0,0),R=R.setSubMatrix(E,A.rows,0),R=R.setSubMatrix(I,0,A.columns),R=R.setSubMatrix(N,A.rows,A.columns),R.subMatrix(0,r-1,0,s-1)}(e,t=o(t,a,u),a,u)}scaleRows(t={}){if("object"!=typeof t)throw new TypeError("options must be an object");const{min:e=0,max:n=1}=t;if(!Number.isFinite(e))throw new TypeError("min must be a number");if(!Number.isFinite(n))throw new TypeError("max must be a number");if(e>=n)throw new RangeError("min must be smaller than max");let r=new v(this.rows,this.columns);for(let t=0;t<this.rows;t++){const i=this.getRow(t);i.length>0&&s(i,{min:e,max:n,output:i}),r.setRow(t,i)}return r}scaleColumns(t={}){if("object"!=typeof t)throw new TypeError("options must be an object");const{min:e=0,max:n=1}=t;if(!Number.isFinite(e))throw new TypeError("min must be a number");if(!Number.isFinite(n))throw new TypeError("max must be a number");if(e>=n)throw new RangeError("min must be smaller than max");let r=new v(this.rows,this.columns);for(let t=0;t<this.columns;t++){const i=this.getColumn(t);i.length&&s(i,{min:e,max:n,output:i}),r.setColumn(t,i)}return r}flipRows(){const t=Math.ceil(this.columns/2);for(let e=0;e<this.rows;e++)for(let n=0;n<t;n++){let t=this.get(e,n),r=this.get(e,this.columns-1-n);this.set(e,n,r),this.set(e,this.columns-1-n,t)}return this}flipColumns(){const t=Math.ceil(this.rows/2);for(let e=0;e<this.columns;e++)for(let n=0;n<t;n++){let t=this.get(n,e),r=this.get(this.rows-1-n,e);this.set(n,e,r),this.set(this.rows-1-n,e,t)}return this}kroneckerProduct(t){t=v.checkMatrix(t);let e=this.rows,n=this.columns,r=t.rows,s=t.columns,i=new v(e*r,n*s);for(let o=0;o<e;o++)for(let e=0;e<n;e++)for(let n=0;n<r;n++)for(let a=0;a<s;a++)i.set(r*o+n,s*e+a,this.get(o,e)*t.get(n,a));return i}kroneckerSum(t){if(t=v.checkMatrix(t),!this.isSquare()||!t.isSquare())throw new Error("Kronecker Sum needs two Square Matrices");let e=this.rows,n=t.rows,r=this.kroneckerProduct(v.eye(n,n)),s=v.eye(e,e).kroneckerProduct(t);return r.add(s)}transpose(){let t=new v(this.columns,this.rows);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)t.set(n,e,this.get(e,n));return t}sortRows(t=M){for(let e=0;e<this.rows;e++)this.setRow(e,this.getRow(e).sort(t));return this}sortColumns(t=M){for(let e=0;e<this.columns;e++)this.setColumn(e,this.getColumn(e).sort(t));return this}subMatrix(t,e,n,r){p(this,t,e,n,r);let s=new v(e-t+1,r-n+1);for(let i=t;i<=e;i++)for(let e=n;e<=r;e++)s.set(i-t,e-n,this.get(i,e));return s}subMatrixRow(t,e,n){if(void 0===e&&(e=0),void 0===n&&(n=this.columns-1),e>n||e<0||e>=this.columns||n<0||n>=this.columns)throw new RangeError("Argument out of range");let r=new v(t.length,n-e+1);for(let s=0;s<t.length;s++)for(let i=e;i<=n;i++){if(t[s]<0||t[s]>=this.rows)throw new RangeError(`Row index out of range: ${t[s]}`);r.set(s,i-e,this.get(t[s],i))}return r}subMatrixColumn(t,e,n){if(void 0===e&&(e=0),void 0===n&&(n=this.rows-1),e>n||e<0||e>=this.rows||n<0||n>=this.rows)throw new RangeError("Argument out of range");let r=new v(n-e+1,t.length);for(let s=0;s<t.length;s++)for(let i=e;i<=n;i++){if(t[s]<0||t[s]>=this.columns)throw new RangeError(`Column index out of range: ${t[s]}`);r.set(i-e,s,this.get(i,t[s]))}return r}setSubMatrix(t,e,n){if((t=v.checkMatrix(t)).isEmpty())return this;p(this,e,e+t.rows-1,n,n+t.columns-1);for(let r=0;r<t.rows;r++)for(let s=0;s<t.columns;s++)this.set(e+r,n+s,t.get(r,s));return this}selection(t,e){g(this,t),d(this,e);let n=new v(t.length,e.length);for(let r=0;r<t.length;r++){let s=t[r];for(let t=0;t<e.length;t++){let i=e[t];n.set(r,t,this.get(s,i))}}return n}trace(){let t=Math.min(this.rows,this.columns),e=0;for(let n=0;n<t;n++)e+=this.get(n,n);return e}clone(){return this.constructor.copy(this,new v(this.rows,this.columns))}static copy(t,e){for(const[n,r,s]of t.entries())e.set(n,r,s);return e}sum(t){switch(t){case"row":return function(t){let e=w(t.rows);for(let n=0;n<t.rows;++n)for(let r=0;r<t.columns;++r)e[n]+=t.get(n,r);return e}(this);case"column":return function(t){let e=w(t.columns);for(let n=0;n<t.rows;++n)for(let r=0;r<t.columns;++r)e[r]+=t.get(n,r);return e}(this);case void 0:return function(t){let e=0;for(let n=0;n<t.rows;n++)for(let r=0;r<t.columns;r++)e+=t.get(n,r);return e}(this);default:throw new Error(`invalid option: ${t}`)}}product(t){switch(t){case"row":return function(t){let e=w(t.rows,1);for(let n=0;n<t.rows;++n)for(let r=0;r<t.columns;++r)e[n]*=t.get(n,r);return e}(this);case"column":return function(t){let e=w(t.columns,1);for(let n=0;n<t.rows;++n)for(let r=0;r<t.columns;++r)e[r]*=t.get(n,r);return e}(this);case void 0:return function(t){let e=1;for(let n=0;n<t.rows;n++)for(let r=0;r<t.columns;r++)e*=t.get(n,r);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:n=!0,mean:s=this.mean(t)}=e;if("boolean"!=typeof n)throw new TypeError("unbiased must be a boolean");switch(t){case"row":if(!r.isAnyArray(s))throw new TypeError("mean must be an array");return function(t,e,n){const r=t.rows,s=t.columns,i=[];for(let o=0;o<r;o++){let r=0,a=0,u=0;for(let e=0;e<s;e++)u=t.get(o,e)-n[o],r+=u,a+=u*u;e?i.push((a-r*r/s)/(s-1)):i.push((a-r*r/s)/s)}return i}(this,n,s);case"column":if(!r.isAnyArray(s))throw new TypeError("mean must be an array");return function(t,e,n){const r=t.rows,s=t.columns,i=[];for(let o=0;o<s;o++){let s=0,a=0,u=0;for(let e=0;e<r;e++)u=t.get(e,o)-n[o],s+=u,a+=u*u;e?i.push((a-s*s/r)/(r-1)):i.push((a-s*s/r)/r)}return i}(this,n,s);case void 0:if("number"!=typeof s)throw new TypeError("mean must be a number");return function(t,e,n){const r=t.rows,s=t.columns,i=r*s;let o=0,a=0,u=0;for(let e=0;e<r;e++)for(let r=0;r<s;r++)u=t.get(e,r)-n,o+=u,a+=u*u;return e?(a-o*o/i)/(i-1):(a-o*o/i)/i}(this,n,s);default:throw new Error(`invalid option: ${t}`)}}standardDeviation(t,e){"object"==typeof t&&(e=t,t=void 0);const n=this.variance(t,e);if(void 0===t)return Math.sqrt(n);for(let t=0;t<n.length;t++)n[t]=Math.sqrt(n[t]);return n}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:n=this.mean(t)}=e;switch(t){case"row":if(!r.isAnyArray(n))throw new TypeError("center must be an array");return function(t,e){for(let n=0;n<t.rows;n++)for(let r=0;r<t.columns;r++)t.set(n,r,t.get(n,r)-e[n])}(this,n),this;case"column":if(!r.isAnyArray(n))throw new TypeError("center must be an array");return function(t,e){for(let n=0;n<t.rows;n++)for(let r=0;r<t.columns;r++)t.set(n,r,t.get(n,r)-e[r])}(this,n),this;case void 0:if("number"!=typeof n)throw new TypeError("center must be a number");return function(t,e){for(let n=0;n<t.rows;n++)for(let r=0;r<t.columns;r++)t.set(n,r,t.get(n,r)-e)}(this,n),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 n=e.scale;switch(t){case"row":if(void 0===n)n=function(t){const e=[];for(let n=0;n<t.rows;n++){let r=0;for(let e=0;e<t.columns;e++)r+=t.get(n,e)**2/(t.columns-1);e.push(Math.sqrt(r))}return e}(this);else if(!r.isAnyArray(n))throw new TypeError("scale must be an array");return function(t,e){for(let n=0;n<t.rows;n++)for(let r=0;r<t.columns;r++)t.set(n,r,t.get(n,r)/e[n])}(this,n),this;case"column":if(void 0===n)n=function(t){const e=[];for(let n=0;n<t.columns;n++){let r=0;for(let e=0;e<t.rows;e++)r+=t.get(e,n)**2/(t.rows-1);e.push(Math.sqrt(r))}return e}(this);else if(!r.isAnyArray(n))throw new TypeError("scale must be an array");return function(t,e){for(let n=0;n<t.rows;n++)for(let r=0;r<t.columns;r++)t.set(n,r,t.get(n,r)/e[r])}(this,n),this;case void 0:if(void 0===n)n=function(t){const e=t.size-1;let n=0;for(let r=0;r<t.columns;r++)for(let s=0;s<t.rows;s++)n+=t.get(s,r)**2/e;return Math.sqrt(n)}(this);else if("number"!=typeof n)throw new TypeError("scale must be a number");return function(t,e){for(let n=0;n<t.rows;n++)for(let r=0;r<t.columns;r++)t.set(n,r,t.get(n,r)/e)}(this,n),this;default:throw new Error(`invalid option: ${t}`)}}toString(t){return a(this,t)}[Symbol.iterator](){return this.entries()}*entries(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)yield[t,e,this.get(t,e)]}*values(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)yield this.get(t,e)}}function M(t,e){return t-e}x.prototype.klass="Matrix","undefined"!=typeof Symbol&&(x.prototype[Symbol.for("nodejs.util.inspect.custom")]=function(){return a(this)}),x.random=x.rand,x.randomInt=x.randInt,x.diagonal=x.diag,x.prototype.diagonal=x.prototype.diag,x.identity=x.eye,x.prototype.negate=x.prototype.neg,x.prototype.tensorProduct=x.prototype.kroneckerProduct;class v extends x{data;#t(t,e){if(this.data=[],!(Number.isInteger(e)&&e>=0))throw new TypeError("nColumns must be a positive integer");for(let n=0;n<t;n++)this.data.push(new Float64Array(e));this.rows=t,this.columns=e}constructor(t,e){if(super(),v.isMatrix(t))this.#t(t.rows,t.columns),v.copy(t,this);else if(Number.isInteger(t)&&t>=0)this.#t(t,e);else{if(!r.isAnyArray(t))throw new TypeError("First argument must be a positive number or an array");{const n=t;if("number"!=typeof(e=(t=n.length)?n[0].length:0))throw new TypeError("Data must be a 2D array with at least one element");this.data=[];for(let r=0;r<t;r++){if(n[r].length!==e)throw new RangeError("Inconsistent array dimensions");if(!n[r].every((t=>"number"==typeof t)))throw new TypeError("Input data contains non-numeric values");this.data.push(Float64Array.from(n[r]))}this.rows=t,this.columns=e}}}set(t,e,n){return this.data[t][e]=n,this}get(t,e){return this.data[t][e]}removeRow(t){return h(this,t),this.data.splice(t,1),this.rows-=1,this}addRow(t,e){return void 0===e&&(e=t,t=this.rows),h(this,t,!0),e=Float64Array.from(f(this,e)),this.data.splice(t,0,e),this.rows+=1,this}removeColumn(t){c(this,t);for(let e=0;e<this.rows;e++){const n=new Float64Array(this.columns-1);for(let r=0;r<t;r++)n[r]=this.data[e][r];for(let r=t+1;r<this.columns;r++)n[r-1]=this.data[e][r];this.data[e]=n}return this.columns-=1,this}addColumn(t,e){void 0===e&&(e=t,t=this.columns),c(this,t,!0),e=m(this,e);for(let n=0;n<this.rows;n++){const r=new Float64Array(this.columns+1);let s=0;for(;s<t;s++)r[s]=this.data[n][s];for(r[s++]=e[n];s<this.columns+1;s++)r[s]=this.data[n][s-1];this.data[n]=r}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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)+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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)+t.get(e,n));return this},t.add=function(t,n){return new e(t).add(n)},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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)-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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)-t.get(e,n));return this},t.sub=function(t,n){return new e(t).sub(n)},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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)*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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)*t.get(e,n));return this},t.mul=function(t,n){return new e(t).mul(n)},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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)/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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)/t.get(e,n));return this},t.div=function(t,n){return new e(t).div(n)},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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)%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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)%t.get(e,n));return this},t.mod=function(t,n){return new e(t).mod(n)},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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)&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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)&t.get(e,n));return this},t.and=function(t,n){return new e(t).and(n)},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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)|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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)|t.get(e,n));return this},t.or=function(t,n){return new e(t).or(n)},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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)^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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)^t.get(e,n));return this},t.xor=function(t,n){return new e(t).xor(n)},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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)<<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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)<<t.get(e,n));return this},t.leftShift=function(t,n){return new e(t).leftShift(n)},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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)>>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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)>>t.get(e,n));return this},t.signPropagatingRightShift=function(t,n){return new e(t).signPropagatingRightShift(n)},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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)>>>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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)>>>t.get(e,n));return this},t.rightShift=function(t,n){return new e(t).rightShift(n)},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,n){return new e(t).pow(n)},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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)**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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)**t.get(e,n));return this}}(x,v);class S extends x{#e;get size(){return this.#e.size}get rows(){return this.#e.rows}get columns(){return this.#e.columns}get diagonalSize(){return this.rows}static isSymmetricMatrix(t){return v.isMatrix(t)&&"SymmetricMatrix"===t.klassType}static zeros(t){return new this(t)}static ones(t){return new this(t).fill(1)}constructor(t){if(super(),v.isMatrix(t)){if(!t.isSymmetric())throw new TypeError("not symmetric data");this.#e=v.copy(t,new v(t.rows,t.rows))}else if(Number.isInteger(t)&&t>=0)this.#e=new v(t,t);else if(this.#e=new v(t),!this.isSymmetric())throw new TypeError("not symmetric data")}clone(){const t=new S(this.diagonalSize);for(const[e,n,r]of this.upperRightEntries())t.set(e,n,r);return t}toMatrix(){return new v(this)}get(t,e){return this.#e.get(t,e)}set(t,e,n){return this.#e.set(t,e,n),this.#e.set(e,t,n),this}removeCross(t){return this.#e.removeRow(t),this.#e.removeColumn(t),this}addCross(t,e){void 0===e&&(e=t,t=this.diagonalSize);const n=e.slice();return n.splice(t,1),this.#e.addRow(t,n),this.#e.addColumn(t,e),this}applyMask(t){if(t.length!==this.diagonalSize)throw new RangeError("Mask size do not match with matrix size");const e=[];for(const[n,r]of t.entries())r||e.push(n);e.reverse();for(const t of e)this.removeCross(t);return this}toCompact(){const{diagonalSize:t}=this,e=new Array(t*(t+1)/2);for(let n=0,r=0,s=0;s<e.length;s++)e[s]=this.get(r,n),++n>=t&&(n=++r);return e}static fromCompact(t){const e=t.length,n=(Math.sqrt(8*e+1)-1)/2;if(!Number.isInteger(n))throw new TypeError(`This array is not a compact representation of a Symmetric Matrix, ${JSON.stringify(t)}`);const r=new S(n);for(let s=0,i=0,o=0;o<e;o++)r.set(s,i,t[o]),++s>=n&&(s=++i);return r}*upperRightEntries(){for(let t=0,e=0;t<this.diagonalSize;void 0){const n=this.get(t,e);yield[t,e,n],++e>=this.diagonalSize&&(e=++t)}}*upperRightValues(){for(let t=0,e=0;t<this.diagonalSize;void 0){const n=this.get(t,e);yield n,++e>=this.diagonalSize&&(e=++t)}}}S.prototype.klassType="SymmetricMatrix";class A extends S{static isDistanceMatrix(t){return S.isSymmetricMatrix(t)&&"DistanceMatrix"===t.klassSubType}constructor(t){if(super(t),!this.isDistance())throw new TypeError("Provided arguments do no produce a distance matrix")}set(t,e,n){return t===e&&(n=0),super.set(t,e,n)}addCross(t,e){return void 0===e&&(e=t,t=this.diagonalSize),(e=e.slice())[t]=0,super.addCross(t,e)}toSymmetricMatrix(){return new S(this)}clone(){const t=new A(this.diagonalSize);for(const[e,n,r]of this.upperRightEntries())e!==n&&t.set(e,n,r);return t}toCompact(){const{diagonalSize:t}=this,e=new Array((t-1)*t/2);for(let n=1,r=0,s=0;s<e.length;s++)e[s]=this.get(r,n),++n>=t&&(n=1+ ++r);return e}static fromCompact(t){const e=t.length;if(0===e)return new this(0);const n=(Math.sqrt(8*e+1)+1)/2;if(!Number.isInteger(n))throw new TypeError(`This array is not a compact representation of a DistanceMatrix, ${JSON.stringify(t)}`);const r=new this(n);for(let s=1,i=0,o=0;o<e;o++)r.set(s,i,t[o]),++s>=n&&(s=1+ ++i);return r}}A.prototype.klassSubType="DistanceMatrix";class E extends x{constructor(t,e,n){super(),this.matrix=t,this.rows=e,this.columns=n}}class I extends E{constructor(t,e,n){g(t,e),d(t,n),super(t,e.length,n.length),this.rowIndices=e,this.columnIndices=n}set(t,e,n){return this.matrix.set(this.rowIndices[t],this.columnIndices[e],n),this}get(t,e){return this.matrix.get(this.rowIndices[t],this.columnIndices[e])}}class N extends x{constructor(t,e={}){const{rows:n=1}=e;if(t.length%n!=0)throw new Error("the data length is not divisible by the number of rows");super(),this.rows=n,this.columns=t.length/n,this.data=t}set(t,e,n){let r=this._calculateIndex(t,e);return this.data[r]=n,this}get(t,e){let n=this._calculateIndex(t,e);return this.data[n]}_calculateIndex(t,e){return t*this.columns+e}}class R extends x{constructor(t){super(),this.data=t,this.rows=t.length,this.columns=t[0].length}set(t,e,n){return this.data[t][e]=n,this}get(t,e){return this.data[t][e]}}class k{constructor(t){let e,n,r,s,i,o,a,u,l,h=(t=R.checkMatrix(t)).clone(),c=h.rows,f=h.columns,m=new Float64Array(c),g=1;for(e=0;e<c;e++)m[e]=e;for(u=new Float64Array(c),n=0;n<f;n++){for(e=0;e<c;e++)u[e]=h.get(e,n);for(e=0;e<c;e++){for(l=Math.min(e,n),i=0,r=0;r<l;r++)i+=h.get(e,r)*u[r];u[e]-=i,h.set(e,n,u[e])}for(s=n,e=n+1;e<c;e++)Math.abs(u[e])>Math.abs(u[s])&&(s=e);if(s!==n){for(r=0;r<f;r++)o=h.get(s,r),h.set(s,r,h.get(n,r)),h.set(n,r,o);a=m[s],m[s]=m[n],m[n]=a,g=-g}if(n<c&&0!==h.get(n,n))for(e=n+1;e<c;e++)h.set(e,n,h.get(e,n)/h.get(n,n))}this.LU=h,this.pivotVector=m,this.pivotSign=g}isSingular(){let t=this.LU,e=t.columns;for(let n=0;n<e;n++)if(0===t.get(n,n))return!0;return!1}solve(t){t=v.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 n,r,s,i=t.columns,o=t.subMatrixRow(this.pivotVector,0,i-1),a=e.columns;for(s=0;s<a;s++)for(n=s+1;n<a;n++)for(r=0;r<i;r++)o.set(n,r,o.get(n,r)-o.get(s,r)*e.get(n,s));for(s=a-1;s>=0;s--){for(r=0;r<i;r++)o.set(s,r,o.get(s,r)/e.get(s,s));for(n=0;n<s;n++)for(r=0;r<i;r++)o.set(n,r,o.get(n,r)-o.get(s,r)*e.get(n,s))}return o}get determinant(){let t=this.LU;if(!t.isSquare())throw new Error("Matrix must be square");let e=this.pivotSign,n=t.columns;for(let r=0;r<n;r++)e*=t.get(r,r);return e}get lowerTriangularMatrix(){let t=this.LU,e=t.rows,n=t.columns,r=new v(e,n);for(let s=0;s<e;s++)for(let e=0;e<n;e++)s>e?r.set(s,e,t.get(s,e)):s===e?r.set(s,e,1):r.set(s,e,0);return r}get upperTriangularMatrix(){let t=this.LU,e=t.rows,n=t.columns,r=new v(e,n);for(let s=0;s<e;s++)for(let e=0;e<n;e++)s<=e?r.set(s,e,t.get(s,e)):r.set(s,e,0);return r}get pivotPermutationVector(){return Array.from(this.pivotVector)}}function C(t,e){let n=0;return Math.abs(t)>Math.abs(e)?(n=e/t,Math.abs(t)*Math.sqrt(1+n*n)):0!==e?(n=t/e,Math.abs(e)*Math.sqrt(1+n*n)):0}class T{constructor(t){let e,n,r,s,i=(t=R.checkMatrix(t)).clone(),o=t.rows,a=t.columns,u=new Float64Array(a);for(r=0;r<a;r++){let t=0;for(e=r;e<o;e++)t=C(t,i.get(e,r));if(0!==t){for(i.get(r,r)<0&&(t=-t),e=r;e<o;e++)i.set(e,r,i.get(e,r)/t);for(i.set(r,r,i.get(r,r)+1),n=r+1;n<a;n++){for(s=0,e=r;e<o;e++)s+=i.get(e,r)*i.get(e,n);for(s=-s/i.get(r,r),e=r;e<o;e++)i.set(e,n,i.get(e,n)+s*i.get(e,r))}}u[r]=-t}this.QR=i,this.Rdiag=u}solve(t){t=v.checkMatrix(t);let e=this.QR,n=e.rows;if(t.rows!==n)throw new Error("Matrix row dimensions must agree");if(!this.isFullRank())throw new Error("Matrix is rank deficient");let r,s,i,o,a=t.columns,u=t.clone(),l=e.columns;for(i=0;i<l;i++)for(s=0;s<a;s++){for(o=0,r=i;r<n;r++)o+=e.get(r,i)*u.get(r,s);for(o=-o/e.get(i,i),r=i;r<n;r++)u.set(r,s,u.get(r,s)+o*e.get(r,i))}for(i=l-1;i>=0;i--){for(s=0;s<a;s++)u.set(i,s,u.get(i,s)/this.Rdiag[i]);for(r=0;r<i;r++)for(s=0;s<a;s++)u.set(r,s,u.get(r,s)-u.get(i,s)*e.get(r,i))}return u.subMatrix(0,l-1,0,a-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,n=this.QR,r=n.columns,s=new v(r,r);for(t=0;t<r;t++)for(e=0;e<r;e++)t<e?s.set(t,e,n.get(t,e)):t===e?s.set(t,e,this.Rdiag[t]):s.set(t,e,0);return s}get orthogonalMatrix(){let t,e,n,r,s=this.QR,i=s.rows,o=s.columns,a=new v(i,o);for(n=o-1;n>=0;n--){for(t=0;t<i;t++)a.set(t,n,0);for(a.set(n,n,1),e=n;e<o;e++)if(0!==s.get(n,n)){for(r=0,t=n;t<i;t++)r+=s.get(t,n)*a.get(t,e);for(r=-r/s.get(n,n),t=n;t<i;t++)a.set(t,e,a.get(t,e)+r*s.get(t,n))}}return a}}class P{constructor(t,e={}){if((t=R.checkMatrix(t)).isEmpty())throw new Error("Matrix must be non-empty");let n=t.rows,r=t.columns;const{computeLeftSingularVectors:s=!0,computeRightSingularVectors:i=!0,autoTranspose:o=!1}=e;let a,u=Boolean(s),l=Boolean(i),h=!1;if(n<r)if(o){a=t.transpose(),n=a.rows,r=a.columns,h=!0;let e=u;u=l,l=e}else a=t.clone(),console.warn("Computing SVD on a matrix with more columns than rows. Consider enabling autoTranspose");else a=t.clone();let c=Math.min(n,r),f=Math.min(n+1,r),m=new Float64Array(f),g=new v(n,c),d=new v(r,r),p=new Float64Array(r),w=new Float64Array(n),y=new Float64Array(f);for(let t=0;t<f;t++)y[t]=t;let b=Math.min(n-1,r),x=Math.max(0,Math.min(r-2,n)),M=Math.max(b,x);for(let t=0;t<M;t++){if(t<b){m[t]=0;for(let e=t;e<n;e++)m[t]=C(m[t],a.get(e,t));if(0!==m[t]){a.get(t,t)<0&&(m[t]=-m[t]);for(let e=t;e<n;e++)a.set(e,t,a.get(e,t)/m[t]);a.set(t,t,a.get(t,t)+1)}m[t]=-m[t]}for(let e=t+1;e<r;e++){if(t<b&&0!==m[t]){let r=0;for(let s=t;s<n;s++)r+=a.get(s,t)*a.get(s,e);r=-r/a.get(t,t);for(let s=t;s<n;s++)a.set(s,e,a.get(s,e)+r*a.get(s,t))}p[e]=a.get(t,e)}if(u&&t<b)for(let e=t;e<n;e++)g.set(e,t,a.get(e,t));if(t<x){p[t]=0;for(let e=t+1;e<r;e++)p[t]=C(p[t],p[e]);if(0!==p[t]){p[t+1]<0&&(p[t]=0-p[t]);for(let e=t+1;e<r;e++)p[e]/=p[t];p[t+1]+=1}if(p[t]=-p[t],t+1<n&&0!==p[t]){for(let e=t+1;e<n;e++)w[e]=0;for(let e=t+1;e<n;e++)for(let n=t+1;n<r;n++)w[e]+=p[n]*a.get(e,n);for(let e=t+1;e<r;e++){let r=-p[e]/p[t+1];for(let s=t+1;s<n;s++)a.set(s,e,a.get(s,e)+r*w[s])}}if(l)for(let e=t+1;e<r;e++)d.set(e,t,p[e])}}let S=Math.min(r,n+1);if(b<r&&(m[b]=a.get(b,b)),n<S&&(m[S-1]=0),x+1<S&&(p[x]=a.get(x,S-1)),p[S-1]=0,u){for(let t=b;t<c;t++){for(let e=0;e<n;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 r=0;for(let s=t;s<n;s++)r+=g.get(s,t)*g.get(s,e);r=-r/g.get(t,t);for(let s=t;s<n;s++)g.set(s,e,g.get(s,e)+r*g.get(s,t))}for(let e=t;e<n;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<n;e++)g.set(e,t,0);g.set(t,t,1)}}if(l)for(let t=r-1;t>=0;t--){if(t<x&&0!==p[t])for(let e=t+1;e<r;e++){let n=0;for(let s=t+1;s<r;s++)n+=d.get(s,t)*d.get(s,e);n=-n/d.get(t+1,t);for(let s=t+1;s<r;s++)d.set(s,e,d.get(s,e)+n*d.get(s,t))}for(let e=0;e<r;e++)d.set(e,t,0);d.set(t,t,1)}let A=S-1,E=Number.EPSILON;for(;S>0;){let t,e;for(t=S-2;t>=-1&&-1!==t;t--){const e=Number.MIN_VALUE+E*Math.abs(m[t]+Math.abs(m[t+1]));if(Math.abs(p[t])<=e||Number.isNaN(p[t])){p[t]=0;break}}if(t===S-2)e=4;else{let n;for(n=S-1;n>=t&&n!==t;n--){let e=(n!==S?Math.abs(p[n]):0)+(n!==t+1?Math.abs(p[n-1]):0);if(Math.abs(m[n])<=E*e){m[n]=0;break}}n===t?e=3:n===S-1?e=1:(e=2,t=n)}switch(t++,e){case 1:{let e=p[S-2];p[S-2]=0;for(let n=S-2;n>=t;n--){let s=C(m[n],e),i=m[n]/s,o=e/s;if(m[n]=s,n!==t&&(e=-o*p[n-1],p[n-1]=i*p[n-1]),l)for(let t=0;t<r;t++)s=i*d.get(t,n)+o*d.get(t,S-1),d.set(t,S-1,-o*d.get(t,n)+i*d.get(t,S-1)),d.set(t,n,s)}break}case 2:{let e=p[t-1];p[t-1]=0;for(let r=t;r<S;r++){let s=C(m[r],e),i=m[r]/s,o=e/s;if(m[r]=s,e=-o*p[r],p[r]=i*p[r],u)for(let e=0;e<n;e++)s=i*g.get(e,r)+o*g.get(e,t-1),g.set(e,t-1,-o*g.get(e,r)+i*g.get(e,t-1)),g.set(e,r,s)}break}case 3:{const e=Math.max(Math.abs(m[S-1]),Math.abs(m[S-2]),Math.abs(p[S-2]),Math.abs(m[t]),Math.abs(p[t])),s=m[S-1]/e,i=m[S-2]/e,o=p[S-2]/e,a=m[t]/e,h=p[t]/e,c=((i+s)*(i-s)+o*o)/2,f=s*o*(s*o);let w=0;0===c&&0===f||(w=c<0?0-Math.sqrt(c*c+f):Math.sqrt(c*c+f),w=f/(c+w));let y=(a+s)*(a-s)+w,b=a*h;for(let e=t;e<S-1;e++){let s=C(y,b);0===s&&(s=Number.MIN_VALUE);let i=y/s,o=b/s;if(e!==t&&(p[e-1]=s),y=i*m[e]+o*p[e],p[e]=i*p[e]-o*m[e],b=o*m[e+1],m[e+1]=i*m[e+1],l)for(let t=0;t<r;t++)s=i*d.get(t,e)+o*d.get(t,e+1),d.set(t,e+1,-o*d.get(t,e)+i*d.get(t,e+1)),d.set(t,e,s);if(s=C(y,b),0===s&&(s=Number.MIN_VALUE),i=y/s,o=b/s,m[e]=s,y=i*p[e]+o*m[e+1],m[e+1]=-o*p[e]+i*m[e+1],b=o*p[e+1],p[e+1]=i*p[e+1],u&&e<n-1)for(let t=0;t<n;t++)s=i*g.get(t,e)+o*g.get(t,e+1),g.set(t,e+1,-o*g.get(t,e)+i*g.get(t,e+1)),g.set(t,e,s)}p[S-2]=y;break}case 4:if(m[t]<=0&&(m[t]=m[t]<0?-m[t]:0,l))for(let e=0;e<=A;e++)d.set(e,t,-d.get(e,t));for(;t<A&&!(m[t]>=m[t+1]);){let e=m[t];if(m[t]=m[t+1],m[t+1]=e,l&&t<r-1)for(let n=0;n<r;n++)e=d.get(n,t+1),d.set(n,t+1,d.get(n,t)),d.set(n,t,e);if(u&&t<n-1)for(let r=0;r<n;r++)e=g.get(r,t+1),g.set(r,t+1,g.get(r,t)),g.set(r,t,e);t++}S--}}if(h){let t=d;d=g,g=t}this.m=n,this.n=r,this.s=m,this.U=g,this.V=d}solve(t){let e=t,n=this.threshold,r=this.s.length,s=v.zeros(r,r);for(let t=0;t<r;t++)Math.abs(this.s[t])<=n?s.set(t,t,0):s.set(t,t,1/this.s[t]);let i=this.U,o=this.rightSingularVectors,a=o.mmul(s),u=o.rows,l=i.rows,h=v.zeros(u,l);for(let t=0;t<u;t++)for(let e=0;e<l;e++){let n=0;for(let s=0;s<r;s++)n+=a.get(t,s)*i.get(e,s);h.set(t,e,n)}return h.mmul(e)}solveForDiagonal(t){return this.solve(v.diag(t))}inverse(){let t=this.V,e=this.threshold,n=t.rows,r=t.columns,s=new v(n,this.s.length);for(let i=0;i<n;i++)for(let n=0;n<r;n++)Math.abs(this.s[n])>e&&s.set(i,n,t.get(i,n)/this.s[n]);let i=this.U,o=i.rows,a=i.columns,u=new v(n,o);for(let t=0;t<n;t++)for(let e=0;e<o;e++){let n=0;for(let r=0;r<a;r++)n+=s.get(t,r)*i.get(e,r);u.set(t,e,n)}return u}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,n=this.s;for(let r=0,s=n.length;r<s;r++)n[r]>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 v.diag(this.s)}}function D(t,e,n=!1){return t=R.checkMatrix(t),e=R.checkMatrix(e),n?new P(t).solve(e):t.isSquare()?new k(t).solve(e):new T(t).solve(e)}function U(t,e){let n=[];for(let r=0;r<t;r++)r!==e&&n.push(r);return n}function O(t,e,n,r=1e-9,s=1e-9){if(t>s)return new Array(e.rows+1).fill(0);{let t=e.addRow(n,[0]);for(let e=0;e<t.rows;e++)Math.abs(t.get(e,0))<r&&t.set(e,0,0);return t.to1DArray()}}class z{constructor(t,e={}){const{assumeSymmetric:n=!1}=e;if(!(t=R.checkMatrix(t)).isSquare())throw new Error("Matrix is not a square matrix");if(t.isEmpty())throw new Error("Matrix must be non-empty");let r,s,i=t.columns,o=new v(i,i),a=new Float64Array(i),u=new Float64Array(i),l=t,h=!1;if(h=!!n||t.isSymmetric(),h){for(r=0;r<i;r++)for(s=0;s<i;s++)o.set(r,s,l.get(r,s));!function(t,e,n,r){let s,i,o,a,u,l,h,c;for(u=0;u<t;u++)n[u]=r.get(t-1,u);for(a=t-1;a>0;a--){for(c=0,o=0,l=0;l<a;l++)c+=Math.abs(n[l]);if(0===c)for(e[a]=n[a-1],u=0;u<a;u++)n[u]=r.get(a-1,u),r.set(a,u,0),r.set(u,a,0);else{for(l=0;l<a;l++)n[l]/=c,o+=n[l]*n[l];for(s=n[a-1],i=Math.sqrt(o),s>0&&(i=-i),e[a]=c*i,o-=s*i,n[a-1]=s-i,u=0;u<a;u++)e[u]=0;for(u=0;u<a;u++){for(s=n[u],r.set(u,a,s),i=e[u]+r.get(u,u)*s,l=u+1;l<=a-1;l++)i+=r.get(l,u)*n[l],e[l]+=r.get(l,u)*s;e[u]=i}for(s=0,u=0;u<a;u++)e[u]/=o,s+=e[u]*n[u];for(h=s/(o+o),u=0;u<a;u++)e[u]-=h*n[u];for(u=0;u<a;u++){for(s=n[u],i=e[u],l=u;l<=a-1;l++)r.set(l,u,r.get(l,u)-(s*e[l]+i*n[l]));n[u]=r.get(a-1,u),r.set(a,u,0)}}n[a]=o}for(a=0;a<t-1;a++){if(r.set(t-1,a,r.get(a,a)),r.set(a,a,1),o=n[a+1],0!==o){for(l=0;l<=a;l++)n[l]=r.get(l,a+1)/o;for(u=0;u<=a;u++){for(i=0,l=0;l<=a;l++)i+=r.get(l,a+1)*r.get(l,u);for(l=0;l<=a;l++)r.set(l,u,r.get(l,u)-i*n[l])}}for(l=0;l<=a;l++)r.set(l,a+1,0)}for(u=0;u<t;u++)n[u]=r.get(t-1,u),r.set(t-1,u,0);r.set(t-1,t-1,1),e[0]=0}(i,u,a,o),function(t,e,n,r){let s,i,o,a,u,l,h,c,f,m,g,d,p,w,y,b;for(o=1;o<t;o++)e[o-1]=e[o];e[t-1]=0;let x=0,M=0,v=Number.EPSILON;for(l=0;l<t;l++){for(M=Math.max(M,Math.abs(n[l])+Math.abs(e[l])),h=l;h<t&&!(Math.abs(e[h])<=v*M);)h++;if(h>l)do{for(s=n[l],c=(n[l+1]-s)/(2*e[l]),f=C(c,1),c<0&&(f=-f),n[l]=e[l]/(c+f),n[l+1]=e[l]*(c+f),m=n[l+1],i=s-n[l],o=l+2;o<t;o++)n[o]-=i;for(x+=i,c=n[h],g=1,d=g,p=g,w=e[l+1],y=0,b=0,o=h-1;o>=l;o--)for(p=d,d=g,b=y,s=g*e[o],i=g*c,f=C(c,e[o]),e[o+1]=y*f,y=e[o]/f,g=c/f,c=g*n[o]-y*s,n[o+1]=i+y*(g*s+y*n[o]),u=0;u<t;u++)i=r.get(u,o+1),r.set(u,o+1,y*r.get(u,o)+g*i),r.set(u,o,g*r.get(u,o)-y*i);c=-y*b*p*w*e[l]/m,e[l]=y*c,n[l]=g*c}while(Math.abs(e[l])>v*M);n[l]=n[l]+x,e[l]=0}for(o=0;o<t-1;o++){for(u=o,c=n[o],a=o+1;a<t;a++)n[a]<c&&(u=a,c=n[a]);if(u!==o)for(n[u]=n[o],n[o]=c,a=0;a<t;a++)c=r.get(a,o),r.set(a,o,r.get(a,u)),r.set(a,u,c)}}(i,u,a,o)}else{let t=new v(i,i),e=new Float64Array(i);for(s=0;s<i;s++)for(r=0;r<i;r++)t.set(r,s,l.get(r,s));!function(t,e,n,r){let s,i,o,a,u,l,h,c=t-1;for(l=1;l<=c-1;l++){for(h=0,a=l;a<=c;a++)h+=Math.abs(e.get(a,l-1));if(0!==h){for(o=0,a=c;a>=l;a--)n[a]=e.get(a,l-1)/h,o+=n[a]*n[a];for(i=Math.sqrt(o),n[l]>0&&(i=-i),o-=n[l]*i,n[l]=n[l]-i,u=l;u<t;u++){for(s=0,a=c;a>=l;a--)s+=n[a]*e.get(a,u);for(s/=o,a=l;a<=c;a++)e.set(a,u,e.get(a,u)-s*n[a])}for(a=0;a<=c;a++){for(s=0,u=c;u>=l;u--)s+=n[u]*e.get(a,u);for(s/=o,u=l;u<=c;u++)e.set(a,u,e.get(a,u)-s*n[u])}n[l]=h*n[l],e.set(l,l-1,h*i)}}for(a=0;a<t;a++)for(u=0;u<t;u++)r.set(a,u,a===u?1:0);for(l=c-1;l>=1;l--)if(0!==e.get(l,l-1)){for(a=l+1;a<=c;a++)n[a]=e.get(a,l-1);for(u=l;u<=c;u++){for(i=0,a=l;a<=c;a++)i+=n[a]*r.get(a,u);for(i=i/n[l]/e.get(l,l-1),a=l;a<=c;a++)r.set(a,u,r.get(a,u)+i*n[a])}}}(i,t,e,o),function(t,e,n,r,s){let i,o,a,u,l,h,c,f,m,g,d,p,w,y,b,x=t-1,M=t-1,v=Number.EPSILON,S=0,A=0,E=0,I=0,N=0,R=0,k=0,C=0;for(i=0;i<t;i++)for((i<0||i>M)&&(n[i]=s.get(i,i),e[i]=0),o=Math.max(i-1,0);o<t;o++)A+=Math.abs(s.get(i,o));for(;x>=0;){for(u=x;u>0&&(R=Math.abs(s.get(u-1,u-1))+Math.abs(s.get(u,u)),0===R&&(R=A),!(Math.abs(s.get(u,u-1))<v*R));)u--;if(u===x)s.set(x,x,s.get(x,x)+S),n[x]=s.get(x,x),e[x]=0,x--,C=0;else if(u===x-1){if(c=s.get(x,x-1)*s.get(x-1,x),E=(s.get(x-1,x-1)-s.get(x,x))/2,I=E*E+c,k=Math.sqrt(Math.abs(I)),s.set(x,x,s.get(x,x)+S),s.set(x-1,x-1,s.get(x-1,x-1)+S),f=s.get(x,x),I>=0){for(k=E>=0?E+k:E-k,n[x-1]=f+k,n[x]=n[x-1],0!==k&&(n[x]=f-c/k),e[x-1]=0,e[x]=0,f=s.get(x,x-1),R=Math.abs(f)+Math.abs(k),E=f/R,I=k/R,N=Math.sqrt(E*E+I*I),E/=N,I/=N,o=x-1;o<t;o++)k=s.get(x-1,o),s.set(x-1,o,I*k+E*s.get(x,o)),s.set(x,o,I*s.get(x,o)-E*k);for(i=0;i<=x;i++)k=s.get(i,x-1),s.set(i,x-1,I*k+E*s.get(i,x)),s.set(i,x,I*s.get(i,x)-E*k);for(i=0;i<=M;i++)k=r.get(i,x-1),r.set(i,x-1,I*k+E*r.get(i,x)),r.set(i,x,I*r.get(i,x)-E*k)}else n[x-1]=f+E,n[x]=f+E,e[x-1]=k,e[x]=-k;x-=2,C=0}else{if(f=s.get(x,x),m=0,c=0,u<x&&(m=s.get(x-1,x-1),c=s.get(x,x-1)*s.get(x-1,x)),10===C){for(S+=f,i=0;i<=x;i++)s.set(i,i,s.get(i,i)-f);R=Math.abs(s.get(x,x-1))+Math.abs(s.get(x-1,x-2)),f=m=.75*R,c=-.4375*R*R}if(30===C&&(R=(m-f)/2,R=R*R+c,R>0)){for(R=Math.sqrt(R),m<f&&(R=-R),R=f-c/((m-f)/2+R),i=0;i<=x;i++)s.set(i,i,s.get(i,i)-R);S+=R,f=m=c=.964}for(C+=1,l=x-2;l>=u&&(k=s.get(l,l),N=f-k,R=m-k,E=(N*R-c)/s.get(l+1,l)+s.get(l,l+1),I=s.get(l+1,l+1)-k-N-R,N=s.get(l+2,l+1),R=Math.abs(E)+Math.abs(I)+Math.abs(N),E/=R,I/=R,N/=R,l!==u)&&!(Math.abs(s.get(l,l-1))*(Math.abs(I)+Math.abs(N))<v*(Math.abs(E)*(Math.abs(s.get(l-1,l-1))+Math.abs(k)+Math.abs(s.get(l+1,l+1)))));)l--;for(i=l+2;i<=x;i++)s.set(i,i-2,0),i>l+2&&s.set(i,i-3,0);for(a=l;a<=x-1&&(y=a!==x-1,a!==l&&(E=s.get(a,a-1),I=s.get(a+1,a-1),N=y?s.get(a+2,a-1):0,f=Math.abs(E)+Math.abs(I)+Math.abs(N),0!==f&&(E/=f,I/=f,N/=f)),0!==f);a++)if(R=Math.sqrt(E*E+I*I+N*N),E<0&&(R=-R),0!==R){for(a!==l?s.set(a,a-1,-R*f):u!==l&&s.set(a,a-1,-s.get(a,a-1)),E+=R,f=E/R,m=I/R,k=N/R,I/=E,N/=E,o=a;o<t;o++)E=s.get(a,o)+I*s.get(a+1,o),y&&(E+=N*s.get(a+2,o),s.set(a+2,o,s.get(a+2,o)-E*k)),s.set(a,o,s.get(a,o)-E*f),s.set(a+1,o,s.get(a+1,o)-E*m);for(i=0;i<=Math.min(x,a+3);i++)E=f*s.get(i,a)+m*s.get(i,a+1),y&&(E+=k*s.get(i,a+2),s.set(i,a+2,s.get(i,a+2)-E*N)),s.set(i,a,s.get(i,a)-E),s.set(i,a+1,s.get(i,a+1)-E*I);for(i=0;i<=M;i++)E=f*r.get(i,a)+m*r.get(i,a+1),y&&(E+=k*r.get(i,a+2),r.set(i,a+2,r.get(i,a+2)-E*N)),r.set(i,a,r.get(i,a)-E),r.set(i,a+1,r.get(i,a+1)-E*I)}}}if(0!==A){for(x=t-1;x>=0;x--)if(E=n[x],I=e[x],0===I)for(u=x,s.set(x,x,1),i=x-1;i>=0;i--){for(c=s.get(i,i)-E,N=0,o=u;o<=x;o++)N+=s.get(i,o)*s.get(o,x);if(e[i]<0)k=c,R=N;else if(u=i,0===e[i]?s.set(i,x,0!==c?-N/c:-N/(v*A)):(f=s.get(i,i+1),m=s.get(i+1,i),I=(n[i]-E)*(n[i]-E)+e[i]*e[i],h=(f*R-k*N)/I,s.set(i,x,h),s.set(i+1,x,Math.abs(f)>Math.abs(k)?(-N-c*h)/f:(-R-m*h)/k)),h=Math.abs(s.get(i,x)),v*h*h>1)for(o=i;o<=x;o++)s.set(o,x,s.get(o,x)/h)}else if(I<0)for(u=x-1,Math.abs(s.get(x,x-1))>Math.abs(s.get(x-1,x))?(s.set(x-1,x-1,I/s.get(x,x-1)),s.set(x-1,x,-(s.get(x,x)-E)/s.get(x,x-1))):(b=B(0,-s.get(x-1,x),s.get(x-1,x-1)-E,I),s.set(x-1,x-1,b[0]),s.set(x-1,x,b[1])),s.set(x,x-1,0),s.set(x,x,1),i=x-2;i>=0;i--){for(g=0,d=0,o=u;o<=x;o++)g+=s.get(i,o)*s.get(o,x-1),d+=s.get(i,o)*s.get(o,x);if(c=s.get(i,i)-E,e[i]<0)k=c,N=g,R=d;else if(u=i,0===e[i]?(b=B(-g,-d,c,I),s.set(i,x-1,b[0]),s.set(i,x,b[1])):(f=s.get(i,i+1),m=s.get(i+1,i),p=(n[i]-E)*(n[i]-E)+e[i]*e[i]-I*I,w=2*(n[i]-E)*I,0===p&&0===w&&(p=v*A*(Math.abs(c)+Math.abs(I)+Math.abs(f)+Math.abs(m)+Math.abs(k))),b=B(f*N-k*g+I*d,f*R-k*d-I*g,p,w),s.set(i,x-1,b[0]),s.set(i,x,b[1]),Math.abs(f)>Math.abs(k)+Math.abs(I)?(s.set(i+1,x-1,(-g-c*s.get(i,x-1)+I*s.get(i,x))/f),s.set(i+1,x,(-d-c*s.get(i,x)-I*s.get(i,x-1))/f)):(b=B(-N-m*s.get(i,x-1),-R-m*s.get(i,x),k,I),s.set(i+1,x-1,b[0]),s.set(i+1,x,b[1]))),h=Math.max(Math.abs(s.get(i,x-1)),Math.abs(s.get(i,x))),v*h*h>1)for(o=i;o<=x;o++)s.set(o,x-1,s.get(o,x-1)/h),s.set(o,x,s.get(o,x)/h)}for(i=0;i<t;i++)if(i<0||i>M)for(o=i;o<t;o++)r.set(i,o,s.get(i,o));for(o=t-1;o>=0;o--)for(i=0;i<=M;i++){for(k=0,a=0;a<=Math.min(o,M);a++)k+=r.get(i,a)*s.get(a,o);r.set(i,o,k)}}}(i,u,a,o,t)}this.n=i,this.e=u,this.d=a,this.V=o}get realEigenvalues(){return Array.from(this.d)}get imaginaryEigenvalues(){return Array.from(this.e)}get eigenvectorMatrix(){return this.V}get diagonalMatrix(){let t,e,n=this.n,r=this.e,s=this.d,i=new v(n,n);for(t=0;t<n;t++){for(e=0;e<n;e++)i.set(t,e,0);i.set(t,t,s[t]),r[t]>0?i.set(t,t+1,r[t]):r[t]<0&&i.set(t,t-1,r[t])}return i}}function B(t,e,n,r){let s,i;return Math.abs(n)>Math.abs(r)?(s=r/n,i=n+s*r,[(t+s*e)/i,(e-s*t)/i]):(s=n/r,i=r+s*n,[(s*t+e)/i,(s*e-t)/i])}class _{constructor(t){if(!(t=R.checkMatrix(t)).isSymmetric())throw new Error("Matrix is not symmetric");let e,n,r,s=t,i=s.rows,o=new v(i,i),a=!0;for(n=0;n<i;n++){let t=0;for(r=0;r<n;r++){let i=0;for(e=0;e<r;e++)i+=o.get(r,e)*o.get(n,e);i=(s.get(n,r)-i)/o.get(r,r),o.set(n,r,i),t+=i*i}for(t=s.get(n,n)-t,a&&=t>0,o.set(n,n,Math.sqrt(Math.max(t,0))),r=n+1;r<i;r++)o.set(n,r,0)}this.L=o,this.positiveDefinite=a}isPositiveDefinite(){return this.positiveDefinite}solve(t){t=R.checkMatrix(t);let e=this.L,n=e.rows;if(t.rows!==n)throw new Error("Matrix dimensions do not match");if(!1===this.isPositiveDefinite())throw new Error("Matrix is not positive definite");let r,s,i,o=t.columns,a=t.clone();for(i=0;i<n;i++)for(s=0;s<o;s++){for(r=0;r<i;r++)a.set(i,s,a.get(i,s)-a.get(r,s)*e.get(i,r));a.set(i,s,a.get(i,s)/e.get(i,i))}for(i=n-1;i>=0;i--)for(s=0;s<o;s++){for(r=i+1;r<n;r++)a.set(i,s,a.get(i,s)-a.get(r,s)*e.get(r,i));a.set(i,s,a.get(i,s)/e.get(i,i))}return a}get lowerTriangularMatrix(){return this.L}}class L{constructor(t,e={}){t=R.checkMatrix(t);let{Y:n}=e;const{scaleScores:s=!1,maxIterations:i=1e3,terminationCriteria:o=1e-10}=e;let a;if(n){if(n=r.isAnyArray(n)&&"number"==typeof n[0]?v.columnVector(n):R.checkMatrix(n),n.rows!==t.rows)throw new Error("Y should have the same number of rows as X");a=n.getColumnVector(0)}else a=t.getColumnVector(0);let u,l,h,c,f=1;for(let e=0;e<i&&f>o;e++)h=t.transpose().mmul(a).div(a.transpose().mmul(a).get(0,0)),h=h.div(h.norm()),u=t.mmul(h).div(h.transpose().mmul(h).get(0,0)),e>0&&(f=u.clone().sub(c).pow(2).sum()),c=u.clone(),n?(l=n.transpose().mmul(u).div(u.transpose().mmul(u).get(0,0)),l=l.div(l.norm()),a=n.mmul(l).div(l.transpose().mmul(l).get(0,0))):a=u;if(n){let e=t.transpose().mmul(u).div(u.transpose().mmul(u).get(0,0));e=e.div(e.norm());let r=t.clone().sub(u.clone().mmul(e.transpose())),s=a.transpose().mmul(u).div(u.transpose().mmul(u).get(0,0)),i=n.clone().sub(u.clone().mulS(s.get(0,0)).mmul(l.transpose()));this.t=u,this.p=e.transpose(),this.w=h.transpose(),this.q=l,this.u=a,this.s=u.transpose().mmul(u),this.xResidual=r,this.yResidual=i,this.betas=s}else this.w=h.transpose(),this.s=u.transpose().mmul(u).sqrt(),this.t=s?u.clone().div(this.s.get(0,0)):u,this.xResidual=t.sub(u.mmul(h.transpose()))}}e.y3=x,e.jy=_,e.oN=_,e.Hc=A,e.cg=z,e.hj=z,e.LU=k,e.Tb=k,e.uq=v,e.Zm=class extends E{constructor(t,e){d(t,e),super(t,t.rows,e.length),this.columnIndices=e}set(t,e,n){return this.matrix.set(t,this.columnIndices[e],n),this}get(t,e){return this.matrix.get(t,this.columnIndices[e])}},e.Dq=class extends E{constructor(t,e){c(t,e),super(t,t.rows,1),this.column=e}set(t,e,n){return this.matrix.set(t,this.column,n),this}get(t){return this.matrix.get(t,this.column)}},e.__=class extends E{constructor(t){super(t,t.rows,t.columns)}set(t,e,n){return this.matrix.set(t,this.columns-e-1,n),this}get(t,e){return this.matrix.get(t,this.columns-e-1)}},e.q0=class extends E{constructor(t){super(t,t.rows,t.columns)}set(t,e,n){return this.matrix.set(this.rows-t-1,e,n),this}get(t,e){return this.matrix.get(this.rows-t-1,e)}},e.lh=class extends E{constructor(t,e){g(t,e),super(t,e.length,t.columns),this.rowIndices=e}set(t,e,n){return this.matrix.set(this.rowIndices[t],e,n),this}get(t,e){return this.matrix.get(this.rowIndices[t],e)}},e.pI=class extends E{constructor(t,e){h(t,e),super(t,1,t.columns),this.row=e}set(t,e,n){return this.matrix.set(this.row,e,n),this}get(t,e){return this.matrix.get(this.row,e)}},e.zC=I,e.zg=class extends E{constructor(t,e,n,r,s){p(t,e,n,r,s),super(t,n-e+1,s-r+1),this.startRow=e,this.startColumn=r}set(t,e,n){return this.matrix.set(this.startRow+t,this.startColumn+e,n),this}get(t,e){return this.matrix.get(this.startRow+t,this.startColumn+e)}},e.g6=class extends E{constructor(t){super(t,t.columns,t.rows)}set(t,e,n){return this.matrix.set(e,t,n),this}get(t,e){return this.matrix.get(e,t)}},e.OL=L,e.ks=L,e.QR=T,e.jp=T,e.mk=P,e.W2=P,e.l=S,e.KY=N,e.dv=R,e.BR=function(t,e=t,n={}){t=new v(t);let s=!1;if("object"!=typeof e||v.isMatrix(e)||r.isAnyArray(e)?e=new v(e):(n=e,e=t,s=!0),t.rows!==e.rows)throw new TypeError("Both matrices must have the same number of rows");const{center:i=!0,scale:o=!0}=n;i&&(t.center("column"),s||e.center("column")),o&&(t.scale("column"),s||e.scale("column"));const a=t.standardDeviation("column",{unbiased:!0}),u=s?a:e.standardDeviation("column",{unbiased:!0}),l=t.transpose().mmul(e);for(let e=0;e<l.rows;e++)for(let n=0;n<l.columns;n++)l.set(e,n,l.get(e,n)*(1/(a[e]*u[n]))*(1/(t.rows-1)));return l},e.Wu=function(t,e=t,n={}){t=new v(t);let s=!1;if("object"!=typeof e||v.isMatrix(e)||r.isAnyArray(e)?e=new v(e):(n=e,e=t,s=!0),t.rows!==e.rows)throw new TypeError("Both matrices must have the same number of rows");const{center:i=!0}=n;i&&(t=t.center("column"),s||(e=e.center("column")));const o=t.transpose().mmul(e);for(let e=0;e<o.rows;e++)for(let n=0;n<o.columns;n++)o.set(e,n,o.get(e,n)*(1/(t.rows-1)));return o},e.a4=function t(e){if((e=v.checkMatrix(e)).isSquare()){if(0===e.columns)return 1;let n,r,s,i;if(2===e.columns)return n=e.get(0,0),r=e.get(0,1),s=e.get(1,0),i=e.get(1,1),n*i-r*s;if(3===e.columns){let i,o,a;return i=new I(e,[1,2],[1,2]),o=new I(e,[1,2],[0,2]),a=new I(e,[1,2],[0,1]),n=e.get(0,0),r=e.get(0,1),s=e.get(0,2),n*t(i)-r*t(o)+s*t(a)}return new k(e).determinant}throw Error("determinant can only be calculated for a square matrix")},e.DI=function(t,e=!1){return t=R.checkMatrix(t),e?new P(t).inverse():D(t,v.eye(t.rows))},e.Jo=function(t,e={}){const{thresholdValue:n=1e-9,thresholdError:r=1e-9}=e;let s=(t=v.checkMatrix(t)).rows,i=new v(s,s);for(let e=0;e<s;e++){let o=v.columnVector(t.getRow(e)),a=t.subMatrixRow(U(s,e)).transpose(),u=new P(a).solve(o),l=v.sub(o,a.mmul(u)).abs().max();i.setRow(e,O(l,u,e,n,r))}return i},e.Zi=function(t,e=Number.EPSILON){if((t=v.checkMatrix(t)).isEmpty())return t.transpose();let n=new P(t,{autoTranspose:!0}),r=n.leftSingularVectors,s=n.rightSingularVectors,i=n.diagonal;for(let t=0;t<i.length;t++)Math.abs(i[t])>e?i[t]=1/i[t]:i[t]=0;return s.mmul(v.diag(i).mmul(r.transpose()))},e.kH=D,e.LV=function(t,e){if(r.isAnyArray(t))return t[0]&&r.isAnyArray(t[0])?new R(t):new N(t,e);throw new Error("the argument is not an array")}},7391:(t,e,n)=>{var r=n(7180),s=n(3181),i=n(3031),o=n(9067),a=n(6833),u=n(3717),l=n(4801);l.alea=r,l.xor128=s,l.xorwow=i,l.xorshift7=o,l.xor4096=a,l.tychei=u,t.exports=l},7180:function(t,e,n){var r;!function(t,s){function i(t){var e,n=this,r=(e=4022871197,function(t){t=String(t);for(var n=0;n<t.length;n++){var r=.02519603282416938*(e+=t.charCodeAt(n));r-=e=r>>>0,e=(r*=e)>>>0,e+=4294967296*(r-=e)}return 2.3283064365386963e-10*(e>>>0)});n.next=function(){var t=2091639*n.s0+2.3283064365386963e-10*n.c;return n.s0=n.s1,n.s1=n.s2,n.s2=t-(n.c=0|t)},n.c=1,n.s0=r(" "),n.s1=r(" "),n.s2=r(" "),n.s0-=r(t),n.s0<0&&(n.s0+=1),n.s1-=r(t),n.s1<0&&(n.s1+=1),n.s2-=r(t),n.s2<0&&(n.s2+=1),r=null}function o(t,e){return e.c=t.c,e.s0=t.s0,e.s1=t.s1,e.s2=t.s2,e}function a(t,e){var n=new i(t),r=e&&e.state,s=n.next;return s.int32=function(){return 4294967296*n.next()|0},s.double=function(){return s()+11102230246251565e-32*(2097152*s()|0)},s.quick=s,r&&("object"==typeof r&&o(r,n),s.state=function(){return o(n,{})}),s}s&&s.exports?s.exports=a:n.amdD&&n.amdO?void 0===(r=function(){return a}.call(e,n,e,s))||(s.exports=r):this.alea=a}(0,t=n.nmd(t),n.amdD)},3717:function(t,e,n){var r;!function(t,s){function i(t){var e=this,n="";e.next=function(){var t=e.b,n=e.c,r=e.d,s=e.a;return t=t<<25^t>>>7^n,n=n-r|0,r=r<<24^r>>>8^s,s=s-t|0,e.b=t=t<<20^t>>>12^n,e.c=n=n-r|0,e.d=r<<16^n>>>16^s,e.a=s-t|0},e.a=0,e.b=0,e.c=-1640531527,e.d=1367130551,t===Math.floor(t)?(e.a=t/4294967296|0,e.b=0|t):n+=t;for(var r=0;r<n.length+20;r++)e.b^=0|n.charCodeAt(r),e.next()}function o(t,e){return e.a=t.a,e.b=t.b,e.c=t.c,e.d=t.d,e}function a(t,e){var n=new i(t),r=e&&e.state,s=function(){return(n.next()>>>0)/4294967296};return s.double=function(){do{var t=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===t);return t},s.int32=n.next,s.quick=s,r&&("object"==typeof r&&o(r,n),s.state=function(){return o(n,{})}),s}s&&s.exports?s.exports=a:n.amdD&&n.amdO?void 0===(r=function(){return a}.call(e,n,e,s))||(s.exports=r):this.tychei=a}(0,t=n.nmd(t),n.amdD)},3181:function(t,e,n){var r;!function(t,s){function i(t){var e=this,n="";e.x=0,e.y=0,e.z=0,e.w=0,e.next=function(){var t=e.x^e.x<<11;return e.x=e.y,e.y=e.z,e.z=e.w,e.w^=e.w>>>19^t^t>>>8},t===(0|t)?e.x=t:n+=t;for(var r=0;r<n.length+64;r++)e.x^=0|n.charCodeAt(r),e.next()}function o(t,e){return e.x=t.x,e.y=t.y,e.z=t.z,e.w=t.w,e}function a(t,e){var n=new i(t),r=e&&e.state,s=function(){return(n.next()>>>0)/4294967296};return s.double=function(){do{var t=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===t);return t},s.int32=n.next,s.quick=s,r&&("object"==typeof r&&o(r,n),s.state=function(){return o(n,{})}),s}s&&s.exports?s.exports=a:n.amdD&&n.amdO?void 0===(r=function(){return a}.call(e,n,e,s))||(s.exports=r):this.xor128=a}(0,t=n.nmd(t),n.amdD)},6833:function(t,e,n){var r;!function(t,s){function i(t){var e=this;e.next=function(){var t,n,r=e.w,s=e.X,i=e.i;return e.w=r=r+1640531527|0,n=s[i+34&127],t=s[i=i+1&127],n^=n<<13,t^=t<<17,n^=n>>>15,t^=t>>>12,n=s[i]=n^t,e.i=i,n+(r^r>>>16)|0},function(t,e){var n,r,s,i,o,a=[],u=128;for(e===(0|e)?(r=e,e=null):(e+="\0",r=0,u=Math.max(u,e.length)),s=0,i=-32;i<u;++i)e&&(r^=e.charCodeAt((i+32)%e.length)),0===i&&(o=r),r^=r<<10,r^=r>>>15,r^=r<<4,r^=r>>>13,i>=0&&(o=o+1640531527|0,s=0==(n=a[127&i]^=r+o)?s+1:0);for(s>=128&&(a[127&(e&&e.length||0)]=-1),s=127,i=512;i>0;--i)r=a[s+34&127],n=a[s=s+1&127],r^=r<<13,n^=n<<17,r^=r>>>15,n^=n>>>12,a[s]=r^n;t.w=o,t.X=a,t.i=s}(e,t)}function o(t,e){return e.i=t.i,e.w=t.w,e.X=t.X.slice(),e}function a(t,e){null==t&&(t=+new Date);var n=new i(t),r=e&&e.state,s=function(){return(n.next()>>>0)/4294967296};return s.double=function(){do{var t=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===t);return t},s.int32=n.next,s.quick=s,r&&(r.X&&o(r,n),s.state=function(){return o(n,{})}),s}s&&s.exports?s.exports=a:n.amdD&&n.amdO?void 0===(r=function(){return a}.call(e,n,e,s))||(s.exports=r):this.xor4096=a}(0,t=n.nmd(t),n.amdD)},9067:function(t,e,n){var r;!function(t,s){function i(t){var e=this;e.next=function(){var t,n,r=e.x,s=e.i;return t=r[s],n=(t^=t>>>7)^t<<24,n^=(t=r[s+1&7])^t>>>10,n^=(t=r[s+3&7])^t>>>3,n^=(t=r[s+4&7])^t<<7,t=r[s+7&7],n^=(t^=t<<13)^t<<9,r[s]=n,e.i=s+1&7,n},function(t,e){var n,r=[];if(e===(0|e))r[0]=e;else for(e=""+e,n=0;n<e.length;++n)r[7&n]=r[7&n]<<15^e.charCodeAt(n)+r[n+1&7]<<13;for(;r.length<8;)r.push(0);for(n=0;n<8&&0===r[n];++n);for(8==n?r[7]=-1:r[n],t.x=r,t.i=0,n=256;n>0;--n)t.next()}(e,t)}function o(t,e){return e.x=t.x.slice(),e.i=t.i,e}function a(t,e){null==t&&(t=+new Date);var n=new i(t),r=e&&e.state,s=function(){return(n.next()>>>0)/4294967296};return s.double=function(){do{var t=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===t);return t},s.int32=n.next,s.quick=s,r&&(r.x&&o(r,n),s.state=function(){return o(n,{})}),s}s&&s.exports?s.exports=a:n.amdD&&n.amdO?void 0===(r=function(){return a}.call(e,n,e,s))||(s.exports=r):this.xorshift7=a}(0,t=n.nmd(t),n.amdD)},3031:function(t,e,n){var r;!function(t,s){function i(t){var e=this,n="";e.next=function(){var t=e.x^e.x>>>2;return e.x=e.y,e.y=e.z,e.z=e.w,e.w=e.v,(e.d=e.d+362437|0)+(e.v=e.v^e.v<<4^t^t<<1)|0},e.x=0,e.y=0,e.z=0,e.w=0,e.v=0,t===(0|t)?e.x=t:n+=t;for(var r=0;r<n.length+64;r++)e.x^=0|n.charCodeAt(r),r==n.length&&(e.d=e.x<<10^e.x>>>4),e.next()}function o(t,e){return e.x=t.x,e.y=t.y,e.z=t.z,e.w=t.w,e.v=t.v,e.d=t.d,e}function a(t,e){var n=new i(t),r=e&&e.state,s=function(){return(n.next()>>>0)/4294967296};return s.double=function(){do{var t=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===t);return t},s.int32=n.next,s.quick=s,r&&("object"==typeof r&&o(r,n),s.state=function(){return o(n,{})}),s}s&&s.exports?s.exports=a:n.amdD&&n.amdO?void 0===(r=function(){return a}.call(e,n,e,s))||(s.exports=r):this.xorwow=a}(0,t=n.nmd(t),n.amdD)},4801:function(t,e,n){var r;!function(s,i,o){var a,u=256,l=o.pow(u,6),h=o.pow(2,52),c=2*h,f=255;function m(t,e,n){var r=[],f=w(p((e=1==e?{entropy:!0}:e||{}).entropy?[t,y(i)]:null==t?function(){try{var t;return a&&(t=a.randomBytes)?t=t(u):(t=new Uint8Array(u),(s.crypto||s.msCrypto).getRandomValues(t)),y(t)}catch(t){var e=s.navigator,n=e&&e.plugins;return[+new Date,s,n,s.screen,y(i)]}}():t,3),r),m=new g(r),b=function(){for(var t=m.g(6),e=l,n=0;t<h;)t=(t+n)*u,e*=u,n=m.g(1);for(;t>=c;)t/=2,e/=2,n>>>=1;return(t+n)/e};return b.int32=function(){return 0|m.g(4)},b.quick=function(){return m.g(4)/4294967296},b.double=b,w(y(m.S),i),(e.pass||n||function(t,e,n,r){return r&&(r.S&&d(r,m),t.state=function(){return d(m,{})}),n?(o.random=t,e):t})(b,f,"global"in e?e.global:this==o,e.state)}function g(t){var e,n=t.length,r=this,s=0,i=r.i=r.j=0,o=r.S=[];for(n||(t=[n++]);s<u;)o[s]=s++;for(s=0;s<u;s++)o[s]=o[i=f&i+t[s%n]+(e=o[s])],o[i]=e;(r.g=function(t){for(var e,n=0,s=r.i,i=r.j,o=r.S;t--;)e=o[s=f&s+1],n=n*u+o[f&(o[s]=o[i=f&i+e])+(o[i]=e)];return r.i=s,r.j=i,n})(u)}function d(t,e){return e.i=t.i,e.j=t.j,e.S=t.S.slice(),e}function p(t,e){var n,r=[],s=typeof t;if(e&&"object"==s)for(n in t)try{r.push(p(t[n],e-1))}catch(t){}return r.length?r:"string"==s?t:t+"\0"}function w(t,e){for(var n,r=t+"",s=0;s<r.length;)e[f&s]=f&(n^=19*e[f&s])+r.charCodeAt(s++);return y(e)}function y(t){return String.fromCharCode.apply(0,t)}if(w(o.random(),i),t.exports){t.exports=m;try{a=n(1234)}catch(t){}}else void 0===(r=function(){return m}.call(e,n,e,t))||(t.exports=r)}("undefined"!=typeof self?self:this,[],Math)}}]);
2
+ (self.webpackChunkbio=self.webpackChunkbio||[]).push([[284],{606:(t,e,n)=>{"use strict";n.d(e,{Qt:()=>s,gD:()=>r}),n(6066);const r=t=>null==t;function s(t,e,n,r){if(n>t[t.length-1])return;const s=t.findIndex(t=>n<t);t.pop(),t.splice(s,0,n),e.pop(),e.splice(s,0,r)}},788:(t,e,n)=>{"use strict";n.r(e),n.d(e,{isAnyArray:()=>s});const r=Object.prototype.toString;function s(t){const e=r.call(t);return e.endsWith("Array]")&&!e.includes("Big")}},3031:function(t,e,n){var r;!function(t,s){function i(t){var e=this,n="";e.next=function(){var t=e.x^e.x>>>2;return e.x=e.y,e.y=e.z,e.z=e.w,e.w=e.v,(e.d=e.d+362437|0)+(e.v=e.v^e.v<<4^t^t<<1)|0},e.x=0,e.y=0,e.z=0,e.w=0,e.v=0,t===(0|t)?e.x=t:n+=t;for(var r=0;r<n.length+64;r++)e.x^=0|n.charCodeAt(r),r==n.length&&(e.d=e.x<<10^e.x>>>4),e.next()}function o(t,e){return e.x=t.x,e.y=t.y,e.z=t.z,e.w=t.w,e.v=t.v,e.d=t.d,e}function a(t,e){var n=new i(t),r=e&&e.state,s=function(){return(n.next()>>>0)/4294967296};return s.double=function(){do{var t=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===t);return t},s.int32=n.next,s.quick=s,r&&("object"==typeof r&&o(r,n),s.state=function(){return o(n,{})}),s}s&&s.exports?s.exports=a:n.amdD&&n.amdO?void 0===(r=function(){return a}.call(e,n,e,s))||(s.exports=r):this.xorwow=a}(0,t=n.nmd(t),n.amdD)},3054:(t,e,n)=>{"use strict";var r=n(788),s=n(3718);const i=" ".repeat(2),o=" ".repeat(4);function a(t,e={}){const{maxRows:n=15,maxColumns:r=10,maxNumSize:s=8,padMinus:a="auto"}=e;return`${t.constructor.name} {\n${i}[\n${o}${function(t,e,n,r,s){const{rows:i,columns:a}=t,l=Math.min(i,e),h=Math.min(a,n),c=[];if("auto"===s){s=!1;t:for(let e=0;e<l;e++)for(let n=0;n<h;n++)if(t.get(e,n)<0){s=!0;break t}}for(let e=0;e<l;e++){let n=[];for(let i=0;i<h;i++)n.push(u(t.get(e,i),r,s));c.push(`${n.join(" ")}`)}return h!==a&&(c[c.length-1]+=` ... ${a-n} more columns`),l!==i&&c.push(`... ${i-e} more rows`),c.join(`\n${o}`)}(t,n,r,s,a)}\n${i}]\n${i}rows: ${t.rows}\n${i}columns: ${t.columns}\n}`}function u(t,e,n){return(t>=0&&n?` ${l(t,e-1)}`:l(t,e)).padEnd(e)}function l(t,e){let n=t.toString();if(n.length<=e)return n;let r=t.toFixed(e);if(r.length>e&&(r=t.toFixed(Math.max(0,e-(r.length-e)))),r.length<=e&&!r.startsWith("0.000")&&!r.startsWith("-0.000"))return r;let s=t.toExponential(e);return s.length>e&&(s=t.toExponential(Math.max(0,e-(s.length-e)))),s.slice(0)}function h(t,e,n){let r=n?t.rows:t.rows-1;if(e<0||e>r)throw new RangeError("Row index out of range")}function c(t,e,n){let r=n?t.columns:t.columns-1;if(e<0||e>r)throw new RangeError("Column index out of range")}function f(t,e){if(e.to1DArray&&(e=e.to1DArray()),e.length!==t.columns)throw new RangeError("vector size must be the same as the number of columns");return e}function m(t,e){if(e.to1DArray&&(e=e.to1DArray()),e.length!==t.rows)throw new RangeError("vector size must be the same as the number of rows");return e}function g(t,e){if(!r.isAnyArray(e))throw new TypeError("row indices must be an array");for(let n=0;n<e.length;n++)if(e[n]<0||e[n]>=t.rows)throw new RangeError("row indices are out of range")}function d(t,e){if(!r.isAnyArray(e))throw new TypeError("column indices must be an array");for(let n=0;n<e.length;n++)if(e[n]<0||e[n]>=t.columns)throw new RangeError("column indices are out of range")}function p(t,e,n,r,s){if(5!==arguments.length)throw new RangeError("expected 4 arguments");if(y("startRow",e),y("endRow",n),y("startColumn",r),y("endColumn",s),e>n||r>s||e<0||e>=t.rows||n<0||n>=t.rows||r<0||r>=t.columns||s<0||s>=t.columns)throw new RangeError("Submatrix indices are out of range")}function w(t,e=0){let n=[];for(let r=0;r<t;r++)n.push(e);return n}function y(t,e){if("number"!=typeof e)throw new TypeError(`${t} must be a number`)}function b(t){if(t.isEmpty())throw new Error("Empty matrix has no elements to index")}class x{static from1DArray(t,e,n){if(t*e!==n.length)throw new RangeError("data length does not match given dimensions");let r=new S(t,e);for(let s=0;s<t;s++)for(let t=0;t<e;t++)r.set(s,t,n[s*e+t]);return r}static rowVector(t){let e=new S(1,t.length);for(let n=0;n<t.length;n++)e.set(0,n,t[n]);return e}static columnVector(t){let e=new S(t.length,1);for(let n=0;n<t.length;n++)e.set(n,0,t[n]);return e}static zeros(t,e){return new S(t,e)}static ones(t,e){return new S(t,e).fill(1)}static rand(t,e,n={}){if("object"!=typeof n)throw new TypeError("options must be an object");const{random:r=Math.random}=n;let s=new S(t,e);for(let n=0;n<t;n++)for(let t=0;t<e;t++)s.set(n,t,r());return s}static randInt(t,e,n={}){if("object"!=typeof n)throw new TypeError("options must be an object");const{min:r=0,max:s=1e3,random:i=Math.random}=n;if(!Number.isInteger(r))throw new TypeError("min must be an integer");if(!Number.isInteger(s))throw new TypeError("max must be an integer");if(r>=s)throw new RangeError("min must be smaller than max");let o=s-r,a=new S(t,e);for(let n=0;n<t;n++)for(let t=0;t<e;t++){let e=r+Math.round(i()*o);a.set(n,t,e)}return a}static eye(t,e,n){void 0===e&&(e=t),void 0===n&&(n=1);let r=Math.min(t,e),s=this.zeros(t,e);for(let t=0;t<r;t++)s.set(t,t,n);return s}static diag(t,e,n){let r=t.length;void 0===e&&(e=r),void 0===n&&(n=e);let s=Math.min(r,e,n),i=this.zeros(e,n);for(let e=0;e<s;e++)i.set(e,e,t[e]);return i}static min(t,e){t=this.checkMatrix(t),e=this.checkMatrix(e);let n=t.rows,r=t.columns,s=new S(n,r);for(let i=0;i<n;i++)for(let n=0;n<r;n++)s.set(i,n,Math.min(t.get(i,n),e.get(i,n)));return s}static max(t,e){t=this.checkMatrix(t),e=this.checkMatrix(e);let n=t.rows,r=t.columns,s=new this(n,r);for(let i=0;i<n;i++)for(let n=0;n<r;n++)s.set(i,n,Math.max(t.get(i,n),e.get(i,n)));return s}static checkMatrix(t){return x.isMatrix(t)?t:new S(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 n=0;n<this.columns;n++)t.call(this,e,n);return this}to1DArray(){let t=[];for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)t.push(this.get(e,n));return t}to2DArray(){let t=[];for(let e=0;e<this.rows;e++){t.push([]);for(let n=0;n<this.columns;n++)t[e].push(this.get(e,n))}return t}toJSON(){return this.to2DArray()}isRowVector(){return 1===this.rows}isColumnVector(){return 1===this.columns}isVector(){return 1===this.rows||1===this.columns}isSquare(){return this.rows===this.columns}isEmpty(){return 0===this.rows||0===this.columns}isSymmetric(){if(this.isSquare()){for(let t=0;t<this.rows;t++)for(let e=0;e<=t;e++)if(this.get(t,e)!==this.get(e,t))return!1;return!0}return!1}isDistance(){if(!this.isSymmetric())return!1;for(let t=0;t<this.rows;t++)if(0!==this.get(t,t))return!1;return!0}isEchelonForm(){let t=0,e=0,n=-1,r=!0,s=!1;for(;t<this.rows&&r;){for(e=0,s=!1;e<this.columns&&!1===s;)0===this.get(t,e)?e++:1===this.get(t,e)&&e>n?(s=!0,n=e):(r=!1,s=!0);t++}return r}isReducedEchelonForm(){let t=0,e=0,n=-1,r=!0,s=!1;for(;t<this.rows&&r;){for(e=0,s=!1;e<this.columns&&!1===s;)0===this.get(t,e)?e++:1===this.get(t,e)&&e>n?(s=!0,n=e):(r=!1,s=!0);for(let n=e+1;n<this.rows;n++)0!==this.get(t,n)&&(r=!1);t++}return r}echelonForm(){let t=this.clone(),e=0,n=0;for(;e<t.rows&&n<t.columns;){let r=e;for(let s=e;s<t.rows;s++)t.get(s,n)>t.get(r,n)&&(r=s);if(0===t.get(r,n))n++;else{t.swapRows(e,r);let s=t.get(e,n);for(let r=n;r<t.columns;r++)t.set(e,r,t.get(e,r)/s);for(let r=e+1;r<t.rows;r++){let s=t.get(r,n)/t.get(e,n);t.set(r,n,0);for(let i=n+1;i<t.columns;i++)t.set(r,i,t.get(r,i)-t.get(e,i)*s)}e++,n++}}return t}reducedEchelonForm(){let t=this.echelonForm(),e=t.columns,n=t.rows,r=n-1;for(;r>=0;)if(0===t.maxRow(r))r--;else{let s=0,i=!1;for(;s<n&&!1===i;)1===t.get(r,s)?i=!0:s++;for(let n=0;n<r;n++){let i=t.get(n,s);for(let o=s;o<e;o++){let e=t.get(n,o)-i*t.get(r,o);t.set(n,o,e)}}r--}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:n=1}=t;if(!Number.isInteger(e)||e<=0)throw new TypeError("rows must be a positive integer");if(!Number.isInteger(n)||n<=0)throw new TypeError("columns must be a positive integer");let r=new S(this.rows*e,this.columns*n);for(let t=0;t<e;t++)for(let e=0;e<n;e++)r.setSubMatrix(this,this.rows*t,this.columns*e);return r}fill(t){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,t);return this}neg(){return this.mulS(-1)}getRow(t){h(this,t);let e=[];for(let n=0;n<this.columns;n++)e.push(this.get(t,n));return e}getRowVector(t){return S.rowVector(this.getRow(t))}setRow(t,e){h(this,t),e=f(this,e);for(let n=0;n<this.columns;n++)this.set(t,n,e[n]);return this}swapRows(t,e){h(this,t),h(this,e);for(let n=0;n<this.columns;n++){let r=this.get(t,n);this.set(t,n,this.get(e,n)),this.set(e,n,r)}return this}getColumn(t){c(this,t);let e=[];for(let n=0;n<this.rows;n++)e.push(this.get(n,t));return e}getColumnVector(t){return S.columnVector(this.getColumn(t))}setColumn(t,e){c(this,t),e=m(this,e);for(let n=0;n<this.rows;n++)this.set(n,t,e[n]);return this}swapColumns(t,e){c(this,t),c(this,e);for(let n=0;n<this.rows;n++){let r=this.get(n,t);this.set(n,t,this.get(n,e)),this.set(n,e,r)}return this}addRowVector(t){t=f(this,t);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)+t[n]);return this}subRowVector(t){t=f(this,t);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)-t[n]);return this}mulRowVector(t){t=f(this,t);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)*t[n]);return this}divRowVector(t){t=f(this,t);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)/t[n]);return this}addColumnVector(t){t=m(this,t);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)+t[e]);return this}subColumnVector(t){t=m(this,t);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)-t[e]);return this}mulColumnVector(t){t=m(this,t);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)*t[e]);return this}divColumnVector(t){t=m(this,t);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)/t[e]);return this}mulRow(t,e){h(this,t);for(let n=0;n<this.columns;n++)this.set(t,n,this.get(t,n)*e);return this}mulColumn(t,e){c(this,t);for(let n=0;n<this.rows;n++)this.set(n,t,this.get(n,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 n=0;n<this.columns;n++)this.get(e,n)>t[e]&&(t[e]=this.get(e,n));return t}case"column":{const t=new Array(this.columns).fill(Number.NEGATIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.get(e,n)>t[n]&&(t[n]=this.get(e,n));return t}case void 0:{let t=this.get(0,0);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.get(e,n)>t&&(t=this.get(e,n));return t}default:throw new Error(`invalid option: ${t}`)}}maxIndex(){b(this);let t=this.get(0,0),e=[0,0];for(let n=0;n<this.rows;n++)for(let r=0;r<this.columns;r++)this.get(n,r)>t&&(t=this.get(n,r),e[0]=n,e[1]=r);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 n=0;n<this.columns;n++)this.get(e,n)<t[e]&&(t[e]=this.get(e,n));return t}case"column":{const t=new Array(this.columns).fill(Number.POSITIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.get(e,n)<t[n]&&(t[n]=this.get(e,n));return t}case void 0:{let t=this.get(0,0);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.get(e,n)<t&&(t=this.get(e,n));return t}default:throw new Error(`invalid option: ${t}`)}}minIndex(){b(this);let t=this.get(0,0),e=[0,0];for(let n=0;n<this.rows;n++)for(let r=0;r<this.columns;r++)this.get(n,r)<t&&(t=this.get(n,r),e[0]=n,e[1]=r);return e}maxRow(t){if(h(this,t),this.isEmpty())return NaN;let e=this.get(t,0);for(let n=1;n<this.columns;n++)this.get(t,n)>e&&(e=this.get(t,n));return e}maxRowIndex(t){h(this,t),b(this);let e=this.get(t,0),n=[t,0];for(let r=1;r<this.columns;r++)this.get(t,r)>e&&(e=this.get(t,r),n[1]=r);return n}minRow(t){if(h(this,t),this.isEmpty())return NaN;let e=this.get(t,0);for(let n=1;n<this.columns;n++)this.get(t,n)<e&&(e=this.get(t,n));return e}minRowIndex(t){h(this,t),b(this);let e=this.get(t,0),n=[t,0];for(let r=1;r<this.columns;r++)this.get(t,r)<e&&(e=this.get(t,r),n[1]=r);return n}maxColumn(t){if(c(this,t),this.isEmpty())return NaN;let e=this.get(0,t);for(let n=1;n<this.rows;n++)this.get(n,t)>e&&(e=this.get(n,t));return e}maxColumnIndex(t){c(this,t),b(this);let e=this.get(0,t),n=[0,t];for(let r=1;r<this.rows;r++)this.get(r,t)>e&&(e=this.get(r,t),n[0]=r);return n}minColumn(t){if(c(this,t),this.isEmpty())return NaN;let e=this.get(0,t);for(let n=1;n<this.rows;n++)this.get(n,t)<e&&(e=this.get(n,t));return e}minColumnIndex(t){c(this,t),b(this);let e=this.get(0,t),n=[0,t];for(let r=1;r<this.rows;r++)this.get(r,t)<e&&(e=this.get(r,t),n[0]=r);return n}diag(){let t=Math.min(this.rows,this.columns),e=[];for(let n=0;n<t;n++)e.push(this.get(n,n));return e}norm(t="frobenius"){switch(t){case"max":return this.max();case"frobenius":return Math.sqrt(this.dot(this));default:throw new RangeError(`unknown norm type: ${t}`)}}cumulativeSum(){let t=0;for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)t+=this.get(e,n),this.set(e,n,t);return this}dot(t){x.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 n=0;for(let r=0;r<e.length;r++)n+=e[r]*t[r];return n}mmul(t){t=S.checkMatrix(t);let e=this.rows,n=this.columns,r=t.columns,s=new S(e,r),i=new Float64Array(n);for(let o=0;o<r;o++){for(let e=0;e<n;e++)i[e]=t.get(e,o);for(let t=0;t<e;t++){let e=0;for(let r=0;r<n;r++)e+=this.get(t,r)*i[r];s.set(t,o,e)}}return s}mpow(t){if(!this.isSquare())throw new RangeError("Matrix must be square");if(!Number.isInteger(t)||t<0)throw new RangeError("Exponent must be a non-negative integer");let e=S.eye(this.rows),n=this;for(let r=t;r>=1;r/=2)1&r&&(e=e.mmul(n)),n=n.mmul(n);return e}strassen2x2(t){t=S.checkMatrix(t);let e=new S(2,2);const n=this.get(0,0),r=t.get(0,0),s=this.get(0,1),i=t.get(0,1),o=this.get(1,0),a=t.get(1,0),u=this.get(1,1),l=t.get(1,1),h=(n+u)*(r+l),c=(o+u)*r,f=n*(i-l),m=u*(a-r),g=(n+s)*l,d=h+m-g+(s-u)*(a+l),p=f+g,w=c+m,y=h-c+f+(o-n)*(r+i);return e.set(0,0,d),e.set(0,1,p),e.set(1,0,w),e.set(1,1,y),e}strassen3x3(t){t=S.checkMatrix(t);let e=new S(3,3);const n=this.get(0,0),r=this.get(0,1),s=this.get(0,2),i=this.get(1,0),o=this.get(1,1),a=this.get(1,2),u=this.get(2,0),l=this.get(2,1),h=this.get(2,2),c=t.get(0,0),f=t.get(0,1),m=t.get(0,2),g=t.get(1,0),d=t.get(1,1),p=t.get(1,2),w=t.get(2,0),y=t.get(2,1),b=t.get(2,2),x=(n-i)*(-f+d),M=(-n+i+o)*(c-f+d),v=(i+o)*(-c+f),A=n*c,E=(-n+u+l)*(c-m+p),I=(-n+u)*(m-p),N=(u+l)*(-c+m),R=(-s+l+h)*(d+w-y),k=(s-h)*(d-y),C=s*w,T=(l+h)*(-w+y),P=(-s+o+a)*(p+w-b),D=(s-a)*(p-b),U=(o+a)*(-w+b),O=A+C+r*g,z=(n+r+s-i-o-l-h)*d+M+v+A+R+C+T,B=A+E+N+(n+r+s-o-a-u-l)*p+C+P+U,_=x+o*(-c+f+g-d-p-w+b)+M+A+C+P+D,L=x+M+v+A+a*y,$=C+P+D+U+i*m,G=A+E+I+l*(-c+m+g-d-p-w+y)+R+k+C,F=R+k+C+T+u*f,j=A+E+I+N+h*b;return e.set(0,0,O),e.set(0,1,z),e.set(0,2,B),e.set(1,0,_),e.set(1,1,L),e.set(1,2,$),e.set(2,0,G),e.set(2,1,F),e.set(2,2,j),e}mmulStrassen(t){t=S.checkMatrix(t);let e=this.clone(),n=e.rows,r=e.columns,s=t.rows,i=t.columns;function o(t,e,n){let r=t.rows,s=t.columns;if(r===e&&s===n)return t;{let r=x.zeros(e,n);return r=r.setSubMatrix(t,0,0),r}}r!==s&&console.warn(`Multiplying ${n} x ${r} and ${s} x ${i} matrix: dimensions do not match.`);let a=Math.max(n,s),u=Math.max(r,i);return e=o(e,a,u),function t(e,n,r,s){if(r<=512||s<=512)return e.mmul(n);r%2==1&&s%2==1?(e=o(e,r+1,s+1),n=o(n,r+1,s+1)):r%2==1?(e=o(e,r+1,s),n=o(n,r+1,s)):s%2==1&&(e=o(e,r,s+1),n=o(n,r,s+1));let i=parseInt(e.rows/2,10),a=parseInt(e.columns/2,10),u=e.subMatrix(0,i-1,0,a-1),l=n.subMatrix(0,i-1,0,a-1),h=e.subMatrix(0,i-1,a,e.columns-1),c=n.subMatrix(0,i-1,a,n.columns-1),f=e.subMatrix(i,e.rows-1,0,a-1),m=n.subMatrix(i,n.rows-1,0,a-1),g=e.subMatrix(i,e.rows-1,a,e.columns-1),d=n.subMatrix(i,n.rows-1,a,n.columns-1),p=t(x.add(u,g),x.add(l,d),i,a),w=t(x.add(f,g),l,i,a),y=t(u,x.sub(c,d),i,a),b=t(g,x.sub(m,l),i,a),M=t(x.add(u,h),d,i,a),v=t(x.sub(f,u),x.add(l,c),i,a),S=t(x.sub(h,g),x.add(m,d),i,a),A=x.add(p,b);A.sub(M),A.add(S);let E=x.add(y,M),I=x.add(w,b),N=x.sub(p,w);N.add(y),N.add(v);let R=x.zeros(2*A.rows,2*A.columns);return R=R.setSubMatrix(A,0,0),R=R.setSubMatrix(E,A.rows,0),R=R.setSubMatrix(I,0,A.columns),R=R.setSubMatrix(N,A.rows,A.columns),R.subMatrix(0,r-1,0,s-1)}(e,t=o(t,a,u),a,u)}scaleRows(t={}){if("object"!=typeof t)throw new TypeError("options must be an object");const{min:e=0,max:n=1}=t;if(!Number.isFinite(e))throw new TypeError("min must be a number");if(!Number.isFinite(n))throw new TypeError("max must be a number");if(e>=n)throw new RangeError("min must be smaller than max");let r=new S(this.rows,this.columns);for(let t=0;t<this.rows;t++){const i=this.getRow(t);i.length>0&&s(i,{min:e,max:n,output:i}),r.setRow(t,i)}return r}scaleColumns(t={}){if("object"!=typeof t)throw new TypeError("options must be an object");const{min:e=0,max:n=1}=t;if(!Number.isFinite(e))throw new TypeError("min must be a number");if(!Number.isFinite(n))throw new TypeError("max must be a number");if(e>=n)throw new RangeError("min must be smaller than max");let r=new S(this.rows,this.columns);for(let t=0;t<this.columns;t++){const i=this.getColumn(t);i.length&&s(i,{min:e,max:n,output:i}),r.setColumn(t,i)}return r}flipRows(){const t=Math.ceil(this.columns/2);for(let e=0;e<this.rows;e++)for(let n=0;n<t;n++){let t=this.get(e,n),r=this.get(e,this.columns-1-n);this.set(e,n,r),this.set(e,this.columns-1-n,t)}return this}flipColumns(){const t=Math.ceil(this.rows/2);for(let e=0;e<this.columns;e++)for(let n=0;n<t;n++){let t=this.get(n,e),r=this.get(this.rows-1-n,e);this.set(n,e,r),this.set(this.rows-1-n,e,t)}return this}kroneckerProduct(t){t=S.checkMatrix(t);let e=this.rows,n=this.columns,r=t.rows,s=t.columns,i=new S(e*r,n*s);for(let o=0;o<e;o++)for(let e=0;e<n;e++)for(let n=0;n<r;n++)for(let a=0;a<s;a++)i.set(r*o+n,s*e+a,this.get(o,e)*t.get(n,a));return i}kroneckerSum(t){if(t=S.checkMatrix(t),!this.isSquare()||!t.isSquare())throw new Error("Kronecker Sum needs two Square Matrices");let e=this.rows,n=t.rows,r=this.kroneckerProduct(S.eye(n,n)),s=S.eye(e,e).kroneckerProduct(t);return r.add(s)}transpose(){let t=new S(this.columns,this.rows);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)t.set(n,e,this.get(e,n));return t}sortRows(t=M){for(let e=0;e<this.rows;e++)this.setRow(e,this.getRow(e).sort(t));return this}sortColumns(t=M){for(let e=0;e<this.columns;e++)this.setColumn(e,this.getColumn(e).sort(t));return this}subMatrix(t,e,n,r){p(this,t,e,n,r);let s=new S(e-t+1,r-n+1);for(let i=t;i<=e;i++)for(let e=n;e<=r;e++)s.set(i-t,e-n,this.get(i,e));return s}subMatrixRow(t,e,n){if(void 0===e&&(e=0),void 0===n&&(n=this.columns-1),e>n||e<0||e>=this.columns||n<0||n>=this.columns)throw new RangeError("Argument out of range");let r=new S(t.length,n-e+1);for(let s=0;s<t.length;s++)for(let i=e;i<=n;i++){if(t[s]<0||t[s]>=this.rows)throw new RangeError(`Row index out of range: ${t[s]}`);r.set(s,i-e,this.get(t[s],i))}return r}subMatrixColumn(t,e,n){if(void 0===e&&(e=0),void 0===n&&(n=this.rows-1),e>n||e<0||e>=this.rows||n<0||n>=this.rows)throw new RangeError("Argument out of range");let r=new S(n-e+1,t.length);for(let s=0;s<t.length;s++)for(let i=e;i<=n;i++){if(t[s]<0||t[s]>=this.columns)throw new RangeError(`Column index out of range: ${t[s]}`);r.set(i-e,s,this.get(i,t[s]))}return r}setSubMatrix(t,e,n){if((t=S.checkMatrix(t)).isEmpty())return this;p(this,e,e+t.rows-1,n,n+t.columns-1);for(let r=0;r<t.rows;r++)for(let s=0;s<t.columns;s++)this.set(e+r,n+s,t.get(r,s));return this}selection(t,e){g(this,t),d(this,e);let n=new S(t.length,e.length);for(let r=0;r<t.length;r++){let s=t[r];for(let t=0;t<e.length;t++){let i=e[t];n.set(r,t,this.get(s,i))}}return n}trace(){let t=Math.min(this.rows,this.columns),e=0;for(let n=0;n<t;n++)e+=this.get(n,n);return e}clone(){return this.constructor.copy(this,new S(this.rows,this.columns))}static copy(t,e){for(const[n,r,s]of t.entries())e.set(n,r,s);return e}sum(t){switch(t){case"row":return function(t){let e=w(t.rows);for(let n=0;n<t.rows;++n)for(let r=0;r<t.columns;++r)e[n]+=t.get(n,r);return e}(this);case"column":return function(t){let e=w(t.columns);for(let n=0;n<t.rows;++n)for(let r=0;r<t.columns;++r)e[r]+=t.get(n,r);return e}(this);case void 0:return function(t){let e=0;for(let n=0;n<t.rows;n++)for(let r=0;r<t.columns;r++)e+=t.get(n,r);return e}(this);default:throw new Error(`invalid option: ${t}`)}}product(t){switch(t){case"row":return function(t){let e=w(t.rows,1);for(let n=0;n<t.rows;++n)for(let r=0;r<t.columns;++r)e[n]*=t.get(n,r);return e}(this);case"column":return function(t){let e=w(t.columns,1);for(let n=0;n<t.rows;++n)for(let r=0;r<t.columns;++r)e[r]*=t.get(n,r);return e}(this);case void 0:return function(t){let e=1;for(let n=0;n<t.rows;n++)for(let r=0;r<t.columns;r++)e*=t.get(n,r);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:n=!0,mean:s=this.mean(t)}=e;if("boolean"!=typeof n)throw new TypeError("unbiased must be a boolean");switch(t){case"row":if(!r.isAnyArray(s))throw new TypeError("mean must be an array");return function(t,e,n){const r=t.rows,s=t.columns,i=[];for(let o=0;o<r;o++){let r=0,a=0,u=0;for(let e=0;e<s;e++)u=t.get(o,e)-n[o],r+=u,a+=u*u;e?i.push((a-r*r/s)/(s-1)):i.push((a-r*r/s)/s)}return i}(this,n,s);case"column":if(!r.isAnyArray(s))throw new TypeError("mean must be an array");return function(t,e,n){const r=t.rows,s=t.columns,i=[];for(let o=0;o<s;o++){let s=0,a=0,u=0;for(let e=0;e<r;e++)u=t.get(e,o)-n[o],s+=u,a+=u*u;e?i.push((a-s*s/r)/(r-1)):i.push((a-s*s/r)/r)}return i}(this,n,s);case void 0:if("number"!=typeof s)throw new TypeError("mean must be a number");return function(t,e,n){const r=t.rows,s=t.columns,i=r*s;let o=0,a=0,u=0;for(let e=0;e<r;e++)for(let r=0;r<s;r++)u=t.get(e,r)-n,o+=u,a+=u*u;return e?(a-o*o/i)/(i-1):(a-o*o/i)/i}(this,n,s);default:throw new Error(`invalid option: ${t}`)}}standardDeviation(t,e){"object"==typeof t&&(e=t,t=void 0);const n=this.variance(t,e);if(void 0===t)return Math.sqrt(n);for(let t=0;t<n.length;t++)n[t]=Math.sqrt(n[t]);return n}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:n=this.mean(t)}=e;switch(t){case"row":if(!r.isAnyArray(n))throw new TypeError("center must be an array");return function(t,e){for(let n=0;n<t.rows;n++)for(let r=0;r<t.columns;r++)t.set(n,r,t.get(n,r)-e[n])}(this,n),this;case"column":if(!r.isAnyArray(n))throw new TypeError("center must be an array");return function(t,e){for(let n=0;n<t.rows;n++)for(let r=0;r<t.columns;r++)t.set(n,r,t.get(n,r)-e[r])}(this,n),this;case void 0:if("number"!=typeof n)throw new TypeError("center must be a number");return function(t,e){for(let n=0;n<t.rows;n++)for(let r=0;r<t.columns;r++)t.set(n,r,t.get(n,r)-e)}(this,n),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 n=e.scale;switch(t){case"row":if(void 0===n)n=function(t){const e=[];for(let n=0;n<t.rows;n++){let r=0;for(let e=0;e<t.columns;e++)r+=t.get(n,e)**2/(t.columns-1);e.push(Math.sqrt(r))}return e}(this);else if(!r.isAnyArray(n))throw new TypeError("scale must be an array");return function(t,e){for(let n=0;n<t.rows;n++)for(let r=0;r<t.columns;r++)t.set(n,r,t.get(n,r)/e[n])}(this,n),this;case"column":if(void 0===n)n=function(t){const e=[];for(let n=0;n<t.columns;n++){let r=0;for(let e=0;e<t.rows;e++)r+=t.get(e,n)**2/(t.rows-1);e.push(Math.sqrt(r))}return e}(this);else if(!r.isAnyArray(n))throw new TypeError("scale must be an array");return function(t,e){for(let n=0;n<t.rows;n++)for(let r=0;r<t.columns;r++)t.set(n,r,t.get(n,r)/e[r])}(this,n),this;case void 0:if(void 0===n)n=function(t){const e=t.size-1;let n=0;for(let r=0;r<t.columns;r++)for(let s=0;s<t.rows;s++)n+=t.get(s,r)**2/e;return Math.sqrt(n)}(this);else if("number"!=typeof n)throw new TypeError("scale must be a number");return function(t,e){for(let n=0;n<t.rows;n++)for(let r=0;r<t.columns;r++)t.set(n,r,t.get(n,r)/e)}(this,n),this;default:throw new Error(`invalid option: ${t}`)}}toString(t){return a(this,t)}[Symbol.iterator](){return this.entries()}*entries(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)yield[t,e,this.get(t,e)]}*values(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)yield this.get(t,e)}}function M(t,e){return t-e}function v(t){return t.every(t=>"number"==typeof t)}x.prototype.klass="Matrix","undefined"!=typeof Symbol&&(x.prototype[Symbol.for("nodejs.util.inspect.custom")]=function(){return a(this)}),x.random=x.rand,x.randomInt=x.randInt,x.diagonal=x.diag,x.prototype.diagonal=x.prototype.diag,x.identity=x.eye,x.prototype.negate=x.prototype.neg,x.prototype.tensorProduct=x.prototype.kroneckerProduct;class S extends x{data;#t(t,e){if(this.data=[],!(Number.isInteger(e)&&e>=0))throw new TypeError("nColumns must be a positive integer");for(let n=0;n<t;n++)this.data.push(new Float64Array(e));this.rows=t,this.columns=e}constructor(t,e){if(super(),S.isMatrix(t))this.#t(t.rows,t.columns),S.copy(t,this);else if(Number.isInteger(t)&&t>=0)this.#t(t,e);else{if(!r.isAnyArray(t))throw new TypeError("First argument must be a positive number or an array");{const n=t;if("number"!=typeof(e=(t=n.length)?n[0].length:0))throw new TypeError("Data must be a 2D array with at least one element");this.data=[];for(let r=0;r<t;r++){if(n[r].length!==e)throw new RangeError("Inconsistent array dimensions");if(!v(n[r]))throw new TypeError("Input data contains non-numeric values");this.data.push(Float64Array.from(n[r]))}this.rows=t,this.columns=e}}}set(t,e,n){return this.data[t][e]=n,this}get(t,e){return this.data[t][e]}removeRow(t){return h(this,t),this.data.splice(t,1),this.rows-=1,this}addRow(t,e){return void 0===e&&(e=t,t=this.rows),h(this,t,!0),e=Float64Array.from(f(this,e)),this.data.splice(t,0,e),this.rows+=1,this}removeColumn(t){c(this,t);for(let e=0;e<this.rows;e++){const n=new Float64Array(this.columns-1);for(let r=0;r<t;r++)n[r]=this.data[e][r];for(let r=t+1;r<this.columns;r++)n[r-1]=this.data[e][r];this.data[e]=n}return this.columns-=1,this}addColumn(t,e){void 0===e&&(e=t,t=this.columns),c(this,t,!0),e=m(this,e);for(let n=0;n<this.rows;n++){const r=new Float64Array(this.columns+1);let s=0;for(;s<t;s++)r[s]=this.data[n][s];for(r[s++]=e[n];s<this.columns+1;s++)r[s]=this.data[n][s-1];this.data[n]=r}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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)+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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)+t.get(e,n));return this},t.add=function(t,n){return new e(t).add(n)},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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)-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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)-t.get(e,n));return this},t.sub=function(t,n){return new e(t).sub(n)},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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)*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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)*t.get(e,n));return this},t.mul=function(t,n){return new e(t).mul(n)},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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)/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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)/t.get(e,n));return this},t.div=function(t,n){return new e(t).div(n)},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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)%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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)%t.get(e,n));return this},t.mod=function(t,n){return new e(t).mod(n)},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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)&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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)&t.get(e,n));return this},t.and=function(t,n){return new e(t).and(n)},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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)|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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)|t.get(e,n));return this},t.or=function(t,n){return new e(t).or(n)},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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)^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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)^t.get(e,n));return this},t.xor=function(t,n){return new e(t).xor(n)},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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)<<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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)<<t.get(e,n));return this},t.leftShift=function(t,n){return new e(t).leftShift(n)},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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)>>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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)>>t.get(e,n));return this},t.signPropagatingRightShift=function(t,n){return new e(t).signPropagatingRightShift(n)},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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)>>>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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)>>>t.get(e,n));return this},t.rightShift=function(t,n){return new e(t).rightShift(n)},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,n){return new e(t).pow(n)},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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)**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 n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)**t.get(e,n));return this}}(x,S);class A extends x{#e;get size(){return this.#e.size}get rows(){return this.#e.rows}get columns(){return this.#e.columns}get diagonalSize(){return this.rows}static isSymmetricMatrix(t){return S.isMatrix(t)&&"SymmetricMatrix"===t.klassType}static zeros(t){return new this(t)}static ones(t){return new this(t).fill(1)}constructor(t){if(super(),S.isMatrix(t)){if(!t.isSymmetric())throw new TypeError("not symmetric data");this.#e=S.copy(t,new S(t.rows,t.rows))}else if(Number.isInteger(t)&&t>=0)this.#e=new S(t,t);else if(this.#e=new S(t),!this.isSymmetric())throw new TypeError("not symmetric data")}clone(){const t=new A(this.diagonalSize);for(const[e,n,r]of this.upperRightEntries())t.set(e,n,r);return t}toMatrix(){return new S(this)}get(t,e){return this.#e.get(t,e)}set(t,e,n){return this.#e.set(t,e,n),this.#e.set(e,t,n),this}removeCross(t){return this.#e.removeRow(t),this.#e.removeColumn(t),this}addCross(t,e){void 0===e&&(e=t,t=this.diagonalSize);const n=e.slice();return n.splice(t,1),this.#e.addRow(t,n),this.#e.addColumn(t,e),this}applyMask(t){if(t.length!==this.diagonalSize)throw new RangeError("Mask size do not match with matrix size");const e=[];for(const[n,r]of t.entries())r||e.push(n);e.reverse();for(const t of e)this.removeCross(t);return this}toCompact(){const{diagonalSize:t}=this,e=new Array(t*(t+1)/2);for(let n=0,r=0,s=0;s<e.length;s++)e[s]=this.get(r,n),++n>=t&&(n=++r);return e}static fromCompact(t){const e=t.length,n=(Math.sqrt(8*e+1)-1)/2;if(!Number.isInteger(n))throw new TypeError(`This array is not a compact representation of a Symmetric Matrix, ${JSON.stringify(t)}`);const r=new A(n);for(let s=0,i=0,o=0;o<e;o++)r.set(s,i,t[o]),++s>=n&&(s=++i);return r}*upperRightEntries(){for(let t=0,e=0;t<this.diagonalSize;void 0){const n=this.get(t,e);yield[t,e,n],++e>=this.diagonalSize&&(e=++t)}}*upperRightValues(){for(let t=0,e=0;t<this.diagonalSize;void 0){const n=this.get(t,e);yield n,++e>=this.diagonalSize&&(e=++t)}}}A.prototype.klassType="SymmetricMatrix";class E extends A{static isDistanceMatrix(t){return A.isSymmetricMatrix(t)&&"DistanceMatrix"===t.klassSubType}constructor(t){if(super(t),!this.isDistance())throw new TypeError("Provided arguments do no produce a distance matrix")}set(t,e,n){return t===e&&(n=0),super.set(t,e,n)}addCross(t,e){return void 0===e&&(e=t,t=this.diagonalSize),(e=e.slice())[t]=0,super.addCross(t,e)}toSymmetricMatrix(){return new A(this)}clone(){const t=new E(this.diagonalSize);for(const[e,n,r]of this.upperRightEntries())e!==n&&t.set(e,n,r);return t}toCompact(){const{diagonalSize:t}=this,e=new Array((t-1)*t/2);for(let n=1,r=0,s=0;s<e.length;s++)e[s]=this.get(r,n),++n>=t&&(n=1+ ++r);return e}static fromCompact(t){const e=t.length;if(0===e)return new this(0);const n=(Math.sqrt(8*e+1)+1)/2;if(!Number.isInteger(n))throw new TypeError(`This array is not a compact representation of a DistanceMatrix, ${JSON.stringify(t)}`);const r=new this(n);for(let s=1,i=0,o=0;o<e;o++)r.set(s,i,t[o]),++s>=n&&(s=1+ ++i);return r}}E.prototype.klassSubType="DistanceMatrix";class I extends x{constructor(t,e,n){super(),this.matrix=t,this.rows=e,this.columns=n}}class N extends I{constructor(t,e,n){g(t,e),d(t,n),super(t,e.length,n.length),this.rowIndices=e,this.columnIndices=n}set(t,e,n){return this.matrix.set(this.rowIndices[t],this.columnIndices[e],n),this}get(t,e){return this.matrix.get(this.rowIndices[t],this.columnIndices[e])}}class R extends x{constructor(t,e={}){const{rows:n=1}=e;if(t.length%n!==0)throw new Error("the data length is not divisible by the number of rows");super(),this.rows=n,this.columns=t.length/n,this.data=t}set(t,e,n){let r=this._calculateIndex(t,e);return this.data[r]=n,this}get(t,e){let n=this._calculateIndex(t,e);return this.data[n]}_calculateIndex(t,e){return t*this.columns+e}}class k extends x{constructor(t){super(),this.data=t,this.rows=t.length,this.columns=t[0].length}set(t,e,n){return this.data[t][e]=n,this}get(t,e){return this.data[t][e]}}class C{constructor(t){let e,n,r,s,i,o,a,u,l,h=(t=k.checkMatrix(t)).clone(),c=h.rows,f=h.columns,m=new Float64Array(c),g=1;for(e=0;e<c;e++)m[e]=e;for(u=new Float64Array(c),n=0;n<f;n++){for(e=0;e<c;e++)u[e]=h.get(e,n);for(e=0;e<c;e++){for(l=Math.min(e,n),i=0,r=0;r<l;r++)i+=h.get(e,r)*u[r];u[e]-=i,h.set(e,n,u[e])}for(s=n,e=n+1;e<c;e++)Math.abs(u[e])>Math.abs(u[s])&&(s=e);if(s!==n){for(r=0;r<f;r++)o=h.get(s,r),h.set(s,r,h.get(n,r)),h.set(n,r,o);a=m[s],m[s]=m[n],m[n]=a,g=-g}if(n<c&&0!==h.get(n,n))for(e=n+1;e<c;e++)h.set(e,n,h.get(e,n)/h.get(n,n))}this.LU=h,this.pivotVector=m,this.pivotSign=g}isSingular(){let t=this.LU,e=t.columns;for(let n=0;n<e;n++)if(0===t.get(n,n))return!0;return!1}solve(t){t=S.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 n,r,s,i=t.columns,o=t.subMatrixRow(this.pivotVector,0,i-1),a=e.columns;for(s=0;s<a;s++)for(n=s+1;n<a;n++)for(r=0;r<i;r++)o.set(n,r,o.get(n,r)-o.get(s,r)*e.get(n,s));for(s=a-1;s>=0;s--){for(r=0;r<i;r++)o.set(s,r,o.get(s,r)/e.get(s,s));for(n=0;n<s;n++)for(r=0;r<i;r++)o.set(n,r,o.get(n,r)-o.get(s,r)*e.get(n,s))}return o}get determinant(){let t=this.LU;if(!t.isSquare())throw new Error("Matrix must be square");let e=this.pivotSign,n=t.columns;for(let r=0;r<n;r++)e*=t.get(r,r);return e}get lowerTriangularMatrix(){let t=this.LU,e=t.rows,n=t.columns,r=new S(e,n);for(let s=0;s<e;s++)for(let e=0;e<n;e++)s>e?r.set(s,e,t.get(s,e)):s===e?r.set(s,e,1):r.set(s,e,0);return r}get upperTriangularMatrix(){let t=this.LU,e=t.rows,n=t.columns,r=new S(e,n);for(let s=0;s<e;s++)for(let e=0;e<n;e++)s<=e?r.set(s,e,t.get(s,e)):r.set(s,e,0);return r}get pivotPermutationVector(){return Array.from(this.pivotVector)}}function T(t,e){let n=0;return Math.abs(t)>Math.abs(e)?(n=e/t,Math.abs(t)*Math.sqrt(1+n*n)):0!==e?(n=t/e,Math.abs(e)*Math.sqrt(1+n*n)):0}class P{constructor(t){let e,n,r,s,i=(t=k.checkMatrix(t)).clone(),o=t.rows,a=t.columns,u=new Float64Array(a);for(r=0;r<a;r++){let t=0;for(e=r;e<o;e++)t=T(t,i.get(e,r));if(0!==t){for(i.get(r,r)<0&&(t=-t),e=r;e<o;e++)i.set(e,r,i.get(e,r)/t);for(i.set(r,r,i.get(r,r)+1),n=r+1;n<a;n++){for(s=0,e=r;e<o;e++)s+=i.get(e,r)*i.get(e,n);for(s=-s/i.get(r,r),e=r;e<o;e++)i.set(e,n,i.get(e,n)+s*i.get(e,r))}}u[r]=-t}this.QR=i,this.Rdiag=u}solve(t){t=S.checkMatrix(t);let e=this.QR,n=e.rows;if(t.rows!==n)throw new Error("Matrix row dimensions must agree");if(!this.isFullRank())throw new Error("Matrix is rank deficient");let r,s,i,o,a=t.columns,u=t.clone(),l=e.columns;for(i=0;i<l;i++)for(s=0;s<a;s++){for(o=0,r=i;r<n;r++)o+=e.get(r,i)*u.get(r,s);for(o=-o/e.get(i,i),r=i;r<n;r++)u.set(r,s,u.get(r,s)+o*e.get(r,i))}for(i=l-1;i>=0;i--){for(s=0;s<a;s++)u.set(i,s,u.get(i,s)/this.Rdiag[i]);for(r=0;r<i;r++)for(s=0;s<a;s++)u.set(r,s,u.get(r,s)-u.get(i,s)*e.get(r,i))}return u.subMatrix(0,l-1,0,a-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,n=this.QR,r=n.columns,s=new S(r,r);for(t=0;t<r;t++)for(e=0;e<r;e++)t<e?s.set(t,e,n.get(t,e)):t===e?s.set(t,e,this.Rdiag[t]):s.set(t,e,0);return s}get orthogonalMatrix(){let t,e,n,r,s=this.QR,i=s.rows,o=s.columns,a=new S(i,o);for(n=o-1;n>=0;n--){for(t=0;t<i;t++)a.set(t,n,0);for(a.set(n,n,1),e=n;e<o;e++)if(0!==s.get(n,n)){for(r=0,t=n;t<i;t++)r+=s.get(t,n)*a.get(t,e);for(r=-r/s.get(n,n),t=n;t<i;t++)a.set(t,e,a.get(t,e)+r*s.get(t,n))}}return a}}class D{constructor(t,e={}){if((t=k.checkMatrix(t)).isEmpty())throw new Error("Matrix must be non-empty");let n=t.rows,r=t.columns;const{computeLeftSingularVectors:s=!0,computeRightSingularVectors:i=!0,autoTranspose:o=!1}=e;let a,u=Boolean(s),l=Boolean(i),h=!1;if(n<r)if(o){a=t.transpose(),n=a.rows,r=a.columns,h=!0;let e=u;u=l,l=e}else a=t.clone(),console.warn("Computing SVD on a matrix with more columns than rows. Consider enabling autoTranspose");else a=t.clone();let c=Math.min(n,r),f=Math.min(n+1,r),m=new Float64Array(f),g=new S(n,c),d=new S(r,r),p=new Float64Array(r),w=new Float64Array(n),y=new Float64Array(f);for(let t=0;t<f;t++)y[t]=t;let b=Math.min(n-1,r),x=Math.max(0,Math.min(r-2,n)),M=Math.max(b,x);for(let t=0;t<M;t++){if(t<b){m[t]=0;for(let e=t;e<n;e++)m[t]=T(m[t],a.get(e,t));if(0!==m[t]){a.get(t,t)<0&&(m[t]=-m[t]);for(let e=t;e<n;e++)a.set(e,t,a.get(e,t)/m[t]);a.set(t,t,a.get(t,t)+1)}m[t]=-m[t]}for(let e=t+1;e<r;e++){if(t<b&&0!==m[t]){let r=0;for(let s=t;s<n;s++)r+=a.get(s,t)*a.get(s,e);r=-r/a.get(t,t);for(let s=t;s<n;s++)a.set(s,e,a.get(s,e)+r*a.get(s,t))}p[e]=a.get(t,e)}if(u&&t<b)for(let e=t;e<n;e++)g.set(e,t,a.get(e,t));if(t<x){p[t]=0;for(let e=t+1;e<r;e++)p[t]=T(p[t],p[e]);if(0!==p[t]){p[t+1]<0&&(p[t]=0-p[t]);for(let e=t+1;e<r;e++)p[e]/=p[t];p[t+1]+=1}if(p[t]=-p[t],t+1<n&&0!==p[t]){for(let e=t+1;e<n;e++)w[e]=0;for(let e=t+1;e<n;e++)for(let n=t+1;n<r;n++)w[e]+=p[n]*a.get(e,n);for(let e=t+1;e<r;e++){let r=-p[e]/p[t+1];for(let s=t+1;s<n;s++)a.set(s,e,a.get(s,e)+r*w[s])}}if(l)for(let e=t+1;e<r;e++)d.set(e,t,p[e])}}let v=Math.min(r,n+1);if(b<r&&(m[b]=a.get(b,b)),n<v&&(m[v-1]=0),x+1<v&&(p[x]=a.get(x,v-1)),p[v-1]=0,u){for(let t=b;t<c;t++){for(let e=0;e<n;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 r=0;for(let s=t;s<n;s++)r+=g.get(s,t)*g.get(s,e);r=-r/g.get(t,t);for(let s=t;s<n;s++)g.set(s,e,g.get(s,e)+r*g.get(s,t))}for(let e=t;e<n;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<n;e++)g.set(e,t,0);g.set(t,t,1)}}if(l)for(let t=r-1;t>=0;t--){if(t<x&&0!==p[t])for(let e=t+1;e<r;e++){let n=0;for(let s=t+1;s<r;s++)n+=d.get(s,t)*d.get(s,e);n=-n/d.get(t+1,t);for(let s=t+1;s<r;s++)d.set(s,e,d.get(s,e)+n*d.get(s,t))}for(let e=0;e<r;e++)d.set(e,t,0);d.set(t,t,1)}let A=v-1,E=Number.EPSILON;for(;v>0;){let t,e;for(t=v-2;t>=-1&&-1!==t;t--){const e=Number.MIN_VALUE+E*Math.abs(m[t]+Math.abs(m[t+1]));if(Math.abs(p[t])<=e||Number.isNaN(p[t])){p[t]=0;break}}if(t===v-2)e=4;else{let n;for(n=v-1;n>=t&&n!==t;n--){let e=(n!==v?Math.abs(p[n]):0)+(n!==t+1?Math.abs(p[n-1]):0);if(Math.abs(m[n])<=E*e){m[n]=0;break}}n===t?e=3:n===v-1?e=1:(e=2,t=n)}switch(t++,e){case 1:{let e=p[v-2];p[v-2]=0;for(let n=v-2;n>=t;n--){let s=T(m[n],e),i=m[n]/s,o=e/s;if(m[n]=s,n!==t&&(e=-o*p[n-1],p[n-1]=i*p[n-1]),l)for(let t=0;t<r;t++)s=i*d.get(t,n)+o*d.get(t,v-1),d.set(t,v-1,-o*d.get(t,n)+i*d.get(t,v-1)),d.set(t,n,s)}break}case 2:{let e=p[t-1];p[t-1]=0;for(let r=t;r<v;r++){let s=T(m[r],e),i=m[r]/s,o=e/s;if(m[r]=s,e=-o*p[r],p[r]=i*p[r],u)for(let e=0;e<n;e++)s=i*g.get(e,r)+o*g.get(e,t-1),g.set(e,t-1,-o*g.get(e,r)+i*g.get(e,t-1)),g.set(e,r,s)}break}case 3:{const e=Math.max(Math.abs(m[v-1]),Math.abs(m[v-2]),Math.abs(p[v-2]),Math.abs(m[t]),Math.abs(p[t])),s=m[v-1]/e,i=m[v-2]/e,o=p[v-2]/e,a=m[t]/e,h=p[t]/e,c=((i+s)*(i-s)+o*o)/2,f=s*o*(s*o);let w=0;0===c&&0===f||(w=c<0?0-Math.sqrt(c*c+f):Math.sqrt(c*c+f),w=f/(c+w));let y=(a+s)*(a-s)+w,b=a*h;for(let e=t;e<v-1;e++){let s=T(y,b);0===s&&(s=Number.MIN_VALUE);let i=y/s,o=b/s;if(e!==t&&(p[e-1]=s),y=i*m[e]+o*p[e],p[e]=i*p[e]-o*m[e],b=o*m[e+1],m[e+1]=i*m[e+1],l)for(let t=0;t<r;t++)s=i*d.get(t,e)+o*d.get(t,e+1),d.set(t,e+1,-o*d.get(t,e)+i*d.get(t,e+1)),d.set(t,e,s);if(s=T(y,b),0===s&&(s=Number.MIN_VALUE),i=y/s,o=b/s,m[e]=s,y=i*p[e]+o*m[e+1],m[e+1]=-o*p[e]+i*m[e+1],b=o*p[e+1],p[e+1]=i*p[e+1],u&&e<n-1)for(let t=0;t<n;t++)s=i*g.get(t,e)+o*g.get(t,e+1),g.set(t,e+1,-o*g.get(t,e)+i*g.get(t,e+1)),g.set(t,e,s)}p[v-2]=y;break}case 4:if(m[t]<=0&&(m[t]=m[t]<0?-m[t]:0,l))for(let e=0;e<=A;e++)d.set(e,t,-d.get(e,t));for(;t<A&&!(m[t]>=m[t+1]);){let e=m[t];if(m[t]=m[t+1],m[t+1]=e,l&&t<r-1)for(let n=0;n<r;n++)e=d.get(n,t+1),d.set(n,t+1,d.get(n,t)),d.set(n,t,e);if(u&&t<n-1)for(let r=0;r<n;r++)e=g.get(r,t+1),g.set(r,t+1,g.get(r,t)),g.set(r,t,e);t++}v--}}if(h){let t=d;d=g,g=t}this.m=n,this.n=r,this.s=m,this.U=g,this.V=d}solve(t){let e=t,n=this.threshold,r=this.s.length,s=S.zeros(r,r);for(let t=0;t<r;t++)Math.abs(this.s[t])<=n?s.set(t,t,0):s.set(t,t,1/this.s[t]);let i=this.U,o=this.rightSingularVectors,a=o.mmul(s),u=o.rows,l=i.rows,h=S.zeros(u,l);for(let t=0;t<u;t++)for(let e=0;e<l;e++){let n=0;for(let s=0;s<r;s++)n+=a.get(t,s)*i.get(e,s);h.set(t,e,n)}return h.mmul(e)}solveForDiagonal(t){return this.solve(S.diag(t))}inverse(){let t=this.V,e=this.threshold,n=t.rows,r=t.columns,s=new S(n,this.s.length);for(let i=0;i<n;i++)for(let n=0;n<r;n++)Math.abs(this.s[n])>e&&s.set(i,n,t.get(i,n)/this.s[n]);let i=this.U,o=i.rows,a=i.columns,u=new S(n,o);for(let t=0;t<n;t++)for(let e=0;e<o;e++){let n=0;for(let r=0;r<a;r++)n+=s.get(t,r)*i.get(e,r);u.set(t,e,n)}return u}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,n=this.s;for(let r=0,s=n.length;r<s;r++)n[r]>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 S.diag(this.s)}}function U(t,e,n=!1){return t=k.checkMatrix(t),e=k.checkMatrix(e),n?new D(t).solve(e):t.isSquare()?new C(t).solve(e):new P(t).solve(e)}function O(t,e){let n=[];for(let r=0;r<t;r++)r!==e&&n.push(r);return n}function z(t,e,n,r=1e-9,s=1e-9){if(t>s)return new Array(e.rows+1).fill(0);{let t=e.addRow(n,[0]);for(let e=0;e<t.rows;e++)Math.abs(t.get(e,0))<r&&t.set(e,0,0);return t.to1DArray()}}class B{constructor(t,e={}){const{assumeSymmetric:n=!1}=e;if(!(t=k.checkMatrix(t)).isSquare())throw new Error("Matrix is not a square matrix");if(t.isEmpty())throw new Error("Matrix must be non-empty");let r,s,i=t.columns,o=new S(i,i),a=new Float64Array(i),u=new Float64Array(i),l=t,h=!1;if(h=!!n||t.isSymmetric(),h){for(r=0;r<i;r++)for(s=0;s<i;s++)o.set(r,s,l.get(r,s));!function(t,e,n,r){let s,i,o,a,u,l,h,c;for(u=0;u<t;u++)n[u]=r.get(t-1,u);for(a=t-1;a>0;a--){for(c=0,o=0,l=0;l<a;l++)c+=Math.abs(n[l]);if(0===c)for(e[a]=n[a-1],u=0;u<a;u++)n[u]=r.get(a-1,u),r.set(a,u,0),r.set(u,a,0);else{for(l=0;l<a;l++)n[l]/=c,o+=n[l]*n[l];for(s=n[a-1],i=Math.sqrt(o),s>0&&(i=-i),e[a]=c*i,o-=s*i,n[a-1]=s-i,u=0;u<a;u++)e[u]=0;for(u=0;u<a;u++){for(s=n[u],r.set(u,a,s),i=e[u]+r.get(u,u)*s,l=u+1;l<=a-1;l++)i+=r.get(l,u)*n[l],e[l]+=r.get(l,u)*s;e[u]=i}for(s=0,u=0;u<a;u++)e[u]/=o,s+=e[u]*n[u];for(h=s/(o+o),u=0;u<a;u++)e[u]-=h*n[u];for(u=0;u<a;u++){for(s=n[u],i=e[u],l=u;l<=a-1;l++)r.set(l,u,r.get(l,u)-(s*e[l]+i*n[l]));n[u]=r.get(a-1,u),r.set(a,u,0)}}n[a]=o}for(a=0;a<t-1;a++){if(r.set(t-1,a,r.get(a,a)),r.set(a,a,1),o=n[a+1],0!==o){for(l=0;l<=a;l++)n[l]=r.get(l,a+1)/o;for(u=0;u<=a;u++){for(i=0,l=0;l<=a;l++)i+=r.get(l,a+1)*r.get(l,u);for(l=0;l<=a;l++)r.set(l,u,r.get(l,u)-i*n[l])}}for(l=0;l<=a;l++)r.set(l,a+1,0)}for(u=0;u<t;u++)n[u]=r.get(t-1,u),r.set(t-1,u,0);r.set(t-1,t-1,1),e[0]=0}(i,u,a,o),function(t,e,n,r){let s,i,o,a,u,l,h,c,f,m,g,d,p,w,y,b;for(o=1;o<t;o++)e[o-1]=e[o];e[t-1]=0;let x=0,M=0,v=Number.EPSILON;for(l=0;l<t;l++){for(M=Math.max(M,Math.abs(n[l])+Math.abs(e[l])),h=l;h<t&&!(Math.abs(e[h])<=v*M);)h++;if(h>l)do{for(s=n[l],c=(n[l+1]-s)/(2*e[l]),f=T(c,1),c<0&&(f=-f),n[l]=e[l]/(c+f),n[l+1]=e[l]*(c+f),m=n[l+1],i=s-n[l],o=l+2;o<t;o++)n[o]-=i;for(x+=i,c=n[h],g=1,d=g,p=g,w=e[l+1],y=0,b=0,o=h-1;o>=l;o--)for(p=d,d=g,b=y,s=g*e[o],i=g*c,f=T(c,e[o]),e[o+1]=y*f,y=e[o]/f,g=c/f,c=g*n[o]-y*s,n[o+1]=i+y*(g*s+y*n[o]),u=0;u<t;u++)i=r.get(u,o+1),r.set(u,o+1,y*r.get(u,o)+g*i),r.set(u,o,g*r.get(u,o)-y*i);c=-y*b*p*w*e[l]/m,e[l]=y*c,n[l]=g*c}while(Math.abs(e[l])>v*M);n[l]=n[l]+x,e[l]=0}for(o=0;o<t-1;o++){for(u=o,c=n[o],a=o+1;a<t;a++)n[a]<c&&(u=a,c=n[a]);if(u!==o)for(n[u]=n[o],n[o]=c,a=0;a<t;a++)c=r.get(a,o),r.set(a,o,r.get(a,u)),r.set(a,u,c)}}(i,u,a,o)}else{let t=new S(i,i),e=new Float64Array(i);for(s=0;s<i;s++)for(r=0;r<i;r++)t.set(r,s,l.get(r,s));!function(t,e,n,r){let s,i,o,a,u,l,h,c=t-1;for(l=1;l<=c-1;l++){for(h=0,a=l;a<=c;a++)h+=Math.abs(e.get(a,l-1));if(0!==h){for(o=0,a=c;a>=l;a--)n[a]=e.get(a,l-1)/h,o+=n[a]*n[a];for(i=Math.sqrt(o),n[l]>0&&(i=-i),o-=n[l]*i,n[l]=n[l]-i,u=l;u<t;u++){for(s=0,a=c;a>=l;a--)s+=n[a]*e.get(a,u);for(s/=o,a=l;a<=c;a++)e.set(a,u,e.get(a,u)-s*n[a])}for(a=0;a<=c;a++){for(s=0,u=c;u>=l;u--)s+=n[u]*e.get(a,u);for(s/=o,u=l;u<=c;u++)e.set(a,u,e.get(a,u)-s*n[u])}n[l]=h*n[l],e.set(l,l-1,h*i)}}for(a=0;a<t;a++)for(u=0;u<t;u++)r.set(a,u,a===u?1:0);for(l=c-1;l>=1;l--)if(0!==e.get(l,l-1)){for(a=l+1;a<=c;a++)n[a]=e.get(a,l-1);for(u=l;u<=c;u++){for(i=0,a=l;a<=c;a++)i+=n[a]*r.get(a,u);for(i=i/n[l]/e.get(l,l-1),a=l;a<=c;a++)r.set(a,u,r.get(a,u)+i*n[a])}}}(i,t,e,o),function(t,e,n,r,s){let i,o,a,u,l,h,c,f,m,g,d,p,w,y,b,x=t-1,M=t-1,v=Number.EPSILON,S=0,A=0,E=0,I=0,N=0,R=0,k=0,C=0;for(i=0;i<t;i++)for((i<0||i>M)&&(n[i]=s.get(i,i),e[i]=0),o=Math.max(i-1,0);o<t;o++)A+=Math.abs(s.get(i,o));for(;x>=0;){for(u=x;u>0&&(R=Math.abs(s.get(u-1,u-1))+Math.abs(s.get(u,u)),0===R&&(R=A),!(Math.abs(s.get(u,u-1))<v*R));)u--;if(u===x)s.set(x,x,s.get(x,x)+S),n[x]=s.get(x,x),e[x]=0,x--,C=0;else if(u===x-1){if(c=s.get(x,x-1)*s.get(x-1,x),E=(s.get(x-1,x-1)-s.get(x,x))/2,I=E*E+c,k=Math.sqrt(Math.abs(I)),s.set(x,x,s.get(x,x)+S),s.set(x-1,x-1,s.get(x-1,x-1)+S),f=s.get(x,x),I>=0){for(k=E>=0?E+k:E-k,n[x-1]=f+k,n[x]=n[x-1],0!==k&&(n[x]=f-c/k),e[x-1]=0,e[x]=0,f=s.get(x,x-1),R=Math.abs(f)+Math.abs(k),E=f/R,I=k/R,N=Math.sqrt(E*E+I*I),E/=N,I/=N,o=x-1;o<t;o++)k=s.get(x-1,o),s.set(x-1,o,I*k+E*s.get(x,o)),s.set(x,o,I*s.get(x,o)-E*k);for(i=0;i<=x;i++)k=s.get(i,x-1),s.set(i,x-1,I*k+E*s.get(i,x)),s.set(i,x,I*s.get(i,x)-E*k);for(i=0;i<=M;i++)k=r.get(i,x-1),r.set(i,x-1,I*k+E*r.get(i,x)),r.set(i,x,I*r.get(i,x)-E*k)}else n[x-1]=f+E,n[x]=f+E,e[x-1]=k,e[x]=-k;x-=2,C=0}else{if(f=s.get(x,x),m=0,c=0,u<x&&(m=s.get(x-1,x-1),c=s.get(x,x-1)*s.get(x-1,x)),10===C){for(S+=f,i=0;i<=x;i++)s.set(i,i,s.get(i,i)-f);R=Math.abs(s.get(x,x-1))+Math.abs(s.get(x-1,x-2)),f=m=.75*R,c=-.4375*R*R}if(30===C&&(R=(m-f)/2,R=R*R+c,R>0)){for(R=Math.sqrt(R),m<f&&(R=-R),R=f-c/((m-f)/2+R),i=0;i<=x;i++)s.set(i,i,s.get(i,i)-R);S+=R,f=m=c=.964}for(C+=1,l=x-2;l>=u&&(k=s.get(l,l),N=f-k,R=m-k,E=(N*R-c)/s.get(l+1,l)+s.get(l,l+1),I=s.get(l+1,l+1)-k-N-R,N=s.get(l+2,l+1),R=Math.abs(E)+Math.abs(I)+Math.abs(N),E/=R,I/=R,N/=R,l!==u)&&!(Math.abs(s.get(l,l-1))*(Math.abs(I)+Math.abs(N))<v*(Math.abs(E)*(Math.abs(s.get(l-1,l-1))+Math.abs(k)+Math.abs(s.get(l+1,l+1)))));)l--;for(i=l+2;i<=x;i++)s.set(i,i-2,0),i>l+2&&s.set(i,i-3,0);for(a=l;a<=x-1&&(y=a!==x-1,a!==l&&(E=s.get(a,a-1),I=s.get(a+1,a-1),N=y?s.get(a+2,a-1):0,f=Math.abs(E)+Math.abs(I)+Math.abs(N),0!==f&&(E/=f,I/=f,N/=f)),0!==f);a++)if(R=Math.sqrt(E*E+I*I+N*N),E<0&&(R=-R),0!==R){for(a!==l?s.set(a,a-1,-R*f):u!==l&&s.set(a,a-1,-s.get(a,a-1)),E+=R,f=E/R,m=I/R,k=N/R,I/=E,N/=E,o=a;o<t;o++)E=s.get(a,o)+I*s.get(a+1,o),y&&(E+=N*s.get(a+2,o),s.set(a+2,o,s.get(a+2,o)-E*k)),s.set(a,o,s.get(a,o)-E*f),s.set(a+1,o,s.get(a+1,o)-E*m);for(i=0;i<=Math.min(x,a+3);i++)E=f*s.get(i,a)+m*s.get(i,a+1),y&&(E+=k*s.get(i,a+2),s.set(i,a+2,s.get(i,a+2)-E*N)),s.set(i,a,s.get(i,a)-E),s.set(i,a+1,s.get(i,a+1)-E*I);for(i=0;i<=M;i++)E=f*r.get(i,a)+m*r.get(i,a+1),y&&(E+=k*r.get(i,a+2),r.set(i,a+2,r.get(i,a+2)-E*N)),r.set(i,a,r.get(i,a)-E),r.set(i,a+1,r.get(i,a+1)-E*I)}}}if(0!==A){for(x=t-1;x>=0;x--)if(E=n[x],I=e[x],0===I)for(u=x,s.set(x,x,1),i=x-1;i>=0;i--){for(c=s.get(i,i)-E,N=0,o=u;o<=x;o++)N+=s.get(i,o)*s.get(o,x);if(e[i]<0)k=c,R=N;else if(u=i,0===e[i]?s.set(i,x,0!==c?-N/c:-N/(v*A)):(f=s.get(i,i+1),m=s.get(i+1,i),I=(n[i]-E)*(n[i]-E)+e[i]*e[i],h=(f*R-k*N)/I,s.set(i,x,h),s.set(i+1,x,Math.abs(f)>Math.abs(k)?(-N-c*h)/f:(-R-m*h)/k)),h=Math.abs(s.get(i,x)),v*h*h>1)for(o=i;o<=x;o++)s.set(o,x,s.get(o,x)/h)}else if(I<0)for(u=x-1,Math.abs(s.get(x,x-1))>Math.abs(s.get(x-1,x))?(s.set(x-1,x-1,I/s.get(x,x-1)),s.set(x-1,x,-(s.get(x,x)-E)/s.get(x,x-1))):(b=_(0,-s.get(x-1,x),s.get(x-1,x-1)-E,I),s.set(x-1,x-1,b[0]),s.set(x-1,x,b[1])),s.set(x,x-1,0),s.set(x,x,1),i=x-2;i>=0;i--){for(g=0,d=0,o=u;o<=x;o++)g+=s.get(i,o)*s.get(o,x-1),d+=s.get(i,o)*s.get(o,x);if(c=s.get(i,i)-E,e[i]<0)k=c,N=g,R=d;else if(u=i,0===e[i]?(b=_(-g,-d,c,I),s.set(i,x-1,b[0]),s.set(i,x,b[1])):(f=s.get(i,i+1),m=s.get(i+1,i),p=(n[i]-E)*(n[i]-E)+e[i]*e[i]-I*I,w=2*(n[i]-E)*I,0===p&&0===w&&(p=v*A*(Math.abs(c)+Math.abs(I)+Math.abs(f)+Math.abs(m)+Math.abs(k))),b=_(f*N-k*g+I*d,f*R-k*d-I*g,p,w),s.set(i,x-1,b[0]),s.set(i,x,b[1]),Math.abs(f)>Math.abs(k)+Math.abs(I)?(s.set(i+1,x-1,(-g-c*s.get(i,x-1)+I*s.get(i,x))/f),s.set(i+1,x,(-d-c*s.get(i,x)-I*s.get(i,x-1))/f)):(b=_(-N-m*s.get(i,x-1),-R-m*s.get(i,x),k,I),s.set(i+1,x-1,b[0]),s.set(i+1,x,b[1]))),h=Math.max(Math.abs(s.get(i,x-1)),Math.abs(s.get(i,x))),v*h*h>1)for(o=i;o<=x;o++)s.set(o,x-1,s.get(o,x-1)/h),s.set(o,x,s.get(o,x)/h)}for(i=0;i<t;i++)if(i<0||i>M)for(o=i;o<t;o++)r.set(i,o,s.get(i,o));for(o=t-1;o>=0;o--)for(i=0;i<=M;i++){for(k=0,a=0;a<=Math.min(o,M);a++)k+=r.get(i,a)*s.get(a,o);r.set(i,o,k)}}}(i,u,a,o,t)}this.n=i,this.e=u,this.d=a,this.V=o}get realEigenvalues(){return Array.from(this.d)}get imaginaryEigenvalues(){return Array.from(this.e)}get eigenvectorMatrix(){return this.V}get diagonalMatrix(){let t,e,n=this.n,r=this.e,s=this.d,i=new S(n,n);for(t=0;t<n;t++){for(e=0;e<n;e++)i.set(t,e,0);i.set(t,t,s[t]),r[t]>0?i.set(t,t+1,r[t]):r[t]<0&&i.set(t,t-1,r[t])}return i}}function _(t,e,n,r){let s,i;return Math.abs(n)>Math.abs(r)?(s=r/n,i=n+s*r,[(t+s*e)/i,(e-s*t)/i]):(s=n/r,i=r+s*n,[(s*t+e)/i,(s*e-t)/i])}class L{constructor(t){if(!(t=k.checkMatrix(t)).isSymmetric())throw new Error("Matrix is not symmetric");let e,n,r,s=t,i=s.rows,o=new S(i,i),a=!0;for(n=0;n<i;n++){let t=0;for(r=0;r<n;r++){let i=0;for(e=0;e<r;e++)i+=o.get(r,e)*o.get(n,e);i=(s.get(n,r)-i)/o.get(r,r),o.set(n,r,i),t+=i*i}for(t=s.get(n,n)-t,a&&=t>0,o.set(n,n,Math.sqrt(Math.max(t,0))),r=n+1;r<i;r++)o.set(n,r,0)}this.L=o,this.positiveDefinite=a}isPositiveDefinite(){return this.positiveDefinite}solve(t){t=k.checkMatrix(t);let e=this.L,n=e.rows;if(t.rows!==n)throw new Error("Matrix dimensions do not match");if(!1===this.isPositiveDefinite())throw new Error("Matrix is not positive definite");let r,s,i,o=t.columns,a=t.clone();for(i=0;i<n;i++)for(s=0;s<o;s++){for(r=0;r<i;r++)a.set(i,s,a.get(i,s)-a.get(r,s)*e.get(i,r));a.set(i,s,a.get(i,s)/e.get(i,i))}for(i=n-1;i>=0;i--)for(s=0;s<o;s++){for(r=i+1;r<n;r++)a.set(i,s,a.get(i,s)-a.get(r,s)*e.get(r,i));a.set(i,s,a.get(i,s)/e.get(i,i))}return a}get lowerTriangularMatrix(){return this.L}}class ${constructor(t,e={}){t=k.checkMatrix(t);let{Y:n}=e;const{scaleScores:s=!1,maxIterations:i=1e3,terminationCriteria:o=1e-10}=e;let a;if(n){if(n=r.isAnyArray(n)&&"number"==typeof n[0]?S.columnVector(n):k.checkMatrix(n),n.rows!==t.rows)throw new Error("Y should have the same number of rows as X");a=n.getColumnVector(0)}else a=t.getColumnVector(0);let u,l,h,c,f=1;for(let e=0;e<i&&f>o;e++)h=t.transpose().mmul(a).div(a.transpose().mmul(a).get(0,0)),h=h.div(h.norm()),u=t.mmul(h).div(h.transpose().mmul(h).get(0,0)),e>0&&(f=u.clone().sub(c).pow(2).sum()),c=u.clone(),n?(l=n.transpose().mmul(u).div(u.transpose().mmul(u).get(0,0)),l=l.div(l.norm()),a=n.mmul(l).div(l.transpose().mmul(l).get(0,0))):a=u;if(n){let e=t.transpose().mmul(u).div(u.transpose().mmul(u).get(0,0));e=e.div(e.norm());let r=t.clone().sub(u.clone().mmul(e.transpose())),s=a.transpose().mmul(u).div(u.transpose().mmul(u).get(0,0)),i=n.clone().sub(u.clone().mulS(s.get(0,0)).mmul(l.transpose()));this.t=u,this.p=e.transpose(),this.w=h.transpose(),this.q=l,this.u=a,this.s=u.transpose().mmul(u),this.xResidual=r,this.yResidual=i,this.betas=s}else this.w=h.transpose(),this.s=u.transpose().mmul(u).sqrt(),this.t=s?u.clone().div(this.s.get(0,0)):u,this.xResidual=t.sub(u.mmul(h.transpose()))}}e.y3=x,e.jy=L,e.oN=L,e.Hc=E,e.cg=B,e.hj=B,e.LU=C,e.Tb=C,e.uq=S,e.Zm=class extends I{constructor(t,e){d(t,e),super(t,t.rows,e.length),this.columnIndices=e}set(t,e,n){return this.matrix.set(t,this.columnIndices[e],n),this}get(t,e){return this.matrix.get(t,this.columnIndices[e])}},e.Dq=class extends I{constructor(t,e){c(t,e),super(t,t.rows,1),this.column=e}set(t,e,n){return this.matrix.set(t,this.column,n),this}get(t){return this.matrix.get(t,this.column)}},e.__=class extends I{constructor(t){super(t,t.rows,t.columns)}set(t,e,n){return this.matrix.set(t,this.columns-e-1,n),this}get(t,e){return this.matrix.get(t,this.columns-e-1)}},e.q0=class extends I{constructor(t){super(t,t.rows,t.columns)}set(t,e,n){return this.matrix.set(this.rows-t-1,e,n),this}get(t,e){return this.matrix.get(this.rows-t-1,e)}},e.lh=class extends I{constructor(t,e){g(t,e),super(t,e.length,t.columns),this.rowIndices=e}set(t,e,n){return this.matrix.set(this.rowIndices[t],e,n),this}get(t,e){return this.matrix.get(this.rowIndices[t],e)}},e.pI=class extends I{constructor(t,e){h(t,e),super(t,1,t.columns),this.row=e}set(t,e,n){return this.matrix.set(this.row,e,n),this}get(t,e){return this.matrix.get(this.row,e)}},e.zC=N,e.zg=class extends I{constructor(t,e,n,r,s){p(t,e,n,r,s),super(t,n-e+1,s-r+1),this.startRow=e,this.startColumn=r}set(t,e,n){return this.matrix.set(this.startRow+t,this.startColumn+e,n),this}get(t,e){return this.matrix.get(this.startRow+t,this.startColumn+e)}},e.g6=class extends I{constructor(t){super(t,t.columns,t.rows)}set(t,e,n){return this.matrix.set(e,t,n),this}get(t,e){return this.matrix.get(e,t)}},e.OL=$,e.ks=$,e.QR=P,e.jp=P,e.mk=D,e.W2=D,e.l=A,e.KY=R,e.dv=k,e.BR=function(t,e=t,n={}){t=new S(t);let s=!1;if("object"!=typeof e||S.isMatrix(e)||r.isAnyArray(e)?e=new S(e):(n=e,e=t,s=!0),t.rows!==e.rows)throw new TypeError("Both matrices must have the same number of rows");const{center:i=!0,scale:o=!0}=n;i&&(t.center("column"),s||e.center("column")),o&&(t.scale("column"),s||e.scale("column"));const a=t.standardDeviation("column",{unbiased:!0}),u=s?a:e.standardDeviation("column",{unbiased:!0}),l=t.transpose().mmul(e);for(let e=0;e<l.rows;e++)for(let n=0;n<l.columns;n++)l.set(e,n,l.get(e,n)*(1/(a[e]*u[n]))*(1/(t.rows-1)));return l},e.Wu=function(t,e=t,n={}){t=new S(t);let s=!1;if("object"!=typeof e||S.isMatrix(e)||r.isAnyArray(e)?e=new S(e):(n=e,e=t,s=!0),t.rows!==e.rows)throw new TypeError("Both matrices must have the same number of rows");const{center:i=!0}=n;i&&(t=t.center("column"),s||(e=e.center("column")));const o=t.transpose().mmul(e);for(let e=0;e<o.rows;e++)for(let n=0;n<o.columns;n++)o.set(e,n,o.get(e,n)*(1/(t.rows-1)));return o},e.a4=function t(e){if((e=S.checkMatrix(e)).isSquare()){if(0===e.columns)return 1;let n,r,s,i;if(2===e.columns)return n=e.get(0,0),r=e.get(0,1),s=e.get(1,0),i=e.get(1,1),n*i-r*s;if(3===e.columns){let i,o,a;return i=new N(e,[1,2],[1,2]),o=new N(e,[1,2],[0,2]),a=new N(e,[1,2],[0,1]),n=e.get(0,0),r=e.get(0,1),s=e.get(0,2),n*t(i)-r*t(o)+s*t(a)}return new C(e).determinant}throw Error("determinant can only be calculated for a square matrix")},e.DI=function(t,e=!1){return t=k.checkMatrix(t),e?new D(t).inverse():U(t,S.eye(t.rows))},e.Jo=function(t,e={}){const{thresholdValue:n=1e-9,thresholdError:r=1e-9}=e;let s=(t=S.checkMatrix(t)).rows,i=new S(s,s);for(let e=0;e<s;e++){let o=S.columnVector(t.getRow(e)),a=t.subMatrixRow(O(s,e)).transpose(),u=new D(a).solve(o),l=S.sub(o,a.mmul(u)).abs().max();i.setRow(e,z(l,u,e,n,r))}return i},e.Zi=function(t,e=Number.EPSILON){if((t=S.checkMatrix(t)).isEmpty())return t.transpose();let n=new D(t,{autoTranspose:!0}),r=n.leftSingularVectors,s=n.rightSingularVectors,i=n.diagonal;for(let t=0;t<i.length;t++)Math.abs(i[t])>e?i[t]=1/i[t]:i[t]=0;return s.mmul(S.diag(i).mmul(r.transpose()))},e.kH=U,e.LV=function(t,e){if(r.isAnyArray(t))return t[0]&&r.isAnyArray(t[0])?new k(t):new R(t,e);throw new Error("the argument is not an array")}},3181:function(t,e,n){var r;!function(t,s){function i(t){var e=this,n="";e.x=0,e.y=0,e.z=0,e.w=0,e.next=function(){var t=e.x^e.x<<11;return e.x=e.y,e.y=e.z,e.z=e.w,e.w^=e.w>>>19^t^t>>>8},t===(0|t)?e.x=t:n+=t;for(var r=0;r<n.length+64;r++)e.x^=0|n.charCodeAt(r),e.next()}function o(t,e){return e.x=t.x,e.y=t.y,e.z=t.z,e.w=t.w,e}function a(t,e){var n=new i(t),r=e&&e.state,s=function(){return(n.next()>>>0)/4294967296};return s.double=function(){do{var t=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===t);return t},s.int32=n.next,s.quick=s,r&&("object"==typeof r&&o(r,n),s.state=function(){return o(n,{})}),s}s&&s.exports?s.exports=a:n.amdD&&n.amdO?void 0===(r=function(){return a}.call(e,n,e,s))||(s.exports=r):this.xor128=a}(0,t=n.nmd(t),n.amdD)},3717:function(t,e,n){var r;!function(t,s){function i(t){var e=this,n="";e.next=function(){var t=e.b,n=e.c,r=e.d,s=e.a;return t=t<<25^t>>>7^n,n=n-r|0,r=r<<24^r>>>8^s,s=s-t|0,e.b=t=t<<20^t>>>12^n,e.c=n=n-r|0,e.d=r<<16^n>>>16^s,e.a=s-t|0},e.a=0,e.b=0,e.c=-1640531527,e.d=1367130551,t===Math.floor(t)?(e.a=t/4294967296|0,e.b=0|t):n+=t;for(var r=0;r<n.length+20;r++)e.b^=0|n.charCodeAt(r),e.next()}function o(t,e){return e.a=t.a,e.b=t.b,e.c=t.c,e.d=t.d,e}function a(t,e){var n=new i(t),r=e&&e.state,s=function(){return(n.next()>>>0)/4294967296};return s.double=function(){do{var t=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===t);return t},s.int32=n.next,s.quick=s,r&&("object"==typeof r&&o(r,n),s.state=function(){return o(n,{})}),s}s&&s.exports?s.exports=a:n.amdD&&n.amdO?void 0===(r=function(){return a}.call(e,n,e,s))||(s.exports=r):this.tychei=a}(0,t=n.nmd(t),n.amdD)},3718:(t,e,n)=>{"use strict";n.r(e),n.d(e,{default:()=>s});var r=n(788);function s(t){var e,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!(0,r.isAnyArray)(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");if(void 0!==n.output){if(!(0,r.isAnyArray)(n.output))throw new TypeError("output option must be an array if specified");e=n.output}else e=new Array(t.length);var s=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!(0,r.isAnyArray)(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");var n=e.fromIndex,s=void 0===n?0:n,i=e.toIndex,o=void 0===i?t.length:i;if(s<0||s>=t.length||!Number.isInteger(s))throw new Error("fromIndex must be a positive integer smaller than length");if(o<=s||o>t.length||!Number.isInteger(o))throw new Error("toIndex must be an integer greater than fromIndex and at most equal to length");for(var a=t[s],u=s+1;u<o;u++)t[u]<a&&(a=t[u]);return a}(t),i=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!(0,r.isAnyArray)(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");var n=e.fromIndex,s=void 0===n?0:n,i=e.toIndex,o=void 0===i?t.length:i;if(s<0||s>=t.length||!Number.isInteger(s))throw new Error("fromIndex must be a positive integer smaller than length");if(o<=s||o>t.length||!Number.isInteger(o))throw new Error("toIndex must be an integer greater than fromIndex and at most equal to length");for(var a=t[s],u=s+1;u<o;u++)t[u]>a&&(a=t[u]);return a}(t);if(s===i)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");var o=n.min,a=void 0===o?n.autoMinMax?s:0:o,u=n.max,l=void 0===u?n.autoMinMax?i:1:u;if(a>=l)throw new RangeError("min option must be smaller than max option");for(var h=(l-a)/(i-s),c=0;c<t.length;c++)e[c]=(t[c]-s)*h+a;return e}},4801:function(t,e,n){var r;!function(s,i,o){var a,u=256,l=o.pow(u,6),h=o.pow(2,52),c=2*h,f=255;function m(t,e,n){var r=[],f=w(p((e=1==e?{entropy:!0}:e||{}).entropy?[t,y(i)]:null==t?function(){try{var t;return a&&(t=a.randomBytes)?t=t(u):(t=new Uint8Array(u),(s.crypto||s.msCrypto).getRandomValues(t)),y(t)}catch(t){var e=s.navigator,n=e&&e.plugins;return[+new Date,s,n,s.screen,y(i)]}}():t,3),r),m=new g(r),b=function(){for(var t=m.g(6),e=l,n=0;t<h;)t=(t+n)*u,e*=u,n=m.g(1);for(;t>=c;)t/=2,e/=2,n>>>=1;return(t+n)/e};return b.int32=function(){return 0|m.g(4)},b.quick=function(){return m.g(4)/4294967296},b.double=b,w(y(m.S),i),(e.pass||n||function(t,e,n,r){return r&&(r.S&&d(r,m),t.state=function(){return d(m,{})}),n?(o.random=t,e):t})(b,f,"global"in e?e.global:this==o,e.state)}function g(t){var e,n=t.length,r=this,s=0,i=r.i=r.j=0,o=r.S=[];for(n||(t=[n++]);s<u;)o[s]=s++;for(s=0;s<u;s++)o[s]=o[i=f&i+t[s%n]+(e=o[s])],o[i]=e;(r.g=function(t){for(var e,n=0,s=r.i,i=r.j,o=r.S;t--;)e=o[s=f&s+1],n=n*u+o[f&(o[s]=o[i=f&i+e])+(o[i]=e)];return r.i=s,r.j=i,n})(u)}function d(t,e){return e.i=t.i,e.j=t.j,e.S=t.S.slice(),e}function p(t,e){var n,r=[],s=typeof t;if(e&&"object"==s)for(n in t)try{r.push(p(t[n],e-1))}catch(t){}return r.length?r:"string"==s?t:t+"\0"}function w(t,e){for(var n,r=t+"",s=0;s<r.length;)e[f&s]=f&(n^=19*e[f&s])+r.charCodeAt(s++);return y(e)}function y(t){return String.fromCharCode.apply(0,t)}if(w(o.random(),i),t.exports){t.exports=m;try{a=n(1234)}catch(t){}}else void 0===(r=function(){return m}.call(e,n,e,t))||(t.exports=r)}("undefined"!=typeof self?self:this,[],Math)},5284:(t,e,n)=>{"use strict";class r extends Float32Array{}var s=n(8498),i=n(2787),o=n(944),a=n(6295),u=n(6863);const l={[s.Pn.Euclidean]:a.jL,[s.Pn.Manhattan]:a.Hf,[s.Pn.Cosine]:a.w_},h={[s.xL.Levenshtein]:i.I,[s.xL.JaroWinkler]:o.Vb,[s.xL.Manhattan]:function(t,e){if(t.length!==e.length)return 1;{let n=0;for(let r=1;r<t.length;r++)n+=t[r]==e[r]?0:1;return n/t.length}},[s.xL.Onehot]:function(t,e){return t===e?0:1}},c={[s.aT.Tanimoto]:a.tO,[s.aT.Dice]:a.zn,[s.aT.Asymmetric]:a.gt,[s.aT.BraunBlanquet]:a.ij,[s.aT.Cosine]:a.lO,[s.aT.Kulczynski]:a.rR,[s.aT.McConnaughey]:a.lP,[s.aT.RogotGoldberg]:a.TK,[s.aT.Russel]:a.yK,[s.aT.Sokal]:a.Ii,[s.aT.Hamming]:a.be,[s.aT.Euclidean]:a.Qj},f={[s.m6.TanimotoIntArray]:a.v_},m={[s.RR.Difference]:a.T6},g={[s.Wn.CommonItems]:a.Hr},d={[s.EG.Vector]:{[s.Pn.Euclidean]:l[s.Pn.Euclidean],[s.Pn.Manhattan]:l[s.Pn.Manhattan],[s.Pn.Cosine]:l[s.Pn.Cosine]},[s.EG.String]:{[s.xL.Levenshtein]:h[s.xL.Levenshtein],[s.xL.JaroWinkler]:h[s.xL.JaroWinkler],[s.xL.Manhattan]:h[s.xL.Manhattan],[s.xL.Onehot]:h[s.xL.Onehot]},[s.EG.BitArray]:{[s.aT.Tanimoto]:c[s.aT.Tanimoto],[s.aT.Dice]:c[s.aT.Dice],[s.aT.Asymmetric]:c[s.aT.Asymmetric],[s.aT.BraunBlanquet]:c[s.aT.BraunBlanquet],[s.aT.Cosine]:c[s.aT.Cosine],[s.aT.Kulczynski]:c[s.aT.Kulczynski],[s.aT.McConnaughey]:c[s.aT.McConnaughey],[s.aT.RogotGoldberg]:c[s.aT.RogotGoldberg],[s.aT.Russel]:c[s.aT.Russel],[s.aT.Sokal]:c[s.aT.Sokal]},[s.EG.MacroMolecule]:{[u.Z.HAMMING]:u.V[u.Z.HAMMING],[u.Z.LEVENSHTEIN]:u.V[u.Z.LEVENSHTEIN],[u.Z.NEEDLEMANN_WUNSCH]:u.V[u.Z.NEEDLEMANN_WUNSCH],[u.Z.MONOMER_CHEMICAL_DISTANCE]:u.V[u.Z.MONOMER_CHEMICAL_DISTANCE]},[s.EG.Number]:{[s.RR.Difference]:m[s.RR.Difference]},[s.EG.IntArray]:{[s.m6.TanimotoIntArray]:f[s.m6.TanimotoIntArray]},[s.EG.NumberArray]:{[s.Wn.CommonItems]:g[s.Wn.CommonItems]}},p=Object.keys(d).reduce((t,e)=>{for(const n of Object.keys(d[e]))t[n]=e;return t},{});function w(t){return"BitArray"==p[t]}class y{constructor(t){this.method=t,this.dataType=p[t]}metricNeedsArgs(t){return p[t]==s.EG.MacroMolecule.toString()||p[t]==s.EG.Number.toString()||function(t){return p[t]==s.EG.NumberArray.toString()}(t)}getMeasure(t){const e=d;if(!e.hasOwnProperty(this.dataType)||!e[this.dataType].hasOwnProperty(this.method))throw new Error(`Unknown measure ${this.method} for data type ${this.dataType}`);return this.metricNeedsArgs(this.method)?e[this.dataType][this.method](t):e[this.dataType][this.method]}static getMetricByDataType(t){return Object.keys(d[t])}static get availableMeasures(){return Object.keys(d)}}n(606);var b=n(6066);class x{constructor(t=!0,e=!0){const r=navigator.hardwareConcurrency;this._workerCount=t?Math.max(r-2,1):1,this._workers=new Array(this._workerCount).fill(null).map(()=>new Worker(new URL(n.p+n.u(793),n.b))),this._terminateOnComplete=e}async calc(t,e,n=!0,r){return await this.calcMulti([t],[e],n,[r??{}],[1],b.G.MANHATTAN)}async calcMulti(t,e,n=!0,r=[{}],s=[1],i=b.G.MANHATTAN){if(t.length<1)throw new Error("values must contain at least one array");if(e.length!==t.length||r.length!==t.length||s.length!==t.length)throw new Error("values, fnNames, weights and opts must have the same length");return new Promise(async(o,a)=>{try{const a=t[0].length,u=new Array(this._workerCount),l=a*(a-1)/2;this._workerCount=Math.min(this._workerCount,l);const h=l/this._workerCount,c=new Float32Array(l);let f=0,m=1,g=0,d=Number.MIN_VALUE;for(let n=0;n<this._workerCount;n++){const o=Math.floor(n*h),p=n===this._workerCount-1?l:Math.floor((n+1)*h),w=f,y=m;n!==this._workerCount-1&&(f=a-2-Math.floor(Math.sqrt(-8*p+4*a*(a-1)-7)/2-.5),m=p-a*f+Math.floor((f+1)*(f+2)/2)),this._workers[n].postMessage({values:t,fnNames:e,startRow:w,startCol:y,chunckSize:p-o,opts:r,weights:s,aggregationMethod:i}),u[n]=new Promise((t,e)=>{this._workers[n].onmessage=({data:{error:r,distanceMatrixData:s,min:i,max:a}})=>{this._terminateOnComplete&&setTimeout(()=>this._workers[n].terminate()),r?e(r):(c.set(s,o),i<g&&(g=i),a>d&&(d=a),t())}})}await Promise.all(u),n&&c.forEach((t,e)=>{c[e]=(t-g)/(d-g)}),o(c)}catch(t){a(t)}})}terminate(){this._workers.forEach(t=>t.terminate())}}function M(t,e){return Math.floor(e()*t)}function v(t){return t()}function S(t){const e=[];for(let n=0;n<t;n++)e.push(void 0);return e}function A(t){return S(t).map((t,e)=>e)}function E(t,e){return S(t).map(()=>e)}function I(t){return E(t,0)}function N(t){return function(t){return t.reduce((t,e)=>t+e)}(t)/t.length}function R(t){let e=0;for(let n=0;n<t.length;n++)e=t[n]>e?t[n]:e;return e}function k(t,e,n){const r=I(t);for(let s=0;s<t;s++){let t=!0;for(;t;){const i=M(e,n);let o=!1;for(let t=0;t<s;t++)if(i===r[t]){o=!0;break}o||(t=!1),r[s]=i}}return r}function C(t,e,n){const r=[];let s=0;if(t.length!==e*n)throw new Error("Array dimensions must match input length.");for(let i=0;i<e;i++){const e=[];for(let r=0;r<n;r++)e.push(t[s]),s+=1;r.push(e)}return r}function T(t,e){const n=n=>S(t).map(()=>E(e,n)),r=[];return r.push(n(-1)),r.push(n(1/0)),r.push(n(0)),r}function P(t,e,n){const r=I(t);for(let s=0;s<t;s++){let t=!0,i=0;for(;t;){i=M(e,n);let o=!1;for(let t=0;t<s;t++)if(i===r[t]){o=!0;break}o||(t=!1)}r[s]=i}return r}function D(t,e,n,r,s){e=Math.floor(e);const i=t[0][e];if(n>=t[1][e][0])return 0;for(let t=0;t<i.length;t++)if(r===i[t])return 0;return U(t,e,n,r,s)}function U(t,e,n,r,s){const i=t[0][e],o=t[1][e],a=t[2][e];if(n>=o[0])return 0;o[0]=n,i[0]=r,a[0]=s;let u=0,l=0;for(;;){const e=2*u+1,r=e+1,s=t[0][0].length;if(e>=s)break;if(r>=s){if(!(o[e]>n))break;l=e}else if(o[e]>=o[r]){if(!(n<o[e]))break;l=e}else{if(!(n<o[r]))break;l=r}o[u]=o[l],i[u]=i[l],a[u]=a[l],u=l}return o[u]=n,i[u]=r,a[u]=s,1}function O(t,e,n,r,s){const i=T(e,r);for(let r=0;r<e;r++)for(let e=0;e<n;e++){if(t[0][r][e]<0)continue;const n=t[0][r][e],o=t[2][r][e],a=v(s);D(i,r,a,n,o),D(i,n,a,r,o),t[2][r][e]=0}return i}function z(t){const e=t[0],n=t[1];for(let t=0;t<e.length;t++){const r=e[t],s=n[t];for(let t=0;t<r.length-1;t++){const e=r.length-t-1,n=s.length-t-1,i=r[0];r[0]=r[e],r[e]=i;const o=s[0];s[0]=s[n],s[n]=o,B(s,r,n,0)}}return{indices:e,weights:n}}function B(t,e,n,r){for(;2*r+1<n;){const s=2*r+1,i=s+1;let o=r;if(t[o]<t[s]&&(o=s),i<n&&t[o]<t[i]&&(o=i),o===r)break;{const n=t[r];t[r]=t[o],t[o]=n;const s=e[r];e[r]=e[o],e[o]=s,r=o}}}function _(t,e){const n=t[0][e],r=t[1][e],s=t[2][e];let i=1/0,o=-1;for(let t=0;t>n.length;t++)1===s[t]&&r[t]<i&&(i=r[t],o=t);return o>=0?(s[o]=0,Math.floor(n[o])):-1}class L{constructor(t,e,n,r){if(this.entries=new Map,this.nRows=0,this.nCols=0,t.length!==e.length||t.length!==n.length)throw new Error("rows, cols and values arrays must all have the same length");this.nRows=r[0],this.nCols=r[1];for(let r=0;r<n.length;r++){const s=t[r],i=e[r];this.checkDims(s,i);const o=this.makeKey(s,i);this.entries.set(o,{value:n[r],row:s,col:i})}}makeKey(t,e){return`${t}:${e}`}checkDims(t,e){if(!(t<this.nRows&&e<this.nCols))throw new Error("row and/or col specified outside of matrix dimensions")}set(t,e,n){this.checkDims(t,e);const r=this.makeKey(t,e);this.entries.has(r)?this.entries.get(r).value=n:this.entries.set(r,{value:n,row:t,col:e})}get(t,e,n=0){const r=this.makeKey(t,e);return this.entries.has(r)?this.entries.get(r).value:n}getAll(t=!0){const e=new Array(this.entries.size).fill(null);let n=0;return this.entries.forEach(t=>{e[n++]=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 n=0;this.entries.forEach(r=>{e[n++]=t(r.value,r.row,r.col)});const r=[this.nRows,this.nCols];return new L(this.getRows(),this.getCols(),e,r)}toArray(){const t=S(this.nRows).map(()=>I(this.nCols));return this.entries.forEach(e=>{t[e.row][e.col]=e.value}),t}}function $(t){const e=t.getRows(),n=t.getCols(),r=t.getValues(),s=n.length,i=new Int32Array(s),o=new Int32Array(s),a=new Float32Array(s);i.set(e),o.set(n),a.set(r);const u=[t.nCols,t.nRows];return new L(o,i,a,u)}function G(t,e){return W(t,e,(t,e)=>t*e)}function F(t,e){return W(t,e,(t,e)=>t+e)}function j(t,e){return W(t,e,(t,e)=>t-e)}function q(t,e){return t.map(t=>t*e)}function V(t){const e=new Set,n=t.getValues(),r=t.getRows(),s=t.getCols();for(let t=0;t<n.length;t++)0===n[t]&&e.add(t);const i=(t,n)=>!e.has(n),o=n.filter(i),a=r.filter(i),u=s.filter(i);return new L(a,u,o,t.getDims())}function Y(t,e="l2"){const n=K[e],r=new Map;t.forEach((t,e,n)=>{const s=r.get(e)||[];s.push(n),r.set(e,s)});const s=new L([],[],[],t.getDims());for(const e of r.keys()){const i=r.get(e).sort(),o=n(i.map(n=>t.get(e,n)));for(let t=0;t<o.length;t++)s.set(e,i[t],o[t])}return s}const K={max:t=>{let e=-1/0;for(let n=0;n<t.length;n++)e=t[n]>e?t[n]:e;return t.map(t=>t/e)},l1:t=>{let e=0;for(let n=0;n<t.length;n++)e+=t[n];return t.map(t=>t/e)},l2:t=>{let e=0;for(let n=0;n<t.length;n++)e+=t[n]**2;return t.map(t=>Math.sqrt(t**2/e))}};function W(t,e,n){const r=new Set,s=[],i=[],o=[],a=(r,a)=>{s.push(r),i.push(a);const u=n(t.get(r,a),e.get(r,a));o.push(u)},u=t.getValues(),l=t.getRows(),h=t.getCols();for(let t=0;t<u.length;t++){const e=l[t],n=h[t],s=`${e}:${n}`;r.add(s),a(e,n)}const c=e.getValues(),f=e.getRows(),m=e.getCols();for(let t=0;t<c.length;t++){const e=f[t],n=m[t],s=`${e}:${n}`;r.has(s)||a(e,n)}const g=[t.nRows,t.nCols];return new L(s,i,o,g)}function H(t){const e=[];t.forEach((t,n,r)=>{e.push({value:t,row:n,col:r})}),e.sort((t,e)=>t.row===e.row?t.col-e.col:t.row-e.row);const n=[],r=[],s=[];let i=-1;for(let t=0;t<e.length;t++){const{row:o,col:a,value:u}=e[t];o!==i&&(i=o,s.push(t)),n.push(a),r.push(u)}return{indices:n,values:r,indptr:s}}class Z{constructor(t,e,n,r){this.hyperplanes=t,this.offsets=e,this.children=n,this.indices=r}}function X(t,e,n,r){const s=Math.max(10,e),i=A(n).map((e,n)=>function(t,e=30,n,r){return Q(t,A(t.length),e,n,r)}(t,s,n,r)),o=i.map(t=>function(t,e){const n=tt(t),r=et(t),s=A(n).map(()=>t.hyperplane?1:0),i=I(n),o=A(n).map(()=>[-1,-1]),a=A(r).map(()=>A(e).map(()=>-1));return J(t,s,i,o,a,0,0),new Z(s,i,o,a)}(t,s));return o}function Q(t,e,n=30,r,s){if(e.length>n){const i=function(t,e,n){const r=M(e.length,n);let s=M(e.length,n);s+=r===s?1:0,s%=e.length;const i=e[r],o=e[s];let a=0,u=0;u=t[i]-t[o],a-=u*(t[i]+t[o])/2;let l=0,h=0;const c=I(e.length);for(let r=0;r<e.length;r++){let s=a;s+=u*t[e[r]],0===s?(c[r]=M(2,n),0===c[r]?l+=1:h+=1):s>0?(c[r]=0,l+=1):(c[r]=1,h+=1)}const f=I(l),m=I(h);l=0,h=0;for(let t=0;t<c.length;t++)0===c[t]?(f[l]=e[t],l+=1):(m[h]=e[t],h+=1);return{indicesLeft:f,indicesRight:m,hyperplane:u,offset:a}}(t,e,s),{indicesLeft:o,indicesRight:a,hyperplane:u,offset:l}=i;return{leftChild:Q(t,o,n,r+1,s),rightChild:Q(t,a,n,r+1,s),isLeaf:!1,hyperplane:u,offset:l}}return{indices:e,isLeaf:!0}}function J(t,e,n,r,s,i,o){if(t.isLeaf)return r[i][0]=-o,s[o].splice(0,t.indices.length,...t.indices),{nodeNum:i,leafNum:o+=1};{e[i]=t.hyperplane,n[i]=t.offset,r[i][0]=i+1;const a=i;let u=J(t.leftChild,e,n,r,s,i+1,o);return i=u.nodeNum,o=u.leafNum,r[a][1]=i+1,u=J(t.rightChild,e,n,r,s,i+1,o),{nodeNum:u.nodeNum,leafNum:u.leafNum}}}function tt(t){return t.isLeaf?1:1+tt(t.leftChild)+tt(t.rightChild)}function et(t){return t.isLeaf?1:et(t.leftChild)+et(t.rightChild)}function nt(t,e,n,r){let s=e;return s+=t*n,0===s?M(2,r):s>0?0:1}function rt(t,e,n){let r=0;for(;e.children[r][0]>0;)r=0===nt(e.hyperplanes[r],e.offsets[r],t,n)?e.children[r][0]:e.children[r][1];const s=-1*e.children[r][0];return e.indices[s]}const st=Object.prototype.toString;function it(t){return st.call(t).endsWith("Array]")}function ot(t,e,n){let r=0;const s=n(e);for(let e=0;e<t.x.length;e++)r+=Math.abs(t.y[e]-s(t.x[e]));return r}var at=n(3054);at.y3,at.jy,at.oN,at.Hc,at.cg,at.hj,at.LU,at.Tb;const ut=at.uq,lt=(at.Zm,at.Dq,at.__,at.q0,at.lh,at.pI,at.zC,at.zg,at.g6,at.OL,at.ks,at.QR,at.jp,at.mk,at.W2,at.l,at.KY,at.dv,at.BR,at.Wu,at.uq,at.uq,at.a4,at.DI);function ht(t,e,n,r,s){let i=n*r*r,o=ut.eye(e.length,e.length,i);const a=s(e);let u=new Float64Array(t.x.length);for(let e=0;e<t.x.length;e++)u[e]=a(t.x[e]);let l=function(t,e,n,r,s){const i=n.length,o=t.x.length;let a=new Array(i);for(let u=0;u<i;u++){a[u]=new Array(o);let i=n.slice();i[u]+=r;let l=s(i);for(let n=0;n<o;n++)a[u][n]=e[n]-l(t.x[n])}return new ut(a)}(t,u,e,r,s),h=function(t,e){const n=t.x.length;let r=new Array(n);for(let s=0;s<n;s++)r[s]=[t.y[s]-e[s]];return new ut(r)}(t,u),c=lt(o.add(l.mmul(l.transpose())));return(e=(e=new ut([e])).sub(c.mmul(l).mmul(h).mul(r).transpose())).to1DArray()}at.Jo,at.Zi,at.kH,at.LV;const ct=1e-5,ft=.001;class mt{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=15,this.distanceFn=gt,this.isInitialized=!1,this.rpForest=[],this.embedding=[],this.optimizationState=new dt;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"),this.targetNNeighbors=t.nNeighbors||this.nNeighbors||this.targetNNeighbors}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:n,epochsPerSample:r}=this.initializeSimplicialSetEmbedding();return this.optimizationState.head=e,this.optimizationState.tail=n,this.optimizationState.epochsPerSample=r,this.initializeOptimization(),this.prepareForOptimizationLoop(),this.isInitialized=!0,this.getNEpochs()}makeSearchFns(){const{initFromTree:t,initFromRandom:e}=(n=this.distanceFn,{initFromRandom:function(t,e,r,s,i){for(let o=0;o<r.length;o++){const a=k(t,e.length,i);for(let t=0;t<a.length;t++)a[t]<0||D(s,o,n(e[a[t]],r[o]),a[t],1)}},initFromTree:function(t,e,r,s,i){for(let o=0;o<r.length;o++){const a=rt(r[o],t,i);for(let t=0;t<a.length;t++){if(a[t]<0)return;D(s,o,n(e[a[t]],r[o]),a[t],1)}}}});var n;this.initFromTree=t,this.initFromRandom=e,this.search=function(t){return function(e,n,r,s){const{indices:i,indptr:o}=H(n);for(let n=0;n<s.length;n++){const a=new Set(r[0][n]);for(;;){const u=_(r,n);if(-1===u)break;const l=i.slice(o[u],o[u+1]);for(const i of l)i===u||-1===i||a.has(i)||(U(r,n,t(e[i],s[n]),i,1),a.add(i))}}return r}}(this.distanceFn)}makeSearchGraph(t){const e=this.knnIndices,n=this.knnDistances,r=[t.length,t.length],s=new L([],[],[],r);for(let t=0;t<e.length;t++){const r=e[t],i=n[t];for(let e=0;e<r.length;e++){const n=r[e],o=i[e];o>0&&s.set(t,n,o)}}return W(s,$(s),(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 n=Math.floor(this.nNeighbors*this.transformQueueSize);n=Math.min(e.length,n);const r=function(t,e,n,r,s,i,o){const a=T(n.length,r);if(s(r,e,n,a,o),t)for(const r of t)i(r,e,n,a,o);return a}(this.rpForest,e,t,n,this.initFromRandom,this.initFromTree,this.random),s=this.search(e,this.searchGraph,r,t);let{indices:i,weights:o}=z(s);i=i.map(t=>t.slice(0,this.nNeighbors)),o=o.map(t=>t.slice(0,this.nNeighbors));const a=Math.max(0,this.localConnectivity-1),{sigmas:u,rhos:l}=this.smoothKNNDistance(o,this.nNeighbors,a),{rows:h,cols:c,vals:f}=this.computeMembershipStrengths(i,o,u,l),m=[t.length,e.length];let g=new L(h,c,f,m);const d=H(Y(g,"l1")),p=t.length,w=function(t,e,n){const r=I(t.length).map(t=>I(n[0].length));for(let s=0;s<t.length;s++)for(let i=0;i<t[0].length;i++)for(let o=0;o<n[0].length;o++){const a=t[s][i];r[s][o]+=e[s][i]*n[a][o]}return r}(C(d.indices,p,this.nNeighbors),C(d.values,p,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=V(g);const x=this.makeEpochsPerSample(g.getValues(),y),M=g.getRows(),v=g.getCols();return this.assignOptimizationStateParameters({headEmbedding:w,tailEmbedding:this.embedding,head:M,tail:v,currentEpoch:0,nEpochs:y,nVertices:g.getDims()[1],epochsPerSample:x}),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:n}=this,r=function(t,e){return function(n,r,s,i=10,o=50,a=.001,u=.5,l=!0){const h=n.length,c=T(n.length,s);for(let r=0;r<n.length;r++){const i=P(s,n.length,e);for(let e=0;e<i.length;e++){const s=t(n[r],n[i[e]]);D(c,r,s,i[e],1),D(c,i[e],s,r,1)}}if(l)for(let e=0;e<r.length;e++)for(let s=0;s<r[e].length&&!(r[e][s]<0);s++)for(let i=s+1;i<r[e].length&&!(r[e][i]<0);i++){const o=t(n[r[e][s]],n[r[e][i]]);D(c,r[e][s],o,r[e][i],1),D(c,r[e][i],o,r[e][s],1)}for(let r=0;r<i;r++){const r=O(c,h,s,o,e);let i=0;for(let s=0;s<h;s++)for(let a=0;a<o;a++){const l=Math.floor(r[0][s][a]);if(!(l<0||v(e)<u))for(let e=0;e<o;e++){const o=Math.floor(r[0][s][e]),u=r[2][s][a],h=r[2][s][e];if(o<0||!u&&!h)continue;const f=t(n[l],n[o]);i+=D(c,l,f,o,1),i+=D(c,o,f,l,1)}}if(i<=a*s*n.length)break}return z(c)}}(e,this.random),s=5+Math.floor(.5==(i=t.length**.5/20)?0:Math.round(i));var i;const o=Math.max(5,Math.floor(Math.round((t=>Math.log(t)/Math.log(2))(t.length))));this.rpForest=X(t,n,s,this.random);const a=function(t){if(t.length>0){const e=[];for(const n of t)e.push(...n.indices);return e}return[[-1]]}(this.rpForest),{indices:u,weights:l}=r(t,a,n,o);return{knnIndices:u,knnDistances:l}}fuzzySimplicialSet(t,e,n=1){const{knnIndices:r=[],knnDistances:s=[],localConnectivity:i}=this,{sigmas:o,rhos:a}=this.smoothKNNDistance(s,e,i),{rows:u,cols:l,vals:h}=this.computeMembershipStrengths(r,s,o,a),c=[t.length,t.length],f=new L(u,l,h,c),m=$(f),g=G(f,m),d=j(F(f,m),g);return F(q(d,n),q(g,1-n))}categoricalSimplicialSetIntersection(t,e,n,r=1){let s=function(t,e,n=1,r=5){return t.map((t,s,i)=>-1===e[s]||-1===e[i]?t*Math.exp(-n):e[s]!==e[i]?t*Math.exp(-r):t)}(t,e,r,n);return s=V(s),function(t){const e=$(t=Y(t,"max"));return V(t=F(t,j(e,G(e,t))))}(s)}smoothKNNDistance(t,e,n=1,r=64,s=1){const i=Math.log(e)/Math.log(2)*s,o=I(t.length),a=I(t.length);for(let e=0;e<t.length;e++){let s=0,u=1/0,l=1;const h=t[e],c=h.filter(t=>t>0);if(c.length>=n){const t=Math.floor(n),r=n-t;t>0?(o[e]=c[t-1],r>ct&&(o[e]+=r*(c[t]-c[t-1]))):o[e]=r*c[0]}else c.length>0&&(o[e]=R(c));for(let n=0;n<r;n++){let n=0;for(let r=1;r<t[e].length;r++){const s=t[e][r]-o[e];n+=s>0?Math.exp(-s/l):1}if(Math.abs(n-i)<ct)break;n>i?(u=l,l=(s+u)/2):(s=l,u===1/0?l*=2:l=(s+u)/2)}if(a[e]=l,o[e]>0){const t=N(h);a[e]<ft*t&&(a[e]=ft*t)}else{const n=N(t.map(N));a[e]<ft*n&&(a[e]=ft*n)}}return{sigmas:a,rhos:o}}computeMembershipStrengths(t,e,n,r){const s=t.length,i=t[0].length,o=I(s*i),a=I(s*i),u=I(s*i);for(let l=0;l<s;l++)for(let s=0;s<i;s++){let h=0;-1!==t[l][s]&&(h=t[l][s]===l?0:e[l][s]-r[l]<=0?1:Math.exp(-(e[l][s]-r[l])/n[l]),o[l*i+s]=l,a[l*i+s]=t[l][s],u[l*i+s]=h)}return{rows:o,cols:a,vals:u}}initializeSimplicialSetEmbedding(){const t=this.getNEpochs(),{nComponents:e}=this,n=this.graph.getValues();let r=0;for(let t=0;t<n.length;t++){const e=n[t];r<n[t]&&(r=e)}const s=this.graph.map(e=>e<r/t?0:e);this.embedding=I(s.nRows).map(()=>I(e).map(()=>20*v(this.random)-10));const i=[],o=[],a=[],u=s.getAll();for(let t=0;t<u.length;t++){const e=u[t];e.value&&(i.push(e.value),a.push(e.row),o.push(e.col))}return{head:o,tail:a,epochsPerSample:this.makeEpochsPerSample(i,t)}}makeEpochsPerSample(t,e){const n=E(t.length,-1),r=R(t),s=t.map(t=>t/r*e);return s.forEach((t,r)=>{t>0&&(n[r]=e/s[r])}),n}assignOptimizationStateParameters(t){Object.assign(this.optimizationState,t)}prepareForOptimizationLoop(){const{repulsionStrength:t,learningRate:e,negativeSampleRate:n}=this,{epochsPerSample:r,headEmbedding:s,tailEmbedding:i}=this.optimizationState,o=s[0].length,a=s.length===i.length,u=r.map(t=>t/n),l=[...u],h=[...r];this.assignOptimizationStateParameters({epochOfNextSample:h,epochOfNextNegativeSample:l,epochsPerNegativeSample:u,moveOther:a,initialAlpha:e,alpha:e,gamma:t,dim:o})}initializeOptimization(){const t=this.embedding,e=this.embedding,{head:n,tail:r,epochsPerSample:s}=this.optimizationState,i=this.getNEpochs(),o=this.graph.nCols,{a,b:u}=function(t,e){const n=function(t,e){return S(300).map((t,n)=>0+n*((e-0)/299))}(0,3*t).map(t=>t<e?1:t),r=I(n.length).map((r,s)=>n[s]>=e?Math.exp(-(n[s]-e)/t):r),s={x:n,y:r},{parameterValues:i}=function(t,e,n={}){let{maxIterations:r=100,gradientDifference:s=.1,damping:i=0,errorTolerance:o=.01,minValues:a,maxValues:u,initialValues:l}=n;if(i<=0)throw new Error("The damping option must be a positive number");if(!t.x||!t.y)throw new Error("The data parameter must have x and y elements");if(!it(t.x)||t.x.length<2||!it(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 h=l||new Array(e.length).fill(1),c=h.length;if(u=u||new Array(c).fill(Number.MAX_SAFE_INTEGER),a=a||new Array(c).fill(Number.MIN_SAFE_INTEGER),u.length!==a.length)throw new Error("minValues and maxValues must be the same size");if(!it(h))throw new Error("initialValues must be an array");let f,m=ot(t,h,e),g=m<=o;for(f=0;f<r&&!g;f++){h=ht(t,h,i,s,e);for(let t=0;t<c;t++)h[t]=Math.min(Math.max(a[t],h[t]),u[t]);if(m=ot(t,h,e),isNaN(m))break;g=m<=o}return{parameterValues:h,parameterError:m,iterations:f}}(s,([t,e])=>n=>1/(1+t*n**(2*e)),{damping:1.5,initialValues:[.5,.5],gradientDifference:.1,maxIterations:100,errorTolerance:.01}),[o,a]=i;return{a:o,b:a}}(this.spread,this.minDist);this.assignOptimizationStateParameters({headEmbedding:t,tailEmbedding:e,head:n,tail:r,epochsPerSample:s,a,b:u,nEpochs:i,nVertices:o})}optimizeLayoutStep(t){const{optimizationState:e}=this,{head:n,tail:r,headEmbedding:s,tailEmbedding:i,epochsPerSample:o,epochOfNextSample:a,epochOfNextNegativeSample:u,epochsPerNegativeSample:l,moveOther:h,initialAlpha:c,alpha:f,gamma:m,a:g,b:d,dim:p,nEpochs:w,nVertices:y}=e;for(let e=0;e<o.length;e++){if(a[e]>t)continue;const c=n[e],w=r[e],b=s[c],x=i[w],v=wt(b,x);let S=0;v>0&&(S=-2*g*d*Math.pow(v,d-1),S/=g*Math.pow(v,d)+1);for(let t=0;t<p;t++){const e=pt(S*(b[t]-x[t]),4);b[t]+=e*f,h&&(x[t]+=-e*f)}a[e]+=o[e];const A=Math.floor((t-u[e])/l[e]);for(let t=0;t<A;t++){const t=M(y,this.random),e=i[t],n=wt(b,e);let r=0;if(n>0)r=2*m*d,r/=(.001+n)*(g*Math.pow(n,d)+1);else if(c===t)continue;for(let t=0;t<p;t++){let n=4;r>0&&(n=pt(r*(b[t]-e[t]),4)),b[t]+=n*f}}u[e]+=A*l[e]}return e.alpha=c*(1-t/w),e.currentEpoch+=1,s}optimizeLayoutAsync(t=()=>!0){return new Promise((e,n)=>{const r=async()=>{try{const{nEpochs:n,currentEpoch:s}=this.optimizationState;this.embedding=this.optimizeLayoutStep(s);const i=this.optimizationState.currentEpoch,o=!1===t(i),a=i===n;if(o||a)return e(a);setTimeout(()=>r(),0)}catch(t){n(t)}};setTimeout(()=>r(),0)})}optimizeLayout(t=()=>!0){let e=!1,n=[];for(;!e;){const{nEpochs:r,currentEpoch:s}=this.optimizationState;n=this.optimizeLayoutStep(s);const i=this.optimizationState.currentEpoch,o=!1===t(i);e=i===r||o}return n}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 gt(t,e){return Math.abs(t-e)}class dt{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 pt(t,e){return t>e?e:t<-e?-e:t}function wt(t,e){let n=0;for(let r=0;r<t.length;r++)n+=Math.pow(t[r]-e[r],2);return n}function yt(t=!1,e="Assertion error."){if(!t)throw new Error(e)}var bt=n(5731),xt=n(8774),Mt=n(7391),vt=n.n(Mt);class St{assert(t,e){if(!t)throw e||"Assertion failed"}getopt(t,e,n){return t.hasOwnProperty(e)?t[e]:n}gaussRandom(){if(this.returnV)return this.returnV=!1,this.vValue;const t=2*this.random()-1,e=2*this.random()-1,n=t*t+e*e;if(0===n||n>1)return this.gaussRandom();const r=Math.sqrt(-2*Math.log(n)/n);return this.vValue=e*r,this.returnV=!0,t*r}randn(t,e){return t+this.gaussRandom()*e}zeros(t){if(void 0===t||isNaN(t))return new Float32Array;if("undefined"==typeof ArrayBuffer){const e=new Float32Array(t);for(let n=0;n<t;n++)e[n]=0;return e}return new Float32Array(t)}randn2d(t,e,n){const r=void 0!==n,s=new Array(t).fill(null).map(()=>new Float32Array(e));if(r)for(let r=0;r<t;r++)s[r]=new Float32Array(e).fill(n);else for(let n=0;n<t;n++)for(let t=0;t<e;t++)s[n][t]=this.randn(0,1e-4);return s}l2(t,e){const n=t.length;let r=0;for(let s=0;s<n;s++){const n=t[s],i=e[s];r+=(n-i)*(n-i)}return r}xtod(t){const e=t.length,n=this.zeros(e*e);for(let r=0;r<e;r++)for(let s=r+1;s<e;s++){const i=this.l2(t[r],t[s]);n[r*e+s]=i,n[s*e+r]=i}return n}getIterSize(t){return t<=2e3?100:t<=3e3?90:t<=5e3?80:70}d2p(t,e,n,r){const s=(o=r,(t,e)=>o*t+e-Math.floor((t+2)*(t+1)/2)),i=new Float32Array(r*r);var o;for(let e=0;e<r;e++)for(let n=e+1;n<r;n++)i[e*r+n]=i[n*r+e]=t[s(e,n)];const a=r,u=Math.log(e),l=this.zeros(a*a),h=this.zeros(a);for(let t=0;t<a;t++){let e=-1/0,s=1/0,o=1,c=!1;const f=Math.floor(this.getIterSize(r)/5);let m=0;for(;!c;){let r=0;for(let e=0;e<a;e++){const n=t===e?0:Math.exp(-i[t*a+e]*o);h[e]=n,r+=n}let l=0;for(let t=0;t<a;t++){let e;e=0===r?0:h[t]/r,h[t]=e,e>1e-7&&(l-=e*Math.log(e))}l>u?(e=o,s===1/0?o*=2:o=(o+s)/2):(s=o,e===-1/0?o/=2:o=(o+e)/2),m++,Math.abs(l-u)<n&&(c=!0),m>=f&&(c=!0)}for(let e=0;e<a;e++)l[t*a+e]=h[e]}const c=this.zeros(a*a),f=2*a;for(let t=0;t<a;t++)for(let e=0;e<a;e++)c[t*a+e]=Math.max((l[t*a+e]+l[e*a+t])/f,1e-100);return c}sign(t){return t>0?1:t<0?-1:0}constructor(t){this.iter=0,this.random=Math.random,this.returnV=!1,this.vValue=0,t=t||{},this.perplexity=this.getopt(t,"perplexity",30),this.dim=this.getopt(t,"dim",2),this.epsilon=this.getopt(t,"epsilon",10),this.random=this.getopt(t,"random",Math.random)}initDataRaw(t){const e=t.length,n=t[0].length;this.assert(e>0," X is empty? You must have some data!"),this.assert(n>0," X[0] is empty? Where is the data?");const r=this.xtod(t);this.P=this.d2p(r,this.perplexity,1e-4,n),this.N=e,this.initSolution()}initDataDist(t,e){const n=t.length;this.assert(n>0," X is empty? You must have some data!"),console.time("distances to matrix"),this.P=this.d2p(t,this.perplexity,1e-4,e),console.timeEnd("distances to matrix"),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),n=e.cost,r=e.grad,s=this.zeros(this.dim);for(let e=0;e<t;e++)for(let t=0;t<this.dim;t++){const n=r[e][t],i=this.ystep[e][t],o=this.gains[e][t];let a=this.sign(n)===this.sign(i)?.8*o:o+.2;a<.01&&(a=.01),this.gains[e][t]=a;const u=(this.iter<250?.5:.8)*i-this.epsilon*a*r[e][t];this.ystep[e][t]=u,this.Y[e][t]+=u,s[t]+=this.Y[e][t]}for(let e=0;e<t;e++)for(let n=0;n<this.dim;n++)this.Y[e][n]-=s[n]/t;return n}debugGrad(){const t=this.N,e=this.costGrad(this.Y).grad,n=1e-5;for(let r=0;r<t;r++)for(let t=0;t<this.dim;t++){const s=this.Y[r][t];this.Y[r][t]=s+n;const i=this.costGrad(this.Y);this.Y[r][t]=s-n;const o=this.costGrad(this.Y),a=e[r][t],u=(i.cost-o.cost)/(2*n);console.log(r+","+t+": gradcheck analytic: "+a+" vs. numerical: "+u),this.Y[r][t]=s}}costGrad(t){const e=this.N,n=this.dim,r=this.P,s=this.iter<100?4:1;this.quArr??(this.quArr=this.zeros(e*e));let i=0;for(let r=0;r<e;r++)for(let s=r+1;s<e;s++){const o=1/(1+new Array(n).reduce((e,n,i)=>e+Math.pow(t[r][i]-t[s][i],2),0));this.quArr[r*e+s]=o,this.quArr[s*e+r]=o,i+=2*o}let o=0;const a=new Array(e).fill(null).map(()=>new Float32Array(n).fill(0));for(let u=0;u<e;u++)for(let l=u+1;l<e;l++){const h=Math.max(this.quArr[u*e+l]/i,1e-100);o+=-r[u*e+l]*Math.log(h)*2;const c=4*(s*r[u*e+l]-h)*this.quArr[u*e+l];for(let e=0;e<n;e++)a[u][e]+=c*(t[u][e]-t[l][e]),a[l][e]+=c*(t[l][e]-t[u][e])}return{cost:o,grad:a}}}n(5540);let At=null,Et=null;function It(){return t=this,e=void 0,r=function*(){if(!navigator.gpu)return console.error("WebGPU is not supported in this browser"),null;if(!At&&(At=yield navigator.gpu.requestAdapter({powerPreference:"high-performance"}),null==At))return null;let t=!1;if(Et&&(Et.lost.then(()=>{t=!0}),yield new Promise(t=>setTimeout(t,10))),!Et||t){const t=1e9,e=At.limits,n=e.maxBufferSize,r=e.maxStorageBufferBindingSize;try{return Et=yield At.requestDevice({requiredLimits:{maxBufferSize:Math.min(n,t),maxStorageBufferBindingSize:Math.min(r,t)}}),Et}catch(t){return console.error("Failed to create device with required limits",t),Et=yield At.requestDevice(),Et}}return Et},new((n=void 0)||(n=Promise))(function(s,i){function o(t){try{u(r.next(t))}catch(t){i(t)}}function a(t){try{u(r.throw(t))}catch(t){i(t)}}function u(t){var e;t.done?s(t.value):(e=t.value,e instanceof n?e:new n(function(t){t(e)})).then(o,a)}u((r=r.apply(t,e||[])).next())});var t,e,n,r}var Nt,Rt;(Rt=Nt||(Nt={})).EUCLIDEAN="EUCLIDEAN",Rt.MANHATTAN="MANHATTAN";const kt={[Nt.EUCLIDEAN]:function(t){return`\n var sum = 0.0;\n for (var i = 0u; i < ${t}; i = i + 1u) {\n sum = sum + distances[i] * distances[i] * computeInfo.weights[i] * computeInfo.weights[i];\n }\n return sqrt(sum);\n `},[Nt.MANHATTAN]:function(t){return`\n var sum = 0.0;\n for (var i = 0u; i < ${t}; i = i + 1u) {\n sum = sum + abs(distances[i]) * computeInfo.weights[i];\n }\n return sum;\n `}};var Ct,Tt;(Tt=Ct||(Ct={})).HAMMING="Hamming",Tt.EUCLIDEAN="Euclidean",Tt.VECTOR_COSINE="Vector Cosine",Tt.MANHATTAN="Manhattan",Tt.TANIMOTO="Tanimoto",Tt.LEVENSTEIN="Levenshtein",Tt.NEEDLEMAN_WUNSCH="Needlemann-Wunsch",Tt.MONOMER_CHEMICAL_DISTANCE="Monomer chemical distance",Tt.SOKAL="Sokal",Tt.COSINE="Cosine",Tt.ASYMMETRIC="Asymmetric",Tt.Difference="Difference",Tt.OneHot="One-Hot";const Pt={[Ct.HAMMING]:function(t,e){return`\n let aLength: u32 = computeInfo.entrySizes[${e}][aIndex];\n let bLength: u32 = computeInfo.entrySizes[${e}][bIndex];\n let maxLength: u32 = max(aLength, bLength);\n let minLength: u32 = min(aLength, bLength);\n let sizeDiff: u32 = maxLength - minLength;\n \n let maxIntDistance = ceil(maxDistance * f32(maxLength)) - f32(sizeDiff);\n\n var diff: f32 = 0.0;\n for (var i = 0u; i < ${t}; i = i + 1u) {\n diff = diff + f32(a[i] != b[i]);\n if (diff > maxIntDistance) {\n return 1.0;\n }\n }\n diff += f32(sizeDiff);\n return diff / ${t};\n `},[Ct.EUCLIDEAN]:function(t,e){return`\n var dist: f32 = 0.0;\n for (var i = 0u; i < ${t}; i = i + 1u) {\n dist = dist + f32(a[i] - b[i]) * f32(a[i] - b[i]);\n }\n return sqrt(dist);\n `},[Ct.MANHATTAN]:function(t,e){return`\n var dist: f32 = 0.0;\n for (var i = 0u; i < ${t}; i = i + 1u) {\n dist = dist + abs(f32(a[i] - b[i]));\n }\n return dist;\n `},[Ct.VECTOR_COSINE]:function(t,e){return`\n var dist: f32 = 0.0;\n var productSum: f32 = 0.0;\n var aSquareSum: f32 = 0.0;\n var bSquareSum: f32 = 0.0;\n for (var i = 0u; i < ${t}; i = i + 1u) {\n productSum = productSum + f32(a[i] * b[i]);\n aSquareSum = aSquareSum + f32(a[i] * a[i]);\n bSquareSum = bSquareSum + f32(b[i] * b[i]);\n }\n var sim = productSum / (sqrt(aSquareSum) * sqrt(bSquareSum));\n return (1.0 - sim) / 2.0;\n `},[Ct.TANIMOTO]:function(t,e){return`\n var onBitsa: u32 = 0u;\n var onBitsb: u32 = 0u;\n for (var i = 0u; i < ${t}u; i = i + 1u) {\n onBitsa = onBitsa + countOneBits(a[i]);\n onBitsb = onBitsb + countOneBits(b[i]);\n }\n\n if (onBitsa == 0u && onBitsb == 0u) {\n return 0.0;\n }\n\n let totalOnBits = onBitsa + onBitsb;\n var commonBits: u32 = 0u;\n for (var i = 0u; i < ${t}u; i = i + 1u) {\n commonBits = commonBits + countOneBits(a[i] & b[i]);\n }\n\n return 1.0 - f32(commonBits) / f32(totalOnBits - commonBits);\n `},[Ct.LEVENSTEIN]:function(t,e){return`\n let aLength: u32 = computeInfo.entrySizes[${e}][aIndex];\n let bLength: u32 = computeInfo.entrySizes[${e}][bIndex];\n let maxLength: u32 = max(aLength, bLength);\n let minLength: u32 = min(aLength, bLength);\n\n let maxIntDistance = ceil(maxDistance * f32(maxLength));\n\n // we will store two arrays as matrix and swap the working indices per pass.\n // this way we can reduce memory usage per computation to just O(aLength)\n // the grid will have aLength + 1 columns and bLength + 1 rows\n // this will be guaranteed by iteration, but the array sizes must be known at compile time, so we will use a fixed size of maxArraySize\n var dynamicPassMat: array<array<f32, ${t+1}u>, 2>; // initialize to 0\n \n var prevIndex: u32 = 0;\n var curIndex: u32 = 1; // we will swap these indices per pass\n\n // initialize the first row\n for (var i = 0u; i <= aLength; i = i + 1u) {\n dynamicPassMat[prevIndex][i] = f32(i);\n }\n\n // iterate over the rows\n for (var i = 1u; i <= bLength; i = i + 1u) {\n dynamicPassMat[curIndex][0] = f32(i);\n var minEntry: f32 = f32(maxLength);\n let prevRow = &dynamicPassMat[prevIndex];\n let curRow = &dynamicPassMat[curIndex];\n let bMon = u32(b[i - 1]);\n for (var j = 1u; j <= aLength; j = j + 1u) {\n var cost: f32 = f32(a[j - 1] != bMon);\n var res: f32 = min(\n min(\n (*prevRow)[j] + 1.0, // deletion\n (*curRow)[j - 1] + 1.0, // insertion\n ),\n (*prevRow)[j - 1] + cost // substitution\n );\n (*curRow)[j] = res;\n if (res < minEntry) {\n minEntry = res;\n }\n }\n // swap the indices\n let temp: u32 = prevIndex;\n prevIndex = curIndex;\n curIndex = temp;\n if (minEntry > maxIntDistance) {\n return 1.0;\n }\n }\n\n return dynamicPassMat[prevIndex][aLength] / f32(maxLength);\n `},[Ct.NEEDLEMAN_WUNSCH]:function(t,e){return`\n let aLength: u32 = computeInfo.entrySizes[${e}][aIndex];\n let bLength: u32 = computeInfo.entrySizes[${e}][bIndex];\n let maxLength: u32 = max(aLength, bLength);\n let minLength: u32 = min(aLength, bLength);\n \n let maxIntDistance = ceil(maxDistance * f32(maxLength));\n // we will store two arrays as matrix and swap the working indices per pass.\n // this way we can reduce memory usage per computation to just O(aLength)\n // the grid will have aLength + 1 columns and bLength + 1 rows\n // this will be guaranteed by iteration, but the array sizes must be known at compile time, so we will use a fixed size of maxArraySize\n var dynamicPassMat: array<array<f32, ${t+1}u>, 2>; // initialize to 0\n \n // we need to keep track of which operation led to the current cell\n // i.e. whether we came from the left, top or diagonal to assign gap open/gap extend penalty\n var verticalGaps: array<u32, ${t+1}u>;\n var horizontalGaps: array<u32, ${t+1}u>;\n\n let gapOpenPenalty: f32 = suppInfo.gapOpenPenalty${e};\n let gapExtensionPenalty: f32 = suppInfo.gapExtensionPenalty${e};\n var prevIndex: u32 = 0;\n var curIndex: u32 = 1; // we will swap these indices per pass\n // initialize the first row\n for (var i = 0u; i <= aLength; i = i + 1u) {\n dynamicPassMat[prevIndex][i] = gapExtensionPenalty + f32(i - 1) * gapExtensionPenalty; // accounting for the fact that left and right gaps are less costly\n dynamicPassMat[curIndex][i] = 0.0;\n }\n dynamicPassMat[0][0] = 0.0;\n\n let simMatrix = &suppInfo.similarityMatrix${e}; // using pointers make things faster\n // iterate over the rows\n for (var i = 1u; i <= bLength; i = i + 1u) {\n let prevRow = &dynamicPassMat[prevIndex];\n let curRow = &dynamicPassMat[curIndex];\n (*curRow)[0] = gapExtensionPenalty + f32(i - 1) * gapExtensionPenalty;\n var minEntry: f32 = f32(maxLength);\n let monB = u32(b[i - 1]);\n for (var j = 1u; j <= aLength; j = j + 1u) {\n let monA = u32(a[j - 1]);\n \n let cost: f32 = (*prevRow)[j - 1] + 1f - (*simMatrix)[monA][monB];\n var top = (*prevRow)[j]; // deletion\n if (verticalGaps[j] > 0 || i == 1 || i == bLength) {\n top = top + gapExtensionPenalty;\n } else {\n top = top + gapOpenPenalty;\n }\n var left = (*curRow)[j - 1]; // insertion\n if (horizontalGaps[j - 1] > 0 || j == 1 || j == aLength) {\n left = left + gapExtensionPenalty;\n } else {\n left = left + gapOpenPenalty;\n }\n var res: f32 = min(\n min(\n top, // deletion\n left, // insertion\n ),\n cost // substitution\n );\n (*curRow)[j] = res;\n if (res < minEntry) {\n minEntry = res;\n }\n // update the horizontal and vertical gaps\n if (res == cost) {\n verticalGaps[j] = 0;\n horizontalGaps[j] = 0;\n } else if (res == left) {\n verticalGaps[j] = 0;\n horizontalGaps[j] = 1;\n } else {\n verticalGaps[j] = 1;\n horizontalGaps[j] = 0;\n }\n }\n // swap the indices\n let temp: u32 = prevIndex;\n prevIndex = curIndex;\n curIndex = temp;\n if (minEntry > maxIntDistance) {\n return 1.0;\n }\n }\n return dynamicPassMat[prevIndex][aLength] / f32(minLength);\n\n `},[Ct.MONOMER_CHEMICAL_DISTANCE]:function(t,e){return`\n let aLength: u32 = computeInfo.entrySizes[${e}][aIndex];\n let bLength: u32 = computeInfo.entrySizes[${e}][bIndex];\n let maxLength: u32 = max(aLength, bLength);\n let minLength: u32 = min(aLength, bLength);\n let sizeDiff: u32 = maxLength - minLength;\n \n let maxIntDistance = ceil(maxDistance * f32(maxLength)) - f32(sizeDiff);\n\n let simMatrix = &(suppInfo.similarityMatrix${e}); // using pointers make things faster\n var diff: f32 = 0.0;\n for (var i = 0u; i < ${t}; i = i + 1u) {\n diff = diff + 1.0 - (*simMatrix)[u32(a[i])][u32(b[i])];\n if (diff > maxIntDistance) {\n return 1.0;\n }\n }\n diff += f32(sizeDiff);\n return diff / ${t};\n `},[Ct.SOKAL]:function(t,e){return`\n var onBitsa: u32 = 0u;\n var onBitsb: u32 = 0u;\n for (var i = 0u; i < ${t}u; i = i + 1u) {\n onBitsa = onBitsa + countOneBits(a[i]);\n onBitsb = onBitsb + countOneBits(b[i]);\n }\n let total = onBitsa + onBitsb;\n if (total == 0u) {\n return 1.0;\n }\n var commonBits: u32 = 0u;\n for (var i = 0u; i < ${t}u; i = i + 1u) {\n commonBits = commonBits + countOneBits(a[i] & b[i]);\n }\n return 1.0 - f32(commonBits) / f32(total * 2 - commonBits * 3);\n `},[Ct.COSINE]:function(t,e){return`\n var onBitsa: u32 = 0u;\n var onBitsb: u32 = 0u;\n for (var i = 0u; i < ${t}u; i = i + 1u) {\n onBitsa = onBitsa + countOneBits(a[i]);\n onBitsb = onBitsb + countOneBits(b[i]);\n }\n let total = onBitsa * onBitsb; // p.s. here total is taken by multiplying\n if (total == 0u) {\n return 1.0;\n }\n var commonBits: u32 = 0u;\n for (var i = 0u; i < ${t}u; i = i + 1u) {\n commonBits = commonBits + countOneBits(a[i] & b[i]);\n }\n return 1.0 - f32(commonBits) / sqrt(f32(total));\n `},[Ct.ASYMMETRIC]:function(t,e){return`\n var onBitsa: u32 = 0u;\n var onBitsb: u32 = 0u;\n for (var i = 0u; i < ${t}u; i = i + 1u) {\n onBitsa = onBitsa + countOneBits(a[i]);\n onBitsb = onBitsb + countOneBits(b[i]);\n }\n let min = min(onBitsa, onBitsb);\n if (min == 0u) {\n return 1.0;\n }\n var commonBits: u32 = 0u;\n for (var i = 0u; i < ${t}u; i = i + 1u) {\n commonBits = commonBits + countOneBits(a[i] & b[i]);\n }\n return 1.0 - f32(commonBits) / f32(min);\n `},[Ct.Difference]:function(t,e){return`\n let range = suppInfo.range${e};\n return f32(abs(f32(a[0]) - f32(b[0])) / range);\n `},[Ct.OneHot]:function(t,e){return`\n let aLength: u32 = computeInfo.entrySizes[${e}][aIndex];\n let bLength: u32 = computeInfo.entrySizes[${e}][bIndex];\n if (aLength != bLength) {\n return 1.0;\n }\n for (var i = 0u; i < aLength; i = i + 1u) {\n if(a[i] != b[i]) {\n return 1.0;\n }\n }\n return 0.0;\n `}},Dt={[Ct.HAMMING]:t=>Math.ceil(t/30),[Ct.EUCLIDEAN]:t=>Math.ceil(t/30),[Ct.MANHATTAN]:t=>Math.ceil(t/30),[Ct.TANIMOTO]:t=>Math.ceil(t/30),[Ct.SOKAL]:t=>Math.ceil(t/30),[Ct.COSINE]:t=>Math.ceil(t/30),[Ct.ASYMMETRIC]:t=>Math.ceil(t/30),[Ct.LEVENSTEIN]:t=>Math.ceil(t*t/60),[Ct.NEEDLEMAN_WUNSCH]:t=>Math.ceil(t*t/60),[Ct.MONOMER_CHEMICAL_DISTANCE]:t=>Math.ceil(t/25),[Ct.Difference]:t=>1,[Ct.OneHot]:t=>Math.ceil(t/40),[Ct.VECTOR_COSINE]:t=>Math.ceil(t/30)},Ut={STRING:new Set([Ct.HAMMING,Ct.LEVENSTEIN,Ct.NEEDLEMAN_WUNSCH,Ct.MONOMER_CHEMICAL_DISTANCE,Ct.OneHot]),UINT32ARRAY:new Set([Ct.HAMMING,Ct.EUCLIDEAN,Ct.MANHATTAN,Ct.MONOMER_CHEMICAL_DISTANCE,Ct.LEVENSTEIN,Ct.NEEDLEMAN_WUNSCH,Ct.TANIMOTO,Ct.COSINE,Ct.VECTOR_COSINE,Ct.SOKAL,Ct.ASYMMETRIC,Ct.OneHot,Ct.Difference]),INT32ARRAY:new Set([Ct.EUCLIDEAN,Ct.MANHATTAN,Ct.OneHot,Ct.Difference,Ct.VECTOR_COSINE]),FLOAT32ARRAY:new Set([Ct.EUCLIDEAN,Ct.MANHATTAN,Ct.Difference,Ct.VECTOR_COSINE]),NUMBER:new Set([Ct.EUCLIDEAN,Ct.MANHATTAN,Ct.Difference]),BITARRAY:new Set([Ct.TANIMOTO,Ct.COSINE,Ct.SOKAL,Ct.ASYMMETRIC])};var Ot,zt,Bt;function _t(t,e=15,n,r,s,i){return o=this,a=void 0,l=function*(){if(i.length!==t.length||i.length!==n.length||i.length!==s.length)throw new Error("Options, weigths and distance functions must be provided for each column");if(t.some(e=>e.length!==t[0].length))throw new Error("All entry lists must be the same length");const o=Object.values(Ct);if(n.some(t=>!o.includes(t)))throw new Error("Invalid distance metrics provided: "+n.join(", "));if(!Object.values(Nt).includes(r))throw new Error("Invalid aggregation function provided: "+r);const a=t.length;if(0===a)throw new Error("No columns provided. Please provide at least one column of data.");const u=yield It();if(!u)return null;let l=!1;u.lost.then(()=>{l=!0});const h=t[0].length,c=t.map((t,e)=>function(t,e=Ct.HAMMING,n,r={gapOpenPenalty:1,gapExtensionPenalty:.6}){var s,i;let o=null;const a=t.some(t=>"string"==typeof t)?(o="STRING",t.map(t=>new Uint32Array(t.split("").map(t=>t.charCodeAt(0))))):t.some(t=>"number"==typeof t)?(o="NUMBER",t.map(t=>new Float32Array([t]))):"object"==typeof t[0]&&t.some(t=>"_data"in t&&"_length"in t)?(o="BITARRAY",t.map(t=>t._data)):t.some(t=>t instanceof Float32Array)?(o="FLOAT32ARRAY",t):t.some(t=>t instanceof Uint32Array)?(o="UINT32ARRAY",t):t.some(t=>t instanceof Int32Array)?(o="INT32ARRAY",t):void 0;if(!a||!o)throw new Error("Invalid entry type, could not determine entry type from input list");const u=a[0]instanceof Int32Array?"INT32ARRAY":a[0]instanceof Float32Array?"FLOAT32ARRAY":"UINT32ARRAY",l=new Uint32Array(a.map(t=>t.length));if(!Ut[o]||!Ut[o].has(e))throw new Error(`Distance metric '${e}' not supported for entry type '${o}'`);const h=l.reduce((t,e)=>Math.max(t,e),0),c=Dt[e](h),f="INT32ARRAY"===u?Int32Array:"FLOAT32ARRAY"===u?Float32Array:Uint32Array,m=new f(a.length*h);a.forEach((t,e)=>{m.set(t,e*h)});let g="",d=0,p="FLOAT32ARRAY",w=null;if(e===Ct.NEEDLEMAN_WUNSCH||e===Ct.MONOMER_CHEMICAL_DISTANCE){let t=r.scoringMatrix&&r.alphabetIndexes?Object.keys(r.alphabetIndexes).reduce((t,e)=>Math.max(t,e.charCodeAt(0)),0):-1;if(!r.alphabetIndexes||!r.scoringMatrix){for(let e=0;e<m.length;e++)m[e]>t&&(t=m[e]);r.scoringMatrix=new Array(t+1).fill(null).map(()=>new Array(t+1).fill(0)),r.alphabetIndexes={};for(let t=0;t<r.scoringMatrix.length;t++)r.scoringMatrix[t][t]=1,r.alphabetIndexes[String.fromCharCode(t)]=t}const e=(t+1)*(t+1),o=new Array(t+1).fill(null).map(()=>new Float32Array(t+1));for(let e=0;e<t+1;e++)o[e][e]=1;const a=r.alphabetIndexes;for(const t of Object.keys(a))for(const e of Object.keys(a))t!==e&&(o[t.charCodeAt(0)][e.charCodeAt(0)]=r.scoringMatrix[a[t]][a[e]]);d=2+e,p="FLOAT32ARRAY",w=new Float32Array(d),w[0]=null!==(s=r.gapOpenPenalty)&&void 0!==s?s:1,w[1]=null!==(i=r.gapExtensionPenalty)&&void 0!==i?i:.6;let u=2;for(let t=0;t<o.length;t++)w.set(o[t],u),u+=o[t].length;g=`\n gapOpenPenalty${n}: f32,\n gapExtensionPenalty${n}: f32,\n similarityMatrix${n}: array<array<f32, ${t+1}>, ${t+1}>`}else if(e===Ct.Difference){if(!r.range||"number"!=typeof r.range||r.range<=0){const t=m.reduce((t,e)=>Math.min(t,e),m[0]),e=m.reduce((t,e)=>Math.max(t,e),m[0]);r.range=e-t}r.range<=0&&(r.range=1),d=1,p="FLOAT32ARRAY",w=new Float32Array([r.range]),g=`\n range${n}: f32`}const y=m instanceof Int32Array?"i32":m instanceof Float32Array?"f32":"u32",b=`data${n}: array<array<${y}, ${h}>, ${a.length}>`;return{flatSourceArray:m,sourceArraySize:m.length,maxEntryLen:h,arraySizes:l,complexity:c,suppInfoBuffer:w,suppInfoSize:d,suppInfoType:p,suppInfoStructWgsl:g,entryType:o,dataTypeWGSL:y,dataStructWgsl:b,EncodedArrayConstructor:f}}(t,n[e],e,i[e]));1===a&&(r=Nt.MANHATTAN);let f=c.map(t=>t.suppInfoStructWgsl).filter(t=>!!t&&""!=t).join(",\n"),m=!1;f&&""!=f.trim()||(m=!0,f="\ndummy: f32\n");const g=c.reduce((t,e)=>t+e.complexity,0),d=c.map(t=>t.dataStructWgsl).filter(t=>!!t&&""!=t).join(",\n"),p=new Uint32Array(a*h);c.forEach((t,e)=>{p.set(t.arraySizes,e*h)});const w=1e4,y=Math.ceil(1e4/g),b=Math.ceil(Math.sqrt(Math.ceil(100))),x=10*b,M=new Array(h).fill(null).map(()=>new Uint32Array(e)),v=new Array(h).fill(null).map(()=>new Float32Array(e)),S=u.createShaderModule({label:"KNN compute shader",code:`\n // this struct will contain all the info about the computation, startAtEndAt will contain the start and end of the knnDistances and knnIndexes.\n // array of sizes for each entries, and the main data as arrays of arrays called data0, data1 and so on. good thing is that because the first entry is vec4<u32>,\n // there will be no paddings, so no need to worry about padding data. also, arrays and matrices get stucked together, so no padding there as well.\n // what we need to worry about is the padding of overall struct. the size of overall struct will be factor of 16 bytes, so keep that in mind.\n struct ComputeInfo {\n // the x coordinate will contain the start index of the knnDistances and knnIndexes, while y will contain the end index \n // the z coordinate will contain the start of the pair comparisons, while w will contain the end of the pair comparisons\n // just keep in mind that this vec4 will be in first 16 bytes of corresponding buffer.\n startAtEndAt: vec4<u32>,\n // the ACTUALLY sizes of each entry\n entrySizes: array<array<u32, ${h}>, ${a}>,\n // the weights for each entry\n weights: array<f32, ${a}>,\n // the data for each entry\n ${d} // an example of the dataWgsl would be:\n //data0: array<array<u32,20>,100>,\n //data1: array<array<u32,20>,100>\n };\n\n struct SuppInfo {\n // struct containing all the supplementary info, like scoring matrix, alphabet indexes, range, etc.\n ${f}\n };\n \n @group(0) @binding(0) var<storage, read_write> knnIndexes: array<array<u32, ${e}>, 10000>;\n @group(0) @binding(1) var<storage, read_write> knnDistances: array<array<f32, ${e}>, 10000>; // each time just compute for a subset of the list\n @group(0) @binding(2) var<storage, read_write> computeInfo: ComputeInfo;\n @group(0) @binding(3) var<storage, read_write> suppInfo: SuppInfo;\n \n @compute @workgroup_size(10, 10) fn calcKNN(\n @builtin(global_invocation_id) id: vec3<u32>\n ) {\n ${m?"let otherDummy = suppInfo.dummy * 2;":""} // just to make sure that the suppInfo is not optimized out\n let col = id.x; //* 10 + localId.x;\n let row = id.y; //* 10 + localId.y;\n let graphIndex = row * ${x} + col;\n let index = graphIndex + computeInfo.startAtEndAt.x; // add the starting index of the knnDistances and knnIndexes\n \n if (index >= min(${h}u, computeInfo.startAtEndAt.y)) {return;}\n \n let pairComparisonStartAt = computeInfo.startAtEndAt.z;\n let pairComparisonEndAt = min(computeInfo.startAtEndAt.w, ${h}u);\n \n \n // only clear the knnDistances and knnIndexes if we are at the start of the pair comparison\n if (pairComparisonStartAt == 0u) {\n for (var i = 0u; i < ${e}; i = i + 1u) {\n knnDistances[graphIndex][i] = 99999.0;\n knnIndexes[graphIndex][i] = 0u;\n }\n }\n \n for (var i: u32 = pairComparisonStartAt; i < pairComparisonEndAt; i = i + 1u) {\n if (i == index) {continue;}\n let dist = combinedDistance(index, i);\n insertKnn(graphIndex, dist, i);\n }\n \n }\n // this will generate the distance script for each distance metric and then combine them into one\n ${Lt(n,c.map(t=>t.maxEntryLen),e,r)}\n \n fn insertKnn(knnIndex: u32, dist: f32, index: u32) {\n // small optimization, if the distance is larger than the last element in the knnDistances, we can skip\n if (dist >= knnDistances[knnIndex][${e} - 1u]) {return;}\n for (var i = 0u; i < ${e}; i = i + 1u) {\n if (dist < knnDistances[knnIndex][i]) {\n for (var j = ${e} - 1u; j > i; j = j - 1u) {\n knnDistances[knnIndex][j] = knnDistances[knnIndex][j - 1u];\n knnIndexes[knnIndex][j] = knnIndexes[knnIndex][j - 1u];\n }\n knnDistances[knnIndex][i] = dist;\n knnIndexes[knnIndex][i] = index;\n return;\n }\n }\n }\n `}),A=u.createComputePipeline({label:"hamming compute pipeline",layout:"auto",compute:{module:S,entryPoint:"calcKNN"}}),E=4+a*h+a+c.reduce((t,e)=>t+e.sourceArraySize,0),I=c.reduce((t,e)=>t+e.suppInfoSize,0),N=E*Uint32Array.BYTES_PER_ELEMENT;let R=N;const k=15&N;0!==k&&(R+=16-k);const C=u.createBuffer({label:"compute info buffer",size:R,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST,mappedAtCreation:!0}),T=C.getMappedRange();let P=0,D=w,U=0,O=y,z=0;new Uint32Array(T,z,4).set([P,D,U,O]),z+=4*Uint32Array.BYTES_PER_ELEMENT,new Uint32Array(T,z,p.length).set(p),z+=p.byteLength,new Float32Array(T,z,a).set(s),z+=a*Float32Array.BYTES_PER_ELEMENT;for(const t of c){const e=t.EncodedArrayConstructor,n=t.sourceArraySize;new e(T,z,n).set(t.flatSourceArray),z+=n*e.BYTES_PER_ELEMENT}C.unmap();const B=I*Uint32Array.BYTES_PER_ELEMENT;let _=B;const L=15&B;0!==L&&(_+=16-L),_=Math.max(_,16);const $=u.createBuffer({label:"supp info buffer",size:_,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST,mappedAtCreation:!0}),G=$.getMappedRange();let F=0;for(const t of c)t.suppInfoBuffer&&t.suppInfoBuffer.byteLength>0&&t.suppInfoSize>0&&(new("UINT32ARRAY"===t.suppInfoType?Uint32Array:Float32Array)(G,F,t.suppInfoBuffer.length).set(t.suppInfoBuffer),F+=t.suppInfoBuffer.byteLength);0===F&&new Uint32Array(G,0,4).set([1,1,1,1]),$.unmap();const j=e*Uint32Array.BYTES_PER_ELEMENT*w,q=u.createBuffer({label:"buffer distances",size:j,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC}),V=u.createBuffer({label:"buffer indexes",size:j,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC}),Y=e*Uint32Array.BYTES_PER_ELEMENT,K=u.createBindGroup({label:"bindGroup for knn buffer",layout:A.getBindGroupLayout(0),entries:[{binding:0,resource:{buffer:V}},{binding:1,resource:{buffer:q}},{binding:2,resource:{buffer:C}},{binding:3,resource:{buffer:$}}]}),W=u.createBuffer({label:"result buffer distances",size:q.size,usage:GPUBufferUsage.MAP_READ|GPUBufferUsage.COPY_DST}),H=u.createBuffer({label:"result buffer indexes",size:V.size,usage:GPUBufferUsage.MAP_READ|GPUBufferUsage.COPY_DST});for(let t=0;t<Math.ceil(h/w);t++){P=t*w,D=Math.min((t+1)*w,h);const n=Math.ceil(h/y);for(let t=0;t<n;t++){U=t*y,O=Math.min((t+1)*y,h),u.queue.writeBuffer(C,0,new Uint32Array([P,D,U,O]));const r=u.createCommandEncoder({label:"distance encoder"}),s=r.beginComputePass({label:"distance compute pass"});if(s.setPipeline(A),s.setBindGroup(0,K),s.dispatchWorkgroups(b,Math.ceil(w/b/100)),s.end(),t===n-1){r.copyBufferToBuffer(q,0,W,0,W.size),r.copyBufferToBuffer(V,0,H,0,H.size);const t=r.finish();u.queue.submit([t]),yield u.queue.onSubmittedWorkDone(),yield W.mapAsync(GPUMapMode.READ),yield H.mapAsync(GPUMapMode.READ);const n=H.getMappedRange(),s=W.getMappedRange();for(let t=0;t<D-P;t++){const r=new Uint32Array(n,t*Y,e),i=new Float32Array(s,t*Y,e);M[P+t].set(r),v[P+t].set(i)}H.unmap(),W.unmap()}else{const t=r.finish();u.queue.submit([t]),yield u.queue.onSubmittedWorkDone()}if(l)return null}}return q.destroy(),V.destroy(),C.destroy(),$.destroy(),W.destroy(),H.destroy(),l?null:{knnIndexes:M,knnDistances:v}},new((u=void 0)||(u=Promise))(function(t,e){function n(t){try{s(l.next(t))}catch(t){e(t)}}function r(t){try{s(l.throw(t))}catch(t){e(t)}}function s(e){var s;e.done?t(e.value):(s=e.value,s instanceof u?s:new u(function(t){t(s)})).then(n,r)}s((l=l.apply(o,a||[])).next())});var o,a,u,l}function Lt(t,e,n,r){return t.map((t,r)=>`\n fn distanceScript${r}(aIndex: u32, bIndex: u32) -> f32 {\n let a = computeInfo.data${r}[aIndex];\n let b = computeInfo.data${r}[bIndex];\n let maxDistance: f32 = knnDistances[aIndex - computeInfo.startAtEndAt.x][${n} - 1u];\n ${Pt[t](e[r],r)}\n }\n `).join("\n")+"\n"+`\n fn combinedDistance(aIndex: u32, bIndex: u32) -> f32 {\n var distances: array<f32, ${t.length}>;\n ${t.map((t,e)=>`distances[${e}] = distanceScript${e}(aIndex, bIndex);`).join("\n")}\n ${kt[r](t.length)}\n }\n \n `}!function(t){t.ADD="ADD",t.SUB="SUB",t.MULT="MULT"}(Ot||(Ot={})),function(t){t.SQUARE="SQUARE",t.INVERSE="INVERSE",t.TRANSPOSE="TRANSPOSE",t.NORM="NORM",t.COLUMN_NORM="COLUMN_NORM"}(zt||(zt={})),function(t){t.SCALARMULT="SCALARMULT",t.SCALARADD="SCALARADD",t.SCALARPOW="SCALARPOW"}(Bt||(Bt={}));function $t(t,e,n,r,s,i,o,a,u){return l=this,h=void 0,f=function*(){const l=Math.ceil(o/100),h=Math.ceil(Math.sqrt(l)),c=10*h,f=n[n.length-1],m=i[i.length-1],g=a[a.length-1],d=`\n struct SparseKNNStorage1 {\n indexes:array<i32, ${f}>,\n knnDistances: array<f32, ${f}>,\n offsets: array<u32, ${o+1}>\n }\n\n struct SparseKNNStorage2 {\n indexes:array<i32, ${m}>,\n knnDistances: array<f32, ${m}>,\n offsets: array<u32, ${o+1}>\n }\n\n struct ResKnn {\n knnIndexes: array<i32, ${g}>,\n knnDistances: array<f32, ${g}>\n }\n\n @group(0) @binding(0) var<storage, read_write> source1: SparseKNNStorage1;\n @group(0) @binding(1) var<storage, read_write> source2: SparseKNNStorage2;\n @group(0) @binding(2) var<storage, read_write> result: ResKnn;\n @group(0) @binding(3) var<storage, read_write> unionMatrixOffsets: array<u32, ${o+1}>;\n @compute @workgroup_size(10, 10) fn pairwiseOp(\n @builtin(global_invocation_id) id: vec3<u32>,\n ) {\n\n let col: u32 = id.x;\n let row: u32 = id.y;\n let workingIndex: u32 = row * ${c} + col;\n\n if (workingIndex >= ${o}) {\n return;\n }\n var curUnionOffset: u32 = unionMatrixOffsets[workingIndex]; // offset at which to start writing in union matrix\n\n let start1 = source1.offsets[workingIndex];\n let end1 = source1.offsets[workingIndex + 1];\n let start2 = source2.offsets[workingIndex];\n let end2 = source2.offsets[workingIndex + 1];\n // TODO: sort a copy of these arrays, will make operation faster\n for (var i: u32 = start1; i < end1; i++) {\n let val1: f32 = source1.knnDistances[i];\n var val2: f32 = 0.0;\n let curIndex: i32 = source1.indexes[i];\n for (var j: u32 = start2; j < end2; j++) {\n if (source2.indexes[j] == curIndex) {\n val2 = source2.knnDistances[j];\n break;\n }\n }\n \n result.knnIndexes[curUnionOffset] = curIndex;\n result.knnDistances[curUnionOffset] = val1 ${u} val2;\n curUnionOffset += 1;\n }\n\n if (curUnionOffset >= unionMatrixOffsets[workingIndex + 1]) {\n // small optimization applicable only to our case, but saves a good amount of time\n return;\n }\n\n // do the same for source2 but skip the indexes where they are already present in source1\n for (var i: u32 = start2; i < end2; i++) {\n let val1: f32 = source2.knnDistances[i];\n let curIndex: i32 = source2.indexes[i];\n var found = false;\n for (var j: u32 = start1; j < end1; j++) {\n if (source1.indexes[j] == curIndex) {\n found = true;\n break;\n }\n }\n if (!found) {\n result.knnIndexes[curUnionOffset] = curIndex;\n result.knnDistances[curUnionOffset] = val1 ${u} 0.0;\n curUnionOffset += 1;\n }\n }\n }\n`;let p=4*(2*f+o+1);const w=15&p;0!==w&&(p+=16-w);let y=4*(2*m+o+1);const b=15&y;0!==b&&(y+=16-b);let x=2*g*4;const M=15&x;0!==M&&(x+=16-M);const v=yield It();if(null==v)return;const S=v.createShaderModule({label:"pairwiseOpShader",code:d}),A=v.createComputePipeline({label:"pairwiseOpPipeline",layout:"auto",compute:{module:S,entryPoint:"pairwiseOp"}}),E=v.createBuffer({label:"source 1 buffer",size:p,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST,mappedAtCreation:!0}),I=E.getMappedRange();new Int32Array(I,0,f).set(t),new Float32Array(I,4*f,f).set(e),new Uint32Array(I,8*f,o+1).set(n),E.unmap();const N=v.createBuffer({label:"source 2 buffer",size:y,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST,mappedAtCreation:!0}),R=N.getMappedRange();new Int32Array(R,0,m).set(r),new Float32Array(R,4*m,m).set(s),new Uint32Array(R,8*m,o+1).set(i),N.unmap();const k=v.createBuffer({label:"res buffer",size:x,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST}),C=v.createBuffer({label:"union matrix offsets buffer",size:4*(o+1),usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST,mappedAtCreation:!0}),T=C.getMappedRange();new Uint32Array(T).set(a),C.unmap();const P=v.createBindGroup({label:"bindGroup for pairwise ops",layout:A.getBindGroupLayout(0),entries:[{binding:0,resource:{buffer:E}},{binding:1,resource:{buffer:N}},{binding:2,resource:{buffer:k}},{binding:3,resource:{buffer:C}}]}),D=v.createCommandEncoder({label:"matrix ops encoder"}),U=D.beginComputePass({label:"matrix ops compute pass"});U.setPipeline(A),U.setBindGroup(0,P),U.dispatchWorkgroups(h,h),U.end();const O=v.createBuffer({label:"result buffer",size:x,usage:GPUBufferUsage.MAP_READ|GPUBufferUsage.COPY_DST});D.copyBufferToBuffer(k,0,O,0,O.size);const z=D.finish();v.queue.submit([z]),yield v.queue.onSubmittedWorkDone(),yield O.mapAsync(GPUMapMode.READ);const B=O.getMappedRange(),_=new Int32Array(g),L=new Float32Array(g);return _.set(new Int32Array(B,0,g)),L.set(new Float32Array(B,4*g,g)),O.unmap(),O.destroy(),E.destroy(),N.destroy(),k.destroy(),C.destroy(),{resultKnnIndexes:_,resultKnnDistances:L}},new((c=void 0)||(c=Promise))(function(t,e){function n(t){try{s(f.next(t))}catch(t){e(t)}}function r(t){try{s(f.throw(t))}catch(t){e(t)}}function s(e){var s;e.done?t(e.value):(s=e.value,s instanceof c?s:new c(function(t){t(s)})).then(n,r)}s((f=f.apply(l,h||[])).next())});var l,h,c,f}var Gt=n(788);function Ft(t,e,n,r){let s=0;const i=n(e);for(let e=0;e<t.x.length;e++)s+=Math.pow(t.y[e]-i(t.x[e]),2)/r[e];return s}function jt(t,e,n,r,s,i,o){let a=n,u=ut.eye(e.length,e.length,a);const l=s(e);let h=new Float64Array(t.x.length);for(let e=0;e<t.x.length;e++)h[e]=l(t.x[e]);let c=function(t,e,n,r,s,i){const o=n.length,a=t.x.length;let u=ut.zeros(o,a),l=0;for(let h=0;h<o;h++){if(0===r[h])continue;let o=r[h],c=n.slice();c[h]+=o;let f=s(c);if(i){c=n.slice(),c[h]-=o,o*=2;let e=s(c);for(let n=0;n<a;n++)u.set(l,n,(e(t.x[n])-f(t.x[n]))/o)}else for(let n=0;n<a;n++)u.set(l,n,(e[n]-f(t.x[n]))/o);l++}return u}(t,h,e,r,s,i),f=function(t,e){const n=t.x.length;let r=new ut(n,1);for(let s=0;s<n;s++)r.set(s,0,t.y[s]-e[s]);return r}(t,h),m=lt(u.add(c.mmul(c.transpose().scale("row",{scale:o})))),g=c.mmul(f.scale("row",{scale:o}));return{perturbations:m.mmul(g),jacobianWeightResidualError:g}}function qt(t,e){const n=function(t,e){const n=(e-0)/299;return Array.from({length:300},(t,e)=>0+n*e)}(0,3*t).map(t=>t<e?1:t),r=new Array(n.length).fill(0).map((r,s)=>n[s]>=e?Math.exp(-(n[s]-e)/t):r),s={x:n,y:r},{parameterValues:i}=function(t,e,n){let{checkTimeout:r,minValues:s,maxValues:i,parameters:o,weightSquare:a,damping:u,dampingStepUp:l,dampingStepDown:h,maxIterations:c,errorTolerance:f,centralDifference:m,gradientDifference:g,improvementThreshold:d}=function(t,e,n){let{timeout:r,minValues:s,maxValues:i,initialValues:o,weights:a=1,damping:u=.01,dampingStepUp:l=11,dampingStepDown:h=9,maxIterations:c=100,errorTolerance:f=1e-7,centralDifference:m=!1,gradientDifference:g=.1,improvementThreshold:d=.001}=n;if(u<=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(!(0,Gt.isAnyArray)(t.x)||t.x.length<2||!(0,Gt.isAnyArray)(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");if(!(o&&o.length>0))throw new Error("The initialValues option is mandatory and must be an array");let p,w,y=o,b=t.y.length,x=y.length;if(i=i||new Array(x).fill(Number.MAX_SAFE_INTEGER),s=s||new Array(x).fill(Number.MIN_SAFE_INTEGER),i.length!==s.length)throw new Error("minValues and maxValues must be the same size");if("number"==typeof g)g=new Array(y.length).fill(g);else{if(!(0,Gt.isAnyArray)(g))throw new Error("gradientDifference should be a number or array with length equal to the number of parameters");g.length!==x&&(g=new Array(x).fill(g[0]))}if("number"==typeof a){let t=1/a**2;p=()=>t}else{if(!(0,Gt.isAnyArray)(a))throw new Error("weights should be a number or array with length equal to the number of data points");if(a.length<t.x.length){let t=1/a[0]**2;p=()=>t}else p=t=>1/a[t]**2}if(void 0!==r){if("number"!=typeof r)throw new Error("timeout should be a number");let t=Date.now()+1e3*r;w=()=>Date.now()>t}else w=()=>!1;let M=new Array(t.x.length);for(let t=0;t<b;t++)M[t]=p(t);return{checkTimeout:w,minValues:s,maxValues:i,parameters:y,weightSquare:M,damping:u,dampingStepUp:l,dampingStepDown:h,maxIterations:c,errorTolerance:f,centralDifference:m,gradientDifference:g,improvementThreshold:d}}(t,0,n),p=Ft(t,o,e,a),w=p,y=o.slice(),b=p<=f,x=0;for(;x<c&&!b;x++){let c=p,{perturbations:x,jacobianWeightResidualError:M}=jt(t,o,u,g,e,m,a);for(let t=0;t<o.length;t++)o[t]=Math.min(Math.max(s[t],o[t]-x.get(t,0)),i[t]);if(p=Ft(t,o,e,a),isNaN(p))break;if(p<w-f&&(w=p,y=o.slice()),u=(c-p)/x.transpose().mmul(x.mul(u).add(M)).get(0,0)>d?Math.max(u/h,1e-7):Math.min(u*l,1e7),r())throw new Error(`The execution time is over to ${n.timeout} seconds`);b=p<=f}return{parameterValues:y,parameterError:w,iterations:x}}(s,([t,e])=>n=>1/(1+t*Math.pow(n,2*e)),{damping:1.5,initialValues:[.5,.5],gradientDifference:.1,maxIterations:100,errorTolerance:.01}),[o,a]=i;return{a:o,b:a}}function Vt(t){const e=new Uint32Array(t.length+1);let n=0;for(let r=0;r<t.length;r++)e[r]=n,n+=t[r];return e[t.length]=n,e}function Yt(t,e){for(let n=0;n<t.length;n++)t[n]=t[n]*e}function Kt(t,e,n=1){return r=this,s=void 0,o=function*(){const r=new Int32Array(t.length*t[0].length),s=new Float32Array(e.length*e[0].length);for(let n=0;n<t.length;n++)r.set(t[n],n*t[n].length),s.set(e[n],n*e[n].length);const i=yield function(t){return e=this,n=void 0,s=function*(){const e=t.length,n=Math.ceil(e/100),r=Math.ceil(Math.sqrt(n)),s=t[0].length,i=function(t,e,n,r=15){return`\n\n @group(0) @binding(0) var<storage, read_write> knnIndexes: array<array<i32, ${r}>, ${n}>;\n @group(0) @binding(1) var<storage, read_write> resTransposedSizes: array<atomic<u32>, ${n}>;\n // this array will store the union of knn and its transpose sizes per index. should be initialized to knnSize each.\n @group(0) @binding(2) var<storage, read_write> resUnionMatrixSizes: array<atomic<u32>, ${n}>;\n\n @compute @workgroup_size(${t}, ${t}) fn countTransposedCols(\n @builtin(global_invocation_id) id: vec3<u32>,\n ) {\n let col: u32 = id.x;\n let row: u32 = id.y;\n let workingIndex: u32 = row * ${e} + col;\n\n if (workingIndex >= ${n}) {\n return;\n }\n \n for (var i = 0u; i < ${r}u; i++) {\n let otherIndex: i32 = knnIndexes[workingIndex][i];\n if (otherIndex != -1 && otherIndex < ${n}) {\n atomicAdd(&resTransposedSizes[otherIndex], 1u);\n atomicAdd(&resUnionMatrixSizes[otherIndex], 1u);\n let otherIndexes = &knnIndexes[otherIndex];\n for(var j = 0u; j < ${r}; j++) {\n if ((*otherIndexes)[j] == i32(workingIndex)) {\n atomicSub(&resUnionMatrixSizes[workingIndex], 1u);\n // if same is found in other array, decrement by one;\n break;\n }\n }\n }\n\n }\n }\n `}(10,10*r,e,s),o=e*t[0].length,a=yield It();if(null==a)return;const u=a.createShaderModule({label:"transposedSizesCalulation",code:i}),l=a.createComputePipeline({label:"transposedSizesPipeline",layout:"auto",compute:{module:u,entryPoint:"countTransposedCols"}}),h=a.createBuffer({label:"indexes buffer",size:4*o,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST,mappedAtCreation:!0}),c=h.getMappedRange(),f=new Int32Array(c);for(let e=0;e<t.length;e++)f.set(t[e],e*t[e].length);h.unmap();const m=a.createBuffer({label:"transpose sizes buffer",size:4*t.length,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST}),g=a.createBuffer({label:"union sizes buffer",size:4*t.length,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST,mappedAtCreation:!0}),d=g.getMappedRange();new Uint32Array(d).fill(s),g.unmap();const p=a.createBindGroup({label:"bindGroup for count ops",layout:l.getBindGroupLayout(0),entries:[{binding:0,resource:{buffer:h}},{binding:1,resource:{buffer:m}},{binding:2,resource:{buffer:g}}]}),w=a.createCommandEncoder({label:"matrix ops encoder"}),y=w.beginComputePass({label:"matrix ops compute pass"});y.setPipeline(l),y.setBindGroup(0,p),y.dispatchWorkgroups(r,r),y.end();const b=a.createBuffer({label:"result transpose sizes",size:m.size,usage:GPUBufferUsage.MAP_READ|GPUBufferUsage.COPY_DST}),x=a.createBuffer({label:"result union sizes",size:g.size,usage:GPUBufferUsage.MAP_READ|GPUBufferUsage.COPY_DST});w.copyBufferToBuffer(m,0,b,0,b.size),w.copyBufferToBuffer(g,0,x,0,x.size);const M=w.finish();a.queue.submit([M]),yield a.queue.onSubmittedWorkDone(),yield b.mapAsync(GPUMapMode.READ),yield x.mapAsync(GPUMapMode.READ);const v=b.getMappedRange(),S=x.getMappedRange(),A=new Uint32Array(t.length);A.set(new Uint32Array(v,0,t.length)),b.unmap();const E=new Uint32Array(t.length);E.set(new Uint32Array(S,0,t.length)),x.unmap();const I=E.reduce((t,e)=>t+e,0);return x.destroy(),b.destroy(),m.destroy(),h.destroy(),g.destroy(),{resultTransposeSizesArray:A,resultUnionSizesArray:E,unionMatrixSize:I}},new((r=void 0)||(r=Promise))(function(t,i){function o(t){try{u(s.next(t))}catch(t){i(t)}}function a(t){try{u(s.throw(t))}catch(t){i(t)}}function u(e){var n;e.done?t(e.value):(n=e.value,n instanceof r?n:new r(function(t){t(n)})).then(o,a)}u((s=s.apply(e,n||[])).next())});var e,n,r,s}(t),o=t.length,a=yield function(t,e,n,r=15){return s=this,i=void 0,a=function*(){const s=t.length,i=r*s,o=Vt(n.resultTransposeSizesArray),a=new Uint32Array(o.length);a.set(o);const u=new Int32Array(i).fill(0),l=new Float32Array(i).fill(0);for(let n=0;n<s;n++)for(let s=0;s<r;s++){const r=t[n][s],i=a[r];u[i]=n,l[i]=e[n][s],a[r]+=1}return{transposeKNNIndexes:u,transposeKNNDistances:l,transposeOffsets:o}},new((o=void 0)||(o=Promise))(function(t,e){function n(t){try{u(a.next(t))}catch(t){e(t)}}function r(t){try{u(a.throw(t))}catch(t){e(t)}}function u(e){var s;e.done?t(e.value):(s=e.value,s instanceof o?s:new o(function(t){t(s)})).then(n,r)}u((a=a.apply(s,i||[])).next())});var s,i,o,a}(t,e,i,e[0].length),u=Vt(new Uint32Array(o).fill(t[0].length)),l=Vt(i.resultUnionSizesArray),h=yield $t(r,s,u,a.transposeKNNIndexes,a.transposeKNNDistances,a.transposeOffsets,o,l,"*"),c=yield $t(r,s,u,a.transposeKNNIndexes,a.transposeKNNDistances,a.transposeOffsets,o,l,"+"),f=yield $t(c.resultKnnIndexes,c.resultKnnDistances,l,h.resultKnnIndexes,h.resultKnnDistances,l,o,l,"-");return 1!==n?(Yt(f.resultKnnDistances,n),Yt(h.resultKnnDistances,1-n),{res:yield $t(f.resultKnnIndexes,f.resultKnnDistances,l,h.resultKnnIndexes,h.resultKnnDistances,l,o,l,"+"),unionMatrixOffsets:l}):{res:f,unionMatrixOffsets:l,unionSizes:i.resultUnionSizesArray}},new((i=void 0)||(i=Promise))(function(t,e){function n(t){try{u(o.next(t))}catch(t){e(t)}}function a(t){try{u(o.throw(t))}catch(t){e(t)}}function u(e){var r;e.done?t(e.value):(r=e.value,r instanceof i?r:new i(function(t){t(r)})).then(n,a)}u((o=o.apply(r,s||[])).next())});var r,s,i,o}function Wt(t,e=15,n=1,r=64,s=1){return i=this,o=void 0,u=function*(){const i=t.length,o=Math.ceil(i/100),a=Math.ceil(Math.sqrt(o)),u=function(t=10,e,n,r=15,s=1,i=64,o=1){const a=n.length;let u=0;for(let t=0;t<a;t++){let e=0;for(let s=0;s<r;s++)e+=n[t][s];u+=e/r}return` \n var<private> SMOOTH_K_TOLERANCE: f32 = 1e-5;\n var<private> MIN_K_DIST_SCALE: f32 = 1e-3;\n var<private> nNeighbors: u32 = ${r};\n var<private> localConnectivity: f32 = ${s}; \n var<private> nIter: u32 = ${i};\n var<private> bandwidth: u32 = ${o};\n var<private> meanDistances: f32 = ${u/a};\n var<private> targetValue: f32 = ${Math.log(r)/Math.log(2)*o};\n @group(0) @binding(0) var<storage, read_write> sigmas: array<f32, ${a}>;\n @group(0) @binding(1) var<storage, read_write> rhos: array<f32, ${a}>;\n @group(0) @binding(2) var<storage, read> distances: array<array<f32, ${r}>, ${a}>;\n @compute @workgroup_size(${t}, ${t}) fn smoothKNNDistance(\n @builtin(global_invocation_id) id: vec3<u32>,\n ) {\n let col: u32 = id.x;\n let row: u32 = id.y;\n let workingIndex: u32 = row * ${e} + col;\n\n if (workingIndex >= ${a}) {\n return;\n }\n\n var lo: f32 = 0.0;\n var hi: f32 = 1.0e+30; // treated as Infinity\n var mid: f32 = 1.0;\n\n var nonZeroDists: array<f32, ${r}>;\n var nonZeroDistsSize: u32 = 0;\n for (var j = 0u; j < nNeighbors; j = j + 1u) {\n if (distances[workingIndex][j] > 0.0) {\n nonZeroDists[nonZeroDistsSize] = distances[workingIndex][j];\n nonZeroDistsSize = nonZeroDistsSize + 1u;\n }\n }\n\n if (f32(nonZeroDistsSize) >= localConnectivity) {\n let index: u32 = u32(floor(localConnectivity));\n let interpolation: f32 = localConnectivity - f32(index);\n if (index > 0) {\n rhos[workingIndex] = nonZeroDists[index - 1];\n if (interpolation > SMOOTH_K_TOLERANCE) {\n rhos[workingIndex] += interpolation * (nonZeroDists[index] - nonZeroDists[index - 1]);\n }\n }\n else {\n rhos[workingIndex] = interpolation * nonZeroDists[0];\n }\n }\n else if (nonZeroDistsSize > 0u) {\n var maxDist: f32 = 0.0;\n for (var j = 0u; j < nonZeroDistsSize; j = j + 1u) {\n maxDist = max(nonZeroDists[j], maxDist);\n }\n rhos[workingIndex] = maxDist;\n }\n\n for (var n = 0u; n < nIter; n = n + 1u) {\n var psum: f32 = 0.0;\n for (var j = 1u; j < ${r}; j = j + 1u) {\n let d: f32 = distances[workingIndex][j] - rhos[workingIndex];\n if (d > 0.0) {\n psum += exp(0.0 - (d / mid));\n }\n else {\n psum += 1.0;\n }\n }\n\n if (abs(psum - targetValue) < SMOOTH_K_TOLERANCE) {\n break;\n }\n\n if (psum > targetValue) {\n hi = mid;\n mid = (lo + hi) / 2.0;\n }\n else {\n lo = mid;\n if (hi == 1.0e+30) {\n mid *= 2.0;\n }\n else {\n mid = (lo + hi) / 2.0;\n }\n }\n }\n\n sigmas[workingIndex] = mid;\n\n if (rhos[workingIndex] > 0.0) {\n var sum: f32 = 0.0;\n for (var j = 0u; j < ${r}; j = j + 1u) {\n sum += distances[workingIndex][j];\n }\n let meanIthDistances: f32 = sum / ${r}.0;\n if (sigmas[workingIndex] < MIN_K_DIST_SCALE * meanIthDistances) {\n sigmas[workingIndex] = MIN_K_DIST_SCALE * meanIthDistances;\n }\n }\n else {\n if (sigmas[workingIndex] < MIN_K_DIST_SCALE * meanDistances) {\n sigmas[workingIndex] = MIN_K_DIST_SCALE * meanDistances;\n }\n }\n }\n `}(10,10*a,t,e,n,r,s),l=i*t[0].length,h=yield It();if(null==h)return;const c=new Float32Array(i),f=new Float32Array(i),m=h.createShaderModule({label:"sigmaRhoCalulation",code:u}),g=h.createComputePipeline({label:"sigmaRhoPipeline",layout:"auto",compute:{module:m,entryPoint:"smoothKNNDistance"}}),d=h.createBuffer({label:"distance buffer",size:4*l,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST,mappedAtCreation:!0}),p=d.getMappedRange(),w=new Float32Array(p);for(let e=0;e<t.length;e++)w.set(t[e],e*t[e].length);d.unmap();const y=h.createBuffer({label:"buffer sigmas",size:4*i,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC}),b=h.createBuffer({label:"buffer Rhos",size:4*i,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC}),x=h.createBindGroup({label:"bindGroup for sigmarho buffers",layout:g.getBindGroupLayout(0),entries:[{binding:0,resource:{buffer:y}},{binding:1,resource:{buffer:b}},{binding:2,resource:{buffer:d}}]}),M=h.createCommandEncoder({label:"sigmarho encoder"}),v=M.beginComputePass({label:"sigmarho compute pass"});v.setPipeline(g),v.setBindGroup(0,x),v.dispatchWorkgroups(a,a),v.end();const S=h.createBuffer({label:"result buffer rhos",size:b.size,usage:GPUBufferUsage.MAP_READ|GPUBufferUsage.COPY_DST}),A=h.createBuffer({label:"result buffer sigmas",size:y.size,usage:GPUBufferUsage.MAP_READ|GPUBufferUsage.COPY_DST});M.copyBufferToBuffer(b,0,S,0,S.size),M.copyBufferToBuffer(y,0,A,0,A.size);const E=M.finish();return h.queue.submit([E]),yield h.queue.onSubmittedWorkDone(),yield S.mapAsync(GPUMapMode.READ),yield A.mapAsync(GPUMapMode.READ),f.set(new Float32Array(S.getMappedRange())),c.set(new Float32Array(A.getMappedRange())),S.unmap(),A.unmap(),d.destroy(),y.destroy(),b.destroy(),S.destroy(),A.destroy(),{resultRhos:f,resultSigmas:c}},new((a=void 0)||(a=Promise))(function(t,e){function n(t){try{s(u.next(t))}catch(t){e(t)}}function r(t){try{s(u.throw(t))}catch(t){e(t)}}function s(e){var s;e.done?t(e.value):(s=e.value,s instanceof a?s:new a(function(t){t(s)})).then(n,r)}s((u=u.apply(i,o||[])).next())});var i,o,a,u}var Ht=function(t,e,n,r){return new(n||(n=Promise))(function(s,i){function o(t){try{u(r.next(t))}catch(t){i(t)}}function a(t){try{u(r.throw(t))}catch(t){i(t)}}function u(t){var e;t.done?s(t.value):(e=t.value,e instanceof n?e:new n(function(t){t(e)})).then(o,a)}u((r=r.apply(t,e||[])).next())})};class Zt{constructor(){this.nComponents=2,this.gamma=1,this.alpha=1,this.entryLen=0,this.nNeighbours=15,this.spread=1,this.minDist=.1,this.negativeSampleRate=5,this.localConnectivity=1,this.setOpMixRatio=1}}class Xt{constructor(t,e){this.params=new Zt,this.knnIndexes=null,this.knnDistances=null,this.rhos=null,this.sigmas=null,this.membershipStrengths=null,this.params.entryLen=t,Object.assign(this.params,e)}setPrecomputedKNN(t,e){this.knnIndexes=t,this.knnDistances=e}initializeFit(){return Ht(this,void 0,void 0,function*(){yield this.calcSmoothKNNDistances(),yield this.calcMembershipStrengths()})}fit(){return Ht(this,void 0,void 0,function*(){yield this.initializeFit();const t=yield this.initializeSimplicialSetEmbedding();if(!t)throw new Error("failed to compute umap simplistical set embeddings");const{head:e,tail:n,epochsPerSample:r,epochsPerNegativeSample:s,a:i,b:o,gamma:a,initialAlpha:u,nComponents:l,nEpochs:h,entryLen:c}=t,f=yield function(t,e,n,r,s,i,o,a,u,l,h,c){return f=this,m=void 0,d=function*(){const f=Math.ceil(t.length/100),m=Math.ceil(Math.sqrt(f)),g=10*m,d=yield It();if(!d)return;const p=1e7,w=t.length,y=function(t,e,n,r,s,i,o,a,u,l,h,c,f=1e5){return`\n// first define some functions\n\nfn rDist(ar1: array<f32, ${a}>, ar2: array<f32, ${a}>) -> f32 {\n var res = 0.0;\n for (var i1 = 0u; i1 < ${a}; i1++) {\n let diff = ar1[i1] - ar2[i1];\n res += pow(diff, 2);\n }\n return res;\n}\n\nfn clip(x: f32, clipValue: f32) -> f32 {\n if (x > clipValue) {\n return clipValue;\n }\n else if (x < 0.0 - clipValue) {\n return 0.0 - clipValue;\n }\n return x;\n}\n\nvar<private> randomSeedInt: u32 = 0;\nvar<private> nVertices: u32 = ${l};\nvar<private> initialAlpha: f32 = ${r};\nvar<private> gamma: f32 = ${s};\nvar<private> a: f32 = ${i};\nvar<private> b: f32 = ${o};\nvar<private> dim: u32 = ${a};\nvar<private> nEpochs: f32 = ${u};\nvar<private> moveOther: u32 = 1;\n\nfn random() -> u32 {\n randomSeedInt = (randomSeedInt ^ 61) ^ (randomSeedInt >> 16);\n randomSeedInt *= 9;\n randomSeedInt = randomSeedInt ^ (randomSeedInt >> 4);\n randomSeedInt *= 0x27d4eb2d;\n randomSeedInt = randomSeedInt ^ (randomSeedInt >> 15);\n return randomSeedInt;\n}\n\nfn randInt(maxVal: u32) -> u32 {\n let nextRandomNum = random();\n return u32(floor((f32(nextRandomNum) * (f32(maxVal) / 4294967296.0))));\n}\n\nstruct MatrixStorage {\n head: array<u32, ${t}>,\n tail: array<u32, ${t}>,\n}\n\nstruct EpochStorage {\n epochsPerSample: array<f32, ${t}>,\n epochOfNextSample: array<f32, ${t}>,\n epochsPerNegativeSample: array<f32, ${t}>,\n epochOfNextNegativeSample: array<f32, ${t}>\n}\n\nstruct EmbeddingStorage {\n // embeddings will be in range of -10 to 10, as atomics only can store i32 or u32, we will store them as floor(f32 * 100_000)\n headEmbeddings: array<array<atomic<i32>, ${a}>, ${e}>,\n tailEmbeddings: array<array<atomic<i32>, ${a}>, ${e}>\n}\n\nstruct ComputeInfo {\n n: f32,\n alpha: f32,\n randomNumbers: array<u32, ${t}>,\n //tailOffsets: array<u32, ${e+1}>\n}\n @group(0) @binding(0) var<storage, read_write> matrixStorage: MatrixStorage;\n @group(0) @binding(1) var<storage, read_write> epochStorage: EpochStorage;\n @group(0) @binding(2) var<storage, read_write> embedStorage: EmbeddingStorage;\n @group(0) @binding(3) var<storage, read_write> computeInfo: ComputeInfo;\n @compute @workgroup_size(${h}, ${h}) fn optimizeStep(\n @builtin(global_invocation_id) id: vec3<u32>,\n ) {\n let divisionFactor: f32 = ${f};\n let col: u32 = id.x;\n let row: u32 = id.y;\n let threadIndex: u32 = row * ${c} + col;\n let clipValue: f32 = 4.0;\n\n if (threadIndex >= ${t}) {\n return;\n }\n\n let workingIndex = threadIndex;\n //let startAtOffset = computeInfo.tailOffsets[threadIndex];\n //let endAtOffset = computeInfo.tailOffsets[threadIndex + 1];\n //for (var workingIndex = startAtOffset; workingIndex < endAtOffset; workingIndex++) {\n randomSeedInt = computeInfo.randomNumbers[workingIndex] * u32(computeInfo.n);\n\n let epochOfNextSample = epochStorage.epochOfNextSample[workingIndex];\n if (epochOfNextSample > computeInfo.n) {\n return;\n }\n\n let j = matrixStorage.head[workingIndex];\n let k = matrixStorage.tail[workingIndex];\n\n // as said before, we will store embeddings as floor(f32 * 100_000)\n var current: array<f32, ${a}>;\n var other: array<f32, ${a}>;\n for (var i = 0u; i < ${a}u; i++) {\n current[i] = f32(atomicLoad(&embedStorage.headEmbeddings[j][i])) / divisionFactor;\n other[i] = f32(atomicLoad(&embedStorage.tailEmbeddings[k][i])) / divisionFactor;\n }\n\n let distSquared = rDist(current, other);\n \n var gradCoeff: f32 = 0.0;\n if (distSquared > 0.0) {\n gradCoeff = (0.0 - 2.0) * ${i} * ${o} * pow(distSquared, b - 1.0);\n gradCoeff /= (a * pow(distSquared, b) + 1.0);\n }\n\n var gradBuff: array<i32, ${a}>;\n for (var d = 0u; d < ${a}u; d++) {\n let gradD = clip((gradCoeff * (current[d] - other[d])), clipValue);\n let toAdd = gradD * computeInfo.alpha;\n gradBuff[d] = i32(floor(toAdd * divisionFactor));\n current[d] += toAdd;\n other[d] -= toAdd;\n }\n\n for (var d = 0u; d < ${a}u; d++) {\n atomicAdd(&embedStorage.headEmbeddings[j][d], gradBuff[d]);\n atomicSub(&embedStorage.tailEmbeddings[k][d], gradBuff[d]);\n }\n\n epochStorage.epochOfNextSample[workingIndex] += epochStorage.epochsPerSample[workingIndex];\n var nNegSamples: i32 = i32(floor((computeInfo.n - epochStorage.epochOfNextNegativeSample[workingIndex]) / epochStorage.epochsPerNegativeSample[workingIndex]));\n\n for (var p = 0i; p < nNegSamples; p++) {\n let k1 = randInt(nVertices);\n var other1: array<f32, ${a}>;\n for (var i = 0u; i < ${a}u; i++) {\n other1[i] = f32(atomicLoad(&embedStorage.tailEmbeddings[k1][i])) / divisionFactor;\n }\n\n let distSquared1 = rDist(current, other1);\n\n var gradCoeff1: f32 = 0.0;\n if (distSquared1 > 0.0) {\n gradCoeff1 = 2.0 * gamma * b;\n gradCoeff1 /= (0.001 + distSquared1) * (a * pow(distSquared1, b) + 1);\n }\n else if (j == k1) {\n continue;\n }\n\n var gradDBuff: array<i32, ${a}>;\n for (var d = 0u; d < ${a}u; d++) {\n var gradD: f32 = 4.0;\n if (gradCoeff1 > 0.0) {\n gradD = clip(gradCoeff1 * (current[d] - other1[d]), clipValue);\n }\n\n gradDBuff[d] = i32(floor(gradD * computeInfo.alpha * divisionFactor));\n \n }\n for (var d = 0u; d < ${a}u; d++) {\n atomicAdd(&embedStorage.headEmbeddings[j][d], gradDBuff[d]);\n }\n }\n epochStorage.epochOfNextNegativeSample[workingIndex] += f32(nNegSamples) * epochStorage.epochsPerNegativeSample[workingIndex];\n //}\n }\n`}(w,n,0,i,o,a,u,l,h,c,10,g,p);let b=2*w*4;const x=15&b;0!==x&&(b+=16-x);let M=4*w*4;const v=15&M;0!==v&&(M+=16-v);let S=2*n*2*4;const A=15&S;0!==A&&(S+=16-A);let E=4*(2+w);const I=15&E;0!==I&&(E+=16-I);const N=d.createShaderModule({label:"optimizeShader",code:y}),R=d.createComputePipeline({label:"optimizePipeline",layout:"auto",compute:{module:N,entryPoint:"optimizeStep"}}),k=d.createBuffer({label:"matrix storage buffer",size:b,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST,mappedAtCreation:!0}),C=k.getMappedRange();new Uint32Array(C,0,w).set(t),new Uint32Array(C,4*w,w).set(e),k.unmap();const T=d.createBuffer({label:"epoch storage buffer",size:M,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST,mappedAtCreation:!0}),P=T.getMappedRange(),D=new Float32Array(P);D.set(r,0),D.set(r,w),D.set(s,2*w),D.set(s,3*w),T.unmap();const U=new Int32Array(n*l).map(()=>Math.floor((2*Math.random()-1)*p));new Int32Array(n*l).set(U);const O=d.createBuffer({label:"embedding storage buffer",size:S,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST,mappedAtCreation:!0}),z=O.getMappedRange(),B=new Int32Array(z);B.set(U,0),B.set(U,U.length),O.unmap();const _=d.createBuffer({label:"compute info buffer",size:E,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST,mappedAtCreation:!0}),L=new Uint32Array(w).map(()=>Math.floor(Math.random()*p)),$=_.getMappedRange();new Float32Array($,0,2).set([0,i]),new Uint32Array($,8,w).set(L),_.unmap();const G=d.createBindGroup({label:"bindGroup for optimize",layout:R.getBindGroupLayout(0),entries:[{binding:0,resource:{buffer:k}},{binding:1,resource:{buffer:T}},{binding:2,resource:{buffer:O}},{binding:3,resource:{buffer:_}}]});let F=i;const j=d.createBuffer({label:"result buffer",size:O.size,usage:GPUBufferUsage.MAP_READ|GPUBufferUsage.COPY_DST});for(let t=0;t<h;t++){d.queue.writeBuffer(_,0,new Float32Array([t,F]),0);const e=d.createCommandEncoder({label:"matrix ops encoder"}),n=e.beginComputePass({label:"matrix ops compute pass"});n.setPipeline(R),n.setBindGroup(0,G),n.dispatchWorkgroups(m,m),n.end(),t===h-1&&e.copyBufferToBuffer(O,0,j,0,j.size);const r=e.finish();d.queue.submit([r]),yield d.queue.onSubmittedWorkDone(),F=i*(1-t/h)}yield j.mapAsync(GPUMapMode.READ);const q=j.getMappedRange(),V=new Int32Array(q,0,U.length);U.set(V),j.unmap();const Y=new Array(l).fill(null).map(t=>new Float32Array(n));for(let t=0;t<n;t++)for(let e=0;e<l;e++)Y[e][t]=U[t*l+e]/p;return k.destroy(),T.destroy(),O.destroy(),_.destroy(),j.destroy(),Y},new((g=void 0)||(g=Promise))(function(t,e){function n(t){try{s(d.next(t))}catch(t){e(t)}}function r(t){try{s(d.throw(t))}catch(t){e(t)}}function s(e){var s;e.done?t(e.value):(s=e.value,s instanceof g?s:new g(function(t){t(s)})).then(n,r)}s((d=d.apply(f,m||[])).next())});var f,m,g,d}(e,n,c,r,s,u,a,i,o,l,h,c);return f})}calcSmoothKNNDistances(){return Ht(this,void 0,void 0,function*(){if(!this.knnIndexes||!this.knnDistances)throw new Error("knn indexes and distances must be set before calling fit");const t=yield Wt(this.knnDistances,this.params.nNeighbours,this.params.localConnectivity,64);if(!t)throw new Error("failed to compute smooth knn distances");this.rhos=t.resultRhos,this.sigmas=t.resultSigmas})}calcMembershipStrengths(){return Ht(this,void 0,void 0,function*(){if(!(this.knnIndexes&&this.knnDistances&&this.rhos&&this.sigmas))throw new Error("knn indexes, distances, rhos, and sigmas must be set before calling fit");const t=yield function(t,e,n){return r=this,s=void 0,o=function*(){const r=t.length,s=Math.ceil(r/100),i=Math.ceil(Math.sqrt(s)),o=function(t=10,e,n,r,s){const i=n.length,o=r.length,a=s.length;return`\n struct MembershipStrengthsInfo {\n knnDistances: array<array<f32, ${n[0].length}>, ${i}>,\n sigmas: array<f32, ${o}>,\n rhos: array<f32, ${a}>\n };\n var<private> nNeighbors: u32 = ${n[0].length};\n @group(0) @binding(0) var<storage, read> membershipStrengthsInfo: MembershipStrengthsInfo;\n @group(0) @binding(1) var<storage, read_write> resultKnnDistances: array<array<f32, ${n[0].length}>, ${i}>;\n @compute @workgroup_size(${t}, ${t}) fn computeMembershipStrengths(\n @builtin(global_invocation_id) id: vec3<u32>,\n ) {\n let col: u32 = id.x;\n let row: u32 = id.y;\n let workingIndex: u32 = row * ${e} + col;\n\n if (workingIndex >= ${i}) {\n return;\n }\n\n let workingResKnnDistances = &resultKnnDistances[workingIndex];\n let knnDistances = &membershipStrengthsInfo.knnDistances[workingIndex];\n for (var j = 0u; j < nNeighbors; j = j + 1u) {\n var val: f32 = 0.0;\n\n if ((*knnDistances)[j] - membershipStrengthsInfo.rhos[workingIndex] <= 0.0) {\n val = 1.0;\n } else {\n val = exp(0.0 - ((knnDistances[j] - membershipStrengthsInfo.rhos[workingIndex]) / membershipStrengthsInfo.sigmas[workingIndex])); \n }\n\n (*workingResKnnDistances)[j] = val;\n }\n }\n `}(10,10*i,t,e,n),a=r*t[0].length,u=yield It();if(null==u)return;const l=u.createShaderModule({label:"rowsColsValsCalulation",code:o}),h=u.createComputePipeline({label:"rowsColsValsPipeline",layout:"auto",compute:{module:l,entryPoint:"computeMembershipStrengths"}});let c=4*(n.length+e.length+a);const f=15&c;0!=f&&(c+=16-f);const m=u.createBuffer({label:"info buffer",size:c,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST,mappedAtCreation:!0}),g=m.getMappedRange(),d=new Float32Array(g);for(let e=0;e<t.length;e++)d.set(t[e],e*t[e].length);let p=t[0].length*t.length;d.set(e,p),p+=e.length,d.set(n,p),m.unmap();const w=u.createBuffer({label:"knn distance buffer",size:t[0].length*t.length*Float32Array.BYTES_PER_ELEMENT,usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST}),y=u.createBindGroup({label:"bindGroup for membership strhegths buffers",layout:h.getBindGroupLayout(0),entries:[{binding:0,resource:{buffer:m}},{binding:1,resource:{buffer:w}}]}),b=u.createCommandEncoder({label:"membership strengths encoder"}),x=b.beginComputePass({label:"membership strengths compute pass"});x.setPipeline(h),x.setBindGroup(0,y),x.dispatchWorkgroups(i,i),x.end();const M=u.createBuffer({label:"result buffer distances",size:w.size,usage:GPUBufferUsage.MAP_READ|GPUBufferUsage.COPY_DST});b.copyBufferToBuffer(w,0,M,0,M.size);const v=b.finish();u.queue.submit([v]),yield u.queue.onSubmittedWorkDone(),yield M.mapAsync(GPUMapMode.READ);const S=M.getMappedRange(),A=t[0].length*Float32Array.BYTES_PER_ELEMENT,E=new Array(t.length).fill(null).map((e,n)=>{const r=new Float32Array(S,A*n,t[0].length),s=new Float32Array(t[0].length);return s.set(r),s});return M.unmap(),M.destroy(),w.destroy(),m.destroy(),E},new((i=void 0)||(i=Promise))(function(t,e){function n(t){try{u(o.next(t))}catch(t){e(t)}}function a(t){try{u(o.throw(t))}catch(t){e(t)}}function u(e){var r;e.done?t(e.value):(r=e.value,r instanceof i?r:new i(function(t){t(r)})).then(n,a)}u((o=o.apply(r,s||[])).next())});var r,s,i,o}(this.knnDistances,this.sigmas,this.rhos);if(!t)throw new Error("failed to compute membership strengths");this.membershipStrengths=t})}fuzzySimplicialSet(){return Ht(this,void 0,void 0,function*(){if(!(this.knnIndexes&&this.knnDistances&&this.rhos&&this.sigmas&&this.membershipStrengths))throw new Error("knn indexes, distances, rhos, sigmas, and membership strengths must be set before calling fit");const t=yield Kt(this.knnIndexes,this.membershipStrengths,this.params.setOpMixRatio);if(!t)throw new Error("failed to compute pairwise multiply with transpose");return t})}initializeSimplicialSetEmbedding(){return Ht(this,void 0,void 0,function*(){const t=yield this.fuzzySimplicialSet();if(!(t&&t.res&&t.unionMatrixOffsets&&t.unionSizes))throw new Error("failed to compute fuzzy simplicial set");const e=yield function(t,e,n,r,s=5){return i=this,o=void 0,u=function*(){if(!t.unionSizes)return;const i=function(t){return t<=2500?1e3:t<=5e3?800:t<=7500?650:400}(e),o=t.res.resultKnnDistances.reduce((t,e)=>Math.max(t,e)),a=o/i,u=new Uint32Array(t.unionSizes.length);u.set(t.unionSizes);let l=0;for(let e=0;e<t.unionSizes.length;e++){for(let n=0;n<t.unionSizes[e];n++){const r=l+n;t.res.resultKnnDistances[r]<a&&(t.res.resultKnnDistances[r]=0,u[e]--)}l+=t.unionSizes[e]}const h=Vt(u),c=h[h.length-1],f=new Float32Array(c),m=new Uint32Array(c),g=new Uint32Array(c);l=0;for(let e=0;e<t.unionSizes.length;e++)for(let n=0;n<t.unionSizes[e];n++){const r=t.unionMatrixOffsets[e]+n;0!=t.res.resultKnnDistances[r]&&(f[l]=t.res.resultKnnDistances[r],m[l]=t.res.resultKnnIndexes[r],g[l]=e,l+=1)}const d=new Float32Array(f.length).fill(-1),p=f.map(t=>t/o*i);p.forEach((t,e)=>{t>0&&(d[e]=i/p[e])});const{a:w,b:y}=qt(n,r),b=d.map(t=>t/s);return{epochsPerSample:d,epochsPerNegativeSample:b,a:w,b:y,head:m,tail:g,entryLen:e,nEpochs:i,nComponents:2,initialAlpha:1,gamma:1}},new((a=void 0)||(a=Promise))(function(t,e){function n(t){try{s(u.next(t))}catch(t){e(t)}}function r(t){try{s(u.throw(t))}catch(t){e(t)}}function s(e){var s;e.done?t(e.value):(s=e.value,s instanceof a?s:new a(function(t){t(s)})).then(n,r)}s((u=u.apply(i,o||[])).next())});var i,o,a,u}(t,this.params.entryLen,this.params.spread,this.params.minDist,this.params.negativeSampleRate);if(!e)throw new Error("failed to compute umap simplistical set embeddings");return e})}}class Qt{constructor(t){this.data=t.data,this.weights=t.weights,this.aggregationMethod=t.aggregationMethod}}class Jt extends Qt{constructor(t){super(t);const e=t.randomSeed??Date(),n=vt()(e);t.dim=2,t.random=n,this.reducer=new St(t),this.iterations=t?.iterations??this.reducer.getIterSize(this.data[0].length),this.distanceFnames=t.distanceFnames,this.distanceFns=t.distanceFns,this.distanceFnArgs=t.distanceFnArgs,this.progressFunc=t.progressFunc}async transform(){if(this.data[0].length>1e4)throw new Error("Maximum number of samples for T-SNE is 10000");const t=new x(!0,!1);try{const e=await t.calcMulti(this.data,this.distanceFnames,!1,this.distanceFnArgs,this.weights,this.aggregationMethod);t.terminate(),this.reducer.initDataDist(e,this.data[0].length);for(let t=0;t<this.iterations;++t)this.reducer.step(),this.progressFunc&&this.progressFunc(t,this.iterations,[]);return this.reducer.getSolution()}catch(e){throw t.terminate(),e}}}class te extends Qt{constructor(t){const e=t.randomSeed??Date(),n=vt()(e);super(t),this.useWebGPU=!1,yt("distanceFnames"in t),yt("distanceFns"in t),this.distanceFnArgs=t.distanceFnArgs,this.distanceFns=t.distanceFns,this.progressFunc=t.progressFunc,this.useWebGPU=t.useWebGPU??!1,this.distanceFnames=t.distanceFnames,t.nComponents=2,this.vectors=new Array(this.data[0].length).fill(0).map((t,e)=>e),this.data[0].length<=(t.nNeighbors??15)&&(t.nNeighbors=this.data[0].length-1),t.random=n,this.reducer=new mt(t)}async transform(t){let e;if(console.time("knn graph"),this.useWebGPU)try{e=await _t(this.data,this.reducer.neighbors,this.distanceFnames,this.aggregationMethod,this.weights,this.distanceFnArgs)}catch(t){console.error(t)}if(e||(this.useWebGPU&&console.error("WEBGPU KNN failed, falling back to multithreaded CPU implementation"),e=await(new bt.p).multiColumnKNN(this.data,this.distanceFnames,this.reducer.neighbors,this.distanceFnArgs,this.weights,this.aggregationMethod)),console.timeEnd("knn graph"),!e)throw new Error("Failed to compute KNN graph");const n=this.data[0].length;for(let t=0;t<e.knnIndexes.length;++t)for(let r=0;r<e.knnIndexes[t].length;++r)e.knnDistances[t][r]<0&&(e.knnDistances[t][r]=0),(e.knnIndexes[t][r]<0||e.knnIndexes[t][r]>=n||e.knnDistances[t][r]>10)&&(e.knnIndexes[t][r]=t==r?r+1:r,e.knnDistances[t][r]=10);this.useWebGPU?(this.webGPUReducer=new Xt(this.vectors.length,{nComponents:this.reducer.nComponents,gamma:this.reducer.repulsionStrength,alpha:this.reducer.learningRate,nNeighbours:this.reducer.neighbors,spread:this.reducer.spread,minDist:this.reducer.minDist,negativeSampleRate:this.reducer.negativeSampleRate,localConnectivity:this.reducer.localConnectivity,setOpMixRatio:this.reducer.setOpMixRatio}),this.webGPUReducer.setPrecomputedKNN(e.knnIndexes,e.knnDistances)):this.reducer.setPrecomputedKNN(e.knnIndexes,e.knnDistances),await new Promise(t=>{setTimeout(()=>{t()},300)});let s=null;console.time("fit");try{if(this.useWebGPU&&this.webGPUReducer){if(s=await this.webGPUReducer.fit(),!s)throw new Error("Failed to compute embedding");s=new Array(s[0].length).fill(null).map((t,e)=>new Float32Array(s.map(t=>t[e])))}}catch(t){console.error(t)}return s||(this.useWebGPU&&(console.error("WEBGPU UMAP failed, falling back to CPU implementation"),this.reducer.setPrecomputedKNN(e.knnIndexes,e.knnDistances)),s=await this.reducer.fitAsync(this.vectors,t=>{this.progressFunc&&this.progressFunc(t,this.reducer.getNEpochs(),this.reducer.getEmbedding())})),console.timeEnd("fit"),i=s,new Array(i.length).fill(0).map((t,e)=>r.from(i[e]));var i}}const ee={UMAP:te,"t-SNE":Jt};class ne{constructor(t,e,n,r,s,i){const o=[];for(let e=0;e<n.length;++e){const r=new y(n[e]).getMeasure(i.distanceFnArgs[e]);o.push(r);let s=2048;for(let n=0;n<t[e].length;++n)if(t[e][n]&&t[e][n]._length){s=t[e][n]._length;break}if(w(n[e]))for(let n=0;n<t[e].length;++n)t[e][n]&&t[e][n]._data?t[e][n]=new xt.A(t[e][n]._data,t[e][n]._length):t[e][n]=new xt.A(s)}"UMAP"==e?this.reducer=new te({data:t,distanceFnames:n,distanceFns:o,distanceFnArgs:i.distanceFnArgs,weights:r,aggregationMethod:s,...i}):"t-SNE"==e&&(this.reducer=new Jt({data:t,distanceFnames:n,distanceFns:o,distanceFnArgs:i.distanceFnArgs,weights:r,aggregationMethod:s,...i}))}async transform(t=!1){if(void 0===this.reducer)throw new Error("Reducer was not defined.");let e=await this.reducer.transform();return t&&(e=function(t){return new Array(t[0].length).fill(0).map((e,n)=>new r(t.length).fill(0).map((e,r)=>t[r][n]))}(e)),e}static availableMetricsByType(t){return Object.keys(d[t])}static get availableMethods(){return Object.keys(ee)}static get availableMetrics(){let t=[];return Object.values(d).forEach(e=>{const n=Object.values(e);t=[...t,...n]}),t}}async function re(t,e,n){t%5==0&&self.postMessage({epochNum:t,epochsLength:e,embedding:n})}self.onmessage=async({data:{columnsData:t,method:e,distanceMetrics:n,options:r,weights:s,aggregationMethod:i}})=>{let o;try{const a=await async function(t,e,n,r,s,i){const o=new ne(t,e,n,r,s,{...i,progressFunc:re});return await o.transform(!0)}(t,e,n,s,i,r);o={embedding:a}}catch(t){o={error:t}}self.postMessage({error:o.error,embedding:o.embedding})}},5540:(t,e,n)=>{"use strict";t=n.hmd(t);var r,s=(r="undefined"!=typeof document&&document.currentScript?document.currentScript.src:void 0,function(t={}){var e,n,s=void 0!==t?t:{};s.ready=new Promise(function(t,r){e=t,n=r});var i,o=Object.assign({},s),a="object"==typeof window,u="function"==typeof importScripts,l=("object"==typeof process&&"object"==typeof process.versions&&process.versions.node,"");(a||u)&&(u?l=self.location.href:"undefined"!=typeof document&&document.currentScript&&(l=document.currentScript.src),r&&(l=r),l=0!==l.indexOf("blob:")?l.substr(0,l.replace(/[?#].*/,"").lastIndexOf("/")+1):"",u&&(i=t=>{var e=new XMLHttpRequest;return e.open("GET",t,!1),e.responseType="arraybuffer",e.send(null),new Uint8Array(e.response)})),s.print||console.log.bind(console);var h,c,f=s.printErr||console.warn.bind(console);Object.assign(s,o),o=null,s.arguments&&s.arguments,s.thisProgram&&s.thisProgram,s.quit&&s.quit,s.wasmBinary&&(h=s.wasmBinary),s.noExitRuntime,"object"!=typeof WebAssembly&&R("no native wasm support detected");var m,g,d,p=!1;function w(){var t=c.buffer;s.HEAP8=m=new Int8Array(t),s.HEAP16=new Int16Array(t),s.HEAP32=new Int32Array(t),s.HEAPU8=g=new Uint8Array(t),s.HEAPU16=new Uint16Array(t),s.HEAPU32=d=new Uint32Array(t),s.HEAPF32=new Float32Array(t),s.HEAPF64=new Float64Array(t)}var y=[],b=[],x=[];function M(t){y.unshift(t)}function v(t){x.unshift(t)}var S,A,E=0,I=null,N=null;function R(t){s.onAbort&&s.onAbort(t),f(t="Aborted("+t+")"),p=!0,t+=". Build with -sASSERTIONS for more info.";var e=new WebAssembly.RuntimeError(t);throw n(e),e}function k(t){return t.startsWith("data:application/octet-stream;base64,")}function C(t){try{if(t==S&&h)return new Uint8Array(h);if(i)return i(t);throw"both async and sync fetching of the wasm failed"}catch(t){R(t)}}function T(t,e,n){return function(t){return h||!a&&!u||"function"!=typeof fetch?Promise.resolve().then(function(){return C(t)}):fetch(t,{credentials:"same-origin"}).then(function(e){if(!e.ok)throw"failed to load wasm binary file at '"+t+"'";return e.arrayBuffer()}).catch(function(){return C(t)})}(t).then(function(t){return WebAssembly.instantiate(t,e)}).then(function(t){return t}).then(n,function(t){f("failed to asynchronously prepare wasm: "+t),R(t)})}function P(t){for(;t.length>0;)t.shift()(s)}function D(t){this.excPtr=t,this.ptr=t-24,this.set_type=function(t){d[this.ptr+4>>2]=t},this.get_type=function(){return d[this.ptr+4>>2]},this.set_destructor=function(t){d[this.ptr+8>>2]=t},this.get_destructor=function(){return d[this.ptr+8>>2]},this.set_caught=function(t){t=t?1:0,m[this.ptr+12|0]=t},this.get_caught=function(){return 0!=m[this.ptr+12|0]},this.set_rethrown=function(t){t=t?1:0,m[this.ptr+13|0]=t},this.get_rethrown=function(){return 0!=m[this.ptr+13|0]},this.init=function(t,e){this.set_adjusted_ptr(0),this.set_type(t),this.set_destructor(e)},this.set_adjusted_ptr=function(t){d[this.ptr+16>>2]=t},this.get_adjusted_ptr=function(){return d[this.ptr+16>>2]},this.get_exception_ptr=function(){if(j(this.get_type()))return d[this.excPtr>>2];var t=this.get_adjusted_ptr();return 0!==t?t:this.excPtr}}function U(t){var e=c.buffer;try{return c.grow(t-e.byteLength+65535>>>16),w(),1}catch(t){}}function O(t){return s["_"+t]}k(S="wasmDbscan.wasm")||(A=S,S=s.locateFile?s.locateFile(A,l):l+A);var z="undefined"!=typeof TextDecoder?new TextDecoder("utf8"):void 0;function B(t,e,n,r,s){var i={string:t=>{var e=0;return null!=t&&0!==t&&(e=function(t){var e=function(t){for(var e=0,n=0;n<t.length;++n){var r=t.charCodeAt(n);r<=127?e++:r<=2047?e+=2:r>=55296&&r<=57343?(e+=4,++n):e+=3}return e}(t)+1,n=F(e);return function(t,e,n){!function(t,e,n,r){if(!(r>0))return 0;for(var s=n+r-1,i=0;i<t.length;++i){var o=t.charCodeAt(i);if(o>=55296&&o<=57343&&(o=65536+((1023&o)<<10)|1023&t.charCodeAt(++i)),o<=127){if(n>=s)break;e[n++]=o}else if(o<=2047){if(n+1>=s)break;e[n++]=192|o>>6,e[n++]=128|63&o}else if(o<=65535){if(n+2>=s)break;e[n++]=224|o>>12,e[n++]=128|o>>6&63,e[n++]=128|63&o}else{if(n+3>=s)break;e[n++]=240|o>>18,e[n++]=128|o>>12&63,e[n++]=128|o>>6&63,e[n++]=128|63&o}}e[n]=0}(t,g,e,n)}(t,n,e),n}(t)),e},array:t=>{var e,n,r=F(t.length);return e=t,n=r,m.set(e,n),r}},o=O(t),a=[],u=0;if(r)for(var l=0;l<r.length;l++){var h=i[n[l]];h?(0===u&&(u=$()),a[l]=h(r[l])):a[l]=r[l]}var c=o.apply(null,a);return function(t){return 0!==u&&G(u),function(t){return"string"===e?(n=t)?function(t,e,n){for(var r=e+n,s=e;t[s]&&!(s>=r);)++s;if(s-e>16&&t.buffer&&z)return z.decode(t.subarray(e,s));for(var i="";e<s;){var o=t[e++];if(128&o){var a=63&t[e++];if(192!=(224&o)){var u=63&t[e++];if((o=224==(240&o)?(15&o)<<12|a<<6|u:(7&o)<<18|a<<12|u<<6|63&t[e++])<65536)i+=String.fromCharCode(o);else{var l=o-65536;i+=String.fromCharCode(55296|l>>10,56320|1023&l)}}else i+=String.fromCharCode((31&o)<<6|a)}else i+=String.fromCharCode(o)}return i}(g,n,r):"":"boolean"===e?Boolean(t):t;var n,r}(t)}(c)}var _,L={__cxa_throw:function(t,e,n){throw new D(t).init(e,n),t},abort:function(){R("")},emscripten_memcpy_big:function(t,e,n){g.copyWithin(t,e,e+n)},emscripten_resize_heap:function(t){var e=g.length,n=2147483648;if((t>>>=0)>n)return!1;let r=(t,e)=>t+(e-t%e)%e;for(var s=1;s<=4;s*=2){var i=e*(1+.2/s);if(i=Math.min(i,t+100663296),U(Math.min(n,r(Math.max(t,i),65536))))return!0}return!1}},$=(function(){var t,e,r,i,o={env:L,wasi_snapshot_preview1:L};function a(t,e){var n,r=t.exports;return s.asm=r,c=s.asm.memory,w(),s.asm.__indirect_function_table,n=s.asm.__wasm_call_ctors,b.unshift(n),function(){if(E--,s.monitorRunDependencies&&s.monitorRunDependencies(E),0==E&&(null!==I&&(clearInterval(I),I=null),N)){var t=N;N=null,t()}}(),r}if(E++,s.monitorRunDependencies&&s.monitorRunDependencies(E),s.instantiateWasm)try{return s.instantiateWasm(o,a)}catch(t){f("Module.instantiateWasm callback failed with error: "+t),n(t)}(t=h,e=S,r=o,i=function(t){a(t.instance)},t||"function"!=typeof WebAssembly.instantiateStreaming||k(e)||"function"!=typeof fetch?T(e,r,i):fetch(e,{credentials:"same-origin"}).then(function(t){return WebAssembly.instantiateStreaming(t,r).then(i,function(t){return f("wasm streaming compile failed: "+t),f("falling back to ArrayBuffer instantiation"),T(e,r,i)})})).catch(n)}(),s._dbscan=function(){return(s._dbscan=s.asm.dbscan).apply(null,arguments)},s._malloc=function(){return(s._malloc=s.asm.malloc).apply(null,arguments)},s._free=function(){return(s._free=s.asm.free).apply(null,arguments)},function(){return($=s.asm.stackSave).apply(null,arguments)}),G=function(){return(G=s.asm.stackRestore).apply(null,arguments)},F=function(){return(F=s.asm.stackAlloc).apply(null,arguments)},j=function(){return(j=s.asm.__cxa_is_pointer_type).apply(null,arguments)};function q(){function t(){_||(_=!0,s.calledRun=!0,p||(P(b),e(s),s.onRuntimeInitialized&&s.onRuntimeInitialized(),function(){if(s.postRun)for("function"==typeof s.postRun&&(s.postRun=[s.postRun]);s.postRun.length;)v(s.postRun.shift());P(x)}()))}E>0||(function(){if(s.preRun)for("function"==typeof s.preRun&&(s.preRun=[s.preRun]);s.preRun.length;)M(s.preRun.shift());P(y)}(),E>0||(s.setStatus?(s.setStatus("Running..."),setTimeout(function(){setTimeout(function(){s.setStatus("")},1),t()},1)):t()))}if(s.ccall=B,s.cwrap=function(t,e,n,r){var s=!n||n.every(t=>"number"===t||"boolean"===t);return"string"!==e&&s&&!r?O(t):function(){return B(t,e,n,arguments)}},N=function t(){_||q(),_||(N=t)},s.preInit)for("function"==typeof s.preInit&&(s.preInit=[s.preInit]);s.preInit.length>0;)s.preInit.pop()();return q(),t.ready});"object"==typeof exports?t.exports=s:"function"==typeof define&&n.amdO?define([],function(){return s}):"object"==typeof exports&&(exports.exportCppDbscanLib=s)},6833:function(t,e,n){var r;!function(t,s){function i(t){var e=this;e.next=function(){var t,n,r=e.w,s=e.X,i=e.i;return e.w=r=r+1640531527|0,n=s[i+34&127],t=s[i=i+1&127],n^=n<<13,t^=t<<17,n^=n>>>15,t^=t>>>12,n=s[i]=n^t,e.i=i,n+(r^r>>>16)|0},function(t,e){var n,r,s,i,o,a=[],u=128;for(e===(0|e)?(r=e,e=null):(e+="\0",r=0,u=Math.max(u,e.length)),s=0,i=-32;i<u;++i)e&&(r^=e.charCodeAt((i+32)%e.length)),0===i&&(o=r),r^=r<<10,r^=r>>>15,r^=r<<4,r^=r>>>13,i>=0&&(o=o+1640531527|0,s=0==(n=a[127&i]^=r+o)?s+1:0);for(s>=128&&(a[127&(e&&e.length||0)]=-1),s=127,i=512;i>0;--i)r=a[s+34&127],n=a[s=s+1&127],r^=r<<13,n^=n<<17,r^=r>>>15,n^=n>>>12,a[s]=r^n;t.w=o,t.X=a,t.i=s}(e,t)}function o(t,e){return e.i=t.i,e.w=t.w,e.X=t.X.slice(),e}function a(t,e){null==t&&(t=+new Date);var n=new i(t),r=e&&e.state,s=function(){return(n.next()>>>0)/4294967296};return s.double=function(){do{var t=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===t);return t},s.int32=n.next,s.quick=s,r&&(r.X&&o(r,n),s.state=function(){return o(n,{})}),s}s&&s.exports?s.exports=a:n.amdD&&n.amdO?void 0===(r=function(){return a}.call(e,n,e,s))||(s.exports=r):this.xor4096=a}(0,t=n.nmd(t),n.amdD)},7180:function(t,e,n){var r;!function(t,s){function i(t){var e,n=this,r=(e=4022871197,function(t){t=String(t);for(var n=0;n<t.length;n++){var r=.02519603282416938*(e+=t.charCodeAt(n));r-=e=r>>>0,e=(r*=e)>>>0,e+=4294967296*(r-=e)}return 2.3283064365386963e-10*(e>>>0)});n.next=function(){var t=2091639*n.s0+2.3283064365386963e-10*n.c;return n.s0=n.s1,n.s1=n.s2,n.s2=t-(n.c=0|t)},n.c=1,n.s0=r(" "),n.s1=r(" "),n.s2=r(" "),n.s0-=r(t),n.s0<0&&(n.s0+=1),n.s1-=r(t),n.s1<0&&(n.s1+=1),n.s2-=r(t),n.s2<0&&(n.s2+=1),r=null}function o(t,e){return e.c=t.c,e.s0=t.s0,e.s1=t.s1,e.s2=t.s2,e}function a(t,e){var n=new i(t),r=e&&e.state,s=n.next;return s.int32=function(){return 4294967296*n.next()|0},s.double=function(){return s()+11102230246251565e-32*(2097152*s()|0)},s.quick=s,r&&("object"==typeof r&&o(r,n),s.state=function(){return o(n,{})}),s}s&&s.exports?s.exports=a:n.amdD&&n.amdO?void 0===(r=function(){return a}.call(e,n,e,s))||(s.exports=r):this.alea=a}(0,t=n.nmd(t),n.amdD)},7391:(t,e,n)=>{var r=n(7180),s=n(3181),i=n(3031),o=n(9067),a=n(6833),u=n(3717),l=n(4801);l.alea=r,l.xor128=s,l.xorwow=i,l.xorshift7=o,l.xor4096=a,l.tychei=u,t.exports=l},9067:function(t,e,n){var r;!function(t,s){function i(t){var e=this;e.next=function(){var t,n,r=e.x,s=e.i;return t=r[s],n=(t^=t>>>7)^t<<24,n^=(t=r[s+1&7])^t>>>10,n^=(t=r[s+3&7])^t>>>3,n^=(t=r[s+4&7])^t<<7,t=r[s+7&7],n^=(t^=t<<13)^t<<9,r[s]=n,e.i=s+1&7,n},function(t,e){var n,r=[];if(e===(0|e))r[0]=e;else for(e=""+e,n=0;n<e.length;++n)r[7&n]=r[7&n]<<15^e.charCodeAt(n)+r[n+1&7]<<13;for(;r.length<8;)r.push(0);for(n=0;n<8&&0===r[n];++n);for(8==n?r[7]=-1:r[n],t.x=r,t.i=0,n=256;n>0;--n)t.next()}(e,t)}function o(t,e){return e.x=t.x.slice(),e.i=t.i,e}function a(t,e){null==t&&(t=+new Date);var n=new i(t),r=e&&e.state,s=function(){return(n.next()>>>0)/4294967296};return s.double=function(){do{var t=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===t);return t},s.int32=n.next,s.quick=s,r&&(r.x&&o(r,n),s.state=function(){return o(n,{})}),s}s&&s.exports?s.exports=a:n.amdD&&n.amdO?void 0===(r=function(){return a}.call(e,n,e,s))||(s.exports=r):this.xorshift7=a}(0,t=n.nmd(t),n.amdD)}}]);
3
3
  //# sourceMappingURL=284.js.map