Example #1
0
def test_primitive_element():
    assert primitive_element([sqrt(2)]) == (PurePoly(x**2 - 2), [1], [[1, 0]])

    assert (primitive_element([sqrt(2), sqrt(3)]) ==
            (PurePoly(x**4 - 10*x**2 + 1), [1, 1], [[QQ(+1, 2), 0, -QQ(9, 2), 0],
                                                    [QQ(-1, 2), 0, QQ(11, 2), 0]]))
    A = QQ.algebraic_field(sqrt(2))
    assert (primitive_element([sqrt(2), sqrt(3)], domain=A) ==
            (PurePoly(x**2 - 2*sqrt(2)*x - 1, x, domain=A), [1, 1],
             [[A.unit], [1, -A.unit]]))
    assert (primitive_element([sqrt(2), sqrt(2 + sqrt(2))], domain=A) ==
            (PurePoly(x**2 - 2*sqrt(2)*x - sqrt(2), x, domain=A), [1, 1],
             [[A.unit], [1, -A.unit]]))

    A = QQ.algebraic_field(sqrt(2) + sqrt(3))
    assert (primitive_element([sqrt(2), sqrt(3)], domain=A) ==
            (PurePoly(x - sqrt(2) - sqrt(3), x, domain=A), [1, 1],
             [[A([QQ(1, 2), 0, -QQ(9, 2), 0])], [A([-QQ(1, 2), 0, QQ(11, 2), 0])]]))

    pytest.raises(ValueError, lambda: primitive_element([]))

    # issue sympy/sympy#13849
    assert (primitive_element([sqrt(2), sqrt(2) + sqrt(5)]) ==
            (PurePoly(x**4 - 76*x**2 + 4), [1, 2], [[QQ(1, 12), 0, QQ(-37, 6), 0],
                                                    [QQ(-1, 24), 0, QQ(43, 12), 0]]))

    # issue sympy/sympy#14117
    assert (primitive_element([I*sqrt(2*sqrt(2) + 3), I*sqrt(-2*sqrt(2) + 3), I]) ==
            (PurePoly(x**4 + 54*x**2 + 81), [1, 2, 4], [[QQ(1, 3), 0], [QQ(1, 27), 0, 2, 0],
                                                        [QQ(-1, 54), 0, QQ(-5, 6), 0]]))
Example #2
0
def test_primitive_element():
    assert primitive_element([sqrt(2)]) == (PurePoly(x**2 - 2), [1], [[1, 0]])

    assert (primitive_element([sqrt(2), sqrt(3)]) ==
            (PurePoly(x**4 - 10*x**2 + 1), [1, 1], [[QQ(+1, 2), 0, -QQ(9, 2), 0],
                                                    [QQ(-1, 2), 0, QQ(11, 2), 0]]))
    A = QQ.algebraic_field(sqrt(2))
    assert (primitive_element([sqrt(2), sqrt(3)], domain=A) ==
            (PurePoly(x**2 - 2*sqrt(2)*x - 1, x, domain=A), [1, 1],
             [[A.unit], [1, -A.unit]]))
    assert (primitive_element([sqrt(2), sqrt(2 + sqrt(2))], domain=A) ==
            (PurePoly(x**2 - 2*sqrt(2)*x - sqrt(2), x, domain=A), [1, 1],
             [[A.unit], [1, -A.unit]]))

    A = QQ.algebraic_field(sqrt(2) + sqrt(3))
    assert (primitive_element([sqrt(2), sqrt(3)], domain=A) ==
            (PurePoly(x - sqrt(2) - sqrt(3), x, domain=A), [1, 1],
             [[A([QQ(1, 2), 0, -QQ(9, 2), 0])], [A([-QQ(1, 2), 0, QQ(11, 2), 0])]]))

    pytest.raises(ValueError, lambda: primitive_element([]))

    # issue sympy/sympy#13849
    assert (primitive_element([sqrt(2), sqrt(2) + sqrt(5)]) ==
            (PurePoly(x**4 - 76*x**2 + 4), [1, 2], [[QQ(1, 12), 0, QQ(-37, 6), 0],
                                                    [QQ(-1, 24), 0, QQ(43, 12), 0]]))

    # issue sympy/sympy#14117
    assert (primitive_element([I*sqrt(2*sqrt(2) + 3), I*sqrt(-2*sqrt(2) + 3), I]) ==
            (PurePoly(x**4 + 54*x**2 + 81), [1, 2, 4], [[QQ(1, 3), 0], [QQ(1, 27), 0, 2, 0],
                                                        [QQ(-1, 54), 0, QQ(-5, 6), 0]]))
Example #3
0
def _construct_algebraic(coeffs, opt):
    """We know that coefficients are algebraic so construct the extension. """
    from diofant.polys.numberfields import primitive_element

    result, exts = [], set()

    for coeff in coeffs:
        if coeff.is_Rational:
            coeff = (None, 0, QQ.from_diofant(coeff))
        else:
            a = coeff.as_coeff_add()[0]
            coeff -= a

            b = coeff.as_coeff_mul()[0]
            coeff /= b

            exts.add(coeff)

            a = QQ.from_diofant(a)
            b = QQ.from_diofant(b)

            coeff = (coeff, b, a)

        result.append(coeff)

    exts = list(exts)

    g, span, H = primitive_element(exts, ex=True, polys=True)
    root = sum(s * ext for s, ext in zip(span, exts))

    domain, g = QQ.algebraic_field((g, root)), g.rep.rep

    for i, (coeff, a, b) in enumerate(result):
        if coeff is not None:
            coeff = a * domain.dtype.from_list(H[exts.index(coeff)], g, QQ) + b
        else:
            coeff = domain.dtype.from_list([b], g, QQ)

        result[i] = coeff

    return domain, result
Example #4
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), [1])
    assert primitive_element([sqrt(2), sqrt(3)], x,
                             polys=True) == (Poly(x**4 - 10 * x**2 + 1),
                                             [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), [1], [[1, 0]])
    assert primitive_element([sqrt(2), sqrt(3)], x, ex=True, polys=True) == \
        (Poly(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)], polys=True) == (Poly(x**2 - 2), [1])

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