gmpyintegerring.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. """Implementation of :class:`GMPYIntegerRing` class. """
  2. from sympy.polys.domains.groundtypes import (
  3. GMPYInteger, SymPyInteger,
  4. factorial as gmpy_factorial,
  5. gmpy_gcdex, gmpy_gcd, gmpy_lcm, sqrt as gmpy_sqrt,
  6. )
  7. from sympy.polys.domains.integerring import IntegerRing
  8. from sympy.polys.polyerrors import CoercionFailed
  9. from sympy.utilities import public
  10. @public
  11. class GMPYIntegerRing(IntegerRing):
  12. """Integer ring based on GMPY's ``mpz`` type.
  13. This will be the implementation of :ref:`ZZ` if ``gmpy`` or ``gmpy2`` is
  14. installed. Elements will be of type ``gmpy.mpz``.
  15. """
  16. dtype = GMPYInteger
  17. zero = dtype(0)
  18. one = dtype(1)
  19. tp = type(one)
  20. alias = 'ZZ_gmpy'
  21. def __init__(self):
  22. """Allow instantiation of this domain. """
  23. def to_sympy(self, a):
  24. """Convert ``a`` to a SymPy object. """
  25. return SymPyInteger(int(a))
  26. def from_sympy(self, a):
  27. """Convert SymPy's Integer to ``dtype``. """
  28. if a.is_Integer:
  29. return GMPYInteger(a.p)
  30. elif a.is_Float and int(a) == a:
  31. return GMPYInteger(int(a))
  32. else:
  33. raise CoercionFailed("expected an integer, got %s" % a)
  34. def from_FF_python(K1, a, K0):
  35. """Convert ``ModularInteger(int)`` to GMPY's ``mpz``. """
  36. return GMPYInteger(a.to_int())
  37. def from_ZZ_python(K1, a, K0):
  38. """Convert Python's ``int`` to GMPY's ``mpz``. """
  39. return GMPYInteger(a)
  40. def from_QQ(K1, a, K0):
  41. """Convert Python's ``Fraction`` to GMPY's ``mpz``. """
  42. if a.denominator == 1:
  43. return GMPYInteger(a.numerator)
  44. def from_QQ_python(K1, a, K0):
  45. """Convert Python's ``Fraction`` to GMPY's ``mpz``. """
  46. if a.denominator == 1:
  47. return GMPYInteger(a.numerator)
  48. def from_FF_gmpy(K1, a, K0):
  49. """Convert ``ModularInteger(mpz)`` to GMPY's ``mpz``. """
  50. return a.to_int()
  51. def from_ZZ_gmpy(K1, a, K0):
  52. """Convert GMPY's ``mpz`` to GMPY's ``mpz``. """
  53. return a
  54. def from_QQ_gmpy(K1, a, K0):
  55. """Convert GMPY ``mpq`` to GMPY's ``mpz``. """
  56. if a.denominator == 1:
  57. return a.numerator
  58. def from_RealField(K1, a, K0):
  59. """Convert mpmath's ``mpf`` to GMPY's ``mpz``. """
  60. p, q = K0.to_rational(a)
  61. if q == 1:
  62. return GMPYInteger(p)
  63. def from_GaussianIntegerRing(K1, a, K0):
  64. if a.y == 0:
  65. return a.x
  66. def gcdex(self, a, b):
  67. """Compute extended GCD of ``a`` and ``b``. """
  68. h, s, t = gmpy_gcdex(a, b)
  69. return s, t, h
  70. def gcd(self, a, b):
  71. """Compute GCD of ``a`` and ``b``. """
  72. return gmpy_gcd(a, b)
  73. def lcm(self, a, b):
  74. """Compute LCM of ``a`` and ``b``. """
  75. return gmpy_lcm(a, b)
  76. def sqrt(self, a):
  77. """Compute square root of ``a``. """
  78. return gmpy_sqrt(a)
  79. def factorial(self, a):
  80. """Compute factorial of ``a``. """
  81. return gmpy_factorial(a)