compare.js 4.2 KB

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