-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinear-algebra.min.js
1 lines (1 loc) · 6.16 KB
/
linear-algebra.min.js
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,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}});