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