nthRoot.js (6350B)
1 import { factory } from '../../utils/factory.js'; 2 import { createAlgorithm01 } from '../../type/matrix/utils/algorithm01.js'; 3 import { createAlgorithm02 } from '../../type/matrix/utils/algorithm02.js'; 4 import { createAlgorithm06 } from '../../type/matrix/utils/algorithm06.js'; 5 import { createAlgorithm11 } from '../../type/matrix/utils/algorithm11.js'; 6 import { createAlgorithm13 } from '../../type/matrix/utils/algorithm13.js'; 7 import { createAlgorithm14 } from '../../type/matrix/utils/algorithm14.js'; 8 import { nthRootNumber } from '../../plain/number/index.js'; 9 var name = 'nthRoot'; 10 var dependencies = ['typed', 'matrix', 'equalScalar', 'BigNumber']; 11 export var createNthRoot = /* #__PURE__ */factory(name, dependencies, _ref => { 12 var { 13 typed, 14 matrix, 15 equalScalar, 16 BigNumber: _BigNumber 17 } = _ref; 18 var algorithm01 = createAlgorithm01({ 19 typed 20 }); 21 var algorithm02 = createAlgorithm02({ 22 typed, 23 equalScalar 24 }); 25 var algorithm06 = createAlgorithm06({ 26 typed, 27 equalScalar 28 }); 29 var algorithm11 = createAlgorithm11({ 30 typed, 31 equalScalar 32 }); 33 var algorithm13 = createAlgorithm13({ 34 typed 35 }); 36 var algorithm14 = createAlgorithm14({ 37 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 return 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 this(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, this); 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, this, 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, this, 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, this); 113 }, 114 'Array, Array': function ArrayArray(x, y) { 115 // use matrix implementation 116 return this(matrix(x), matrix(y)).valueOf(); 117 }, 118 'Array, Matrix': function ArrayMatrix(x, y) { 119 // use matrix implementation 120 return this(matrix(x), y); 121 }, 122 'Matrix, Array': function MatrixArray(x, y) { 123 // use matrix implementation 124 return this(x, matrix(y)); 125 }, 126 'SparseMatrix, number | BigNumber': function SparseMatrixNumberBigNumber(x, y) { 127 return algorithm11(x, y, this, false); 128 }, 129 'DenseMatrix, number | BigNumber': function DenseMatrixNumberBigNumber(x, y) { 130 return algorithm14(x, y, this, 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, this, 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, this, true); 144 }, 145 'Array, number | BigNumber': function ArrayNumberBigNumber(x, y) { 146 // use matrix implementation 147 return this(matrix(x), y).valueOf(); 148 }, 149 'number | BigNumber, Array': function numberBigNumberArray(x, y) { 150 // use matrix implementation 151 return this(x, matrix(y)).valueOf(); 152 } 153 }); 154 /** 155 * Calculate the nth root of a for BigNumbers, solve x^root == a 156 * https://rosettacode.org/wiki/Nth_root#JavaScript 157 * @param {BigNumber} a 158 * @param {BigNumber} root 159 * @private 160 */ 161 162 function _bigNthRoot(a, root) { 163 var precision = _BigNumber.precision; 164 165 var Big = _BigNumber.clone({ 166 precision: precision + 2 167 }); 168 169 var zero = new _BigNumber(0); 170 var one = new Big(1); 171 var inv = root.isNegative(); 172 173 if (inv) { 174 root = root.neg(); 175 } 176 177 if (root.isZero()) { 178 throw new Error('Root must be non-zero'); 179 } 180 181 if (a.isNegative() && !root.abs().mod(2).equals(1)) { 182 throw new Error('Root must be odd when a is negative.'); 183 } // edge cases zero and infinity 184 185 186 if (a.isZero()) { 187 return inv ? new Big(Infinity) : 0; 188 } 189 190 if (!a.isFinite()) { 191 return inv ? zero : a; 192 } 193 194 var x = a.abs().pow(one.div(root)); // If a < 0, we require that root is an odd integer, 195 // so (-1) ^ (1/root) = -1 196 197 x = a.isNeg() ? x.neg() : x; 198 return new _BigNumber((inv ? one.div(x) : x).toPrecision(precision)); 199 } 200 }); 201 export var createNthRootNumber = /* #__PURE__ */factory(name, ['typed'], _ref2 => { 202 var { 203 typed 204 } = _ref2; 205 return typed(name, { 206 number: nthRootNumber, 207 'number, number': nthRootNumber 208 }); 209 });