Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

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

 

from __future__ import print_function, division 

 

from sympy.polys.domains.ring import Ring 

from sympy.polys.polyerrors import NotReversible, DomainError 

from sympy.utilities import public 

 

@public 

class Field(Ring): 

"""Represents a field domain. """ 

 

has_Field = True 

 

def get_ring(self): 

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

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

 

def get_field(self): 

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

return self 

 

def exquo(self, a, b): 

"""Exact quotient of ``a`` and ``b``, implies ``__div__``. """ 

return a / b 

 

def quo(self, a, b): 

"""Quotient of ``a`` and ``b``, implies ``__div__``. """ 

return a / b 

 

def rem(self, a, b): 

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

return self.zero 

 

def div(self, a, b): 

"""Division of ``a`` and ``b``, implies ``__div__``. """ 

return a / b, self.zero 

 

def gcd(self, a, b): 

""" 

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

 

This definition of GCD over fields allows to clear denominators 

in `primitive()`. 

 

>>> from sympy.polys.domains import QQ 

>>> from sympy import S, gcd, primitive 

>>> from sympy.abc import x 

 

>>> QQ.gcd(QQ(2, 3), QQ(4, 9)) 

2/9 

>>> gcd(S(2)/3, S(4)/9) 

2/9 

>>> primitive(2*x/3 + S(4)/9) 

(2/9, 3*x + 2) 

 

""" 

try: 

ring = self.get_ring() 

except DomainError: 

return self.one 

 

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

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

 

return self.convert(p, ring)/q 

 

def lcm(self, a, b): 

""" 

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

 

>>> from sympy.polys.domains import QQ 

>>> from sympy import S, lcm 

 

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

4/3 

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

4/3 

 

""" 

 

try: 

ring = self.get_ring() 

except DomainError: 

return a*b 

 

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

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

 

return self.convert(p, ring)/q 

 

def revert(self, a): 

"""Returns ``a**(-1)`` if possible. """ 

if a: 

return 1/a 

else: 

raise NotReversible('zero is not reversible')