simple-squiggle

A restricted subset of Squiggle
Log | Files | Refs | README

compare.js (5991B)


      1 "use strict";
      2 
      3 Object.defineProperty(exports, "__esModule", {
      4   value: true
      5 });
      6 exports.createCompareNumber = exports.createCompare = void 0;
      7 
      8 var _nearlyEqual = require("../../utils/bignumber/nearlyEqual.js");
      9 
     10 var _number = require("../../utils/number.js");
     11 
     12 var _factory = require("../../utils/factory.js");
     13 
     14 var _algorithm = require("../../type/matrix/utils/algorithm03.js");
     15 
     16 var _algorithm2 = require("../../type/matrix/utils/algorithm12.js");
     17 
     18 var _algorithm3 = require("../../type/matrix/utils/algorithm14.js");
     19 
     20 var _algorithm4 = require("../../type/matrix/utils/algorithm13.js");
     21 
     22 var _algorithm5 = require("../../type/matrix/utils/algorithm05.js");
     23 
     24 var name = 'compare';
     25 var dependencies = ['typed', 'config', 'matrix', 'equalScalar', 'BigNumber', 'Fraction', 'DenseMatrix'];
     26 var 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   return 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 this(x.value, y.value);
    111     },
    112     'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) {
    113       return algorithm05(x, y, this);
    114     },
    115     'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) {
    116       return algorithm03(y, x, this, true);
    117     },
    118     'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) {
    119       return algorithm03(x, y, this, false);
    120     },
    121     'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) {
    122       return algorithm13(x, y, this);
    123     },
    124     'Array, Array': function ArrayArray(x, y) {
    125       // use matrix implementation
    126       return this(matrix(x), matrix(y)).valueOf();
    127     },
    128     'Array, Matrix': function ArrayMatrix(x, y) {
    129       // use matrix implementation
    130       return this(matrix(x), y);
    131     },
    132     'Matrix, Array': function MatrixArray(x, y) {
    133       // use matrix implementation
    134       return this(x, matrix(y));
    135     },
    136     'SparseMatrix, any': function SparseMatrixAny(x, y) {
    137       return algorithm12(x, y, this, false);
    138     },
    139     'DenseMatrix, any': function DenseMatrixAny(x, y) {
    140       return algorithm14(x, y, this, false);
    141     },
    142     'any, SparseMatrix': function anySparseMatrix(x, y) {
    143       return algorithm12(y, x, this, true);
    144     },
    145     'any, DenseMatrix': function anyDenseMatrix(x, y) {
    146       return algorithm14(y, x, this, true);
    147     },
    148     'Array, any': function ArrayAny(x, y) {
    149       // use matrix implementation
    150       return algorithm14(matrix(x), y, this, false).valueOf();
    151     },
    152     'any, Array': function anyArray(x, y) {
    153       // use matrix implementation
    154       return algorithm14(matrix(y), x, this, true).valueOf();
    155     }
    156   });
    157 });
    158 exports.createCompare = createCompare;
    159 var createCompareNumber = /* #__PURE__ */(0, _factory.factory)(name, ['typed', 'config'], function (_ref2) {
    160   var typed = _ref2.typed,
    161       config = _ref2.config;
    162   return typed(name, {
    163     'number, number': function numberNumber(x, y) {
    164       return (0, _number.nearlyEqual)(x, y, config.epsilon) ? 0 : x > y ? 1 : -1;
    165     }
    166   });
    167 });
    168 exports.createCompareNumber = createCompareNumber;