1 |
|
2 |
|
3 | const deepMap = require('../../utils/collection/deepMap')
|
4 |
|
5 | function factory (type, config, load, typed) {
|
6 | const latex = require('../../utils/latex')
|
7 |
|
8 | /**
|
9 | * Calculate the value of subtracting 1 from the exponential value.
|
10 | * For matrices, the function is evaluated element wise.
|
11 | *
|
12 | * Syntax:
|
13 | *
|
14 | * math.expm1(x)
|
15 | *
|
16 | * Examples:
|
17 | *
|
18 | * math.expm1(2) // returns number 6.38905609893065
|
19 | * math.pow(math.e, 2) - 1 // returns number 6.3890560989306495
|
20 | * math.log(math.expm1(2) + 1) // returns number 2
|
21 | *
|
22 | * math.expm1([1, 2, 3])
|
23 | * // returns Array [
|
24 | * // 1.718281828459045,
|
25 | * // 6.3890560989306495,
|
26 | * // 19.085536923187668
|
27 | * // ]
|
28 | *
|
29 | * See also:
|
30 | *
|
31 | * exp, log, pow
|
32 | *
|
33 | * @param {number | BigNumber | Complex | Array | Matrix} x A number or matrix to apply expm1
|
34 | * @return {number | BigNumber | Complex | Array | Matrix} Exponent of `x`
|
35 | */
|
36 | const expm1 = typed('expm1', {
|
37 | 'number': Math.expm1 || _expm1,
|
38 |
|
39 | 'Complex': function (x) {
|
40 | const r = Math.exp(x.re)
|
41 | return new type.Complex(
|
42 | r * Math.cos(x.im) - 1,
|
43 | r * Math.sin(x.im)
|
44 | )
|
45 | },
|
46 |
|
47 | 'BigNumber': function (x) {
|
48 | return x.exp().minus(1)
|
49 | },
|
50 |
|
51 | 'Array | Matrix': function (x) {
|
52 | return deepMap(x, expm1)
|
53 | }
|
54 | })
|
55 |
|
56 | /**
|
57 | * Calculates exponentiation minus 1.
|
58 | * @param {number} x
|
59 | * @return {number} res
|
60 | * @private
|
61 | */
|
62 | function _expm1 (x) {
|
63 | return (x >= 2e-4 || x <= -2e-4)
|
64 | ? Math.exp(x) - 1
|
65 | : x + x * x / 2 + x * x * x / 6
|
66 | }
|
67 |
|
68 | expm1.toTex = `\\left(e${latex.operators['pow']}{\${args[0]}}-1\\right)`
|
69 |
|
70 | return expm1
|
71 | }
|
72 |
|
73 | exports.name = 'expm1'
|
74 | exports.factory = factory
|