smaller.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 = 'smaller';
  10. var dependencies = ['typed', 'config', 'matrix', 'DenseMatrix', 'concat'];
  11. export var createSmaller = /* #__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 than y.
  40. *
  41. * The function returns true when x is smaller than y and 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.smaller(x, y)
  51. *
  52. * Examples:
  53. *
  54. * math.smaller(2, 3) // returns true
  55. * math.smaller(5, 2 * 2) // returns false
  56. *
  57. * const a = math.unit('5 cm')
  58. * const b = math.unit('2 inch')
  59. * math.smaller(a, b) // returns true
  60. *
  61. * See also:
  62. *
  63. * equal, unequal, smallerEq, smaller, smallerEq, compare
  64. *
  65. * @param {number | BigNumber | Fraction | boolean | Unit | string | Array | Matrix} x First value to compare
  66. * @param {number | BigNumber | Fraction | boolean | Unit | string | Array | Matrix} y Second value to compare
  67. * @return {boolean | Array | Matrix} Returns true when the x is smaller than y, else returns false
  68. */
  69. return typed(name, createSmallerNumber({
  70. typed,
  71. config
  72. }), {
  73. 'boolean, boolean': (x, y) => x < y,
  74. 'BigNumber, BigNumber': function BigNumberBigNumber(x, y) {
  75. return x.lt(y) && !bigNearlyEqual(x, y, config.epsilon);
  76. },
  77. 'Fraction, Fraction': (x, y) => x.compare(y) === -1,
  78. 'Complex, Complex': function ComplexComplex(x, y) {
  79. throw new TypeError('No ordering relation is defined for complex numbers');
  80. }
  81. }, compareUnits, matrixAlgorithmSuite({
  82. SS: matAlgo07xSSf,
  83. DS: matAlgo03xDSf,
  84. Ss: matAlgo12xSfs
  85. }));
  86. });
  87. export var createSmallerNumber = /* #__PURE__ */factory(name, ['typed', 'config'], _ref2 => {
  88. var {
  89. typed,
  90. config
  91. } = _ref2;
  92. return typed(name, {
  93. 'number, number': function numberNumber(x, y) {
  94. return x < y && !nearlyEqual(x, y, config.epsilon);
  95. }
  96. });
  97. });