UNPKG

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