1 | import { nearlyEqual as bigNearlyEqual } from '../../utils/bignumber/nearlyEqual'
|
2 | import { nearlyEqual } from '../../utils/number'
|
3 | import { factory } from '../../utils/factory'
|
4 | import { createAlgorithm03 } from '../../type/matrix/utils/algorithm03'
|
5 | import { createAlgorithm07 } from '../../type/matrix/utils/algorithm07'
|
6 | import { createAlgorithm12 } from '../../type/matrix/utils/algorithm12'
|
7 | import { createAlgorithm14 } from '../../type/matrix/utils/algorithm14'
|
8 | import { createAlgorithm13 } from '../../type/matrix/utils/algorithm13'
|
9 |
|
10 | const name = 'smallerEq'
|
11 | const dependencies = [
|
12 | 'typed',
|
13 | 'config',
|
14 | 'matrix',
|
15 | 'DenseMatrix'
|
16 | ]
|
17 |
|
18 | export const createSmallerEq = 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 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
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 |
|
99 | return smallerEq(matrix(x), matrix(y)).valueOf()
|
100 | },
|
101 |
|
102 | 'Array, Matrix': function (x, y) {
|
103 |
|
104 | return smallerEq(matrix(x), y)
|
105 | },
|
106 |
|
107 | 'Matrix, Array': function (x, y) {
|
108 |
|
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 |
|
130 | return algorithm14(matrix(x), y, smallerEq, false).valueOf()
|
131 | },
|
132 |
|
133 | 'any, Array': function (x, y) {
|
134 |
|
135 | return algorithm14(matrix(y), x, smallerEq, true).valueOf()
|
136 | }
|
137 | })
|
138 |
|
139 | return smallerEq
|
140 | })
|
141 |
|
142 | export const createSmallerEqNumber = 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 | })
|