Advertisement
Guest User

Math.mjs

a guest
Jun 21st, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const range = (from, to) =>
  2.   Array.from(Array(to - from), (...[, i]) => i + from);
  3.  
  4. Object.assign(Math, {
  5.   abs: (x) => Math.max(x, -x),
  6.   acos: (x) => Math.PI / 2 - Math.asin(x),
  7.   acosh: (x) => Math.abs(Math.asinh(Math.sqrt(x**2 - 1))),
  8.   asin: (x) => Math.atan(x / Math.sqrt(1 - x**2)),
  9.   asinh: (x) => Math.atanh(x / Math.sqrt(1 + x**2)),
  10.   atan: (x) =>
  11.     !Number.isFinite(x)
  12.       ? Math.sign(x) * (Math.PI / 2)
  13.       : Math.sum(
  14.           (n) =>
  15.             ((2**(2 * n) * Math.factorial(n)**2) /
  16.               Math.factorial(2 * n + 1)) *
  17.             (x**(2 * n + 1) / (1 + x**2)**(n + 1)),
  18.           0,
  19.           Infinity,
  20.         ),
  21.   atan2: (y, x) => 2 * Math.atan(y / (Math.sqrt(x**2 + y**2) + x)),
  22.   atanh: (x) => (1 / 2) * Math.log((1 + x) / (1 - x)),
  23.   cbrt: (x) => x**(1 / 3),
  24.   ceil: (x) => Math.trunc(x + (x > 0 && !Number.isInteger(x))),
  25.   cos: (x) =>
  26.     Math.sum(
  27.       (n) => ((-1)**n * x**(2 * n)) / Math.factorial(2 * n),
  28.       0,
  29.       Infinity,
  30.     ),
  31.   cosh: (x) => (Math.exp(x) + Math.exp(-x)) / 2,
  32.   exp: (x) => Math.sum((n) => x**n / Math.factorial(n), 0, Infinity),
  33.   floor: (x) => Math.trunc(x - (x < 0 && !Number.isInteger(x))),
  34.   log: (x) => Math.integrate((y) => 1 / y, 1, x),
  35.   max: (...args) => args.reduce((a, b) => (a > b ? a : b), -Infinity),
  36.   min: (...args) => args.reduce((a, b) => (a < b ? a : b), Infinity),
  37.   pow: (x, y) => x**y,
  38.   random: ((x) => () =>
  39.     (((x ^= x << 13), (x ^= x >> 17), (x ^= x << 5)) >>> 0) / 2**32)(
  40.     Date.now(),
  41.   ),
  42.   round: (x) => Math.trunc(x + (x < 0 ? -0.5 : 0.5)),
  43.   sign: (x) => (x > 0) - (x < 0),
  44.   sin: (x) => Math.cos(Math.PI / 2 - x),
  45.   sinh: (x) => Math.exp(x) - Math.cosh(x),
  46.   sqrt: (x) => x**(1 / 2),
  47.   tan: (x) => Math.sin(x) / Math.cos(x),
  48.   tanh: (x) => Math.sinh(x) / Math.cosh(x),
  49.   trunc: (x) => ~~x,
  50.   /* auxiliary functions */
  51.   acot: (x) => Math.atan(1 / x),
  52.   acoth: (x) => Math.atanh(1 / x),
  53.   acsc: (x) => Math.asin(1 / x),
  54.   acsch: (x) => Math.asinh(1 / x),
  55.   asec: (x) => Math.acos(1 / x),
  56.   asech: (x) => Math.acosh(1 / x),
  57.   cot: (x) => 1 / Math.tan(x),
  58.   coth: (x) => 1 / Math.tanh(x),
  59.   csc: (x) => 1 / Math.sin(x),
  60.   csch: (x) => 1 / Math.sinh(x),
  61.   D: (f, n = 1) =>
  62.     n === 0
  63.       ? f
  64.       : n === 1
  65.       ? (x) => Math.lim((h) => (f(x + h) - f(x)) / h, 0)
  66.       : Math.D(Math.D(f, n - 1)),
  67.   factorial: (n) => (n === 0 ? 1 : n * Math.factorial(n - 1)),
  68.   integrate(f, a, b) {
  69.     const N = 1e6;
  70.     const h = (b - a) / N;
  71.     return h * ((f(a) + f(b)) / 2 + Math.sum((k) => f(a + k * h), 1, N - 1));
  72.   },
  73.   lim(f, p) {
  74.     const x =
  75.       p === Infinity
  76.         ? (i) => 2**i
  77.         : p === -Infinity
  78.         ? (i) => 2**-i
  79.         : (i) => p + 2**-i;
  80.  
  81.     for (let i = 0; ; i += 1) {
  82.       const L = f(x(i + 1));
  83.       const y = f(x(i));
  84.  
  85.       if (isNaN(L) || Math.abs(y - L) < Number.MIN_VALUE) {
  86.         return y;
  87.       }
  88.     }
  89.   },
  90.   sec: (x) => 1 / Math.cos(x),
  91.   sech: (x) => 1 / Math.cosh(x),
  92.   sum: (f, m, n) =>
  93.     n === Infinity
  94.       ? Math.lim((n) => Math.sum(f, m, n), Infinity)
  95.       : range(m, n + 1)
  96.           .map((i) => f(i))
  97.           .reduce((a, b) => a + b, 0),
  98. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement