Source code for modelparameters.sympy.polys.domains.gmpyintegerring
"""Implementaton of :class:`GMPYIntegerRing` class. """
from __future__ import print_function, division
from .integerring import IntegerRing
from .groundtypes import (
    GMPYInteger, SymPyInteger,
    gmpy_factorial,
    gmpy_gcdex, gmpy_gcd, gmpy_lcm, gmpy_sqrt,
)
from ..polyerrors import CoercionFailed
from ...utilities import public
[docs]@public
class GMPYIntegerRing(IntegerRing):
    """Integer ring based on GMPY's ``mpz`` type. """
    dtype = GMPYInteger
    zero = dtype(0)
    one = dtype(1)
    tp = type(one)
    alias = 'ZZ_gmpy'
    def __init__(self):
        """Allow instantiation of this domain. """
[docs]    def to_sympy(self, a):
        """Convert ``a`` to a SymPy object. """
        return SymPyInteger(int(a)) 
[docs]    def from_sympy(self, a):
        """Convert SymPy's Integer to ``dtype``. """
        if a.is_Integer:
            return GMPYInteger(a.p)
        elif a.is_Float and int(a) == a:
            return GMPYInteger(int(a))
        else:
            raise CoercionFailed("expected an integer, got %s" % a) 
[docs]    def from_FF_python(K1, a, K0):
        """Convert ``ModularInteger(int)`` to GMPY's ``mpz``. """
        return GMPYInteger(a.to_int()) 
[docs]    def from_ZZ_python(K1, a, K0):
        """Convert Python's ``int`` to GMPY's ``mpz``. """
        return GMPYInteger(a) 
[docs]    def from_QQ_python(K1, a, K0):
        """Convert Python's ``Fraction`` to GMPY's ``mpz``. """
        if a.denominator == 1:
            return GMPYInteger(a.numerator) 
[docs]    def from_FF_gmpy(K1, a, K0):
        """Convert ``ModularInteger(mpz)`` to GMPY's ``mpz``. """
        return a.to_int() 
[docs]    def from_ZZ_gmpy(K1, a, K0):
        """Convert GMPY's ``mpz`` to GMPY's ``mpz``. """
        return a 
[docs]    def from_QQ_gmpy(K1, a, K0):
        """Convert GMPY ``mpq`` to GMPY's ``mpz``. """
        if a.denominator == 1:
            return a.numerator 
[docs]    def from_RealField(K1, a, K0):
        """Convert mpmath's ``mpf`` to GMPY's ``mpz``. """
        p, q = K0.to_rational(a)
        if q == 1:
            return GMPYInteger(p) 
[docs]    def gcdex(self, a, b):
        """Compute extended GCD of ``a`` and ``b``. """
        h, s, t = gmpy_gcdex(a, b)
        return s, t, h 
[docs]    def gcd(self, a, b):
        """Compute GCD of ``a`` and ``b``. """
        return gmpy_gcd(a, b) 
[docs]    def lcm(self, a, b):
        """Compute LCM of ``a`` and ``b``. """
        return gmpy_lcm(a, b) 
[docs]    def sqrt(self, a):
        """Compute square root of ``a``. """
        return gmpy_sqrt(a) 
[docs]    def factorial(self, a):
        """Compute factorial of ``a``. """
        return gmpy_factorial(a)