w-cluster 1.0.35 → 1.0.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -277,9 +277,9 @@ Add script for w-cluster.
277
277
  ```alias
278
278
 
279
279
  <!-- for basic -->
280
- <script src="https://cdn.jsdelivr.net/npm/w-cluster@1.0.35/dist/w-cluster.umd.js"></script>
280
+ <script src="https://cdn.jsdelivr.net/npm/w-cluster@1.0.36/dist/w-cluster.umd.js"></script>
281
281
 
282
282
  <!-- for web workers -->
283
- <script src="https://cdn.jsdelivr.net/npm/w-cluster@1.0.35/dist/w-cluster.wk.umd.js"></script>
283
+ <script src="https://cdn.jsdelivr.net/npm/w-cluster@1.0.36/dist/w-cluster.wk.umd.js"></script>
284
284
 
285
285
  ```
@@ -1,7 +1,7 @@
1
1
  /*!
2
- * w-cluster v1.0.35
2
+ * w-cluster v1.0.36
3
3
  * (c) 2018-2021 yuda-lyu(semisphere)
4
4
  * Released under the MIT License.
5
5
  */
6
- !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self)["w-cluster"]=e()}(this,(function(){"use strict";var t=Array.isArray,e="object"==typeof global&&global&&global.Object===Object&&global,r="object"==typeof self&&self&&self.Object===Object&&self,n=e||r||Function("return this")(),o=n.Symbol,s=Object.prototype,i=s.hasOwnProperty,u=s.toString,l=o?o.toStringTag:void 0;var a=Object.prototype.toString;var h="[object Null]",c="[object Undefined]",f=o?o.toStringTag:void 0;function m(t){return null==t?void 0===t?c:h:f&&f in Object(t)?function(t){var e=i.call(t,l),r=t[l];try{t[l]=void 0;var n=!0}catch(t){}var o=u.call(t);return n&&(e?t[l]=r:delete t[l]),o}(t):function(t){return a.call(t)}(t)}function g(t){return null!=t&&"object"==typeof t}var w="[object Symbol]";function d(t){return"symbol"==typeof t||g(t)&&m(t)==w}var p=/\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/,y=/^\w*$/;function b(e,r){if(t(e))return!1;var n=typeof e;return!("number"!=n&&"symbol"!=n&&"boolean"!=n&&null!=e&&!d(e))||(y.test(e)||!p.test(e)||null!=r&&e in Object(r))}function v(t){var e=typeof t;return null!=t&&("object"==e||"function"==e)}var M="[object AsyncFunction]",x="[object Function]",E="[object GeneratorFunction]",S="[object Proxy]";function _(t){if(!v(t))return!1;var e=m(t);return e==x||e==E||e==M||e==S}var j,R=n["__core-js_shared__"],A=(j=/[^.]+$/.exec(R&&R.keys&&R.keys.IE_PROTO||""))?"Symbol(src)_1."+j:"";var I=Function.prototype.toString;function C(t){if(null!=t){try{return I.call(t)}catch(t){}try{return t+""}catch(t){}}return""}var k=/^\[object .+?Constructor\]$/,N=Function.prototype,T=Object.prototype,F=N.toString,O=T.hasOwnProperty,V=RegExp("^"+F.call(O).replace(/[\\^$.*+?()[\]{}|]/g,"\\$&").replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g,"$1.*?")+"$");function z(t){return!(!v(t)||(e=t,A&&A in e))&&(_(t)?V:k).test(C(t));var e}function D(t,e){var r=function(t,e){return null==t?void 0:t[e]}(t,e);return z(r)?r:void 0}var P=D(Object,"create");var q=Object.prototype.hasOwnProperty;var $=Object.prototype.hasOwnProperty;function L(t){var e=-1,r=null==t?0:t.length;for(this.clear();++e<r;){var n=t[e];this.set(n[0],n[1])}}function U(t,e){return t===e||t!=t&&e!=e}function B(t,e){for(var r=t.length;r--;)if(U(t[r][0],e))return r;return-1}L.prototype.clear=function(){this.__data__=P?P(null):{},this.size=0},L.prototype.delete=function(t){var e=this.has(t)&&delete this.__data__[t];return this.size-=e?1:0,e},L.prototype.get=function(t){var e=this.__data__;if(P){var r=e[t];return"__lodash_hash_undefined__"===r?void 0:r}return q.call(e,t)?e[t]:void 0},L.prototype.has=function(t){var e=this.__data__;return P?void 0!==e[t]:$.call(e,t)},L.prototype.set=function(t,e){var r=this.__data__;return this.size+=this.has(t)?0:1,r[t]=P&&void 0===e?"__lodash_hash_undefined__":e,this};var G=Array.prototype.splice;function W(t){var e=-1,r=null==t?0:t.length;for(this.clear();++e<r;){var n=t[e];this.set(n[0],n[1])}}W.prototype.clear=function(){this.__data__=[],this.size=0},W.prototype.delete=function(t){var e=this.__data__,r=B(e,t);return!(r<0)&&(r==e.length-1?e.pop():G.call(e,r,1),--this.size,!0)},W.prototype.get=function(t){var e=this.__data__,r=B(e,t);return r<0?void 0:e[r][1]},W.prototype.has=function(t){return B(this.__data__,t)>-1},W.prototype.set=function(t,e){var r=this.__data__,n=B(r,t);return n<0?(++this.size,r.push([t,e])):r[n][1]=e,this};var K=D(n,"Map");function Q(t,e){var r,n,o=t.__data__;return("string"==(n=typeof(r=e))||"number"==n||"symbol"==n||"boolean"==n?"__proto__"!==r:null===r)?o["string"==typeof e?"string":"hash"]:o.map}function Y(t){var e=-1,r=null==t?0:t.length;for(this.clear();++e<r;){var n=t[e];this.set(n[0],n[1])}}Y.prototype.clear=function(){this.size=0,this.__data__={hash:new L,map:new(K||W),string:new L}},Y.prototype.delete=function(t){var e=Q(this,t).delete(t);return this.size-=e?1:0,e},Y.prototype.get=function(t){return Q(this,t).get(t)},Y.prototype.has=function(t){return Q(this,t).has(t)},Y.prototype.set=function(t,e){var r=Q(this,t),n=r.size;return r.set(t,e),this.size+=r.size==n?0:1,this};var J="Expected a function";function X(t,e){if("function"!=typeof t||null!=e&&"function"!=typeof e)throw new TypeError(J);var r=function(){var n=arguments,o=e?e.apply(this,n):n[0],s=r.cache;if(s.has(o))return s.get(o);var i=t.apply(this,n);return r.cache=s.set(o,i)||s,i};return r.cache=new(X.Cache||Y),r}X.Cache=Y;var H,Z,tt,et=/[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g,rt=/\\(\\)?/g,nt=(H=function(t){var e=[];return 46===t.charCodeAt(0)&&e.push(""),t.replace(et,(function(t,r,n,o){e.push(n?o.replace(rt,"$1"):r||t)})),e},Z=X(H,(function(t){return 500===tt.size&&tt.clear(),t})),tt=Z.cache,Z),ot=nt;function st(t,e){for(var r=-1,n=null==t?0:t.length,o=Array(n);++r<n;)o[r]=e(t[r],r,t);return o}var it=1/0,ut=o?o.prototype:void 0,lt=ut?ut.toString:void 0;function at(e){if("string"==typeof e)return e;if(t(e))return st(e,at)+"";if(d(e))return lt?lt.call(e):"";var r=e+"";return"0"==r&&1/e==-it?"-0":r}function ht(t){return null==t?"":at(t)}function ct(e,r){return t(e)?e:b(e,r)?[e]:ot(ht(e))}var ft=1/0;function mt(t){if("string"==typeof t||d(t))return t;var e=t+"";return"0"==e&&1/t==-ft?"-0":e}function gt(t,e){for(var r=0,n=(e=ct(e,t)).length;null!=t&&r<n;)t=t[mt(e[r++])];return r&&r==n?t:void 0}function wt(t,e,r){var n=null==t?void 0:gt(t,e);return void 0===n?r:n}var dt=Object.prototype;var pt=function(t,e){return function(r){return t(e(r))}}(Object.keys,Object),yt=pt,bt=Object.prototype.hasOwnProperty;function vt(t){if(r=(e=t)&&e.constructor,e!==("function"==typeof r&&r.prototype||dt))return yt(t);var e,r,n=[];for(var o in Object(t))bt.call(t,o)&&"constructor"!=o&&n.push(o);return n}var Mt=D(n,"DataView"),xt=D(n,"Promise"),Et=D(n,"Set"),St=D(n,"WeakMap"),_t="[object Map]",jt="[object Promise]",Rt="[object Set]",At="[object WeakMap]",It="[object DataView]",Ct=C(Mt),kt=C(K),Nt=C(xt),Tt=C(Et),Ft=C(St),Ot=m;(Mt&&Ot(new Mt(new ArrayBuffer(1)))!=It||K&&Ot(new K)!=_t||xt&&Ot(xt.resolve())!=jt||Et&&Ot(new Et)!=Rt||St&&Ot(new St)!=At)&&(Ot=function(t){var e=m(t),r="[object Object]"==e?t.constructor:void 0,n=r?C(r):"";if(n)switch(n){case Ct:return It;case kt:return _t;case Nt:return jt;case Tt:return Rt;case Ft:return At}return e});var Vt=Ot,zt=9007199254740991;function Dt(t){return"number"==typeof t&&t>-1&&t%1==0&&t<=zt}function Pt(t){return null!=t&&Dt(t.length)&&!_(t)}var qt="[object String]";function $t(t){return function(e){return null==e?void 0:e[t]}}var Lt=$t("length"),Ut=RegExp("[\\u200d\\ud800-\\udfff\\u0300-\\u036f\\ufe20-\\ufe2f\\u20d0-\\u20ff\\ufe0e\\ufe0f]");var Bt="\\ud800-\\udfff",Gt="["+Bt+"]",Wt="[\\u0300-\\u036f\\ufe20-\\ufe2f\\u20d0-\\u20ff]",Kt="\\ud83c[\\udffb-\\udfff]",Qt="[^"+Bt+"]",Yt="(?:\\ud83c[\\udde6-\\uddff]){2}",Jt="[\\ud800-\\udbff][\\udc00-\\udfff]",Xt="(?:"+Wt+"|"+Kt+")"+"?",Ht="[\\ufe0e\\ufe0f]?",Zt=Ht+Xt+("(?:\\u200d(?:"+[Qt,Yt,Jt].join("|")+")"+Ht+Xt+")*"),te="(?:"+[Qt+Wt+"?",Wt,Yt,Jt,Gt].join("|")+")",ee=RegExp(Kt+"(?="+Kt+")|"+te+Zt,"g");function re(t){return function(t){return Ut.test(t)}(t)?function(t){for(var e=ee.lastIndex=0;ee.test(t);)++e;return e}(t):Lt(t)}var ne="[object Map]",oe="[object Set]";function se(e){if(null==e)return 0;if(Pt(e))return"string"==typeof(r=e)||!t(r)&&g(r)&&m(r)==qt?re(e):e.length;var r,n=Vt(e);return n==ne||n==oe?e.size:vt(e).length}function ie(t){return"[object Array]"===Object.prototype.toString.call(t)}function ue(t){return"[object Object]"===Object.prototype.toString.call(t)}function le(t){return"[object String]"===Object.prototype.toString.call(t)}function ae(t){return t!=t}function he(t){return!!function(t){return"[object Undefined]"===Object.prototype.toString.call(t)}(t)||(!!function(t){return"[object Null]"===Object.prototype.toString.call(t)}(t)||(!!function(t){if(ue(t)){for(let e in t)return!1;return!0}return!1}(t)||(!!function(t){return!(!le(t)||""!==t)}(t)||(!!function(t){return!!ie(t)&&0===t.length}(t)||!!ae(t)))))}function ce(t){return!!ie(t)&&(0!==t.length&&(1!==t.length||!he(t[0])))}var fe="[object Boolean]";function me(t){return!0===(e=t)||!1===e||g(e)&&m(e)==fe;var e}function ge(t){return!(!le(t)||""===t)}function we(t){let e=!1;if(ge(t))e=!isNaN(Number(t));else if(function(t){return"[object Number]"===Object.prototype.toString.call(t)}(t)){if(ae(t))return!1;e=!0}return e}var de=/\s/;var pe=/^\s+/;function ye(t){return t?t.slice(0,function(t){for(var e=t.length;e--&&de.test(t.charAt(e)););return e}(t)+1).replace(pe,""):t}var be=NaN,ve=/^[-+]0x[0-9a-f]+$/i,Me=/^0b[01]+$/i,xe=/^0o[0-7]+$/i,Ee=parseInt;function Se(t){if("number"==typeof t)return t;if(d(t))return be;if(v(t)){var e="function"==typeof t.valueOf?t.valueOf():t;t=v(e)?e+"":e}if("string"!=typeof t)return 0===t?t:+t;t=ye(t);var r=Me.test(t);return r||xe.test(t)?Ee(t.slice(2),r?2:8):ve.test(t)?be:+t}var _e=1/0,je=17976931348623157e292;function Re(t){return t?(t=Se(t))===_e||t===-_e?(t<0?-1:1)*je:t==t?t:0:0===t?t:0}var Ae=n.isFinite,Ie=Math.min;var Ce=function(t){var e=Math[t];return function(t,r){if(t=Se(t),r=null==r?0:Ie(function(t){var e=Re(t),r=e%1;return e==e?r?e-r:e:0}(r),292),r&&Ae(t)){var n=(ht(t)+"e").split("e");return+((n=(ht(e(n[0]+"e"+(+n[1]+r)))+"e").split("e"))[0]+"e"+(+n[1]-r))}return e(t)}}("round"),ke=Ce;function Ne(t){if(!we(t))return 0;t=function(t){return we(t)?Re(t):0}(t);let e=ke(t);return"0"===String(e)?0:e}function Te(t,e,r){if(!e.has(t))throw new TypeError("attempted to "+r+" private field on non-instance");return e.get(t)}function Fe(t,e,r){return function(t,e,r){if(e.set)e.set.call(t,r);else{if(!e.writable)throw new TypeError("attempted to set read only private field");e.value=r}}(t,Te(t,e,"set"),r),r}function Oe(t,e){return function(t,e){return e.get?e.get.call(t):e.value}(t,Te(t,e,"get"))}function Ve(t){return Ve="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},Ve(t)}function ze(t){var e=function(t,e){if("object"!==Ve(t)||null===t)return t;var r=t[Symbol.toPrimitive];if(void 0!==r){var n=r.call(t,e||"default");if("object"!==Ve(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"===Ve(e)?e:String(e)}function De(t,e,r){return(e=ze(e))in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function Pe(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}var qe={};function $e(t,e){if(e.has(t))throw new TypeError("Cannot initialize the same private elements twice on an object")}function Le(t,e,r){if(!e.has(t))throw new TypeError("attempted to get private field on non-instance");return r}Object.defineProperty(qe,"__esModule",{value:!0});const Ue=Object.prototype.toString;function Be(t){const e=Ue.call(t);return e.endsWith("Array]")&&!e.includes("Big")}function Ge(t){let e,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!Be(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");if(void 0!==r.output){if(!Be(r.output))throw new TypeError("output option must be an array if specified");e=r.output}else e=new Array(t.length);const n=function(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!Be(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");const{fromIndex:r=0,toIndex:n=t.length}=e;if(r<0||r>=t.length||!Number.isInteger(r))throw new Error("fromIndex must be a positive integer smaller than length");if(n<=r||n>t.length||!Number.isInteger(n))throw new Error("toIndex must be an integer greater than fromIndex and at most equal to length");let o=t[r];for(let e=r+1;e<n;e++)t[e]<o&&(o=t[e]);return o}(t),o=function(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!Be(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");const{fromIndex:r=0,toIndex:n=t.length}=e;if(r<0||r>=t.length||!Number.isInteger(r))throw new Error("fromIndex must be a positive integer smaller than length");if(n<=r||n>t.length||!Number.isInteger(n))throw new Error("toIndex must be an integer greater than fromIndex and at most equal to length");let o=t[r];for(let e=r+1;e<n;e++)t[e]>o&&(o=t[e]);return o}(t);if(n===o)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");const{min:s=(r.autoMinMax?n:0),max:i=(r.autoMinMax?o:1)}=r;if(s>=i)throw new RangeError("min option must be smaller than max option");const u=(i-s)/(o-n);for(let r=0;r<t.length;r++)e[r]=(t[r]-n)*u+s;return e}const We=" ".repeat(2),Ke=" ".repeat(4);function Qe(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};const{maxRows:r=15,maxColumns:n=10,maxNumSize:o=8,padMinus:s="auto"}=e;return`${t.constructor.name} {\n${We}[\n${Ke}${function(t,e,r,n,o){const{rows:s,columns:i}=t,u=Math.min(s,e),l=Math.min(i,r),a=[];if("auto"===o){o=!1;t:for(let e=0;e<u;e++)for(let r=0;r<l;r++)if(t.get(e,r)<0){o=!0;break t}}for(let e=0;e<u;e++){let r=[];for(let s=0;s<l;s++)r.push(Ye(t.get(e,s),n,o));a.push(`${r.join(" ")}`)}l!==i&&(a[a.length-1]+=` ... ${i-r} more columns`);u!==s&&a.push(`... ${s-e} more rows`);return a.join(`\n${Ke}`)}(t,r,n,o,s)}\n${We}]\n${We}rows: ${t.rows}\n${We}columns: ${t.columns}\n}`}function Ye(t,e,r){return(t>=0&&r?` ${Je(t,e-1)}`:Je(t,e)).padEnd(e)}function Je(t,e){let r=t.toString();if(r.length<=e)return r;let n=t.toFixed(e);if(n.length>e&&(n=t.toFixed(Math.max(0,e-(n.length-e)))),n.length<=e&&!n.startsWith("0.000")&&!n.startsWith("-0.000"))return n;let o=t.toExponential(e);return o.length>e&&(o=t.toExponential(Math.max(0,e-(o.length-e)))),o.slice(0)}function Xe(t,e,r){let n=r?t.rows:t.rows-1;if(e<0||e>n)throw new RangeError("Row index out of range")}function He(t,e,r){let n=r?t.columns:t.columns-1;if(e<0||e>n)throw new RangeError("Column index out of range")}function Ze(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 tr(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 er(t,e){if(!Be(e))throw new TypeError("row indices must be an array");for(let r=0;r<e.length;r++)if(e[r]<0||e[r]>=t.rows)throw new RangeError("row indices are out of range")}function rr(t,e){if(!Be(e))throw new TypeError("column indices must be an array");for(let r=0;r<e.length;r++)if(e[r]<0||e[r]>=t.columns)throw new RangeError("column indices are out of range")}function nr(t,e,r,n,o){if(5!==arguments.length)throw new RangeError("expected 4 arguments");if(sr("startRow",e),sr("endRow",r),sr("startColumn",n),sr("endColumn",o),e>r||n>o||e<0||e>=t.rows||r<0||r>=t.rows||n<0||n>=t.columns||o<0||o>=t.columns)throw new RangeError("Submatrix indices are out of range")}function or(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,r=[];for(let n=0;n<t;n++)r.push(e);return r}function sr(t,e){if("number"!=typeof e)throw new TypeError(`${t} must be a number`)}function ir(t){if(t.isEmpty())throw new Error("Empty matrix has no elements to index")}class ur{static from1DArray(t,e,r){if(t*e!==r.length)throw new RangeError("data length does not match given dimensions");let n=new hr(t,e);for(let o=0;o<t;o++)for(let t=0;t<e;t++)n.set(o,t,r[o*e+t]);return n}static rowVector(t){let e=new hr(1,t.length);for(let r=0;r<t.length;r++)e.set(0,r,t[r]);return e}static columnVector(t){let e=new hr(t.length,1);for(let r=0;r<t.length;r++)e.set(r,0,t[r]);return e}static zeros(t,e){return new hr(t,e)}static ones(t,e){return new hr(t,e).fill(1)}static rand(t,e){let r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};if("object"!=typeof r)throw new TypeError("options must be an object");const{random:n=Math.random}=r;let o=new hr(t,e);for(let r=0;r<t;r++)for(let t=0;t<e;t++)o.set(r,t,n());return o}static randInt(t,e){let r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};if("object"!=typeof r)throw new TypeError("options must be an object");const{min:n=0,max:o=1e3,random:s=Math.random}=r;if(!Number.isInteger(n))throw new TypeError("min must be an integer");if(!Number.isInteger(o))throw new TypeError("max must be an integer");if(n>=o)throw new RangeError("min must be smaller than max");let i=o-n,u=new hr(t,e);for(let r=0;r<t;r++)for(let t=0;t<e;t++){let e=n+Math.round(s()*i);u.set(r,t,e)}return u}static eye(t,e,r){void 0===e&&(e=t),void 0===r&&(r=1);let n=Math.min(t,e),o=this.zeros(t,e);for(let t=0;t<n;t++)o.set(t,t,r);return o}static diag(t,e,r){let n=t.length;void 0===e&&(e=n),void 0===r&&(r=e);let o=Math.min(n,e,r),s=this.zeros(e,r);for(let e=0;e<o;e++)s.set(e,e,t[e]);return s}static min(t,e){t=this.checkMatrix(t),e=this.checkMatrix(e);let r=t.rows,n=t.columns,o=new hr(r,n);for(let s=0;s<r;s++)for(let r=0;r<n;r++)o.set(s,r,Math.min(t.get(s,r),e.get(s,r)));return o}static max(t,e){t=this.checkMatrix(t),e=this.checkMatrix(e);let r=t.rows,n=t.columns,o=new this(r,n);for(let s=0;s<r;s++)for(let r=0;r<n;r++)o.set(s,r,Math.max(t.get(s,r),e.get(s,r)));return o}static checkMatrix(t){return ur.isMatrix(t)?t:new hr(t)}static isMatrix(t){return null!=t&&"Matrix"===t.klass}get size(){return this.rows*this.columns}apply(t){if("function"!=typeof t)throw new TypeError("callback must be a function");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)t.call(this,e,r);return this}to1DArray(){let t=[];for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)t.push(this.get(e,r));return t}to2DArray(){let t=[];for(let e=0;e<this.rows;e++){t.push([]);for(let r=0;r<this.columns;r++)t[e].push(this.get(e,r))}return t}toJSON(){return this.to2DArray()}isRowVector(){return 1===this.rows}isColumnVector(){return 1===this.columns}isVector(){return 1===this.rows||1===this.columns}isSquare(){return this.rows===this.columns}isEmpty(){return 0===this.rows||0===this.columns}isSymmetric(){if(this.isSquare()){for(let t=0;t<this.rows;t++)for(let e=0;e<=t;e++)if(this.get(t,e)!==this.get(e,t))return!1;return!0}return!1}isDistance(){if(!this.isSymmetric())return!1;for(let t=0;t<this.rows;t++)if(0!==this.get(t,t))return!1;return!0}isEchelonForm(){let t=0,e=0,r=-1,n=!0,o=!1;for(;t<this.rows&&n;){for(e=0,o=!1;e<this.columns&&!1===o;)0===this.get(t,e)?e++:1===this.get(t,e)&&e>r?(o=!0,r=e):(n=!1,o=!0);t++}return n}isReducedEchelonForm(){let t=0,e=0,r=-1,n=!0,o=!1;for(;t<this.rows&&n;){for(e=0,o=!1;e<this.columns&&!1===o;)0===this.get(t,e)?e++:1===this.get(t,e)&&e>r?(o=!0,r=e):(n=!1,o=!0);for(let r=e+1;r<this.rows;r++)0!==this.get(t,r)&&(n=!1);t++}return n}echelonForm(){let t=this.clone(),e=0,r=0;for(;e<t.rows&&r<t.columns;){let n=e;for(let o=e;o<t.rows;o++)t.get(o,r)>t.get(n,r)&&(n=o);if(0===t.get(n,r))r++;else{t.swapRows(e,n);let o=t.get(e,r);for(let n=r;n<t.columns;n++)t.set(e,n,t.get(e,n)/o);for(let n=e+1;n<t.rows;n++){let o=t.get(n,r)/t.get(e,r);t.set(n,r,0);for(let s=r+1;s<t.columns;s++)t.set(n,s,t.get(n,s)-t.get(e,s)*o)}e++,r++}}return t}reducedEchelonForm(){let t=this.echelonForm(),e=t.columns,r=t.rows,n=r-1;for(;n>=0;)if(0===t.maxRow(n))n--;else{let o=0,s=!1;for(;o<r&&!1===s;)1===t.get(n,o)?s=!0:o++;for(let r=0;r<n;r++){let s=t.get(r,o);for(let i=o;i<e;i++){let e=t.get(r,i)-s*t.get(n,i);t.set(r,i,e)}}n--}return t}set(){throw new Error("set method is unimplemented")}get(){throw new Error("get method is unimplemented")}repeat(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};if("object"!=typeof t)throw new TypeError("options must be an object");const{rows:e=1,columns:r=1}=t;if(!Number.isInteger(e)||e<=0)throw new TypeError("rows must be a positive integer");if(!Number.isInteger(r)||r<=0)throw new TypeError("columns must be a positive integer");let n=new hr(this.rows*e,this.columns*r);for(let t=0;t<e;t++)for(let e=0;e<r;e++)n.setSubMatrix(this,this.rows*t,this.columns*e);return n}fill(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,t);return this}neg(){return this.mulS(-1)}getRow(t){Xe(this,t);let e=[];for(let r=0;r<this.columns;r++)e.push(this.get(t,r));return e}getRowVector(t){return hr.rowVector(this.getRow(t))}setRow(t,e){Xe(this,t),e=Ze(this,e);for(let r=0;r<this.columns;r++)this.set(t,r,e[r]);return this}swapRows(t,e){Xe(this,t),Xe(this,e);for(let r=0;r<this.columns;r++){let n=this.get(t,r);this.set(t,r,this.get(e,r)),this.set(e,r,n)}return this}getColumn(t){He(this,t);let e=[];for(let r=0;r<this.rows;r++)e.push(this.get(r,t));return e}getColumnVector(t){return hr.columnVector(this.getColumn(t))}setColumn(t,e){He(this,t),e=tr(this,e);for(let r=0;r<this.rows;r++)this.set(r,t,e[r]);return this}swapColumns(t,e){He(this,t),He(this,e);for(let r=0;r<this.rows;r++){let n=this.get(r,t);this.set(r,t,this.get(r,e)),this.set(r,e,n)}return this}addRowVector(t){t=Ze(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)+t[r]);return this}subRowVector(t){t=Ze(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)-t[r]);return this}mulRowVector(t){t=Ze(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)*t[r]);return this}divRowVector(t){t=Ze(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)/t[r]);return this}addColumnVector(t){t=tr(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)+t[e]);return this}subColumnVector(t){t=tr(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)-t[e]);return this}mulColumnVector(t){t=tr(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)*t[e]);return this}divColumnVector(t){t=tr(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)/t[e]);return this}mulRow(t,e){Xe(this,t);for(let r=0;r<this.columns;r++)this.set(t,r,this.get(t,r)*e);return this}mulColumn(t,e){He(this,t);for(let r=0;r<this.rows;r++)this.set(r,t,this.get(r,t)*e);return this}max(t){if(this.isEmpty())return NaN;switch(t){case"row":{const t=new Array(this.rows).fill(Number.NEGATIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.get(e,r)>t[e]&&(t[e]=this.get(e,r));return t}case"column":{const t=new Array(this.columns).fill(Number.NEGATIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.get(e,r)>t[r]&&(t[r]=this.get(e,r));return t}case void 0:{let t=this.get(0,0);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.get(e,r)>t&&(t=this.get(e,r));return t}default:throw new Error(`invalid option: ${t}`)}}maxIndex(){ir(this);let t=this.get(0,0),e=[0,0];for(let r=0;r<this.rows;r++)for(let n=0;n<this.columns;n++)this.get(r,n)>t&&(t=this.get(r,n),e[0]=r,e[1]=n);return e}min(t){if(this.isEmpty())return NaN;switch(t){case"row":{const t=new Array(this.rows).fill(Number.POSITIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.get(e,r)<t[e]&&(t[e]=this.get(e,r));return t}case"column":{const t=new Array(this.columns).fill(Number.POSITIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.get(e,r)<t[r]&&(t[r]=this.get(e,r));return t}case void 0:{let t=this.get(0,0);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.get(e,r)<t&&(t=this.get(e,r));return t}default:throw new Error(`invalid option: ${t}`)}}minIndex(){ir(this);let t=this.get(0,0),e=[0,0];for(let r=0;r<this.rows;r++)for(let n=0;n<this.columns;n++)this.get(r,n)<t&&(t=this.get(r,n),e[0]=r,e[1]=n);return e}maxRow(t){if(Xe(this,t),this.isEmpty())return NaN;let e=this.get(t,0);for(let r=1;r<this.columns;r++)this.get(t,r)>e&&(e=this.get(t,r));return e}maxRowIndex(t){Xe(this,t),ir(this);let e=this.get(t,0),r=[t,0];for(let n=1;n<this.columns;n++)this.get(t,n)>e&&(e=this.get(t,n),r[1]=n);return r}minRow(t){if(Xe(this,t),this.isEmpty())return NaN;let e=this.get(t,0);for(let r=1;r<this.columns;r++)this.get(t,r)<e&&(e=this.get(t,r));return e}minRowIndex(t){Xe(this,t),ir(this);let e=this.get(t,0),r=[t,0];for(let n=1;n<this.columns;n++)this.get(t,n)<e&&(e=this.get(t,n),r[1]=n);return r}maxColumn(t){if(He(this,t),this.isEmpty())return NaN;let e=this.get(0,t);for(let r=1;r<this.rows;r++)this.get(r,t)>e&&(e=this.get(r,t));return e}maxColumnIndex(t){He(this,t),ir(this);let e=this.get(0,t),r=[0,t];for(let n=1;n<this.rows;n++)this.get(n,t)>e&&(e=this.get(n,t),r[0]=n);return r}minColumn(t){if(He(this,t),this.isEmpty())return NaN;let e=this.get(0,t);for(let r=1;r<this.rows;r++)this.get(r,t)<e&&(e=this.get(r,t));return e}minColumnIndex(t){He(this,t),ir(this);let e=this.get(0,t),r=[0,t];for(let n=1;n<this.rows;n++)this.get(n,t)<e&&(e=this.get(n,t),r[0]=n);return r}diag(){let t=Math.min(this.rows,this.columns),e=[];for(let r=0;r<t;r++)e.push(this.get(r,r));return e}norm(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"frobenius";switch(t){case"max":return this.max();case"frobenius":return Math.sqrt(this.dot(this));default:throw new RangeError(`unknown norm type: ${t}`)}}cumulativeSum(){let t=0;for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)t+=this.get(e,r),this.set(e,r,t);return this}dot(t){ur.isMatrix(t)&&(t=t.to1DArray());let e=this.to1DArray();if(e.length!==t.length)throw new RangeError("vectors do not have the same size");let r=0;for(let n=0;n<e.length;n++)r+=e[n]*t[n];return r}mmul(t){t=hr.checkMatrix(t);let e=this.rows,r=this.columns,n=t.columns,o=new hr(e,n),s=new Float64Array(r);for(let i=0;i<n;i++){for(let e=0;e<r;e++)s[e]=t.get(e,i);for(let t=0;t<e;t++){let e=0;for(let n=0;n<r;n++)e+=this.get(t,n)*s[n];o.set(t,i,e)}}return o}mpow(t){if(!this.isSquare())throw new RangeError("Matrix must be square");if(!Number.isInteger(t)||t<0)throw new RangeError("Exponent must be a non-negative integer");let e=hr.eye(this.rows),r=this;for(let n=t;n>=1;n/=2)0!=(1&n)&&(e=e.mmul(r)),r=r.mmul(r);return e}strassen2x2(t){t=hr.checkMatrix(t);let e=new hr(2,2);const r=this.get(0,0),n=t.get(0,0),o=this.get(0,1),s=t.get(0,1),i=this.get(1,0),u=t.get(1,0),l=this.get(1,1),a=t.get(1,1),h=(r+l)*(n+a),c=(i+l)*n,f=r*(s-a),m=l*(u-n),g=(r+o)*a,w=h+m-g+(o-l)*(u+a),d=f+g,p=c+m,y=h-c+f+(i-r)*(n+s);return e.set(0,0,w),e.set(0,1,d),e.set(1,0,p),e.set(1,1,y),e}strassen3x3(t){t=hr.checkMatrix(t);let e=new hr(3,3);const r=this.get(0,0),n=this.get(0,1),o=this.get(0,2),s=this.get(1,0),i=this.get(1,1),u=this.get(1,2),l=this.get(2,0),a=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),w=t.get(1,1),d=t.get(1,2),p=t.get(2,0),y=t.get(2,1),b=t.get(2,2),v=(r-s)*(-f+w),M=(-r+s+i)*(c-f+w),x=(s+i)*(-c+f),E=r*c,S=(-r+l+a)*(c-m+d),_=(-r+l)*(m-d),j=(l+a)*(-c+m),R=(-o+a+h)*(w+p-y),A=(o-h)*(w-y),I=o*p,C=(a+h)*(-p+y),k=(-o+i+u)*(d+p-b),N=(o-u)*(d-b),T=(i+u)*(-p+b),F=E+I+n*g,O=(r+n+o-s-i-a-h)*w+M+x+E+R+I+C,V=E+S+j+(r+n+o-i-u-l-a)*d+I+k+T,z=v+i*(-c+f+g-w-d-p+b)+M+E+I+k+N,D=v+M+x+E+u*y,P=I+k+N+T+s*m,q=E+S+_+a*(-c+m+g-w-d-p+y)+R+A+I,$=R+A+I+C+l*f,L=E+S+_+j+h*b;return e.set(0,0,F),e.set(0,1,O),e.set(0,2,V),e.set(1,0,z),e.set(1,1,D),e.set(1,2,P),e.set(2,0,q),e.set(2,1,$),e.set(2,2,L),e}mmulStrassen(t){t=hr.checkMatrix(t);let e=this.clone(),r=e.rows,n=e.columns,o=t.rows,s=t.columns;function i(t,e,r){let n=t.rows,o=t.columns;if(n===e&&o===r)return t;{let n=ur.zeros(e,r);return n=n.setSubMatrix(t,0,0),n}}n!==o&&console.warn(`Multiplying ${r} x ${n} and ${o} x ${s} matrix: dimensions do not match.`);let u=Math.max(r,o),l=Math.max(n,s);return e=i(e,u,l),function t(e,r,n,o){if(n<=512||o<=512)return e.mmul(r);n%2==1&&o%2==1?(e=i(e,n+1,o+1),r=i(r,n+1,o+1)):n%2==1?(e=i(e,n+1,o),r=i(r,n+1,o)):o%2==1&&(e=i(e,n,o+1),r=i(r,n,o+1));let s=parseInt(e.rows/2,10),u=parseInt(e.columns/2,10),l=e.subMatrix(0,s-1,0,u-1),a=r.subMatrix(0,s-1,0,u-1),h=e.subMatrix(0,s-1,u,e.columns-1),c=r.subMatrix(0,s-1,u,r.columns-1),f=e.subMatrix(s,e.rows-1,0,u-1),m=r.subMatrix(s,r.rows-1,0,u-1),g=e.subMatrix(s,e.rows-1,u,e.columns-1),w=r.subMatrix(s,r.rows-1,u,r.columns-1),d=t(ur.add(l,g),ur.add(a,w),s,u),p=t(ur.add(f,g),a,s,u),y=t(l,ur.sub(c,w),s,u),b=t(g,ur.sub(m,a),s,u),v=t(ur.add(l,h),w,s,u),M=t(ur.sub(f,l),ur.add(a,c),s,u),x=t(ur.sub(h,g),ur.add(m,w),s,u),E=ur.add(d,b);E.sub(v),E.add(x);let S=ur.add(y,v),_=ur.add(p,b),j=ur.sub(d,p);j.add(y),j.add(M);let R=ur.zeros(2*E.rows,2*E.columns);return R=R.setSubMatrix(E,0,0),R=R.setSubMatrix(S,E.rows,0),R=R.setSubMatrix(_,0,E.columns),R=R.setSubMatrix(j,E.rows,E.columns),R.subMatrix(0,n-1,0,o-1)}(e,t=i(t,u,l),u,l)}scaleRows(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};if("object"!=typeof t)throw new TypeError("options must be an object");const{min:e=0,max:r=1}=t;if(!Number.isFinite(e))throw new TypeError("min must be a number");if(!Number.isFinite(r))throw new TypeError("max must be a number");if(e>=r)throw new RangeError("min must be smaller than max");let n=new hr(this.rows,this.columns);for(let t=0;t<this.rows;t++){const o=this.getRow(t);o.length>0&&Ge(o,{min:e,max:r,output:o}),n.setRow(t,o)}return n}scaleColumns(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};if("object"!=typeof t)throw new TypeError("options must be an object");const{min:e=0,max:r=1}=t;if(!Number.isFinite(e))throw new TypeError("min must be a number");if(!Number.isFinite(r))throw new TypeError("max must be a number");if(e>=r)throw new RangeError("min must be smaller than max");let n=new hr(this.rows,this.columns);for(let t=0;t<this.columns;t++){const o=this.getColumn(t);o.length&&Ge(o,{min:e,max:r,output:o}),n.setColumn(t,o)}return n}flipRows(){const t=Math.ceil(this.columns/2);for(let e=0;e<this.rows;e++)for(let r=0;r<t;r++){let t=this.get(e,r),n=this.get(e,this.columns-1-r);this.set(e,r,n),this.set(e,this.columns-1-r,t)}return this}flipColumns(){const t=Math.ceil(this.rows/2);for(let e=0;e<this.columns;e++)for(let r=0;r<t;r++){let t=this.get(r,e),n=this.get(this.rows-1-r,e);this.set(r,e,n),this.set(this.rows-1-r,e,t)}return this}kroneckerProduct(t){t=hr.checkMatrix(t);let e=this.rows,r=this.columns,n=t.rows,o=t.columns,s=new hr(e*n,r*o);for(let i=0;i<e;i++)for(let e=0;e<r;e++)for(let r=0;r<n;r++)for(let u=0;u<o;u++)s.set(n*i+r,o*e+u,this.get(i,e)*t.get(r,u));return s}kroneckerSum(t){if(t=hr.checkMatrix(t),!this.isSquare()||!t.isSquare())throw new Error("Kronecker Sum needs two Square Matrices");let e=this.rows,r=t.rows,n=this.kroneckerProduct(hr.eye(r,r)),o=hr.eye(e,e).kroneckerProduct(t);return n.add(o)}transpose(){let t=new hr(this.columns,this.rows);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)t.set(r,e,this.get(e,r));return t}sortRows(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:lr;for(let e=0;e<this.rows;e++)this.setRow(e,this.getRow(e).sort(t));return this}sortColumns(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:lr;for(let e=0;e<this.columns;e++)this.setColumn(e,this.getColumn(e).sort(t));return this}subMatrix(t,e,r,n){nr(this,t,e,r,n);let o=new hr(e-t+1,n-r+1);for(let s=t;s<=e;s++)for(let e=r;e<=n;e++)o.set(s-t,e-r,this.get(s,e));return o}subMatrixRow(t,e,r){if(void 0===e&&(e=0),void 0===r&&(r=this.columns-1),e>r||e<0||e>=this.columns||r<0||r>=this.columns)throw new RangeError("Argument out of range");let n=new hr(t.length,r-e+1);for(let o=0;o<t.length;o++)for(let s=e;s<=r;s++){if(t[o]<0||t[o]>=this.rows)throw new RangeError(`Row index out of range: ${t[o]}`);n.set(o,s-e,this.get(t[o],s))}return n}subMatrixColumn(t,e,r){if(void 0===e&&(e=0),void 0===r&&(r=this.rows-1),e>r||e<0||e>=this.rows||r<0||r>=this.rows)throw new RangeError("Argument out of range");let n=new hr(r-e+1,t.length);for(let o=0;o<t.length;o++)for(let s=e;s<=r;s++){if(t[o]<0||t[o]>=this.columns)throw new RangeError(`Column index out of range: ${t[o]}`);n.set(s-e,o,this.get(s,t[o]))}return n}setSubMatrix(t,e,r){if((t=hr.checkMatrix(t)).isEmpty())return this;nr(this,e,e+t.rows-1,r,r+t.columns-1);for(let n=0;n<t.rows;n++)for(let o=0;o<t.columns;o++)this.set(e+n,r+o,t.get(n,o));return this}selection(t,e){er(this,t),rr(this,e);let r=new hr(t.length,e.length);for(let n=0;n<t.length;n++){let o=t[n];for(let t=0;t<e.length;t++){let s=e[t];r.set(n,t,this.get(o,s))}}return r}trace(){let t=Math.min(this.rows,this.columns),e=0;for(let r=0;r<t;r++)e+=this.get(r,r);return e}clone(){return this.constructor.copy(this,new hr(this.rows,this.columns))}static copy(t,e){for(const[r,n,o]of t.entries())e.set(r,n,o);return e}sum(t){switch(t){case"row":return function(t){let e=or(t.rows);for(let r=0;r<t.rows;++r)for(let n=0;n<t.columns;++n)e[r]+=t.get(r,n);return e}(this);case"column":return function(t){let e=or(t.columns);for(let r=0;r<t.rows;++r)for(let n=0;n<t.columns;++n)e[n]+=t.get(r,n);return e}(this);case void 0:return function(t){let e=0;for(let r=0;r<t.rows;r++)for(let n=0;n<t.columns;n++)e+=t.get(r,n);return e}(this);default:throw new Error(`invalid option: ${t}`)}}product(t){switch(t){case"row":return function(t){let e=or(t.rows,1);for(let r=0;r<t.rows;++r)for(let n=0;n<t.columns;++n)e[r]*=t.get(r,n);return e}(this);case"column":return function(t){let e=or(t.columns,1);for(let r=0;r<t.rows;++r)for(let n=0;n<t.columns;++n)e[n]*=t.get(r,n);return e}(this);case void 0:return function(t){let e=1;for(let r=0;r<t.rows;r++)for(let n=0;n<t.columns;n++)e*=t.get(r,n);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){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if("object"==typeof t&&(e=t,t=void 0),"object"!=typeof e)throw new TypeError("options must be an object");const{unbiased:r=!0,mean:n=this.mean(t)}=e;if("boolean"!=typeof r)throw new TypeError("unbiased must be a boolean");switch(t){case"row":if(!Be(n))throw new TypeError("mean must be an array");return function(t,e,r){const n=t.rows,o=t.columns,s=[];for(let i=0;i<n;i++){let n=0,u=0,l=0;for(let e=0;e<o;e++)l=t.get(i,e)-r[i],n+=l,u+=l*l;e?s.push((u-n*n/o)/(o-1)):s.push((u-n*n/o)/o)}return s}(this,r,n);case"column":if(!Be(n))throw new TypeError("mean must be an array");return function(t,e,r){const n=t.rows,o=t.columns,s=[];for(let i=0;i<o;i++){let o=0,u=0,l=0;for(let e=0;e<n;e++)l=t.get(e,i)-r[i],o+=l,u+=l*l;e?s.push((u-o*o/n)/(n-1)):s.push((u-o*o/n)/n)}return s}(this,r,n);case void 0:if("number"!=typeof n)throw new TypeError("mean must be a number");return function(t,e,r){const n=t.rows,o=t.columns,s=n*o;let i=0,u=0,l=0;for(let e=0;e<n;e++)for(let n=0;n<o;n++)l=t.get(e,n)-r,i+=l,u+=l*l;return e?(u-i*i/s)/(s-1):(u-i*i/s)/s}(this,r,n);default:throw new Error(`invalid option: ${t}`)}}standardDeviation(t,e){"object"==typeof t&&(e=t,t=void 0);const r=this.variance(t,e);if(void 0===t)return Math.sqrt(r);for(let t=0;t<r.length;t++)r[t]=Math.sqrt(r[t]);return r}center(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if("object"==typeof t&&(e=t,t=void 0),"object"!=typeof e)throw new TypeError("options must be an object");const{center:r=this.mean(t)}=e;switch(t){case"row":if(!Be(r))throw new TypeError("center must be an array");return function(t,e){for(let r=0;r<t.rows;r++)for(let n=0;n<t.columns;n++)t.set(r,n,t.get(r,n)-e[r])}(this,r),this;case"column":if(!Be(r))throw new TypeError("center must be an array");return function(t,e){for(let r=0;r<t.rows;r++)for(let n=0;n<t.columns;n++)t.set(r,n,t.get(r,n)-e[n])}(this,r),this;case void 0:if("number"!=typeof r)throw new TypeError("center must be a number");return function(t,e){for(let r=0;r<t.rows;r++)for(let n=0;n<t.columns;n++)t.set(r,n,t.get(r,n)-e)}(this,r),this;default:throw new Error(`invalid option: ${t}`)}}scale(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if("object"==typeof t&&(e=t,t=void 0),"object"!=typeof e)throw new TypeError("options must be an object");let r=e.scale;switch(t){case"row":if(void 0===r)r=function(t){const e=[];for(let r=0;r<t.rows;r++){let n=0;for(let e=0;e<t.columns;e++)n+=t.get(r,e)**2/(t.columns-1);e.push(Math.sqrt(n))}return e}(this);else if(!Be(r))throw new TypeError("scale must be an array");return function(t,e){for(let r=0;r<t.rows;r++)for(let n=0;n<t.columns;n++)t.set(r,n,t.get(r,n)/e[r])}(this,r),this;case"column":if(void 0===r)r=function(t){const e=[];for(let r=0;r<t.columns;r++){let n=0;for(let e=0;e<t.rows;e++)n+=t.get(e,r)**2/(t.rows-1);e.push(Math.sqrt(n))}return e}(this);else if(!Be(r))throw new TypeError("scale must be an array");return function(t,e){for(let r=0;r<t.rows;r++)for(let n=0;n<t.columns;n++)t.set(r,n,t.get(r,n)/e[n])}(this,r),this;case void 0:if(void 0===r)r=function(t){const e=t.size-1;let r=0;for(let n=0;n<t.columns;n++)for(let o=0;o<t.rows;o++)r+=t.get(o,n)**2/e;return Math.sqrt(r)}(this);else if("number"!=typeof r)throw new TypeError("scale must be a number");return function(t,e){for(let r=0;r<t.rows;r++)for(let n=0;n<t.columns;n++)t.set(r,n,t.get(r,n)/e)}(this,r),this;default:throw new Error(`invalid option: ${t}`)}}toString(t){return Qe(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 lr(t,e){return t-e}ur.prototype.klass="Matrix","undefined"!=typeof Symbol&&(ur.prototype[Symbol.for("nodejs.util.inspect.custom")]=function(){return Qe(this)}),ur.random=ur.rand,ur.randomInt=ur.randInt,ur.diagonal=ur.diag,ur.prototype.diagonal=ur.prototype.diag,ur.identity=ur.eye,ur.prototype.negate=ur.prototype.neg,ur.prototype.tensorProduct=ur.prototype.kroneckerProduct;var ar=new WeakSet;let hr=class t extends ur{constructor(e,r){var n,o;if(super(),$e(n=this,o=ar),o.add(n),De(this,"data",void 0),t.isMatrix(e))Le(this,ar,cr).call(this,e.rows,e.columns),t.copy(e,this);else if(Number.isInteger(e)&&e>=0)Le(this,ar,cr).call(this,e,r);else{if(!Be(e))throw new TypeError("First argument must be a positive number or an array");{const t=e;if("number"!=typeof(r=(e=t.length)?t[0].length:0))throw new TypeError("Data must be a 2D array with at least one element");this.data=[];for(let n=0;n<e;n++){if(t[n].length!==r)throw new RangeError("Inconsistent array dimensions");if(!t[n].every((t=>"number"==typeof t)))throw new TypeError("Input data contains non-numeric values");this.data.push(Float64Array.from(t[n]))}this.rows=e,this.columns=r}}}set(t,e,r){return this.data[t][e]=r,this}get(t,e){return this.data[t][e]}removeRow(t){return Xe(this,t),this.data.splice(t,1),this.rows-=1,this}addRow(t,e){return void 0===e&&(e=t,t=this.rows),Xe(this,t,!0),e=Float64Array.from(Ze(this,e)),this.data.splice(t,0,e),this.rows+=1,this}removeColumn(t){He(this,t);for(let e=0;e<this.rows;e++){const r=new Float64Array(this.columns-1);for(let n=0;n<t;n++)r[n]=this.data[e][n];for(let n=t+1;n<this.columns;n++)r[n-1]=this.data[e][n];this.data[e]=r}return this.columns-=1,this}addColumn(t,e){void 0===e&&(e=t,t=this.columns),He(this,t,!0),e=tr(this,e);for(let r=0;r<this.rows;r++){const n=new Float64Array(this.columns+1);let o=0;for(;o<t;o++)n[o]=this.data[r][o];for(n[o++]=e[r];o<this.columns+1;o++)n[o]=this.data[r][o-1];this.data[r]=n}return this.columns+=1,this}};function cr(t,e){if(this.data=[],!(Number.isInteger(e)&&e>=0))throw new TypeError("nColumns must be a positive integer");for(let r=0;r<t;r++)this.data.push(new Float64Array(e));this.rows=t,this.columns=e}!function(t,e){t.prototype.add=function(t){return"number"==typeof t?this.addS(t):this.addM(t)},t.prototype.addS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)+t);return this},t.prototype.addM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)+t.get(e,r));return this},t.add=function(t,r){return new e(t).add(r)},t.prototype.sub=function(t){return"number"==typeof t?this.subS(t):this.subM(t)},t.prototype.subS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)-t);return this},t.prototype.subM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)-t.get(e,r));return this},t.sub=function(t,r){return new e(t).sub(r)},t.prototype.subtract=t.prototype.sub,t.prototype.subtractS=t.prototype.subS,t.prototype.subtractM=t.prototype.subM,t.subtract=t.sub,t.prototype.mul=function(t){return"number"==typeof t?this.mulS(t):this.mulM(t)},t.prototype.mulS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)*t);return this},t.prototype.mulM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)*t.get(e,r));return this},t.mul=function(t,r){return new e(t).mul(r)},t.prototype.multiply=t.prototype.mul,t.prototype.multiplyS=t.prototype.mulS,t.prototype.multiplyM=t.prototype.mulM,t.multiply=t.mul,t.prototype.div=function(t){return"number"==typeof t?this.divS(t):this.divM(t)},t.prototype.divS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)/t);return this},t.prototype.divM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)/t.get(e,r));return this},t.div=function(t,r){return new e(t).div(r)},t.prototype.divide=t.prototype.div,t.prototype.divideS=t.prototype.divS,t.prototype.divideM=t.prototype.divM,t.divide=t.div,t.prototype.mod=function(t){return"number"==typeof t?this.modS(t):this.modM(t)},t.prototype.modS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)%t);return this},t.prototype.modM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)%t.get(e,r));return this},t.mod=function(t,r){return new e(t).mod(r)},t.prototype.modulus=t.prototype.mod,t.prototype.modulusS=t.prototype.modS,t.prototype.modulusM=t.prototype.modM,t.modulus=t.mod,t.prototype.and=function(t){return"number"==typeof t?this.andS(t):this.andM(t)},t.prototype.andS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)&t);return this},t.prototype.andM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)&t.get(e,r));return this},t.and=function(t,r){return new e(t).and(r)},t.prototype.or=function(t){return"number"==typeof t?this.orS(t):this.orM(t)},t.prototype.orS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)|t);return this},t.prototype.orM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)|t.get(e,r));return this},t.or=function(t,r){return new e(t).or(r)},t.prototype.xor=function(t){return"number"==typeof t?this.xorS(t):this.xorM(t)},t.prototype.xorS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)^t);return this},t.prototype.xorM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)^t.get(e,r));return this},t.xor=function(t,r){return new e(t).xor(r)},t.prototype.leftShift=function(t){return"number"==typeof t?this.leftShiftS(t):this.leftShiftM(t)},t.prototype.leftShiftS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)<<t);return this},t.prototype.leftShiftM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)<<t.get(e,r));return this},t.leftShift=function(t,r){return new e(t).leftShift(r)},t.prototype.signPropagatingRightShift=function(t){return"number"==typeof t?this.signPropagatingRightShiftS(t):this.signPropagatingRightShiftM(t)},t.prototype.signPropagatingRightShiftS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)>>t);return this},t.prototype.signPropagatingRightShiftM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)>>t.get(e,r));return this},t.signPropagatingRightShift=function(t,r){return new e(t).signPropagatingRightShift(r)},t.prototype.rightShift=function(t){return"number"==typeof t?this.rightShiftS(t):this.rightShiftM(t)},t.prototype.rightShiftS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)>>>t);return this},t.prototype.rightShiftM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)>>>t.get(e,r));return this},t.rightShift=function(t,r){return new e(t).rightShift(r)},t.prototype.zeroFillRightShift=t.prototype.rightShift,t.prototype.zeroFillRightShiftS=t.prototype.rightShiftS,t.prototype.zeroFillRightShiftM=t.prototype.rightShiftM,t.zeroFillRightShift=t.rightShift,t.prototype.not=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,~this.get(t,e));return this},t.not=function(t){return new e(t).not()},t.prototype.abs=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.abs(this.get(t,e)));return this},t.abs=function(t){return new e(t).abs()},t.prototype.acos=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.acos(this.get(t,e)));return this},t.acos=function(t){return new e(t).acos()},t.prototype.acosh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.acosh(this.get(t,e)));return this},t.acosh=function(t){return new e(t).acosh()},t.prototype.asin=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.asin(this.get(t,e)));return this},t.asin=function(t){return new e(t).asin()},t.prototype.asinh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.asinh(this.get(t,e)));return this},t.asinh=function(t){return new e(t).asinh()},t.prototype.atan=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.atan(this.get(t,e)));return this},t.atan=function(t){return new e(t).atan()},t.prototype.atanh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.atanh(this.get(t,e)));return this},t.atanh=function(t){return new e(t).atanh()},t.prototype.cbrt=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.cbrt(this.get(t,e)));return this},t.cbrt=function(t){return new e(t).cbrt()},t.prototype.ceil=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.ceil(this.get(t,e)));return this},t.ceil=function(t){return new e(t).ceil()},t.prototype.clz32=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.clz32(this.get(t,e)));return this},t.clz32=function(t){return new e(t).clz32()},t.prototype.cos=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.cos(this.get(t,e)));return this},t.cos=function(t){return new e(t).cos()},t.prototype.cosh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.cosh(this.get(t,e)));return this},t.cosh=function(t){return new e(t).cosh()},t.prototype.exp=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.exp(this.get(t,e)));return this},t.exp=function(t){return new e(t).exp()},t.prototype.expm1=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.expm1(this.get(t,e)));return this},t.expm1=function(t){return new e(t).expm1()},t.prototype.floor=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.floor(this.get(t,e)));return this},t.floor=function(t){return new e(t).floor()},t.prototype.fround=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.fround(this.get(t,e)));return this},t.fround=function(t){return new e(t).fround()},t.prototype.log=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.log(this.get(t,e)));return this},t.log=function(t){return new e(t).log()},t.prototype.log1p=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.log1p(this.get(t,e)));return this},t.log1p=function(t){return new e(t).log1p()},t.prototype.log10=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.log10(this.get(t,e)));return this},t.log10=function(t){return new e(t).log10()},t.prototype.log2=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.log2(this.get(t,e)));return this},t.log2=function(t){return new e(t).log2()},t.prototype.round=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.round(this.get(t,e)));return this},t.round=function(t){return new e(t).round()},t.prototype.sign=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.sign(this.get(t,e)));return this},t.sign=function(t){return new e(t).sign()},t.prototype.sin=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.sin(this.get(t,e)));return this},t.sin=function(t){return new e(t).sin()},t.prototype.sinh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.sinh(this.get(t,e)));return this},t.sinh=function(t){return new e(t).sinh()},t.prototype.sqrt=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.sqrt(this.get(t,e)));return this},t.sqrt=function(t){return new e(t).sqrt()},t.prototype.tan=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.tan(this.get(t,e)));return this},t.tan=function(t){return new e(t).tan()},t.prototype.tanh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.tanh(this.get(t,e)));return this},t.tanh=function(t){return new e(t).tanh()},t.prototype.trunc=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.trunc(this.get(t,e)));return this},t.trunc=function(t){return new e(t).trunc()},t.pow=function(t,r){return new e(t).pow(r)},t.prototype.pow=function(t){return"number"==typeof t?this.powS(t):this.powM(t)},t.prototype.powS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)**t);return this},t.prototype.powM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)**t.get(e,r));return this}}(ur,hr);var fr=new WeakMap;class mr extends ur{get size(){return Oe(this,fr).size}get rows(){return Oe(this,fr).rows}get columns(){return Oe(this,fr).columns}get diagonalSize(){return this.rows}static isSymmetricMatrix(t){return hr.isMatrix(t)&&"SymmetricMatrix"===t.klassType}static zeros(t){return new this(t)}static ones(t){return new this(t).fill(1)}constructor(t){var e,r,n;if(super(),n={writable:!0,value:void 0},$e(e=this,r=fr),r.set(e,n),hr.isMatrix(t)){if(!t.isSymmetric())throw new TypeError("not symmetric data");Fe(this,fr,hr.copy(t,new hr(t.rows,t.rows)))}else if(Number.isInteger(t)&&t>=0)Fe(this,fr,new hr(t,t));else if(Fe(this,fr,new hr(t)),!this.isSymmetric())throw new TypeError("not symmetric data")}clone(){const t=new mr(this.diagonalSize);for(const[e,r,n]of this.upperRightEntries())t.set(e,r,n);return t}toMatrix(){return new hr(this)}get(t,e){return Oe(this,fr).get(t,e)}set(t,e,r){return Oe(this,fr).set(t,e,r),Oe(this,fr).set(e,t,r),this}removeCross(t){return Oe(this,fr).removeRow(t),Oe(this,fr).removeColumn(t),this}addCross(t,e){void 0===e&&(e=t,t=this.diagonalSize);const r=e.slice();return r.splice(t,1),Oe(this,fr).addRow(t,r),Oe(this,fr).addColumn(t,e),this}applyMask(t){if(t.length!==this.diagonalSize)throw new RangeError("Mask size do not match with matrix size");const e=[];for(const[r,n]of t.entries())n||e.push(r);e.reverse();for(const t of e)this.removeCross(t);return this}toCompact(){const{diagonalSize:t}=this,e=new Array(t*(t+1)/2);for(let r=0,n=0,o=0;o<e.length;o++)e[o]=this.get(n,r),++r>=t&&(r=++n);return e}static fromCompact(t){const e=t.length,r=(Math.sqrt(8*e+1)-1)/2;if(!Number.isInteger(r))throw new TypeError(`This array is not a compact representation of a Symmetric Matrix, ${JSON.stringify(t)}`);const n=new mr(r);for(let o=0,s=0,i=0;i<e;i++)n.set(o,s,t[i]),++o>=r&&(o=++s);return n}*upperRightEntries(){for(let t=0,e=0;t<this.diagonalSize;void 0){const r=this.get(t,e);yield[t,e,r],++e>=this.diagonalSize&&(e=++t)}}*upperRightValues(){for(let t=0,e=0;t<this.diagonalSize;void 0){const r=this.get(t,e);yield r,++e>=this.diagonalSize&&(e=++t)}}}mr.prototype.klassType="SymmetricMatrix";class gr extends mr{static isDistanceMatrix(t){return mr.isSymmetricMatrix(t)&&"DistanceMatrix"===t.klassSubType}constructor(t){if(super(t),!this.isDistance())throw new TypeError("Provided arguments do no produce a distance matrix")}set(t,e,r){return t===e&&(r=0),super.set(t,e,r)}addCross(t,e){return void 0===e&&(e=t,t=this.diagonalSize),(e=e.slice())[t]=0,super.addCross(t,e)}toSymmetricMatrix(){return new mr(this)}clone(){const t=new gr(this.diagonalSize);for(const[e,r,n]of this.upperRightEntries())e!==r&&t.set(e,r,n);return t}toCompact(){const{diagonalSize:t}=this,e=new Array((t-1)*t/2);for(let r=1,n=0,o=0;o<e.length;o++)e[o]=this.get(n,r),++r>=t&&(r=1+ ++n);return e}static fromCompact(t){const e=t.length;if(0===e)return new this(0);const r=(Math.sqrt(8*e+1)+1)/2;if(!Number.isInteger(r))throw new TypeError(`This array is not a compact representation of a DistanceMatrix, ${JSON.stringify(t)}`);const n=new this(r);for(let o=1,s=0,i=0;i<e;i++)n.set(o,s,t[i]),++o>=r&&(o=1+ ++s);return n}}gr.prototype.klassSubType="DistanceMatrix";class wr extends ur{constructor(t,e,r){super(),this.matrix=t,this.rows=e,this.columns=r}}class dr extends wr{constructor(t,e,r){er(t,e),rr(t,r),super(t,e.length,r.length),this.rowIndices=e,this.columnIndices=r}set(t,e,r){return this.matrix.set(this.rowIndices[t],this.columnIndices[e],r),this}get(t,e){return this.matrix.get(this.rowIndices[t],this.columnIndices[e])}}class pr extends ur{constructor(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};const{rows:r=1}=e;if(t.length%r!=0)throw new Error("the data length is not divisible by the number of rows");super(),this.rows=r,this.columns=t.length/r,this.data=t}set(t,e,r){let n=this._calculateIndex(t,e);return this.data[n]=r,this}get(t,e){let r=this._calculateIndex(t,e);return this.data[r]}_calculateIndex(t,e){return t*this.columns+e}}class yr extends ur{constructor(t){super(),this.data=t,this.rows=t.length,this.columns=t[0].length}set(t,e,r){return this.data[t][e]=r,this}get(t,e){return this.data[t][e]}}class br{constructor(t){let e,r,n,o,s,i,u,l,a,h=(t=yr.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(l=new Float64Array(c),r=0;r<f;r++){for(e=0;e<c;e++)l[e]=h.get(e,r);for(e=0;e<c;e++){for(a=Math.min(e,r),s=0,n=0;n<a;n++)s+=h.get(e,n)*l[n];l[e]-=s,h.set(e,r,l[e])}for(o=r,e=r+1;e<c;e++)Math.abs(l[e])>Math.abs(l[o])&&(o=e);if(o!==r){for(n=0;n<f;n++)i=h.get(o,n),h.set(o,n,h.get(r,n)),h.set(r,n,i);u=m[o],m[o]=m[r],m[r]=u,g=-g}if(r<c&&0!==h.get(r,r))for(e=r+1;e<c;e++)h.set(e,r,h.get(e,r)/h.get(r,r))}this.LU=h,this.pivotVector=m,this.pivotSign=g}isSingular(){let t=this.LU,e=t.columns;for(let r=0;r<e;r++)if(0===t.get(r,r))return!0;return!1}solve(t){t=hr.checkMatrix(t);let e=this.LU;if(e.rows!==t.rows)throw new Error("Invalid matrix dimensions");if(this.isSingular())throw new Error("LU matrix is singular");let r,n,o,s=t.columns,i=t.subMatrixRow(this.pivotVector,0,s-1),u=e.columns;for(o=0;o<u;o++)for(r=o+1;r<u;r++)for(n=0;n<s;n++)i.set(r,n,i.get(r,n)-i.get(o,n)*e.get(r,o));for(o=u-1;o>=0;o--){for(n=0;n<s;n++)i.set(o,n,i.get(o,n)/e.get(o,o));for(r=0;r<o;r++)for(n=0;n<s;n++)i.set(r,n,i.get(r,n)-i.get(o,n)*e.get(r,o))}return i}get determinant(){let t=this.LU;if(!t.isSquare())throw new Error("Matrix must be square");let e=this.pivotSign,r=t.columns;for(let n=0;n<r;n++)e*=t.get(n,n);return e}get lowerTriangularMatrix(){let t=this.LU,e=t.rows,r=t.columns,n=new hr(e,r);for(let o=0;o<e;o++)for(let e=0;e<r;e++)o>e?n.set(o,e,t.get(o,e)):o===e?n.set(o,e,1):n.set(o,e,0);return n}get upperTriangularMatrix(){let t=this.LU,e=t.rows,r=t.columns,n=new hr(e,r);for(let o=0;o<e;o++)for(let e=0;e<r;e++)o<=e?n.set(o,e,t.get(o,e)):n.set(o,e,0);return n}get pivotPermutationVector(){return Array.from(this.pivotVector)}}function vr(t,e){let r=0;return Math.abs(t)>Math.abs(e)?(r=e/t,Math.abs(t)*Math.sqrt(1+r*r)):0!==e?(r=t/e,Math.abs(e)*Math.sqrt(1+r*r)):0}class Mr{constructor(t){let e,r,n,o,s=(t=yr.checkMatrix(t)).clone(),i=t.rows,u=t.columns,l=new Float64Array(u);for(n=0;n<u;n++){let t=0;for(e=n;e<i;e++)t=vr(t,s.get(e,n));if(0!==t){for(s.get(n,n)<0&&(t=-t),e=n;e<i;e++)s.set(e,n,s.get(e,n)/t);for(s.set(n,n,s.get(n,n)+1),r=n+1;r<u;r++){for(o=0,e=n;e<i;e++)o+=s.get(e,n)*s.get(e,r);for(o=-o/s.get(n,n),e=n;e<i;e++)s.set(e,r,s.get(e,r)+o*s.get(e,n))}}l[n]=-t}this.QR=s,this.Rdiag=l}solve(t){t=hr.checkMatrix(t);let e=this.QR,r=e.rows;if(t.rows!==r)throw new Error("Matrix row dimensions must agree");if(!this.isFullRank())throw new Error("Matrix is rank deficient");let n,o,s,i,u=t.columns,l=t.clone(),a=e.columns;for(s=0;s<a;s++)for(o=0;o<u;o++){for(i=0,n=s;n<r;n++)i+=e.get(n,s)*l.get(n,o);for(i=-i/e.get(s,s),n=s;n<r;n++)l.set(n,o,l.get(n,o)+i*e.get(n,s))}for(s=a-1;s>=0;s--){for(o=0;o<u;o++)l.set(s,o,l.get(s,o)/this.Rdiag[s]);for(n=0;n<s;n++)for(o=0;o<u;o++)l.set(n,o,l.get(n,o)-l.get(s,o)*e.get(n,s))}return l.subMatrix(0,a-1,0,u-1)}isFullRank(){let t=this.QR.columns;for(let e=0;e<t;e++)if(0===this.Rdiag[e])return!1;return!0}get upperTriangularMatrix(){let t,e,r=this.QR,n=r.columns,o=new hr(n,n);for(t=0;t<n;t++)for(e=0;e<n;e++)t<e?o.set(t,e,r.get(t,e)):t===e?o.set(t,e,this.Rdiag[t]):o.set(t,e,0);return o}get orthogonalMatrix(){let t,e,r,n,o=this.QR,s=o.rows,i=o.columns,u=new hr(s,i);for(r=i-1;r>=0;r--){for(t=0;t<s;t++)u.set(t,r,0);for(u.set(r,r,1),e=r;e<i;e++)if(0!==o.get(r,r)){for(n=0,t=r;t<s;t++)n+=o.get(t,r)*u.get(t,e);for(n=-n/o.get(r,r),t=r;t<s;t++)u.set(t,e,u.get(t,e)+n*o.get(t,r))}}return u}}class xr{constructor(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if((t=yr.checkMatrix(t)).isEmpty())throw new Error("Matrix must be non-empty");let r=t.rows,n=t.columns;const{computeLeftSingularVectors:o=!0,computeRightSingularVectors:s=!0,autoTranspose:i=!1}=e;let u,l=Boolean(o),a=Boolean(s),h=!1;if(r<n)if(i){u=t.transpose(),r=u.rows,n=u.columns,h=!0;let e=l;l=a,a=e}else u=t.clone(),console.warn("Computing SVD on a matrix with more columns than rows. Consider enabling autoTranspose");else u=t.clone();let c=Math.min(r,n),f=Math.min(r+1,n),m=new Float64Array(f),g=new hr(r,c),w=new hr(n,n),d=new Float64Array(n),p=new Float64Array(r),y=new Float64Array(f);for(let t=0;t<f;t++)y[t]=t;let b=Math.min(r-1,n),v=Math.max(0,Math.min(n-2,r)),M=Math.max(b,v);for(let t=0;t<M;t++){if(t<b){m[t]=0;for(let e=t;e<r;e++)m[t]=vr(m[t],u.get(e,t));if(0!==m[t]){u.get(t,t)<0&&(m[t]=-m[t]);for(let e=t;e<r;e++)u.set(e,t,u.get(e,t)/m[t]);u.set(t,t,u.get(t,t)+1)}m[t]=-m[t]}for(let e=t+1;e<n;e++){if(t<b&&0!==m[t]){let n=0;for(let o=t;o<r;o++)n+=u.get(o,t)*u.get(o,e);n=-n/u.get(t,t);for(let o=t;o<r;o++)u.set(o,e,u.get(o,e)+n*u.get(o,t))}d[e]=u.get(t,e)}if(l&&t<b)for(let e=t;e<r;e++)g.set(e,t,u.get(e,t));if(t<v){d[t]=0;for(let e=t+1;e<n;e++)d[t]=vr(d[t],d[e]);if(0!==d[t]){d[t+1]<0&&(d[t]=0-d[t]);for(let e=t+1;e<n;e++)d[e]/=d[t];d[t+1]+=1}if(d[t]=-d[t],t+1<r&&0!==d[t]){for(let e=t+1;e<r;e++)p[e]=0;for(let e=t+1;e<r;e++)for(let r=t+1;r<n;r++)p[e]+=d[r]*u.get(e,r);for(let e=t+1;e<n;e++){let n=-d[e]/d[t+1];for(let o=t+1;o<r;o++)u.set(o,e,u.get(o,e)+n*p[o])}}if(a)for(let e=t+1;e<n;e++)w.set(e,t,d[e])}}let x=Math.min(n,r+1);if(b<n&&(m[b]=u.get(b,b)),r<x&&(m[x-1]=0),v+1<x&&(d[v]=u.get(v,x-1)),d[x-1]=0,l){for(let t=b;t<c;t++){for(let e=0;e<r;e++)g.set(e,t,0);g.set(t,t,1)}for(let t=b-1;t>=0;t--)if(0!==m[t]){for(let e=t+1;e<c;e++){let n=0;for(let o=t;o<r;o++)n+=g.get(o,t)*g.get(o,e);n=-n/g.get(t,t);for(let o=t;o<r;o++)g.set(o,e,g.get(o,e)+n*g.get(o,t))}for(let e=t;e<r;e++)g.set(e,t,-g.get(e,t));g.set(t,t,1+g.get(t,t));for(let e=0;e<t-1;e++)g.set(e,t,0)}else{for(let e=0;e<r;e++)g.set(e,t,0);g.set(t,t,1)}}if(a)for(let t=n-1;t>=0;t--){if(t<v&&0!==d[t])for(let e=t+1;e<n;e++){let r=0;for(let o=t+1;o<n;o++)r+=w.get(o,t)*w.get(o,e);r=-r/w.get(t+1,t);for(let o=t+1;o<n;o++)w.set(o,e,w.get(o,e)+r*w.get(o,t))}for(let e=0;e<n;e++)w.set(e,t,0);w.set(t,t,1)}let E=x-1,S=Number.EPSILON;for(;x>0;){let t,e;for(t=x-2;t>=-1&&-1!==t;t--){const e=Number.MIN_VALUE+S*Math.abs(m[t]+Math.abs(m[t+1]));if(Math.abs(d[t])<=e||Number.isNaN(d[t])){d[t]=0;break}}if(t===x-2)e=4;else{let r;for(r=x-1;r>=t&&r!==t;r--){let e=(r!==x?Math.abs(d[r]):0)+(r!==t+1?Math.abs(d[r-1]):0);if(Math.abs(m[r])<=S*e){m[r]=0;break}}r===t?e=3:r===x-1?e=1:(e=2,t=r)}switch(t++,e){case 1:{let e=d[x-2];d[x-2]=0;for(let r=x-2;r>=t;r--){let o=vr(m[r],e),s=m[r]/o,i=e/o;if(m[r]=o,r!==t&&(e=-i*d[r-1],d[r-1]=s*d[r-1]),a)for(let t=0;t<n;t++)o=s*w.get(t,r)+i*w.get(t,x-1),w.set(t,x-1,-i*w.get(t,r)+s*w.get(t,x-1)),w.set(t,r,o)}break}case 2:{let e=d[t-1];d[t-1]=0;for(let n=t;n<x;n++){let o=vr(m[n],e),s=m[n]/o,i=e/o;if(m[n]=o,e=-i*d[n],d[n]=s*d[n],l)for(let e=0;e<r;e++)o=s*g.get(e,n)+i*g.get(e,t-1),g.set(e,t-1,-i*g.get(e,n)+s*g.get(e,t-1)),g.set(e,n,o)}break}case 3:{const e=Math.max(Math.abs(m[x-1]),Math.abs(m[x-2]),Math.abs(d[x-2]),Math.abs(m[t]),Math.abs(d[t])),o=m[x-1]/e,s=m[x-2]/e,i=d[x-2]/e,u=m[t]/e,h=d[t]/e,c=((s+o)*(s-o)+i*i)/2,f=o*i*(o*i);let p=0;0===c&&0===f||(p=c<0?0-Math.sqrt(c*c+f):Math.sqrt(c*c+f),p=f/(c+p));let y=(u+o)*(u-o)+p,b=u*h;for(let e=t;e<x-1;e++){let o=vr(y,b);0===o&&(o=Number.MIN_VALUE);let s=y/o,i=b/o;if(e!==t&&(d[e-1]=o),y=s*m[e]+i*d[e],d[e]=s*d[e]-i*m[e],b=i*m[e+1],m[e+1]=s*m[e+1],a)for(let t=0;t<n;t++)o=s*w.get(t,e)+i*w.get(t,e+1),w.set(t,e+1,-i*w.get(t,e)+s*w.get(t,e+1)),w.set(t,e,o);if(o=vr(y,b),0===o&&(o=Number.MIN_VALUE),s=y/o,i=b/o,m[e]=o,y=s*d[e]+i*m[e+1],m[e+1]=-i*d[e]+s*m[e+1],b=i*d[e+1],d[e+1]=s*d[e+1],l&&e<r-1)for(let t=0;t<r;t++)o=s*g.get(t,e)+i*g.get(t,e+1),g.set(t,e+1,-i*g.get(t,e)+s*g.get(t,e+1)),g.set(t,e,o)}d[x-2]=y;break}case 4:if(m[t]<=0&&(m[t]=m[t]<0?-m[t]:0,a))for(let e=0;e<=E;e++)w.set(e,t,-w.get(e,t));for(;t<E&&!(m[t]>=m[t+1]);){let e=m[t];if(m[t]=m[t+1],m[t+1]=e,a&&t<n-1)for(let r=0;r<n;r++)e=w.get(r,t+1),w.set(r,t+1,w.get(r,t)),w.set(r,t,e);if(l&&t<r-1)for(let n=0;n<r;n++)e=g.get(n,t+1),g.set(n,t+1,g.get(n,t)),g.set(n,t,e);t++}x--}}if(h){let t=w;w=g,g=t}this.m=r,this.n=n,this.s=m,this.U=g,this.V=w}solve(t){let e=t,r=this.threshold,n=this.s.length,o=hr.zeros(n,n);for(let t=0;t<n;t++)Math.abs(this.s[t])<=r?o.set(t,t,0):o.set(t,t,1/this.s[t]);let s=this.U,i=this.rightSingularVectors,u=i.mmul(o),l=i.rows,a=s.rows,h=hr.zeros(l,a);for(let t=0;t<l;t++)for(let e=0;e<a;e++){let r=0;for(let o=0;o<n;o++)r+=u.get(t,o)*s.get(e,o);h.set(t,e,r)}return h.mmul(e)}solveForDiagonal(t){return this.solve(hr.diag(t))}inverse(){let t=this.V,e=this.threshold,r=t.rows,n=t.columns,o=new hr(r,this.s.length);for(let s=0;s<r;s++)for(let r=0;r<n;r++)Math.abs(this.s[r])>e&&o.set(s,r,t.get(s,r)/this.s[r]);let s=this.U,i=s.rows,u=s.columns,l=new hr(r,i);for(let t=0;t<r;t++)for(let e=0;e<i;e++){let r=0;for(let n=0;n<u;n++)r+=o.get(t,n)*s.get(e,n);l.set(t,e,r)}return l}get condition(){return this.s[0]/this.s[Math.min(this.m,this.n)-1]}get norm2(){return this.s[0]}get rank(){let t=Math.max(this.m,this.n)*this.s[0]*Number.EPSILON,e=0,r=this.s;for(let n=0,o=r.length;n<o;n++)r[n]>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 hr.diag(this.s)}}function Er(t,e){let r=arguments.length>2&&void 0!==arguments[2]&&arguments[2];return t=yr.checkMatrix(t),e=yr.checkMatrix(e),r?new xr(t).solve(e):t.isSquare()?new br(t).solve(e):new Mr(t).solve(e)}function Sr(t,e){let r=[];for(let n=0;n<t;n++)n!==e&&r.push(n);return r}function _r(t,e,r){let n=arguments.length>3&&void 0!==arguments[3]?arguments[3]:1e-9;if(t>(arguments.length>4&&void 0!==arguments[4]?arguments[4]:1e-9))return new Array(e.rows+1).fill(0);{let t=e.addRow(r,[0]);for(let e=0;e<t.rows;e++)Math.abs(t.get(e,0))<n&&t.set(e,0,0);return t.to1DArray()}}class jr{constructor(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};const{assumeSymmetric:r=!1}=e;if(!(t=yr.checkMatrix(t)).isSquare())throw new Error("Matrix is not a square matrix");if(t.isEmpty())throw new Error("Matrix must be non-empty");let n,o,s=t.columns,i=new hr(s,s),u=new Float64Array(s),l=new Float64Array(s),a=t,h=!1;if(h=!!r||t.isSymmetric(),h){for(n=0;n<s;n++)for(o=0;o<s;o++)i.set(n,o,a.get(n,o));!function(t,e,r,n){let o,s,i,u,l,a,h,c;for(l=0;l<t;l++)r[l]=n.get(t-1,l);for(u=t-1;u>0;u--){for(c=0,i=0,a=0;a<u;a++)c+=Math.abs(r[a]);if(0===c)for(e[u]=r[u-1],l=0;l<u;l++)r[l]=n.get(u-1,l),n.set(u,l,0),n.set(l,u,0);else{for(a=0;a<u;a++)r[a]/=c,i+=r[a]*r[a];for(o=r[u-1],s=Math.sqrt(i),o>0&&(s=-s),e[u]=c*s,i-=o*s,r[u-1]=o-s,l=0;l<u;l++)e[l]=0;for(l=0;l<u;l++){for(o=r[l],n.set(l,u,o),s=e[l]+n.get(l,l)*o,a=l+1;a<=u-1;a++)s+=n.get(a,l)*r[a],e[a]+=n.get(a,l)*o;e[l]=s}for(o=0,l=0;l<u;l++)e[l]/=i,o+=e[l]*r[l];for(h=o/(i+i),l=0;l<u;l++)e[l]-=h*r[l];for(l=0;l<u;l++){for(o=r[l],s=e[l],a=l;a<=u-1;a++)n.set(a,l,n.get(a,l)-(o*e[a]+s*r[a]));r[l]=n.get(u-1,l),n.set(u,l,0)}}r[u]=i}for(u=0;u<t-1;u++){if(n.set(t-1,u,n.get(u,u)),n.set(u,u,1),i=r[u+1],0!==i){for(a=0;a<=u;a++)r[a]=n.get(a,u+1)/i;for(l=0;l<=u;l++){for(s=0,a=0;a<=u;a++)s+=n.get(a,u+1)*n.get(a,l);for(a=0;a<=u;a++)n.set(a,l,n.get(a,l)-s*r[a])}}for(a=0;a<=u;a++)n.set(a,u+1,0)}for(l=0;l<t;l++)r[l]=n.get(t-1,l),n.set(t-1,l,0);n.set(t-1,t-1,1),e[0]=0}(s,l,u,i),function(t,e,r,n){let o,s,i,u,l,a,h,c,f,m,g,w,d,p,y,b;for(i=1;i<t;i++)e[i-1]=e[i];e[t-1]=0;let v=0,M=0,x=Number.EPSILON;for(a=0;a<t;a++){for(M=Math.max(M,Math.abs(r[a])+Math.abs(e[a])),h=a;h<t&&!(Math.abs(e[h])<=x*M);)h++;if(h>a)do{for(o=r[a],c=(r[a+1]-o)/(2*e[a]),f=vr(c,1),c<0&&(f=-f),r[a]=e[a]/(c+f),r[a+1]=e[a]*(c+f),m=r[a+1],s=o-r[a],i=a+2;i<t;i++)r[i]-=s;for(v+=s,c=r[h],g=1,w=g,d=g,p=e[a+1],y=0,b=0,i=h-1;i>=a;i--)for(d=w,w=g,b=y,o=g*e[i],s=g*c,f=vr(c,e[i]),e[i+1]=y*f,y=e[i]/f,g=c/f,c=g*r[i]-y*o,r[i+1]=s+y*(g*o+y*r[i]),l=0;l<t;l++)s=n.get(l,i+1),n.set(l,i+1,y*n.get(l,i)+g*s),n.set(l,i,g*n.get(l,i)-y*s);c=-y*b*d*p*e[a]/m,e[a]=y*c,r[a]=g*c}while(Math.abs(e[a])>x*M);r[a]=r[a]+v,e[a]=0}for(i=0;i<t-1;i++){for(l=i,c=r[i],u=i+1;u<t;u++)r[u]<c&&(l=u,c=r[u]);if(l!==i)for(r[l]=r[i],r[i]=c,u=0;u<t;u++)c=n.get(u,i),n.set(u,i,n.get(u,l)),n.set(u,l,c)}}(s,l,u,i)}else{let t=new hr(s,s),e=new Float64Array(s);for(o=0;o<s;o++)for(n=0;n<s;n++)t.set(n,o,a.get(n,o));!function(t,e,r,n){let o,s,i,u,l,a,h,c=0,f=t-1;for(a=c+1;a<=f-1;a++){for(h=0,u=a;u<=f;u++)h+=Math.abs(e.get(u,a-1));if(0!==h){for(i=0,u=f;u>=a;u--)r[u]=e.get(u,a-1)/h,i+=r[u]*r[u];for(s=Math.sqrt(i),r[a]>0&&(s=-s),i-=r[a]*s,r[a]=r[a]-s,l=a;l<t;l++){for(o=0,u=f;u>=a;u--)o+=r[u]*e.get(u,l);for(o/=i,u=a;u<=f;u++)e.set(u,l,e.get(u,l)-o*r[u])}for(u=0;u<=f;u++){for(o=0,l=f;l>=a;l--)o+=r[l]*e.get(u,l);for(o/=i,l=a;l<=f;l++)e.set(u,l,e.get(u,l)-o*r[l])}r[a]=h*r[a],e.set(a,a-1,h*s)}}for(u=0;u<t;u++)for(l=0;l<t;l++)n.set(u,l,u===l?1:0);for(a=f-1;a>=c+1;a--)if(0!==e.get(a,a-1)){for(u=a+1;u<=f;u++)r[u]=e.get(u,a-1);for(l=a;l<=f;l++){for(s=0,u=a;u<=f;u++)s+=r[u]*n.get(u,l);for(s=s/r[a]/e.get(a,a-1),u=a;u<=f;u++)n.set(u,l,n.get(u,l)+s*r[u])}}}(s,t,e,i),function(t,e,r,n,o){let s,i,u,l,a,h,c,f,m,g,w,d,p,y,b,v=t-1,M=0,x=t-1,E=Number.EPSILON,S=0,_=0,j=0,R=0,A=0,I=0,C=0,k=0;for(s=0;s<t;s++)for((s<M||s>x)&&(r[s]=o.get(s,s),e[s]=0),i=Math.max(s-1,0);i<t;i++)_+=Math.abs(o.get(s,i));for(;v>=M;){for(l=v;l>M&&(I=Math.abs(o.get(l-1,l-1))+Math.abs(o.get(l,l)),0===I&&(I=_),!(Math.abs(o.get(l,l-1))<E*I));)l--;if(l===v)o.set(v,v,o.get(v,v)+S),r[v]=o.get(v,v),e[v]=0,v--,k=0;else if(l===v-1){if(c=o.get(v,v-1)*o.get(v-1,v),j=(o.get(v-1,v-1)-o.get(v,v))/2,R=j*j+c,C=Math.sqrt(Math.abs(R)),o.set(v,v,o.get(v,v)+S),o.set(v-1,v-1,o.get(v-1,v-1)+S),f=o.get(v,v),R>=0){for(C=j>=0?j+C:j-C,r[v-1]=f+C,r[v]=r[v-1],0!==C&&(r[v]=f-c/C),e[v-1]=0,e[v]=0,f=o.get(v,v-1),I=Math.abs(f)+Math.abs(C),j=f/I,R=C/I,A=Math.sqrt(j*j+R*R),j/=A,R/=A,i=v-1;i<t;i++)C=o.get(v-1,i),o.set(v-1,i,R*C+j*o.get(v,i)),o.set(v,i,R*o.get(v,i)-j*C);for(s=0;s<=v;s++)C=o.get(s,v-1),o.set(s,v-1,R*C+j*o.get(s,v)),o.set(s,v,R*o.get(s,v)-j*C);for(s=M;s<=x;s++)C=n.get(s,v-1),n.set(s,v-1,R*C+j*n.get(s,v)),n.set(s,v,R*n.get(s,v)-j*C)}else r[v-1]=f+j,r[v]=f+j,e[v-1]=C,e[v]=-C;v-=2,k=0}else{if(f=o.get(v,v),m=0,c=0,l<v&&(m=o.get(v-1,v-1),c=o.get(v,v-1)*o.get(v-1,v)),10===k){for(S+=f,s=M;s<=v;s++)o.set(s,s,o.get(s,s)-f);I=Math.abs(o.get(v,v-1))+Math.abs(o.get(v-1,v-2)),f=m=.75*I,c=-.4375*I*I}if(30===k&&(I=(m-f)/2,I=I*I+c,I>0)){for(I=Math.sqrt(I),m<f&&(I=-I),I=f-c/((m-f)/2+I),s=M;s<=v;s++)o.set(s,s,o.get(s,s)-I);S+=I,f=m=c=.964}for(k+=1,a=v-2;a>=l&&(C=o.get(a,a),A=f-C,I=m-C,j=(A*I-c)/o.get(a+1,a)+o.get(a,a+1),R=o.get(a+1,a+1)-C-A-I,A=o.get(a+2,a+1),I=Math.abs(j)+Math.abs(R)+Math.abs(A),j/=I,R/=I,A/=I,a!==l)&&!(Math.abs(o.get(a,a-1))*(Math.abs(R)+Math.abs(A))<E*(Math.abs(j)*(Math.abs(o.get(a-1,a-1))+Math.abs(C)+Math.abs(o.get(a+1,a+1)))));)a--;for(s=a+2;s<=v;s++)o.set(s,s-2,0),s>a+2&&o.set(s,s-3,0);for(u=a;u<=v-1&&(y=u!==v-1,u!==a&&(j=o.get(u,u-1),R=o.get(u+1,u-1),A=y?o.get(u+2,u-1):0,f=Math.abs(j)+Math.abs(R)+Math.abs(A),0!==f&&(j/=f,R/=f,A/=f)),0!==f);u++)if(I=Math.sqrt(j*j+R*R+A*A),j<0&&(I=-I),0!==I){for(u!==a?o.set(u,u-1,-I*f):l!==a&&o.set(u,u-1,-o.get(u,u-1)),j+=I,f=j/I,m=R/I,C=A/I,R/=j,A/=j,i=u;i<t;i++)j=o.get(u,i)+R*o.get(u+1,i),y&&(j+=A*o.get(u+2,i),o.set(u+2,i,o.get(u+2,i)-j*C)),o.set(u,i,o.get(u,i)-j*f),o.set(u+1,i,o.get(u+1,i)-j*m);for(s=0;s<=Math.min(v,u+3);s++)j=f*o.get(s,u)+m*o.get(s,u+1),y&&(j+=C*o.get(s,u+2),o.set(s,u+2,o.get(s,u+2)-j*A)),o.set(s,u,o.get(s,u)-j),o.set(s,u+1,o.get(s,u+1)-j*R);for(s=M;s<=x;s++)j=f*n.get(s,u)+m*n.get(s,u+1),y&&(j+=C*n.get(s,u+2),n.set(s,u+2,n.get(s,u+2)-j*A)),n.set(s,u,n.get(s,u)-j),n.set(s,u+1,n.get(s,u+1)-j*R)}}}if(0===_)return;for(v=t-1;v>=0;v--)if(j=r[v],R=e[v],0===R)for(l=v,o.set(v,v,1),s=v-1;s>=0;s--){for(c=o.get(s,s)-j,A=0,i=l;i<=v;i++)A+=o.get(s,i)*o.get(i,v);if(e[s]<0)C=c,I=A;else if(l=s,0===e[s]?o.set(s,v,0!==c?-A/c:-A/(E*_)):(f=o.get(s,s+1),m=o.get(s+1,s),R=(r[s]-j)*(r[s]-j)+e[s]*e[s],h=(f*I-C*A)/R,o.set(s,v,h),o.set(s+1,v,Math.abs(f)>Math.abs(C)?(-A-c*h)/f:(-I-m*h)/C)),h=Math.abs(o.get(s,v)),E*h*h>1)for(i=s;i<=v;i++)o.set(i,v,o.get(i,v)/h)}else if(R<0)for(l=v-1,Math.abs(o.get(v,v-1))>Math.abs(o.get(v-1,v))?(o.set(v-1,v-1,R/o.get(v,v-1)),o.set(v-1,v,-(o.get(v,v)-j)/o.get(v,v-1))):(b=Rr(0,-o.get(v-1,v),o.get(v-1,v-1)-j,R),o.set(v-1,v-1,b[0]),o.set(v-1,v,b[1])),o.set(v,v-1,0),o.set(v,v,1),s=v-2;s>=0;s--){for(g=0,w=0,i=l;i<=v;i++)g+=o.get(s,i)*o.get(i,v-1),w+=o.get(s,i)*o.get(i,v);if(c=o.get(s,s)-j,e[s]<0)C=c,A=g,I=w;else if(l=s,0===e[s]?(b=Rr(-g,-w,c,R),o.set(s,v-1,b[0]),o.set(s,v,b[1])):(f=o.get(s,s+1),m=o.get(s+1,s),d=(r[s]-j)*(r[s]-j)+e[s]*e[s]-R*R,p=2*(r[s]-j)*R,0===d&&0===p&&(d=E*_*(Math.abs(c)+Math.abs(R)+Math.abs(f)+Math.abs(m)+Math.abs(C))),b=Rr(f*A-C*g+R*w,f*I-C*w-R*g,d,p),o.set(s,v-1,b[0]),o.set(s,v,b[1]),Math.abs(f)>Math.abs(C)+Math.abs(R)?(o.set(s+1,v-1,(-g-c*o.get(s,v-1)+R*o.get(s,v))/f),o.set(s+1,v,(-w-c*o.get(s,v)-R*o.get(s,v-1))/f)):(b=Rr(-A-m*o.get(s,v-1),-I-m*o.get(s,v),C,R),o.set(s+1,v-1,b[0]),o.set(s+1,v,b[1]))),h=Math.max(Math.abs(o.get(s,v-1)),Math.abs(o.get(s,v))),E*h*h>1)for(i=s;i<=v;i++)o.set(i,v-1,o.get(i,v-1)/h),o.set(i,v,o.get(i,v)/h)}for(s=0;s<t;s++)if(s<M||s>x)for(i=s;i<t;i++)n.set(s,i,o.get(s,i));for(i=t-1;i>=M;i--)for(s=M;s<=x;s++){for(C=0,u=M;u<=Math.min(i,x);u++)C+=n.get(s,u)*o.get(u,i);n.set(s,i,C)}}(s,l,u,i,t)}this.n=s,this.e=l,this.d=u,this.V=i}get realEigenvalues(){return Array.from(this.d)}get imaginaryEigenvalues(){return Array.from(this.e)}get eigenvectorMatrix(){return this.V}get diagonalMatrix(){let t,e,r=this.n,n=this.e,o=this.d,s=new hr(r,r);for(t=0;t<r;t++){for(e=0;e<r;e++)s.set(t,e,0);s.set(t,t,o[t]),n[t]>0?s.set(t,t+1,n[t]):n[t]<0&&s.set(t,t-1,n[t])}return s}}function Rr(t,e,r,n){let o,s;return Math.abs(r)>Math.abs(n)?(o=n/r,s=r+o*n,[(t+o*e)/s,(e-o*t)/s]):(o=r/n,s=n+o*r,[(o*t+e)/s,(o*e-t)/s])}class Ar{constructor(t){if(!(t=yr.checkMatrix(t)).isSymmetric())throw new Error("Matrix is not symmetric");let e,r,n,o=t,s=o.rows,i=new hr(s,s),u=!0;for(r=0;r<s;r++){let t=0;for(n=0;n<r;n++){let s=0;for(e=0;e<n;e++)s+=i.get(n,e)*i.get(r,e);s=(o.get(r,n)-s)/i.get(n,n),i.set(r,n,s),t+=s*s}for(t=o.get(r,r)-t,u&&=t>0,i.set(r,r,Math.sqrt(Math.max(t,0))),n=r+1;n<s;n++)i.set(r,n,0)}this.L=i,this.positiveDefinite=u}isPositiveDefinite(){return this.positiveDefinite}solve(t){t=yr.checkMatrix(t);let e=this.L,r=e.rows;if(t.rows!==r)throw new Error("Matrix dimensions do not match");if(!1===this.isPositiveDefinite())throw new Error("Matrix is not positive definite");let n,o,s,i=t.columns,u=t.clone();for(s=0;s<r;s++)for(o=0;o<i;o++){for(n=0;n<s;n++)u.set(s,o,u.get(s,o)-u.get(n,o)*e.get(s,n));u.set(s,o,u.get(s,o)/e.get(s,s))}for(s=r-1;s>=0;s--)for(o=0;o<i;o++){for(n=s+1;n<r;n++)u.set(s,o,u.get(s,o)-u.get(n,o)*e.get(n,s));u.set(s,o,u.get(s,o)/e.get(s,s))}return u}get lowerTriangularMatrix(){return this.L}}class Ir{constructor(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};t=yr.checkMatrix(t);let{Y:r}=e;const{scaleScores:n=!1,maxIterations:o=1e3,terminationCriteria:s=1e-10}=e;let i;if(r){if(r=Be(r)&&"number"==typeof r[0]?hr.columnVector(r):yr.checkMatrix(r),r.rows!==t.rows)throw new Error("Y should have the same number of rows as X");i=r.getColumnVector(0)}else i=t.getColumnVector(0);let u,l,a,h,c=1;for(let e=0;e<o&&c>s;e++)a=t.transpose().mmul(i).div(i.transpose().mmul(i).get(0,0)),a=a.div(a.norm()),u=t.mmul(a).div(a.transpose().mmul(a).get(0,0)),e>0&&(c=u.clone().sub(h).pow(2).sum()),h=u.clone(),r?(l=r.transpose().mmul(u).div(u.transpose().mmul(u).get(0,0)),l=l.div(l.norm()),i=r.mmul(l).div(l.transpose().mmul(l).get(0,0))):i=u;if(r){let e=t.transpose().mmul(u).div(u.transpose().mmul(u).get(0,0));e=e.div(e.norm());let n=t.clone().sub(u.clone().mmul(e.transpose())),o=i.transpose().mmul(u).div(u.transpose().mmul(u).get(0,0)),s=r.clone().sub(u.clone().mulS(o.get(0,0)).mmul(l.transpose()));this.t=u,this.p=e.transpose(),this.w=a.transpose(),this.q=l,this.u=i,this.s=u.transpose().mmul(u),this.xResidual=n,this.yResidual=s,this.betas=o}else this.w=a.transpose(),this.s=u.transpose().mmul(u).sqrt(),this.t=n?u.clone().div(this.s.get(0,0)):u,this.xResidual=t.sub(u.mmul(a.transpose()))}}qe.AbstractMatrix=ur,qe.CHO=Ar,qe.CholeskyDecomposition=Ar,qe.DistanceMatrix=gr;var Cr=qe.EVD=jr;qe.EigenvalueDecomposition=jr,qe.LU=br,qe.LuDecomposition=br;var kr=qe.Matrix=hr;qe.MatrixColumnSelectionView=class extends wr{constructor(t,e){rr(t,e),super(t,t.rows,e.length),this.columnIndices=e}set(t,e,r){return this.matrix.set(t,this.columnIndices[e],r),this}get(t,e){return this.matrix.get(t,this.columnIndices[e])}},qe.MatrixColumnView=class extends wr{constructor(t,e){He(t,e),super(t,t.rows,1),this.column=e}set(t,e,r){return this.matrix.set(t,this.column,r),this}get(t){return this.matrix.get(t,this.column)}},qe.MatrixFlipColumnView=class extends wr{constructor(t){super(t,t.rows,t.columns)}set(t,e,r){return this.matrix.set(t,this.columns-e-1,r),this}get(t,e){return this.matrix.get(t,this.columns-e-1)}},qe.MatrixFlipRowView=class extends wr{constructor(t){super(t,t.rows,t.columns)}set(t,e,r){return this.matrix.set(this.rows-t-1,e,r),this}get(t,e){return this.matrix.get(this.rows-t-1,e)}},qe.MatrixRowSelectionView=class extends wr{constructor(t,e){er(t,e),super(t,e.length,t.columns),this.rowIndices=e}set(t,e,r){return this.matrix.set(this.rowIndices[t],e,r),this}get(t,e){return this.matrix.get(this.rowIndices[t],e)}},qe.MatrixRowView=class extends wr{constructor(t,e){Xe(t,e),super(t,1,t.columns),this.row=e}set(t,e,r){return this.matrix.set(this.row,e,r),this}get(t,e){return this.matrix.get(this.row,e)}},qe.MatrixSelectionView=dr,qe.MatrixSubView=class extends wr{constructor(t,e,r,n,o){nr(t,e,r,n,o),super(t,r-e+1,o-n+1),this.startRow=e,this.startColumn=n}set(t,e,r){return this.matrix.set(this.startRow+t,this.startColumn+e,r),this}get(t,e){return this.matrix.get(this.startRow+t,this.startColumn+e)}};var Nr=qe.MatrixTransposeView=class extends wr{constructor(t){super(t,t.columns,t.rows)}set(t,e,r){return this.matrix.set(e,t,r),this}get(t,e){return this.matrix.get(e,t)}},Tr=qe.NIPALS=Ir;qe.Nipals=Ir,qe.QR=Mr,qe.QrDecomposition=Mr;var Fr=qe.SVD=xr;qe.SingularValueDecomposition=xr,qe.SymmetricMatrix=mr,qe.WrapperMatrix1D=pr,qe.WrapperMatrix2D=yr,qe.correlation=function(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:t,r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};t=new hr(t);let n=!1;if("object"!=typeof e||hr.isMatrix(e)||Be(e)?e=new hr(e):(r=e,e=t,n=!0),t.rows!==e.rows)throw new TypeError("Both matrices must have the same number of rows");const{center:o=!0,scale:s=!0}=r;o&&(t.center("column"),n||e.center("column")),s&&(t.scale("column"),n||e.scale("column"));const i=t.standardDeviation("column",{unbiased:!0}),u=n?i:e.standardDeviation("column",{unbiased:!0}),l=t.transpose().mmul(e);for(let e=0;e<l.rows;e++)for(let r=0;r<l.columns;r++)l.set(e,r,l.get(e,r)*(1/(i[e]*u[r]))*(1/(t.rows-1)));return l},qe.covariance=function(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:t,r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};t=new hr(t);let n=!1;if("object"!=typeof e||hr.isMatrix(e)||Be(e)?e=new hr(e):(r=e,e=t,n=!0),t.rows!==e.rows)throw new TypeError("Both matrices must have the same number of rows");const{center:o=!0}=r;o&&(t=t.center("column"),n||(e=e.center("column")));const s=t.transpose().mmul(e);for(let e=0;e<s.rows;e++)for(let r=0;r<s.columns;r++)s.set(e,r,s.get(e,r)*(1/(t.rows-1)));return s};var Or=qe.default=hr;qe.determinant=function t(e){if((e=hr.checkMatrix(e)).isSquare()){if(0===e.columns)return 1;let r,n,o,s;if(2===e.columns)return r=e.get(0,0),n=e.get(0,1),o=e.get(1,0),s=e.get(1,1),r*s-n*o;if(3===e.columns){let s,i,u;return s=new dr(e,[1,2],[1,2]),i=new dr(e,[1,2],[0,2]),u=new dr(e,[1,2],[0,1]),r=e.get(0,0),n=e.get(0,1),o=e.get(0,2),r*t(s)-n*t(i)+o*t(u)}return new br(e).determinant}throw Error("determinant can only be calculated for a square matrix")},qe.inverse=function(t){let e=arguments.length>1&&void 0!==arguments[1]&&arguments[1];return t=yr.checkMatrix(t),e?new xr(t).inverse():Er(t,hr.eye(t.rows))},qe.linearDependencies=function(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};const{thresholdValue:r=1e-9,thresholdError:n=1e-9}=e;let o=(t=hr.checkMatrix(t)).rows,s=new hr(o,o);for(let e=0;e<o;e++){let i=hr.columnVector(t.getRow(e)),u=t.subMatrixRow(Sr(o,e)).transpose(),l=new xr(u).solve(i),a=hr.sub(i,u.mmul(l)).abs().max();s.setRow(e,_r(a,l,e,r,n))}return s},qe.pseudoInverse=function(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:Number.EPSILON;if((t=hr.checkMatrix(t)).isEmpty())return t.transpose();let r=new xr(t,{autoTranspose:!0}),n=r.leftSingularVectors,o=r.rightSingularVectors,s=r.diagonal;for(let t=0;t<s.length;t++)Math.abs(s[t])>e?s[t]=1/s[t]:s[t]=0;return o.mmul(hr.diag(s).mmul(n.transpose()))},qe.solve=Er,qe.wrap=function(t,e){if(Be(t))return t[0]&&Be(t[0])?new yr(t):new pr(t,e);throw new Error("the argument is not an array")};const Vr=Cr,zr=kr,Dr=Nr,Pr=Tr,qr=Fr;Or.Matrix&&Or.Matrix;let $r=class t{constructor(t){let e,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=arguments.length>2?arguments[2]:void 0;if(this.U=null,this.S=null,n)return this.center=n.center,this.scale=n.scale,this.means=n.means,this.stdevs=n.stdevs,this.U=zr.checkMatrix(n.U),this.S=n.S,this.R=n.R,void(this.excludedFeatures=n.excludedFeatures||[]);e=(Array.isArray(t),new zr(t));const{isCovarianceMatrix:o=!1,method:s="SVD",nCompNIPALS:i=2,center:u=!0,scale:l=!1,ignoreZeroVariance:a=!1}=r;if(this.center=u,this.scale=l,this.means=null,this.stdevs=null,this.excludedFeatures=[],o)this._computeFromCovarianceMatrix(e);else switch(this._adjust(e,a),s){case"covarianceMatrix":{const t=new Dr(e).mmul(e).div(e.rows-1);this._computeFromCovarianceMatrix(t);break}case"NIPALS":this._computeWithNIPALS(e,i);break;case"SVD":{const t=new qr(e,{computeLeftSingularVectors:!1,computeRightSingularVectors:!0,autoTranspose:!0});this.U=t.rightSingularVectors;const r=t.diagonal,n=[];for(const t of r)n.push(t*t/(e.rows-1));this.S=n;break}default:throw new Error(`unknown method: ${s}`)}}static load(e){if("string"!=typeof e.name)throw new TypeError("model must have a name property");if("PCA"!==e.name)throw new RangeError(`invalid model: ${e.name}`);return new t(void 0,void 0,e)}predict(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};const{nComponents:r=this.U.columns}=e;let n;if(n=(Array.isArray(t),new zr(t)),this.center&&(n.subRowVector(this.means),this.scale)){for(let t of this.excludedFeatures)n.removeColumn(t);n.divRowVector(this.stdevs)}let o=n.mmul(this.U);return o.subMatrix(0,o.rows-1,0,r-1)}invert(t){let e=(t=zr.checkMatrix(t)).mmul(this.U.transpose());return this.center&&(this.scale&&e.mulRowVector(this.stdevs),e.addRowVector(this.means)),e}getExplainedVariance(){let t=0;if(this.S)for(const e of this.S)t+=e;return this.S?this.S.map((e=>e/t)):[]}getCumulativeVariance(){let t=this.getExplainedVariance();for(let e=1;e<t.length;e++)t[e]+=t[e-1];return t}getEigenvectors(){return this.U}getEigenvalues(){return this.S}getStandardDeviations(){return this.S.map((t=>Math.sqrt(t)))}getLoadings(){return this.U.transpose()}toJSON(){return{name:"PCA",center:this.center,scale:this.scale,means:this.means,stdevs:this.stdevs,U:this.U,S:this.S,excludedFeatures:this.excludedFeatures}}_adjust(t,e){if(this.center){const r=t.mean("column"),n=this.scale?t.standardDeviation("column",{mean:r}):null;if(this.means=r,t.subRowVector(r),this.scale){for(let r=0;r<n.length;r++)if(0===n[r]){if(!e)throw new RangeError(`Cannot scale the dataset (standard deviation is zero at index ${r}`);t.removeColumn(r),n.splice(r,1),this.excludedFeatures.push(r),r--}this.stdevs=n,t.divRowVector(n)}}}_computeFromCovarianceMatrix(t){const e=new Vr(t,{assumeSymmetric:!0});this.U=e.eigenvectorMatrix,this.U.flipRows(),this.S=e.realEigenvalues,this.S.reverse()}_computeWithNIPALS(t,e){this.U=new zr(e,t.columns),this.S=[];let r=t;for(let t=0;t<e;t++){let e=new Pr(r);this.U.setRow(t,e.w.transpose()),this.S.push(Math.pow(e.s.get(0,0),2)),r=e.xResidual}this.U=this.U.transpose()}};function Lr(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!ce(t))throw new Error("data is not eff. array");let r=wt(e,"scale");me(r)||(r=!0);let n=wt(e,"nCompNIPALS");we(n)||(n=2),n=Ne(n);let o=se(wt(t,0,[]));if(0===o)throw new Error("invalid dimension of data");if(n>o)throw new Error(`nCompNIPALS[${n}] is more than the dimension[${o}] of data`);return new $r(t,{method:"NIPALS",scale:r,nCompNIPALS:n}).predict(t).toJSON()}function Ur(t,e){for(var r=-1,n=null==t?0:t.length;++r<n&&!1!==e(t[r],r,t););return t}var Br,Gr=function(t,e,r){for(var n=-1,o=Object(t),s=r(t),i=s.length;i--;){var u=s[Br?i:++n];if(!1===e(o[u],u,o))break}return t};function Wr(t){return g(t)&&"[object Arguments]"==m(t)}var Kr=Object.prototype,Qr=Kr.hasOwnProperty,Yr=Kr.propertyIsEnumerable,Jr=Wr(function(){return arguments}())?Wr:function(t){return g(t)&&Qr.call(t,"callee")&&!Yr.call(t,"callee")},Xr=Jr;var Hr="object"==typeof exports&&exports&&!exports.nodeType&&exports,Zr=Hr&&"object"==typeof module&&module&&!module.nodeType&&module,tn=Zr&&Zr.exports===Hr?n.Buffer:void 0,en=(tn?tn.isBuffer:void 0)||function(){return!1},rn=9007199254740991,nn=/^(?:0|[1-9]\d*)$/;function on(t,e){var r=typeof t;return!!(e=null==e?rn:e)&&("number"==r||"symbol"!=r&&nn.test(t))&&t>-1&&t%1==0&&t<e}var sn={};sn["[object Float32Array]"]=sn["[object Float64Array]"]=sn["[object Int8Array]"]=sn["[object Int16Array]"]=sn["[object Int32Array]"]=sn["[object Uint8Array]"]=sn["[object Uint8ClampedArray]"]=sn["[object Uint16Array]"]=sn["[object Uint32Array]"]=!0,sn["[object Arguments]"]=sn["[object Array]"]=sn["[object ArrayBuffer]"]=sn["[object Boolean]"]=sn["[object DataView]"]=sn["[object Date]"]=sn["[object Error]"]=sn["[object Function]"]=sn["[object Map]"]=sn["[object Number]"]=sn["[object Object]"]=sn["[object RegExp]"]=sn["[object Set]"]=sn["[object String]"]=sn["[object WeakMap]"]=!1;var un="object"==typeof exports&&exports&&!exports.nodeType&&exports,ln=un&&"object"==typeof module&&module&&!module.nodeType&&module,an=ln&&ln.exports===un&&e.process,hn=function(){try{var t=ln&&ln.require&&ln.require("util").types;return t||an&&an.binding&&an.binding("util")}catch(t){}}(),cn=hn&&hn.isTypedArray,fn=cn?function(t){return function(e){return t(e)}}(cn):function(t){return g(t)&&Dt(t.length)&&!!sn[m(t)]},mn=fn,gn=Object.prototype.hasOwnProperty;function wn(e,r){var n=t(e),o=!n&&Xr(e),s=!n&&!o&&en(e),i=!n&&!o&&!s&&mn(e),u=n||o||s||i,l=u?function(t,e){for(var r=-1,n=Array(t);++r<t;)n[r]=e(r);return n}(e.length,String):[],a=l.length;for(var h in e)!r&&!gn.call(e,h)||u&&("length"==h||s&&("offset"==h||"parent"==h)||i&&("buffer"==h||"byteLength"==h||"byteOffset"==h)||on(h,a))||l.push(h);return l}function dn(t){return Pt(t)?wn(t):vt(t)}var pn=function(t,e){return function(r,n){if(null==r)return r;if(!Pt(r))return t(r,n);for(var o=r.length,s=e?o:-1,i=Object(r);(e?s--:++s<o)&&!1!==n(i[s],s,i););return r}}((function(t,e){return t&&Gr(t,e,dn)})),yn=pn;function bn(t){return t}function vn(e,r){var n;return(t(e)?Ur:yn)(e,"function"==typeof(n=r)?n:bn)}function Mn(t){var e=this.__data__=new W(t);this.size=e.size}Mn.prototype.clear=function(){this.__data__=new W,this.size=0},Mn.prototype.delete=function(t){var e=this.__data__,r=e.delete(t);return this.size=e.size,r},Mn.prototype.get=function(t){return this.__data__.get(t)},Mn.prototype.has=function(t){return this.__data__.has(t)},Mn.prototype.set=function(t,e){var r=this.__data__;if(r instanceof W){var n=r.__data__;if(!K||n.length<199)return n.push([t,e]),this.size=++r.size,this;r=this.__data__=new Y(n)}return r.set(t,e),this.size=r.size,this};function xn(t){var e=-1,r=null==t?0:t.length;for(this.__data__=new Y;++e<r;)this.add(t[e])}function En(t,e){for(var r=-1,n=null==t?0:t.length;++r<n;)if(e(t[r],r,t))return!0;return!1}function Sn(t,e){return t.has(e)}xn.prototype.add=xn.prototype.push=function(t){return this.__data__.set(t,"__lodash_hash_undefined__"),this},xn.prototype.has=function(t){return this.__data__.has(t)};var _n=1,jn=2;function Rn(t,e,r,n,o,s){var i=r&_n,u=t.length,l=e.length;if(u!=l&&!(i&&l>u))return!1;var a=s.get(t),h=s.get(e);if(a&&h)return a==e&&h==t;var c=-1,f=!0,m=r&jn?new xn:void 0;for(s.set(t,e),s.set(e,t);++c<u;){var g=t[c],w=e[c];if(n)var d=i?n(w,g,c,e,t,s):n(g,w,c,t,e,s);if(void 0!==d){if(d)continue;f=!1;break}if(m){if(!En(e,(function(t,e){if(!Sn(m,e)&&(g===t||o(g,t,r,n,s)))return m.push(e)}))){f=!1;break}}else if(g!==w&&!o(g,w,r,n,s)){f=!1;break}}return s.delete(t),s.delete(e),f}var An=n.Uint8Array;function In(t){var e=-1,r=Array(t.size);return t.forEach((function(t,n){r[++e]=[n,t]})),r}function Cn(t){var e=-1,r=Array(t.size);return t.forEach((function(t){r[++e]=t})),r}var kn=1,Nn=2,Tn="[object Boolean]",Fn="[object Date]",On="[object Error]",Vn="[object Map]",zn="[object Number]",Dn="[object RegExp]",Pn="[object Set]",qn="[object String]",$n="[object Symbol]",Ln="[object ArrayBuffer]",Un="[object DataView]",Bn=o?o.prototype:void 0,Gn=Bn?Bn.valueOf:void 0;var Wn=Object.prototype.propertyIsEnumerable,Kn=Object.getOwnPropertySymbols,Qn=Kn?function(t){return null==t?[]:(t=Object(t),function(t,e){for(var r=-1,n=null==t?0:t.length,o=0,s=[];++r<n;){var i=t[r];e(i,r,t)&&(s[o++]=i)}return s}(Kn(t),(function(e){return Wn.call(t,e)})))}:function(){return[]},Yn=Qn;function Jn(e){return function(e,r,n){var o=r(e);return t(e)?o:function(t,e){for(var r=-1,n=e.length,o=t.length;++r<n;)t[o+r]=e[r];return t}(o,n(e))}(e,dn,Yn)}var Xn=1,Hn=Object.prototype.hasOwnProperty;var Zn=1,to="[object Arguments]",eo="[object Array]",ro="[object Object]",no=Object.prototype.hasOwnProperty;function oo(e,r,n,o,s,i){var u=t(e),l=t(r),a=u?eo:Vt(e),h=l?eo:Vt(r),c=(a=a==to?ro:a)==ro,f=(h=h==to?ro:h)==ro,m=a==h;if(m&&en(e)){if(!en(r))return!1;u=!0,c=!1}if(m&&!c)return i||(i=new Mn),u||mn(e)?Rn(e,r,n,o,s,i):function(t,e,r,n,o,s,i){switch(r){case Un:if(t.byteLength!=e.byteLength||t.byteOffset!=e.byteOffset)return!1;t=t.buffer,e=e.buffer;case Ln:return!(t.byteLength!=e.byteLength||!s(new An(t),new An(e)));case Tn:case Fn:case zn:return U(+t,+e);case On:return t.name==e.name&&t.message==e.message;case Dn:case qn:return t==e+"";case Vn:var u=In;case Pn:var l=n&kn;if(u||(u=Cn),t.size!=e.size&&!l)return!1;var a=i.get(t);if(a)return a==e;n|=Nn,i.set(t,e);var h=Rn(u(t),u(e),n,o,s,i);return i.delete(t),h;case $n:if(Gn)return Gn.call(t)==Gn.call(e)}return!1}(e,r,a,n,o,s,i);if(!(n&Zn)){var g=c&&no.call(e,"__wrapped__"),w=f&&no.call(r,"__wrapped__");if(g||w){var d=g?e.value():e,p=w?r.value():r;return i||(i=new Mn),s(d,p,n,o,i)}}return!!m&&(i||(i=new Mn),function(t,e,r,n,o,s){var i=r&Xn,u=Jn(t),l=u.length;if(l!=Jn(e).length&&!i)return!1;for(var a=l;a--;){var h=u[a];if(!(i?h in e:Hn.call(e,h)))return!1}var c=s.get(t),f=s.get(e);if(c&&f)return c==e&&f==t;var m=!0;s.set(t,e),s.set(e,t);for(var g=i;++a<l;){var w=t[h=u[a]],d=e[h];if(n)var p=i?n(d,w,h,e,t,s):n(w,d,h,t,e,s);if(!(void 0===p?w===d||o(w,d,r,n,s):p)){m=!1;break}g||(g="constructor"==h)}if(m&&!g){var y=t.constructor,b=e.constructor;y==b||!("constructor"in t)||!("constructor"in e)||"function"==typeof y&&y instanceof y&&"function"==typeof b&&b instanceof b||(m=!1)}return s.delete(t),s.delete(e),m}(e,r,n,o,s,i))}function so(t,e,r,n,o){return t===e||(null==t||null==e||!g(t)&&!g(e)?t!=t&&e!=e:oo(t,e,r,n,so,o))}var io=1,uo=2;function lo(t){return t==t&&!v(t)}function ao(t,e){return function(r){return null!=r&&(r[t]===e&&(void 0!==e||t in Object(r)))}}function ho(t){var e=function(t){for(var e=dn(t),r=e.length;r--;){var n=e[r],o=t[n];e[r]=[n,o,lo(o)]}return e}(t);return 1==e.length&&e[0][2]?ao(e[0][0],e[0][1]):function(r){return r===t||function(t,e,r,n){var o=r.length,s=o,i=!n;if(null==t)return!s;for(t=Object(t);o--;){var u=r[o];if(i&&u[2]?u[1]!==t[u[0]]:!(u[0]in t))return!1}for(;++o<s;){var l=(u=r[o])[0],a=t[l],h=u[1];if(i&&u[2]){if(void 0===a&&!(l in t))return!1}else{var c=new Mn;if(n)var f=n(a,h,l,t,e,c);if(!(void 0===f?so(h,a,io|uo,n,c):f))return!1}}return!0}(r,t,e)}}function co(t,e){return null!=t&&e in Object(t)}function fo(e,r){return null!=e&&function(e,r,n){for(var o=-1,s=(r=ct(r,e)).length,i=!1;++o<s;){var u=mt(r[o]);if(!(i=null!=e&&n(e,u)))break;e=e[u]}return i||++o!=s?i:!!(s=null==e?0:e.length)&&Dt(s)&&on(u,s)&&(t(e)||Xr(e))}(e,r,co)}var mo=1,go=2;function wo(t){return b(t)?$t(mt(t)):function(t){return function(e){return gt(e,t)}}(t)}function po(e){return"function"==typeof e?e:null==e?bn:"object"==typeof e?t(e)?(r=e[0],n=e[1],b(r)&&lo(n)?ao(mt(r),n):function(t){var e=wt(t,r);return void 0===e&&e===n?fo(t,r):so(n,e,mo|go)}):ho(e):wo(e);var r,n}function yo(t,e){var r=-1,n=Pt(t)?Array(t.length):[];return yn(t,(function(t,o,s){n[++r]=e(t,o,s)})),n}function bo(e,r){return(t(e)?st:yo)(e,po(r))}function vo(t){return null==t?[]:function(t,e){return st(e,(function(e){return t[e]}))}(t,dn(t))}function Mo(t,e){if(!ue(t))return{};if(!ce(e))return{};let r={};return vn(e,(function(e){(function(t,e){return!!ue(t)&&!(!ge(e)&&!we(e))&&e in t})(t,e)&&(r[e]=wt(t,e))})),r}var xo={},Eo={};Object.defineProperty(Eo,"__esModule",{value:!0});class So{constructor(t){this.getCost=()=>this.Elements.map((t=>t.DistanceFromMedoid)).reduce(((t,e)=>t+e),0),this.findBestMedoidIdx=t=>this.Elements.map((e=>this.calcCostForGivenElementAsMedoid(e,t))).reduce(((t,e,r,n)=>e.CostIfMadeMedoid<n[t].CostIfMadeMedoid?r:t),0),this.calcCostForGivenElementAsMedoid=(t,e)=>({CostIfMadeMedoid:new So({Elements:this.Elements.map((r=>({DistanceFromMedoid:e(r.Element,t.Element),Element:r.Element}))),Medoid:t.Element}).getCost(),Element:t}),this.Elements=t.Elements,this.Medoid=t.Medoid}}Eo.Cluster=So;var _o={},jo={};Object.defineProperty(jo,"__esModule",{value:!0}),jo.distance=(t,e)=>{if(t.length!==e.length)throw new Error("the inputs must have the same dimension");const r=Array.from(t.keys()).map((r=>Math.pow(Math.abs(t[r]-e[r]),2))).reduce(((t,e)=>t+e),0);return Math.sqrt(r)};var Ro={};Object.defineProperty(Ro,"__esModule",{value:!0}),Ro.kGtElementArrLength="k must be less than the number of elements in the input array",Object.defineProperty(_o,"__esModule",{value:!0});const Ao=jo,Io=Ro,Co=Eo;class ko{constructor(t,e,r){this.Elements=t,this.K=e,this.DistanceFn=r,this.OptimisationCompleted=!1,this.iterate=()=>{this.IterationCount++,this.Clusters||this.allocateToClustersAroundCurrentMedoids();const t=this.Medoids,e=this.Clusters,r=this.calculateCurrentCost();let n=r;for(let t=0;t<this.Medoids.length;t++){const e=t=>0===this.Medoids.filter((e=>0===this.DistanceFn(t,e))).length;for(const r of this.Elements.filter(e)){const e=this.Medoids.slice();e[t]=r;const o=new ko(this.Elements.slice(),this.K,this.DistanceFn);o.Medoids=e,o.allocateToClustersAroundCurrentMedoids();const s=o.calculateCurrentCost();s<n&&(n=s,this.Clusters=o.Clusters,this.Medoids=e)}}return this.calculateCurrentCost()<r||(this.Clusters=e,this.Medoids=t,this.OptimisationCompleted=!0,!1)},this.getClusteredData=()=>(this.OptimisationCompleted||this.runToCompletion(),this.Clusters.map((t=>t.Elements.map((t=>t.Element))))),this.runToCompletion=()=>{for(;this.iterate(););},this.allocateToClustersAroundCurrentMedoids=()=>{this.Clusters=new Array,this.Medoids.forEach((t=>{this.Clusters.push(new Co.Cluster({Elements:[],Medoid:t}))})),this.Elements.forEach(((t,e)=>{const r=this.findDistances(this.Clusters.map((t=>t.Medoid)),t),n=r.reduce(((t,e,r,n)=>e<n[t]?r:t),0);this.Clusters[n].Elements.push({DistanceFromMedoid:r[n],Element:t})}))},this.calculateCurrentCost=()=>this.Clusters.map((t=>t.getCost())).reduce(((t,e)=>t+e),0),this.findDistances=(t,e)=>t.map((t=>this.DistanceFn(t,e))),this.selectInitialMedoids=()=>{if(this.K>=this.Elements.length)throw Io.kGtElementArrLength;const t=[],e=(t,e)=>{for(const r of e)if(0===this.DistanceFn(r,t))return!1;return!0};let r=0;for(;r<this.Elements.length&&t.length<this.K;)e(this.Elements[r],t)&&t.push(this.Elements[r]),r++;return t},this.DistanceFn=this.DistanceFn||Ao.distance,this.Medoids=this.selectInitialMedoids(),this.IterationCount=0}get OptimisationStarted(){return this.IterationCount>0}}ko.clusterElements=(t,e,r)=>{ko.getInstance(t,e,r);return new Array},ko.getInstance=(t,e,r)=>new ko(t,e,r),_o.Clusterer=ko,function(t){function e(e){for(var r in e)t.hasOwnProperty(r)||(t[r]=e[r])}Object.defineProperty(t,"__esModule",{value:!0}),e(Eo),e(_o)}(xo);var No=Pe(xo);function To(t,e){let r=0;for(let n=0;n<t.length;n++)r+=(t[n]-e[n])*(t[n]-e[n]);return r}const Fo=Object.prototype.toString;function Oo(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};const{minLength:r}=e;if(!function(t){const e=Fo.call(t);return e.endsWith("Array]")&&!e.includes("Big")}(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");if("number"!=typeof t[0])throw new TypeError("input must contain numbers");if(r&&t.length<r)throw new Error(`input must have a length of at least ${r}`)}function Vo(t,e){let r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};const{sorted:n=!0}=r;if(n){let r=0,n=t.length-1,o=0;for(;n-r>1;)if(o=r+(n-r>>1),t[o]<e)r=o;else{if(!(t[o]>e))return o;n=o}return r<t.length-1?Math.abs(e-t[r])<Math.abs(t[r+1]-e)?r:r+1:r}{let r=0,n=Number.POSITIVE_INFINITY;for(let o=0;o<t.length;o++){const s=Math.abs(t[o]-e);s<n&&(n=s,r=o)}return r}}function zo(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},{fromIndex:r,toIndex:n}=e;const{from:o,to:s}=e;return void 0===r&&(r=void 0!==o?Vo(t,o):0),void 0===n&&(n=void 0!==s?Vo(t,s):t.length-1),r<0&&(r=0),n<0&&(n=0),r>=t.length&&(r=t.length-1),n>=t.length&&(n=t.length-1),r>n&&([r,n]=[n,r]),{fromIndex:r,toIndex:n}}function Do(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};Oo(t);const{fromIndex:r,toIndex:n}=zo(t,e);let o=t[r];for(let e=r+1;e<=n;e++)o+=t[e];return o/(n-r+1)}function Po(t,e){const r=65535&(t>>>=0);return((t-r)*(e>>>=0)>>>0)+r*e>>>0}class qo{constructor(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Date.now();De(this,"random",void 0),De(this,"state",void 0),this.state=new Uint32Array(4),this.init(t),this.random=this.getFloat.bind(this)}getUint32(){return this.nextState(),this.state[3]+this.state[2]>>>0}getFloat(){return 5.960464477539063e-8*(this.getUint32()>>>8)}init(t){if(!Number.isInteger(t))throw new TypeError("seed must be an integer");this.state[0]=t,this.state[1]=0,this.state[2]=0,this.state[3]=0;for(let t=1;t<8;t++)this.state[3&t]^=t+Po(1812433253,this.state[t-1&3]^this.state[t-1&3]>>>30>>>0)>>>0;this.periodCertification();for(let t=0;t<8;t++)this.nextState()}periodCertification(){0===this.state[0]&&0===this.state[1]&&0===this.state[2]&&0===this.state[3]&&(this.state[0]=88,this.state[1]=83,this.state[2]=65,this.state[3]=68)}nextState(){let t=this.state[0];t^=t<<15,t^=t>>>18,t^=this.state[3]<<11,this.state[0]=this.state[1],this.state[1]=this.state[2],this.state[2]=this.state[3],this.state[3]=t}}function $o(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};Oo(t);const{fromIndex:r,toIndex:n}=zo(t,e);let o=t[r];for(let e=r+1;e<=n;e++)o+=t[e];return o}const Lo={distanceFunction:To};function Uo(t,e){let r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:Lo;const n=r.distanceFunction||Lo.distanceFunction,o=r.similarityFunction||Lo.similarityFunction;let s=-1;if("function"==typeof o){let r=Number.MIN_VALUE;for(let n=0;n<t.length;n++){const i=o(e,t[n]);i>r&&(r=i,s=n)}}else{if("function"!=typeof n)throw new Error("A similarity or distance function it's required");{let r=Number.MAX_VALUE;for(let o=0;o<t.length;o++){const i=n(e,t[o]);i<r&&(r=i,s=o)}}}return s}function Bo(t,e,r,n){const o={distanceFunction:n};for(let n=0;n<t.length;n++)r[n]=Uo(e,t[n],o);return r}class Go{constructor(t,e,r,n,o){De(this,"clusters",void 0),De(this,"centroids",void 0),De(this,"converged",void 0),De(this,"iterations",void 0),De(this,"distance",void 0),this.clusters=t,this.centroids=e,this.converged=r,this.iterations=n,this.distance=o}nearest(t){const e=new Array(t.length);return Bo(t,this.centroids,e,this.distance)}computeInformation(t){const e=this.centroids.map((()=>[]));for(let r=0;r<t.length;r++){const n=this.clusters[r];e[n].push(this.distance(t[r],this.centroids[n]))}return this.centroids.map(((t,r)=>{const n=e[r];return{centroid:t,error:n.length>0?Do(n):-1,size:n.length}}))}}function Wo(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:Math.random;const{size:n=1,replace:o=!1,probabilities:s}=e;let i,u;if(i="number"==typeof t?function(t){const e=[];for(let r=0;r<t;r++)e.push(r);return e}(t):t.slice(),s){if(!o)throw new Error("choice with probabilities and no replacement is not implemented");if(s.length!==i.length)throw new Error("the length of probabilities option should be equal to the number of choices");u=[s[0]];for(let t=1;t<s.length;t++)u[t]=u[t-1]+s[t];if(Math.abs(1-u.at(-1))>1e-8)throw new Error(`probabilities should sum to 1, but instead sums to ${u.at(-1)}`)}if(!o&&n>i.length)throw new Error("size option is too large");const l=[];for(let t=0;t<n;t++){const t=Ko(i.length,r,u);l.push(i[t]),o||i.splice(t,1)}return l}function Ko(t,e,r){const n=e();if(r){let t=0;for(;n>r[t];)t++;return t}return Math.floor(n*t)}class Qo{constructor(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Math.random;if(De(this,"randomGenerator",void 0),"number"==typeof t){const e=new qo(t);this.randomGenerator=e.random}else this.randomGenerator=t}choice(t,e){return Wo(t,e,this.randomGenerator)}random(){return this.randomGenerator()}randInt(t,e){return void 0===e&&(e=t,t=0),t+Math.floor(this.randomGenerator()*(e-t))}randomSample(t){const e=[];for(let r=0;r<t;r++)e.push(this.random());return e}}function Yo(t,e){const r=new zr(t.rows,e.rows);for(let n=0;n<t.rows;n++)for(let o=0;o<e.rows;o++)r.set(n,o,To(t.getRow(n),e.getRow(o)));return r}const Jo={maxIterations:100,tolerance:1e-6,initialization:"kmeans++",distanceFunction:To};function Xo(t,e,r,n,o,s){const i=function(t,e,r,n){const o=e[0].length,s=new Array(n),i=new Array(n);for(let t=0;t<n;t++){s[t]=new Array(o),i[t]=0;for(let e=0;e<o;e++)s[t][e]=0}for(let t=0;t<e.length;t++){i[r[t]]++;for(let n=0;n<o;n++)s[r[t]][n]+=e[t][n]}for(let e=0;e<n;e++)for(let r=0;r<o;r++)i[e]?s[e][r]/=i[e]:s[e][r]=t[e][r];return s}(t,e,r=Bo(e,t,r,o.distanceFunction),n),u=function(t,e,r,n){for(let o=0;o<t.length;o++)if(r(t[o],e[o])>n)return!1;return!0}(i,t,o.distanceFunction,o.tolerance);return new Go(r,i,u,s,o.distanceFunction)}function Ho(t,e,r){const n=function(t){return{...Jo,...t}}(r);!function(t,e){if(e<=0||e>t.length||!Number.isInteger(e))throw new Error("K should be a positive integer smaller than the number of points")}(t,e);let o=function(t,e,r){let n;if(Array.isArray(r.initialization)){if(r.initialization.length!==e)throw new Error("The initial centers should have the same length as K");n=r.initialization}else switch(r.initialization){case"kmeans++":n=function(t,e){let r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};const n=new zr(t),o=n.rows,s=new Qo(r.seed),i=[],u=r.localTrials||2+Math.floor(Math.log(e)),l=s.randInt(o);i.push(n.getRow(l));let a=new zr(1,n.rows);for(let t=0;t<n.rows;t++)a.set(0,t,To(n.getRow(t),i[0]));let h=zr.mul(a,1/$o(a.getRow(0)));for(let t=1;t<e;t++){const e=s.choice(o,{replace:!0,size:u,probabilities:h.getRow(0)}),r=Yo(n.subMatrixRow(e),n);let l=1/0,c=1/0,f=a;for(let t=0;t<u;t++){const n=zr.min(a,[r.getRow(t)]),o=n.sum();o<c&&(l=e[t],c=o,f=n)}i[t]=n.getRow(l),a=f,h=zr.mul(a,1/$o(a.getRow(0)))}return i}(t,e,r);break;case"random":n=function(t,e,r){return new Qo(r).choice(t,{size:e})}(t,e,r.seed);break;case"mostDistant":n=function(t,e,r,n){const o=new Qo(n),s=new Array(e);if(s[0]=Math.floor(o.random()*t.length),e>1){const n={dist:-1,index:-1};for(let e=0;e<t.length;++e)r[s[0]][e]>n.dist&&(n.dist=r[s[0]][e],n.index=e);if(s[1]=n.index,e>2)for(let n=2;n<e;++n){let e={dist:-1,index:-1};for(let o=0;o<t.length;++o){let t={dist:Number.MAX_VALUE,index:-1};if(!s.includes(o))for(let e=0;e<n;++e)r[s[e]][o]<t.dist&&(t={dist:r[s[e]][o],index:o});t.dist!==Number.MAX_VALUE&&t.dist>e.dist&&(e={...t})}s[n]=e.index}}return s.map((e=>t[e]))}(t,e,function(t,e){const r=new Array(t.length);for(let e=0;e<t.length;++e)r[e]=new Array(t.length);for(let n=0;n<t.length;++n)for(let o=n;o<t.length;++o){const s=e(t[n],t[o]);r[n][o]=s,r[o][n]=s}return r}(t,r.distanceFunction),r.seed);break;default:!function(t,e){throw new Error(`${e}: "${String(t)}"`)}(r.initialization,"Unknown initialization method")}return n}(t,e,n);0===n.maxIterations&&(n.maxIterations=Number.MAX_VALUE);const s=new Array(t.length);let i,u=!1,l=0;for(;!u&&l<n.maxIterations;)i=Xo(o,t,s,e,n,++l),u=i.converged,o=i.centroids;if(!i)throw new Error("unreachable: no kmeans step executed");return i}class Zo{constructor(t){this.rows=t}len(){return this.rows.length}isSquare(){const t=this.rows.length;for(let e=0;e<t;e++)if(this.rows[e].length!==t)return!1;return!0}get(t,e){return this.rows[t][e]}}const ts=4294967295;class es{constructor(t,e){this.i=t,this.d=e}clone(){return new es(this.i,this.d)}static empty(){return new es(ts,0)}}class rs{constructor(t,e,r,n){this.near=new es(t,e),this.seco=new es(r,n)}clone(){return new rs(this.near.i,this.near.d,this.seco.i,this.seco.d)}static empty(){return new rs(ts,0,ts,0)}}function ns(t,e,r){const n=(t=function(t){if(t&&"function"==typeof t.get&&"function"==typeof t.len)return t;if(Array.isArray(t))return new Zo(t);throw new Error("Unsupported matrix input: provide a 2D array, a DenseMatrix, or a LowerTriangle")}(t)).len(),o=e.length;if(1===o){const r=new Array(n).fill(0),[o,s]=function(t,e,r,n){const o=r[n];let s=o,i=0;for(let r=0;r<e.length;r++)o!==r&&e[r]===n&&(i+=t.get(o,r));for(let r=0;r<e.length;r++)if(r!==o&&e[r]===n){let o=0;for(let s=0;s<e.length;s++)s!==r&&e[s]===n&&(o+=t.get(r,s));o<i&&(s=r,i=o)}return r[n]=s,[s!==o,i]}(t,r,e,0);return{loss:s,assi:r,nIter:1,nSwaps:o?1:0}}let[s,i]=function(t,e){const r=t.len(),n=e.length;if(!t.isSquare())throw new Error("Dissimilarity matrix is not square");if(!(r<=ts))throw new Error("N is too large");if(!(n>0&&n<ts))throw new Error("invalid N");if(!(n<=r))throw new Error("k must be at most N");const o=Array.from({length:t.len()},(()=>rs.empty())),s=e[0];let i=0;for(let r=0;r<o.length;r++){o[r]=new rs(0,t.get(r,s),ts,0);const n=o[r];for(let o=1;o<e.length;o++){const s=e[o],i=t.get(r,s);i<n.near.d||r===s?(n.seco=n.near.clone(),n.near=new es(o,i)):(n.seco.i===ts||i<n.seco.d)&&(n.seco=new es(o,i))}i+=n.near.d}return[i,o]}(t,e),u=new Array(o).fill(0);ss(i,u);let l=n,a=0,h=0;for(;h<r;){h+=1;const r=a,o=s;for(let r=0;r<n&&r!==l;r++){if(r===e[i[r].near.i])continue;const[n,o]=os(t,u,i,r);n>=0||(a+=1,l=r,s=us(t,e,i,o,r),ss(i,u))}if(a===r||s>=o)break}return{loss:s,assi:i.map((t=>t.near.i)),nIter:h,nSwaps:a}}function os(t,e,r,n){const o=e.slice();let s=0;for(let e=0;e<r.length;e++){const i=r[e],u=t.get(e,n);u<i.near.d?(s+=u-i.near.d,o[i.near.i]+=i.near.d-i.seco.d):u<i.seco.d&&(o[i.near.i]+=u-i.seco.d)}const[i,u]=function(t){let e=0,r=t[0];for(let n=1;n<t.length;n++)t[n]<r&&(e=n,r=t[n]);return[e,r]}(o);return[u+s,i]}function ss(t,e){e.fill(0);for(let r=0;r<t.length;r++){const n=t[r];e[n.near.i]+=n.seco.d-n.near.d}}function is(t,e,r,n,o,s){let i=new es(n,s);for(let s=0;s<e.length;s++){const u=e[s];if(s===r||s===n)continue;const l=t.get(o,u);l<i.d&&(i=new es(s,l))}return i}function us(t,e,r,n,o){const s=t.len();if(!(n<e.length))throw new Error("invalid medoid number");if(!(o<s))throw new Error("invalid object number");e[n]=o;let i=0;for(let s=0;s<r.length;s++){const u=r[s];if(s===o){u.near.i!==n&&(u.seco=u.near.clone()),u.near=new es(n,0),i+=0;continue}const l=t.get(s,o);u.near.i===n?l<u.seco.d?u.near=new es(n,l):(u.near=u.seco.clone(),u.seco=is(t,e,u.near.i,n,s,l)):l<u.near.d?(u.seco=u.near.clone(),u.near=new es(n,l)):l<u.seco.d?u.seco=new es(n,l):u.seco.i===n&&(u.seco=is(t,e,u.near.i,n,s,l)),i+=u.near.d}return i}function ls(t,e){let r=0;for(let n=0;n<t.length;n++){let o=t[n]-e[n];r+=o*o}return Math.sqrt(r)}function as(t,e,r){let n=t.length,o=function(t){let e=Object.prototype.toString.call(t);return"[object Function]"===e||"[object AsyncFunction]"===e}(r)?r:ls,s=Array.from({length:n},(()=>new Float64Array(n)));for(let e=0;e<n;e++)for(let r=e+1;r<n;r++){let n=o(t[e],t[r]);s[e][r]=n,s[r][e]=n}var i;let u=ns(s,(i=e,Array.from({length:i},((t,e)=>e))),100),l={};return vn(u.assi,((t,e)=>{l[t]||(l[t]=[]),l[t].push(e)})),vo(l)}async function hs(t,e){let r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},n=wt(r,"funDist",null);return"simple"===wt(r,"useMethod","fasterPAM")?function(t,e,r){let n=new Map;vn(t,((t,e)=>{n.set(t,e)}));let o=No.Clusterer.getInstance(t,e,r).getClusteredData();return o=bo(o,(t=>bo(t,(t=>n.get(t))))),o}(t,e,n):as(t,e,n)}async function cs(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=wt(e,"kNumber");r=we(r)?Ne(r):2;let n=wt(e,"mode");"k-medoids"!==n&&"k-means"!==n&&(n="k-medoids");let o,s=wt(e,"seed",null),i=wt(e,"funDist",null),u=wt(e,"useMethod","fasterPAM");if("k-medoids"===n)o=await hs(t,r,{funDist:i,useMethod:u});else{if("k-means"!==n)throw new Error(`invalid mode[${n}]`);o=await async function(t,e){let r=wt(arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},"seed",null),n={};null!==r&&(n={seed:r});let o=Ho(t,e,n),s={};return vn(o.clusters,((t,e)=>{s[t]||(s[t]=[]),s[t].push(e)})),vo(s)}(t,r,{seed:s})}return o}async function fs(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!ce(t))throw new Error("data is not eff. array");let r=wt(e,"kNumber",null);we(r)||(r=2),r=Ne(r);let n=wt(e,"usePCA",null);me(n)||(n=!0);let o=wt(e,"nCompNIPALS",null);we(o)||(o=2),o=Ne(o);let s=wt(e,"mode",null),i=wt(e,"seed",null),u=wt(e,"funDist",null),l=wt(e,"useMethod","fasterPAM"),a="",h=se(t),c=0,f=0;if(vn(t,(t=>{!function(t){if(ue(t)){for(let e in t)return!0;return!1}return!1}(t)?ce(t)&&(f+=1):c+=1})),c===h)a="obj";else{if(f!==h)throw new Error("data is not of the same type");a="mat"}let m,g=null;"obj"===a?(g=function(t){let e={};vn(t,(t=>{vn(t,((t,r)=>{e[r]||(e[r]=0),0===e[r]&&we(t)&&(e[r]=1),1!==e[r]||we(t)||(e[r]=-1)}))}));let r=[];return vn(e,((t,e)=>{1===t&&r.push(e)})),r}(t),m=function(t,e){return bo(t,(t=>vo(Mo(t,e))))}(t,g)):m=t;let w=m;n&&(w=Lr(m,{nCompNIPALS:o}));let d=await cs(w,{kNumber:r,mode:s,seed:i,funDist:u,useMethod:l});return{keys:g,ginds:d,gmat:bo(d,(t=>bo(t,(t=>w[t])))),gltdt:bo(d,(e=>bo(e,(e=>t[e]))))}}let ms={PCA:async function(t){return Lr(t,arguments.length>1&&void 0!==arguments[1]?arguments[1]:{})},cluster:async function(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};try{return await fs(t,e)}catch(t){return Promise.reject(t)}}};return ms}));
6
+ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self)["w-cluster"]=e()}(this,(function(){"use strict";var t=Array.isArray,e="object"==typeof global&&global&&global.Object===Object&&global,r="object"==typeof self&&self&&self.Object===Object&&self,n=e||r||Function("return this")(),o=n.Symbol,s=Object.prototype,i=s.hasOwnProperty,u=s.toString,l=o?o.toStringTag:void 0;var a=Object.prototype.toString;var h="[object Null]",c="[object Undefined]",f=o?o.toStringTag:void 0;function m(t){return null==t?void 0===t?c:h:f&&f in Object(t)?function(t){var e=i.call(t,l),r=t[l];try{t[l]=void 0;var n=!0}catch(t){}var o=u.call(t);return n&&(e?t[l]=r:delete t[l]),o}(t):function(t){return a.call(t)}(t)}function g(t){return null!=t&&"object"==typeof t}var w="[object Symbol]";function d(t){return"symbol"==typeof t||g(t)&&m(t)==w}var p=/\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/,y=/^\w*$/;function b(e,r){if(t(e))return!1;var n=typeof e;return!("number"!=n&&"symbol"!=n&&"boolean"!=n&&null!=e&&!d(e))||(y.test(e)||!p.test(e)||null!=r&&e in Object(r))}function v(t){var e=typeof t;return null!=t&&("object"==e||"function"==e)}var M="[object AsyncFunction]",x="[object Function]",E="[object GeneratorFunction]",S="[object Proxy]";function _(t){if(!v(t))return!1;var e=m(t);return e==x||e==E||e==M||e==S}var j,A=n["__core-js_shared__"],R=(j=/[^.]+$/.exec(A&&A.keys&&A.keys.IE_PROTO||""))?"Symbol(src)_1."+j:"";var I=Function.prototype.toString;function C(t){if(null!=t){try{return I.call(t)}catch(t){}try{return t+""}catch(t){}}return""}var N=/^\[object .+?Constructor\]$/,k=Function.prototype,T=Object.prototype,F=k.toString,O=T.hasOwnProperty,V=RegExp("^"+F.call(O).replace(/[\\^$.*+?()[\]{}|]/g,"\\$&").replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g,"$1.*?")+"$");function z(t){return!(!v(t)||(e=t,R&&R in e))&&(_(t)?V:N).test(C(t));var e}function D(t,e){var r=function(t,e){return null==t?void 0:t[e]}(t,e);return z(r)?r:void 0}var P=D(Object,"create");var q=Object.prototype.hasOwnProperty;var $=Object.prototype.hasOwnProperty;function L(t){var e=-1,r=null==t?0:t.length;for(this.clear();++e<r;){var n=t[e];this.set(n[0],n[1])}}function U(t,e){return t===e||t!=t&&e!=e}function G(t,e){for(var r=t.length;r--;)if(U(t[r][0],e))return r;return-1}L.prototype.clear=function(){this.__data__=P?P(null):{},this.size=0},L.prototype.delete=function(t){var e=this.has(t)&&delete this.__data__[t];return this.size-=e?1:0,e},L.prototype.get=function(t){var e=this.__data__;if(P){var r=e[t];return"__lodash_hash_undefined__"===r?void 0:r}return q.call(e,t)?e[t]:void 0},L.prototype.has=function(t){var e=this.__data__;return P?void 0!==e[t]:$.call(e,t)},L.prototype.set=function(t,e){var r=this.__data__;return this.size+=this.has(t)?0:1,r[t]=P&&void 0===e?"__lodash_hash_undefined__":e,this};var B=Array.prototype.splice;function W(t){var e=-1,r=null==t?0:t.length;for(this.clear();++e<r;){var n=t[e];this.set(n[0],n[1])}}W.prototype.clear=function(){this.__data__=[],this.size=0},W.prototype.delete=function(t){var e=this.__data__,r=G(e,t);return!(r<0)&&(r==e.length-1?e.pop():B.call(e,r,1),--this.size,!0)},W.prototype.get=function(t){var e=this.__data__,r=G(e,t);return r<0?void 0:e[r][1]},W.prototype.has=function(t){return G(this.__data__,t)>-1},W.prototype.set=function(t,e){var r=this.__data__,n=G(r,t);return n<0?(++this.size,r.push([t,e])):r[n][1]=e,this};var K=D(n,"Map");function Q(t,e){var r,n,o=t.__data__;return("string"==(n=typeof(r=e))||"number"==n||"symbol"==n||"boolean"==n?"__proto__"!==r:null===r)?o["string"==typeof e?"string":"hash"]:o.map}function X(t){var e=-1,r=null==t?0:t.length;for(this.clear();++e<r;){var n=t[e];this.set(n[0],n[1])}}X.prototype.clear=function(){this.size=0,this.__data__={hash:new L,map:new(K||W),string:new L}},X.prototype.delete=function(t){var e=Q(this,t).delete(t);return this.size-=e?1:0,e},X.prototype.get=function(t){return Q(this,t).get(t)},X.prototype.has=function(t){return Q(this,t).has(t)},X.prototype.set=function(t,e){var r=Q(this,t),n=r.size;return r.set(t,e),this.size+=r.size==n?0:1,this};var Y="Expected a function";function J(t,e){if("function"!=typeof t||null!=e&&"function"!=typeof e)throw new TypeError(Y);var r=function(){var n=arguments,o=e?e.apply(this,n):n[0],s=r.cache;if(s.has(o))return s.get(o);var i=t.apply(this,n);return r.cache=s.set(o,i)||s,i};return r.cache=new(J.Cache||X),r}J.Cache=X;var H,Z,tt,et=/[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g,rt=/\\(\\)?/g,nt=(H=function(t){var e=[];return 46===t.charCodeAt(0)&&e.push(""),t.replace(et,(function(t,r,n,o){e.push(n?o.replace(rt,"$1"):r||t)})),e},Z=J(H,(function(t){return 500===tt.size&&tt.clear(),t})),tt=Z.cache,Z),ot=nt;function st(t,e){for(var r=-1,n=null==t?0:t.length,o=Array(n);++r<n;)o[r]=e(t[r],r,t);return o}var it=1/0,ut=o?o.prototype:void 0,lt=ut?ut.toString:void 0;function at(e){if("string"==typeof e)return e;if(t(e))return st(e,at)+"";if(d(e))return lt?lt.call(e):"";var r=e+"";return"0"==r&&1/e==-it?"-0":r}function ht(t){return null==t?"":at(t)}function ct(e,r){return t(e)?e:b(e,r)?[e]:ot(ht(e))}var ft=1/0;function mt(t){if("string"==typeof t||d(t))return t;var e=t+"";return"0"==e&&1/t==-ft?"-0":e}function gt(t,e){for(var r=0,n=(e=ct(e,t)).length;null!=t&&r<n;)t=t[mt(e[r++])];return r&&r==n?t:void 0}function wt(t,e,r){var n=null==t?void 0:gt(t,e);return void 0===n?r:n}var dt=Object.prototype;var pt=function(t,e){return function(r){return t(e(r))}}(Object.keys,Object),yt=pt,bt=Object.prototype.hasOwnProperty;function vt(t){if(r=(e=t)&&e.constructor,e!==("function"==typeof r&&r.prototype||dt))return yt(t);var e,r,n=[];for(var o in Object(t))bt.call(t,o)&&"constructor"!=o&&n.push(o);return n}var Mt=D(n,"DataView"),xt=D(n,"Promise"),Et=D(n,"Set"),St=D(n,"WeakMap"),_t="[object Map]",jt="[object Promise]",At="[object Set]",Rt="[object WeakMap]",It="[object DataView]",Ct=C(Mt),Nt=C(K),kt=C(xt),Tt=C(Et),Ft=C(St),Ot=m;(Mt&&Ot(new Mt(new ArrayBuffer(1)))!=It||K&&Ot(new K)!=_t||xt&&Ot(xt.resolve())!=jt||Et&&Ot(new Et)!=At||St&&Ot(new St)!=Rt)&&(Ot=function(t){var e=m(t),r="[object Object]"==e?t.constructor:void 0,n=r?C(r):"";if(n)switch(n){case Ct:return It;case Nt:return _t;case kt:return jt;case Tt:return At;case Ft:return Rt}return e});var Vt=Ot,zt=9007199254740991;function Dt(t){return"number"==typeof t&&t>-1&&t%1==0&&t<=zt}function Pt(t){return null!=t&&Dt(t.length)&&!_(t)}var qt="[object String]";function $t(t){return function(e){return null==e?void 0:e[t]}}var Lt=$t("length"),Ut=RegExp("[\\u200d\\ud800-\\udfff\\u0300-\\u036f\\ufe20-\\ufe2f\\u20d0-\\u20ff\\ufe0e\\ufe0f]");var Gt="\\ud800-\\udfff",Bt="["+Gt+"]",Wt="[\\u0300-\\u036f\\ufe20-\\ufe2f\\u20d0-\\u20ff]",Kt="\\ud83c[\\udffb-\\udfff]",Qt="[^"+Gt+"]",Xt="(?:\\ud83c[\\udde6-\\uddff]){2}",Yt="[\\ud800-\\udbff][\\udc00-\\udfff]",Jt="(?:"+Wt+"|"+Kt+")"+"?",Ht="[\\ufe0e\\ufe0f]?",Zt=Ht+Jt+("(?:\\u200d(?:"+[Qt,Xt,Yt].join("|")+")"+Ht+Jt+")*"),te="(?:"+[Qt+Wt+"?",Wt,Xt,Yt,Bt].join("|")+")",ee=RegExp(Kt+"(?="+Kt+")|"+te+Zt,"g");function re(t){return function(t){return Ut.test(t)}(t)?function(t){for(var e=ee.lastIndex=0;ee.test(t);)++e;return e}(t):Lt(t)}var ne="[object Map]",oe="[object Set]";function se(e){if(null==e)return 0;if(Pt(e))return"string"==typeof(r=e)||!t(r)&&g(r)&&m(r)==qt?re(e):e.length;var r,n=Vt(e);return n==ne||n==oe?e.size:vt(e).length}function ie(t){return"[object Array]"===Object.prototype.toString.call(t)}function ue(t){return"[object Object]"===Object.prototype.toString.call(t)}function le(t){return"[object String]"===Object.prototype.toString.call(t)}function ae(t){return t!=t}function he(t){return!!function(t){return"[object Undefined]"===Object.prototype.toString.call(t)}(t)||(!!function(t){return"[object Null]"===Object.prototype.toString.call(t)}(t)||(!!function(t){if(ue(t)){for(let e in t)return!1;return!0}return!1}(t)||(!!function(t){return!(!le(t)||""!==t)}(t)||(!!function(t){return!!ie(t)&&0===t.length}(t)||!!ae(t)))))}function ce(t){return!!ie(t)&&(0!==t.length&&(1!==t.length||!he(t[0])))}var fe="[object Boolean]";function me(t){return!0===(e=t)||!1===e||g(e)&&m(e)==fe;var e}function ge(t){return!(!le(t)||""===t)}function we(t){let e=!1;if(ge(t))e=!isNaN(Number(t));else if(function(t){return"[object Number]"===Object.prototype.toString.call(t)}(t)){if(ae(t))return!1;e=!0}return e}var de=/\s/;var pe=/^\s+/;function ye(t){return t?t.slice(0,function(t){for(var e=t.length;e--&&de.test(t.charAt(e)););return e}(t)+1).replace(pe,""):t}var be=NaN,ve=/^[-+]0x[0-9a-f]+$/i,Me=/^0b[01]+$/i,xe=/^0o[0-7]+$/i,Ee=parseInt;function Se(t){if("number"==typeof t)return t;if(d(t))return be;if(v(t)){var e="function"==typeof t.valueOf?t.valueOf():t;t=v(e)?e+"":e}if("string"!=typeof t)return 0===t?t:+t;t=ye(t);var r=Me.test(t);return r||xe.test(t)?Ee(t.slice(2),r?2:8):ve.test(t)?be:+t}var _e=1/0,je=17976931348623157e292;function Ae(t){return t?(t=Se(t))===_e||t===-_e?(t<0?-1:1)*je:t==t?t:0:0===t?t:0}var Re=n.isFinite,Ie=Math.min;var Ce=function(t){var e=Math[t];return function(t,r){if(t=Se(t),r=null==r?0:Ie(function(t){var e=Ae(t),r=e%1;return e==e?r?e-r:e:0}(r),292),r&&Re(t)){var n=(ht(t)+"e").split("e");return+((n=(ht(e(n[0]+"e"+(+n[1]+r)))+"e").split("e"))[0]+"e"+(+n[1]-r))}return e(t)}}("round"),Ne=Ce;function ke(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=wt(e,"useClampSafe",null);me(r)||(r=!1);let n=wt(e,"useLimitSafe",null);me(n)||(n=!1);let o=wt(e,"returnWithStateAndMsg",null);me(o)||(o=!1);let s=t=>o?{state:"success",msg:t}:t,i=t=>{if(o)return{state:"error",msg:t};throw new Error(t)};if(!we(t))return s(0);let u=null;try{t=function(t){return we(t)?Ae(t):0}(t),u=Ne(t),r&&(u>Number.MAX_SAFE_INTEGER?u=Number.MAX_SAFE_INTEGER:u<Number.MIN_SAFE_INTEGER&&(u=Number.MIN_SAFE_INTEGER))}catch(t){return i(t.toString())}return n&&!Number.isSafeInteger(Number(u))?i(`v[${t}] is not a safe integer`):("0"===String(u)&&(u=0),s(u))}function Te(t,e,r){if(!e.has(t))throw new TypeError("attempted to "+r+" private field on non-instance");return e.get(t)}function Fe(t,e,r){return function(t,e,r){if(e.set)e.set.call(t,r);else{if(!e.writable)throw new TypeError("attempted to set read only private field");e.value=r}}(t,Te(t,e,"set"),r),r}function Oe(t,e){return function(t,e){return e.get?e.get.call(t):e.value}(t,Te(t,e,"get"))}function Ve(t){return Ve="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},Ve(t)}function ze(t){var e=function(t,e){if("object"!==Ve(t)||null===t)return t;var r=t[Symbol.toPrimitive];if(void 0!==r){var n=r.call(t,e||"default");if("object"!==Ve(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"===Ve(e)?e:String(e)}function De(t,e,r){return(e=ze(e))in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function Pe(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}var qe={};function $e(t,e){if(e.has(t))throw new TypeError("Cannot initialize the same private elements twice on an object")}function Le(t,e,r){if(!e.has(t))throw new TypeError("attempted to get private field on non-instance");return r}Object.defineProperty(qe,"__esModule",{value:!0});const Ue=Object.prototype.toString;function Ge(t){const e=Ue.call(t);return e.endsWith("Array]")&&!e.includes("Big")}function Be(t){let e,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!Ge(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");if(void 0!==r.output){if(!Ge(r.output))throw new TypeError("output option must be an array if specified");e=r.output}else e=new Array(t.length);const n=function(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!Ge(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");const{fromIndex:r=0,toIndex:n=t.length}=e;if(r<0||r>=t.length||!Number.isInteger(r))throw new Error("fromIndex must be a positive integer smaller than length");if(n<=r||n>t.length||!Number.isInteger(n))throw new Error("toIndex must be an integer greater than fromIndex and at most equal to length");let o=t[r];for(let e=r+1;e<n;e++)t[e]<o&&(o=t[e]);return o}(t),o=function(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!Ge(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");const{fromIndex:r=0,toIndex:n=t.length}=e;if(r<0||r>=t.length||!Number.isInteger(r))throw new Error("fromIndex must be a positive integer smaller than length");if(n<=r||n>t.length||!Number.isInteger(n))throw new Error("toIndex must be an integer greater than fromIndex and at most equal to length");let o=t[r];for(let e=r+1;e<n;e++)t[e]>o&&(o=t[e]);return o}(t);if(n===o)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");const{min:s=(r.autoMinMax?n:0),max:i=(r.autoMinMax?o:1)}=r;if(s>=i)throw new RangeError("min option must be smaller than max option");const u=(i-s)/(o-n);for(let r=0;r<t.length;r++)e[r]=(t[r]-n)*u+s;return e}const We=" ".repeat(2),Ke=" ".repeat(4);function Qe(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};const{maxRows:r=15,maxColumns:n=10,maxNumSize:o=8,padMinus:s="auto"}=e;return`${t.constructor.name} {\n${We}[\n${Ke}${function(t,e,r,n,o){const{rows:s,columns:i}=t,u=Math.min(s,e),l=Math.min(i,r),a=[];if("auto"===o){o=!1;t:for(let e=0;e<u;e++)for(let r=0;r<l;r++)if(t.get(e,r)<0){o=!0;break t}}for(let e=0;e<u;e++){let r=[];for(let s=0;s<l;s++)r.push(Xe(t.get(e,s),n,o));a.push(`${r.join(" ")}`)}l!==i&&(a[a.length-1]+=` ... ${i-r} more columns`);u!==s&&a.push(`... ${s-e} more rows`);return a.join(`\n${Ke}`)}(t,r,n,o,s)}\n${We}]\n${We}rows: ${t.rows}\n${We}columns: ${t.columns}\n}`}function Xe(t,e,r){return(t>=0&&r?` ${Ye(t,e-1)}`:Ye(t,e)).padEnd(e)}function Ye(t,e){let r=t.toString();if(r.length<=e)return r;let n=t.toFixed(e);if(n.length>e&&(n=t.toFixed(Math.max(0,e-(n.length-e)))),n.length<=e&&!n.startsWith("0.000")&&!n.startsWith("-0.000"))return n;let o=t.toExponential(e);return o.length>e&&(o=t.toExponential(Math.max(0,e-(o.length-e)))),o.slice(0)}function Je(t,e,r){let n=r?t.rows:t.rows-1;if(e<0||e>n)throw new RangeError("Row index out of range")}function He(t,e,r){let n=r?t.columns:t.columns-1;if(e<0||e>n)throw new RangeError("Column index out of range")}function Ze(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 tr(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 er(t,e){if(!Ge(e))throw new TypeError("row indices must be an array");for(let r=0;r<e.length;r++)if(e[r]<0||e[r]>=t.rows)throw new RangeError("row indices are out of range")}function rr(t,e){if(!Ge(e))throw new TypeError("column indices must be an array");for(let r=0;r<e.length;r++)if(e[r]<0||e[r]>=t.columns)throw new RangeError("column indices are out of range")}function nr(t,e,r,n,o){if(5!==arguments.length)throw new RangeError("expected 4 arguments");if(sr("startRow",e),sr("endRow",r),sr("startColumn",n),sr("endColumn",o),e>r||n>o||e<0||e>=t.rows||r<0||r>=t.rows||n<0||n>=t.columns||o<0||o>=t.columns)throw new RangeError("Submatrix indices are out of range")}function or(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,r=[];for(let n=0;n<t;n++)r.push(e);return r}function sr(t,e){if("number"!=typeof e)throw new TypeError(`${t} must be a number`)}function ir(t){if(t.isEmpty())throw new Error("Empty matrix has no elements to index")}class ur{static from1DArray(t,e,r){if(t*e!==r.length)throw new RangeError("data length does not match given dimensions");let n=new hr(t,e);for(let o=0;o<t;o++)for(let t=0;t<e;t++)n.set(o,t,r[o*e+t]);return n}static rowVector(t){let e=new hr(1,t.length);for(let r=0;r<t.length;r++)e.set(0,r,t[r]);return e}static columnVector(t){let e=new hr(t.length,1);for(let r=0;r<t.length;r++)e.set(r,0,t[r]);return e}static zeros(t,e){return new hr(t,e)}static ones(t,e){return new hr(t,e).fill(1)}static rand(t,e){let r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};if("object"!=typeof r)throw new TypeError("options must be an object");const{random:n=Math.random}=r;let o=new hr(t,e);for(let r=0;r<t;r++)for(let t=0;t<e;t++)o.set(r,t,n());return o}static randInt(t,e){let r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};if("object"!=typeof r)throw new TypeError("options must be an object");const{min:n=0,max:o=1e3,random:s=Math.random}=r;if(!Number.isInteger(n))throw new TypeError("min must be an integer");if(!Number.isInteger(o))throw new TypeError("max must be an integer");if(n>=o)throw new RangeError("min must be smaller than max");let i=o-n,u=new hr(t,e);for(let r=0;r<t;r++)for(let t=0;t<e;t++){let e=n+Math.round(s()*i);u.set(r,t,e)}return u}static eye(t,e,r){void 0===e&&(e=t),void 0===r&&(r=1);let n=Math.min(t,e),o=this.zeros(t,e);for(let t=0;t<n;t++)o.set(t,t,r);return o}static diag(t,e,r){let n=t.length;void 0===e&&(e=n),void 0===r&&(r=e);let o=Math.min(n,e,r),s=this.zeros(e,r);for(let e=0;e<o;e++)s.set(e,e,t[e]);return s}static min(t,e){t=this.checkMatrix(t),e=this.checkMatrix(e);let r=t.rows,n=t.columns,o=new hr(r,n);for(let s=0;s<r;s++)for(let r=0;r<n;r++)o.set(s,r,Math.min(t.get(s,r),e.get(s,r)));return o}static max(t,e){t=this.checkMatrix(t),e=this.checkMatrix(e);let r=t.rows,n=t.columns,o=new this(r,n);for(let s=0;s<r;s++)for(let r=0;r<n;r++)o.set(s,r,Math.max(t.get(s,r),e.get(s,r)));return o}static checkMatrix(t){return ur.isMatrix(t)?t:new hr(t)}static isMatrix(t){return null!=t&&"Matrix"===t.klass}get size(){return this.rows*this.columns}apply(t){if("function"!=typeof t)throw new TypeError("callback must be a function");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)t.call(this,e,r);return this}to1DArray(){let t=[];for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)t.push(this.get(e,r));return t}to2DArray(){let t=[];for(let e=0;e<this.rows;e++){t.push([]);for(let r=0;r<this.columns;r++)t[e].push(this.get(e,r))}return t}toJSON(){return this.to2DArray()}isRowVector(){return 1===this.rows}isColumnVector(){return 1===this.columns}isVector(){return 1===this.rows||1===this.columns}isSquare(){return this.rows===this.columns}isEmpty(){return 0===this.rows||0===this.columns}isSymmetric(){if(this.isSquare()){for(let t=0;t<this.rows;t++)for(let e=0;e<=t;e++)if(this.get(t,e)!==this.get(e,t))return!1;return!0}return!1}isDistance(){if(!this.isSymmetric())return!1;for(let t=0;t<this.rows;t++)if(0!==this.get(t,t))return!1;return!0}isEchelonForm(){let t=0,e=0,r=-1,n=!0,o=!1;for(;t<this.rows&&n;){for(e=0,o=!1;e<this.columns&&!1===o;)0===this.get(t,e)?e++:1===this.get(t,e)&&e>r?(o=!0,r=e):(n=!1,o=!0);t++}return n}isReducedEchelonForm(){let t=0,e=0,r=-1,n=!0,o=!1;for(;t<this.rows&&n;){for(e=0,o=!1;e<this.columns&&!1===o;)0===this.get(t,e)?e++:1===this.get(t,e)&&e>r?(o=!0,r=e):(n=!1,o=!0);for(let r=e+1;r<this.rows;r++)0!==this.get(t,r)&&(n=!1);t++}return n}echelonForm(){let t=this.clone(),e=0,r=0;for(;e<t.rows&&r<t.columns;){let n=e;for(let o=e;o<t.rows;o++)t.get(o,r)>t.get(n,r)&&(n=o);if(0===t.get(n,r))r++;else{t.swapRows(e,n);let o=t.get(e,r);for(let n=r;n<t.columns;n++)t.set(e,n,t.get(e,n)/o);for(let n=e+1;n<t.rows;n++){let o=t.get(n,r)/t.get(e,r);t.set(n,r,0);for(let s=r+1;s<t.columns;s++)t.set(n,s,t.get(n,s)-t.get(e,s)*o)}e++,r++}}return t}reducedEchelonForm(){let t=this.echelonForm(),e=t.columns,r=t.rows,n=r-1;for(;n>=0;)if(0===t.maxRow(n))n--;else{let o=0,s=!1;for(;o<r&&!1===s;)1===t.get(n,o)?s=!0:o++;for(let r=0;r<n;r++){let s=t.get(r,o);for(let i=o;i<e;i++){let e=t.get(r,i)-s*t.get(n,i);t.set(r,i,e)}}n--}return t}set(){throw new Error("set method is unimplemented")}get(){throw new Error("get method is unimplemented")}repeat(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};if("object"!=typeof t)throw new TypeError("options must be an object");const{rows:e=1,columns:r=1}=t;if(!Number.isInteger(e)||e<=0)throw new TypeError("rows must be a positive integer");if(!Number.isInteger(r)||r<=0)throw new TypeError("columns must be a positive integer");let n=new hr(this.rows*e,this.columns*r);for(let t=0;t<e;t++)for(let e=0;e<r;e++)n.setSubMatrix(this,this.rows*t,this.columns*e);return n}fill(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,t);return this}neg(){return this.mulS(-1)}getRow(t){Je(this,t);let e=[];for(let r=0;r<this.columns;r++)e.push(this.get(t,r));return e}getRowVector(t){return hr.rowVector(this.getRow(t))}setRow(t,e){Je(this,t),e=Ze(this,e);for(let r=0;r<this.columns;r++)this.set(t,r,e[r]);return this}swapRows(t,e){Je(this,t),Je(this,e);for(let r=0;r<this.columns;r++){let n=this.get(t,r);this.set(t,r,this.get(e,r)),this.set(e,r,n)}return this}getColumn(t){He(this,t);let e=[];for(let r=0;r<this.rows;r++)e.push(this.get(r,t));return e}getColumnVector(t){return hr.columnVector(this.getColumn(t))}setColumn(t,e){He(this,t),e=tr(this,e);for(let r=0;r<this.rows;r++)this.set(r,t,e[r]);return this}swapColumns(t,e){He(this,t),He(this,e);for(let r=0;r<this.rows;r++){let n=this.get(r,t);this.set(r,t,this.get(r,e)),this.set(r,e,n)}return this}addRowVector(t){t=Ze(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)+t[r]);return this}subRowVector(t){t=Ze(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)-t[r]);return this}mulRowVector(t){t=Ze(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)*t[r]);return this}divRowVector(t){t=Ze(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)/t[r]);return this}addColumnVector(t){t=tr(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)+t[e]);return this}subColumnVector(t){t=tr(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)-t[e]);return this}mulColumnVector(t){t=tr(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)*t[e]);return this}divColumnVector(t){t=tr(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)/t[e]);return this}mulRow(t,e){Je(this,t);for(let r=0;r<this.columns;r++)this.set(t,r,this.get(t,r)*e);return this}mulColumn(t,e){He(this,t);for(let r=0;r<this.rows;r++)this.set(r,t,this.get(r,t)*e);return this}max(t){if(this.isEmpty())return NaN;switch(t){case"row":{const t=new Array(this.rows).fill(Number.NEGATIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.get(e,r)>t[e]&&(t[e]=this.get(e,r));return t}case"column":{const t=new Array(this.columns).fill(Number.NEGATIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.get(e,r)>t[r]&&(t[r]=this.get(e,r));return t}case void 0:{let t=this.get(0,0);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.get(e,r)>t&&(t=this.get(e,r));return t}default:throw new Error(`invalid option: ${t}`)}}maxIndex(){ir(this);let t=this.get(0,0),e=[0,0];for(let r=0;r<this.rows;r++)for(let n=0;n<this.columns;n++)this.get(r,n)>t&&(t=this.get(r,n),e[0]=r,e[1]=n);return e}min(t){if(this.isEmpty())return NaN;switch(t){case"row":{const t=new Array(this.rows).fill(Number.POSITIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.get(e,r)<t[e]&&(t[e]=this.get(e,r));return t}case"column":{const t=new Array(this.columns).fill(Number.POSITIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.get(e,r)<t[r]&&(t[r]=this.get(e,r));return t}case void 0:{let t=this.get(0,0);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.get(e,r)<t&&(t=this.get(e,r));return t}default:throw new Error(`invalid option: ${t}`)}}minIndex(){ir(this);let t=this.get(0,0),e=[0,0];for(let r=0;r<this.rows;r++)for(let n=0;n<this.columns;n++)this.get(r,n)<t&&(t=this.get(r,n),e[0]=r,e[1]=n);return e}maxRow(t){if(Je(this,t),this.isEmpty())return NaN;let e=this.get(t,0);for(let r=1;r<this.columns;r++)this.get(t,r)>e&&(e=this.get(t,r));return e}maxRowIndex(t){Je(this,t),ir(this);let e=this.get(t,0),r=[t,0];for(let n=1;n<this.columns;n++)this.get(t,n)>e&&(e=this.get(t,n),r[1]=n);return r}minRow(t){if(Je(this,t),this.isEmpty())return NaN;let e=this.get(t,0);for(let r=1;r<this.columns;r++)this.get(t,r)<e&&(e=this.get(t,r));return e}minRowIndex(t){Je(this,t),ir(this);let e=this.get(t,0),r=[t,0];for(let n=1;n<this.columns;n++)this.get(t,n)<e&&(e=this.get(t,n),r[1]=n);return r}maxColumn(t){if(He(this,t),this.isEmpty())return NaN;let e=this.get(0,t);for(let r=1;r<this.rows;r++)this.get(r,t)>e&&(e=this.get(r,t));return e}maxColumnIndex(t){He(this,t),ir(this);let e=this.get(0,t),r=[0,t];for(let n=1;n<this.rows;n++)this.get(n,t)>e&&(e=this.get(n,t),r[0]=n);return r}minColumn(t){if(He(this,t),this.isEmpty())return NaN;let e=this.get(0,t);for(let r=1;r<this.rows;r++)this.get(r,t)<e&&(e=this.get(r,t));return e}minColumnIndex(t){He(this,t),ir(this);let e=this.get(0,t),r=[0,t];for(let n=1;n<this.rows;n++)this.get(n,t)<e&&(e=this.get(n,t),r[0]=n);return r}diag(){let t=Math.min(this.rows,this.columns),e=[];for(let r=0;r<t;r++)e.push(this.get(r,r));return e}norm(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"frobenius";switch(t){case"max":return this.max();case"frobenius":return Math.sqrt(this.dot(this));default:throw new RangeError(`unknown norm type: ${t}`)}}cumulativeSum(){let t=0;for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)t+=this.get(e,r),this.set(e,r,t);return this}dot(t){ur.isMatrix(t)&&(t=t.to1DArray());let e=this.to1DArray();if(e.length!==t.length)throw new RangeError("vectors do not have the same size");let r=0;for(let n=0;n<e.length;n++)r+=e[n]*t[n];return r}mmul(t){t=hr.checkMatrix(t);let e=this.rows,r=this.columns,n=t.columns,o=new hr(e,n),s=new Float64Array(r);for(let i=0;i<n;i++){for(let e=0;e<r;e++)s[e]=t.get(e,i);for(let t=0;t<e;t++){let e=0;for(let n=0;n<r;n++)e+=this.get(t,n)*s[n];o.set(t,i,e)}}return o}mpow(t){if(!this.isSquare())throw new RangeError("Matrix must be square");if(!Number.isInteger(t)||t<0)throw new RangeError("Exponent must be a non-negative integer");let e=hr.eye(this.rows),r=this;for(let n=t;n>=1;n/=2)0!=(1&n)&&(e=e.mmul(r)),r=r.mmul(r);return e}strassen2x2(t){t=hr.checkMatrix(t);let e=new hr(2,2);const r=this.get(0,0),n=t.get(0,0),o=this.get(0,1),s=t.get(0,1),i=this.get(1,0),u=t.get(1,0),l=this.get(1,1),a=t.get(1,1),h=(r+l)*(n+a),c=(i+l)*n,f=r*(s-a),m=l*(u-n),g=(r+o)*a,w=h+m-g+(o-l)*(u+a),d=f+g,p=c+m,y=h-c+f+(i-r)*(n+s);return e.set(0,0,w),e.set(0,1,d),e.set(1,0,p),e.set(1,1,y),e}strassen3x3(t){t=hr.checkMatrix(t);let e=new hr(3,3);const r=this.get(0,0),n=this.get(0,1),o=this.get(0,2),s=this.get(1,0),i=this.get(1,1),u=this.get(1,2),l=this.get(2,0),a=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),w=t.get(1,1),d=t.get(1,2),p=t.get(2,0),y=t.get(2,1),b=t.get(2,2),v=(r-s)*(-f+w),M=(-r+s+i)*(c-f+w),x=(s+i)*(-c+f),E=r*c,S=(-r+l+a)*(c-m+d),_=(-r+l)*(m-d),j=(l+a)*(-c+m),A=(-o+a+h)*(w+p-y),R=(o-h)*(w-y),I=o*p,C=(a+h)*(-p+y),N=(-o+i+u)*(d+p-b),k=(o-u)*(d-b),T=(i+u)*(-p+b),F=E+I+n*g,O=(r+n+o-s-i-a-h)*w+M+x+E+A+I+C,V=E+S+j+(r+n+o-i-u-l-a)*d+I+N+T,z=v+i*(-c+f+g-w-d-p+b)+M+E+I+N+k,D=v+M+x+E+u*y,P=I+N+k+T+s*m,q=E+S+_+a*(-c+m+g-w-d-p+y)+A+R+I,$=A+R+I+C+l*f,L=E+S+_+j+h*b;return e.set(0,0,F),e.set(0,1,O),e.set(0,2,V),e.set(1,0,z),e.set(1,1,D),e.set(1,2,P),e.set(2,0,q),e.set(2,1,$),e.set(2,2,L),e}mmulStrassen(t){t=hr.checkMatrix(t);let e=this.clone(),r=e.rows,n=e.columns,o=t.rows,s=t.columns;function i(t,e,r){let n=t.rows,o=t.columns;if(n===e&&o===r)return t;{let n=ur.zeros(e,r);return n=n.setSubMatrix(t,0,0),n}}n!==o&&console.warn(`Multiplying ${r} x ${n} and ${o} x ${s} matrix: dimensions do not match.`);let u=Math.max(r,o),l=Math.max(n,s);return e=i(e,u,l),function t(e,r,n,o){if(n<=512||o<=512)return e.mmul(r);n%2==1&&o%2==1?(e=i(e,n+1,o+1),r=i(r,n+1,o+1)):n%2==1?(e=i(e,n+1,o),r=i(r,n+1,o)):o%2==1&&(e=i(e,n,o+1),r=i(r,n,o+1));let s=parseInt(e.rows/2,10),u=parseInt(e.columns/2,10),l=e.subMatrix(0,s-1,0,u-1),a=r.subMatrix(0,s-1,0,u-1),h=e.subMatrix(0,s-1,u,e.columns-1),c=r.subMatrix(0,s-1,u,r.columns-1),f=e.subMatrix(s,e.rows-1,0,u-1),m=r.subMatrix(s,r.rows-1,0,u-1),g=e.subMatrix(s,e.rows-1,u,e.columns-1),w=r.subMatrix(s,r.rows-1,u,r.columns-1),d=t(ur.add(l,g),ur.add(a,w),s,u),p=t(ur.add(f,g),a,s,u),y=t(l,ur.sub(c,w),s,u),b=t(g,ur.sub(m,a),s,u),v=t(ur.add(l,h),w,s,u),M=t(ur.sub(f,l),ur.add(a,c),s,u),x=t(ur.sub(h,g),ur.add(m,w),s,u),E=ur.add(d,b);E.sub(v),E.add(x);let S=ur.add(y,v),_=ur.add(p,b),j=ur.sub(d,p);j.add(y),j.add(M);let A=ur.zeros(2*E.rows,2*E.columns);return A=A.setSubMatrix(E,0,0),A=A.setSubMatrix(S,E.rows,0),A=A.setSubMatrix(_,0,E.columns),A=A.setSubMatrix(j,E.rows,E.columns),A.subMatrix(0,n-1,0,o-1)}(e,t=i(t,u,l),u,l)}scaleRows(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};if("object"!=typeof t)throw new TypeError("options must be an object");const{min:e=0,max:r=1}=t;if(!Number.isFinite(e))throw new TypeError("min must be a number");if(!Number.isFinite(r))throw new TypeError("max must be a number");if(e>=r)throw new RangeError("min must be smaller than max");let n=new hr(this.rows,this.columns);for(let t=0;t<this.rows;t++){const o=this.getRow(t);o.length>0&&Be(o,{min:e,max:r,output:o}),n.setRow(t,o)}return n}scaleColumns(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};if("object"!=typeof t)throw new TypeError("options must be an object");const{min:e=0,max:r=1}=t;if(!Number.isFinite(e))throw new TypeError("min must be a number");if(!Number.isFinite(r))throw new TypeError("max must be a number");if(e>=r)throw new RangeError("min must be smaller than max");let n=new hr(this.rows,this.columns);for(let t=0;t<this.columns;t++){const o=this.getColumn(t);o.length&&Be(o,{min:e,max:r,output:o}),n.setColumn(t,o)}return n}flipRows(){const t=Math.ceil(this.columns/2);for(let e=0;e<this.rows;e++)for(let r=0;r<t;r++){let t=this.get(e,r),n=this.get(e,this.columns-1-r);this.set(e,r,n),this.set(e,this.columns-1-r,t)}return this}flipColumns(){const t=Math.ceil(this.rows/2);for(let e=0;e<this.columns;e++)for(let r=0;r<t;r++){let t=this.get(r,e),n=this.get(this.rows-1-r,e);this.set(r,e,n),this.set(this.rows-1-r,e,t)}return this}kroneckerProduct(t){t=hr.checkMatrix(t);let e=this.rows,r=this.columns,n=t.rows,o=t.columns,s=new hr(e*n,r*o);for(let i=0;i<e;i++)for(let e=0;e<r;e++)for(let r=0;r<n;r++)for(let u=0;u<o;u++)s.set(n*i+r,o*e+u,this.get(i,e)*t.get(r,u));return s}kroneckerSum(t){if(t=hr.checkMatrix(t),!this.isSquare()||!t.isSquare())throw new Error("Kronecker Sum needs two Square Matrices");let e=this.rows,r=t.rows,n=this.kroneckerProduct(hr.eye(r,r)),o=hr.eye(e,e).kroneckerProduct(t);return n.add(o)}transpose(){let t=new hr(this.columns,this.rows);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)t.set(r,e,this.get(e,r));return t}sortRows(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:lr;for(let e=0;e<this.rows;e++)this.setRow(e,this.getRow(e).sort(t));return this}sortColumns(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:lr;for(let e=0;e<this.columns;e++)this.setColumn(e,this.getColumn(e).sort(t));return this}subMatrix(t,e,r,n){nr(this,t,e,r,n);let o=new hr(e-t+1,n-r+1);for(let s=t;s<=e;s++)for(let e=r;e<=n;e++)o.set(s-t,e-r,this.get(s,e));return o}subMatrixRow(t,e,r){if(void 0===e&&(e=0),void 0===r&&(r=this.columns-1),e>r||e<0||e>=this.columns||r<0||r>=this.columns)throw new RangeError("Argument out of range");let n=new hr(t.length,r-e+1);for(let o=0;o<t.length;o++)for(let s=e;s<=r;s++){if(t[o]<0||t[o]>=this.rows)throw new RangeError(`Row index out of range: ${t[o]}`);n.set(o,s-e,this.get(t[o],s))}return n}subMatrixColumn(t,e,r){if(void 0===e&&(e=0),void 0===r&&(r=this.rows-1),e>r||e<0||e>=this.rows||r<0||r>=this.rows)throw new RangeError("Argument out of range");let n=new hr(r-e+1,t.length);for(let o=0;o<t.length;o++)for(let s=e;s<=r;s++){if(t[o]<0||t[o]>=this.columns)throw new RangeError(`Column index out of range: ${t[o]}`);n.set(s-e,o,this.get(s,t[o]))}return n}setSubMatrix(t,e,r){if((t=hr.checkMatrix(t)).isEmpty())return this;nr(this,e,e+t.rows-1,r,r+t.columns-1);for(let n=0;n<t.rows;n++)for(let o=0;o<t.columns;o++)this.set(e+n,r+o,t.get(n,o));return this}selection(t,e){er(this,t),rr(this,e);let r=new hr(t.length,e.length);for(let n=0;n<t.length;n++){let o=t[n];for(let t=0;t<e.length;t++){let s=e[t];r.set(n,t,this.get(o,s))}}return r}trace(){let t=Math.min(this.rows,this.columns),e=0;for(let r=0;r<t;r++)e+=this.get(r,r);return e}clone(){return this.constructor.copy(this,new hr(this.rows,this.columns))}static copy(t,e){for(const[r,n,o]of t.entries())e.set(r,n,o);return e}sum(t){switch(t){case"row":return function(t){let e=or(t.rows);for(let r=0;r<t.rows;++r)for(let n=0;n<t.columns;++n)e[r]+=t.get(r,n);return e}(this);case"column":return function(t){let e=or(t.columns);for(let r=0;r<t.rows;++r)for(let n=0;n<t.columns;++n)e[n]+=t.get(r,n);return e}(this);case void 0:return function(t){let e=0;for(let r=0;r<t.rows;r++)for(let n=0;n<t.columns;n++)e+=t.get(r,n);return e}(this);default:throw new Error(`invalid option: ${t}`)}}product(t){switch(t){case"row":return function(t){let e=or(t.rows,1);for(let r=0;r<t.rows;++r)for(let n=0;n<t.columns;++n)e[r]*=t.get(r,n);return e}(this);case"column":return function(t){let e=or(t.columns,1);for(let r=0;r<t.rows;++r)for(let n=0;n<t.columns;++n)e[n]*=t.get(r,n);return e}(this);case void 0:return function(t){let e=1;for(let r=0;r<t.rows;r++)for(let n=0;n<t.columns;n++)e*=t.get(r,n);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){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if("object"==typeof t&&(e=t,t=void 0),"object"!=typeof e)throw new TypeError("options must be an object");const{unbiased:r=!0,mean:n=this.mean(t)}=e;if("boolean"!=typeof r)throw new TypeError("unbiased must be a boolean");switch(t){case"row":if(!Ge(n))throw new TypeError("mean must be an array");return function(t,e,r){const n=t.rows,o=t.columns,s=[];for(let i=0;i<n;i++){let n=0,u=0,l=0;for(let e=0;e<o;e++)l=t.get(i,e)-r[i],n+=l,u+=l*l;e?s.push((u-n*n/o)/(o-1)):s.push((u-n*n/o)/o)}return s}(this,r,n);case"column":if(!Ge(n))throw new TypeError("mean must be an array");return function(t,e,r){const n=t.rows,o=t.columns,s=[];for(let i=0;i<o;i++){let o=0,u=0,l=0;for(let e=0;e<n;e++)l=t.get(e,i)-r[i],o+=l,u+=l*l;e?s.push((u-o*o/n)/(n-1)):s.push((u-o*o/n)/n)}return s}(this,r,n);case void 0:if("number"!=typeof n)throw new TypeError("mean must be a number");return function(t,e,r){const n=t.rows,o=t.columns,s=n*o;let i=0,u=0,l=0;for(let e=0;e<n;e++)for(let n=0;n<o;n++)l=t.get(e,n)-r,i+=l,u+=l*l;return e?(u-i*i/s)/(s-1):(u-i*i/s)/s}(this,r,n);default:throw new Error(`invalid option: ${t}`)}}standardDeviation(t,e){"object"==typeof t&&(e=t,t=void 0);const r=this.variance(t,e);if(void 0===t)return Math.sqrt(r);for(let t=0;t<r.length;t++)r[t]=Math.sqrt(r[t]);return r}center(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if("object"==typeof t&&(e=t,t=void 0),"object"!=typeof e)throw new TypeError("options must be an object");const{center:r=this.mean(t)}=e;switch(t){case"row":if(!Ge(r))throw new TypeError("center must be an array");return function(t,e){for(let r=0;r<t.rows;r++)for(let n=0;n<t.columns;n++)t.set(r,n,t.get(r,n)-e[r])}(this,r),this;case"column":if(!Ge(r))throw new TypeError("center must be an array");return function(t,e){for(let r=0;r<t.rows;r++)for(let n=0;n<t.columns;n++)t.set(r,n,t.get(r,n)-e[n])}(this,r),this;case void 0:if("number"!=typeof r)throw new TypeError("center must be a number");return function(t,e){for(let r=0;r<t.rows;r++)for(let n=0;n<t.columns;n++)t.set(r,n,t.get(r,n)-e)}(this,r),this;default:throw new Error(`invalid option: ${t}`)}}scale(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if("object"==typeof t&&(e=t,t=void 0),"object"!=typeof e)throw new TypeError("options must be an object");let r=e.scale;switch(t){case"row":if(void 0===r)r=function(t){const e=[];for(let r=0;r<t.rows;r++){let n=0;for(let e=0;e<t.columns;e++)n+=t.get(r,e)**2/(t.columns-1);e.push(Math.sqrt(n))}return e}(this);else if(!Ge(r))throw new TypeError("scale must be an array");return function(t,e){for(let r=0;r<t.rows;r++)for(let n=0;n<t.columns;n++)t.set(r,n,t.get(r,n)/e[r])}(this,r),this;case"column":if(void 0===r)r=function(t){const e=[];for(let r=0;r<t.columns;r++){let n=0;for(let e=0;e<t.rows;e++)n+=t.get(e,r)**2/(t.rows-1);e.push(Math.sqrt(n))}return e}(this);else if(!Ge(r))throw new TypeError("scale must be an array");return function(t,e){for(let r=0;r<t.rows;r++)for(let n=0;n<t.columns;n++)t.set(r,n,t.get(r,n)/e[n])}(this,r),this;case void 0:if(void 0===r)r=function(t){const e=t.size-1;let r=0;for(let n=0;n<t.columns;n++)for(let o=0;o<t.rows;o++)r+=t.get(o,n)**2/e;return Math.sqrt(r)}(this);else if("number"!=typeof r)throw new TypeError("scale must be a number");return function(t,e){for(let r=0;r<t.rows;r++)for(let n=0;n<t.columns;n++)t.set(r,n,t.get(r,n)/e)}(this,r),this;default:throw new Error(`invalid option: ${t}`)}}toString(t){return Qe(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 lr(t,e){return t-e}ur.prototype.klass="Matrix","undefined"!=typeof Symbol&&(ur.prototype[Symbol.for("nodejs.util.inspect.custom")]=function(){return Qe(this)}),ur.random=ur.rand,ur.randomInt=ur.randInt,ur.diagonal=ur.diag,ur.prototype.diagonal=ur.prototype.diag,ur.identity=ur.eye,ur.prototype.negate=ur.prototype.neg,ur.prototype.tensorProduct=ur.prototype.kroneckerProduct;var ar=new WeakSet;let hr=class t extends ur{constructor(e,r){var n,o;if(super(),$e(n=this,o=ar),o.add(n),De(this,"data",void 0),t.isMatrix(e))Le(this,ar,cr).call(this,e.rows,e.columns),t.copy(e,this);else if(Number.isInteger(e)&&e>=0)Le(this,ar,cr).call(this,e,r);else{if(!Ge(e))throw new TypeError("First argument must be a positive number or an array");{const t=e;if("number"!=typeof(r=(e=t.length)?t[0].length:0))throw new TypeError("Data must be a 2D array with at least one element");this.data=[];for(let n=0;n<e;n++){if(t[n].length!==r)throw new RangeError("Inconsistent array dimensions");if(!t[n].every((t=>"number"==typeof t)))throw new TypeError("Input data contains non-numeric values");this.data.push(Float64Array.from(t[n]))}this.rows=e,this.columns=r}}}set(t,e,r){return this.data[t][e]=r,this}get(t,e){return this.data[t][e]}removeRow(t){return Je(this,t),this.data.splice(t,1),this.rows-=1,this}addRow(t,e){return void 0===e&&(e=t,t=this.rows),Je(this,t,!0),e=Float64Array.from(Ze(this,e)),this.data.splice(t,0,e),this.rows+=1,this}removeColumn(t){He(this,t);for(let e=0;e<this.rows;e++){const r=new Float64Array(this.columns-1);for(let n=0;n<t;n++)r[n]=this.data[e][n];for(let n=t+1;n<this.columns;n++)r[n-1]=this.data[e][n];this.data[e]=r}return this.columns-=1,this}addColumn(t,e){void 0===e&&(e=t,t=this.columns),He(this,t,!0),e=tr(this,e);for(let r=0;r<this.rows;r++){const n=new Float64Array(this.columns+1);let o=0;for(;o<t;o++)n[o]=this.data[r][o];for(n[o++]=e[r];o<this.columns+1;o++)n[o]=this.data[r][o-1];this.data[r]=n}return this.columns+=1,this}};function cr(t,e){if(this.data=[],!(Number.isInteger(e)&&e>=0))throw new TypeError("nColumns must be a positive integer");for(let r=0;r<t;r++)this.data.push(new Float64Array(e));this.rows=t,this.columns=e}!function(t,e){t.prototype.add=function(t){return"number"==typeof t?this.addS(t):this.addM(t)},t.prototype.addS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)+t);return this},t.prototype.addM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)+t.get(e,r));return this},t.add=function(t,r){return new e(t).add(r)},t.prototype.sub=function(t){return"number"==typeof t?this.subS(t):this.subM(t)},t.prototype.subS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)-t);return this},t.prototype.subM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)-t.get(e,r));return this},t.sub=function(t,r){return new e(t).sub(r)},t.prototype.subtract=t.prototype.sub,t.prototype.subtractS=t.prototype.subS,t.prototype.subtractM=t.prototype.subM,t.subtract=t.sub,t.prototype.mul=function(t){return"number"==typeof t?this.mulS(t):this.mulM(t)},t.prototype.mulS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)*t);return this},t.prototype.mulM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)*t.get(e,r));return this},t.mul=function(t,r){return new e(t).mul(r)},t.prototype.multiply=t.prototype.mul,t.prototype.multiplyS=t.prototype.mulS,t.prototype.multiplyM=t.prototype.mulM,t.multiply=t.mul,t.prototype.div=function(t){return"number"==typeof t?this.divS(t):this.divM(t)},t.prototype.divS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)/t);return this},t.prototype.divM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)/t.get(e,r));return this},t.div=function(t,r){return new e(t).div(r)},t.prototype.divide=t.prototype.div,t.prototype.divideS=t.prototype.divS,t.prototype.divideM=t.prototype.divM,t.divide=t.div,t.prototype.mod=function(t){return"number"==typeof t?this.modS(t):this.modM(t)},t.prototype.modS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)%t);return this},t.prototype.modM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)%t.get(e,r));return this},t.mod=function(t,r){return new e(t).mod(r)},t.prototype.modulus=t.prototype.mod,t.prototype.modulusS=t.prototype.modS,t.prototype.modulusM=t.prototype.modM,t.modulus=t.mod,t.prototype.and=function(t){return"number"==typeof t?this.andS(t):this.andM(t)},t.prototype.andS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)&t);return this},t.prototype.andM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)&t.get(e,r));return this},t.and=function(t,r){return new e(t).and(r)},t.prototype.or=function(t){return"number"==typeof t?this.orS(t):this.orM(t)},t.prototype.orS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)|t);return this},t.prototype.orM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)|t.get(e,r));return this},t.or=function(t,r){return new e(t).or(r)},t.prototype.xor=function(t){return"number"==typeof t?this.xorS(t):this.xorM(t)},t.prototype.xorS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)^t);return this},t.prototype.xorM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)^t.get(e,r));return this},t.xor=function(t,r){return new e(t).xor(r)},t.prototype.leftShift=function(t){return"number"==typeof t?this.leftShiftS(t):this.leftShiftM(t)},t.prototype.leftShiftS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)<<t);return this},t.prototype.leftShiftM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)<<t.get(e,r));return this},t.leftShift=function(t,r){return new e(t).leftShift(r)},t.prototype.signPropagatingRightShift=function(t){return"number"==typeof t?this.signPropagatingRightShiftS(t):this.signPropagatingRightShiftM(t)},t.prototype.signPropagatingRightShiftS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)>>t);return this},t.prototype.signPropagatingRightShiftM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)>>t.get(e,r));return this},t.signPropagatingRightShift=function(t,r){return new e(t).signPropagatingRightShift(r)},t.prototype.rightShift=function(t){return"number"==typeof t?this.rightShiftS(t):this.rightShiftM(t)},t.prototype.rightShiftS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)>>>t);return this},t.prototype.rightShiftM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)>>>t.get(e,r));return this},t.rightShift=function(t,r){return new e(t).rightShift(r)},t.prototype.zeroFillRightShift=t.prototype.rightShift,t.prototype.zeroFillRightShiftS=t.prototype.rightShiftS,t.prototype.zeroFillRightShiftM=t.prototype.rightShiftM,t.zeroFillRightShift=t.rightShift,t.prototype.not=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,~this.get(t,e));return this},t.not=function(t){return new e(t).not()},t.prototype.abs=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.abs(this.get(t,e)));return this},t.abs=function(t){return new e(t).abs()},t.prototype.acos=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.acos(this.get(t,e)));return this},t.acos=function(t){return new e(t).acos()},t.prototype.acosh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.acosh(this.get(t,e)));return this},t.acosh=function(t){return new e(t).acosh()},t.prototype.asin=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.asin(this.get(t,e)));return this},t.asin=function(t){return new e(t).asin()},t.prototype.asinh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.asinh(this.get(t,e)));return this},t.asinh=function(t){return new e(t).asinh()},t.prototype.atan=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.atan(this.get(t,e)));return this},t.atan=function(t){return new e(t).atan()},t.prototype.atanh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.atanh(this.get(t,e)));return this},t.atanh=function(t){return new e(t).atanh()},t.prototype.cbrt=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.cbrt(this.get(t,e)));return this},t.cbrt=function(t){return new e(t).cbrt()},t.prototype.ceil=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.ceil(this.get(t,e)));return this},t.ceil=function(t){return new e(t).ceil()},t.prototype.clz32=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.clz32(this.get(t,e)));return this},t.clz32=function(t){return new e(t).clz32()},t.prototype.cos=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.cos(this.get(t,e)));return this},t.cos=function(t){return new e(t).cos()},t.prototype.cosh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.cosh(this.get(t,e)));return this},t.cosh=function(t){return new e(t).cosh()},t.prototype.exp=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.exp(this.get(t,e)));return this},t.exp=function(t){return new e(t).exp()},t.prototype.expm1=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.expm1(this.get(t,e)));return this},t.expm1=function(t){return new e(t).expm1()},t.prototype.floor=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.floor(this.get(t,e)));return this},t.floor=function(t){return new e(t).floor()},t.prototype.fround=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.fround(this.get(t,e)));return this},t.fround=function(t){return new e(t).fround()},t.prototype.log=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.log(this.get(t,e)));return this},t.log=function(t){return new e(t).log()},t.prototype.log1p=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.log1p(this.get(t,e)));return this},t.log1p=function(t){return new e(t).log1p()},t.prototype.log10=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.log10(this.get(t,e)));return this},t.log10=function(t){return new e(t).log10()},t.prototype.log2=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.log2(this.get(t,e)));return this},t.log2=function(t){return new e(t).log2()},t.prototype.round=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.round(this.get(t,e)));return this},t.round=function(t){return new e(t).round()},t.prototype.sign=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.sign(this.get(t,e)));return this},t.sign=function(t){return new e(t).sign()},t.prototype.sin=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.sin(this.get(t,e)));return this},t.sin=function(t){return new e(t).sin()},t.prototype.sinh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.sinh(this.get(t,e)));return this},t.sinh=function(t){return new e(t).sinh()},t.prototype.sqrt=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.sqrt(this.get(t,e)));return this},t.sqrt=function(t){return new e(t).sqrt()},t.prototype.tan=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.tan(this.get(t,e)));return this},t.tan=function(t){return new e(t).tan()},t.prototype.tanh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.tanh(this.get(t,e)));return this},t.tanh=function(t){return new e(t).tanh()},t.prototype.trunc=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.trunc(this.get(t,e)));return this},t.trunc=function(t){return new e(t).trunc()},t.pow=function(t,r){return new e(t).pow(r)},t.prototype.pow=function(t){return"number"==typeof t?this.powS(t):this.powM(t)},t.prototype.powS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)**t);return this},t.prototype.powM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)**t.get(e,r));return this}}(ur,hr);var fr=new WeakMap;class mr extends ur{get size(){return Oe(this,fr).size}get rows(){return Oe(this,fr).rows}get columns(){return Oe(this,fr).columns}get diagonalSize(){return this.rows}static isSymmetricMatrix(t){return hr.isMatrix(t)&&"SymmetricMatrix"===t.klassType}static zeros(t){return new this(t)}static ones(t){return new this(t).fill(1)}constructor(t){var e,r,n;if(super(),n={writable:!0,value:void 0},$e(e=this,r=fr),r.set(e,n),hr.isMatrix(t)){if(!t.isSymmetric())throw new TypeError("not symmetric data");Fe(this,fr,hr.copy(t,new hr(t.rows,t.rows)))}else if(Number.isInteger(t)&&t>=0)Fe(this,fr,new hr(t,t));else if(Fe(this,fr,new hr(t)),!this.isSymmetric())throw new TypeError("not symmetric data")}clone(){const t=new mr(this.diagonalSize);for(const[e,r,n]of this.upperRightEntries())t.set(e,r,n);return t}toMatrix(){return new hr(this)}get(t,e){return Oe(this,fr).get(t,e)}set(t,e,r){return Oe(this,fr).set(t,e,r),Oe(this,fr).set(e,t,r),this}removeCross(t){return Oe(this,fr).removeRow(t),Oe(this,fr).removeColumn(t),this}addCross(t,e){void 0===e&&(e=t,t=this.diagonalSize);const r=e.slice();return r.splice(t,1),Oe(this,fr).addRow(t,r),Oe(this,fr).addColumn(t,e),this}applyMask(t){if(t.length!==this.diagonalSize)throw new RangeError("Mask size do not match with matrix size");const e=[];for(const[r,n]of t.entries())n||e.push(r);e.reverse();for(const t of e)this.removeCross(t);return this}toCompact(){const{diagonalSize:t}=this,e=new Array(t*(t+1)/2);for(let r=0,n=0,o=0;o<e.length;o++)e[o]=this.get(n,r),++r>=t&&(r=++n);return e}static fromCompact(t){const e=t.length,r=(Math.sqrt(8*e+1)-1)/2;if(!Number.isInteger(r))throw new TypeError(`This array is not a compact representation of a Symmetric Matrix, ${JSON.stringify(t)}`);const n=new mr(r);for(let o=0,s=0,i=0;i<e;i++)n.set(o,s,t[i]),++o>=r&&(o=++s);return n}*upperRightEntries(){for(let t=0,e=0;t<this.diagonalSize;void 0){const r=this.get(t,e);yield[t,e,r],++e>=this.diagonalSize&&(e=++t)}}*upperRightValues(){for(let t=0,e=0;t<this.diagonalSize;void 0){const r=this.get(t,e);yield r,++e>=this.diagonalSize&&(e=++t)}}}mr.prototype.klassType="SymmetricMatrix";class gr extends mr{static isDistanceMatrix(t){return mr.isSymmetricMatrix(t)&&"DistanceMatrix"===t.klassSubType}constructor(t){if(super(t),!this.isDistance())throw new TypeError("Provided arguments do no produce a distance matrix")}set(t,e,r){return t===e&&(r=0),super.set(t,e,r)}addCross(t,e){return void 0===e&&(e=t,t=this.diagonalSize),(e=e.slice())[t]=0,super.addCross(t,e)}toSymmetricMatrix(){return new mr(this)}clone(){const t=new gr(this.diagonalSize);for(const[e,r,n]of this.upperRightEntries())e!==r&&t.set(e,r,n);return t}toCompact(){const{diagonalSize:t}=this,e=new Array((t-1)*t/2);for(let r=1,n=0,o=0;o<e.length;o++)e[o]=this.get(n,r),++r>=t&&(r=1+ ++n);return e}static fromCompact(t){const e=t.length;if(0===e)return new this(0);const r=(Math.sqrt(8*e+1)+1)/2;if(!Number.isInteger(r))throw new TypeError(`This array is not a compact representation of a DistanceMatrix, ${JSON.stringify(t)}`);const n=new this(r);for(let o=1,s=0,i=0;i<e;i++)n.set(o,s,t[i]),++o>=r&&(o=1+ ++s);return n}}gr.prototype.klassSubType="DistanceMatrix";class wr extends ur{constructor(t,e,r){super(),this.matrix=t,this.rows=e,this.columns=r}}class dr extends wr{constructor(t,e,r){er(t,e),rr(t,r),super(t,e.length,r.length),this.rowIndices=e,this.columnIndices=r}set(t,e,r){return this.matrix.set(this.rowIndices[t],this.columnIndices[e],r),this}get(t,e){return this.matrix.get(this.rowIndices[t],this.columnIndices[e])}}class pr extends ur{constructor(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};const{rows:r=1}=e;if(t.length%r!=0)throw new Error("the data length is not divisible by the number of rows");super(),this.rows=r,this.columns=t.length/r,this.data=t}set(t,e,r){let n=this._calculateIndex(t,e);return this.data[n]=r,this}get(t,e){let r=this._calculateIndex(t,e);return this.data[r]}_calculateIndex(t,e){return t*this.columns+e}}class yr extends ur{constructor(t){super(),this.data=t,this.rows=t.length,this.columns=t[0].length}set(t,e,r){return this.data[t][e]=r,this}get(t,e){return this.data[t][e]}}class br{constructor(t){let e,r,n,o,s,i,u,l,a,h=(t=yr.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(l=new Float64Array(c),r=0;r<f;r++){for(e=0;e<c;e++)l[e]=h.get(e,r);for(e=0;e<c;e++){for(a=Math.min(e,r),s=0,n=0;n<a;n++)s+=h.get(e,n)*l[n];l[e]-=s,h.set(e,r,l[e])}for(o=r,e=r+1;e<c;e++)Math.abs(l[e])>Math.abs(l[o])&&(o=e);if(o!==r){for(n=0;n<f;n++)i=h.get(o,n),h.set(o,n,h.get(r,n)),h.set(r,n,i);u=m[o],m[o]=m[r],m[r]=u,g=-g}if(r<c&&0!==h.get(r,r))for(e=r+1;e<c;e++)h.set(e,r,h.get(e,r)/h.get(r,r))}this.LU=h,this.pivotVector=m,this.pivotSign=g}isSingular(){let t=this.LU,e=t.columns;for(let r=0;r<e;r++)if(0===t.get(r,r))return!0;return!1}solve(t){t=hr.checkMatrix(t);let e=this.LU;if(e.rows!==t.rows)throw new Error("Invalid matrix dimensions");if(this.isSingular())throw new Error("LU matrix is singular");let r,n,o,s=t.columns,i=t.subMatrixRow(this.pivotVector,0,s-1),u=e.columns;for(o=0;o<u;o++)for(r=o+1;r<u;r++)for(n=0;n<s;n++)i.set(r,n,i.get(r,n)-i.get(o,n)*e.get(r,o));for(o=u-1;o>=0;o--){for(n=0;n<s;n++)i.set(o,n,i.get(o,n)/e.get(o,o));for(r=0;r<o;r++)for(n=0;n<s;n++)i.set(r,n,i.get(r,n)-i.get(o,n)*e.get(r,o))}return i}get determinant(){let t=this.LU;if(!t.isSquare())throw new Error("Matrix must be square");let e=this.pivotSign,r=t.columns;for(let n=0;n<r;n++)e*=t.get(n,n);return e}get lowerTriangularMatrix(){let t=this.LU,e=t.rows,r=t.columns,n=new hr(e,r);for(let o=0;o<e;o++)for(let e=0;e<r;e++)o>e?n.set(o,e,t.get(o,e)):o===e?n.set(o,e,1):n.set(o,e,0);return n}get upperTriangularMatrix(){let t=this.LU,e=t.rows,r=t.columns,n=new hr(e,r);for(let o=0;o<e;o++)for(let e=0;e<r;e++)o<=e?n.set(o,e,t.get(o,e)):n.set(o,e,0);return n}get pivotPermutationVector(){return Array.from(this.pivotVector)}}function vr(t,e){let r=0;return Math.abs(t)>Math.abs(e)?(r=e/t,Math.abs(t)*Math.sqrt(1+r*r)):0!==e?(r=t/e,Math.abs(e)*Math.sqrt(1+r*r)):0}class Mr{constructor(t){let e,r,n,o,s=(t=yr.checkMatrix(t)).clone(),i=t.rows,u=t.columns,l=new Float64Array(u);for(n=0;n<u;n++){let t=0;for(e=n;e<i;e++)t=vr(t,s.get(e,n));if(0!==t){for(s.get(n,n)<0&&(t=-t),e=n;e<i;e++)s.set(e,n,s.get(e,n)/t);for(s.set(n,n,s.get(n,n)+1),r=n+1;r<u;r++){for(o=0,e=n;e<i;e++)o+=s.get(e,n)*s.get(e,r);for(o=-o/s.get(n,n),e=n;e<i;e++)s.set(e,r,s.get(e,r)+o*s.get(e,n))}}l[n]=-t}this.QR=s,this.Rdiag=l}solve(t){t=hr.checkMatrix(t);let e=this.QR,r=e.rows;if(t.rows!==r)throw new Error("Matrix row dimensions must agree");if(!this.isFullRank())throw new Error("Matrix is rank deficient");let n,o,s,i,u=t.columns,l=t.clone(),a=e.columns;for(s=0;s<a;s++)for(o=0;o<u;o++){for(i=0,n=s;n<r;n++)i+=e.get(n,s)*l.get(n,o);for(i=-i/e.get(s,s),n=s;n<r;n++)l.set(n,o,l.get(n,o)+i*e.get(n,s))}for(s=a-1;s>=0;s--){for(o=0;o<u;o++)l.set(s,o,l.get(s,o)/this.Rdiag[s]);for(n=0;n<s;n++)for(o=0;o<u;o++)l.set(n,o,l.get(n,o)-l.get(s,o)*e.get(n,s))}return l.subMatrix(0,a-1,0,u-1)}isFullRank(){let t=this.QR.columns;for(let e=0;e<t;e++)if(0===this.Rdiag[e])return!1;return!0}get upperTriangularMatrix(){let t,e,r=this.QR,n=r.columns,o=new hr(n,n);for(t=0;t<n;t++)for(e=0;e<n;e++)t<e?o.set(t,e,r.get(t,e)):t===e?o.set(t,e,this.Rdiag[t]):o.set(t,e,0);return o}get orthogonalMatrix(){let t,e,r,n,o=this.QR,s=o.rows,i=o.columns,u=new hr(s,i);for(r=i-1;r>=0;r--){for(t=0;t<s;t++)u.set(t,r,0);for(u.set(r,r,1),e=r;e<i;e++)if(0!==o.get(r,r)){for(n=0,t=r;t<s;t++)n+=o.get(t,r)*u.get(t,e);for(n=-n/o.get(r,r),t=r;t<s;t++)u.set(t,e,u.get(t,e)+n*o.get(t,r))}}return u}}class xr{constructor(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if((t=yr.checkMatrix(t)).isEmpty())throw new Error("Matrix must be non-empty");let r=t.rows,n=t.columns;const{computeLeftSingularVectors:o=!0,computeRightSingularVectors:s=!0,autoTranspose:i=!1}=e;let u,l=Boolean(o),a=Boolean(s),h=!1;if(r<n)if(i){u=t.transpose(),r=u.rows,n=u.columns,h=!0;let e=l;l=a,a=e}else u=t.clone(),console.warn("Computing SVD on a matrix with more columns than rows. Consider enabling autoTranspose");else u=t.clone();let c=Math.min(r,n),f=Math.min(r+1,n),m=new Float64Array(f),g=new hr(r,c),w=new hr(n,n),d=new Float64Array(n),p=new Float64Array(r),y=new Float64Array(f);for(let t=0;t<f;t++)y[t]=t;let b=Math.min(r-1,n),v=Math.max(0,Math.min(n-2,r)),M=Math.max(b,v);for(let t=0;t<M;t++){if(t<b){m[t]=0;for(let e=t;e<r;e++)m[t]=vr(m[t],u.get(e,t));if(0!==m[t]){u.get(t,t)<0&&(m[t]=-m[t]);for(let e=t;e<r;e++)u.set(e,t,u.get(e,t)/m[t]);u.set(t,t,u.get(t,t)+1)}m[t]=-m[t]}for(let e=t+1;e<n;e++){if(t<b&&0!==m[t]){let n=0;for(let o=t;o<r;o++)n+=u.get(o,t)*u.get(o,e);n=-n/u.get(t,t);for(let o=t;o<r;o++)u.set(o,e,u.get(o,e)+n*u.get(o,t))}d[e]=u.get(t,e)}if(l&&t<b)for(let e=t;e<r;e++)g.set(e,t,u.get(e,t));if(t<v){d[t]=0;for(let e=t+1;e<n;e++)d[t]=vr(d[t],d[e]);if(0!==d[t]){d[t+1]<0&&(d[t]=0-d[t]);for(let e=t+1;e<n;e++)d[e]/=d[t];d[t+1]+=1}if(d[t]=-d[t],t+1<r&&0!==d[t]){for(let e=t+1;e<r;e++)p[e]=0;for(let e=t+1;e<r;e++)for(let r=t+1;r<n;r++)p[e]+=d[r]*u.get(e,r);for(let e=t+1;e<n;e++){let n=-d[e]/d[t+1];for(let o=t+1;o<r;o++)u.set(o,e,u.get(o,e)+n*p[o])}}if(a)for(let e=t+1;e<n;e++)w.set(e,t,d[e])}}let x=Math.min(n,r+1);if(b<n&&(m[b]=u.get(b,b)),r<x&&(m[x-1]=0),v+1<x&&(d[v]=u.get(v,x-1)),d[x-1]=0,l){for(let t=b;t<c;t++){for(let e=0;e<r;e++)g.set(e,t,0);g.set(t,t,1)}for(let t=b-1;t>=0;t--)if(0!==m[t]){for(let e=t+1;e<c;e++){let n=0;for(let o=t;o<r;o++)n+=g.get(o,t)*g.get(o,e);n=-n/g.get(t,t);for(let o=t;o<r;o++)g.set(o,e,g.get(o,e)+n*g.get(o,t))}for(let e=t;e<r;e++)g.set(e,t,-g.get(e,t));g.set(t,t,1+g.get(t,t));for(let e=0;e<t-1;e++)g.set(e,t,0)}else{for(let e=0;e<r;e++)g.set(e,t,0);g.set(t,t,1)}}if(a)for(let t=n-1;t>=0;t--){if(t<v&&0!==d[t])for(let e=t+1;e<n;e++){let r=0;for(let o=t+1;o<n;o++)r+=w.get(o,t)*w.get(o,e);r=-r/w.get(t+1,t);for(let o=t+1;o<n;o++)w.set(o,e,w.get(o,e)+r*w.get(o,t))}for(let e=0;e<n;e++)w.set(e,t,0);w.set(t,t,1)}let E=x-1,S=Number.EPSILON;for(;x>0;){let t,e;for(t=x-2;t>=-1&&-1!==t;t--){const e=Number.MIN_VALUE+S*Math.abs(m[t]+Math.abs(m[t+1]));if(Math.abs(d[t])<=e||Number.isNaN(d[t])){d[t]=0;break}}if(t===x-2)e=4;else{let r;for(r=x-1;r>=t&&r!==t;r--){let e=(r!==x?Math.abs(d[r]):0)+(r!==t+1?Math.abs(d[r-1]):0);if(Math.abs(m[r])<=S*e){m[r]=0;break}}r===t?e=3:r===x-1?e=1:(e=2,t=r)}switch(t++,e){case 1:{let e=d[x-2];d[x-2]=0;for(let r=x-2;r>=t;r--){let o=vr(m[r],e),s=m[r]/o,i=e/o;if(m[r]=o,r!==t&&(e=-i*d[r-1],d[r-1]=s*d[r-1]),a)for(let t=0;t<n;t++)o=s*w.get(t,r)+i*w.get(t,x-1),w.set(t,x-1,-i*w.get(t,r)+s*w.get(t,x-1)),w.set(t,r,o)}break}case 2:{let e=d[t-1];d[t-1]=0;for(let n=t;n<x;n++){let o=vr(m[n],e),s=m[n]/o,i=e/o;if(m[n]=o,e=-i*d[n],d[n]=s*d[n],l)for(let e=0;e<r;e++)o=s*g.get(e,n)+i*g.get(e,t-1),g.set(e,t-1,-i*g.get(e,n)+s*g.get(e,t-1)),g.set(e,n,o)}break}case 3:{const e=Math.max(Math.abs(m[x-1]),Math.abs(m[x-2]),Math.abs(d[x-2]),Math.abs(m[t]),Math.abs(d[t])),o=m[x-1]/e,s=m[x-2]/e,i=d[x-2]/e,u=m[t]/e,h=d[t]/e,c=((s+o)*(s-o)+i*i)/2,f=o*i*(o*i);let p=0;0===c&&0===f||(p=c<0?0-Math.sqrt(c*c+f):Math.sqrt(c*c+f),p=f/(c+p));let y=(u+o)*(u-o)+p,b=u*h;for(let e=t;e<x-1;e++){let o=vr(y,b);0===o&&(o=Number.MIN_VALUE);let s=y/o,i=b/o;if(e!==t&&(d[e-1]=o),y=s*m[e]+i*d[e],d[e]=s*d[e]-i*m[e],b=i*m[e+1],m[e+1]=s*m[e+1],a)for(let t=0;t<n;t++)o=s*w.get(t,e)+i*w.get(t,e+1),w.set(t,e+1,-i*w.get(t,e)+s*w.get(t,e+1)),w.set(t,e,o);if(o=vr(y,b),0===o&&(o=Number.MIN_VALUE),s=y/o,i=b/o,m[e]=o,y=s*d[e]+i*m[e+1],m[e+1]=-i*d[e]+s*m[e+1],b=i*d[e+1],d[e+1]=s*d[e+1],l&&e<r-1)for(let t=0;t<r;t++)o=s*g.get(t,e)+i*g.get(t,e+1),g.set(t,e+1,-i*g.get(t,e)+s*g.get(t,e+1)),g.set(t,e,o)}d[x-2]=y;break}case 4:if(m[t]<=0&&(m[t]=m[t]<0?-m[t]:0,a))for(let e=0;e<=E;e++)w.set(e,t,-w.get(e,t));for(;t<E&&!(m[t]>=m[t+1]);){let e=m[t];if(m[t]=m[t+1],m[t+1]=e,a&&t<n-1)for(let r=0;r<n;r++)e=w.get(r,t+1),w.set(r,t+1,w.get(r,t)),w.set(r,t,e);if(l&&t<r-1)for(let n=0;n<r;n++)e=g.get(n,t+1),g.set(n,t+1,g.get(n,t)),g.set(n,t,e);t++}x--}}if(h){let t=w;w=g,g=t}this.m=r,this.n=n,this.s=m,this.U=g,this.V=w}solve(t){let e=t,r=this.threshold,n=this.s.length,o=hr.zeros(n,n);for(let t=0;t<n;t++)Math.abs(this.s[t])<=r?o.set(t,t,0):o.set(t,t,1/this.s[t]);let s=this.U,i=this.rightSingularVectors,u=i.mmul(o),l=i.rows,a=s.rows,h=hr.zeros(l,a);for(let t=0;t<l;t++)for(let e=0;e<a;e++){let r=0;for(let o=0;o<n;o++)r+=u.get(t,o)*s.get(e,o);h.set(t,e,r)}return h.mmul(e)}solveForDiagonal(t){return this.solve(hr.diag(t))}inverse(){let t=this.V,e=this.threshold,r=t.rows,n=t.columns,o=new hr(r,this.s.length);for(let s=0;s<r;s++)for(let r=0;r<n;r++)Math.abs(this.s[r])>e&&o.set(s,r,t.get(s,r)/this.s[r]);let s=this.U,i=s.rows,u=s.columns,l=new hr(r,i);for(let t=0;t<r;t++)for(let e=0;e<i;e++){let r=0;for(let n=0;n<u;n++)r+=o.get(t,n)*s.get(e,n);l.set(t,e,r)}return l}get condition(){return this.s[0]/this.s[Math.min(this.m,this.n)-1]}get norm2(){return this.s[0]}get rank(){let t=Math.max(this.m,this.n)*this.s[0]*Number.EPSILON,e=0,r=this.s;for(let n=0,o=r.length;n<o;n++)r[n]>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 hr.diag(this.s)}}function Er(t,e){let r=arguments.length>2&&void 0!==arguments[2]&&arguments[2];return t=yr.checkMatrix(t),e=yr.checkMatrix(e),r?new xr(t).solve(e):t.isSquare()?new br(t).solve(e):new Mr(t).solve(e)}function Sr(t,e){let r=[];for(let n=0;n<t;n++)n!==e&&r.push(n);return r}function _r(t,e,r){let n=arguments.length>3&&void 0!==arguments[3]?arguments[3]:1e-9;if(t>(arguments.length>4&&void 0!==arguments[4]?arguments[4]:1e-9))return new Array(e.rows+1).fill(0);{let t=e.addRow(r,[0]);for(let e=0;e<t.rows;e++)Math.abs(t.get(e,0))<n&&t.set(e,0,0);return t.to1DArray()}}class jr{constructor(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};const{assumeSymmetric:r=!1}=e;if(!(t=yr.checkMatrix(t)).isSquare())throw new Error("Matrix is not a square matrix");if(t.isEmpty())throw new Error("Matrix must be non-empty");let n,o,s=t.columns,i=new hr(s,s),u=new Float64Array(s),l=new Float64Array(s),a=t,h=!1;if(h=!!r||t.isSymmetric(),h){for(n=0;n<s;n++)for(o=0;o<s;o++)i.set(n,o,a.get(n,o));!function(t,e,r,n){let o,s,i,u,l,a,h,c;for(l=0;l<t;l++)r[l]=n.get(t-1,l);for(u=t-1;u>0;u--){for(c=0,i=0,a=0;a<u;a++)c+=Math.abs(r[a]);if(0===c)for(e[u]=r[u-1],l=0;l<u;l++)r[l]=n.get(u-1,l),n.set(u,l,0),n.set(l,u,0);else{for(a=0;a<u;a++)r[a]/=c,i+=r[a]*r[a];for(o=r[u-1],s=Math.sqrt(i),o>0&&(s=-s),e[u]=c*s,i-=o*s,r[u-1]=o-s,l=0;l<u;l++)e[l]=0;for(l=0;l<u;l++){for(o=r[l],n.set(l,u,o),s=e[l]+n.get(l,l)*o,a=l+1;a<=u-1;a++)s+=n.get(a,l)*r[a],e[a]+=n.get(a,l)*o;e[l]=s}for(o=0,l=0;l<u;l++)e[l]/=i,o+=e[l]*r[l];for(h=o/(i+i),l=0;l<u;l++)e[l]-=h*r[l];for(l=0;l<u;l++){for(o=r[l],s=e[l],a=l;a<=u-1;a++)n.set(a,l,n.get(a,l)-(o*e[a]+s*r[a]));r[l]=n.get(u-1,l),n.set(u,l,0)}}r[u]=i}for(u=0;u<t-1;u++){if(n.set(t-1,u,n.get(u,u)),n.set(u,u,1),i=r[u+1],0!==i){for(a=0;a<=u;a++)r[a]=n.get(a,u+1)/i;for(l=0;l<=u;l++){for(s=0,a=0;a<=u;a++)s+=n.get(a,u+1)*n.get(a,l);for(a=0;a<=u;a++)n.set(a,l,n.get(a,l)-s*r[a])}}for(a=0;a<=u;a++)n.set(a,u+1,0)}for(l=0;l<t;l++)r[l]=n.get(t-1,l),n.set(t-1,l,0);n.set(t-1,t-1,1),e[0]=0}(s,l,u,i),function(t,e,r,n){let o,s,i,u,l,a,h,c,f,m,g,w,d,p,y,b;for(i=1;i<t;i++)e[i-1]=e[i];e[t-1]=0;let v=0,M=0,x=Number.EPSILON;for(a=0;a<t;a++){for(M=Math.max(M,Math.abs(r[a])+Math.abs(e[a])),h=a;h<t&&!(Math.abs(e[h])<=x*M);)h++;if(h>a)do{for(o=r[a],c=(r[a+1]-o)/(2*e[a]),f=vr(c,1),c<0&&(f=-f),r[a]=e[a]/(c+f),r[a+1]=e[a]*(c+f),m=r[a+1],s=o-r[a],i=a+2;i<t;i++)r[i]-=s;for(v+=s,c=r[h],g=1,w=g,d=g,p=e[a+1],y=0,b=0,i=h-1;i>=a;i--)for(d=w,w=g,b=y,o=g*e[i],s=g*c,f=vr(c,e[i]),e[i+1]=y*f,y=e[i]/f,g=c/f,c=g*r[i]-y*o,r[i+1]=s+y*(g*o+y*r[i]),l=0;l<t;l++)s=n.get(l,i+1),n.set(l,i+1,y*n.get(l,i)+g*s),n.set(l,i,g*n.get(l,i)-y*s);c=-y*b*d*p*e[a]/m,e[a]=y*c,r[a]=g*c}while(Math.abs(e[a])>x*M);r[a]=r[a]+v,e[a]=0}for(i=0;i<t-1;i++){for(l=i,c=r[i],u=i+1;u<t;u++)r[u]<c&&(l=u,c=r[u]);if(l!==i)for(r[l]=r[i],r[i]=c,u=0;u<t;u++)c=n.get(u,i),n.set(u,i,n.get(u,l)),n.set(u,l,c)}}(s,l,u,i)}else{let t=new hr(s,s),e=new Float64Array(s);for(o=0;o<s;o++)for(n=0;n<s;n++)t.set(n,o,a.get(n,o));!function(t,e,r,n){let o,s,i,u,l,a,h,c=0,f=t-1;for(a=c+1;a<=f-1;a++){for(h=0,u=a;u<=f;u++)h+=Math.abs(e.get(u,a-1));if(0!==h){for(i=0,u=f;u>=a;u--)r[u]=e.get(u,a-1)/h,i+=r[u]*r[u];for(s=Math.sqrt(i),r[a]>0&&(s=-s),i-=r[a]*s,r[a]=r[a]-s,l=a;l<t;l++){for(o=0,u=f;u>=a;u--)o+=r[u]*e.get(u,l);for(o/=i,u=a;u<=f;u++)e.set(u,l,e.get(u,l)-o*r[u])}for(u=0;u<=f;u++){for(o=0,l=f;l>=a;l--)o+=r[l]*e.get(u,l);for(o/=i,l=a;l<=f;l++)e.set(u,l,e.get(u,l)-o*r[l])}r[a]=h*r[a],e.set(a,a-1,h*s)}}for(u=0;u<t;u++)for(l=0;l<t;l++)n.set(u,l,u===l?1:0);for(a=f-1;a>=c+1;a--)if(0!==e.get(a,a-1)){for(u=a+1;u<=f;u++)r[u]=e.get(u,a-1);for(l=a;l<=f;l++){for(s=0,u=a;u<=f;u++)s+=r[u]*n.get(u,l);for(s=s/r[a]/e.get(a,a-1),u=a;u<=f;u++)n.set(u,l,n.get(u,l)+s*r[u])}}}(s,t,e,i),function(t,e,r,n,o){let s,i,u,l,a,h,c,f,m,g,w,d,p,y,b,v=t-1,M=0,x=t-1,E=Number.EPSILON,S=0,_=0,j=0,A=0,R=0,I=0,C=0,N=0;for(s=0;s<t;s++)for((s<M||s>x)&&(r[s]=o.get(s,s),e[s]=0),i=Math.max(s-1,0);i<t;i++)_+=Math.abs(o.get(s,i));for(;v>=M;){for(l=v;l>M&&(I=Math.abs(o.get(l-1,l-1))+Math.abs(o.get(l,l)),0===I&&(I=_),!(Math.abs(o.get(l,l-1))<E*I));)l--;if(l===v)o.set(v,v,o.get(v,v)+S),r[v]=o.get(v,v),e[v]=0,v--,N=0;else if(l===v-1){if(c=o.get(v,v-1)*o.get(v-1,v),j=(o.get(v-1,v-1)-o.get(v,v))/2,A=j*j+c,C=Math.sqrt(Math.abs(A)),o.set(v,v,o.get(v,v)+S),o.set(v-1,v-1,o.get(v-1,v-1)+S),f=o.get(v,v),A>=0){for(C=j>=0?j+C:j-C,r[v-1]=f+C,r[v]=r[v-1],0!==C&&(r[v]=f-c/C),e[v-1]=0,e[v]=0,f=o.get(v,v-1),I=Math.abs(f)+Math.abs(C),j=f/I,A=C/I,R=Math.sqrt(j*j+A*A),j/=R,A/=R,i=v-1;i<t;i++)C=o.get(v-1,i),o.set(v-1,i,A*C+j*o.get(v,i)),o.set(v,i,A*o.get(v,i)-j*C);for(s=0;s<=v;s++)C=o.get(s,v-1),o.set(s,v-1,A*C+j*o.get(s,v)),o.set(s,v,A*o.get(s,v)-j*C);for(s=M;s<=x;s++)C=n.get(s,v-1),n.set(s,v-1,A*C+j*n.get(s,v)),n.set(s,v,A*n.get(s,v)-j*C)}else r[v-1]=f+j,r[v]=f+j,e[v-1]=C,e[v]=-C;v-=2,N=0}else{if(f=o.get(v,v),m=0,c=0,l<v&&(m=o.get(v-1,v-1),c=o.get(v,v-1)*o.get(v-1,v)),10===N){for(S+=f,s=M;s<=v;s++)o.set(s,s,o.get(s,s)-f);I=Math.abs(o.get(v,v-1))+Math.abs(o.get(v-1,v-2)),f=m=.75*I,c=-.4375*I*I}if(30===N&&(I=(m-f)/2,I=I*I+c,I>0)){for(I=Math.sqrt(I),m<f&&(I=-I),I=f-c/((m-f)/2+I),s=M;s<=v;s++)o.set(s,s,o.get(s,s)-I);S+=I,f=m=c=.964}for(N+=1,a=v-2;a>=l&&(C=o.get(a,a),R=f-C,I=m-C,j=(R*I-c)/o.get(a+1,a)+o.get(a,a+1),A=o.get(a+1,a+1)-C-R-I,R=o.get(a+2,a+1),I=Math.abs(j)+Math.abs(A)+Math.abs(R),j/=I,A/=I,R/=I,a!==l)&&!(Math.abs(o.get(a,a-1))*(Math.abs(A)+Math.abs(R))<E*(Math.abs(j)*(Math.abs(o.get(a-1,a-1))+Math.abs(C)+Math.abs(o.get(a+1,a+1)))));)a--;for(s=a+2;s<=v;s++)o.set(s,s-2,0),s>a+2&&o.set(s,s-3,0);for(u=a;u<=v-1&&(y=u!==v-1,u!==a&&(j=o.get(u,u-1),A=o.get(u+1,u-1),R=y?o.get(u+2,u-1):0,f=Math.abs(j)+Math.abs(A)+Math.abs(R),0!==f&&(j/=f,A/=f,R/=f)),0!==f);u++)if(I=Math.sqrt(j*j+A*A+R*R),j<0&&(I=-I),0!==I){for(u!==a?o.set(u,u-1,-I*f):l!==a&&o.set(u,u-1,-o.get(u,u-1)),j+=I,f=j/I,m=A/I,C=R/I,A/=j,R/=j,i=u;i<t;i++)j=o.get(u,i)+A*o.get(u+1,i),y&&(j+=R*o.get(u+2,i),o.set(u+2,i,o.get(u+2,i)-j*C)),o.set(u,i,o.get(u,i)-j*f),o.set(u+1,i,o.get(u+1,i)-j*m);for(s=0;s<=Math.min(v,u+3);s++)j=f*o.get(s,u)+m*o.get(s,u+1),y&&(j+=C*o.get(s,u+2),o.set(s,u+2,o.get(s,u+2)-j*R)),o.set(s,u,o.get(s,u)-j),o.set(s,u+1,o.get(s,u+1)-j*A);for(s=M;s<=x;s++)j=f*n.get(s,u)+m*n.get(s,u+1),y&&(j+=C*n.get(s,u+2),n.set(s,u+2,n.get(s,u+2)-j*R)),n.set(s,u,n.get(s,u)-j),n.set(s,u+1,n.get(s,u+1)-j*A)}}}if(0===_)return;for(v=t-1;v>=0;v--)if(j=r[v],A=e[v],0===A)for(l=v,o.set(v,v,1),s=v-1;s>=0;s--){for(c=o.get(s,s)-j,R=0,i=l;i<=v;i++)R+=o.get(s,i)*o.get(i,v);if(e[s]<0)C=c,I=R;else if(l=s,0===e[s]?o.set(s,v,0!==c?-R/c:-R/(E*_)):(f=o.get(s,s+1),m=o.get(s+1,s),A=(r[s]-j)*(r[s]-j)+e[s]*e[s],h=(f*I-C*R)/A,o.set(s,v,h),o.set(s+1,v,Math.abs(f)>Math.abs(C)?(-R-c*h)/f:(-I-m*h)/C)),h=Math.abs(o.get(s,v)),E*h*h>1)for(i=s;i<=v;i++)o.set(i,v,o.get(i,v)/h)}else if(A<0)for(l=v-1,Math.abs(o.get(v,v-1))>Math.abs(o.get(v-1,v))?(o.set(v-1,v-1,A/o.get(v,v-1)),o.set(v-1,v,-(o.get(v,v)-j)/o.get(v,v-1))):(b=Ar(0,-o.get(v-1,v),o.get(v-1,v-1)-j,A),o.set(v-1,v-1,b[0]),o.set(v-1,v,b[1])),o.set(v,v-1,0),o.set(v,v,1),s=v-2;s>=0;s--){for(g=0,w=0,i=l;i<=v;i++)g+=o.get(s,i)*o.get(i,v-1),w+=o.get(s,i)*o.get(i,v);if(c=o.get(s,s)-j,e[s]<0)C=c,R=g,I=w;else if(l=s,0===e[s]?(b=Ar(-g,-w,c,A),o.set(s,v-1,b[0]),o.set(s,v,b[1])):(f=o.get(s,s+1),m=o.get(s+1,s),d=(r[s]-j)*(r[s]-j)+e[s]*e[s]-A*A,p=2*(r[s]-j)*A,0===d&&0===p&&(d=E*_*(Math.abs(c)+Math.abs(A)+Math.abs(f)+Math.abs(m)+Math.abs(C))),b=Ar(f*R-C*g+A*w,f*I-C*w-A*g,d,p),o.set(s,v-1,b[0]),o.set(s,v,b[1]),Math.abs(f)>Math.abs(C)+Math.abs(A)?(o.set(s+1,v-1,(-g-c*o.get(s,v-1)+A*o.get(s,v))/f),o.set(s+1,v,(-w-c*o.get(s,v)-A*o.get(s,v-1))/f)):(b=Ar(-R-m*o.get(s,v-1),-I-m*o.get(s,v),C,A),o.set(s+1,v-1,b[0]),o.set(s+1,v,b[1]))),h=Math.max(Math.abs(o.get(s,v-1)),Math.abs(o.get(s,v))),E*h*h>1)for(i=s;i<=v;i++)o.set(i,v-1,o.get(i,v-1)/h),o.set(i,v,o.get(i,v)/h)}for(s=0;s<t;s++)if(s<M||s>x)for(i=s;i<t;i++)n.set(s,i,o.get(s,i));for(i=t-1;i>=M;i--)for(s=M;s<=x;s++){for(C=0,u=M;u<=Math.min(i,x);u++)C+=n.get(s,u)*o.get(u,i);n.set(s,i,C)}}(s,l,u,i,t)}this.n=s,this.e=l,this.d=u,this.V=i}get realEigenvalues(){return Array.from(this.d)}get imaginaryEigenvalues(){return Array.from(this.e)}get eigenvectorMatrix(){return this.V}get diagonalMatrix(){let t,e,r=this.n,n=this.e,o=this.d,s=new hr(r,r);for(t=0;t<r;t++){for(e=0;e<r;e++)s.set(t,e,0);s.set(t,t,o[t]),n[t]>0?s.set(t,t+1,n[t]):n[t]<0&&s.set(t,t-1,n[t])}return s}}function Ar(t,e,r,n){let o,s;return Math.abs(r)>Math.abs(n)?(o=n/r,s=r+o*n,[(t+o*e)/s,(e-o*t)/s]):(o=r/n,s=n+o*r,[(o*t+e)/s,(o*e-t)/s])}class Rr{constructor(t){if(!(t=yr.checkMatrix(t)).isSymmetric())throw new Error("Matrix is not symmetric");let e,r,n,o=t,s=o.rows,i=new hr(s,s),u=!0;for(r=0;r<s;r++){let t=0;for(n=0;n<r;n++){let s=0;for(e=0;e<n;e++)s+=i.get(n,e)*i.get(r,e);s=(o.get(r,n)-s)/i.get(n,n),i.set(r,n,s),t+=s*s}for(t=o.get(r,r)-t,u&&=t>0,i.set(r,r,Math.sqrt(Math.max(t,0))),n=r+1;n<s;n++)i.set(r,n,0)}this.L=i,this.positiveDefinite=u}isPositiveDefinite(){return this.positiveDefinite}solve(t){t=yr.checkMatrix(t);let e=this.L,r=e.rows;if(t.rows!==r)throw new Error("Matrix dimensions do not match");if(!1===this.isPositiveDefinite())throw new Error("Matrix is not positive definite");let n,o,s,i=t.columns,u=t.clone();for(s=0;s<r;s++)for(o=0;o<i;o++){for(n=0;n<s;n++)u.set(s,o,u.get(s,o)-u.get(n,o)*e.get(s,n));u.set(s,o,u.get(s,o)/e.get(s,s))}for(s=r-1;s>=0;s--)for(o=0;o<i;o++){for(n=s+1;n<r;n++)u.set(s,o,u.get(s,o)-u.get(n,o)*e.get(n,s));u.set(s,o,u.get(s,o)/e.get(s,s))}return u}get lowerTriangularMatrix(){return this.L}}class Ir{constructor(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};t=yr.checkMatrix(t);let{Y:r}=e;const{scaleScores:n=!1,maxIterations:o=1e3,terminationCriteria:s=1e-10}=e;let i;if(r){if(r=Ge(r)&&"number"==typeof r[0]?hr.columnVector(r):yr.checkMatrix(r),r.rows!==t.rows)throw new Error("Y should have the same number of rows as X");i=r.getColumnVector(0)}else i=t.getColumnVector(0);let u,l,a,h,c=1;for(let e=0;e<o&&c>s;e++)a=t.transpose().mmul(i).div(i.transpose().mmul(i).get(0,0)),a=a.div(a.norm()),u=t.mmul(a).div(a.transpose().mmul(a).get(0,0)),e>0&&(c=u.clone().sub(h).pow(2).sum()),h=u.clone(),r?(l=r.transpose().mmul(u).div(u.transpose().mmul(u).get(0,0)),l=l.div(l.norm()),i=r.mmul(l).div(l.transpose().mmul(l).get(0,0))):i=u;if(r){let e=t.transpose().mmul(u).div(u.transpose().mmul(u).get(0,0));e=e.div(e.norm());let n=t.clone().sub(u.clone().mmul(e.transpose())),o=i.transpose().mmul(u).div(u.transpose().mmul(u).get(0,0)),s=r.clone().sub(u.clone().mulS(o.get(0,0)).mmul(l.transpose()));this.t=u,this.p=e.transpose(),this.w=a.transpose(),this.q=l,this.u=i,this.s=u.transpose().mmul(u),this.xResidual=n,this.yResidual=s,this.betas=o}else this.w=a.transpose(),this.s=u.transpose().mmul(u).sqrt(),this.t=n?u.clone().div(this.s.get(0,0)):u,this.xResidual=t.sub(u.mmul(a.transpose()))}}qe.AbstractMatrix=ur,qe.CHO=Rr,qe.CholeskyDecomposition=Rr,qe.DistanceMatrix=gr;var Cr=qe.EVD=jr;qe.EigenvalueDecomposition=jr,qe.LU=br,qe.LuDecomposition=br;var Nr=qe.Matrix=hr;qe.MatrixColumnSelectionView=class extends wr{constructor(t,e){rr(t,e),super(t,t.rows,e.length),this.columnIndices=e}set(t,e,r){return this.matrix.set(t,this.columnIndices[e],r),this}get(t,e){return this.matrix.get(t,this.columnIndices[e])}},qe.MatrixColumnView=class extends wr{constructor(t,e){He(t,e),super(t,t.rows,1),this.column=e}set(t,e,r){return this.matrix.set(t,this.column,r),this}get(t){return this.matrix.get(t,this.column)}},qe.MatrixFlipColumnView=class extends wr{constructor(t){super(t,t.rows,t.columns)}set(t,e,r){return this.matrix.set(t,this.columns-e-1,r),this}get(t,e){return this.matrix.get(t,this.columns-e-1)}},qe.MatrixFlipRowView=class extends wr{constructor(t){super(t,t.rows,t.columns)}set(t,e,r){return this.matrix.set(this.rows-t-1,e,r),this}get(t,e){return this.matrix.get(this.rows-t-1,e)}},qe.MatrixRowSelectionView=class extends wr{constructor(t,e){er(t,e),super(t,e.length,t.columns),this.rowIndices=e}set(t,e,r){return this.matrix.set(this.rowIndices[t],e,r),this}get(t,e){return this.matrix.get(this.rowIndices[t],e)}},qe.MatrixRowView=class extends wr{constructor(t,e){Je(t,e),super(t,1,t.columns),this.row=e}set(t,e,r){return this.matrix.set(this.row,e,r),this}get(t,e){return this.matrix.get(this.row,e)}},qe.MatrixSelectionView=dr,qe.MatrixSubView=class extends wr{constructor(t,e,r,n,o){nr(t,e,r,n,o),super(t,r-e+1,o-n+1),this.startRow=e,this.startColumn=n}set(t,e,r){return this.matrix.set(this.startRow+t,this.startColumn+e,r),this}get(t,e){return this.matrix.get(this.startRow+t,this.startColumn+e)}};var kr=qe.MatrixTransposeView=class extends wr{constructor(t){super(t,t.columns,t.rows)}set(t,e,r){return this.matrix.set(e,t,r),this}get(t,e){return this.matrix.get(e,t)}},Tr=qe.NIPALS=Ir;qe.Nipals=Ir,qe.QR=Mr,qe.QrDecomposition=Mr;var Fr=qe.SVD=xr;qe.SingularValueDecomposition=xr,qe.SymmetricMatrix=mr,qe.WrapperMatrix1D=pr,qe.WrapperMatrix2D=yr,qe.correlation=function(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:t,r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};t=new hr(t);let n=!1;if("object"!=typeof e||hr.isMatrix(e)||Ge(e)?e=new hr(e):(r=e,e=t,n=!0),t.rows!==e.rows)throw new TypeError("Both matrices must have the same number of rows");const{center:o=!0,scale:s=!0}=r;o&&(t.center("column"),n||e.center("column")),s&&(t.scale("column"),n||e.scale("column"));const i=t.standardDeviation("column",{unbiased:!0}),u=n?i:e.standardDeviation("column",{unbiased:!0}),l=t.transpose().mmul(e);for(let e=0;e<l.rows;e++)for(let r=0;r<l.columns;r++)l.set(e,r,l.get(e,r)*(1/(i[e]*u[r]))*(1/(t.rows-1)));return l},qe.covariance=function(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:t,r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};t=new hr(t);let n=!1;if("object"!=typeof e||hr.isMatrix(e)||Ge(e)?e=new hr(e):(r=e,e=t,n=!0),t.rows!==e.rows)throw new TypeError("Both matrices must have the same number of rows");const{center:o=!0}=r;o&&(t=t.center("column"),n||(e=e.center("column")));const s=t.transpose().mmul(e);for(let e=0;e<s.rows;e++)for(let r=0;r<s.columns;r++)s.set(e,r,s.get(e,r)*(1/(t.rows-1)));return s};var Or=qe.default=hr;qe.determinant=function t(e){if((e=hr.checkMatrix(e)).isSquare()){if(0===e.columns)return 1;let r,n,o,s;if(2===e.columns)return r=e.get(0,0),n=e.get(0,1),o=e.get(1,0),s=e.get(1,1),r*s-n*o;if(3===e.columns){let s,i,u;return s=new dr(e,[1,2],[1,2]),i=new dr(e,[1,2],[0,2]),u=new dr(e,[1,2],[0,1]),r=e.get(0,0),n=e.get(0,1),o=e.get(0,2),r*t(s)-n*t(i)+o*t(u)}return new br(e).determinant}throw Error("determinant can only be calculated for a square matrix")},qe.inverse=function(t){let e=arguments.length>1&&void 0!==arguments[1]&&arguments[1];return t=yr.checkMatrix(t),e?new xr(t).inverse():Er(t,hr.eye(t.rows))},qe.linearDependencies=function(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};const{thresholdValue:r=1e-9,thresholdError:n=1e-9}=e;let o=(t=hr.checkMatrix(t)).rows,s=new hr(o,o);for(let e=0;e<o;e++){let i=hr.columnVector(t.getRow(e)),u=t.subMatrixRow(Sr(o,e)).transpose(),l=new xr(u).solve(i),a=hr.sub(i,u.mmul(l)).abs().max();s.setRow(e,_r(a,l,e,r,n))}return s},qe.pseudoInverse=function(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:Number.EPSILON;if((t=hr.checkMatrix(t)).isEmpty())return t.transpose();let r=new xr(t,{autoTranspose:!0}),n=r.leftSingularVectors,o=r.rightSingularVectors,s=r.diagonal;for(let t=0;t<s.length;t++)Math.abs(s[t])>e?s[t]=1/s[t]:s[t]=0;return o.mmul(hr.diag(s).mmul(n.transpose()))},qe.solve=Er,qe.wrap=function(t,e){if(Ge(t))return t[0]&&Ge(t[0])?new yr(t):new pr(t,e);throw new Error("the argument is not an array")};const Vr=Cr,zr=Nr,Dr=kr,Pr=Tr,qr=Fr;Or.Matrix&&Or.Matrix;let $r=class t{constructor(t){let e,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=arguments.length>2?arguments[2]:void 0;if(this.U=null,this.S=null,n)return this.center=n.center,this.scale=n.scale,this.means=n.means,this.stdevs=n.stdevs,this.U=zr.checkMatrix(n.U),this.S=n.S,this.R=n.R,void(this.excludedFeatures=n.excludedFeatures||[]);e=(Array.isArray(t),new zr(t));const{isCovarianceMatrix:o=!1,method:s="SVD",nCompNIPALS:i=2,center:u=!0,scale:l=!1,ignoreZeroVariance:a=!1}=r;if(this.center=u,this.scale=l,this.means=null,this.stdevs=null,this.excludedFeatures=[],o)this._computeFromCovarianceMatrix(e);else switch(this._adjust(e,a),s){case"covarianceMatrix":{const t=new Dr(e).mmul(e).div(e.rows-1);this._computeFromCovarianceMatrix(t);break}case"NIPALS":this._computeWithNIPALS(e,i);break;case"SVD":{const t=new qr(e,{computeLeftSingularVectors:!1,computeRightSingularVectors:!0,autoTranspose:!0});this.U=t.rightSingularVectors;const r=t.diagonal,n=[];for(const t of r)n.push(t*t/(e.rows-1));this.S=n;break}default:throw new Error(`unknown method: ${s}`)}}static load(e){if("string"!=typeof e.name)throw new TypeError("model must have a name property");if("PCA"!==e.name)throw new RangeError(`invalid model: ${e.name}`);return new t(void 0,void 0,e)}predict(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};const{nComponents:r=this.U.columns}=e;let n;if(n=(Array.isArray(t),new zr(t)),this.center&&(n.subRowVector(this.means),this.scale)){for(let t of this.excludedFeatures)n.removeColumn(t);n.divRowVector(this.stdevs)}let o=n.mmul(this.U);return o.subMatrix(0,o.rows-1,0,r-1)}invert(t){let e=(t=zr.checkMatrix(t)).mmul(this.U.transpose());return this.center&&(this.scale&&e.mulRowVector(this.stdevs),e.addRowVector(this.means)),e}getExplainedVariance(){let t=0;if(this.S)for(const e of this.S)t+=e;return this.S?this.S.map((e=>e/t)):[]}getCumulativeVariance(){let t=this.getExplainedVariance();for(let e=1;e<t.length;e++)t[e]+=t[e-1];return t}getEigenvectors(){return this.U}getEigenvalues(){return this.S}getStandardDeviations(){return this.S.map((t=>Math.sqrt(t)))}getLoadings(){return this.U.transpose()}toJSON(){return{name:"PCA",center:this.center,scale:this.scale,means:this.means,stdevs:this.stdevs,U:this.U,S:this.S,excludedFeatures:this.excludedFeatures}}_adjust(t,e){if(this.center){const r=t.mean("column"),n=this.scale?t.standardDeviation("column",{mean:r}):null;if(this.means=r,t.subRowVector(r),this.scale){for(let r=0;r<n.length;r++)if(0===n[r]){if(!e)throw new RangeError(`Cannot scale the dataset (standard deviation is zero at index ${r}`);t.removeColumn(r),n.splice(r,1),this.excludedFeatures.push(r),r--}this.stdevs=n,t.divRowVector(n)}}}_computeFromCovarianceMatrix(t){const e=new Vr(t,{assumeSymmetric:!0});this.U=e.eigenvectorMatrix,this.U.flipRows(),this.S=e.realEigenvalues,this.S.reverse()}_computeWithNIPALS(t,e){this.U=new zr(e,t.columns),this.S=[];let r=t;for(let t=0;t<e;t++){let e=new Pr(r);this.U.setRow(t,e.w.transpose()),this.S.push(Math.pow(e.s.get(0,0),2)),r=e.xResidual}this.U=this.U.transpose()}};function Lr(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!ce(t))throw new Error("data is not eff. array");let r=wt(e,"scale");me(r)||(r=!0);let n=wt(e,"nCompNIPALS");we(n)||(n=2),n=ke(n);let o=se(wt(t,0,[]));if(0===o)throw new Error("invalid dimension of data");if(n>o)throw new Error(`nCompNIPALS[${n}] is more than the dimension[${o}] of data`);return new $r(t,{method:"NIPALS",scale:r,nCompNIPALS:n}).predict(t).toJSON()}function Ur(t,e){for(var r=-1,n=null==t?0:t.length;++r<n&&!1!==e(t[r],r,t););return t}var Gr,Br=function(t,e,r){for(var n=-1,o=Object(t),s=r(t),i=s.length;i--;){var u=s[Gr?i:++n];if(!1===e(o[u],u,o))break}return t};function Wr(t){return g(t)&&"[object Arguments]"==m(t)}var Kr=Object.prototype,Qr=Kr.hasOwnProperty,Xr=Kr.propertyIsEnumerable,Yr=Wr(function(){return arguments}())?Wr:function(t){return g(t)&&Qr.call(t,"callee")&&!Xr.call(t,"callee")},Jr=Yr;var Hr="object"==typeof exports&&exports&&!exports.nodeType&&exports,Zr=Hr&&"object"==typeof module&&module&&!module.nodeType&&module,tn=Zr&&Zr.exports===Hr?n.Buffer:void 0,en=(tn?tn.isBuffer:void 0)||function(){return!1},rn=9007199254740991,nn=/^(?:0|[1-9]\d*)$/;function on(t,e){var r=typeof t;return!!(e=null==e?rn:e)&&("number"==r||"symbol"!=r&&nn.test(t))&&t>-1&&t%1==0&&t<e}var sn={};sn["[object Float32Array]"]=sn["[object Float64Array]"]=sn["[object Int8Array]"]=sn["[object Int16Array]"]=sn["[object Int32Array]"]=sn["[object Uint8Array]"]=sn["[object Uint8ClampedArray]"]=sn["[object Uint16Array]"]=sn["[object Uint32Array]"]=!0,sn["[object Arguments]"]=sn["[object Array]"]=sn["[object ArrayBuffer]"]=sn["[object Boolean]"]=sn["[object DataView]"]=sn["[object Date]"]=sn["[object Error]"]=sn["[object Function]"]=sn["[object Map]"]=sn["[object Number]"]=sn["[object Object]"]=sn["[object RegExp]"]=sn["[object Set]"]=sn["[object String]"]=sn["[object WeakMap]"]=!1;var un="object"==typeof exports&&exports&&!exports.nodeType&&exports,ln=un&&"object"==typeof module&&module&&!module.nodeType&&module,an=ln&&ln.exports===un&&e.process,hn=function(){try{var t=ln&&ln.require&&ln.require("util").types;return t||an&&an.binding&&an.binding("util")}catch(t){}}(),cn=hn&&hn.isTypedArray,fn=cn?function(t){return function(e){return t(e)}}(cn):function(t){return g(t)&&Dt(t.length)&&!!sn[m(t)]},mn=fn,gn=Object.prototype.hasOwnProperty;function wn(e,r){var n=t(e),o=!n&&Jr(e),s=!n&&!o&&en(e),i=!n&&!o&&!s&&mn(e),u=n||o||s||i,l=u?function(t,e){for(var r=-1,n=Array(t);++r<t;)n[r]=e(r);return n}(e.length,String):[],a=l.length;for(var h in e)!r&&!gn.call(e,h)||u&&("length"==h||s&&("offset"==h||"parent"==h)||i&&("buffer"==h||"byteLength"==h||"byteOffset"==h)||on(h,a))||l.push(h);return l}function dn(t){return Pt(t)?wn(t):vt(t)}var pn=function(t,e){return function(r,n){if(null==r)return r;if(!Pt(r))return t(r,n);for(var o=r.length,s=e?o:-1,i=Object(r);(e?s--:++s<o)&&!1!==n(i[s],s,i););return r}}((function(t,e){return t&&Br(t,e,dn)})),yn=pn;function bn(t){return t}function vn(e,r){var n;return(t(e)?Ur:yn)(e,"function"==typeof(n=r)?n:bn)}function Mn(t){var e=this.__data__=new W(t);this.size=e.size}Mn.prototype.clear=function(){this.__data__=new W,this.size=0},Mn.prototype.delete=function(t){var e=this.__data__,r=e.delete(t);return this.size=e.size,r},Mn.prototype.get=function(t){return this.__data__.get(t)},Mn.prototype.has=function(t){return this.__data__.has(t)},Mn.prototype.set=function(t,e){var r=this.__data__;if(r instanceof W){var n=r.__data__;if(!K||n.length<199)return n.push([t,e]),this.size=++r.size,this;r=this.__data__=new X(n)}return r.set(t,e),this.size=r.size,this};function xn(t){var e=-1,r=null==t?0:t.length;for(this.__data__=new X;++e<r;)this.add(t[e])}function En(t,e){for(var r=-1,n=null==t?0:t.length;++r<n;)if(e(t[r],r,t))return!0;return!1}function Sn(t,e){return t.has(e)}xn.prototype.add=xn.prototype.push=function(t){return this.__data__.set(t,"__lodash_hash_undefined__"),this},xn.prototype.has=function(t){return this.__data__.has(t)};var _n=1,jn=2;function An(t,e,r,n,o,s){var i=r&_n,u=t.length,l=e.length;if(u!=l&&!(i&&l>u))return!1;var a=s.get(t),h=s.get(e);if(a&&h)return a==e&&h==t;var c=-1,f=!0,m=r&jn?new xn:void 0;for(s.set(t,e),s.set(e,t);++c<u;){var g=t[c],w=e[c];if(n)var d=i?n(w,g,c,e,t,s):n(g,w,c,t,e,s);if(void 0!==d){if(d)continue;f=!1;break}if(m){if(!En(e,(function(t,e){if(!Sn(m,e)&&(g===t||o(g,t,r,n,s)))return m.push(e)}))){f=!1;break}}else if(g!==w&&!o(g,w,r,n,s)){f=!1;break}}return s.delete(t),s.delete(e),f}var Rn=n.Uint8Array;function In(t){var e=-1,r=Array(t.size);return t.forEach((function(t,n){r[++e]=[n,t]})),r}function Cn(t){var e=-1,r=Array(t.size);return t.forEach((function(t){r[++e]=t})),r}var Nn=1,kn=2,Tn="[object Boolean]",Fn="[object Date]",On="[object Error]",Vn="[object Map]",zn="[object Number]",Dn="[object RegExp]",Pn="[object Set]",qn="[object String]",$n="[object Symbol]",Ln="[object ArrayBuffer]",Un="[object DataView]",Gn=o?o.prototype:void 0,Bn=Gn?Gn.valueOf:void 0;var Wn=Object.prototype.propertyIsEnumerable,Kn=Object.getOwnPropertySymbols,Qn=Kn?function(t){return null==t?[]:(t=Object(t),function(t,e){for(var r=-1,n=null==t?0:t.length,o=0,s=[];++r<n;){var i=t[r];e(i,r,t)&&(s[o++]=i)}return s}(Kn(t),(function(e){return Wn.call(t,e)})))}:function(){return[]},Xn=Qn;function Yn(e){return function(e,r,n){var o=r(e);return t(e)?o:function(t,e){for(var r=-1,n=e.length,o=t.length;++r<n;)t[o+r]=e[r];return t}(o,n(e))}(e,dn,Xn)}var Jn=1,Hn=Object.prototype.hasOwnProperty;var Zn=1,to="[object Arguments]",eo="[object Array]",ro="[object Object]",no=Object.prototype.hasOwnProperty;function oo(e,r,n,o,s,i){var u=t(e),l=t(r),a=u?eo:Vt(e),h=l?eo:Vt(r),c=(a=a==to?ro:a)==ro,f=(h=h==to?ro:h)==ro,m=a==h;if(m&&en(e)){if(!en(r))return!1;u=!0,c=!1}if(m&&!c)return i||(i=new Mn),u||mn(e)?An(e,r,n,o,s,i):function(t,e,r,n,o,s,i){switch(r){case Un:if(t.byteLength!=e.byteLength||t.byteOffset!=e.byteOffset)return!1;t=t.buffer,e=e.buffer;case Ln:return!(t.byteLength!=e.byteLength||!s(new Rn(t),new Rn(e)));case Tn:case Fn:case zn:return U(+t,+e);case On:return t.name==e.name&&t.message==e.message;case Dn:case qn:return t==e+"";case Vn:var u=In;case Pn:var l=n&Nn;if(u||(u=Cn),t.size!=e.size&&!l)return!1;var a=i.get(t);if(a)return a==e;n|=kn,i.set(t,e);var h=An(u(t),u(e),n,o,s,i);return i.delete(t),h;case $n:if(Bn)return Bn.call(t)==Bn.call(e)}return!1}(e,r,a,n,o,s,i);if(!(n&Zn)){var g=c&&no.call(e,"__wrapped__"),w=f&&no.call(r,"__wrapped__");if(g||w){var d=g?e.value():e,p=w?r.value():r;return i||(i=new Mn),s(d,p,n,o,i)}}return!!m&&(i||(i=new Mn),function(t,e,r,n,o,s){var i=r&Jn,u=Yn(t),l=u.length;if(l!=Yn(e).length&&!i)return!1;for(var a=l;a--;){var h=u[a];if(!(i?h in e:Hn.call(e,h)))return!1}var c=s.get(t),f=s.get(e);if(c&&f)return c==e&&f==t;var m=!0;s.set(t,e),s.set(e,t);for(var g=i;++a<l;){var w=t[h=u[a]],d=e[h];if(n)var p=i?n(d,w,h,e,t,s):n(w,d,h,t,e,s);if(!(void 0===p?w===d||o(w,d,r,n,s):p)){m=!1;break}g||(g="constructor"==h)}if(m&&!g){var y=t.constructor,b=e.constructor;y==b||!("constructor"in t)||!("constructor"in e)||"function"==typeof y&&y instanceof y&&"function"==typeof b&&b instanceof b||(m=!1)}return s.delete(t),s.delete(e),m}(e,r,n,o,s,i))}function so(t,e,r,n,o){return t===e||(null==t||null==e||!g(t)&&!g(e)?t!=t&&e!=e:oo(t,e,r,n,so,o))}var io=1,uo=2;function lo(t){return t==t&&!v(t)}function ao(t,e){return function(r){return null!=r&&(r[t]===e&&(void 0!==e||t in Object(r)))}}function ho(t){var e=function(t){for(var e=dn(t),r=e.length;r--;){var n=e[r],o=t[n];e[r]=[n,o,lo(o)]}return e}(t);return 1==e.length&&e[0][2]?ao(e[0][0],e[0][1]):function(r){return r===t||function(t,e,r,n){var o=r.length,s=o,i=!n;if(null==t)return!s;for(t=Object(t);o--;){var u=r[o];if(i&&u[2]?u[1]!==t[u[0]]:!(u[0]in t))return!1}for(;++o<s;){var l=(u=r[o])[0],a=t[l],h=u[1];if(i&&u[2]){if(void 0===a&&!(l in t))return!1}else{var c=new Mn;if(n)var f=n(a,h,l,t,e,c);if(!(void 0===f?so(h,a,io|uo,n,c):f))return!1}}return!0}(r,t,e)}}function co(t,e){return null!=t&&e in Object(t)}function fo(e,r){return null!=e&&function(e,r,n){for(var o=-1,s=(r=ct(r,e)).length,i=!1;++o<s;){var u=mt(r[o]);if(!(i=null!=e&&n(e,u)))break;e=e[u]}return i||++o!=s?i:!!(s=null==e?0:e.length)&&Dt(s)&&on(u,s)&&(t(e)||Jr(e))}(e,r,co)}var mo=1,go=2;function wo(t){return b(t)?$t(mt(t)):function(t){return function(e){return gt(e,t)}}(t)}function po(e){return"function"==typeof e?e:null==e?bn:"object"==typeof e?t(e)?(r=e[0],n=e[1],b(r)&&lo(n)?ao(mt(r),n):function(t){var e=wt(t,r);return void 0===e&&e===n?fo(t,r):so(n,e,mo|go)}):ho(e):wo(e);var r,n}function yo(t,e){var r=-1,n=Pt(t)?Array(t.length):[];return yn(t,(function(t,o,s){n[++r]=e(t,o,s)})),n}function bo(e,r){return(t(e)?st:yo)(e,po(r))}function vo(t){return null==t?[]:function(t,e){return st(e,(function(e){return t[e]}))}(t,dn(t))}function Mo(t,e){if(!ue(t))return{};if(!ce(e))return{};let r={};return vn(e,(function(e){(function(t,e){return!!ue(t)&&!(!ge(e)&&!we(e))&&e in t})(t,e)&&(r[e]=wt(t,e))})),r}var xo={},Eo={};Object.defineProperty(Eo,"__esModule",{value:!0});class So{constructor(t){this.getCost=()=>this.Elements.map((t=>t.DistanceFromMedoid)).reduce(((t,e)=>t+e),0),this.findBestMedoidIdx=t=>this.Elements.map((e=>this.calcCostForGivenElementAsMedoid(e,t))).reduce(((t,e,r,n)=>e.CostIfMadeMedoid<n[t].CostIfMadeMedoid?r:t),0),this.calcCostForGivenElementAsMedoid=(t,e)=>({CostIfMadeMedoid:new So({Elements:this.Elements.map((r=>({DistanceFromMedoid:e(r.Element,t.Element),Element:r.Element}))),Medoid:t.Element}).getCost(),Element:t}),this.Elements=t.Elements,this.Medoid=t.Medoid}}Eo.Cluster=So;var _o={},jo={};Object.defineProperty(jo,"__esModule",{value:!0}),jo.distance=(t,e)=>{if(t.length!==e.length)throw new Error("the inputs must have the same dimension");const r=Array.from(t.keys()).map((r=>Math.pow(Math.abs(t[r]-e[r]),2))).reduce(((t,e)=>t+e),0);return Math.sqrt(r)};var Ao={};Object.defineProperty(Ao,"__esModule",{value:!0}),Ao.kGtElementArrLength="k must be less than the number of elements in the input array",Object.defineProperty(_o,"__esModule",{value:!0});const Ro=jo,Io=Ao,Co=Eo;class No{constructor(t,e,r){this.Elements=t,this.K=e,this.DistanceFn=r,this.OptimisationCompleted=!1,this.iterate=()=>{this.IterationCount++,this.Clusters||this.allocateToClustersAroundCurrentMedoids();const t=this.Medoids,e=this.Clusters,r=this.calculateCurrentCost();let n=r;for(let t=0;t<this.Medoids.length;t++){const e=t=>0===this.Medoids.filter((e=>0===this.DistanceFn(t,e))).length;for(const r of this.Elements.filter(e)){const e=this.Medoids.slice();e[t]=r;const o=new No(this.Elements.slice(),this.K,this.DistanceFn);o.Medoids=e,o.allocateToClustersAroundCurrentMedoids();const s=o.calculateCurrentCost();s<n&&(n=s,this.Clusters=o.Clusters,this.Medoids=e)}}return this.calculateCurrentCost()<r||(this.Clusters=e,this.Medoids=t,this.OptimisationCompleted=!0,!1)},this.getClusteredData=()=>(this.OptimisationCompleted||this.runToCompletion(),this.Clusters.map((t=>t.Elements.map((t=>t.Element))))),this.runToCompletion=()=>{for(;this.iterate(););},this.allocateToClustersAroundCurrentMedoids=()=>{this.Clusters=new Array,this.Medoids.forEach((t=>{this.Clusters.push(new Co.Cluster({Elements:[],Medoid:t}))})),this.Elements.forEach(((t,e)=>{const r=this.findDistances(this.Clusters.map((t=>t.Medoid)),t),n=r.reduce(((t,e,r,n)=>e<n[t]?r:t),0);this.Clusters[n].Elements.push({DistanceFromMedoid:r[n],Element:t})}))},this.calculateCurrentCost=()=>this.Clusters.map((t=>t.getCost())).reduce(((t,e)=>t+e),0),this.findDistances=(t,e)=>t.map((t=>this.DistanceFn(t,e))),this.selectInitialMedoids=()=>{if(this.K>=this.Elements.length)throw Io.kGtElementArrLength;const t=[],e=(t,e)=>{for(const r of e)if(0===this.DistanceFn(r,t))return!1;return!0};let r=0;for(;r<this.Elements.length&&t.length<this.K;)e(this.Elements[r],t)&&t.push(this.Elements[r]),r++;return t},this.DistanceFn=this.DistanceFn||Ro.distance,this.Medoids=this.selectInitialMedoids(),this.IterationCount=0}get OptimisationStarted(){return this.IterationCount>0}}No.clusterElements=(t,e,r)=>{No.getInstance(t,e,r);return new Array},No.getInstance=(t,e,r)=>new No(t,e,r),_o.Clusterer=No,function(t){function e(e){for(var r in e)t.hasOwnProperty(r)||(t[r]=e[r])}Object.defineProperty(t,"__esModule",{value:!0}),e(Eo),e(_o)}(xo);var ko=Pe(xo);function To(t,e){let r=0;for(let n=0;n<t.length;n++)r+=(t[n]-e[n])*(t[n]-e[n]);return r}const Fo=Object.prototype.toString;function Oo(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};const{minLength:r}=e;if(!function(t){const e=Fo.call(t);return e.endsWith("Array]")&&!e.includes("Big")}(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");if("number"!=typeof t[0])throw new TypeError("input must contain numbers");if(r&&t.length<r)throw new Error(`input must have a length of at least ${r}`)}function Vo(t,e){let r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};const{sorted:n=!0}=r;if(n){let r=0,n=t.length-1,o=0;for(;n-r>1;)if(o=r+(n-r>>1),t[o]<e)r=o;else{if(!(t[o]>e))return o;n=o}return r<t.length-1?Math.abs(e-t[r])<Math.abs(t[r+1]-e)?r:r+1:r}{let r=0,n=Number.POSITIVE_INFINITY;for(let o=0;o<t.length;o++){const s=Math.abs(t[o]-e);s<n&&(n=s,r=o)}return r}}function zo(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},{fromIndex:r,toIndex:n}=e;const{from:o,to:s}=e;return void 0===r&&(r=void 0!==o?Vo(t,o):0),void 0===n&&(n=void 0!==s?Vo(t,s):t.length-1),r<0&&(r=0),n<0&&(n=0),r>=t.length&&(r=t.length-1),n>=t.length&&(n=t.length-1),r>n&&([r,n]=[n,r]),{fromIndex:r,toIndex:n}}function Do(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};Oo(t);const{fromIndex:r,toIndex:n}=zo(t,e);let o=t[r];for(let e=r+1;e<=n;e++)o+=t[e];return o/(n-r+1)}function Po(t,e){const r=65535&(t>>>=0);return((t-r)*(e>>>=0)>>>0)+r*e>>>0}class qo{constructor(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Date.now();De(this,"random",void 0),De(this,"state",void 0),this.state=new Uint32Array(4),this.init(t),this.random=this.getFloat.bind(this)}getUint32(){return this.nextState(),this.state[3]+this.state[2]>>>0}getFloat(){return 5.960464477539063e-8*(this.getUint32()>>>8)}init(t){if(!Number.isInteger(t))throw new TypeError("seed must be an integer");this.state[0]=t,this.state[1]=0,this.state[2]=0,this.state[3]=0;for(let t=1;t<8;t++)this.state[3&t]^=t+Po(1812433253,this.state[t-1&3]^this.state[t-1&3]>>>30>>>0)>>>0;this.periodCertification();for(let t=0;t<8;t++)this.nextState()}periodCertification(){0===this.state[0]&&0===this.state[1]&&0===this.state[2]&&0===this.state[3]&&(this.state[0]=88,this.state[1]=83,this.state[2]=65,this.state[3]=68)}nextState(){let t=this.state[0];t^=t<<15,t^=t>>>18,t^=this.state[3]<<11,this.state[0]=this.state[1],this.state[1]=this.state[2],this.state[2]=this.state[3],this.state[3]=t}}function $o(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};Oo(t);const{fromIndex:r,toIndex:n}=zo(t,e);let o=t[r];for(let e=r+1;e<=n;e++)o+=t[e];return o}const Lo={distanceFunction:To};function Uo(t,e){let r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:Lo;const n=r.distanceFunction||Lo.distanceFunction,o=r.similarityFunction||Lo.similarityFunction;let s=-1;if("function"==typeof o){let r=Number.MIN_VALUE;for(let n=0;n<t.length;n++){const i=o(e,t[n]);i>r&&(r=i,s=n)}}else{if("function"!=typeof n)throw new Error("A similarity or distance function it's required");{let r=Number.MAX_VALUE;for(let o=0;o<t.length;o++){const i=n(e,t[o]);i<r&&(r=i,s=o)}}}return s}function Go(t,e,r,n){const o={distanceFunction:n};for(let n=0;n<t.length;n++)r[n]=Uo(e,t[n],o);return r}class Bo{constructor(t,e,r,n,o){De(this,"clusters",void 0),De(this,"centroids",void 0),De(this,"converged",void 0),De(this,"iterations",void 0),De(this,"distance",void 0),this.clusters=t,this.centroids=e,this.converged=r,this.iterations=n,this.distance=o}nearest(t){const e=new Array(t.length);return Go(t,this.centroids,e,this.distance)}computeInformation(t){const e=this.centroids.map((()=>[]));for(let r=0;r<t.length;r++){const n=this.clusters[r];e[n].push(this.distance(t[r],this.centroids[n]))}return this.centroids.map(((t,r)=>{const n=e[r];return{centroid:t,error:n.length>0?Do(n):-1,size:n.length}}))}}function Wo(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:Math.random;const{size:n=1,replace:o=!1,probabilities:s}=e;let i,u;if(i="number"==typeof t?function(t){const e=[];for(let r=0;r<t;r++)e.push(r);return e}(t):t.slice(),s){if(!o)throw new Error("choice with probabilities and no replacement is not implemented");if(s.length!==i.length)throw new Error("the length of probabilities option should be equal to the number of choices");u=[s[0]];for(let t=1;t<s.length;t++)u[t]=u[t-1]+s[t];if(Math.abs(1-u.at(-1))>1e-8)throw new Error(`probabilities should sum to 1, but instead sums to ${u.at(-1)}`)}if(!o&&n>i.length)throw new Error("size option is too large");const l=[];for(let t=0;t<n;t++){const t=Ko(i.length,r,u);l.push(i[t]),o||i.splice(t,1)}return l}function Ko(t,e,r){const n=e();if(r){let t=0;for(;n>r[t];)t++;return t}return Math.floor(n*t)}class Qo{constructor(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Math.random;if(De(this,"randomGenerator",void 0),"number"==typeof t){const e=new qo(t);this.randomGenerator=e.random}else this.randomGenerator=t}choice(t,e){return Wo(t,e,this.randomGenerator)}random(){return this.randomGenerator()}randInt(t,e){return void 0===e&&(e=t,t=0),t+Math.floor(this.randomGenerator()*(e-t))}randomSample(t){const e=[];for(let r=0;r<t;r++)e.push(this.random());return e}}function Xo(t,e){const r=new zr(t.rows,e.rows);for(let n=0;n<t.rows;n++)for(let o=0;o<e.rows;o++)r.set(n,o,To(t.getRow(n),e.getRow(o)));return r}const Yo={maxIterations:100,tolerance:1e-6,initialization:"kmeans++",distanceFunction:To};function Jo(t,e,r,n,o,s){const i=function(t,e,r,n){const o=e[0].length,s=new Array(n),i=new Array(n);for(let t=0;t<n;t++){s[t]=new Array(o),i[t]=0;for(let e=0;e<o;e++)s[t][e]=0}for(let t=0;t<e.length;t++){i[r[t]]++;for(let n=0;n<o;n++)s[r[t]][n]+=e[t][n]}for(let e=0;e<n;e++)for(let r=0;r<o;r++)i[e]?s[e][r]/=i[e]:s[e][r]=t[e][r];return s}(t,e,r=Go(e,t,r,o.distanceFunction),n),u=function(t,e,r,n){for(let o=0;o<t.length;o++)if(r(t[o],e[o])>n)return!1;return!0}(i,t,o.distanceFunction,o.tolerance);return new Bo(r,i,u,s,o.distanceFunction)}function Ho(t,e,r){const n=function(t){return{...Yo,...t}}(r);!function(t,e){if(e<=0||e>t.length||!Number.isInteger(e))throw new Error("K should be a positive integer smaller than the number of points")}(t,e);let o=function(t,e,r){let n;if(Array.isArray(r.initialization)){if(r.initialization.length!==e)throw new Error("The initial centers should have the same length as K");n=r.initialization}else switch(r.initialization){case"kmeans++":n=function(t,e){let r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};const n=new zr(t),o=n.rows,s=new Qo(r.seed),i=[],u=r.localTrials||2+Math.floor(Math.log(e)),l=s.randInt(o);i.push(n.getRow(l));let a=new zr(1,n.rows);for(let t=0;t<n.rows;t++)a.set(0,t,To(n.getRow(t),i[0]));let h=zr.mul(a,1/$o(a.getRow(0)));for(let t=1;t<e;t++){const e=s.choice(o,{replace:!0,size:u,probabilities:h.getRow(0)}),r=Xo(n.subMatrixRow(e),n);let l=1/0,c=1/0,f=a;for(let t=0;t<u;t++){const n=zr.min(a,[r.getRow(t)]),o=n.sum();o<c&&(l=e[t],c=o,f=n)}i[t]=n.getRow(l),a=f,h=zr.mul(a,1/$o(a.getRow(0)))}return i}(t,e,r);break;case"random":n=function(t,e,r){return new Qo(r).choice(t,{size:e})}(t,e,r.seed);break;case"mostDistant":n=function(t,e,r,n){const o=new Qo(n),s=new Array(e);if(s[0]=Math.floor(o.random()*t.length),e>1){const n={dist:-1,index:-1};for(let e=0;e<t.length;++e)r[s[0]][e]>n.dist&&(n.dist=r[s[0]][e],n.index=e);if(s[1]=n.index,e>2)for(let n=2;n<e;++n){let e={dist:-1,index:-1};for(let o=0;o<t.length;++o){let t={dist:Number.MAX_VALUE,index:-1};if(!s.includes(o))for(let e=0;e<n;++e)r[s[e]][o]<t.dist&&(t={dist:r[s[e]][o],index:o});t.dist!==Number.MAX_VALUE&&t.dist>e.dist&&(e={...t})}s[n]=e.index}}return s.map((e=>t[e]))}(t,e,function(t,e){const r=new Array(t.length);for(let e=0;e<t.length;++e)r[e]=new Array(t.length);for(let n=0;n<t.length;++n)for(let o=n;o<t.length;++o){const s=e(t[n],t[o]);r[n][o]=s,r[o][n]=s}return r}(t,r.distanceFunction),r.seed);break;default:!function(t,e){throw new Error(`${e}: "${String(t)}"`)}(r.initialization,"Unknown initialization method")}return n}(t,e,n);0===n.maxIterations&&(n.maxIterations=Number.MAX_VALUE);const s=new Array(t.length);let i,u=!1,l=0;for(;!u&&l<n.maxIterations;)i=Jo(o,t,s,e,n,++l),u=i.converged,o=i.centroids;if(!i)throw new Error("unreachable: no kmeans step executed");return i}class Zo{constructor(t){this.rows=t}len(){return this.rows.length}isSquare(){const t=this.rows.length;for(let e=0;e<t;e++)if(this.rows[e].length!==t)return!1;return!0}get(t,e){return this.rows[t][e]}}const ts=4294967295;class es{constructor(t,e){this.i=t,this.d=e}clone(){return new es(this.i,this.d)}static empty(){return new es(ts,0)}}class rs{constructor(t,e,r,n){this.near=new es(t,e),this.seco=new es(r,n)}clone(){return new rs(this.near.i,this.near.d,this.seco.i,this.seco.d)}static empty(){return new rs(ts,0,ts,0)}}function ns(t,e,r){const n=(t=function(t){if(t&&"function"==typeof t.get&&"function"==typeof t.len)return t;if(Array.isArray(t))return new Zo(t);throw new Error("Unsupported matrix input: provide a 2D array, a DenseMatrix, or a LowerTriangle")}(t)).len(),o=e.length;if(1===o){const r=new Array(n).fill(0),[o,s]=function(t,e,r,n){const o=r[n];let s=o,i=0;for(let r=0;r<e.length;r++)o!==r&&e[r]===n&&(i+=t.get(o,r));for(let r=0;r<e.length;r++)if(r!==o&&e[r]===n){let o=0;for(let s=0;s<e.length;s++)s!==r&&e[s]===n&&(o+=t.get(r,s));o<i&&(s=r,i=o)}return r[n]=s,[s!==o,i]}(t,r,e,0);return{loss:s,assi:r,nIter:1,nSwaps:o?1:0}}let[s,i]=function(t,e){const r=t.len(),n=e.length;if(!t.isSquare())throw new Error("Dissimilarity matrix is not square");if(!(r<=ts))throw new Error("N is too large");if(!(n>0&&n<ts))throw new Error("invalid N");if(!(n<=r))throw new Error("k must be at most N");const o=Array.from({length:t.len()},(()=>rs.empty())),s=e[0];let i=0;for(let r=0;r<o.length;r++){o[r]=new rs(0,t.get(r,s),ts,0);const n=o[r];for(let o=1;o<e.length;o++){const s=e[o],i=t.get(r,s);i<n.near.d||r===s?(n.seco=n.near.clone(),n.near=new es(o,i)):(n.seco.i===ts||i<n.seco.d)&&(n.seco=new es(o,i))}i+=n.near.d}return[i,o]}(t,e),u=new Array(o).fill(0);ss(i,u);let l=n,a=0,h=0;for(;h<r;){h+=1;const r=a,o=s;for(let r=0;r<n&&r!==l;r++){if(r===e[i[r].near.i])continue;const[n,o]=os(t,u,i,r);n>=0||(a+=1,l=r,s=us(t,e,i,o,r),ss(i,u))}if(a===r||s>=o)break}return{loss:s,assi:i.map((t=>t.near.i)),nIter:h,nSwaps:a}}function os(t,e,r,n){const o=e.slice();let s=0;for(let e=0;e<r.length;e++){const i=r[e],u=t.get(e,n);u<i.near.d?(s+=u-i.near.d,o[i.near.i]+=i.near.d-i.seco.d):u<i.seco.d&&(o[i.near.i]+=u-i.seco.d)}const[i,u]=function(t){let e=0,r=t[0];for(let n=1;n<t.length;n++)t[n]<r&&(e=n,r=t[n]);return[e,r]}(o);return[u+s,i]}function ss(t,e){e.fill(0);for(let r=0;r<t.length;r++){const n=t[r];e[n.near.i]+=n.seco.d-n.near.d}}function is(t,e,r,n,o,s){let i=new es(n,s);for(let s=0;s<e.length;s++){const u=e[s];if(s===r||s===n)continue;const l=t.get(o,u);l<i.d&&(i=new es(s,l))}return i}function us(t,e,r,n,o){const s=t.len();if(!(n<e.length))throw new Error("invalid medoid number");if(!(o<s))throw new Error("invalid object number");e[n]=o;let i=0;for(let s=0;s<r.length;s++){const u=r[s];if(s===o){u.near.i!==n&&(u.seco=u.near.clone()),u.near=new es(n,0),i+=0;continue}const l=t.get(s,o);u.near.i===n?l<u.seco.d?u.near=new es(n,l):(u.near=u.seco.clone(),u.seco=is(t,e,u.near.i,n,s,l)):l<u.near.d?(u.seco=u.near.clone(),u.near=new es(n,l)):l<u.seco.d?u.seco=new es(n,l):u.seco.i===n&&(u.seco=is(t,e,u.near.i,n,s,l)),i+=u.near.d}return i}function ls(t,e){let r=0;for(let n=0;n<t.length;n++){let o=t[n]-e[n];r+=o*o}return Math.sqrt(r)}function as(t,e,r){let n=t.length,o=function(t){let e=Object.prototype.toString.call(t);return"[object Function]"===e||"[object AsyncFunction]"===e}(r)?r:ls,s=Array.from({length:n},(()=>new Float64Array(n)));for(let e=0;e<n;e++)for(let r=e+1;r<n;r++){let n=o(t[e],t[r]);s[e][r]=n,s[r][e]=n}var i;let u=ns(s,(i=e,Array.from({length:i},((t,e)=>e))),100),l={};return vn(u.assi,((t,e)=>{l[t]||(l[t]=[]),l[t].push(e)})),vo(l)}async function hs(t,e){let r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},n=wt(r,"funDist",null);return"simple"===wt(r,"useMethod","fasterPAM")?function(t,e,r){let n=new Map;vn(t,((t,e)=>{n.set(t,e)}));let o=ko.Clusterer.getInstance(t,e,r).getClusteredData();return o=bo(o,(t=>bo(t,(t=>n.get(t))))),o}(t,e,n):as(t,e,n)}async function cs(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=wt(e,"kNumber");r=we(r)?ke(r):2;let n=wt(e,"mode");"k-medoids"!==n&&"k-means"!==n&&(n="k-medoids");let o,s=wt(e,"seed",null),i=wt(e,"funDist",null),u=wt(e,"useMethod","fasterPAM");if("k-medoids"===n)o=await hs(t,r,{funDist:i,useMethod:u});else{if("k-means"!==n)throw new Error(`invalid mode[${n}]`);o=await async function(t,e){let r=wt(arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},"seed",null),n={};null!==r&&(n={seed:r});let o=Ho(t,e,n),s={};return vn(o.clusters,((t,e)=>{s[t]||(s[t]=[]),s[t].push(e)})),vo(s)}(t,r,{seed:s})}return o}async function fs(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!ce(t))throw new Error("data is not eff. array");let r=wt(e,"kNumber",null);we(r)||(r=2),r=ke(r);let n=wt(e,"usePCA",null);me(n)||(n=!0);let o=wt(e,"nCompNIPALS",null);we(o)||(o=2),o=ke(o);let s=wt(e,"mode",null),i=wt(e,"seed",null),u=wt(e,"funDist",null),l=wt(e,"useMethod","fasterPAM"),a="",h=se(t),c=0,f=0;if(vn(t,(t=>{!function(t){if(ue(t)){for(let e in t)return!0;return!1}return!1}(t)?ce(t)&&(f+=1):c+=1})),c===h)a="obj";else{if(f!==h)throw new Error("data is not of the same type");a="mat"}let m,g=null;"obj"===a?(g=function(t){let e={};vn(t,(t=>{vn(t,((t,r)=>{e[r]||(e[r]=0),0===e[r]&&we(t)&&(e[r]=1),1!==e[r]||we(t)||(e[r]=-1)}))}));let r=[];return vn(e,((t,e)=>{1===t&&r.push(e)})),r}(t),m=function(t,e){return bo(t,(t=>vo(Mo(t,e))))}(t,g)):m=t;let w=m;n&&(w=Lr(m,{nCompNIPALS:o}));let d=await cs(w,{kNumber:r,mode:s,seed:i,funDist:u,useMethod:l});return{keys:g,ginds:d,gmat:bo(d,(t=>bo(t,(t=>w[t])))),gltdt:bo(d,(e=>bo(e,(e=>t[e]))))}}let ms={PCA:async function(t){return Lr(t,arguments.length>1&&void 0!==arguments[1]?arguments[1]:{})},cluster:async function(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};try{return await fs(t,e)}catch(t){return Promise.reject(t)}}};return ms}));
7
7
  //# sourceMappingURL=w-cluster.umd.js.map