1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.createNthRoots = void 0;
|
7 |
|
8 | var _factory = require("../../utils/factory");
|
9 |
|
10 | var name = 'nthRoots';
|
11 | var dependencies = ['config', 'typed', 'divideScalar', 'Complex'];
|
12 | var createNthRoots = (0, _factory.factory)(name, dependencies, function (_ref) {
|
13 | var typed = _ref.typed,
|
14 | config = _ref.config,
|
15 | divideScalar = _ref.divideScalar,
|
16 | Complex = _ref.Complex;
|
17 |
|
18 | |
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | var nthRoots = typed(name, {
|
51 | Complex: function Complex(x) {
|
52 | return _nthComplexRoots(x, 2);
|
53 | },
|
54 | 'Complex, number': _nthComplexRoots
|
55 | });
|
56 | |
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 | var _calculateExactResult = [function realPos(val) {
|
64 | return new Complex(val, 0);
|
65 | }, function imagPos(val) {
|
66 | return new Complex(0, val);
|
67 | }, function realNeg(val) {
|
68 | return new Complex(-val, 0);
|
69 | }, function imagNeg(val) {
|
70 | return new Complex(0, -val);
|
71 | }];
|
72 | |
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | function _nthComplexRoots(a, root) {
|
80 | if (root < 0) throw new Error('Root must be greater than zero');
|
81 | if (root === 0) throw new Error('Root must be non-zero');
|
82 | if (root % 1 !== 0) throw new Error('Root must be an integer');
|
83 | if (a === 0 || a.abs() === 0) return [new Complex(0, 0)];
|
84 | var aIsNumeric = typeof a === 'number';
|
85 | var offset;
|
86 |
|
87 | if (aIsNumeric || a.re === 0 || a.im === 0) {
|
88 | if (aIsNumeric) {
|
89 | offset = 2 * +(a < 0);
|
90 | } else if (a.im === 0) {
|
91 | offset = 2 * +(a.re < 0);
|
92 | } else {
|
93 | offset = 2 * +(a.im < 0) + 1;
|
94 | }
|
95 | }
|
96 |
|
97 | var arg = a.arg();
|
98 | var abs = a.abs();
|
99 | var roots = [];
|
100 | var r = Math.pow(abs, 1 / root);
|
101 |
|
102 | for (var k = 0; k < root; k++) {
|
103 | var halfPiFactor = (offset + 4 * k) / root;
|
104 | |
105 |
|
106 |
|
107 |
|
108 |
|
109 | if (halfPiFactor === Math.round(halfPiFactor)) {
|
110 | roots.push(_calculateExactResult[halfPiFactor % 4](r));
|
111 | continue;
|
112 | }
|
113 |
|
114 | roots.push(new Complex({
|
115 | r: r,
|
116 | phi: (arg + 2 * Math.PI * k) / root
|
117 | }));
|
118 | }
|
119 |
|
120 | return roots;
|
121 | }
|
122 |
|
123 | return nthRoots;
|
124 | });
|
125 | exports.createNthRoots = createNthRoots; |
\ | No newline at end of file |