linear-algebra
Advanced tools
Comparing version
@@ -17,3 +17,3 @@ var linearAlgebra = require('../index'); | ||
*/ | ||
exports.buildMatrix = function(rows, cols, MatrixClass) { | ||
var buildMatrix = exports.buildMatrix = function(rows, cols, MatrixClass) { | ||
if (!MatrixClass) { | ||
@@ -23,2 +23,14 @@ MatrixClass = normalPrecision.Matrix; | ||
return new MatrixClass(buildArray(rows, cols)); | ||
}; | ||
/** | ||
* Build an array of random numbers | ||
* @return {MatrixClass} | ||
*/ | ||
var buildArray = exports.buildArray = function(rows, cols) { | ||
var a = new Array(rows); | ||
@@ -34,6 +46,4 @@ | ||
return new MatrixClass(a); | ||
return a; | ||
}; | ||
{ | ||
"name": "linear-algebra", | ||
"main": "dist/linear-algebra.js", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"authors": [ | ||
@@ -34,4 +34,6 @@ "Ramesh Nair <ram@hiddentao.com>" | ||
"test", | ||
"build", | ||
"benchmark", | ||
"gulpfile.js" | ||
] | ||
} |
@@ -75,2 +75,14 @@ (function (root, factory) { | ||
Matrix.prototype.clone = function() { | ||
return new Matrix(this.toArray()); | ||
}; | ||
/** | ||
* Get plain array version of this matrix. | ||
* | ||
* @return {Array} | ||
*/ | ||
Matrix.prototype.toArray = function() { | ||
var thisData = this.data, | ||
@@ -86,3 +98,3 @@ rows = this.rows, | ||
return new Matrix(a); | ||
return a; | ||
}; | ||
@@ -93,7 +105,2 @@ | ||
/** | ||
@@ -100,0 +107,0 @@ * Create an identity matrix of given dimensions. |
@@ -1,1 +0,1 @@ | ||
!function(r,t){"use strict";"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?module.exports=t():r.linearAlgebra=t()}(this,function(){"use strict";var r=function(r){throw new Error("linear-algebra: "+r)},t=function(t,o,s){r("["+t+"] op1 is "+o.rows+" x "+o.cols+" and op2 is "+s.rows+" x "+s.cols)};return function(r){r=r||{};var o={},s=o.Matrix=function(r){Array.isArray(r[0])?(this.data=r,this.rows=r.length,this.cols=r[0].length):(this.data=[r],this.rows=1,this.cols=r.length)};s.prototype.clone=function(){for(var r=this.data,t=this.rows,o=this.cols,a=new Array(t),i=0;t>i;++i)a[i]=r[i].slice(0,o);return new s(a)},s.identity=function(r){return s.scalar(r,1)},s.scalar=function(r,t){var o,a,i=new Array(r);for(o=0;r>o;++o){for(i[o]=new Array(r),a=0;r>a;++a)i[o][a]=0;i[o][o]=t}return new s(i)};o.Vector={zero:function(r){for(var t=new Array(r),o=0;r>o;++o)t[o]=0;return new s(t)}};return r.add&&console.warn("linear-algebra: adder (options.add) will not be used in non-precision version"),s.prototype.trans=function(){var r,t,o=this.data,a=this.rows,i=this.cols,n=new Array(i);for(t=0;i>t;++t)for(n[t]=new Array(a),r=0;a>r;++r)n[t][r]=o[r][t];return new s(n)},s.prototype.trans_=function(){var r,t,o,s=this.data,a=this.rows,i=this.cols,n=i===a,e=i>a?a:i;for(r=0;e>r;++r)for(t=r+1;e>t;++t)o=s[t][r],s[t][r]=s[r][t],s[r][t]=o;if(!n){if(i>a)for(t=a;i>t;++t)for(Array.isArray(s[t])||(s[t]=new Array(a)),r=0;a>r;++r)s[t][r]=s[r][t];else for(r=i;a>r;++r)for(t=0;i>t;++t)s[t][r]=s[r][t];o=a,this.rows=i,this.cols=o}return this},s.prototype.mul=function(r){var o=this.data,a=this.rows,i=this.cols,n=r.data,e=r.rows,h=r.cols;(a!==e||i!==h)&&t("mul",this,r);var f,c,u=new Array(i);for(f=0;a>f;++f)for(u[f]=new Array(i),c=0;i>c;++c)u[f][c]=o[f][c]*n[f][c];return new s(u)},s.prototype.mul_=function(r){var o=this.data,s=this.rows,a=this.cols,i=r.data,n=r.rows,e=r.cols;(s!==n||a!==e)&&t("mul_",this,r);var h,f;for(h=0;s>h;++h)for(f=0;a>f;++f)o[h][f]=o[h][f]*i[h][f];return this},s.prototype.plus=function(r){var o=this.data,a=this.rows,i=this.cols,n=r.data,e=r.rows,h=r.cols;(a!==e||i!==h)&&t("plus",this,r);var f,c,u=new Array(i);for(f=0;a>f;++f)for(u[f]=new Array(i),c=0;i>c;++c)u[f][c]=o[f][c]+n[f][c];return new s(u)},s.prototype.plus_=function(r){var o=this.data,s=this.rows,a=this.cols,i=r.data,n=r.rows,e=r.cols;(s!==n||a!==e)&&t("plus_",this,r);var h,f;for(h=0;s>h;++h)for(f=0;a>f;++f)o[h][f]=o[h][f]+i[h][f];return this},s.prototype.minus=function(r){var o=this.data,a=this.rows,i=this.cols,n=r.data,e=r.rows,h=r.cols;(a!==e||i!==h)&&t("minus",this,r);var f,c,u=new Array(i);for(f=0;a>f;++f)for(u[f]=new Array(i),c=0;i>c;++c)u[f][c]=o[f][c]-n[f][c];return new s(u)},s.prototype.minus_=function(r){var o=this.data,s=this.rows,a=this.cols,i=r.data,n=r.rows,e=r.cols;(s!==n||a!==e)&&t("minus_",this,r);var h,f;for(h=0;s>h;++h)for(f=0;a>f;++f)o[h][f]=o[h][f]-i[h][f];return this},s.prototype.dot=function(r){var o=this.data,a=this.rows,i=this.cols,n=r.data,e=r.rows,h=r.cols;i!==e&&t("dot",this,r);var f,c,u,w=new Array(a);for(f=0;a>f;++f)for(w[f]=new Array(h),u=0;h>u;++u)for(w[f][u]=0,c=0;e>c;++c)w[f][u]+=o[f][c]*n[c][u];return new s(w)},s.prototype.dot_=function(r){var o=this.data,s=this.rows,a=this.cols,i=r.data,n=r.rows,e=r.cols;a!==n&&t("dot_",this,r);var h,f,c,u;for(h=0;s>h;++h)for(u=o[h].slice(0,a),c=0;e>c;++c)for(o[h][c]=0,f=0;n>f;++f)o[h][c]+=u[f]*i[f][c];return this.cols=e,this},s.prototype.getSum=function(){for(var r=this.data,t=this.rows,o=this.cols,s=0,a=0;t>a;++a)for(var i=0;o>i;++i)s+=r[a][i];return s},s.prototype.map=function(r){var t,o,a=this.data,i=this.rows,n=this.cols,e=new Array(i);for(t=0;i>t;++t)for(e[t]=new Array(n),o=0;n>o;++o)e[t][o]=r(a[t][o]);return new s(e)},s.prototype.map_=function(r){var t,o,s=this.data,a=this.rows,i=this.cols;for(t=0;a>t;++t)for(o=0;i>o;++o)s[t][o]=r(s[t][o]);return this},s.prototype.log=function(){var r,t,o=this.data,a=this.rows,i=this.cols,n=new Array(a);for(r=0;a>r;++r)for(n[r]=new Array(i),t=0;i>t;++t)n[r][t]=Math.log(o[r][t]);return new s(n)},s.prototype.log_=function(){var r,t,o=this.data,s=this.rows,a=this.cols;for(r=0;s>r;++r)for(t=0;a>t;++t)o[r][t]=Math.log(o[r][t]);return this},s.prototype.sigmoid=function(){var r,t,o=this.data,a=this.rows,i=this.cols,n=new Array(a);for(r=0;a>r;++r)for(n[r]=new Array(i),t=0;i>t;++t)n[r][t]=1/(1+Math.exp(-o[r][t]));return new s(n)},s.prototype.sigmoid_=function(){var r,t,o=this.data,s=this.rows,a=this.cols;for(r=0;s>r;++r)for(t=0;a>t;++t)o[r][t]=1/(1+Math.exp(-o[r][t]));return this},s.prototype.mulEach=function(r){var t,o,a=this.data,i=this.rows,n=this.cols,e=new Array(i);for(t=0;i>t;++t)for(e[t]=new Array(n),o=0;n>o;++o)e[t][o]=a[t][o]*r;return new s(e)},s.prototype.mulEach_=function(r){var t,o,s=this.data,a=this.rows,i=this.cols;for(t=0;a>t;++t)for(o=0;i>o;++o)s[t][o]=s[t][o]*r;return this},s.prototype.plusEach=function(r){var t,o,a=this.data,i=this.rows,n=this.cols,e=new Array(i);for(t=0;i>t;++t)for(e[t]=new Array(n),o=0;n>o;++o)e[t][o]=a[t][o]+r;return new s(e)},s.prototype.plusEach_=function(r){var t,o,s=this.data,a=this.rows,i=this.cols;for(t=0;a>t;++t)for(o=0;i>o;++o)s[t][o]=s[t][o]+r;return this},o}}); | ||
!function(r,t){"use strict";"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?module.exports=t():r.linearAlgebra=t()}(this,function(){"use strict";var r=function(r){throw new Error("linear-algebra: "+r)},t=function(t,o,s){r("["+t+"] op1 is "+o.rows+" x "+o.cols+" and op2 is "+s.rows+" x "+s.cols)};return function(r){r=r||{};var o={},s=o.Matrix=function(r){Array.isArray(r[0])?(this.data=r,this.rows=r.length,this.cols=r[0].length):(this.data=[r],this.rows=1,this.cols=r.length)};s.prototype.clone=function(){return new s(this.toArray())},s.prototype.toArray=function(){for(var r=this.data,t=this.rows,o=this.cols,s=new Array(t),a=0;t>a;++a)s[a]=r[a].slice(0,o);return s},s.identity=function(r){return s.scalar(r,1)},s.scalar=function(r,t){var o,a,i=new Array(r);for(o=0;r>o;++o){for(i[o]=new Array(r),a=0;r>a;++a)i[o][a]=0;i[o][o]=t}return new s(i)};o.Vector={zero:function(r){for(var t=new Array(r),o=0;r>o;++o)t[o]=0;return new s(t)}};return r.add&&console.warn("linear-algebra: adder (options.add) will not be used in non-precision version"),s.prototype.trans=function(){var r,t,o=this.data,a=this.rows,i=this.cols,n=new Array(i);for(t=0;i>t;++t)for(n[t]=new Array(a),r=0;a>r;++r)n[t][r]=o[r][t];return new s(n)},s.prototype.trans_=function(){var r,t,o,s=this.data,a=this.rows,i=this.cols,n=i===a,e=i>a?a:i;for(r=0;e>r;++r)for(t=r+1;e>t;++t)o=s[t][r],s[t][r]=s[r][t],s[r][t]=o;if(!n){if(i>a)for(t=a;i>t;++t)for(Array.isArray(s[t])||(s[t]=new Array(a)),r=0;a>r;++r)s[t][r]=s[r][t];else for(r=i;a>r;++r)for(t=0;i>t;++t)s[t][r]=s[r][t];o=a,this.rows=i,this.cols=o}return this},s.prototype.mul=function(r){var o=this.data,a=this.rows,i=this.cols,n=r.data,e=r.rows,h=r.cols;(a!==e||i!==h)&&t("mul",this,r);var f,c,u=new Array(i);for(f=0;a>f;++f)for(u[f]=new Array(i),c=0;i>c;++c)u[f][c]=o[f][c]*n[f][c];return new s(u)},s.prototype.mul_=function(r){var o=this.data,s=this.rows,a=this.cols,i=r.data,n=r.rows,e=r.cols;(s!==n||a!==e)&&t("mul_",this,r);var h,f;for(h=0;s>h;++h)for(f=0;a>f;++f)o[h][f]=o[h][f]*i[h][f];return this},s.prototype.plus=function(r){var o=this.data,a=this.rows,i=this.cols,n=r.data,e=r.rows,h=r.cols;(a!==e||i!==h)&&t("plus",this,r);var f,c,u=new Array(i);for(f=0;a>f;++f)for(u[f]=new Array(i),c=0;i>c;++c)u[f][c]=o[f][c]+n[f][c];return new s(u)},s.prototype.plus_=function(r){var o=this.data,s=this.rows,a=this.cols,i=r.data,n=r.rows,e=r.cols;(s!==n||a!==e)&&t("plus_",this,r);var h,f;for(h=0;s>h;++h)for(f=0;a>f;++f)o[h][f]=o[h][f]+i[h][f];return this},s.prototype.minus=function(r){var o=this.data,a=this.rows,i=this.cols,n=r.data,e=r.rows,h=r.cols;(a!==e||i!==h)&&t("minus",this,r);var f,c,u=new Array(i);for(f=0;a>f;++f)for(u[f]=new Array(i),c=0;i>c;++c)u[f][c]=o[f][c]-n[f][c];return new s(u)},s.prototype.minus_=function(r){var o=this.data,s=this.rows,a=this.cols,i=r.data,n=r.rows,e=r.cols;(s!==n||a!==e)&&t("minus_",this,r);var h,f;for(h=0;s>h;++h)for(f=0;a>f;++f)o[h][f]=o[h][f]-i[h][f];return this},s.prototype.dot=function(r){var o=this.data,a=this.rows,i=this.cols,n=r.data,e=r.rows,h=r.cols;i!==e&&t("dot",this,r);var f,c,u,w=new Array(a);for(f=0;a>f;++f)for(w[f]=new Array(h),u=0;h>u;++u)for(w[f][u]=0,c=0;e>c;++c)w[f][u]+=o[f][c]*n[c][u];return new s(w)},s.prototype.dot_=function(r){var o=this.data,s=this.rows,a=this.cols,i=r.data,n=r.rows,e=r.cols;a!==n&&t("dot_",this,r);var h,f,c,u;for(h=0;s>h;++h)for(u=o[h].slice(0,a),c=0;e>c;++c)for(o[h][c]=0,f=0;n>f;++f)o[h][c]+=u[f]*i[f][c];return this.cols=e,this},s.prototype.getSum=function(){for(var r=this.data,t=this.rows,o=this.cols,s=0,a=0;t>a;++a)for(var i=0;o>i;++i)s+=r[a][i];return s},s.prototype.map=function(r){var t,o,a=this.data,i=this.rows,n=this.cols,e=new Array(i);for(t=0;i>t;++t)for(e[t]=new Array(n),o=0;n>o;++o)e[t][o]=r(a[t][o]);return new s(e)},s.prototype.map_=function(r){var t,o,s=this.data,a=this.rows,i=this.cols;for(t=0;a>t;++t)for(o=0;i>o;++o)s[t][o]=r(s[t][o]);return this},s.prototype.log=function(){var r,t,o=this.data,a=this.rows,i=this.cols,n=new Array(a);for(r=0;a>r;++r)for(n[r]=new Array(i),t=0;i>t;++t)n[r][t]=Math.log(o[r][t]);return new s(n)},s.prototype.log_=function(){var r,t,o=this.data,s=this.rows,a=this.cols;for(r=0;s>r;++r)for(t=0;a>t;++t)o[r][t]=Math.log(o[r][t]);return this},s.prototype.sigmoid=function(){var r,t,o=this.data,a=this.rows,i=this.cols,n=new Array(a);for(r=0;a>r;++r)for(n[r]=new Array(i),t=0;i>t;++t)n[r][t]=1/(1+Math.exp(-o[r][t]));return new s(n)},s.prototype.sigmoid_=function(){var r,t,o=this.data,s=this.rows,a=this.cols;for(r=0;s>r;++r)for(t=0;a>t;++t)o[r][t]=1/(1+Math.exp(-o[r][t]));return this},s.prototype.mulEach=function(r){var t,o,a=this.data,i=this.rows,n=this.cols,e=new Array(i);for(t=0;i>t;++t)for(e[t]=new Array(n),o=0;n>o;++o)e[t][o]=a[t][o]*r;return new s(e)},s.prototype.mulEach_=function(r){var t,o,s=this.data,a=this.rows,i=this.cols;for(t=0;a>t;++t)for(o=0;i>o;++o)s[t][o]=s[t][o]*r;return this},s.prototype.plusEach=function(r){var t,o,a=this.data,i=this.rows,n=this.cols,e=new Array(i);for(t=0;i>t;++t)for(e[t]=new Array(n),o=0;n>o;++o)e[t][o]=a[t][o]+r;return new s(e)},s.prototype.plusEach_=function(r){var t,o,s=this.data,a=this.rows,i=this.cols;for(t=0;a>t;++t)for(o=0;i>o;++o)s[t][o]=s[t][o]+r;return this},o}}); |
@@ -75,2 +75,14 @@ (function (root, factory) { | ||
Matrix.prototype.clone = function() { | ||
return new Matrix(this.toArray()); | ||
}; | ||
/** | ||
* Get plain array version of this matrix. | ||
* | ||
* @return {Array} | ||
*/ | ||
Matrix.prototype.toArray = function() { | ||
var thisData = this.data, | ||
@@ -86,3 +98,3 @@ rows = this.rows, | ||
return new Matrix(a); | ||
return a; | ||
}; | ||
@@ -93,7 +105,2 @@ | ||
/** | ||
@@ -100,0 +107,0 @@ * Create an identity matrix of given dimensions. |
@@ -1,1 +0,1 @@ | ||
!function(r,t){"use strict";"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?module.exports=t():r.linearAlgebra=t()}(this,function(){"use strict";var r=function(r){throw new Error("linear-algebra: "+r)},t=function(t,o,s){r("["+t+"] op1 is "+o.rows+" x "+o.cols+" and op2 is "+s.rows+" x "+s.cols)};return function(o){o=o||{};var s={},a=s.Matrix=function(r){Array.isArray(r[0])?(this.data=r,this.rows=r.length,this.cols=r[0].length):(this.data=[r],this.rows=1,this.cols=r.length)};a.prototype.clone=function(){for(var r=this.data,t=this.rows,o=this.cols,s=new Array(t),i=0;t>i;++i)s[i]=r[i].slice(0,o);return new a(s)},a.identity=function(r){return a.scalar(r,1)},a.scalar=function(r,t){var o,s,i=new Array(r);for(o=0;r>o;++o){for(i[o]=new Array(r),s=0;r>s;++s)i[o][s]=0;i[o][o]=t}return new a(i)};var i=(s.Vector={zero:function(r){for(var t=new Array(r),o=0;r>o;++o)t[o]=0;return new a(t)}},o.add);return i||r("options.add must be set for precision calculation"),a.prototype.trans=function(){var r,t,o=this.data,s=this.rows,i=this.cols,n=new Array(i);for(t=0;i>t;++t)for(n[t]=new Array(s),r=0;s>r;++r)n[t][r]=o[r][t];return new a(n)},a.prototype.trans_=function(){var r,t,o,s=this.data,a=this.rows,i=this.cols,n=i===a,e=i>a?a:i;for(r=0;e>r;++r)for(t=r+1;e>t;++t)o=s[t][r],s[t][r]=s[r][t],s[r][t]=o;if(!n){if(i>a)for(t=a;i>t;++t)for(Array.isArray(s[t])||(s[t]=new Array(a)),r=0;a>r;++r)s[t][r]=s[r][t];else for(r=i;a>r;++r)for(t=0;i>t;++t)s[t][r]=s[r][t];o=a,this.rows=i,this.cols=o}return this},a.prototype.mul=function(r){var o=this.data,s=this.rows,i=this.cols,n=r.data,e=r.rows,h=r.cols;(s!==e||i!==h)&&t("mul",this,r);var f,c,u=new Array(i);for(f=0;s>f;++f)for(u[f]=new Array(i),c=0;i>c;++c)u[f][c]=o[f][c]*n[f][c];return new a(u)},a.prototype.mul_=function(r){var o=this.data,s=this.rows,a=this.cols,i=r.data,n=r.rows,e=r.cols;(s!==n||a!==e)&&t("mul_",this,r);var h,f;for(h=0;s>h;++h)for(f=0;a>f;++f)o[h][f]=o[h][f]*i[h][f];return this},a.prototype.plus=function(r){var o=this.data,s=this.rows,i=this.cols,n=r.data,e=r.rows,h=r.cols;(s!==e||i!==h)&&t("plus",this,r);var f,c,u=new Array(i);for(f=0;s>f;++f)for(u[f]=new Array(i),c=0;i>c;++c)u[f][c]=o[f][c]+n[f][c];return new a(u)},a.prototype.plus_=function(r){var o=this.data,s=this.rows,a=this.cols,i=r.data,n=r.rows,e=r.cols;(s!==n||a!==e)&&t("plus_",this,r);var h,f;for(h=0;s>h;++h)for(f=0;a>f;++f)o[h][f]=o[h][f]+i[h][f];return this},a.prototype.minus=function(r){var o=this.data,s=this.rows,i=this.cols,n=r.data,e=r.rows,h=r.cols;(s!==e||i!==h)&&t("minus",this,r);var f,c,u=new Array(i);for(f=0;s>f;++f)for(u[f]=new Array(i),c=0;i>c;++c)u[f][c]=o[f][c]-n[f][c];return new a(u)},a.prototype.minus_=function(r){var o=this.data,s=this.rows,a=this.cols,i=r.data,n=r.rows,e=r.cols;(s!==n||a!==e)&&t("minus_",this,r);var h,f;for(h=0;s>h;++h)for(f=0;a>f;++f)o[h][f]=o[h][f]-i[h][f];return this},a.prototype.dot=function(r){var o=this.data,s=this.rows,n=this.cols,e=r.data,h=r.rows,f=r.cols;n!==h&&t("dot",this,r);var c,u,w,l,p=new Array(s);for(c=0;s>c;++c)for(p[c]=new Array(f),w=0;f>w;++w){for(l=new Array(h),u=0;h>u;++u)l[u]=o[c][u]*e[u][w];p[c][w]=i(l)}return new a(p)},a.prototype.dot_=function(r){var o=this.data,s=this.rows,a=this.cols,n=r.data,e=r.rows,h=r.cols;a!==e&&t("dot_",this,r);var f,c,u,w,l;for(f=0;s>f;++f)for(w=o[f].slice(0,a),u=0;h>u;++u){for(l=new Array(e),c=0;c<r.rows;++c)l[c]=w[c]*n[c][u];o[f][u]=i(l)}return this.cols=h,this},a.prototype.getSum=function(){for(var r=this.data,t=this.rows,o=this.cols,s=new Array(t*o),a=0,n=0;t>a;++a,n+=o)for(var e=0;o>e;++e)s[n+e]=r[a][e];return i(s)},a.prototype.map=function(r){var t,o,s=this.data,i=this.rows,n=this.cols,e=new Array(i);for(t=0;i>t;++t)for(e[t]=new Array(n),o=0;n>o;++o)e[t][o]=r(s[t][o]);return new a(e)},a.prototype.map_=function(r){var t,o,s=this.data,a=this.rows,i=this.cols;for(t=0;a>t;++t)for(o=0;i>o;++o)s[t][o]=r(s[t][o]);return this},a.prototype.log=function(){var r,t,o=this.data,s=this.rows,i=this.cols,n=new Array(s);for(r=0;s>r;++r)for(n[r]=new Array(i),t=0;i>t;++t)n[r][t]=Math.log(o[r][t]);return new a(n)},a.prototype.log_=function(){var r,t,o=this.data,s=this.rows,a=this.cols;for(r=0;s>r;++r)for(t=0;a>t;++t)o[r][t]=Math.log(o[r][t]);return this},a.prototype.sigmoid=function(){var r,t,o=this.data,s=this.rows,i=this.cols,n=new Array(s);for(r=0;s>r;++r)for(n[r]=new Array(i),t=0;i>t;++t)n[r][t]=1/(1+Math.exp(-o[r][t]));return new a(n)},a.prototype.sigmoid_=function(){var r,t,o=this.data,s=this.rows,a=this.cols;for(r=0;s>r;++r)for(t=0;a>t;++t)o[r][t]=1/(1+Math.exp(-o[r][t]));return this},a.prototype.mulEach=function(r){var t,o,s=this.data,i=this.rows,n=this.cols,e=new Array(i);for(t=0;i>t;++t)for(e[t]=new Array(n),o=0;n>o;++o)e[t][o]=s[t][o]*r;return new a(e)},a.prototype.mulEach_=function(r){var t,o,s=this.data,a=this.rows,i=this.cols;for(t=0;a>t;++t)for(o=0;i>o;++o)s[t][o]=s[t][o]*r;return this},a.prototype.plusEach=function(r){var t,o,s=this.data,i=this.rows,n=this.cols,e=new Array(i);for(t=0;i>t;++t)for(e[t]=new Array(n),o=0;n>o;++o)e[t][o]=s[t][o]+r;return new a(e)},a.prototype.plusEach_=function(r){var t,o,s=this.data,a=this.rows,i=this.cols;for(t=0;a>t;++t)for(o=0;i>o;++o)s[t][o]=s[t][o]+r;return this},s}}); | ||
!function(r,t){"use strict";"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?module.exports=t():r.linearAlgebra=t()}(this,function(){"use strict";var r=function(r){throw new Error("linear-algebra: "+r)},t=function(t,o,s){r("["+t+"] op1 is "+o.rows+" x "+o.cols+" and op2 is "+s.rows+" x "+s.cols)};return function(o){o=o||{};var s={},a=s.Matrix=function(r){Array.isArray(r[0])?(this.data=r,this.rows=r.length,this.cols=r[0].length):(this.data=[r],this.rows=1,this.cols=r.length)};a.prototype.clone=function(){return new a(this.toArray())},a.prototype.toArray=function(){for(var r=this.data,t=this.rows,o=this.cols,s=new Array(t),a=0;t>a;++a)s[a]=r[a].slice(0,o);return s},a.identity=function(r){return a.scalar(r,1)},a.scalar=function(r,t){var o,s,i=new Array(r);for(o=0;r>o;++o){for(i[o]=new Array(r),s=0;r>s;++s)i[o][s]=0;i[o][o]=t}return new a(i)};var i=(s.Vector={zero:function(r){for(var t=new Array(r),o=0;r>o;++o)t[o]=0;return new a(t)}},o.add);return i||r("options.add must be set for precision calculation"),a.prototype.trans=function(){var r,t,o=this.data,s=this.rows,i=this.cols,n=new Array(i);for(t=0;i>t;++t)for(n[t]=new Array(s),r=0;s>r;++r)n[t][r]=o[r][t];return new a(n)},a.prototype.trans_=function(){var r,t,o,s=this.data,a=this.rows,i=this.cols,n=i===a,e=i>a?a:i;for(r=0;e>r;++r)for(t=r+1;e>t;++t)o=s[t][r],s[t][r]=s[r][t],s[r][t]=o;if(!n){if(i>a)for(t=a;i>t;++t)for(Array.isArray(s[t])||(s[t]=new Array(a)),r=0;a>r;++r)s[t][r]=s[r][t];else for(r=i;a>r;++r)for(t=0;i>t;++t)s[t][r]=s[r][t];o=a,this.rows=i,this.cols=o}return this},a.prototype.mul=function(r){var o=this.data,s=this.rows,i=this.cols,n=r.data,e=r.rows,h=r.cols;(s!==e||i!==h)&&t("mul",this,r);var f,c,u=new Array(i);for(f=0;s>f;++f)for(u[f]=new Array(i),c=0;i>c;++c)u[f][c]=o[f][c]*n[f][c];return new a(u)},a.prototype.mul_=function(r){var o=this.data,s=this.rows,a=this.cols,i=r.data,n=r.rows,e=r.cols;(s!==n||a!==e)&&t("mul_",this,r);var h,f;for(h=0;s>h;++h)for(f=0;a>f;++f)o[h][f]=o[h][f]*i[h][f];return this},a.prototype.plus=function(r){var o=this.data,s=this.rows,i=this.cols,n=r.data,e=r.rows,h=r.cols;(s!==e||i!==h)&&t("plus",this,r);var f,c,u=new Array(i);for(f=0;s>f;++f)for(u[f]=new Array(i),c=0;i>c;++c)u[f][c]=o[f][c]+n[f][c];return new a(u)},a.prototype.plus_=function(r){var o=this.data,s=this.rows,a=this.cols,i=r.data,n=r.rows,e=r.cols;(s!==n||a!==e)&&t("plus_",this,r);var h,f;for(h=0;s>h;++h)for(f=0;a>f;++f)o[h][f]=o[h][f]+i[h][f];return this},a.prototype.minus=function(r){var o=this.data,s=this.rows,i=this.cols,n=r.data,e=r.rows,h=r.cols;(s!==e||i!==h)&&t("minus",this,r);var f,c,u=new Array(i);for(f=0;s>f;++f)for(u[f]=new Array(i),c=0;i>c;++c)u[f][c]=o[f][c]-n[f][c];return new a(u)},a.prototype.minus_=function(r){var o=this.data,s=this.rows,a=this.cols,i=r.data,n=r.rows,e=r.cols;(s!==n||a!==e)&&t("minus_",this,r);var h,f;for(h=0;s>h;++h)for(f=0;a>f;++f)o[h][f]=o[h][f]-i[h][f];return this},a.prototype.dot=function(r){var o=this.data,s=this.rows,n=this.cols,e=r.data,h=r.rows,f=r.cols;n!==h&&t("dot",this,r);var c,u,w,l,p=new Array(s);for(c=0;s>c;++c)for(p[c]=new Array(f),w=0;f>w;++w){for(l=new Array(h),u=0;h>u;++u)l[u]=o[c][u]*e[u][w];p[c][w]=i(l)}return new a(p)},a.prototype.dot_=function(r){var o=this.data,s=this.rows,a=this.cols,n=r.data,e=r.rows,h=r.cols;a!==e&&t("dot_",this,r);var f,c,u,w,l;for(f=0;s>f;++f)for(w=o[f].slice(0,a),u=0;h>u;++u){for(l=new Array(e),c=0;c<r.rows;++c)l[c]=w[c]*n[c][u];o[f][u]=i(l)}return this.cols=h,this},a.prototype.getSum=function(){for(var r=this.data,t=this.rows,o=this.cols,s=new Array(t*o),a=0,n=0;t>a;++a,n+=o)for(var e=0;o>e;++e)s[n+e]=r[a][e];return i(s)},a.prototype.map=function(r){var t,o,s=this.data,i=this.rows,n=this.cols,e=new Array(i);for(t=0;i>t;++t)for(e[t]=new Array(n),o=0;n>o;++o)e[t][o]=r(s[t][o]);return new a(e)},a.prototype.map_=function(r){var t,o,s=this.data,a=this.rows,i=this.cols;for(t=0;a>t;++t)for(o=0;i>o;++o)s[t][o]=r(s[t][o]);return this},a.prototype.log=function(){var r,t,o=this.data,s=this.rows,i=this.cols,n=new Array(s);for(r=0;s>r;++r)for(n[r]=new Array(i),t=0;i>t;++t)n[r][t]=Math.log(o[r][t]);return new a(n)},a.prototype.log_=function(){var r,t,o=this.data,s=this.rows,a=this.cols;for(r=0;s>r;++r)for(t=0;a>t;++t)o[r][t]=Math.log(o[r][t]);return this},a.prototype.sigmoid=function(){var r,t,o=this.data,s=this.rows,i=this.cols,n=new Array(s);for(r=0;s>r;++r)for(n[r]=new Array(i),t=0;i>t;++t)n[r][t]=1/(1+Math.exp(-o[r][t]));return new a(n)},a.prototype.sigmoid_=function(){var r,t,o=this.data,s=this.rows,a=this.cols;for(r=0;s>r;++r)for(t=0;a>t;++t)o[r][t]=1/(1+Math.exp(-o[r][t]));return this},a.prototype.mulEach=function(r){var t,o,s=this.data,i=this.rows,n=this.cols,e=new Array(i);for(t=0;i>t;++t)for(e[t]=new Array(n),o=0;n>o;++o)e[t][o]=s[t][o]*r;return new a(e)},a.prototype.mulEach_=function(r){var t,o,s=this.data,a=this.rows,i=this.cols;for(t=0;a>t;++t)for(o=0;i>o;++o)s[t][o]=s[t][o]*r;return this},a.prototype.plusEach=function(r){var t,o,s=this.data,i=this.rows,n=this.cols,e=new Array(i);for(t=0;i>t;++t)for(e[t]=new Array(n),o=0;n>o;++o)e[t][o]=s[t][o]+r;return new a(e)},a.prototype.plusEach_=function(r){var t,o,s=this.data,a=this.rows,i=this.cols;for(t=0;a>t;++t)for(o=0;i>o;++o)s[t][o]=s[t][o]+r;return this},s}}); |
{ | ||
"name": "linear-algebra", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "Efficient, high-performance linear algebra library", | ||
@@ -46,4 +46,6 @@ "main": "index.js", | ||
"gulp-replace": "~0.4.0", | ||
"coffee-script": "~1.7.1" | ||
"coffee-script": "~1.7.1", | ||
"sylvester": "0.0.21", | ||
"linalg": "~0.3.2" | ||
} | ||
} |
@@ -11,3 +11,3 @@ # linear-algebra | ||
* Simple, expressive API. | ||
* Simple, expressive, chainable API. | ||
* Array implementation with [performance optimizations](#performance). | ||
@@ -37,2 +37,6 @@ * Enhanced [floating point precision](#higher-precision) if needed. | ||
Since linear algebra calculations tend to be CPU-intensive it is highly recommended that you run them within a separate thread or process. For browsers this means using a [web worker](https://en.wikipedia.org/wiki/Web_worker). For node.js there are plenty of [similar solutions](https://www.npmjs.org/search?q=webworker) available. | ||
### Initialisation | ||
The examples below assume you are running in node.js. The library needs to be initialised once loaded: | ||
@@ -58,7 +62,7 @@ | ||
// default | ||
var m2 = m.mul(5); // multiply every element by 5 | ||
var m2 = m.mulEach(5); // multiply every element by 5 | ||
m2 === m1; // false | ||
// in-place | ||
var m2 = m.mul_(5); // notice the _ suffix | ||
var m2 = m.mulEach_(5); // notice the _ suffix | ||
m2 === m1; // true | ||
@@ -163,2 +167,13 @@ ``` | ||
/* Other methods */ | ||
// cloning | ||
m = new Matrix([ [1, 2], [3, 4], [5, 6] ]); | ||
m2 = m.clone(); | ||
console.log( m2.data ); // [ [1, 2], [3, 4], [5, 6] ] | ||
// to plain array | ||
m = new Matrix([ [1, 2], [3, 4], [5, 6] ]); | ||
m2 = m.toArray(); | ||
console.log( m2 ); // [ [1, 2], [3, 4], [5, 6] ] | ||
``` | ||
@@ -202,2 +217,11 @@ | ||
Performance vs. similar modules: | ||
```bash | ||
[17:23:14] Running suite vs. other modules [/Users/home/dev/js/linear-algebra/benchmark/vs-other-modules.perf.js]... | ||
[17:23:20] Matrix dot-product (100x100) - linear-algebra x 288 ops/sec ±1.21% (88 runs sampled) | ||
[17:23:25] Matrix dot-product (100x100) - sylvester x 56.77 ops/sec ±4.51% (61 runs sampled) | ||
[17:23:25] Fastest test is Matrix dot-product (100x100) - linear-algebra at 5.1x faster than Matrix dot-product (100x100) - sylvester | ||
``` | ||
To run the performance benchmarks: | ||
@@ -211,6 +235,19 @@ | ||
As mentioned earlier, matrix operations which result in a new matrix are implemented as two methods - a default method which returns a new `Matrix` instance and an _in-place_ method which causes the original to be overwritten. | ||
Matrix operations which result in a new matrix are implemented as two methods - a default method which returns a new `Matrix` instance and an _in-place_ method which causes the original to be overwritten. | ||
The _in-place_ versions are provided because in general, overwriting an existing array is [twice as fast](http://jsperf.com/create-new-array-vs-overwrite-existing) as creating a new one. However, this may not be true for all the matrix operations contained in this library. | ||
The _in-place_ versions are provided because - general speaking- memory allocations and garbage collection are expensive operations you don't want happening when you're performing lots of calculations. Overwriting an existing array is [twice as fast](http://jsperf.com/create-new-array-vs-overwrite-existing) as creating a new one. And since changing the size of an array is also an expensive operation, even if a matrix operation results in a smaller matrix than before the internal array is kept at the same size: | ||
```js | ||
var m = new Matrix([ [1, 2, 3], [4, 5, 6] ]); | ||
var m2 = new Matrix([ [7], [8], [9] ]); | ||
m.dot_(m2); | ||
console.log( m.data ); // [ [43, 2, 3], [112, 5, 6] ] | ||
console.log( m.rows ); // 2 | ||
console.log( m.cols ); // 1 | ||
``` | ||
The _in-place_ versions attempt to limit memory allocations as much as possible and therefore ought to be faster. However, this may not be true for all the matrix operations contained in this library. | ||
If you're dealing with large matrices (>100 rows, columns) then you're more likely to see a benefit from using the _in-place_ versions of methods: | ||
@@ -217,0 +254,0 @@ |
@@ -75,2 +75,14 @@ (function (root, factory) { | ||
Matrix.prototype.clone = function() { | ||
return new Matrix(this.toArray()); | ||
}; | ||
/** | ||
* Get plain array version of this matrix. | ||
* | ||
* @return {Array} | ||
*/ | ||
Matrix.prototype.toArray = function() { | ||
var thisData = this.data, | ||
@@ -86,3 +98,3 @@ rows = this.rows, | ||
return new Matrix(a); | ||
return a; | ||
}; | ||
@@ -93,7 +105,2 @@ | ||
/** | ||
@@ -100,0 +107,0 @@ * Create an identity matrix of given dimensions. |
@@ -38,2 +38,27 @@ var chai = require('chai'), | ||
'toArray': { | ||
'deep copy': function() { | ||
var a = [ [1, 2], [3, 4], [5, 6] ]; | ||
var m = new this.Matrix(a); | ||
var c = m.toArray(); | ||
(c === m.data).should.not.be.true; | ||
c.should.eql(m.data); | ||
a[0][1] = 5; | ||
c[0][1].should.eql(2); | ||
}, | ||
'only what is valid': function() { | ||
var a = [ [1, 2, 5], [3, 4, 6], [5, 6, 7] ]; | ||
var m = new this.Matrix(a); | ||
// artificially limit | ||
m.cols = 2; | ||
m.rows = 2; | ||
var c = m.toArray(); | ||
c.should.eql([ [1, 2], [3, 4] ]); | ||
} | ||
}, | ||
'clone': { | ||
@@ -45,6 +70,7 @@ 'deep copy': function() { | ||
var c = m.clone(); | ||
c.data.should.eql(a); | ||
c.rows.should.eql(3); | ||
c.cols.should.eql(2); | ||
(c.data === m.data).should.not.be.true; | ||
c.data.should.eql(m.data); | ||
m.data[0][1] = 5; | ||
@@ -51,0 +77,0 @@ c.data[0][1].should.eql(2); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
86841
5.12%32
3.23%2209
3.9%278
15.35%0
-100%16
14.29%