matrixjs
Version:
A JavaScript utility library for working with mathematical matrices
2 lines • 3.33 kB
JavaScript
/*! Matrix v0.0.0 | https://github.com/angusgibbs/matrix/blob/master/LICENSE */
;(function(){var a;function b(a){var c,d;if(a.rows===2&&a.cols===2)return a[0][0]*a[1][1]-a[0][1]*a[1][0];c=0;for(d=0;d<a.cols;d++)c+=a[0][d]*(d%2===0?1:-1)*b(a._submatrix(0,d));return c}function c(b){if(!a.silent)throw new Error(b)}a=function(a){var b=arguments;if(b.length===2&&typeof b[0]=="number"&&typeof b[1]=="number"){var d=b[0],e=b[1];a=[];for(var f=0;f<d;f++){a[f]=[];for(var g=0;g<e;g++)a[f][g]=0}}Array.isArray(a)||c("Array must be passed");var h=this;h.rows=a.length,a.forEach(function(a,b){Array.isArray(a)||c("The array passed must be a two-dimensional array"),h.cols||(h.cols=a.length),h[b]=[],a.forEach(function(a,d){typeof a!="number"&&c("The matrix fields must be numeric"),h[b][d]=a})})};a.silent=!1,a.prototype._submatrix=function(b,c){var e,f,g,h,d=[];for(e=0,f=0;e<this.rows;e++){if(e===b)continue;d[f]=[];for(g=0,h=0;g<this.cols;g++){if(g===c)continue;d[f][h]=this[e][g],h++}f++}return new a(d)},a.prototype.determinant=function(){return this.rows!==this.cols&&c("Cannot compute the determinant of a non-square matrix"),b(this)},a.prototype.multiply=function(b){var d,e,f,g,h;Array.isArray(b)&&(b=new a(b)),!b instanceof a&&c("Argument passed is not a valid Matrix object"),this.cols!==b.rows&&c("Invalid dimensions");d=new a(this.rows,b.cols);for(e=0;e<this.rows;e++)for(f=0;f<b.cols;f++){g=0;for(h=0;h<this.cols;h++)g+=this[e][h]*b[h][f];d[e][f]=g}return this._setData(d)},a.prototype.scalar=function(a){var b,d;typeof a!="number"&&c("The scalar must be numeric");for(b=0;b<this.rows;b++)for(d=0;d<this.cols;d++)this[b][d]*=a;return this},a.prototype.add=function(){var b=Array.prototype.slice.call(arguments,0),d=this;return b.forEach(function(b){var e,f;Array.isArray(b)&&(b=new a(b)),(d.rows!==b.rows||d.cols!==b.cols)&&c("Matrix dimensions do not match");for(e=0;e<d.rows;e++)for(f=0;f<d.cols;f++)d[e][f]+=b[e][f]}),this},a.prototype.subtract=function(b){var d,e;Array.isArray(b)&&(b=new a(b)),(this.rows!==b.rows||this.cols!==b.cols)&&c("Matrix dimensions do not match");for(d=0;d<this.rows;d++)for(e=0;e<this.cols;e++)this[d][e]-=b[d][e];return this},a.prototype.raise=function(a){var b;(typeof a!="number"||Math.round(a,0)!==a)&&c("The power must be an integer"),a<2&&c("The power must be greater than or equal to 1");b=this.clone();while(--a)b.multiply(this);return this._setData(b)},a.prototype.square=function(){return this.raise(2),this},a.prototype.cube=function(){return this.raise(3),this},a.prototype.inverse=function(){},a.prototype.clone=function(){return new a(this.toArray())},a.prototype.toJSON=a.prototype.toArray=function(){var b,c,a=[];for(b=0;b<this.rows;b++){a[b]=[];for(c=0;c<this.cols;c++)a[b][c]=this[b][c]}return a},a.prototype._setData=function(b){var d,e;for(d=0;d<this.rows;d++)delete this[d];Array.isArray(b)&&(b=new a(b)),!b instanceof a&&c("The data to set must be either a Matrix object or a two dimensional array"),this.rows=b.rows,this.cols=b.cols;for(d=0;d<this.rows;d++){this[d]=[];for(e=0;e<this.cols;e++)this[d][e]=b[d][e]}return this},a.identity=function(b){var d,e,c=[];for(d=0;d<b;d++){c[d]=[];for(e=0;e<b;e++)c[d][e]=d===e?1:0}return new a(c)},typeof define=="function"?define(function(){return a}):typeof module!="undefined"&&module.exports?module.exports=a:window.Matrix=a})()