simple-squiggle

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

log.js (2301B)


      1 import { factory } from '../../utils/factory.js';
      2 import { deepMap } from '../../utils/collection.js';
      3 import { logNumber } from '../../plain/number/index.js';
      4 var name = 'log';
      5 var dependencies = ['config', 'typed', 'divideScalar', 'Complex'];
      6 export var createLog = /* #__PURE__ */factory(name, dependencies, _ref => {
      7   var {
      8     typed,
      9     config,
     10     divideScalar,
     11     Complex
     12   } = _ref;
     13 
     14   /**
     15    * Calculate the logarithm of a value.
     16    *
     17    * For matrices, the function is evaluated element wise.
     18    *
     19    * Syntax:
     20    *
     21    *    math.log(x)
     22    *    math.log(x, base)
     23    *
     24    * Examples:
     25    *
     26    *    math.log(3.5)                  // returns 1.252762968495368
     27    *    math.exp(math.log(2.4))        // returns 2.4
     28    *
     29    *    math.pow(10, 4)                // returns 10000
     30    *    math.log(10000, 10)            // returns 4
     31    *    math.log(10000) / math.log(10) // returns 4
     32    *
     33    *    math.log(1024, 2)              // returns 10
     34    *    math.pow(2, 10)                // returns 1024
     35    *
     36    * See also:
     37    *
     38    *    exp, log2, log10, log1p
     39    *
     40    * @param {number | BigNumber | Complex | Array | Matrix} x
     41    *            Value for which to calculate the logarithm.
     42    * @param {number | BigNumber | Complex} [base=e]
     43    *            Optional base for the logarithm. If not provided, the natural
     44    *            logarithm of `x` is calculated.
     45    * @return {number | BigNumber | Complex | Array | Matrix}
     46    *            Returns the logarithm of `x`
     47    */
     48   return typed(name, {
     49     number: function number(x) {
     50       if (x >= 0 || config.predictable) {
     51         return logNumber(x);
     52       } else {
     53         // negative value -> complex value computation
     54         return new Complex(x, 0).log();
     55       }
     56     },
     57     Complex: function Complex(x) {
     58       return x.log();
     59     },
     60     BigNumber: function BigNumber(x) {
     61       if (!x.isNegative() || config.predictable) {
     62         return x.ln();
     63       } else {
     64         // downgrade to number, return Complex valued result
     65         return new Complex(x.toNumber(), 0).log();
     66       }
     67     },
     68     'Array | Matrix': function ArrayMatrix(x) {
     69       return deepMap(x, this);
     70     },
     71     'any, any': function anyAny(x, base) {
     72       // calculate logarithm for a specified base, log(x, base)
     73       return divideScalar(this(x), this(base));
     74     }
     75   });
     76 });