/
ring.py
72 lines (55 loc) · 1.88 KB
/
ring.py
1
2
3
4
5
6
7
8
9
10
11
12
13
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""Implementation of :class:`CommutativeRing` class."""
import abc
from ..polys.polyerrors import ExactQuotientFailed, NotInvertible
from .domain import Domain
class CommutativeRing(Domain):
"""Represents a ring domain."""
is_Ring = True
@property
def ring(self):
"""Returns a ring associated with ``self``."""
return self
def exquo(self, a, b):
"""Exact quotient of ``a`` and ``b``, implies ``__floordiv__``."""
if a % b:
raise ExactQuotientFailed(a, b, self)
else:
return a // b
def quo(self, a, b):
"""Quotient of ``a`` and ``b``, implies ``__floordiv__``."""
return a // b
def rem(self, a, b):
"""Remainder of ``a`` and ``b``, implies ``__mod__``."""
return a % b
def div(self, a, b):
"""Division of ``a`` and ``b``, implies ``__divmod__``."""
return divmod(a, b)
def invert(self, a, b):
"""Returns inversion of ``a mod b``."""
s, h = self.half_gcdex(a, b)
if h == 1:
return s % b
else:
raise NotInvertible('zero divisor')
def half_gcdex(self, a, b):
"""Half extended GCD of ``a`` and ``b``."""
s, _, h = self.gcdex(a, b)
return s, h
def cofactors(self, a, b):
"""Returns GCD and cofactors of ``a`` and ``b``."""
gcd, cfa, cfb = self.gcd(a, b), self.zero, self.zero
if gcd:
cfa = self.quo(a, gcd)
cfb = self.quo(b, gcd)
return gcd, cfa, cfb
def lcm(self, a, b):
"""Returns LCM of ``a`` and ``b``."""
return abs(a*b)//self.gcd(a, b)
@property
@abc.abstractmethod
def characteristic(self):
"""Return the characteristic of this ring."""
raise NotImplementedError
def is_normal(self, a):
"""Returns True if ``a`` is unit normal."""
return a >= 0