projection-3d-2d
Version:
Project (transform) point coordinates from 3D to 2D and unproject it back.
1 lines • 80.1 kB
JavaScript
var Projection3d2d;(()=>{var t={497:(t,r,e)=>{function n(t){return(n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function o(t,r){return function(t){if(Array.isArray(t))return t}(t)||function(t,r){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(t)){var e=[],n=!0,o=!1,i=void 0;try{for(var u,s=t[Symbol.iterator]();!(n=(u=s.next()).done)&&(e.push(u.value),!r||e.length!==r);n=!0);}catch(t){o=!0,i=t}finally{try{n||null==s.return||s.return()}finally{if(o)throw i}}return e}}(t,r)||function(t,r){if(t){if("string"==typeof t)return i(t,r);var e=Object.prototype.toString.call(t).slice(8,-1);return"Object"===e&&t.constructor&&(e=t.constructor.name),"Map"===e||"Set"===e?Array.from(t):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?i(t,r):void 0}}(t,r)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function i(t,r){(null==r||r>t.length)&&(r=t.length);for(var e=0,n=new Array(r);e<r;e++)n[e]=t[e];return n}function u(t,r){for(var e=0;e<r.length;e++){var n=r[e];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function s(t,r,e){return r&&u(t.prototype,r),e&&u(t,e),t}function a(t,r){if("function"!=typeof r&&null!==r)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(r&&r.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),r&&c(t,r)}function c(t,r){return(c=Object.setPrototypeOf||function(t,r){return t.__proto__=r,t})(t,r)}function f(t){var r=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}();return function(){var e,n=h(t);if(r){var o=h(this).constructor;e=Reflect.construct(n,arguments,o)}else e=n.apply(this,arguments);return l(this,e)}}function l(t,r){return!r||"object"!==n(r)&&"function"!=typeof r?function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t):r}function h(t){return(h=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function y(t,r){if(!(t instanceof r))throw new TypeError("Cannot call a class as a function")}var p=e(581),v=p.Matrix,m=p.SingularValueDecomposition,b=p.inverse,w=p.solve,g="See example: https://github.com/Infl1ght/projection-3d-2d#example-2d-to-2d",d=function t(r,e){if(y(this,t),!r||!e)throw new Error("Two arrays with points must be provided. "+g);if(r.length!==e.length)throw new Error("Lengths of point arrays must be equal. "+g);this.points3d=r,this.points2d=e},x=function(t){a(e,t);var r=f(e);function e(t,n){var o;if(y(this,e),o=r.call(this,t,n),t.length<6)throw new Error("6 points must be provided for ProjectionCalculator3d. "+g);return o.calculateMatrix(),o}return s(e,[{key:"calculateMatrix",value:function(){for(var t=new v(2*this.points2d.length,12),r=0,e=0;e<this.points3d.length;e+=1){var n=o(this.points3d[e],3);t[r][0]=n[0],t[r][1]=n[1],t[r][2]=n[2],t[r][3]=1,t[r][4]=0,t[r][5]=0,t[r][6]=0,t[r][7]=0,t[r][8]=-this.points3d[e][0]*this.points2d[e][0],t[r][9]=-this.points3d[e][1]*this.points2d[e][0],t[r][10]=-this.points3d[e][2]*this.points2d[e][0],t[r][11]=-this.points2d[e][0],t[r+1][0]=0,t[r+1][1]=0,t[r+1][2]=0,t[r+1][3]=0;var i=o(this.points3d[e],3);t[r+1][4]=i[0],t[r+1][5]=i[1],t[r+1][6]=i[2],t[r+1][7]=1,t[r+1][8]=-this.points3d[e][0]*this.points2d[e][1],t[r+1][9]=-this.points3d[e][1]*this.points2d[e][1],t[r+1][10]=-this.points3d[e][2]*this.points2d[e][1],t[r+1][11]=-this.points2d[e][1],r+=2}var u=new m(t),s=new v(u.V).transpose().subMatrix(11,11,0,11)[0];this.resultMatrix=new v([[s[0],s[1],s[2],s[3]],[s[4],s[5],s[6],s[7]],[s[8],s[9],s[10],s[11]],[0,0,0,1]]),this.resultMatrixInversed=b(this.resultMatrix)}},{key:"getProjectedPoint",value:function(t){var r=v.columnVector([t[0],t[1],t[2],1]),e=this.resultMatrix.mmul(r);return[e[0]/e[2],e[1]/e[2]]}},{key:"getUnprojectedPoint",value:function(t,r){if(void 0===r)throw new Error("Point height must be defined for 3d unprojection");var n=v.columnVector([t[0],t[1],1,1]),o=v.columnVector([100*t[0],100*t[1],100,1]),i=this.resultMatrixInversed.mmul(n),u=this.resultMatrixInversed.mmul(o);return e.getIntersectionLineAndPlane(i,u,[0,0,r],[0,10,r],[10,0,r])}}],[{key:"getIntersectionLineAndPlane",value:function(t,r,e,n,o){var i=(n[1]-e[1])*(o[2]-e[2])-(n[2]-e[2])*(o[1]-e[1]),u=(n[2]-e[2])*(o[0]-e[0])-(n[0]-e[0])*(o[2]-e[2]),s=(n[0]-e[0])*(o[1]-e[1])-(n[1]-e[1])*(o[0]-e[0]),a=i*e[0]+u*e[1]+s*e[2],c=r[0]-t[0],f=r[1]-t[1],l=r[2]-t[2],h=c*t[1]-f*t[0],y=c*t[2]-l*t[0],p=(c*a-h*u-y*s)/(c*i+f*u+l*s);return[p,(f*p+h)/c,(l*p+y)/c]}}]),e}(d),M=function(t){a(e,t);var r=f(e);function e(t,n){var o;if(y(this,e),o=r.call(this,t,n),t.length<4)throw new Error("4 points must be provided for ProjectionCalculator3d. "+g);return o.calculateMatrix(),o}return s(e,[{key:"calculateMatrix",value:function(){for(var t=new v(2*this.points2d.length,8),r=new v(8,1),e=0,n=0;n<this.points3d.length;n+=1){var i=o(this.points3d[n],2);t[e][0]=i[0],t[e][1]=i[1],t[e][2]=1,t[e][3]=0,t[e][4]=0,t[e][5]=0,t[e][6]=-this.points3d[n][0]*this.points2d[n][0],t[e][7]=-this.points3d[n][1]*this.points2d[n][0];var u=o(this.points2d[n],1);r[e][0]=u[0],t[e+1][0]=0,t[e+1][1]=0,t[e+1][2]=0;var s=o(this.points3d[n],2);t[e+1][3]=s[0],t[e+1][4]=s[1],t[e+1][5]=1,t[e+1][6]=-this.points3d[n][0]*this.points2d[n][1],t[e+1][7]=-this.points3d[n][1]*this.points2d[n][1];var a=o(this.points2d[n],2);r[e+1][0]=a[1],e+=2}var c=w(t,r);this.resultMatrix=new v([[c[0][0],c[1][0],c[2][0]],[c[3][0],c[4][0],c[5][0]],[c[6][0],c[7][0],1]]),this.resultMatrixInversed=b(this.resultMatrix)}},{key:"getProjectedPoint",value:function(t){var r=v.columnVector([t[0],t[1],1]),e=this.resultMatrix.mmul(r);return[e[0]/e[2],e[1]/e[2]]}},{key:"getUnprojectedPoint",value:function(t){var r=v.columnVector([t[0],t[1],1]),e=this.resultMatrixInversed.mmul(r);return[e[0]/e[2],e[1]/e[2]]}}]),e}(d);t.exports.ProjectionCalculator3d=x,t.exports.ProjectionCalculator2d=M},581:(t,r,e)=>{"use strict";e.r(r),e.d(r,{CHO:()=>Or,CholeskyDecomposition:()=>Or,EVD:()=>br,EigenvalueDecomposition:()=>br,LU:()=>c,LuDecomposition:()=>c,Matrix:()=>$t,QR:()=>cr,QrDecomposition:()=>cr,SVD:()=>p,SingularValueDecomposition:()=>p,WrapperMatrix1D:()=>tr,WrapperMatrix2D:()=>ur,abstractMatrix:()=>Nt,default:()=>$t,inverse:()=>fr,linearDependencies:()=>pr,solve:()=>lr,wrap:()=>sr});var n=Object.prototype.toString;function o(t){return n.call(t).endsWith("Array]")}const i=function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!o(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");var e=r.fromIndex,n=void 0===e?0:e,i=r.toIndex,u=void 0===i?t.length:i;if(n<0||n>=t.length||!Number.isInteger(n))throw new Error("fromIndex must be a positive integer smaller than length");if(u<=n||u>t.length||!Number.isInteger(u))throw new Error("toIndex must be an integer greater than fromIndex and at most equal to length");for(var s=t[n],a=n+1;a<u;a++)t[a]>s&&(s=t[a]);return s},u=function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!o(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");var e=r.fromIndex,n=void 0===e?0:e,i=r.toIndex,u=void 0===i?t.length:i;if(n<0||n>=t.length||!Number.isInteger(n))throw new Error("fromIndex must be a positive integer smaller than length");if(u<=n||u>t.length||!Number.isInteger(u))throw new Error("toIndex must be an integer greater than fromIndex and at most equal to length");for(var s=t[n],a=n+1;a<u;a++)t[a]<s&&(s=t[a]);return s},s=function(t){var r,e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!o(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");if(void 0!==e.output){if(!o(e.output))throw new TypeError("output option must be an array if specified");r=e.output}else r=new Array(t.length);var n=u(t),s=i(t);if(n===s)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");var a=e.min,c=void 0===a?e.autoMinMax?n:0:a,f=e.max,l=void 0===f?e.autoMinMax?s:1:f;if(c>=l)throw new RangeError("min option must be smaller than max option");for(var h=(l-c)/(s-n),y=0;y<t.length;y++)r[y]=(t[y]-n)*h+c;return r};function a(t,r){for(var e=0;e<r.length;e++){var n=r[e];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}var c=function(){function t(r){!function(t,r){if(!(t instanceof r))throw new TypeError("Cannot call a class as a function")}(this,t);var e,n,o,i,u,s,a,c,f,l=(r=ur.checkMatrix(r)).clone(),h=l.rows,y=l.columns,p=new Array(h),v=1;for(e=0;e<h;e++)p[e]=e;for(c=new Array(h),n=0;n<y;n++){for(e=0;e<h;e++)c[e]=l.get(e,n);for(e=0;e<h;e++){for(f=Math.min(e,n),u=0,o=0;o<f;o++)u+=l.get(e,o)*c[o];c[e]-=u,l.set(e,n,c[e])}for(i=n,e=n+1;e<h;e++)Math.abs(c[e])>Math.abs(c[i])&&(i=e);if(i!==n){for(o=0;o<y;o++)s=l.get(i,o),l.set(i,o,l.get(n,o)),l.set(n,o,s);a=p[i],p[i]=p[n],p[n]=a,v=-v}if(n<h&&0!==l.get(n,n))for(e=n+1;e<h;e++)l.set(e,n,l.get(e,n)/l.get(n,n))}this.LU=l,this.pivotVector=p,this.pivotSign=v}var r,e;return r=t,(e=[{key:"isSingular",value:function(){for(var t=this.LU,r=t.columns,e=0;e<r;e++)if(0===t[e][e])return!0;return!1}},{key:"solve",value:function(t){t=$t.checkMatrix(t);var r=this.LU;if(r.rows!==t.rows)throw new Error("Invalid matrix dimensions");if(this.isSingular())throw new Error("LU matrix is singular");var e,n,o,i=t.columns,u=t.subMatrixRow(this.pivotVector,0,i-1),s=r.columns;for(o=0;o<s;o++)for(e=o+1;e<s;e++)for(n=0;n<i;n++)u[e][n]-=u[o][n]*r[e][o];for(o=s-1;o>=0;o--){for(n=0;n<i;n++)u[o][n]/=r[o][o];for(e=0;e<o;e++)for(n=0;n<i;n++)u[e][n]-=u[o][n]*r[e][o]}return u}},{key:"determinant",get:function(){var t=this.LU;if(!t.isSquare())throw new Error("Matrix must be square");for(var r=this.pivotSign,e=t.columns,n=0;n<e;n++)r*=t[n][n];return r}},{key:"lowerTriangularMatrix",get:function(){for(var t=this.LU,r=t.rows,e=t.columns,n=new $t(r,e),o=0;o<r;o++)for(var i=0;i<e;i++)n[o][i]=o>i?t[o][i]:o===i?1:0;return n}},{key:"upperTriangularMatrix",get:function(){for(var t=this.LU,r=t.rows,e=t.columns,n=new $t(r,e),o=0;o<r;o++)for(var i=0;i<e;i++)n[o][i]=o<=i?t[o][i]:0;return n}},{key:"pivotPermutationVector",get:function(){return this.pivotVector.slice()}}])&&a(r.prototype,e),t}();function f(t,r){var e=0;return Math.abs(t)>Math.abs(r)?(e=r/t,Math.abs(t)*Math.sqrt(1+e*e)):0!==r?(e=t/r,Math.abs(r)*Math.sqrt(1+e*e)):0}function l(t,r,e){for(var n=new Array(t),o=0;o<t;o++){n[o]=new Array(r);for(var i=0;i<r;i++)n[o][i]=e}return n}function h(t,r){if(!(t instanceof r))throw new TypeError("Cannot call a class as a function")}function y(t,r){for(var e=0;e<r.length;e++){var n=r[e];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}var p=function(){function t(r){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};h(this,t);var n,o=(r=ur.checkMatrix(r)).rows,i=r.columns,u=e.computeLeftSingularVectors,s=void 0===u||u,a=e.computeRightSingularVectors,c=void 0===a||a,y=e.autoTranspose,p=void 0!==y&&y,v=Boolean(s),m=Boolean(c),b=!1;if(o<i)if(p){o=(n=r.transpose()).rows,i=n.columns,b=!0;var w=v;v=m,m=w}else n=r.clone(),console.warn("Computing SVD on a matrix with more columns than rows. Consider enabling autoTranspose");else n=r.clone();for(var g=Math.min(o,i),d=Math.min(o+1,i),x=new Array(d),M=l(o,g,0),k=l(i,i,0),O=new Array(i),S=new Array(o),R=new Array(d),j=0;j<d;j++)R[j]=j;for(var E=Math.min(o-1,i),_=Math.max(0,Math.min(i-2,o)),P=Math.max(E,_),V=0;V<P;V++){if(V<E){x[V]=0;for(var A=V;A<o;A++)x[V]=f(x[V],n[A][V]);if(0!==x[V]){n[V][V]<0&&(x[V]=-x[V]);for(var C=V;C<o;C++)n[C][V]/=x[V];n[V][V]+=1}x[V]=-x[V]}for(var I=V+1;I<i;I++){if(V<E&&0!==x[V]){for(var T=0,B=V;B<o;B++)T+=n[B][V]*n[B][I];T=-T/n[V][V];for(var q=V;q<o;q++)n[q][I]+=T*n[q][V]}O[I]=n[V][I]}if(v&&V<E)for(var D=V;D<o;D++)M[D][V]=n[D][V];if(V<_){O[V]=0;for(var N=V+1;N<i;N++)O[V]=f(O[V],O[N]);if(0!==O[V]){O[V+1]<0&&(O[V]=0-O[V]);for(var L=V+1;L<i;L++)O[L]/=O[V];O[V+1]+=1}if(O[V]=-O[V],V+1<o&&0!==O[V]){for(var U=V+1;U<o;U++)S[U]=0;for(var z=V+1;z<o;z++)for(var Q=V+1;Q<i;Q++)S[z]+=O[Q]*n[z][Q];for(var F=V+1;F<i;F++)for(var W=-O[F]/O[V+1],H=V+1;H<o;H++)n[H][F]+=W*S[H]}if(m)for(var $=V+1;$<i;$++)k[$][V]=O[$]}}var G=Math.min(i,o+1);if(E<i&&(x[E]=n[E][E]),o<G&&(x[G-1]=0),_+1<G&&(O[_]=n[_][G-1]),O[G-1]=0,v){for(var J=E;J<g;J++){for(var K=0;K<o;K++)M[K][J]=0;M[J][J]=1}for(var X=E-1;X>=0;X--)if(0!==x[X]){for(var Y=X+1;Y<g;Y++){for(var Z=0,tt=X;tt<o;tt++)Z+=M[tt][X]*M[tt][Y];Z=-Z/M[X][X];for(var rt=X;rt<o;rt++)M[rt][Y]+=Z*M[rt][X]}for(var et=X;et<o;et++)M[et][X]=-M[et][X];M[X][X]=1+M[X][X];for(var nt=0;nt<X-1;nt++)M[nt][X]=0}else{for(var ot=0;ot<o;ot++)M[ot][X]=0;M[X][X]=1}}if(m)for(var it=i-1;it>=0;it--){if(it<_&&0!==O[it])for(var ut=it+1;ut<i;ut++){for(var st=0,at=it+1;at<i;at++)st+=k[at][it]*k[at][ut];st=-st/k[it+1][it];for(var ct=it+1;ct<i;ct++)k[ct][ut]+=st*k[ct][it]}for(var ft=0;ft<i;ft++)k[ft][it]=0;k[it][it]=1}for(var lt=G-1,ht=Number.EPSILON;G>0;){var yt=void 0,pt=void 0;for(yt=G-2;yt>=-1&&-1!==yt;yt--){var vt=Number.MIN_VALUE+ht*Math.abs(x[yt]+Math.abs(x[yt+1]));if(Math.abs(O[yt])<=vt||Number.isNaN(O[yt])){O[yt]=0;break}}if(yt===G-2)pt=4;else{var mt=void 0;for(mt=G-1;mt>=yt&&mt!==yt;mt--){var bt=(mt!==G?Math.abs(O[mt]):0)+(mt!==yt+1?Math.abs(O[mt-1]):0);if(Math.abs(x[mt])<=ht*bt){x[mt]=0;break}}mt===yt?pt=3:mt===G-1?pt=1:(pt=2,yt=mt)}switch(yt++,pt){case 1:var wt=O[G-2];O[G-2]=0;for(var gt=G-2;gt>=yt;gt--){var dt=f(x[gt],wt),xt=x[gt]/dt,Mt=wt/dt;if(x[gt]=dt,gt!==yt&&(wt=-Mt*O[gt-1],O[gt-1]=xt*O[gt-1]),m)for(var kt=0;kt<i;kt++)dt=xt*k[kt][gt]+Mt*k[kt][G-1],k[kt][G-1]=-Mt*k[kt][gt]+xt*k[kt][G-1],k[kt][gt]=dt}break;case 2:var Ot=O[yt-1];O[yt-1]=0;for(var St=yt;St<G;St++){var Rt=f(x[St],Ot),jt=x[St]/Rt,Et=Ot/Rt;if(x[St]=Rt,Ot=-Et*O[St],O[St]=jt*O[St],v)for(var _t=0;_t<o;_t++)Rt=jt*M[_t][St]+Et*M[_t][yt-1],M[_t][yt-1]=-Et*M[_t][St]+jt*M[_t][yt-1],M[_t][St]=Rt}break;case 3:var Pt=Math.max(Math.abs(x[G-1]),Math.abs(x[G-2]),Math.abs(O[G-2]),Math.abs(x[yt]),Math.abs(O[yt])),Vt=x[G-1]/Pt,At=x[G-2]/Pt,Ct=O[G-2]/Pt,It=x[yt]/Pt,Tt=O[yt]/Pt,Bt=((At+Vt)*(At-Vt)+Ct*Ct)/2,qt=Vt*Ct*(Vt*Ct),Dt=0;0===Bt&&0===qt||(Dt=qt/(Bt+(Dt=Bt<0?0-Math.sqrt(Bt*Bt+qt):Math.sqrt(Bt*Bt+qt))));for(var Nt=(It+Vt)*(It-Vt)+Dt,Lt=It*Tt,Ut=yt;Ut<G-1;Ut++){var zt=f(Nt,Lt);0===zt&&(zt=Number.MIN_VALUE);var Qt=Nt/zt,Ft=Lt/zt;if(Ut!==yt&&(O[Ut-1]=zt),Nt=Qt*x[Ut]+Ft*O[Ut],O[Ut]=Qt*O[Ut]-Ft*x[Ut],Lt=Ft*x[Ut+1],x[Ut+1]=Qt*x[Ut+1],m)for(var Wt=0;Wt<i;Wt++)zt=Qt*k[Wt][Ut]+Ft*k[Wt][Ut+1],k[Wt][Ut+1]=-Ft*k[Wt][Ut]+Qt*k[Wt][Ut+1],k[Wt][Ut]=zt;if(0===(zt=f(Nt,Lt))&&(zt=Number.MIN_VALUE),Qt=Nt/zt,Ft=Lt/zt,x[Ut]=zt,Nt=Qt*O[Ut]+Ft*x[Ut+1],x[Ut+1]=-Ft*O[Ut]+Qt*x[Ut+1],Lt=Ft*O[Ut+1],O[Ut+1]=Qt*O[Ut+1],v&&Ut<o-1)for(var Ht=0;Ht<o;Ht++)zt=Qt*M[Ht][Ut]+Ft*M[Ht][Ut+1],M[Ht][Ut+1]=-Ft*M[Ht][Ut]+Qt*M[Ht][Ut+1],M[Ht][Ut]=zt}O[G-2]=Nt;break;case 4:if(x[yt]<=0&&(x[yt]=x[yt]<0?-x[yt]:0,m))for(var $t=0;$t<=lt;$t++)k[$t][yt]=-k[$t][yt];for(;yt<lt&&!(x[yt]>=x[yt+1]);){var Gt=x[yt];if(x[yt]=x[yt+1],x[yt+1]=Gt,m&&yt<i-1)for(var Jt=0;Jt<i;Jt++)Gt=k[Jt][yt+1],k[Jt][yt+1]=k[Jt][yt],k[Jt][yt]=Gt;if(v&&yt<o-1)for(var Kt=0;Kt<o;Kt++)Gt=M[Kt][yt+1],M[Kt][yt+1]=M[Kt][yt],M[Kt][yt]=Gt;yt++}G--}}if(b){var Xt=k;k=M,M=Xt}this.m=o,this.n=i,this.s=x,this.U=M,this.V=k}var r,e;return r=t,(e=[{key:"solve",value:function(t){for(var r=t,e=this.threshold,n=this.s.length,o=$t.zeros(n,n),i=0;i<n;i++)Math.abs(this.s[i])<=e?o[i][i]=0:o[i][i]=1/this.s[i];for(var u=this.U,s=this.rightSingularVectors,a=s.mmul(o),c=s.rows,f=u.length,l=$t.zeros(c,f),h=0;h<c;h++)for(var y=0;y<f;y++){for(var p=0,v=0;v<n;v++)p+=a[h][v]*u[y][v];l[h][y]=p}return l.mmul(r)}},{key:"solveForDiagonal",value:function(t){return this.solve($t.diag(t))}},{key:"inverse",value:function(){for(var t=this.V,r=this.threshold,e=t.length,n=t[0].length,o=new $t(e,this.s.length),i=0;i<e;i++)for(var u=0;u<n;u++)Math.abs(this.s[u])>r?o[i][u]=t[i][u]/this.s[u]:o[i][u]=0;for(var s=this.U,a=s.length,c=s[0].length,f=new $t(e,a),l=0;l<e;l++)for(var h=0;h<a;h++){for(var y=0,p=0;p<c;p++)y+=o[l][p]*s[h][p];f[l][h]=y}return f}},{key:"condition",get:function(){return this.s[0]/this.s[Math.min(this.m,this.n)-1]}},{key:"norm2",get:function(){return this.s[0]}},{key:"rank",get:function(){for(var t=Math.max(this.m,this.n)*this.s[0]*Number.EPSILON,r=0,e=this.s,n=0,o=e.length;n<o;n++)e[n]>t&&r++;return r}},{key:"diagonal",get:function(){return this.s}},{key:"threshold",get:function(){return Number.EPSILON/2*Math.max(this.m,this.n)*this.s[0]}},{key:"leftSingularVectors",get:function(){return $t.isMatrix(this.U)||(this.U=new $t(this.U)),this.U}},{key:"rightSingularVectors",get:function(){return $t.isMatrix(this.V)||(this.V=new $t(this.V)),this.V}},{key:"diagonalMatrix",get:function(){return $t.diag(this.s)}}])&&y(r.prototype,e),t}();function v(t){return(v="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function m(t,r,e){var n=e?t.rows:t.rows-1;if(r<0||r>n)throw new RangeError("Row index out of range")}function b(t,r,e){var n=e?t.columns:t.columns-1;if(r<0||r>n)throw new RangeError("Column index out of range")}function w(t,r){if(r.to1DArray&&(r=r.to1DArray()),r.length!==t.columns)throw new RangeError("vector size must be the same as the number of columns");return r}function g(t,r){if(r.to1DArray&&(r=r.to1DArray()),r.length!==t.rows)throw new RangeError("vector size must be the same as the number of rows");return r}function d(t,r,e){return{row:x(t,r),column:M(t,e)}}function x(t,r){if("object"!==v(r))throw new TypeError("unexpected type for row indices");if(r.some((function(r){return r<0||r>=t.rows})))throw new RangeError("row indices are out of range");return Array.isArray(r)||(r=Array.from(r)),r}function M(t,r){if("object"!==v(r))throw new TypeError("unexpected type for column indices");if(r.some((function(r){return r<0||r>=t.columns})))throw new RangeError("column indices are out of range");return Array.isArray(r)||(r=Array.from(r)),r}function k(t,r,e,n,o){if(5!==arguments.length)throw new RangeError("expected 4 arguments");if(R("startRow",r),R("endRow",e),R("startColumn",n),R("endColumn",o),r>e||n>o||r<0||r>=t.rows||e<0||e>=t.rows||n<0||n>=t.columns||o<0||o>=t.columns)throw new RangeError("Submatrix indices are out of range")}function O(t){for(var r=$t.zeros(t.rows,1),e=0;e<t.rows;++e)for(var n=0;n<t.columns;++n)r.set(e,0,r.get(e,0)+t.get(e,n));return r}function S(t){for(var r=$t.zeros(1,t.columns),e=0;e<t.rows;++e)for(var n=0;n<t.columns;++n)r.set(0,n,r.get(0,n)+t.get(e,n));return r}function R(t,r){if("number"!=typeof r)throw new TypeError("".concat(t," must be a number"))}function j(t){return(j="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function E(t,r){for(var e=0;e<r.length;e++){var n=r[e];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function _(t,r){return(_=Object.setPrototypeOf||function(t,r){return t.__proto__=r,t})(t,r)}function P(t,r){return!r||"object"!==j(r)&&"function"!=typeof r?function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t):r}function V(t){return(V=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}var A=function(t){!function(t,r){if("function"!=typeof r&&null!==r)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(r&&r.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),r&&_(t,r)}(u,t);var r,e,n,o,i=(n=u,o=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}(),function(){var t,r=V(n);if(o){var e=V(this).constructor;t=Reflect.construct(r,arguments,e)}else t=r.apply(this,arguments);return P(this,t)});function u(t,r,e){var n;return function(t,r){if(!(t instanceof r))throw new TypeError("Cannot call a class as a function")}(this,u),(n=i.call(this)).matrix=t,n.rows=r,n.columns=e,n}return r=u,null,(e=[{key:Symbol.species,get:function(){return $t}}])&&E(r,e),u}(Nt());function C(t){return(C="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function I(t,r){for(var e=0;e<r.length;e++){var n=r[e];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function T(t,r){return(T=Object.setPrototypeOf||function(t,r){return t.__proto__=r,t})(t,r)}function B(t,r){return!r||"object"!==C(r)&&"function"!=typeof r?function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t):r}function q(t){return(q=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}var D=function(t){!function(t,r){if("function"!=typeof r&&null!==r)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(r&&r.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),r&&T(t,r)}(u,t);var r,e,n,o,i=(n=u,o=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}(),function(){var t,r=q(n);if(o){var e=q(this).constructor;t=Reflect.construct(r,arguments,e)}else t=r.apply(this,arguments);return B(this,t)});function u(t){return function(t,r){if(!(t instanceof r))throw new TypeError("Cannot call a class as a function")}(this,u),i.call(this,t,t.columns,t.rows)}return r=u,(e=[{key:"set",value:function(t,r,e){return this.matrix.set(r,t,e),this}},{key:"get",value:function(t,r){return this.matrix.get(r,t)}}])&&I(r.prototype,e),u}(A);function N(t){return(N="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function L(t,r){for(var e=0;e<r.length;e++){var n=r[e];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function U(t,r){return(U=Object.setPrototypeOf||function(t,r){return t.__proto__=r,t})(t,r)}function z(t,r){return!r||"object"!==N(r)&&"function"!=typeof r?function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t):r}function Q(t){return(Q=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}var F=function(t){!function(t,r){if("function"!=typeof r&&null!==r)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(r&&r.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),r&&U(t,r)}(u,t);var r,e,n,o,i=(n=u,o=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}(),function(){var t,r=Q(n);if(o){var e=Q(this).constructor;t=Reflect.construct(r,arguments,e)}else t=r.apply(this,arguments);return z(this,t)});function u(t,r){var e;return function(t,r){if(!(t instanceof r))throw new TypeError("Cannot call a class as a function")}(this,u),(e=i.call(this,t,1,t.columns)).row=r,e}return r=u,(e=[{key:"set",value:function(t,r,e){return this.matrix.set(this.row,r,e),this}},{key:"get",value:function(t,r){return this.matrix.get(this.row,r)}}])&&L(r.prototype,e),u}(A);function W(t){return(W="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function H(t,r){for(var e=0;e<r.length;e++){var n=r[e];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function $(t,r){return($=Object.setPrototypeOf||function(t,r){return t.__proto__=r,t})(t,r)}function G(t,r){return!r||"object"!==W(r)&&"function"!=typeof r?function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t):r}function J(t){return(J=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}var K=function(t){!function(t,r){if("function"!=typeof r&&null!==r)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(r&&r.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),r&&$(t,r)}(u,t);var r,e,n,o,i=(n=u,o=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}(),function(){var t,r=J(n);if(o){var e=J(this).constructor;t=Reflect.construct(r,arguments,e)}else t=r.apply(this,arguments);return G(this,t)});function u(t,r,e,n,o){var s;return function(t,r){if(!(t instanceof r))throw new TypeError("Cannot call a class as a function")}(this,u),k(t,r,e,n,o),(s=i.call(this,t,e-r+1,o-n+1)).startRow=r,s.startColumn=n,s}return r=u,(e=[{key:"set",value:function(t,r,e){return this.matrix.set(this.startRow+t,this.startColumn+r,e),this}},{key:"get",value:function(t,r){return this.matrix.get(this.startRow+t,this.startColumn+r)}}])&&H(r.prototype,e),u}(A);function X(t){return(X="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function Y(t,r){for(var e=0;e<r.length;e++){var n=r[e];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function Z(t,r){return(Z=Object.setPrototypeOf||function(t,r){return t.__proto__=r,t})(t,r)}function tt(t,r){return!r||"object"!==X(r)&&"function"!=typeof r?function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t):r}function rt(t){return(rt=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}var et=function(t){!function(t,r){if("function"!=typeof r&&null!==r)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(r&&r.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),r&&Z(t,r)}(u,t);var r,e,n,o,i=(n=u,o=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}(),function(){var t,r=rt(n);if(o){var e=rt(this).constructor;t=Reflect.construct(r,arguments,e)}else t=r.apply(this,arguments);return tt(this,t)});function u(t,r,e){var n;!function(t,r){if(!(t instanceof r))throw new TypeError("Cannot call a class as a function")}(this,u);var o=d(t,r,e);return(n=i.call(this,t,o.row.length,o.column.length)).rowIndices=o.row,n.columnIndices=o.column,n}return r=u,(e=[{key:"set",value:function(t,r,e){return this.matrix.set(this.rowIndices[t],this.columnIndices[r],e),this}},{key:"get",value:function(t,r){return this.matrix.get(this.rowIndices[t],this.columnIndices[r])}}])&&Y(r.prototype,e),u}(A);function nt(t){return(nt="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function ot(t,r){for(var e=0;e<r.length;e++){var n=r[e];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function it(t,r){return(it=Object.setPrototypeOf||function(t,r){return t.__proto__=r,t})(t,r)}function ut(t,r){return!r||"object"!==nt(r)&&"function"!=typeof r?function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t):r}function st(t){return(st=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}var at=function(t){!function(t,r){if("function"!=typeof r&&null!==r)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(r&&r.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),r&&it(t,r)}(u,t);var r,e,n,o,i=(n=u,o=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}(),function(){var t,r=st(n);if(o){var e=st(this).constructor;t=Reflect.construct(r,arguments,e)}else t=r.apply(this,arguments);return ut(this,t)});function u(t,r){var e;return function(t,r){if(!(t instanceof r))throw new TypeError("Cannot call a class as a function")}(this,u),r=x(t,r),(e=i.call(this,t,r.length,t.columns)).rowIndices=r,e}return r=u,(e=[{key:"set",value:function(t,r,e){return this.matrix.set(this.rowIndices[t],r,e),this}},{key:"get",value:function(t,r){return this.matrix.get(this.rowIndices[t],r)}}])&&ot(r.prototype,e),u}(A);function ct(t){return(ct="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function ft(t,r){for(var e=0;e<r.length;e++){var n=r[e];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function lt(t,r){return(lt=Object.setPrototypeOf||function(t,r){return t.__proto__=r,t})(t,r)}function ht(t,r){return!r||"object"!==ct(r)&&"function"!=typeof r?function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t):r}function yt(t){return(yt=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}var pt=function(t){!function(t,r){if("function"!=typeof r&&null!==r)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(r&&r.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),r&<(t,r)}(u,t);var r,e,n,o,i=(n=u,o=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}(),function(){var t,r=yt(n);if(o){var e=yt(this).constructor;t=Reflect.construct(r,arguments,e)}else t=r.apply(this,arguments);return ht(this,t)});function u(t,r){var e;return function(t,r){if(!(t instanceof r))throw new TypeError("Cannot call a class as a function")}(this,u),r=M(t,r),(e=i.call(this,t,t.rows,r.length)).columnIndices=r,e}return r=u,(e=[{key:"set",value:function(t,r,e){return this.matrix.set(t,this.columnIndices[r],e),this}},{key:"get",value:function(t,r){return this.matrix.get(t,this.columnIndices[r])}}])&&ft(r.prototype,e),u}(A);function vt(t){return(vt="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function mt(t,r){for(var e=0;e<r.length;e++){var n=r[e];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function bt(t,r){return(bt=Object.setPrototypeOf||function(t,r){return t.__proto__=r,t})(t,r)}function wt(t,r){return!r||"object"!==vt(r)&&"function"!=typeof r?function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t):r}function gt(t){return(gt=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}var dt=function(t){!function(t,r){if("function"!=typeof r&&null!==r)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(r&&r.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),r&&bt(t,r)}(u,t);var r,e,n,o,i=(n=u,o=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}(),function(){var t,r=gt(n);if(o){var e=gt(this).constructor;t=Reflect.construct(r,arguments,e)}else t=r.apply(this,arguments);return wt(this,t)});function u(t,r){var e;return function(t,r){if(!(t instanceof r))throw new TypeError("Cannot call a class as a function")}(this,u),(e=i.call(this,t,t.rows,1)).column=r,e}return r=u,(e=[{key:"set",value:function(t,r,e){return this.matrix.set(t,this.column,e),this}},{key:"get",value:function(t){return this.matrix.get(t,this.column)}}])&&mt(r.prototype,e),u}(A);function xt(t){return(xt="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function Mt(t,r){for(var e=0;e<r.length;e++){var n=r[e];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function kt(t,r){return(kt=Object.setPrototypeOf||function(t,r){return t.__proto__=r,t})(t,r)}function Ot(t,r){return!r||"object"!==xt(r)&&"function"!=typeof r?function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t):r}function St(t){return(St=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}var Rt=function(t){!function(t,r){if("function"!=typeof r&&null!==r)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(r&&r.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),r&&kt(t,r)}(u,t);var r,e,n,o,i=(n=u,o=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}(),function(){var t,r=St(n);if(o){var e=St(this).constructor;t=Reflect.construct(r,arguments,e)}else t=r.apply(this,arguments);return Ot(this,t)});function u(t){return function(t,r){if(!(t instanceof r))throw new TypeError("Cannot call a class as a function")}(this,u),i.call(this,t,t.rows,t.columns)}return r=u,(e=[{key:"set",value:function(t,r,e){return this.matrix.set(this.rows-t-1,r,e),this}},{key:"get",value:function(t,r){return this.matrix.get(this.rows-t-1,r)}}])&&Mt(r.prototype,e),u}(A);function jt(t){return(jt="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function Et(t,r){for(var e=0;e<r.length;e++){var n=r[e];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function _t(t,r){return(_t=Object.setPrototypeOf||function(t,r){return t.__proto__=r,t})(t,r)}function Pt(t,r){return!r||"object"!==jt(r)&&"function"!=typeof r?function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t):r}function Vt(t){return(Vt=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}var At=function(t){!function(t,r){if("function"!=typeof r&&null!==r)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(r&&r.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),r&&_t(t,r)}(u,t);var r,e,n,o,i=(n=u,o=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}(),function(){var t,r=Vt(n);if(o){var e=Vt(this).constructor;t=Reflect.construct(r,arguments,e)}else t=r.apply(this,arguments);return Pt(this,t)});function u(t){return function(t,r){if(!(t instanceof r))throw new TypeError("Cannot call a class as a function")}(this,u),i.call(this,t,t.rows,t.columns)}return r=u,(e=[{key:"set",value:function(t,r,e){return this.matrix.set(t,this.columns-r-1,e),this}},{key:"get",value:function(t,r){return this.matrix.get(t,this.columns-r-1)}}])&&Et(r.prototype,e),u}(A);function Ct(t){return(Ct="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function It(t,r){if(!(t instanceof r))throw new TypeError("Cannot call a class as a function")}function Tt(t,r){for(var e=0;e<r.length;e++){var n=r[e];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function Bt(t,r){return(Bt=Object.setPrototypeOf||function(t,r){return t.__proto__=r,t})(t,r)}function qt(t,r){return!r||"object"!==Ct(r)&&"function"!=typeof r?function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t):r}function Dt(t){return(Dt=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function Nt(t){void 0===t&&(t=Object);var r=function(t){!function(t,r){if("function"!=typeof r&&null!==r)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(r&&r.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),r&&Bt(t,r)}(f,t);var r,n,o,i,u,a=(i=f,u=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}(),function(){var t,r=Dt(i);if(u){var e=Dt(this).constructor;t=Reflect.construct(r,arguments,e)}else t=r.apply(this,arguments);return qt(this,t)});function f(){return It(this,f),a.apply(this,arguments)}return r=f,n=[{key:"size",get:function(){return this.rows*this.columns}},{key:"apply",value:function(t){if("function"!=typeof t)throw new TypeError("callback must be a function");for(var r=this.rows,e=this.columns,n=0;n<r;n++)for(var o=0;o<e;o++)t.call(this,n,o);return this}},{key:"to1DArray",value:function(){for(var t=new Array(this.size),r=0;r<this.rows;r++)for(var e=0;e<this.columns;e++)t[r*this.columns+e]=this.get(r,e);return t}},{key:"to2DArray",value:function(){for(var t=new Array(this.rows),r=0;r<this.rows;r++){t[r]=new Array(this.columns);for(var e=0;e<this.columns;e++)t[r][e]=this.get(r,e)}return t}},{key:"isRowVector",value:function(){return 1===this.rows}},{key:"isColumnVector",value:function(){return 1===this.columns}},{key:"isVector",value:function(){return 1===this.rows||1===this.columns}},{key:"isSquare",value:function(){return this.rows===this.columns}},{key:"isSymmetric",value:function(){if(this.isSquare()){for(var t=0;t<this.rows;t++)for(var r=0;r<=t;r++)if(this.get(t,r)!==this.get(r,t))return!1;return!0}return!1}},{key:"set",value:function(t,r,e){throw new Error("set method is unimplemented")}},{key:"get",value:function(t,r){throw new Error("get method is unimplemented")}},{key:"repeat",value:function(t,r){t=t||1,r=r||1;for(var e=new this.constructor[Symbol.species](this.rows*t,this.columns*r),n=0;n<t;n++)for(var o=0;o<r;o++)e.setSubMatrix(this,this.rows*n,this.columns*o);return e}},{key:"fill",value:function(t){for(var r=0;r<this.rows;r++)for(var e=0;e<this.columns;e++)this.set(r,e,t);return this}},{key:"neg",value:function(){return this.mulS(-1)}},{key:"getRow",value:function(t){m(this,t);for(var r=new Array(this.columns),e=0;e<this.columns;e++)r[e]=this.get(t,e);return r}},{key:"getRowVector",value:function(t){return this.constructor.rowVector(this.getRow(t))}},{key:"setRow",value:function(t,r){m(this,t),r=w(this,r);for(var e=0;e<this.columns;e++)this.set(t,e,r[e]);return this}},{key:"swapRows",value:function(t,r){m(this,t),m(this,r);for(var e=0;e<this.columns;e++){var n=this.get(t,e);this.set(t,e,this.get(r,e)),this.set(r,e,n)}return this}},{key:"getColumn",value:function(t){b(this,t);for(var r=new Array(this.rows),e=0;e<this.rows;e++)r[e]=this.get(e,t);return r}},{key:"getColumnVector",value:function(t){return this.constructor.columnVector(this.getColumn(t))}},{key:"setColumn",value:function(t,r){b(this,t),r=g(this,r);for(var e=0;e<this.rows;e++)this.set(e,t,r[e]);return this}},{key:"swapColumns",value:function(t,r){b(this,t),b(this,r);for(var e=0;e<this.rows;e++){var n=this.get(e,t);this.set(e,t,this.get(e,r)),this.set(e,r,n)}return this}},{key:"addRowVector",value:function(t){t=w(this,t);for(var r=0;r<this.rows;r++)for(var e=0;e<this.columns;e++)this.set(r,e,this.get(r,e)+t[e]);return this}},{key:"subRowVector",value:function(t){t=w(this,t);for(var r=0;r<this.rows;r++)for(var e=0;e<this.columns;e++)this.set(r,e,this.get(r,e)-t[e]);return this}},{key:"mulRowVector",value:function(t){t=w(this,t);for(var r=0;r<this.rows;r++)for(var e=0;e<this.columns;e++)this.set(r,e,this.get(r,e)*t[e]);return this}},{key:"divRowVector",value:function(t){t=w(this,t);for(var r=0;r<this.rows;r++)for(var e=0;e<this.columns;e++)this.set(r,e,this.get(r,e)/t[e]);return this}},{key:"addColumnVector",value:function(t){t=g(this,t);for(var r=0;r<this.rows;r++)for(var e=0;e<this.columns;e++)this.set(r,e,this.get(r,e)+t[r]);return this}},{key:"subColumnVector",value:function(t){t=g(this,t);for(var r=0;r<this.rows;r++)for(var e=0;e<this.columns;e++)this.set(r,e,this.get(r,e)-t[r]);return this}},{key:"mulColumnVector",value:function(t){t=g(this,t);for(var r=0;r<this.rows;r++)for(var e=0;e<this.columns;e++)this.set(r,e,this.get(r,e)*t[r]);return this}},{key:"divColumnVector",value:function(t){t=g(this,t);for(var r=0;r<this.rows;r++)for(var e=0;e<this.columns;e++)this.set(r,e,this.get(r,e)/t[r]);return this}},{key:"mulRow",value:function(t,r){m(this,t);for(var e=0;e<this.columns;e++)this.set(t,e,this.get(t,e)*r);return this}},{key:"mulColumn",value:function(t,r){b(this,t);for(var e=0;e<this.rows;e++)this.set(e,t,this.get(e,t)*r);return this}},{key:"max",value:function(){for(var t=this.get(0,0),r=0;r<this.rows;r++)for(var e=0;e<this.columns;e++)this.get(r,e)>t&&(t=this.get(r,e));return t}},{key:"maxIndex",value:function(){for(var t=this.get(0,0),r=[0,0],e=0;e<this.rows;e++)for(var n=0;n<this.columns;n++)this.get(e,n)>t&&(t=this.get(e,n),r[0]=e,r[1]=n);return r}},{key:"min",value:function(){for(var t=this.get(0,0),r=0;r<this.rows;r++)for(var e=0;e<this.columns;e++)this.get(r,e)<t&&(t=this.get(r,e));return t}},{key:"minIndex",value:function(){for(var t=this.get(0,0),r=[0,0],e=0;e<this.rows;e++)for(var n=0;n<this.columns;n++)this.get(e,n)<t&&(t=this.get(e,n),r[0]=e,r[1]=n);return r}},{key:"maxRow",value:function(t){m(this,t);for(var r=this.get(t,0),e=1;e<this.columns;e++)this.get(t,e)>r&&(r=this.get(t,e));return r}},{key:"maxRowIndex",value:function(t){m(this,t);for(var r=this.get(t,0),e=[t,0],n=1;n<this.columns;n++)this.get(t,n)>r&&(r=this.get(t,n),e[1]=n);return e}},{key:"minRow",value:function(t){m(this,t);for(var r=this.get(t,0),e=1;e<this.columns;e++)this.get(t,e)<r&&(r=this.get(t,e));return r}},{key:"minRowIndex",value:function(t){m(this,t);for(var r=this.get(t,0),e=[t,0],n=1;n<this.columns;n++)this.get(t,n)<r&&(r=this.get(t,n),e[1]=n);return e}},{key:"maxColumn",value:function(t){b(this,t);for(var r=this.get(0,t),e=1;e<this.rows;e++)this.get(e,t)>r&&(r=this.get(e,t));return r}},{key:"maxColumnIndex",value:function(t){b(this,t);for(var r=this.get(0,t),e=[0,t],n=1;n<this.rows;n++)this.get(n,t)>r&&(r=this.get(n,t),e[0]=n);return e}},{key:"minColumn",value:function(t){b(this,t);for(var r=this.get(0,t),e=1;e<this.rows;e++)this.get(e,t)<r&&(r=this.get(e,t));return r}},{key:"minColumnIndex",value:function(t){b(this,t);for(var r=this.get(0,t),e=[0,t],n=1;n<this.rows;n++)this.get(n,t)<r&&(r=this.get(n,t),e[0]=n);return e}},{key:"diag",value:function(){for(var t=Math.min(this.rows,this.columns),r=new Array(t),e=0;e<t;e++)r[e]=this.get(e,e);return r}},{key:"sum",value:function(t){switch(t){case"row":return O(this);case"column":return S(this);default:return function(t){for(var r=0,e=0;e<t.rows;e++)for(var n=0;n<t.columns;n++)r+=t.get(e,n);return r}(this)}}},{key:"mean",value:function(){return this.sum()/this.size}},{key:"prod",value:function(){for(var t=1,r=0;r<this.rows;r++)for(var e=0;e<this.columns;e++)t*=this.get(r,e);return t}},{key:"norm",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"frobenius",r=0;if("max"===t)return this.max();if("frobenius"===t){for(var e=0;e<this.rows;e++)for(var n=0;n<this.columns;n++)r+=this.get(e,n)*this.get(e,n);return Math.sqrt(r)}throw new RangeError("unknown norm type: ".concat(t))}},{key:"cumulativeSum",value:function(){for(var t=0,r=0;r<this.rows;r++)for(var e=0;e<this.columns;e++)t+=this.get(r,e),this.set(r,e,t);return this}},{key:"dot",value:function(t){f.isMatrix(t)&&(t=t.to1DArray());var r=this.to1DArray();if(r.length!==t.length)throw new RangeError("vectors do not have the same size");for(var e=0,n=0;n<r.length;n++)e+=r[n]*t[n];return e}},{key:"mmul",value:function(t){t=this.constructor.checkMatrix(t),this.columns!==t.rows&&console.warn("Number of columns of left matrix are not equal to number of rows of right matrix.");for(var r=this.rows,e=this.columns,n=t.columns,o=new this.constructor[Symbol.species](r,n),i=new Array(e),u=0;u<n;u++){for(var s=0;s<e;s++)i[s]=t.get(s,u);for(var a=0;a<r;a++){var c=0;for(s=0;s<e;s++)c+=this.get(a,s)*i[s];o.set(a,u,c)}}return o}},{key:"strassen2x2",value:function(t){var r=new this.constructor[Symbol.species](2,2),e=this.get(0,0),n=t.get(0,0),o=this.get(0,1),i=t.get(0,1),u=this.get(1,0),s=t.get(1,0),a=this.get(1,1),c=t.get(1,1),f=(e+a)*(n+c),l=(u+a)*n,h=e*(i-c),y=a*(s-n),p=(e+o)*c,v=f+y-p+(o-a)*(s+c),m=h+p,b=l+y,w=f-l+h+(u-e)*(n+i);return r.set(0,0,v),r.set(0,1,m),r.set(1,0,b),r.set(1,1,w),r}},{key:"strassen3x3",value:function(t){var r=new this.constructor[Symbol.species](3,3),e=this.get(0,0),n=this.get(0,1),o=this.get(0,2),i=this.get(1,0),u=this.get(1,1),s=this.get(1,2),a=this.get(2,0),c=this.get(2,1),f=this.get(2,2),l=t.get(0,0),h=t.get(0,1),y=t.get(0,2),p=t.get(1,0),v=t.get(1,1),m=t.get(1,2),b=t.get(2,0),w=t.get(2,1),g=t.get(2,2),d=(e-i)*(-h+v),x=(-e+i+u)*(l-h+v),M=(i+u)*(-l+h),k=e*l,O=(-e+a+c)*(l-y+m),S=(-e+a)*(y-m),R=(a+c)*(-l+y),j=(-o+c+f)*(v+b-w),E=(o-f)*(v-w),_=o*b,P=(c+f)*(-b+w),V=(-o+u+s)*(m+b-g),A=(o-s)*(m-g),C=(u+s)*(-b+g),I=k+_+n*p,T=(e+n+o-i-u-c-f)*v+x+M+k+j+_+P,B=k+O+R+(e+n+o-u-s-a-c)*m+_+V+C,q=d+u*(-l+h+p-v-m-b+g)+x+k+_+V+A,D=d+x+M+k+s*w,N=_+V+A+C+i*y,L=k+O+S+c*(-l+y+p-v-m-b+w)+j+E+_,U=j+E+_+P+a*h,z=k+O+S+R+f*g;return r.set(0,0,I),r.set(0,1,T),r.set(0,2,B),r.set(1,0,q),r.set(1,1,D),r.set(1,2,N),r.set(2,0,L),r.set(2,1,U),r.set(2,2,z),r}},{key:"mmulStrassen",value:function(t){var r=this.clone(),e=r.rows,n=r.columns,o=t.rows,i=t.columns;function u(t,r,e){var n=t.rows,o=t.columns;if(n===r&&o===e)return t;var i=f.zeros(r,e);return i.setSubMatrix(t,0,0)}n!==o&&console.warn("Multiplying ".concat(e," x ").concat(n," and ").concat(o," x ").concat(i," matrix: dimensions do not match."));var s=Math.max(e,o),a=Math.max(n,i);return function t(r,e,n,o){if(n<=512||o<=512)return r.mmul(e);n%2==1&&o%2==1?(r=u(r,n+1,o+1),e=u(e,n+1,o+1)):n%2==1?(r=u(r,n+1,o),e=u(e,n+1,o)):o%2==1&&(r=u(r,n,o+1),e=u(e,n,o+1));var i=parseInt(r.rows/2,10),s=parseInt(r.columns/2,10),a=r.subMatrix(0,i-1,0,s-1),c=e.subMatrix(0,i-1,0,s-1),l=r.subMatrix(0,i-1,s,r.columns-1),h=e.subMatrix(0,i-1,s,e.columns-1),y=r.subMatrix(i,r.rows-1,0,s-1),p=e.subMatrix(i,e.rows-1,0,s-1),v=r.subMatrix(i,r.rows-1,s,r.columns-1),m=e.subMatrix(i,e.rows-1,s,e.columns-1),b=t(f.add(a,v),f.add(c,m),i,s),w=t(f.add(y,v),c,i,s),g=t(a,f.sub(h,m),i,s),d=t(v,f.sub(p,c),i,s),x=t(f.add(a,l),m,i,s),M=t(f.sub(y,a),f.add(c,h),i,s),k=t(f.sub(l,v),f.add(p,m),i,s),O=f.add(b,d);O.sub(x),O.add(k);var S=f.add(g,x),R=f.add(w,d),j=f.sub(b,w);j.add(g),j.add(M);var E=f.zeros(2*O.rows,2*O.columns);return(E=(E=(E=(E=E.setSubMatrix(O,0,0)).setSubMatrix(S,O.rows,0)).setSubMatrix(R,0,O.columns)).setSubMatrix(j,O.rows,O.columns)).subMatrix(0,n-1,0,o-1)}(r=u(r,s,a),t=u(t,s,a),s,a)}},{key:"scaleRows",value:function(t,r){if((t=void 0===t?0:t)>=(r=void 0===r?1:r))throw new RangeError("min should be strictly smaller than max");for(var e=this.constructor.empty(this.rows,this.columns),n=0;n<this.rows;n++){var o=s(this.getRow(n),{min:t,max:r});e.setRow(n,o)}return e}},{key:"scaleColumn