1 | 'use strict'
|
2 |
|
3 | function factory (type, config, load, typed) {
|
4 | const matrix = load(require('../../type/matrix/function/matrix'))
|
5 | const latex = require('../../utils/latex')
|
6 |
|
7 | const algorithm02 = load(require('../../type/matrix/utils/algorithm02'))
|
8 | const algorithm03 = load(require('../../type/matrix/utils/algorithm03'))
|
9 | const algorithm05 = load(require('../../type/matrix/utils/algorithm05'))
|
10 | const algorithm11 = load(require('../../type/matrix/utils/algorithm11'))
|
11 | const algorithm12 = load(require('../../type/matrix/utils/algorithm12'))
|
12 | const algorithm13 = load(require('../../type/matrix/utils/algorithm13'))
|
13 | const algorithm14 = load(require('../../type/matrix/utils/algorithm14'))
|
14 |
|
15 | |
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | const mod = typed('mod', {
|
51 |
|
52 | 'number, number': _mod,
|
53 |
|
54 | 'BigNumber, BigNumber': function (x, y) {
|
55 | return y.isZero() ? x : x.mod(y)
|
56 | },
|
57 |
|
58 | 'Fraction, Fraction': function (x, y) {
|
59 | return x.mod(y)
|
60 | },
|
61 |
|
62 | 'SparseMatrix, SparseMatrix': function (x, y) {
|
63 | return algorithm05(x, y, mod, false)
|
64 | },
|
65 |
|
66 | 'SparseMatrix, DenseMatrix': function (x, y) {
|
67 | return algorithm02(y, x, mod, true)
|
68 | },
|
69 |
|
70 | 'DenseMatrix, SparseMatrix': function (x, y) {
|
71 | return algorithm03(x, y, mod, false)
|
72 | },
|
73 |
|
74 | 'DenseMatrix, DenseMatrix': function (x, y) {
|
75 | return algorithm13(x, y, mod)
|
76 | },
|
77 |
|
78 | 'Array, Array': function (x, y) {
|
79 |
|
80 | return mod(matrix(x), matrix(y)).valueOf()
|
81 | },
|
82 |
|
83 | 'Array, Matrix': function (x, y) {
|
84 |
|
85 | return mod(matrix(x), y)
|
86 | },
|
87 |
|
88 | 'Matrix, Array': function (x, y) {
|
89 |
|
90 | return mod(x, matrix(y))
|
91 | },
|
92 |
|
93 | 'SparseMatrix, any': function (x, y) {
|
94 | return algorithm11(x, y, mod, false)
|
95 | },
|
96 |
|
97 | 'DenseMatrix, any': function (x, y) {
|
98 | return algorithm14(x, y, mod, false)
|
99 | },
|
100 |
|
101 | 'any, SparseMatrix': function (x, y) {
|
102 | return algorithm12(y, x, mod, true)
|
103 | },
|
104 |
|
105 | 'any, DenseMatrix': function (x, y) {
|
106 | return algorithm14(y, x, mod, true)
|
107 | },
|
108 |
|
109 | 'Array, any': function (x, y) {
|
110 |
|
111 | return algorithm14(matrix(x), y, mod, false).valueOf()
|
112 | },
|
113 |
|
114 | 'any, Array': function (x, y) {
|
115 |
|
116 | return algorithm14(matrix(y), x, mod, true).valueOf()
|
117 | }
|
118 | })
|
119 |
|
120 | mod.toTex = {
|
121 | 2: `\\left(\${args[0]}${latex.operators['mod']}\${args[1]}\\right)`
|
122 | }
|
123 |
|
124 | return mod
|
125 |
|
126 | |
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 | function _mod (x, y) {
|
134 | if (y > 0) {
|
135 |
|
136 |
|
137 |
|
138 | return x - y * Math.floor(x / y)
|
139 | } else if (y === 0) {
|
140 | return x
|
141 | } else {
|
142 |
|
143 | throw new Error('Cannot calculate mod for a negative divisor')
|
144 | }
|
145 | }
|
146 | }
|
147 |
|
148 | exports.name = 'mod'
|
149 | exports.factory = factory
|