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