uapca
Version:
Uncertainty-aware principal component analysis.
3 lines (2 loc) • 49.2 kB
JavaScript
// uapca v0.8.0 Copyright 2019 Jochen Görtler
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t=t||self).uapca={})}(this,(function(t){"use strict";var e=function t(e){function r(t,r){var s,o;return t=null==t?0:+t,r=null==r?1:+r,function(){var n;if(null!=s)n=s,s=null;else do{s=2*e()-1,n=2*e()-1,o=s*s+n*n}while(!o||o>1);return t+r*n*Math.sqrt(-2*Math.log(o)/o)}}return r.source=t,r}(Math.random);const r=Object.prototype.toString;var s=function(t){return r.call(t).endsWith("Array]")};function o(t){var e,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!s(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(!s(r.output))throw new TypeError("output option must be an array if specified");e=r.output}else e=new Array(t.length);var o=function(t){if(!s(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");for(var e=t[0],r=1;r<t.length;r++)t[r]<e&&(e=t[r]);return e}(t),n=function(t){if(!s(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");for(var e=t[0],r=1;r<t.length;r++)t[r]>e&&(e=t[r]);return e}(t);if(o===n)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");var i=r.min,h=void 0===i?r.autoMinMax?o:0:i,u=r.max,l=void 0===u?r.autoMinMax?n:1:u;if(h>=l)throw new RangeError("min option must be smaller than max option");for(var a=(l-h)/(n-o),f=0;f<t.length;f++)e[f]=(t[f]-o)*a+h;return e}function n(t,e,r){let s=r?t.rows:t.rows-1;if(e<0||e>s)throw new RangeError("Row index out of range")}function i(t,e,r){let s=r?t.columns:t.columns-1;if(e<0||e>s)throw new RangeError("Column index out of range")}function h(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 u(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 l(t,e){if("object"!=typeof e)throw new TypeError("unexpected type for row indices");if(e.some(e=>e<0||e>=t.rows))throw new RangeError("row indices are out of range");return Array.isArray(e)||(e=Array.from(e)),e}function a(t,e){if("object"!=typeof e)throw new TypeError("unexpected type for column indices");if(e.some(e=>e<0||e>=t.columns))throw new RangeError("column indices are out of range");return Array.isArray(e)||(e=Array.from(e)),e}function f(t,e,r,s,o){if(5!==arguments.length)throw new RangeError("expected 4 arguments");if(m("startRow",e),m("endRow",r),m("startColumn",s),m("endColumn",o),e>r||s>o||e<0||e>=t.rows||r<0||r>=t.rows||s<0||s>=t.columns||o<0||o>=t.columns)throw new RangeError("Submatrix indices are out of range")}function c(t,e=0){let r=[];for(let s=0;s<t;s++)r.push(e);return r}function m(t,e){if("number"!=typeof e)throw new TypeError(`${t} must be a number`)}const g=15,w=10,p=8;function d(t){const e=String(t);if(e.length<=p)return e.padEnd(p," ");const r=t.toPrecision(p-2);if(r.length<=p)return r;const s=t.toExponential(p-2),o=s.indexOf("e"),n=s.substring(o);return s.substring(0,p-n.length)+n}class y{static from1DArray(t,e,r){if(t*e!==r.length)throw new RangeError("data length does not match given dimensions");let s=new b(t,e);for(let o=0;o<t;o++)for(let t=0;t<e;t++)s.set(o,t,r[o*e+t]);return s}static rowVector(t){let e=new b(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 b(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 b(t,e)}static ones(t,e){return new b(t,e).fill(1)}static rand(t,e,r={}){if("object"!=typeof r)throw new TypeError("options must be an object");const{random:s=Math.random}=r;let o=new b(t,e);for(let r=0;r<t;r++)for(let t=0;t<e;t++)o.set(r,t,s());return o}static randInt(t,e,r={}){if("object"!=typeof r)throw new TypeError("options must be an object");const{min:s=0,max:o=1e3,random:n=Math.random}=r;if(!Number.isInteger(s))throw new TypeError("min must be an integer");if(!Number.isInteger(o))throw new TypeError("max must be an integer");if(s>=o)throw new RangeError("min must be smaller than max");let i=o-s,h=new b(t,e);for(let r=0;r<t;r++)for(let t=0;t<e;t++){let e=s+Math.round(n()*i);h.set(r,t,e)}return h}static eye(t,e,r){void 0===e&&(e=t),void 0===r&&(r=1);let s=Math.min(t,e),o=this.zeros(t,e);for(let t=0;t<s;t++)o.set(t,t,r);return o}static diag(t,e,r){let s=t.length;void 0===e&&(e=s),void 0===r&&(r=e);let o=Math.min(s,e,r),n=this.zeros(e,r);for(let e=0;e<o;e++)n.set(e,e,t[e]);return n}static min(t,e){t=this.checkMatrix(t),e=this.checkMatrix(e);let r=t.rows,s=t.columns,o=new b(r,s);for(let n=0;n<r;n++)for(let r=0;r<s;r++)o.set(n,r,Math.min(t.get(n,r),e.get(n,r)));return o}static max(t,e){t=this.checkMatrix(t),e=this.checkMatrix(e);let r=t.rows,s=t.columns,o=new this(r,s);for(let n=0;n<r;n++)for(let r=0;r<s;r++)o.set(n,r,Math.max(t.get(n,r),e.get(n,r)));return o}static checkMatrix(t){return y.isMatrix(t)?t:new b(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}isSymmetric(){if(this.isSquare()){for(let t=0;t<this.rows;t++)for(let e=0;e<=t;e++)if(this.get(t,e)!==this.get(e,t))return!1;return!0}return!1}isEchelonForm(){let t=0,e=0,r=-1,s=!0,o=!1;for(;t<this.rows&&s;){for(e=0,o=!1;e<this.columns&&!1===o;)0===this.get(t,e)?e++:1===this.get(t,e)&&e>r?(o=!0,r=e):(s=!1,o=!0);t++}return s}isReducedEchelonForm(){let t=0,e=0,r=-1,s=!0,o=!1;for(;t<this.rows&&s;){for(e=0,o=!1;e<this.columns&&!1===o;)0===this.get(t,e)?e++:1===this.get(t,e)&&e>r?(o=!0,r=e):(s=!1,o=!0);for(let r=e+1;r<this.rows;r++)0!==this.get(t,r)&&(s=!1);t++}return s}echelonForm(){let t=this.clone(),e=0,r=0;for(;e<t.rows&&r<t.columns;){let s=e;for(let o=e;o<t.rows;o++)t.get(o,r)>t.get(s,r)&&(s=o);if(0===t.get(s,r))r++;else{t.swapRows(e,s);let o=t.get(e,r);for(let s=r;s<t.columns;s++)t.set(e,s,t.get(e,s)/o);for(let s=e+1;s<t.rows;s++){let o=t.get(s,r)/t.get(e,r);t.set(s,r,0);for(let n=r+1;n<t.columns;n++)t.set(s,n,t.get(s,n)-t.get(e,n)*o)}e++,r++}}return t}reducedEchelonForm(){let t=this.echelonForm(),e=t.columns,r=t.rows,s=r-1;for(;s>=0;)if(0===t.maxRow(s))s--;else{let o=0,n=!1;for(;o<r&&!1===n;)1===t.get(s,o)?n=!0:o++;for(let r=0;r<s;r++){let n=t.get(r,o);for(let i=o;i<e;i++){let e=t.get(r,i)-n*t.get(s,i);t.set(r,i,e)}}s--}return t}set(){throw new Error("set method is unimplemented")}get(){throw new Error("get method is unimplemented")}repeat(t={}){if("object"!=typeof t)throw new TypeError("options must be an object");const{rows:e=1,columns:r=1}=t;if(!Number.isInteger(e)||e<=0)throw new TypeError("rows must be a positive integer");if(!Number.isInteger(r)||r<=0)throw new TypeError("columns must be a positive integer");let s=new b(this.rows*e,this.columns*r);for(let t=0;t<e;t++)for(let e=0;e<r;e++)s.setSubMatrix(this,this.rows*t,this.columns*e);return s}fill(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,t);return this}neg(){return this.mulS(-1)}getRow(t){n(this,t);let e=[];for(let r=0;r<this.columns;r++)e.push(this.get(t,r));return e}getRowVector(t){return b.rowVector(this.getRow(t))}setRow(t,e){n(this,t),e=h(this,e);for(let r=0;r<this.columns;r++)this.set(t,r,e[r]);return this}swapRows(t,e){n(this,t),n(this,e);for(let r=0;r<this.columns;r++){let s=this.get(t,r);this.set(t,r,this.get(e,r)),this.set(e,r,s)}return this}getColumn(t){i(this,t);let e=[];for(let r=0;r<this.rows;r++)e.push(this.get(r,t));return e}getColumnVector(t){return b.columnVector(this.getColumn(t))}setColumn(t,e){i(this,t),e=u(this,e);for(let r=0;r<this.rows;r++)this.set(r,t,e[r]);return this}swapColumns(t,e){i(this,t),i(this,e);for(let r=0;r<this.rows;r++){let s=this.get(r,t);this.set(r,t,this.get(r,e)),this.set(r,e,s)}return this}addRowVector(t){t=h(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=h(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=h(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=h(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=u(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)+t[e]);return this}subColumnVector(t){t=u(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)-t[e]);return this}mulColumnVector(t){t=u(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)*t[e]);return this}divColumnVector(t){t=u(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)/t[e]);return this}mulRow(t,e){n(this,t);for(let r=0;r<this.columns;r++)this.set(t,r,this.get(t,r)*e);return this}mulColumn(t,e){i(this,t);for(let r=0;r<this.rows;r++)this.set(r,t,this.get(r,t)*e);return this}max(){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}maxIndex(){let t=this.get(0,0),e=[0,0];for(let r=0;r<this.rows;r++)for(let s=0;s<this.columns;s++)this.get(r,s)>t&&(t=this.get(r,s),e[0]=r,e[1]=s);return e}min(){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}minIndex(){let t=this.get(0,0),e=[0,0];for(let r=0;r<this.rows;r++)for(let s=0;s<this.columns;s++)this.get(r,s)<t&&(t=this.get(r,s),e[0]=r,e[1]=s);return e}maxRow(t){n(this,t);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){n(this,t);let e=this.get(t,0),r=[t,0];for(let s=1;s<this.columns;s++)this.get(t,s)>e&&(e=this.get(t,s),r[1]=s);return r}minRow(t){n(this,t);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){n(this,t);let e=this.get(t,0),r=[t,0];for(let s=1;s<this.columns;s++)this.get(t,s)<e&&(e=this.get(t,s),r[1]=s);return r}maxColumn(t){i(this,t);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){i(this,t);let e=this.get(0,t),r=[0,t];for(let s=1;s<this.rows;s++)this.get(s,t)>e&&(e=this.get(s,t),r[0]=s);return r}minColumn(t){i(this,t);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){i(this,t);let e=this.get(0,t),r=[0,t];for(let s=1;s<this.rows;s++)this.get(s,t)<e&&(e=this.get(s,t),r[0]=s);return r}diag(){let t=Math.min(this.rows,this.columns),e=[];for(let r=0;r<t;r++)e.push(this.get(r,r));return e}norm(t="frobenius"){let e=0;if("max"===t)return this.max();if("frobenius"===t){for(let t=0;t<this.rows;t++)for(let r=0;r<this.columns;r++)e+=this.get(t,r)*this.get(t,r);return Math.sqrt(e)}throw new RangeError(`unknown norm type: ${t}`)}cumulativeSum(){let t=0;for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)t+=this.get(e,r),this.set(e,r,t);return this}dot(t){y.isMatrix(t)&&(t=t.to1DArray());let e=this.to1DArray();if(e.length!==t.length)throw new RangeError("vectors do not have the same size");let r=0;for(let s=0;s<e.length;s++)r+=e[s]*t[s];return r}mmul(t){t=b.checkMatrix(t);let e=this.rows,r=this.columns,s=t.columns,o=new b(e,s),n=new Float64Array(r);for(let i=0;i<s;i++){for(let e=0;e<r;e++)n[e]=t.get(e,i);for(let t=0;t<e;t++){let e=0;for(let s=0;s<r;s++)e+=this.get(t,s)*n[s];o.set(t,i,e)}}return o}strassen2x2(t){t=b.checkMatrix(t);let e=new b(2,2);const r=this.get(0,0),s=t.get(0,0),o=this.get(0,1),n=t.get(0,1),i=this.get(1,0),h=t.get(1,0),u=this.get(1,1),l=t.get(1,1),a=(r+u)*(s+l),f=(i+u)*s,c=r*(n-l),m=u*(h-s),g=(r+o)*l,w=a+m-g+(o-u)*(h+l),p=c+g,d=f+m,y=a-f+c+(i-r)*(s+n);return e.set(0,0,w),e.set(0,1,p),e.set(1,0,d),e.set(1,1,y),e}strassen3x3(t){t=b.checkMatrix(t);let e=new b(3,3);const r=this.get(0,0),s=this.get(0,1),o=this.get(0,2),n=this.get(1,0),i=this.get(1,1),h=this.get(1,2),u=this.get(2,0),l=this.get(2,1),a=this.get(2,2),f=t.get(0,0),c=t.get(0,1),m=t.get(0,2),g=t.get(1,0),w=t.get(1,1),p=t.get(1,2),d=t.get(2,0),y=t.get(2,1),M=t.get(2,2),x=(r-n)*(-c+w),v=(-r+n+i)*(f-c+w),E=(n+i)*(-f+c),S=r*f,R=(-r+u+l)*(f-m+p),A=(-r+u)*(m-p),T=(u+l)*(-f+m),q=(-o+l+a)*(w+d-y),C=(o-a)*(w-y),j=o*d,k=(l+a)*(-d+y),V=(-o+i+h)*(p+d-M),$=(o-h)*(p-M),F=(i+h)*(-d+M),z=S+j+s*g,N=(r+s+o-n-i-l-a)*w+v+E+S+q+j+k,I=S+R+T+(r+s+o-i-h-u-l)*p+j+V+F,D=x+i*(-f+c+g-w-p-d+M)+v+S+j+V+$,P=x+v+E+S+h*y,O=j+V+$+F+n*m,L=S+R+A+l*(-f+m+g-w-p-d+y)+q+C+j,_=q+C+j+k+u*c,J=S+R+A+T+a*M;return e.set(0,0,z),e.set(0,1,N),e.set(0,2,I),e.set(1,0,D),e.set(1,1,P),e.set(1,2,O),e.set(2,0,L),e.set(2,1,_),e.set(2,2,J),e}mmulStrassen(t){t=b.checkMatrix(t);let e=this.clone(),r=e.rows,s=e.columns,o=t.rows,n=t.columns;function i(t,e,r){let s=t.rows,o=t.columns;if(s===e&&o===r)return t;{let s=y.zeros(e,r);return s=s.setSubMatrix(t,0,0)}}s!==o&&console.warn(`Multiplying ${r} x ${s} and ${o} x ${n} matrix: dimensions do not match.`);let h=Math.max(r,o),u=Math.max(s,n);return function t(e,r,s,o){if(s<=512||o<=512)return e.mmul(r);s%2==1&&o%2==1?(e=i(e,s+1,o+1),r=i(r,s+1,o+1)):s%2==1?(e=i(e,s+1,o),r=i(r,s+1,o)):o%2==1&&(e=i(e,s,o+1),r=i(r,s,o+1));let n=parseInt(e.rows/2,10),h=parseInt(e.columns/2,10),u=e.subMatrix(0,n-1,0,h-1),l=r.subMatrix(0,n-1,0,h-1),a=e.subMatrix(0,n-1,h,e.columns-1),f=r.subMatrix(0,n-1,h,r.columns-1),c=e.subMatrix(n,e.rows-1,0,h-1),m=r.subMatrix(n,r.rows-1,0,h-1),g=e.subMatrix(n,e.rows-1,h,e.columns-1),w=r.subMatrix(n,r.rows-1,h,r.columns-1),p=t(y.add(u,g),y.add(l,w),n,h),d=t(y.add(c,g),l,n,h),M=t(u,y.sub(f,w),n,h),b=t(g,y.sub(m,l),n,h),x=t(y.add(u,a),w,n,h),v=t(y.sub(c,u),y.add(l,f),n,h),E=t(y.sub(a,g),y.add(m,w),n,h),S=y.add(p,b);S.sub(x),S.add(E);let R=y.add(M,x),A=y.add(d,b),T=y.sub(p,d);T.add(M),T.add(v);let q=y.zeros(2*S.rows,2*S.columns);return(q=(q=(q=(q=q.setSubMatrix(S,0,0)).setSubMatrix(R,S.rows,0)).setSubMatrix(A,0,S.columns)).setSubMatrix(T,S.rows,S.columns)).subMatrix(0,s-1,0,o-1)}(e=i(e,h,u),t=i(t,h,u),h,u)}scaleRows(t={}){if("object"!=typeof t)throw new TypeError("options must be an object");const{min:e=0,max:r=1}=t;if(!Number.isFinite(e))throw new TypeError("min must be a number");if(!Number.isFinite(r))throw new TypeError("max must be a number");if(e>=r)throw new RangeError("min must be smaller than max");let s=new b(this.rows,this.columns);for(let t=0;t<this.rows;t++){const n=this.getRow(t);o(n,{min:e,max:r,output:n}),s.setRow(t,n)}return s}scaleColumns(t={}){if("object"!=typeof t)throw new TypeError("options must be an object");const{min:e=0,max:r=1}=t;if(!Number.isFinite(e))throw new TypeError("min must be a number");if(!Number.isFinite(r))throw new TypeError("max must be a number");if(e>=r)throw new RangeError("min must be smaller than max");let s=new b(this.rows,this.columns);for(let t=0;t<this.columns;t++){const n=this.getColumn(t);o(n,{min:e,max:r,output:n}),s.setColumn(t,n)}return s}flipRows(){const t=Math.ceil(this.columns/2);for(let e=0;e<this.rows;e++)for(let r=0;r<t;r++){let t=this.get(e,r),s=this.get(e,this.columns-1-r);this.set(e,r,s),this.set(e,this.columns-1-r,t)}return this}flipColumns(){const t=Math.ceil(this.rows/2);for(let e=0;e<this.columns;e++)for(let r=0;r<t;r++){let t=this.get(r,e),s=this.get(this.rows-1-r,e);this.set(r,e,s),this.set(this.rows-1-r,e,t)}return this}kroneckerProduct(t){t=b.checkMatrix(t);let e=this.rows,r=this.columns,s=t.rows,o=t.columns,n=new b(e*s,r*o);for(let i=0;i<e;i++)for(let e=0;e<r;e++)for(let r=0;r<s;r++)for(let h=0;h<o;h++)n.set(s*i+r,o*e+h,this.get(i,e)*t.get(r,h));return n}transpose(){let t=new b(this.columns,this.rows);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)t.set(r,e,this.get(e,r));return t}sortRows(t=M){for(let e=0;e<this.rows;e++)this.setRow(e,this.getRow(e).sort(t));return this}sortColumns(t=M){for(let e=0;e<this.columns;e++)this.setColumn(e,this.getColumn(e).sort(t));return this}subMatrix(t,e,r,s){f(this,t,e,r,s);let o=new b(e-t+1,s-r+1);for(let n=t;n<=e;n++)for(let e=r;e<=s;e++)o.set(n-t,e-r,this.get(n,e));return o}subMatrixRow(t,e,r){if(void 0===e&&(e=0),void 0===r&&(r=this.columns-1),e>r||e<0||e>=this.columns||r<0||r>=this.columns)throw new RangeError("Argument out of range");let s=new b(t.length,r-e+1);for(let o=0;o<t.length;o++)for(let n=e;n<=r;n++){if(t[o]<0||t[o]>=this.rows)throw new RangeError(`Row index out of range: ${t[o]}`);s.set(o,n-e,this.get(t[o],n))}return s}subMatrixColumn(t,e,r){if(void 0===e&&(e=0),void 0===r&&(r=this.rows-1),e>r||e<0||e>=this.rows||r<0||r>=this.rows)throw new RangeError("Argument out of range");let s=new b(r-e+1,t.length);for(let o=0;o<t.length;o++)for(let n=e;n<=r;n++){if(t[o]<0||t[o]>=this.columns)throw new RangeError(`Column index out of range: ${t[o]}`);s.set(n-e,o,this.get(n,t[o]))}return s}setSubMatrix(t,e,r){f(this,e,e+(t=b.checkMatrix(t)).rows-1,r,r+t.columns-1);for(let s=0;s<t.rows;s++)for(let o=0;o<t.columns;o++)this.set(e+s,r+o,t.get(s,o));return this}selection(t,e){let r=function(t,e,r){return{row:l(t,e),column:a(t,r)}}(this,t,e),s=new b(t.length,e.length);for(let t=0;t<r.row.length;t++){let e=r.row[t];for(let o=0;o<r.column.length;o++){let n=r.column[o];s.set(t,o,this.get(e,n))}}return s}trace(){let t=Math.min(this.rows,this.columns),e=0;for(let r=0;r<t;r++)e+=this.get(r,r);return e}clone(){let t=new b(this.rows,this.columns);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)t.set(e,r,this.get(e,r));return t}sum(t){switch(t){case"row":return function(t){let e=c(t.rows);for(let r=0;r<t.rows;++r)for(let s=0;s<t.columns;++s)e[r]+=t.get(r,s);return e}(this);case"column":return function(t){let e=c(t.columns);for(let r=0;r<t.rows;++r)for(let s=0;s<t.columns;++s)e[s]+=t.get(r,s);return e}(this);case void 0:return function(t){let e=0;for(let r=0;r<t.rows;r++)for(let s=0;s<t.columns;s++)e+=t.get(r,s);return e}(this);default:throw new Error(`invalid option: ${t}`)}}product(t){switch(t){case"row":return function(t){let e=c(t.rows,1);for(let r=0;r<t.rows;++r)for(let s=0;s<t.columns;++s)e[r]*=t.get(r,s);return e}(this);case"column":return function(t){let e=c(t.columns,1);for(let r=0;r<t.rows;++r)for(let s=0;s<t.columns;++s)e[s]*=t.get(r,s);return e}(this);case void 0:return function(t){let e=1;for(let r=0;r<t.rows;r++)for(let s=0;s<t.columns;s++)e*=t.get(r,s);return e}(this);default:throw new Error(`invalid option: ${t}`)}}mean(t){const e=this.sum(t);switch(t){case"row":for(let t=0;t<this.rows;t++)e[t]/=this.columns;return e;case"column":for(let t=0;t<this.columns;t++)e[t]/=this.rows;return e;case void 0:return e/this.size;default:throw new Error(`invalid option: ${t}`)}}variance(t,e={}){if("object"==typeof t&&(e=t,t=void 0),"object"!=typeof e)throw new TypeError("options must be an object");const{unbiased:r=!0,mean:s=this.mean(t)}=e;if("boolean"!=typeof r)throw new TypeError("unbiased must be a boolean");switch(t){case"row":if(!Array.isArray(s))throw new TypeError("mean must be an array");return function(t,e,r){const s=t.rows,o=t.columns,n=[];for(let i=0;i<s;i++){let s=0,h=0,u=0;for(let e=0;e<o;e++)s+=u=t.get(i,e)-r[i],h+=u*u;e?n.push((h-s*s/o)/(o-1)):n.push((h-s*s/o)/o)}return n}(this,r,s);case"column":if(!Array.isArray(s))throw new TypeError("mean must be an array");return function(t,e,r){const s=t.rows,o=t.columns,n=[];for(let i=0;i<o;i++){let o=0,h=0,u=0;for(let e=0;e<s;e++)o+=u=t.get(e,i)-r[i],h+=u*u;e?n.push((h-o*o/s)/(s-1)):n.push((h-o*o/s)/s)}return n}(this,r,s);case void 0:if("number"!=typeof s)throw new TypeError("mean must be a number");return function(t,e,r){const s=t.rows,o=t.columns,n=s*o;let i=0,h=0,u=0;for(let e=0;e<s;e++)for(let s=0;s<o;s++)i+=u=t.get(e,s)-r,h+=u*u;return e?(h-i*i/n)/(n-1):(h-i*i/n)/n}(this,r,s);default:throw new Error(`invalid option: ${t}`)}}standardDeviation(t,e){"object"==typeof t&&(e=t,t=void 0);const r=this.variance(t,e);if(void 0===t)return Math.sqrt(r);for(let t=0;t<r.length;t++)r[t]=Math.sqrt(r[t]);return r}center(t,e={}){if("object"==typeof t&&(e=t,t=void 0),"object"!=typeof e)throw new TypeError("options must be an object");const{center:r=this.mean(t)}=e;switch(t){case"row":if(!Array.isArray(r))throw new TypeError("center must be an array");return function(t,e){for(let r=0;r<t.rows;r++)for(let s=0;s<t.columns;s++)t.set(r,s,t.get(r,s)-e[r])}(this,r),this;case"column":if(!Array.isArray(r))throw new TypeError("center must be an array");return function(t,e){for(let r=0;r<t.rows;r++)for(let s=0;s<t.columns;s++)t.set(r,s,t.get(r,s)-e[s])}(this,r),this;case void 0:if("number"!=typeof r)throw new TypeError("center must be a number");return function(t,e){for(let r=0;r<t.rows;r++)for(let s=0;s<t.columns;s++)t.set(r,s,t.get(r,s)-e)}(this,r),this;default:throw new Error(`invalid option: ${t}`)}}scale(t,e={}){if("object"==typeof t&&(e=t,t=void 0),"object"!=typeof e)throw new TypeError("options must be an object");let r=e.scale;switch(t){case"row":if(void 0===r)r=function(t){const e=[];for(let r=0;r<t.rows;r++){let s=0;for(let e=0;e<t.columns;e++)s+=Math.pow(t.get(r,e),2)/(t.columns-1);e.push(Math.sqrt(s))}return e}(this);else if(!Array.isArray(r))throw new TypeError("scale must be an array");return function(t,e){for(let r=0;r<t.rows;r++)for(let s=0;s<t.columns;s++)t.set(r,s,t.get(r,s)/e[r])}(this,r),this;case"column":if(void 0===r)r=function(t){const e=[];for(let r=0;r<t.columns;r++){let s=0;for(let e=0;e<t.rows;e++)s+=Math.pow(t.get(e,r),2)/(t.rows-1);e.push(Math.sqrt(s))}return e}(this);else if(!Array.isArray(r))throw new TypeError("scale must be an array");return function(t,e){for(let r=0;r<t.rows;r++)for(let s=0;s<t.columns;s++)t.set(r,s,t.get(r,s)/e[s])}(this,r),this;case void 0:if(void 0===r)r=function(t){const e=t.size-1;let r=0;for(let s=0;s<t.columns;s++)for(let o=0;o<t.rows;o++)r+=Math.pow(t.get(o,s),2)/e;return Math.sqrt(r)}(this);else if("number"!=typeof r)throw new TypeError("scale must be a number");return function(t,e){for(let r=0;r<t.rows;r++)for(let s=0;s<t.columns;s++)t.set(r,s,t.get(r,s)/e)}(this,r),this;default:throw new Error(`invalid option: ${t}`)}}}function M(t,e){return t-e}y.prototype.klass="Matrix","undefined"!=typeof Symbol&&(y.prototype[Symbol.for("nodejs.util.inspect.custom")]=function(){const t=" ".repeat(2),e=" ".repeat(4);return`${this.constructor.name} {\n${t}[\n${e}${function(t,e){const{rows:r,columns:s}=t,o=Math.min(r,g),n=Math.min(s,w),i=[];for(let e=0;e<o;e++){let r=[];for(let s=0;s<n;s++)r.push(d(t.get(e,s)));i.push(`${r.join(" ")}`)}n!==s&&(i[i.length-1]+=` ... ${s-w} more columns`);o!==r&&i.push(`... ${r-g} more rows`);return i.join(`\n${e}`)}(this,e)}\n${t}]\n${t}rows: ${this.rows}\n${t}columns: ${this.columns}\n}`}),y.random=y.rand,y.randomInt=y.randInt,y.diagonal=y.diag,y.prototype.diagonal=y.prototype.diag,y.identity=y.eye,y.prototype.negate=y.prototype.neg,y.prototype.tensorProduct=y.prototype.kroneckerProduct;class b extends y{constructor(t,e){if(super(),b.isMatrix(t))return t.clone();if(Number.isInteger(t)&&t>0){if(this.data=[],!(Number.isInteger(e)&&e>0))throw new TypeError("nColumns must be a positive integer");for(let r=0;r<t;r++)this.data.push(new Float64Array(e))}else{if(!Array.isArray(t))throw new TypeError("First argument must be a positive number or an array");{const r=t;if(t=r.length,"number"!=typeof(e=r[0].length)||0===e)throw new TypeError("Data must be a 2D array with at least one element");this.data=[];for(let s=0;s<t;s++){if(r[s].length!==e)throw new RangeError("Inconsistent array dimensions");this.data.push(Float64Array.from(r[s]))}}}return this.rows=t,this.columns=e,this}set(t,e,r){return this.data[t][e]=r,this}get(t,e){return this.data[t][e]}removeRow(t){if(n(this,t),1===this.rows)throw new RangeError("A matrix cannot have less than one row");return this.data.splice(t,1),this.rows-=1,this}addRow(t,e){return void 0===e&&(e=t,t=this.rows),n(this,t,!0),e=Float64Array.from(h(this,e)),this.data.splice(t,0,e),this.rows+=1,this}removeColumn(t){if(i(this,t),1===this.columns)throw new RangeError("A matrix cannot have less than one column");for(let e=0;e<this.rows;e++){const r=new Float64Array(this.columns-1);for(let s=0;s<t;s++)r[s]=this.data[e][s];for(let s=t+1;s<this.columns;s++)r[s-1]=this.data[e][s];this.data[e]=r}return this.columns-=1,this}addColumn(t,e){void 0===e&&(e=t,t=this.columns),i(this,t,!0),e=u(this,e);for(let r=0;r<this.rows;r++){const s=new Float64Array(this.columns+1);let o=0;for(;o<t;o++)s[o]=this.data[r][o];for(s[o++]=e[r];o<this.columns+1;o++)s[o]=this.data[r][o-1];this.data[r]=s}return this.columns+=1,this}}!function(t,e){t.prototype.add=function(t){return"number"==typeof t?this.addS(t):this.addM(t)},t.prototype.addS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)+t);return this},t.prototype.addM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)+t.get(e,r));return this},t.add=function(t,r){return new e(t).add(r)},t.prototype.sub=function(t){return"number"==typeof t?this.subS(t):this.subM(t)},t.prototype.subS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)-t);return this},t.prototype.subM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)-t.get(e,r));return this},t.sub=function(t,r){return new e(t).sub(r)},t.prototype.subtract=t.prototype.sub,t.prototype.subtractS=t.prototype.subS,t.prototype.subtractM=t.prototype.subM,t.subtract=t.sub,t.prototype.mul=function(t){return"number"==typeof t?this.mulS(t):this.mulM(t)},t.prototype.mulS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)*t);return this},t.prototype.mulM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)*t.get(e,r));return this},t.mul=function(t,r){return new e(t).mul(r)},t.prototype.multiply=t.prototype.mul,t.prototype.multiplyS=t.prototype.mulS,t.prototype.multiplyM=t.prototype.mulM,t.multiply=t.mul,t.prototype.div=function(t){return"number"==typeof t?this.divS(t):this.divM(t)},t.prototype.divS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)/t);return this},t.prototype.divM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)/t.get(e,r));return this},t.div=function(t,r){return new e(t).div(r)},t.prototype.divide=t.prototype.div,t.prototype.divideS=t.prototype.divS,t.prototype.divideM=t.prototype.divM,t.divide=t.div,t.prototype.mod=function(t){return"number"==typeof t?this.modS(t):this.modM(t)},t.prototype.modS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)%t);return this},t.prototype.modM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)%t.get(e,r));return this},t.mod=function(t,r){return new e(t).mod(r)},t.prototype.modulus=t.prototype.mod,t.prototype.modulusS=t.prototype.modS,t.prototype.modulusM=t.prototype.modM,t.modulus=t.mod,t.prototype.and=function(t){return"number"==typeof t?this.andS(t):this.andM(t)},t.prototype.andS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)&t);return this},t.prototype.andM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)&t.get(e,r));return this},t.and=function(t,r){return new e(t).and(r)},t.prototype.or=function(t){return"number"==typeof t?this.orS(t):this.orM(t)},t.prototype.orS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)|t);return this},t.prototype.orM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)|t.get(e,r));return this},t.or=function(t,r){return new e(t).or(r)},t.prototype.xor=function(t){return"number"==typeof t?this.xorS(t):this.xorM(t)},t.prototype.xorS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)^t);return this},t.prototype.xorM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)^t.get(e,r));return this},t.xor=function(t,r){return new e(t).xor(r)},t.prototype.leftShift=function(t){return"number"==typeof t?this.leftShiftS(t):this.leftShiftM(t)},t.prototype.leftShiftS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)<<t);return this},t.prototype.leftShiftM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)<<t.get(e,r));return this},t.leftShift=function(t,r){return new e(t).leftShift(r)},t.prototype.signPropagatingRightShift=function(t){return"number"==typeof t?this.signPropagatingRightShiftS(t):this.signPropagatingRightShiftM(t)},t.prototype.signPropagatingRightShiftS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)>>t);return this},t.prototype.signPropagatingRightShiftM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)>>t.get(e,r));return this},t.signPropagatingRightShift=function(t,r){return new e(t).signPropagatingRightShift(r)},t.prototype.rightShift=function(t){return"number"==typeof t?this.rightShiftS(t):this.rightShiftM(t)},t.prototype.rightShiftS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)>>>t);return this},t.prototype.rightShiftM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)>>>t.get(e,r));return this},t.rightShift=function(t,r){return new e(t).rightShift(r)},t.prototype.zeroFillRightShift=t.prototype.rightShift,t.prototype.zeroFillRightShiftS=t.prototype.rightShiftS,t.prototype.zeroFillRightShiftM=t.prototype.rightShiftM,t.zeroFillRightShift=t.rightShift,t.prototype.not=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,~this.get(t,e));return this},t.not=function(t){return new e(t).not()},t.prototype.abs=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.abs(this.get(t,e)));return this},t.abs=function(t){return new e(t).abs()},t.prototype.acos=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.acos(this.get(t,e)));return this},t.acos=function(t){return new e(t).acos()},t.prototype.acosh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.acosh(this.get(t,e)));return this},t.acosh=function(t){return new e(t).acosh()},t.prototype.asin=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.asin(this.get(t,e)));return this},t.asin=function(t){return new e(t).asin()},t.prototype.asinh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.asinh(this.get(t,e)));return this},t.asinh=function(t){return new e(t).asinh()},t.prototype.atan=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.atan(this.get(t,e)));return this},t.atan=function(t){return new e(t).atan()},t.prototype.atanh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.atanh(this.get(t,e)));return this},t.atanh=function(t){return new e(t).atanh()},t.prototype.cbrt=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.cbrt(this.get(t,e)));return this},t.cbrt=function(t){return new e(t).cbrt()},t.prototype.ceil=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.ceil(this.get(t,e)));return this},t.ceil=function(t){return new e(t).ceil()},t.prototype.clz32=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.clz32(this.get(t,e)));return this},t.clz32=function(t){return new e(t).clz32()},t.prototype.cos=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.cos(this.get(t,e)));return this},t.cos=function(t){return new e(t).cos()},t.prototype.cosh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.cosh(this.get(t,e)));return this},t.cosh=function(t){return new e(t).cosh()},t.prototype.exp=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.exp(this.get(t,e)));return this},t.exp=function(t){return new e(t).exp()},t.prototype.expm1=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.expm1(this.get(t,e)));return this},t.expm1=function(t){return new e(t).expm1()},t.prototype.floor=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.floor(this.get(t,e)));return this},t.floor=function(t){return new e(t).floor()},t.prototype.fround=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.fround(this.get(t,e)));return this},t.fround=function(t){return new e(t).fround()},t.prototype.log=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.log(this.get(t,e)));return this},t.log=function(t){return new e(t).log()},t.prototype.log1p=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.log1p(this.get(t,e)));return this},t.log1p=function(t){return new e(t).log1p()},t.prototype.log10=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.log10(this.get(t,e)));return this},t.log10=function(t){return new e(t).log10()},t.prototype.log2=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.log2(this.get(t,e)));return this},t.log2=function(t){return new e(t).log2()},t.prototype.round=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.round(this.get(t,e)));return this},t.round=function(t){return new e(t).round()},t.prototype.sign=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.sign(this.get(t,e)));return this},t.sign=function(t){return new e(t).sign()},t.prototype.sin=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.sin(this.get(t,e)));return this},t.sin=function(t){return new e(t).sin()},t.prototype.sinh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.sinh(this.get(t,e)));return this},t.sinh=function(t){return new e(t).sinh()},t.prototype.sqrt=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.sqrt(this.get(t,e)));return this},t.sqrt=function(t){return new e(t).sqrt()},t.prototype.tan=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.tan(this.get(t,e)));return this},t.tan=function(t){return new e(t).tan()},t.prototype.tanh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.tanh(this.get(t,e)));return this},t.tanh=function(t){return new e(t).tanh()},t.prototype.trunc=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.trunc(this.get(t,e)));return this},t.trunc=function(t){return new e(t).trunc()},t.pow=function(t,r){return new e(t).pow(r)},t.prototype.pow=function(t){return"number"==typeof t?this.powS(t):this.powM(t)},t.prototype.powS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,Math.pow(this.get(e,r),t));return this},t.prototype.powM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,Math.pow(this.get(e,r),t.get(e,r)));return this}}(y,b);class x extends y{constructor(t,e,r){super(),this.matrix=t,this.rows=e,this.columns=r}}class v extends x{constructor(t,e,r,s,o){f(t,e,r,s,o),super(t,r-e+1,o-s+1),this.startRow=e,this.startColumn=s}set(t,e,r){return this.matrix.set(this.startRow+t,this.startColumn+e,r),this}get(t,e){return this.matrix.get(this.startRow+t,this.startColumn+e)}}class E extends y{constructor(t){super(),this.data=t,this.rows=t.length,this.columns=t[0].length}set(t,e,r){return this.data[t][e]=r,this}get(t,e){return this.data[t][e]}}function S(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 R{constructor(t,e={}){const{assumeSymmetric:r=!1}=e;if(!(t=E.checkMatrix(t)).isSquare())throw new Error("Matrix is not a square matrix");let s,o,n=t.columns,i=new b(n,n),h=new Float64Array(n),u=new Float64Array(n),l=t,a=!1;if(a=!!r||t.isSymmetric()){for(s=0;s<n;s++)for(o=0;o<n;o++)i.set(s,o,l.get(s,o));!function(t,e,r,s){let o,n,i,h,u,l,a,f;for(u=0;u<t;u++)r[u]=s.get(t-1,u);for(h=t-1;h>0;h--){for(f=0,i=0,l=0;l<h;l++)f+=Math.abs(r[l]);if(0===f)for(e[h]=r[h-1],u=0;u<h;u++)r[u]=s.get(h-1,u),s.set(h,u,0),s.set(u,h,0);else{for(l=0;l<h;l++)r[l]/=f,i+=r[l]*r[l];for(o=r[h-1],n=Math.sqrt(i),o>0&&(n=-n),e[h]=f*n,i-=o*n,r[h-1]=o-n,u=0;u<h;u++)e[u]=0;for(u=0;u<h;u++){for(o=r[u],s.set(u,h,o),n=e[u]+s.get(u,u)*o,l=u+1;l<=h-1;l++)n+=s.get(l,u)*r[l],e[l]+=s.get(l,u)*o;e[u]=n}for(o=0,u=0;u<h;u++)e[u]/=i,o+=e[u]*r[u];for(a=o/(i+i),u=0;u<h;u++)e[u]-=a*r[u];for(u=0;u<h;u++){for(o=r[u],n=e[u],l=u;l<=h-1;l++)s.set(l,u,s.get(l,u)-(o*e[l]+n*r[l]));r[u]=s.get(h-1,u),s.set(h,u,0)}}r[h]=i}for(h=0;h<t-1;h++){if(s.set(t-1,h,s.get(h,h)),s.set(h,h,1),0!==(i=r[h+1])){for(l=0;l<=h;l++)r[l]=s.get(l,h+1)/i;for(u=0;u<=h;u++){for(n=0,l=0;l<=h;l++)n+=s.get(l,h+1)*s.get(l,u);for(l=0;l<=h;l++)s.set(l,u,s.get(l,u)-n*r[l])}}for(l=0;l<=h;l++)s.set(l,h+1,0)}for(u=0;u<t;u++)r[u]=s.get(t-1,u),s.set(t-1,u,0);s.set(t-1,t-1,1),e[0]=0}(n,u,h,i),function(t,e,r,s){let o,n,i,h,u,l,a,f,c,m,g,w,p,d,y,M;for(i=1;i<t;i++)e[i-1]=e[i];e[t-1]=0;let b=0,x=0,v=Number.EPSILON;for(l=0;l<t;l++){for(x=Math.max(x,Math.abs(r[l])+Math.abs(e[l])),a=l;a<t&&!(Math.abs(e[a])<=v*x);)a++;if(a>l)do{for(o=r[l],f=(r[l+1]-o)/(2*e[l]),c=S(f,1),f<0&&(c=-c),r[l]=e[l]/(f+c),r[l+1]=e[l]*(f+c),m=r[l+1],n=o-r[l],i=l+2;i<t;i++)r[i]-=n;for(b+=n,f=r[a],w=g=1,p=g,d=e[l+1],y=0,M=0,i=a-1;i>=l;i--)for(p=w,w=g,M=y,o=g*e[i],n=g*f,c=S(f,e[i]),e[i+1]=y*c,y=e[i]/c,f=(g=f/c)*r[i]-y*o,r[i+1]=n+y*(g*o+y*r[i]),u=0;u<t;u++)n=s.get(u,i+1),s.set(u,i+1,y*s.get(u,i)+g*n),s.set(u,i,g*s.get(u,i)-y*n);f=-y*M*p*d*e[l]/m,e[l]=y*f,r[l]=g*f}while(Math.abs(e[l])>v*x);r[l]=r[l]+b,e[l]=0}for(i=0;i<t-1;i++){for(u=i,f=r[i],h=i+1;h<t;h++)r[h]<f&&(u=h,f=r[h]);if(u!==i)for(r[u]=r[i],r[i]=f,h=0;h<t;h++)f=s.get(h,i),s.set(h,i,s.get(h,u)),s.set(h,u,f)}}(n,u,h,i)}else{let t=new b(n,n),e=new Float64Array(n);for(o=0;o<n;o++)for(s=0;s<n;s++)t.set(s,o,l.get(s,o));!function(t,e,r,s){let o,n,i,h,u,l,a,f=t-1;for(l=1;l<=f-1;l++){for(a=0,h=l;h<=f;h++)a+=Math.abs(e.get(h,l-1));if(0!==a){for(i=0,h=f;h>=l;h--)r[h]=e.get(h,l-1)/a,i+=r[h]*r[h];for(n=Math.sqrt(i),r[l]>0&&(n=-n),i-=r[l]*n,r[l]=r[l]-n,u=l;u<t;u++){for(o=0,h=f;h>=l;h--)o+=r[h]*e.get(h,u);for(o/=i,h=l;h<=f;h++)e.set(h,u,e.get(h,u)-o*r[h])}for(h=0;h<=f;h++){for(o=0,u=f;u>=l;u--)o+=r[u]*e.get(h,u);for(o/=i,u=l;u<=f;u++)e.set(h,u,e.get(h,u)-o*r[u])}r[l]=a*r[l],e.set(l,l-1,a*n)}}for(h=0;h<t;h++)for(u=0;u<t;u++)s.set(h,u,h===u?1:0);for(l=f-1;l>=1;l--)if(0!==e.get(l,l-1)){for(h=l+1;h<=f;h++)r[h]=e.get(h,l-1);for(u=l;u<=f;u++){for(n=0,h=l;h<=f;h++)n+=r[h]*s.get(h,u);for(n=n/r[l]/e.get(l,l-1),h=l;h<=f;h++)s.set(h,u,s.get(h,u)+n*r[h])}}}(n,t,e,i),function(t,e,r,s,o){let n,i,h,u,l,a,f,c,m,g,w,p,d,y,M,b=t-1,x=t-1,v=Number.EPSILON,E=0,S=0,R=0,T=0,q=0,C=0,j=0,k=0;for(n=0;n<t;n++)for((n<0||n>x)&&(r[n]=o.get(n,n),e[n]=0),i=Math.max(n-1,0);i<t;i++)S+=Math.abs(o.get(n,i));for(;b>=0;){for(u=b;u>0&&(0===(C=Math.abs(o.get(u-1,u-1))+Math.abs(o.get(u,u)))&&(C=S),!(Math.abs(o.get(u,u-1))<v*C));)u--;if(u===b)o.set(b,b,o.get(b,b)+E),r[b]=o.get(b,b),e[b]=0,b--,k=0;else if(u===b-1){if(f=o.get(b,b-1)*o.get(b-1,b),R=(o.get(b-1,b-1)-o.get(b,b))/2,T=R*R+f,j=Math.sqrt(Math.abs(T)),o.set(b,b,o.get(b,b)+E),o.set(b-1,b-1,o.get(b-1,b-1)+E),c=o.get(b,b),T>=0){for(j=R>=0?R+j:R-j,r[b-1]=c+j,r[b]=r[b-1],0!==j&&(r[b]=c-f/j),e[b-1]=0,e[b]=0,c=o.get(b,b-1),C=Math.abs(c)+Math.abs(j),R=c/C,T=j/C,q=Math.sqrt(R*R+T*T),R/=q,T/=q,i=b-1;i<t;i++)j=o.get(b-1,i),o.set(b-1,i,T*j+R*o.get(b,i)),o.set(b,i,T*o.get(b,i)-R*j);for(n=0;n<=b;n++)j=o.get(n,b-1),o.set(n,b-1,T*j+R*o.get(n,b)),o.set(n,b,T*o.get(n,b)-R*j);for(n=0;n<=x;n++)j=s.get(n,b-1),s.set(n,b-1,T*j+R*s.get(n,b)),s.set(n,b,T*s.get(n,b)-R*j)}else r[b-1]=c+R,r[b]=c+R,e[b-1]=j,e[b]=-j;b-=2,k=0}else{if(c=o.get(b,b),m=0,f=0,u<b&&(m=o.get(b-1,b-1),f=o.get(b,b-1)*o.get(b-1,b)),10===k){for(E+=c,n=0;n<=b;n++)o.set(n,n,o.get(n,n)-c);C=Math.abs(o.get(b,b-1))+Math.abs(o.get(b-1,b-2)),c=m=.75*C,f=-.4375*C*C}if(30===k&&(C=(C=(m-c)/2)*C+f)>0){for(C=Math.sqrt(C),m<c&&(C=-C),C=c-f/((m-c)/2+C),n=0;n<=b;n++)o.set(n,n,o.get(n,n)-C);E+=C,c=m=f=.964}for(k+=1,l=b-2;l>=u&&(j=o.get(l,l),R=((q=c-j)*(C=m-j)-f)/o.get(l+1,l)+o.get(l,l+1),T=o.get(l+1,l+1)-j-q-C,q=o.get(l+2,l+1),C=Math.abs(R)+Math.abs(T)+Math.abs(q),R/=C,T/=C,q/=C,l!==u)&&!(Math.abs(o.get(l,l-1))*(Math.abs(T)+Math.abs(q))<v*(Math.abs(R)*(Math.abs(o.get(l-1,l-1))+Math.abs(j)+Math.abs(o.get(l+1,l+1)))));)l--;for(n=l+2;n<=b;n++)o.set(n,n-2,0),n>l+2&&o.set(n,n-3,0);for(h=l;h<=b-1&&(y=h!==b-1,h!==l&&(R=o.get(h,h-1),T=o.get(h+1,h-1),q=y?o.get(h+2,h-1):0,0!==(c=Math.abs(R)+Math.abs(T)+Math.abs(q))&&(R/=c,T/=c,q/=c)),0!==c);h++)if(C=Math.sqrt(R*R+T*T+q*q),R<0&&(C=-C),0!==C){for(h!==l?o.set(h,h-1,-C*c):u!==l&&o.set(h,h-1,-o.get(h,h-1)),c=(R+=C)/C,m=T/C,j=q/C,T/=R,q/=R,i=h;i<t;i++)R=o.get(h,i)+T*o.get(h+1,i),y&&(R+=q*o.get(h+2,i),o.set(h+2,i,o.get(h+2,i)-R*j)),o.set(h,i,o.get(h,i)-R*c),o.set(h+1,i,o.get(h+1,i)-R*m);for(n=0;n<=Math.min(b,h+3);n++)R=c*o.get(n,h)+m*o.get(n,h+1),y&&(R+=j*o.get(n,h+2),o.set(n,h+2,o.get(n,h+2)-R*q)),o.set(n,h,o.get(n,h)-R),o.set(n,h+1,o.get(n,h+1)-R*T);for(n=0;n<=x;n++)R=c*s.get(n,h)+m*s.get(n,h+1),y&&(R+=j*s.get(n,h+2),s.set(n,h+2,s.get(n,h+2)-R*q)),s.set(n,h,s.get(n,h)-R),s.set(n,h+1,s.get(n,h+1)-R*T)}}}if(0===S)return;for(b=t-1;b>=0;b--)if(R=r[b],0===(T=e[b]))for(u=b,o.set(b,b,1),n=b-1;n>=0;n--){for(f=o.get(n,n)-R,q=0,i=u;i<=b;i++)q+=o.get(n,i)*o.get(i,b);if(e[n]<0)j=f,C=q;else if(u=n,0===e[n]?o.set(n,b,0!==f?-q/f:-q/(v*S)):(c=o.get(n,n+1),m=o.get(n+1,n),T=(r[n]-R)*(r[n]-R)+e[n]*e[n],a=(c*C-j*q)/T,o.set(n,b,a),o.set(n+1,b,Math.abs(c)>Math.abs(j)?(-q-f*a)/c:(-C-m*a)/j)),a=Math.abs(o.get(n,b)),v*a*a>1)for(i=n;i<=b;i++)o.set(i,b,o.get(i,b)/a)}else if(T<0)for(u=b-1,Math.abs(o.get(b,b-1))>Math.abs(o.get(b-1,b))?(o.set(b-1,b-1,T/o.get(b,b-1)),o.set(b-1,b,-(o.get(b,b)-R)/o.get(b,b-1))):(M=A(0,-o.get(b-1,b),o.get(b-1,b-1)-R,T),o.set(b-1,b-1,M[0]),o.set(b-1,b,M[1])),o.set(b,b-1,0),o.set(b,b,1),n=b-2;n>=0;n--){for(g=0,w=0,i=u;i<=b;i++)g+=o.get(n,i)*o.get(i,b-1),w+=o.get(n,i)*o.get(i,b);if(f=o.get(n,n)-R,e[n]<0)j=f,q=g,C=w;else if(u=n,0===e[n]?(M=A(-g,-w,f,T),o.set(n,b-1,M[0]),o.set(n,b,M[1])):(c=o.get(n,n+1),m=o.get(n+1,n),p=(r[n]-R)*(r[n]-R)+e[n]*e[n]-T*T,d=2*(r[n]-R)*T,0===p&&0===d&&(p=v*S*(Math.abs(f)+Math.abs(T)+Math.abs(c)+Math.abs(m)+Math.abs(j))),M=A(c*q-j*g+T*w,c*C-j*w-T*g,p,d),o.set(n,b-1,M[0]),o.set(n,b,M[1]),Math.abs(c)>Math.abs(j)+Math.abs(T)?(o.set(n+1,b-1,(-g-f*o.get(n,b-1)+T*o.get(n,b))/c),o.set(n+1,b,(-w-f*o.get(n,b)-T*o.get(n,b-1))/c)):(M=A(-q-m*o.get(n,b-1),-C-m*o.get(n,b),j,T),o.set(n+1,b-1,M[0]),o.set(n+1,b,M[1]))),a=Math.max(Math.abs(o.get(n,b-1)),Math.abs(o.get(n,b))),v*a*a>1)for(i=n;i<=b;i++)o.set(i,b-1,o.get(i,b-1)/a),o.set(i,b,o.get(i,b)/a)}for(n=0;n<t;n++)if(n<0||n>x)for(i=n;i<t;i++)s.set(n,i,o.get(n,i));for(i=t-1;i>=0;i--)for(n=0;n<=x;n++){for(j=0,h=0;h<=Math.min(i,x);h++)j+=s.get(n,h)*o.get(h,i);s.set(n,i,j)}}(n,u,h,i,t)}this.n=n,this.e=u,this.d=h,this.V=i}get realEigenvalues(){return Array.from(this.d)}get imaginaryEigenvalues(){return Array.from(this.e)}get eigenvectorMatrix(){return this.V}get diagonalMatrix(){let t,e,r=this.n,s=this.e,o=this.d,n=new b(r,r);for(t=0;t<r;t++){for(e=0;e<r;e++)n.set(t,e,0);n.set(t,t,o[t]),s[t]>0?n.set(t,t+1,s[t]):s[t]<0&&n.set(t,t-1,s[t])}return n}}function A(t,e,r,s){let o,n;return Math.abs(r)>Math.abs(s)?[(t+(o=s/r)*e)/(n=r+o*s),(e-o*t)/n]:[((o=r/s)*t+e)/(n=s+o*r),(o*e-t)/n]}class T{constructor(t,e){this.meanVec=t instanceof b?t:b.rowVector(t),this.covMat=e instanceof b?e:new b(e)}static standard(t){return new T(b.zeros(1,t),b.eye(t,t))}mean(){return this.meanVec}covariance(){return this.covMat}affineTransformation(t,e){const r=this.meanVec.mmul(t).add(e),s=t.transpose().mmul(this.covMat).mmul(t);return new T(r,s)}project(t){return this.affineTransformation(t,b.zeros(1,t.columns))}}class q{constructor(){this.random=e()}}function C(t){const e=new R(t.covariance(),{assumeSymmetric:!0}),r=e.eigenvectorMatrix.transpose();return b.diag(e.realEigenvalues.map(t=>Math.sqrt(t))).mmul(r)}function j(t){return t.transpose().mmul(t)}function k(t){const e=t[0].rows,r=t[0].columns,s=t.length;return t.reduce((t,e)=>t.add(e),b.zeros(e,r)).div(s)}class V{constructor(t,e,r){this.lengths=t,this.vectors=e,this.mean=r}static fit(t,e=1){const r=k(t.map(t=>t.mean())),s=j(r),o=k(t.map(t=>j(t.mean()).add(b.mul(t.covariance(),e*e)).sub(s))),n=new R(o,{assumeSymmetric:!0}),i=n.realEigenvalues,h=n.eigenvectorMatrix.transpose().to2DArray(),u=i.map((t,e)=>[t,h[e]]).sort((t,e)=>e[0]-t[0]);return new V(u.map(t=>t[0]),new b(u.map(t=>t[1])),r)}aligned(){const t=this.vectors;for(let e=0;e<t.rows;++e)t.get(e,e)<0&&t.setRow(e,t.getRowVector(e).mul(-1));return new V(this.lengths,t,this.mean)}eigenvalues(t){return t?this.lengths.slice(0,t):this.lengths}projectionMatrix(t){return t?new v(this.vectors,0,t-1,0,this.vectors.columns-1):this.vectors}transform(t,e){const r=this.projectionMatrix(e);return t.map(t=>t.affineTransformation(b.eye(this.mean.columns,this.mean.columns),b.mul(this.mean,-1))).map(t=>t.project(r.transpose()))}}class ${constructor(t){this.samples=t}points(){return this.samples.to2DArray()}pointsFlipped(){return this.samples.mul(-1).to2DArray()}}t.FactorTraces=class{constructor(t,e,r,s,o){this.N=e||100,this.min=r||0,this.max=s||1e3,this.components=o||2;const n=Math.pow(this.min,2),i=(Math.pow(this.max,2)-n)/this.N,h=function(t,e,r){t=+t,e=+e,r=(o=arguments.length)<2?(e=t,t=0,1):o<3?1:+r;for(var s=-1,o=0|Math.max(0,Math.ceil((e-t)/r)),n=new Array(o);++s<o;)n[s]=t+s*r;return n}(this.N).map(t=>Math.sqrt(n+t*i)).map(e=>V.fit(t,e).aligned()),u=t[0].mean().columns;this.traceSamples=h.map(t=>{const e=t.projectionMatrix(this.components).transpose();return b.eye(u,u).mmul(e)})}getTrace(t){const e=this.traceSamples[0],r=b.zeros(this.N,this.components);r.setRow(0,e.getRowVector(t));for(let e=1;e<this.traceSamples.length;++e){const s=this.traceSamples[e],o=s.getRowVector(t),n=s.getRowVector(t).mul(-1),i=b.sub(o,r.getRowVector(e-1)),h=b.sub(n,r.getRowVector(e-1));i.dot(i)<h.dot(h)?r.setRow(e,o):r.setRow(e,n)}return new $(r)}},t.Matrix=b,t.MultivariateNormal=T,t.Sampler=class{constructor(t){this.mean=t.mean(),this.A=C(t),this.gen=new q}sampleN(t){return b.random(t,this.dims(),this.gen).mmul(this.A).addRowVector(this.mean).to2DArray()}dims(){return this.mean.columns}},t.Trace=$,t.UaPCA=V,t.arithmeticMean=k,t.transformationMatrix=C,Object.defineProperty(t,"__esModule",{value:!0})}));