lcm.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { factory } from '../../utils/factory.js';
  2. import { createMatAlgo02xDS0 } from '../../type/matrix/utils/matAlgo02xDS0.js';
  3. import { createMatAlgo06xS0S0 } from '../../type/matrix/utils/matAlgo06xS0S0.js';
  4. import { createMatAlgo11xS0s } from '../../type/matrix/utils/matAlgo11xS0s.js';
  5. import { createMatrixAlgorithmSuite } from '../../type/matrix/utils/matrixAlgorithmSuite.js';
  6. import { lcmNumber } from '../../plain/number/index.js';
  7. var name = 'lcm';
  8. var dependencies = ['typed', 'matrix', 'equalScalar', 'concat'];
  9. export var createLcm = /* #__PURE__ */factory(name, dependencies, _ref => {
  10. var {
  11. typed,
  12. matrix,
  13. equalScalar,
  14. concat
  15. } = _ref;
  16. var matAlgo02xDS0 = createMatAlgo02xDS0({
  17. typed,
  18. equalScalar
  19. });
  20. var matAlgo06xS0S0 = createMatAlgo06xS0S0({
  21. typed,
  22. equalScalar
  23. });
  24. var matAlgo11xS0s = createMatAlgo11xS0s({
  25. typed,
  26. equalScalar
  27. });
  28. var matrixAlgorithmSuite = createMatrixAlgorithmSuite({
  29. typed,
  30. matrix,
  31. concat
  32. });
  33. var lcmTypes = 'number | BigNumber | Fraction | Matrix | Array';
  34. var lcmManySignature = {};
  35. lcmManySignature["".concat(lcmTypes, ", ").concat(lcmTypes, ", ...").concat(lcmTypes)] = typed.referToSelf(self => (a, b, args) => {
  36. var res = self(a, b);
  37. for (var i = 0; i < args.length; i++) {
  38. res = self(res, args[i]);
  39. }
  40. return res;
  41. });
  42. /**
  43. * Calculate the least common multiple for two or more values or arrays.
  44. *
  45. * lcm is defined as:
  46. *
  47. * lcm(a, b) = abs(a * b) / gcd(a, b)
  48. *
  49. * For matrices, the function is evaluated element wise.
  50. *
  51. * Syntax:
  52. *
  53. * math.lcm(a, b)
  54. * math.lcm(a, b, c, ...)
  55. *
  56. * Examples:
  57. *
  58. * math.lcm(4, 6) // returns 12
  59. * math.lcm(6, 21) // returns 42
  60. * math.lcm(6, 21, 5) // returns 210
  61. *
  62. * math.lcm([4, 6], [6, 21]) // returns [12, 42]
  63. *
  64. * See also:
  65. *
  66. * gcd, xgcd
  67. *
  68. * @param {... number | BigNumber | Array | Matrix} args Two or more integer numbers
  69. * @return {number | BigNumber | Array | Matrix} The least common multiple
  70. */
  71. return typed(name, {
  72. 'number, number': lcmNumber,
  73. 'BigNumber, BigNumber': _lcmBigNumber,
  74. 'Fraction, Fraction': (x, y) => x.lcm(y)
  75. }, matrixAlgorithmSuite({
  76. SS: matAlgo06xS0S0,
  77. DS: matAlgo02xDS0,
  78. Ss: matAlgo11xS0s
  79. }), lcmManySignature);
  80. /**
  81. * Calculate lcm for two BigNumbers
  82. * @param {BigNumber} a
  83. * @param {BigNumber} b
  84. * @returns {BigNumber} Returns the least common multiple of a and b
  85. * @private
  86. */
  87. function _lcmBigNumber(a, b) {
  88. if (!a.isInt() || !b.isInt()) {
  89. throw new Error('Parameters in function lcm must be integer numbers');
  90. }
  91. if (a.isZero()) {
  92. return a;
  93. }
  94. if (b.isZero()) {
  95. return b;
  96. }
  97. // https://en.wikipedia.org/wiki/Euclidean_algorithm
  98. // evaluate lcm here inline to reduce overhead
  99. var prod = a.times(b);
  100. while (!b.isZero()) {
  101. var t = b;
  102. b = a.mod(t);
  103. a = t;
  104. }
  105. return prod.div(a).abs();
  106. }
  107. });