simple-squiggle

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

and.js (4403B)


      1 import { createAlgorithm02 } from '../../type/matrix/utils/algorithm02.js';
      2 import { createAlgorithm11 } from '../../type/matrix/utils/algorithm11.js';
      3 import { createAlgorithm13 } from '../../type/matrix/utils/algorithm13.js';
      4 import { createAlgorithm14 } from '../../type/matrix/utils/algorithm14.js';
      5 import { createAlgorithm06 } from '../../type/matrix/utils/algorithm06.js';
      6 import { factory } from '../../utils/factory.js';
      7 import { andNumber } from '../../plain/number/index.js';
      8 var name = 'and';
      9 var dependencies = ['typed', 'matrix', 'equalScalar', 'zeros', 'not'];
     10 export var createAnd = /* #__PURE__ */factory(name, dependencies, _ref => {
     11   var {
     12     typed,
     13     matrix,
     14     equalScalar,
     15     zeros,
     16     not
     17   } = _ref;
     18   var algorithm02 = createAlgorithm02({
     19     typed,
     20     equalScalar
     21   });
     22   var algorithm06 = createAlgorithm06({
     23     typed,
     24     equalScalar
     25   });
     26   var algorithm11 = createAlgorithm11({
     27     typed,
     28     equalScalar
     29   });
     30   var algorithm13 = createAlgorithm13({
     31     typed
     32   });
     33   var algorithm14 = createAlgorithm14({
     34     typed
     35   });
     36   /**
     37    * Logical `and`. Test whether two values are both defined with a nonzero/nonempty value.
     38    * For matrices, the function is evaluated element wise.
     39    *
     40    * Syntax:
     41    *
     42    *    math.and(x, y)
     43    *
     44    * Examples:
     45    *
     46    *    math.and(2, 4)   // returns true
     47    *
     48    *    a = [2, 0, 0]
     49    *    b = [3, 7, 0]
     50    *    c = 0
     51    *
     52    *    math.and(a, b)   // returns [true, false, false]
     53    *    math.and(a, c)   // returns [false, false, false]
     54    *
     55    * See also:
     56    *
     57    *    not, or, xor
     58    *
     59    * @param  {number | BigNumber | Complex | Unit | Array | Matrix} x First value to check
     60    * @param  {number | BigNumber | Complex | Unit | Array | Matrix} y Second value to check
     61    * @return {boolean | Array | Matrix}
     62    *            Returns true when both inputs are defined with a nonzero/nonempty value.
     63    */
     64 
     65   return typed(name, {
     66     'number, number': andNumber,
     67     'Complex, Complex': function ComplexComplex(x, y) {
     68       return (x.re !== 0 || x.im !== 0) && (y.re !== 0 || y.im !== 0);
     69     },
     70     'BigNumber, BigNumber': function BigNumberBigNumber(x, y) {
     71       return !x.isZero() && !y.isZero() && !x.isNaN() && !y.isNaN();
     72     },
     73     'Unit, Unit': function UnitUnit(x, y) {
     74       return this(x.value || 0, y.value || 0);
     75     },
     76     'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) {
     77       return algorithm06(x, y, this, false);
     78     },
     79     'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) {
     80       return algorithm02(y, x, this, true);
     81     },
     82     'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) {
     83       return algorithm02(x, y, this, false);
     84     },
     85     'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) {
     86       return algorithm13(x, y, this);
     87     },
     88     'Array, Array': function ArrayArray(x, y) {
     89       // use matrix implementation
     90       return this(matrix(x), matrix(y)).valueOf();
     91     },
     92     'Array, Matrix': function ArrayMatrix(x, y) {
     93       // use matrix implementation
     94       return this(matrix(x), y);
     95     },
     96     'Matrix, Array': function MatrixArray(x, y) {
     97       // use matrix implementation
     98       return this(x, matrix(y));
     99     },
    100     'SparseMatrix, any': function SparseMatrixAny(x, y) {
    101       // check scalar
    102       if (not(y)) {
    103         // return zero matrix
    104         return zeros(x.size(), x.storage());
    105       }
    106 
    107       return algorithm11(x, y, this, false);
    108     },
    109     'DenseMatrix, any': function DenseMatrixAny(x, y) {
    110       // check scalar
    111       if (not(y)) {
    112         // return zero matrix
    113         return zeros(x.size(), x.storage());
    114       }
    115 
    116       return algorithm14(x, y, this, false);
    117     },
    118     'any, SparseMatrix': function anySparseMatrix(x, y) {
    119       // check scalar
    120       if (not(x)) {
    121         // return zero matrix
    122         return zeros(x.size(), x.storage());
    123       }
    124 
    125       return algorithm11(y, x, this, true);
    126     },
    127     'any, DenseMatrix': function anyDenseMatrix(x, y) {
    128       // check scalar
    129       if (not(x)) {
    130         // return zero matrix
    131         return zeros(x.size(), x.storage());
    132       }
    133 
    134       return algorithm14(y, x, this, true);
    135     },
    136     'Array, any': function ArrayAny(x, y) {
    137       // use matrix implementation
    138       return this(matrix(x), y).valueOf();
    139     },
    140     'any, Array': function anyArray(x, y) {
    141       // use matrix implementation
    142       return this(x, matrix(y)).valueOf();
    143     }
    144   });
    145 });