예제 #1
0
    def convert(self, element, base=None):
        """Convert ``element`` to ``self.dtype``. """
        if _not_a_coeff(element):
            raise CoercionFailed('%s is not in any domain' % element)

        if base is not None:
            return self.convert_from(element, base)

        if self.of_type(element):
            return element

        from sympy.polys.domains import PythonIntegerRing, GMPYIntegerRing, GMPYRationalField, RealField, ComplexField

        if isinstance(element, int):
            return self.convert_from(element, PythonIntegerRing())

        if HAS_GMPY:
            integers = GMPYIntegerRing()
            if isinstance(element, integers.tp):
                return self.convert_from(element, integers)

            rationals = GMPYRationalField()
            if isinstance(element, rationals.tp):
                return self.convert_from(element, rationals)

        if isinstance(element, float):
            parent = RealField(tol=False)
            return self.convert_from(parent(element), parent)

        if isinstance(element, complex):
            parent = ComplexField(tol=False)
            return self.convert_from(parent(element), parent)

        if isinstance(element, DomainElement):
            return self.convert_from(element, element.parent())

        # TODO: implement this in from_ methods
        if self.is_Numerical and getattr(element, 'is_ground', False):
            return self.convert(element.LC())

        if isinstance(element, Basic):
            try:
                return self.from_sympy(element)
            except (TypeError, ValueError):
                pass
        else:  # TODO: remove this branch
            if not is_sequence(element):
                try:
                    element = sympify(element)

                    if isinstance(element, Basic):
                        return self.from_sympy(element)
                except (TypeError, ValueError):
                    pass

        raise CoercionFailed("can't convert %s of type %s to %s" %
                             (element, type(element), self))
예제 #2
0
def test_polys():
    x = Symbol("x")

    ZZ = PythonIntegerRing()
    QQ = SymPyRationalField()

    for c in (Poly, Poly(x, x)):
        check(c)

    for c in (GFP, GFP([ZZ(1), ZZ(2), ZZ(3)], ZZ(7), ZZ)):
        check(c)
    for c in (DMP, DMP([ZZ(1), ZZ(2), ZZ(3)], 0, ZZ)):
        check(c)
    for c in (DMF, DMF(([ZZ(1), ZZ(2)], [ZZ(1), ZZ(3)], ZZ))):
        check(c)
    for c in (ANP, ANP([QQ(1), QQ(2)], [QQ(1), QQ(2), QQ(3)], QQ)):
        check(c)

    for c in (PythonIntegerRing, PythonIntegerRing()):
        check(c)
    for c in (SymPyIntegerRing, SymPyIntegerRing()):
        check(c)
    for c in (SymPyRationalField, SymPyRationalField()):
        check(c)

    for c in (PolynomialRing, PolynomialRing(ZZ, 'x', 'y')):
        check(c)
    for c in (FractionField, FractionField(ZZ, 'x', 'y')):
        check(c)

    for c in (ExpressionDomain, ExpressionDomain()):
        check(c)

    from sympy.polys.domains import HAS_FRACTION, HAS_GMPY

    if HAS_FRACTION:
        from sympy.polys.domains import PythonRationalField

        for c in (PythonRationalField, PythonRationalField()):
            check(c)

    if HAS_GMPY:
        from sympy.polys.domains import GMPYIntegerRing, GMPYRationalField

        for c in (GMPYIntegerRing, GMPYIntegerRing()):
            check(c)
        for c in (GMPYRationalField, GMPYRationalField()):
            check(c)

    f = x**3 + x + 3
    g = lambda x: x

    for c in (RootOf, RootOf(f, 0), RootSum, RootSum(f, g)):
        check(c)
예제 #3
0
def test_dup_ff_div_gmpy2():
    try:
        from gmpy2 import mpq
    except ImportError:
        return

    from sympy.polys.domains import GMPYRationalField
    K = GMPYRationalField()

    f = [mpq(1, 3), mpq(3, 2)]
    g = [mpq(2, 1)]
    assert dmp_ff_div(f, g, 0, K) == ([mpq(1, 6), mpq(3, 4)], [])

    f = [mpq(1, 2), mpq(1, 3), mpq(1, 4), mpq(1, 5)]
    g = [mpq(-1, 1), mpq(1, 1), mpq(-1, 1)]
    assert dmp_ff_div(f, g, 0, K) == ([mpq(-1, 2),
                                       mpq(-5, 6)], [mpq(7, 12),
                                                     mpq(-19, 30)])
예제 #4
0
def test_polys():
    x = Symbol("X")

    ZZ = PythonIntegerRing()
    QQ = PythonRationalField()

    for c in (Poly, Poly(x, x)):
        check(c)

    for c in (DMP, DMP([[ZZ(1)], [ZZ(2)], [ZZ(3)]], ZZ)):
        check(c)
    for c in (DMF, DMF(([ZZ(1), ZZ(2)], [ZZ(1), ZZ(3)]), ZZ)):
        check(c)
    for c in (ANP, ANP([QQ(1), QQ(2)], [QQ(1), QQ(2), QQ(3)], QQ)):
        check(c)

    for c in (PythonIntegerRing, PythonIntegerRing()):
        check(c)
    for c in (PythonRationalField, PythonRationalField()):
        check(c)

    for c in (PolynomialRing, PolynomialRing(ZZ, 'x', 'y')):
        check(c)
    for c in (FractionField, FractionField(ZZ, 'x', 'y')):
        check(c)

    for c in (ExpressionDomain, ExpressionDomain()):
        check(c)

    from sympy.core.compatibility import HAS_GMPY

    if HAS_GMPY:
        from sympy.polys.domains import GMPYIntegerRing, GMPYRationalField

        for c in (GMPYIntegerRing, GMPYIntegerRing()):
            check(c)
        for c in (GMPYRationalField, GMPYRationalField()):
            check(c)

    f = x**3 + x + 3
    g = exp

    for c in (RootOf, RootOf(f, 0), RootSum, RootSum(f, g)):
        check(c)