New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@thi.ng/sparse

Package Overview
Dependencies
Maintainers
1
Versions
239
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@thi.ng/sparse - npm Package Compare versions

Comparing version 0.1.37 to 0.1.38

8

CHANGELOG.md

@@ -6,2 +6,10 @@ # Change Log

## [0.1.38](https://github.com/thi-ng/umbrella/compare/@thi.ng/sparse@0.1.37...@thi.ng/sparse@0.1.38) (2020-05-03)
**Note:** Version bump only for package @thi.ng/sparse
## [0.1.37](https://github.com/thi-ng/umbrella/compare/@thi.ng/sparse@0.1.36...@thi.ng/sparse@0.1.37) (2020-04-28)

@@ -8,0 +16,0 @@

2

lib/index.umd.js

@@ -1,1 +0,1 @@

!function(t,s){"object"==typeof exports&&"undefined"!=typeof module?s(exports,require("@thi.ng/api"),require("@thi.ng/transducers")):"function"==typeof define&&define.amd?define(["exports","@thi.ng/api","@thi.ng/transducers"],s):s(((t=t||self).thi=t.thi||{},t.thi.ng=t.thi.ng||{},t.thi.ng.sparse={}),t.thi.ng.api,t.thi.ng.transducers)}(this,(function(t,s,e){"use strict";class n{constructor(t,s){this.m=t,this.n=s}trace(){s.assert(this.m===this.n,"matrix is non-square");let t=0;for(let s=this.m;--s>=0;)t+=this.at(s,s,!1);return t}ensureIndex(t,e){s.assert(t>=0&&t<this.m&&e>=0&&e<this.n,`index out of bounds (${t}, ${e})`)}ensureSize(t){s.assert(t.m===this.m&&t.n===this.n,`incompatible size: (${t.m},${t.n})`)}}const r=(t,s,e)=>{const n=[0],r=[],i=[];for(let o=0,h=0;h<s;h++){let s=0;for(let n=0;n<t;o++,n++)0!==e[o]&&(i.push(e[o]),r.push(n),s++);n.push(n[n.length-1]+s)}return[n,r,i]},i=t=>{const s=t.length,e=[],n=[];for(let t=0;t<s;t++)e.push(t),n.push(t);return e.push(s),[e,n]},o=(t,s,e,n,r)=>{const i=e[t+1];for(let o=e[t];o<i;o++)if(n[o]===s)return r[o];return 0},h=(t,s,e,n,r,i,o,h=!0)=>{const u=0!==n,c=r[t+1];for(let d=r[t];d<c;d++){const c=i[d];if(c===s)return u||!h?o[d]=n:a(t,e,d,r,i,o),[r,i,o];if(c>s&&u)return l(t,s,e,n,d,r,i,o)}return u?l(t,s,e,n,c,r,i,o):[r,i,o]},l=(t,s,e,n,r,i,o,h)=>{h=h.slice(0,r).concat(n,h.slice(r)),o=o.slice(0,r).concat(s,o.slice(r));for(let s=t+1;s<=e;s++)i[s]++;return[i,o,h]},a=(t,s,e,n,r,i)=>{i.splice(e,1),r.splice(e,1);for(let e=t+1;e<=s;e++)n[e]--};class u extends n{constructor(t,s,e,n,r){super(t,s),this.cols=n,this.rows=r,this.data=e}static fromDense(t,s,e){const[n,i,o]=r(t,s,e);return new u(t,s,o,n,i)}static empty(t,s=t){return new u(t,s,[],new Array(s+1).fill(0),[])}static identity(t){return u.diag(new Array(t).fill(1))}static diag(t){const[s,e]=i(t);return new u(t.length,t.length,t,s,e)}copy(){return new u(this.m,this.n,this.data.slice(),this.cols.slice(),this.rows.slice())}zero(){return this.data.length=this.rows.length=0,this.cols.fill(0),this}*nzEntries(){const t=this.cols,s=this.rows,e=this.data;for(let n=0;n<this.n;n++)for(let r=t[n],i=t[n+1];r<i;r++)yield[s[r],n,e[r]]}at(t,s,e=!0){return e&&this.ensureIndex(t,s),o(s,t,this.cols,this.rows,this.data)}setAt(t,s,e,n=!0,r=!0){n&&this.ensureIndex(t,s);const i=h(s,t,this.n,e,this.cols,this.rows,this.data,r);return this.cols=i[0],this.rows=i[1],this.data=i[2],this}mul(t){s.assert(this.n===t.m,"incompatible matrix sizes");const e=u.empty(this.m,t.n);for(let s=0;s<t.n;s++)if(t.nnzCol(s)>0)for(let n=0;n<t.m;n++){const r=t.at(n,s);if(0!==r)for(let t=0;t<this.m;t++){const i=this.at(t,n,!1);0!==i&&e.setAt(t,s,e.at(t,s,!1)+i*r,!1)}}return e}mulV(t){s.assert(this.m===t.length,`vector length != ${this.m}`);const e=this.cols,n=this.rows,r=this.data,i=new Array(this.m).fill(0);for(let s=0;s<this.n;s++){const o=e[s+1];for(let h=e[s];h<o;h++)i[n[h]]+=r[h]*t[s]}return i}nnz(){return this.data.length}nnzCol(t){return this.cols[t+1]-this.cols[t]}nzColRows(t){return this.rows.slice(this.cols[t],this.cols[t+1])}nzColVals(t){return this.data.slice(this.cols[t],this.cols[t+1])}nnzRow(t){const s=this.rows;let e=0;for(let n=s.length;--n>=0;)s[n]===t&&e++;return e}nzRowVals(t){const s=this.rows,e=this.data,n=[];for(let r=0,i=s.length;r<i;r++)s[r]===t&&n.push(e[r]);return n}nzRowCols(t){const s=[];for(let e=0;e<this.n;e++)0!==this.at(t,e,!1)&&s.push(e);return s}transpose(){const t=u.empty(this.n,this.m),s=this.rows,e=this.data;for(let n=0;n<this.n;n++){const r=this.cols[n+1];for(let i=this.cols[n];i<r;i++)t.setAt(n,s[i],e[i])}return t}denseCol(t){const s=new Array(this.m).fill(0),e=this.cols[t+1],n=this.rows,r=this.data;for(let i=this.cols[t];i<e;i++)s[n[i]]=r[i];return s}denseRow(t){const s=new Array(this.n);for(let e=0;e<this.n;e++)s[e]=this.at(t,e,!1);return s}toDense(){let t=[];for(let s=0;s<this.n;s++)t=t.concat(this.denseCol(s));return t}toString(){const t=[];for(let s=0;s<this.m;s++)t.push(this.denseRow(s).join(" "));return t.join("\n")}}class c extends n{constructor(t,s,e,n,r){super(t,s),this.rows=n,this.cols=r,this.data=e}static fromDense(t,s,e){const[n,i,o]=r(t,s,e);return new c(t,s,o,n,i)}static empty(t,s=t){return new c(t,s,[],new Array(t+1).fill(0),[])}static identity(t){return c.diag(new Array(t).fill(1))}static diag(t){const[s,e]=i(t);return new c(t.length,t.length,t,s,e)}copy(){return new c(this.m,this.n,this.data.slice(),this.rows.slice(),this.cols.slice())}zero(){return this.data.length=this.cols.length=0,this.rows.fill(0),this}*nzEntries(){const t=this.rows,s=this.cols,e=this.data;for(let n=0;n<this.m;n++)for(let r=t[n],i=t[n+1];r<i;r++)yield[n,s[r],e[r]]}reshape(t,s=t){const e=this.data,n=this.rows,r=this.cols;if(t>this.m)for(let s=t-this.m,e=this.nnz();s>0;s--)n.push(e);else if(t<this.m){const s=n[t];e.length=s,r.length=s,n.length=t+1}if(this.m=t,s<this.n)for(let i=0;i<t;i++)for(let o=n[i],h=n[i+1];o<h;)r[o]>=s?(a(i,t,o,n,r,e),h--):o++;return this.n=s,this}extract(t,s,e,n){const r=t+e,i=s+n,o=this.rows,h=this.cols,l=this.data,a=[0],u=[],d=[];for(let e=t;e<r;e++){if(e<this.m){const t=o[e+1];for(let n=o[e];n<t;n++){const t=h[n];t>=s&&t<i&&(d.push(l[n]),u.push(t-s))}}a.push(d.length)}return new c(e,n,d,a,u)}at(t,s,e=!0){return e&&this.ensureIndex(t,s),o(t,s,this.rows,this.cols,this.data)}setAt(t,s,e,n=!0,r=!0){n&&this.ensureIndex(t,s);const i=h(t,s,this.m,e,this.rows,this.cols,this.data,r);return this.rows=i[0],this.cols=i[1],this.data=i[2],this}denseRow(t){const s=new Array(this.n).fill(0),e=this.cols,n=this.data;for(let r=this.rows[t],i=this.rows[t+1];r<i;r++)s[e[r]]=n[r];return s}denseCol(t){const s=new Array(this.m);for(let e=0;e<this.m;e++)s[e]=this.at(e,t,!1);return s}add(t){if(this.ensureSize(t),this===t)return this.mulN(2);if(0===t.nnz())return this.copy();const s=c.empty(this.m,this.n);for(let e=0;e<this.m;e++){const n=t.rows[e+1];t:for(let r=t.rows[e];r<n;r++){const n=t.cols[r],i=this.rows[e+1];for(let o=this.rows[e];o<i;o++)if(this.cols[o]===n){s.setAt(e,n,this.data[o]+t.data[r],!1);continue t}s.setAt(e,n,t.data[r],!1)}}return s}sub(t){this.ensureSize(t);const s=c.empty(this.m,this.n);if(this===t||0===t.nnz())return s;for(let e=0;e<this.m;e++){const n=t.rows[e+1];t:for(let r=t.rows[e];r<n;r++){const n=t.cols[r],i=this.rows[e+1];for(let o=this.rows[e];o<i;o++)if(this.cols[o]===n){s.setAt(e,n,this.data[o]-t.data[r],!1);continue t}s.setAt(e,n,-t.data[r],!1)}}return s}mul(t){s.assert(this.n===t.m,"incompatible matrix sizes");const e=c.empty(this.m,t.n);for(let s=0;s<t.n;s++)if(t.nnzCol(s)>0)for(let n=0;n<t.m;n++){const r=t.at(n,s);if(0!==r)for(let t=0;t<this.m;t++){const i=this.at(t,n,!1);0!==i&&e.setAt(t,s,e.at(t,s,!1)+i*r,!1)}}return e}mulV(t){s.assert(this.m===t.length,`vector length != ${this.m}`);const e=this.rows,n=this.cols,r=this.data,i=new Array(t.length).fill(0);for(let s=0;s<this.m;s++){const o=e[s+1];for(let h=e[s];h<o;h++)i[s]+=r[h]*t[n[h]]}return i}mulN(t){if(0===t)return c.empty(this.m,this.n);const s=this.copy(),e=s.data;for(let s=e.length;--s>=0;)e[s]*=t;return s}nnz(){return this.data.length}nnzRow(t){return this.rows[t+1]-this.rows[t]}nzRowCols(t){return this.cols.slice(this.rows[t],this.rows[t+1])}nzRowVals(t){return this.data.slice(this.rows[t],this.rows[t+1])}nnzCol(t){const s=this.cols;let e=0;for(let n=s.length-1;n>=0;n--)s[n]===t&&e++;return e}nzColVals(t){const s=this.cols,e=this.data,n=[];for(let r=0,i=s.length;r<i;r++)s[r]===t&&n.push(e[r]);return n}nzColRows(t){const s=[];for(let e=0;e<this.m;e++)0!==this.at(e,t,!1)&&s.push(e);return s}transpose(){const t=c.empty(this.n,this.m),s=this.cols,e=this.data;for(let n=0;n<this.m;n++){const r=this.rows[n+1];for(let i=this.rows[n];i<r;i++)t.setAt(s[i],n,e[i])}return t}toDense(){let t=[];for(let s=0;s<this.m;s++)t=t.concat(this.denseRow(s));return t}toString(){const t=this.toDense(),s=[];for(let e=0;e<this.m;e++)s.push(t.slice(e*this.n,(e+1)*this.n).join(" "));return s.join("\n")}}class d extends n{constructor(t,s=t,e){super(t,s),this.data=e||[]}static fromDense(t,s,e){const n=[];for(let r=0,i=0;r<t;r++)for(let t=0;t<s;t++,i++)0!==e[i]&&n.push(r,t,e[i]);return new d(t,s,n)}static identity(t){return d.diag(new Array(t).fill(1))}static diag(t){const s=[],e=t.length;for(let n=0;n<e;n++)s.push(n,n,t[n]);return new d(e,e,s)}nzEntries(){return e.partition(3,3,this.data)}at(t,s,e=!0){e&&this.ensureIndex(t,s);const n=this.data;for(let e=0,r=n.length;e<r&&n[e]<=t;e+=3)if(n[e]===t&&n[e+1]===s)return n[e+2];return 0}setAt(t,s,e,n=!0){n&&this.ensureIndex(t,s);const r=this.data;for(let n=0,i=r.length;n<i;n+=3){const i=r[n],o=r[n+1];if(t<i||t===i&&s<o)return 0!==e&&r.splice(n,0,t,s,e),this;if(t===i&&s===o)return 0!==e?r[n+2]=e:r.splice(n,3),this}return 0!==e&&r.push(t,s,e),this}mulV(t){const s=new Array(this.m).fill(0);for(let e=this.data,n=e.length-3;n>=0;n-=3)s[e[n]]+=e[n+2]*t[e[n+1]];return s}nnz(){return this.data.length/3}nnzCol(t){throw new Error("Method not implemented.")}nnzRow(t){throw new Error("Method not implemented.")}nzColRows(t){throw new Error("Method not implemented.")}nzColVals(t){throw new Error("Method not implemented.")}nzRowCols(t){throw new Error("Method not implemented.")}nzRowVals(t){throw new Error("Method not implemented.")}toDense(){const t=this.n,s=this.data,e=new Array(this.m*t).fill(0);for(let n=s.length-3;n>=0;n-=3)e[s[n]*t+s[n+1]]=s[n+2];return e}toCSC(){const t=[],s=[0],e=[],n=[...this.nzEntries()].sort((t,s)=>t[1]-s[1]);for(let r=0,i=0;r<n.length;r++){const o=n[r];o[1]!==i&&(i=o[1],s.push(t.length)),e.push(o[0]),t.push(o[2])}return s.push(t.length),new u(this.m,this.n,t,s,e)}toCSR(){const t=[],s=[0],e=[],n=this.data;for(let r=0,i=0;r<n.length;r+=3)n[r]!==i&&(i=n[r],s.push(t.length)),e.push(n[r+1]),t.push(n[r+2]);return s.push(t.length),new c(this.m,this.n,t,s,e)}}const f=(t,s)=>t+s,m=(t,s)=>t-s,w=(t,s)=>t*s,p=(t,s)=>t/s;class g{constructor(t,s){this.m=t,this.data=s||[]}static fromDense(t){const s=[],e=t.length;for(let n=0;n<e;n++){const e=t[n];0!==e&&s.push(n,e)}return new g(e,s)}copy(){return new g(this.m,this.data.slice())}get length(){return this.m}get nnz(){return this.data.length>>>1}*nzEntries(){const t=this.data;for(let s=0,e=t.length;s<e;s+=2)yield[t[s],0,t[s+1]]}at(t,s=!0){s&&this.ensureIndex(t);const e=this.data;for(let s=0,n=e.length;s<n&&e[s]<=t;s+=2)if(t===e[s])return e[s+1];return 0}setAt(t,s,e=!0){e&&this.ensureIndex(t);const n=this.data;for(let e=0,r=n.length;e<r;e+=2){if(t<n[e])return 0!==s&&n.splice(e,0,t,s),this;if(t===n[e])return 0!==s?n[e+1]=s:n.splice(e,2),this}return 0!==s&&n.push(t,s),this}binopN(t,s){const e=this.data,n=this.m,r=[];for(let i=0,o=0,h=e[o];i<n;i++){let n=t(i===h?(o+=2,h=e[o],e[o-1]):0,s);0!==n&&r.push(i,n)}return new g(this.m,r)}binop(t,s){this.ensureSize(s);const e=this.data,n=s.data,r=[];for(let s=0,i=0,o=e.length,h=n.length;s<o||i<h;){const o=e[s],h=n[i];if(o===h){const h=t(e[s+1],n[i+1]);0!==h&&r.push(o,h),s+=2,i+=2}else if(void 0===h||o<h){const n=t(e[s+1],0);0!==n&&r.push(o,n),s+=2}else{const s=t(0,n[i+1]);0!==s&&r.push(h,s),i+=2}}return new g(this.m,r)}add(t){return this.binop(f,t)}sub(t){return this.binop(m,t)}mul(t){return this.binop(w,t)}div(t){return this.binop(p,t)}addN(t){return this.binopN(f,t)}subN(t){return this.binopN(m,t)}mulN(t){const s=this.data,e=s.length,n=new Array(e);for(let r=0;r<e;r+=2)n[r]=s[r],n[r+1]=s[r+1]*t;return new g(this.m,n)}divN(t){const s=this.data,e=s.length,n=new Array(e);for(let r=0;r<e;r+=2)n[r]=s[r],n[r+1]=s[r+1]/t;return new g(this.m,n)}dot(t){this.ensureSize(t);const s=this.data,e=t.data;let n=0;for(let t=s.length-2,r=e.length-2;t>=0&&r>=0;){const i=s[t],o=e[r];i===o?(n+=s[t+1]*e[r+1],t-=2,r-=2):i>o?t-=2:r-=2}return n}magSquared(){const t=this.data;let s=0;for(let e=t.length-1;e>=1;e-=2)s+=t[e]*t[e];return s}mag(){return Math.sqrt(this.magSquared())}normalize(t=1){const s=this.magSquared();if(s>1e-9){t/=Math.sqrt(s);const e=this.data;for(let s=e.length-1;s>=1;s-=2)e[s]*=t}return this}toDense(){const t=new Array(this.m).fill(0),s=this.data;for(let e=s.length-2;e>=0;e-=2)t[s[e]]=s[e+1];return t}ensureIndex(t){s.assert(t>=0&&t<this.m,`index out of bounds: ${t}`)}ensureSize(t){s.assert(this.m===t.m,`wrong vector size: ${t.m}`)}}class z extends n{constructor(t){t instanceof g?(super(t.m,t.m),this.data=t):(super(t.length,t.length),this.data=g.fromDense(t))}static identity(t){return new z(new Array(t).fill(1))}*nzEntries(){for(let t of this.data.nzEntries())yield[t[0],t[0],t[2]]}at(t,s,e=!0){return e&&this.ensureIndex(t,s),t===s?this.data.at(t,!1):0}setAt(t,e,n,r=!0){return r&&s.assert(t===e&&t>=0&&t<this.m,`invalid index: ${t},${e}`),this.data.setAt(t,n,!1),this}nnz(){return this.data.length}nnzCol(t){return 0!==this.data.at(t)?1:0}nnzRow(t){return this.nnzCol(t)}nzColRows(t){return 0!==this.data.at(t)?[t]:[]}nzColVals(t){const s=this.data.at(t);return 0!==s?[s]:[]}nzRowCols(t){return this.nzColRows(t)}nzRowVals(t){return this.nzColVals(t)}toDense(){const t=this.n,s=this.data,e=new Array(t*t).fill(0);for(let n=0;n<t;n++)e[n*t+n]=s.at(n,!1);return e}toCSC(){return u.diag(this.data.toDense())}toCSR(){return c.diag(this.data.toDense())}}t.ASparseMatrix=n,t.COO=d,t.CSC=u,t.CSR=c,t.Diag=z,t.SparseVec=g,Object.defineProperty(t,"__esModule",{value:!0})}));
!function(t,s){"object"==typeof exports&&"undefined"!=typeof module?s(exports,require("@thi.ng/api"),require("@thi.ng/transducers")):"function"==typeof define&&define.amd?define(["exports","@thi.ng/api","@thi.ng/transducers"],s):s(((t=t||self).thi=t.thi||{},t.thi.ng=t.thi.ng||{},t.thi.ng.sparse={}),t.thi.ng.api,t.thi.ng.transducers)}(this,(function(t,s,e){"use strict";class n{constructor(t,s){this.m=t,this.n=s}trace(){s.assert(this.m===this.n,"matrix is non-square");let t=0;for(let s=this.m;--s>=0;)t+=this.at(s,s,!1);return t}ensureIndex(t,e){s.assert(t>=0&&t<this.m&&e>=0&&e<this.n,`index out of bounds (${t}, ${e})`)}ensureSize(t){s.assert(t.m===this.m&&t.n===this.n,`incompatible size: (${t.m},${t.n})`)}}const r=(t,s,e)=>{const n=[0],r=[],i=[];for(let o=0,h=0;h<s;h++){let s=0;for(let n=0;n<t;o++,n++)0!==e[o]&&(i.push(e[o]),r.push(n),s++);n.push(n[n.length-1]+s)}return[n,r,i]},i=t=>{const s=t.length,e=[],n=[];for(let t=0;t<s;t++)e.push(t),n.push(t);return e.push(s),[e,n]},o=(t,s,e,n,r)=>{const i=e[t+1];for(let o=e[t];o<i;o++)if(n[o]===s)return r[o];return 0},h=(t,s,e,n,r,i,o,h=!0)=>{const u=0!==n,c=r[t+1];for(let d=r[t];d<c;d++){const c=i[d];if(c===s)return u||!h?o[d]=n:a(t,e,d,r,i,o),[r,i,o];if(c>s&&u)return l(t,s,e,n,d,r,i,o)}return u?l(t,s,e,n,c,r,i,o):[r,i,o]},l=(t,s,e,n,r,i,o,h)=>{h=h.slice(0,r).concat(n,h.slice(r)),o=o.slice(0,r).concat(s,o.slice(r));for(let s=t+1;s<=e;s++)i[s]++;return[i,o,h]},a=(t,s,e,n,r,i)=>{i.splice(e,1),r.splice(e,1);for(let e=t+1;e<=s;e++)n[e]--};class u extends n{constructor(t,s,e,n,r){super(t,s),this.cols=n,this.rows=r,this.data=e}static fromDense(t,s,e){const[n,i,o]=r(t,s,e);return new u(t,s,o,n,i)}static empty(t,s=t){return new u(t,s,[],new Array(s+1).fill(0),[])}static identity(t){return u.diag(new Array(t).fill(1))}static diag(t){const[s,e]=i(t);return new u(t.length,t.length,t,s,e)}copy(){return new u(this.m,this.n,this.data.slice(),this.cols.slice(),this.rows.slice())}zero(){return this.data.length=this.rows.length=0,this.cols.fill(0),this}*nzEntries(){const t=this.cols,s=this.rows,e=this.data;for(let n=0;n<this.n;n++)for(let r=t[n],i=t[n+1];r<i;r++)yield[s[r],n,e[r]]}at(t,s,e=!0){return e&&this.ensureIndex(t,s),o(s,t,this.cols,this.rows,this.data)}setAt(t,s,e,n=!0,r=!0){n&&this.ensureIndex(t,s);const i=h(s,t,this.n,e,this.cols,this.rows,this.data,r);return this.cols=i[0],this.rows=i[1],this.data=i[2],this}mul(t){s.assert(this.n===t.m,"incompatible matrix sizes");const e=u.empty(this.m,t.n);for(let s=0;s<t.n;s++)if(t.nnzCol(s)>0)for(let n=0;n<t.m;n++){const r=t.at(n,s);if(0!==r)for(let t=0;t<this.m;t++){const i=this.at(t,n,!1);0!==i&&e.setAt(t,s,e.at(t,s,!1)+i*r,!1)}}return e}mulV(t){s.assert(this.m===t.length,"vector length != "+this.m);const e=this.cols,n=this.rows,r=this.data,i=new Array(this.m).fill(0);for(let s=0;s<this.n;s++){const o=e[s+1];for(let h=e[s];h<o;h++)i[n[h]]+=r[h]*t[s]}return i}nnz(){return this.data.length}nnzCol(t){return this.cols[t+1]-this.cols[t]}nzColRows(t){return this.rows.slice(this.cols[t],this.cols[t+1])}nzColVals(t){return this.data.slice(this.cols[t],this.cols[t+1])}nnzRow(t){const s=this.rows;let e=0;for(let n=s.length;--n>=0;)s[n]===t&&e++;return e}nzRowVals(t){const s=this.rows,e=this.data,n=[];for(let r=0,i=s.length;r<i;r++)s[r]===t&&n.push(e[r]);return n}nzRowCols(t){const s=[];for(let e=0;e<this.n;e++)0!==this.at(t,e,!1)&&s.push(e);return s}transpose(){const t=u.empty(this.n,this.m),s=this.rows,e=this.data;for(let n=0;n<this.n;n++){const r=this.cols[n+1];for(let i=this.cols[n];i<r;i++)t.setAt(n,s[i],e[i])}return t}denseCol(t){const s=new Array(this.m).fill(0),e=this.cols[t+1],n=this.rows,r=this.data;for(let i=this.cols[t];i<e;i++)s[n[i]]=r[i];return s}denseRow(t){const s=new Array(this.n);for(let e=0;e<this.n;e++)s[e]=this.at(t,e,!1);return s}toDense(){let t=[];for(let s=0;s<this.n;s++)t=t.concat(this.denseCol(s));return t}toString(){const t=[];for(let s=0;s<this.m;s++)t.push(this.denseRow(s).join(" "));return t.join("\n")}}class c extends n{constructor(t,s,e,n,r){super(t,s),this.rows=n,this.cols=r,this.data=e}static fromDense(t,s,e){const[n,i,o]=r(t,s,e);return new c(t,s,o,n,i)}static empty(t,s=t){return new c(t,s,[],new Array(t+1).fill(0),[])}static identity(t){return c.diag(new Array(t).fill(1))}static diag(t){const[s,e]=i(t);return new c(t.length,t.length,t,s,e)}copy(){return new c(this.m,this.n,this.data.slice(),this.rows.slice(),this.cols.slice())}zero(){return this.data.length=this.cols.length=0,this.rows.fill(0),this}*nzEntries(){const t=this.rows,s=this.cols,e=this.data;for(let n=0;n<this.m;n++)for(let r=t[n],i=t[n+1];r<i;r++)yield[n,s[r],e[r]]}reshape(t,s=t){const e=this.data,n=this.rows,r=this.cols;if(t>this.m)for(let s=t-this.m,e=this.nnz();s>0;s--)n.push(e);else if(t<this.m){const s=n[t];e.length=s,r.length=s,n.length=t+1}if(this.m=t,s<this.n)for(let i=0;i<t;i++)for(let o=n[i],h=n[i+1];o<h;)r[o]>=s?(a(i,t,o,n,r,e),h--):o++;return this.n=s,this}extract(t,s,e,n){const r=t+e,i=s+n,o=this.rows,h=this.cols,l=this.data,a=[0],u=[],d=[];for(let e=t;e<r;e++){if(e<this.m){const t=o[e+1];for(let n=o[e];n<t;n++){const t=h[n];t>=s&&t<i&&(d.push(l[n]),u.push(t-s))}}a.push(d.length)}return new c(e,n,d,a,u)}at(t,s,e=!0){return e&&this.ensureIndex(t,s),o(t,s,this.rows,this.cols,this.data)}setAt(t,s,e,n=!0,r=!0){n&&this.ensureIndex(t,s);const i=h(t,s,this.m,e,this.rows,this.cols,this.data,r);return this.rows=i[0],this.cols=i[1],this.data=i[2],this}denseRow(t){const s=new Array(this.n).fill(0),e=this.cols,n=this.data;for(let r=this.rows[t],i=this.rows[t+1];r<i;r++)s[e[r]]=n[r];return s}denseCol(t){const s=new Array(this.m);for(let e=0;e<this.m;e++)s[e]=this.at(e,t,!1);return s}add(t){if(this.ensureSize(t),this===t)return this.mulN(2);if(0===t.nnz())return this.copy();const s=c.empty(this.m,this.n);for(let e=0;e<this.m;e++){const n=t.rows[e+1];t:for(let r=t.rows[e];r<n;r++){const n=t.cols[r],i=this.rows[e+1];for(let o=this.rows[e];o<i;o++)if(this.cols[o]===n){s.setAt(e,n,this.data[o]+t.data[r],!1);continue t}s.setAt(e,n,t.data[r],!1)}}return s}sub(t){this.ensureSize(t);const s=c.empty(this.m,this.n);if(this===t||0===t.nnz())return s;for(let e=0;e<this.m;e++){const n=t.rows[e+1];t:for(let r=t.rows[e];r<n;r++){const n=t.cols[r],i=this.rows[e+1];for(let o=this.rows[e];o<i;o++)if(this.cols[o]===n){s.setAt(e,n,this.data[o]-t.data[r],!1);continue t}s.setAt(e,n,-t.data[r],!1)}}return s}mul(t){s.assert(this.n===t.m,"incompatible matrix sizes");const e=c.empty(this.m,t.n);for(let s=0;s<t.n;s++)if(t.nnzCol(s)>0)for(let n=0;n<t.m;n++){const r=t.at(n,s);if(0!==r)for(let t=0;t<this.m;t++){const i=this.at(t,n,!1);0!==i&&e.setAt(t,s,e.at(t,s,!1)+i*r,!1)}}return e}mulV(t){s.assert(this.m===t.length,"vector length != "+this.m);const e=this.rows,n=this.cols,r=this.data,i=new Array(t.length).fill(0);for(let s=0;s<this.m;s++){const o=e[s+1];for(let h=e[s];h<o;h++)i[s]+=r[h]*t[n[h]]}return i}mulN(t){if(0===t)return c.empty(this.m,this.n);const s=this.copy(),e=s.data;for(let s=e.length;--s>=0;)e[s]*=t;return s}nnz(){return this.data.length}nnzRow(t){return this.rows[t+1]-this.rows[t]}nzRowCols(t){return this.cols.slice(this.rows[t],this.rows[t+1])}nzRowVals(t){return this.data.slice(this.rows[t],this.rows[t+1])}nnzCol(t){const s=this.cols;let e=0;for(let n=s.length-1;n>=0;n--)s[n]===t&&e++;return e}nzColVals(t){const s=this.cols,e=this.data,n=[];for(let r=0,i=s.length;r<i;r++)s[r]===t&&n.push(e[r]);return n}nzColRows(t){const s=[];for(let e=0;e<this.m;e++)0!==this.at(e,t,!1)&&s.push(e);return s}transpose(){const t=c.empty(this.n,this.m),s=this.cols,e=this.data;for(let n=0;n<this.m;n++){const r=this.rows[n+1];for(let i=this.rows[n];i<r;i++)t.setAt(s[i],n,e[i])}return t}toDense(){let t=[];for(let s=0;s<this.m;s++)t=t.concat(this.denseRow(s));return t}toString(){const t=this.toDense(),s=[];for(let e=0;e<this.m;e++)s.push(t.slice(e*this.n,(e+1)*this.n).join(" "));return s.join("\n")}}class d extends n{constructor(t,s=t,e){super(t,s),this.data=e||[]}static fromDense(t,s,e){const n=[];for(let r=0,i=0;r<t;r++)for(let t=0;t<s;t++,i++)0!==e[i]&&n.push(r,t,e[i]);return new d(t,s,n)}static identity(t){return d.diag(new Array(t).fill(1))}static diag(t){const s=[],e=t.length;for(let n=0;n<e;n++)s.push(n,n,t[n]);return new d(e,e,s)}nzEntries(){return e.partition(3,3,this.data)}at(t,s,e=!0){e&&this.ensureIndex(t,s);const n=this.data;for(let e=0,r=n.length;e<r&&n[e]<=t;e+=3)if(n[e]===t&&n[e+1]===s)return n[e+2];return 0}setAt(t,s,e,n=!0){n&&this.ensureIndex(t,s);const r=this.data;for(let n=0,i=r.length;n<i;n+=3){const i=r[n],o=r[n+1];if(t<i||t===i&&s<o)return 0!==e&&r.splice(n,0,t,s,e),this;if(t===i&&s===o)return 0!==e?r[n+2]=e:r.splice(n,3),this}return 0!==e&&r.push(t,s,e),this}mulV(t){const s=new Array(this.m).fill(0);for(let e=this.data,n=e.length-3;n>=0;n-=3)s[e[n]]+=e[n+2]*t[e[n+1]];return s}nnz(){return this.data.length/3}nnzCol(t){throw new Error("Method not implemented.")}nnzRow(t){throw new Error("Method not implemented.")}nzColRows(t){throw new Error("Method not implemented.")}nzColVals(t){throw new Error("Method not implemented.")}nzRowCols(t){throw new Error("Method not implemented.")}nzRowVals(t){throw new Error("Method not implemented.")}toDense(){const t=this.n,s=this.data,e=new Array(this.m*t).fill(0);for(let n=s.length-3;n>=0;n-=3)e[s[n]*t+s[n+1]]=s[n+2];return e}toCSC(){const t=[],s=[0],e=[],n=[...this.nzEntries()].sort((t,s)=>t[1]-s[1]);for(let r=0,i=0;r<n.length;r++){const o=n[r];o[1]!==i&&(i=o[1],s.push(t.length)),e.push(o[0]),t.push(o[2])}return s.push(t.length),new u(this.m,this.n,t,s,e)}toCSR(){const t=[],s=[0],e=[],n=this.data;for(let r=0,i=0;r<n.length;r+=3)n[r]!==i&&(i=n[r],s.push(t.length)),e.push(n[r+1]),t.push(n[r+2]);return s.push(t.length),new c(this.m,this.n,t,s,e)}}const f=(t,s)=>t+s,m=(t,s)=>t-s,w=(t,s)=>t*s,p=(t,s)=>t/s;class g{constructor(t,s){this.m=t,this.data=s||[]}static fromDense(t){const s=[],e=t.length;for(let n=0;n<e;n++){const e=t[n];0!==e&&s.push(n,e)}return new g(e,s)}copy(){return new g(this.m,this.data.slice())}get length(){return this.m}get nnz(){return this.data.length>>>1}*nzEntries(){const t=this.data;for(let s=0,e=t.length;s<e;s+=2)yield[t[s],0,t[s+1]]}at(t,s=!0){s&&this.ensureIndex(t);const e=this.data;for(let s=0,n=e.length;s<n&&e[s]<=t;s+=2)if(t===e[s])return e[s+1];return 0}setAt(t,s,e=!0){e&&this.ensureIndex(t);const n=this.data;for(let e=0,r=n.length;e<r;e+=2){if(t<n[e])return 0!==s&&n.splice(e,0,t,s),this;if(t===n[e])return 0!==s?n[e+1]=s:n.splice(e,2),this}return 0!==s&&n.push(t,s),this}binopN(t,s){const e=this.data,n=this.m,r=[];for(let i=0,o=0,h=e[o];i<n;i++){let n=t(i===h?(o+=2,h=e[o],e[o-1]):0,s);0!==n&&r.push(i,n)}return new g(this.m,r)}binop(t,s){this.ensureSize(s);const e=this.data,n=s.data,r=[];for(let s=0,i=0,o=e.length,h=n.length;s<o||i<h;){const o=e[s],h=n[i];if(o===h){const h=t(e[s+1],n[i+1]);0!==h&&r.push(o,h),s+=2,i+=2}else if(void 0===h||o<h){const n=t(e[s+1],0);0!==n&&r.push(o,n),s+=2}else{const s=t(0,n[i+1]);0!==s&&r.push(h,s),i+=2}}return new g(this.m,r)}add(t){return this.binop(f,t)}sub(t){return this.binop(m,t)}mul(t){return this.binop(w,t)}div(t){return this.binop(p,t)}addN(t){return this.binopN(f,t)}subN(t){return this.binopN(m,t)}mulN(t){const s=this.data,e=s.length,n=new Array(e);for(let r=0;r<e;r+=2)n[r]=s[r],n[r+1]=s[r+1]*t;return new g(this.m,n)}divN(t){const s=this.data,e=s.length,n=new Array(e);for(let r=0;r<e;r+=2)n[r]=s[r],n[r+1]=s[r+1]/t;return new g(this.m,n)}dot(t){this.ensureSize(t);const s=this.data,e=t.data;let n=0;for(let t=s.length-2,r=e.length-2;t>=0&&r>=0;){const i=s[t],o=e[r];i===o?(n+=s[t+1]*e[r+1],t-=2,r-=2):i>o?t-=2:r-=2}return n}magSquared(){const t=this.data;let s=0;for(let e=t.length-1;e>=1;e-=2)s+=t[e]*t[e];return s}mag(){return Math.sqrt(this.magSquared())}normalize(t=1){const s=this.magSquared();if(s>1e-9){t/=Math.sqrt(s);const e=this.data;for(let s=e.length-1;s>=1;s-=2)e[s]*=t}return this}toDense(){const t=new Array(this.m).fill(0),s=this.data;for(let e=s.length-2;e>=0;e-=2)t[s[e]]=s[e+1];return t}ensureIndex(t){s.assert(t>=0&&t<this.m,"index out of bounds: "+t)}ensureSize(t){s.assert(this.m===t.m,"wrong vector size: "+t.m)}}class z extends n{constructor(t){t instanceof g?(super(t.m,t.m),this.data=t):(super(t.length,t.length),this.data=g.fromDense(t))}static identity(t){return new z(new Array(t).fill(1))}*nzEntries(){for(let t of this.data.nzEntries())yield[t[0],t[0],t[2]]}at(t,s,e=!0){return e&&this.ensureIndex(t,s),t===s?this.data.at(t,!1):0}setAt(t,e,n,r=!0){return r&&s.assert(t===e&&t>=0&&t<this.m,`invalid index: ${t},${e}`),this.data.setAt(t,n,!1),this}nnz(){return this.data.length}nnzCol(t){return 0!==this.data.at(t)?1:0}nnzRow(t){return this.nnzCol(t)}nzColRows(t){return 0!==this.data.at(t)?[t]:[]}nzColVals(t){const s=this.data.at(t);return 0!==s?[s]:[]}nzRowCols(t){return this.nzColRows(t)}nzRowVals(t){return this.nzColVals(t)}toDense(){const t=this.n,s=this.data,e=new Array(t*t).fill(0);for(let n=0;n<t;n++)e[n*t+n]=s.at(n,!1);return e}toCSC(){return u.diag(this.data.toDense())}toCSR(){return c.diag(this.data.toDense())}}t.ASparseMatrix=n,t.COO=d,t.CSC=u,t.CSR=c,t.Diag=z,t.SparseVec=g,Object.defineProperty(t,"__esModule",{value:!0})}));
{
"name": "@thi.ng/sparse",
"version": "0.1.37",
"version": "0.1.38",
"description": "Sparse vector & matrix implementations",

@@ -36,14 +36,14 @@ "module": "./index.js",

"@istanbuljs/nyc-config-typescript": "^1.0.1",
"@microsoft/api-extractor": "^7.7.8",
"@types/mocha": "^7.0.1",
"@types/node": "^13.7.4",
"mocha": "^7.1.1",
"nyc": "^15.0.0",
"ts-node": "^8.6.2",
"typedoc": "^0.16.10",
"@microsoft/api-extractor": "^7.7.13",
"@types/mocha": "^7.0.2",
"@types/node": "^13.13.4",
"mocha": "^7.1.2",
"nyc": "^15.0.1",
"ts-node": "^8.9.1",
"typedoc": "^0.17.6",
"typescript": "^3.8.3"
},
"dependencies": {
"@thi.ng/api": "^6.10.3",
"@thi.ng/transducers": "^6.4.8",
"@thi.ng/api": "^6.10.4",
"@thi.ng/transducers": "^6.4.9",
"tslib": "^1.11.1"

@@ -74,3 +74,3 @@ },

},
"gitHead": "bb751118ba5a7261d322952a7ec5530860d73881"
"gitHead": "c11f130122cbaa05b8d48d394c3a164e238c8078"
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc