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