-
Notifications
You must be signed in to change notification settings - Fork 2
/
luqr.min.js
2 lines (2 loc) · 5.17 KB
/
luqr.min.js
1
2
/* LU, LDL, and QR Matrix Decomposer and Solver. v0.3.0 (c) 2015 Bill Dwyer. MIT License. Build 14D11DBCD20 */
!function(r){var n,t,u,o,f,e,l,i,c,a,p,s,h,v,g,d;return o=function(r){var n,u,o,f,e,l,i,c,p,s,h,v,g,d,y,L,m,Q,R,b,U,q,w,A,D,S,k;if(!a(r))return null;for(c=r.length,n=function(){U=[];for(var r=0;c>=0?c>r:r>c;c>=0?r++:r--)U.push(r);return U}.apply(this).map(function(){return t(r[0],function(){return 0})}),u=function(){q=[];for(var r=0;c>=0?c>r:r>c;c>=0?r++:r--)q.push(r);return q}.apply(this).map(function(){return t(r[0],function(){return 0})}),l=s=0,v=c;v>=0?v>s:s>v;l=v>=0?++s:--s)n[l][l]=1;for(l=h=0,g=c;g>=0?g>h:h>g;l=g>=0?++h:--h)u[0][l]=r[0][l];for(e=A=1,d=c;d>=1?d>A:A>d;e=d>=1?++A:--A)for(l=D=0,y=c;y>=0?y>D:D>y;l=y>=0?++D:--D){for(i=S=0,L=e;L>=0?L>S:S>L;i=L>=0?++S:--S){for(w=r[e][i],p=k=0,m=i;m>=0?m>k:k>m;p=m>=0?++k:--k)w-=n[e][p]*u[p][i];n[e][i]=w/u[i][i]}for(i=o=Q=e,R=c;R>=Q?R>o:o>R;i=R>=Q?++o:--o){for(w=r[e][i],p=f=0,b=e;b>=0?b>f:f>b;p=b>=0?++f:--f)w-=n[e][p]*u[p][i];u[e][i]=w}}return{L:n,U:u}},u=function(r){var n,u,o,f,e,l,i,c,s,h,v,g,d,y,L,m,Q;if(!a(r)||!p(r))return null;for(i=r.length,n=function(){m=[];for(var r=0;i>=0?i>r:r>i;i>=0?r++:r--)m.push(r);return m}.apply(this).map(function(){return t(r[0],function(){return 0})}),o=t(r[0],function(){return 0}),e=c=0,v=i;v>=0?v>c:c>v;e=v>=0?++c:--c){for(n[e][e]=1,u=r[e][e],l=s=0,g=e;g>=0?g>s:s>g;l=g>=0?++s:--s)u-=o[l]*n[e][l]*n[e][l];if(o[e]=u,0===o[e])return null;for(f=h=d=e+1,y=i;y>=d?y>h:h>y;f=y>=d?++h:--h){for(n[e][f]=0,u=r[f][e],l=Q=0,L=e;L>=0?L>Q:Q>L;l=L>=0?++Q:--Q)u-=o[l]*n[f][l]*n[e][l];n[f][e]=u/o[e]}}return{L:n,d:o}},e=function(r){var n,u,o,f,e,l,i,c,a,p,s,h,v,g,d,y,L,m,Q,R,b,U,q,w,A,D,S,k;for(n=t(r,function(r){return t(r)}),l=r.length,i=r[0].length,u=function(){q=[];for(var r=0;l>=0?l>r:r>l;l>=0?r++:r--)q.push(r);return q}.apply(this).map(function(){return 0}),w=!1,e=a=0,h=i;h>a;e=a+=1){for(A=0,o=p=v=e,d=l;d>p;o=p+=1)A+=n[o][e]*n[o][e];if(c=Math.sqrt(A),0!==c){for(n[e][e]<0&&(c*=-1),o=s=y=e,L=l;L>s;o=s+=1)n[o][e]/=c;for(n[e][e]+=1,f=D=m=e+1,Q=i;Q>D;f=D+=1){for(A=0,o=S=R=e,b=l;b>S;o=S+=1)A+=n[o][e]*n[o][f];for(A=-A/n[e][e],o=k=U=e,g=l;g>k;o=k+=1)n[o][f]+=A*n[o][e]}u[e]=-c}else u[e]=0,w=!0}return{QR:n,d:u,singular:w}},f=function(r){var n,t,u,o,f,l,i,c,a,p,h,v,g,d,y,L,m,Q,R,b,U,q,w,A,D,S,k,M,P,j;for(d=e(r),t=d.QR,o=d.d,k=d.singular,a=r.length,p=r[0].length,u=s(a,p,r[0]),f=c=0,y=a;y>=0?y>c:c>y;f=y>=0?++c:--c)for(l=h=0,Q=p;Q>=0?Q>h:h>Q;l=Q>=0?++h:--h)u[f][l]=l>f||l>=a?t[f][l]:f===l?o[f]:0;for(n=s(a,a,r[0]),i=v=R=a-1;v>=0;i=v+=-1){for(f=g=0,b=a;b>g;f=g+=1)n[f][i]=0;for(n[i][i]=1,l=M=U=i,q=a;q>M;l=M+=1)if(a>l&&0!==t[i][i]&&null!=(null!=(w=t[i])?w[i]:void 0)){for(S=0,f=P=A=i,D=a;D>P;f=P+=1)S+=t[f][i]*n[f][l];for(S=-S/t[i][i],f=j=L=i,m=a;m>j;f=j+=1)n[f][l]+=S*t[f][i]}}return{Q:n,R:u,singular:k}},h=function(r,n){return n.length!==r.length?null:a(r)?p(r)?v(r,n):g(r,n):d(r,n)},g=function(r,t){var u,f,e,l,i;return e=o(r),null==e?null:(u=e.L,f=e.U,i=c(u,t),l=n(f,i))},v=function(r,o){var f,e,l,i,a,p,s;return l=u(r),null==l?null:(f=l.L,e=l.d,p=c(f,o),s=t(p,function(r,n){return r/e[n]}),a=n(f,s,i=!0))},d=function(r,n){var u,o,f,l,i,c,a,p,s,h,v,g,d,y,L,m,Q,R,b,U,q,w,A,D,S,k;for(g=e(r),u=g.QR,f=g.d,k=t(n),a=u.length,p=u[0].length,o=p>a?a:p,i=c=0,d=o;d>c;i=c+=1){for(w=0,l=s=y=i,L=a;L>s;l=s+=1)w+=u[l][i]*k[l];for(w=-w/u[i][i],l=h=m=i,Q=a;Q>h;l=h+=1)k[l]+=w*u[l][i]}for(S=t(k),i=v=R=o-1;v>=0;i=v+=-1)for(S[i]/=f[i],l=A=0,b=i;b>=0?b>A:A>b;l=b>=0?++A:--A)S[l]-=S[i]*u[l][i];if(p>a)for(i=D=U=o,q=p;q>D;i=D+=1)S[i]=0;else a>p&&(S=S.slice(0,p));return S},c=function(r,n){var u,o,f,e,l,i,c,a;for(e=r.length,a=t(n,function(){return 0}),u=f=0,i=e;i>=0?i>f:f>i;u=i>=0?++f:--f){for(a[u]=n[u],o=l=0,c=u;c>=0?c>l:l>c;o=c>=0?++l:--l)a[u]-=a[o]*r[u][o];a[u]/=r[u][u]}return a},n=function(r,n,u){var o,f,e,l,i,c,a,p,s;for(null==u&&(u=!1),l=r.length,s=t(n,function(){return 0}),o=e=c=l-1;e>=0;o=e+=-1){for(s[o]=n[o],f=i=a=o+1,p=l;p>i;f=i+=1)s[o]-=s[f]*(u?r[f][o]:r[o][f]);s[o]/=r[o][o]}return s},a=function(r){return Array.isArray(r)&&r.length>0?r[0].length!==r.length?!1:!0:!1},p=function(r){var n,t,u,o,f,e,l;for(o=r.length,n=u=0,e=o;e>=0?e>u:u>e;n=e>=0?++u:--u)for(t=f=0,l=o;l>=0?l>f:f>l;t=l>=0?++f:--f)if(t!==n&&r[n][t]!==r[t][n])return!1;return!0},t=function(r,n){var t,u,o,f,e;for(e=r.constructor.apply(null,[r.length]),t=function(){var t;return e[u]=null!=(t="function"==typeof n?n(r[u],u):void 0)?t:r[u]},u=o=0,f=r.length;f>=0?f>o:o>f;u=f>=0?++o:--o)t();return e},s=function(r,n,t){var u;return function(){u=[];for(var n=0;r>=0?r>n:n>r;r>=0?n++:n--)u.push(n);return u}.apply(this).map(function(){var r,u,o,f;for(f=t.constructor.apply(null,[n]),r=u=0,o=n;o>=0?o>u:u>o;r=o>=0?++u:--u)f[r]=0;return f})},i=function(r,n){var t,u,o,f;return null==n&&(n=1),u=null!=(f=r.slice)?f:r.subarray,t=function(){var t,f,e,l;for(l=[],o=t=0,f=r.length,e=n;e>0?f>t:t>f;o=t+=e)l.push(function(){return u.apply(r,[o,o+n])}());return l}()},l=function(r){var n,t,u,o,f,e,l,i;for(f=r.length,n=r[0].constructor.apply(null,[f*f]),t=o=0,l=f;l>=0?l>o:o>l;t=l>=0?++o:--o)for(u=e=0,i=f;i>=0?i>e:e>i;u=i>=0?++e:--e)n[t*f+u]=r[t][u];return n},r.luqr={solve:h,solveLU:g,solveLDL:v,solveQR:d,decomposeLU:o,decomposeLDL:u,decomposeQR:f,decomposeQRPartial:e,forwardSubstition:c,backwardSubtitution:n,fold:i,flatten:l}}(this);