quintex
Version:
On your command line, run the following commands: ```bash npm install quintex ``` Write the following code in your Node.JS server JavaScript file: ```js const $ = require("quintex"); ```
552 lines (539 loc) • 10.9 kB
JavaScript
//jshint esversion:6
// $.math Functions
function add(nums=[]) {
let sum = 0;
nums.forEach((num) => {
sum += num;
});
return sum;
}
function sub(num1,num2) {
let diff = num1 - num2;
return diff;
}
function mult(nums=[]) {
let prod = 1;
nums.forEach((num) => {
prod*=num;
});
return prod;
}
function div(num1,num2) {
return num1/num2;
}
function pow(num1,num2) {
return Math.pow(num1, num2);
}
function tpow(x) {
return pow(10,x);
}
function percOf(x, of) {
return (x/100)*of;
}
function sq(x) {
return x*x;
}
function cb(x) {
return x*x*x;
}
function rcp(x) {
return 1/x;
}
function eX(x) {
return pow(e, x);
}
function sqrt(x) {
return pow(x, 1/2)
}
function cbrt(x) {
return pow(x,1/3);
}
function log(base, x) {
return (Math.log(x) / Math.log(base));
}
function log10(x) {
return log(10, x);
}
function log2(x) {
return log(2, x);
}
function ln(x) {
return Math.log(x);
}
function randInt(min, max) {
return Math.floor((Math.random()) * (max + 1 - min)) + min;
}
function odd(x) {
return x%2===1;
}
function even(x) {
return x%2===0;
}
function oddOrEven(x) {
return (x%2==0)?"Even":"Odd";
}
function subify(x) {
let stringX = String(x);
let subbed = "";
for (var i = 0; i < stringX.length; i++) {
let num = stringX[i];
let ac = "";
switch (num) {
case "1":
ac = "₁";
break;
case "2":
ac = "₂";
break;
case "3":
ac = "₃";
break;
case "4":
ac = "₄";
break;
case "5":
ac = "₅";
break;
case "6":
ac = "₆";
break;
case "7":
ac = "₇";
break;
case "8":
ac = "₈";
break;
case "9":
ac = "₉";
break;
case "0":
ac = "₀";
break;
}
subbed += ac;
};
return subbed;
}
function supify(x) {
let stringX = String(x);
let supped = "";
for (var i = 0; i < stringX.length; i++) {
let num = stringX[i];
let ac = "";
switch (num) {
case "1":
ac = "¹";
break;
case "2":
ac = "²";
break;
case "3":
ac = "³";
break;
case "4":
ac = "⁴";
break;
case "5":
ac = "⁵";
break;
case "6":
ac = "⁶";
break;
case "7":
ac = "⁷";
break;
case "8":
ac = "⁸";
break;
case "9":
ac = "⁹";
break;
case "0":
ac = "⁰";
break;
}
supped += ac;
}
return supped;
}
//$.math Constants
const pi=3.141592653589793238462;
const e=2.718281828459045235360287471352662497757247;
const tau=2*pi;
const phi=1.61803398874989484820;
function Polynomial(x) {
this.degree = x.degree;
this.coef = x.coef;
}
function Complex(x) {
this.imaginary = x.imaginary;
this.real = x.real;
}
//$.math Sections
const trig = {
sin: (x) => {
const rad = x * (pi/180);
return Math.sin(rad);
},
cos: (x) => {
const rad = x * (pi/180);
return Math.cos(rad);
},
tan: (x) => {
const rad = x * (pi/180);
return Math.tan(rad);
},
sec: (x) => {
const rad = x * (pi/180);
return 1/Math.cos(rad);
},
csc: (x) => {
const rad = x * (pi/180);
return 1/Math.sin(rad);
},
cot: (x) => {
const rad = x * (pi/180);
return 1/Math.tan(rad);
},
sinh: (x) => {
const rad = x * (pi/180);
return Math.sinh(rad);
},
cosh: (x) => {
const rad = x * (pi/180);
return Math.cosh(rad);
},
tanh: (x) => {
const rad = x * (pi/180);
return Math.tanh(rad);
},
sech: (x) => {
const rad = x * (pi/180);
return 1/Math.cosh(rad);
},
csch: (x) => {
const rad = x * (pi/180);
return 1/Math.sinh(rad);
},
coth: (x) => {
const rad = x * (pi/180);
return 1/Math.tanh(rad);
},
asin: (x) => {
const rad = x * (pi/180);
return Math.asin(rad);
},
acos: (x) => {
const rad = x * (pi/180);
return Math.acos(rad);
},
atan: (x) => {
const rad = x * (pi/180);
return Math.atan(rad);
},
asec: (x) => {
const rad = x * (pi/180);
return 1/Math.acos(rad);
},
acsc: (x) => {
const rad = x * (pi/180);
return 1/Math.asin(rad);
},
acot: (x) => {
const rad = x * (pi/180);
return 1/Math.atan(rad);
},
asinh: (x) => {
const rad = x * (pi/180);
return Math.asinh(rad);
},
acosh: (x) => {
const rad = x * (pi/180);
return Math.acosh(rad);
},
atanh: (x) => {
const rad = x * (pi/180);
return Math.atanh(rad);
},
asech: (x) => {
const rad = x * (pi/180);
return 1/Math.acosh(rad);
},
acsch: (x) => {
const rad = x * (pi/180);
return 1/Math.asinh(rad);
},
acoth: (x) => {
const rad = x * (pi/180);
return 1/Math.atanh(rad);
}
}
const area = {
}
const per = {
}
const vol = {
}
const stats = {
mean: (list) => {
let sum = 0;
list.forEach((num) => {
sum += num;
});
const mode = sum / list.length;
return mode;
},
median: (list) => {
if (oddOrEven(list.length)=="Odd"){
return list[((list.length + 1) / 2) - 1];
} else {
return stats.mean([list[list.length/2-1],list[list.length]]);
}
},
mode: (list) => {
let repetition = [];
list .forEach((number) => {
// Step 1: Create 'repetition' array
let isThere = false;
repetition.forEach((entry) => {
if (entry[0] === number) {
entry[1]++;
isThere = true;
}
});
if (isThere === false) {
repetition.push([number, 1]);
}
});
// Step 2: Create 'highest' array
let highest = [[0, 0]];
repetition.forEach((entry) => {
if (entry[1] > highest[0][1]) {
highest = [];
highest.push(entry);
} else if (entry[1] === highest[0][1]) {
highest.push(entry);
}
});
// Step 3: Return result
let allHighest = [];
highest.forEach((entry) => {
allHighest.push(entry[0]);
});
return allHighest;
},
stdev: (list) => {
// Step 1: Find the mean.
let mean = stats.mean(list);
// Step 2: For each data point, find the square of its distance to the mean.
let squares = [];
list.forEach((num) => {
squares.push(sq(num - mean));
});
// Step 3: Sum the values from Step 2.
let sum = add(squares);
// Step 4: Divide by the number of data points.
let step4 = sum / list.length;
// Step 5: Take the square root.
let ans = sqrt(step4);
// Step 6: Return the result.
return ans;
},
variance: (list) => {
return sq(stats.stdev(list));
}
}
const conv = {
afn: 59.07,
all: 76.38,
dzd: 99.26,
aoa: 474.81,
arp: 71,
amd: 366.5,
awg: 1.33,
aud: 0.99,
azn: 1.26,
bsd: 0.74,
bhd: 0.28,
bbd: 1.49,
bdt: 62.45,
byn: 1.89,
bzd: 1.48,
bmd: 0.74,
btn: 54.89,
bitcoin: 0.000023,
bitcoinCash: 0.0016,
bob: 5.09,
bam: 1.22,
bwp: 8.16,
brl: 3.75,
bnd: 1,
bgn: 1.22,
bif: 1458.93,
cfp: 74.86,
khr: 2998.35,
cad: 0.93,
cve: 68.84,
kyd: 0.61,
xaf: 409.76,
clp: 550.81,
clf: 0.019,
cny: 4.78,
cop: 2804.57,
kmf: 307.81,
cdf: 1478.42,
money: (from, to, howMuch) => {
}
}
const poly = {
getPoly: (poly) => {
let polynomial = "";
let deg = poly.degree;
if ((deg + 1) != poly.coef.length) {
console.error("$.math.poly.getPoly() ERROR: Degree must match coefficient array length");
} else {
poly.coef.forEach((coefficient) => {
if (coefficient === 0) {
if ((deg) != poly.degree) {
polynomial += "+";
}
} else if (coefficient === 1) {
if (deg === 1) {
polynomial += (coefficient + "x" + "+");
} else if (deg === 0) {
polynomial += (coefficient + "+");
} else {
polynomial += ("x" + supify(deg) + "+");
}
} else {
if (deg === 1) {
polynomial += (coefficient + "x" + "+");
} else if (deg === 0) {
polynomial += (coefficient + "+");
} else {
polynomial += (coefficient + "x" + supify(deg) + "+");
}
}
deg--;
});
}
// Format polynomial
let arrayPoly = polynomial.split("");
arrayPoly.pop();
let rPOLY = "";
arrayPoly.forEach((char) => {
rPOLY += char;
});
return rPOLY;
},
addPoly: (poly1, poly2) => {
let a1POLY = poly1.coef.reverse();
let a2POLY = poly2.coef.reverse();
let resultPOLY = [];
if (a1POLY.length >= a2POLY.length) {
a1POLY.forEach((x) => {
if (a1POLY.indexOf(x) in a2POLY) {
resultPOLY.push(x + a2POLY[a1POLY.indexOf(x)]);
} else {
resultPOLY.push(x);
}
});
} else {
a2POLY.forEach((y) => {
if (a2POLY.indexOf(y) in a1POLY) {
resultPOLY.push(y + a1POLY[a2POLY.indexOf(y)]);
} else {
resultPOLY.push(y);
}
});
}
let rCoef = resultPOLY.reverse();
let rDeg = (resultPOLY.length - 1);
let result = new Polynomial({
degree: rDeg,
coef: rCoef
});
return result;
},
subPoly: (poly1, poly2) => {
let a1POLY = poly1.coef.reverse();
let a2POLY = poly2.coef.reverse().map(x => x * -1);
let resultPOLY = [];
if (a1POLY.length >= a2POLY.length) {
a1POLY.forEach((x) => {
if (a1POLY.indexOf(x) in a2POLY) {
resultPOLY.push(x + a2POLY[a1POLY.indexOf(x)]);
} else {
resultPOLY.push(x);
}
});
} else {
a2POLY.forEach((y) => {
if (a2POLY.indexOf(y) in a1POLY) {
resultPOLY.push(y + a1POLY[a2POLY.indexOf(y)]);
} else {
resultPOLY.push(y);
}
});
}
let rCoef = resultPOLY.reverse();
let rDeg = (resultPOLY.length - 1);
let result = new Polynomial({
degree: rDeg,
coef: rCoef
});
return result;
}
}
const complex = {
getComplex: (x) => {
let complexNumber = "";
if (x.imaginary === 0) {} else {
if (x.real === 0) {
complexNumber = x.imaginary + "i";
} else {
complexNumber = x.imaginary + "i+" + x.real;
}
}
return complexNumber;
}
}
exports.math = {
Complex: Complex,
complex: complex,
add: add,
sub: sub,
mult: mult,
div: div,
pow: pow,
tpow: tpow,
percOf: percOf,
sq: sq,
cb: cb,
rcp: rcp,
eX: eX,
sqrt: sqrt,
cbrt: cbrt,
log: log,
log10: log10,
log2: log2,
ln: ln,
randInt: randInt,
odd: odd,
even: even,
oddOrEven: oddOrEven,
subify: subify,
supify: supify,
pi: pi,
e: e,
tau: tau,
phi: phi,
Polynomial: Polynomial,
trig: trig,
area: area,
per: per,
vol: vol,
st: stats,
conv: conv,
poly: poly
}