Beispiel #1
0
def test_Algebra_get_ring():
    assert ZZ.has_assoc_Ring == True
    assert QQ.has_assoc_Ring == True
    assert ZZ[x].has_assoc_Ring == True
    assert QQ[x].has_assoc_Ring == True
    assert ZZ[x,y].has_assoc_Ring == True
    assert QQ[x,y].has_assoc_Ring == True
    assert ZZ.frac_field(x).has_assoc_Ring == True
    assert QQ.frac_field(x).has_assoc_Ring == True
    assert ZZ.frac_field(x,y).has_assoc_Ring == True
    assert QQ.frac_field(x,y).has_assoc_Ring == True

    assert EX.has_assoc_Ring == False
    assert RR.has_assoc_Ring == False
    assert ALG.has_assoc_Ring == False

    assert ZZ.get_ring() == ZZ
    assert QQ.get_ring() == ZZ
    assert ZZ[x].get_ring() == ZZ[x]
    assert QQ[x].get_ring() == ZZ[x]
    assert ZZ[x,y].get_ring() == ZZ[x,y]
    assert QQ[x,y].get_ring() == ZZ[x,y]
    assert ZZ.frac_field(x).get_ring() == ZZ[x]
    assert QQ.frac_field(x).get_ring() == ZZ[x]
    assert ZZ.frac_field(x,y).get_ring() == ZZ[x,y]
    assert QQ.frac_field(x,y).get_ring() == ZZ[x,y]

    raises(DomainError, "EX.get_ring()")
    raises(DomainError, "RR.get_ring()")
    raises(DomainError, "ALG.get_ring()")
Beispiel #2
0
def test_Algebra_get_exact():
    assert EX.get_exact() == EX
    assert ZZ.get_exact() == ZZ
    assert QQ.get_exact() == QQ
    assert RR.get_exact() == QQ
    assert ALG.get_exact() == ALG
    assert ZZ[x].get_exact() == ZZ[x]
    assert QQ[x].get_exact() == QQ[x]
    assert ZZ[x,y].get_exact() == ZZ[x,y]
    assert QQ[x,y].get_exact() == QQ[x,y]
    assert ZZ.frac_field(x).get_exact() == ZZ.frac_field(x)
    assert QQ.frac_field(x).get_exact() == QQ.frac_field(x)
    assert ZZ.frac_field(x,y).get_exact() == ZZ.frac_field(x,y)
    assert QQ.frac_field(x,y).get_exact() == QQ.frac_field(x,y)
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),
        ])
Beispiel #4
0
def test_Algebra_get_field():
    assert EX.has_assoc_Field == True
    assert ZZ.has_assoc_Field == True
    assert QQ.has_assoc_Field == True
    assert ALG.has_assoc_Field == True
    assert ZZ[x].has_assoc_Field == True
    assert QQ[x].has_assoc_Field == True
    assert ZZ[x,y].has_assoc_Field == True
    assert QQ[x,y].has_assoc_Field == True

    assert RR.has_assoc_Field == False

    assert EX.get_field() == EX
    assert ZZ.get_field() == QQ
    assert QQ.get_field() == QQ
    assert ALG.get_field() == ALG
    assert ZZ[x].get_field() == ZZ.frac_field(x)
    assert QQ[x].get_field() == ZZ.frac_field(x)
    assert ZZ[x,y].get_field() == ZZ.frac_field(x,y)
    assert QQ[x,y].get_field() == ZZ.frac_field(x,y)

    raises(DomainError, "RR.get_field()")
