mod.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createMod = void 0;
  6. var _factory = require("../../utils/factory.js");
  7. var _matAlgo02xDS = require("../../type/matrix/utils/matAlgo02xDS0.js");
  8. var _matAlgo03xDSf = require("../../type/matrix/utils/matAlgo03xDSf.js");
  9. var _matAlgo05xSfSf = require("../../type/matrix/utils/matAlgo05xSfSf.js");
  10. var _matAlgo11xS0s = require("../../type/matrix/utils/matAlgo11xS0s.js");
  11. var _matAlgo12xSfs = require("../../type/matrix/utils/matAlgo12xSfs.js");
  12. var _index = require("../../plain/number/index.js");
  13. var _matrixAlgorithmSuite = require("../../type/matrix/utils/matrixAlgorithmSuite.js");
  14. var name = 'mod';
  15. var dependencies = ['typed', 'matrix', 'equalScalar', 'DenseMatrix', 'concat'];
  16. var createMod = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
  17. var typed = _ref.typed,
  18. matrix = _ref.matrix,
  19. equalScalar = _ref.equalScalar,
  20. DenseMatrix = _ref.DenseMatrix,
  21. concat = _ref.concat;
  22. var matAlgo02xDS0 = (0, _matAlgo02xDS.createMatAlgo02xDS0)({
  23. typed: typed,
  24. equalScalar: equalScalar
  25. });
  26. var matAlgo03xDSf = (0, _matAlgo03xDSf.createMatAlgo03xDSf)({
  27. typed: typed
  28. });
  29. var matAlgo05xSfSf = (0, _matAlgo05xSfSf.createMatAlgo05xSfSf)({
  30. typed: typed,
  31. equalScalar: equalScalar
  32. });
  33. var matAlgo11xS0s = (0, _matAlgo11xS0s.createMatAlgo11xS0s)({
  34. typed: typed,
  35. equalScalar: equalScalar
  36. });
  37. var matAlgo12xSfs = (0, _matAlgo12xSfs.createMatAlgo12xSfs)({
  38. typed: typed,
  39. DenseMatrix: DenseMatrix
  40. });
  41. var matrixAlgorithmSuite = (0, _matrixAlgorithmSuite.createMatrixAlgorithmSuite)({
  42. typed: typed,
  43. matrix: matrix,
  44. concat: concat
  45. });
  46. /**
  47. * Calculates the modulus, the remainder of an integer division.
  48. *
  49. * For matrices, the function is evaluated element wise.
  50. *
  51. * The modulus is defined as:
  52. *
  53. * x - y * floor(x / y)
  54. *
  55. * See https://en.wikipedia.org/wiki/Modulo_operation.
  56. *
  57. * Syntax:
  58. *
  59. * math.mod(x, y)
  60. *
  61. * Examples:
  62. *
  63. * math.mod(8, 3) // returns 2
  64. * math.mod(11, 2) // returns 1
  65. *
  66. * function isOdd(x) {
  67. * return math.mod(x, 2) != 0
  68. * }
  69. *
  70. * isOdd(2) // returns false
  71. * isOdd(3) // returns true
  72. *
  73. * See also:
  74. *
  75. * divide
  76. *
  77. * @param {number | BigNumber | Fraction | Array | Matrix} x Dividend
  78. * @param {number | BigNumber | Fraction | Array | Matrix} y Divisor
  79. * @return {number | BigNumber | Fraction | Array | Matrix} Returns the remainder of `x` divided by `y`.
  80. */
  81. return typed(name, {
  82. 'number, number': _index.modNumber,
  83. 'BigNumber, BigNumber': function BigNumberBigNumber(x, y) {
  84. if (y.isNeg()) {
  85. throw new Error('Cannot calculate mod for a negative divisor');
  86. }
  87. return y.isZero() ? x : x.mod(y);
  88. },
  89. 'Fraction, Fraction': function FractionFraction(x, y) {
  90. if (y.compare(0) < 0) {
  91. throw new Error('Cannot calculate mod for a negative divisor');
  92. }
  93. // Workaround suggested in Fraction.js library to calculate correct modulo for negative dividend
  94. return x.compare(0) >= 0 ? x.mod(y) : x.mod(y).add(y).mod(y);
  95. }
  96. }, matrixAlgorithmSuite({
  97. SS: matAlgo05xSfSf,
  98. DS: matAlgo03xDSf,
  99. SD: matAlgo02xDS0,
  100. Ss: matAlgo11xS0s,
  101. sS: matAlgo12xSfs
  102. }));
  103. });
  104. exports.createMod = createMod;