linear-algebra
Advanced tools
Comparing version
@@ -22,2 +22,3 @@ # Contribute to linear-algebra | ||
* Doesn't add unnessecary configuration options since they complicate future changes | ||
* Has updated the documentation for the project where necessary to document the new features and/or changes made | ||
@@ -142,2 +142,49 @@ (function (root, factory) { | ||
/** | ||
* Create a matrix of zeros. | ||
* @param {Integer} rows Number of rows. | ||
* @param {Integer} bols Number of bols. | ||
* @return {Matrix} | ||
*/ | ||
Matrix.zero = function(rows, cols) { | ||
var a = new Array(rows); | ||
for (var i=0; i<rows; ++i) { | ||
a[i] = new Array(cols); | ||
for (var j=0; j<cols; ++j) { | ||
a[i][j] = 0; | ||
} | ||
} | ||
return new Matrix(a); | ||
}; | ||
/** | ||
* Reshape array into matrix. | ||
* | ||
* @param {Array} values 1D array (vector) | ||
* @param {Number} rows Number of rows. | ||
* @param {Number} cols Number of cols. | ||
* | ||
* @return {Matrix} | ||
*/ | ||
Matrix.reshapeFrom = function(values, rows, cols) { | ||
if (values.length !== rows * cols) { | ||
_throwError('cannot reshape array of length ' + values.length + ' into ' + rows + 'x' + cols + ' matrix'); | ||
} | ||
var a = []; | ||
for (var i=0; i<values.length; i += cols) { | ||
a.push(values.slice(i, cols + i)); | ||
} | ||
return new Matrix(a); | ||
}; | ||
/** | ||
* Helpers to create vectors, i.e. matrices with a single row. | ||
@@ -280,3 +327,3 @@ */ | ||
var row, col, result = new Array(cols); | ||
var row, col, result = new Array(rows); | ||
@@ -335,3 +382,3 @@ for (row=0; row<rows; ++row) { | ||
var row, col, result = new Array(cols); | ||
var row, col, result = new Array(rows); | ||
@@ -390,3 +437,3 @@ for (row=0; row<rows; ++row) { | ||
var row, col, result = new Array(cols); | ||
var row, col, result = new Array(rows); | ||
@@ -445,3 +492,3 @@ for (row=0; row<rows; ++row) { | ||
var row, col, result = new Array(cols); | ||
var row, col, result = new Array(rows); | ||
@@ -448,0 +495,0 @@ for (row=0; row<rows; ++row) { |
@@ -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(){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.div=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("div",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.div_=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("div_",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.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},s.prototype.eleMap=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],t,o);return new s(e)},s.prototype.eleMap_=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],t,o);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(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,n=new Array(r);for(o=0;r>o;++o){for(n[o]=new Array(r),s=0;r>s;++s)n[o][s]=0;n[o][o]=t}return new a(n)},a.zero=function(r,t){for(var o=new Array(r),s=0;r>s;++s){o[s]=new Array(t);for(var n=0;t>n;++n)o[s][n]=0}return new a(o)},a.reshapeFrom=function(t,o,s){t.length!==o*s&&r("cannot reshape array of length "+t.length+" into "+o+"x"+s+" matrix");for(var n=[],i=0;i<t.length;i+=s)n.push(t.slice(i,s+i));return new a(n)};s.Vector={zero:function(r){for(var t=new Array(r),o=0;r>o;++o)t[o]=0;return new a(t)}};return o.add&&console.warn("linear-algebra: adder (options.add) will not be used in non-precision version"),a.prototype.trans=function(){var r,t,o=this.data,s=this.rows,n=this.cols,i=new Array(n);for(t=0;n>t;++t)for(i[t]=new Array(s),r=0;s>r;++r)i[t][r]=o[r][t];return new a(i)},a.prototype.trans_=function(){var r,t,o,s=this.data,a=this.rows,n=this.cols,i=n===a,e=n>a?a:n;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(!i){if(n>a)for(t=a;n>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=n;a>r;++r)for(t=0;n>t;++t)s[t][r]=s[r][t];o=a,this.rows=n,this.cols=o}return this},a.prototype.div=function(r){var o=this.data,s=this.rows,n=this.cols,i=r.data,e=r.rows,h=r.cols;(s!==e||n!==h)&&t("div",this,r);var f,c,u=new Array(s);for(f=0;s>f;++f)for(u[f]=new Array(n),c=0;n>c;++c)u[f][c]=o[f][c]/i[f][c];return new a(u)},a.prototype.div_=function(r){var o=this.data,s=this.rows,a=this.cols,n=r.data,i=r.rows,e=r.cols;(s!==i||a!==e)&&t("div_",this,r);var h,f;for(h=0;s>h;++h)for(f=0;a>f;++f)o[h][f]=o[h][f]/n[h][f];return this},a.prototype.mul=function(r){var o=this.data,s=this.rows,n=this.cols,i=r.data,e=r.rows,h=r.cols;(s!==e||n!==h)&&t("mul",this,r);var f,c,u=new Array(s);for(f=0;s>f;++f)for(u[f]=new Array(n),c=0;n>c;++c)u[f][c]=o[f][c]*i[f][c];return new a(u)},a.prototype.mul_=function(r){var o=this.data,s=this.rows,a=this.cols,n=r.data,i=r.rows,e=r.cols;(s!==i||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]*n[h][f];return this},a.prototype.plus=function(r){var o=this.data,s=this.rows,n=this.cols,i=r.data,e=r.rows,h=r.cols;(s!==e||n!==h)&&t("plus",this,r);var f,c,u=new Array(s);for(f=0;s>f;++f)for(u[f]=new Array(n),c=0;n>c;++c)u[f][c]=o[f][c]+i[f][c];return new a(u)},a.prototype.plus_=function(r){var o=this.data,s=this.rows,a=this.cols,n=r.data,i=r.rows,e=r.cols;(s!==i||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]+n[h][f];return this},a.prototype.minus=function(r){var o=this.data,s=this.rows,n=this.cols,i=r.data,e=r.rows,h=r.cols;(s!==e||n!==h)&&t("minus",this,r);var f,c,u=new Array(s);for(f=0;s>f;++f)for(u[f]=new Array(n),c=0;n>c;++c)u[f][c]=o[f][c]-i[f][c];return new a(u)},a.prototype.minus_=function(r){var o=this.data,s=this.rows,a=this.cols,n=r.data,i=r.rows,e=r.cols;(s!==i||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]-n[h][f];return this},a.prototype.dot=function(r){var o=this.data,s=this.rows,n=this.cols,i=r.data,e=r.rows,h=r.cols;n!==e&&t("dot",this,r);var f,c,u,w=new Array(s);for(f=0;s>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]*i[c][u];return new a(w)},a.prototype.dot_=function(r){var o=this.data,s=this.rows,a=this.cols,n=r.data,i=r.rows,e=r.cols;a!==i&&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;i>f;++f)o[h][c]+=u[f]*n[f][c];return this.cols=e,this},a.prototype.getSum=function(){for(var r=this.data,t=this.rows,o=this.cols,s=0,a=0;t>a;++a)for(var n=0;o>n;++n)s+=r[a][n];return s},a.prototype.map=function(r){var t,o,s=this.data,n=this.rows,i=this.cols,e=new Array(n);for(t=0;n>t;++t)for(e[t]=new Array(i),o=0;i>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,n=this.cols;for(t=0;a>t;++t)for(o=0;n>o;++o)s[t][o]=r(s[t][o]);return this},a.prototype.log=function(){var r,t,o=this.data,s=this.rows,n=this.cols,i=new Array(s);for(r=0;s>r;++r)for(i[r]=new Array(n),t=0;n>t;++t)i[r][t]=Math.log(o[r][t]);return new a(i)},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,n=this.cols,i=new Array(s);for(r=0;s>r;++r)for(i[r]=new Array(n),t=0;n>t;++t)i[r][t]=1/(1+Math.exp(-o[r][t]));return new a(i)},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,n=this.rows,i=this.cols,e=new Array(n);for(t=0;n>t;++t)for(e[t]=new Array(i),o=0;i>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,n=this.cols;for(t=0;a>t;++t)for(o=0;n>o;++o)s[t][o]=s[t][o]*r;return this},a.prototype.plusEach=function(r){var t,o,s=this.data,n=this.rows,i=this.cols,e=new Array(n);for(t=0;n>t;++t)for(e[t]=new Array(i),o=0;i>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,n=this.cols;for(t=0;a>t;++t)for(o=0;n>o;++o)s[t][o]=s[t][o]+r;return this},a.prototype.eleMap=function(r){var t,o,s=this.data,n=this.rows,i=this.cols,e=new Array(n);for(t=0;n>t;++t)for(e[t]=new Array(i),o=0;i>o;++o)e[t][o]=r(s[t][o],t,o);return new a(e)},a.prototype.eleMap_=function(r){var t,o,s=this.data,a=this.rows,n=this.cols;for(t=0;a>t;++t)for(o=0;n>o;++o)s[t][o]=r(s[t][o],t,o);return this},s}}); |
@@ -142,2 +142,49 @@ (function (root, factory) { | ||
/** | ||
* Create a matrix of zeros. | ||
* @param {Integer} rows Number of rows. | ||
* @param {Integer} bols Number of bols. | ||
* @return {Matrix} | ||
*/ | ||
Matrix.zero = function(rows, cols) { | ||
var a = new Array(rows); | ||
for (var i=0; i<rows; ++i) { | ||
a[i] = new Array(cols); | ||
for (var j=0; j<cols; ++j) { | ||
a[i][j] = 0; | ||
} | ||
} | ||
return new Matrix(a); | ||
}; | ||
/** | ||
* Reshape array into matrix. | ||
* | ||
* @param {Array} values 1D array (vector) | ||
* @param {Number} rows Number of rows. | ||
* @param {Number} cols Number of cols. | ||
* | ||
* @return {Matrix} | ||
*/ | ||
Matrix.reshapeFrom = function(values, rows, cols) { | ||
if (values.length !== rows * cols) { | ||
_throwError('cannot reshape array of length ' + values.length + ' into ' + rows + 'x' + cols + ' matrix'); | ||
} | ||
var a = []; | ||
for (var i=0; i<values.length; i += cols) { | ||
a.push(values.slice(i, cols + i)); | ||
} | ||
return new Matrix(a); | ||
}; | ||
/** | ||
* Helpers to create vectors, i.e. matrices with a single row. | ||
@@ -282,3 +329,3 @@ */ | ||
var row, col, result = new Array(cols); | ||
var row, col, result = new Array(rows); | ||
@@ -337,3 +384,3 @@ for (row=0; row<rows; ++row) { | ||
var row, col, result = new Array(cols); | ||
var row, col, result = new Array(rows); | ||
@@ -392,3 +439,3 @@ for (row=0; row<rows; ++row) { | ||
var row, col, result = new Array(cols); | ||
var row, col, result = new Array(rows); | ||
@@ -447,3 +494,3 @@ for (row=0; row<rows; ++row) { | ||
var row, col, result = new Array(cols); | ||
var row, col, result = new Array(rows); | ||
@@ -450,0 +497,0 @@ for (row=0; row<rows; ++row) { |
@@ -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(){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.div=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("div",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.div_=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("div_",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.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},a.prototype.eleMap=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],t,o);return new a(e)},a.prototype.eleMap_=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],t,o);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,n=new Array(r);for(o=0;r>o;++o){for(n[o]=new Array(r),s=0;r>s;++s)n[o][s]=0;n[o][o]=t}return new a(n)},a.zero=function(r,t){for(var o=new Array(r),s=0;r>s;++s){o[s]=new Array(t);for(var n=0;t>n;++n)o[s][n]=0}return new a(o)},a.reshapeFrom=function(t,o,s){t.length!==o*s&&r("cannot reshape array of length "+t.length+" into "+o+"x"+s+" matrix");for(var n=[],i=0;i<t.length;i+=s)n.push(t.slice(i,s+i));return new a(n)};var n=(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 n||r("options.add must be set for precision calculation"),a.prototype.trans=function(){var r,t,o=this.data,s=this.rows,n=this.cols,i=new Array(n);for(t=0;n>t;++t)for(i[t]=new Array(s),r=0;s>r;++r)i[t][r]=o[r][t];return new a(i)},a.prototype.trans_=function(){var r,t,o,s=this.data,a=this.rows,n=this.cols,i=n===a,e=n>a?a:n;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(!i){if(n>a)for(t=a;n>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=n;a>r;++r)for(t=0;n>t;++t)s[t][r]=s[r][t];o=a,this.rows=n,this.cols=o}return this},a.prototype.div=function(r){var o=this.data,s=this.rows,n=this.cols,i=r.data,e=r.rows,h=r.cols;(s!==e||n!==h)&&t("div",this,r);var f,c,u=new Array(s);for(f=0;s>f;++f)for(u[f]=new Array(n),c=0;n>c;++c)u[f][c]=o[f][c]/i[f][c];return new a(u)},a.prototype.div_=function(r){var o=this.data,s=this.rows,a=this.cols,n=r.data,i=r.rows,e=r.cols;(s!==i||a!==e)&&t("div_",this,r);var h,f;for(h=0;s>h;++h)for(f=0;a>f;++f)o[h][f]=o[h][f]/n[h][f];return this},a.prototype.mul=function(r){var o=this.data,s=this.rows,n=this.cols,i=r.data,e=r.rows,h=r.cols;(s!==e||n!==h)&&t("mul",this,r);var f,c,u=new Array(s);for(f=0;s>f;++f)for(u[f]=new Array(n),c=0;n>c;++c)u[f][c]=o[f][c]*i[f][c];return new a(u)},a.prototype.mul_=function(r){var o=this.data,s=this.rows,a=this.cols,n=r.data,i=r.rows,e=r.cols;(s!==i||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]*n[h][f];return this},a.prototype.plus=function(r){var o=this.data,s=this.rows,n=this.cols,i=r.data,e=r.rows,h=r.cols;(s!==e||n!==h)&&t("plus",this,r);var f,c,u=new Array(s);for(f=0;s>f;++f)for(u[f]=new Array(n),c=0;n>c;++c)u[f][c]=o[f][c]+i[f][c];return new a(u)},a.prototype.plus_=function(r){var o=this.data,s=this.rows,a=this.cols,n=r.data,i=r.rows,e=r.cols;(s!==i||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]+n[h][f];return this},a.prototype.minus=function(r){var o=this.data,s=this.rows,n=this.cols,i=r.data,e=r.rows,h=r.cols;(s!==e||n!==h)&&t("minus",this,r);var f,c,u=new Array(s);for(f=0;s>f;++f)for(u[f]=new Array(n),c=0;n>c;++c)u[f][c]=o[f][c]-i[f][c];return new a(u)},a.prototype.minus_=function(r){var o=this.data,s=this.rows,a=this.cols,n=r.data,i=r.rows,e=r.cols;(s!==i||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]-n[h][f];return this},a.prototype.dot=function(r){var o=this.data,s=this.rows,i=this.cols,e=r.data,h=r.rows,f=r.cols;i!==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]=n(l)}return new a(p)},a.prototype.dot_=function(r){var o=this.data,s=this.rows,a=this.cols,i=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]*i[c][u];o[f][u]=n(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,i=0;t>a;++a,i+=o)for(var e=0;o>e;++e)s[i+e]=r[a][e];return n(s)},a.prototype.map=function(r){var t,o,s=this.data,n=this.rows,i=this.cols,e=new Array(n);for(t=0;n>t;++t)for(e[t]=new Array(i),o=0;i>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,n=this.cols;for(t=0;a>t;++t)for(o=0;n>o;++o)s[t][o]=r(s[t][o]);return this},a.prototype.log=function(){var r,t,o=this.data,s=this.rows,n=this.cols,i=new Array(s);for(r=0;s>r;++r)for(i[r]=new Array(n),t=0;n>t;++t)i[r][t]=Math.log(o[r][t]);return new a(i)},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,n=this.cols,i=new Array(s);for(r=0;s>r;++r)for(i[r]=new Array(n),t=0;n>t;++t)i[r][t]=1/(1+Math.exp(-o[r][t]));return new a(i)},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,n=this.rows,i=this.cols,e=new Array(n);for(t=0;n>t;++t)for(e[t]=new Array(i),o=0;i>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,n=this.cols;for(t=0;a>t;++t)for(o=0;n>o;++o)s[t][o]=s[t][o]*r;return this},a.prototype.plusEach=function(r){var t,o,s=this.data,n=this.rows,i=this.cols,e=new Array(n);for(t=0;n>t;++t)for(e[t]=new Array(i),o=0;i>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,n=this.cols;for(t=0;a>t;++t)for(o=0;n>o;++o)s[t][o]=s[t][o]+r;return this},a.prototype.eleMap=function(r){var t,o,s=this.data,n=this.rows,i=this.cols,e=new Array(n);for(t=0;n>t;++t)for(e[t]=new Array(i),o=0;i>o;++o)e[t][o]=r(s[t][o],t,o);return new a(e)},a.prototype.eleMap_=function(r){var t,o,s=this.data,a=this.rows,n=this.cols;for(t=0;a>t;++t)for(o=0;n>o;++o)s[t][o]=r(s[t][o],t,o);return this},s}}); |
{ | ||
"name": "linear-algebra", | ||
"version": "3.1.2", | ||
"version": "3.1.3", | ||
"description": "Efficient, high-performance linear algebra library", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -88,2 +88,10 @@ # linear-algebra | ||
// zeros | ||
m = Matrix.zero(3, 2); | ||
console.log( m.data ); // [ [0, 0], [0, 0], [0, 0] ] | ||
// reshape from array | ||
m = Matrix.reshapeFrom([1, 2, 3, 4, 5, 6], 2, 3); | ||
console.log( m.data ); // [ [1, 2, 3,], [4, 5, 6] ] | ||
// vector (a 1-row matrix) | ||
@@ -161,3 +169,10 @@ m = Vector.zero(5); | ||
// any function with row and column passed-in | ||
m = new Matrix([ [1, 2], [3, 4], [5, 6] ]); | ||
m2 = m.eleMap(function(v, row, col) { | ||
return (0 === row && 1 === col ? v : v * 2 + 1); | ||
}); | ||
console.log(m2.data); // [ [1, 1], [7, 9], [11, 13] ] | ||
/* Calculations */ | ||
@@ -164,0 +179,0 @@ |
@@ -142,2 +142,49 @@ (function (root, factory) { | ||
/** | ||
* Create a matrix of zeros. | ||
* @param {Integer} rows Number of rows. | ||
* @param {Integer} bols Number of bols. | ||
* @return {Matrix} | ||
*/ | ||
Matrix.zero = function(rows, cols) { | ||
var a = new Array(rows); | ||
for (var i=0; i<rows; ++i) { | ||
a[i] = new Array(cols); | ||
for (var j=0; j<cols; ++j) { | ||
a[i][j] = 0; | ||
} | ||
} | ||
return new Matrix(a); | ||
}; | ||
/** | ||
* Reshape array into matrix. | ||
* | ||
* @param {Array} values 1D array (vector) | ||
* @param {Number} rows Number of rows. | ||
* @param {Number} cols Number of cols. | ||
* | ||
* @return {Matrix} | ||
*/ | ||
Matrix.reshapeFrom = function(values, rows, cols) { | ||
if (values.length !== rows * cols) { | ||
_throwError('cannot reshape array of length ' + values.length + ' into ' + rows + 'x' + cols + ' matrix'); | ||
} | ||
var a = []; | ||
for (var i=0; i<values.length; i += cols) { | ||
a.push(values.slice(i, cols + i)); | ||
} | ||
return new Matrix(a); | ||
}; | ||
/** | ||
* Helpers to create vectors, i.e. matrices with a single row. | ||
@@ -144,0 +191,0 @@ */ |
@@ -13,3 +13,3 @@ Matrix.prototype.NAME = function(op2) { | ||
var row, col, result = new Array(cols); | ||
var row, col, result = new Array(rows); | ||
@@ -16,0 +16,0 @@ for (row=0; row<rows; ++row) { |
@@ -106,2 +106,21 @@ var chai = require('chai'), | ||
}, | ||
'.zero': function() { | ||
var m = this.Matrix.zero(4, 3); | ||
m.should.be.instanceOf(this.Matrix); | ||
m.data.should.eql( [ [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0] ]); | ||
}, | ||
'.reshapeFrom': function() { | ||
var m = this.Matrix.reshapeFrom([1, 2, 5, 3, 4, 6, 5, 6, 7, 7, 8, 8], 4, 3); | ||
m.should.be.instanceOf(this.Matrix); | ||
m.data.should.eql([ [ 1, 2, 5 ], [ 3, 4, 6 ], [ 5, 6, 7 ], [ 7, 8, 8 ] ]); | ||
var that = this; | ||
expect(function() { | ||
that.Matrix.reshapeFrom([1, 2, 3], 2, 2) | ||
}).throws('linear-algebra: cannot reshape array of length 3 into 2x2 matrix'); | ||
} | ||
}; | ||
@@ -266,3 +285,3 @@ | ||
for (var j=0; j<3; ++j) { | ||
for (var j=0; j<4; ++j) { | ||
ret[i][j] = fnExpCalcFn(m.data[i][j], m2.data[i][j]); | ||
@@ -290,4 +309,4 @@ } | ||
'size match': function() { | ||
var m = new this.Matrix([ [1, 2, 5], [3, 4, 6], [5, 6, 7] ]); | ||
var m2 = new this.Matrix([ [1, 2, 5], [3, 4, 6], [0.5, 0.6, 0.7] ]); | ||
var m = new this.Matrix([ [1, 2, 5, 7], [3, 4, 6, 7], [5, 6, 7, 7] ]); | ||
var m2 = new this.Matrix([ [1, 2, 5, 7], [3, 4, 6, 7], [0.5, 0.6, 0.7, 0.7] ]); | ||
@@ -300,4 +319,6 @@ var m3 = m[fnName](m2); | ||
m3.data.should.eql(this.buildExpArr(m, m2)); | ||
m3.rows.should.eql(3); | ||
m3.cols.should.eql(4); | ||
m3.rows = 3; | ||
m3.cols = 3; | ||
m3.cols = 4; | ||
} | ||
@@ -320,5 +341,5 @@ }, | ||
'size match': function() { | ||
var m = new this.Matrix([ [1, 2, 5], [3, 4, 6], [5, 6, 7] ]); | ||
var m = new this.Matrix([ [1, 2, 5, 7], [3, 4, 6, 7], [5, 6, 7, 7] ]); | ||
var mCopy = m.clone(); | ||
var m2 = new this.Matrix([ [1, 2, 5], [3, 4, 6], [0.5, 0.6, 0.7] ]); | ||
var m2 = new this.Matrix([ [1, 2, 5, 7], [3, 4, 6, 7], [0.5, 0.6, 0.7, 0.7] ]); | ||
@@ -330,4 +351,6 @@ var m3 = m[fnName+'_'](m2); | ||
m3.data.should.eql(this.buildExpArr(mCopy, m2)); | ||
m3.rows.should.eql(3); | ||
m3.cols.should.eql(4); | ||
m3.rows = 3; | ||
m3.cols = 3; | ||
m3.cols = 4; | ||
} | ||
@@ -334,0 +357,0 @@ } |
102207
5.05%2580
5.01%297
5.32%