UNPKG

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