1 | ;
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.createCbrt = void 0;
|
7 |
|
8 | var _factory = require("../../utils/factory");
|
9 |
|
10 | var _is = require("../../utils/is");
|
11 |
|
12 | var _collection = require("../../utils/collection");
|
13 |
|
14 | var _number = require("../../plain/number");
|
15 |
|
16 | var name = 'cbrt';
|
17 | var dependencies = ['config', 'typed', 'isNegative', 'unaryMinus', 'matrix', 'Complex', 'BigNumber', 'Fraction'];
|
18 | var createCbrt = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
|
19 | var config = _ref.config,
|
20 | typed = _ref.typed,
|
21 | isNegative = _ref.isNegative,
|
22 | unaryMinus = _ref.unaryMinus,
|
23 | matrix = _ref.matrix,
|
24 | Complex = _ref.Complex,
|
25 | BigNumber = _ref.BigNumber,
|
26 | Fraction = _ref.Fraction;
|
27 |
|
28 | /**
|
29 | * Calculate the cubic root of a value.
|
30 | *
|
31 | * For matrices, the function is evaluated element wise.
|
32 | *
|
33 | * Syntax:
|
34 | *
|
35 | * math.cbrt(x)
|
36 | * math.cbrt(x, allRoots)
|
37 | *
|
38 | * Examples:
|
39 | *
|
40 | * math.cbrt(27) // returns 3
|
41 | * math.cube(3) // returns 27
|
42 | * math.cbrt(-64) // returns -4
|
43 | * math.cbrt(math.unit('27 m^3')) // returns Unit 3 m
|
44 | * math.cbrt([27, 64, 125]) // returns [3, 4, 5]
|
45 | *
|
46 | * const x = math.complex('8i')
|
47 | * math.cbrt(x) // returns Complex 1.7320508075689 + i
|
48 | * math.cbrt(x, true) // returns Matrix [
|
49 | * // 1.7320508075689 + i
|
50 | * // -1.7320508075689 + i
|
51 | * // -2i
|
52 | * // ]
|
53 | *
|
54 | * See also:
|
55 | *
|
56 | * square, sqrt, cube
|
57 | *
|
58 | * @param {number | BigNumber | Complex | Unit | Array | Matrix} x
|
59 | * Value for which to calculate the cubic root.
|
60 | * @param {boolean} [allRoots] Optional, false by default. Only applicable
|
61 | * when `x` is a number or complex number. If true, all complex
|
62 | * roots are returned, if false (default) the principal root is
|
63 | * returned.
|
64 | * @return {number | BigNumber | Complex | Unit | Array | Matrix}
|
65 | * Returns the cubic root of `x`
|
66 | */
|
67 | var cbrt = typed(name, {
|
68 | number: _number.cbrtNumber,
|
69 | // note: signature 'number, boolean' is also supported,
|
70 | // created by typed as it knows how to convert number to Complex
|
71 | Complex: _cbrtComplex,
|
72 | 'Complex, boolean': _cbrtComplex,
|
73 | BigNumber: function BigNumber(x) {
|
74 | return x.cbrt();
|
75 | },
|
76 | Unit: _cbrtUnit,
|
77 | 'Array | Matrix': function ArrayMatrix(x) {
|
78 | // deep map collection, skip zeros since cbrt(0) = 0
|
79 | return (0, _collection.deepMap)(x, cbrt, true);
|
80 | }
|
81 | });
|
82 | /**
|
83 | * Calculate the cubic root for a complex number
|
84 | * @param {Complex} x
|
85 | * @param {boolean} [allRoots] If true, the function will return an array
|
86 | * with all three roots. If false or undefined,
|
87 | * the principal root is returned.
|
88 | * @returns {Complex | Array.<Complex> | Matrix.<Complex>} Returns the cubic root(s) of x
|
89 | * @private
|
90 | */
|
91 |
|
92 | function _cbrtComplex(x, allRoots) {
|
93 | // https://www.wikiwand.com/en/Cube_root#/Complex_numbers
|
94 | var arg3 = x.arg() / 3;
|
95 | var abs = x.abs(); // principal root:
|
96 |
|
97 | var principal = new Complex((0, _number.cbrtNumber)(abs), 0).mul(new Complex(0, arg3).exp());
|
98 |
|
99 | if (allRoots) {
|
100 | var all = [principal, new Complex((0, _number.cbrtNumber)(abs), 0).mul(new Complex(0, arg3 + Math.PI * 2 / 3).exp()), new Complex((0, _number.cbrtNumber)(abs), 0).mul(new Complex(0, arg3 - Math.PI * 2 / 3).exp())];
|
101 | return config.matrix === 'Array' ? all : matrix(all);
|
102 | } else {
|
103 | return principal;
|
104 | }
|
105 | }
|
106 | /**
|
107 | * Calculate the cubic root for a Unit
|
108 | * @param {Unit} x
|
109 | * @return {Unit} Returns the cubic root of x
|
110 | * @private
|
111 | */
|
112 |
|
113 |
|
114 | function _cbrtUnit(x) {
|
115 | if (x.value && (0, _is.isComplex)(x.value)) {
|
116 | var result = x.clone();
|
117 | result.value = 1.0;
|
118 | result = result.pow(1.0 / 3); // Compute the units
|
119 |
|
120 | result.value = _cbrtComplex(x.value); // Compute the value
|
121 |
|
122 | return result;
|
123 | } else {
|
124 | var negate = isNegative(x.value);
|
125 |
|
126 | if (negate) {
|
127 | x.value = unaryMinus(x.value);
|
128 | } // TODO: create a helper function for this
|
129 |
|
130 |
|
131 | var third;
|
132 |
|
133 | if ((0, _is.isBigNumber)(x.value)) {
|
134 | third = new BigNumber(1).div(3);
|
135 | } else if ((0, _is.isFraction)(x.value)) {
|
136 | third = new Fraction(1, 3);
|
137 | } else {
|
138 | third = 1 / 3;
|
139 | }
|
140 |
|
141 | var _result = x.pow(third);
|
142 |
|
143 | if (negate) {
|
144 | _result.value = unaryMinus(_result.value);
|
145 | }
|
146 |
|
147 | return _result;
|
148 | }
|
149 | }
|
150 |
|
151 | return cbrt;
|
152 | });
|
153 | exports.createCbrt = createCbrt; |
\ | No newline at end of file |