Debian Codegraph @

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

raw
1 --- @class EuclideanDomain
2 --- Interface for an element of a euclidean domain.
3 EuclideanDomain = {}
4 local __EuclideanDomain = {}
5
6 ----------------------
7 -- Required methods --
8 ----------------------
9
10 --- @param b EuclideanDomain
11 --- @return EuclideanDomain, EuclideanDomain
12 function EuclideanDomain:divremainder(b)
13 error("Called unimplemented method : divremainder()")
14 end
15
16 ----------------------------
17 -- Instance functionality --
18 ----------------------------
19
20 --- @return boolean
21 function EuclideanDomain:iscommutative()
22 return true
23 end
24
25 --------------------------
26 -- Instance metamethods --
27 --------------------------
28
29 __EuclideanOperations = Copy(__RingOperations)
30
31 -- Division with remainder
32 -- Unfortunately, this can only return 1 result, so it returns the quotient - for the remainder use a % b, or a:divremainder(b)
33 __EuclideanOperations.__idiv = function(a, b)
34 if(b == b:zero()) then
35 error("Cannot divide by zero.")
36 end
37 local aring, bring = a:getring(), b:getring()
38 local oring = Ring.resultantring(aring, bring)
39 if not oring then
40 error("Attempted to divide two elements of incompatable rings")
41 end
42 return a:inring(oring):divremainder(b:inring(oring))
43 end
44
45 __EuclideanOperations.__mod = function(a, b)
46 if(b == b:zero()) then
47 error("Cannot divide by zero.")
48 end
49 local aring, bring = a:getring(), b:getring()
50 local oring = Ring.resultantring(aring, bring)
51 if not oring then
52 error("Attempted to divide two elements of incompatable rings")
53 end
54 local _,q = a:inring(oring):divremainder(b:inring(oring))
55 return q
56 end
57
58 -----------------
59 -- Inheritance --
60 -----------------
61
62 __EuclideanDomain.__index = Ring
63 EuclideanDomain = setmetatable(EuclideanDomain, __EuclideanDomain)