UNPKG

3.51 kBJavaScriptView Raw
1'use strict'
2
3function factory (type, config, load, typed) {
4 const matrix = load(require('../../type/matrix/function/matrix'))
5
6 const algorithm02 = load(require('../../type/matrix/utils/algorithm02'))
7 const algorithm03 = load(require('../../type/matrix/utils/algorithm03'))
8 const algorithm09 = load(require('../../type/matrix/utils/algorithm09'))
9 const algorithm11 = load(require('../../type/matrix/utils/algorithm11'))
10 const algorithm12 = load(require('../../type/matrix/utils/algorithm12'))
11 const algorithm13 = load(require('../../type/matrix/utils/algorithm13'))
12 const algorithm14 = load(require('../../type/matrix/utils/algorithm14'))
13
14 /**
15 * Calculate the inverse tangent function with two arguments, y/x.
16 * By providing two arguments, the right quadrant of the computed angle can be
17 * determined.
18 *
19 * For matrices, the function is evaluated element wise.
20 *
21 * Syntax:
22 *
23 * math.atan2(y, x)
24 *
25 * Examples:
26 *
27 * math.atan2(2, 2) / math.pi // returns number 0.25
28 *
29 * const angle = math.unit(60, 'deg') // returns Unit 60 deg
30 * const x = math.cos(angle)
31 * const y = math.sin(angle)
32 *
33 * math.atan(2) // returns Complex 1.5707963267948966 -1.3169578969248166 i
34 *
35 * See also:
36 *
37 * tan, atan, sin, cos
38 *
39 * @param {number | Array | Matrix} y Second dimension
40 * @param {number | Array | Matrix} x First dimension
41 * @return {number | Array | Matrix} Four-quadrant inverse tangent
42 */
43 const atan2 = typed('atan2', {
44
45 'number, number': Math.atan2,
46
47 // Complex numbers doesn't seem to have a reasonable implementation of
48 // atan2(). Even Matlab removed the support, after they only calculated
49 // the atan only on base of the real part of the numbers and ignored the imaginary.
50
51 'BigNumber, BigNumber': function (y, x) {
52 return type.BigNumber.atan2(y, x)
53 },
54
55 'SparseMatrix, SparseMatrix': function (x, y) {
56 return algorithm09(x, y, atan2, false)
57 },
58
59 'SparseMatrix, DenseMatrix': function (x, y) {
60 // mind the order of y and x!
61 return algorithm02(y, x, atan2, true)
62 },
63
64 'DenseMatrix, SparseMatrix': function (x, y) {
65 return algorithm03(x, y, atan2, false)
66 },
67
68 'DenseMatrix, DenseMatrix': function (x, y) {
69 return algorithm13(x, y, atan2)
70 },
71
72 'Array, Array': function (x, y) {
73 return atan2(matrix(x), matrix(y)).valueOf()
74 },
75
76 'Array, Matrix': function (x, y) {
77 return atan2(matrix(x), y)
78 },
79
80 'Matrix, Array': function (x, y) {
81 return atan2(x, matrix(y))
82 },
83
84 'SparseMatrix, number | BigNumber': function (x, y) {
85 return algorithm11(x, y, atan2, false)
86 },
87
88 'DenseMatrix, number | BigNumber': function (x, y) {
89 return algorithm14(x, y, atan2, false)
90 },
91
92 'number | BigNumber, SparseMatrix': function (x, y) {
93 // mind the order of y and x
94 return algorithm12(y, x, atan2, true)
95 },
96
97 'number | BigNumber, DenseMatrix': function (x, y) {
98 // mind the order of y and x
99 return algorithm14(y, x, atan2, true)
100 },
101
102 'Array, number | BigNumber': function (x, y) {
103 return algorithm14(matrix(x), y, atan2, false).valueOf()
104 },
105
106 'number | BigNumber, Array': function (x, y) {
107 return algorithm14(matrix(y), x, atan2, true).valueOf()
108 }
109 })
110
111 atan2.toTex = { 2: `\\mathrm{atan2}\\left(\${args}\\right)` }
112
113 return atan2
114}
115
116exports.name = 'atan2'
117exports.factory = factory