Coverage for /usr/lib/python3/dist-packages/sympy/polys/domains/gmpyintegerring.py: 51%
55 statements
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-14 15:55 +0200
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-14 15:55 +0200
1"""Implementation of :class:`GMPYIntegerRing` class. """
4from sympy.polys.domains.groundtypes import (
5 GMPYInteger, SymPyInteger,
6 factorial as gmpy_factorial,
7 gmpy_gcdex, gmpy_gcd, gmpy_lcm, sqrt as gmpy_sqrt,
8)
9from sympy.polys.domains.integerring import IntegerRing
10from sympy.polys.polyerrors import CoercionFailed
11from sympy.utilities import public
13@public
14class GMPYIntegerRing(IntegerRing):
15 """Integer ring based on GMPY's ``mpz`` type.
17 This will be the implementation of :ref:`ZZ` if ``gmpy`` or ``gmpy2`` is
18 installed. Elements will be of type ``gmpy.mpz``.
19 """
21 dtype = GMPYInteger
22 zero = dtype(0)
23 one = dtype(1)
24 tp = type(one)
25 alias = 'ZZ_gmpy'
27 def __init__(self):
28 """Allow instantiation of this domain. """
30 def to_sympy(self, a):
31 """Convert ``a`` to a SymPy object. """
32 return SymPyInteger(int(a))
34 def from_sympy(self, a):
35 """Convert SymPy's Integer to ``dtype``. """
36 if a.is_Integer:
37 return GMPYInteger(a.p)
38 elif a.is_Float and int(a) == a:
39 return GMPYInteger(int(a))
40 else:
41 raise CoercionFailed("expected an integer, got %s" % a)
43 def from_FF_python(K1, a, K0):
44 """Convert ``ModularInteger(int)`` to GMPY's ``mpz``. """
45 return GMPYInteger(a.to_int())
47 def from_ZZ_python(K1, a, K0):
48 """Convert Python's ``int`` to GMPY's ``mpz``. """
49 return GMPYInteger(a)
51 def from_QQ(K1, a, K0):
52 """Convert Python's ``Fraction`` to GMPY's ``mpz``. """
53 if a.denominator == 1:
54 return GMPYInteger(a.numerator)
56 def from_QQ_python(K1, a, K0):
57 """Convert Python's ``Fraction`` to GMPY's ``mpz``. """
58 if a.denominator == 1:
59 return GMPYInteger(a.numerator)
61 def from_FF_gmpy(K1, a, K0):
62 """Convert ``ModularInteger(mpz)`` to GMPY's ``mpz``. """
63 return a.to_int()
65 def from_ZZ_gmpy(K1, a, K0):
66 """Convert GMPY's ``mpz`` to GMPY's ``mpz``. """
67 return a
69 def from_QQ_gmpy(K1, a, K0):
70 """Convert GMPY ``mpq`` to GMPY's ``mpz``. """
71 if a.denominator == 1:
72 return a.numerator
74 def from_RealField(K1, a, K0):
75 """Convert mpmath's ``mpf`` to GMPY's ``mpz``. """
76 p, q = K0.to_rational(a)
78 if q == 1:
79 return GMPYInteger(p)
81 def from_GaussianIntegerRing(K1, a, K0):
82 if a.y == 0:
83 return a.x
85 def gcdex(self, a, b):
86 """Compute extended GCD of ``a`` and ``b``. """
87 h, s, t = gmpy_gcdex(a, b)
88 return s, t, h
90 def gcd(self, a, b):
91 """Compute GCD of ``a`` and ``b``. """
92 return gmpy_gcd(a, b)
94 def lcm(self, a, b):
95 """Compute LCM of ``a`` and ``b``. """
96 return gmpy_lcm(a, b)
98 def sqrt(self, a):
99 """Compute square root of ``a``. """
100 return gmpy_sqrt(a)
102 def factorial(self, a):
103 """Compute factorial of ``a``. """
104 return gmpy_factorial(a)