Example #1
0
def test_Domain_convert():

    def check_element(e1, e2, K1, K2, K3):
        assert type(e1) is type(e2), '%s, %s: %s %s -> %s' % (e1, e2, K1, K2, K3)
        assert e1 == e2, '%s, %s: %s %s -> %s' % (e1, e2, K1, K2, K3)

    def check_domains(K1, K2):
        K3 = K1.unify(K2)
        check_element(K3.convert_from(K1.one,  K1), K3.one , K1, K2, K3)
        check_element(K3.convert_from(K2.one,  K2), K3.one , K1, K2, K3)
        check_element(K3.convert_from(K1.zero, K1), K3.zero, K1, K2, K3)
        check_element(K3.convert_from(K2.zero, K2), K3.zero, K1, K2, K3)

    def composite_domains(K):
        return [K, K[y], K[z], K[y, z],
                   K.frac_field(y), K.frac_field(z), K.frac_field(y, z)]

    QQ2 = QQ.algebraic_field(sqrt(2))
    QQ3 = QQ.algebraic_field(sqrt(3))
    doms = [ZZ, QQ, QQ2, QQ3, QQ_I, ZZ_I, RR, CC]

    for i, K1 in enumerate(doms):
        for K2 in doms[i:]:
            for K3 in composite_domains(K1):
                for K4 in composite_domains(K2):
                    check_domains(K3, K4)

    assert QQ.convert(10e-52) == QQ(1684996666696915, 1684996666696914987166688442938726917102321526408785780068975640576)

    R, x = ring("x", ZZ)
    assert ZZ.convert(x - x) == 0
    assert ZZ.convert(x - x, R.to_domain()) == 0

    assert CC.convert(ZZ_I(1, 2)) == CC(1, 2)
    assert CC.convert(QQ_I(1, 2)) == CC(1, 2)
Example #2
0
def test_Domain_convert():
    def check_element(e1, e2, K1, K2, K3):
        assert type(e1) is type(e2), '%s, %s: %s %s -> %s' % (e1, e2, K1, K2,
                                                              K3)
        assert e1 == e2, '%s, %s: %s %s -> %s' % (e1, e2, K1, K2, K3)

    def check_domains(K1, K2):
        K3 = K1.unify(K2)
        check_element(K3.convert_from(K1.one, K1), K3.one, K1, K2, K3)
        check_element(K3.convert_from(K2.one, K2), K3.one, K1, K2, K3)
        check_element(K3.convert_from(K1.zero, K1), K3.zero, K1, K2, K3)
        check_element(K3.convert_from(K2.zero, K2), K3.zero, K1, K2, K3)

    def composite_domains(K):
        domains = [
            K,
            K[y],
            K[z],
            K[y, z],
            K.frac_field(y),
            K.frac_field(z),
            K.frac_field(y, z),
            # XXX: These should be tested and made to work...
            # K.old_poly_ring(y), K.old_frac_field(y),
        ]
        return domains

    QQ2 = QQ.algebraic_field(sqrt(2))
    QQ3 = QQ.algebraic_field(sqrt(3))
    doms = [ZZ, QQ, QQ2, QQ3, QQ_I, ZZ_I, RR, CC]

    for i, K1 in enumerate(doms):
        for K2 in doms[i:]:
            for K3 in composite_domains(K1):
                for K4 in composite_domains(K2):
                    check_domains(K3, K4)

    assert QQ.convert(10e-52) == QQ(
        1684996666696915,
        1684996666696914987166688442938726917102321526408785780068975640576)

    R, xr = ring("x", ZZ)
    assert ZZ.convert(xr - xr) == 0
    assert ZZ.convert(xr - xr, R.to_domain()) == 0

    assert CC.convert(ZZ_I(1, 2)) == CC(1, 2)
    assert CC.convert(QQ_I(1, 2)) == CC(1, 2)

    K1 = QQ.frac_field(x)
    K2 = ZZ.frac_field(x)
    K3 = QQ[x]
    K4 = ZZ[x]
    Ks = [K1, K2, K3, K4]
    for Ka, Kb in product(Ks, Ks):
        assert Ka.convert_from(Kb.from_sympy(x), Kb) == Ka.from_sympy(x)

    assert K2.convert_from(QQ(1, 2), QQ) == K2(QQ(1, 2))
Example #3
0
def test_minpoly_domain():
    assert minimal_polynomial(sqrt(2), x, domain=QQ.algebraic_field(sqrt(2))) == \
        x - sqrt(2)
    assert minimal_polynomial(sqrt(8), x, domain=QQ.algebraic_field(sqrt(2))) == \
        x - 2*sqrt(2)
    assert minimal_polynomial(sqrt(Rational(3,2)), x,
        domain=QQ.algebraic_field(sqrt(2))) == 2*x**2 - 3

    raises(NotAlgebraic, lambda: minimal_polynomial(y, x, domain=QQ))
def test_minpoly_domain():
    assert minimal_polynomial(sqrt(2), x, domain=QQ.algebraic_field(sqrt(2))) == \
        x - sqrt(2)
    assert minimal_polynomial(sqrt(8), x, domain=QQ.algebraic_field(sqrt(2))) == \
        x - 2*sqrt(2)
    assert minimal_polynomial(sqrt(Rational(3,2)), x,
        domain=QQ.algebraic_field(sqrt(2))) == 2*x**2 - 3

    raises(NotAlgebraic, lambda: minimal_polynomial(y, x, domain=QQ))
Example #5
0
def test_PolyElement_sqf_norm():
    R, x = ring("x", QQ.algebraic_field(sqrt(3)))
    X = R.to_ground().x

    assert (x**2 - 2).sqf_norm() == (1, x**2 - 2*sqrt(3)*x + 1, X**4 - 10*X**2 + 1)

    R, x = ring("x", QQ.algebraic_field(sqrt(2)))
    X = R.to_ground().x

    assert (x**2 - 3).sqf_norm() == (1, x**2 - 2*sqrt(2)*x - 1, X**4 - 10*X**2 + 1)
Example #6
0
def test_Domain_preprocess():
    assert Domain.preprocess(ZZ) == ZZ
    assert Domain.preprocess(QQ) == QQ
    assert Domain.preprocess(EX) == EX
    assert Domain.preprocess(FF(2)) == FF(2)
    assert Domain.preprocess(ZZ[x, y]) == ZZ[x, y]

    assert Domain.preprocess('Z') == ZZ
    assert Domain.preprocess('Q') == QQ

    assert Domain.preprocess('ZZ') == ZZ
    assert Domain.preprocess('QQ') == QQ

    assert Domain.preprocess('EX') == EX

    assert Domain.preprocess('FF(23)') == FF(23)
    assert Domain.preprocess('GF(23)') == GF(23)

    raises(OptionError, lambda: Domain.preprocess('Z[]'))

    assert Domain.preprocess('Z[x]') == ZZ[x]
    assert Domain.preprocess('Q[x]') == QQ[x]

    assert Domain.preprocess('ZZ[x]') == ZZ[x]
    assert Domain.preprocess('QQ[x]') == QQ[x]

    assert Domain.preprocess('Z[x,y]') == ZZ[x, y]
    assert Domain.preprocess('Q[x,y]') == QQ[x, y]

    assert Domain.preprocess('ZZ[x,y]') == ZZ[x, y]
    assert Domain.preprocess('QQ[x,y]') == QQ[x, y]

    raises(OptionError, lambda: Domain.preprocess('Z()'))

    assert Domain.preprocess('Z(x)') == ZZ.frac_field(x)
    assert Domain.preprocess('Q(x)') == QQ.frac_field(x)

    assert Domain.preprocess('ZZ(x)') == ZZ.frac_field(x)
    assert Domain.preprocess('QQ(x)') == QQ.frac_field(x)

    assert Domain.preprocess('Z(x,y)') == ZZ.frac_field(x, y)
    assert Domain.preprocess('Q(x,y)') == QQ.frac_field(x, y)

    assert Domain.preprocess('ZZ(x,y)') == ZZ.frac_field(x, y)
    assert Domain.preprocess('QQ(x,y)') == QQ.frac_field(x, y)

    assert Domain.preprocess('Q<I>') == QQ.algebraic_field(I)
    assert Domain.preprocess('QQ<I>') == QQ.algebraic_field(I)

    assert Domain.preprocess('Q<sqrt(2), I>') == QQ.algebraic_field(sqrt(2), I)
    assert Domain.preprocess('QQ<sqrt(2), I>') == QQ.algebraic_field(
        sqrt(2), I)

    raises(OptionError, lambda: Domain.preprocess('abc'))
def test_Domain_preprocess():
    assert Domain.preprocess(ZZ) == ZZ
    assert Domain.preprocess(QQ) == QQ
    assert Domain.preprocess(EX) == EX
    assert Domain.preprocess(FF(2)) == FF(2)
    assert Domain.preprocess(ZZ[x, y]) == ZZ[x, y]

    assert Domain.preprocess('Z') == ZZ
    assert Domain.preprocess('Q') == QQ

    assert Domain.preprocess('ZZ') == ZZ
    assert Domain.preprocess('QQ') == QQ

    assert Domain.preprocess('EX') == EX

    assert Domain.preprocess('FF(23)') == FF(23)
    assert Domain.preprocess('GF(23)') == GF(23)

    raises(OptionError, lambda: Domain.preprocess('Z[]'))

    assert Domain.preprocess('Z[x]') == ZZ[x]
    assert Domain.preprocess('Q[x]') == QQ[x]

    assert Domain.preprocess('ZZ[x]') == ZZ[x]
    assert Domain.preprocess('QQ[x]') == QQ[x]

    assert Domain.preprocess('Z[x,y]') == ZZ[x, y]
    assert Domain.preprocess('Q[x,y]') == QQ[x, y]

    assert Domain.preprocess('ZZ[x,y]') == ZZ[x, y]
    assert Domain.preprocess('QQ[x,y]') == QQ[x, y]

    raises(OptionError, lambda: Domain.preprocess('Z()'))

    assert Domain.preprocess('Z(x)') == ZZ.frac_field(x)
    assert Domain.preprocess('Q(x)') == QQ.frac_field(x)

    assert Domain.preprocess('ZZ(x)') == ZZ.frac_field(x)
    assert Domain.preprocess('QQ(x)') == QQ.frac_field(x)

    assert Domain.preprocess('Z(x,y)') == ZZ.frac_field(x, y)
    assert Domain.preprocess('Q(x,y)') == QQ.frac_field(x, y)

    assert Domain.preprocess('ZZ(x,y)') == ZZ.frac_field(x, y)
    assert Domain.preprocess('QQ(x,y)') == QQ.frac_field(x, y)

    assert Domain.preprocess('Q<I>') == QQ.algebraic_field(I)
    assert Domain.preprocess('QQ<I>') == QQ.algebraic_field(I)

    assert Domain.preprocess('Q<sqrt(2), I>') == QQ.algebraic_field(sqrt(2), I)
    assert Domain.preprocess(
        'QQ<sqrt(2), I>') == QQ.algebraic_field(sqrt(2), I)

    raises(OptionError, lambda: Domain.preprocess('abc'))
Example #8
0
def test_Domain_preprocess():
    assert Domain.preprocess(ZZ) == ZZ
    assert Domain.preprocess(QQ) == QQ
    assert Domain.preprocess(EX) == EX
    assert Domain.preprocess(FF(2)) == FF(2)
    assert Domain.preprocess(ZZ[x, y]) == ZZ[x, y]

    assert Domain.preprocess("Z") == ZZ
    assert Domain.preprocess("Q") == QQ

    assert Domain.preprocess("ZZ") == ZZ
    assert Domain.preprocess("QQ") == QQ

    assert Domain.preprocess("EX") == EX

    assert Domain.preprocess("FF(23)") == FF(23)
    assert Domain.preprocess("GF(23)") == GF(23)

    raises(OptionError, "Domain.preprocess('Z[]')")

    assert Domain.preprocess("Z[x]") == ZZ[x]
    assert Domain.preprocess("Q[x]") == QQ[x]

    assert Domain.preprocess("ZZ[x]") == ZZ[x]
    assert Domain.preprocess("QQ[x]") == QQ[x]

    assert Domain.preprocess("Z[x,y]") == ZZ[x, y]
    assert Domain.preprocess("Q[x,y]") == QQ[x, y]

    assert Domain.preprocess("ZZ[x,y]") == ZZ[x, y]
    assert Domain.preprocess("QQ[x,y]") == QQ[x, y]

    raises(OptionError, "Domain.preprocess('Z()')")

    assert Domain.preprocess("Z(x)") == ZZ.frac_field(x)
    assert Domain.preprocess("Q(x)") == QQ.frac_field(x)

    assert Domain.preprocess("ZZ(x)") == ZZ.frac_field(x)
    assert Domain.preprocess("QQ(x)") == QQ.frac_field(x)

    assert Domain.preprocess("Z(x,y)") == ZZ.frac_field(x, y)
    assert Domain.preprocess("Q(x,y)") == QQ.frac_field(x, y)

    assert Domain.preprocess("ZZ(x,y)") == ZZ.frac_field(x, y)
    assert Domain.preprocess("QQ(x,y)") == QQ.frac_field(x, y)

    assert Domain.preprocess("Q<I>") == QQ.algebraic_field(I)
    assert Domain.preprocess("QQ<I>") == QQ.algebraic_field(I)

    assert Domain.preprocess("Q<sqrt(2), I>") == QQ.algebraic_field(sqrt(2), I)
    assert Domain.preprocess("QQ<sqrt(2), I>") == QQ.algebraic_field(sqrt(2), I)

    raises(OptionError, "Domain.preprocess('abc')")
Example #9
0
def primitive_element(extension, x=None, *, ex=False, polys=False):
    """Construct a common number field for all extensions. """
    if not extension:
        raise ValueError("can't compute primitive element for empty extension")

    if x is not None:
        x, cls = sympify(x), Poly
    else:
        x, cls = Dummy('x'), PurePoly

    if not ex:
        gen, coeffs = extension[0], [1]
        g = minimal_polynomial(gen, x, polys=True)
        for ext in extension[1:]:
            _, factors = factor_list(g, extension=ext)
            g = _choose_factor(factors, x, gen)
            s, _, g = g.sqf_norm()
            gen += s * ext
            coeffs.append(s)

        if not polys:
            return g.as_expr(), coeffs
        else:
            return cls(g), coeffs

    gen, coeffs = extension[0], [1]
    f = minimal_polynomial(gen, x, polys=True)
    K = QQ.algebraic_field((f, gen))  # incrementally constructed field
    reps = [K.unit]  # representations of extension elements in K
    for ext in extension[1:]:
        p = minimal_polynomial(ext, x, polys=True)
        L = QQ.algebraic_field((p, ext))
        _, factors = factor_list(f, domain=L)
        f = _choose_factor(factors, x, gen)
        s, g, f = f.sqf_norm()
        gen += s * ext
        coeffs.append(s)
        K = QQ.algebraic_field((f, gen))
        h = _switch_domain(g, K)
        erep = _linsolve(h.gcd(p))  # ext as element of K
        ogen = K.unit - s * erep  # old gen as element of K
        reps = [dup_eval(_.rep, ogen, K) for _ in reps] + [erep]

    H = [_.rep for _ in reps]
    if not polys:
        return f.as_expr(), coeffs, H
    else:
        return f, coeffs, H
Example #10
0
def test_sring():
    x, y, z, t = symbols("x,y,z,t")

    R = PolyRing("x,y,z", ZZ, lex)
    assert sring(x + 2*y + 3*z) == (R, R.x + 2*R.y + 3*R.z)

    R = PolyRing("x,y,z", QQ, lex)
    assert sring(x + 2*y + z/3) == (R, R.x + 2*R.y + R.z/3)
    assert sring([x, 2*y, z/3]) == (R, [R.x, 2*R.y, R.z/3])

    Rt = PolyRing("t", ZZ, lex)
    R = PolyRing("x,y,z", Rt, lex)
    assert sring(x + 2*t*y + 3*t**2*z, x, y, z) == (R, R.x + 2*Rt.t*R.y + 3*Rt.t**2*R.z)

    Rt = PolyRing("t", QQ, lex)
    R = PolyRing("x,y,z", Rt, lex)
    assert sring(x + t*y/2 + t**2*z/3, x, y, z) == (R, R.x + Rt.t*R.y/2 + Rt.t**2*R.z/3)

    Rt = FracField("t", ZZ, lex)
    R = PolyRing("x,y,z", Rt, lex)
    assert sring(x + 2*y/t + t**2*z/3, x, y, z) == (R, R.x + 2*R.y/Rt.t + Rt.t**2*R.z/3)

    r = sqrt(2) - sqrt(3)
    R, a = sring(r, extension=True)
    assert R.domain == QQ.algebraic_field(r)
    assert R.gens == ()
    assert a == R.domain.from_sympy(r)
Example #11
0
def test_complex_exponential():
    w = exp(-I * 2 * pi / 3, evaluate=False)
    alg = QQ.algebraic_field(w)
    assert construct_domain([w**2, w, 1], extension=True) == (alg, [
        alg.convert(w**2), alg.convert(w),
        alg.convert(1)
    ])
Example #12
0
def test_dmp_lift():
    q = [QQ(1, 1), QQ(0, 1), QQ(1, 1)]

    f = [
        ANP([QQ(1, 1)], q, QQ),
        ANP([], q, QQ),
        ANP([], q, QQ),
        ANP([QQ(1, 1), QQ(0, 1)], q, QQ),
        ANP([QQ(17, 1), QQ(0, 1)], q, QQ),
    ]

    assert dmp_lift(f, 0, QQ.algebraic_field(I)) == [
        QQ(1),
        QQ(0),
        QQ(0),
        QQ(0),
        QQ(0),
        QQ(0),
        QQ(2),
        QQ(0),
        QQ(578),
        QQ(0),
        QQ(0),
        QQ(0),
        QQ(1),
        QQ(0),
        QQ(-578),
        QQ(0),
        QQ(83521),
    ]

    raises(DomainError, lambda: dmp_lift([EX(1), EX(2)], 0, EX))
