UNPKG

6.03 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.createCompareNumber = exports.createCompare = void 0;
7
8var _nearlyEqual = require("../../utils/bignumber/nearlyEqual");
9
10var _number = require("../../utils/number");
11
12var _factory = require("../../utils/factory");
13
14var _algorithm = require("../../type/matrix/utils/algorithm03");
15
16var _algorithm2 = require("../../type/matrix/utils/algorithm12");
17
18var _algorithm3 = require("../../type/matrix/utils/algorithm14");
19
20var _algorithm4 = require("../../type/matrix/utils/algorithm13");
21
22var _algorithm5 = require("../../type/matrix/utils/algorithm05");
23
24var name = 'compare';
25var dependencies = ['typed', 'config', 'matrix', 'equalScalar', 'BigNumber', 'Fraction', 'DenseMatrix'];
26var createCompare = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
27 var typed = _ref.typed,
28 config = _ref.config,
29 equalScalar = _ref.equalScalar,
30 matrix = _ref.matrix,
31 BigNumber = _ref.BigNumber,
32 Fraction = _ref.Fraction,
33 DenseMatrix = _ref.DenseMatrix;
34 var algorithm03 = (0, _algorithm.createAlgorithm03)({
35 typed: typed
36 });
37 var algorithm05 = (0, _algorithm5.createAlgorithm05)({
38 typed: typed,
39 equalScalar: equalScalar
40 });
41 var algorithm12 = (0, _algorithm2.createAlgorithm12)({
42 typed: typed,
43 DenseMatrix: DenseMatrix
44 });
45 var algorithm13 = (0, _algorithm4.createAlgorithm13)({
46 typed: typed
47 });
48 var algorithm14 = (0, _algorithm3.createAlgorithm14)({
49 typed: typed
50 });
51 /**
52 * Compare two values. Returns 1 when x > y, -1 when x < y, and 0 when x == y.
53 *
54 * x and y are considered equal when the relative difference between x and y
55 * is smaller than the configured epsilon. The function cannot be used to
56 * compare values smaller than approximately 2.22e-16.
57 *
58 * For matrices, the function is evaluated element wise.
59 * Strings are compared by their numerical value.
60 *
61 * Syntax:
62 *
63 * math.compare(x, y)
64 *
65 * Examples:
66 *
67 * math.compare(6, 1) // returns 1
68 * math.compare(2, 3) // returns -1
69 * math.compare(7, 7) // returns 0
70 * math.compare('10', '2') // returns 1
71 * math.compare('1000', '1e3') // returns 0
72 *
73 * const a = math.unit('5 cm')
74 * const b = math.unit('40 mm')
75 * math.compare(a, b) // returns 1
76 *
77 * math.compare(2, [1, 2, 3]) // returns [1, 0, -1]
78 *
79 * See also:
80 *
81 * equal, unequal, smaller, smallerEq, larger, largerEq, compareNatural, compareText
82 *
83 * @param {number | BigNumber | Fraction | Unit | string | Array | Matrix} x First value to compare
84 * @param {number | BigNumber | Fraction | Unit | string | Array | Matrix} y Second value to compare
85 * @return {number | BigNumber | Fraction | Array | Matrix} Returns the result of the comparison:
86 * 1 when x > y, -1 when x < y, and 0 when x == y.
87 */
88
89 var compare = typed(name, {
90 'boolean, boolean': function booleanBoolean(x, y) {
91 return x === y ? 0 : x > y ? 1 : -1;
92 },
93 'number, number': function numberNumber(x, y) {
94 return (0, _number.nearlyEqual)(x, y, config.epsilon) ? 0 : x > y ? 1 : -1;
95 },
96 'BigNumber, BigNumber': function BigNumberBigNumber(x, y) {
97 return (0, _nearlyEqual.nearlyEqual)(x, y, config.epsilon) ? new BigNumber(0) : new BigNumber(x.cmp(y));
98 },
99 'Fraction, Fraction': function FractionFraction(x, y) {
100 return new Fraction(x.compare(y));
101 },
102 'Complex, Complex': function ComplexComplex() {
103 throw new TypeError('No ordering relation is defined for complex numbers');
104 },
105 'Unit, Unit': function UnitUnit(x, y) {
106 if (!x.equalBase(y)) {
107 throw new Error('Cannot compare units with different base');
108 }
109
110 return compare(x.value, y.value);
111 },
112 'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) {
113 return algorithm05(x, y, compare);
114 },
115 'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) {
116 return algorithm03(y, x, compare, true);
117 },
118 'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) {
119 return algorithm03(x, y, compare, false);
120 },
121 'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) {
122 return algorithm13(x, y, compare);
123 },
124 'Array, Array': function ArrayArray(x, y) {
125 // use matrix implementation
126 return compare(matrix(x), matrix(y)).valueOf();
127 },
128 'Array, Matrix': function ArrayMatrix(x, y) {
129 // use matrix implementation
130 return compare(matrix(x), y);
131 },
132 'Matrix, Array': function MatrixArray(x, y) {
133 // use matrix implementation
134 return compare(x, matrix(y));
135 },
136 'SparseMatrix, any': function SparseMatrixAny(x, y) {
137 return algorithm12(x, y, compare, false);
138 },
139 'DenseMatrix, any': function DenseMatrixAny(x, y) {
140 return algorithm14(x, y, compare, false);
141 },
142 'any, SparseMatrix': function anySparseMatrix(x, y) {
143 return algorithm12(y, x, compare, true);
144 },
145 'any, DenseMatrix': function anyDenseMatrix(x, y) {
146 return algorithm14(y, x, compare, true);
147 },
148 'Array, any': function ArrayAny(x, y) {
149 // use matrix implementation
150 return algorithm14(matrix(x), y, compare, false).valueOf();
151 },
152 'any, Array': function anyArray(x, y) {
153 // use matrix implementation
154 return algorithm14(matrix(y), x, compare, true).valueOf();
155 }
156 });
157 return compare;
158});
159exports.createCompare = createCompare;
160var createCompareNumber = /* #__PURE__ */(0, _factory.factory)(name, ['typed', 'config'], function (_ref2) {
161 var typed = _ref2.typed,
162 config = _ref2.config;
163 return typed(name, {
164 'number, number': function numberNumber(x, y) {
165 return (0, _number.nearlyEqual)(x, y, config.epsilon) ? 0 : x > y ? 1 : -1;
166 }
167 });
168});
169exports.createCompareNumber = createCompareNumber;
\No newline at end of file