UNPKG

1.9 kBJavaScriptView Raw
1'use strict'
2
3const deepMap = require('../../utils/collection/deepMap')
4
5function factory (type, config, load, typed) {
6 /**
7 * Calculate the 10-base logarithm of a value. This is the same as calculating `log(x, 10)`.
8 *
9 * For matrices, the function is evaluated element wise.
10 *
11 * Syntax:
12 *
13 * math.log10(x)
14 *
15 * Examples:
16 *
17 * math.log10(0.00001) // returns -5
18 * math.log10(10000) // returns 4
19 * math.log(10000) / math.log(10) // returns 4
20 * math.pow(10, 4) // returns 10000
21 *
22 * See also:
23 *
24 * exp, log, log1p, log2
25 *
26 * @param {number | BigNumber | Complex | Array | Matrix} x
27 * Value for which to calculate the logarithm.
28 * @return {number | BigNumber | Complex | Array | Matrix}
29 * Returns the 10-base logarithm of `x`
30 */
31 const log10 = typed('log10', {
32 'number': function (x) {
33 if (x >= 0 || config.predictable) {
34 return _log10(x)
35 } else {
36 // negative value -> complex value computation
37 return new type.Complex(x, 0).log().div(Math.LN10)
38 }
39 },
40
41 'Complex': function (x) {
42 return new type.Complex(x).log().div(Math.LN10)
43 },
44
45 'BigNumber': function (x) {
46 if (!x.isNegative() || config.predictable) {
47 return x.log()
48 } else {
49 // downgrade to number, return Complex valued result
50 return new type.Complex(x.toNumber(), 0).log().div(Math.LN10)
51 }
52 },
53
54 'Array | Matrix': function (x) {
55 return deepMap(x, log10)
56 }
57 })
58
59 log10.toTex = { 1: `\\log_{10}\\left(\${args[0]}\\right)` }
60
61 return log10
62}
63
64/**
65 * Calculate the 10-base logarithm of a number
66 * @param {number} x
67 * @return {number}
68 * @private
69 */
70const _log10 = Math.log10 || function (x) {
71 return Math.log(x) / Math.LN10
72}
73
74exports.name = 'log10'
75exports.factory = factory