UNPKG

t-matrix

Version:

A small type-array based matrix lib. A core matrix type plus just the other functions you need.

4 lines (3 loc) 12.6 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const t=Symbol(),e=Symbol(),r=Symbol(),n=Symbol(),o=t=>"number"==typeof t||"boolean"==typeof t,i=Array.isArray;function s(t,e,r){return o(t)||(t=r),t<0&&e?t+e:t}function*f(t,e){i(t)||(t=[t]);for(let r of function*(t){let e,r,n=0,i=0;for(;n<t.length;)switch(e=t[n],i){case 0:o(e)?(r=e,i=1):":"===e?(r={step:1},i=3):"::"===e&&(r={},i=2),n++;break;case 1:o(e)?(yield r,r=e):":"===e?(r={start:r,step:1},i=3):"::"===e?(r={start:r},i=2):(yield r,i=0),n++;break;case 2:o(e)?(r.step=e,n++,i=3):yield r;break;case 3:o(e)&&(r.end=e,n++),yield r,i=0}0!==i&&(yield r)}(t))if(o(r))yield s(r,e);else if(r.step>0){let t=s(r.start,e,0);const n=s(r.end,e,-1);for(;t<=n;t+=r.step)yield t}else{if(!(r.step<0))throw new Error("Matrix:: Invalid range specification.");{let t=s(r.start,e,-1);const n=s(r.end,e,0);for(;t>=n;t+=r.step)yield t}}}function*l(t,e){for(let r of t)yield e(r)}const a=Symbol.iterator;function*c(t,e){for(let r=0;r<e;r++)yield t}function*u(...t){let e=(t=t.map(t=>t[a]())).map(t=>t.next());for(;e.every(t=>!t.done);)yield e.map(t=>t.value),e=t.map(t=>t.next())}function p(t){return[...f([...t])]}const h=Float64Array,m=Uint8Array,w=Uint32Array;function*d(n,o,i,s,f){const l=f[t],a=f[e],c=f[r];if(a.length!==o||c.length!==s)throw new Error("Matrix::binary addressing error, matrix dimensions must agree");for(let t=0;t<o;t++)for(let e=0;e<s;e++)l[a[t]+c[e]]&&(yield n[t]+i[e])}function y(t,e,r,n,o){return[b(o)?w.from(d(t,e,r,n,o)):w.from(x(o)?o:f(o,e*n)).map(e=>t[e/n|0]+r[e%n]),new w(1)]}function g(n,o,i){return b(i)?w.from(function*(n,o,i){const s=i[t],f=i[e],l=i[r],a=f.length,c=l.length;if(a*c!==o)throw new Error("Matrix::binary addressing error, matrix element count must agree with dimension size");for(let t=0,e=0;e<a;e++)for(let r=0;r<c;r++,t++)s[f[e]+l[r]]&&(yield n[t])}(n,o,i)):w.from(x(i)?i:f(i,o)).map(t=>n[t])}const x=t=>t instanceof M,b=e=>e instanceof M&&e[t].constructor===m;class M{constructor(n,i,s,l){const{binary:a=!1}=l||{};o(i)&&(i=[":",i-1]),Array.isArray(i)&&(i=w.from([...f(i)]));const c=i[i.length-1]+1;o(n)&&(n=["::",c,c*(n-1)]),Array.isArray(n)&&(n=w.from([...f(n)]));const u=c+n[n.length-1];if(a?s instanceof m||(s=m.from(s)):s?o(s)?s=new h(u).fill(s):s instanceof h||(s=h.from(s)):s=new h(u),s.length<u)throw new Error("Matrix:: Data array too small for specified rows and columns");Object.defineProperties(this,{[t]:{value:s},[e]:{value:n},[r]:{value:i}})}*[Symbol.iterator](){for(let n of this[e])for(let e of this[r])yield this[t][n+e]}get size(){return[this[e].length,this[r].length]}get t(){return new M(this[r],this[e],this[t])}get(n,i){const s=this[t],f=this[e],l=this[r],a=f.length,c=l.length,u=b(this);return o(n)&&o(i)?s[f[(n+a)%a]+l[(i+c)%c]]:1===arguments.length?o(n)?s[f[n/c|0]+l[n%c]]:new M(...y(f,a,l,c,n),s,{binary:u}):new M(g(f,a,n),g(l,c,i),s,{binary:u})}set(n,i,s){let f=this[e],l=this[r],a=f.length,c=l.length;const u=this[t],p=b(this);let h=o(n),m=o(i);if(h&&m&&o(s))return u[f[n]+l[i]]=p?s?1:0:s,this;switch(arguments.length){case 1:s=n;break;case 2:if(h&&m)return u[f[n/c|0]+l[n%c]]=p?i?1:0:i,this;[f,l]=y(f,a,l,c,n),a=f.length,c=1,s=i;break;case 3:f=g(f,a,n),a=f.length,l=g(l,c,i),c=l.length}if(o(s)){p&&(s=s?1:0);for(let t of f)for(let e of l)u[t+e]=s;return this}if("function"==typeof s){if(p)for(let t=0;t<a;t++)for(let e=0;e<c;e++)u[f[t]+l[e]]=s(u[f[t]+l[e]],t,e)?1:0;else for(let t=0;t<a;t++)for(let e=0;e<c;e++)u[f[t]+l[e]]=s(u[f[t]+l[e]],t,e);return this}x(s)||(s=z(s));const[w,d]=s.size;if(a!==w||c!==d)throw new Error("Matrix::set Assignment error, matrix dimensions must agree");let M=s[t],v=s[e],E=s[r];if(M===u&&(M=u.slice()),p)for(let t=0;t<a;t++)for(let e=0;e<c;e++)u[f[t]+l[e]]=M[v[t]+E[e]]?1:0;else for(let t=0;t<a;t++)for(let e=0;e<c;e++)u[f[t]+l[e]]=M[v[t]+E[e]];return this}clone(t,n){return t?this.get(t,n).clone():new M(this[e].length,this[r].length,this)}map(t){return new M(this[e].length,this[r].length,l(this,t))}}function z(t){if(x(t))return t;if(i(t)&&t.length){if(o(t[0]))return t=[...f(t)],new M(t.length,[0],t);if(i(t[0])){const e=(t=t.map(t=>[...f(t)])).length,r=t[0].length;if(t.every(t=>t.length===r))return new M(e,r,function*t(e){for(let r of e)r[Symbol.iterator]?yield*t(r):yield r}(t))}}throw new TypeError("Matrix::from Unsupported data type")}function v(...t){let e=null;for(let r of function*(t){for(let e of t)if("function"==typeof e||"string"==typeof e)yield e;else for(let t of Object.keys(e))"function"==typeof e[t]&&(yield e[t])}(t))"function"==typeof r&&(e||r[n])&&(M.prototype[r[n]||e]=function(...t){return r(this,...t)}),e="string"==typeof r?r:null}function*E(n){n=z(n);const o=Array.from(n[r]);for(let r of n[e])yield o.map(e=>n[t][r+e])}function*A(n){n=z(n);const o=Array.from(n[e]);for(let e of n[r])yield o.map(r=>n[t][r+e])}function k(n,o){const i=(n=z(n))[e],s=n[r],f=n[t],l=s.length,a=i.length;if(1===l){if(1===a)return o?n.set(o):n;o=n,n=new M(a,a)}return o?(k(n).set(o),n):new M(a<l?i.map((t,e)=>t+s[e]):s.map((t,e)=>i[e]+t),[0],f)}function S(t,e,r){return new M(e,r,t)}function N(t,r,n){const o=t[e];for(let t of u(f(r),f(n)))[o[t[0]],o[t[1]]]=[o[t[1]],o[t[0]]];return t}function O(t,e,n){const o=t[r];for(let t of u(f(e),f(n)))[o[t[0]],o[t[1]]]=[o[t[1]],o[t[0]]];return t}function j(n,o,i){return new M(n[e].filter((t,e)=>e!==o),n[r].filter((t,e)=>e!==i),n[t])}function I(t,e=1,r=1){const n=t.size;return new M(n[0]*e,n[1]*r,function*(t,e,r){for(let n=0;n<e;n++)for(let e of E(t))for(let t=0;t<r;t++)yield*e}(t,e,r))}function U(...t){const e=(t=t.map(t=>z(t))).map(t=>t.size),r=e[0][1];if(e.some(t=>t[1]!==r))throw new Error("Matrix::vcat Matrices must have the same width.");const n=e.reduce((t,e)=>t+e[0],0);return new M(n,r,function*(t){for(let e of t)yield*e}(t))}function _(...t){const e=(t=t.map(t=>z(t))).map(t=>t.size),r=e[0][0];if(e.some(t=>t[0]!==r))throw new Error("Matrix::vcat Matrices must have the same width.");const n=e.reduce((t,e)=>t+e[1],0);return new M(r,n,function*(t){for(let e of u(...t.map(t=>E(t))))for(let t of e)yield*t}(t))}E[n]="rows",A[n]="cols",v("toJSON",t=>1===t[r].length?[...t]:[...E(t)]),k[n]="diag",S[n]="reshape",N[n]="swapRows",O[n]="swapCols",j[n]="minor",I[n]="repmat",U[n]="vcat",_[n]="hcat";const P=(t=1,e=t)=>i(t)?new M(t[0],t[1]):new M(t,e),C=t=>k(P(t),1); //! Error class function L(t){const e=Error.call(this,t);return e.name="MatrixError",e.message=t,e}L.prototype=Object.create(Error.prototype,{constructor:{value:Error,enumerable:!1,writable:!0,configurable:!0}}),Object.setPrototypeOf(L,Error);const R="Invalid matrix dimensions for the operation.",T=t=>{let e,r=0;for(e of t)r+=e;return r},V=t=>{let e,r=-Number.MAX_VALUE;for(e of t)e>r&&(r=e);return r},X=t=>{let e,r=Number.MAX_VALUE;for(e of t)e<r&&(r=e);return r},q=t=>{let e,r=1;for(e of t)r*=e;return r};function D(...t){return W(T,...t)}function F(...t){return W(V,...t)}function J(...t){return W(X,...t)}function B(...t){return W(q,...t)}function G(t){return D(k(t))}function H(...t){const e=t.pop();return Q(t=>e.apply(null,t),{},...t)}function K(...t){const e=1===t.length?t=>t:t.pop();return Q(t=>e.apply(null,t)?1:0,{binary:!0},...t)}function Q(t,e,...r){r=r.map(t=>o(t)?z([t]):z(t));const[n,i]=r.reduce(([t,e],r)=>{const[n,o]=r.size;return[n>t?n:t,o>e?o:e]},[1,1]);return r=r.map(t=>function*(t,e,r){const[n,o]=t.size;if(n===e&&o===r)yield*t;else if(1===n&&1===o)for(let n=e*r,o=t.get(0,0);n--;)yield o;else if(1===n&&o===r)for(let r=e;r--;)yield*t;else{if(n!==e||1!==o)throw new L(R);for(let e of t)for(let t=r;t--;)yield e}}(t,n,i)),new M(n,i,l(u(...r),t),e)}function W(t,...e){return null==e[1]?((t,e,r)=>{switch(e){case 1:return new M(1,t.size[1],l(A(t),r));case 2:return new M(t.size[0],1,l(E(t),r));default:return r(t)}})(e[0],e[2],t):Q(t,{},...e)}function Y(...t){let e,r,n,i=1;for(let s of t)if(o(s))i*=s;else if(s=z(s),e){const[t,o]=s.size;if(n!==t)throw new L(R);e=new M(r,o,Z(e,s,n)),n=o}else e=s,[r,n]=e.size;return 1===i?e:B(e,i)}function*Z(n,o,i){const s=n[r],f=n[t],l=o[e],a=o[t];for(let t of n[e])for(let e of o[r]){let r=0;for(let n=0;n<i;n++)r+=f[t+s[n]]*a[l[n]+e];yield r}}function $(t){t=z(t);const[e,r]=t.size;if(e!==r)return 0;if(e<4){const r=[...t];return 2===e?r[0]*r[3]-r[1]*r[2]:r[0]*(r[4]*r[8]-r[7]*r[5])+r[1]*(r[5]*r[6]-r[8]*r[3])+r[2]*(r[3]*r[7]-r[6]*r[4])}let n=0;for(let e=1;e<=r;e+=2)n+=t.get(0,e-1)*$(j(t,0,e-1)),e<r&&(n-=t.get(0,e)*$(j(t,0,e)));return n}function tt(n,o){n=z(n),o=z(o);const i=n.clone(),{[e]:s,[r]:f,[t]:l}=i,a=o.clone(),{[e]:c,[r]:u,[t]:p}=a,[h]=o.size,[m,w]=n.size;if(m!==w||m!==h)throw new L(R);for(let t=0;t<h;t++){if(Math.abs(l[s[t]+f[t]])<1e-10){let e;for(e=t+1;e<h;e++)if(Math.abs(l[s[e]+f[t]])>1e-10){[s[t],s[e]]=[s[e],s[t]],[c[t],c[e]]=[c[e],c[t]];break}if(e>=h)throw new L("Matrix is singular to working precision.")}const e=s[t],r=c[t],n=1/l[e+f[t]];for(let t of f)l[e+t]*=n;for(let t of u)p[r+t]*=n;for(let n=0;n<h;n++){if(n===t)continue;const o=s[n],i=c[n],a=l[o+f[t]];for(let t of f)l[o+t]-=a*l[e+t];for(let t of u)p[i+t]-=a*p[r+t]}}return a}function et(t,e){return tt(e.t,t.t).t}function rt(t){return tt(t,C(t.size[0]))}function nt(t){return z(t).map(Math.abs)}function ot(t,e){return o(t)&&(t=[":",t-1]),t=p(t),o(e)&&(e=[":",e-1]),e=e?p(e):t,[I(new M(t.length,1,t),1,e.length),I(new M(1,e.length,e),t.length,1)]}function it(t,e,r){t=z(t),e=z(e);const[n,o]=t.size,[i,s]=e.size;if(!r)if(3===n&&3===i)r=1;else{if(3!==o||3!==s)throw L(R);r=2}if(1===r&&(3!==n||3!==i)||2===r&&(3!==o||3!==s))throw L(R);if(1===r&&o!==s&&o>1&&s>1||2===r&&n!==i&&n>1&&i>1)throw L(R);if(1===r){const r=Math.max(o,s),n=1===o?c(t,r):A(t),i=1===s?c(e,r):A(e);return new M(r,3,st(n,i)).t}{const r=Math.max(n,i),o=1===n?c(t,r):E(t),s=1===i?c(e,r):E(e);return new M(r,3,st(o,s))}}function*st(t,e){for(let[r,n]of u(t,e)){const[t,e,o]=r,[i,s,f]=n;yield e*f-o*s,yield i*o-t*f,yield t*s-i*e}}function ft(t){return U(...t.map(t=>_(...t)))}D[n]="sum",F[n]="max",J[n]="min",B[n]="product",G[n]="trace",H[n]="mapMany",K[n]="bin",Y[n]="mult",$[n]="det",tt[n]="ldiv",et[n]="div",rt[n]="inv",nt[n]="abs",it[n]="cross",exports.abs=nt,exports.bin=K,exports.cols=A,exports.cross=it,exports.cumsum=function(t,e){const r=z(t).clone(),[n,o]=r.size;if(2===e)for(let t=0;t<n;t++){let e=0;r.set(t,":",t=>e+=t)}else for(let t=0;t<o;t++){let e=0;r.set(":",t,t=>e+=t)}return r},exports.det=$,exports.diag=k,exports.div=et,exports.dot=function(t,e,r){t=z(t),e=z(e);const[n,o]=t.size,[i,s]=e.size;if(r){if(1===r&&(1===n||1===i||n!==i)||2===r&&(1===o||1===s||o!==s))throw L(R)}else if(n>1&&i>1&&n===i)r=1;else{if(!(o>1&&s>1&&o===s))throw L(R);r=2}return D(B(t,e),null,r)},exports.eye=C,exports.from=z,exports.grid=ot,exports.gridInterp1=function(t,e){t=z(t);const[r,n]=t.size;e=z(e);const[o,i]=e.size;if(i>1&&n>1)throw new Error("Matrix:: gridInterp1 - either the first or second parameter must be a column vector");const s=K(e,t=>t>=0&&t<=r),f=e.get(s);let l,a,c;const u=f.map(Math.floor),p=u.map(t=>t+1);l=[u,p],a=[f.map(t=>t%1)],c=(t,e,r)=>(1-r)*t+r*e;const[h,m]=n>1?[n,[":"]]:[i,[]],w=P(o,h).set(Number.NaN),d=l.map(e=>t.get(e,...m));return w.set(s,...m,H(...d,...a,c))},exports.hcat=_,exports.inv=rt,exports.isMatrix=x,exports.kron=function(t,e){t=z(t),e=z(e);const r=[];for(let n of E(t))r.push(_(...n.map(t=>e.map(e=>t*e))));return U(...r)},exports.ldiv=tt,exports.magic=function t(e){if(2===e||e<1)throw new Error("Matrix::magic size must be one or greater and not equal to two.");if(!Number.isInteger(e))throw new TypeError("Matrix:magic size must be an integer.");if(e%2){const[t,r]=ot([1,":",e]),n=D(t,r,e-3>>1).map(t=>t%e),o=D(t,B(2,r),-2).map(t=>t%e);return D(B(e,n),o,1)}if(!(e%4)){const[t,r]=ot([1,":",e]),n=S(z([1,":",e*e]),e,e),o=K(t,r,(t,e)=>Math.floor(t%4/2)===Math.floor(e%4/2));return n.set(o,t=>e*e+1-t),n}const r=e>>1,n=t(r),o=ft([[n,D(n,2*r*r)],[D(n,3*r*r),D(n,r*r)]]);let i=e-2>>2,s=[":",i-1,e+1-i,":"];return o.set(":",s,o.get([r,":",":",r-1],s)),s=[0,i],o.set([i,i+r],s,o.get([i+r,i],s)),o},exports.mapMany=H,exports.max=F,exports.mcat=ft,exports.min=J,exports.minor=j,exports.mixin=v,exports.mult=Y,exports.ones=(t=1,e=t)=>i(t)?new M(t[0],t[1],1):new M(t,e,1),exports.product=B,exports.rand=(t,e)=>P(t,e).set(()=>Math.random()),exports.repmat=I,exports.reshape=S,exports.rows=E,exports.shift=function(t,e,r){if(t=z(t),!e&&!r)return t;if(o(e)&&o(r))return t.get(0===e?":":[-e,":",":",-e-1],0===r?":":[-r,":",":",-r-1]);if(~r&&o(e)){const r=[-e,":",":",-e-1];return 1===t.size[0]?t.get(":",r):t.get(r,":")}throw new Error("Matrix::shift the second and third parameters, if defined, must be numbers")},exports.sum=D,exports.swapCols=O,exports.swapRows=N,exports.trace=G,exports.vcat=U,exports.zeros=P;