isZero.js 2.3 KB

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