equal.js 3.3 KB

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