polynomialring.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. """Implementation of :class:`PolynomialRing` class. """
  2. from sympy.polys.domains.ring import Ring
  3. from sympy.polys.domains.compositedomain import CompositeDomain
  4. from sympy.polys.polyerrors import CoercionFailed, GeneratorsError
  5. from sympy.utilities import public
  6. @public
  7. class PolynomialRing(Ring, CompositeDomain):
  8. """A class for representing multivariate polynomial rings. """
  9. is_PolynomialRing = is_Poly = True
  10. has_assoc_Ring = True
  11. has_assoc_Field = True
  12. def __init__(self, domain_or_ring, symbols=None, order=None):
  13. from sympy.polys.rings import PolyRing
  14. if isinstance(domain_or_ring, PolyRing) and symbols is None and order is None:
  15. ring = domain_or_ring
  16. else:
  17. ring = PolyRing(symbols, domain_or_ring, order)
  18. self.ring = ring
  19. self.dtype = ring.dtype
  20. self.gens = ring.gens
  21. self.ngens = ring.ngens
  22. self.symbols = ring.symbols
  23. self.domain = ring.domain
  24. if symbols:
  25. if ring.domain.is_Field and ring.domain.is_Exact and len(symbols)==1:
  26. self.is_PID = True
  27. # TODO: remove this
  28. self.dom = self.domain
  29. def new(self, element):
  30. return self.ring.ring_new(element)
  31. @property
  32. def zero(self):
  33. return self.ring.zero
  34. @property
  35. def one(self):
  36. return self.ring.one
  37. @property
  38. def order(self):
  39. return self.ring.order
  40. def __str__(self):
  41. return str(self.domain) + '[' + ','.join(map(str, self.symbols)) + ']'
  42. def __hash__(self):
  43. return hash((self.__class__.__name__, self.dtype.ring, self.domain, self.symbols))
  44. def __eq__(self, other):
  45. """Returns `True` if two domains are equivalent. """
  46. return isinstance(other, PolynomialRing) and \
  47. (self.dtype.ring, self.domain, self.symbols) == \
  48. (other.dtype.ring, other.domain, other.symbols)
  49. def is_unit(self, a):
  50. """Returns ``True`` if ``a`` is a unit of ``self``"""
  51. if not a.is_ground:
  52. return False
  53. K = self.domain
  54. return K.is_unit(K.convert_from(a, self))
  55. def canonical_unit(self, a):
  56. u = self.domain.canonical_unit(a.LC)
  57. return self.ring.ground_new(u)
  58. def to_sympy(self, a):
  59. """Convert `a` to a SymPy object. """
  60. return a.as_expr()
  61. def from_sympy(self, a):
  62. """Convert SymPy's expression to `dtype`. """
  63. return self.ring.from_expr(a)
  64. def from_ZZ(K1, a, K0):
  65. """Convert a Python `int` object to `dtype`. """
  66. return K1(K1.domain.convert(a, K0))
  67. def from_ZZ_python(K1, a, K0):
  68. """Convert a Python `int` object to `dtype`. """
  69. return K1(K1.domain.convert(a, K0))
  70. def from_QQ(K1, a, K0):
  71. """Convert a Python `Fraction` object to `dtype`. """
  72. return K1(K1.domain.convert(a, K0))
  73. def from_QQ_python(K1, a, K0):
  74. """Convert a Python `Fraction` object to `dtype`. """
  75. return K1(K1.domain.convert(a, K0))
  76. def from_ZZ_gmpy(K1, a, K0):
  77. """Convert a GMPY `mpz` object to `dtype`. """
  78. return K1(K1.domain.convert(a, K0))
  79. def from_QQ_gmpy(K1, a, K0):
  80. """Convert a GMPY `mpq` object to `dtype`. """
  81. return K1(K1.domain.convert(a, K0))
  82. def from_GaussianIntegerRing(K1, a, K0):
  83. """Convert a `GaussianInteger` object to `dtype`. """
  84. return K1(K1.domain.convert(a, K0))
  85. def from_GaussianRationalField(K1, a, K0):
  86. """Convert a `GaussianRational` object to `dtype`. """
  87. return K1(K1.domain.convert(a, K0))
  88. def from_RealField(K1, a, K0):
  89. """Convert a mpmath `mpf` object to `dtype`. """
  90. return K1(K1.domain.convert(a, K0))
  91. def from_ComplexField(K1, a, K0):
  92. """Convert a mpmath `mpf` object to `dtype`. """
  93. return K1(K1.domain.convert(a, K0))
  94. def from_AlgebraicField(K1, a, K0):
  95. """Convert an algebraic number to ``dtype``. """
  96. if K1.domain != K0:
  97. a = K1.domain.convert_from(a, K0)
  98. if a is not None:
  99. return K1.new(a)
  100. def from_PolynomialRing(K1, a, K0):
  101. """Convert a polynomial to ``dtype``. """
  102. try:
  103. return a.set_ring(K1.ring)
  104. except (CoercionFailed, GeneratorsError):
  105. return None
  106. def from_FractionField(K1, a, K0):
  107. """Convert a rational function to ``dtype``. """
  108. if K1.domain == K0:
  109. return K1.ring.from_list([a])
  110. q, r = K0.numer(a).div(K0.denom(a))
  111. if r.is_zero:
  112. return K1.from_PolynomialRing(q, K0.field.ring.to_domain())
  113. else:
  114. return None
  115. def from_GlobalPolynomialRing(K1, a, K0):
  116. """Convert from old poly ring to ``dtype``. """
  117. if K1.symbols == K0.gens:
  118. ad = a.to_dict()
  119. if K1.domain != K0.domain:
  120. ad = {m: K1.domain.convert(c) for m, c in ad.items()}
  121. return K1(ad)
  122. elif a.is_ground and K0.domain == K1:
  123. return K1.convert_from(a.to_list()[0], K0.domain)
  124. def get_field(self):
  125. """Returns a field associated with `self`. """
  126. return self.ring.to_field().to_domain()
  127. def is_positive(self, a):
  128. """Returns True if `LC(a)` is positive. """
  129. return self.domain.is_positive(a.LC)
  130. def is_negative(self, a):
  131. """Returns True if `LC(a)` is negative. """
  132. return self.domain.is_negative(a.LC)
  133. def is_nonpositive(self, a):
  134. """Returns True if `LC(a)` is non-positive. """
  135. return self.domain.is_nonpositive(a.LC)
  136. def is_nonnegative(self, a):
  137. """Returns True if `LC(a)` is non-negative. """
  138. return self.domain.is_nonnegative(a.LC)
  139. def gcdex(self, a, b):
  140. """Extended GCD of `a` and `b`. """
  141. return a.gcdex(b)
  142. def gcd(self, a, b):
  143. """Returns GCD of `a` and `b`. """
  144. return a.gcd(b)
  145. def lcm(self, a, b):
  146. """Returns LCM of `a` and `b`. """
  147. return a.lcm(b)
  148. def factorial(self, a):
  149. """Returns factorial of `a`. """
  150. return self.dtype(self.domain.factorial(a))