Example #13
0
def test_dmp_ext_factor():
    h = [QQ(1),QQ(0),QQ(-2)]
    K = QQ.algebraic_field(sqrt(2))

    assert dmp_ext_factor([], 0, K) == (ANP([], h, QQ), [])
    assert dmp_ext_factor([[]], 1, K) == (ANP([], h, QQ), [])

    f = [[ANP([QQ(1)], h, QQ)], [ANP([QQ(1)], h, QQ)]]

    assert dmp_ext_factor(f, 1, K) == (ANP([QQ(1)], h, QQ), [(f, 1)])

    g = [[ANP([QQ(2)], h, QQ)], [ANP([QQ(2)], h, QQ)]]

    assert dmp_ext_factor(g, 1, K) == (ANP([QQ(2)], h, QQ), [(f, 1)])

    f = [[ANP([QQ(1)], h, QQ)], [], [ANP([QQ(-2)], h, QQ), ANP([], h, QQ), ANP([], h, QQ)]]

    assert dmp_ext_factor(f, 1, K) == \
        (ANP([QQ(1)], h, QQ), [
            ([[ANP([QQ(1)], h, QQ)], [ANP([QQ(-1),QQ(0)], h, QQ), ANP([], h, QQ)]], 1),
            ([[ANP([QQ(1)], h, QQ)], [ANP([QQ( 1),QQ(0)], h, QQ), ANP([], h, QQ)]], 1),
        ])

    f = [[ANP([QQ(2)], h, QQ)], [], [ANP([QQ(-4)], h, QQ), ANP([], h, QQ), ANP([], h, QQ)]]

    assert dmp_ext_factor(f, 1, K) == \
        (ANP([QQ(2)], h, QQ), [
            ([[ANP([QQ(1)], h, QQ)], [ANP([QQ(-1),QQ(0)], h, QQ), ANP([], h, QQ)]], 1),
            ([[ANP([QQ(1)], h, QQ)], [ANP([QQ( 1),QQ(0)], h, QQ), ANP([], h, QQ)]], 1),
        ])
Example #14
0
def test_dmp_lift():
    q = [QQ(1, 1), QQ(0, 1), QQ(1, 1)]

    f = [
        ANP([QQ(1, 1)], q, QQ),
        ANP([], q, QQ),
        ANP([], q, QQ),
        ANP([QQ(1, 1), QQ(0, 1)], q, QQ),
        ANP([QQ(17, 1), QQ(0, 1)], q, QQ),
    ]

    assert dmp_lift(f, 0, QQ.algebraic_field(I)) == [
        QQ(1),
        QQ(0),
        QQ(0),
        QQ(0),
        QQ(0),
        QQ(0),
        QQ(2),
        QQ(0),
        QQ(578),
        QQ(0),
        QQ(0),
        QQ(0),
        QQ(1),
        QQ(0),
        QQ(-578),
        QQ(0),
        QQ(83521),
    ]

    raises(DomainError, "dmp_lift([EX(1), EX(2)], 0, EX)")
Example #15
0
def test_dmp_ext_factor():
    h = [QQ(1),QQ(0),QQ(-2)]
    K = QQ.algebraic_field(sqrt(2))

    assert dmp_ext_factor([], 0, K) == (ANP([], h, QQ), [])
    assert dmp_ext_factor([[]], 1, K) == (ANP([], h, QQ), [])

    f = [[ANP([QQ(1)], h, QQ)], [ANP([QQ(1)], h, QQ)]]

    assert dmp_ext_factor(f, 1, K) == (ANP([QQ(1)], h, QQ), [(f, 1)])

    g = [[ANP([QQ(2)], h, QQ)], [ANP([QQ(2)], h, QQ)]]

    assert dmp_ext_factor(g, 1, K) == (ANP([QQ(2)], h, QQ), [(f, 1)])

    f = [[ANP([QQ(1)], h, QQ)], [], [ANP([QQ(-2)], h, QQ), ANP([], h, QQ), ANP([], h, QQ)]]

    assert dmp_ext_factor(f, 1, K) == \
        (ANP([QQ(1)], h, QQ), [
            ([[ANP([QQ(1)], h, QQ)], [ANP([QQ(-1),QQ(0)], h, QQ), ANP([], h, QQ)]], 1),
            ([[ANP([QQ(1)], h, QQ)], [ANP([QQ( 1),QQ(0)], h, QQ), ANP([], h, QQ)]], 1),
        ])

    f = [[ANP([QQ(2)], h, QQ)], [], [ANP([QQ(-4)], h, QQ), ANP([], h, QQ), ANP([], h, QQ)]]

    assert dmp_ext_factor(f, 1, K) == \
        (ANP([QQ(2)], h, QQ), [
            ([[ANP([QQ(1)], h, QQ)], [ANP([QQ(-1),QQ(0)], h, QQ), ANP([], h, QQ)]], 1),
            ([[ANP([QQ(1)], h, QQ)], [ANP([QQ( 1),QQ(0)], h, QQ), ANP([], h, QQ)]], 1),
        ])
Example #16
0
def test_DomainMatrix_from_Matrix():
    sdm = SDM({0: {0: ZZ(1), 1: ZZ(2)}, 1: {0: ZZ(3), 1: ZZ(4)}}, (2, 2), ZZ)
    A = DomainMatrix.from_Matrix(Matrix([[1, 2], [3, 4]]))
    assert A.rep == sdm
    assert A.shape == (2, 2)
    assert A.domain == ZZ

    K = QQ.algebraic_field(sqrt(2))
    sdm = SDM(
        {
            0: {
                0: K.convert(1 + sqrt(2)),
                1: K.convert(2 + sqrt(2))
            },
            1: {
                0: K.convert(3 + sqrt(2)),
                1: K.convert(4 + sqrt(2))
            }
        }, (2, 2), K)
    A = DomainMatrix.from_Matrix(Matrix([[1 + sqrt(2), 2 + sqrt(2)],
                                         [3 + sqrt(2), 4 + sqrt(2)]]),
                                 extension=True)
    assert A.rep == sdm
    assert A.shape == (2, 2)
    assert A.domain == K

    A = DomainMatrix.from_Matrix(Matrix([[QQ(1, 2), QQ(3, 4)],
                                         [QQ(0, 1), QQ(0, 1)]]),
                                 fmt='dense')
    ddm = DDM([[QQ(1, 2), QQ(3, 4)], [QQ(0, 1), QQ(0, 1)]], (2, 2), QQ)

    assert A.rep == ddm
    assert A.shape == (2, 2)
    assert A.domain == QQ
Example #17
0
def test_dmp_ext_factor():
    R, x,y = ring("x,y", QQ.algebraic_field(sqrt(2)))
    def anp(x):
        return ANP(x, [QQ(1), QQ(0), QQ(-2)], QQ)

    assert R.dmp_ext_factor(0) == (anp([]), [])

    f = anp([QQ(1)])*x + anp([QQ(1)])

    assert R.dmp_ext_factor(f) == (anp([QQ(1)]), [(f, 1)])

    g = anp([QQ(2)])*x + anp([QQ(2)])

    assert R.dmp_ext_factor(g) == (anp([QQ(2)]), [(f, 1)])

    f = anp([QQ(1)])*x**2 + anp([QQ(-2)])*y**2

    assert R.dmp_ext_factor(f) == \
        (anp([QQ(1)]), [(anp([QQ(1)])*x + anp([QQ(-1), QQ(0)])*y, 1),
                        (anp([QQ(1)])*x + anp([QQ( 1), QQ(0)])*y, 1)])

    f = anp([QQ(2)])*x**2 + anp([QQ(-4)])*y**2

    assert R.dmp_ext_factor(f) == \
        (anp([QQ(2)]), [(anp([QQ(1)])*x + anp([QQ(-1), QQ(0)])*y, 1),
                        (anp([QQ(1)])*x + anp([QQ( 1), QQ(0)])*y, 1)])
Example #18
0
def test_sring():
    x, y, z, t = symbols("x,y,z,t")

    R = PolyRing("x,y,z", ZZ, lex)
    assert sring(x + 2 * y + 3 * z) == (R, R.x + 2 * R.y + 3 * R.z)

    R = PolyRing("x,y,z", QQ, lex)
    assert sring(x + 2 * y + z / 3) == (R, R.x + 2 * R.y + R.z / 3)
    assert sring([x, 2 * y, z / 3]) == (R, [R.x, 2 * R.y, R.z / 3])

    Rt = PolyRing("t", ZZ, lex)
    R = PolyRing("x,y,z", Rt, lex)
    assert sring(x + 2 * t * y + 3 * t**2 * z, x, y,
                 z) == (R, R.x + 2 * Rt.t * R.y + 3 * Rt.t**2 * R.z)

    Rt = PolyRing("t", QQ, lex)
    R = PolyRing("x,y,z", Rt, lex)
    assert sring(x + t * y / 2 + t**2 * z / 3, x, y,
                 z) == (R, R.x + Rt.t * R.y / 2 + Rt.t**2 * R.z / 3)

    Rt = FracField("t", ZZ, lex)
    R = PolyRing("x,y,z", Rt, lex)
    assert sring(x + 2 * y / t + t**2 * z / 3, x, y,
                 z) == (R, R.x + 2 * R.y / Rt.t + Rt.t**2 * R.z / 3)

    r = sqrt(2) - sqrt(3)
    R, a = sring(r, extension=True)
    assert R.domain == QQ.algebraic_field(r)
    assert R.gens == ()
    assert a == R.domain.from_sympy(r)
Example #19
0
def test_dmp_ext_factor():
    R, x,y = ring("x,y", QQ.algebraic_field(sqrt(2)))
    def anp(x):
        return ANP(x, [QQ(1), QQ(0), QQ(-2)], QQ)

    assert R.dmp_ext_factor(0) == (anp([]), [])

    f = anp([QQ(1)])*x + anp([QQ(1)])

    assert R.dmp_ext_factor(f) == (anp([QQ(1)]), [(f, 1)])

    g = anp([QQ(2)])*x + anp([QQ(2)])

    assert R.dmp_ext_factor(g) == (anp([QQ(2)]), [(f, 1)])

    f = anp([QQ(1)])*x**2 + anp([QQ(-2)])*y**2

    assert R.dmp_ext_factor(f) == \
        (anp([QQ(1)]), [(anp([QQ(1)])*x + anp([QQ(-1), QQ(0)])*y, 1),
                        (anp([QQ(1)])*x + anp([QQ( 1), QQ(0)])*y, 1)])

    f = anp([QQ(2)])*x**2 + anp([QQ(-4)])*y**2

    assert R.dmp_ext_factor(f) == \
        (anp([QQ(2)]), [(anp([QQ(1)])*x + anp([QQ(-1), QQ(0)])*y, 1),
                        (anp([QQ(1)])*x + anp([QQ( 1), QQ(0)])*y, 1)])
Example #20
0
def test_round_two():
    # Poly must be monic, irreducible, and over ZZ:
    raises(ValueError, lambda: round_two(Poly(3 * x ** 2 + 1)))
    raises(ValueError, lambda: round_two(Poly(x ** 2 - 1)))
    raises(ValueError, lambda: round_two(Poly(x ** 2 + QQ(1, 2))))

    # Test on many fields:
    cases = (
        # A couple of cyclotomic fields:
        (cyclotomic_poly(5), DomainMatrix.eye(4, QQ), 125),
        (cyclotomic_poly(7), DomainMatrix.eye(6, QQ), -16807),
        # A couple of quadratic fields (one 1 mod 4, one 3 mod 4):
        (x ** 2 - 5, DM([[1, (1, 2)], [0, (1, 2)]], QQ), 5),
        (x ** 2 - 7, DM([[1, 0], [0, 1]], QQ), 28),
        # Dedekind's example of a field with 2 as essential disc divisor:
        (x ** 3 + x ** 2 - 2 * x + 8, DM([[1, 0, 0], [0, 1, 0], [0, (1, 2), (1, 2)]], QQ).transpose(), -503),
        # A bunch of cubics with various forms for F -- all of these require
        # second or third enlargements. (Five of them require a third, while the rest require just a second.)
        # F = 2^2
        (x**3 + 3 * x**2 - 4 * x + 4, DM([((1, 2), (1, 4), (1, 4)), (0, (1, 2), (1, 2)), (0, 0, 1)], QQ).transpose(), -83),
        # F = 2^2 * 3
        (x**3 + 3 * x**2 + 3 * x - 3, DM([((1, 2), 0, (1, 2)), (0, 1, 0), (0, 0, 1)], QQ).transpose(), -108),
        # F = 2^3
        (x**3 + 5 * x**2 - x + 3, DM([((1, 4), 0, (3, 4)), (0, (1, 2), (1, 2)), (0, 0, 1)], QQ).transpose(), -31),
        # F = 2^2 * 5
        (x**3 + 5 * x**2 - 5 * x - 5, DM([((1, 2), 0, (1, 2)), (0, 1, 0), (0, 0, 1)], QQ).transpose(), 1300),
        # F = 3^2
        (x**3 + 3 * x**2 + 5, DM([((1, 3), (1, 3), (1, 3)), (0, 1, 0), (0, 0, 1)], QQ).transpose(), -135),
        # F = 3^3
        (x**3 + 6 * x**2 + 3 * x - 1, DM([((1, 3), (1, 3), (1, 3)), (0, 1, 0), (0, 0, 1)], QQ).transpose(), 81),
        # F = 2^2 * 3^2
        (x**3 + 6 * x**2 + 4, DM([((1, 3), (2, 3), (1, 3)), (0, 1, 0), (0, 0, (1, 2))], QQ).transpose(), -108),
        # F = 2^3 * 7
        (x**3 + 7 * x**2 + 7 * x - 7, DM([((1, 4), 0, (3, 4)), (0, (1, 2), (1, 2)), (0, 0, 1)], QQ).transpose(), 49),
        # F = 2^2 * 13
        (x**3 + 7 * x**2 - x + 5, DM([((1, 2), 0, (1, 2)), (0, 1, 0), (0, 0, 1)], QQ).transpose(), -2028),
        # F = 2^4
        (x**3 + 7 * x**2 - 5 * x + 5, DM([((1, 4), 0, (3, 4)), (0, (1, 2), (1, 2)), (0, 0, 1)], QQ).transpose(), -140),
        # F = 5^2
        (x**3 + 4 * x**2 - 3 * x + 7, DM([((1, 5), (4, 5), (4, 5)), (0, 1, 0), (0, 0, 1)], QQ).transpose(), -175),
        # F = 7^2
        (x**3 + 8 * x**2 + 5 * x - 1, DM([((1, 7), (6, 7), (2, 7)), (0, 1, 0), (0, 0, 1)], QQ).transpose(), 49),
        # F = 2 * 5 * 7
        (x**3 + 8 * x**2 - 2 * x + 6, DM([(1, 0, 0), (0, 1, 0), (0, 0, 1)], QQ).transpose(), -14700),
        # F = 2^2 * 3 * 5
        (x**3 + 6 * x**2 - 3 * x + 8, DM([(1, 0, 0), (0, (1, 4), (1, 4)), (0, 0, 1)], QQ).transpose(), -675),
        # F = 2 * 3^2 * 7
        (x**3 + 9 * x**2 + 6 * x - 8, DM([(1, 0, 0), (0, (1, 2), (1, 2)), (0, 0, 1)], QQ).transpose(), 3969),
        # F = 2^2 * 3^2 * 7
        (x**3 + 15 * x**2 - 9 * x + 13, DM([((1, 6), (1, 3), (1, 6)), (0, 1, 0), (0, 0, 1)], QQ).transpose(), -5292),
    )
    for f, B_exp, d_exp in cases:
        K = QQ.algebraic_field((f, theta))
        B = K.maximal_order().QQ_matrix
        d = K.discriminant()
        assert d == d_exp
        # The computed basis need not equal the expected one, but their quotient
        # must be unimodular:
        assert (B.inv()*B_exp).det()**2 == 1
Example #21
0
def test_dup_factor_list():
    assert dup_factor_list([], ZZ) == (ZZ(0), [])
    assert dup_factor_list([], QQ) == (QQ(0), [])
    assert dup_factor_list([], ZZ['y']) == (DMP([],ZZ), [])
    assert dup_factor_list([], QQ['y']) == (DMP([],QQ), [])

    assert dup_factor_list_include([], ZZ) == [([], 1)]

    assert dup_factor_list([ZZ(7)], ZZ) == (ZZ(7), [])
    assert dup_factor_list([QQ(1,7)], QQ) == (QQ(1,7), [])
    assert dup_factor_list([DMP([ZZ(7)],ZZ)], ZZ['y']) == (DMP([ZZ(7)],ZZ), [])
    assert dup_factor_list([DMP([QQ(1,7)],QQ)], QQ['y']) == (DMP([QQ(1,7)],QQ), [])

    assert dup_factor_list_include([ZZ(7)], ZZ) == [([ZZ(7)], 1)]

    assert dup_factor_list([ZZ(1),ZZ(2),ZZ(1)], ZZ) == \
        (ZZ(1), [([ZZ(1), ZZ(1)], 2)])
    assert dup_factor_list([QQ(1,2),QQ(1),QQ(1,2)], QQ) == \
        (QQ(1,2), [([QQ(1),QQ(1)], 2)])

    assert dup_factor_list_include([ZZ(1),ZZ(2),ZZ(1)], ZZ) == \
        [([ZZ(1), ZZ(1)], 2)]

    K = FF(2)

    assert dup_factor_list([K(1),K(0),K(1)], K) == \
        (K(1), [([K(1), K(1)], 2)])

    assert dup_factor_list([RR(1.0),RR(2.0),RR(1.0)], RR) == \
        (RR(1.0), [([RR(1.0),RR(1.0)], 2)])
    assert dup_factor_list([RR(2.0),RR(4.0),RR(2.0)], RR) == \
        (RR(2.0), [([RR(1.0),RR(1.0)], 2)])


    f = [DMP([ZZ(4),ZZ(0)],ZZ),DMP([ZZ(4),ZZ(0),ZZ(0)],ZZ),DMP([],ZZ)]

    assert dup_factor_list(f, ZZ['y']) == \
        (DMP([ZZ(4)],ZZ), [([DMP([ZZ(1),ZZ(0)],ZZ)], 1),
                           ([DMP([ZZ(1)],ZZ),DMP([],ZZ)], 1),
                           ([DMP([ZZ(1)],ZZ),DMP([ZZ(1),ZZ(0)],ZZ)], 1)])


    f = [DMP([QQ(1,2),QQ(0)],ZZ),DMP([QQ(1,2),QQ(0),QQ(0)],ZZ),DMP([],ZZ)]

    assert dup_factor_list(f, QQ['y']) == \
        (DMP([QQ(1,2)],QQ), [([DMP([QQ(1),QQ(0)],QQ)], 1),
                             ([DMP([QQ(1)],QQ),DMP([],QQ)], 1),
                             ([DMP([QQ(1)],QQ),DMP([QQ(1),QQ(0)],QQ)], 1)])

    K = QQ.algebraic_field(I)
    h = [QQ(1,1), QQ(0,1), QQ(1,1)]

    f = [ANP([QQ(1,1)], h, QQ), ANP([], h, QQ), ANP([QQ(2,1)], h, QQ), ANP([], h, QQ), ANP([], h, QQ)]

    assert dup_factor_list(f, K) == \
        (ANP([QQ(1,1)], h, QQ), [([ANP([QQ(1,1)], h, QQ), ANP([], h, QQ)], 2),
                                 ([ANP([QQ(1,1)], h, QQ), ANP([], h, QQ), ANP([QQ(2,1)], h, QQ)], 1)])

    raises(DomainError, "dup_factor_list([EX(sin(1))], EX)")