def test_dup_add():
    assert dup_add([], [], ZZ) == []
    assert dup_add([ZZ(1)], [], ZZ) == [ZZ(1)]
    assert dup_add([], [ZZ(1)], ZZ) == [ZZ(1)]
    assert dup_add([ZZ(1)], [ZZ(1)], ZZ) == [ZZ(2)]
    assert dup_add([ZZ(1)], [ZZ(2)], ZZ) == [ZZ(3)]

    assert dup_add([ZZ(1),ZZ(2)], [ZZ(1)], ZZ) == [ZZ(1),ZZ(3)]
    assert dup_add([ZZ(1)], [ZZ(1),ZZ(2)], ZZ) == [ZZ(1),ZZ(3)]

    assert dup_add([ZZ(1),ZZ(2),ZZ(3)], [ZZ(8),ZZ(9),ZZ(10)], ZZ) == [ZZ(9),ZZ(11),ZZ(13)]

    assert dup_add([], [], QQ) == []
    assert dup_add([QQ(1,2)], [], QQ) == [QQ(1,2)]
    assert dup_add([], [QQ(1,2)], QQ) == [QQ(1,2)]
    assert dup_add([QQ(1,4)], [QQ(1,4)], QQ) == [QQ(1,2)]
    assert dup_add([QQ(1,4)], [QQ(1,2)], QQ) == [QQ(3,4)]

    assert dup_add([QQ(1,2),QQ(2,3)], [QQ(1)], QQ) == [QQ(1,2),QQ(5,3)]
    assert dup_add([QQ(1)], [QQ(1,2),QQ(2,3)], QQ) == [QQ(1,2),QQ(5,3)]

    assert dup_add([QQ(1,7),QQ(2,7),QQ(3,7)], [QQ(8,7),QQ(9,7),QQ(10,7)], QQ) == [QQ(9,7),QQ(11,7),QQ(13,7)]
    dmp_div, dmp_rem, dmp_quo, dmp_exquo,
    dup_max_norm, dmp_max_norm,
    dup_l1_norm, dmp_l1_norm,
    dup_expand, dmp_expand,
)

from sympy.polys.polyerrors import (
    ExactQuotientFailed,
)

from sympy.polys.specialpolys import f_0
from sympy.polys.algebratools import ZZ, QQ

from sympy import raises

F_0 = dmp_mul_ground(dmp_normal(f_0, 2, QQ), QQ(1,7), 2, QQ)

def test_dup_add_term():
    f = dup_normal([], ZZ)

    assert dup_add_term(f, ZZ(0), 0, ZZ) == dup_normal([], ZZ)

    assert dup_add_term(f, ZZ(1), 0, ZZ) == dup_normal([1], ZZ)
    assert dup_add_term(f, ZZ(1), 1, ZZ) == dup_normal([1, 0], ZZ)
    assert dup_add_term(f, ZZ(1), 2, ZZ) == dup_normal([1, 0, 0], ZZ)

    f = dup_normal([1,1,1], ZZ)

    assert dup_add_term(f, ZZ(1), 0, ZZ) == dup_normal([1, 1, 2], ZZ)
    assert dup_add_term(f, ZZ(1), 1, ZZ) == dup_normal([1, 2, 1], ZZ)
    assert dup_add_term(f, ZZ(1), 2, ZZ) == dup_normal([2, 1, 1], ZZ)
def test_dmp_mul_ground():
    assert dmp_mul_ground(f_0, ZZ(2), 2, ZZ) == [
        [[ZZ(2),ZZ(4),ZZ(6)], [ZZ(4)]],
        [[ZZ(6)]],
        [[ZZ(8),ZZ(10),ZZ(12)], [ZZ(2),ZZ(4),ZZ(2)], [ZZ(2)]]
    ]

    assert dmp_mul_ground(F_0, QQ(1,2), 2, QQ) == [
        [[QQ(1,14),QQ(2,14),QQ(3,14)], [QQ(2,14)]],
        [[QQ(3,14)]],
        [[QQ(4,14),QQ(5,14),QQ(6,14)], [QQ(1,14),QQ(2,14),QQ(1,14)], [QQ(1,14)]]
    ]
def test_dup_pow():
    assert dup_pow([], 0, ZZ) == [ZZ(1)]
    assert dup_pow([], 0, QQ) == [QQ(1)]

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

    assert dup_pow([ZZ(1)], 0, ZZ) == [ZZ(1)]
    assert dup_pow([ZZ(1)], 1, ZZ) == [ZZ(1)]
    assert dup_pow([ZZ(1)], 7, ZZ) == [ZZ(1)]

    assert dup_pow([ZZ(3)], 0, ZZ) == [ZZ(1)]
    assert dup_pow([ZZ(3)], 1, ZZ) == [ZZ(3)]
    assert dup_pow([ZZ(3)], 7, ZZ) == [ZZ(2187)]

    assert dup_pow([QQ(1, 1)], 0, QQ) == [QQ(1, 1)]
    assert dup_pow([QQ(1, 1)], 1, QQ) == [QQ(1, 1)]
    assert dup_pow([QQ(1, 1)], 7, QQ) == [QQ(1, 1)]

    assert dup_pow([QQ(3, 7)], 0, QQ) == [QQ(1, 1)]
    assert dup_pow([QQ(3, 7)], 1, QQ) == [QQ(3, 7)]
    assert dup_pow([QQ(3, 7)], 7, QQ) == [QQ(2187, 823543)]

    f = dup_normal([2, 0, 0, 1, 7], ZZ)

    assert dup_pow(f, 0, ZZ) == dup_normal([1], ZZ)
    assert dup_pow(f, 1, ZZ) == dup_normal([2, 0, 0, 1, 7], ZZ)
    assert dup_pow(f, 2, ZZ) == dup_normal([4, 0, 0, 4, 28, 0, 1, 14, 49], ZZ)
    assert dup_pow(f, 3, ZZ) == dup_normal(
        [8, 0, 0, 12, 84, 0, 6, 84, 294, 1, 21, 147, 343], ZZ)
