pythonintegerring.py 2.9 KB

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