1 |
|
2 |
|
3 | const deepMap = require('../../utils/collection/deepMap')
|
4 |
|
5 | function factory (type, config, load, typed) {
|
6 | /**
|
7 | * Calculate the absolute value of a number. For matrices, the function is
|
8 | * evaluated element wise.
|
9 | *
|
10 | * Syntax:
|
11 | *
|
12 | * math.abs(x)
|
13 | *
|
14 | * Examples:
|
15 | *
|
16 | * math.abs(3.5) // returns number 3.5
|
17 | * math.abs(-4.2) // returns number 4.2
|
18 | *
|
19 | * math.abs([3, -5, -1, 0, 2]) // returns Array [3, 5, 1, 0, 2]
|
20 | *
|
21 | * See also:
|
22 | *
|
23 | * sign
|
24 | *
|
25 | * @param {number | BigNumber | Fraction | Complex | Array | Matrix | Unit} x
|
26 | * A number or matrix for which to get the absolute value
|
27 | * @return {number | BigNumber | Fraction | Complex | Array | Matrix | Unit}
|
28 | * Absolute value of `x`
|
29 | */
|
30 | const abs = typed('abs', {
|
31 | 'number': Math.abs,
|
32 |
|
33 | 'Complex': function (x) {
|
34 | return x.abs()
|
35 | },
|
36 |
|
37 | 'BigNumber': function (x) {
|
38 | return x.abs()
|
39 | },
|
40 |
|
41 | 'Fraction': function (x) {
|
42 | return x.abs()
|
43 | },
|
44 |
|
45 | 'Array | Matrix': function (x) {
|
46 | // deep map collection, skip zeros since abs(0) = 0
|
47 | return deepMap(x, abs, true)
|
48 | },
|
49 |
|
50 | 'Unit': function (x) {
|
51 | return x.abs()
|
52 | }
|
53 | })
|
54 |
|
55 | abs.toTex = { 1: `\\left|\${args[0]}\\right|` }
|
56 |
|
57 | return abs
|
58 | }
|
59 |
|
60 | exports.name = 'abs'
|
61 | exports.factory = factory
|