simple-squiggle

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

equal.js (5341B)


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