simple-squiggle

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

log2.js (2271B)


      1 "use strict";
      2 
      3 Object.defineProperty(exports, "__esModule", {
      4   value: true
      5 });
      6 exports.createLog2 = void 0;
      7 
      8 var _factory = require("../../utils/factory.js");
      9 
     10 var _collection = require("../../utils/collection.js");
     11 
     12 var _index = require("../../plain/number/index.js");
     13 
     14 var name = 'log2';
     15 var dependencies = ['typed', 'config', 'Complex'];
     16 var createLog2 = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
     17   var typed = _ref.typed,
     18       config = _ref.config,
     19       Complex = _ref.Complex;
     20 
     21   /**
     22    * Calculate the 2-base of a value. This is the same as calculating `log(x, 2)`.
     23    *
     24    * For matrices, the function is evaluated element wise.
     25    *
     26    * Syntax:
     27    *
     28    *    math.log2(x)
     29    *
     30    * Examples:
     31    *
     32    *    math.log2(0.03125)           // returns -5
     33    *    math.log2(16)                // returns 4
     34    *    math.log2(16) / math.log2(2) // returns 4
     35    *    math.pow(2, 4)               // returns 16
     36    *
     37    * See also:
     38    *
     39    *    exp, log, log1p, log10
     40    *
     41    * @param {number | BigNumber | Complex | Array | Matrix} x
     42    *            Value for which to calculate the logarithm.
     43    * @return {number | BigNumber | Complex | Array | Matrix}
     44    *            Returns the 2-base logarithm of `x`
     45    */
     46   return typed(name, {
     47     number: function number(x) {
     48       if (x >= 0 || config.predictable) {
     49         return (0, _index.log2Number)(x);
     50       } else {
     51         // negative value -> complex value computation
     52         return _log2Complex(new Complex(x, 0));
     53       }
     54     },
     55     Complex: _log2Complex,
     56     BigNumber: function BigNumber(x) {
     57       if (!x.isNegative() || config.predictable) {
     58         return x.log(2);
     59       } else {
     60         // downgrade to number, return Complex valued result
     61         return _log2Complex(new Complex(x.toNumber(), 0));
     62       }
     63     },
     64     'Array | Matrix': function ArrayMatrix(x) {
     65       return (0, _collection.deepMap)(x, this);
     66     }
     67   });
     68   /**
     69    * Calculate log2 for a complex value
     70    * @param {Complex} x
     71    * @returns {Complex}
     72    * @private
     73    */
     74 
     75   function _log2Complex(x) {
     76     var newX = Math.sqrt(x.re * x.re + x.im * x.im);
     77     return new Complex(Math.log2 ? Math.log2(newX) : Math.log(newX) / Math.LN2, Math.atan2(x.im, x.re) / Math.LN2);
     78   }
     79 });
     80 exports.createLog2 = createLog2;