gmpyintegerring.py 3.0 KB

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