Example #22
0
def test_dup_factor_list():
    assert dup_factor_list([], ZZ) == (ZZ(0), [])
    assert dup_factor_list([], QQ) == (QQ(0), [])
    assert dup_factor_list([], ZZ['y']) == (DMP([],ZZ), [])
    assert dup_factor_list([], QQ['y']) == (DMP([],QQ), [])

    assert dup_factor_list_include([], ZZ) == [([], 1)]

    assert dup_factor_list([ZZ(7)], ZZ) == (ZZ(7), [])
    assert dup_factor_list([QQ(1,7)], QQ) == (QQ(1,7), [])
    assert dup_factor_list([DMP([ZZ(7)],ZZ)], ZZ['y']) == (DMP([ZZ(7)],ZZ), [])
    assert dup_factor_list([DMP([QQ(1,7)],QQ)], QQ['y']) == (DMP([QQ(1,7)],QQ), [])

    assert dup_factor_list_include([ZZ(7)], ZZ) == [([ZZ(7)], 1)]

    assert dup_factor_list([ZZ(1),ZZ(2),ZZ(1)], ZZ) == \
        (ZZ(1), [([ZZ(1), ZZ(1)], 2)])
    assert dup_factor_list([QQ(1,2),QQ(1),QQ(1,2)], QQ) == \
        (QQ(1,2), [([QQ(1),QQ(1)], 2)])

    assert dup_factor_list_include([ZZ(1),ZZ(2),ZZ(1)], ZZ) == \
        [([ZZ(1), ZZ(1)], 2)]

    K = FF(2)

    assert dup_factor_list([K(1),K(0),K(1)], K) == \
        (K(1), [([K(1), K(1)], 2)])

    assert dup_factor_list([RR(1.0),RR(2.0),RR(1.0)], RR) == \
        (RR(1.0), [([RR(1.0),RR(1.0)], 2)])
    assert dup_factor_list([RR(2.0),RR(4.0),RR(2.0)], RR) == \
        (RR(2.0), [([RR(1.0),RR(1.0)], 2)])


    f = [DMP([ZZ(4),ZZ(0)],ZZ),DMP([ZZ(4),ZZ(0),ZZ(0)],ZZ),DMP([],ZZ)]

    assert dup_factor_list(f, ZZ['y']) == \
        (DMP([ZZ(4)],ZZ), [([DMP([ZZ(1),ZZ(0)],ZZ)], 1),
                           ([DMP([ZZ(1)],ZZ),DMP([],ZZ)], 1),
                           ([DMP([ZZ(1)],ZZ),DMP([ZZ(1),ZZ(0)],ZZ)], 1)])


    f = [DMP([QQ(1,2),QQ(0)],ZZ),DMP([QQ(1,2),QQ(0),QQ(0)],ZZ),DMP([],ZZ)]

    assert dup_factor_list(f, QQ['y']) == \
        (DMP([QQ(1,2)],QQ), [([DMP([QQ(1),QQ(0)],QQ)], 1),
                             ([DMP([QQ(1)],QQ),DMP([],QQ)], 1),
                             ([DMP([QQ(1)],QQ),DMP([QQ(1),QQ(0)],QQ)], 1)])

    K = QQ.algebraic_field(I)
    h = [QQ(1,1), QQ(0,1), QQ(1,1)]

    f = [ANP([QQ(1,1)], h, QQ), ANP([], h, QQ), ANP([QQ(2,1)], h, QQ), ANP([], h, QQ), ANP([], h, QQ)]

    assert dup_factor_list(f, K) == \
        (ANP([QQ(1,1)], h, QQ), [([ANP([QQ(1,1)], h, QQ), ANP([], h, QQ)], 2),
                                 ([ANP([QQ(1,1)], h, QQ), ANP([], h, QQ), ANP([QQ(2,1)], h, QQ)], 1)])

    raises(DomainError, "dup_factor_list([EX(sin(1))], EX)")
Example #23
0
def test_AlgebraicField_alias():
    # No default alias:
    k = QQ.algebraic_field(sqrt(2))
    assert k.ext.alias is None

    # For a single extension, its alias is used:
    alpha = AlgebraicNumber(sqrt(2), alias='alpha')
    k = QQ.algebraic_field(alpha)
    assert k.ext.alias.name == 'alpha'

    # Can override the alias of a single extension:
    k = QQ.algebraic_field(alpha, alias='theta')
    assert k.ext.alias.name == 'theta'

    # With multiple extensions, no default alias:
    k = QQ.algebraic_field(sqrt(2), sqrt(3))
    assert k.ext.alias is None

    # With multiple extensions, no default alias, even if one of
    # the extensions has one:
    k = QQ.algebraic_field(alpha, sqrt(3))
    assert k.ext.alias is None

    # With multiple extensions, may set an alias:
    k = QQ.algebraic_field(sqrt(2), sqrt(3), alias='theta')
    assert k.ext.alias.name == 'theta'

    # Alias is passed to constructed field elements:
    k = QQ.algebraic_field(alpha)
    beta = k.to_alg_num(k([1, 2, 3]))
    assert beta.alias is alpha.alias
def test_Gaussian_postprocess():
    opt = {'gaussian': True}
    Gaussian.postprocess(opt)

    assert opt == {
        'gaussian': True,
        'extension': set([I]),
        'domain': QQ.algebraic_field(I),
    }
Example #25
0
def test_Domain_unify_algebraic():
    sqrt5 = QQ.algebraic_field(sqrt(5))
    sqrt7 = QQ.algebraic_field(sqrt(7))
    sqrt57 = QQ.algebraic_field(sqrt(5), sqrt(7))

    assert sqrt5.unify(sqrt7) == sqrt57

    assert sqrt5.unify(sqrt5[x, y]) == sqrt5[x, y]
    assert sqrt5[x, y].unify(sqrt5) == sqrt5[x, y]

    assert sqrt5.unify(sqrt5.frac_field(x, y)) == sqrt5.frac_field(x, y)
    assert sqrt5.frac_field(x, y).unify(sqrt5) == sqrt5.frac_field(x, y)

    assert sqrt5.unify(sqrt7[x, y]) == sqrt57[x, y]
    assert sqrt5[x, y].unify(sqrt7) == sqrt57[x, y]

    assert sqrt5.unify(sqrt7.frac_field(x, y)) == sqrt57.frac_field(x, y)
    assert sqrt5.frac_field(x, y).unify(sqrt7) == sqrt57.frac_field(x, y)
Example #26
0
def test_Domain_unify_algebraic():
    sqrt5 = QQ.algebraic_field(sqrt(5))
    sqrt7 = QQ.algebraic_field(sqrt(7))
    sqrt57 = QQ.algebraic_field(sqrt(5), sqrt(7))

    assert sqrt5.unify(sqrt7) == sqrt57

    assert sqrt5.unify(sqrt5[x, y]) == sqrt5[x, y]
    assert sqrt5[x, y].unify(sqrt5) == sqrt5[x, y]

    assert sqrt5.unify(sqrt5.frac_field(x, y)) == sqrt5.frac_field(x, y)
    assert sqrt5.frac_field(x, y).unify(sqrt5) == sqrt5.frac_field(x, y)

    assert sqrt5.unify(sqrt7[x, y]) == sqrt57[x, y]
    assert sqrt5[x, y].unify(sqrt7) == sqrt57[x, y]

    assert sqrt5.unify(sqrt7.frac_field(x, y)) == sqrt57.frac_field(x, y)
    assert sqrt5.frac_field(x, y).unify(sqrt7) == sqrt57.frac_field(x, y)
Example #27
0
def test_Gaussian_postprocess():
    opt = {'gaussian': True}
    Gaussian.postprocess(opt)

    assert opt == {
        'gaussian': True,
        'extension': set([I]),
        'domain': QQ.algebraic_field(I),
    }
Example #28
0
def test_Extension_postprocess():
    opt = {"extension": set([sqrt(2)])}
    Extension.postprocess(opt)

    assert opt == {"extension": set([sqrt(2)]), "domain": QQ.algebraic_field(sqrt(2))}

    opt = {"extension": True}
    Extension.postprocess(opt)

    assert opt == {"extension": True}
Example #29
0
def test_AlgebraicField_integral_basis():
    alpha = AlgebraicNumber(sqrt(5), alias='alpha')
    k = QQ.algebraic_field(alpha)
    B0 = k.integral_basis()
    B1 = k.integral_basis(fmt='sympy')
    B2 = k.integral_basis(fmt='alg')
    assert B0 == [k([1]), k([S.Half, S.Half])]
    assert B1 == [1, S.Half + alpha/2]
    assert B2 == [alpha.field_element([1]),
                  alpha.field_element([S.Half, S.Half])]
Example #30
0
def test_Domain__algebraic_field():
    alg = ZZ.algebraic_field(sqrt(2))
    assert alg.ext.minpoly == pure**2 - 2
    assert alg.dom == QQ

    alg = QQ.algebraic_field(sqrt(2))
    assert alg.ext.minpoly == pure**2 - 2
    assert alg.dom == QQ

    alg = alg.algebraic_field(sqrt(3))
    assert alg.ext.minpoly == pure**4 - 10*pure**2 + 1
Example #31
0
def test_Domain__algebraic_field():
    alg = ZZ.algebraic_field(sqrt(2))
    assert alg.ext.minpoly == pure**2 - 2
    assert alg.dom == QQ

    alg = QQ.algebraic_field(sqrt(2))
    assert alg.ext.minpoly == pure**2 - 2
    assert alg.dom == QQ

    alg = alg.algebraic_field(sqrt(3))
    assert alg.ext.minpoly == pure**4 - 10*pure**2 + 1
