Beispiel #1
0
def test_pickling_polys_errors():
    # TODO: TypeError: __init__() takes at least 3 arguments (1 given)
    # for c in (ExactQuotientFailed, ExactQuotientFailed(x, 3*x, ZZ)):
    #    check(c)

    # TODO: TypeError: can't pickle instancemethod objects
    # for c in (OperationNotSupported, OperationNotSupported(Poly(x), Poly.gcd)):
    #    check(c)

    for c in (HeuristicGCDFailed, HeuristicGCDFailed(), HomomorphismFailed,
              HomomorphismFailed(), IsomorphismFailed, IsomorphismFailed(),
              ExtraneousFactors, ExtraneousFactors(), EvaluationFailed,
              EvaluationFailed(),
              RefinementFailed, RefinementFailed(), CoercionFailed,
              CoercionFailed(), NotInvertible, NotInvertible(), NotReversible,
              NotReversible(), NotAlgebraic, NotAlgebraic(), DomainError,
              DomainError(), PolynomialError, PolynomialError(),
              UnificationFailed, UnificationFailed(), GeneratorsError,
              GeneratorsError(), GeneratorsNeeded, GeneratorsNeeded()):
        check(c)

    # TODO: PicklingError: Can't pickle <function <lambda> at 0x38578c0>: it's not found as __main__.<lambda>
    # for c in (ComputationFailed, ComputationFailed(lambda t: t, 3, None)):
    #    check(c)

    for c in (UnivariatePolynomialError, UnivariatePolynomialError(),
              MultivariatePolynomialError, MultivariatePolynomialError()):
        check(c)

    # TODO: TypeError: __init__() takes at least 3 arguments (1 given)
    # for c in (PolificationFailed, PolificationFailed({}, x, x, False)):
    #    check(c)

    for c in (OptionError, OptionError(), FlagError, FlagError()):
        check(c)
Beispiel #2
0
 def from_diofant(self, a):
     """Convert Diofant's Integer to Diofant's ``Integer``. """
     if a.is_Integer:
         return self.dtype(self.domain.dtype(int(a)))
     elif a.is_Float and int(a) == a:
         return self.dtype(self.domain.dtype(int(a)))
     else:
         raise CoercionFailed("expected an integer, got %s" % a)
Beispiel #3
0
 def from_diofant(self, a):
     """Convert Diofant's Integer to ``dtype``. """
     if a.is_Integer:
         return PythonInteger(a.p)
     elif a.is_Float and int(a) == a:
         return PythonInteger(int(a))
     else:
         raise CoercionFailed("expected an integer, got %s" % a)
Beispiel #4
0
    def from_diofant(self, expr):
        """Convert Diofant's number to ``dtype``. """
        number = expr.evalf(n=self.dps)

        if number.is_Number:
            return self.dtype(number)
        else:
            raise CoercionFailed("expected real number, got %s" % expr)
Beispiel #5
0
    def from_diofant(self, expr):
        """Convert Diofant's number to ``dtype``. """
        number = expr.evalf(n=self.dps)
        real, imag = number.as_real_imag()

        if real.is_Number and imag.is_Number:
            return self.dtype(real, imag)
        else:
            raise CoercionFailed("expected complex number, got %s" % expr)
Beispiel #6
0
 def from_diofant(self, a):
     """Convert Diofant's Rational to `dtype`. """
     if a.is_Rational:
         return PythonRational(a.p, a.q)
     elif a.is_Float:
         from diofant.polys.domains import RR
         p, q = RR.to_rational(a)
         return PythonRational(int(p), int(q))
     else:
         raise CoercionFailed("expected `Rational` object, got %s" % a)
Beispiel #7
0
    def new(self, a):
        """Construct an element of `self` domain from `a`. """
        res = self.dtype(a, self.domain, len(self.gens) - 1, ring=self)

        # make sure res is actually in our ring
        if res.denom().terms(order=self.order)[0][0] != (0,)*len(self.gens):
            from diofant.printing.str import sstr
            raise CoercionFailed("denominator %s not allowed in %s"
                                 % (sstr(res), self))
        return res
Beispiel #8
0
    def from_diofant(self, a):
        """Convert Diofant's expression to `dtype`. """
        try:
            rep, _ = dict_from_basic(a, gens=self.gens)
        except PolynomialError:
            raise CoercionFailed("can't convert %s to type %s" % (a, self))

        for k, v in rep.items():
            rep[k] = self.domain.from_diofant(v)

        return self(rep)
Beispiel #9
0
    def convert(self, element, base=None):
        """Convert ``element`` to ``self.dtype``. """
        if base is not None:
            return self.convert_from(element, base)

        if self.of_type(element):
            return element

        from diofant.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_diofant(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_diofant(element)
                except (TypeError, ValueError):
                    pass

        raise CoercionFailed("can't convert %s of type %s to %s" %
                             (element, type(element), self))
Beispiel #10
0
    def from_diofant(self, a):
        """Convert Diofant's expression to ``dtype``. """
        try:
            return self([self.domain.from_diofant(a)])
        except CoercionFailed:
            pass

        from diofant.polys.numberfields import to_number_field

        try:
            return self(to_number_field(a, self.ext).native_coeffs())
        except (NotAlgebraic, IsomorphismFailed):
            raise CoercionFailed("%s is not a valid algebraic number in %s" %
                                 (a, self))
Beispiel #11
0
    def convert_from(self, element, base):
        """Convert ``element`` to ``self.dtype`` given the base domain. """
        if base.alias is not None:
            method = "from_" + base.alias
        else:
            method = "from_" + base.__class__.__name__

        _convert = getattr(self, method)

        if _convert is not None:
            result = _convert(element, base)

            if result is not None:
                return result

        raise CoercionFailed("can't convert %s of type %s from %s to %s" %
                             (element, type(element), base, self))