compare.js 4.2 KB

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