def test_dmp_ff_div():
    raises(ZeroDivisionError, "dmp_ff_div([[1,2],[3]], [[]], 1, QQ)")

    f = dmp_normal([[1], [], [1, 0, 0]], 1, QQ)
    g = dmp_normal([[1], [-1, 0]], 1, QQ)

    q = [[QQ(1, 1)], [QQ(1, 1), QQ(0, 1)]]
    r = [[QQ(2, 1), QQ(0, 1), QQ(0, 1)]]

    assert dmp_ff_div(f, g, 1, QQ) == (q, r)

    f = dmp_normal([[1], [], [1, 0, 0]], 1, QQ)
    g = dmp_normal([[-1], [1, 0]], 1, QQ)

    q = [[QQ(-1, 1)], [QQ(-1, 1), QQ(0, 1)]]
    r = [[QQ(2, 1), QQ(0, 1), QQ(0, 1)]]

    assert dmp_ff_div(f, g, 1, QQ) == (q, r)

    f = dmp_normal([[1], [], [1, 0, 0]], 1, QQ)
    g = dmp_normal([[2], [-2, 0]], 1, QQ)

    q = [[QQ(1, 2)], [QQ(1, 2), QQ(0, 1)]]
    r = [[QQ(2, 1), QQ(0, 1), QQ(0, 1)]]

    assert dmp_ff_div(f, g, 1, QQ) == (q, r)
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),
        ])
def test_sdp_LT():
    assert sdp_LT([], 1, QQ) == ((0, 0), QQ(0))
    assert sdp_LT([((1,0), QQ(1,2))], 1, QQ) == ((1, 0), QQ(1,2))
    assert sdp_LT([((1,1), QQ(1,4)), ((1,0), QQ(1,2))], 1, QQ) == ((1, 1), QQ(1,4))
def test_sdp_LC():
    assert sdp_LC([], QQ) == QQ(0)
    assert sdp_LC([((1,0), QQ(1,2))], QQ) == QQ(1,2)
    assert sdp_LC([((1,1), QQ(1,4)), ((1,0), QQ(1,2))], QQ) == QQ(1,4)
def test_sdp_div():
    f = sdp_from_dict({(2,1): 4, (1,1): -2, (1,0): 4, (0,1): -2, (0,0): 8}, O_grlex)

    assert sdp_div(f, [sdp_from_dict({(0,0): 2}, O_grlex)], 1, O_grlex, ZZ) == \
        ([sdp_from_dict({(2,1): 2, (1,1): -1, (1,0): 2, (0,1): -1, (0,0): 4}, O_grlex)], [])

    assert sdp_div(f, [sdp_from_dict({(0,1): 2}, O_grlex)], 1, O_grlex, ZZ) == \
        ([sdp_from_dict({(2,0): 2, (1,0): -1, (0,0): -1}, O_grlex)],
          sdp_from_dict({(1,0): 4, (0,0): 8}, O_grlex))

    f = sdp_from_dict({(1,0): 1, (0,0): -1}, O_grlex)
    g = sdp_from_dict({(0,1): 1, (0,0): -1}, O_grlex)

    assert sdp_div(f, [g], 1, O_grlex, ZZ) == ([[]], f)

    f = sdp_from_dict({(3,): 1, (2,): -12, (0,): -42}, O_grlex)
    g = sdp_from_dict({(1,): 1, (0,): -3}, O_grlex)

    q = sdp_from_dict({(2,): 1, (1,): -9, (0,): -27}, O_grlex)
    r = sdp_from_dict({(0,): -123}, O_grlex)

    assert sdp_div(f, [g], 0, O_grlex, ZZ) == ([q], r)

    f = sdp_from_dict({(2,): QQ(1), (1,): QQ(2), (0,): QQ(2)}, O_grlex)

    g = sdp_from_dict({(0,): QQ(1)}, O_grlex)
    h = sdp_from_dict({(0,): QQ(2)}, O_grlex)

    q = sdp_from_dict({(2,): QQ(1,2), (1,): QQ(1), (0,): QQ(1)}, O_grlex)

    assert sdp_div(f, [g], 0, O_grlex, QQ) == ([f], [])
    assert sdp_div(f, [h], 0, O_grlex, QQ) == ([q], [])

    f = sdp_from_dict({(1,2): 1, (0,0): 1}, O_grlex)
    G = [sdp_from_dict({(1,1): 1, (0,0): 1}, O_grlex),
         sdp_from_dict({(0,1): 1, (0,0): 1}, O_grlex)]

    Q = [sdp_from_dict({(0,1): 1}, O_grlex),
         sdp_from_dict({(0,0): -1}, O_grlex)]
    r = sdp_from_dict({(0,0): 2}, O_grlex)

    assert sdp_div(f, G, 1, O_grlex, ZZ) == (Q, r)

    f = sdp_from_dict({(2,1): 1, (1,2): 1, (0,2): 1}, O_grlex)

    G = [sdp_from_dict({(1,1): 1, (0,0): -1}, O_grlex),
         sdp_from_dict({(0,2): 1, (0,0): -1}, O_grlex)]

    Q = [sdp_from_dict({(1,0): 1, (0,1): 1}, O_grlex),
         sdp_from_dict({(0,0): 1}, O_grlex)]
    r = sdp_from_dict({(1,0): 1, (0,1): 1, (0,0): 1}, O_grlex)

    assert sdp_div(f, G, 1, O_grlex, ZZ) == (Q, r)

    G = [sdp_from_dict({(0,2): 1, (0,0): -1}, O_grlex),
         sdp_from_dict({(1,1): 1, (0,0): -1}, O_grlex)]

    Q = [sdp_from_dict({(1,0): 1, (0,0): 1}, O_grlex),
         sdp_from_dict({(1,0): 1}, O_grlex)]
    r = sdp_from_dict({(1,0): 2, (0,0): 1}, O_grlex)

    assert sdp_div(f, G, 1, O_grlex, ZZ) == (Q, r)
