equal.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { factory } from '../../utils/factory.js';
  2. import { createMatAlgo03xDSf } from '../../type/matrix/utils/matAlgo03xDSf.js';
  3. import { createMatAlgo07xSSf } from '../../type/matrix/utils/matAlgo07xSSf.js';
  4. import { createMatAlgo12xSfs } from '../../type/matrix/utils/matAlgo12xSfs.js';
  5. import { createMatrixAlgorithmSuite } from '../../type/matrix/utils/matrixAlgorithmSuite.js';
  6. var name = 'equal';
  7. var dependencies = ['typed', 'matrix', 'equalScalar', 'DenseMatrix', 'concat'];
  8. export var createEqual = /* #__PURE__ */factory(name, dependencies, _ref => {
  9. var {
  10. typed,
  11. matrix,
  12. equalScalar,
  13. DenseMatrix,
  14. concat
  15. } = _ref;
  16. var matAlgo03xDSf = createMatAlgo03xDSf({
  17. typed
  18. });
  19. var matAlgo07xSSf = createMatAlgo07xSSf({
  20. typed,
  21. DenseMatrix
  22. });
  23. var matAlgo12xSfs = createMatAlgo12xSfs({
  24. typed,
  25. DenseMatrix
  26. });
  27. var matrixAlgorithmSuite = createMatrixAlgorithmSuite({
  28. typed,
  29. matrix,
  30. concat
  31. });
  32. /**
  33. * Test whether two values are equal.
  34. *
  35. * The function tests whether the relative difference between x and y is
  36. * smaller than the configured epsilon. The function cannot be used to
  37. * compare values smaller than approximately 2.22e-16.
  38. *
  39. * For matrices, the function is evaluated element wise.
  40. * In case of complex numbers, x.re must equal y.re, and x.im must equal y.im.
  41. *
  42. * Values `null` and `undefined` are compared strictly, thus `null` is only
  43. * equal to `null` and nothing else, and `undefined` is only equal to
  44. * `undefined` and nothing else. Strings are compared by their numerical value.
  45. *
  46. * Syntax:
  47. *
  48. * math.equal(x, y)
  49. *
  50. * Examples:
  51. *
  52. * math.equal(2 + 2, 3) // returns false
  53. * math.equal(2 + 2, 4) // returns true
  54. *
  55. * const a = math.unit('50 cm')
  56. * const b = math.unit('5 m')
  57. * math.equal(a, b) // returns true
  58. *
  59. * const c = [2, 5, 1]
  60. * const d = [2, 7, 1]
  61. *
  62. * math.equal(c, d) // returns [true, false, true]
  63. * math.deepEqual(c, d) // returns false
  64. *
  65. * math.equal("1000", "1e3") // returns true
  66. * math.equal(0, null) // returns false
  67. *
  68. * See also:
  69. *
  70. * unequal, smaller, smallerEq, larger, largerEq, compare, deepEqual, equalText
  71. *
  72. * @param {number | BigNumber | boolean | Complex | Unit | string | Array | Matrix} x First value to compare
  73. * @param {number | BigNumber | boolean | Complex | Unit | string | Array | Matrix} y Second value to compare
  74. * @return {boolean | Array | Matrix} Returns true when the compared values are equal, else returns false
  75. */
  76. return typed(name, createEqualNumber({
  77. typed,
  78. equalScalar
  79. }), matrixAlgorithmSuite({
  80. elop: equalScalar,
  81. SS: matAlgo07xSSf,
  82. DS: matAlgo03xDSf,
  83. Ss: matAlgo12xSfs
  84. }));
  85. });
  86. export var createEqualNumber = factory(name, ['typed', 'equalScalar'], _ref2 => {
  87. var {
  88. typed,
  89. equalScalar
  90. } = _ref2;
  91. return typed(name, {
  92. 'any, any': function anyAny(x, y) {
  93. // strict equality for null and undefined?
  94. if (x === null) {
  95. return y === null;
  96. }
  97. if (y === null) {
  98. return x === null;
  99. }
  100. if (x === undefined) {
  101. return y === undefined;
  102. }
  103. if (y === undefined) {
  104. return x === undefined;
  105. }
  106. return equalScalar(x, y);
  107. }
  108. });
  109. });