mod.js 3.3 KB

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