Beispiel #14
0
"""Tests for classes defining properties of ground domains, e.g. ZZ, QQ, ZZ[x] ... """

from sympy.polys.algebratools import ZZ, QQ, RR, PolynomialRing, FractionField, EX
from sympy.polys.polyerrors import (UnificationFailed, GeneratorsNeeded,
    DomainError,)

from sympy import S, sqrt, sin, oo
from sympy.abc import x, y

from sympy.utilities.pytest import raises

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

def test_Algebra__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
Beispiel #15
0
def test_Algebra__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')

    raises(UnificationFailed, "ZZ.poly_ring('x','y').unify(ZZ, gens=('y', 'z'))")
    raises(UnificationFailed, "ZZ.unify(ZZ.poly_ring('x','y'), gens=('y', 'z'))")
def test_sdp_monoms():
    assert sdp_monoms([]) == []
    assert sdp_monoms([((1,0), QQ(1,2))]) == [(1,0)]
    assert sdp_monoms([((1,1), QQ(1,4)), ((1,0), QQ(1,2))]) == [(1,1), (1,0)]
def test_sdp_coeffs():
    assert sdp_coeffs([]) == []
    assert sdp_coeffs([((1,0), QQ(1,2))]) == [QQ(1,2)]
    assert sdp_coeffs([((1,1), QQ(1,4)), ((1,0), QQ(1,2))]) == [QQ(1,4), QQ(1,2)]
def test_sdp_del_LT():
    assert sdp_del_LT([]) == []
    assert sdp_del_LT([((1,0), QQ(1,2))]) == []
    assert sdp_del_LT([((1,1), QQ(1,4)), ((1,0), QQ(1,2))]) == [((1,0), QQ(1,2))]
Beispiel #19
0
def test_dmp_add():
    assert dmp_add([ZZ(1),ZZ(2)], [ZZ(1)], 0, ZZ) == \
           dup_add([ZZ(1),ZZ(2)], [ZZ(1)], ZZ)
    assert dmp_add([QQ(1,2),QQ(2,3)], [QQ(1)], 0, QQ) == \
           dup_add([QQ(1,2),QQ(2,3)], [QQ(1)], QQ)

    assert dmp_add([[[]]], [[[]]], 2, ZZ) == [[[]]]
    assert dmp_add([[[ZZ(1)]]], [[[]]], 2, ZZ) == [[[ZZ(1)]]]
    assert dmp_add([[[]]], [[[ZZ(1)]]], 2, ZZ) == [[[ZZ(1)]]]
    assert dmp_add([[[ZZ(2)]]], [[[ZZ(1)]]], 2, ZZ) == [[[ZZ(3)]]]
    assert dmp_add([[[ZZ(1)]]], [[[ZZ(2)]]], 2, ZZ) == [[[ZZ(3)]]]

    assert dmp_add([[[]]], [[[]]], 2, QQ) == [[[]]]
    assert dmp_add([[[QQ(1,2)]]], [[[]]], 2, QQ) == [[[QQ(1,2)]]]
    assert dmp_add([[[]]], [[[QQ(1,2)]]], 2, QQ) == [[[QQ(1,2)]]]
    assert dmp_add([[[QQ(2,7)]]], [[[QQ(1,7)]]], 2, QQ) == [[[QQ(3,7)]]]
    assert dmp_add([[[QQ(1,7)]]], [[[QQ(2,7)]]], 2, QQ) == [[[QQ(3,7)]]]
