isZero.js 2.0 KB

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