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

1"""Implementation of :class:`Field` class. """ 

2 

3 

4from sympy.polys.domains.ring import Ring 

5from sympy.polys.polyerrors import NotReversible, DomainError 

6from sympy.utilities import public 

7 

8@public 

9class Field(Ring): 

10 """Represents a field domain. """ 

11 

12 is_Field = True 

13 is_PID = True 

14 

15 def get_ring(self): 

16 """Returns a ring associated with ``self``. """ 

17 raise DomainError('there is no ring associated with %s' % self) 

18 

19 def get_field(self): 

20 """Returns a field associated with ``self``. """ 

21 return self 

22 

23 def exquo(self, a, b): 

24 """Exact quotient of ``a`` and ``b``, implies ``__truediv__``. """ 

25 return a / b 

26 

27 def quo(self, a, b): 

28 """Quotient of ``a`` and ``b``, implies ``__truediv__``. """ 

29 return a / b 

30 

31 def rem(self, a, b): 

32 """Remainder of ``a`` and ``b``, implies nothing. """ 

33 return self.zero 

34 

35 def div(self, a, b): 

36 """Division of ``a`` and ``b``, implies ``__truediv__``. """ 

37 return a / b, self.zero 

38 

39 def gcd(self, a, b): 

40 """ 

41 Returns GCD of ``a`` and ``b``. 

42 

43 This definition of GCD over fields allows to clear denominators 

44 in `primitive()`. 

45 

46 Examples 

47 ======== 

48 

49 >>> from sympy.polys.domains import QQ 

50 >>> from sympy import S, gcd, primitive 

51 >>> from sympy.abc import x 

52 

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) 

59 

60 """ 

61 try: 

62 ring = self.get_ring() 

63 except DomainError: 

64 return self.one 

65 

66 p = ring.gcd(self.numer(a), self.numer(b)) 

67 q = ring.lcm(self.denom(a), self.denom(b)) 

68 

69 return self.convert(p, ring)/q 

70 

71 def lcm(self, a, b): 

72 """ 

73 Returns LCM of ``a`` and ``b``. 

74 

75 >>> from sympy.polys.domains import QQ 

76 >>> from sympy import S, lcm 

77 

78 >>> QQ.lcm(QQ(2, 3), QQ(4, 9)) 

79 4/3 

80 >>> lcm(S(2)/3, S(4)/9) 

81 4/3 

82 

83 """ 

84 

85 try: 

86 ring = self.get_ring() 

87 except DomainError: 

88 return a*b 

89 

90 p = ring.lcm(self.numer(a), self.numer(b)) 

91 q = ring.gcd(self.denom(a), self.denom(b)) 

92 

93 return self.convert(p, ring)/q 

94 

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') 

101 

102 def is_unit(self, a): 

103 """Return true if ``a`` is a invertible""" 

104 return bool(a)