linear-algebra
Advanced tools
Comparing version
@@ -831,4 +831,51 @@ (function (root, factory) { | ||
/** | ||
* Apply function with row and column parameters to all elements in matrix | ||
* | ||
* Used to apply different transformations depending on placement in matrix. | ||
*/ | ||
Matrix.prototype.eleMap = function(transformFn) { | ||
var thisData = this.data, | ||
rows = this.rows, | ||
cols = this.cols; | ||
var row, col, result = new Array(rows); | ||
for (row=0; row<rows; ++row) { | ||
result[row] = new Array(cols); | ||
for (col=0; col<cols; ++col) { | ||
result[row][col] = transformFn(thisData[row][col], row, col); | ||
} | ||
} | ||
return new Matrix(result); | ||
}; | ||
Matrix.prototype.eleMap_ = function(transformFn) { | ||
var thisData = this.data, | ||
rows = this.rows, | ||
cols = this.cols; | ||
var row, col; | ||
for (row=0; row<rows; ++row) { | ||
for (col=0; col<cols; ++col) { | ||
thisData[row][col] = transformFn(thisData[row][col], row, col); | ||
} | ||
} | ||
return this; | ||
}; | ||
return LinAlg; | ||
@@ -835,0 +882,0 @@ }; |
@@ -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),i=0;t>i;++i)s[i]=r[i].slice(0,o);return s},s.identity=function(r){return s.scalar(r,1)},s.scalar=function(r,t){var o,i,a=new Array(r);for(o=0;r>o;++o){for(a[o]=new Array(r),i=0;r>i;++i)a[o][i]=0;a[o][o]=t}return new s(a)};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,i=this.rows,a=this.cols,n=new Array(a);for(t=0;a>t;++t)for(n[t]=new Array(i),r=0;i>r;++r)n[t][r]=o[r][t];return new s(n)},s.prototype.trans_=function(){var r,t,o,s=this.data,i=this.rows,a=this.cols,n=a===i,e=a>i?i:a;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(a>i)for(t=i;a>t;++t)for(Array.isArray(s[t])||(s[t]=new Array(i)),r=0;i>r;++r)s[t][r]=s[r][t];else for(r=a;i>r;++r)for(t=0;a>t;++t)s[t][r]=s[r][t];o=i,this.rows=a,this.cols=o}return this},s.prototype.div=function(r){var o=this.data,i=this.rows,a=this.cols,n=r.data,e=r.rows,h=r.cols;(i!==e||a!==h)&&t("div",this,r);var f,c,u=new Array(a);for(f=0;i>f;++f)for(u[f]=new Array(a),c=0;a>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,i=this.cols,a=r.data,n=r.rows,e=r.cols;(s!==n||i!==e)&&t("div_",this,r);var h,f;for(h=0;s>h;++h)for(f=0;i>f;++f)o[h][f]=o[h][f]/a[h][f];return this},s.prototype.mul=function(r){var o=this.data,i=this.rows,a=this.cols,n=r.data,e=r.rows,h=r.cols;(i!==e||a!==h)&&t("mul",this,r);var f,c,u=new Array(a);for(f=0;i>f;++f)for(u[f]=new Array(a),c=0;a>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,i=this.cols,a=r.data,n=r.rows,e=r.cols;(s!==n||i!==e)&&t("mul_",this,r);var h,f;for(h=0;s>h;++h)for(f=0;i>f;++f)o[h][f]=o[h][f]*a[h][f];return this},s.prototype.plus=function(r){var o=this.data,i=this.rows,a=this.cols,n=r.data,e=r.rows,h=r.cols;(i!==e||a!==h)&&t("plus",this,r);var f,c,u=new Array(a);for(f=0;i>f;++f)for(u[f]=new Array(a),c=0;a>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,i=this.cols,a=r.data,n=r.rows,e=r.cols;(s!==n||i!==e)&&t("plus_",this,r);var h,f;for(h=0;s>h;++h)for(f=0;i>f;++f)o[h][f]=o[h][f]+a[h][f];return this},s.prototype.minus=function(r){var o=this.data,i=this.rows,a=this.cols,n=r.data,e=r.rows,h=r.cols;(i!==e||a!==h)&&t("minus",this,r);var f,c,u=new Array(a);for(f=0;i>f;++f)for(u[f]=new Array(a),c=0;a>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,i=this.cols,a=r.data,n=r.rows,e=r.cols;(s!==n||i!==e)&&t("minus_",this,r);var h,f;for(h=0;s>h;++h)for(f=0;i>f;++f)o[h][f]=o[h][f]-a[h][f];return this},s.prototype.dot=function(r){var o=this.data,i=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=new Array(i);for(f=0;i>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,i=this.cols,a=r.data,n=r.rows,e=r.cols;i!==n&&t("dot_",this,r);var h,f,c,u;for(h=0;s>h;++h)for(u=o[h].slice(0,i),c=0;e>c;++c)for(o[h][c]=0,f=0;n>f;++f)o[h][c]+=u[f]*a[f][c];return this.cols=e,this},s.prototype.getSum=function(){for(var r=this.data,t=this.rows,o=this.cols,s=0,i=0;t>i;++i)for(var a=0;o>a;++a)s+=r[i][a];return s},s.prototype.map=function(r){var t,o,i=this.data,a=this.rows,n=this.cols,e=new Array(a);for(t=0;a>t;++t)for(e[t]=new Array(n),o=0;n>o;++o)e[t][o]=r(i[t][o]);return new s(e)},s.prototype.map_=function(r){var t,o,s=this.data,i=this.rows,a=this.cols;for(t=0;i>t;++t)for(o=0;a>o;++o)s[t][o]=r(s[t][o]);return this},s.prototype.log=function(){var r,t,o=this.data,i=this.rows,a=this.cols,n=new Array(i);for(r=0;i>r;++r)for(n[r]=new Array(a),t=0;a>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,i=this.cols;for(r=0;s>r;++r)for(t=0;i>t;++t)o[r][t]=Math.log(o[r][t]);return this},s.prototype.sigmoid=function(){var r,t,o=this.data,i=this.rows,a=this.cols,n=new Array(i);for(r=0;i>r;++r)for(n[r]=new Array(a),t=0;a>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,i=this.cols;for(r=0;s>r;++r)for(t=0;i>t;++t)o[r][t]=1/(1+Math.exp(-o[r][t]));return this},s.prototype.mulEach=function(r){var t,o,i=this.data,a=this.rows,n=this.cols,e=new Array(a);for(t=0;a>t;++t)for(e[t]=new Array(n),o=0;n>o;++o)e[t][o]=i[t][o]*r;return new s(e)},s.prototype.mulEach_=function(r){var t,o,s=this.data,i=this.rows,a=this.cols;for(t=0;i>t;++t)for(o=0;a>o;++o)s[t][o]=s[t][o]*r;return this},s.prototype.plusEach=function(r){var t,o,i=this.data,a=this.rows,n=this.cols,e=new Array(a);for(t=0;a>t;++t)for(e[t]=new Array(n),o=0;n>o;++o)e[t][o]=i[t][o]+r;return new s(e)},s.prototype.plusEach_=function(r){var t,o,s=this.data,i=this.rows,a=this.cols;for(t=0;i>t;++t)for(o=0;a>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.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}}); |
@@ -852,4 +852,51 @@ (function (root, factory) { | ||
/** | ||
* Apply function with row and column parameters to all elements in matrix | ||
* | ||
* Used to apply different transformations depending on placement in matrix. | ||
*/ | ||
Matrix.prototype.eleMap = function(transformFn) { | ||
var thisData = this.data, | ||
rows = this.rows, | ||
cols = this.cols; | ||
var row, col, result = new Array(rows); | ||
for (row=0; row<rows; ++row) { | ||
result[row] = new Array(cols); | ||
for (col=0; col<cols; ++col) { | ||
result[row][col] = transformFn(thisData[row][col], row, col); | ||
} | ||
} | ||
return new Matrix(result); | ||
}; | ||
Matrix.prototype.eleMap_ = function(transformFn) { | ||
var thisData = this.data, | ||
rows = this.rows, | ||
cols = this.cols; | ||
var row, col; | ||
for (row=0; row<rows; ++row) { | ||
for (col=0; col<cols; ++col) { | ||
thisData[row][col] = transformFn(thisData[row][col], row, col); | ||
} | ||
} | ||
return this; | ||
}; | ||
return LinAlg; | ||
@@ -856,0 +903,0 @@ }; |
@@ -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},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.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}}); |
{ | ||
"name": "linear-algebra", | ||
"version": "3.1.1", | ||
"version": "3.1.2", | ||
"description": "Efficient, high-performance linear algebra library", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -20,3 +20,3 @@ # linear-algebra | ||
### node.js | ||
### CommonJS | ||
@@ -29,6 +29,4 @@ Install using [npm](http://npmjs.org/): | ||
Use [bower](https://github.com/bower/bower): | ||
Include `dist/linear-algebra.js` script into your HTML. | ||
$ bower install linear-algebra | ||
In the browser the library is exposed via the `linearAlgebra()` function. | ||
@@ -35,0 +33,0 @@ |
@@ -34,1 +34,8 @@ /** | ||
BUILD(MATH_OP, plusEach, thisData[row][col] + value, value) | ||
/** | ||
* Apply function with row and column parameters to all elements in matrix | ||
* | ||
* Used to apply different transformations depending on placement in matrix. | ||
*/ | ||
BUILD(ELE_MATH_OP, eleMap, transformFn(thisData[row][col], row, col), transformFn) |
@@ -389,2 +389,170 @@ var chai = require('chai'), | ||
} | ||
}, | ||
'eleMap': { | ||
'default': { | ||
'row transform': function() { | ||
var stub = this.mocker.spy(function(v, row, col) { | ||
switch (row){ | ||
case 0: return v * 2; | ||
case 1: return v * 3; | ||
default: return v * 10; | ||
} | ||
}); | ||
var m = new this.Matrix([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]); | ||
var m2 = m.eleMap(stub); | ||
m2.should.not.eql(m); | ||
m2.data.should.not.eql(m.data); | ||
m2.data.should.eql([ [2, 4, 6], [12, 15, 18], [70, 80, 90] ]); | ||
m2.rows.should.eql(3); | ||
m2.cols.should.eql(3); | ||
stub.callCount.should.eql(9); | ||
}, | ||
'column transform': function() { | ||
var stub = this.mocker.spy(function(v, row, col) { | ||
switch (col) { | ||
case 0: return v * 2; | ||
case 1: return v * 3; | ||
default: return v * 10; | ||
} | ||
}); | ||
var m = new this.Matrix([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]); | ||
var m2 = m.eleMap(stub); | ||
m2.should.not.eql(m); | ||
m2.data.should.not.eql(m.data); | ||
m2.data.should.eql([ [2, 6, 30], [8, 15, 60], [14, 24, 90] ]); | ||
m2.rows.should.eql(3); | ||
m2.cols.should.eql(3); | ||
stub.callCount.should.eql(9); | ||
}, | ||
'element transform': function() { | ||
var stub = this.mocker.spy(function(v, row, col) { | ||
switch (row) { | ||
case 0: | ||
//First row | ||
switch (col) { | ||
case 0: return v + 1; | ||
case 1: return v + 2; | ||
default: return v + 3; | ||
} | ||
case 1: | ||
//Second row | ||
switch (col) { | ||
case 0: return v; | ||
case 1: return v * 2; | ||
default: return v * 3; | ||
} | ||
default: | ||
// All other rows | ||
switch (col) { | ||
case 0: return v - 1; | ||
case 1: return v - 2; | ||
default: return v -3; | ||
} | ||
} | ||
}); | ||
var m = new this.Matrix([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]); | ||
var m2 = m.eleMap(stub); | ||
m2.should.not.eql(m); | ||
m2.data.should.not.eql(m.data); | ||
m2.data.should.eql([ [2, 4, 6], [4, 10, 18], [6, 6, 6] ]); | ||
m2.rows.should.eql(3); | ||
m2.cols.should.eql(3); | ||
stub.callCount.should.eql(9); | ||
} | ||
}, | ||
'in-place': { | ||
'row transform': function() { | ||
var stub = this.mocker.spy(function(v, row, col) { | ||
switch (row){ | ||
case 0: return v * 2; | ||
case 1: return v * 3; | ||
default: return v * 10; | ||
} | ||
}); | ||
var m = new this.Matrix([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]); | ||
var m2 = m.eleMap_(stub); | ||
m2.should.eql(m); | ||
m2.data.should.eql(m.data); | ||
m2.data.should.eql([ [2, 4, 6], [12, 15, 18], [70, 80, 90] ]); | ||
m2.rows.should.eql(3); | ||
m2.cols.should.eql(3); | ||
stub.callCount.should.eql(9); | ||
}, | ||
'column transform': function() { | ||
var stub = this.mocker.spy(function(v, row, col) { | ||
switch (col) { | ||
case 0: return v * 2; | ||
case 1: return v * 3; | ||
default: return v * 10; | ||
} | ||
}); | ||
var m = new this.Matrix([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]); | ||
var m2 = m.eleMap_(stub); | ||
m2.should.eql(m); | ||
m2.data.should.eql(m.data); | ||
m2.data.should.eql([ [2, 6, 30], [8, 15, 60], [14, 24, 90] ]); | ||
m2.rows.should.eql(3); | ||
m2.cols.should.eql(3); | ||
stub.callCount.should.eql(9); | ||
}, | ||
'element transform': function() { | ||
var stub = this.mocker.spy(function(v, row, col) { | ||
switch (row) { | ||
case 0: | ||
//First row | ||
switch (col) { | ||
case 0: return v + 1; | ||
case 1: return v + 2; | ||
default: return v + 3; | ||
} | ||
case 1: | ||
//Second row | ||
switch (col) { | ||
case 0: return v; | ||
case 1: return v * 2; | ||
default: return v * 3; | ||
} | ||
default: | ||
//All other rows | ||
switch (col) { | ||
case 0: return v - 1; | ||
case 1: return v - 2; | ||
default: return v -3; | ||
} | ||
} | ||
}); | ||
var m = new this.Matrix([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]); | ||
var m2 = m.eleMap_(stub); | ||
m2.should.eql(m); | ||
m2.data.should.eql(m.data); | ||
m2.data.should.eql([ [2, 4, 6], [4, 10, 18], [6, 6, 6] ]); | ||
m2.rows.should.eql(3); | ||
m2.cols.should.eql(3); | ||
stub.callCount.should.eql(9); | ||
} | ||
} | ||
} | ||
@@ -391,0 +559,0 @@ } |
Sorry, the diff of this file is not supported yet
97294
8.04%2457
7.53%31
-3.12%282
-0.7%