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");
|
9 |
|
10 | var _algorithm = require("../../type/matrix/utils/algorithm01");
|
11 |
|
12 | var _algorithm2 = require("../../type/matrix/utils/algorithm04");
|
13 |
|
14 | var _algorithm3 = require("../../type/matrix/utils/algorithm10");
|
15 |
|
16 | var _algorithm4 = require("../../type/matrix/utils/algorithm13");
|
17 |
|
18 | var _algorithm5 = require("../../type/matrix/utils/algorithm14");
|
19 |
|
20 | var _number = require("../../plain/number");
|
21 |
|
22 | var name = 'gcd';
|
23 | var dependencies = ['typed', 'matrix', 'equalScalar', 'BigNumber', 'DenseMatrix'];
|
24 | var createGcd = (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 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | var gcd = typed(name, {
|
74 | 'number, number': _number.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, gcd);
|
81 | },
|
82 | 'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) {
|
83 | return algorithm01(y, x, gcd, true);
|
84 | },
|
85 | 'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) {
|
86 | return algorithm01(x, y, gcd, false);
|
87 | },
|
88 | 'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) {
|
89 | return algorithm13(x, y, gcd);
|
90 | },
|
91 | 'Array, Array': function ArrayArray(x, y) {
|
92 |
|
93 | return gcd(matrix(x), matrix(y)).valueOf();
|
94 | },
|
95 | 'Array, Matrix': function ArrayMatrix(x, y) {
|
96 |
|
97 | return gcd(matrix(x), y);
|
98 | },
|
99 | 'Matrix, Array': function MatrixArray(x, y) {
|
100 |
|
101 | return gcd(x, matrix(y));
|
102 | },
|
103 | 'SparseMatrix, number | BigNumber': function SparseMatrixNumberBigNumber(x, y) {
|
104 | return algorithm10(x, y, gcd, false);
|
105 | },
|
106 | 'DenseMatrix, number | BigNumber': function DenseMatrixNumberBigNumber(x, y) {
|
107 | return algorithm14(x, y, gcd, false);
|
108 | },
|
109 | 'number | BigNumber, SparseMatrix': function numberBigNumberSparseMatrix(x, y) {
|
110 | return algorithm10(y, x, gcd, true);
|
111 | },
|
112 | 'number | BigNumber, DenseMatrix': function numberBigNumberDenseMatrix(x, y) {
|
113 | return algorithm14(y, x, gcd, true);
|
114 | },
|
115 | 'Array, number | BigNumber': function ArrayNumberBigNumber(x, y) {
|
116 |
|
117 | return algorithm14(matrix(x), y, gcd, false).valueOf();
|
118 | },
|
119 | 'number | BigNumber, Array': function numberBigNumberArray(x, y) {
|
120 |
|
121 | return algorithm14(matrix(y), x, gcd, true).valueOf();
|
122 | },
|
123 |
|
124 | 'Array | Matrix | number | BigNumber, Array | Matrix | number | BigNumber, ...Array | Matrix | number | BigNumber': function ArrayMatrixNumberBigNumberArrayMatrixNumberBigNumberArrayMatrixNumberBigNumber(a, b, args) {
|
125 | var res = gcd(a, b);
|
126 |
|
127 | for (var i = 0; i < args.length; i++) {
|
128 | res = gcd(res, args[i]);
|
129 | }
|
130 |
|
131 | return res;
|
132 | }
|
133 | });
|
134 | return gcd;
|
135 | |
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 | function _gcdBigNumber(a, b) {
|
144 | if (!a.isInt() || !b.isInt()) {
|
145 | throw new Error('Parameters in function gcd must be integer numbers');
|
146 | }
|
147 |
|
148 |
|
149 | var zero = new BigNumber(0);
|
150 |
|
151 | while (!b.isZero()) {
|
152 | var r = a.mod(b);
|
153 | a = b;
|
154 | b = r;
|
155 | }
|
156 |
|
157 | return a.lt(zero) ? a.neg() : a;
|
158 | }
|
159 | });
|
160 | exports.createGcd = createGcd; |
\ | No newline at end of file |