mathjs
Version:
Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser with support for symbolic computation, comes with a large set of built-in functions and constants, and offers an integrated solution to work with dif
56 lines (53 loc) • 1.4 kB
JavaScript
;
var deepMap = require('../../utils/collection/deepMap');
function factory(type, config, load, typed) {
/**
* Calculate the absolute value of a number. For matrices, the function is
* evaluated element wise.
*
* Syntax:
*
* math.abs(x)
*
* Examples:
*
* math.abs(3.5) // returns number 3.5
* math.abs(-4.2) // returns number 4.2
*
* math.abs([3, -5, -1, 0, 2]) // returns Array [3, 5, 1, 0, 2]
*
* See also:
*
* sign
*
* @param {number | BigNumber | Fraction | Complex | Array | Matrix | Unit} x
* A number or matrix for which to get the absolute value
* @return {number | BigNumber | Fraction | Complex | Array | Matrix | Unit}
* Absolute value of `x`
*/
var abs = typed('abs', {
'number': Math.abs,
'Complex': function Complex(x) {
return x.abs();
},
'BigNumber': function BigNumber(x) {
return x.abs();
},
'Fraction': function Fraction(x) {
return x.abs();
},
'Array | Matrix': function ArrayMatrix(x) {
// deep map collection, skip zeros since abs(0) = 0
return deepMap(x, abs, true);
},
'Unit': function Unit(x) {
return x.abs();
}
});
abs.toTex = {
1: "\\left|${args[0]}\\right|"
};
return abs;
}
exports.name = 'abs';
exports.factory = factory;