def test_sdp_groebner():
    f = sdp_from_dict({(1,2): QQ(2,), (2,0): QQ(1)}, O_lex)
    g = sdp_from_dict({(0,3): QQ(2), (1,1): QQ(1), (0,0): QQ(-1)},  O_lex)

    a = sdp_from_dict({(1,0): QQ(1,1)}, O_lex)
    b = sdp_from_dict({(0,3): QQ(1,1), (0,0): QQ(-1,2)}, O_lex)

    assert sdp_groebner((f, g), 1, O_lex, QQ) == [a, b]

    f = sdp_from_dict({(2,1): QQ(2,), (0,2): QQ(1)}, O_lex)
    g = sdp_from_dict({(3,0): QQ(2), (1,1): QQ(1), (0,0): QQ(-1)},  O_lex)

    a = sdp_from_dict({(0,1): QQ(1,1)}, O_lex)
    b = sdp_from_dict({(3,0): QQ(1,1), (0,0): QQ(-1,2)}, O_lex)

    assert sdp_groebner((f, g), 1, O_lex, QQ) == [b, a]

    f = sdp_from_dict({(0,0,2): QQ(-1), (1,0,0): QQ(1)}, O_lex)
    g = sdp_from_dict({(0,0,3): QQ(-1), (0,1,0): QQ(1)}, O_lex)

    assert sdp_groebner((f, g), 1, O_lex, QQ) == [f, g]

    f = sdp_from_dict({(3,0): QQ(1), (1,1): QQ(-2)}, O_grlex)
    g = sdp_from_dict({(2,1): QQ(1), (0,2): QQ(-2), (1,0): QQ(1)}, O_grlex)

    a = sdp_from_dict({(2,0): QQ(1)}, O_grlex)
    b = sdp_from_dict({(1,1): QQ(1)}, O_grlex)
    c = sdp_from_dict({(0,2): QQ(1), (1, 0): QQ(-1,2)}, O_grlex)

    assert sdp_groebner((f, g), 1, O_grlex, QQ) == [a, b, c]

    f = sdp_from_dict({(2,0,0): -1, (0,1,0): 1}, O_lex)
    g = sdp_from_dict({(3,0,0): -1, (0,0,1): 1}, O_lex)

    assert sdp_groebner((f, g), 2, O_lex, ZZ) == [
        sdp_from_dict({(2,0,0): 1, (0,1,0): -1}, O_lex),
        sdp_from_dict({(1,1,0): 1, (0,0,1): -1}, O_lex),
        sdp_from_dict({(1,0,1): 1, (0,2,0): -1}, O_lex),
        sdp_from_dict({(0,3,0): 1, (0,0,2): -1}, O_lex),
    ]

    f = sdp_from_dict({(2,0,0): -1, (0,1,0): 1}, O_grlex)
    g = sdp_from_dict({(3,0,0): -1, (0,0,1): 1}, O_grlex)

    assert sdp_groebner((f, g), 2, O_grlex, ZZ) == [
        sdp_from_dict({(0,3,0): 1, (0,0,2): -1}, O_grlex),
        sdp_from_dict({(2,0,0): 1, (0,1,0): -1}, O_grlex),
        sdp_from_dict({(1,1,0): 1, (0,0,1): -1}, O_grlex),
        sdp_from_dict({(1,0,1): 1, (0,2,0): -1}, O_grlex),
    ]

    f = sdp_from_dict({(2,0,0): -1, (0,0,1): 1}, O_lex)
    g = sdp_from_dict({(3,0,0): -1, (0,1,0): 1}, O_lex)

    assert sdp_groebner((f, g), 2, O_lex, ZZ) == [
        sdp_from_dict({(2,0,0): 1, (0,0,1): -1}, O_lex),
        sdp_from_dict({(1,1,0): 1, (0,0,2): -1}, O_lex),
        sdp_from_dict({(1,0,1): 1, (0,1,0): -1}, O_lex),
        sdp_from_dict({(0,2,0): 1, (0,0,3): -1}, O_lex),
    ]

    f = sdp_from_dict({(2,0,0): -1, (0,0,1): 1}, O_grlex)
    g = sdp_from_dict({(3,0,0): -1, (0,1,0): 1}, O_grlex)

    assert sdp_groebner((f, g), 2, O_grlex, ZZ) == [
        sdp_from_dict({(0,0,3): 1, (0,2,0): -1}, O_grlex),
        sdp_from_dict({(2,0,0): 1, (0,0,1): -1}, O_grlex),
        sdp_from_dict({(1,1,0): 1, (0,0,2): -1}, O_grlex),
        sdp_from_dict({(1,0,1): 1, (0,1,0): -1}, O_grlex),
    ]

    f = sdp_from_dict({(0,2,0): -1, (1,0,0): 1}, O_lex)
    g = sdp_from_dict({(0,3,0): -1, (0,0,1): 1}, O_lex)

    assert sdp_groebner((f, g), 2, O_lex, ZZ) == [
        sdp_from_dict({(1,0,0): 1, (0,2,0): -1}, O_lex),
        sdp_from_dict({(0,3,0): 1, (0,0,1): -1}, O_lex),
    ]

    f = sdp_from_dict({(0,2,0): -1, (1,0,0): 1}, O_grlex)
    g = sdp_from_dict({(0,3,0): -1, (0,0,1): 1}, O_grlex)

    assert sdp_groebner((f, g), 2, O_grlex, ZZ) == [
        sdp_from_dict({(2,0,0): 1, (0,1,1): -1}, O_grlex),
        sdp_from_dict({(1,1,0): 1, (0,0,1): -1}, O_grlex),
        sdp_from_dict({(0,2,0): 1, (1,0,0): -1}, O_grlex),
    ]

    f = sdp_from_dict({(0,0,2): -1, (1,0,0): 1}, O_lex)
    g = sdp_from_dict({(0,0,3): -1, (0,1,0): 1}, O_lex)

    assert sdp_groebner((f, g), 2, O_lex, ZZ) == [
        sdp_from_dict({(1,0,0): 1, (0,0,2): -1}, O_lex),
        sdp_from_dict({(0,1,0): 1, (0,0,3): -1}, O_lex),
    ]

    f = sdp_from_dict({(0,0,2): -1, (1,0,0): 1}, O_grlex)
    g = sdp_from_dict({(0,0,3): -1, (0,1,0): 1}, O_grlex)

    assert sdp_groebner((f, g), 2, O_grlex, ZZ) == [
        sdp_from_dict({(2,0,0): 1, (0,1,1): -1}, O_grlex),
        sdp_from_dict({(1,0,1): 1, (0,1,0): -1}, O_grlex),
        sdp_from_dict({(0,0,2): 1, (1,0,0): -1}, O_grlex),
    ]

    f = sdp_from_dict({(0,2,0): -1, (0,0,1): 1}, O_lex)
    g = sdp_from_dict({(0,3,0): -1, (1,0,0): 1}, O_lex)

    assert sdp_groebner((f, g), 2, O_lex, ZZ) == [
        sdp_from_dict({(1,0,0): 1, (0,1,1): -1}, O_lex),
        sdp_from_dict({(0,2,0): 1, (0,0,1): -1}, O_lex),
    ]

    f = sdp_from_dict({(0,2,0): -1, (0,0,1): 1}, O_grlex)
    g = sdp_from_dict({(0,3,0): -1, (1,0,0): 1}, O_grlex)

    assert sdp_groebner((f, g), 2, O_grlex, ZZ) == [
        sdp_from_dict({(0,0,3): 1, (2,0,0): -1}, O_grlex),
        sdp_from_dict({(1,1,0): 1, (0,0,2): -1}, O_grlex),
        sdp_from_dict({(0,2,0): 1, (0,0,1): -1}, O_grlex),
        sdp_from_dict({(0,1,1): 1, (1,0,0): -1}, O_grlex),
    ]

    f = sdp_from_dict({(0,0,2): -1, (0,1,0): 1}, O_lex)
    g = sdp_from_dict({(0,0,3): -1, (1,0,0): 1}, O_lex)

    assert sdp_groebner((f, g), 2, O_lex, ZZ) == [
        sdp_from_dict({(1,0,0): 1, (0,0,3): -1}, O_lex),
        sdp_from_dict({(0,1,0): 1, (0,0,2): -1}, O_lex),
    ]

    f = sdp_from_dict({(0,0,2): -1, (0,1,0): 1}, O_grlex)
    g = sdp_from_dict({(0,0,3): -1, (1,0,0): 1}, O_grlex)

    assert sdp_groebner((f, g), 2, O_grlex, ZZ) == [
        sdp_from_dict({(0,3,0): 1, (2,0,0): -1}, O_grlex),
        sdp_from_dict({(1,0,1): 1, (0,2,0): -1}, O_grlex),
        sdp_from_dict({(0,1,1): 1, (1,0,0): -1}, O_grlex),
        sdp_from_dict({(0,0,2): 1, (0,1,0): -1}, O_grlex),
    ]
