UNPKG

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