mod.js 3.0 KB

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