UNPKG

4.42 kBJavaScriptView Raw
1import { factory } from '../../utils/factory'
2import { createAlgorithm02 } from '../../type/matrix/utils/algorithm02'
3import { createAlgorithm06 } from '../../type/matrix/utils/algorithm06'
4import { createAlgorithm11 } from '../../type/matrix/utils/algorithm11'
5import { createAlgorithm13 } from '../../type/matrix/utils/algorithm13'
6import { createAlgorithm14 } from '../../type/matrix/utils/algorithm14'
7import { lcmNumber } from '../../plain/number'
8
9const name = 'lcm'
10const dependencies = [
11 'typed',
12 'matrix',
13 'equalScalar'
14]
15
16export const createLcm = /* #__PURE__ */ 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 * Calculate the least common multiple for two or more values or arrays.
25 *
26 * lcm is defined as:
27 *
28 * lcm(a, b) = abs(a * b) / gcd(a, b)
29 *
30 * For matrices, the function is evaluated element wise.
31 *
32 * Syntax:
33 *
34 * math.lcm(a, b)
35 * math.lcm(a, b, c, ...)
36 *
37 * Examples:
38 *
39 * math.lcm(4, 6) // returns 12
40 * math.lcm(6, 21) // returns 42
41 * math.lcm(6, 21, 5) // returns 210
42 *
43 * math.lcm([4, 6], [6, 21]) // returns [12, 42]
44 *
45 * See also:
46 *
47 * gcd, xgcd
48 *
49 * @param {... number | BigNumber | Array | Matrix} args Two or more integer numbers
50 * @return {number | BigNumber | Array | Matrix} The least common multiple
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 // use matrix implementation
79 return lcm(matrix(x), matrix(y)).valueOf()
80 },
81
82 'Array, Matrix': function (x, y) {
83 // use matrix implementation
84 return lcm(matrix(x), y)
85 },
86
87 'Matrix, Array': function (x, y) {
88 // use matrix implementation
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 // use matrix implementation
110 return algorithm14(matrix(x), y, lcm, false).valueOf()
111 },
112
113 'number | BigNumber, Array': function (x, y) {
114 // use matrix implementation
115 return algorithm14(matrix(y), x, lcm, true).valueOf()
116 },
117
118 // TODO: need a smarter notation here
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 * Calculate lcm for two BigNumbers
132 * @param {BigNumber} a
133 * @param {BigNumber} b
134 * @returns {BigNumber} Returns the least common multiple of a and b
135 * @private
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 // https://en.wikipedia.org/wiki/Euclidean_algorithm
150 // evaluate lcm here inline to reduce overhead
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})