lcm.js 3.4 KB

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