UNPKG

3.67 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.createNthRoots = void 0;
7
8var _factory = require("../../utils/factory");
9
10var name = 'nthRoots';
11var dependencies = ['config', 'typed', 'divideScalar', 'Complex'];
12var createNthRoots = /* #__PURE__ */(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 * Calculate the nth roots of a value.
20 * An nth root of a positive real number A,
21 * is a positive real solution of the equation "x^root = A".
22 * This function returns an array of complex values.
23 *
24 * Syntax:
25 *
26 * math.nthRoots(x)
27 * math.nthRoots(x, root)
28 *
29 * Examples:
30 *
31 * math.nthRoots(1)
32 * // returns [
33 * // {re: 1, im: 0},
34 * // {re: -1, im: 0}
35 * // ]
36 * nthRoots(1, 3)
37 * // returns [
38 * // { re: 1, im: 0 },
39 * // { re: -0.4999999999999998, im: 0.8660254037844387 },
40 * // { re: -0.5000000000000004, im: -0.8660254037844385 }
41 * ]
42 *
43 * See also:
44 *
45 * nthRoot, pow, sqrt
46 *
47 * @param {number | BigNumber | Fraction | Complex | Array | Matrix} x Number to be rounded
48 * @return {number | BigNumber | Fraction | Complex | Array | Matrix} Rounded value
49 */
50 var nthRoots = typed(name, {
51 Complex: function Complex(x) {
52 return _nthComplexRoots(x, 2);
53 },
54 'Complex, number': _nthComplexRoots
55 });
56 /**
57 * Each function here returns a real multiple of i as a Complex value.
58 * @param {number} val
59 * @return {Complex} val, i*val, -val or -i*val for index 0, 1, 2, 3
60 */
61 // This is used to fix float artifacts for zero-valued components.
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 * Calculate the nth root of a Complex Number a using De Movire's Theorem.
74 * @param {Complex} a
75 * @param {number} root
76 * @return {Array} array of n Complex Roots
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; // determine the offset (argument of a)/(pi/2)
86
87 if (aIsNumeric || a.re === 0 || a.im === 0) {
88 if (aIsNumeric) {
89 offset = 2 * +(a < 0); // numeric value on the real axis
90 } else if (a.im === 0) {
91 offset = 2 * +(a.re < 0); // complex value on the real axis
92 } else {
93 offset = 2 * +(a.im < 0) + 1; // complex value on the imaginary axis
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 * If (offset + 4*k)/root is an integral multiple of pi/2
106 * then we can produce a more exact result.
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});
125exports.createNthRoots = createNthRoots;
\No newline at end of file