1 | import { factory } from '../../utils/factory'
|
2 | import { createAlgorithm02 } from '../../type/matrix/utils/algorithm02'
|
3 | import { createAlgorithm06 } from '../../type/matrix/utils/algorithm06'
|
4 | import { createAlgorithm11 } from '../../type/matrix/utils/algorithm11'
|
5 | import { createAlgorithm13 } from '../../type/matrix/utils/algorithm13'
|
6 | import { createAlgorithm14 } from '../../type/matrix/utils/algorithm14'
|
7 | import { lcmNumber } from '../../plain/number'
|
8 |
|
9 | const name = 'lcm'
|
10 | const dependencies = [
|
11 | 'typed',
|
12 | 'matrix',
|
13 | 'equalScalar'
|
14 | ]
|
15 |
|
16 | export const createLcm = factory(name, dependencies, ({ typed, matrix, equalScalar }) => {
|
17 | const algorithm02 = createAlgorithm02({ typed, equalScalar })
|
18 | const algorithm06 = createAlgorithm06({ typed, equalScalar })
|
19 | const algorithm11 = createAlgorithm11({ typed, equalScalar })
|
20 | const algorithm13 = createAlgorithm13({ typed })
|
21 | const algorithm14 = createAlgorithm14({ typed })
|
22 |
|
23 | |
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | const lcm = typed(name, {
|
53 | 'number, number': lcmNumber,
|
54 |
|
55 | 'BigNumber, BigNumber': _lcmBigNumber,
|
56 |
|
57 | 'Fraction, Fraction': function (x, y) {
|
58 | return x.lcm(y)
|
59 | },
|
60 |
|
61 | 'SparseMatrix, SparseMatrix': function (x, y) {
|
62 | return algorithm06(x, y, lcm)
|
63 | },
|
64 |
|
65 | 'SparseMatrix, DenseMatrix': function (x, y) {
|
66 | return algorithm02(y, x, lcm, true)
|
67 | },
|
68 |
|
69 | 'DenseMatrix, SparseMatrix': function (x, y) {
|
70 | return algorithm02(x, y, lcm, false)
|
71 | },
|
72 |
|
73 | 'DenseMatrix, DenseMatrix': function (x, y) {
|
74 | return algorithm13(x, y, lcm)
|
75 | },
|
76 |
|
77 | 'Array, Array': function (x, y) {
|
78 |
|
79 | return lcm(matrix(x), matrix(y)).valueOf()
|
80 | },
|
81 |
|
82 | 'Array, Matrix': function (x, y) {
|
83 |
|
84 | return lcm(matrix(x), y)
|
85 | },
|
86 |
|
87 | 'Matrix, Array': function (x, y) {
|
88 |
|
89 | return lcm(x, matrix(y))
|
90 | },
|
91 |
|
92 | 'SparseMatrix, number | BigNumber': function (x, y) {
|
93 | return algorithm11(x, y, lcm, false)
|
94 | },
|
95 |
|
96 | 'DenseMatrix, number | BigNumber': function (x, y) {
|
97 | return algorithm14(x, y, lcm, false)
|
98 | },
|
99 |
|
100 | 'number | BigNumber, SparseMatrix': function (x, y) {
|
101 | return algorithm11(y, x, lcm, true)
|
102 | },
|
103 |
|
104 | 'number | BigNumber, DenseMatrix': function (x, y) {
|
105 | return algorithm14(y, x, lcm, true)
|
106 | },
|
107 |
|
108 | 'Array, number | BigNumber': function (x, y) {
|
109 |
|
110 | return algorithm14(matrix(x), y, lcm, false).valueOf()
|
111 | },
|
112 |
|
113 | 'number | BigNumber, Array': function (x, y) {
|
114 |
|
115 | return algorithm14(matrix(y), x, lcm, true).valueOf()
|
116 | },
|
117 |
|
118 |
|
119 | 'Array | Matrix | number | BigNumber, Array | Matrix | number | BigNumber, ...Array | Matrix | number | BigNumber': function (a, b, args) {
|
120 | let res = lcm(a, b)
|
121 | for (let i = 0; i < args.length; i++) {
|
122 | res = lcm(res, args[i])
|
123 | }
|
124 | return res
|
125 | }
|
126 | })
|
127 |
|
128 | return lcm
|
129 |
|
130 | |
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 | function _lcmBigNumber (a, b) {
|
138 | if (!a.isInt() || !b.isInt()) {
|
139 | throw new Error('Parameters in function lcm must be integer numbers')
|
140 | }
|
141 |
|
142 | if (a.isZero()) {
|
143 | return a
|
144 | }
|
145 | if (b.isZero()) {
|
146 | return b
|
147 | }
|
148 |
|
149 |
|
150 |
|
151 | const prod = a.times(b)
|
152 | while (!b.isZero()) {
|
153 | const t = b
|
154 | b = a.mod(t)
|
155 | a = t
|
156 | }
|
157 | return prod.div(a).abs()
|
158 | }
|
159 | })
|