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.

56 lines (46 loc) 1.34 kB
module.exports = function (math) { var util = require('../../util/index'), BigNumber = math.type.BigNumber, Complex = require('../../type/Complex'), Matrix = require('../../type/Matrix'), collection = require('../../type/collection'), isNumber = util.number.isNumber, isBoolean = util['boolean'].isBoolean, isComplex = Complex.isComplex, isCollection = collection.isCollection; /** * Calculate the exponent of a value * * exp(x) * * For matrices, the function is evaluated element wise. * * @param {Number | BigNumber | Boolean | Complex | Array | Matrix} x * @return {Number | BigNumber | Complex | Array | Matrix} res */ math.exp = function exp (x) { if (arguments.length != 1) { throw new math.error.ArgumentsError('exp', arguments.length, 1); } if (isNumber(x)) { return Math.exp(x); } if (isComplex(x)) { var r = Math.exp(x.re); return new Complex( r * Math.cos(x.im), r * Math.sin(x.im) ); } if (x instanceof BigNumber) { return x.exp(); } if (isCollection(x)) { return collection.deepMap(x, exp); } if (isBoolean(x)) { return Math.exp(x); } throw new math.error.UnsupportedTypeError('exp', math['typeof'](x)); }; };