simple-squiggle

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

equal.js (5779B)


      1 "use strict";
      2 
      3 Object.defineProperty(exports, "__esModule", {
      4   value: true
      5 });
      6 exports.createEqualNumber = exports.createEqual = void 0;
      7 
      8 var _factory = require("../../utils/factory.js");
      9 
     10 var _algorithm = require("../../type/matrix/utils/algorithm03.js");
     11 
     12 var _algorithm2 = require("../../type/matrix/utils/algorithm07.js");
     13 
     14 var _algorithm3 = require("../../type/matrix/utils/algorithm12.js");
     15 
     16 var _algorithm4 = require("../../type/matrix/utils/algorithm13.js");
     17 
     18 var _algorithm5 = require("../../type/matrix/utils/algorithm14.js");
     19 
     20 var name = 'equal';
     21 var dependencies = ['typed', 'matrix', 'equalScalar', 'DenseMatrix'];
     22 var createEqual = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
     23   var typed = _ref.typed,
     24       matrix = _ref.matrix,
     25       equalScalar = _ref.equalScalar,
     26       DenseMatrix = _ref.DenseMatrix;
     27   var algorithm03 = (0, _algorithm.createAlgorithm03)({
     28     typed: typed
     29   });
     30   var algorithm07 = (0, _algorithm2.createAlgorithm07)({
     31     typed: typed,
     32     DenseMatrix: DenseMatrix
     33   });
     34   var algorithm12 = (0, _algorithm3.createAlgorithm12)({
     35     typed: typed,
     36     DenseMatrix: DenseMatrix
     37   });
     38   var algorithm13 = (0, _algorithm4.createAlgorithm13)({
     39     typed: typed
     40   });
     41   var algorithm14 = (0, _algorithm5.createAlgorithm14)({
     42     typed: typed
     43   });
     44   /**
     45    * Test whether two values are equal.
     46    *
     47    * The function tests whether the relative difference between x and y is
     48    * smaller than the configured epsilon. The function cannot be used to
     49    * compare values smaller than approximately 2.22e-16.
     50    *
     51    * For matrices, the function is evaluated element wise.
     52    * In case of complex numbers, x.re must equal y.re, and x.im must equal y.im.
     53    *
     54    * Values `null` and `undefined` are compared strictly, thus `null` is only
     55    * equal to `null` and nothing else, and `undefined` is only equal to
     56    * `undefined` and nothing else. Strings are compared by their numerical value.
     57    *
     58    * Syntax:
     59    *
     60    *    math.equal(x, y)
     61    *
     62    * Examples:
     63    *
     64    *    math.equal(2 + 2, 3)         // returns false
     65    *    math.equal(2 + 2, 4)         // returns true
     66    *
     67    *    const a = math.unit('50 cm')
     68    *    const b = math.unit('5 m')
     69    *    math.equal(a, b)             // returns true
     70    *
     71    *    const c = [2, 5, 1]
     72    *    const d = [2, 7, 1]
     73    *
     74    *    math.equal(c, d)             // returns [true, false, true]
     75    *    math.deepEqual(c, d)         // returns false
     76    *
     77    *    math.equal("1000", "1e3")    // returns true
     78    *    math.equal(0, null)          // returns false
     79    *
     80    * See also:
     81    *
     82    *    unequal, smaller, smallerEq, larger, largerEq, compare, deepEqual, equalText
     83    *
     84    * @param  {number | BigNumber | boolean | Complex | Unit | string | Array | Matrix} x First value to compare
     85    * @param  {number | BigNumber | boolean | Complex | Unit | string | Array | Matrix} y Second value to compare
     86    * @return {boolean | Array | Matrix} Returns true when the compared values are equal, else returns false
     87    */
     88 
     89   return typed(name, {
     90     'any, any': function anyAny(x, y) {
     91       // strict equality for null and undefined?
     92       if (x === null) {
     93         return y === null;
     94       }
     95 
     96       if (y === null) {
     97         return x === null;
     98       }
     99 
    100       if (x === undefined) {
    101         return y === undefined;
    102       }
    103 
    104       if (y === undefined) {
    105         return x === undefined;
    106       }
    107 
    108       return equalScalar(x, y);
    109     },
    110     'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) {
    111       return algorithm07(x, y, equalScalar);
    112     },
    113     'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) {
    114       return algorithm03(y, x, equalScalar, true);
    115     },
    116     'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) {
    117       return algorithm03(x, y, equalScalar, false);
    118     },
    119     'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) {
    120       return algorithm13(x, y, equalScalar);
    121     },
    122     'Array, Array': function ArrayArray(x, y) {
    123       // use matrix implementation
    124       return this(matrix(x), matrix(y)).valueOf();
    125     },
    126     'Array, Matrix': function ArrayMatrix(x, y) {
    127       // use matrix implementation
    128       return this(matrix(x), y);
    129     },
    130     'Matrix, Array': function MatrixArray(x, y) {
    131       // use matrix implementation
    132       return this(x, matrix(y));
    133     },
    134     'SparseMatrix, any': function SparseMatrixAny(x, y) {
    135       return algorithm12(x, y, equalScalar, false);
    136     },
    137     'DenseMatrix, any': function DenseMatrixAny(x, y) {
    138       return algorithm14(x, y, equalScalar, false);
    139     },
    140     'any, SparseMatrix': function anySparseMatrix(x, y) {
    141       return algorithm12(y, x, equalScalar, true);
    142     },
    143     'any, DenseMatrix': function anyDenseMatrix(x, y) {
    144       return algorithm14(y, x, equalScalar, true);
    145     },
    146     'Array, any': function ArrayAny(x, y) {
    147       // use matrix implementation
    148       return algorithm14(matrix(x), y, equalScalar, false).valueOf();
    149     },
    150     'any, Array': function anyArray(x, y) {
    151       // use matrix implementation
    152       return algorithm14(matrix(y), x, equalScalar, true).valueOf();
    153     }
    154   });
    155 });
    156 exports.createEqual = createEqual;
    157 var createEqualNumber = (0, _factory.factory)(name, ['typed', 'equalScalar'], function (_ref2) {
    158   var typed = _ref2.typed,
    159       equalScalar = _ref2.equalScalar;
    160   return typed(name, {
    161     'any, any': function anyAny(x, y) {
    162       // strict equality for null and undefined?
    163       if (x === null) {
    164         return y === null;
    165       }
    166 
    167       if (y === null) {
    168         return x === null;
    169       }
    170 
    171       if (x === undefined) {
    172         return y === undefined;
    173       }
    174 
    175       if (y === undefined) {
    176         return x === undefined;
    177       }
    178 
    179       return equalScalar(x, y);
    180     }
    181   });
    182 });
    183 exports.createEqualNumber = createEqualNumber;