forked from josdejong/mathjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
abs.js
57 lines (50 loc) · 1.36 KB
/
abs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { factory } from '../../utils/factory'
import { deepMap } from '../../utils/collection'
import { absNumber } from '../../plain/number'
const name = 'abs'
const dependencies = ['typed']
export const createAbs = /* #__PURE__ */ factory(name, dependencies, ({ 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`
*/
return typed(name, {
number: absNumber,
Complex: function (x) {
return x.abs()
},
BigNumber: function (x) {
return x.abs()
},
Fraction: function (x) {
return x.abs()
},
'Array | Matrix': function (x) {
// deep map collection, skip zeros since abs(0) = 0
return deepMap(x, this, true)
},
Unit: function (x) {
return x.abs()
}
})
})