Example #32
0
def _construct_algebraic(coeffs, opt):
    """We know that coefficients are algebraic so construct the extension. """
    from sympy.polys.numberfields import primitive_element

    exts = set()

    def build_trees(args):
        trees = []
        for a in args:
            if a.is_Rational:
                tree = ('Q', QQ.from_sympy(a))
            elif a.is_Add:
                tree = ('+', build_trees(a.args))
            elif a.is_Mul:
                tree = ('*', build_trees(a.args))
            else:
                tree = ('e', a)
                exts.add(a)
            trees.append(tree)
        return trees

    trees = build_trees(coeffs)
    exts = list(ordered(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

    exts_dom = [domain.dtype.from_list(h, g, QQ) for h in H]
    exts_map = dict(zip(exts, exts_dom))

    def convert_tree(tree):
        op, args = tree
        if op == 'Q':
            return domain.dtype.from_list([args], g, QQ)
        elif op == '+':
            return sum((convert_tree(a) for a in args), domain.zero)
        elif op == '*':
            # return prod(convert(a) for a in args)
            t = convert_tree(args[0])
            for a in args[1:]:
                t *= convert_tree(a)
            return t
        elif op == 'e':
            return exts_map[args]
        else:
            raise RuntimeError

    result = [convert_tree(tree) for tree in trees]

    return domain, result
Example #33
0
def test_Extension_postprocess():
    opt = {'extension': set([sqrt(2)])}
    Extension.postprocess(opt)

    assert opt == {
        'extension': set([sqrt(2)]),
        'domain': QQ.algebraic_field(sqrt(2)),
    }

    opt = {'extension': True}
    Extension.postprocess(opt)

    assert opt == {'extension': True}
def test_Extension_postprocess():
    opt = {'extension': set([sqrt(2)])}
    Extension.postprocess(opt)

    assert opt == {
        'extension': set([sqrt(2)]),
        'domain': QQ.algebraic_field(sqrt(2)),
    }

    opt = {'extension': True}
    Extension.postprocess(opt)

    assert opt == {'extension': True}
Example #35
0
def test_DomainMatrix_from_list_sympy():
    ddm = DDM([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ)
    A = DomainMatrix.from_list_sympy(2, 2, [[1, 2], [3, 4]])
    assert A.rep == ddm
    assert A.shape == (2, 2)
    assert A.domain == ZZ

    K = QQ.algebraic_field(sqrt(2))
    ddm = DDM([[
        K.convert(1 + sqrt(2)), K.convert(2 + sqrt(2))
    ], [K.convert(3 + sqrt(2)), K.convert(4 + sqrt(2))]], (2, 2), K)
    A = DomainMatrix.from_list_sympy(
        2,
        2, [[1 + sqrt(2), 2 + sqrt(2)], [3 + sqrt(2), 4 + sqrt(2)]],
        extension=True)
    assert A.rep == ddm
    assert A.shape == (2, 2)
    assert A.domain == K
Example #36
0
def _construct_algebraic(coeffs, opt):
    """We know that coefficients are algebraic so construct the extension. """
    from sympy.polys.numberfields import primitive_element

    result, exts = [], set([])

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

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

            exts.add(coeff)

            a = QQ.from_sympy(a)
            b = QQ.from_sympy(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
def _construct_algebraic(coeffs, opt):
    """We know that coefficients are algebraic so construct the extension. """
    from sympy.polys.numberfields import primitive_element

    result, exts = [], set([])

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

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

            exts.add(coeff)

            a = QQ.from_sympy(a)
            b = QQ.from_sympy(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 #38
0
def test_dup_factor_list():
    R, x = ring("x", ZZ)
    assert R.dup_factor_list(0) == (0, [])
    assert R.dup_factor_list(7) == (7, [])

    R, x = ring("x", QQ)
    assert R.dup_factor_list(0) == (0, [])
    assert R.dup_factor_list(QQ(1, 7)) == (QQ(1, 7), [])

    R, x = ring("x", ZZ["t"])
    assert R.dup_factor_list(0) == (DMP([], ZZ), [])
    assert R.dup_factor_list(DMP([ZZ(7)], ZZ)) == (DMP([ZZ(7)], ZZ), [])

    R, x = ring("x", QQ["t"])
    assert R.dup_factor_list(0) == (DMP([], QQ), [])
    assert R.dup_factor_list(DMP([QQ(1, 7)], QQ)) == (DMP([QQ(1, 7)], QQ), [])

    R, x = ring("x", ZZ)
    assert R.dup_factor_list_include(0) == [(0, 1)]
    assert R.dup_factor_list_include(7) == [(7, 1)]

    assert R.dup_factor_list(x ** 2 + 2 * x + 1) == (1, [(x + 1, 2)])
    assert R.dup_factor_list_include(x ** 2 + 2 * x + 1) == [(x + 1, 2)]

    R, x = ring("x", QQ)
    assert R.dup_factor_list(QQ(1, 2) * x ** 2 + x + QQ(1, 2)) == (QQ(1, 2), [(x + 1, 2)])

    R, x = ring("x", FF(2))
    assert R.dup_factor_list(x ** 2 + 1) == (1, [(x + 1, 2)])

    R, x = ring("x", RR)
    assert R.dup_factor_list(1.0 * x ** 2 + 2.0 * x + 1.0) == (1.0, [(1.0 * x + 1.0, 2)])
    assert R.dup_factor_list(2.0 * x ** 2 + 4.0 * x + 2.0) == (2.0, [(1.0 * x + 1.0, 2)])

    R, x = ring("x", ZZ["t"])
    f = DMP([ZZ(4), ZZ(0)], ZZ) * x ** 2 + DMP([ZZ(4), ZZ(0), ZZ(0)], ZZ) * x

    assert R.dup_factor_list(f) == (
        DMP([ZZ(4)], ZZ),
        [(DMP([ZZ(1), ZZ(0)], ZZ), 1), (DMP([ZZ(1)], ZZ) * x, 1), (DMP([ZZ(1)], ZZ) * x + DMP([ZZ(1), ZZ(0)], ZZ), 1)],
    )

    R, x = ring("x", QQ["t"])
    f = DMP([QQ(1, 2), QQ(0)], QQ) * x ** 2 + DMP([QQ(1, 2), QQ(0), QQ(0)], QQ) * x

    assert R.dup_factor_list(f) == (
        DMP([QQ(1, 2)], QQ),
        [
            (DMP([QQ(1), QQ(0)], QQ), 1),
            (DMP([QQ(1)], QQ) * x + DMP([], QQ), 1),
            (DMP([QQ(1)], QQ) * x + DMP([QQ(1), QQ(0)], QQ), 1),
        ],
    )

    R, x = ring("x", QQ.algebraic_field(I))

    def anp(element):
        return ANP(element, [QQ(1), QQ(0), QQ(1)], QQ)

    f = anp([QQ(1, 1)]) * x ** 4 + anp([QQ(2, 1)]) * x ** 2

    assert R.dup_factor_list(f) == (
        anp([QQ(1, 1)]),
        [(anp([QQ(1, 1)]) * x, 2), (anp([QQ(1, 1)]) * x ** 2 + anp([]) * x + anp([QQ(2, 1)]), 1)],
    )

    R, x = ring("x", EX)
    raises(DomainError, lambda: R.dup_factor_list(EX(sin(1))))
Example #39
0
def test_Domain_unify():
    F3 = GF(3)

    assert unify(F3, F3) == F3
    assert unify(F3, ZZ) == ZZ
    assert unify(F3, QQ) == QQ
    assert unify(F3, ALG) == ALG
    assert unify(F3, RR) == RR
    assert unify(F3, CC) == CC
    assert unify(F3, ZZ[x]) == ZZ[x]
    assert unify(F3, ZZ.frac_field(x)) == ZZ.frac_field(x)
    assert unify(F3, EX) == EX

    assert unify(ZZ, F3) == ZZ
    assert unify(ZZ, ZZ) == ZZ
    assert unify(ZZ, QQ) == QQ
    assert unify(ZZ, ALG) == ALG
    assert unify(ZZ, RR) == RR
    assert unify(ZZ, CC) == CC
    assert unify(ZZ, ZZ[x]) == ZZ[x]
    assert unify(ZZ, ZZ.frac_field(x)) == ZZ.frac_field(x)
    assert unify(ZZ, EX) == EX

    assert unify(QQ, F3) == QQ
    assert unify(QQ, ZZ) == QQ
    assert unify(QQ, QQ) == QQ
    assert unify(QQ, ALG) == ALG
    assert unify(QQ, RR) == RR
    assert unify(QQ, CC) == CC
    assert unify(QQ, ZZ[x]) == QQ[x]
    assert unify(QQ, ZZ.frac_field(x)) == QQ.frac_field(x)
    assert unify(QQ, EX) == EX

    assert unify(ZZ_I, F3) == ZZ_I
    assert unify(ZZ_I, ZZ) == ZZ_I
    assert unify(ZZ_I, ZZ_I) == ZZ_I
    assert unify(ZZ_I, QQ) == QQ_I
    assert unify(ZZ_I, ALG) == QQ.algebraic_field(I, sqrt(2), sqrt(3))
    assert unify(ZZ_I, RR) == CC
    assert unify(ZZ_I, CC) == CC
    assert unify(ZZ_I, ZZ[x]) == ZZ_I[x]
    assert unify(ZZ_I, ZZ_I[x]) == ZZ_I[x]
    assert unify(ZZ_I, ZZ.frac_field(x)) == ZZ_I.frac_field(x)
    assert unify(ZZ_I, ZZ_I.frac_field(x)) == ZZ_I.frac_field(x)
    assert unify(ZZ_I, EX) == EX

    assert unify(QQ_I, F3) == QQ_I
    assert unify(QQ_I, ZZ) == QQ_I
    assert unify(QQ_I, ZZ_I) == QQ_I
    assert unify(QQ_I, QQ) == QQ_I
    assert unify(QQ_I, ALG) == QQ.algebraic_field(I, sqrt(2), sqrt(3))
    assert unify(QQ_I, RR) == CC
    assert unify(QQ_I, CC) == CC
    assert unify(QQ_I, ZZ[x]) == QQ_I[x]
    assert unify(QQ_I, ZZ_I[x]) == QQ_I[x]
    assert unify(QQ_I, QQ[x]) == QQ_I[x]
    assert unify(QQ_I, QQ_I[x]) == QQ_I[x]
    assert unify(QQ_I, ZZ.frac_field(x)) == QQ_I.frac_field(x)
    assert unify(QQ_I, ZZ_I.frac_field(x)) == QQ_I.frac_field(x)
    assert unify(QQ_I, QQ.frac_field(x)) == QQ_I.frac_field(x)
    assert unify(QQ_I, QQ_I.frac_field(x)) == QQ_I.frac_field(x)
    assert unify(QQ_I, EX) == EX

    assert unify(RR, F3) == RR
    assert unify(RR, ZZ) == RR
    assert unify(RR, QQ) == RR
    assert unify(RR, ALG) == RR
    assert unify(RR, RR) == RR
    assert unify(RR, CC) == CC
    assert unify(RR, ZZ[x]) == RR[x]
    assert unify(RR, ZZ.frac_field(x)) == RR.frac_field(x)
    assert unify(RR, EX) == EX
    assert RR[x].unify(ZZ.frac_field(y)) == RR.frac_field(x, y)

    assert unify(CC, F3) == CC
    assert unify(CC, ZZ) == CC
    assert unify(CC, QQ) == CC
    assert unify(CC, ALG) == CC
    assert unify(CC, RR) == CC
    assert unify(CC, CC) == CC
    assert unify(CC, ZZ[x]) == CC[x]
    assert unify(CC, ZZ.frac_field(x)) == CC.frac_field(x)
    assert unify(CC, EX) == EX

    assert unify(ZZ[x], F3) == ZZ[x]
    assert unify(ZZ[x], ZZ) == ZZ[x]
    assert unify(ZZ[x], QQ) == QQ[x]
    assert unify(ZZ[x], ALG) == ALG[x]
    assert unify(ZZ[x], RR) == RR[x]
    assert unify(ZZ[x], CC) == CC[x]
    assert unify(ZZ[x], ZZ[x]) == ZZ[x]
    assert unify(ZZ[x], ZZ.frac_field(x)) == ZZ.frac_field(x)
    assert unify(ZZ[x], EX) == EX

    assert unify(ZZ.frac_field(x), F3) == ZZ.frac_field(x)
    assert unify(ZZ.frac_field(x), ZZ) == ZZ.frac_field(x)
    assert unify(ZZ.frac_field(x), QQ) == QQ.frac_field(x)
    assert unify(ZZ.frac_field(x), ALG) == ALG.frac_field(x)
    assert unify(ZZ.frac_field(x), RR) == RR.frac_field(x)
    assert unify(ZZ.frac_field(x), CC) == CC.frac_field(x)
    assert unify(ZZ.frac_field(x), ZZ[x]) == ZZ.frac_field(x)
    assert unify(ZZ.frac_field(x), ZZ.frac_field(x)) == ZZ.frac_field(x)
    assert unify(ZZ.frac_field(x), EX) == EX

    assert unify(EX, F3) == EX
    assert unify(EX, ZZ) == EX
    assert unify(EX, QQ) == EX
    assert unify(EX, ALG) == EX
    assert unify(EX, RR) == EX
    assert unify(EX, CC) == EX
    assert unify(EX, ZZ[x]) == EX
    assert unify(EX, ZZ.frac_field(x)) == EX
    assert unify(EX, EX) == EX
Example #40
0
def test_Domain__unify():
    assert ZZ.unify(ZZ) == ZZ
    assert QQ.unify(QQ) == QQ

    assert ZZ.unify(QQ) == QQ
    assert QQ.unify(ZZ) == QQ

    assert EX.unify(EX) == EX

    assert ZZ.unify(EX) == EX
    assert QQ.unify(EX) == EX
    assert EX.unify(ZZ) == EX
    assert EX.unify(QQ) == EX

    assert ZZ.poly_ring('x').unify(EX) == EX
    assert ZZ.frac_field('x').unify(EX) == EX
    assert EX.unify(ZZ.poly_ring('x')) == EX
    assert EX.unify(ZZ.frac_field('x')) == EX

    assert ZZ.poly_ring('x','y').unify(EX) == EX
    assert ZZ.frac_field('x','y').unify(EX) == EX
    assert EX.unify(ZZ.poly_ring('x','y')) == EX
    assert EX.unify(ZZ.frac_field('x','y')) == EX

    assert QQ.poly_ring('x').unify(EX) == EX
    assert QQ.frac_field('x').unify(EX) == EX
    assert EX.unify(QQ.poly_ring('x')) == EX
    assert EX.unify(QQ.frac_field('x')) == EX

    assert QQ.poly_ring('x','y').unify(EX) == EX
    assert QQ.frac_field('x','y').unify(EX) == EX
    assert EX.unify(QQ.poly_ring('x','y')) == EX
    assert EX.unify(QQ.frac_field('x','y')) == EX

    assert ZZ.poly_ring('x').unify(ZZ) == ZZ.poly_ring('x')
    assert ZZ.poly_ring('x').unify(QQ) == QQ.poly_ring('x')
    assert QQ.poly_ring('x').unify(ZZ) == QQ.poly_ring('x')
    assert QQ.poly_ring('x').unify(QQ) == QQ.poly_ring('x')

    assert ZZ.unify(ZZ.poly_ring('x')) == ZZ.poly_ring('x')
    assert QQ.unify(ZZ.poly_ring('x')) == QQ.poly_ring('x')
    assert ZZ.unify(QQ.poly_ring('x')) == QQ.poly_ring('x')
    assert QQ.unify(QQ.poly_ring('x')) == QQ.poly_ring('x')

    assert ZZ.poly_ring('x','y').unify(ZZ) == ZZ.poly_ring('x','y')
    assert ZZ.poly_ring('x','y').unify(QQ) == QQ.poly_ring('x','y')
    assert QQ.poly_ring('x','y').unify(ZZ) == QQ.poly_ring('x','y')
    assert QQ.poly_ring('x','y').unify(QQ) == QQ.poly_ring('x','y')

    assert ZZ.unify(ZZ.poly_ring('x','y')) == ZZ.poly_ring('x','y')
    assert QQ.unify(ZZ.poly_ring('x','y')) == QQ.poly_ring('x','y')
    assert ZZ.unify(QQ.poly_ring('x','y')) == QQ.poly_ring('x','y')
    assert QQ.unify(QQ.poly_ring('x','y')) == QQ.poly_ring('x','y')

    assert ZZ.frac_field('x').unify(ZZ) == ZZ.frac_field('x')
    assert ZZ.frac_field('x').unify(QQ) == EX # QQ.frac_field('x')
    assert QQ.frac_field('x').unify(ZZ) == EX # QQ.frac_field('x')
    assert QQ.frac_field('x').unify(QQ) == QQ.frac_field('x')

    assert ZZ.unify(ZZ.frac_field('x')) == ZZ.frac_field('x')
    assert QQ.unify(ZZ.frac_field('x')) == EX # QQ.frac_field('x')
    assert ZZ.unify(QQ.frac_field('x')) == EX # QQ.frac_field('x')
    assert QQ.unify(QQ.frac_field('x')) == QQ.frac_field('x')

    assert ZZ.frac_field('x','y').unify(ZZ) == ZZ.frac_field('x','y')
    assert ZZ.frac_field('x','y').unify(QQ) == EX # QQ.frac_field('x','y')
    assert QQ.frac_field('x','y').unify(ZZ) == EX # QQ.frac_field('x','y')
    assert QQ.frac_field('x','y').unify(QQ) == QQ.frac_field('x','y')

    assert ZZ.unify(ZZ.frac_field('x','y')) == ZZ.frac_field('x','y')
    assert QQ.unify(ZZ.frac_field('x','y')) == EX # QQ.frac_field('x','y')
    assert ZZ.unify(QQ.frac_field('x','y')) == EX # QQ.frac_field('x','y')
    assert QQ.unify(QQ.frac_field('x','y')) == QQ.frac_field('x','y')

    assert ZZ.poly_ring('x').unify(ZZ.poly_ring('x')) == ZZ.poly_ring('x')
    assert ZZ.poly_ring('x').unify(QQ.poly_ring('x')) == QQ.poly_ring('x')
    assert QQ.poly_ring('x').unify(ZZ.poly_ring('x')) == QQ.poly_ring('x')
    assert QQ.poly_ring('x').unify(QQ.poly_ring('x')) == QQ.poly_ring('x')

    assert ZZ.poly_ring('x','y').unify(ZZ.poly_ring('x')) == ZZ.poly_ring('x','y')
    assert ZZ.poly_ring('x','y').unify(QQ.poly_ring('x')) == QQ.poly_ring('x','y')
    assert QQ.poly_ring('x','y').unify(ZZ.poly_ring('x')) == QQ.poly_ring('x','y')
    assert QQ.poly_ring('x','y').unify(QQ.poly_ring('x')) == QQ.poly_ring('x','y')

    assert ZZ.poly_ring('x').unify(ZZ.poly_ring('x','y')) == ZZ.poly_ring('x','y')
    assert ZZ.poly_ring('x').unify(QQ.poly_ring('x','y')) == QQ.poly_ring('x','y')
    assert QQ.poly_ring('x').unify(ZZ.poly_ring('x','y')) == QQ.poly_ring('x','y')
    assert QQ.poly_ring('x').unify(QQ.poly_ring('x','y')) == QQ.poly_ring('x','y')

    assert ZZ.poly_ring('x','y').unify(ZZ.poly_ring('x','z')) == ZZ.poly_ring('x','y','z')
    assert ZZ.poly_ring('x','y').unify(QQ.poly_ring('x','z')) == QQ.poly_ring('x','y','z')
    assert QQ.poly_ring('x','y').unify(ZZ.poly_ring('x','z')) == QQ.poly_ring('x','y','z')
    assert QQ.poly_ring('x','y').unify(QQ.poly_ring('x','z')) == QQ.poly_ring('x','y','z')

    assert ZZ.frac_field('x').unify(ZZ.frac_field('x')) == ZZ.frac_field('x')
    assert ZZ.frac_field('x').unify(QQ.frac_field('x')) == QQ.frac_field('x')
    assert QQ.frac_field('x').unify(ZZ.frac_field('x')) == QQ.frac_field('x')
    assert QQ.frac_field('x').unify(QQ.frac_field('x')) == QQ.frac_field('x')

    assert ZZ.frac_field('x','y').unify(ZZ.frac_field('x')) == ZZ.frac_field('x','y')
    assert ZZ.frac_field('x','y').unify(QQ.frac_field('x')) == QQ.frac_field('x','y')
    assert QQ.frac_field('x','y').unify(ZZ.frac_field('x')) == QQ.frac_field('x','y')
    assert QQ.frac_field('x','y').unify(QQ.frac_field('x')) == QQ.frac_field('x','y')

    assert ZZ.frac_field('x').unify(ZZ.frac_field('x','y')) == ZZ.frac_field('x','y')
    assert ZZ.frac_field('x').unify(QQ.frac_field('x','y')) == QQ.frac_field('x','y')
    assert QQ.frac_field('x').unify(ZZ.frac_field('x','y')) == QQ.frac_field('x','y')
    assert QQ.frac_field('x').unify(QQ.frac_field('x','y')) == QQ.frac_field('x','y')

    assert ZZ.frac_field('x','y').unify(ZZ.frac_field('x','z')) == ZZ.frac_field('x','y','z')
    assert ZZ.frac_field('x','y').unify(QQ.frac_field('x','z')) == QQ.frac_field('x','y','z')
    assert QQ.frac_field('x','y').unify(ZZ.frac_field('x','z')) == QQ.frac_field('x','y','z')
    assert QQ.frac_field('x','y').unify(QQ.frac_field('x','z')) == QQ.frac_field('x','y','z')

    assert ZZ.poly_ring('x').unify(ZZ.frac_field('x')) == ZZ.frac_field('x')
    assert ZZ.poly_ring('x').unify(QQ.frac_field('x')) == EX # QQ.frac_field('x')
    assert QQ.poly_ring('x').unify(ZZ.frac_field('x')) == EX # QQ.frac_field('x')
    assert QQ.poly_ring('x').unify(QQ.frac_field('x')) == QQ.frac_field('x')

    assert ZZ.poly_ring('x','y').unify(ZZ.frac_field('x')) == ZZ.frac_field('x','y')
    assert ZZ.poly_ring('x','y').unify(QQ.frac_field('x')) == EX # QQ.frac_field('x','y')
    assert QQ.poly_ring('x','y').unify(ZZ.frac_field('x')) == EX # QQ.frac_field('x','y')
    assert QQ.poly_ring('x','y').unify(QQ.frac_field('x')) == QQ.frac_field('x','y')

    assert ZZ.poly_ring('x').unify(ZZ.frac_field('x','y')) == ZZ.frac_field('x','y')
    assert ZZ.poly_ring('x').unify(QQ.frac_field('x','y')) == EX # QQ.frac_field('x','y')
    assert QQ.poly_ring('x').unify(ZZ.frac_field('x','y')) == EX # QQ.frac_field('x','y')
    assert QQ.poly_ring('x').unify(QQ.frac_field('x','y')) == QQ.frac_field('x','y')

    assert ZZ.poly_ring('x','y').unify(ZZ.frac_field('x','z')) == ZZ.frac_field('x','y','z')
    assert ZZ.poly_ring('x','y').unify(QQ.frac_field('x','z')) == EX # QQ.frac_field('x','y','z')
    assert QQ.poly_ring('x','y').unify(ZZ.frac_field('x','z')) == EX # QQ.frac_field('x','y','z')
    assert QQ.poly_ring('x','y').unify(QQ.frac_field('x','z')) == QQ.frac_field('x','y','z')

    assert ZZ.frac_field('x').unify(ZZ.poly_ring('x')) == ZZ.frac_field('x')
    assert ZZ.frac_field('x').unify(QQ.poly_ring('x')) == EX # QQ.frac_field('x')
    assert QQ.frac_field('x').unify(ZZ.poly_ring('x')) == EX # QQ.frac_field('x')
    assert QQ.frac_field('x').unify(QQ.poly_ring('x')) == QQ.frac_field('x')

    assert ZZ.frac_field('x','y').unify(ZZ.poly_ring('x')) == ZZ.frac_field('x','y')
    assert ZZ.frac_field('x','y').unify(QQ.poly_ring('x')) == EX # QQ.frac_field('x','y')
    assert QQ.frac_field('x','y').unify(ZZ.poly_ring('x')) == EX # QQ.frac_field('x','y')
    assert QQ.frac_field('x','y').unify(QQ.poly_ring('x')) == QQ.frac_field('x','y')

    assert ZZ.frac_field('x').unify(ZZ.poly_ring('x','y')) == ZZ.frac_field('x','y')
    assert ZZ.frac_field('x').unify(QQ.poly_ring('x','y')) == EX # QQ.frac_field('x','y')
    assert QQ.frac_field('x').unify(ZZ.poly_ring('x','y')) == EX # QQ.frac_field('x','y')
    assert QQ.frac_field('x').unify(QQ.poly_ring('x','y')) == QQ.frac_field('x','y')

    assert ZZ.frac_field('x','y').unify(ZZ.poly_ring('x','z')) == ZZ.frac_field('x','y','z')
    assert ZZ.frac_field('x','y').unify(QQ.poly_ring('x','z')) == EX # QQ.frac_field('x','y','z')
    assert QQ.frac_field('x','y').unify(ZZ.poly_ring('x','z')) == EX # QQ.frac_field('x','y','z')
    assert QQ.frac_field('x','y').unify(QQ.poly_ring('x','z')) == QQ.frac_field('x','y','z')

    alg = QQ.algebraic_field(sqrt(5))

    assert alg.unify(alg['x','y']) == alg['x','y']
    assert alg['x','y'].unify(alg) == alg['x','y']

    assert alg.unify(alg.frac_field('x','y')) == alg.frac_field('x','y')
    assert alg.frac_field('x','y').unify(alg) == alg.frac_field('x','y')

    ext = QQ.algebraic_field(sqrt(7))

    raises(NotImplementedError, "alg.unify(ext)")

    raises(UnificationFailed, "ZZ.poly_ring('x','y').unify(ZZ, gens=('y', 'z'))")
    raises(UnificationFailed, "ZZ.unify(ZZ.poly_ring('x','y'), gens=('y', 'z'))")
Example #41
0
from sympy.polys.domains import (
    ZZ, QQ, RR, PythonRationalType as Q, ZZ_sympy, QQ_sympy,
    RR_mpmath, RR_sympy, PolynomialRing, FractionField, EX)

from sympy.polys.polyerrors import (
    UnificationFailed,
    GeneratorsNeeded,
    GeneratorsError,
    CoercionFailed,
    DomainError)

from sympy.polys.polyclasses import DMP, DMF
from sympy.utilities.pytest import raises

ALG = QQ.algebraic_field(sqrt(2) + sqrt(3))

def test_Domain__unify():
    assert ZZ.unify(ZZ) == ZZ
    assert QQ.unify(QQ) == QQ

    assert ZZ.unify(QQ) == QQ
    assert QQ.unify(ZZ) == QQ

    assert EX.unify(EX) == EX

    assert ZZ.unify(EX) == EX
    assert QQ.unify(EX) == EX
    assert EX.unify(ZZ) == EX
    assert EX.unify(QQ) == EX
Example #42
0
def test_dup_ext_factor():
    h = [QQ(1),QQ(0),QQ(1)]
    K = QQ.algebraic_field(I)

    assert dup_ext_factor([], K) == (ANP([], h, QQ), [])

    f = [ANP([QQ(1)], h, QQ), ANP([QQ(1)], h, QQ)]

    assert dup_ext_factor(f, K) == (ANP([QQ(1)], h, QQ), [(f, 1)])

    g = [ANP([QQ(2)], h, QQ), ANP([QQ(2)], h, QQ)]

    assert dup_ext_factor(g, K) == (ANP([QQ(2)], h, QQ), [(f, 1)])

    f = [ANP([QQ(7)], h, QQ), ANP([], h, QQ), ANP([], h, QQ), ANP([], h, QQ), ANP([QQ(1,1)], h, QQ)]
    g = [ANP([QQ(1)], h, QQ), ANP([], h, QQ), ANP([], h, QQ), ANP([], h, QQ), ANP([QQ(1,7)], h, QQ)]

    assert dup_ext_factor(f, K) == (ANP([QQ(7)], h, QQ), [(g, 1)])

    f = [ANP([QQ(1)], h, QQ), ANP([], h, QQ), ANP([], h, QQ), ANP([], h, QQ), ANP([QQ(1)], h, QQ)]

    assert dup_ext_factor(f, K) == \
        (ANP([QQ(1,1)], h, QQ), [
            ([ANP([QQ(1)], h, QQ), ANP([], h, QQ), ANP([QQ(-1),QQ(0)], h, QQ)], 1),
            ([ANP([QQ(1)], h, QQ), ANP([], h, QQ), ANP([QQ( 1),QQ(0)], h, QQ)], 1),
         ])

    f = [ANP([QQ(1)], h, QQ), ANP([], h, QQ), ANP([], h, QQ), ANP([], h, QQ), ANP([QQ(1)], h, QQ)]

    assert dup_ext_factor(f, K) == \
        (ANP([QQ(1,1)], h, QQ), [
            ([ANP([QQ(1)], h, QQ), ANP([], h, QQ), ANP([QQ(-1),QQ(0)], h, QQ)], 1),
            ([ANP([QQ(1)], h, QQ), ANP([], h, QQ), ANP([QQ( 1),QQ(0)], h, QQ)], 1),
         ])

    h = [QQ(1),QQ(0),QQ(-2)]
    K = QQ.algebraic_field(sqrt(2))

    f = [ANP([QQ(1)], h, QQ), ANP([], h, QQ), ANP([], h, QQ), ANP([], h, QQ), ANP([QQ(1,1)], h, QQ)]

    assert dup_ext_factor(f, K) == \
        (ANP([QQ(1)], h, QQ), [
            ([ANP([QQ(1)], h, QQ), ANP([QQ(-1),QQ(0)], h, QQ), ANP([QQ(1)], h, QQ)], 1),
            ([ANP([QQ(1)], h, QQ), ANP([QQ( 1),QQ(0)], h, QQ), ANP([QQ(1)], h, QQ)], 1),
         ])

    f = [ANP([QQ(1,1)], h, QQ), ANP([2,0], h, QQ), ANP([QQ(2,1)], h, QQ)]

    assert dup_ext_factor(f, K) == \
        (ANP([QQ(1,1)], h, QQ), [
            ([ANP([1], h, QQ), ANP([1,0], h, QQ)], 2),
        ])

    assert dup_ext_factor(dup_pow(f, 3, K), K) == \
        (ANP([QQ(1,1)], h, QQ), [
            ([ANP([1], h, QQ), ANP([1,0], h, QQ)], 6),
        ])

    f = dup_mul_ground(f, ANP([QQ(2,1)], h, QQ), K)

    assert dup_ext_factor(f, K) == \
        (ANP([QQ(2,1)], h, QQ), [
            ([ANP([1], h, QQ), ANP([1,0], h, QQ)], 2),
        ])

    assert dup_ext_factor(dup_pow(f, 3, K), K) == \
        (ANP([QQ(8,1)], h, QQ), [
            ([ANP([1], h, QQ), ANP([1,0], h, QQ)], 6),
        ])

    h = [QQ(1,1), QQ(0,1), QQ(1,1)]
    K = QQ.algebraic_field(I)

    f = [ANP([QQ(4,1)], h, QQ), ANP([], h, QQ), ANP([QQ(9,1)], h, QQ)]

    assert dup_ext_factor(f, K) == \
        (ANP([QQ(4,1)], h, QQ), [
            ([ANP([QQ(1,1)], h, QQ), ANP([-QQ(3,2), QQ(0,1)], h, QQ)], 1),
            ([ANP([QQ(1,1)], h, QQ), ANP([ QQ(3,2), QQ(0,1)], h, QQ)], 1),
        ])

    f = [ANP([QQ(4,1)], h, QQ), ANP([QQ(8,1)], h, QQ), ANP([QQ(77,1)], h, QQ), ANP([QQ(18,1)], h, QQ), ANP([QQ(153,1)], h, QQ)]

    assert dup_ext_factor(f, K) == \
        (ANP([QQ(4,1)], h, QQ), [
            ([ANP([QQ(1,1)], h, QQ), ANP([-QQ(4,1), QQ(1,1)], h, QQ)], 1),
            ([ANP([QQ(1,1)], h, QQ), ANP([-QQ(3,2), QQ(0,1)], h, QQ)], 1),
            ([ANP([QQ(1,1)], h, QQ), ANP([ QQ(3,2), QQ(0,1)], h, QQ)], 1),
            ([ANP([QQ(1,1)], h, QQ), ANP([ QQ(4,1), QQ(1,1)], h, QQ)], 1),
        ])
Example #43
0
def test_construct_domain():
    assert construct_domain([1, 2, 3]) == (ZZ, [ZZ(1), ZZ(2), ZZ(3)])
    assert construct_domain([1, 2, 3], field=True) == (QQ, [QQ(1), QQ(2), QQ(3)])

    assert construct_domain([S(1), S(2), S(3)]) == (ZZ, [ZZ(1), ZZ(2), ZZ(3)])
    assert construct_domain([S(1), S(2), S(3)], field=True) == (QQ, [QQ(1), QQ(2), QQ(3)])

    assert construct_domain([S(1)/2, S(2)]) == (QQ, [QQ(1, 2), QQ(2)])
    result = construct_domain([3.14, 1, S(1)/2])
    assert isinstance(result[0], RealField)
    assert result[1] == [RR(3.14), RR(1.0), RR(0.5)]

    assert construct_domain([3.14, sqrt(2)], extension=None) == (EX, [EX(3.14), EX(sqrt(2))])
    assert construct_domain([3.14, sqrt(2)], extension=True) == (EX, [EX(3.14), EX(sqrt(2))])

    assert construct_domain([1, sqrt(2)], extension=None) == (EX, [EX(1), EX(sqrt(2))])

    assert construct_domain([x, sqrt(x)]) == (EX, [EX(x), EX(sqrt(x))])
    assert construct_domain([x, sqrt(x), sqrt(y)]) == (EX, [EX(x), EX(sqrt(x)), EX(sqrt(y))])

    alg = QQ.algebraic_field(sqrt(2))

    assert construct_domain([7, S(1)/2, sqrt(2)], extension=True) == \
        (alg, [alg.convert(7), alg.convert(S(1)/2), alg.convert(sqrt(2))])

    alg = QQ.algebraic_field(sqrt(2) + sqrt(3))

    assert construct_domain([7, sqrt(2), sqrt(3)], extension=True) == \
        (alg, [alg.convert(7), alg.convert(sqrt(2)), alg.convert(sqrt(3))])

    dom = ZZ[x]

    assert construct_domain([2*x, 3]) == \
        (dom, [dom.convert(2*x), dom.convert(3)])

    dom = ZZ[x, y]

    assert construct_domain([2*x, 3*y]) == \
        (dom, [dom.convert(2*x), dom.convert(3*y)])

    dom = QQ[x]

    assert construct_domain([x/2, 3]) == \
        (dom, [dom.convert(x/2), dom.convert(3)])

    dom = QQ[x, y]

    assert construct_domain([x/2, 3*y]) == \
        (dom, [dom.convert(x/2), dom.convert(3*y)])

    dom = RR[x]

    assert construct_domain([x/2, 3.5]) == \
        (dom, [dom.convert(x/2), dom.convert(3.5)])

    dom = RR[x, y]

    assert construct_domain([x/2, 3.5*y]) == \
        (dom, [dom.convert(x/2), dom.convert(3.5*y)])

    dom = ZZ.frac_field(x)

    assert construct_domain([2/x, 3]) == \
        (dom, [dom.convert(2/x), dom.convert(3)])

    dom = ZZ.frac_field(x, y)

    assert construct_domain([2/x, 3*y]) == \
        (dom, [dom.convert(2/x), dom.convert(3*y)])

    dom = RR.frac_field(x)

    assert construct_domain([2/x, 3.5]) == \
        (dom, [dom.convert(2/x), dom.convert(3.5)])

    dom = RR.frac_field(x, y)

    assert construct_domain([2/x, 3.5*y]) == \
        (dom, [dom.convert(2/x), dom.convert(3.5*y)])

    dom = RealField(prec=336)[x]

    assert construct_domain([pi.evalf(100)*x]) == \
        (dom, [dom.convert(pi.evalf(100)*x)])

    assert construct_domain(2) == (ZZ, ZZ(2))
    assert construct_domain(S(2)/3) == (QQ, QQ(2, 3))

    assert construct_domain({}) == (ZZ, {})
Example #44
0
def test_construct_domain():
    assert construct_domain([1, 2, 3]) == (ZZ, [ZZ(1), ZZ(2), ZZ(3)])
    assert construct_domain(
        [1, 2, 3], field=True) == (QQ, [QQ(1), QQ(2), QQ(3)])

    assert construct_domain([S(1), S(2), S(3)]) == (ZZ, [ZZ(1), ZZ(2), ZZ(3)])
    assert construct_domain(
        [S(1), S(2), S(3)], field=True) == (QQ, [QQ(1), QQ(2), QQ(3)])

    assert construct_domain([S(1)/2, S(2)]) == (QQ, [QQ(1, 2), QQ(2)])
    assert construct_domain(
        [3.14, 1, S(1)/2]) == (RR, [RR(3.14), RR(1.0), RR(0.5)])

    assert construct_domain(
        [3.14, sqrt(2)], extension=None) == (EX, [EX(3.14), EX(sqrt(2))])
    assert construct_domain(
        [3.14, sqrt(2)], extension=True) == (EX, [EX(3.14), EX(sqrt(2))])

    assert construct_domain(
        [1, sqrt(2)], extension=None) == (EX, [EX(1), EX(sqrt(2))])

    alg = QQ.algebraic_field(sqrt(2))

    assert construct_domain([7, S(1)/2, sqrt(2)], extension=True) == \
        (alg, [alg.convert(7), alg.convert(S(1)/2), alg.convert(sqrt(2))])

    alg = QQ.algebraic_field(sqrt(2) + sqrt(3))

    assert construct_domain([7, sqrt(2), sqrt(3)], extension=True) == \
        (alg, [alg.convert(7), alg.convert(sqrt(2)), alg.convert(sqrt(3))])

    dom = ZZ[x]

    assert construct_domain([2*x, 3]) == \
        (dom, [dom.convert(2*x), dom.convert(3)])

    dom = ZZ[x, y]

    assert construct_domain([2*x, 3*y]) == \
        (dom, [dom.convert(2*x), dom.convert(3*y)])

    dom = QQ[x]

    assert construct_domain([x/2, 3]) == \
        (dom, [dom.convert(x/2), dom.convert(3)])

    dom = QQ[x, y]

    assert construct_domain([x/2, 3*y]) == \
        (dom, [dom.convert(x/2), dom.convert(3*y)])

    dom = RR[x]

    assert construct_domain([x/2, 3.5]) == \
        (dom, [dom.convert(x/2), dom.convert(3.5)])

    dom = RR[x, y]

    assert construct_domain([x/2, 3.5*y]) == \
        (dom, [dom.convert(x/2), dom.convert(3.5*y)])

    dom = ZZ.frac_field(x)

    assert construct_domain([2/x, 3]) == \
        (dom, [dom.convert(2/x), dom.convert(3)])

    dom = ZZ.frac_field(x, y)

    assert construct_domain([2/x, 3*y]) == \
        (dom, [dom.convert(2/x), dom.convert(3*y)])

    dom = RR.frac_field(x)

    assert construct_domain([2/x, 3.5]) == \
        (dom, [dom.convert(2/x), dom.convert(3.5)])

    dom = RR.frac_field(x, y)

    assert construct_domain([2/x, 3.5*y]) == \
        (dom, [dom.convert(2/x), dom.convert(3.5*y)])

    assert construct_domain(2) == (ZZ, ZZ(2))
    assert construct_domain(S(2)/3) == (QQ, QQ(2, 3))
Example #45
0
def test_dup_ext_factor():
    R, x = ring("x", QQ.algebraic_field(I))
    def anp(element):
        return ANP(element, [QQ(1), QQ(0), QQ(1)], QQ)

    assert R.dup_ext_factor(0) == (anp([]), [])

    f = anp([QQ(1)])*x + anp([QQ(1)])

    assert R.dup_ext_factor(f) == (anp([QQ(1)]), [(f, 1)])

    g = anp([QQ(2)])*x + anp([QQ(2)])

    assert R.dup_ext_factor(g) == (anp([QQ(2)]), [(f, 1)])

    f = anp([QQ(7)])*x**4 + anp([QQ(1, 1)])
    g = anp([QQ(1)])*x**4 + anp([QQ(1, 7)])

    assert R.dup_ext_factor(f) == (anp([QQ(7)]), [(g, 1)])

    f = anp([QQ(1)])*x**4 + anp([QQ(1)])

    assert R.dup_ext_factor(f) == \
        (anp([QQ(1, 1)]), [(anp([QQ(1)])*x**2 + anp([QQ(-1), QQ(0)]), 1),
                           (anp([QQ(1)])*x**2 + anp([QQ( 1), QQ(0)]), 1)])

    f = anp([QQ(4, 1)])*x**2 + anp([QQ(9, 1)])

    assert R.dup_ext_factor(f) == \
        (anp([QQ(4, 1)]), [(anp([QQ(1, 1)])*x + anp([-QQ(3, 2), QQ(0, 1)]), 1),
                           (anp([QQ(1, 1)])*x + anp([ QQ(3, 2), QQ(0, 1)]), 1)])

    f = anp([QQ(4, 1)])*x**4 + anp([QQ(8, 1)])*x**3 + anp([QQ(77, 1)])*x**2 + anp([QQ(18, 1)])*x + anp([QQ(153, 1)])

    assert R.dup_ext_factor(f) == \
        (anp([QQ(4, 1)]), [(anp([QQ(1, 1)])*x + anp([-QQ(4, 1), QQ(1, 1)]), 1),
                           (anp([QQ(1, 1)])*x + anp([-QQ(3, 2), QQ(0, 1)]), 1),
                           (anp([QQ(1, 1)])*x + anp([ QQ(3, 2), QQ(0, 1)]), 1),
                           (anp([QQ(1, 1)])*x + anp([ QQ(4, 1), QQ(1, 1)]), 1)])

    R, x = ring("x", QQ.algebraic_field(sqrt(2)))
    def anp(element):
        return ANP(element, [QQ(1), QQ(0), QQ(-2)], QQ)

    f = anp([QQ(1)])*x**4 + anp([QQ(1, 1)])

    assert R.dup_ext_factor(f) == \
        (anp([QQ(1)]), [(anp([QQ(1)])*x**2 + anp([QQ(-1), QQ(0)])*x + anp([QQ(1)]), 1),
                        (anp([QQ(1)])*x**2 + anp([QQ( 1), QQ(0)])*x + anp([QQ(1)]), 1)])

    f = anp([QQ(1, 1)])*x**2 + anp([QQ(2), QQ(0)])*x + anp([QQ(2, 1)])

    assert R.dup_ext_factor(f) == \
        (anp([QQ(1, 1)]), [(anp([1])*x + anp([1, 0]), 2)])

    assert R.dup_ext_factor(f**3) == \
        (anp([QQ(1, 1)]), [(anp([1])*x + anp([1, 0]), 6)])

    f *= anp([QQ(2, 1)])

    assert R.dup_ext_factor(f) == \
        (anp([QQ(2, 1)]), [(anp([1])*x + anp([1, 0]), 2)])

    assert R.dup_ext_factor(f**3) == \
        (anp([QQ(8, 1)]), [(anp([1])*x + anp([1, 0]), 6)])
Example #46
0
from sympy.abc import x, y, z

from sympy.polys.domains import (ZZ, QQ, RR, FF, PythonRationalType as Q,
                                 ZZ_sympy, QQ_sympy, RR_mpmath, RR_sympy,
                                 PolynomialRing, FractionField, EX)

from sympy.polys.domains.modularinteger import ModularIntegerFactory

from sympy.polys.polyerrors import (UnificationFailed, GeneratorsNeeded,
                                    GeneratorsError, CoercionFailed,
                                    NotInvertible, DomainError)

from sympy.polys.polyclasses import DMP, DMF
from sympy.utilities.pytest import raises

ALG = QQ.algebraic_field(sqrt(2) + sqrt(3))


def test_Domain__unify():
    assert ZZ.unify(ZZ) == ZZ
    assert QQ.unify(QQ) == QQ

    assert ZZ.unify(QQ) == QQ
    assert QQ.unify(ZZ) == QQ

    assert EX.unify(EX) == EX

    assert ZZ.unify(EX) == EX
    assert QQ.unify(EX) == EX
    assert EX.unify(ZZ) == EX
    assert EX.unify(QQ) == EX
Example #47
0
def test_dup_ext_factor():
    R, x = ring("x", QQ.algebraic_field(I))
    def anp(element):
        return ANP(element, [QQ(1), QQ(0), QQ(1)], QQ)

    assert R.dup_ext_factor(0) == (anp([]), [])

    f = anp([QQ(1)])*x + anp([QQ(1)])

    assert R.dup_ext_factor(f) == (anp([QQ(1)]), [(f, 1)])

    g = anp([QQ(2)])*x + anp([QQ(2)])

    assert R.dup_ext_factor(g) == (anp([QQ(2)]), [(f, 1)])

    f = anp([QQ(7)])*x**4 + anp([QQ(1, 1)])
    g = anp([QQ(1)])*x**4 + anp([QQ(1, 7)])

    assert R.dup_ext_factor(f) == (anp([QQ(7)]), [(g, 1)])

    f = anp([QQ(1)])*x**4 + anp([QQ(1)])

    assert R.dup_ext_factor(f) == \
        (anp([QQ(1, 1)]), [(anp([QQ(1)])*x**2 + anp([QQ(-1), QQ(0)]), 1),
                           (anp([QQ(1)])*x**2 + anp([QQ( 1), QQ(0)]), 1)])

    f = anp([QQ(4, 1)])*x**2 + anp([QQ(9, 1)])

    assert R.dup_ext_factor(f) == \
        (anp([QQ(4, 1)]), [(anp([QQ(1, 1)])*x + anp([-QQ(3, 2), QQ(0, 1)]), 1),
                           (anp([QQ(1, 1)])*x + anp([ QQ(3, 2), QQ(0, 1)]), 1)])

    f = anp([QQ(4, 1)])*x**4 + anp([QQ(8, 1)])*x**3 + anp([QQ(77, 1)])*x**2 + anp([QQ(18, 1)])*x + anp([QQ(153, 1)])

    assert R.dup_ext_factor(f) == \
        (anp([QQ(4, 1)]), [(anp([QQ(1, 1)])*x + anp([-QQ(4, 1), QQ(1, 1)]), 1),
                           (anp([QQ(1, 1)])*x + anp([-QQ(3, 2), QQ(0, 1)]), 1),
                           (anp([QQ(1, 1)])*x + anp([ QQ(3, 2), QQ(0, 1)]), 1),
                           (anp([QQ(1, 1)])*x + anp([ QQ(4, 1), QQ(1, 1)]), 1)])

    R, x = ring("x", QQ.algebraic_field(sqrt(2)))
    def anp(element):
        return ANP(element, [QQ(1), QQ(0), QQ(-2)], QQ)

    f = anp([QQ(1)])*x**4 + anp([QQ(1, 1)])

    assert R.dup_ext_factor(f) == \
        (anp([QQ(1)]), [(anp([QQ(1)])*x**2 + anp([QQ(-1), QQ(0)])*x + anp([QQ(1)]), 1),
                        (anp([QQ(1)])*x**2 + anp([QQ( 1), QQ(0)])*x + anp([QQ(1)]), 1)])

    f = anp([QQ(1, 1)])*x**2 + anp([QQ(2), QQ(0)])*x + anp([QQ(2, 1)])

    assert R.dup_ext_factor(f) == \
        (anp([QQ(1, 1)]), [(anp([1])*x + anp([1, 0]), 2)])

    assert R.dup_ext_factor(f**3) == \
        (anp([QQ(1, 1)]), [(anp([1])*x + anp([1, 0]), 6)])

    f *= anp([QQ(2, 1)])

    assert R.dup_ext_factor(f) == \
        (anp([QQ(2, 1)]), [(anp([1])*x + anp([1, 0]), 2)])

    assert R.dup_ext_factor(f**3) == \
        (anp([QQ(8, 1)]), [(anp([1])*x + anp([1, 0]), 6)])
Example #48
0
def test_Gaussian_postprocess():
    opt = {"gaussian": True}
    Gaussian.postprocess(opt)

    assert opt == {"gaussian": True, "extension": set([I]), "domain": QQ.algebraic_field(I)}
Example #49
0
def test_dup_factor_list():
    R, x = ring("x", ZZ)
    assert R.dup_factor_list(0) == (0, [])
    assert R.dup_factor_list(7) == (7, [])

    R, x = ring("x", QQ)
    assert R.dup_factor_list(0) == (0, [])
    assert R.dup_factor_list(QQ(1, 7)) == (QQ(1, 7), [])

    R, x = ring("x", ZZ['t'])
    assert R.dup_factor_list(0) == (0, [])
    assert R.dup_factor_list(7) == (7, [])

    R, x = ring("x", QQ['t'])
    assert R.dup_factor_list(0) == (0, [])
    assert R.dup_factor_list(QQ(1, 7)) == (QQ(1, 7), [])

    R, x = ring("x", ZZ)
    assert R.dup_factor_list_include(0) == [(0, 1)]
    assert R.dup_factor_list_include(7) == [(7, 1)]

    assert R.dup_factor_list(x**2 + 2*x + 1) == (1, [(x + 1, 2)])
    assert R.dup_factor_list_include(x**2 + 2*x + 1) == [(x + 1, 2)]

    R, x = ring("x", QQ)
    assert R.dup_factor_list(QQ(1,2)*x**2 + x + QQ(1,2)) == (QQ(1, 2), [(x + 1, 2)])

    R, x = ring("x", FF(2))
    assert R.dup_factor_list(x**2 + 1) == (1, [(x + 1, 2)])

    R, x = ring("x", RR)
    assert R.dup_factor_list(1.0*x**2 + 2.0*x + 1.0) == (1.0, [(1.0*x + 1.0, 2)])
    assert R.dup_factor_list(2.0*x**2 + 4.0*x + 2.0) == (2.0, [(1.0*x + 1.0, 2)])

    f = 6.7225336055071*x**2 - 10.6463972754741*x - 0.33469524022264
    coeff, factors = R.dup_factor_list(f)
    assert coeff == RR(1.0) and len(factors) == 1 and factors[0][0].almosteq(f, 1e-10) and factors[0][1] == 1

    Rt, t = ring("t", ZZ)
    R, x = ring("x", Rt)

    f = 4*t*x**2 + 4*t**2*x

    assert R.dup_factor_list(f) == \
        (4*t, [(x, 1),
             (x + t, 1)])

    Rt, t = ring("t", QQ)
    R, x = ring("x", Rt)

    f = QQ(1, 2)*t*x**2 + QQ(1, 2)*t**2*x

    assert R.dup_factor_list(f) == \
        (QQ(1, 2)*t, [(x, 1),
                    (x + t, 1)])

    R, x = ring("x", QQ.algebraic_field(I))
    def anp(element):
        return ANP(element, [QQ(1), QQ(0), QQ(1)], QQ)

    f = anp([QQ(1, 1)])*x**4 + anp([QQ(2, 1)])*x**2

    assert R.dup_factor_list(f) == \
        (anp([QQ(1, 1)]), [(anp([QQ(1, 1)])*x, 2),
                           (anp([QQ(1, 1)])*x**2 + anp([])*x + anp([QQ(2, 1)]), 1)])

    R, x = ring("x", EX)
    raises(DomainError, lambda: R.dup_factor_list(EX(sin(1))))
Example #50
0
def test_Domain__unify():
    assert ZZ.unify(ZZ) == ZZ
    assert QQ.unify(QQ) == QQ

    assert ZZ.unify(QQ) == QQ
    assert QQ.unify(ZZ) == QQ

    assert EX.unify(EX) == EX

    assert ZZ.unify(EX) == EX
    assert QQ.unify(EX) == EX
    assert EX.unify(ZZ) == EX
    assert EX.unify(QQ) == EX

    assert ZZ.poly_ring('x').unify(EX) == EX
    assert ZZ.frac_field('x').unify(EX) == EX
    assert EX.unify(ZZ.poly_ring('x')) == EX
    assert EX.unify(ZZ.frac_field('x')) == EX

    assert ZZ.poly_ring('x', 'y').unify(EX) == EX
    assert ZZ.frac_field('x', 'y').unify(EX) == EX
    assert EX.unify(ZZ.poly_ring('x', 'y')) == EX
    assert EX.unify(ZZ.frac_field('x', 'y')) == EX

    assert QQ.poly_ring('x').unify(EX) == EX
    assert QQ.frac_field('x').unify(EX) == EX
    assert EX.unify(QQ.poly_ring('x')) == EX
    assert EX.unify(QQ.frac_field('x')) == EX

    assert QQ.poly_ring('x', 'y').unify(EX) == EX
    assert QQ.frac_field('x', 'y').unify(EX) == EX
    assert EX.unify(QQ.poly_ring('x', 'y')) == EX
    assert EX.unify(QQ.frac_field('x', 'y')) == EX

    assert ZZ.poly_ring('x').unify(ZZ) == ZZ.poly_ring('x')
    assert ZZ.poly_ring('x').unify(QQ) == QQ.poly_ring('x')
    assert QQ.poly_ring('x').unify(ZZ) == QQ.poly_ring('x')
    assert QQ.poly_ring('x').unify(QQ) == QQ.poly_ring('x')

    assert ZZ.unify(ZZ.poly_ring('x')) == ZZ.poly_ring('x')
    assert QQ.unify(ZZ.poly_ring('x')) == QQ.poly_ring('x')
    assert ZZ.unify(QQ.poly_ring('x')) == QQ.poly_ring('x')
    assert QQ.unify(QQ.poly_ring('x')) == QQ.poly_ring('x')

    assert ZZ.poly_ring('x', 'y').unify(ZZ) == ZZ.poly_ring('x', 'y')
    assert ZZ.poly_ring('x', 'y').unify(QQ) == QQ.poly_ring('x', 'y')
    assert QQ.poly_ring('x', 'y').unify(ZZ) == QQ.poly_ring('x', 'y')
    assert QQ.poly_ring('x', 'y').unify(QQ) == QQ.poly_ring('x', 'y')

    assert ZZ.unify(ZZ.poly_ring('x', 'y')) == ZZ.poly_ring('x', 'y')
    assert QQ.unify(ZZ.poly_ring('x', 'y')) == QQ.poly_ring('x', 'y')
    assert ZZ.unify(QQ.poly_ring('x', 'y')) == QQ.poly_ring('x', 'y')
    assert QQ.unify(QQ.poly_ring('x', 'y')) == QQ.poly_ring('x', 'y')

    assert ZZ.frac_field('x').unify(ZZ) == ZZ.frac_field('x')
    assert ZZ.frac_field('x').unify(QQ) == EX  # QQ.frac_field('x')
    assert QQ.frac_field('x').unify(ZZ) == EX  # QQ.frac_field('x')
    assert QQ.frac_field('x').unify(QQ) == QQ.frac_field('x')

    assert ZZ.unify(ZZ.frac_field('x')) == ZZ.frac_field('x')
    assert QQ.unify(ZZ.frac_field('x')) == EX  # QQ.frac_field('x')
    assert ZZ.unify(QQ.frac_field('x')) == EX  # QQ.frac_field('x')
    assert QQ.unify(QQ.frac_field('x')) == QQ.frac_field('x')

    assert ZZ.frac_field('x', 'y').unify(ZZ) == ZZ.frac_field('x', 'y')
    assert ZZ.frac_field('x', 'y').unify(QQ) == EX  # QQ.frac_field('x','y')
    assert QQ.frac_field('x', 'y').unify(ZZ) == EX  # QQ.frac_field('x','y')
    assert QQ.frac_field('x', 'y').unify(QQ) == QQ.frac_field('x', 'y')

    assert ZZ.unify(ZZ.frac_field('x', 'y')) == ZZ.frac_field('x', 'y')
    assert QQ.unify(ZZ.frac_field('x', 'y')) == EX  # QQ.frac_field('x','y')
    assert ZZ.unify(QQ.frac_field('x', 'y')) == EX  # QQ.frac_field('x','y')
    assert QQ.unify(QQ.frac_field('x', 'y')) == QQ.frac_field('x', 'y')

    assert ZZ.poly_ring('x').unify(ZZ.poly_ring('x')) == ZZ.poly_ring('x')
    assert ZZ.poly_ring('x').unify(QQ.poly_ring('x')) == QQ.poly_ring('x')
    assert QQ.poly_ring('x').unify(ZZ.poly_ring('x')) == QQ.poly_ring('x')
    assert QQ.poly_ring('x').unify(QQ.poly_ring('x')) == QQ.poly_ring('x')

    assert ZZ.poly_ring('x', 'y').unify(ZZ.poly_ring('x')) == ZZ.poly_ring(
        'x', 'y')
    assert ZZ.poly_ring('x', 'y').unify(QQ.poly_ring('x')) == QQ.poly_ring(
        'x', 'y')
    assert QQ.poly_ring('x', 'y').unify(ZZ.poly_ring('x')) == QQ.poly_ring(
        'x', 'y')
    assert QQ.poly_ring('x', 'y').unify(QQ.poly_ring('x')) == QQ.poly_ring(
        'x', 'y')

    assert ZZ.poly_ring('x').unify(ZZ.poly_ring('x', 'y')) == ZZ.poly_ring(
        'x', 'y')
    assert ZZ.poly_ring('x').unify(QQ.poly_ring('x', 'y')) == QQ.poly_ring(
        'x', 'y')
    assert QQ.poly_ring('x').unify(ZZ.poly_ring('x', 'y')) == QQ.poly_ring(
        'x', 'y')
    assert QQ.poly_ring('x').unify(QQ.poly_ring('x', 'y')) == QQ.poly_ring(
        'x', 'y')

    assert ZZ.poly_ring('x', 'y').unify(ZZ.poly_ring('x',
                                                     'z')) == ZZ.poly_ring(
                                                         'x', 'y', 'z')
    assert ZZ.poly_ring('x', 'y').unify(QQ.poly_ring('x',
                                                     'z')) == QQ.poly_ring(
                                                         'x', 'y', 'z')
    assert QQ.poly_ring('x', 'y').unify(ZZ.poly_ring('x',
                                                     'z')) == QQ.poly_ring(
                                                         'x', 'y', 'z')
    assert QQ.poly_ring('x', 'y').unify(QQ.poly_ring('x',
                                                     'z')) == QQ.poly_ring(
                                                         'x', 'y', 'z')

    assert ZZ.frac_field('x').unify(ZZ.frac_field('x')) == ZZ.frac_field('x')
    assert ZZ.frac_field('x').unify(QQ.frac_field('x')) == QQ.frac_field('x')
    assert QQ.frac_field('x').unify(ZZ.frac_field('x')) == QQ.frac_field('x')
    assert QQ.frac_field('x').unify(QQ.frac_field('x')) == QQ.frac_field('x')

    assert ZZ.frac_field('x', 'y').unify(ZZ.frac_field('x')) == ZZ.frac_field(
        'x', 'y')
    assert ZZ.frac_field('x', 'y').unify(QQ.frac_field('x')) == QQ.frac_field(
        'x', 'y')
    assert QQ.frac_field('x', 'y').unify(ZZ.frac_field('x')) == QQ.frac_field(
        'x', 'y')
    assert QQ.frac_field('x', 'y').unify(QQ.frac_field('x')) == QQ.frac_field(
        'x', 'y')

    assert ZZ.frac_field('x').unify(ZZ.frac_field('x', 'y')) == ZZ.frac_field(
        'x', 'y')
    assert ZZ.frac_field('x').unify(QQ.frac_field('x', 'y')) == QQ.frac_field(
        'x', 'y')
    assert QQ.frac_field('x').unify(ZZ.frac_field('x', 'y')) == QQ.frac_field(
        'x', 'y')
    assert QQ.frac_field('x').unify(QQ.frac_field('x', 'y')) == QQ.frac_field(
        'x', 'y')

    assert ZZ.frac_field('x', 'y').unify(ZZ.frac_field('x',
                                                       'z')) == ZZ.frac_field(
                                                           'x', 'y', 'z')
    assert ZZ.frac_field('x', 'y').unify(QQ.frac_field('x',
                                                       'z')) == QQ.frac_field(
                                                           'x', 'y', 'z')
    assert QQ.frac_field('x', 'y').unify(ZZ.frac_field('x',
                                                       'z')) == QQ.frac_field(
                                                           'x', 'y', 'z')
    assert QQ.frac_field('x', 'y').unify(QQ.frac_field('x',
                                                       'z')) == QQ.frac_field(
                                                           'x', 'y', 'z')

    assert ZZ.poly_ring('x').unify(ZZ.frac_field('x')) == ZZ.frac_field('x')
    assert ZZ.poly_ring('x').unify(
        QQ.frac_field('x')) == EX  # QQ.frac_field('x')
    assert QQ.poly_ring('x').unify(
        ZZ.frac_field('x')) == EX  # QQ.frac_field('x')
    assert QQ.poly_ring('x').unify(QQ.frac_field('x')) == QQ.frac_field('x')

    assert ZZ.poly_ring('x', 'y').unify(ZZ.frac_field('x')) == ZZ.frac_field(
        'x', 'y')
    assert ZZ.poly_ring('x', 'y').unify(
        QQ.frac_field('x')) == EX  # QQ.frac_field('x','y')
    assert QQ.poly_ring('x', 'y').unify(
        ZZ.frac_field('x')) == EX  # QQ.frac_field('x','y')
    assert QQ.poly_ring('x', 'y').unify(QQ.frac_field('x')) == QQ.frac_field(
        'x', 'y')

    assert ZZ.poly_ring('x').unify(ZZ.frac_field('x', 'y')) == ZZ.frac_field(
        'x', 'y')
    assert ZZ.poly_ring('x').unify(QQ.frac_field(
        'x', 'y')) == EX  # QQ.frac_field('x','y')
    assert QQ.poly_ring('x').unify(ZZ.frac_field(
        'x', 'y')) == EX  # QQ.frac_field('x','y')
    assert QQ.poly_ring('x').unify(QQ.frac_field('x', 'y')) == QQ.frac_field(
        'x', 'y')

    assert ZZ.poly_ring('x', 'y').unify(ZZ.frac_field('x',
                                                      'z')) == ZZ.frac_field(
                                                          'x', 'y', 'z')
    assert ZZ.poly_ring('x', 'y').unify(QQ.frac_field(
        'x', 'z')) == EX  # QQ.frac_field('x','y','z')
    assert QQ.poly_ring('x', 'y').unify(ZZ.frac_field(
        'x', 'z')) == EX  # QQ.frac_field('x','y','z')
    assert QQ.poly_ring('x', 'y').unify(QQ.frac_field('x',
                                                      'z')) == QQ.frac_field(
                                                          'x', 'y', 'z')

    assert ZZ.frac_field('x').unify(ZZ.poly_ring('x')) == ZZ.frac_field('x')
    assert ZZ.frac_field('x').unify(
        QQ.poly_ring('x')) == EX  # QQ.frac_field('x')
    assert QQ.frac_field('x').unify(
        ZZ.poly_ring('x')) == EX  # QQ.frac_field('x')
    assert QQ.frac_field('x').unify(QQ.poly_ring('x')) == QQ.frac_field('x')

    assert ZZ.frac_field('x', 'y').unify(ZZ.poly_ring('x')) == ZZ.frac_field(
        'x', 'y')
    assert ZZ.frac_field('x', 'y').unify(
        QQ.poly_ring('x')) == EX  # QQ.frac_field('x','y')
    assert QQ.frac_field('x', 'y').unify(
        ZZ.poly_ring('x')) == EX  # QQ.frac_field('x','y')
    assert QQ.frac_field('x', 'y').unify(QQ.poly_ring('x')) == QQ.frac_field(
        'x', 'y')

    assert ZZ.frac_field('x').unify(ZZ.poly_ring('x', 'y')) == ZZ.frac_field(
        'x', 'y')
    assert ZZ.frac_field('x').unify(QQ.poly_ring(
        'x', 'y')) == EX  # QQ.frac_field('x','y')
    assert QQ.frac_field('x').unify(ZZ.poly_ring(
        'x', 'y')) == EX  # QQ.frac_field('x','y')
    assert QQ.frac_field('x').unify(QQ.poly_ring('x', 'y')) == QQ.frac_field(
        'x', 'y')

    assert ZZ.frac_field('x', 'y').unify(ZZ.poly_ring('x',
                                                      'z')) == ZZ.frac_field(
                                                          'x', 'y', 'z')
    assert ZZ.frac_field('x', 'y').unify(QQ.poly_ring(
        'x', 'z')) == EX  # QQ.frac_field('x','y','z')
    assert QQ.frac_field('x', 'y').unify(ZZ.poly_ring(
        'x', 'z')) == EX  # QQ.frac_field('x','y','z')
    assert QQ.frac_field('x', 'y').unify(QQ.poly_ring('x',
                                                      'z')) == QQ.frac_field(
                                                          'x', 'y', 'z')

    alg = QQ.algebraic_field(sqrt(5))

    assert alg.unify(alg['x', 'y']) == alg['x', 'y']
    assert alg['x', 'y'].unify(alg) == alg['x', 'y']

    assert alg.unify(alg.frac_field('x', 'y')) == alg.frac_field('x', 'y')
    assert alg.frac_field('x', 'y').unify(alg) == alg.frac_field('x', 'y')

    ext = QQ.algebraic_field(sqrt(7))

    raises(NotImplementedError, "alg.unify(ext)")

    raises(UnificationFailed,
           "ZZ.poly_ring('x','y').unify(ZZ, gens=('y', 'z'))")
    raises(UnificationFailed,
           "ZZ.unify(ZZ.poly_ring('x','y'), gens=('y', 'z'))")
Example #51
0
def test_dup_factor_list():
    R, x = ring("x", ZZ)
    assert R.dup_factor_list(0) == (0, [])
    assert R.dup_factor_list(7) == (7, [])

    R, x = ring("x", QQ)
    assert R.dup_factor_list(0) == (0, [])
    assert R.dup_factor_list(QQ(1, 7)) == (QQ(1, 7), [])

    R, x = ring("x", ZZ['t'])
    assert R.dup_factor_list(0) == (0, [])
    assert R.dup_factor_list(7) == (7, [])

    R, x = ring("x", QQ['t'])
    assert R.dup_factor_list(0) == (0, [])
    assert R.dup_factor_list(QQ(1, 7)) == (QQ(1, 7), [])

    R, x = ring("x", ZZ)
    assert R.dup_factor_list_include(0) == [(0, 1)]
    assert R.dup_factor_list_include(7) == [(7, 1)]

    assert R.dup_factor_list(x**2 + 2*x + 1) == (1, [(x + 1, 2)])
    assert R.dup_factor_list_include(x**2 + 2*x + 1) == [(x + 1, 2)]

    R, x = ring("x", QQ)
    assert R.dup_factor_list(QQ(1,2)*x**2 + x + QQ(1,2)) == (QQ(1, 2), [(x + 1, 2)])

    R, x = ring("x", FF(2))
    assert R.dup_factor_list(x**2 + 1) == (1, [(x + 1, 2)])

    R, x = ring("x", RR)
    assert R.dup_factor_list(1.0*x**2 + 2.0*x + 1.0) == (1.0, [(1.0*x + 1.0, 2)])
    assert R.dup_factor_list(2.0*x**2 + 4.0*x + 2.0) == (2.0, [(1.0*x + 1.0, 2)])

    f = 6.7225336055071*x**2 - 10.6463972754741*x - 0.33469524022264
    coeff, factors = R.dup_factor_list(f)
    assert coeff == RR(1.0) and len(factors) == 1 and factors[0][0].almosteq(f, 1e-10) and factors[0][1] == 1

    Rt, t = ring("t", ZZ)
    R, x = ring("x", Rt)

    f = 4*t*x**2 + 4*t**2*x

    assert R.dup_factor_list(f) == \
        (4*t, [(x, 1),
             (x + t, 1)])

    Rt, t = ring("t", QQ)
    R, x = ring("x", Rt)

    f = QQ(1, 2)*t*x**2 + QQ(1, 2)*t**2*x

    assert R.dup_factor_list(f) == \
        (QQ(1, 2)*t, [(x, 1),
                    (x + t, 1)])

    R, x = ring("x", QQ.algebraic_field(I))
    def anp(element):
        return ANP(element, [QQ(1), QQ(0), QQ(1)], QQ)

    f = anp([QQ(1, 1)])*x**4 + anp([QQ(2, 1)])*x**2

    assert R.dup_factor_list(f) == \
        (anp([QQ(1, 1)]), [(anp([QQ(1, 1)])*x, 2),
                           (anp([QQ(1, 1)])*x**2 + anp([])*x + anp([QQ(2, 1)]), 1)])

    R, x = ring("x", EX)
    raises(DomainError, lambda: R.dup_factor_list(EX(sin(1))))
Example #52
0
def test_Domain__unify():
    assert ZZ.unify(ZZ) == ZZ
    assert QQ.unify(QQ) == QQ

    assert ZZ.unify(QQ) == QQ
    assert QQ.unify(ZZ) == QQ

    assert EX.unify(EX) == EX

    assert ZZ.unify(EX) == EX
    assert QQ.unify(EX) == EX
    assert EX.unify(ZZ) == EX
    assert EX.unify(QQ) == EX

    assert ZZ.poly_ring(x).unify(EX) == EX
    assert ZZ.frac_field(x).unify(EX) == EX
    assert EX.unify(ZZ.poly_ring(x)) == EX
    assert EX.unify(ZZ.frac_field(x)) == EX

    assert ZZ.poly_ring(x, y).unify(EX) == EX
    assert ZZ.frac_field(x, y).unify(EX) == EX
    assert EX.unify(ZZ.poly_ring(x, y)) == EX
    assert EX.unify(ZZ.frac_field(x, y)) == EX

    assert QQ.poly_ring(x).unify(EX) == EX
    assert QQ.frac_field(x).unify(EX) == EX
    assert EX.unify(QQ.poly_ring(x)) == EX
    assert EX.unify(QQ.frac_field(x)) == EX

    assert QQ.poly_ring(x, y).unify(EX) == EX
    assert QQ.frac_field(x, y).unify(EX) == EX
    assert EX.unify(QQ.poly_ring(x, y)) == EX
    assert EX.unify(QQ.frac_field(x, y)) == EX

    assert ZZ.poly_ring(x).unify(ZZ) == ZZ.poly_ring(x)
    assert ZZ.poly_ring(x).unify(QQ) == QQ.poly_ring(x)
    assert QQ.poly_ring(x).unify(ZZ) == QQ.poly_ring(x)
    assert QQ.poly_ring(x).unify(QQ) == QQ.poly_ring(x)

    assert ZZ.unify(ZZ.poly_ring(x)) == ZZ.poly_ring(x)
    assert QQ.unify(ZZ.poly_ring(x)) == QQ.poly_ring(x)
    assert ZZ.unify(QQ.poly_ring(x)) == QQ.poly_ring(x)
    assert QQ.unify(QQ.poly_ring(x)) == QQ.poly_ring(x)

    assert ZZ.poly_ring(x, y).unify(ZZ) == ZZ.poly_ring(x, y)
    assert ZZ.poly_ring(x, y).unify(QQ) == QQ.poly_ring(x, y)
    assert QQ.poly_ring(x, y).unify(ZZ) == QQ.poly_ring(x, y)
    assert QQ.poly_ring(x, y).unify(QQ) == QQ.poly_ring(x, y)

    assert ZZ.unify(ZZ.poly_ring(x, y)) == ZZ.poly_ring(x, y)
    assert QQ.unify(ZZ.poly_ring(x, y)) == QQ.poly_ring(x, y)
    assert ZZ.unify(QQ.poly_ring(x, y)) == QQ.poly_ring(x, y)
    assert QQ.unify(QQ.poly_ring(x, y)) == QQ.poly_ring(x, y)

    assert ZZ.frac_field(x).unify(ZZ) == ZZ.frac_field(x)
    assert ZZ.frac_field(x).unify(QQ) == EX  # QQ.frac_field(x)
    assert QQ.frac_field(x).unify(ZZ) == EX  # QQ.frac_field(x)
    assert QQ.frac_field(x).unify(QQ) == QQ.frac_field(x)

    assert ZZ.unify(ZZ.frac_field(x)) == ZZ.frac_field(x)
    assert QQ.unify(ZZ.frac_field(x)) == EX  # QQ.frac_field(x)
    assert ZZ.unify(QQ.frac_field(x)) == EX  # QQ.frac_field(x)
    assert QQ.unify(QQ.frac_field(x)) == QQ.frac_field(x)

    assert ZZ.frac_field(x, y).unify(ZZ) == ZZ.frac_field(x, y)
    assert ZZ.frac_field(x, y).unify(QQ) == EX  # QQ.frac_field(x,y)
    assert QQ.frac_field(x, y).unify(ZZ) == EX  # QQ.frac_field(x,y)
    assert QQ.frac_field(x, y).unify(QQ) == QQ.frac_field(x, y)

    assert ZZ.unify(ZZ.frac_field(x, y)) == ZZ.frac_field(x, y)
    assert QQ.unify(ZZ.frac_field(x, y)) == EX  # QQ.frac_field(x,y)
    assert ZZ.unify(QQ.frac_field(x, y)) == EX  # QQ.frac_field(x,y)
    assert QQ.unify(QQ.frac_field(x, y)) == QQ.frac_field(x, y)

    assert ZZ.poly_ring(x).unify(ZZ.poly_ring(x)) == ZZ.poly_ring(x)
    assert ZZ.poly_ring(x).unify(QQ.poly_ring(x)) == QQ.poly_ring(x)
    assert QQ.poly_ring(x).unify(ZZ.poly_ring(x)) == QQ.poly_ring(x)
    assert QQ.poly_ring(x).unify(QQ.poly_ring(x)) == QQ.poly_ring(x)

    assert ZZ.poly_ring(x, y).unify(ZZ.poly_ring(x)) == ZZ.poly_ring(x, y)
    assert ZZ.poly_ring(x, y).unify(QQ.poly_ring(x)) == QQ.poly_ring(x, y)
    assert QQ.poly_ring(x, y).unify(ZZ.poly_ring(x)) == QQ.poly_ring(x, y)
    assert QQ.poly_ring(x, y).unify(QQ.poly_ring(x)) == QQ.poly_ring(x, y)

    assert ZZ.poly_ring(x).unify(ZZ.poly_ring(x, y)) == ZZ.poly_ring(x, y)
    assert ZZ.poly_ring(x).unify(QQ.poly_ring(x, y)) == QQ.poly_ring(x, y)
    assert QQ.poly_ring(x).unify(ZZ.poly_ring(x, y)) == QQ.poly_ring(x, y)
    assert QQ.poly_ring(x).unify(QQ.poly_ring(x, y)) == QQ.poly_ring(x, y)

    assert ZZ.poly_ring(x, y).unify(ZZ.poly_ring(x, z)) == ZZ.poly_ring(x, y, z)
    assert ZZ.poly_ring(x, y).unify(QQ.poly_ring(x, z)) == QQ.poly_ring(x, y, z)
    assert QQ.poly_ring(x, y).unify(ZZ.poly_ring(x, z)) == QQ.poly_ring(x, y, z)
    assert QQ.poly_ring(x, y).unify(QQ.poly_ring(x, z)) == QQ.poly_ring(x, y, z)

    assert ZZ.frac_field(x).unify(ZZ.frac_field(x)) == ZZ.frac_field(x)
    assert ZZ.frac_field(x).unify(QQ.frac_field(x)) == QQ.frac_field(x)
    assert QQ.frac_field(x).unify(ZZ.frac_field(x)) == QQ.frac_field(x)
    assert QQ.frac_field(x).unify(QQ.frac_field(x)) == QQ.frac_field(x)

    assert ZZ.frac_field(x, y).unify(ZZ.frac_field(x)) == ZZ.frac_field(x, y)
    assert ZZ.frac_field(x, y).unify(QQ.frac_field(x)) == QQ.frac_field(x, y)
    assert QQ.frac_field(x, y).unify(ZZ.frac_field(x)) == QQ.frac_field(x, y)
    assert QQ.frac_field(x, y).unify(QQ.frac_field(x)) == QQ.frac_field(x, y)

    assert ZZ.frac_field(x).unify(ZZ.frac_field(x, y)) == ZZ.frac_field(x, y)
    assert ZZ.frac_field(x).unify(QQ.frac_field(x, y)) == QQ.frac_field(x, y)
    assert QQ.frac_field(x).unify(ZZ.frac_field(x, y)) == QQ.frac_field(x, y)
    assert QQ.frac_field(x).unify(QQ.frac_field(x, y)) == QQ.frac_field(x, y)

    assert ZZ.frac_field(x, y).unify(ZZ.frac_field(x, z)) == ZZ.frac_field(x, y, z)
    assert ZZ.frac_field(x, y).unify(QQ.frac_field(x, z)) == QQ.frac_field(x, y, z)
    assert QQ.frac_field(x, y).unify(ZZ.frac_field(x, z)) == QQ.frac_field(x, y, z)
    assert QQ.frac_field(x, y).unify(QQ.frac_field(x, z)) == QQ.frac_field(x, y, z)

    assert ZZ.poly_ring(x).unify(ZZ.frac_field(x)) == ZZ.frac_field(x)
    assert ZZ.poly_ring(x).unify(QQ.frac_field(x)) == EX  # QQ.frac_field(x)
    assert QQ.poly_ring(x).unify(ZZ.frac_field(x)) == EX  # QQ.frac_field(x)
    assert QQ.poly_ring(x).unify(QQ.frac_field(x)) == QQ.frac_field(x)

    assert ZZ.poly_ring(x, y).unify(ZZ.frac_field(x)) == ZZ.frac_field(x, y)
    assert ZZ.poly_ring(x, y).unify(QQ.frac_field(x)) == EX  # QQ.frac_field(x,y)
    assert QQ.poly_ring(x, y).unify(ZZ.frac_field(x)) == EX  # QQ.frac_field(x,y)
    assert QQ.poly_ring(x, y).unify(QQ.frac_field(x)) == QQ.frac_field(x, y)

    assert ZZ.poly_ring(x).unify(ZZ.frac_field(x, y)) == ZZ.frac_field(x, y)
    assert ZZ.poly_ring(x).unify(QQ.frac_field(x, y)) == EX  # QQ.frac_field(x,y)
    assert QQ.poly_ring(x).unify(ZZ.frac_field(x, y)) == EX  # QQ.frac_field(x,y)
    assert QQ.poly_ring(x).unify(QQ.frac_field(x, y)) == QQ.frac_field(x, y)

    assert ZZ.poly_ring(x, y).unify(ZZ.frac_field(x, z)) == ZZ.frac_field(x, y, z)
    assert ZZ.poly_ring(x, y).unify(QQ.frac_field(x, z)) == EX  # QQ.frac_field(x,y,z)
    assert QQ.poly_ring(x, y).unify(ZZ.frac_field(x, z)) == EX  # QQ.frac_field(x,y,z)
    assert QQ.poly_ring(x, y).unify(QQ.frac_field(x, z)) == QQ.frac_field(x, y, z)

    assert ZZ.frac_field(x).unify(ZZ.poly_ring(x)) == ZZ.frac_field(x)
    assert ZZ.frac_field(x).unify(QQ.poly_ring(x)) == EX  # QQ.frac_field(x)
    assert QQ.frac_field(x).unify(ZZ.poly_ring(x)) == EX  # QQ.frac_field(x)
    assert QQ.frac_field(x).unify(QQ.poly_ring(x)) == QQ.frac_field(x)

    assert ZZ.frac_field(x, y).unify(ZZ.poly_ring(x)) == ZZ.frac_field(x, y)
    assert ZZ.frac_field(x, y).unify(QQ.poly_ring(x)) == EX  # QQ.frac_field(x,y)
    assert QQ.frac_field(x, y).unify(ZZ.poly_ring(x)) == EX  # QQ.frac_field(x,y)
    assert QQ.frac_field(x, y).unify(QQ.poly_ring(x)) == QQ.frac_field(x, y)

    assert ZZ.frac_field(x).unify(ZZ.poly_ring(x, y)) == ZZ.frac_field(x, y)
    assert ZZ.frac_field(x).unify(QQ.poly_ring(x, y)) == EX  # QQ.frac_field(x,y)
    assert QQ.frac_field(x).unify(ZZ.poly_ring(x, y)) == EX  # QQ.frac_field(x,y)
    assert QQ.frac_field(x).unify(QQ.poly_ring(x, y)) == QQ.frac_field(x, y)

    assert ZZ.frac_field(x, y).unify(ZZ.poly_ring(x, z)) == ZZ.frac_field(x, y, z)
    assert ZZ.frac_field(x, y).unify(QQ.poly_ring(x, z)) == EX  # QQ.frac_field(x,y,z)
    assert QQ.frac_field(x, y).unify(ZZ.poly_ring(x, z)) == EX  # QQ.frac_field(x,y,z)
    assert QQ.frac_field(x, y).unify(QQ.poly_ring(x, z)) == QQ.frac_field(x, y, z)

    alg = QQ.algebraic_field(sqrt(5))

    assert alg.unify(alg[x, y]) == alg[x, y]
    assert alg[x, y].unify(alg) == alg[x, y]

    assert alg.unify(alg.frac_field(x, y)) == alg.frac_field(x, y)
    assert alg.frac_field(x, y).unify(alg) == alg.frac_field(x, y)

    ext = QQ.algebraic_field(sqrt(7))

    raises(NotImplementedError, lambda: alg.unify(ext))

    raises(UnificationFailed, lambda: ZZ.poly_ring(x, y).unify(ZZ, gens=(y, z)))
    raises(UnificationFailed, lambda: ZZ.unify(ZZ.poly_ring(x, y), gens=(y, z)))
def test_minimal_polynomial():
    assert minimal_polynomial(-7, x) == x + 7
    assert minimal_polynomial(-1, x) == x + 1
    assert minimal_polynomial( 0, x) == x
    assert minimal_polynomial( 1, x) == x - 1
    assert minimal_polynomial( 7, x) == x - 7

    assert minimal_polynomial(sqrt(2), x) == x**2 - 2
    assert minimal_polynomial(sqrt(5), x) == x**2 - 5
    assert minimal_polynomial(sqrt(6), x) == x**2 - 6

    assert minimal_polynomial(2*sqrt(2), x) == x**2 - 8
    assert minimal_polynomial(3*sqrt(5), x) == x**2 - 45
    assert minimal_polynomial(4*sqrt(6), x) == x**2 - 96

    assert minimal_polynomial(2*sqrt(2) + 3, x) == x**2 - 6*x + 1
    assert minimal_polynomial(3*sqrt(5) + 6, x) == x**2 - 12*x - 9
    assert minimal_polynomial(4*sqrt(6) + 7, x) == x**2 - 14*x - 47

    assert minimal_polynomial(2*sqrt(2) - 3, x) == x**2 + 6*x + 1
    assert minimal_polynomial(3*sqrt(5) - 6, x) == x**2 + 12*x - 9
    assert minimal_polynomial(4*sqrt(6) - 7, x) == x**2 + 14*x - 47

    assert minimal_polynomial(sqrt(1 + sqrt(6)), x) == x**4 - 2*x**2 - 5
    assert minimal_polynomial(sqrt(I + sqrt(6)), x) == x**8 - 10*x**4 + 49

    assert minimal_polynomial(2*I + sqrt(2 + I), x) == x**4 + 4*x**2 + 8*x + 37

    assert minimal_polynomial(sqrt(2) + sqrt(3), x) == x**4 - 10*x**2 + 1
    assert minimal_polynomial(
        sqrt(2) + sqrt(3) + sqrt(6), x) == x**4 - 22*x**2 - 48*x - 23

    a = 1 - 9*sqrt(2) + 7*sqrt(3)

    assert minimal_polynomial(
        1/a, x) == 392*x**4 - 1232*x**3 + 612*x**2 + 4*x - 1
    assert minimal_polynomial(
        1/sqrt(a), x) == 392*x**8 - 1232*x**6 + 612*x**4 + 4*x**2 - 1

    raises(NotAlgebraic, lambda: minimal_polynomial(oo, x))
    raises(NotAlgebraic, lambda: minimal_polynomial(2**y, x))
    raises(NotAlgebraic, lambda: minimal_polynomial(sin(1), x))

    assert minimal_polynomial(sqrt(2)).dummy_eq(x**2 - 2)
    assert minimal_polynomial(sqrt(2), x) == x**2 - 2

    assert minimal_polynomial(sqrt(2), polys=True) == Poly(x**2 - 2)
    assert minimal_polynomial(sqrt(2), x, polys=True) == Poly(x**2 - 2)
    assert minimal_polynomial(sqrt(2), x, polys=True, compose=False) == Poly(x**2 - 2)

    a = AlgebraicNumber(sqrt(2))
    b = AlgebraicNumber(sqrt(3))

    assert minimal_polynomial(a, x) == x**2 - 2
    assert minimal_polynomial(b, x) == x**2 - 3

    assert minimal_polynomial(a, x, polys=True) == Poly(x**2 - 2)
    assert minimal_polynomial(b, x, polys=True) == Poly(x**2 - 3)

    assert minimal_polynomial(sqrt(a/2 + 17), x) == 2*x**4 - 68*x**2 + 577
    assert minimal_polynomial(sqrt(b/2 + 17), x) == 4*x**4 - 136*x**2 + 1153

    a, b = sqrt(2)/3 + 7, AlgebraicNumber(sqrt(2)/3 + 7)

    f = 81*x**8 - 2268*x**6 - 4536*x**5 + 22644*x**4 + 63216*x**3 - \
        31608*x**2 - 189648*x + 141358

    assert minimal_polynomial(sqrt(a) + sqrt(sqrt(a)), x) == f
    assert minimal_polynomial(sqrt(b) + sqrt(sqrt(b)), x) == f

    assert minimal_polynomial(
        a**Q(3, 2), x) == 729*x**4 - 506898*x**2 + 84604519

    # issue 5994
    eq = S('''
        -1/(800*sqrt(-1/240 + 1/(18000*(-1/17280000 +
        sqrt(15)*I/28800000)**(1/3)) + 2*(-1/17280000 +
        sqrt(15)*I/28800000)**(1/3)))''')
    assert minimal_polynomial(eq, x) == 8000*x**2 - 1

    ex = 1 + sqrt(2) + sqrt(3)
    mp = minimal_polynomial(ex, x)
    assert mp == x**4 - 4*x**3 - 4*x**2 + 16*x - 8

    ex = 1/(1 + sqrt(2) + sqrt(3))
    mp = minimal_polynomial(ex, x)
    assert mp == 8*x**4 - 16*x**3 + 4*x**2 + 4*x - 1

    p = (expand((1 + sqrt(2) - 2*sqrt(3) + sqrt(7))**3))**Rational(1, 3)
    mp = minimal_polynomial(p, x)
    assert mp == x**8 - 8*x**7 - 56*x**6 + 448*x**5 + 480*x**4 - 5056*x**3 + 1984*x**2 + 7424*x - 3008
    p = expand((1 + sqrt(2) - 2*sqrt(3) + sqrt(7))**3)
    mp = minimal_polynomial(p, x)
    assert mp == x**8 - 512*x**7 - 118208*x**6 + 31131136*x**5 + 647362560*x**4 - 56026611712*x**3 + 116994310144*x**2 + 404854931456*x - 27216576512

    assert minimal_polynomial(S("-sqrt(5)/2 - 1/2 + (-sqrt(5)/2 - 1/2)**2"), x) == x - 1
    a = 1 + sqrt(2)
    assert minimal_polynomial((a*sqrt(2) + a)**3, x) == x**2 - 198*x + 1

    p = 1/(1 + sqrt(2) + sqrt(3))
    assert minimal_polynomial(p, x, compose=False) == 8*x**4 - 16*x**3 + 4*x**2 + 4*x - 1

    p = 2/(1 + sqrt(2) + sqrt(3))
    assert minimal_polynomial(p, x, compose=False) == x**4 - 4*x**3 + 2*x**2 + 4*x - 2

    assert minimal_polynomial(1 + sqrt(2)*I, x, compose=False) == x**2 - 2*x + 3
    assert minimal_polynomial(1/(1 + sqrt(2)) + 1, x, compose=False) == x**2 - 2
    assert minimal_polynomial(sqrt(2)*I + I*(1 + sqrt(2)), x,
            compose=False) ==  x**4 + 18*x**2 + 49

    # minimal polynomial of I
    assert minimal_polynomial(I, x, domain=QQ.algebraic_field(I)) == x - I
    K = QQ.algebraic_field(I*(sqrt(2) + 1))
    assert minimal_polynomial(I, x, domain=K) == x - I
    assert minimal_polynomial(I, x, domain=QQ) == x**2 + 1
    assert minimal_polynomial(I, x, domain='QQ(y)') == x**2 + 1
Example #54
0
def primitive_element(extension, x=None, *, ex=False, polys=False):
    r"""
    Find a single generator for a number field given by several generators.

    Explanation
    ===========

    The basic problem is this: Given several algebraic numbers
    $\alpha_1, \alpha_2, \ldots, \alpha_n$, find a single algebraic number
    $\theta$ such that
    $\mathbb{Q}(\alpha_1, \alpha_2, \ldots, \alpha_n) = \mathbb{Q}(\theta)$.

    This function actually guarantees that $\theta$ will be a linear
    combination of the $\alpha_i$, with non-negative integer coefficients.

    Furthermore, if desired, this function will tell you how to express each
    $\alpha_i$ as a $\mathbb{Q}$-linear combination of the powers of $\theta$.

    Examples
    ========

    >>> from sympy import primitive_element, sqrt, S, minpoly, simplify
    >>> from sympy.abc import x
    >>> f, lincomb, reps = primitive_element([sqrt(2), sqrt(3)], x, ex=True)

    Then ``lincomb`` tells us the primitive element as a linear combination of
    the given generators ``sqrt(2)`` and ``sqrt(3)``.

    >>> print(lincomb)
    [1, 1]

    This means the primtiive element is $\sqrt{2} + \sqrt{3}$.
    Meanwhile ``f`` is the minimal polynomial for this primitive element.

    >>> print(f)
    x**4 - 10*x**2 + 1
    >>> print(minpoly(sqrt(2) + sqrt(3), x))
    x**4 - 10*x**2 + 1

    Finally, ``reps`` (which was returned only because we set keyword arg
    ``ex=True``) tells us how to recover each of the generators $\sqrt{2}$ and
    $\sqrt{3}$ as $\mathbb{Q}$-linear combinations of the powers of the
    primitive element $\sqrt{2} + \sqrt{3}$.

    >>> print([S(r) for r in reps[0]])
    [1/2, 0, -9/2, 0]
    >>> theta = sqrt(2) + sqrt(3)
    >>> print(simplify(theta**3/2 - 9*theta/2))
    sqrt(2)
    >>> print([S(r) for r in reps[1]])
    [-1/2, 0, 11/2, 0]
    >>> print(simplify(-theta**3/2 + 11*theta/2))
    sqrt(3)

    Parameters
    ==========

    extension : list of :py:class:`~.Expr`
        Each expression must represent an algebraic number $\alpha_i$.
    x : :py:class:`~.Symbol`, optional (default=None)
        The desired symbol to appear in the computed minimal polynomial for the
        primitive element $\theta$. If ``None``, we use a dummy symbol.
    ex : boolean, optional (default=False)
        If and only if ``True``, compute the representation of each $\alpha_i$
        as a $\mathbb{Q}$-linear combination over the powers of $\theta$.
    polys : boolean, optional (default=False)
        If ``True``, return the minimal polynomial as a :py:class:`~.Poly`.
        Otherwise return it as an :py:class:`~.Expr`.

    Returns
    =======

    Pair (f, coeffs) or triple (f, coeffs, reps), where:
        ``f`` is the minimal polynomial for the primitive element.
        ``coeffs`` gives the primitive element as a linear combination of the
        given generators.
        ``reps`` is present if and only if argument ``ex=True`` was passed,
        and is a list of lists of rational numbers. Each list gives the
        coefficients of falling powers of the primitive element, to recover
        one of the original, given generators.

    """
    if not extension:
        raise ValueError("Cannot compute primitive element for empty extension")
    extension = [_sympify(ext) for ext in extension]

    if x is not None:
        x, cls = sympify(x), Poly
    else:
        x, cls = Dummy('x'), PurePoly

    if not ex:
        gen, coeffs = extension[0], [1]
        g = minimal_polynomial(gen, x, polys=True)
        for ext in extension[1:]:
            if ext.is_Rational:
                coeffs.append(0)
                continue
            _, factors = factor_list(g, extension=ext)
            g = _choose_factor(factors, x, gen)
            s, _, g = g.sqf_norm()
            gen += s*ext
            coeffs.append(s)

        if not polys:
            return g.as_expr(), coeffs
        else:
            return cls(g), coeffs

    gen, coeffs = extension[0], [1]
    f = minimal_polynomial(gen, x, polys=True)
    K = QQ.algebraic_field((f, gen))  # incrementally constructed field
    reps = [K.unit]  # representations of extension elements in K
    for ext in extension[1:]:
        if ext.is_Rational:
            coeffs.append(0)    # rational ext is not included in the expression of a primitive element
            reps.append(K.convert(ext))    # but it is included in reps
            continue
        p = minimal_polynomial(ext, x, polys=True)
        L = QQ.algebraic_field((p, ext))
        _, factors = factor_list(f, domain=L)
        f = _choose_factor(factors, x, gen)
        s, g, f = f.sqf_norm()
        gen += s*ext
        coeffs.append(s)
        K = QQ.algebraic_field((f, gen))
        h = _switch_domain(g, K)
        erep = _linsolve(h.gcd(p))  # ext as element of K
        ogen = K.unit - s*erep  # old gen as element of K
        reps = [dup_eval(_.rep, ogen, K) for _ in reps] + [erep]

    if K.ext.root.is_Rational:  # all extensions are rational
        H = [K.convert(_).rep for _ in extension]
        coeffs = [0]*len(extension)
        f = cls(x, domain=QQ)
    else:
        H = [_.rep for _ in reps]
    if not polys:
        return f.as_expr(), coeffs, H
    else:
        return f, coeffs, H
Example #55
0
def test_minimal_polynomial():
    assert minimal_polynomial(-7, x) == x + 7
    assert minimal_polynomial(-1, x) == x + 1
    assert minimal_polynomial(0, x) == x
    assert minimal_polynomial(1, x) == x - 1
    assert minimal_polynomial(7, x) == x - 7

    assert minimal_polynomial(sqrt(2), x) == x**2 - 2
    assert minimal_polynomial(sqrt(5), x) == x**2 - 5
    assert minimal_polynomial(sqrt(6), x) == x**2 - 6

    assert minimal_polynomial(2 * sqrt(2), x) == x**2 - 8
    assert minimal_polynomial(3 * sqrt(5), x) == x**2 - 45
    assert minimal_polynomial(4 * sqrt(6), x) == x**2 - 96

    assert minimal_polynomial(2 * sqrt(2) + 3, x) == x**2 - 6 * x + 1
    assert minimal_polynomial(3 * sqrt(5) + 6, x) == x**2 - 12 * x - 9
    assert minimal_polynomial(4 * sqrt(6) + 7, x) == x**2 - 14 * x - 47

    assert minimal_polynomial(2 * sqrt(2) - 3, x) == x**2 + 6 * x + 1
    assert minimal_polynomial(3 * sqrt(5) - 6, x) == x**2 + 12 * x - 9
    assert minimal_polynomial(4 * sqrt(6) - 7, x) == x**2 + 14 * x - 47

    assert minimal_polynomial(sqrt(1 + sqrt(6)), x) == x**4 - 2 * x**2 - 5
    assert minimal_polynomial(sqrt(I + sqrt(6)), x) == x**8 - 10 * x**4 + 49

    assert (minimal_polynomial(2 * I + sqrt(2 + I),
                               x) == x**4 + 4 * x**2 + 8 * x + 37)

    assert minimal_polynomial(sqrt(2) + sqrt(3), x) == x**4 - 10 * x**2 + 1
    assert (minimal_polynomial(sqrt(2) + sqrt(3) + sqrt(6),
                               x) == x**4 - 22 * x**2 - 48 * x - 23)

    a = 1 - 9 * sqrt(2) + 7 * sqrt(3)

    assert (minimal_polynomial(1 / a, x) == 392 * x**4 - 1232 * x**3 +
            612 * x**2 + 4 * x - 1)
    assert (minimal_polynomial(1 / sqrt(a), x) == 392 * x**8 - 1232 * x**6 +
            612 * x**4 + 4 * x**2 - 1)

    raises(NotAlgebraic, lambda: minimal_polynomial(oo, x))
    raises(NotAlgebraic, lambda: minimal_polynomial(2**y, x))
    raises(NotAlgebraic, lambda: minimal_polynomial(sin(1), x))

    assert minimal_polynomial(sqrt(2)).dummy_eq(x**2 - 2)
    assert minimal_polynomial(sqrt(2), x) == x**2 - 2

    assert minimal_polynomial(sqrt(2), polys=True) == Poly(x**2 - 2)
    assert minimal_polynomial(sqrt(2), x, polys=True) == Poly(x**2 - 2)
    assert minimal_polynomial(sqrt(2), x, polys=True,
                              compose=False) == Poly(x**2 - 2)

    a = AlgebraicNumber(sqrt(2))
    b = AlgebraicNumber(sqrt(3))

    assert minimal_polynomial(a, x) == x**2 - 2
    assert minimal_polynomial(b, x) == x**2 - 3

    assert minimal_polynomial(a, x, polys=True) == Poly(x**2 - 2)
    assert minimal_polynomial(b, x, polys=True) == Poly(x**2 - 3)

    assert minimal_polynomial(sqrt(a / 2 + 17),
                              x) == 2 * x**4 - 68 * x**2 + 577
    assert minimal_polynomial(sqrt(b / 2 + 17),
                              x) == 4 * x**4 - 136 * x**2 + 1153

    a, b = sqrt(2) / 3 + 7, AlgebraicNumber(sqrt(2) / 3 + 7)

    f = (81 * x**8 - 2268 * x**6 - 4536 * x**5 + 22644 * x**4 + 63216 * x**3 -
         31608 * x**2 - 189648 * x + 141358)

    assert minimal_polynomial(sqrt(a) + sqrt(sqrt(a)), x) == f
    assert minimal_polynomial(sqrt(b) + sqrt(sqrt(b)), x) == f

    assert (minimal_polynomial(a**Q(3, 2),
                               x) == 729 * x**4 - 506898 * x**2 + 84604519)

    # issue 5994
    eq = S("""
        -1/(800*sqrt(-1/240 + 1/(18000*(-1/17280000 +
        sqrt(15)*I/28800000)**(1/3)) + 2*(-1/17280000 +
        sqrt(15)*I/28800000)**(1/3)))""")
    assert minimal_polynomial(eq, x) == 8000 * x**2 - 1

    ex = 1 + sqrt(2) + sqrt(3)
    mp = minimal_polynomial(ex, x)
    assert mp == x**4 - 4 * x**3 - 4 * x**2 + 16 * x - 8

    ex = 1 / (1 + sqrt(2) + sqrt(3))
    mp = minimal_polynomial(ex, x)
    assert mp == 8 * x**4 - 16 * x**3 + 4 * x**2 + 4 * x - 1

    p = (expand((1 + sqrt(2) - 2 * sqrt(3) + sqrt(7))**3))**Rational(1, 3)
    mp = minimal_polynomial(p, x)
    assert (mp == x**8 - 8 * x**7 - 56 * x**6 + 448 * x**5 + 480 * x**4 -
            5056 * x**3 + 1984 * x**2 + 7424 * x - 3008)
    p = expand((1 + sqrt(2) - 2 * sqrt(3) + sqrt(7))**3)
    mp = minimal_polynomial(p, x)
    assert (mp == x**8 - 512 * x**7 - 118208 * x**6 + 31131136 * x**5 +
            647362560 * x**4 - 56026611712 * x**3 + 116994310144 * x**2 +
            404854931456 * x - 27216576512)

    assert minimal_polynomial(S("-sqrt(5)/2 - 1/2 + (-sqrt(5)/2 - 1/2)**2"),
                              x) == x - 1
    a = 1 + sqrt(2)
    assert minimal_polynomial((a * sqrt(2) + a)**3, x) == x**2 - 198 * x + 1

    p = 1 / (1 + sqrt(2) + sqrt(3))
    assert (minimal_polynomial(p, x, compose=False) == 8 * x**4 - 16 * x**3 +
            4 * x**2 + 4 * x - 1)

    p = 2 / (1 + sqrt(2) + sqrt(3))
    assert (minimal_polynomial(p, x, compose=False) == x**4 - 4 * x**3 +
            2 * x**2 + 4 * x - 2)

    assert minimal_polynomial(1 + sqrt(2) * I, x,
                              compose=False) == x**2 - 2 * x + 3
    assert minimal_polynomial(1 / (1 + sqrt(2)) + 1, x,
                              compose=False) == x**2 - 2
    assert (minimal_polynomial(sqrt(2) * I + I * (1 + sqrt(2)),
                               x,
                               compose=False) == x**4 + 18 * x**2 + 49)

    # minimal polynomial of I
    assert minimal_polynomial(I, x, domain=QQ.algebraic_field(I)) == x - I
    K = QQ.algebraic_field(I * (sqrt(2) + 1))
    assert minimal_polynomial(I, x, domain=K) == x - I
    assert minimal_polynomial(I, x, domain=QQ) == x**2 + 1
    assert minimal_polynomial(I, x, domain="QQ(y)") == x**2 + 1

    # issue 11553
    assert minimal_polynomial(GoldenRatio, x) == x**2 - x - 1
    assert (minimal_polynomial(TribonacciConstant + 3,
                               x) == x**3 - 10 * x**2 + 32 * x - 34)
    assert (minimal_polynomial(GoldenRatio,
                               x,
                               domain=QQ.algebraic_field(sqrt(5))) == 2 * x -
            sqrt(5) - 1)
    assert (minimal_polynomial(TribonacciConstant,
                               x,
                               domain=QQ.algebraic_field(
                                   cbrt(19 - 3 * sqrt(33)))) == 48 * x - 19 *
            (19 - 3 * sqrt(33))**Rational(2, 3) - 3 * sqrt(33) *
            (19 - 3 * sqrt(33))**Rational(2, 3) - 16 *
            (19 - 3 * sqrt(33))**Rational(1, 3) - 16)