-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath.js
374 lines (374 loc) · 12.8 KB
/
math.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
"use strict";
/*
MathJS v1.3.7
Last Modified: 04/06/2024 <DD/MM/YYYY>
Author: Satyam Verma <github.com/SatyamV7>
Description: A JavaScript library for basic and advanced arithmetic operations, Satistical functions, logical functions, factorial and fibonacci functions, random number functions, and trigonometric functions.
Note: The author is not resposible fo accuracy of the results
Repository: github.com/SatyamV7/MathJS
License: MIT License
*/
/*
Helper Functions
*/
function getUnits(str) {
str = str.toString();
const units = str.match(/(?!\b(pi|tau)\b)\b[a-z]+\b/gi);
math.debuggingMode ? console.log('Units:', units) : null;
return units ? units.join('') : '';
}
function convertToRadians(n) {
let num = math.evaluate(n.toString().replace(/(?<![a-zA-Z])(pi|tau)(?![a-zA-Z])|[a-zA-Z]/gi, (match, p) => p ? match : ''), {});
switch (getUnits(n)) {
case 'deg':
case '':
return num * Math.PI / 180;
case 'grad':
return num * Math.PI / 200;
case 'rad':
return num;
default:
throw new Error('Invalid Unit');
}
}
function validateAngle(Angle, Function) {
return ((Function === 'tan' || Function === 'sec') && (Angle === Math.PI / 2 || Angle === 3 * Math.PI / 2)) ? Number.POSITIVE_INFINITY : ((Function === 'cot' || Function === 'csc') && (Angle === 0 || Angle === Math.PI)) ? Number.POSITIVE_INFINITY : Angle;
}
const math = {
// Debugging Mode
debuggingMode: false,
/*
Mathematical Constants
*/
e: Math.E,
PI: Math.PI,
LN2: Math.LN2,
LN10: Math.LN10,
TAU: 2 * Math.PI,
LOG2E: Math.LOG2E,
LOG10E: Math.LOG10E,
EPSILON: Number.EPSILON,
PHI: (1 + Math.sqrt(5)) / 2,
/*
Basic Arithmetic Functions
*/
add: (...n) => n.reduce((a, b) => a + b, 0),
subtract: (...n) => n.reduce((a, b) => a - b),
multiply: (...n) => n.reduce((a, b) => a * b),
divide: (...n) => n.reduce((a, b) => a / b),
remainder: (...n) => n.reduce((a, b) => a % b),
/*
Statistical Functions
*/
max: (...n) => Math.max(...n),
min: (...n) => Math.min(...n),
average: (...n) => n.reduce((a, b) => a + b, 0) / n.length,
mean: (...n) => math.average(...n),
median: (...n) => {
n.sort((a, b) => a - b);
const mid = Math.floor(n.length / 2);
return n.length % 2 !== 0 ? n[mid] : (n[mid - 1] + n[mid]) / 2;
},
mode: (...n) => {
let returnType = 'Str';
if (typeof n[n.length - 1] === 'string' && ['Str', 'Arr'].includes(n[n.length - 1])) {
returnType = n.pop();
}
const count = {};
const modes = [];
let maxCount = 0;
n.forEach(e => {
const key = typeof e === 'string' ? `s_${e}` : e.toString();
count[key] = (count[key] || 0) + 1;
if (count[key] > maxCount) {
modes.splice(0, modes.length, e);
maxCount = count[key];
}
else if (count[key] === maxCount) {
modes.push(e);
}
});
return returnType === 'Str' ? modes.map(e => typeof e === 'string' ? e : e.toString()).join(', ') : modes;
},
range: (...n) => {
let returnType = 'Str';
if (typeof n[n.length - 1] === 'string' && ['Str', 'Arr'].includes(n[n.length - 1])) {
returnType = n.pop();
}
const numbers = n.filter((item) => typeof item === 'number');
const min = Math.min(...numbers);
const max = Math.max(...numbers);
const range = [min, max];
return returnType === 'Str' ? range.join(', ') : range;
},
variance: (...n) => {
const mean = math.mean(...n);
return math.mean(...n.map(e => math.square(e - mean)));
},
standardDeviation: (...n) => Math.sqrt(math.variance(...n)),
/*
Logical Functions
*/
isEqual: (a, b) => a === b,
isNearlyEqual: (a, b, epsilon = Number.EPSILON) => Math.abs(a - b) < epsilon,
isEven: (n) => n % 2 === 0,
isOdd: (n) => n % 2 !== 0,
isPositive: (n) => n > 0,
isNegative: (n) => n < 0,
isZero: (n) => n === 0,
isInteger: (n) => Number.isInteger(n),
isFloat: (n) => Number(n) === n && n % 1 !== 0,
isPrime: (n) => {
if (n === 2) {
return true;
}
if (n === 1 || n % 2 === 0) {
return false;
}
const sqrt = Math.sqrt(n);
for (let i = 3; i <= sqrt; i += 2) {
if (n % i === 0) {
return false;
}
}
return true;
},
isComposite: (n) => !math.isPrime(n),
isDivisible: (n, d) => n % d === 0,
isPowerOf: (n, e) => Math.log(n) / Math.log(e) % 1 === 0,
isPerfectSquare: (n) => Math.sqrt(n) % 1 === 0,
isPerfectCube: (n) => Math.cbrt(n) % 1 === 0,
isMultiple: (n, m) => n % m === 0,
isFactor: (n, f) => f % n === 0,
isArmstrong: (n) => n === n.toString().split('').reduce((a, b) => a + Math.pow(parseInt(b), n.toString().length), 0),
isPalindrome: (n) => n.toString() === n.toString().split('').reverse().join(''),
isFinite: (n) => Number.isFinite(n),
isInfinite: (n) => !Number.isFinite(n),
/*
Factorial and Fibonacci Functions
*/
factorial: (n) => {
if (n === 0)
return 1;
let factorial = 1;
for (let i = 1; i <= n; i++) {
factorial *= i;
}
return factorial;
},
fibonacci: (n) => {
let a = 0, b = 1;
for (let i = 2; i <= n; i++) {
[a, b] = [b, a + b];
}
return b;
},
fibonacciSeries: (n, returnType = 'Str') => {
const series = Array.from({ length: n }, (_, i) => math.fibonacci(i));
return returnType === 'Str' ? series.join(', ') : series;
},
/*
Random Number Functions
*/
random: (a, b) => Math.floor(Math.random() * (b - a + 1)) + a,
/*
Advanced Arithmetic Functions
*/
log: (n, b) => {
if (b !== undefined) {
const log = Math.log(n) / Math.log(b);
return +log.toFixed(2);
}
const log = Math.log(n);
return +log.toFixed(2);
},
logBase2: (n) => Math.log2(n),
logBase5: (n) => Math.log(n) / Math.log(5),
logBase10: (n) => Math.log10(n),
square: (n) => n ** 2,
cube: (n) => n ** 3,
power: (n, e) => n ** e,
root: (n, e) => n ** (1 / e),
round: (n) => Math.round(n),
roundUp: (n) => Math.ceil(n),
roundDown: (n) => Math.floor(n),
absolute: (n) => Math.abs(n),
sqrt: (n) => Math.sqrt(n),
cbrt: (n) => Math.cbrt(n),
hypotenuse: (a, b) => Math.hypot(a, b),
factorsOf: (n, returnType = 'Arr') => {
const factors = [];
for (let i = 1; i <= n; i++) {
if (n % i === 0) {
factors.push(i);
}
}
return returnType === 'Str' ? factors.join(', ') : factors;
},
primeFactorsOf: (n, returnType = 'Arr') => {
const primeFactors = [];
for (let i = 2; i <= n; i++) {
while (n % i === 0) {
if (!primeFactors.includes(i)) {
primeFactors.push(i);
}
n /= i;
}
}
return returnType === 'Str' ? primeFactors.join(', ') : primeFactors;
},
primeFactorizationOf: (n, returnType = 'Arr') => {
const factors = [];
for (let i = 2; i <= Math.sqrt(n); i++) {
while (n % i === 0) {
factors.push(i);
n /= i;
}
}
if (n > 1) {
factors.push(n);
}
return returnType === 'Str' ? factors.join(', ') : factors;
},
greatestCommonDivisor: (...n) => {
const gcd = (x, y) => (!y ? x : gcd(y, x % y));
return n.reduce((a, b) => gcd(a, b));
},
GCD: (...n) => math.greatestCommonDivisor(...n),
highestCommonFactor: (...n) => math.greatestCommonDivisor(...n),
HCF: (...n) => math.greatestCommonDivisor(...n),
leastCommonMultiple: (...n) => {
const hcf = (x, y) => (!y ? x : hcf(y, x % y));
const lcm = (x, y) => (x * y) / n.reduce((a, b) => hcf(a, b));
return n.reduce((a, b) => lcm(a, b));
},
LCM: (...n) => math.leastCommonMultiple(...n),
/*
Trigonometric Functions
*/
sin: (n) => {
const angle = convertToRadians(n);
const sine = Math.sin(angle);
return +sine.toFixed(2);
},
cos: (n) => {
const angle = convertToRadians(n);
const cosine = Math.cos(angle);
return +cosine.toFixed(2);
},
tan: (n) => {
const angle = validateAngle(convertToRadians(n), 'tan');
const tangent = Math.tan(angle);
return +tangent.toFixed(2);
},
cot: (n) => {
const angle = validateAngle(convertToRadians(n), 'cot');
const cotangent = 1 / Math.tan(angle);
return +cotangent.toFixed(2);
},
sec: (n) => {
const angle = validateAngle(convertToRadians(n), 'sec');
const secant = 1 / Math.cos(angle);
return +secant.toFixed(2);
},
csc: (n) => {
const angle = validateAngle(convertToRadians(n), 'csc');
const cosecant = 1 / Math.sin(angle);
return +cosecant.toFixed(2);
},
/*
Evaluate Function
*/
evaluate: (expression, variables) => {
try {
if (typeof expression !== 'string') {
throw new Error('Expression must be a string');
}
expression = expression.replace(/(\d)(?=[a-zA-Z])/g, '$1*');
if (variables) {
for (let variable in variables) {
const regex = new RegExp('\\b' + variable + '\\b', 'g');
expression = expression.replace(regex, variables[variable].toString());
}
}
expression = expression
.replace(/\^/g, '**')
.replace(/÷/g, '/')
.replace(/×/g, '*')
.replace(/\bpi\b/gi, 'PI')
.replace(/\btau\b/gi, 'TAU');
for (let method in math) {
if (method !== 'pi' && method !== 'tau') {
const regex = new RegExp(`\\b${method}\\b`, 'g');
expression = expression.replace(regex, `math['${method}']`);
}
}
const allowedCharactersRegex = /^[\d\s+\-*/()a-zA-Z"''\[\].,]+$/;
for (let i = 0; i < expression.length; i++) {
if (!allowedCharactersRegex.test(expression[i])) {
throw new Error(`Expression contains disallowed character: ${expression[i]} at character position: ${i} in expression: ${expression}`);
}
}
math.debuggingMode ? console.log('Expression:', expression) : null;
return Function('math', `'use strict'; return (${expression})`)(math);
}
catch (error) {
throw new Error('Error occurred while evaluating the expression: ' + error);
}
},
/*
Chain Handler
*/
chain: (initialValue = 0) => {
let result = initialValue;
const chained = {};
for (const method in math) {
if (typeof math[method] === 'function' && method !== 'chain') {
chained[method] = (...args) => {
try {
if (typeof result === 'number') {
result = math[method](result, ...args);
}
}
catch (error) {
console.error(`Error executing method ${method}:`, error);
}
return chained;
};
}
}
chained.result = () => {
return result;
};
if (typeof initialValue === 'number') {
return chained;
}
else {
throw new Error('Initial value must be a number');
}
}
};
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
}
else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory();
}
else if (typeof module === 'object' && typeof module.exports === 'object') {
// CommonJS and ES6 Modules
module.exports = factory(); // This line is for CommonJS
module.exports.default = factory(); // This line is for ES6 default import
}
else {
// Browser globals (root is window)
root.math = factory();
}
}(typeof self !== 'undefined' ? self : this, function () {
return math;
}));
//# sourceMappingURL=math.js.map