smallerEq.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 { createMatAlgo07xSSf } from '../../type/matrix/utils/matAlgo07xSSf.js';
  6. import { createMatAlgo12xSfs } from '../../type/matrix/utils/matAlgo12xSfs.js';
  7. import { createMatrixAlgorithmSuite } from '../../type/matrix/utils/matrixAlgorithmSuite.js';
  8. import { createCompareUnits } from './compareUnits.js';
  9. var name = 'smallerEq';
  10. var dependencies = ['typed', 'config', 'matrix', 'DenseMatrix', 'concat'];
  11. export var createSmallerEq = /* #__PURE__ */factory(name, dependencies, _ref => {
  12. var {
  13. typed,
  14. config,
  15. matrix,
  16. DenseMatrix,
  17. concat
  18. } = _ref;
  19. var matAlgo03xDSf = createMatAlgo03xDSf({
  20. typed
  21. });
  22. var matAlgo07xSSf = createMatAlgo07xSSf({
  23. typed,
  24. DenseMatrix
  25. });
  26. var matAlgo12xSfs = createMatAlgo12xSfs({
  27. typed,
  28. DenseMatrix
  29. });
  30. var matrixAlgorithmSuite = createMatrixAlgorithmSuite({
  31. typed,
  32. matrix,
  33. concat
  34. });
  35. var compareUnits = createCompareUnits({
  36. typed
  37. });
  38. /**
  39. * Test whether value x is smaller or equal to y.
  40. *
  41. * The function returns true when x is smaller than y or the relative
  42. * difference between x and y is smaller than the configured epsilon. The
  43. * function cannot be used to compare values smaller than approximately 2.22e-16.
  44. *
  45. * For matrices, the function is evaluated element wise.
  46. * Strings are compared by their numerical value.
  47. *
  48. * Syntax:
  49. *
  50. * math.smallerEq(x, y)
  51. *
  52. * Examples:
  53. *
  54. * math.smaller(1 + 2, 3) // returns false
  55. * math.smallerEq(1 + 2, 3) // returns true
  56. *
  57. * See also:
  58. *
  59. * equal, unequal, smaller, larger, largerEq, compare
  60. *
  61. * @param {number | BigNumber | Fraction | boolean | Unit | string | Array | Matrix} x First value to compare
  62. * @param {number | BigNumber | Fraction | boolean | Unit | string | Array | Matrix} y Second value to compare
  63. * @return {boolean | Array | Matrix} Returns true when the x is smaller than y, else returns false
  64. */
  65. return typed(name, createSmallerEqNumber({
  66. typed,
  67. config
  68. }), {
  69. 'boolean, boolean': (x, y) => x <= y,
  70. 'BigNumber, BigNumber': function BigNumberBigNumber(x, y) {
  71. return x.lte(y) || bigNearlyEqual(x, y, config.epsilon);
  72. },
  73. 'Fraction, Fraction': (x, y) => x.compare(y) !== 1,
  74. 'Complex, Complex': function ComplexComplex() {
  75. throw new TypeError('No ordering relation is defined for complex numbers');
  76. }
  77. }, compareUnits, matrixAlgorithmSuite({
  78. SS: matAlgo07xSSf,
  79. DS: matAlgo03xDSf,
  80. Ss: matAlgo12xSfs
  81. }));
  82. });
  83. export var createSmallerEqNumber = /* #__PURE__ */factory(name, ['typed', 'config'], _ref2 => {
  84. var {
  85. typed,
  86. config
  87. } = _ref2;
  88. return typed(name, {
  89. 'number, number': function numberNumber(x, y) {
  90. return x <= y || nearlyEqual(x, y, config.epsilon);
  91. }
  92. });
  93. });