simple-squiggle

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

divide.js (2794B)


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