Comparing version 6.5.2 to 6.5.3
@@ -0,0 +0,0 @@ type MaybeMatrix = AbstractMatrix | number[][]; |
@@ -1,1 +0,1 @@ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).mlMatrix={})}(this,(function(t){"use strict";const e=Object.prototype.toString;function r(t){return e.call(t).endsWith("Array]")}function s(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!r(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");var s=e.fromIndex,o=void 0===s?0:s,n=e.toIndex,i=void 0===n?t.length:n;if(o<0||o>=t.length||!Number.isInteger(o))throw new Error("fromIndex must be a positive integer smaller than length");if(i<=o||i>t.length||!Number.isInteger(i))throw new Error("toIndex must be an integer greater than fromIndex and at most equal to length");for(var h=t[o],u=o+1;u<i;u++)t[u]>h&&(h=t[u]);return h}function o(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!r(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");var s=e.fromIndex,o=void 0===s?0:s,n=e.toIndex,i=void 0===n?t.length:n;if(o<0||o>=t.length||!Number.isInteger(o))throw new Error("fromIndex must be a positive integer smaller than length");if(i<=o||i>t.length||!Number.isInteger(i))throw new Error("toIndex must be an integer greater than fromIndex and at most equal to length");for(var h=t[o],u=o+1;u<i;u++)t[u]<h&&(h=t[u]);return h}function n(t){var e,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!r(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");if(void 0!==n.output){if(!r(n.output))throw new TypeError("output option must be an array if specified");e=n.output}else e=new Array(t.length);var i=o(t),h=s(t);if(i===h)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");var u=n.min,l=void 0===u?n.autoMinMax?i:0:u,a=n.max,f=void 0===a?n.autoMinMax?h:1:a;if(l>=f)throw new RangeError("min option must be smaller than max option");for(var c=(f-l)/(h-i),g=0;g<t.length;g++)e[g]=(t[g]-i)*c+l;return e}const i=" ".repeat(2),h=" ".repeat(4);function u(t,e={}){const{maxRows:r=15,maxColumns:s=10,maxNumSize:o=8}=e;return`${t.constructor.name} {\n${i}[\n${h}${function(t,e,r,s){const{rows:o,columns:n}=t,i=Math.min(o,e),u=Math.min(n,r),a=[];for(let e=0;e<i;e++){let r=[];for(let o=0;o<u;o++)r.push(l(t.get(e,o),s));a.push(""+r.join(" "))}u!==n&&(a[a.length-1]+=` ... ${n-r} more columns`);i!==o&&a.push(`... ${o-e} more rows`);return a.join("\n"+h)}(t,r,s,o)}\n${i}]\n${i}rows: ${t.rows}\n${i}columns: ${t.columns}\n}`}function l(t,e){const r=String(t);if(r.length<=e)return r.padEnd(e," ");const s=t.toPrecision(e-2);if(s.length<=e)return s;const o=t.toExponential(e-2),n=o.indexOf("e"),i=o.slice(n);return o.slice(0,e-i.length)+i}function a(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 f(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 c(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 g(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 m(t,e,r){return{row:w(t,e),column:p(t,r)}}function w(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 p(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 d(t,e,r,s,o){if(5!==arguments.length)throw new RangeError("expected 4 arguments");if(b("startRow",e),b("endRow",r),b("startColumn",s),b("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 M(t,e=0){let r=[];for(let s=0;s<t;s++)r.push(e);return r}function b(t,e){if("number"!=typeof e)throw new TypeError(t+" must be a number")}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 v(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 v(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 v(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 v(t,e)}static ones(t,e){return new v(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 v(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 v(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 v(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 v(t)}static isMatrix(t){return null!=t&&"Matrix"===t.klass}get size(){return this.rows*this.columns}apply(t){if("function"!=typeof t)throw new TypeError("callback must be a function");for(let e=0;e<this.rows;e++)for(let 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 v(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){a(this,t);let e=[];for(let r=0;r<this.columns;r++)e.push(this.get(t,r));return e}getRowVector(t){return v.rowVector(this.getRow(t))}setRow(t,e){a(this,t),e=c(this,e);for(let r=0;r<this.columns;r++)this.set(t,r,e[r]);return this}swapRows(t,e){a(this,t),a(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){f(this,t);let e=[];for(let r=0;r<this.rows;r++)e.push(this.get(r,t));return e}getColumnVector(t){return v.columnVector(this.getColumn(t))}setColumn(t,e){f(this,t),e=g(this,e);for(let r=0;r<this.rows;r++)this.set(r,t,e[r]);return this}swapColumns(t,e){f(this,t),f(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=c(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=c(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=c(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=c(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=g(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=g(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=g(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=g(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){a(this,t);for(let r=0;r<this.columns;r++)this.set(t,r,this.get(t,r)*e);return this}mulColumn(t,e){f(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){a(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){a(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){a(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){a(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){f(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){f(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){f(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){f(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=v.checkMatrix(t);let e=this.rows,r=this.columns,s=t.columns,o=new v(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=v.checkMatrix(t);let e=new v(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),g=u*(h-s),m=(r+o)*l,w=a+g-m+(o-u)*(h+l),p=c+m,d=f+g,M=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,M),e}strassen3x3(t){t=v.checkMatrix(t);let e=new v(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),g=t.get(0,2),m=t.get(1,0),w=t.get(1,1),p=t.get(1,2),d=t.get(2,0),M=t.get(2,1),b=t.get(2,2),y=(r-n)*(-c+w),x=(-r+n+i)*(f-c+w),E=(n+i)*(-f+c),S=r*f,R=(-r+u+l)*(f-g+p),A=(-r+u)*(g-p),I=(u+l)*(-f+g),k=(-o+l+a)*(w+d-M),V=(o-a)*(w-M),T=o*d,q=(l+a)*(-d+M),C=(-o+i+h)*(p+d-b),N=(o-h)*(p-b),F=(i+h)*(-d+b),D=S+T+s*m,j=(r+s+o-n-i-l-a)*w+x+E+S+k+T+q,L=S+R+I+(r+s+o-i-h-u-l)*p+T+C+F,z=y+i*(-f+c+m-w-p-d+b)+x+S+T+C+N,P=y+x+E+S+h*M,U=T+C+N+F+n*g,$=S+R+A+l*(-f+g+m-w-p-d+M)+k+V+T,O=k+V+T+q+u*c,_=S+R+A+I+a*b;return e.set(0,0,D),e.set(0,1,j),e.set(0,2,L),e.set(1,0,z),e.set(1,1,P),e.set(1,2,U),e.set(2,0,$),e.set(2,1,O),e.set(2,2,_),e}mmulStrassen(t){t=v.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}}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 e=i(e,h,u),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),g=r.subMatrix(n,r.rows-1,0,h-1),m=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,m),y.add(l,w),n,h),d=t(y.add(c,m),l,n,h),M=t(u,y.sub(f,w),n,h),b=t(m,y.sub(g,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,m),y.add(g,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),I=y.sub(p,d);I.add(M),I.add(v);let k=y.zeros(2*S.rows,2*S.columns);return k=k.setSubMatrix(S,0,0),k=k.setSubMatrix(R,S.rows,0),k=k.setSubMatrix(A,0,S.columns),k=k.setSubMatrix(I,S.rows,S.columns),k.subMatrix(0,s-1,0,o-1)}(e,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 v(this.rows,this.columns);for(let t=0;t<this.rows;t++){const o=this.getRow(t);n(o,{min:e,max:r,output:o}),s.setRow(t,o)}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 v(this.rows,this.columns);for(let t=0;t<this.columns;t++){const o=this.getColumn(t);n(o,{min:e,max:r,output:o}),s.setColumn(t,o)}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=v.checkMatrix(t);let e=this.rows,r=this.columns,s=t.rows,o=t.columns,n=new v(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 v(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=x){for(let e=0;e<this.rows;e++)this.setRow(e,this.getRow(e).sort(t));return this}sortColumns(t=x){for(let e=0;e<this.columns;e++)this.setColumn(e,this.getColumn(e).sort(t));return this}subMatrix(t,e,r,s){d(this,t,e,r,s);let o=new v(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 v(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 v(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){d(this,e,e+(t=v.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=m(this,t,e),s=new v(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 v(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=M(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=M(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=M(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=M(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++)u=t.get(i,e)-r[i],s+=u,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++)u=t.get(e,i)-r[i],o+=u,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++)u=t.get(e,s)-r,i+=u,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)}}toString(t){return u(this,t)}}function x(t,e){return t-e}y.prototype.klass="Matrix","undefined"!=typeof Symbol&&(y.prototype[Symbol.for("nodejs.util.inspect.custom")]=function(){return u(this)}),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 v extends y{constructor(t,e){if(super(),v.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(a(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),a(this,t,!0),e=Float64Array.from(c(this,e)),this.data.splice(t,0,e),this.rows+=1,this}removeColumn(t){if(f(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),f(this,t,!0),e=g(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,v);class E extends y{constructor(t,e,r){super(),this.matrix=t,this.rows=e,this.columns=r}}class S extends E{constructor(t,e,r){let s=m(t,e,r);super(t,s.row.length,s.column.length),this.rowIndices=s.row,this.columnIndices=s.column}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 R extends y{constructor(t,e={}){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 s=this._calculateIndex(t,e);return this.data[s]=r,this}get(t,e){let r=this._calculateIndex(t,e);return this.data[r]}_calculateIndex(t,e){return t*this.columns+e}}class A 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]}}class I{constructor(t){let e,r,s,o,n,i,h,u,l,a=(t=A.checkMatrix(t)).clone(),f=a.rows,c=a.columns,g=new Float64Array(f),m=1;for(e=0;e<f;e++)g[e]=e;for(u=new Float64Array(f),r=0;r<c;r++){for(e=0;e<f;e++)u[e]=a.get(e,r);for(e=0;e<f;e++){for(l=Math.min(e,r),n=0,s=0;s<l;s++)n+=a.get(e,s)*u[s];u[e]-=n,a.set(e,r,u[e])}for(o=r,e=r+1;e<f;e++)Math.abs(u[e])>Math.abs(u[o])&&(o=e);if(o!==r){for(s=0;s<c;s++)i=a.get(o,s),a.set(o,s,a.get(r,s)),a.set(r,s,i);h=g[o],g[o]=g[r],g[r]=h,m=-m}if(r<f&&0!==a.get(r,r))for(e=r+1;e<f;e++)a.set(e,r,a.get(e,r)/a.get(r,r))}this.LU=a,this.pivotVector=g,this.pivotSign=m}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=v.checkMatrix(t);let e=this.LU;if(e.rows!==t.rows)throw new Error("Invalid matrix dimensions");if(this.isSingular())throw new Error("LU matrix is singular");let r,s,o,n=t.columns,i=t.subMatrixRow(this.pivotVector,0,n-1),h=e.columns;for(o=0;o<h;o++)for(r=o+1;r<h;r++)for(s=0;s<n;s++)i.set(r,s,i.get(r,s)-i.get(o,s)*e.get(r,o));for(o=h-1;o>=0;o--){for(s=0;s<n;s++)i.set(o,s,i.get(o,s)/e.get(o,o));for(r=0;r<o;r++)for(s=0;s<n;s++)i.set(r,s,i.get(r,s)-i.get(o,s)*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 s=0;s<r;s++)e*=t.get(s,s);return e}get lowerTriangularMatrix(){let t=this.LU,e=t.rows,r=t.columns,s=new v(e,r);for(let o=0;o<e;o++)for(let e=0;e<r;e++)o>e?s.set(o,e,t.get(o,e)):o===e?s.set(o,e,1):s.set(o,e,0);return s}get upperTriangularMatrix(){let t=this.LU,e=t.rows,r=t.columns,s=new v(e,r);for(let o=0;o<e;o++)for(let e=0;e<r;e++)o<=e?s.set(o,e,t.get(o,e)):s.set(o,e,0);return s}get pivotPermutationVector(){return Array.from(this.pivotVector)}}function k(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 V{constructor(t){let e,r,s,o,n=(t=A.checkMatrix(t)).clone(),i=t.rows,h=t.columns,u=new Float64Array(h);for(s=0;s<h;s++){let t=0;for(e=s;e<i;e++)t=k(t,n.get(e,s));if(0!==t){for(n.get(s,s)<0&&(t=-t),e=s;e<i;e++)n.set(e,s,n.get(e,s)/t);for(n.set(s,s,n.get(s,s)+1),r=s+1;r<h;r++){for(o=0,e=s;e<i;e++)o+=n.get(e,s)*n.get(e,r);for(o=-o/n.get(s,s),e=s;e<i;e++)n.set(e,r,n.get(e,r)+o*n.get(e,s))}}u[s]=-t}this.QR=n,this.Rdiag=u}solve(t){t=v.checkMatrix(t);let e=this.QR,r=e.rows;if(t.rows!==r)throw new Error("Matrix row dimensions must agree");if(!this.isFullRank())throw new Error("Matrix is rank deficient");let s,o,n,i,h=t.columns,u=t.clone(),l=e.columns;for(n=0;n<l;n++)for(o=0;o<h;o++){for(i=0,s=n;s<r;s++)i+=e.get(s,n)*u.get(s,o);for(i=-i/e.get(n,n),s=n;s<r;s++)u.set(s,o,u.get(s,o)+i*e.get(s,n))}for(n=l-1;n>=0;n--){for(o=0;o<h;o++)u.set(n,o,u.get(n,o)/this.Rdiag[n]);for(s=0;s<n;s++)for(o=0;o<h;o++)u.set(s,o,u.get(s,o)-u.get(n,o)*e.get(s,n))}return u.subMatrix(0,l-1,0,h-1)}isFullRank(){let t=this.QR.columns;for(let e=0;e<t;e++)if(0===this.Rdiag[e])return!1;return!0}get upperTriangularMatrix(){let t,e,r=this.QR,s=r.columns,o=new v(s,s);for(t=0;t<s;t++)for(e=0;e<s;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,s,o=this.QR,n=o.rows,i=o.columns,h=new v(n,i);for(r=i-1;r>=0;r--){for(t=0;t<n;t++)h.set(t,r,0);for(h.set(r,r,1),e=r;e<i;e++)if(0!==o.get(r,r)){for(s=0,t=r;t<n;t++)s+=o.get(t,r)*h.get(t,e);for(s=-s/o.get(r,r),t=r;t<n;t++)h.set(t,e,h.get(t,e)+s*o.get(t,r))}}return h}}class T{constructor(t,e={}){let r=(t=A.checkMatrix(t)).rows,s=t.columns;const{computeLeftSingularVectors:o=!0,computeRightSingularVectors:n=!0,autoTranspose:i=!1}=e;let h,u=Boolean(o),l=Boolean(n),a=!1;if(r<s)if(i){h=t.transpose(),r=h.rows,s=h.columns,a=!0;let e=u;u=l,l=e}else h=t.clone(),console.warn("Computing SVD on a matrix with more columns than rows. Consider enabling autoTranspose");else h=t.clone();let f=Math.min(r,s),c=Math.min(r+1,s),g=new Float64Array(c),m=new v(r,f),w=new v(s,s),p=new Float64Array(s),d=new Float64Array(r),M=new Float64Array(c);for(let t=0;t<c;t++)M[t]=t;let b=Math.min(r-1,s),y=Math.max(0,Math.min(s-2,r)),x=Math.max(b,y);for(let t=0;t<x;t++){if(t<b){g[t]=0;for(let e=t;e<r;e++)g[t]=k(g[t],h.get(e,t));if(0!==g[t]){h.get(t,t)<0&&(g[t]=-g[t]);for(let e=t;e<r;e++)h.set(e,t,h.get(e,t)/g[t]);h.set(t,t,h.get(t,t)+1)}g[t]=-g[t]}for(let e=t+1;e<s;e++){if(t<b&&0!==g[t]){let s=0;for(let o=t;o<r;o++)s+=h.get(o,t)*h.get(o,e);s=-s/h.get(t,t);for(let o=t;o<r;o++)h.set(o,e,h.get(o,e)+s*h.get(o,t))}p[e]=h.get(t,e)}if(u&&t<b)for(let e=t;e<r;e++)m.set(e,t,h.get(e,t));if(t<y){p[t]=0;for(let e=t+1;e<s;e++)p[t]=k(p[t],p[e]);if(0!==p[t]){p[t+1]<0&&(p[t]=0-p[t]);for(let e=t+1;e<s;e++)p[e]/=p[t];p[t+1]+=1}if(p[t]=-p[t],t+1<r&&0!==p[t]){for(let e=t+1;e<r;e++)d[e]=0;for(let e=t+1;e<r;e++)for(let r=t+1;r<s;r++)d[e]+=p[r]*h.get(e,r);for(let e=t+1;e<s;e++){let s=-p[e]/p[t+1];for(let o=t+1;o<r;o++)h.set(o,e,h.get(o,e)+s*d[o])}}if(l)for(let e=t+1;e<s;e++)w.set(e,t,p[e])}}let E=Math.min(s,r+1);if(b<s&&(g[b]=h.get(b,b)),r<E&&(g[E-1]=0),y+1<E&&(p[y]=h.get(y,E-1)),p[E-1]=0,u){for(let t=b;t<f;t++){for(let e=0;e<r;e++)m.set(e,t,0);m.set(t,t,1)}for(let t=b-1;t>=0;t--)if(0!==g[t]){for(let e=t+1;e<f;e++){let s=0;for(let o=t;o<r;o++)s+=m.get(o,t)*m.get(o,e);s=-s/m.get(t,t);for(let o=t;o<r;o++)m.set(o,e,m.get(o,e)+s*m.get(o,t))}for(let e=t;e<r;e++)m.set(e,t,-m.get(e,t));m.set(t,t,1+m.get(t,t));for(let e=0;e<t-1;e++)m.set(e,t,0)}else{for(let e=0;e<r;e++)m.set(e,t,0);m.set(t,t,1)}}if(l)for(let t=s-1;t>=0;t--){if(t<y&&0!==p[t])for(let e=t+1;e<s;e++){let r=0;for(let o=t+1;o<s;o++)r+=w.get(o,t)*w.get(o,e);r=-r/w.get(t+1,t);for(let o=t+1;o<s;o++)w.set(o,e,w.get(o,e)+r*w.get(o,t))}for(let e=0;e<s;e++)w.set(e,t,0);w.set(t,t,1)}let S=E-1,R=Number.EPSILON;for(;E>0;){let t,e;for(t=E-2;t>=-1&&-1!==t;t--){const e=Number.MIN_VALUE+R*Math.abs(g[t]+Math.abs(g[t+1]));if(Math.abs(p[t])<=e||Number.isNaN(p[t])){p[t]=0;break}}if(t===E-2)e=4;else{let r;for(r=E-1;r>=t&&r!==t;r--){let e=(r!==E?Math.abs(p[r]):0)+(r!==t+1?Math.abs(p[r-1]):0);if(Math.abs(g[r])<=R*e){g[r]=0;break}}r===t?e=3:r===E-1?e=1:(e=2,t=r)}switch(t++,e){case 1:{let e=p[E-2];p[E-2]=0;for(let r=E-2;r>=t;r--){let o=k(g[r],e),n=g[r]/o,i=e/o;if(g[r]=o,r!==t&&(e=-i*p[r-1],p[r-1]=n*p[r-1]),l)for(let t=0;t<s;t++)o=n*w.get(t,r)+i*w.get(t,E-1),w.set(t,E-1,-i*w.get(t,r)+n*w.get(t,E-1)),w.set(t,r,o)}break}case 2:{let e=p[t-1];p[t-1]=0;for(let s=t;s<E;s++){let o=k(g[s],e),n=g[s]/o,i=e/o;if(g[s]=o,e=-i*p[s],p[s]=n*p[s],u)for(let e=0;e<r;e++)o=n*m.get(e,s)+i*m.get(e,t-1),m.set(e,t-1,-i*m.get(e,s)+n*m.get(e,t-1)),m.set(e,s,o)}break}case 3:{const e=Math.max(Math.abs(g[E-1]),Math.abs(g[E-2]),Math.abs(p[E-2]),Math.abs(g[t]),Math.abs(p[t])),o=g[E-1]/e,n=g[E-2]/e,i=p[E-2]/e,h=g[t]/e,a=p[t]/e,f=((n+o)*(n-o)+i*i)/2,c=o*i*(o*i);let d=0;0===f&&0===c||(d=f<0?0-Math.sqrt(f*f+c):Math.sqrt(f*f+c),d=c/(f+d));let M=(h+o)*(h-o)+d,b=h*a;for(let e=t;e<E-1;e++){let o=k(M,b);0===o&&(o=Number.MIN_VALUE);let n=M/o,i=b/o;if(e!==t&&(p[e-1]=o),M=n*g[e]+i*p[e],p[e]=n*p[e]-i*g[e],b=i*g[e+1],g[e+1]=n*g[e+1],l)for(let t=0;t<s;t++)o=n*w.get(t,e)+i*w.get(t,e+1),w.set(t,e+1,-i*w.get(t,e)+n*w.get(t,e+1)),w.set(t,e,o);if(o=k(M,b),0===o&&(o=Number.MIN_VALUE),n=M/o,i=b/o,g[e]=o,M=n*p[e]+i*g[e+1],g[e+1]=-i*p[e]+n*g[e+1],b=i*p[e+1],p[e+1]=n*p[e+1],u&&e<r-1)for(let t=0;t<r;t++)o=n*m.get(t,e)+i*m.get(t,e+1),m.set(t,e+1,-i*m.get(t,e)+n*m.get(t,e+1)),m.set(t,e,o)}p[E-2]=M;break}case 4:if(g[t]<=0&&(g[t]=g[t]<0?-g[t]:0,l))for(let e=0;e<=S;e++)w.set(e,t,-w.get(e,t));for(;t<S&&!(g[t]>=g[t+1]);){let e=g[t];if(g[t]=g[t+1],g[t+1]=e,l&&t<s-1)for(let r=0;r<s;r++)e=w.get(r,t+1),w.set(r,t+1,w.get(r,t)),w.set(r,t,e);if(u&&t<r-1)for(let s=0;s<r;s++)e=m.get(s,t+1),m.set(s,t+1,m.get(s,t)),m.set(s,t,e);t++}E--}}if(a){let t=w;w=m,m=t}this.m=r,this.n=s,this.s=g,this.U=m,this.V=w}solve(t){let e=t,r=this.threshold,s=this.s.length,o=v.zeros(s,s);for(let t=0;t<s;t++)Math.abs(this.s[t])<=r?o.set(t,t,0):o.set(t,t,1/this.s[t]);let n=this.U,i=this.rightSingularVectors,h=i.mmul(o),u=i.rows,l=n.rows,a=v.zeros(u,l);for(let t=0;t<u;t++)for(let e=0;e<l;e++){let r=0;for(let o=0;o<s;o++)r+=h.get(t,o)*n.get(e,o);a.set(t,e,r)}return a.mmul(e)}solveForDiagonal(t){return this.solve(v.diag(t))}inverse(){let t=this.V,e=this.threshold,r=t.rows,s=t.columns,o=new v(r,this.s.length);for(let n=0;n<r;n++)for(let r=0;r<s;r++)Math.abs(this.s[r])>e&&o.set(n,r,t.get(n,r)/this.s[r]);let n=this.U,i=n.rows,h=n.columns,u=new v(r,i);for(let t=0;t<r;t++)for(let e=0;e<i;e++){let r=0;for(let s=0;s<h;s++)r+=o.get(t,s)*n.get(e,s);u.set(t,e,r)}return u}get condition(){return this.s[0]/this.s[Math.min(this.m,this.n)-1]}get norm2(){return this.s[0]}get rank(){let t=Math.max(this.m,this.n)*this.s[0]*Number.EPSILON,e=0,r=this.s;for(let s=0,o=r.length;s<o;s++)r[s]>t&&e++;return e}get diagonal(){return Array.from(this.s)}get threshold(){return Number.EPSILON/2*Math.max(this.m,this.n)*this.s[0]}get leftSingularVectors(){return this.U}get rightSingularVectors(){return this.V}get diagonalMatrix(){return v.diag(this.s)}}function q(t,e,r=!1){return t=A.checkMatrix(t),e=A.checkMatrix(e),r?new T(t).solve(e):t.isSquare()?new I(t).solve(e):new V(t).solve(e)}function C(t,e){let r=[];for(let s=0;s<t;s++)s!==e&&r.push(s);return r}function N(t,e,r,s=1e-9,o=1e-9){if(t>o)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))<s&&t.set(e,0,0);return t.to1DArray()}}class F{constructor(t,e={}){const{assumeSymmetric:r=!1}=e;if(!(t=A.checkMatrix(t)).isSquare())throw new Error("Matrix is not a square matrix");let s,o,n=t.columns,i=new v(n,n),h=new Float64Array(n),u=new Float64Array(n),l=t,a=!1;if(a=!!r||t.isSymmetric(),a){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),i=r[h+1],0!==i){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,g,m,w,p,d,M,b;for(i=1;i<t;i++)e[i-1]=e[i];e[t-1]=0;let y=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=k(f,1),f<0&&(c=-c),r[l]=e[l]/(f+c),r[l+1]=e[l]*(f+c),g=r[l+1],n=o-r[l],i=l+2;i<t;i++)r[i]-=n;for(y+=n,f=r[a],m=1,w=m,p=m,d=e[l+1],M=0,b=0,i=a-1;i>=l;i--)for(p=w,w=m,b=M,o=m*e[i],n=m*f,c=k(f,e[i]),e[i+1]=M*c,M=e[i]/c,m=f/c,f=m*r[i]-M*o,r[i+1]=n+M*(m*o+M*r[i]),u=0;u<t;u++)n=s.get(u,i+1),s.set(u,i+1,M*s.get(u,i)+m*n),s.set(u,i,m*s.get(u,i)-M*n);f=-M*b*p*d*e[l]/g,e[l]=M*f,r[l]=m*f}while(Math.abs(e[l])>v*x);r[l]=r[l]+y,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 v(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,g,m,w,p,d,M,b,y=t-1,x=t-1,v=Number.EPSILON,E=0,S=0,R=0,A=0,I=0,k=0,V=0,T=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(;y>=0;){for(u=y;u>0&&(k=Math.abs(o.get(u-1,u-1))+Math.abs(o.get(u,u)),0===k&&(k=S),!(Math.abs(o.get(u,u-1))<v*k));)u--;if(u===y)o.set(y,y,o.get(y,y)+E),r[y]=o.get(y,y),e[y]=0,y--,T=0;else if(u===y-1){if(f=o.get(y,y-1)*o.get(y-1,y),R=(o.get(y-1,y-1)-o.get(y,y))/2,A=R*R+f,V=Math.sqrt(Math.abs(A)),o.set(y,y,o.get(y,y)+E),o.set(y-1,y-1,o.get(y-1,y-1)+E),c=o.get(y,y),A>=0){for(V=R>=0?R+V:R-V,r[y-1]=c+V,r[y]=r[y-1],0!==V&&(r[y]=c-f/V),e[y-1]=0,e[y]=0,c=o.get(y,y-1),k=Math.abs(c)+Math.abs(V),R=c/k,A=V/k,I=Math.sqrt(R*R+A*A),R/=I,A/=I,i=y-1;i<t;i++)V=o.get(y-1,i),o.set(y-1,i,A*V+R*o.get(y,i)),o.set(y,i,A*o.get(y,i)-R*V);for(n=0;n<=y;n++)V=o.get(n,y-1),o.set(n,y-1,A*V+R*o.get(n,y)),o.set(n,y,A*o.get(n,y)-R*V);for(n=0;n<=x;n++)V=s.get(n,y-1),s.set(n,y-1,A*V+R*s.get(n,y)),s.set(n,y,A*s.get(n,y)-R*V)}else r[y-1]=c+R,r[y]=c+R,e[y-1]=V,e[y]=-V;y-=2,T=0}else{if(c=o.get(y,y),g=0,f=0,u<y&&(g=o.get(y-1,y-1),f=o.get(y,y-1)*o.get(y-1,y)),10===T){for(E+=c,n=0;n<=y;n++)o.set(n,n,o.get(n,n)-c);k=Math.abs(o.get(y,y-1))+Math.abs(o.get(y-1,y-2)),c=g=.75*k,f=-.4375*k*k}if(30===T&&(k=(g-c)/2,k=k*k+f,k>0)){for(k=Math.sqrt(k),g<c&&(k=-k),k=c-f/((g-c)/2+k),n=0;n<=y;n++)o.set(n,n,o.get(n,n)-k);E+=k,c=g=f=.964}for(T+=1,l=y-2;l>=u&&(V=o.get(l,l),I=c-V,k=g-V,R=(I*k-f)/o.get(l+1,l)+o.get(l,l+1),A=o.get(l+1,l+1)-V-I-k,I=o.get(l+2,l+1),k=Math.abs(R)+Math.abs(A)+Math.abs(I),R/=k,A/=k,I/=k,l!==u)&&!(Math.abs(o.get(l,l-1))*(Math.abs(A)+Math.abs(I))<v*(Math.abs(R)*(Math.abs(o.get(l-1,l-1))+Math.abs(V)+Math.abs(o.get(l+1,l+1)))));)l--;for(n=l+2;n<=y;n++)o.set(n,n-2,0),n>l+2&&o.set(n,n-3,0);for(h=l;h<=y-1&&(M=h!==y-1,h!==l&&(R=o.get(h,h-1),A=o.get(h+1,h-1),I=M?o.get(h+2,h-1):0,c=Math.abs(R)+Math.abs(A)+Math.abs(I),0!==c&&(R/=c,A/=c,I/=c)),0!==c);h++)if(k=Math.sqrt(R*R+A*A+I*I),R<0&&(k=-k),0!==k){for(h!==l?o.set(h,h-1,-k*c):u!==l&&o.set(h,h-1,-o.get(h,h-1)),R+=k,c=R/k,g=A/k,V=I/k,A/=R,I/=R,i=h;i<t;i++)R=o.get(h,i)+A*o.get(h+1,i),M&&(R+=I*o.get(h+2,i),o.set(h+2,i,o.get(h+2,i)-R*V)),o.set(h,i,o.get(h,i)-R*c),o.set(h+1,i,o.get(h+1,i)-R*g);for(n=0;n<=Math.min(y,h+3);n++)R=c*o.get(n,h)+g*o.get(n,h+1),M&&(R+=V*o.get(n,h+2),o.set(n,h+2,o.get(n,h+2)-R*I)),o.set(n,h,o.get(n,h)-R),o.set(n,h+1,o.get(n,h+1)-R*A);for(n=0;n<=x;n++)R=c*s.get(n,h)+g*s.get(n,h+1),M&&(R+=V*s.get(n,h+2),s.set(n,h+2,s.get(n,h+2)-R*I)),s.set(n,h,s.get(n,h)-R),s.set(n,h+1,s.get(n,h+1)-R*A)}}}if(0===S)return;for(y=t-1;y>=0;y--)if(R=r[y],A=e[y],0===A)for(u=y,o.set(y,y,1),n=y-1;n>=0;n--){for(f=o.get(n,n)-R,I=0,i=u;i<=y;i++)I+=o.get(n,i)*o.get(i,y);if(e[n]<0)V=f,k=I;else if(u=n,0===e[n]?o.set(n,y,0!==f?-I/f:-I/(v*S)):(c=o.get(n,n+1),g=o.get(n+1,n),A=(r[n]-R)*(r[n]-R)+e[n]*e[n],a=(c*k-V*I)/A,o.set(n,y,a),o.set(n+1,y,Math.abs(c)>Math.abs(V)?(-I-f*a)/c:(-k-g*a)/V)),a=Math.abs(o.get(n,y)),v*a*a>1)for(i=n;i<=y;i++)o.set(i,y,o.get(i,y)/a)}else if(A<0)for(u=y-1,Math.abs(o.get(y,y-1))>Math.abs(o.get(y-1,y))?(o.set(y-1,y-1,A/o.get(y,y-1)),o.set(y-1,y,-(o.get(y,y)-R)/o.get(y,y-1))):(b=D(0,-o.get(y-1,y),o.get(y-1,y-1)-R,A),o.set(y-1,y-1,b[0]),o.set(y-1,y,b[1])),o.set(y,y-1,0),o.set(y,y,1),n=y-2;n>=0;n--){for(m=0,w=0,i=u;i<=y;i++)m+=o.get(n,i)*o.get(i,y-1),w+=o.get(n,i)*o.get(i,y);if(f=o.get(n,n)-R,e[n]<0)V=f,I=m,k=w;else if(u=n,0===e[n]?(b=D(-m,-w,f,A),o.set(n,y-1,b[0]),o.set(n,y,b[1])):(c=o.get(n,n+1),g=o.get(n+1,n),p=(r[n]-R)*(r[n]-R)+e[n]*e[n]-A*A,d=2*(r[n]-R)*A,0===p&&0===d&&(p=v*S*(Math.abs(f)+Math.abs(A)+Math.abs(c)+Math.abs(g)+Math.abs(V))),b=D(c*I-V*m+A*w,c*k-V*w-A*m,p,d),o.set(n,y-1,b[0]),o.set(n,y,b[1]),Math.abs(c)>Math.abs(V)+Math.abs(A)?(o.set(n+1,y-1,(-m-f*o.get(n,y-1)+A*o.get(n,y))/c),o.set(n+1,y,(-w-f*o.get(n,y)-A*o.get(n,y-1))/c)):(b=D(-I-g*o.get(n,y-1),-k-g*o.get(n,y),V,A),o.set(n+1,y-1,b[0]),o.set(n+1,y,b[1]))),a=Math.max(Math.abs(o.get(n,y-1)),Math.abs(o.get(n,y))),v*a*a>1)for(i=n;i<=y;i++)o.set(i,y-1,o.get(i,y-1)/a),o.set(i,y,o.get(i,y)/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(V=0,h=0;h<=Math.min(i,x);h++)V+=s.get(n,h)*o.get(h,i);s.set(n,i,V)}}(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 v(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 D(t,e,r,s){let o,n;return Math.abs(r)>Math.abs(s)?(o=s/r,n=r+o*s,[(t+o*e)/n,(e-o*t)/n]):(o=r/s,n=s+o*r,[(o*t+e)/n,(o*e-t)/n])}class j{constructor(t){if(!(t=A.checkMatrix(t)).isSymmetric())throw new Error("Matrix is not symmetric");let e,r,s,o=t,n=o.rows,i=new v(n,n),h=!0;for(r=0;r<n;r++){let t=0;for(s=0;s<r;s++){let n=0;for(e=0;e<s;e++)n+=i.get(s,e)*i.get(r,e);n=(o.get(r,s)-n)/i.get(s,s),i.set(r,s,n),t+=n*n}for(t=o.get(r,r)-t,h&=t>0,i.set(r,r,Math.sqrt(Math.max(t,0))),s=r+1;s<n;s++)i.set(r,s,0)}this.L=i,this.positiveDefinite=Boolean(h)}isPositiveDefinite(){return this.positiveDefinite}solve(t){t=A.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 s,o,n,i=t.columns,h=t.clone();for(n=0;n<r;n++)for(o=0;o<i;o++){for(s=0;s<n;s++)h.set(n,o,h.get(n,o)-h.get(s,o)*e.get(n,s));h.set(n,o,h.get(n,o)/e.get(n,n))}for(n=r-1;n>=0;n--)for(o=0;o<i;o++){for(s=n+1;s<r;s++)h.set(n,o,h.get(n,o)-h.get(s,o)*e.get(s,n));h.set(n,o,h.get(n,o)/e.get(n,n))}return h}get lowerTriangularMatrix(){return this.L}}class L{constructor(t,e={}){t=A.checkMatrix(t);let{Y:r}=e;const{scaleScores:s=!1,maxIterations:o=1e3,terminationCriteria:n=1e-10}=e;let i;if(r){if(r=Array.isArray(r)&&"number"==typeof r[0]?v.columnVector(r):A.checkMatrix(r),!r.isColumnVector()||r.rows!==t.rows)throw new Error("Y must be a column vector of length X.rows");i=r}else i=t.getColumnVector(0);let h,u,l,a,f=1;for(let e=0;e<o&&f>n;e++)l=t.transpose().mmul(i).div(i.transpose().mmul(i).get(0,0)),l=l.div(l.norm()),h=t.mmul(l).div(l.transpose().mmul(l).get(0,0)),e>0&&(f=h.clone().sub(a).pow(2).sum()),a=h.clone(),r?(u=r.transpose().mmul(h).div(h.transpose().mmul(h).get(0,0)),u=u.div(u.norm()),i=r.mmul(u).div(u.transpose().mmul(u).get(0,0))):i=h;if(r){let e=t.transpose().mmul(h).div(h.transpose().mmul(h).get(0,0));e=e.div(e.norm());let s=t.clone().sub(h.clone().mmul(e.transpose())),o=i.transpose().mmul(h).div(h.transpose().mmul(h).get(0,0)),n=r.clone().sub(h.clone().mulS(o.get(0,0)).mmul(u.transpose()));this.t=h,this.p=e.transpose(),this.w=l.transpose(),this.q=u,this.u=i,this.s=h.transpose().mmul(h),this.xResidual=s,this.yResidual=n,this.betas=o}else this.w=l.transpose(),this.s=h.transpose().mmul(h).sqrt(),this.t=s?h.clone().div(this.s.get(0,0)):h,this.xResidual=t.sub(h.mmul(l.transpose()))}}t.AbstractMatrix=y,t.CHO=j,t.CholeskyDecomposition=j,t.EVD=F,t.EigenvalueDecomposition=F,t.LU=I,t.LuDecomposition=I,t.Matrix=v,t.MatrixColumnSelectionView=class extends E{constructor(t,e){e=p(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])}},t.MatrixColumnView=class extends E{constructor(t,e){f(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)}},t.MatrixFlipColumnView=class extends E{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)}},t.MatrixFlipRowView=class extends E{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)}},t.MatrixRowSelectionView=class extends E{constructor(t,e){super(t,(e=w(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)}},t.MatrixRowView=class extends E{constructor(t,e){a(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)}},t.MatrixSelectionView=S,t.MatrixSubView=class extends E{constructor(t,e,r,s,o){d(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)}},t.MatrixTransposeView=class extends E{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)}},t.NIPALS=L,t.Nipals=L,t.QR=V,t.QrDecomposition=V,t.SVD=T,t.SingularValueDecomposition=T,t.WrapperMatrix1D=R,t.WrapperMatrix2D=A,t.correlation=function(t,e=t,r={}){t=new v(t);let s=!1;if("object"!=typeof e||v.isMatrix(e)||Array.isArray(e)?e=new v(e):(r=e,e=t,s=!0),t.rows!==e.rows)throw new TypeError("Both matrices must have the same number of rows");const{center:o=!0,scale:n=!0}=r;o&&(t.center("column"),s||e.center("column")),n&&(t.scale("column"),s||e.scale("column"));const i=t.standardDeviation("column",{unbiased:!0}),h=s?i:e.standardDeviation("column",{unbiased:!0}),u=t.transpose().mmul(e);for(let e=0;e<u.rows;e++)for(let r=0;r<u.columns;r++)u.set(e,r,u.get(e,r)*(1/(i[e]*h[r]))*(1/(t.rows-1)));return u},t.covariance=function(t,e=t,r={}){t=new v(t);let s=!1;if("object"!=typeof e||v.isMatrix(e)||Array.isArray(e)?e=new v(e):(r=e,e=t,s=!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"),s||(e=e.center("column")));const n=t.transpose().mmul(e);for(let e=0;e<n.rows;e++)for(let r=0;r<n.columns;r++)n.set(e,r,n.get(e,r)*(1/(t.rows-1)));return n},t.default=v,t.determinant=function t(e){if((e=v.checkMatrix(e)).isSquare()){let r,s,o,n;if(2===e.columns)return r=e.get(0,0),s=e.get(0,1),o=e.get(1,0),n=e.get(1,1),r*n-s*o;if(3===e.columns){let n,i,h;return n=new S(e,[1,2],[1,2]),i=new S(e,[1,2],[0,2]),h=new S(e,[1,2],[0,1]),r=e.get(0,0),s=e.get(0,1),o=e.get(0,2),r*t(n)-s*t(i)+o*t(h)}return new I(e).determinant}throw Error("determinant can only be calculated for a square matrix")},t.inverse=function(t,e=!1){return t=A.checkMatrix(t),e?new T(t).inverse():q(t,v.eye(t.rows))},t.linearDependencies=function(t,e={}){const{thresholdValue:r=1e-9,thresholdError:s=1e-9}=e;let o=(t=v.checkMatrix(t)).rows,n=new v(o,o);for(let e=0;e<o;e++){let i=v.columnVector(t.getRow(e)),h=t.subMatrixRow(C(o,e)).transpose(),u=new T(h).solve(i),l=v.sub(i,h.mmul(u)).abs().max();n.setRow(e,N(l,u,e,r,s))}return n},t.pseudoInverse=function(t,e=Number.EPSILON){t=v.checkMatrix(t);let r=new T(t,{autoTranspose:!0}),s=r.leftSingularVectors,o=r.rightSingularVectors,n=r.diagonal;for(let t=0;t<n.length;t++)Math.abs(n[t])>e?n[t]=1/n[t]:n[t]=0;return o.mmul(v.diag(n).mmul(s.transpose()))},t.solve=q,t.wrap=function(t,e){if(Array.isArray(t))return t[0]&&Array.isArray(t[0])?new A(t):new R(t,e);throw new Error("the argument is not an array")},Object.defineProperty(t,"__esModule",{value:!0})})); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).mlMatrix={})}(this,(function(t){"use strict";const e=Object.prototype.toString;function r(t){return e.call(t).endsWith("Array]")}function s(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!r(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");var s=e.fromIndex,o=void 0===s?0:s,n=e.toIndex,i=void 0===n?t.length:n;if(o<0||o>=t.length||!Number.isInteger(o))throw new Error("fromIndex must be a positive integer smaller than length");if(i<=o||i>t.length||!Number.isInteger(i))throw new Error("toIndex must be an integer greater than fromIndex and at most equal to length");for(var h=t[o],u=o+1;u<i;u++)t[u]>h&&(h=t[u]);return h}function o(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!r(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");var s=e.fromIndex,o=void 0===s?0:s,n=e.toIndex,i=void 0===n?t.length:n;if(o<0||o>=t.length||!Number.isInteger(o))throw new Error("fromIndex must be a positive integer smaller than length");if(i<=o||i>t.length||!Number.isInteger(i))throw new Error("toIndex must be an integer greater than fromIndex and at most equal to length");for(var h=t[o],u=o+1;u<i;u++)t[u]<h&&(h=t[u]);return h}function n(t){var e,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!r(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");if(void 0!==n.output){if(!r(n.output))throw new TypeError("output option must be an array if specified");e=n.output}else e=new Array(t.length);var i=o(t),h=s(t);if(i===h)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");var u=n.min,l=void 0===u?n.autoMinMax?i:0:u,a=n.max,f=void 0===a?n.autoMinMax?h:1:a;if(l>=f)throw new RangeError("min option must be smaller than max option");for(var c=(f-l)/(h-i),g=0;g<t.length;g++)e[g]=(t[g]-i)*c+l;return e}const i=" ".repeat(2),h=" ".repeat(4);function u(t,e={}){const{maxRows:r=15,maxColumns:s=10,maxNumSize:o=8}=e;return`${t.constructor.name} {\n${i}[\n${h}${function(t,e,r,s){const{rows:o,columns:n}=t,i=Math.min(o,e),u=Math.min(n,r),a=[];for(let e=0;e<i;e++){let r=[];for(let o=0;o<u;o++)r.push(l(t.get(e,o),s));a.push(""+r.join(" "))}u!==n&&(a[a.length-1]+=` ... ${n-r} more columns`);i!==o&&a.push(`... ${o-e} more rows`);return a.join("\n"+h)}(t,r,s,o)}\n${i}]\n${i}rows: ${t.rows}\n${i}columns: ${t.columns}\n}`}function l(t,e){const r=String(t);if(r.length<=e)return r.padEnd(e," ");const s=t.toPrecision(e-2);if(s.length<=e)return s;const o=t.toExponential(e-2),n=o.indexOf("e"),i=o.slice(n);return o.slice(0,e-i.length)+i}function a(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 f(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 c(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 g(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 m(t,e,r){return{row:w(t,e),column:p(t,r)}}function w(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 p(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 d(t,e,r,s,o){if(5!==arguments.length)throw new RangeError("expected 4 arguments");if(b("startRow",e),b("endRow",r),b("startColumn",s),b("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 M(t,e=0){let r=[];for(let s=0;s<t;s++)r.push(e);return r}function b(t,e){if("number"!=typeof e)throw new TypeError(t+" must be a number")}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 v(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 v(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 v(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 v(t,e)}static ones(t,e){return new v(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 v(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 v(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 v(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 v(t)}static isMatrix(t){return null!=t&&"Matrix"===t.klass}get size(){return this.rows*this.columns}apply(t){if("function"!=typeof t)throw new TypeError("callback must be a function");for(let e=0;e<this.rows;e++)for(let 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 v(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){a(this,t);let e=[];for(let r=0;r<this.columns;r++)e.push(this.get(t,r));return e}getRowVector(t){return v.rowVector(this.getRow(t))}setRow(t,e){a(this,t),e=c(this,e);for(let r=0;r<this.columns;r++)this.set(t,r,e[r]);return this}swapRows(t,e){a(this,t),a(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){f(this,t);let e=[];for(let r=0;r<this.rows;r++)e.push(this.get(r,t));return e}getColumnVector(t){return v.columnVector(this.getColumn(t))}setColumn(t,e){f(this,t),e=g(this,e);for(let r=0;r<this.rows;r++)this.set(r,t,e[r]);return this}swapColumns(t,e){f(this,t),f(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=c(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=c(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=c(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=c(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=g(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=g(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=g(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=g(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){a(this,t);for(let r=0;r<this.columns;r++)this.set(t,r,this.get(t,r)*e);return this}mulColumn(t,e){f(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){a(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){a(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){a(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){a(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){f(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){f(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){f(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){f(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=v.checkMatrix(t);let e=this.rows,r=this.columns,s=t.columns,o=new v(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=v.checkMatrix(t);let e=new v(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),g=u*(h-s),m=(r+o)*l,w=a+g-m+(o-u)*(h+l),p=c+m,d=f+g,M=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,M),e}strassen3x3(t){t=v.checkMatrix(t);let e=new v(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),g=t.get(0,2),m=t.get(1,0),w=t.get(1,1),p=t.get(1,2),d=t.get(2,0),M=t.get(2,1),b=t.get(2,2),y=(r-n)*(-c+w),x=(-r+n+i)*(f-c+w),E=(n+i)*(-f+c),S=r*f,R=(-r+u+l)*(f-g+p),A=(-r+u)*(g-p),I=(u+l)*(-f+g),k=(-o+l+a)*(w+d-M),V=(o-a)*(w-M),T=o*d,q=(l+a)*(-d+M),C=(-o+i+h)*(p+d-b),N=(o-h)*(p-b),F=(i+h)*(-d+b),D=S+T+s*m,j=(r+s+o-n-i-l-a)*w+x+E+S+k+T+q,L=S+R+I+(r+s+o-i-h-u-l)*p+T+C+F,z=y+i*(-f+c+m-w-p-d+b)+x+S+T+C+N,P=y+x+E+S+h*M,U=T+C+N+F+n*g,$=S+R+A+l*(-f+g+m-w-p-d+M)+k+V+T,O=k+V+T+q+u*c,_=S+R+A+I+a*b;return e.set(0,0,D),e.set(0,1,j),e.set(0,2,L),e.set(1,0,z),e.set(1,1,P),e.set(1,2,U),e.set(2,0,$),e.set(2,1,O),e.set(2,2,_),e}mmulStrassen(t){t=v.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}}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 e=i(e,h,u),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),g=r.subMatrix(n,r.rows-1,0,h-1),m=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,m),y.add(l,w),n,h),d=t(y.add(c,m),l,n,h),M=t(u,y.sub(f,w),n,h),b=t(m,y.sub(g,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,m),y.add(g,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),I=y.sub(p,d);I.add(M),I.add(v);let k=y.zeros(2*S.rows,2*S.columns);return k=k.setSubMatrix(S,0,0),k=k.setSubMatrix(R,S.rows,0),k=k.setSubMatrix(A,0,S.columns),k=k.setSubMatrix(I,S.rows,S.columns),k.subMatrix(0,s-1,0,o-1)}(e,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 v(this.rows,this.columns);for(let t=0;t<this.rows;t++){const o=this.getRow(t);n(o,{min:e,max:r,output:o}),s.setRow(t,o)}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 v(this.rows,this.columns);for(let t=0;t<this.columns;t++){const o=this.getColumn(t);n(o,{min:e,max:r,output:o}),s.setColumn(t,o)}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=v.checkMatrix(t);let e=this.rows,r=this.columns,s=t.rows,o=t.columns,n=new v(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 v(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=x){for(let e=0;e<this.rows;e++)this.setRow(e,this.getRow(e).sort(t));return this}sortColumns(t=x){for(let e=0;e<this.columns;e++)this.setColumn(e,this.getColumn(e).sort(t));return this}subMatrix(t,e,r,s){d(this,t,e,r,s);let o=new v(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 v(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 v(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){d(this,e,e+(t=v.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=m(this,t,e),s=new v(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 v(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=M(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=M(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=M(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=M(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++)u=t.get(i,e)-r[i],s+=u,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++)u=t.get(e,i)-r[i],o+=u,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++)u=t.get(e,s)-r,i+=u,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)}}toString(t){return u(this,t)}}function x(t,e){return t-e}y.prototype.klass="Matrix","undefined"!=typeof Symbol&&(y.prototype[Symbol.for("nodejs.util.inspect.custom")]=function(){return u(this)}),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 v extends y{constructor(t,e){if(super(),v.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]))}}}this.rows=t,this.columns=e}set(t,e,r){return this.data[t][e]=r,this}get(t,e){return this.data[t][e]}removeRow(t){if(a(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),a(this,t,!0),e=Float64Array.from(c(this,e)),this.data.splice(t,0,e),this.rows+=1,this}removeColumn(t){if(f(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),f(this,t,!0),e=g(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,v);class E extends y{constructor(t,e,r){super(),this.matrix=t,this.rows=e,this.columns=r}}class S extends E{constructor(t,e,r){let s=m(t,e,r);super(t,s.row.length,s.column.length),this.rowIndices=s.row,this.columnIndices=s.column}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 R extends y{constructor(t,e={}){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 s=this._calculateIndex(t,e);return this.data[s]=r,this}get(t,e){let r=this._calculateIndex(t,e);return this.data[r]}_calculateIndex(t,e){return t*this.columns+e}}class A 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]}}class I{constructor(t){let e,r,s,o,n,i,h,u,l,a=(t=A.checkMatrix(t)).clone(),f=a.rows,c=a.columns,g=new Float64Array(f),m=1;for(e=0;e<f;e++)g[e]=e;for(u=new Float64Array(f),r=0;r<c;r++){for(e=0;e<f;e++)u[e]=a.get(e,r);for(e=0;e<f;e++){for(l=Math.min(e,r),n=0,s=0;s<l;s++)n+=a.get(e,s)*u[s];u[e]-=n,a.set(e,r,u[e])}for(o=r,e=r+1;e<f;e++)Math.abs(u[e])>Math.abs(u[o])&&(o=e);if(o!==r){for(s=0;s<c;s++)i=a.get(o,s),a.set(o,s,a.get(r,s)),a.set(r,s,i);h=g[o],g[o]=g[r],g[r]=h,m=-m}if(r<f&&0!==a.get(r,r))for(e=r+1;e<f;e++)a.set(e,r,a.get(e,r)/a.get(r,r))}this.LU=a,this.pivotVector=g,this.pivotSign=m}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=v.checkMatrix(t);let e=this.LU;if(e.rows!==t.rows)throw new Error("Invalid matrix dimensions");if(this.isSingular())throw new Error("LU matrix is singular");let r,s,o,n=t.columns,i=t.subMatrixRow(this.pivotVector,0,n-1),h=e.columns;for(o=0;o<h;o++)for(r=o+1;r<h;r++)for(s=0;s<n;s++)i.set(r,s,i.get(r,s)-i.get(o,s)*e.get(r,o));for(o=h-1;o>=0;o--){for(s=0;s<n;s++)i.set(o,s,i.get(o,s)/e.get(o,o));for(r=0;r<o;r++)for(s=0;s<n;s++)i.set(r,s,i.get(r,s)-i.get(o,s)*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 s=0;s<r;s++)e*=t.get(s,s);return e}get lowerTriangularMatrix(){let t=this.LU,e=t.rows,r=t.columns,s=new v(e,r);for(let o=0;o<e;o++)for(let e=0;e<r;e++)o>e?s.set(o,e,t.get(o,e)):o===e?s.set(o,e,1):s.set(o,e,0);return s}get upperTriangularMatrix(){let t=this.LU,e=t.rows,r=t.columns,s=new v(e,r);for(let o=0;o<e;o++)for(let e=0;e<r;e++)o<=e?s.set(o,e,t.get(o,e)):s.set(o,e,0);return s}get pivotPermutationVector(){return Array.from(this.pivotVector)}}function k(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 V{constructor(t){let e,r,s,o,n=(t=A.checkMatrix(t)).clone(),i=t.rows,h=t.columns,u=new Float64Array(h);for(s=0;s<h;s++){let t=0;for(e=s;e<i;e++)t=k(t,n.get(e,s));if(0!==t){for(n.get(s,s)<0&&(t=-t),e=s;e<i;e++)n.set(e,s,n.get(e,s)/t);for(n.set(s,s,n.get(s,s)+1),r=s+1;r<h;r++){for(o=0,e=s;e<i;e++)o+=n.get(e,s)*n.get(e,r);for(o=-o/n.get(s,s),e=s;e<i;e++)n.set(e,r,n.get(e,r)+o*n.get(e,s))}}u[s]=-t}this.QR=n,this.Rdiag=u}solve(t){t=v.checkMatrix(t);let e=this.QR,r=e.rows;if(t.rows!==r)throw new Error("Matrix row dimensions must agree");if(!this.isFullRank())throw new Error("Matrix is rank deficient");let s,o,n,i,h=t.columns,u=t.clone(),l=e.columns;for(n=0;n<l;n++)for(o=0;o<h;o++){for(i=0,s=n;s<r;s++)i+=e.get(s,n)*u.get(s,o);for(i=-i/e.get(n,n),s=n;s<r;s++)u.set(s,o,u.get(s,o)+i*e.get(s,n))}for(n=l-1;n>=0;n--){for(o=0;o<h;o++)u.set(n,o,u.get(n,o)/this.Rdiag[n]);for(s=0;s<n;s++)for(o=0;o<h;o++)u.set(s,o,u.get(s,o)-u.get(n,o)*e.get(s,n))}return u.subMatrix(0,l-1,0,h-1)}isFullRank(){let t=this.QR.columns;for(let e=0;e<t;e++)if(0===this.Rdiag[e])return!1;return!0}get upperTriangularMatrix(){let t,e,r=this.QR,s=r.columns,o=new v(s,s);for(t=0;t<s;t++)for(e=0;e<s;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,s,o=this.QR,n=o.rows,i=o.columns,h=new v(n,i);for(r=i-1;r>=0;r--){for(t=0;t<n;t++)h.set(t,r,0);for(h.set(r,r,1),e=r;e<i;e++)if(0!==o.get(r,r)){for(s=0,t=r;t<n;t++)s+=o.get(t,r)*h.get(t,e);for(s=-s/o.get(r,r),t=r;t<n;t++)h.set(t,e,h.get(t,e)+s*o.get(t,r))}}return h}}class T{constructor(t,e={}){let r=(t=A.checkMatrix(t)).rows,s=t.columns;const{computeLeftSingularVectors:o=!0,computeRightSingularVectors:n=!0,autoTranspose:i=!1}=e;let h,u=Boolean(o),l=Boolean(n),a=!1;if(r<s)if(i){h=t.transpose(),r=h.rows,s=h.columns,a=!0;let e=u;u=l,l=e}else h=t.clone(),console.warn("Computing SVD on a matrix with more columns than rows. Consider enabling autoTranspose");else h=t.clone();let f=Math.min(r,s),c=Math.min(r+1,s),g=new Float64Array(c),m=new v(r,f),w=new v(s,s),p=new Float64Array(s),d=new Float64Array(r),M=new Float64Array(c);for(let t=0;t<c;t++)M[t]=t;let b=Math.min(r-1,s),y=Math.max(0,Math.min(s-2,r)),x=Math.max(b,y);for(let t=0;t<x;t++){if(t<b){g[t]=0;for(let e=t;e<r;e++)g[t]=k(g[t],h.get(e,t));if(0!==g[t]){h.get(t,t)<0&&(g[t]=-g[t]);for(let e=t;e<r;e++)h.set(e,t,h.get(e,t)/g[t]);h.set(t,t,h.get(t,t)+1)}g[t]=-g[t]}for(let e=t+1;e<s;e++){if(t<b&&0!==g[t]){let s=0;for(let o=t;o<r;o++)s+=h.get(o,t)*h.get(o,e);s=-s/h.get(t,t);for(let o=t;o<r;o++)h.set(o,e,h.get(o,e)+s*h.get(o,t))}p[e]=h.get(t,e)}if(u&&t<b)for(let e=t;e<r;e++)m.set(e,t,h.get(e,t));if(t<y){p[t]=0;for(let e=t+1;e<s;e++)p[t]=k(p[t],p[e]);if(0!==p[t]){p[t+1]<0&&(p[t]=0-p[t]);for(let e=t+1;e<s;e++)p[e]/=p[t];p[t+1]+=1}if(p[t]=-p[t],t+1<r&&0!==p[t]){for(let e=t+1;e<r;e++)d[e]=0;for(let e=t+1;e<r;e++)for(let r=t+1;r<s;r++)d[e]+=p[r]*h.get(e,r);for(let e=t+1;e<s;e++){let s=-p[e]/p[t+1];for(let o=t+1;o<r;o++)h.set(o,e,h.get(o,e)+s*d[o])}}if(l)for(let e=t+1;e<s;e++)w.set(e,t,p[e])}}let E=Math.min(s,r+1);if(b<s&&(g[b]=h.get(b,b)),r<E&&(g[E-1]=0),y+1<E&&(p[y]=h.get(y,E-1)),p[E-1]=0,u){for(let t=b;t<f;t++){for(let e=0;e<r;e++)m.set(e,t,0);m.set(t,t,1)}for(let t=b-1;t>=0;t--)if(0!==g[t]){for(let e=t+1;e<f;e++){let s=0;for(let o=t;o<r;o++)s+=m.get(o,t)*m.get(o,e);s=-s/m.get(t,t);for(let o=t;o<r;o++)m.set(o,e,m.get(o,e)+s*m.get(o,t))}for(let e=t;e<r;e++)m.set(e,t,-m.get(e,t));m.set(t,t,1+m.get(t,t));for(let e=0;e<t-1;e++)m.set(e,t,0)}else{for(let e=0;e<r;e++)m.set(e,t,0);m.set(t,t,1)}}if(l)for(let t=s-1;t>=0;t--){if(t<y&&0!==p[t])for(let e=t+1;e<s;e++){let r=0;for(let o=t+1;o<s;o++)r+=w.get(o,t)*w.get(o,e);r=-r/w.get(t+1,t);for(let o=t+1;o<s;o++)w.set(o,e,w.get(o,e)+r*w.get(o,t))}for(let e=0;e<s;e++)w.set(e,t,0);w.set(t,t,1)}let S=E-1,R=Number.EPSILON;for(;E>0;){let t,e;for(t=E-2;t>=-1&&-1!==t;t--){const e=Number.MIN_VALUE+R*Math.abs(g[t]+Math.abs(g[t+1]));if(Math.abs(p[t])<=e||Number.isNaN(p[t])){p[t]=0;break}}if(t===E-2)e=4;else{let r;for(r=E-1;r>=t&&r!==t;r--){let e=(r!==E?Math.abs(p[r]):0)+(r!==t+1?Math.abs(p[r-1]):0);if(Math.abs(g[r])<=R*e){g[r]=0;break}}r===t?e=3:r===E-1?e=1:(e=2,t=r)}switch(t++,e){case 1:{let e=p[E-2];p[E-2]=0;for(let r=E-2;r>=t;r--){let o=k(g[r],e),n=g[r]/o,i=e/o;if(g[r]=o,r!==t&&(e=-i*p[r-1],p[r-1]=n*p[r-1]),l)for(let t=0;t<s;t++)o=n*w.get(t,r)+i*w.get(t,E-1),w.set(t,E-1,-i*w.get(t,r)+n*w.get(t,E-1)),w.set(t,r,o)}break}case 2:{let e=p[t-1];p[t-1]=0;for(let s=t;s<E;s++){let o=k(g[s],e),n=g[s]/o,i=e/o;if(g[s]=o,e=-i*p[s],p[s]=n*p[s],u)for(let e=0;e<r;e++)o=n*m.get(e,s)+i*m.get(e,t-1),m.set(e,t-1,-i*m.get(e,s)+n*m.get(e,t-1)),m.set(e,s,o)}break}case 3:{const e=Math.max(Math.abs(g[E-1]),Math.abs(g[E-2]),Math.abs(p[E-2]),Math.abs(g[t]),Math.abs(p[t])),o=g[E-1]/e,n=g[E-2]/e,i=p[E-2]/e,h=g[t]/e,a=p[t]/e,f=((n+o)*(n-o)+i*i)/2,c=o*i*(o*i);let d=0;0===f&&0===c||(d=f<0?0-Math.sqrt(f*f+c):Math.sqrt(f*f+c),d=c/(f+d));let M=(h+o)*(h-o)+d,b=h*a;for(let e=t;e<E-1;e++){let o=k(M,b);0===o&&(o=Number.MIN_VALUE);let n=M/o,i=b/o;if(e!==t&&(p[e-1]=o),M=n*g[e]+i*p[e],p[e]=n*p[e]-i*g[e],b=i*g[e+1],g[e+1]=n*g[e+1],l)for(let t=0;t<s;t++)o=n*w.get(t,e)+i*w.get(t,e+1),w.set(t,e+1,-i*w.get(t,e)+n*w.get(t,e+1)),w.set(t,e,o);if(o=k(M,b),0===o&&(o=Number.MIN_VALUE),n=M/o,i=b/o,g[e]=o,M=n*p[e]+i*g[e+1],g[e+1]=-i*p[e]+n*g[e+1],b=i*p[e+1],p[e+1]=n*p[e+1],u&&e<r-1)for(let t=0;t<r;t++)o=n*m.get(t,e)+i*m.get(t,e+1),m.set(t,e+1,-i*m.get(t,e)+n*m.get(t,e+1)),m.set(t,e,o)}p[E-2]=M;break}case 4:if(g[t]<=0&&(g[t]=g[t]<0?-g[t]:0,l))for(let e=0;e<=S;e++)w.set(e,t,-w.get(e,t));for(;t<S&&!(g[t]>=g[t+1]);){let e=g[t];if(g[t]=g[t+1],g[t+1]=e,l&&t<s-1)for(let r=0;r<s;r++)e=w.get(r,t+1),w.set(r,t+1,w.get(r,t)),w.set(r,t,e);if(u&&t<r-1)for(let s=0;s<r;s++)e=m.get(s,t+1),m.set(s,t+1,m.get(s,t)),m.set(s,t,e);t++}E--}}if(a){let t=w;w=m,m=t}this.m=r,this.n=s,this.s=g,this.U=m,this.V=w}solve(t){let e=t,r=this.threshold,s=this.s.length,o=v.zeros(s,s);for(let t=0;t<s;t++)Math.abs(this.s[t])<=r?o.set(t,t,0):o.set(t,t,1/this.s[t]);let n=this.U,i=this.rightSingularVectors,h=i.mmul(o),u=i.rows,l=n.rows,a=v.zeros(u,l);for(let t=0;t<u;t++)for(let e=0;e<l;e++){let r=0;for(let o=0;o<s;o++)r+=h.get(t,o)*n.get(e,o);a.set(t,e,r)}return a.mmul(e)}solveForDiagonal(t){return this.solve(v.diag(t))}inverse(){let t=this.V,e=this.threshold,r=t.rows,s=t.columns,o=new v(r,this.s.length);for(let n=0;n<r;n++)for(let r=0;r<s;r++)Math.abs(this.s[r])>e&&o.set(n,r,t.get(n,r)/this.s[r]);let n=this.U,i=n.rows,h=n.columns,u=new v(r,i);for(let t=0;t<r;t++)for(let e=0;e<i;e++){let r=0;for(let s=0;s<h;s++)r+=o.get(t,s)*n.get(e,s);u.set(t,e,r)}return u}get condition(){return this.s[0]/this.s[Math.min(this.m,this.n)-1]}get norm2(){return this.s[0]}get rank(){let t=Math.max(this.m,this.n)*this.s[0]*Number.EPSILON,e=0,r=this.s;for(let s=0,o=r.length;s<o;s++)r[s]>t&&e++;return e}get diagonal(){return Array.from(this.s)}get threshold(){return Number.EPSILON/2*Math.max(this.m,this.n)*this.s[0]}get leftSingularVectors(){return this.U}get rightSingularVectors(){return this.V}get diagonalMatrix(){return v.diag(this.s)}}function q(t,e,r=!1){return t=A.checkMatrix(t),e=A.checkMatrix(e),r?new T(t).solve(e):t.isSquare()?new I(t).solve(e):new V(t).solve(e)}function C(t,e){let r=[];for(let s=0;s<t;s++)s!==e&&r.push(s);return r}function N(t,e,r,s=1e-9,o=1e-9){if(t>o)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))<s&&t.set(e,0,0);return t.to1DArray()}}class F{constructor(t,e={}){const{assumeSymmetric:r=!1}=e;if(!(t=A.checkMatrix(t)).isSquare())throw new Error("Matrix is not a square matrix");let s,o,n=t.columns,i=new v(n,n),h=new Float64Array(n),u=new Float64Array(n),l=t,a=!1;if(a=!!r||t.isSymmetric(),a){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),i=r[h+1],0!==i){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,g,m,w,p,d,M,b;for(i=1;i<t;i++)e[i-1]=e[i];e[t-1]=0;let y=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=k(f,1),f<0&&(c=-c),r[l]=e[l]/(f+c),r[l+1]=e[l]*(f+c),g=r[l+1],n=o-r[l],i=l+2;i<t;i++)r[i]-=n;for(y+=n,f=r[a],m=1,w=m,p=m,d=e[l+1],M=0,b=0,i=a-1;i>=l;i--)for(p=w,w=m,b=M,o=m*e[i],n=m*f,c=k(f,e[i]),e[i+1]=M*c,M=e[i]/c,m=f/c,f=m*r[i]-M*o,r[i+1]=n+M*(m*o+M*r[i]),u=0;u<t;u++)n=s.get(u,i+1),s.set(u,i+1,M*s.get(u,i)+m*n),s.set(u,i,m*s.get(u,i)-M*n);f=-M*b*p*d*e[l]/g,e[l]=M*f,r[l]=m*f}while(Math.abs(e[l])>v*x);r[l]=r[l]+y,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 v(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=0,c=t-1;for(l=f+1;l<=c-1;l++){for(a=0,h=l;h<=c;h++)a+=Math.abs(e.get(h,l-1));if(0!==a){for(i=0,h=c;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=c;h>=l;h--)o+=r[h]*e.get(h,u);for(o/=i,h=l;h<=c;h++)e.set(h,u,e.get(h,u)-o*r[h])}for(h=0;h<=c;h++){for(o=0,u=c;u>=l;u--)o+=r[u]*e.get(h,u);for(o/=i,u=l;u<=c;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=c-1;l>=f+1;l--)if(0!==e.get(l,l-1)){for(h=l+1;h<=c;h++)r[h]=e.get(h,l-1);for(u=l;u<=c;u++){for(n=0,h=l;h<=c;h++)n+=r[h]*s.get(h,u);for(n=n/r[l]/e.get(l,l-1),h=l;h<=c;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,g,m,w,p,d,M,b,y=t-1,x=0,v=t-1,E=Number.EPSILON,S=0,R=0,A=0,I=0,k=0,V=0,T=0,q=0;for(n=0;n<t;n++)for((n<x||n>v)&&(r[n]=o.get(n,n),e[n]=0),i=Math.max(n-1,0);i<t;i++)R+=Math.abs(o.get(n,i));for(;y>=x;){for(u=y;u>x&&(V=Math.abs(o.get(u-1,u-1))+Math.abs(o.get(u,u)),0===V&&(V=R),!(Math.abs(o.get(u,u-1))<E*V));)u--;if(u===y)o.set(y,y,o.get(y,y)+S),r[y]=o.get(y,y),e[y]=0,y--,q=0;else if(u===y-1){if(f=o.get(y,y-1)*o.get(y-1,y),A=(o.get(y-1,y-1)-o.get(y,y))/2,I=A*A+f,T=Math.sqrt(Math.abs(I)),o.set(y,y,o.get(y,y)+S),o.set(y-1,y-1,o.get(y-1,y-1)+S),c=o.get(y,y),I>=0){for(T=A>=0?A+T:A-T,r[y-1]=c+T,r[y]=r[y-1],0!==T&&(r[y]=c-f/T),e[y-1]=0,e[y]=0,c=o.get(y,y-1),V=Math.abs(c)+Math.abs(T),A=c/V,I=T/V,k=Math.sqrt(A*A+I*I),A/=k,I/=k,i=y-1;i<t;i++)T=o.get(y-1,i),o.set(y-1,i,I*T+A*o.get(y,i)),o.set(y,i,I*o.get(y,i)-A*T);for(n=0;n<=y;n++)T=o.get(n,y-1),o.set(n,y-1,I*T+A*o.get(n,y)),o.set(n,y,I*o.get(n,y)-A*T);for(n=x;n<=v;n++)T=s.get(n,y-1),s.set(n,y-1,I*T+A*s.get(n,y)),s.set(n,y,I*s.get(n,y)-A*T)}else r[y-1]=c+A,r[y]=c+A,e[y-1]=T,e[y]=-T;y-=2,q=0}else{if(c=o.get(y,y),g=0,f=0,u<y&&(g=o.get(y-1,y-1),f=o.get(y,y-1)*o.get(y-1,y)),10===q){for(S+=c,n=x;n<=y;n++)o.set(n,n,o.get(n,n)-c);V=Math.abs(o.get(y,y-1))+Math.abs(o.get(y-1,y-2)),c=g=.75*V,f=-.4375*V*V}if(30===q&&(V=(g-c)/2,V=V*V+f,V>0)){for(V=Math.sqrt(V),g<c&&(V=-V),V=c-f/((g-c)/2+V),n=x;n<=y;n++)o.set(n,n,o.get(n,n)-V);S+=V,c=g=f=.964}for(q+=1,l=y-2;l>=u&&(T=o.get(l,l),k=c-T,V=g-T,A=(k*V-f)/o.get(l+1,l)+o.get(l,l+1),I=o.get(l+1,l+1)-T-k-V,k=o.get(l+2,l+1),V=Math.abs(A)+Math.abs(I)+Math.abs(k),A/=V,I/=V,k/=V,l!==u)&&!(Math.abs(o.get(l,l-1))*(Math.abs(I)+Math.abs(k))<E*(Math.abs(A)*(Math.abs(o.get(l-1,l-1))+Math.abs(T)+Math.abs(o.get(l+1,l+1)))));)l--;for(n=l+2;n<=y;n++)o.set(n,n-2,0),n>l+2&&o.set(n,n-3,0);for(h=l;h<=y-1&&(M=h!==y-1,h!==l&&(A=o.get(h,h-1),I=o.get(h+1,h-1),k=M?o.get(h+2,h-1):0,c=Math.abs(A)+Math.abs(I)+Math.abs(k),0!==c&&(A/=c,I/=c,k/=c)),0!==c);h++)if(V=Math.sqrt(A*A+I*I+k*k),A<0&&(V=-V),0!==V){for(h!==l?o.set(h,h-1,-V*c):u!==l&&o.set(h,h-1,-o.get(h,h-1)),A+=V,c=A/V,g=I/V,T=k/V,I/=A,k/=A,i=h;i<t;i++)A=o.get(h,i)+I*o.get(h+1,i),M&&(A+=k*o.get(h+2,i),o.set(h+2,i,o.get(h+2,i)-A*T)),o.set(h,i,o.get(h,i)-A*c),o.set(h+1,i,o.get(h+1,i)-A*g);for(n=0;n<=Math.min(y,h+3);n++)A=c*o.get(n,h)+g*o.get(n,h+1),M&&(A+=T*o.get(n,h+2),o.set(n,h+2,o.get(n,h+2)-A*k)),o.set(n,h,o.get(n,h)-A),o.set(n,h+1,o.get(n,h+1)-A*I);for(n=x;n<=v;n++)A=c*s.get(n,h)+g*s.get(n,h+1),M&&(A+=T*s.get(n,h+2),s.set(n,h+2,s.get(n,h+2)-A*k)),s.set(n,h,s.get(n,h)-A),s.set(n,h+1,s.get(n,h+1)-A*I)}}}if(0===R)return;for(y=t-1;y>=0;y--)if(A=r[y],I=e[y],0===I)for(u=y,o.set(y,y,1),n=y-1;n>=0;n--){for(f=o.get(n,n)-A,k=0,i=u;i<=y;i++)k+=o.get(n,i)*o.get(i,y);if(e[n]<0)T=f,V=k;else if(u=n,0===e[n]?o.set(n,y,0!==f?-k/f:-k/(E*R)):(c=o.get(n,n+1),g=o.get(n+1,n),I=(r[n]-A)*(r[n]-A)+e[n]*e[n],a=(c*V-T*k)/I,o.set(n,y,a),o.set(n+1,y,Math.abs(c)>Math.abs(T)?(-k-f*a)/c:(-V-g*a)/T)),a=Math.abs(o.get(n,y)),E*a*a>1)for(i=n;i<=y;i++)o.set(i,y,o.get(i,y)/a)}else if(I<0)for(u=y-1,Math.abs(o.get(y,y-1))>Math.abs(o.get(y-1,y))?(o.set(y-1,y-1,I/o.get(y,y-1)),o.set(y-1,y,-(o.get(y,y)-A)/o.get(y,y-1))):(b=D(0,-o.get(y-1,y),o.get(y-1,y-1)-A,I),o.set(y-1,y-1,b[0]),o.set(y-1,y,b[1])),o.set(y,y-1,0),o.set(y,y,1),n=y-2;n>=0;n--){for(m=0,w=0,i=u;i<=y;i++)m+=o.get(n,i)*o.get(i,y-1),w+=o.get(n,i)*o.get(i,y);if(f=o.get(n,n)-A,e[n]<0)T=f,k=m,V=w;else if(u=n,0===e[n]?(b=D(-m,-w,f,I),o.set(n,y-1,b[0]),o.set(n,y,b[1])):(c=o.get(n,n+1),g=o.get(n+1,n),p=(r[n]-A)*(r[n]-A)+e[n]*e[n]-I*I,d=2*(r[n]-A)*I,0===p&&0===d&&(p=E*R*(Math.abs(f)+Math.abs(I)+Math.abs(c)+Math.abs(g)+Math.abs(T))),b=D(c*k-T*m+I*w,c*V-T*w-I*m,p,d),o.set(n,y-1,b[0]),o.set(n,y,b[1]),Math.abs(c)>Math.abs(T)+Math.abs(I)?(o.set(n+1,y-1,(-m-f*o.get(n,y-1)+I*o.get(n,y))/c),o.set(n+1,y,(-w-f*o.get(n,y)-I*o.get(n,y-1))/c)):(b=D(-k-g*o.get(n,y-1),-V-g*o.get(n,y),T,I),o.set(n+1,y-1,b[0]),o.set(n+1,y,b[1]))),a=Math.max(Math.abs(o.get(n,y-1)),Math.abs(o.get(n,y))),E*a*a>1)for(i=n;i<=y;i++)o.set(i,y-1,o.get(i,y-1)/a),o.set(i,y,o.get(i,y)/a)}for(n=0;n<t;n++)if(n<x||n>v)for(i=n;i<t;i++)s.set(n,i,o.get(n,i));for(i=t-1;i>=x;i--)for(n=x;n<=v;n++){for(T=0,h=x;h<=Math.min(i,v);h++)T+=s.get(n,h)*o.get(h,i);s.set(n,i,T)}}(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 v(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 D(t,e,r,s){let o,n;return Math.abs(r)>Math.abs(s)?(o=s/r,n=r+o*s,[(t+o*e)/n,(e-o*t)/n]):(o=r/s,n=s+o*r,[(o*t+e)/n,(o*e-t)/n])}class j{constructor(t){if(!(t=A.checkMatrix(t)).isSymmetric())throw new Error("Matrix is not symmetric");let e,r,s,o=t,n=o.rows,i=new v(n,n),h=!0;for(r=0;r<n;r++){let t=0;for(s=0;s<r;s++){let n=0;for(e=0;e<s;e++)n+=i.get(s,e)*i.get(r,e);n=(o.get(r,s)-n)/i.get(s,s),i.set(r,s,n),t+=n*n}for(t=o.get(r,r)-t,h&=t>0,i.set(r,r,Math.sqrt(Math.max(t,0))),s=r+1;s<n;s++)i.set(r,s,0)}this.L=i,this.positiveDefinite=Boolean(h)}isPositiveDefinite(){return this.positiveDefinite}solve(t){t=A.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 s,o,n,i=t.columns,h=t.clone();for(n=0;n<r;n++)for(o=0;o<i;o++){for(s=0;s<n;s++)h.set(n,o,h.get(n,o)-h.get(s,o)*e.get(n,s));h.set(n,o,h.get(n,o)/e.get(n,n))}for(n=r-1;n>=0;n--)for(o=0;o<i;o++){for(s=n+1;s<r;s++)h.set(n,o,h.get(n,o)-h.get(s,o)*e.get(s,n));h.set(n,o,h.get(n,o)/e.get(n,n))}return h}get lowerTriangularMatrix(){return this.L}}class L{constructor(t,e={}){t=A.checkMatrix(t);let{Y:r}=e;const{scaleScores:s=!1,maxIterations:o=1e3,terminationCriteria:n=1e-10}=e;let i;if(r){if(r=Array.isArray(r)&&"number"==typeof r[0]?v.columnVector(r):A.checkMatrix(r),!r.isColumnVector()||r.rows!==t.rows)throw new Error("Y must be a column vector of length X.rows");i=r}else i=t.getColumnVector(0);let h,u,l,a,f=1;for(let e=0;e<o&&f>n;e++)l=t.transpose().mmul(i).div(i.transpose().mmul(i).get(0,0)),l=l.div(l.norm()),h=t.mmul(l).div(l.transpose().mmul(l).get(0,0)),e>0&&(f=h.clone().sub(a).pow(2).sum()),a=h.clone(),r?(u=r.transpose().mmul(h).div(h.transpose().mmul(h).get(0,0)),u=u.div(u.norm()),i=r.mmul(u).div(u.transpose().mmul(u).get(0,0))):i=h;if(r){let e=t.transpose().mmul(h).div(h.transpose().mmul(h).get(0,0));e=e.div(e.norm());let s=t.clone().sub(h.clone().mmul(e.transpose())),o=i.transpose().mmul(h).div(h.transpose().mmul(h).get(0,0)),n=r.clone().sub(h.clone().mulS(o.get(0,0)).mmul(u.transpose()));this.t=h,this.p=e.transpose(),this.w=l.transpose(),this.q=u,this.u=i,this.s=h.transpose().mmul(h),this.xResidual=s,this.yResidual=n,this.betas=o}else this.w=l.transpose(),this.s=h.transpose().mmul(h).sqrt(),this.t=s?h.clone().div(this.s.get(0,0)):h,this.xResidual=t.sub(h.mmul(l.transpose()))}}t.AbstractMatrix=y,t.CHO=j,t.CholeskyDecomposition=j,t.EVD=F,t.EigenvalueDecomposition=F,t.LU=I,t.LuDecomposition=I,t.Matrix=v,t.MatrixColumnSelectionView=class extends E{constructor(t,e){e=p(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])}},t.MatrixColumnView=class extends E{constructor(t,e){f(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)}},t.MatrixFlipColumnView=class extends E{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)}},t.MatrixFlipRowView=class extends E{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)}},t.MatrixRowSelectionView=class extends E{constructor(t,e){super(t,(e=w(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)}},t.MatrixRowView=class extends E{constructor(t,e){a(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)}},t.MatrixSelectionView=S,t.MatrixSubView=class extends E{constructor(t,e,r,s,o){d(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)}},t.MatrixTransposeView=class extends E{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)}},t.NIPALS=L,t.Nipals=L,t.QR=V,t.QrDecomposition=V,t.SVD=T,t.SingularValueDecomposition=T,t.WrapperMatrix1D=R,t.WrapperMatrix2D=A,t.correlation=function(t,e=t,r={}){t=new v(t);let s=!1;if("object"!=typeof e||v.isMatrix(e)||Array.isArray(e)?e=new v(e):(r=e,e=t,s=!0),t.rows!==e.rows)throw new TypeError("Both matrices must have the same number of rows");const{center:o=!0,scale:n=!0}=r;o&&(t.center("column"),s||e.center("column")),n&&(t.scale("column"),s||e.scale("column"));const i=t.standardDeviation("column",{unbiased:!0}),h=s?i:e.standardDeviation("column",{unbiased:!0}),u=t.transpose().mmul(e);for(let e=0;e<u.rows;e++)for(let r=0;r<u.columns;r++)u.set(e,r,u.get(e,r)*(1/(i[e]*h[r]))*(1/(t.rows-1)));return u},t.covariance=function(t,e=t,r={}){t=new v(t);let s=!1;if("object"!=typeof e||v.isMatrix(e)||Array.isArray(e)?e=new v(e):(r=e,e=t,s=!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"),s||(e=e.center("column")));const n=t.transpose().mmul(e);for(let e=0;e<n.rows;e++)for(let r=0;r<n.columns;r++)n.set(e,r,n.get(e,r)*(1/(t.rows-1)));return n},t.default=v,t.determinant=function t(e){if((e=v.checkMatrix(e)).isSquare()){let r,s,o,n;if(2===e.columns)return r=e.get(0,0),s=e.get(0,1),o=e.get(1,0),n=e.get(1,1),r*n-s*o;if(3===e.columns){let n,i,h;return n=new S(e,[1,2],[1,2]),i=new S(e,[1,2],[0,2]),h=new S(e,[1,2],[0,1]),r=e.get(0,0),s=e.get(0,1),o=e.get(0,2),r*t(n)-s*t(i)+o*t(h)}return new I(e).determinant}throw Error("determinant can only be calculated for a square matrix")},t.inverse=function(t,e=!1){return t=A.checkMatrix(t),e?new T(t).inverse():q(t,v.eye(t.rows))},t.linearDependencies=function(t,e={}){const{thresholdValue:r=1e-9,thresholdError:s=1e-9}=e;let o=(t=v.checkMatrix(t)).rows,n=new v(o,o);for(let e=0;e<o;e++){let i=v.columnVector(t.getRow(e)),h=t.subMatrixRow(C(o,e)).transpose(),u=new T(h).solve(i),l=v.sub(i,h.mmul(u)).abs().max();n.setRow(e,N(l,u,e,r,s))}return n},t.pseudoInverse=function(t,e=Number.EPSILON){t=v.checkMatrix(t);let r=new T(t,{autoTranspose:!0}),s=r.leftSingularVectors,o=r.rightSingularVectors,n=r.diagonal;for(let t=0;t<n.length;t++)Math.abs(n[t])>e?n[t]=1/n[t]:n[t]=0;return o.mmul(v.diag(n).mmul(s.transpose()))},t.solve=q,t.wrap=function(t,e){if(Array.isArray(t))return t[0]&&Array.isArray(t[0])?new A(t):new R(t,e);throw new Error("the argument is not an array")},Object.defineProperty(t,"__esModule",{value:!0})})); |
{ | ||
"name": "ml-matrix", | ||
"version": "6.5.2", | ||
"version": "6.5.3", | ||
"description": "Matrix manipulation and computation library", | ||
@@ -18,3 +18,2 @@ "main": "matrix.js", | ||
"scripts": { | ||
"build-docs": "typedoc --out docs --name \"ml-matrix\" --mode file --includeDeclarations --excludeExternals --hideGenerator --excludePrivate --moduleResolution node matrix.d.ts", | ||
"compile": "rollup -c", | ||
@@ -60,23 +59,21 @@ "eslint": "eslint benchmark src testUtils.js", | ||
"@babel/plugin-transform-modules-commonjs": "^7.10.4", | ||
"@rollup/plugin-commonjs": "^14.0.0", | ||
"@rollup/plugin-node-resolve": "^8.4.0", | ||
"@rollup/plugin-commonjs": "^15.1.0", | ||
"@rollup/plugin-node-resolve": "^9.0.0", | ||
"benchmark": "^2.1.4", | ||
"csv-parse": "^4.11.1", | ||
"eslint": "^7.10.0", | ||
"csv-parse": "^4.12.0", | ||
"eslint": "^7.11.0", | ||
"eslint-config-cheminfo": "^5.2.2", | ||
"jest": "^26.1.0", | ||
"jest": "^26.5.2", | ||
"jest-matcher-deep-close-to": "^2.0.1", | ||
"mathjs": "^7.1.0", | ||
"mathjs": "^7.5.1", | ||
"ml-dataset-iris": "^1.1.1", | ||
"numeric": "^1.2.6", | ||
"prettier": "^2.0.5", | ||
"prettier": "^2.1.2", | ||
"pretty-hrtime": "^1.0.3", | ||
"rollup": "^2.23.0", | ||
"rollup-plugin-terser": "^6.1.0", | ||
"typedoc": "^0.17.8", | ||
"typescript": "^3.9.7" | ||
"rollup": "^2.29.0", | ||
"rollup-plugin-terser": "^7.0.2" | ||
}, | ||
"dependencies": { | ||
"ml-array-rescale": "^1.3.1" | ||
"ml-array-rescale": "^1.3.2" | ||
} | ||
} |
@@ -0,0 +0,0 @@ # ml-matrix |
@@ -0,0 +0,0 @@ import Matrix from './matrix'; |
@@ -0,0 +0,0 @@ import Matrix from './matrix'; |
@@ -0,0 +0,0 @@ import Matrix from '../matrix'; |
@@ -0,0 +0,0 @@ import Matrix from '../matrix'; |
@@ -0,0 +0,0 @@ import Matrix from '../matrix'; |
@@ -0,0 +0,0 @@ import Matrix from '../matrix'; |
@@ -0,0 +0,0 @@ import Matrix from '../matrix'; |
@@ -0,0 +0,0 @@ import Matrix from '../matrix'; |
@@ -0,0 +0,0 @@ export function hypotenuse(a, b) { |
@@ -0,0 +0,0 @@ import LuDecomposition from './dc/lu'; |
@@ -0,0 +0,0 @@ import LuDecomposition from './dc/lu'; |
@@ -0,0 +0,0 @@ export { AbstractMatrix, default, default as Matrix } from './matrix'; |
@@ -0,0 +0,0 @@ const indent = ' '.repeat(2); |
@@ -0,0 +0,0 @@ import SingularValueDecomposition from './dc/svd'; |
@@ -0,0 +0,0 @@ export function installMathOperations(AbstractMatrix, Matrix) { |
@@ -1430,2 +1430,3 @@ import rescale from 'ml-array-rescale'; | ||
if (Matrix.isMatrix(nRows)) { | ||
// eslint-disable-next-line no-constructor-return | ||
return nRows.clone(); | ||
@@ -1466,3 +1467,2 @@ } else if (Number.isInteger(nRows) && nRows > 0) { | ||
this.columns = nColumns; | ||
return this; | ||
} | ||
@@ -1469,0 +1469,0 @@ |
@@ -0,0 +0,0 @@ import SVD from './dc/svd'; |
@@ -0,0 +0,0 @@ import { newArray } from './util'; |
@@ -0,0 +0,0 @@ /** |
@@ -0,0 +0,0 @@ import { AbstractMatrix } from '../matrix'; |
@@ -0,0 +0,0 @@ import { checkColumnIndex } from '../util'; |
@@ -0,0 +0,0 @@ import { checkColumnIndices } from '../util'; |
@@ -0,0 +0,0 @@ import BaseView from './base'; |
@@ -0,0 +0,0 @@ import BaseView from './base'; |
@@ -0,0 +0,0 @@ export { default as MatrixColumnView } from './column'; |
@@ -0,0 +0,0 @@ import { checkRowIndex } from '../util'; |
@@ -0,0 +0,0 @@ import { checkRowIndices } from '../util'; |
@@ -0,0 +0,0 @@ import { checkIndices } from '../util'; |
@@ -0,0 +0,0 @@ import { checkRange } from '../util'; |
@@ -0,0 +0,0 @@ import BaseView from './base'; |
@@ -0,0 +0,0 @@ import WrapperMatrix1D from './WrapperMatrix1D'; |
@@ -0,0 +0,0 @@ import { AbstractMatrix } from '../matrix'; |
@@ -0,0 +0,0 @@ import { AbstractMatrix } from '../matrix'; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16
382059
1
Updatedml-array-rescale@^1.3.2