lcm.js 3.0 KB

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