simple-squiggle

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

xor.js (3900B)


      1 import { createAlgorithm03 } from '../../type/matrix/utils/algorithm03.js';
      2 import { createAlgorithm07 } from '../../type/matrix/utils/algorithm07.js';
      3 import { createAlgorithm12 } from '../../type/matrix/utils/algorithm12.js';
      4 import { createAlgorithm13 } from '../../type/matrix/utils/algorithm13.js';
      5 import { createAlgorithm14 } from '../../type/matrix/utils/algorithm14.js';
      6 import { factory } from '../../utils/factory.js';
      7 import { xorNumber } from '../../plain/number/index.js';
      8 var name = 'xor';
      9 var dependencies = ['typed', 'matrix', 'DenseMatrix'];
     10 export var createXor = /* #__PURE__ */factory(name, dependencies, _ref => {
     11   var {
     12     typed,
     13     matrix,
     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    * Logical `xor`. Test whether one and only one value is defined with a nonzero/nonempty value.
     35    * For matrices, the function is evaluated element wise.
     36    *
     37    * Syntax:
     38    *
     39    *    math.xor(x, y)
     40    *
     41    * Examples:
     42    *
     43    *    math.xor(2, 4)   // returns false
     44    *
     45    *    a = [2, 0, 0]
     46    *    b = [2, 7, 0]
     47    *    c = 0
     48    *
     49    *    math.xor(a, b)   // returns [false, true, false]
     50    *    math.xor(a, c)   // returns [true, false, false]
     51    *
     52    * See also:
     53    *
     54    *    and, not, or
     55    *
     56    * @param  {number | BigNumber | Complex | Unit | Array | Matrix} x First value to check
     57    * @param  {number | BigNumber | Complex | Unit | Array | Matrix} y Second value to check
     58    * @return {boolean | Array | Matrix}
     59    *            Returns true when one and only one input is defined with a nonzero/nonempty value.
     60    */
     61 
     62   return typed(name, {
     63     'number, number': xorNumber,
     64     'Complex, Complex': function ComplexComplex(x, y) {
     65       return (x.re !== 0 || x.im !== 0) !== (y.re !== 0 || y.im !== 0);
     66     },
     67     'BigNumber, BigNumber': function BigNumberBigNumber(x, y) {
     68       return (!x.isZero() && !x.isNaN()) !== (!y.isZero() && !y.isNaN());
     69     },
     70     'Unit, Unit': function UnitUnit(x, y) {
     71       return this(x.value || 0, y.value || 0);
     72     },
     73     'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) {
     74       return algorithm07(x, y, this);
     75     },
     76     'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) {
     77       return algorithm03(y, x, this, true);
     78     },
     79     'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) {
     80       return algorithm03(x, y, this, false);
     81     },
     82     'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) {
     83       return algorithm13(x, y, this);
     84     },
     85     'Array, Array': function ArrayArray(x, y) {
     86       // use matrix implementation
     87       return this(matrix(x), matrix(y)).valueOf();
     88     },
     89     'Array, Matrix': function ArrayMatrix(x, y) {
     90       // use matrix implementation
     91       return this(matrix(x), y);
     92     },
     93     'Matrix, Array': function MatrixArray(x, y) {
     94       // use matrix implementation
     95       return this(x, matrix(y));
     96     },
     97     'SparseMatrix, any': function SparseMatrixAny(x, y) {
     98       return algorithm12(x, y, this, false);
     99     },
    100     'DenseMatrix, any': function DenseMatrixAny(x, y) {
    101       return algorithm14(x, y, this, false);
    102     },
    103     'any, SparseMatrix': function anySparseMatrix(x, y) {
    104       return algorithm12(y, x, this, true);
    105     },
    106     'any, DenseMatrix': function anyDenseMatrix(x, y) {
    107       return algorithm14(y, x, this, true);
    108     },
    109     'Array, any': function ArrayAny(x, y) {
    110       // use matrix implementation
    111       return algorithm14(matrix(x), y, this, false).valueOf();
    112     },
    113     'any, Array': function anyArray(x, y) {
    114       // use matrix implementation
    115       return algorithm14(matrix(y), x, this, true).valueOf();
    116     }
    117   });
    118 });