-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumbers-0.1.0-min.js
1 lines (1 loc) · 4.52 KB
/
numbers-0.1.0-min.js
1
window.numbers={};window.numbers=function(e){var t=[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97];var n=null;(function(){if(window.F!==null&&typeof window.F!=="undefined"){n=window.F}})();e.version=e.ver=e.v="0.1.0";e.noConflict=function(){if(n!==null){window.F=n}return this};e.sum=function(e){var t=e.length;var n=0;for(var r=0;r<t;r++){n+=e[r]}return n};e.product=function(e){var t=e.length;var n=1;for(var r=0;r<t;r++){n*=e[r]}return n};e.avg=e.mean=function(e){var t=e.length;return this.sum(e)/t};e.variance=function(e){var t=e.length;var n=0;var r=this.mean(e);for(var i=0;i<t;i++){n+=Math.pow(e[i]-r,2)}return n/t};e.covariance=function(e,t){var n=e.length;var r=t.length;if(n!==r){throw new Error("Sets are not of equal size.")}var i=this.sum(e);var s=this.sum(t);var o=0;for(var u=0;u<n;u++){o+=e[u]*t[u]}return(o-i*s/n)/(n-1)};e.stdev=e.standardDeviation=function(e){return Math.sqrt(this.variance(e))};e.sem=e.standardErrorOfMean=function(e){return Math.sqrt(this.variance(e)/e.length)};e.z=e.zValue=function(e,t,n){return(e-t)/n};e.nFac=e.factorial=function(e){if(this.factorial.cache[e]){return this.factorial.cache[e]}if(e==0||e==1){return 1}var t=e*this.factorial(e-1);this.factorial.cache[e]=Math.round(t,0);return Math.round(t,0)};e.factorial.cache={};e.nPr=e.permutations=function(e,t){if(this.permutations.cache[e][t]){return this.permutations.cache[e][t]}if(t>e){this.permutations.cache[e][t]=0;return 0}if(t==e||t==0){this.permutations.cache[e][t]=1;return 1}if(t==1){this.permutations.cache[e][t]=e;return e}if(t<Math.floor(e/2)){var n=1;var r=e;var i=e-t;while(r>i){n*=r--}this.permutations.cache[e][t]=Math.round(n,0);return Math.round(n,0)}var s=this.factorial(e);var o=this.factorial(e-t);this.permutations.cache[e][t]=Math.round(s/o,0);return Math.round(s/o,0)};e.permutations.cache={};e.nCr=e.combinations=function(e,t){if(this.combinations.cache[e][t]){return this.combinations.cache[e][t]}if(t>e){this.combinations.cache[e][t]=0;return 0}if(t==e||t==0){this.combinations.cache[e][t]=1;return 1}if(t==1){this.combinations.cache[e][t]=e;return e}var n=this.permutations(e,t);var r=this.factorial(t);this.combinations.cache[e][t]=Math.round(n/r,0);return Math.round(n/r,0)};e.combinations.cache={};e.gcd=e.greatestCommonDivisor=function(e){var t=e.length;if(t===0)return"undefined";if(t===1)return e[0];e.sort(function(e,t){return e-t});if(t===2){var n=Math.abs(e[0]);if(e[1]%n===0)return n;var r=Math.floor(n/2);for(var i=r;i>1;i--){if(e[0]%i===0&&e[1]%i===0){return i}}return 1}var s=e.pop();return this.gcd([this.gcd(e),s])};e.lcm=e.leastCommonMultiple=function(e){var t=e.length;if(t===0)return"undefined";if(t===1)return e[0];if(t===2){return Math.round(this.product(e)/this.gcd(e),0)}var n=e.pop();return this.lcm([n,this.lcm(e)])};e.isPrime=function(e){if(this.isPrime.cache[e]){return this.isPrime.cache[e]}var n=parseInt(e,10);if(n==="NaN"){this.isPrime.cache[e]="undefined";return"undefined"}if(n===1){this.isPrime.cache[e]=false;return false}if(t.indexOf(n)>=0){this.isPrime.cache[e]=true;return true}for(var r=0;r<t.length;r++){if(n%t[r]===0){this.isPrime.cache[e]=false;return false}}var i=Math.floor(Math.sqrt(n));for(var s=101;s<=i;s+=2){var o=false;for(var u=0;u<t.length;u++){if(s%t[u]===0){o=true;break}}if(o)continue;if(n%s===0){this.isPrime.cache[e]=false;return false}}this.isPrime.cache[e]=true;return true};e.isPrime.cache={};e.isRelativelyPrime=function(e,t){return this.gcd(e,t)===1};e.isCongruent=function(e,t,n){return(e-t)%n===0};e.dotProduct=function(e,t){var n=e.length;var r=t.length;if(n!==r){throw new Error("Vectors do not have equal sizes.")}var i=0;for(var s=0;s<n;s++){i+=e[s]*t[s]}return i};e.isOrthogonal=function(e,t){return Math.round(this.dotProduct(e,t),0)===0};e.magnitude=function(e){var t=Math.sqrt(this.dotProduct(e,e));return t};e.normalize=function(e){var t=[];var n=e.length;var r=this.magnitude(e);for(var i=0;i<n;i++){t[i]=e[i]/r}return t};e.tr=e.trace=function(e){var t=e.length;var n=0;for(var r=0;r<t;r++){if(!Array.isArray(e[r])){throw new Error("Missing vector at column "+r)}if(e[r][r]===null||typeof e[r][r]==="undefined"){throw new Error("Missing value at column "+r+", row "+r)}n+=e[r][r]}return n};e.rebase=function(e,t,n){if(n===null||typeof n==="undefined")n=10;if(t==n)return e;return parseInt(parseInt(e,n).toString(t),10)};e.deg2rad=function(e){return Math.PI*e/180};e.rad2deg=function(e){return 180*e/Math.PI};e.rect2pol=function(e,t){return{r:Math.sqrt(Math.pow(e,2)+Math.pow(t,2)),t:Math.atan2(t,e)}};e.pol2rect=function(e,t){return{x:e*Math.cos(t),y:e*Math.sin(t)}};return window.F=e}(window.numbers)