Beispiel #21
0
def test_dup_sub():
    assert dup_sub([], [], ZZ) == []
    assert dup_sub([ZZ(1)], [], ZZ) == [ZZ(1)]
    assert dup_sub([], [ZZ(1)], ZZ) == [ZZ(-1)]
    assert dup_sub([ZZ(1)], [ZZ(1)], ZZ) == []
    assert dup_sub([ZZ(1)], [ZZ(2)], ZZ) == [ZZ(-1)]

    assert dup_sub([ZZ(1),ZZ(2)], [ZZ(1)], ZZ) == [ZZ(1),ZZ(1)]
    assert dup_sub([ZZ(1)], [ZZ(1),ZZ(2)], ZZ) == [ZZ(-1),ZZ(-1)]

    assert dup_sub([ZZ(3),ZZ(2),ZZ(1)], [ZZ(8),ZZ(9),ZZ(10)], ZZ) == [ZZ(-5),ZZ(-7),ZZ(-9)]

    assert dup_sub([], [], QQ) == []
    assert dup_sub([QQ(1,2)], [], QQ) == [QQ(1,2)]
    assert dup_sub([], [QQ(1,2)], QQ) == [QQ(-1,2)]
    assert dup_sub([QQ(1,3)], [QQ(1,3)], QQ) == []
    assert dup_sub([QQ(1,3)], [QQ(2,3)], QQ) == [QQ(-1,3)]

    assert dup_sub([QQ(1,7),QQ(2,7)], [QQ(1)], QQ) == [QQ(1,7),QQ(-5,7)]
    assert dup_sub([QQ(1)], [QQ(1,7),QQ(2,7)], QQ) == [QQ(-1,7),QQ(5,7)]

    assert dup_sub([QQ(3,7),QQ(2,7),QQ(1,7)], [QQ(8,7),QQ(9,7),QQ(10,7)], QQ) == [QQ(-5,7),QQ(-7,7),QQ(-9,7)]
