compare.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { nearlyEqual as bigNearlyEqual } from '../../utils/bignumber/nearlyEqual.js';
  2. import { nearlyEqual } from '../../utils/number.js';
  3. import { factory } from '../../utils/factory.js';
  4. import { createMatAlgo03xDSf } from '../../type/matrix/utils/matAlgo03xDSf.js';
  5. import { createMatAlgo12xSfs } from '../../type/matrix/utils/matAlgo12xSfs.js';
  6. import { createMatAlgo05xSfSf } from '../../type/matrix/utils/matAlgo05xSfSf.js';
  7. import { createMatrixAlgorithmSuite } from '../../type/matrix/utils/matrixAlgorithmSuite.js';
  8. import { createCompareUnits } from './compareUnits.js';
  9. var name = 'compare';
  10. var dependencies = ['typed', 'config', 'matrix', 'equalScalar', 'BigNumber', 'Fraction', 'DenseMatrix', 'concat'];
  11. export var createCompare = /* #__PURE__ */factory(name, dependencies, _ref => {
  12. var {
  13. typed,
  14. config,
  15. equalScalar,
  16. matrix,
  17. BigNumber,
  18. Fraction,
  19. DenseMatrix,
  20. concat
  21. } = _ref;
  22. var matAlgo03xDSf = createMatAlgo03xDSf({
  23. typed
  24. });
  25. var matAlgo05xSfSf = createMatAlgo05xSfSf({
  26. typed,
  27. equalScalar
  28. });
  29. var matAlgo12xSfs = createMatAlgo12xSfs({
  30. typed,
  31. DenseMatrix
  32. });
  33. var matrixAlgorithmSuite = createMatrixAlgorithmSuite({
  34. typed,
  35. matrix,
  36. concat
  37. });
  38. var compareUnits = createCompareUnits({
  39. typed
  40. });
  41. /**
  42. * Compare two values. Returns 1 when x > y, -1 when x < y, and 0 when x == y.
  43. *
  44. * x and y are considered equal when the relative difference between x and y
  45. * is smaller than the configured epsilon. The function cannot be used to
  46. * compare values smaller than approximately 2.22e-16.
  47. *
  48. * For matrices, the function is evaluated element wise.
  49. * Strings are compared by their numerical value.
  50. *
  51. * Syntax:
  52. *
  53. * math.compare(x, y)
  54. *
  55. * Examples:
  56. *
  57. * math.compare(6, 1) // returns 1
  58. * math.compare(2, 3) // returns -1
  59. * math.compare(7, 7) // returns 0
  60. * math.compare('10', '2') // returns 1
  61. * math.compare('1000', '1e3') // returns 0
  62. *
  63. * const a = math.unit('5 cm')
  64. * const b = math.unit('40 mm')
  65. * math.compare(a, b) // returns 1
  66. *
  67. * math.compare(2, [1, 2, 3]) // returns [1, 0, -1]
  68. *
  69. * See also:
  70. *
  71. * equal, unequal, smaller, smallerEq, larger, largerEq, compareNatural, compareText
  72. *
  73. * @param {number | BigNumber | Fraction | Unit | string | Array | Matrix} x First value to compare
  74. * @param {number | BigNumber | Fraction | Unit | string | Array | Matrix} y Second value to compare
  75. * @return {number | BigNumber | Fraction | Array | Matrix} Returns the result of the comparison:
  76. * 1 when x > y, -1 when x < y, and 0 when x == y.
  77. */
  78. return typed(name, createCompareNumber({
  79. typed,
  80. config
  81. }), {
  82. 'boolean, boolean': function booleanBoolean(x, y) {
  83. return x === y ? 0 : x > y ? 1 : -1;
  84. },
  85. 'BigNumber, BigNumber': function BigNumberBigNumber(x, y) {
  86. return bigNearlyEqual(x, y, config.epsilon) ? new BigNumber(0) : new BigNumber(x.cmp(y));
  87. },
  88. 'Fraction, Fraction': function FractionFraction(x, y) {
  89. return new Fraction(x.compare(y));
  90. },
  91. 'Complex, Complex': function ComplexComplex() {
  92. throw new TypeError('No ordering relation is defined for complex numbers');
  93. }
  94. }, compareUnits, matrixAlgorithmSuite({
  95. SS: matAlgo05xSfSf,
  96. DS: matAlgo03xDSf,
  97. Ss: matAlgo12xSfs
  98. }));
  99. });
  100. export var createCompareNumber = /* #__PURE__ */factory(name, ['typed', 'config'], _ref2 => {
  101. var {
  102. typed,
  103. config
  104. } = _ref2;
  105. return typed(name, {
  106. 'number, number': function numberNumber(x, y) {
  107. return nearlyEqual(x, y, config.epsilon) ? 0 : x > y ? 1 : -1;
  108. }
  109. });
  110. });