simple-squiggle

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

log1p.js (2785B)


      1 "use strict";
      2 
      3 Object.defineProperty(exports, "__esModule", {
      4   value: true
      5 });
      6 exports.createLog1p = void 0;
      7 
      8 var _factory = require("../../utils/factory.js");
      9 
     10 var _collection = require("../../utils/collection.js");
     11 
     12 var _number = require("../../utils/number.js");
     13 
     14 var name = 'log1p';
     15 var dependencies = ['typed', 'config', 'divideScalar', 'log', 'Complex'];
     16 var createLog1p = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
     17   var typed = _ref.typed,
     18       config = _ref.config,
     19       divideScalar = _ref.divideScalar,
     20       log = _ref.log,
     21       Complex = _ref.Complex;
     22 
     23   /**
     24    * Calculate the logarithm of a `value+1`.
     25    *
     26    * For matrices, the function is evaluated element wise.
     27    *
     28    * Syntax:
     29    *
     30    *    math.log1p(x)
     31    *    math.log1p(x, base)
     32    *
     33    * Examples:
     34    *
     35    *    math.log1p(2.5)                 // returns 1.252762968495368
     36    *    math.exp(math.log1p(1.4))       // returns 2.4
     37    *
     38    *    math.pow(10, 4)                 // returns 10000
     39    *    math.log1p(9999, 10)            // returns 4
     40    *    math.log1p(9999) / math.log(10) // returns 4
     41    *
     42    * See also:
     43    *
     44    *    exp, log, log2, log10
     45    *
     46    * @param {number | BigNumber | Complex | Array | Matrix} x
     47    *            Value for which to calculate the logarithm of `x+1`.
     48    * @param {number | BigNumber | Complex} [base=e]
     49    *            Optional base for the logarithm. If not provided, the natural
     50    *            logarithm of `x+1` is calculated.
     51    * @return {number | BigNumber | Complex | Array | Matrix}
     52    *            Returns the logarithm of `x+1`
     53    */
     54   return typed(name, {
     55     number: function number(x) {
     56       if (x >= -1 || config.predictable) {
     57         return (0, _number.log1p)(x);
     58       } else {
     59         // negative value -> complex value computation
     60         return _log1pComplex(new Complex(x, 0));
     61       }
     62     },
     63     Complex: _log1pComplex,
     64     BigNumber: function BigNumber(x) {
     65       var y = x.plus(1);
     66 
     67       if (!y.isNegative() || config.predictable) {
     68         return y.ln();
     69       } else {
     70         // downgrade to number, return Complex valued result
     71         return _log1pComplex(new Complex(x.toNumber(), 0));
     72       }
     73     },
     74     'Array | Matrix': function ArrayMatrix(x) {
     75       return (0, _collection.deepMap)(x, this);
     76     },
     77     'any, any': function anyAny(x, base) {
     78       // calculate logarithm for a specified base, log1p(x, base)
     79       return divideScalar(this(x), log(base));
     80     }
     81   });
     82   /**
     83    * Calculate the natural logarithm of a complex number + 1
     84    * @param {Complex} x
     85    * @returns {Complex}
     86    * @private
     87    */
     88 
     89   function _log1pComplex(x) {
     90     var xRe1p = x.re + 1;
     91     return new Complex(Math.log(Math.sqrt(xRe1p * xRe1p + x.im * x.im)), Math.atan2(x.im, xRe1p));
     92   }
     93 });
     94 exports.createLog1p = createLog1p;