Beispiel #22
0
def test_dmp_mul():
    assert dmp_mul([ZZ(5)], [ZZ(7)], 0, ZZ) == \
           dup_mul([ZZ(5)], [ZZ(7)], ZZ)
    assert dmp_mul([QQ(5,7)], [QQ(3,7)], 0, QQ) == \
           dup_mul([QQ(5,7)], [QQ(3,7)], QQ)

    assert dmp_mul([[[]]], [[[]]], 2, ZZ) == [[[]]]
    assert dmp_mul([[[ZZ(1)]]], [[[]]], 2, ZZ) == [[[]]]
    assert dmp_mul([[[]]], [[[ZZ(1)]]], 2, ZZ) == [[[]]]
    assert dmp_mul([[[ZZ(2)]]], [[[ZZ(1)]]], 2, ZZ) == [[[ZZ(2)]]]
    assert dmp_mul([[[ZZ(1)]]], [[[ZZ(2)]]], 2, ZZ) == [[[ZZ(2)]]]

    assert dmp_mul([[[]]], [[[]]], 2, QQ) == [[[]]]
    assert dmp_mul([[[QQ(1, 2)]]], [[[]]], 2, QQ) == [[[]]]
    assert dmp_mul([[[]]], [[[QQ(1, 2)]]], 2, QQ) == [[[]]]
    assert dmp_mul([[[QQ(2, 7)]]], [[[QQ(1, 3)]]], 2, QQ) == [[[QQ(2, 21)]]]
    assert dmp_mul([[[QQ(1, 7)]]], [[[QQ(2, 3)]]], 2, QQ) == [[[QQ(2, 21)]]]
