!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){const r=e.call(t);return r.endsWith("Array]")&&!r.includes("Big")}function s(t){var e,s=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!==s.output){if(!r(s.output))throw new TypeError("output option must be an array if specified");e=s.output}else e=new Array(t.length);var o=function(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,i=e.toIndex,n=void 0===i?t.length:i;if(o<0||o>=t.length||!Number.isInteger(o))throw new Error("fromIndex must be a positive integer smaller than length");if(n<=o||n>t.length||!Number.isInteger(n))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;u1&&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,i=e.toIndex,n=void 0===i?t.length:i;if(o<0||o>=t.length||!Number.isInteger(o))throw new Error("fromIndex must be a positive integer smaller than length");if(n<=o||n>t.length||!Number.isInteger(n))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;uh&&(h=t[u]);return h}(t);if(o===i)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");var n=s.min,h=void 0===n?s.autoMinMax?o:0:n,u=s.max,l=void 0===u?s.autoMinMax?i:1:u;if(h>=l)throw new RangeError("min option must be smaller than max option");for(var a=(l-h)/(i-o),f=0;f=0&&r?` ${u(t,e-1)}`:u(t,e)).padEnd(e)}function u(t,e){let r=t.toString();if(r.length<=e)return r;let s=t.toFixed(e);if(s.length>e&&(s=t.toFixed(Math.max(0,e-(s.length-e)))),s.length<=e&&!s.startsWith("0.000")&&!s.startsWith("-0.000"))return s;let o=t.toExponential(e);return o.length>e&&(o=t.toExponential(Math.max(0,e-(o.length-e)))),o.slice(0)}function l(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 a(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 f(t,e){if(e.to1DArray&&(e=e.to1DArray()),e.length!==t.columns)throw new RangeError("vector size must be the same as the number of columns");return e}function c(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){if(!r(e))throw new TypeError("row indices must be an array");for(let r=0;r=t.rows)throw new RangeError("row indices are out of range")}function g(t,e){if(!r(e))throw new TypeError("column indices must be an array");for(let r=0;r=t.columns)throw new RangeError("column indices are out of range")}function w(t,e,r,s,o){if(5!==arguments.length)throw new RangeError("expected 4 arguments");if(d("startRow",e),d("endRow",r),d("startColumn",s),d("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 p(t,e=0){let r=[];for(let s=0;s=o)throw new RangeError("min must be smaller than max");let n=o-s,h=new x(t,e);for(let r=0;rr?(o=!0,r=e):(s=!1,o=!0);t++}return s}isReducedEchelonForm(){let t=0,e=0,r=-1,s=!0,o=!1;for(;tr?(o=!0,r=e):(s=!1,o=!0);for(let r=e+1;rt.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=0;)if(0===t.maxRow(s))s--;else{let o=0,i=!1;for(;ot[e]&&(t[e]=this.get(e,r));return t}case"column":{const t=new Array(this.columns).fill(Number.NEGATIVE_INFINITY);for(let e=0;et[r]&&(t[r]=this.get(e,r));return t}case void 0:{let t=this.get(0,0);for(let e=0;et&&(t=this.get(e,r));return t}default:throw new Error(`invalid option: ${t}`)}}maxIndex(){y(this);let t=this.get(0,0),e=[0,0];for(let r=0;rt&&(t=this.get(r,s),e[0]=r,e[1]=s);return e}min(t){if(this.isEmpty())return NaN;switch(t){case"row":{const t=new Array(this.rows).fill(Number.POSITIVE_INFINITY);for(let e=0;ee&&(e=this.get(t,r));return e}maxRowIndex(t){l(this,t),y(this);let e=this.get(t,0),r=[t,0];for(let s=1;se&&(e=this.get(t,s),r[1]=s);return r}minRow(t){if(l(this,t),this.isEmpty())return NaN;let e=this.get(t,0);for(let r=1;re&&(e=this.get(r,t));return e}maxColumnIndex(t){a(this,t),y(this);let e=this.get(0,t),r=[0,t];for(let s=1;se&&(e=this.get(s,t),r[0]=s);return r}minColumn(t){if(a(this,t),this.isEmpty())return NaN;let e=this.get(0,t);for(let r=1;r=1;s/=2)1&s&&(e=e.mmul(r)),r=r.mmul(r);return e}strassen2x2(t){t=x.checkMatrix(t);let e=new x(2,2);const r=this.get(0,0),s=t.get(0,0),o=this.get(0,1),i=t.get(0,1),n=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=(n+u)*s,c=r*(i-l),m=u*(h-s),g=(r+o)*l,w=a+m-g+(o-u)*(h+l),p=c+g,d=f+m,y=a-f+c+(n-r)*(s+i);return e.set(0,0,w),e.set(0,1,p),e.set(1,0,d),e.set(1,1,y),e}strassen3x3(t){t=x.checkMatrix(t);let e=new x(3,3);const r=this.get(0,0),s=this.get(0,1),o=this.get(0,2),i=this.get(1,0),n=this.get(1,1),h=this.get(1,2),u=this.get(2,0),l=this.get(2,1),a=this.get(2,2),f=t.get(0,0),c=t.get(0,1),m=t.get(0,2),g=t.get(1,0),w=t.get(1,1),p=t.get(1,2),d=t.get(2,0),y=t.get(2,1),M=t.get(2,2),b=(r-i)*(-c+w),E=(-r+i+n)*(f-c+w),S=(i+n)*(-f+c),v=r*f,R=(-r+u+l)*(f-m+p),I=(-r+u)*(m-p),k=(u+l)*(-f+m),T=(-o+l+a)*(w+d-y),N=(o-a)*(w-y),V=o*d,C=(l+a)*(-d+y),q=(-o+n+h)*(p+d-M),A=(o-h)*(p-M),D=(n+h)*(-d+M),F=v+V+s*g,z=(r+s+o-i-n-l-a)*w+E+S+v+T+V+C,$=v+R+k+(r+s+o-n-h-u-l)*p+V+q+D,j=b+n*(-f+c+g-w-p-d+M)+E+v+V+q+A,P=b+E+S+v+h*y,L=V+q+A+D+i*m,U=v+R+I+l*(-f+m+g-w-p-d+y)+T+N+V,O=T+N+V+C+u*c,_=v+R+I+k+a*M;return e.set(0,0,F),e.set(0,1,z),e.set(0,2,$),e.set(1,0,j),e.set(1,1,P),e.set(1,2,L),e.set(2,0,U),e.set(2,1,O),e.set(2,2,_),e}mmulStrassen(t){t=x.checkMatrix(t);let e=this.clone(),r=e.rows,s=e.columns,o=t.rows,i=t.columns;function n(t,e,r){let s=t.rows,o=t.columns;if(s===e&&o===r)return t;{let s=M.zeros(e,r);return s=s.setSubMatrix(t,0,0),s}}s!==o&&console.warn(`Multiplying ${r} x ${s} and ${o} x ${i} matrix: dimensions do not match.`);let h=Math.max(r,o),u=Math.max(s,i);return e=n(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=n(e,s+1,o+1),r=n(r,s+1,o+1)):s%2==1?(e=n(e,s+1,o),r=n(r,s+1,o)):o%2==1&&(e=n(e,s,o+1),r=n(r,s,o+1));let i=parseInt(e.rows/2,10),h=parseInt(e.columns/2,10),u=e.subMatrix(0,i-1,0,h-1),l=r.subMatrix(0,i-1,0,h-1),a=e.subMatrix(0,i-1,h,e.columns-1),f=r.subMatrix(0,i-1,h,r.columns-1),c=e.subMatrix(i,e.rows-1,0,h-1),m=r.subMatrix(i,r.rows-1,0,h-1),g=e.subMatrix(i,e.rows-1,h,e.columns-1),w=r.subMatrix(i,r.rows-1,h,r.columns-1),p=t(M.add(u,g),M.add(l,w),i,h),d=t(M.add(c,g),l,i,h),y=t(u,M.sub(f,w),i,h),b=t(g,M.sub(m,l),i,h),x=t(M.add(u,a),w,i,h),E=t(M.sub(c,u),M.add(l,f),i,h),S=t(M.sub(a,g),M.add(m,w),i,h),v=M.add(p,b);v.sub(x),v.add(S);let R=M.add(y,x),I=M.add(d,b),k=M.sub(p,d);k.add(y),k.add(E);let T=M.zeros(2*v.rows,2*v.columns);return T=T.setSubMatrix(v,0,0),T=T.setSubMatrix(R,v.rows,0),T=T.setSubMatrix(I,0,v.columns),T=T.setSubMatrix(k,v.rows,v.columns),T.subMatrix(0,s-1,0,o-1)}(e,t=n(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 o=new x(this.rows,this.columns);for(let t=0;t0&&s(i,{min:e,max:r,output:i}),o.setRow(t,i)}return o}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 o=new x(this.rows,this.columns);for(let t=0;tr||e<0||e>=this.columns||r<0||r>=this.columns)throw new RangeError("Argument out of range");let s=new x(t.length,r-e+1);for(let o=0;o=this.rows)throw new RangeError(`Row index out of range: ${t[o]}`);s.set(o,i-e,this.get(t[o],i))}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 x(r-e+1,t.length);for(let o=0;o=this.columns)throw new RangeError(`Column index out of range: ${t[o]}`);s.set(i-e,o,this.get(i,t[o]))}return s}setSubMatrix(t,e,r){if((t=x.checkMatrix(t)).isEmpty())return this;w(this,e,e+t.rows-1,r,r+t.columns-1);for(let s=0;s=0))throw new TypeError("nColumns must be a positive integer");for(let r=0;r=0)this.#t(t,e);else{if(!r(t))throw new TypeError("First argument must be a positive number or an array");{const r=t;if("number"!=typeof(e=(t=r.length)?r[0].length:0))throw new TypeError("Data must be a 2D array with at least one element");this.data=[];for(let s=0;s"number"==typeof t)))throw new TypeError("Input data contains non-numeric values");this.data.push(Float64Array.from(r[s]))}this.rows=t,this.columns=e}}}set(t,e,r){return this.data[t][e]=r,this}get(t,e){return this.data[t][e]}removeRow(t){return l(this,t),this.data.splice(t,1),this.rows-=1,this}addRow(t,e){return void 0===e&&(e=t,t=this.rows),l(this,t,!0),e=Float64Array.from(f(this,e)),this.data.splice(t,0,e),this.rows+=1,this}removeColumn(t){a(this,t);for(let e=0;e>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>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>>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>>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=0)this.#e=new x(t,t);else if(this.#e=new x(t),!this.isSymmetric())throw new TypeError("not symmetric data")}clone(){const t=new E(this.diagonalSize);for(const[e,r,s]of this.upperRightEntries())t.set(e,r,s);return t}toMatrix(){return new x(this)}get(t,e){return this.#e.get(t,e)}set(t,e,r){return this.#e.set(t,e,r),this.#e.set(e,t,r),this}removeCross(t){return this.#e.removeRow(t),this.#e.removeColumn(t),this}addCross(t,e){void 0===e&&(e=t,t=this.diagonalSize);const r=e.slice();return r.splice(t,1),this.#e.addRow(t,r),this.#e.addColumn(t,e),this}applyMask(t){if(t.length!==this.diagonalSize)throw new RangeError("Mask size do not match with matrix size");const e=[];for(const[r,s]of t.entries())s||e.push(r);e.reverse();for(const t of e)this.removeCross(t);return this}toCompact(){const{diagonalSize:t}=this,e=new Array(t*(t+1)/2);for(let r=0,s=0,o=0;o=t&&(r=++s);return e}static fromCompact(t){const e=t.length,r=(Math.sqrt(8*e+1)-1)/2;if(!Number.isInteger(r))throw new TypeError(`This array is not a compact representation of a Symmetric Matrix, ${JSON.stringify(t)}`);const s=new E(r);for(let o=0,i=0,n=0;n=r&&(o=++i);return s}*upperRightEntries(){for(let t=0,e=0;t=this.diagonalSize&&(e=++t)}}*upperRightValues(){for(let t=0,e=0;t=this.diagonalSize&&(e=++t)}}}E.prototype.klassType="SymmetricMatrix";class S extends E{static isDistanceMatrix(t){return E.isSymmetricMatrix(t)&&"DistanceMatrix"===t.klassSubType}constructor(t){if(super(t),!this.isDistance())throw new TypeError("Provided arguments do no produce a distance matrix")}set(t,e,r){return t===e&&(r=0),super.set(t,e,r)}addCross(t,e){return void 0===e&&(e=t,t=this.diagonalSize),(e=e.slice())[t]=0,super.addCross(t,e)}toSymmetricMatrix(){return new E(this)}clone(){const t=new S(this.diagonalSize);for(const[e,r,s]of this.upperRightEntries())e!==r&&t.set(e,r,s);return t}toCompact(){const{diagonalSize:t}=this,e=new Array((t-1)*t/2);for(let r=1,s=0,o=0;o=t&&(r=1+ ++s);return e}static fromCompact(t){const e=t.length;if(0===e)return new this(0);const r=(Math.sqrt(8*e+1)+1)/2;if(!Number.isInteger(r))throw new TypeError(`This array is not a compact representation of a DistanceMatrix, ${JSON.stringify(t)}`);const s=new this(r);for(let o=1,i=0,n=0;n=r&&(o=1+ ++i);return s}}S.prototype.klassSubType="DistanceMatrix";class v extends M{constructor(t,e,r){super(),this.matrix=t,this.rows=e,this.columns=r}}class R extends v{constructor(t,e,r){m(t,e),g(t,r),super(t,e.length,r.length),this.rowIndices=e,this.columnIndices=r}set(t,e,r){return this.matrix.set(this.rowIndices[t],this.columnIndices[e],r),this}get(t,e){return this.matrix.get(this.rowIndices[t],this.columnIndices[e])}}class I extends M{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 k extends M{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 T{constructor(t){let e,r,s,o,i,n,h,u,l,a=(t=k.checkMatrix(t)).clone(),f=a.rows,c=a.columns,m=new Float64Array(f),g=1;for(e=0;eMath.abs(u[o])&&(o=e);if(o!==r){for(s=0;s=0;o--){for(s=0;se?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 x(e,r);for(let o=0;oMath.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,i=(t=k.checkMatrix(t)).clone(),n=t.rows,h=t.columns,u=new Float64Array(h);for(s=0;s=0;i--){for(o=0;o=0;r--){for(t=0;t=0;t--)if(0!==m[t]){for(let e=t+1;e=0;t--){if(t0;){let t,e;for(t=S-2;t>=-1&&-1!==t;t--){const e=Number.MIN_VALUE+R*Math.abs(m[t]+Math.abs(m[t+1]));if(Math.abs(p[t])<=e||Number.isNaN(p[t])){p[t]=0;break}}if(t===S-2)e=4;else{let r;for(r=S-1;r>=t&&r!==t;r--){let e=(r!==S?Math.abs(p[r]):0)+(r!==t+1?Math.abs(p[r-1]):0);if(Math.abs(m[r])<=R*e){m[r]=0;break}}r===t?e=3:r===S-1?e=1:(e=2,t=r)}switch(t++,e){case 1:{let e=p[S-2];p[S-2]=0;for(let r=S-2;r>=t;r--){let o=N(m[r],e),i=m[r]/o,n=e/o;if(m[r]=o,r!==t&&(e=-n*p[r-1],p[r-1]=i*p[r-1]),l)for(let t=0;t=m[t+1]);){let e=m[t];if(m[t]=m[t+1],m[t+1]=e,l&&te&&o.set(i,r,t.get(i,r)/this.s[r]);let i=this.U,n=i.rows,h=i.columns,u=new x(r,n);for(let t=0;tt&&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 x.diag(this.s)}}function q(t,e,r=!1){return t=k.checkMatrix(t),e=k.checkMatrix(e),r?new C(t).solve(e):t.isSquare()?new T(t).solve(e):new V(t).solve(e)}function A(t,e){let r=[];for(let s=0;so)return new Array(e.rows+1).fill(0);{let t=e.addRow(r,[0]);for(let e=0;e0;h--){for(f=0,n=0,l=0;l0&&(i=-i),e[h]=f*i,n-=o*i,r[h-1]=o-i,u=0;ul)do{for(o=r[l],f=(r[l+1]-o)/(2*e[l]),c=N(f,1),f<0&&(c=-c),r[l]=e[l]/(f+c),r[l+1]=e[l]*(f+c),m=r[l+1],i=o-r[l],n=l+2;n=l;n--)for(p=w,w=g,M=y,o=g*e[n],i=g*f,c=N(f,e[n]),e[n+1]=y*c,y=e[n]/c,g=f/c,f=g*r[n]-y*o,r[n+1]=i+y*(g*o+y*r[n]),u=0;uE*x);r[l]=r[l]+b,e[l]=0}for(n=0;n=l;h--)r[h]=e.get(h,l-1)/a,n+=r[h]*r[h];for(i=Math.sqrt(n),r[l]>0&&(i=-i),n-=r[l]*i,r[l]=r[l]-i,u=l;u=l;h--)o+=r[h]*e.get(h,u);for(o/=n,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/=n,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*i)}}for(h=0;h=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(i=0,h=l;h<=c;h++)i+=r[h]*s.get(h,u);for(i=i/r[l]/e.get(l,l-1),h=l;h<=c;h++)s.set(h,u,s.get(h,u)+i*r[h])}}}(i,t,e,n),function(t,e,r,s,o){let i,n,h,u,l,a,f,c,m,g,w,p,d,y,M,b=t-1,x=0,E=t-1,S=Number.EPSILON,v=0,R=0,I=0,k=0,T=0,N=0,V=0,C=0;for(i=0;iE)&&(r[i]=o.get(i,i),e[i]=0),n=Math.max(i-1,0);n=x;){for(u=b;u>x&&(N=Math.abs(o.get(u-1,u-1))+Math.abs(o.get(u,u)),0===N&&(N=R),!(Math.abs(o.get(u,u-1))=0){for(V=I>=0?I+V:I-V,r[b-1]=c+V,r[b]=r[b-1],0!==V&&(r[b]=c-f/V),e[b-1]=0,e[b]=0,c=o.get(b,b-1),N=Math.abs(c)+Math.abs(V),I=c/N,k=V/N,T=Math.sqrt(I*I+k*k),I/=T,k/=T,n=b-1;n0)){for(N=Math.sqrt(N),m=u&&(V=o.get(l,l),T=c-V,N=m-V,I=(T*N-f)/o.get(l+1,l)+o.get(l,l+1),k=o.get(l+1,l+1)-V-T-N,T=o.get(l+2,l+1),N=Math.abs(I)+Math.abs(k)+Math.abs(T),I/=N,k/=N,T/=N,l!==u)&&!(Math.abs(o.get(l,l-1))*(Math.abs(k)+Math.abs(T))l+2&&o.set(i,i-3,0);for(h=l;h<=b-1&&(y=h!==b-1,h!==l&&(I=o.get(h,h-1),k=o.get(h+1,h-1),T=y?o.get(h+2,h-1):0,c=Math.abs(I)+Math.abs(k)+Math.abs(T),0!==c&&(I/=c,k/=c,T/=c)),0!==c);h++)if(N=Math.sqrt(I*I+k*k+T*T),I<0&&(N=-N),0!==N){for(h!==l?o.set(h,h-1,-N*c):u!==l&&o.set(h,h-1,-o.get(h,h-1)),I+=N,c=I/N,m=k/N,V=T/N,k/=I,T/=I,n=h;n=0;b--)if(I=r[b],k=e[b],0===k)for(u=b,o.set(b,b,1),i=b-1;i>=0;i--){for(f=o.get(i,i)-I,T=0,n=u;n<=b;n++)T+=o.get(i,n)*o.get(n,b);if(e[i]<0)V=f,N=T;else if(u=i,0===e[i]?o.set(i,b,0!==f?-T/f:-T/(S*R)):(c=o.get(i,i+1),m=o.get(i+1,i),k=(r[i]-I)*(r[i]-I)+e[i]*e[i],a=(c*N-V*T)/k,o.set(i,b,a),o.set(i+1,b,Math.abs(c)>Math.abs(V)?(-T-f*a)/c:(-N-m*a)/V)),a=Math.abs(o.get(i,b)),S*a*a>1)for(n=i;n<=b;n++)o.set(n,b,o.get(n,b)/a)}else if(k<0)for(u=b-1,Math.abs(o.get(b,b-1))>Math.abs(o.get(b-1,b))?(o.set(b-1,b-1,k/o.get(b,b-1)),o.set(b-1,b,-(o.get(b,b)-I)/o.get(b,b-1))):(M=z(0,-o.get(b-1,b),o.get(b-1,b-1)-I,k),o.set(b-1,b-1,M[0]),o.set(b-1,b,M[1])),o.set(b,b-1,0),o.set(b,b,1),i=b-2;i>=0;i--){for(g=0,w=0,n=u;n<=b;n++)g+=o.get(i,n)*o.get(n,b-1),w+=o.get(i,n)*o.get(n,b);if(f=o.get(i,i)-I,e[i]<0)V=f,T=g,N=w;else if(u=i,0===e[i]?(M=z(-g,-w,f,k),o.set(i,b-1,M[0]),o.set(i,b,M[1])):(c=o.get(i,i+1),m=o.get(i+1,i),p=(r[i]-I)*(r[i]-I)+e[i]*e[i]-k*k,d=2*(r[i]-I)*k,0===p&&0===d&&(p=S*R*(Math.abs(f)+Math.abs(k)+Math.abs(c)+Math.abs(m)+Math.abs(V))),M=z(c*T-V*g+k*w,c*N-V*w-k*g,p,d),o.set(i,b-1,M[0]),o.set(i,b,M[1]),Math.abs(c)>Math.abs(V)+Math.abs(k)?(o.set(i+1,b-1,(-g-f*o.get(i,b-1)+k*o.get(i,b))/c),o.set(i+1,b,(-w-f*o.get(i,b)-k*o.get(i,b-1))/c)):(M=z(-T-m*o.get(i,b-1),-N-m*o.get(i,b),V,k),o.set(i+1,b-1,M[0]),o.set(i+1,b,M[1]))),a=Math.max(Math.abs(o.get(i,b-1)),Math.abs(o.get(i,b))),S*a*a>1)for(n=i;n<=b;n++)o.set(n,b-1,o.get(n,b-1)/a),o.set(n,b,o.get(n,b)/a)}for(i=0;iE)for(n=i;n=x;n--)for(i=x;i<=E;i++){for(V=0,h=x;h<=Math.min(n,E);h++)V+=s.get(i,h)*o.get(h,n);s.set(i,n,V)}}(i,u,h,n,t)}this.n=i,this.e=u,this.d=h,this.V=n}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,i=new x(r,r);for(t=0;t0?i.set(t,t+1,s[t]):s[t]<0&&i.set(t,t-1,s[t])}return i}}function z(t,e,r,s){let o,i;return Math.abs(r)>Math.abs(s)?(o=s/r,i=r+o*s,[(t+o*e)/i,(e-o*t)/i]):(o=r/s,i=s+o*r,[(o*t+e)/i,(o*e-t)/i])}class ${constructor(t){if(!(t=k.checkMatrix(t)).isSymmetric())throw new Error("Matrix is not symmetric");let e,r,s,o=t,i=o.rows,n=new x(i,i),h=!0;for(r=0;r0,n.set(r,r,Math.sqrt(Math.max(t,0))),s=r+1;s=0;i--)for(o=0;on;e++)a=t.transpose().mmul(h).div(h.transpose().mmul(h).get(0,0)),a=a.div(a.norm()),u=t.mmul(a).div(a.transpose().mmul(a).get(0,0)),e>0&&(c=u.clone().sub(f).pow(2).sum()),f=u.clone(),s?(l=s.transpose().mmul(u).div(u.transpose().mmul(u).get(0,0)),l=l.div(l.norm()),h=s.mmul(l).div(l.transpose().mmul(l).get(0,0))):h=u;if(s){let e=t.transpose().mmul(u).div(u.transpose().mmul(u).get(0,0));e=e.div(e.norm());let r=t.clone().sub(u.clone().mmul(e.transpose())),o=h.transpose().mmul(u).div(u.transpose().mmul(u).get(0,0)),i=s.clone().sub(u.clone().mulS(o.get(0,0)).mmul(l.transpose()));this.t=u,this.p=e.transpose(),this.w=a.transpose(),this.q=l,this.u=h,this.s=u.transpose().mmul(u),this.xResidual=r,this.yResidual=i,this.betas=o}else this.w=a.transpose(),this.s=u.transpose().mmul(u).sqrt(),this.t=o?u.clone().div(this.s.get(0,0)):u,this.xResidual=t.sub(u.mmul(a.transpose()))}}t.AbstractMatrix=M,t.CHO=$,t.CholeskyDecomposition=$,t.DistanceMatrix=S,t.EVD=F,t.EigenvalueDecomposition=F,t.LU=T,t.LuDecomposition=T,t.Matrix=x,t.MatrixColumnSelectionView=class extends v{constructor(t,e){g(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 v{constructor(t,e){a(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 v{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 v{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 v{constructor(t,e){m(t,e),super(t,e.length,t.columns),this.rowIndices=e}set(t,e,r){return this.matrix.set(this.rowIndices[t],e,r),this}get(t,e){return this.matrix.get(this.rowIndices[t],e)}},t.MatrixRowView=class extends v{constructor(t,e){l(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=R,t.MatrixSubView=class extends v{constructor(t,e,r,s,o){w(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 v{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=j,t.Nipals=j,t.QR=V,t.QrDecomposition=V,t.SVD=C,t.SingularValueDecomposition=C,t.SymmetricMatrix=E,t.WrapperMatrix1D=I,t.WrapperMatrix2D=k,t.correlation=function(t,e=t,s={}){t=new x(t);let o=!1;if("object"!=typeof e||x.isMatrix(e)||r(e)?e=new x(e):(s=e,e=t,o=!0),t.rows!==e.rows)throw new TypeError("Both matrices must have the same number of rows");const{center:i=!0,scale:n=!0}=s;i&&(t.center("column"),o||e.center("column")),n&&(t.scale("column"),o||e.scale("column"));const h=t.standardDeviation("column",{unbiased:!0}),u=o?h:e.standardDeviation("column",{unbiased:!0}),l=t.transpose().mmul(e);for(let e=0;ee?i[t]=1/i[t]:i[t]=0;return o.mmul(x.diag(i).mmul(s.transpose()))},t.solve=q,t.wrap=function(t,e){if(r(t))return t[0]&&r(t[0])?new k(t):new I(t,e);throw new Error("the argument is not an array")},Object.defineProperty(t,"__esModule",{value:!0})}));