pythonintegerring.py 2.9 KB

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