Debian Codegraph @

texmf-dist/tex/lualatex/luacas/algebra/luacas-ring.lua

raw
1 --- @class Ring
2 --- Interface for an element of a ring with unity.
3 Ring = {}
4 local __Ring = {}
5
6 --------------------------
7 -- Static functionality --
8 --------------------------
9
10 --- Determines which ring the output of a binary operation with inputs in ring1 and ring2 should be, if such a ring exists.
11 --- If one of the rings is a subring of another ring, the result should be one of the two rings.
12 --- @param ring1 RingIdentifier
13 --- @param ring2 RingIdentifier
14 --- @return RingIdentifier
15 function Ring.resultantring(ring1, ring2)
16 if ring1 == ring2 then
17 return ring1
18 end
19
20 if ((ring1 == PolynomialRing.getring() and ring2 == Rational.getring()) or
21 (ring2 == PolynomialRing.getring() and ring1 == Rational.getring()))
22 and ring1.symbol == ring2.symbol then
23 return Rational.makering(ring1.symbol, Ring.resultantring(ring1.child, ring2.child))
24 end
25
26 if ring1 == PolynomialRing.getring() or ring2 == PolynomialRing.getring() then
27 if ring1 == ring2.child then
28 return ring2
29 end
30 if ring2 == ring1.child then
31 return ring1
32 end
33
34 if ring1 == PolynomialRing.getring() and ring2 == PolynomialRing.getring() and ring1.symbol == ring2.symbol then
35 return PolynomialRing.makering(ring1.symbol, Ring.resultantring(ring1.child, ring2.child))
36 end
37
38 -- If none of the above conditions are satisfied, recusion is a pain, so we just strip all of the variables off of both rings.
39 -- TODO: Make this properly recursive, or just use a multivariable polynomial ring class
40 local symbols = {}
41 while ring1 == PolynomialRing.getring() do
42 symbols[#symbols+1] = ring1.symbol
43 ring1 = ring1.child
44 end
45 while ring2 == PolynomialRing.getring() do
46 if not Contains(symbols, ring2.symbol) then
47 symbols[#symbols+1] = ring2.symbol
48 end
49 ring2 = ring2.child
50 end
51 local ring = Ring.resultantring(ring1, ring2)
52
53 if ring == Rational.getring() and Contains(symbols, ring.symbol) then
54 symbols = Remove(symbols, ring.symbol)
55 end
56 for i = #symbols, 1, -1 do
57 ring = PolynomialRing.makering(symbols[i], ring)
58 end
59 return ring
60 end
61
62 if ring1 == Integer.getring() then
63 if ring2 == Integer.getring() then
64 return ring2
65 end
66
67 if ring2 == Rational.getring() then
68 return ring2
69 end
70
71 if ring2 == IntegerModN.getring() then
72 return ring2
73 end
74 end
75
76 if ring1 == Rational.getring() then
77 if ring2 == Integer.getring() then
78 return ring1
79 end
80
81 if ring2 == Rational.getring() then
82 if not ring1.symbol then
83 return Rational.makering(ring2.symbol, Ring.resultantring(ring1, ring2.child))
84 end
85 if not ring2.symbol then
86 return Rational.makering(ring1.symbol, Ring.resultantring(ring1.child, ring2))
87 end
88 if ring1.symbol and ring2.symbol and ring1.symbol == ring2.symbol then
89 return Rational.makering(ring1.symbol, Ring.resultantring(ring1.child, ring2.child))
90 end
91 return ring2
92 end
93
94 if ring2 == IntegerModN.getring() then
95 return nil
96 end
97 end
98
99 if ring1 == IntegerModN.getring() then
100 if ring2 == Integer.getring() then
101 return ring1
102 end
103
104 if ring2 == Rational.getring() then
105 return nil
106 end
107
108 if ring2 == IntegerModN.getring() then
109 return IntegerModN.makering(Integer.gcd(ring1.modulus, ring2.modulus))
110 end
111 end
112
113 return nil
114 end
115
116 --- Returns a particular instantiation of a ring.
117 --- Does the same thing as getring() if there is only one possible ring for a class, i.e., the integers and rationals.
118 --- @return RingIdentifier
119 function Ring.makering()
120 error("Called unimplemented method : makering()")
121 end
122
123 ----------------------
124 -- Required methods --
125 ----------------------
126
127 --- Returns the ring this element is part of.
128 --- @return RingIdentifier
129 function Ring:getring()
130 error("Called unimplemented method : getring()")
131 end
132
133 --- Explicitly converts this element to an element of another ring.
134 --- @param ring RingIdentifier
135 --- @return Ring
136 function Ring:inring(ring)
137 error("Called unimplemented method : in()")
138 end
139
140 --- Returns whether the ring is commutative.
141 --- @return boolean
142 function Ring:iscommutative()
143 error("Called unimplemented method : iscommutative()")
144 end
145
146 --- @return Ring
147 function Ring:add(b)
148 error("Called unimplemented method : add()")
149 end
150
151 --- @return Ring
152 function Ring:sub(b)
153 return(self:add(b:neg()))
154 end
155
156 --- @return Ring
157 function Ring:neg()
158 error("Called unimplemented method : neg()")
159 end
160
161 --- @return Ring
162 function Ring:mul(b)
163 error("Called unimplemented method : mul()")
164 end
165
166 --- Ring exponentiation by definition. Specific rings may implement more efficient methods.
167 --- @return Ring
168 function Ring:pow(n)
169 if(n < Integer.zero()) then
170 error("Execution error: Negative exponentiation is undefined over general rings")
171 end
172 local k = Integer.zero()
173 local b = self:one()
174 while k < n do
175 b = b * self
176 k = k + Integer.one()
177 end
178 return b
179 end
180
181 --- @return boolean
182 function Ring:eq(b)
183 error("Execution error: Ring does not have a total order")
184 end
185
186 --- @return boolean
187 function Ring:lt(b)
188 error("Execution error: Ring does not have a total order")
189 end
190
191 --- @return boolean
192 function Ring:le(b)
193 error("Execution error: Ring does not have a total order")
194 end
195
196 --- The additive identitity of the ring.
197 --- @return Ring
198 function Ring:zero()
199 error("Called unimplemented method : zero()")
200 end
201
202 --- The multiplicative identitity of the ring.
203 --- @return Ring
204 function Ring:one()
205 error("Called unimplemented method : one()")
206 end
207
208 --------------------------
209 -- Instance metamethods --
210 --------------------------
211 __RingOperations = {}
212
213 -- Each of these methods just handles coverting each element in the ring to an instance of the proper ring, if possible,
214 -- then passing the arguments to the function in a specific ring.
215
216 __RingOperations.__unm = function(a)
217 return a:neg()
218 end
219
220 __RingOperations.__add = function(a, b)
221 if not b.getring then
222 return BinaryOperation.ADDEXP({a, b})
223 end
224
225 local aring, bring = a:getring(), b:getring()
226 local oring = Ring.resultantring(aring, bring)
227 if not oring then
228 error("Attempted to add two elements of incompatable rings")
229 end
230 return a:inring(oring):add(b:inring(oring))
231 end
232
233 __RingOperations.__sub = function(a, b)
234 if not b.getring then
235 return BinaryOperation.SUBEXP({a, b})
236 end
237
238 local aring, bring = a:getring(), b:getring()
239 local oring = Ring.resultantring(aring, bring)
240 if not oring then
241 error("Attempted to subtract two elements of incompatable rings")
242 end
243 return a:inring(oring):sub(b:inring(oring))
244 end
245
246 -- Allows for multiplication by writing two expressions next to each other.
247 __RingOperations.__call = function (a, b)
248 return a * b
249 end
250
251 __RingOperations.__mul = function(a, b)
252 if not b.getring then
253 return BinaryOperation.MULEXP({a, b})
254 end
255
256 local aring, bring = a:getring(), b:getring()
257 local oring = Ring.resultantring(aring, bring)
258 if not oring then
259 error("Attempted to muliply two elements of incompatable rings")
260 end
261 return a:inring(oring):mul(b:inring(oring))
262 end
263
264 __RingOperations.__pow = function(a, n)
265 if (not n.getring) or (n.getring and n:getring().ring ~= Integer) then
266 return BinaryOperation.POWEXP({a, n})
267 end
268
269 -- if a == a:zero() and n == Integer.zero() then
270 -- error("Cannot raise 0 to the power of 0")
271 -- end
272
273 return a:pow(n)
274 end
275
276 -- Comparison operations assume, of course, that the ring operation is equipped with a total order
277 -- All elements of all rings need these metamethods, since in Lua comparisons on tables only fire if both objects have the table
278 __RingOperations.__eq = function(a, b)
279 -- This shouldn't be needed, since __eq should only fire if both metamethods have the same function, but for some reason Lua always runs this anyway
280 if not a.getring or not b.getring then
281 return false
282 end
283 local aring, bring = a:getring(), b:getring()
284 if aring == bring then
285 return a:eq(b)
286 end
287 local oring = Ring.resultantring(aring, bring)
288 if not oring then
289 error("Attempted to compare two elements of incompatable rings")
290 end
291 return a:inring(oring):eq(b:inring(oring))
292 end
293
294 __RingOperations.__lt = function(a, b)
295 local aring, bring = a:getring(), b:getring()
296 if aring == bring then
297 return a:lt(b)
298 end
299 local oring = Ring.resultantring(aring, bring)
300 if not oring then
301 error("Attempted to compare two elements of incompatable rings")
302 end
303 return a:inring(oring):lt(b:inring(oring))
304 end
305
306 __RingOperations.__le = function(a, b)
307 local aring, bring = a:getring(), b:getring()
308 if aring == bring then
309 return a:le(b)
310 end
311 local oring = Ring.resultantring(aring, bring)
312 if not oring then
313 error("Attempted to compare two elements of incompatable rings")
314 end
315 return a:inring(oring):le(b:inring(oring))
316 end
317
318 -----------------
319 -- Inheritance --
320 -----------------
321
322 __Ring.__index = ConstantExpression
323 Ring = setmetatable(Ring, __Ring)
324
325 --- Used for comparing and converting between rings.
326 --- @class RingIdentifier