simple-squiggle

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

divide.js (3106B)


      1 "use strict";
      2 
      3 Object.defineProperty(exports, "__esModule", {
      4   value: true
      5 });
      6 exports.createDivide = void 0;
      7 
      8 var _factory = require("../../utils/factory.js");
      9 
     10 var _object = require("../../utils/object.js");
     11 
     12 var _algorithm = require("../../type/matrix/utils/algorithm11.js");
     13 
     14 var _algorithm2 = require("../../type/matrix/utils/algorithm14.js");
     15 
     16 var name = 'divide';
     17 var dependencies = ['typed', 'matrix', 'multiply', 'equalScalar', 'divideScalar', 'inv'];
     18 var createDivide = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
     19   var typed = _ref.typed,
     20       matrix = _ref.matrix,
     21       multiply = _ref.multiply,
     22       equalScalar = _ref.equalScalar,
     23       divideScalar = _ref.divideScalar,
     24       inv = _ref.inv;
     25   var algorithm11 = (0, _algorithm.createAlgorithm11)({
     26     typed: typed,
     27     equalScalar: equalScalar
     28   });
     29   var algorithm14 = (0, _algorithm2.createAlgorithm14)({
     30     typed: typed
     31   });
     32   /**
     33    * Divide two values, `x / y`.
     34    * To divide matrices, `x` is multiplied with the inverse of `y`: `x * inv(y)`.
     35    *
     36    * Syntax:
     37    *
     38    *    math.divide(x, y)
     39    *
     40    * Examples:
     41    *
     42    *    math.divide(2, 3)            // returns number 0.6666666666666666
     43    *
     44    *    const a = math.complex(5, 14)
     45    *    const b = math.complex(4, 1)
     46    *    math.divide(a, b)            // returns Complex 2 + 3i
     47    *
     48    *    const c = [[7, -6], [13, -4]]
     49    *    const d = [[1, 2], [4, 3]]
     50    *    math.divide(c, d)            // returns Array [[-9, 4], [-11, 6]]
     51    *
     52    *    const e = math.unit('18 km')
     53    *    math.divide(e, 4.5)          // returns Unit 4 km
     54    *
     55    * See also:
     56    *
     57    *    multiply
     58    *
     59    * @param  {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x   Numerator
     60    * @param  {number | BigNumber | Fraction | Complex | Array | Matrix} y          Denominator
     61    * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix}                      Quotient, `x / y`
     62    */
     63 
     64   return typed('divide', (0, _object.extend)({
     65     // we extend the signatures of divideScalar with signatures dealing with matrices
     66     'Array | Matrix, Array | Matrix': function ArrayMatrixArrayMatrix(x, y) {
     67       // TODO: implement matrix right division using pseudo inverse
     68       // https://www.mathworks.nl/help/matlab/ref/mrdivide.html
     69       // https://www.gnu.org/software/octave/doc/interpreter/Arithmetic-Ops.html
     70       // https://stackoverflow.com/questions/12263932/how-does-gnu-octave-matrix-division-work-getting-unexpected-behaviour
     71       return multiply(x, inv(y));
     72     },
     73     'DenseMatrix, any': function DenseMatrixAny(x, y) {
     74       return algorithm14(x, y, divideScalar, false);
     75     },
     76     'SparseMatrix, any': function SparseMatrixAny(x, y) {
     77       return algorithm11(x, y, divideScalar, false);
     78     },
     79     'Array, any': function ArrayAny(x, y) {
     80       // use matrix implementation
     81       return algorithm14(matrix(x), y, divideScalar, false).valueOf();
     82     },
     83     'any, Array | Matrix': function anyArrayMatrix(x, y) {
     84       return multiply(x, inv(y));
     85     }
     86   }, divideScalar.signatures));
     87 });
     88 exports.createDivide = createDivide;