UNPKG

4.85 kBJavaScriptView Raw
1import { nearlyEqual as bigNearlyEqual } from '../../utils/bignumber/nearlyEqual.js';
2import { nearlyEqual } from '../../utils/number.js';
3import { factory } from '../../utils/factory.js';
4import { createAlgorithm03 } from '../../type/matrix/utils/algorithm03.js';
5import { createAlgorithm07 } from '../../type/matrix/utils/algorithm07.js';
6import { createAlgorithm12 } from '../../type/matrix/utils/algorithm12.js';
7import { createAlgorithm14 } from '../../type/matrix/utils/algorithm14.js';
8import { createAlgorithm13 } from '../../type/matrix/utils/algorithm13.js';
9var name = 'smallerEq';
10var dependencies = ['typed', 'config', 'matrix', 'DenseMatrix'];
11export var createSmallerEq = /* #__PURE__ */factory(name, dependencies, _ref => {
12 var {
13 typed,
14 config,
15 matrix,
16 DenseMatrix
17 } = _ref;
18 var algorithm03 = createAlgorithm03({
19 typed
20 });
21 var algorithm07 = createAlgorithm07({
22 typed,
23 DenseMatrix
24 });
25 var algorithm12 = createAlgorithm12({
26 typed,
27 DenseMatrix
28 });
29 var algorithm13 = createAlgorithm13({
30 typed
31 });
32 var algorithm14 = createAlgorithm14({
33 typed
34 });
35 /**
36 * Test whether value x is smaller or equal to y.
37 *
38 * The function returns true when x is smaller than y or the relative
39 * difference between x and y is smaller than the configured epsilon. The
40 * function cannot be used to compare values smaller than approximately 2.22e-16.
41 *
42 * For matrices, the function is evaluated element wise.
43 * Strings are compared by their numerical value.
44 *
45 * Syntax:
46 *
47 * math.smallerEq(x, y)
48 *
49 * Examples:
50 *
51 * math.smaller(1 + 2, 3) // returns false
52 * math.smallerEq(1 + 2, 3) // returns true
53 *
54 * See also:
55 *
56 * equal, unequal, smaller, larger, largerEq, compare
57 *
58 * @param {number | BigNumber | Fraction | boolean | Unit | string | Array | Matrix} x First value to compare
59 * @param {number | BigNumber | Fraction | boolean | Unit | string | Array | Matrix} y Second value to compare
60 * @return {boolean | Array | Matrix} Returns true when the x is smaller than y, else returns false
61 */
62
63 return typed(name, {
64 'boolean, boolean': function booleanBoolean(x, y) {
65 return x <= y;
66 },
67 'number, number': function numberNumber(x, y) {
68 return x <= y || nearlyEqual(x, y, config.epsilon);
69 },
70 'BigNumber, BigNumber': function BigNumberBigNumber(x, y) {
71 return x.lte(y) || bigNearlyEqual(x, y, config.epsilon);
72 },
73 'Fraction, Fraction': function FractionFraction(x, y) {
74 return x.compare(y) !== 1;
75 },
76 'Complex, Complex': function ComplexComplex() {
77 throw new TypeError('No ordering relation is defined for complex numbers');
78 },
79 'Unit, Unit': function UnitUnit(x, y) {
80 if (!x.equalBase(y)) {
81 throw new Error('Cannot compare units with different base');
82 }
83
84 return this(x.value, y.value);
85 },
86 'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) {
87 return algorithm07(x, y, this);
88 },
89 'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) {
90 return algorithm03(y, x, this, true);
91 },
92 'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) {
93 return algorithm03(x, y, this, false);
94 },
95 'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) {
96 return algorithm13(x, y, this);
97 },
98 'Array, Array': function ArrayArray(x, y) {
99 // use matrix implementation
100 return this(matrix(x), matrix(y)).valueOf();
101 },
102 'Array, Matrix': function ArrayMatrix(x, y) {
103 // use matrix implementation
104 return this(matrix(x), y);
105 },
106 'Matrix, Array': function MatrixArray(x, y) {
107 // use matrix implementation
108 return this(x, matrix(y));
109 },
110 'SparseMatrix, any': function SparseMatrixAny(x, y) {
111 return algorithm12(x, y, this, false);
112 },
113 'DenseMatrix, any': function DenseMatrixAny(x, y) {
114 return algorithm14(x, y, this, false);
115 },
116 'any, SparseMatrix': function anySparseMatrix(x, y) {
117 return algorithm12(y, x, this, true);
118 },
119 'any, DenseMatrix': function anyDenseMatrix(x, y) {
120 return algorithm14(y, x, this, true);
121 },
122 'Array, any': function ArrayAny(x, y) {
123 // use matrix implementation
124 return algorithm14(matrix(x), y, this, false).valueOf();
125 },
126 'any, Array': function anyArray(x, y) {
127 // use matrix implementation
128 return algorithm14(matrix(y), x, this, true).valueOf();
129 }
130 });
131});
132export var createSmallerEqNumber = /* #__PURE__ */factory(name, ['typed', 'config'], _ref2 => {
133 var {
134 typed,
135 config
136 } = _ref2;
137 return typed(name, {
138 'number, number': function numberNumber(x, y) {
139 return x <= y || nearlyEqual(x, y, config.epsilon);
140 }
141 });
142});
\No newline at end of file