UNPKG

4.52 kBJavaScriptView Raw
1import { factory } from '../../utils/factory.js';
2import { createAlgorithm02 } from '../../type/matrix/utils/algorithm02.js';
3import { createAlgorithm03 } from '../../type/matrix/utils/algorithm03.js';
4import { createAlgorithm05 } from '../../type/matrix/utils/algorithm05.js';
5import { createAlgorithm11 } from '../../type/matrix/utils/algorithm11.js';
6import { createAlgorithm12 } from '../../type/matrix/utils/algorithm12.js';
7import { createAlgorithm13 } from '../../type/matrix/utils/algorithm13.js';
8import { createAlgorithm14 } from '../../type/matrix/utils/algorithm14.js';
9import { modNumber } from '../../plain/number/index.js';
10var name = 'mod';
11var dependencies = ['typed', 'matrix', 'equalScalar', 'DenseMatrix'];
12export var createMod = /* #__PURE__ */factory(name, dependencies, (_ref) => {
13 var {
14 typed,
15 matrix,
16 equalScalar,
17 DenseMatrix
18 } = _ref;
19 var algorithm02 = createAlgorithm02({
20 typed,
21 equalScalar
22 });
23 var algorithm03 = createAlgorithm03({
24 typed
25 });
26 var algorithm05 = createAlgorithm05({
27 typed,
28 equalScalar
29 });
30 var algorithm11 = createAlgorithm11({
31 typed,
32 equalScalar
33 });
34 var algorithm12 = createAlgorithm12({
35 typed,
36 DenseMatrix
37 });
38 var algorithm13 = createAlgorithm13({
39 typed
40 });
41 var algorithm14 = createAlgorithm14({
42 typed
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
80 return typed(name, {
81 'number, number': modNumber,
82 'BigNumber, BigNumber': function BigNumberBigNumber(x, y) {
83 if (y.isNeg()) {
84 throw new Error('Cannot calculate mod for a negative divisor');
85 }
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 } // Workaround suggested in Fraction.js library to calculate correct modulo for negative dividend
93
94
95 return x.compare(0) >= 0 ? x.mod(y) : x.mod(y).add(y).mod(y);
96 },
97 'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) {
98 return algorithm05(x, y, this, false);
99 },
100 'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) {
101 return algorithm02(y, x, this, true);
102 },
103 'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) {
104 return algorithm03(x, y, this, false);
105 },
106 'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) {
107 return algorithm13(x, y, this);
108 },
109 'Array, Array': function ArrayArray(x, y) {
110 // use matrix implementation
111 return this(matrix(x), matrix(y)).valueOf();
112 },
113 'Array, Matrix': function ArrayMatrix(x, y) {
114 // use matrix implementation
115 return this(matrix(x), y);
116 },
117 'Matrix, Array': function MatrixArray(x, y) {
118 // use matrix implementation
119 return this(x, matrix(y));
120 },
121 'SparseMatrix, any': function SparseMatrixAny(x, y) {
122 return algorithm11(x, y, this, false);
123 },
124 'DenseMatrix, any': function DenseMatrixAny(x, y) {
125 return algorithm14(x, y, this, false);
126 },
127 'any, SparseMatrix': function anySparseMatrix(x, y) {
128 return algorithm12(y, x, this, true);
129 },
130 'any, DenseMatrix': function anyDenseMatrix(x, y) {
131 return algorithm14(y, x, this, true);
132 },
133 'Array, any': function ArrayAny(x, y) {
134 // use matrix implementation
135 return algorithm14(matrix(x), y, this, false).valueOf();
136 },
137 'any, Array': function anyArray(x, y) {
138 // use matrix implementation
139 return algorithm14(matrix(y), x, this, true).valueOf();
140 }
141 });
142});
\No newline at end of file