UNPKG

sparse-matrix

Version:
2 lines (1 loc) 3.97 kB
var m=class extends Error{constructor(...a){super("Matrices must have valid dimensions");this.matrices=a}},w=class extends RangeError{constructor(a,r,o){super(`(${a}, ${r}) is out of range`);this.row=a,this.col=r,this.matrix=o}};function d(e){let t=[{row:e[0].col,col:e[0].row,value:e[0].value}],a=Array.from({length:e[0].col},()=>0),r=Array.from({length:e[0].col},()=>0);for(let o=1;o<=e[0].value;++o)a[e[o].col]++;r[0]=1;for(let o=1;o<e[0].col;++o)r[o]=r[o-1]+a[o-1];for(let o=1;o<=e[0].value;++o)t[r[e[o].col]++]={row:e[o].col,col:e[o].row,value:e[o].value};return t}function S(e,t){var l,u,s,f;if(((l=e[0])==null?void 0:l.col)!==((u=t[0])==null?void 0:u.col)||((s=e[0])==null?void 0:s.row)!==((f=t[0])==null?void 0:f.row))throw new m(e,t);let a=[{row:e[0].row,col:e[0].col,value:0}],[r,o,i]=[1,1,1];for(;r<=e[0].value&&o<=t[0].value;)if(e[r].row<t[o].row)a[i++]=e[r++];else if(e[r].row>t[o].row)a[i++]=t[o++];else if(e[r].col<t[o].col)a[i++]=e[r++];else if(e[r].col>t[o].col)a[i++]=t[o++];else{let h={row:e[r].row,col:e[r].col,value:e[r].value+t[o].value};h.value&&(a[i++]=h),r++,o++}for(;r<=e[0].value;)a[i++]=e[r++];for(;o<=t[0].value;)a[i++]=t[o++];return a[0].value=i-1,a}function M(e,t){var f,h,v,x;if(e[0].col!==t[0].row)throw new m(e,t);let a=[{row:e[0].row,col:t[0].col,value:0}],r=d(t),[o,i]=[1,1],[l,u]=[o,i],s=0;for(;o<=e[0].value;)if(e[o].col===r[i].col?(s+=e[o].value*r[i].value,++i):e[o].col<r[i].col?++o:e[o].col>r[i].col&&++i,((f=e[o])==null?void 0:f.row)!==e[l].row&&(o=l,++i),((h=r[i])==null?void 0:h.row)!==((v=r[u])==null?void 0:v.row)&&(s!==0&&(a.push({row:e[l].row,col:r[u].row,value:s}),++a[0].value,s=0),u=i,o=l),i>r[0].value){for(s!==0&&(a.push({row:e[l].row,col:r[u].row,value:s}),++a[0].value,s=0);((x=e[o])==null?void 0:x.row)===e[l].row;)++o;l=o,i=u=1}return a}function b(e,t,a){return e>=0&&t>=0&&e<a[0].row&&t<a[0].col}function g(e){if(e.length===0)throw new Error("Sparse matrix must have at least one term");if(e[0].row<0||e[0].col<0)throw new Error("Sparse matrix must have valid dimensions");if(e[0].value!==e.length-1)throw new Error("Sparse matrix must have valid size");let[t,a]=[0,0];for(let r=1;r<e.length;++r){if(e[r].row<t||e[r].row===t&&e[r].col<a)throw new Error("Sparse matrix must be sorted");if(b(e[r].row,e[r].col,e)===!1)throw new Error("Sparse matrix term must be in range");[t,a]=[e[r].row,e[r].col]}}var n=class{constructor(t){this.data=t,this.validate()}get row(){return this.data[0].row}get col(){return this.data[0].col}get size(){return this.data[0].value}transpose(){return new n(d(this.data))}add(t){return new n(S(this.data,t.data))}multiply(t){return new n(M(this.data,t.data))}map(t){for(let a=1;a<=this.size;++a){let r=this.data[a];this.data[a].value=t(r.value,r.row,r.col)}return this}set(t,a,r){if(b(t,a,this.data)===!1)throw new w(t,a,this.data);if(r!==0){let[o,i]=this.find(t,a);o?this.data[i].value=r:(this.data.splice(i,0,{row:t,col:a,value:r}),++this.data[0].value)}else{let[o,i]=this.find(t,a);o&&(this.data.splice(i,1),--this.data[0].value)}return this}get(t,a){if(b(t,a,this.data)===!1)throw new w(t,a,this.data);let[r,o]=this.find(t,a);return r?this.data[o].value:0}to2d(){let t=[];for(let a=0;a<this.row;++a){t.push([]);for(let r=0;r<this.col;++r)t[a].push(0)}for(let a=1;a<=this.size;++a){let{row:r,col:o,value:i}=this.data[a];t[r][o]=i}return t}validate(){return g(this.data),this}static empty(t,a){return new n([{row:t,col:a,value:0}])}static from2d(t){let a=[{row:t.length,col:t[0].length,value:0}];for(let r=0;r<t.length;++r)for(let o=0;o<t[r].length;++o)t[r][o]!==0&&(a.push({row:r,col:o,value:t[r][o]}),++a[0].value);return new n(a)}find(t,a){let[r,o]=[1,this.size];for(;r<=o;){let i=Math.floor((r+o)/2);if(this.data[i].row===t&&this.data[i].col===a)return[!0,i];this.data[i].row<t||this.data[i].row===t&&this.data[i].col<a?r=i+1:o=i-1}return[!1,r]}};export{m as DimensionError,n as Matrix,w as OutOfRangeError,S as add,b as in_range,M as multiply,d as transpose,g as validate_matrix};