UNPKG

4.61 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 = 'larger'
11const dependencies = [
12 'typed',
13 'config',
14 'matrix',
15 'DenseMatrix'
16]
17
18export const createLarger = /* #__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 larger than y.
27 *
28 * The function returns true when x is larger than y and the relative
29 * difference between x and y is larger 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.larger(x, y)
38 *
39 * Examples:
40 *
41 * math.larger(2, 3) // returns false
42 * math.larger(5, 2 + 2) // returns true
43 *
44 * const a = math.unit('5 cm')
45 * const b = math.unit('2 inch')
46 * math.larger(a, b) // returns false
47 *
48 * See also:
49 *
50 * equal, unequal, smaller, smallerEq, largerEq, compare
51 *
52 * @param {number | BigNumber | Fraction | boolean | Unit | string | Array | Matrix} x First value to compare
53 * @param {number | BigNumber | Fraction | boolean | Unit | string | Array | Matrix} y Second value to compare
54 * @return {boolean | Array | Matrix} Returns true when the x is larger than y, else returns false
55 */
56 const larger = typed(name, {
57
58 'boolean, boolean': function (x, y) {
59 return x > y
60 },
61
62 'number, number': function (x, y) {
63 return x > y && !nearlyEqual(x, y, config.epsilon)
64 },
65
66 'BigNumber, BigNumber': function (x, y) {
67 return x.gt(y) && !bigNearlyEqual(x, y, config.epsilon)
68 },
69
70 'Fraction, Fraction': function (x, y) {
71 return x.compare(y) === 1
72 },
73
74 'Complex, Complex': function () {
75 throw new TypeError('No ordering relation is defined for complex numbers')
76 },
77
78 'Unit, Unit': function (x, y) {
79 if (!x.equalBase(y)) {
80 throw new Error('Cannot compare units with different base')
81 }
82 return larger(x.value, y.value)
83 },
84
85 'SparseMatrix, SparseMatrix': function (x, y) {
86 return algorithm07(x, y, larger)
87 },
88
89 'SparseMatrix, DenseMatrix': function (x, y) {
90 return algorithm03(y, x, larger, true)
91 },
92
93 'DenseMatrix, SparseMatrix': function (x, y) {
94 return algorithm03(x, y, larger, false)
95 },
96
97 'DenseMatrix, DenseMatrix': function (x, y) {
98 return algorithm13(x, y, larger)
99 },
100
101 'Array, Array': function (x, y) {
102 // use matrix implementation
103 return larger(matrix(x), matrix(y)).valueOf()
104 },
105
106 'Array, Matrix': function (x, y) {
107 // use matrix implementation
108 return larger(matrix(x), y)
109 },
110
111 'Matrix, Array': function (x, y) {
112 // use matrix implementation
113 return larger(x, matrix(y))
114 },
115
116 'SparseMatrix, any': function (x, y) {
117 return algorithm12(x, y, larger, false)
118 },
119
120 'DenseMatrix, any': function (x, y) {
121 return algorithm14(x, y, larger, false)
122 },
123
124 'any, SparseMatrix': function (x, y) {
125 return algorithm12(y, x, larger, true)
126 },
127
128 'any, DenseMatrix': function (x, y) {
129 return algorithm14(y, x, larger, true)
130 },
131
132 'Array, any': function (x, y) {
133 // use matrix implementation
134 return algorithm14(matrix(x), y, larger, false).valueOf()
135 },
136
137 'any, Array': function (x, y) {
138 // use matrix implementation
139 return algorithm14(matrix(y), x, larger, true).valueOf()
140 }
141 })
142
143 return larger
144})
145
146export const createLargerNumber = /* #__PURE__ */ factory(name, ['typed', 'config'], ({ typed, config }) => {
147 return typed(name, {
148 'number, number': function (x, y) {
149 return x > y && !nearlyEqual(x, y, config.epsilon)
150 }
151 })
152})