Coverage for /usr/lib/python3/dist-packages/sympy/polys/domains/field.py: 41%
41 statements
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-14 15:55 +0200
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-14 15:55 +0200
1"""Implementation of :class:`Field` class. """
4from sympy.polys.domains.ring import Ring
5from sympy.polys.polyerrors import NotReversible, DomainError
6from sympy.utilities import public
8@public
9class Field(Ring):
10 """Represents a field domain. """
12 is_Field = True
13 is_PID = True
15 def get_ring(self):
16 """Returns a ring associated with ``self``. """
17 raise DomainError('there is no ring associated with %s' % self)
19 def get_field(self):
20 """Returns a field associated with ``self``. """
21 return self
23 def exquo(self, a, b):
24 """Exact quotient of ``a`` and ``b``, implies ``__truediv__``. """
25 return a / b
27 def quo(self, a, b):
28 """Quotient of ``a`` and ``b``, implies ``__truediv__``. """
29 return a / b
31 def rem(self, a, b):
32 """Remainder of ``a`` and ``b``, implies nothing. """
33 return self.zero
35 def div(self, a, b):
36 """Division of ``a`` and ``b``, implies ``__truediv__``. """
37 return a / b, self.zero
39 def gcd(self, a, b):
40 """
41 Returns GCD of ``a`` and ``b``.
43 This definition of GCD over fields allows to clear denominators
44 in `primitive()`.
46 Examples
47 ========
49 >>> from sympy.polys.domains import QQ
50 >>> from sympy import S, gcd, primitive
51 >>> from sympy.abc import x
53 >>> QQ.gcd(QQ(2, 3), QQ(4, 9))
54 2/9
55 >>> gcd(S(2)/3, S(4)/9)
56 2/9
57 >>> primitive(2*x/3 + S(4)/9)
58 (2/9, 3*x + 2)
60 """
61 try:
62 ring = self.get_ring()
63 except DomainError:
64 return self.one
66 p = ring.gcd(self.numer(a), self.numer(b))
67 q = ring.lcm(self.denom(a), self.denom(b))
69 return self.convert(p, ring)/q
71 def lcm(self, a, b):
72 """
73 Returns LCM of ``a`` and ``b``.
75 >>> from sympy.polys.domains import QQ
76 >>> from sympy import S, lcm
78 >>> QQ.lcm(QQ(2, 3), QQ(4, 9))
79 4/3
80 >>> lcm(S(2)/3, S(4)/9)
81 4/3
83 """
85 try:
86 ring = self.get_ring()
87 except DomainError:
88 return a*b
90 p = ring.lcm(self.numer(a), self.numer(b))
91 q = ring.gcd(self.denom(a), self.denom(b))
93 return self.convert(p, ring)/q
95 def revert(self, a):
96 """Returns ``a**(-1)`` if possible. """
97 if a:
98 return 1/a
99 else:
100 raise NotReversible('zero is not reversible')
102 def is_unit(self, a):
103 """Return true if ``a`` is a invertible"""
104 return bool(a)