simple-squiggle

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

lcm.js (5191B)


      1 "use strict";
      2 
      3 Object.defineProperty(exports, "__esModule", {
      4   value: true
      5 });
      6 exports.createLcm = void 0;
      7 
      8 var _factory = require("../../utils/factory.js");
      9 
     10 var _algorithm = require("../../type/matrix/utils/algorithm02.js");
     11 
     12 var _algorithm2 = require("../../type/matrix/utils/algorithm06.js");
     13 
     14 var _algorithm3 = require("../../type/matrix/utils/algorithm11.js");
     15 
     16 var _algorithm4 = require("../../type/matrix/utils/algorithm13.js");
     17 
     18 var _algorithm5 = require("../../type/matrix/utils/algorithm14.js");
     19 
     20 var _index = require("../../plain/number/index.js");
     21 
     22 var name = 'lcm';
     23 var dependencies = ['typed', 'matrix', 'equalScalar'];
     24 var createLcm = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
     25   var typed = _ref.typed,
     26       matrix = _ref.matrix,
     27       equalScalar = _ref.equalScalar;
     28   var algorithm02 = (0, _algorithm.createAlgorithm02)({
     29     typed: typed,
     30     equalScalar: equalScalar
     31   });
     32   var algorithm06 = (0, _algorithm2.createAlgorithm06)({
     33     typed: typed,
     34     equalScalar: equalScalar
     35   });
     36   var algorithm11 = (0, _algorithm3.createAlgorithm11)({
     37     typed: typed,
     38     equalScalar: equalScalar
     39   });
     40   var algorithm13 = (0, _algorithm4.createAlgorithm13)({
     41     typed: typed
     42   });
     43   var algorithm14 = (0, _algorithm5.createAlgorithm14)({
     44     typed: typed
     45   });
     46   /**
     47    * Calculate the least common multiple for two or more values or arrays.
     48    *
     49    * lcm is defined as:
     50    *
     51    *     lcm(a, b) = abs(a * b) / gcd(a, b)
     52    *
     53    * For matrices, the function is evaluated element wise.
     54    *
     55    * Syntax:
     56    *
     57    *    math.lcm(a, b)
     58    *    math.lcm(a, b, c, ...)
     59    *
     60    * Examples:
     61    *
     62    *    math.lcm(4, 6)               // returns 12
     63    *    math.lcm(6, 21)              // returns 42
     64    *    math.lcm(6, 21, 5)           // returns 210
     65    *
     66    *    math.lcm([4, 6], [6, 21])    // returns [12, 42]
     67    *
     68    * See also:
     69    *
     70    *    gcd, xgcd
     71    *
     72    * @param {... number | BigNumber | Array | Matrix} args  Two or more integer numbers
     73    * @return {number | BigNumber | Array | Matrix}                           The least common multiple
     74    */
     75 
     76   return typed(name, {
     77     'number, number': _index.lcmNumber,
     78     'BigNumber, BigNumber': _lcmBigNumber,
     79     'Fraction, Fraction': function FractionFraction(x, y) {
     80       return x.lcm(y);
     81     },
     82     'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) {
     83       return algorithm06(x, y, this);
     84     },
     85     'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) {
     86       return algorithm02(y, x, this, true);
     87     },
     88     'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) {
     89       return algorithm02(x, y, this, false);
     90     },
     91     'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) {
     92       return algorithm13(x, y, this);
     93     },
     94     'Array, Array': function ArrayArray(x, y) {
     95       // use matrix implementation
     96       return this(matrix(x), matrix(y)).valueOf();
     97     },
     98     'Array, Matrix': function ArrayMatrix(x, y) {
     99       // use matrix implementation
    100       return this(matrix(x), y);
    101     },
    102     'Matrix, Array': function MatrixArray(x, y) {
    103       // use matrix implementation
    104       return this(x, matrix(y));
    105     },
    106     'SparseMatrix, number | BigNumber': function SparseMatrixNumberBigNumber(x, y) {
    107       return algorithm11(x, y, this, false);
    108     },
    109     'DenseMatrix, number | BigNumber': function DenseMatrixNumberBigNumber(x, y) {
    110       return algorithm14(x, y, this, false);
    111     },
    112     'number | BigNumber, SparseMatrix': function numberBigNumberSparseMatrix(x, y) {
    113       return algorithm11(y, x, this, true);
    114     },
    115     'number | BigNumber, DenseMatrix': function numberBigNumberDenseMatrix(x, y) {
    116       return algorithm14(y, x, this, true);
    117     },
    118     'Array, number | BigNumber': function ArrayNumberBigNumber(x, y) {
    119       // use matrix implementation
    120       return algorithm14(matrix(x), y, this, false).valueOf();
    121     },
    122     'number | BigNumber, Array': function numberBigNumberArray(x, y) {
    123       // use matrix implementation
    124       return algorithm14(matrix(y), x, this, true).valueOf();
    125     },
    126     // TODO: need a smarter notation here
    127     'Array | Matrix | number | BigNumber, Array | Matrix | number | BigNumber, ...Array | Matrix | number | BigNumber': function ArrayMatrixNumberBigNumberArrayMatrixNumberBigNumberArrayMatrixNumberBigNumber(a, b, args) {
    128       var res = this(a, b);
    129 
    130       for (var i = 0; i < args.length; i++) {
    131         res = this(res, args[i]);
    132       }
    133 
    134       return res;
    135     }
    136   });
    137   /**
    138    * Calculate lcm for two BigNumbers
    139    * @param {BigNumber} a
    140    * @param {BigNumber} b
    141    * @returns {BigNumber} Returns the least common multiple of a and b
    142    * @private
    143    */
    144 
    145   function _lcmBigNumber(a, b) {
    146     if (!a.isInt() || !b.isInt()) {
    147       throw new Error('Parameters in function lcm must be integer numbers');
    148     }
    149 
    150     if (a.isZero()) {
    151       return a;
    152     }
    153 
    154     if (b.isZero()) {
    155       return b;
    156     } // https://en.wikipedia.org/wiki/Euclidean_algorithm
    157     // evaluate lcm here inline to reduce overhead
    158 
    159 
    160     var prod = a.times(b);
    161 
    162     while (!b.isZero()) {
    163       var t = b;
    164       b = a.mod(t);
    165       a = t;
    166     }
    167 
    168     return prod.div(a).abs();
    169   }
    170 });
    171 exports.createLcm = createLcm;