smallerEq.js 3.0 KB

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