simple-squiggle

A restricted subset of Squiggle
Log | Files | Refs | README

nthRoot.js (6822B)


      1 "use strict";
      2 
      3 Object.defineProperty(exports, "__esModule", {
      4   value: true
      5 });
      6 exports.createNthRootNumber = exports.createNthRoot = void 0;
      7 
      8 var _factory = require("../../utils/factory.js");
      9 
     10 var _algorithm = require("../../type/matrix/utils/algorithm01.js");
     11 
     12 var _algorithm2 = require("../../type/matrix/utils/algorithm02.js");
     13 
     14 var _algorithm3 = require("../../type/matrix/utils/algorithm06.js");
     15 
     16 var _algorithm4 = require("../../type/matrix/utils/algorithm11.js");
     17 
     18 var _algorithm5 = require("../../type/matrix/utils/algorithm13.js");
     19 
     20 var _algorithm6 = require("../../type/matrix/utils/algorithm14.js");
     21 
     22 var _index = require("../../plain/number/index.js");
     23 
     24 var name = 'nthRoot';
     25 var dependencies = ['typed', 'matrix', 'equalScalar', 'BigNumber'];
     26 var 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   return typed(name, {
     84     number: function number(x) {
     85       return (0, _index.nthRootNumber)(x, 2);
     86     },
     87     'number, number': _index.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 this(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, this);
    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, this, 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, this, 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, this);
    126     },
    127     'Array, Array': function ArrayArray(x, y) {
    128       // use matrix implementation
    129       return this(matrix(x), matrix(y)).valueOf();
    130     },
    131     'Array, Matrix': function ArrayMatrix(x, y) {
    132       // use matrix implementation
    133       return this(matrix(x), y);
    134     },
    135     'Matrix, Array': function MatrixArray(x, y) {
    136       // use matrix implementation
    137       return this(x, matrix(y));
    138     },
    139     'SparseMatrix, number | BigNumber': function SparseMatrixNumberBigNumber(x, y) {
    140       return algorithm11(x, y, this, false);
    141     },
    142     'DenseMatrix, number | BigNumber': function DenseMatrixNumberBigNumber(x, y) {
    143       return algorithm14(x, y, this, 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, this, 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, this, true);
    157     },
    158     'Array, number | BigNumber': function ArrayNumberBigNumber(x, y) {
    159       // use matrix implementation
    160       return this(matrix(x), y).valueOf();
    161     },
    162     'number | BigNumber, Array': function numberBigNumberArray(x, y) {
    163       // use matrix implementation
    164       return this(x, matrix(y)).valueOf();
    165     }
    166   });
    167   /**
    168    * Calculate the nth root of a for BigNumbers, solve x^root == a
    169    * https://rosettacode.org/wiki/Nth_root#JavaScript
    170    * @param {BigNumber} a
    171    * @param {BigNumber} root
    172    * @private
    173    */
    174 
    175   function _bigNthRoot(a, root) {
    176     var precision = _BigNumber.precision;
    177 
    178     var Big = _BigNumber.clone({
    179       precision: precision + 2
    180     });
    181 
    182     var zero = new _BigNumber(0);
    183     var one = new Big(1);
    184     var inv = root.isNegative();
    185 
    186     if (inv) {
    187       root = root.neg();
    188     }
    189 
    190     if (root.isZero()) {
    191       throw new Error('Root must be non-zero');
    192     }
    193 
    194     if (a.isNegative() && !root.abs().mod(2).equals(1)) {
    195       throw new Error('Root must be odd when a is negative.');
    196     } // edge cases zero and infinity
    197 
    198 
    199     if (a.isZero()) {
    200       return inv ? new Big(Infinity) : 0;
    201     }
    202 
    203     if (!a.isFinite()) {
    204       return inv ? zero : a;
    205     }
    206 
    207     var x = a.abs().pow(one.div(root)); // If a < 0, we require that root is an odd integer,
    208     // so (-1) ^ (1/root) = -1
    209 
    210     x = a.isNeg() ? x.neg() : x;
    211     return new _BigNumber((inv ? one.div(x) : x).toPrecision(precision));
    212   }
    213 });
    214 exports.createNthRoot = createNthRoot;
    215 var createNthRootNumber = /* #__PURE__ */(0, _factory.factory)(name, ['typed'], function (_ref2) {
    216   var typed = _ref2.typed;
    217   return typed(name, {
    218     number: _index.nthRootNumber,
    219     'number, number': _index.nthRootNumber
    220   });
    221 });
    222 exports.createNthRootNumber = createNthRootNumber;