UNPKG

6.86 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.createNthRootNumber = exports.createNthRoot = void 0;
7
8var _factory = require("../../utils/factory");
9
10var _algorithm = require("../../type/matrix/utils/algorithm01");
11
12var _algorithm2 = require("../../type/matrix/utils/algorithm02");
13
14var _algorithm3 = require("../../type/matrix/utils/algorithm06");
15
16var _algorithm4 = require("../../type/matrix/utils/algorithm11");
17
18var _algorithm5 = require("../../type/matrix/utils/algorithm13");
19
20var _algorithm6 = require("../../type/matrix/utils/algorithm14");
21
22var _number = require("../../plain/number");
23
24var name = 'nthRoot';
25var dependencies = ['typed', 'matrix', 'equalScalar', 'BigNumber'];
26var createNthRoot = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
27 var typed = _ref.typed,
28 matrix = _ref.matrix,
29 equalScalar = _ref.equalScalar,
30 _BigNumber = _ref.BigNumber;
31 var algorithm01 = (0, _algorithm.createAlgorithm01)({
32 typed: typed
33 });
34 var algorithm02 = (0, _algorithm2.createAlgorithm02)({
35 typed: typed,
36 equalScalar: equalScalar
37 });
38 var algorithm06 = (0, _algorithm3.createAlgorithm06)({
39 typed: typed,
40 equalScalar: equalScalar
41 });
42 var algorithm11 = (0, _algorithm4.createAlgorithm11)({
43 typed: typed,
44 equalScalar: equalScalar
45 });
46 var algorithm13 = (0, _algorithm5.createAlgorithm13)({
47 typed: typed
48 });
49 var algorithm14 = (0, _algorithm6.createAlgorithm14)({
50 typed: typed
51 });
52 /**
53 * Calculate the nth root of a value.
54 * The principal nth root of a positive real number A, is the positive real
55 * solution of the equation
56 *
57 * x^root = A
58 *
59 * For matrices, the function is evaluated element wise.
60 *
61 * Syntax:
62 *
63 * math.nthRoot(a)
64 * math.nthRoot(a, root)
65 *
66 * Examples:
67 *
68 * math.nthRoot(9, 2) // returns 3, as 3^2 == 9
69 * math.sqrt(9) // returns 3, as 3^2 == 9
70 * math.nthRoot(64, 3) // returns 4, as 4^3 == 64
71 *
72 * See also:
73 *
74 * sqrt, pow
75 *
76 * @param {number | BigNumber | Array | Matrix | Complex} a
77 * Value for which to calculate the nth root
78 * @param {number | BigNumber} [root=2] The root.
79 * @return {number | Complex | Array | Matrix} Returns the nth root of `a`
80 */
81
82 var complexErr = '' + 'Complex number not supported in function nthRoot. ' + 'Use nthRoots instead.';
83 var nthRoot = typed(name, {
84 number: function number(x) {
85 return (0, _number.nthRootNumber)(x, 2);
86 },
87 'number, number': _number.nthRootNumber,
88 BigNumber: function BigNumber(x) {
89 return _bigNthRoot(x, new _BigNumber(2));
90 },
91 Complex: function Complex(x) {
92 throw new Error(complexErr);
93 },
94 'Complex, number': function ComplexNumber(x, y) {
95 throw new Error(complexErr);
96 },
97 'BigNumber, BigNumber': _bigNthRoot,
98 'Array | Matrix': function ArrayMatrix(x) {
99 return nthRoot(x, 2);
100 },
101 'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) {
102 // density must be one (no zeros in matrix)
103 if (y.density() === 1) {
104 // sparse + sparse
105 return algorithm06(x, y, nthRoot);
106 } else {
107 // throw exception
108 throw new Error('Root must be non-zero');
109 }
110 },
111 'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) {
112 return algorithm02(y, x, nthRoot, true);
113 },
114 'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) {
115 // density must be one (no zeros in matrix)
116 if (y.density() === 1) {
117 // dense + sparse
118 return algorithm01(x, y, nthRoot, false);
119 } else {
120 // throw exception
121 throw new Error('Root must be non-zero');
122 }
123 },
124 'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) {
125 return algorithm13(x, y, nthRoot);
126 },
127 'Array, Array': function ArrayArray(x, y) {
128 // use matrix implementation
129 return nthRoot(matrix(x), matrix(y)).valueOf();
130 },
131 'Array, Matrix': function ArrayMatrix(x, y) {
132 // use matrix implementation
133 return nthRoot(matrix(x), y);
134 },
135 'Matrix, Array': function MatrixArray(x, y) {
136 // use matrix implementation
137 return nthRoot(x, matrix(y));
138 },
139 'SparseMatrix, number | BigNumber': function SparseMatrixNumberBigNumber(x, y) {
140 return algorithm11(x, y, nthRoot, false);
141 },
142 'DenseMatrix, number | BigNumber': function DenseMatrixNumberBigNumber(x, y) {
143 return algorithm14(x, y, nthRoot, false);
144 },
145 'number | BigNumber, SparseMatrix': function numberBigNumberSparseMatrix(x, y) {
146 // density must be one (no zeros in matrix)
147 if (y.density() === 1) {
148 // sparse - scalar
149 return algorithm11(y, x, nthRoot, true);
150 } else {
151 // throw exception
152 throw new Error('Root must be non-zero');
153 }
154 },
155 'number | BigNumber, DenseMatrix': function numberBigNumberDenseMatrix(x, y) {
156 return algorithm14(y, x, nthRoot, true);
157 },
158 'Array, number | BigNumber': function ArrayNumberBigNumber(x, y) {
159 // use matrix implementation
160 return nthRoot(matrix(x), y).valueOf();
161 },
162 'number | BigNumber, Array': function numberBigNumberArray(x, y) {
163 // use matrix implementation
164 return nthRoot(x, matrix(y)).valueOf();
165 }
166 });
167 return nthRoot;
168 /**
169 * Calculate the nth root of a for BigNumbers, solve x^root == a
170 * https://rosettacode.org/wiki/Nth_root#JavaScript
171 * @param {BigNumber} a
172 * @param {BigNumber} root
173 * @private
174 */
175
176 function _bigNthRoot(a, root) {
177 var precision = _BigNumber.precision;
178
179 var Big = _BigNumber.clone({
180 precision: precision + 2
181 });
182
183 var zero = new _BigNumber(0);
184 var one = new Big(1);
185 var inv = root.isNegative();
186
187 if (inv) {
188 root = root.neg();
189 }
190
191 if (root.isZero()) {
192 throw new Error('Root must be non-zero');
193 }
194
195 if (a.isNegative() && !root.abs().mod(2).equals(1)) {
196 throw new Error('Root must be odd when a is negative.');
197 } // edge cases zero and infinity
198
199
200 if (a.isZero()) {
201 return inv ? new Big(Infinity) : 0;
202 }
203
204 if (!a.isFinite()) {
205 return inv ? zero : a;
206 }
207
208 var x = a.abs().pow(one.div(root)); // If a < 0, we require that root is an odd integer,
209 // so (-1) ^ (1/root) = -1
210
211 x = a.isNeg() ? x.neg() : x;
212 return new _BigNumber((inv ? one.div(x) : x).toPrecision(precision));
213 }
214});
215exports.createNthRoot = createNthRoot;
216var createNthRootNumber = /* #__PURE__ */(0, _factory.factory)(name, ['typed'], function (_ref2) {
217 var typed = _ref2.typed;
218 return typed(name, {
219 number: _number.nthRootNumber,
220 'number, number': _number.nthRootNumber
221 });
222});
223exports.createNthRootNumber = createNthRootNumber;
\No newline at end of file