UNPKG

sparse-matrix

Version:
2 lines (1 loc) 4.54 kB
"use strict";var b=Object.defineProperty;var _=Object.getOwnPropertyDescriptor;var E=Object.getOwnPropertyNames;var y=Object.prototype.hasOwnProperty;var T=(e,r)=>{for(var a in r)b(e,a,{get:r[a],enumerable:!0})},z=(e,r,a,t)=>{if(r&&typeof r=="object"||typeof r=="function")for(let o of E(r))!y.call(e,o)&&o!==a&&b(e,o,{get:()=>r[o],enumerable:!(t=_(r,o))||t.enumerable});return e};var j=e=>z(b({},"__esModule",{value:!0}),e);var C={};T(C,{DimensionError:()=>c,Matrix:()=>u,OutOfRangeError:()=>m,add:()=>v,in_range:()=>d,multiply:()=>x,transpose:()=>w,validate_matrix:()=>S});module.exports=j(C);var c=class extends Error{constructor(...a){super("Matrices must have valid dimensions");this.matrices=a}},m=class extends RangeError{constructor(a,t,o){super(`(${a}, ${t}) is out of range`);this.row=a,this.col=t,this.matrix=o}};function w(e){let r=[{row:e[0].col,col:e[0].row,value:e[0].value}],a=Array.from({length:e[0].col},()=>0),t=Array.from({length:e[0].col},()=>0);for(let o=1;o<=e[0].value;++o)a[e[o].col]++;t[0]=1;for(let o=1;o<e[0].col;++o)t[o]=t[o-1]+a[o-1];for(let o=1;o<=e[0].value;++o)r[t[e[o].col]++]={row:e[o].col,col:e[o].row,value:e[o].value};return r}function v(e,r){var l,n,s,f;if(((l=e[0])==null?void 0:l.col)!==((n=r[0])==null?void 0:n.col)||((s=e[0])==null?void 0:s.row)!==((f=r[0])==null?void 0:f.row))throw new c(e,r);let a=[{row:e[0].row,col:e[0].col,value:0}],[t,o,i]=[1,1,1];for(;t<=e[0].value&&o<=r[0].value;)if(e[t].row<r[o].row)a[i++]=e[t++];else if(e[t].row>r[o].row)a[i++]=r[o++];else if(e[t].col<r[o].col)a[i++]=e[t++];else if(e[t].col>r[o].col)a[i++]=r[o++];else{let h={row:e[t].row,col:e[t].col,value:e[t].value+r[o].value};h.value&&(a[i++]=h),t++,o++}for(;t<=e[0].value;)a[i++]=e[t++];for(;o<=r[0].value;)a[i++]=r[o++];return a[0].value=i-1,a}function x(e,r){var f,h,M,g;if(e[0].col!==r[0].row)throw new c(e,r);let a=[{row:e[0].row,col:r[0].col,value:0}],t=w(r),[o,i]=[1,1],[l,n]=[o,i],s=0;for(;o<=e[0].value;)if(e[o].col===t[i].col?(s+=e[o].value*t[i].value,++i):e[o].col<t[i].col?++o:e[o].col>t[i].col&&++i,((f=e[o])==null?void 0:f.row)!==e[l].row&&(o=l,++i),((h=t[i])==null?void 0:h.row)!==((M=t[n])==null?void 0:M.row)&&(s!==0&&(a.push({row:e[l].row,col:t[n].row,value:s}),++a[0].value,s=0),n=i,o=l),i>t[0].value){for(s!==0&&(a.push({row:e[l].row,col:t[n].row,value:s}),++a[0].value,s=0);((g=e[o])==null?void 0:g.row)===e[l].row;)++o;l=o,i=n=1}return a}function d(e,r,a){return e>=0&&r>=0&&e<a[0].row&&r<a[0].col}function S(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[r,a]=[0,0];for(let t=1;t<e.length;++t){if(e[t].row<r||e[t].row===r&&e[t].col<a)throw new Error("Sparse matrix must be sorted");if(d(e[t].row,e[t].col,e)===!1)throw new Error("Sparse matrix term must be in range");[r,a]=[e[t].row,e[t].col]}}var u=class{constructor(r){this.data=r,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 u(w(this.data))}add(r){return new u(v(this.data,r.data))}multiply(r){return new u(x(this.data,r.data))}map(r){for(let a=1;a<=this.size;++a){let t=this.data[a];this.data[a].value=r(t.value,t.row,t.col)}return this}set(r,a,t){if(d(r,a,this.data)===!1)throw new m(r,a,this.data);if(t!==0){let[o,i]=this.find(r,a);o?this.data[i].value=t:(this.data.splice(i,0,{row:r,col:a,value:t}),++this.data[0].value)}else{let[o,i]=this.find(r,a);o&&(this.data.splice(i,1),--this.data[0].value)}return this}get(r,a){if(d(r,a,this.data)===!1)throw new m(r,a,this.data);let[t,o]=this.find(r,a);return t?this.data[o].value:0}to2d(){let r=[];for(let a=0;a<this.row;++a){r.push([]);for(let t=0;t<this.col;++t)r[a].push(0)}for(let a=1;a<=this.size;++a){let{row:t,col:o,value:i}=this.data[a];r[t][o]=i}return r}validate(){return S(this.data),this}static empty(r,a){return new u([{row:r,col:a,value:0}])}static from2d(r){let a=[{row:r.length,col:r[0].length,value:0}];for(let t=0;t<r.length;++t)for(let o=0;o<r[t].length;++o)r[t][o]!==0&&(a.push({row:t,col:o,value:r[t][o]}),++a[0].value);return new u(a)}find(r,a){let[t,o]=[1,this.size];for(;t<=o;){let i=Math.floor((t+o)/2);if(this.data[i].row===r&&this.data[i].col===a)return[!0,i];this.data[i].row<r||this.data[i].row===r&&this.data[i].col<a?t=i+1:o=i-1}return[!1,t]}};0&&(module.exports={DimensionError,Matrix,OutOfRangeError,add,in_range,multiply,transpose,validate_matrix});