UNPKG

5.18 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 { createAlgorithm12 } from '../../type/matrix/utils/algorithm12'
6import { createAlgorithm14 } from '../../type/matrix/utils/algorithm14'
7import { createAlgorithm13 } from '../../type/matrix/utils/algorithm13'
8import { createAlgorithm05 } from '../../type/matrix/utils/algorithm05'
9
10const name = 'compare'
11const dependencies = [
12 'typed',
13 'config',
14 'matrix',
15 'equalScalar',
16 'BigNumber',
17 'Fraction',
18 'DenseMatrix'
19]
20
21export const createCompare = /* #__PURE__ */ 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 * Compare two values. Returns 1 when x > y, -1 when x < y, and 0 when x == y.
30 *
31 * x and y are considered equal when the relative difference between x and y
32 * is smaller than the configured epsilon. The function cannot be used to
33 * compare values smaller than approximately 2.22e-16.
34 *
35 * For matrices, the function is evaluated element wise.
36 * Strings are compared by their numerical value.
37 *
38 * Syntax:
39 *
40 * math.compare(x, y)
41 *
42 * Examples:
43 *
44 * math.compare(6, 1) // returns 1
45 * math.compare(2, 3) // returns -1
46 * math.compare(7, 7) // returns 0
47 * math.compare('10', '2') // returns 1
48 * math.compare('1000', '1e3') // returns 0
49 *
50 * const a = math.unit('5 cm')
51 * const b = math.unit('40 mm')
52 * math.compare(a, b) // returns 1
53 *
54 * math.compare(2, [1, 2, 3]) // returns [1, 0, -1]
55 *
56 * See also:
57 *
58 * equal, unequal, smaller, smallerEq, larger, largerEq, compareNatural, compareText
59 *
60 * @param {number | BigNumber | Fraction | Unit | string | Array | Matrix} x First value to compare
61 * @param {number | BigNumber | Fraction | Unit | string | Array | Matrix} y Second value to compare
62 * @return {number | BigNumber | Fraction | Array | Matrix} Returns the result of the comparison:
63 * 1 when x > y, -1 when x < y, and 0 when x == y.
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 // use matrix implementation
116 return compare(matrix(x), matrix(y)).valueOf()
117 },
118
119 'Array, Matrix': function (x, y) {
120 // use matrix implementation
121 return compare(matrix(x), y)
122 },
123
124 'Matrix, Array': function (x, y) {
125 // use matrix implementation
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 // use matrix implementation
147 return algorithm14(matrix(x), y, compare, false).valueOf()
148 },
149
150 'any, Array': function (x, y) {
151 // use matrix implementation
152 return algorithm14(matrix(y), x, compare, true).valueOf()
153 }
154 })
155
156 return compare
157})
158
159export const createCompareNumber = /* #__PURE__ */ 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})