gmpyrationalfield.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. """Implementation of :class:`GMPYRationalField` class. """
  2. from sympy.polys.domains.groundtypes import (
  3. GMPYRational, SymPyRational,
  4. gmpy_numer, gmpy_denom, factorial as gmpy_factorial,
  5. )
  6. from sympy.polys.domains.rationalfield import RationalField
  7. from sympy.polys.polyerrors import CoercionFailed
  8. from sympy.utilities import public
  9. @public
  10. class GMPYRationalField(RationalField):
  11. """Rational field based on GMPY's ``mpq`` type.
  12. This will be the implementation of :ref:`QQ` if ``gmpy`` or ``gmpy2`` is
  13. installed. Elements will be of type ``gmpy.mpq``.
  14. """
  15. dtype = GMPYRational
  16. zero = dtype(0)
  17. one = dtype(1)
  18. tp = type(one)
  19. alias = 'QQ_gmpy'
  20. def __init__(self):
  21. pass
  22. def get_ring(self):
  23. """Returns ring associated with ``self``. """
  24. from sympy.polys.domains import GMPYIntegerRing
  25. return GMPYIntegerRing()
  26. def to_sympy(self, a):
  27. """Convert ``a`` to a SymPy object. """
  28. return SymPyRational(int(gmpy_numer(a)),
  29. int(gmpy_denom(a)))
  30. def from_sympy(self, a):
  31. """Convert SymPy's Integer to ``dtype``. """
  32. if a.is_Rational:
  33. return GMPYRational(a.p, a.q)
  34. elif a.is_Float:
  35. from sympy.polys.domains import RR
  36. return GMPYRational(*map(int, RR.to_rational(a)))
  37. else:
  38. raise CoercionFailed("expected ``Rational`` object, got %s" % a)
  39. def from_ZZ_python(K1, a, K0):
  40. """Convert a Python ``int`` object to ``dtype``. """
  41. return GMPYRational(a)
  42. def from_QQ_python(K1, a, K0):
  43. """Convert a Python ``Fraction`` object to ``dtype``. """
  44. return GMPYRational(a.numerator, a.denominator)
  45. def from_ZZ_gmpy(K1, a, K0):
  46. """Convert a GMPY ``mpz`` object to ``dtype``. """
  47. return GMPYRational(a)
  48. def from_QQ_gmpy(K1, a, K0):
  49. """Convert a GMPY ``mpq`` object to ``dtype``. """
  50. return a
  51. def from_GaussianRationalField(K1, a, K0):
  52. """Convert a ``GaussianElement`` object to ``dtype``. """
  53. if a.y == 0:
  54. return GMPYRational(a.x)
  55. def from_RealField(K1, a, K0):
  56. """Convert a mpmath ``mpf`` object to ``dtype``. """
  57. return GMPYRational(*map(int, K0.to_rational(a)))
  58. def exquo(self, a, b):
  59. """Exact quotient of ``a`` and ``b``, implies ``__truediv__``. """
  60. return GMPYRational(a) / GMPYRational(b)
  61. def quo(self, a, b):
  62. """Quotient of ``a`` and ``b``, implies ``__truediv__``. """
  63. return GMPYRational(a) / GMPYRational(b)
  64. def rem(self, a, b):
  65. """Remainder of ``a`` and ``b``, implies nothing. """
  66. return self.zero
  67. def div(self, a, b):
  68. """Division of ``a`` and ``b``, implies ``__truediv__``. """
  69. return GMPYRational(a) / GMPYRational(b), self.zero
  70. def numer(self, a):
  71. """Returns numerator of ``a``. """
  72. return a.numerator
  73. def denom(self, a):
  74. """Returns denominator of ``a``. """
  75. return a.denominator
  76. def factorial(self, a):
  77. """Returns factorial of ``a``. """
  78. return GMPYRational(gmpy_factorial(int(a)))