lcm.js 3.4 KB

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