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 { createAlgorithm12 } from '../../type/matrix/utils/algorithm12'
|
6 | import { createAlgorithm14 } from '../../type/matrix/utils/algorithm14'
|
7 | import { createAlgorithm13 } from '../../type/matrix/utils/algorithm13'
|
8 | import { createAlgorithm05 } from '../../type/matrix/utils/algorithm05'
|
9 |
|
10 | const name = 'compare'
|
11 | const dependencies = [
|
12 | 'typed',
|
13 | 'config',
|
14 | 'matrix',
|
15 | 'equalScalar',
|
16 | 'BigNumber',
|
17 | 'Fraction',
|
18 | 'DenseMatrix'
|
19 | ]
|
20 |
|
21 | export const createCompare = factory(name, dependencies, ({ typed, config, equalScalar, matrix, BigNumber, Fraction, DenseMatrix }) => {
|
22 | const algorithm03 = createAlgorithm03({ typed })
|
23 | const algorithm05 = createAlgorithm05({ typed, equalScalar })
|
24 | const algorithm12 = createAlgorithm12({ typed, DenseMatrix })
|
25 | const algorithm13 = createAlgorithm13({ typed })
|
26 | const algorithm14 = createAlgorithm14({ typed })
|
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 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | const compare = typed(name, {
|
66 |
|
67 | 'boolean, boolean': function (x, y) {
|
68 | return x === y ? 0 : (x > y ? 1 : -1)
|
69 | },
|
70 |
|
71 | 'number, number': function (x, y) {
|
72 | return nearlyEqual(x, y, config.epsilon)
|
73 | ? 0
|
74 | : (x > y ? 1 : -1)
|
75 | },
|
76 |
|
77 | 'BigNumber, BigNumber': function (x, y) {
|
78 | return bigNearlyEqual(x, y, config.epsilon)
|
79 | ? new BigNumber(0)
|
80 | : new BigNumber(x.cmp(y))
|
81 | },
|
82 |
|
83 | 'Fraction, Fraction': function (x, y) {
|
84 | return new Fraction(x.compare(y))
|
85 | },
|
86 |
|
87 | 'Complex, Complex': function () {
|
88 | throw new TypeError('No ordering relation is defined for complex numbers')
|
89 | },
|
90 |
|
91 | 'Unit, Unit': function (x, y) {
|
92 | if (!x.equalBase(y)) {
|
93 | throw new Error('Cannot compare units with different base')
|
94 | }
|
95 | return compare(x.value, y.value)
|
96 | },
|
97 |
|
98 | 'SparseMatrix, SparseMatrix': function (x, y) {
|
99 | return algorithm05(x, y, compare)
|
100 | },
|
101 |
|
102 | 'SparseMatrix, DenseMatrix': function (x, y) {
|
103 | return algorithm03(y, x, compare, true)
|
104 | },
|
105 |
|
106 | 'DenseMatrix, SparseMatrix': function (x, y) {
|
107 | return algorithm03(x, y, compare, false)
|
108 | },
|
109 |
|
110 | 'DenseMatrix, DenseMatrix': function (x, y) {
|
111 | return algorithm13(x, y, compare)
|
112 | },
|
113 |
|
114 | 'Array, Array': function (x, y) {
|
115 |
|
116 | return compare(matrix(x), matrix(y)).valueOf()
|
117 | },
|
118 |
|
119 | 'Array, Matrix': function (x, y) {
|
120 |
|
121 | return compare(matrix(x), y)
|
122 | },
|
123 |
|
124 | 'Matrix, Array': function (x, y) {
|
125 |
|
126 | return compare(x, matrix(y))
|
127 | },
|
128 |
|
129 | 'SparseMatrix, any': function (x, y) {
|
130 | return algorithm12(x, y, compare, false)
|
131 | },
|
132 |
|
133 | 'DenseMatrix, any': function (x, y) {
|
134 | return algorithm14(x, y, compare, false)
|
135 | },
|
136 |
|
137 | 'any, SparseMatrix': function (x, y) {
|
138 | return algorithm12(y, x, compare, true)
|
139 | },
|
140 |
|
141 | 'any, DenseMatrix': function (x, y) {
|
142 | return algorithm14(y, x, compare, true)
|
143 | },
|
144 |
|
145 | 'Array, any': function (x, y) {
|
146 |
|
147 | return algorithm14(matrix(x), y, compare, false).valueOf()
|
148 | },
|
149 |
|
150 | 'any, Array': function (x, y) {
|
151 |
|
152 | return algorithm14(matrix(y), x, compare, true).valueOf()
|
153 | }
|
154 | })
|
155 |
|
156 | return compare
|
157 | })
|
158 |
|
159 | export const createCompareNumber = factory(name, ['typed', 'config'], ({ typed, config }) => {
|
160 | return typed(name, {
|
161 | 'number, number': function (x, y) {
|
162 | return nearlyEqual(x, y, config.epsilon)
|
163 | ? 0
|
164 | : (x > y ? 1 : -1)
|
165 | }
|
166 | })
|
167 | })
|