96 lines
2.6 KiB
JavaScript

module.exports = function (math) {
var util = require('../../util/index'),
BigNumber = math.type.BigNumber,
Complex = require('../../type/Complex'),
collection = require('../../type/collection'),
isNumber = util.number.isNumber,
isBoolean = util['boolean'].isBoolean,
isComplex = Complex.isComplex,
isCollection = collection.isCollection;
/**
* Calculate the logarithm of a value.
*
* For matrices, the function is evaluated element wise.
*
* Syntax:
*
* math.log(x)
* math.log(x, base)
*
* Examples:
*
* math.log(3.5); // returns 1.252762968495368
* math.exp(math.log(2.4)); // returns 2.4
*
* math.pow(10, 4); // returns 10000
* math.log(10000, 10); // returns 4
* math.log(10000) / math.log(10); // returns 4
*
* math.log(1024, 2); // returns 10
* math.pow(2, 10); // returns 1024
*
* See also:
*
* exp, log10
*
* @param {Number | BigNumber | Boolean | Complex | Array | Matrix} x
* Value for which to calculate the logarithm.
* @param {Number | BigNumber | Boolean | Complex} [base=e]
* Optional base for the logarithm. If not provided, the natural
* logarithm of `x` is calculated.
* @return {Number | BigNumber | Complex | Array | Matrix}
* Returns the logarithm of `x`
*/
math.log = function log(x, base) {
if (arguments.length == 1) {
// calculate natural logarithm, log(x)
if (isNumber(x)) {
if (x >= 0) {
return Math.log(x);
}
else {
// negative value -> complex value computation
return log(new Complex(x, 0));
}
}
if (isComplex(x)) {
return new Complex (
Math.log(Math.sqrt(x.re * x.re + x.im * x.im)),
Math.atan2(x.im, x.re)
);
}
if (x instanceof BigNumber) {
if (x.isNegative()) {
// negative value -> downgrade to number to do complex value computation
return log(x.toNumber());
}
else {
return x.ln();
}
}
if (isCollection(x)) {
return collection.deepMap(x, log);
}
if (isBoolean(x)) {
return log(+x);
}
throw new math.error.UnsupportedTypeError('log', math['typeof'](x));
}
else if (arguments.length == 2) {
// calculate logarithm for a specified base, log(x, base)
return math.divide(log(x), log(base));
}
else {
throw new math.error.ArgumentsError('log', arguments.length, 1, 2);
}
};
};