UNPKG

1.4 kBJavaScriptView Raw
1import { factory } from '../../utils/factory'
2import { deepMap } from '../../utils/collection'
3import { expNumber } from '../../plain/number'
4
5const name = 'exp'
6const dependencies = ['typed']
7
8export const createExp = /* #__PURE__ */ factory(name, dependencies, ({ typed }) => {
9 /**
10 * Calculate the exponent of a value.
11 * For matrices, the function is evaluated element wise.
12 *
13 * Syntax:
14 *
15 * math.exp(x)
16 *
17 * Examples:
18 *
19 * math.exp(2) // returns number 7.3890560989306495
20 * math.pow(math.e, 2) // returns number 7.3890560989306495
21 * math.log(math.exp(2)) // returns number 2
22 *
23 * math.exp([1, 2, 3])
24 * // returns Array [
25 * // 2.718281828459045,
26 * // 7.3890560989306495,
27 * // 20.085536923187668
28 * // ]
29 *
30 * See also:
31 *
32 * expm1, log, pow
33 *
34 * @param {number | BigNumber | Complex | Array | Matrix} x A number or matrix to exponentiate
35 * @return {number | BigNumber | Complex | Array | Matrix} Exponent of `x`
36 */
37 const exp = typed(name, {
38 number: expNumber,
39
40 Complex: function (x) {
41 return x.exp()
42 },
43
44 BigNumber: function (x) {
45 return x.exp()
46 },
47
48 'Array | Matrix': function (x) {
49 // TODO: exp(sparse) should return a dense matrix since exp(0)==1
50 return deepMap(x, exp)
51 }
52 })
53
54 return exp
55})