# Source code for diofant.domains.field

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

from .ring import Ring

__all__ = 'Field',

[docs]class Field(Ring):
"""Represents a field domain. """

is_Field = True

@property
def ring(self):
"""Returns a ring associated with self. """
raise AttributeError('there is no ring associated with %s' % self)

@property
def field(self):
"""Returns a field associated with self. """
return self

[docs]    def exquo(self, a, b):
"""Exact quotient of a and b, implies __truediv__.  """
return a / b

[docs]    def quo(self, a, b):
"""Quotient of a and b, implies __truediv__. """
return a / b

[docs]    def rem(self, a, b):
"""Remainder of a and b, implies nothing.  """
return self.zero

[docs]    def div(self, a, b):
"""Division of a and b, implies __truediv__. """
return a / b, self.zero

[docs]    def gcd(self, a, b):
"""
Returns GCD of a and b.

This definition of GCD over fields allows to clear denominators
in primitive().

>>> QQ.gcd(QQ(2, 3), QQ(4, 9))
2/9
>>> gcd(Rational(2, 3), Rational(4, 9))
2/9
>>> primitive(2*x/3 + Rational(4, 9))
(2/9, 3*x + 2)
"""
try:
ring = self.ring
except AttributeError:
return self.one

p = ring.gcd(a.numerator, b.numerator)
q = ring.lcm(a.denominator, b.denominator)

return self.convert(p, ring)/q

[docs]    def lcm(self, a, b):
"""
Returns LCM of a and b.

>>> QQ.lcm(QQ(2, 3), QQ(4, 9))
4/3
>>> lcm(Rational(2, 3), Rational(4, 9))
4/3
"""

try:
ring = self.ring
except AttributeError:
return a*b

p = ring.lcm(a.numerator, b.numerator)
q = ring.gcd(a.denominator, b.denominator)

return self.convert(p, ring)/q