UNPKG

2.36 kBJavaScriptView Raw
1import { factory } from '../../utils/factory';
2import { deepMap } from '../../utils/collection';
3import { logNumber } from '../../plain/number';
4var name = 'log';
5var dependencies = ['config', 'typed', 'divideScalar', 'Complex'];
6export var createLog =
7/* #__PURE__ */
8factory(name, dependencies, function (_ref) {
9 var typed = _ref.typed,
10 config = _ref.config,
11 divideScalar = _ref.divideScalar,
12 Complex = _ref.Complex;
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 var log = 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, log);
70 },
71 'any, any': function anyAny(x, base) {
72 // calculate logarithm for a specified base, log(x, base)
73 return divideScalar(log(x), log(base));
74 }
75 });
76 return log;
77});
\No newline at end of file