Esempio n. 1
0
def test_primitive_element():
    assert primitive_element([sqrt(2)], x) == (x**2 - 2, [1])
    assert primitive_element(
        [sqrt(2), sqrt(3)], x) == (x**4 - 10*x**2 + 1, [1, 1])

    assert primitive_element([sqrt(2)], x, polys=True) == (Poly(x**2 - 2, domain='QQ'), [1])
    assert primitive_element([sqrt(
        2), sqrt(3)], x, polys=True) == (Poly(x**4 - 10*x**2 + 1, domain='QQ'), [1, 1])

    assert primitive_element(
        [sqrt(2)], x, ex=True) == (x**2 - 2, [1], [[1, 0]])
    assert primitive_element([sqrt(2), sqrt(3)], x, ex=True) == \
        (x**4 - 10*x**2 + 1, [1, 1], [[Q(1, 2), 0, -Q(9, 2), 0], [-
         Q(1, 2), 0, Q(11, 2), 0]])

    assert primitive_element(
        [sqrt(2)], x, ex=True, polys=True) == (Poly(x**2 - 2, domain='QQ'), [1], [[1, 0]])
    assert primitive_element([sqrt(2), sqrt(3)], x, ex=True, polys=True) == \
        (Poly(x**4 - 10*x**2 + 1, domain='QQ'), [1, 1], [[Q(1, 2), 0, -Q(9, 2),
         0], [-Q(1, 2), 0, Q(11, 2), 0]])

    assert primitive_element([sqrt(2)], polys=True) == (Poly(x**2 - 2), [1])

    raises(ValueError, lambda: primitive_element([], x, ex=False))
    raises(ValueError, lambda: primitive_element([], x, ex=True))

    # Issue 14117
    a, b = I*sqrt(2*sqrt(2) + 3), I*sqrt(-2*sqrt(2) + 3)
    assert primitive_element([a, b, I], x) == (x**4 + 6*x**2 + 1, [1, 0, 0])

    assert primitive_element([sqrt(2), 0], x) == (x**2 - 2, [1, 0])
    assert primitive_element([0, sqrt(2)], x) == (x**2 - 2, [1, 1])
    assert primitive_element([sqrt(2), 0], x, ex=True) == (x**2 - 2, [1, 0], [[MPQ(1,1), MPQ(0,1)], []])
    assert primitive_element([0, sqrt(2)], x, ex=True) == (x**2 - 2, [1, 1], [[], [MPQ(1,1), MPQ(0,1)]])
Esempio n. 2
0
 def from_sympy(self, a):
     """Convert SymPy's Integer to ``dtype``. """
     if a.is_Rational:
         return MPQ(a.p, a.q)
     elif a.is_Float:
         from sympy.polys.domains import RR
         return MPQ(*map(int, RR.to_rational(a)))
     else:
         raise CoercionFailed("expected `Rational` object, got %s" % a)
Esempio n. 3
0
 def quo(self, a, b):
     """Quotient of ``a`` and ``b``, implies ``__truediv__``. """
     return MPQ(a) / MPQ(b)
Esempio n. 4
0
 def div(self, a, b):
     """Division of ``a`` and ``b``, implies ``__truediv__``. """
     return MPQ(a) / MPQ(b), self.zero
Esempio n. 5
0
 def from_RealField(K1, a, K0):
     """Convert a mpmath ``mpf`` object to ``dtype``. """
     return MPQ(*map(int, K0.to_rational(a)))
Esempio n. 6
0
 def from_GaussianRationalField(K1, a, K0):
     """Convert a ``GaussianElement`` object to ``dtype``. """
     if a.y == 0:
         return MPQ(a.x)
Esempio n. 7
0
 def from_ZZ_gmpy(K1, a, K0):
     """Convert a GMPY ``mpz`` object to ``dtype``. """
     return MPQ(a)
Esempio n. 8
0
 def from_QQ_python(K1, a, K0):
     """Convert a Python ``Fraction`` object to ``dtype``. """
     return MPQ(a.numerator, a.denominator)
Esempio n. 9
0
 def from_ZZ_python(K1, a, K0):
     """Convert a Python ``int`` object to ``dtype``. """
     return MPQ(a)