UNPKG

3.83 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'
10
11const name = 'mod'
12const dependencies = [
13 'typed',
14 'matrix',
15 'equalScalar',
16 'DenseMatrix'
17]
18
19export const createMod = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, equalScalar, DenseMatrix }) => {
20 const algorithm02 = createAlgorithm02({ typed, equalScalar })
21 const algorithm03 = createAlgorithm03({ typed })
22 const algorithm05 = createAlgorithm05({ typed, equalScalar })
23 const algorithm11 = createAlgorithm11({ typed, equalScalar })
24 const algorithm12 = createAlgorithm12({ typed, DenseMatrix })
25 const algorithm13 = createAlgorithm13({ typed })
26 const algorithm14 = createAlgorithm14({ typed })
27
28 /**
29 * Calculates the modulus, the remainder of an integer division.
30 *
31 * For matrices, the function is evaluated element wise.
32 *
33 * The modulus is defined as:
34 *
35 * x - y * floor(x / y)
36 *
37 * See https://en.wikipedia.org/wiki/Modulo_operation.
38 *
39 * Syntax:
40 *
41 * math.mod(x, y)
42 *
43 * Examples:
44 *
45 * math.mod(8, 3) // returns 2
46 * math.mod(11, 2) // returns 1
47 *
48 * function isOdd(x) {
49 * return math.mod(x, 2) != 0
50 * }
51 *
52 * isOdd(2) // returns false
53 * isOdd(3) // returns true
54 *
55 * See also:
56 *
57 * divide
58 *
59 * @param {number | BigNumber | Fraction | Array | Matrix} x Dividend
60 * @param {number | BigNumber | Fraction | Array | Matrix} y Divisor
61 * @return {number | BigNumber | Fraction | Array | Matrix} Returns the remainder of `x` divided by `y`.
62 */
63 const mod = typed(name, {
64
65 'number, number': modNumber,
66
67 'BigNumber, BigNumber': function (x, y) {
68 return y.isZero() ? x : x.mod(y)
69 },
70
71 'Fraction, Fraction': function (x, y) {
72 return x.mod(y)
73 },
74
75 'SparseMatrix, SparseMatrix': function (x, y) {
76 return algorithm05(x, y, mod, false)
77 },
78
79 'SparseMatrix, DenseMatrix': function (x, y) {
80 return algorithm02(y, x, mod, true)
81 },
82
83 'DenseMatrix, SparseMatrix': function (x, y) {
84 return algorithm03(x, y, mod, false)
85 },
86
87 'DenseMatrix, DenseMatrix': function (x, y) {
88 return algorithm13(x, y, mod)
89 },
90
91 'Array, Array': function (x, y) {
92 // use matrix implementation
93 return mod(matrix(x), matrix(y)).valueOf()
94 },
95
96 'Array, Matrix': function (x, y) {
97 // use matrix implementation
98 return mod(matrix(x), y)
99 },
100
101 'Matrix, Array': function (x, y) {
102 // use matrix implementation
103 return mod(x, matrix(y))
104 },
105
106 'SparseMatrix, any': function (x, y) {
107 return algorithm11(x, y, mod, false)
108 },
109
110 'DenseMatrix, any': function (x, y) {
111 return algorithm14(x, y, mod, false)
112 },
113
114 'any, SparseMatrix': function (x, y) {
115 return algorithm12(y, x, mod, true)
116 },
117
118 'any, DenseMatrix': function (x, y) {
119 return algorithm14(y, x, mod, true)
120 },
121
122 'Array, any': function (x, y) {
123 // use matrix implementation
124 return algorithm14(matrix(x), y, mod, false).valueOf()
125 },
126
127 'any, Array': function (x, y) {
128 // use matrix implementation
129 return algorithm14(matrix(y), x, mod, true).valueOf()
130 }
131 })
132
133 return mod
134})