UNPKG

1.29 kBJavaScriptView Raw
1import { factory } from '../../utils/factory'
2import { deepMap } from '../../utils/collection'
3
4const name = 're'
5const dependencies = ['typed']
6
7export const createRe = /* #__PURE__ */ factory(name, dependencies, ({ typed }) => {
8 /**
9 * Get the real part of a complex number.
10 * For a complex number `a + bi`, the function returns `a`.
11 *
12 * For matrices, the function is evaluated element wise.
13 *
14 * Syntax:
15 *
16 * math.re(x)
17 *
18 * Examples:
19 *
20 * const a = math.complex(2, 3)
21 * math.re(a) // returns number 2
22 * math.im(a) // returns number 3
23 *
24 * math.re(math.complex('-5.2i')) // returns number 0
25 * math.re(math.complex(2.4)) // returns number 2.4
26 *
27 * See also:
28 *
29 * im, conj, abs, arg
30 *
31 * @param {number | BigNumber | Complex | Array | Matrix} x
32 * A complex number or array with complex numbers
33 * @return {number | BigNumber | Array | Matrix} The real part of x
34 */
35 const re = typed(name, {
36 number: function (x) {
37 return x
38 },
39
40 BigNumber: function (x) {
41 return x
42 },
43
44 Complex: function (x) {
45 return x.re
46 },
47
48 'Array | Matrix': function (x) {
49 return deepMap(x, re)
50 }
51 })
52
53 return re
54})