Coverage for /usr/lib/python3/dist-packages/sympy/polys/domains/pythonintegerring.py: 52%

50 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-14 15:55 +0200

1"""Implementation of :class:`PythonIntegerRing` class. """ 

2 

3 

4from sympy.polys.domains.groundtypes import ( 

5 PythonInteger, SymPyInteger, sqrt as python_sqrt, 

6 factorial as python_factorial, python_gcdex, python_gcd, python_lcm, 

7) 

8from sympy.polys.domains.integerring import IntegerRing 

9from sympy.polys.polyerrors import CoercionFailed 

10from sympy.utilities import public 

11 

12@public 

13class PythonIntegerRing(IntegerRing): 

14 """Integer ring based on Python's ``int`` type. 

15 

16 This will be used as :ref:`ZZ` if ``gmpy`` and ``gmpy2`` are not 

17 installed. Elements are instances of the standard Python ``int`` type. 

18 """ 

19 

20 dtype = PythonInteger 

21 zero = dtype(0) 

22 one = dtype(1) 

23 alias = 'ZZ_python' 

24 

25 def __init__(self): 

26 """Allow instantiation of this domain. """ 

27 

28 def to_sympy(self, a): 

29 """Convert ``a`` to a SymPy object. """ 

30 return SymPyInteger(a) 

31 

32 def from_sympy(self, a): 

33 """Convert SymPy's Integer to ``dtype``. """ 

34 if a.is_Integer: 

35 return PythonInteger(a.p) 

36 elif a.is_Float and int(a) == a: 

37 return PythonInteger(int(a)) 

38 else: 

39 raise CoercionFailed("expected an integer, got %s" % a) 

40 

41 def from_FF_python(K1, a, K0): 

42 """Convert ``ModularInteger(int)`` to Python's ``int``. """ 

43 return a.to_int() 

44 

45 def from_ZZ_python(K1, a, K0): 

46 """Convert Python's ``int`` to Python's ``int``. """ 

47 return a 

48 

49 def from_QQ(K1, a, K0): 

50 """Convert Python's ``Fraction`` to Python's ``int``. """ 

51 if a.denominator == 1: 

52 return a.numerator 

53 

54 def from_QQ_python(K1, a, K0): 

55 """Convert Python's ``Fraction`` to Python's ``int``. """ 

56 if a.denominator == 1: 

57 return a.numerator 

58 

59 def from_FF_gmpy(K1, a, K0): 

60 """Convert ``ModularInteger(mpz)`` to Python's ``int``. """ 

61 return PythonInteger(a.to_int()) 

62 

63 def from_ZZ_gmpy(K1, a, K0): 

64 """Convert GMPY's ``mpz`` to Python's ``int``. """ 

65 return PythonInteger(a) 

66 

67 def from_QQ_gmpy(K1, a, K0): 

68 """Convert GMPY's ``mpq`` to Python's ``int``. """ 

69 if a.denom() == 1: 

70 return PythonInteger(a.numer()) 

71 

72 def from_RealField(K1, a, K0): 

73 """Convert mpmath's ``mpf`` to Python's ``int``. """ 

74 p, q = K0.to_rational(a) 

75 

76 if q == 1: 

77 return PythonInteger(p) 

78 

79 def gcdex(self, a, b): 

80 """Compute extended GCD of ``a`` and ``b``. """ 

81 return python_gcdex(a, b) 

82 

83 def gcd(self, a, b): 

84 """Compute GCD of ``a`` and ``b``. """ 

85 return python_gcd(a, b) 

86 

87 def lcm(self, a, b): 

88 """Compute LCM of ``a`` and ``b``. """ 

89 return python_lcm(a, b) 

90 

91 def sqrt(self, a): 

92 """Compute square root of ``a``. """ 

93 return python_sqrt(a) 

94 

95 def factorial(self, a): 

96 """Compute factorial of ``a``. """ 

97 return python_factorial(a)