UNPKG

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