vectorious-plus
Version:
A high performance linear algebra library.
1 lines • 13.3 kB
JavaScript
!function t(r,e,n){function a(i,s){if(!e[i]){if(!r[i]){var h="function"==typeof require&&require;if(!s&&h)return h(i,!0);if(o)return o(i,!0);var p=new Error("Cannot find module '"+i+"'");throw p.code="MODULE_NOT_FOUND",p}var u=e[i]={exports:{}};r[i][0].call(u.exports,function(t){var e=r[i][1][t];return a(e?e:t)},u,u.exports,t,r,e,n)}return e[i].exports}for(var o="function"==typeof require&&require,i=0;i<n.length;i++)a(n[i]);return a}({1:[function(t,r,e){!function(){"use strict";function e(t,r){return this.type=Float64Array,this.shape=[],t&&t.buffer&&t.buffer instanceof ArrayBuffer?e.fromTypedArray(t,r.shape):t instanceof Array?e.fromArray(t):void(t instanceof n?(this.shape=r&&r.shape?r.shape:[t.length,1],this.data=new t.type(t.data),this.type=t.type):t instanceof e&&(this.shape=[t.shape[0],t.shape[1]],this.data=new t.type(t.data),this.type=t.type))}var n=t("./vector");e.fromTypedArray=function(t,r){if(t.length!==r[0]*r[1])throw new Error("Shape does not match typed array dimensions.");var n=Object.create(e.prototype);return n.shape=r,n.data=t,n.type=t.constructor,n},e.fromArray=function(t){var r,n,a=t.length,o=t[0].length,i=new Float64Array(a*o);for(r=0;r<a;++r)for(n=0;n<o;++n)i[r*o+n]=t[r][n];return e.fromTypedArray(i,[a,o])},e.add=function(t,r){return new e(t).add(r)},e.prototype.add=function(t){var r=this.shape[0],e=this.shape[1],n=this.data,a=t.data;if(r!==t.shape[0]||e!==t.shape[1])throw new Error("sizes do not match!");var o;for(o=0;o<r*e;o++)n[o]+=a[o];return this},e.subtract=function(t,r){return new e(t).subtract(r)},e.prototype.subtract=function(t){var r=this.shape[0],e=this.shape[1],n=this.data,a=t.data;if(r!==t.shape[0]||e!==t.shape[1])throw new Error("sizes do not match");var o;for(o=0;o<r*e;o++)n[o]-=a[o];return this},e.scale=function(t,r){return new e(t).scale(r)},e.prototype.scale=function(t){var r,e=this.shape[0],n=this.shape[1],a=this.data;for(r=0;r<e*n;r++)a[r]*=t;return this},e.product=function(t,r){return new e(t).product(r)},e.prototype.product=function(t){if(this.shape[0]!==t.shape[0]||this.shape[1]!==t.shape[1])return new Error("invalid size");var r,e=this.shape[0],n=this.shape[1],a=this.data,o=t.data;for(r=0;r<e*n;r++)a[r]*=o[r];return this},e.zeros=function(t,r,n){if(t<=0||r<=0)throw new Error("invalid size");n=n||Float64Array;var a,o=new n(t*r);for(a=0;a<t*r;a++)o[a]=0;return e.fromTypedArray(o,[t,r])},e.ones=function(t,r,n){if(t<=0||r<=0)throw new Error("invalid size");n=n||Float64Array;var a=new n(t*r),o=0;for(o=0;o<t*r;o++)a[o]=1;return e.fromTypedArray(a,[t,r])},e.random=function(t,r,n,a,o){n=n||1,a=a||0,o=o||Float64Array;var i,s=new o(t*r);for(i=0;i<t*r;i++)s[i]=n*Math.random()+a;return e.fromTypedArray(s,[t,r])},e.multiply=function(t,r){return t.multiply(r)},e.prototype.multiply=function(t){var r=this.shape[0],n=this.shape[1],a=t.shape[0],o=t.shape[1],i=this.data,s=t.data;if(n!==a)throw new Error("sizes do not match");var h,p,u,f,c=new this.type(r*o);for(h=0;h<r;h++)for(p=0;p<o;p++){for(f=0,u=0;u<n;u++)f+=i[h*n+u]*s[p+u*o];c[h*o+p]=f}return e.fromTypedArray(c,[r,o])},Object.defineProperty(e.prototype,"T",{get:function(){return this.transpose()}}),e.prototype.transpose=function(){var t,r,n=this.shape[0],a=this.shape[1],o=new this.type(a*n);for(t=0;t<n;t++)for(r=0;r<a;r++)o[r*n+t]=this.data[t*a+r];return e.fromTypedArray(o,[a,n])},e.prototype.inverse=function(){var t=this.shape[0],r=this.shape[1];if(t!==r)throw new Error("invalid dimensions");var n,a,o=e.identity(t),i=e.augment(this,o),s=i.gauss(),h=e.zeros(t,r),p=e.zeros(t,r),u=s.shape[1];for(n=0;n<t;n++)for(a=0;a<u;a++)a<r?h.set(n,a,s.get(n,a)):p.set(n,a-t,s.get(n,a));if(!h.equals(e.identity(t)))throw new Error("matrix is not invertible");return p},e.prototype.gauss=function(){var t,r,n,a,o,i=this.shape[0],s=this.shape[1],h=new e(this),p=0;for(r=0;r<i;r++){if(s<=p)return new Error("matrix is singular");for(n=r;0===h.data[n*s+p];)if(n++,i===n&&(n=r,p++,s===p))return new Error("matrix is singular");if(h.swap(r,n),t=h.data[r*s+p],0!==t)for(a=0;a<s;a++)h.data[r*s+a]=h.data[r*s+a]/t;for(n=0;n<i;n++)if(o=h.data[n*s+p],n!==r)for(a=0;a<s;a++)h.data[n*s+a]=h.data[n*s+a]-h.data[r*s+a]*o;p++}for(r=0;r<i;r++){for(t=0,n=0;n<s;n++)t||(t=h.data[r*s+n]);if(t)for(a=0;a<s;a++)h.data[r*s+a]=h.data[r*s+a]/t}return h},e.prototype.lu=function(){var t,r,n=this.shape[0],a=this.shape[1],o=e.plu(this),i=o[1],s=(e.identity(n),new e(o[0])),h=new e(o[0]);for(t=0;t<n;t++)for(r=t;r<a;r++)s.data[t*a+r]=t===r?1:0;for(t=0;t<n;t++)for(r=0;r<t&&r<a;r++)h.data[t*a+r]=0;return[s,h,i]},e.plu=function(t){return new e(t).plu()},e.prototype.plu=function(){var t,r,e,n,a,o,i,s=this.data,h=this.shape[0],p=new Int32Array(h);for(i=0;i<h;++i){for(n=i,t=Math.abs(s[i*h+i]),o=i+1;o<h;++o)r=Math.abs(s[o*h+i]),t<r&&(t=r,n=o);for(p[i]=n,n!==i&&this.swap(i,n),e=s[i*h+i],a=i+1;a<h;++a)s[a*h+i]/=e;for(a=i+1;a<h;++a){for(o=i+1;o<h-1;++o)s[a*h+o]-=s[a*h+i]*s[i*h+o],++o,s[a*h+o]-=s[a*h+i]*s[i*h+o];o===h-1&&(s[a*h+o]-=s[a*h+i]*s[i*h+o])}}return[this,p]},e.prototype.lusolve=function(t,r){var e,n,a,o=this.data,i=t.shape[0],s=t.shape[1],h=t.data;for(e=0;e<r.length;e++)e!==r[e]&&t.swap(e,r[e]);for(a=0;a<s;a++){for(e=0;e<i;e++)for(n=0;n<e;n++)h[e*s+a]-=o[e*i+n]*h[n*s+a];for(e=i-1;e>=0;e--){for(n=e+1;n<i;n++)h[e*s+a]-=o[e*i+n]*h[n*s+a];h[e*s+a]/=o[e*i+e]}}return t},e.prototype.solve=function(t){var r=e.plu(this),n=r[0],a=r[1];return n.lusolve(new e(t),a)},e.augment=function(t,r){return new e(t).augment(r)},e.prototype.augment=function(t){if(0===t.shape.length)return this;var r,e,n=this.shape[0],a=this.shape[1],o=t.shape[0],i=t.shape[1],s=this.data,h=t.data;if(n!==o)throw new Error("Rows do not match.");var p=a+i,u=new this.type(p*n);for(r=0;r<n;r++)for(e=0;e<a;e++)u[r*p+e]=s[r*a+e];for(r=0;r<o;r++)for(e=0;e<i;e++)u[r*p+e+a]=h[r*i+e];return this.shape=[n,p],this.data=u,this},e.identity=function(t,r){if(t<0)throw new Error("invalid size");r=r||Float64Array;var n,a=e.zeros(t,t,r);for(n=0;n<t;n++)a.data[n*t+n]=1;return a},e.magic=function(t,r){function n(t,r,e){return(r+2*e+1)%t}if(t<0)throw new Error("invalid size");r=r||Float64Array;var a,o,i=new r(t*t);for(a=0;a<t;a++)for(o=0;o<t;o++)i[(t-a-1)*t+(t-o-1)]=n(t,t-o-1,a)*t+n(t,o,a)+1;return e.fromTypedArray(i,[t,t])},e.prototype.diag=function(){var t,r=this.shape[0],e=this.shape[1],a=new this.type(Math.min(r,e));for(t=0;t<r&&t<e;t++)a[t]=this.data[t*e+t];return new n(a)},e.prototype.determinant=function(){if(this.shape[0]!==this.shape[1])throw new Error("matrix is not square");var t,r=e.plu(this),n=r.pop(),a=r.pop(),o=this.shape[0],i=this.shape[1],s=1,h=1;for(t=0;t<o;t++)t!==n[t]&&(h*=-1);for(t=0;t<o;t++)s*=a.data[t*i+t];return h*s},e.prototype.trace=function(){var t,r,e=this.diag(),n=0;for(t=0,r=e.length;t<r;t++)n+=e.get(t);return n},e.equals=function(t,r){return t.equals(r)},e.prototype.equals=function(t){var r=this.shape[0],e=this.shape[1],n=this.data,a=t.data;if(r!==t.shape[0]||e!==t.shape[1]||this.type!==t.type)return!1;var o;for(o=0;o<r*e;o++)if(n[o]!==a[o])return!1;return!0},e.prototype.get=function(t,r){if(t<0||r<0||t>this.shape[0]-1||r>this.shape[1]-1)throw new Error("index out of bounds");return this.data[t*this.shape[1]+r]},e.prototype.set=function(t,r,e){if(t<0||r<0||t>this.shape[0]-1||r>this.shape[1]-1)throw new Error("index out of bounds");return this.data[t*this.shape[1]+r]=e,this},e.prototype.swap=function(t,r){if(t<0||r<0||t>this.shape[0]-1||r>this.shape[0]-1)throw new Error("index out of bounds");var e=this.shape[1],n=this.data.slice(t*e,(t+1)*e);return this.data.copyWithin(t*e,r*e,(r+1)*e),this.data.set(n,r*e),this},e.prototype.map=function(t){var r,n=this.shape[0],a=this.shape[1],o=new e(this),i=o.data;for(r=0;r<n*a;r++)i[r]=t.call(o,i[r],r/a|0,r%a,i);return o},e.prototype.each=function(t){var r,e=this.shape[0],n=this.shape[1];for(r=0;r<e*n;r++)t.call(this,this.data[r],r/n|0,r%n);return this},e.prototype.reduce=function(t,r){var e=this.shape[0],n=this.shape[1];if(e*n===0&&!r)throw new Error("Reduce of empty matrix with no initial value.");for(var a=0,o=r||this.data[a++];a<e*n;a++)o=t.call(this,o,this.data[a],a/n|0,a%n);return o},e.prototype.rank=function(){var t,r,e,a,o,i,s=this.toArray().map(function(t){return new n(t)}),h=this.shape[0],p=this.shape[1],u=0;for(t=0;t<h-1;t++){for(a=null,r=t;r<h;r++)if(s[t].get(t)){t!==r&&(e=s[t],s[t]=s[r],s[r]=e),a=s[t];break}if(a)for(r=t+1;r<h;r++)o=s[r],i=o.get(t)/a.get(t),s[r]=o.subtract(a.scale(i))}for(t=0;t<h;t++)for(r=0;r<p;r++)if(s[t].get(r)){u++;break}return u},e.rank=function(t){return new e(t).rank()},e.prototype.toString=function(){var t,r=[],e=this.shape[0],n=this.shape[1];for(t=0;t<e;t++)r.push("["+this.data.subarray(t*n,(t+1)*n).toString()+"]");return"["+r.join(", \n")+"]"},e.prototype.toArray=function(){var t,r=[],e=this.shape[0],n=this.shape[1];for(t=0;t<e;t++)r.push(Array.prototype.slice.call(this.data.subarray(t*n,(t+1)*n)));return r},r.exports=e;try{window.Matrix=e}catch(t){}}()},{"./vector":2}],2:[function(t,r,e){!function(){"use strict";function t(r){this.type=Float64Array,this.length=0,r instanceof t?this.combine(r):r&&r.shape?(this.data=new r.type(r.data),this.length=r.shape[0]*r.shape[1],this.type=r.type):r instanceof Array?(this.data=new this.type(r),this.length=r.length):r&&r.buffer&&r.buffer instanceof ArrayBuffer&&(this.data=r,this.length=r.length,this.type=r.constructor)}t.add=function(r,e){return new t(r).add(e)},t.prototype.add=function(t){var r=this.length,e=t.length;if(r!==e)throw new Error("sizes do not match!");if(!r&&!e)return this;var n;for(n=0;n<r;n++)this.data[n]+=t.data[n];return this},t.subtract=function(r,e){return new t(r).subtract(e)},t.prototype.subtract=function(t){var r=this.length,e=t.length;if(r!==e)throw new Error("sizes do not match");if(!r&&!e)return this;var n;for(n=0;n<r;n++)this.data[n]-=t.data[n];return this},t.scale=function(r,e){return new t(r).scale(e)},t.prototype.scale=function(t){var r;for(r=this.length-1;r>=0;r--)this.data[r]*=t;return this},t.normalize=function(r){return new t(r).normalize()},t.prototype.normalize=function(){return this.scale(1/this.magnitude())},t.project=function(r,e){return r.project(new t(e))},t.prototype.project=function(t){return t.scale(this.dot(t)/t.dot(t))},t.zeros=function(r,e){if(r<0)throw new Error("invalid size");if(0===r)return new t;e=e||Float64Array;var n,a=new e(r);for(n=0;n<r;n++)a[n]=0;return new t(a)},t.ones=function(r,e){if(r<0)throw new Error("invalid size");if(0===r)return new t;e=e||Float64Array;var n,a=new e(r);for(n=0;n<r;n++)a[n]=1;return new t(a)},t.range=function(){var r,e,n,a=[].slice.call(arguments,0),o=!1,i=Float64Array;switch("function"==typeof a[a.length-1]&&(i=a.pop()),a.length){case 2:n=a.pop(),e=1,r=a.pop();break;case 3:n=a.pop(),e=a.pop(),r=a.pop();break;default:throw new Error("invalid range")}if(n-r<0){var s=n;n=r,r=s,o=!0}if(e>n-r)throw new Error("invalid range");var h,p,u=new i(Math.ceil((n-r)/e));for(h=r,p=0;h<n;h+=e,p++)u[p]=o?n-h+r:h;return new t(u)},t.random=function(r,e,n,a){e=e||1,n=n||0,a=a||Float64Array;var o,i=new a(r);for(o=0;o<r;o++)i[o]=e*Math.random()+n;return new t(i)},t.dot=function(t,r){return t.dot(r)},t.prototype.dot=function(t){if(this.length!==t.length)throw new Error("sizes do not match");var r,e,n=this.data,a=t.data,o=0;for(r=0,e=this.length;r<e;r++)o+=n[r]*a[r];return o},t.prototype.magnitude=function(){if(!this.length)return 0;var t,r,e=0,n=this.data;for(t=0,r=this.length;t<r;t++)e+=n[t]*n[t];return Math.sqrt(e)},t.angle=function(t,r){return t.angle(r)},t.prototype.angle=function(t){return Math.acos(this.dot(t)/this.magnitude()/t.magnitude())},t.equals=function(t,r){return t.equals(r)},t.prototype.equals=function(t){if(this.length!==t.length)return!1;for(var r=this.data,e=t.data,n=this.length,a=0;a<n&&r[a]===e[a];)a++;return a===n},t.prototype.get=function(t){if(t<0||t>this.length-1)throw new Error("index out of bounds");return this.data[t]},t.prototype.min=function(){var t,r,e,n=Number.POSITIVE_INFINITY,a=this.data;for(r=0,e=a.length;r<e;r++)t=a[r],t<n&&(n=t);return n},t.prototype.max=function(){var t,r,e,n=Number.NEGATIVE_INFINITY,a=this.data;for(r=0,e=this.length;r<e;r++)t=a[r],t>n&&(n=t);return n},t.prototype.set=function(t,r){if(t<0||t>this.length-1)throw new Error("index out of bounds");return this.data[t]=r,this},t.combine=function(r,e){return new t(r).combine(e)},t.prototype.combine=function(t){if(!t.length)return this;if(!this.length)return this.data=new t.type(t.data),this.length=t.length,this.type=t.type,this;var r=this.length,e=t.length,n=this.data,a=t.data,o=new this.type(r+e);return o.set(n),o.set(a,r),this.data=o,this.length=r+e,this},t.prototype.push=function(r){return this.combine(new t([r]))},t.prototype.map=function(r){var e,n=new t(this),a=n.data;for(e=0;e<this.length;e++)a[e]=r.call(n,a[e],e);return n},t.prototype.each=function(t){var r;for(r=0;r<this.length;r++)t.call(this,this.data[r],r);return this},t.prototype.reduce=function(t,r){var e=this.length;if(0===e&&!r)throw new Error("Reduce of empty matrix with no initial value.");for(var n=0,a=r||this.data[n++];n<e;n++)a=t.call(this,a,this.data[n],n);return a},t.prototype.toString=function(){var t,r="";for(t=0;t<this.length;t++)r+=t>0?", "+this.data[t]:this.data[t];return"["+r+"]"},t.prototype.toArray=function(){return this.data?Array.prototype.slice.call(this.data):[]},r.exports=t;try{window.Vector=t}catch(t){}}()},{}]},{},[2,1]);