UNPKG

mathjs

Version:

Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser and offers an integrated solution to work with numbers, big numbers, complex numbers, units, and matrices.

69 lines (60 loc) 1.87 kB
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 * * log(x) * log(x, base) * * base is optional. If not provided, the natural logarithm of x is calculated. * For matrices, the function is evaluated element wise. * * @param {Number | BigNumber | Boolean | Complex | Array | Matrix} x * @param {Number | BigNumber | Boolean | Complex} [base] * @return {Number | BigNumber | Complex | Array | Matrix} res */ 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) { 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); } }; };