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

52 statements  

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

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

2 

3 

4from sympy.polys.domains.groundtypes import ( 

5 GMPYRational, SymPyRational, 

6 gmpy_numer, gmpy_denom, factorial as gmpy_factorial, 

7) 

8from sympy.polys.domains.rationalfield import RationalField 

9from sympy.polys.polyerrors import CoercionFailed 

10from sympy.utilities import public 

11 

12@public 

13class GMPYRationalField(RationalField): 

14 """Rational field based on GMPY's ``mpq`` type. 

15 

16 This will be the implementation of :ref:`QQ` if ``gmpy`` or ``gmpy2`` is 

17 installed. Elements will be of type ``gmpy.mpq``. 

18 """ 

19 

20 dtype = GMPYRational 

21 zero = dtype(0) 

22 one = dtype(1) 

23 tp = type(one) 

24 alias = 'QQ_gmpy' 

25 

26 def __init__(self): 

27 pass 

28 

29 def get_ring(self): 

30 """Returns ring associated with ``self``. """ 

31 from sympy.polys.domains import GMPYIntegerRing 

32 return GMPYIntegerRing() 

33 

34 def to_sympy(self, a): 

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

36 return SymPyRational(int(gmpy_numer(a)), 

37 int(gmpy_denom(a))) 

38 

39 def from_sympy(self, a): 

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

41 if a.is_Rational: 

42 return GMPYRational(a.p, a.q) 

43 elif a.is_Float: 

44 from sympy.polys.domains import RR 

45 return GMPYRational(*map(int, RR.to_rational(a))) 

46 else: 

47 raise CoercionFailed("expected ``Rational`` object, got %s" % a) 

48 

49 def from_ZZ_python(K1, a, K0): 

50 """Convert a Python ``int`` object to ``dtype``. """ 

51 return GMPYRational(a) 

52 

53 def from_QQ_python(K1, a, K0): 

54 """Convert a Python ``Fraction`` object to ``dtype``. """ 

55 return GMPYRational(a.numerator, a.denominator) 

56 

57 def from_ZZ_gmpy(K1, a, K0): 

58 """Convert a GMPY ``mpz`` object to ``dtype``. """ 

59 return GMPYRational(a) 

60 

61 def from_QQ_gmpy(K1, a, K0): 

62 """Convert a GMPY ``mpq`` object to ``dtype``. """ 

63 return a 

64 

65 def from_GaussianRationalField(K1, a, K0): 

66 """Convert a ``GaussianElement`` object to ``dtype``. """ 

67 if a.y == 0: 

68 return GMPYRational(a.x) 

69 

70 def from_RealField(K1, a, K0): 

71 """Convert a mpmath ``mpf`` object to ``dtype``. """ 

72 return GMPYRational(*map(int, K0.to_rational(a))) 

73 

74 def exquo(self, a, b): 

75 """Exact quotient of ``a`` and ``b``, implies ``__truediv__``. """ 

76 return GMPYRational(a) / GMPYRational(b) 

77 

78 def quo(self, a, b): 

79 """Quotient of ``a`` and ``b``, implies ``__truediv__``. """ 

80 return GMPYRational(a) / GMPYRational(b) 

81 

82 def rem(self, a, b): 

83 """Remainder of ``a`` and ``b``, implies nothing. """ 

84 return self.zero 

85 

86 def div(self, a, b): 

87 """Division of ``a`` and ``b``, implies ``__truediv__``. """ 

88 return GMPYRational(a) / GMPYRational(b), self.zero 

89 

90 def numer(self, a): 

91 """Returns numerator of ``a``. """ 

92 return a.numerator 

93 

94 def denom(self, a): 

95 """Returns denominator of ``a``. """ 

96 return a.denominator 

97 

98 def factorial(self, a): 

99 """Returns factorial of ``a``. """ 

100 return GMPYRational(gmpy_factorial(int(a)))