UNPKG

1.43 kBJavaScriptView Raw
1import { factory } from '../../utils/factory'
2import { deepMap } from '../../utils/collection'
3
4const name = 'arg'
5const dependencies = ['typed']
6
7export const createArg = /* #__PURE__ */ factory(name, dependencies, ({ typed }) => {
8 /**
9 * Compute the argument of a complex value.
10 * For a complex number `a + bi`, the argument is computed as `atan2(b, a)`.
11 *
12 * For matrices, the function is evaluated element wise.
13 *
14 * Syntax:
15 *
16 * math.arg(x)
17 *
18 * Examples:
19 *
20 * const a = math.complex(2, 2)
21 * math.arg(a) / math.pi // returns number 0.25
22 *
23 * const b = math.complex('2 + 3i')
24 * math.arg(b) // returns number 0.982793723247329
25 * math.atan2(3, 2) // returns number 0.982793723247329
26 *
27 * See also:
28 *
29 * re, im, conj, abs
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 argument of x
34 */
35 const arg = typed(name, {
36 number: function (x) {
37 return Math.atan2(0, x)
38 },
39
40 BigNumber: function (x) {
41 return x.constructor.atan2(0, x)
42 },
43
44 Complex: function (x) {
45 return x.arg()
46 },
47
48 // TODO: implement BigNumber support for function arg
49
50 'Array | Matrix': function (x) {
51 return deepMap(x, arg)
52 }
53 })
54
55 return arg
56})