Beispiel #23
0
def test_AlgebraicNumber():
    minpoly, root = x**2 - 2, sqrt(2)

    a = AlgebraicNumber(root, gen=x)

    assert a.rep == DMP([QQ(1), QQ(0)], QQ)
    assert a.root == root
    assert a.alias is None
    assert a.minpoly == minpoly

    assert a.is_aliased == False

    assert a.coeffs() == [S(1), S(0)]
    assert a.native_coeffs() == [QQ(1), QQ(0)]

    a = AlgebraicNumber(root, gen=x, alias='y')

    assert a.rep == DMP([QQ(1), QQ(0)], QQ)
    assert a.root == root
    assert a.alias == Symbol('y')
    assert a.minpoly == minpoly

    assert a.is_aliased == True

    a = AlgebraicNumber(root, gen=x, alias=Symbol('y'))

    assert a.rep == DMP([QQ(1), QQ(0)], QQ)
    assert a.root == root
    assert a.alias == Symbol('y')
    assert a.minpoly == minpoly

    assert a.is_aliased == True

    assert AlgebraicNumber(sqrt(2), []).rep == DMP([], QQ)

    assert AlgebraicNumber(sqrt(2), [8]).rep == DMP([QQ(8)], QQ)
    assert AlgebraicNumber(sqrt(2), [S(8) / 3]).rep == DMP([QQ(8, 3)], QQ)

    assert AlgebraicNumber(sqrt(2), [7, 3]).rep == DMP([QQ(7), QQ(3)], QQ)
    assert AlgebraicNumber(sqrt(2), [S(7) / 9, S(3) / 2]).rep == DMP(
        [QQ(7, 9), QQ(3, 2)], QQ)

    assert AlgebraicNumber(sqrt(2), [1, 2, 3]).rep == DMP([QQ(2), QQ(5)], QQ)

    a = AlgebraicNumber(AlgebraicNumber(root, gen=x), [1, 2])

    assert a.rep == DMP([QQ(1), QQ(2)], QQ)
    assert a.root == root
    assert a.alias is None
    assert a.minpoly == minpoly

    assert a.is_aliased == False

    assert a.coeffs() == [S(1), S(2)]
    assert a.native_coeffs() == [QQ(1), QQ(2)]

    a = AlgebraicNumber((minpoly, root), [1, 2])

    assert a.rep == DMP([QQ(1), QQ(2)], QQ)
    assert a.root == root
    assert a.alias is None
    assert a.minpoly == minpoly

    assert a.is_aliased == False

    a = AlgebraicNumber((Poly(minpoly), root), [1, 2])

    assert a.rep == DMP([QQ(1), QQ(2)], QQ)
    assert a.root == root
    assert a.alias is None
    assert a.minpoly == minpoly

    assert a.is_aliased == False

    assert AlgebraicNumber(sqrt(3)).rep == DMP([QQ(1), QQ(0)], QQ)
    assert AlgebraicNumber(-sqrt(3)).rep == DMP([-QQ(1), QQ(0)], QQ)

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

    assert a == b and a == sqrt(2)

    a = AlgebraicNumber(sqrt(2), gen=x)
    b = AlgebraicNumber(sqrt(2), gen=x)

    assert a == b and a == sqrt(2)

    a = AlgebraicNumber(sqrt(2), [1, 2])
    b = AlgebraicNumber(sqrt(2), [1, 3])

    assert a != b and a != sqrt(2) + 3

    assert (a == x) == False and (a != x) == True

    a = AlgebraicNumber(sqrt(2), [1, 0])
    b = AlgebraicNumber(sqrt(2), [1, 0], alias=y)

    assert a.as_poly(x) == Poly(x)
    assert b.as_poly() == Poly(y)

    assert a.as_basic() == sqrt(2)
    assert a.as_basic(x) == x
    assert b.as_basic() == sqrt(2)
    assert b.as_basic(x) == x

    a = AlgebraicNumber(sqrt(2), [2, 3])
    b = AlgebraicNumber(sqrt(2), [2, 3], alias=y)

    p = a.as_poly()

    assert p == Poly(2 * p.gen + 3)

    assert a.as_poly(x) == Poly(2 * x + 3)
    assert b.as_poly() == Poly(2 * y + 3)

    assert a.as_basic() == 2 * sqrt(2) + 3
    assert a.as_basic(x) == 2 * x + 3
    assert b.as_basic() == 2 * sqrt(2) + 3
    assert b.as_basic(x) == 2 * x + 3
def test_sdp_LM():
    assert sdp_LM([], 1) == (0, 0)
    assert sdp_LM([((1,0), QQ(1,2))], 1) == (1, 0)
    assert sdp_LM([((1,1), QQ(1,4)), ((1,0), QQ(1,2))], 1) == (1, 1)