1 |
|
2 |
|
3 | const deepMap = require('../../utils/collection/deepMap')
|
4 |
|
5 | function factory (type, config, load, typed) {
|
6 | /**
|
7 | * Test whether a value is zero.
|
8 | * The function can check for zero for types `number`, `BigNumber`, `Fraction`,
|
9 | * `Complex`, and `Unit`.
|
10 | *
|
11 | * The function is evaluated element-wise in case of Array or Matrix input.
|
12 | *
|
13 | * Syntax:
|
14 | *
|
15 | * math.isZero(x)
|
16 | *
|
17 | * Examples:
|
18 | *
|
19 | * math.isZero(0) // returns true
|
20 | * math.isZero(2) // returns false
|
21 | * math.isZero(0.5) // returns false
|
22 | * math.isZero(math.bignumber(0)) // returns true
|
23 | * math.isZero(math.fraction(0)) // returns true
|
24 | * math.isZero(math.fraction(1,3)) // returns false
|
25 | * math.isZero(math.complex('2 - 4i') // returns false
|
26 | * math.isZero(math.complex('0i') // returns true
|
27 | * math.isZero('0') // returns true
|
28 | * math.isZero('2') // returns false
|
29 | * math.isZero([2, 0, -3]') // returns [false, true, false]
|
30 | *
|
31 | * See also:
|
32 | *
|
33 | * isNumeric, isPositive, isNegative, isInteger
|
34 | *
|
35 | * @param {number | BigNumber | Complex | Fraction | Unit | Array | Matrix} x Value to be tested
|
36 | * @return {boolean} Returns true when `x` is zero.
|
37 | * Throws an error in case of an unknown data type.
|
38 | */
|
39 | const isZero = typed('isZero', {
|
40 | 'number': function (x) {
|
41 | return x === 0
|
42 | },
|
43 |
|
44 | 'BigNumber': function (x) {
|
45 | return x.isZero()
|
46 | },
|
47 |
|
48 | 'Complex': function (x) {
|
49 | return x.re === 0 && x.im === 0
|
50 | },
|
51 |
|
52 | 'Fraction': function (x) {
|
53 | return x.d === 1 && x.n === 0
|
54 | },
|
55 |
|
56 | 'Unit': function (x) {
|
57 | return isZero(x.value)
|
58 | },
|
59 |
|
60 | 'Array | Matrix': function (x) {
|
61 | return deepMap(x, isZero)
|
62 | }
|
63 | })
|
64 |
|
65 | return isZero
|
66 | }
|
67 |
|
68 | exports.name = 'isZero'
|
69 | exports.factory = factory
|