field.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. """Implementation of :class:`Field` class. """
  2. from sympy.polys.domains.ring import Ring
  3. from sympy.polys.polyerrors import NotReversible, DomainError
  4. from sympy.utilities import public
  5. @public
  6. class Field(Ring):
  7. """Represents a field domain. """
  8. is_Field = True
  9. is_PID = True
  10. def get_ring(self):
  11. """Returns a ring associated with ``self``. """
  12. raise DomainError('there is no ring associated with %s' % self)
  13. def get_field(self):
  14. """Returns a field associated with ``self``. """
  15. return self
  16. def exquo(self, a, b):
  17. """Exact quotient of ``a`` and ``b``, implies ``__truediv__``. """
  18. return a / b
  19. def quo(self, a, b):
  20. """Quotient of ``a`` and ``b``, implies ``__truediv__``. """
  21. return a / b
  22. def rem(self, a, b):
  23. """Remainder of ``a`` and ``b``, implies nothing. """
  24. return self.zero
  25. def div(self, a, b):
  26. """Division of ``a`` and ``b``, implies ``__truediv__``. """
  27. return a / b, self.zero
  28. def gcd(self, a, b):
  29. """
  30. Returns GCD of ``a`` and ``b``.
  31. This definition of GCD over fields allows to clear denominators
  32. in `primitive()`.
  33. Examples
  34. ========
  35. >>> from sympy.polys.domains import QQ
  36. >>> from sympy import S, gcd, primitive
  37. >>> from sympy.abc import x
  38. >>> QQ.gcd(QQ(2, 3), QQ(4, 9))
  39. 2/9
  40. >>> gcd(S(2)/3, S(4)/9)
  41. 2/9
  42. >>> primitive(2*x/3 + S(4)/9)
  43. (2/9, 3*x + 2)
  44. """
  45. try:
  46. ring = self.get_ring()
  47. except DomainError:
  48. return self.one
  49. p = ring.gcd(self.numer(a), self.numer(b))
  50. q = ring.lcm(self.denom(a), self.denom(b))
  51. return self.convert(p, ring)/q
  52. def gcdex(self, a, b):
  53. """
  54. Returns x, y, g such that a * x + b * y == g == gcd(a, b)
  55. """
  56. d = self.gcd(a, b)
  57. if a == self.zero:
  58. if b == self.zero:
  59. return self.zero, self.one, self.zero
  60. else:
  61. return self.zero, d/b, d
  62. else:
  63. return d/a, self.zero, d
  64. def lcm(self, a, b):
  65. """
  66. Returns LCM of ``a`` and ``b``.
  67. >>> from sympy.polys.domains import QQ
  68. >>> from sympy import S, lcm
  69. >>> QQ.lcm(QQ(2, 3), QQ(4, 9))
  70. 4/3
  71. >>> lcm(S(2)/3, S(4)/9)
  72. 4/3
  73. """
  74. try:
  75. ring = self.get_ring()
  76. except DomainError:
  77. return a*b
  78. p = ring.lcm(self.numer(a), self.numer(b))
  79. q = ring.gcd(self.denom(a), self.denom(b))
  80. return self.convert(p, ring)/q
  81. def revert(self, a):
  82. """Returns ``a**(-1)`` if possible. """
  83. if a:
  84. return 1/a
  85. else:
  86. raise NotReversible('zero is not reversible')
  87. def is_unit(self, a):
  88. """Return true if ``a`` is a invertible"""
  89. return bool(a)