simple-squiggle

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

subtract.js (5709B)


      1 "use strict";
      2 
      3 Object.defineProperty(exports, "__esModule", {
      4   value: true
      5 });
      6 exports.createSubtract = void 0;
      7 
      8 var _factory = require("../../utils/factory.js");
      9 
     10 var _DimensionError = require("../../error/DimensionError.js");
     11 
     12 var _algorithm = require("../../type/matrix/utils/algorithm01.js");
     13 
     14 var _algorithm2 = require("../../type/matrix/utils/algorithm03.js");
     15 
     16 var _algorithm3 = require("../../type/matrix/utils/algorithm05.js");
     17 
     18 var _algorithm4 = require("../../type/matrix/utils/algorithm10.js");
     19 
     20 var _algorithm5 = require("../../type/matrix/utils/algorithm13.js");
     21 
     22 var _algorithm6 = require("../../type/matrix/utils/algorithm14.js");
     23 
     24 var name = 'subtract';
     25 var dependencies = ['typed', 'matrix', 'equalScalar', 'addScalar', 'unaryMinus', 'DenseMatrix'];
     26 var createSubtract = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
     27   var typed = _ref.typed,
     28       matrix = _ref.matrix,
     29       equalScalar = _ref.equalScalar,
     30       addScalar = _ref.addScalar,
     31       unaryMinus = _ref.unaryMinus,
     32       DenseMatrix = _ref.DenseMatrix;
     33   // TODO: split function subtract in two: subtract and subtractScalar
     34   var algorithm01 = (0, _algorithm.createAlgorithm01)({
     35     typed: typed
     36   });
     37   var algorithm03 = (0, _algorithm2.createAlgorithm03)({
     38     typed: typed
     39   });
     40   var algorithm05 = (0, _algorithm3.createAlgorithm05)({
     41     typed: typed,
     42     equalScalar: equalScalar
     43   });
     44   var algorithm10 = (0, _algorithm4.createAlgorithm10)({
     45     typed: typed,
     46     DenseMatrix: DenseMatrix
     47   });
     48   var algorithm13 = (0, _algorithm5.createAlgorithm13)({
     49     typed: typed
     50   });
     51   var algorithm14 = (0, _algorithm6.createAlgorithm14)({
     52     typed: typed
     53   });
     54   /**
     55    * Subtract two values, `x - y`.
     56    * For matrices, the function is evaluated element wise.
     57    *
     58    * Syntax:
     59    *
     60    *    math.subtract(x, y)
     61    *
     62    * Examples:
     63    *
     64    *    math.subtract(5.3, 2)        // returns number 3.3
     65    *
     66    *    const a = math.complex(2, 3)
     67    *    const b = math.complex(4, 1)
     68    *    math.subtract(a, b)          // returns Complex -2 + 2i
     69    *
     70    *    math.subtract([5, 7, 4], 4)  // returns Array [1, 3, 0]
     71    *
     72    *    const c = math.unit('2.1 km')
     73    *    const d = math.unit('500m')
     74    *    math.subtract(c, d)          // returns Unit 1.6 km
     75    *
     76    * See also:
     77    *
     78    *    add
     79    *
     80    * @param  {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x
     81    *            Initial value
     82    * @param  {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} y
     83    *            Value to subtract from `x`
     84    * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix}
     85    *            Subtraction of `x` and `y`
     86    */
     87 
     88   return typed(name, {
     89     'number, number': function numberNumber(x, y) {
     90       return x - y;
     91     },
     92     'Complex, Complex': function ComplexComplex(x, y) {
     93       return x.sub(y);
     94     },
     95     'BigNumber, BigNumber': function BigNumberBigNumber(x, y) {
     96       return x.minus(y);
     97     },
     98     'Fraction, Fraction': function FractionFraction(x, y) {
     99       return x.sub(y);
    100     },
    101     'Unit, Unit': function UnitUnit(x, y) {
    102       if (x.value === null) {
    103         throw new Error('Parameter x contains a unit with undefined value');
    104       }
    105 
    106       if (y.value === null) {
    107         throw new Error('Parameter y contains a unit with undefined value');
    108       }
    109 
    110       if (!x.equalBase(y)) {
    111         throw new Error('Units do not match');
    112       }
    113 
    114       var res = x.clone();
    115       res.value = this(res.value, y.value);
    116       res.fixPrefix = false;
    117       return res;
    118     },
    119     'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) {
    120       checkEqualDimensions(x, y);
    121       return algorithm05(x, y, this);
    122     },
    123     'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) {
    124       checkEqualDimensions(x, y);
    125       return algorithm03(y, x, this, true);
    126     },
    127     'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) {
    128       checkEqualDimensions(x, y);
    129       return algorithm01(x, y, this, false);
    130     },
    131     'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) {
    132       checkEqualDimensions(x, y);
    133       return algorithm13(x, y, this);
    134     },
    135     'Array, Array': function ArrayArray(x, y) {
    136       // use matrix implementation
    137       return this(matrix(x), matrix(y)).valueOf();
    138     },
    139     'Array, Matrix': function ArrayMatrix(x, y) {
    140       // use matrix implementation
    141       return this(matrix(x), y);
    142     },
    143     'Matrix, Array': function MatrixArray(x, y) {
    144       // use matrix implementation
    145       return this(x, matrix(y));
    146     },
    147     'SparseMatrix, any': function SparseMatrixAny(x, y) {
    148       return algorithm10(x, unaryMinus(y), addScalar);
    149     },
    150     'DenseMatrix, any': function DenseMatrixAny(x, y) {
    151       return algorithm14(x, y, this);
    152     },
    153     'any, SparseMatrix': function anySparseMatrix(x, y) {
    154       return algorithm10(y, x, this, true);
    155     },
    156     'any, DenseMatrix': function anyDenseMatrix(x, y) {
    157       return algorithm14(y, x, this, true);
    158     },
    159     'Array, any': function ArrayAny(x, y) {
    160       // use matrix implementation
    161       return algorithm14(matrix(x), y, this, false).valueOf();
    162     },
    163     'any, Array': function anyArray(x, y) {
    164       // use matrix implementation
    165       return algorithm14(matrix(y), x, this, true).valueOf();
    166     }
    167   });
    168 });
    169 /**
    170  * Check whether matrix x and y have the same number of dimensions.
    171  * Throws a DimensionError when dimensions are not equal
    172  * @param {Matrix} x
    173  * @param {Matrix} y
    174  */
    175 
    176 exports.createSubtract = createSubtract;
    177 
    178 function checkEqualDimensions(x, y) {
    179   var xsize = x.size();
    180   var ysize = y.size();
    181 
    182   if (xsize.length !== ysize.length) {
    183     throw new _DimensionError.DimensionError(xsize.length, ysize.length);
    184   }
    185 }