UNPKG

1.47 kBJavaScriptView Raw
1import { factory } from '../../utils/factory.js';
2import { deepMap } from '../../utils/collection.js';
3import { absNumber } from '../../plain/number/index.js';
4var name = 'abs';
5var dependencies = ['typed'];
6export var createAbs = /* #__PURE__ */factory(name, dependencies, (_ref) => {
7 var {
8 typed
9 } = _ref;
10
11 /**
12 * Calculate the absolute value of a number. For matrices, the function is
13 * evaluated element wise.
14 *
15 * Syntax:
16 *
17 * math.abs(x)
18 *
19 * Examples:
20 *
21 * math.abs(3.5) // returns number 3.5
22 * math.abs(-4.2) // returns number 4.2
23 *
24 * math.abs([3, -5, -1, 0, 2]) // returns Array [3, 5, 1, 0, 2]
25 *
26 * See also:
27 *
28 * sign
29 *
30 * @param {number | BigNumber | Fraction | Complex | Array | Matrix | Unit} x
31 * A number or matrix for which to get the absolute value
32 * @return {number | BigNumber | Fraction | Complex | Array | Matrix | Unit}
33 * Absolute value of `x`
34 */
35 return typed(name, {
36 number: absNumber,
37 Complex: function Complex(x) {
38 return x.abs();
39 },
40 BigNumber: function BigNumber(x) {
41 return x.abs();
42 },
43 Fraction: function Fraction(x) {
44 return x.abs();
45 },
46 'Array | Matrix': function ArrayMatrix(x) {
47 // deep map collection, skip zeros since abs(0) = 0
48 return deepMap(x, this, true);
49 },
50 Unit: function Unit(x) {
51 return x.abs();
52 }
53 });
54});
\No newline at end of file