Exemplo n.º 1
0
def test_dup_isolate_real_roots_list():
    assert dup_isolate_real_roots_list([[1, 1,0], [1,0]], ZZ) == \
        [((-QQ(1), -QQ(1)), {0: 1}), ((QQ(0), QQ(0)), {0: 1, 1: 1})]
    assert dup_isolate_real_roots_list([[1,-1,0], [1,0]], ZZ) == \
        [((QQ(0), QQ(0)), {0: 1, 1: 1}), ((QQ(1), QQ(1)), {0: 1})]

    f = dup_from_raw_dict({5: ZZ(1), 0: -ZZ(200)}, ZZ)
    g = dup_from_raw_dict({5: ZZ(1), 0: -ZZ(201)}, ZZ)

    assert dup_isolate_real_roots_list([f, g], ZZ) == \
        [((QQ(75,26), QQ(101,35)), {0: 1}), ((QQ(283,98), QQ(26,9)), {1: 1})]

    f = dup_from_raw_dict({5: -QQ(1,200), 0: QQ(1)}, QQ)
    g = dup_from_raw_dict({5: -QQ(1,201), 0: QQ(1)}, QQ)

    assert dup_isolate_real_roots_list([f, g], QQ) == \
        [((QQ(75,26), QQ(101,35)), {0: 1}), ((QQ(283,98), QQ(26,9)), {1: 1})]

    assert dup_isolate_real_roots_list([[1,1], [1,2], [1,-1], [1,1], [1,-1], [1,-1]], ZZ) == \
        [((-QQ(2), -QQ(2)), {1: 1}), ((-QQ(1), -QQ(1)), {0: 1, 3: 1}), ((QQ(1), QQ(1)), {2: 1, 4: 1, 5: 1})]

    assert dup_isolate_real_roots_list([[1,1], [1,2], [1,-1], [1,1], [1,-1], [1,2]], ZZ) == \
        [((-QQ(2), -QQ(2)), {1: 1, 5: 1}), ((-QQ(1), -QQ(1)), {0: 1, 3: 1}), ((QQ(1), QQ(1)), {2: 1, 4: 1})]

    f, g = [1, 0, -4, 0, 4], [1, -1]

    assert dup_isolate_real_roots_list([f, g], ZZ, inf=QQ(7,4)) == []
    assert dup_isolate_real_roots_list([f, g], ZZ, inf=QQ(7,5)) == [((QQ(7,5), QQ(3,2)), {0: 2})]
    assert dup_isolate_real_roots_list([f, g], ZZ, sup=QQ(7,5)) == [((-2, -1), {0: 2}), ((1, 1), {1: 1})]
    assert dup_isolate_real_roots_list([f, g], ZZ, sup=QQ(7,4)) == [((-2, -1), {0: 2}), ((1, 1), {1: 1}), ((1, QQ(3,2)), {0: 2})]
    assert dup_isolate_real_roots_list([f, g], ZZ, sup=-QQ(7,4)) == []
    assert dup_isolate_real_roots_list([f, g], ZZ, sup=-QQ(7,5)) == [((-QQ(3,2), -QQ(7,5)), {0: 2})]
    assert dup_isolate_real_roots_list([f, g], ZZ, inf=-QQ(7,5)) == [((1, 1), {1: 1}), ((1, 2), {0: 2})]
    assert dup_isolate_real_roots_list([f, g], ZZ, inf=-QQ(7,4)) == [((-QQ(3,2), -1), {0: 2}), ((1, 1), {1: 1}), ((1, 2), {0: 2})]

    f, g = [2, 0, -1], [1, 0, -2]

    assert dup_isolate_real_roots_list([f, g], ZZ) == \
        [((-QQ(2), -QQ(1)), {1: 1}), ((-QQ(1), QQ(0)), {0: 1}), ((QQ(0), QQ(1)), {0: 1}), ((QQ(1), QQ(2)), {1: 1})]
    assert dup_isolate_real_roots_list([f, g], ZZ, strict=True) == \
        [((-QQ(3,2), -QQ(4,3)), {1: 1}), ((-QQ(1), -QQ(2,3)), {0: 1}), ((QQ(2,3), QQ(1)), {0: 1}), ((QQ(4,3), QQ(3,2)), {1: 1})]

    f, g = [1, 0, -2], [1, -1, -2, 2]

    assert dup_isolate_real_roots_list([f, g], ZZ) == \
        [((-QQ(2), -QQ(1)), {1: 1, 0: 1}), ((QQ(1), QQ(1)), {1: 1}), ((QQ(1), QQ(2)), {1: 1, 0: 1})]

    f, g = [1, 0, -2, 0], [1, -1, -2, 2, 0, 0]

    assert dup_isolate_real_roots_list([f, g], ZZ) == \
        [((-QQ(2), -QQ(1)), {1: 1, 0: 1}), ((QQ(0), QQ(0)), {0: 1, 1: 2}), ((QQ(1), QQ(1)), {1: 1}), ((QQ(1), QQ(2)), {1: 1, 0: 1})]

    f, g = [1, -3, -1, 11, -8, -8, 12, -4, 0, 0], [1, -2, 3, -4, 2, 0]

    assert dup_isolate_real_roots_list([f, g], ZZ, basis=False) == \
        [((-2, -1), {0: 2}), ((0, 0), {0: 2, 1: 1}), ((1, 1), {0: 3, 1: 2}), ((1, 2), {0: 2})]
    assert dup_isolate_real_roots_list([f, g], ZZ, basis=True) == \
        [((-2, -1), {0: 2}, [1, 0, -2]), ((0, 0), {0: 2, 1: 1}, [1, 0]), ((1, 1), {0: 3, 1: 2}, [1, -1]), ((1, 2), {0: 2}, [1, 0, -2])]

    raises(DomainError, "dup_isolate_real_roots_list([[EX(1), EX(2)]], EX)")
Exemplo n.º 2
0
def test_dup_isolate_real_roots_list():
    assert dup_isolate_real_roots_list([[1, 1,0], [1,0]], ZZ) == \
        [((-QQ(1), -QQ(1)), {0: 1}), ((QQ(0), QQ(0)), {0: 1, 1: 1})]
    assert dup_isolate_real_roots_list([[1,-1,0], [1,0]], ZZ) == \
        [((QQ(0), QQ(0)), {0: 1, 1: 1}), ((QQ(1), QQ(1)), {0: 1})]

    f = dup_from_raw_dict({5: ZZ(1), 0: -ZZ(200)}, ZZ)
    g = dup_from_raw_dict({5: ZZ(1), 0: -ZZ(201)}, ZZ)

    assert dup_isolate_real_roots_list([f, g], ZZ) == \
        [((QQ(75,26), QQ(101,35)), {0: 1}), ((QQ(283,98), QQ(26,9)), {1: 1})]

    f = dup_from_raw_dict({5: -QQ(1,200), 0: QQ(1)}, QQ)
    g = dup_from_raw_dict({5: -QQ(1,201), 0: QQ(1)}, QQ)

    assert dup_isolate_real_roots_list([f, g], QQ) == \
        [((QQ(75,26), QQ(101,35)), {0: 1}), ((QQ(283,98), QQ(26,9)), {1: 1})]

    assert dup_isolate_real_roots_list([[1,1], [1,2], [1,-1], [1,1], [1,-1], [1,-1]], ZZ) == \
        [((-QQ(2), -QQ(2)), {1: 1}), ((-QQ(1), -QQ(1)), {0: 1, 3: 1}), ((QQ(1), QQ(1)), {2: 1, 4: 1, 5: 1})]

    assert dup_isolate_real_roots_list([[1,1], [1,2], [1,-1], [1,1], [1,-1], [1,2]], ZZ) == \
        [((-QQ(2), -QQ(2)), {1: 1, 5: 1}), ((-QQ(1), -QQ(1)), {0: 1, 3: 1}), ((QQ(1), QQ(1)), {2: 1, 4: 1})]

    f, g = [1, 0, -4, 0, 4], [1, -1]

    assert dup_isolate_real_roots_list([f, g], ZZ, inf=QQ(7,4)) == []
    assert dup_isolate_real_roots_list([f, g], ZZ, inf=QQ(7,5)) == [((QQ(7,5), QQ(3,2)), {0: 2})]
    assert dup_isolate_real_roots_list([f, g], ZZ, sup=QQ(7,5)) == [((-2, -1), {0: 2}), ((1, 1), {1: 1})]
    assert dup_isolate_real_roots_list([f, g], ZZ, sup=QQ(7,4)) == [((-2, -1), {0: 2}), ((1, 1), {1: 1}), ((1, QQ(3,2)), {0: 2})]
    assert dup_isolate_real_roots_list([f, g], ZZ, sup=-QQ(7,4)) == []
    assert dup_isolate_real_roots_list([f, g], ZZ, sup=-QQ(7,5)) == [((-QQ(3,2), -QQ(7,5)), {0: 2})]
    assert dup_isolate_real_roots_list([f, g], ZZ, inf=-QQ(7,5)) == [((1, 1), {1: 1}), ((1, 2), {0: 2})]
    assert dup_isolate_real_roots_list([f, g], ZZ, inf=-QQ(7,4)) == [((-QQ(3,2), -1), {0: 2}), ((1, 1), {1: 1}), ((1, 2), {0: 2})]

    f, g = [2, 0, -1], [1, 0, -2]

    assert dup_isolate_real_roots_list([f, g], ZZ) == \
        [((-QQ(2), -QQ(1)), {1: 1}), ((-QQ(1), QQ(0)), {0: 1}), ((QQ(0), QQ(1)), {0: 1}), ((QQ(1), QQ(2)), {1: 1})]
    assert dup_isolate_real_roots_list([f, g], ZZ, strict=True) == \
        [((-QQ(3,2), -QQ(4,3)), {1: 1}), ((-QQ(1), -QQ(2,3)), {0: 1}), ((QQ(2,3), QQ(1)), {0: 1}), ((QQ(4,3), QQ(3,2)), {1: 1})]

    f, g = [1, 0, -2], [1, -1, -2, 2]

    assert dup_isolate_real_roots_list([f, g], ZZ) == \
        [((-QQ(2), -QQ(1)), {1: 1, 0: 1}), ((QQ(1), QQ(1)), {1: 1}), ((QQ(1), QQ(2)), {1: 1, 0: 1})]

    f, g = [1, 0, -2, 0], [1, -1, -2, 2, 0, 0]

    assert dup_isolate_real_roots_list([f, g], ZZ) == \
        [((-QQ(2), -QQ(1)), {1: 1, 0: 1}), ((QQ(0), QQ(0)), {0: 1, 1: 2}), ((QQ(1), QQ(1)), {1: 1}), ((QQ(1), QQ(2)), {1: 1, 0: 1})]

    f, g = [1, -3, -1, 11, -8, -8, 12, -4, 0, 0], [1, -2, 3, -4, 2, 0]

    assert dup_isolate_real_roots_list([f, g], ZZ, basis=False) == \
        [((-2, -1), {0: 2}), ((0, 0), {0: 2, 1: 1}), ((1, 1), {0: 3, 1: 2}), ((1, 2), {0: 2})]
    assert dup_isolate_real_roots_list([f, g], ZZ, basis=True) == \
        [((-2, -1), {0: 2}, [1, 0, -2]), ((0, 0), {0: 2, 1: 1}, [1, 0]), ((1, 1), {0: 3, 1: 2}, [1, -1]), ((1, 2), {0: 2}, [1, 0, -2])]

    raises(DomainError, lambda: dup_isolate_real_roots_list([[EX(1), EX(2)]], EX))
Exemplo n.º 3
0
def test_dup_from_to_dict():
    assert dup_from_raw_dict({}, ZZ) == []
    assert dup_from_dict({}, ZZ) == []

    assert dup_to_raw_dict([]) == {}
    assert dup_to_dict([]) == {}

    assert dup_to_raw_dict([], ZZ, zero=True) == {0: ZZ(0)}
    assert dup_to_dict([], ZZ, zero=True) == {(0,): ZZ(0)}

    f = [3,0,0,2,0,0,0,0,8]
    g = {8: 3, 5: 2, 0: 8}
    h = {(8,): 3, (5,): 2, (0,): 8}

    assert dup_from_raw_dict(g, ZZ) == f
    assert dup_from_dict(h, ZZ) == f

    assert dup_to_raw_dict(f) == g
    assert dup_to_dict(f) == h

    K = ZZ['x','y']

    f = [K([[3]]),K([[]]),K([[2]]),K([[]]),K([[]]),K([[8]])]
    g = {5: K([[3]]), 3: K([[2]]), 0: K([[8]])}
    h = {(5,): K([[3]]), (3,): K([[2]]), (0,): K([[8]])}

    assert dup_from_raw_dict(g, K) == f
    assert dup_from_dict(h, K) == f

    assert dup_to_raw_dict(f) == g
    assert dup_to_dict(f) == h
Exemplo n.º 4
0
def test_dup_from_to_dict():
    assert dup_from_raw_dict({}, ZZ) == []
    assert dup_from_dict({}, ZZ) == []

    assert dup_to_raw_dict([]) == {}
    assert dup_to_dict([]) == {}

    assert dup_to_raw_dict([], ZZ, zero=True) == {0: ZZ(0)}
    assert dup_to_dict([], ZZ, zero=True) == {(0,): ZZ(0)}

    f = [3, 0, 0, 2, 0, 0, 0, 0, 8]
    g = {8: 3, 5: 2, 0: 8}
    h = {(8,): 3, (5,): 2, (0,): 8}

    assert dup_from_raw_dict(g, ZZ) == f
    assert dup_from_dict(h, ZZ) == f

    assert dup_to_raw_dict(f) == g
    assert dup_to_dict(f) == h

    R, x,y = ring("x,y", ZZ)
    K = R.to_domain()

    f = [R(3), R(0), R(2), R(0), R(0), R(8)]
    g = {5: R(3), 3: R(2), 0: R(8)}
    h = {(5,): R(3), (3,): R(2), (0,): R(8)}

    assert dup_from_raw_dict(g, K) == f
    assert dup_from_dict(h, K) == f

    assert dup_to_raw_dict(f) == g
    assert dup_to_dict(f) == h
Exemplo n.º 5
0
def test_dup_from_to_dict():
    assert dup_from_raw_dict({}, ZZ) == []
    assert dup_from_dict({}, ZZ) == []

    assert dup_to_raw_dict([]) == {}
    assert dup_to_dict([]) == {}

    assert dup_to_raw_dict([], ZZ, zero=True) == {0: ZZ(0)}
    assert dup_to_dict([], ZZ, zero=True) == {(0, ): ZZ(0)}

    f = [3, 0, 0, 2, 0, 0, 0, 0, 8]
    g = {8: 3, 5: 2, 0: 8}
    h = {(8, ): 3, (5, ): 2, (0, ): 8}

    assert dup_from_raw_dict(g, ZZ) == f
    assert dup_from_dict(h, ZZ) == f

    assert dup_to_raw_dict(f) == g
    assert dup_to_dict(f) == h

    R, x, y = ring("x,y", ZZ)
    K = R.to_domain()

    f = [R(3), R(0), R(2), R(0), R(0), R(8)]
    g = {5: R(3), 3: R(2), 0: R(8)}
    h = {(5, ): R(3), (3, ): R(2), (0, ): R(8)}

    assert dup_from_raw_dict(g, K) == f
    assert dup_from_dict(h, K) == f

    assert dup_to_raw_dict(f) == g
    assert dup_to_dict(f) == h
Exemplo n.º 6
0
def test_dup_zz_hensel_lift():
    f = dup_from_raw_dict({4:1, 0:-1}, ZZ)

    f1 = dup_from_raw_dict({1:1, 0:-1}, ZZ)
    f2 = dup_from_raw_dict({1:1, 0:-2}, ZZ)
    f3 = dup_from_raw_dict({1:1, 0: 2}, ZZ)
    f4 = dup_from_raw_dict({1:1, 0: 1}, ZZ)

    ff_list = dup_zz_hensel_lift(5, f, [f1, f2, f3, f4], 4, ZZ)

    assert dup_to_raw_dict(ff_list[0]) == {0: -1,   1: 1}
    assert dup_to_raw_dict(ff_list[1]) == {0: -182, 1: 1}
    assert dup_to_raw_dict(ff_list[2]) == {0:  182, 1: 1}
    assert dup_to_raw_dict(ff_list[3]) == {0:  1,   1: 1}
Exemplo n.º 7
0
def test_dup_zz_hensel_lift():
    f = dup_from_raw_dict({4: 1, 0: -1}, ZZ)

    f1 = dup_from_raw_dict({1: 1, 0: -1}, ZZ)
    f2 = dup_from_raw_dict({1: 1, 0: -2}, ZZ)
    f3 = dup_from_raw_dict({1: 1, 0: 2}, ZZ)
    f4 = dup_from_raw_dict({1: 1, 0: 1}, ZZ)

    ff_list = dup_zz_hensel_lift(5, f, [f1, f2, f3, f4], 4, ZZ)

    assert dup_to_raw_dict(ff_list[0]) == {0: -1, 1: 1}
    assert dup_to_raw_dict(ff_list[1]) == {0: -182, 1: 1}
    assert dup_to_raw_dict(ff_list[2]) == {0: 182, 1: 1}
    assert dup_to_raw_dict(ff_list[3]) == {0: 1, 1: 1}
Exemplo n.º 8
0
def _dup_right_decompose(f, s, K):
    """Helper function for :func:`_dup_decompose`."""
    n = len(f) - 1
    lc = dup_LC(f, K)

    f = dup_to_raw_dict(f)
    g = {s: K.one}

    r = n // s

    for i in range(1, s):
        coeff = K.zero

        for j in range(0, i):
            if not n + j - i in f:
                continue

            if not s - j in g:
                continue

            fc, gc = f[n + j - i], g[s - j]
            coeff += (i - r * j) * fc * gc

        g[s - i] = K.quo(coeff, i * r * lc)

    return dup_from_raw_dict(g, K)
Exemplo n.º 9
0
def _dup_right_decompose(f, s, K):
    """Helper function for :func:`_dup_decompose`."""
    n = len(f) - 1
    lc = dup_LC(f, K)

    f = dup_to_raw_dict(f)
    g = { s: K.one }

    r = n // s

    for i in range(1, s):
        coeff = K.zero

        for j in range(0, i):
            if not n + j - i in f:
                continue

            if not s - j in g:
                continue

            fc, gc = f[n + j - i], g[s - j]
            coeff += (i - r*j)*fc*gc

        g[s - i] = K.quo(coeff, i*r*lc)

    return dup_from_raw_dict(g, K)
Exemplo n.º 10
0
def test_dup_integrate():
    assert dup_integrate([], 1, QQ) == []
    assert dup_integrate([], 2, QQ) == []

    assert dup_integrate([QQ(1)], 1, QQ) == [QQ(1), QQ(0)]
    assert dup_integrate([QQ(1)], 2, QQ) == [QQ(1, 2), QQ(0), QQ(0)]

    assert dup_integrate([QQ(1), QQ(2), QQ(3)], 0, QQ) == [QQ(1), QQ(2), QQ(3)]
    assert dup_integrate([QQ(1), QQ(2), QQ(3)], 1, QQ) == [QQ(1, 3), QQ(1), QQ(3), QQ(0)]
    assert dup_integrate([QQ(1), QQ(2), QQ(3)], 2, QQ) == [QQ(1, 12), QQ(1, 3), QQ(3, 2), QQ(0), QQ(0)]
    assert dup_integrate([QQ(1), QQ(2), QQ(3)], 3, QQ) == [QQ(1, 60), QQ(1, 12), QQ(1, 2), QQ(0), QQ(0), QQ(0)]

    assert dup_integrate(dup_from_raw_dict({29: QQ(17)}, QQ), 3, QQ) == dup_from_raw_dict({32: QQ(17, 29760)}, QQ)

    assert dup_integrate(dup_from_raw_dict({29: QQ(17), 5: QQ(1, 2)}, QQ), 3, QQ) == dup_from_raw_dict(
        {32: QQ(17, 29760), 8: QQ(1, 672)}, QQ
    )
Exemplo n.º 11
0
def test_dup_zz_hensel_step():
    f = dup_from_raw_dict({4: 1, 0: -1}, ZZ)

    g = dup_from_raw_dict({3: 1, 2: 2, 1: -1, 0: -2}, ZZ)
    h = dup_from_raw_dict({1: 1, 0: -2}, ZZ)
    s = dup_from_raw_dict({0: -2}, ZZ)
    t = dup_from_raw_dict({2: 2, 1: -2, 0: -1}, ZZ)

    G, H, S, T = dup_zz_hensel_step(5, f, g, h, s, t, ZZ)

    assert G == dup_from_raw_dict({3: 1, 2: 7, 1: -1, 0: -7}, ZZ)
    assert H == dup_from_raw_dict({1: 1, 0: -7}, ZZ)
    assert S == dup_from_raw_dict({0: 8}, ZZ)
    assert T == dup_from_raw_dict({2: -8, 1: -12, 0: -1}, ZZ)
Exemplo n.º 12
0
def test_dup_zz_hensel_step():
    f = dup_from_raw_dict({4:1, 0:-1}, ZZ)

    g = dup_from_raw_dict({3:1, 2:2, 1:-1, 0:-2}, ZZ)
    h = dup_from_raw_dict({1:1, 0:-2}, ZZ)
    s = dup_from_raw_dict({0:-2}, ZZ)
    t = dup_from_raw_dict({2:2, 1:-2, 0:-1}, ZZ)

    G, H, S, T = dup_zz_hensel_step(5, f, g, h, s, t, ZZ)

    assert G == dup_from_raw_dict({3:1, 2:7, 1:-1, 0:-7}, ZZ)
    assert H == dup_from_raw_dict({1:1, 0:-7}, ZZ)
    assert S == dup_from_raw_dict({0:8}, ZZ)
    assert T == dup_from_raw_dict({2:-8, 1:-12, 0:-1}, ZZ)
Exemplo n.º 13
0
def test_dup_integrate():
    assert dup_integrate([], 1, QQ) == []
    assert dup_integrate([], 2, QQ) == []

    assert dup_integrate([QQ(1)], 1, QQ) == [QQ(1), QQ(0)]
    assert dup_integrate([QQ(1)], 2, QQ) == [QQ(1, 2), QQ(0), QQ(0)]

    assert dup_integrate([QQ(1), QQ(2), QQ(3)], 0, QQ) == \
        [QQ(1), QQ(2), QQ(3)]
    assert dup_integrate([QQ(1), QQ(2), QQ(3)], 1, QQ) == \
        [QQ(1, 3), QQ(1), QQ(3), QQ(0)]
    assert dup_integrate([QQ(1), QQ(2), QQ(3)], 2, QQ) == \
        [QQ(1, 12), QQ(1, 3), QQ(3, 2), QQ(0), QQ(0)]
    assert dup_integrate([QQ(1), QQ(2), QQ(3)], 3, QQ) == \
        [QQ(1, 60), QQ(1, 12), QQ(1, 2), QQ(0), QQ(0), QQ(0)]

    assert dup_integrate(dup_from_raw_dict({29: QQ(17)}, QQ), 3, QQ) == \
        dup_from_raw_dict({32: QQ(17, 29760)}, QQ)

    assert dup_integrate(dup_from_raw_dict({29: QQ(17), 5: QQ(1, 2)}, QQ), 3, QQ) == \
        dup_from_raw_dict({32: QQ(17, 29760), 8: QQ(1, 672)}, QQ)
Exemplo n.º 14
0
def _dup_left_decompose(f, h, K):
    """Helper function for :func:`_dup_decompose`."""
    g, i = {}, 0

    while f:
        q, r = dup_div(f, h, K)

        if dup_degree(r) > 0:
            return None
        else:
            g[i] = dup_LC(r, K)
            f, i = q, i + 1

    return dup_from_raw_dict(g, K)
Exemplo n.º 15
0
def _dup_left_decompose(f, h, K):
    """Helper function for :func:`_dup_decompose`."""
    g, i = {}, 0

    while f:
        q, r = dup_div(f, h, K)

        if dup_degree(r) > 0:
            return None
        else:
            g[i] = dup_LC(r, K)
            f, i = q, i + 1

    return dup_from_raw_dict(g, K)
Exemplo n.º 16
0
def test_dup_deflate():
    assert dup_deflate([], ZZ) == (1, [])
    assert dup_deflate([2], ZZ) == (1, [2])
    assert dup_deflate([1, 2, 3], ZZ) == (1, [1, 2, 3])
    assert dup_deflate([1, 0, 2, 0, 3], ZZ) == (2, [1, 2, 3])

    assert dup_deflate(dup_from_raw_dict({7: 1, 1: 1}, ZZ), ZZ) == (1, [1, 0, 0, 0, 0, 0, 1, 0])
    assert dup_deflate(dup_from_raw_dict({7: 1, 0: 1}, ZZ), ZZ) == (7, [1, 1])
    assert dup_deflate(dup_from_raw_dict({7: 1, 3: 1}, ZZ), ZZ) == (1, [1, 0, 0, 0, 1, 0, 0, 0])

    assert dup_deflate(dup_from_raw_dict({7: 1, 4: 1}, ZZ), ZZ) == (1, [1, 0, 0, 1, 0, 0, 0, 0])
    assert dup_deflate(dup_from_raw_dict({8: 1, 4: 1}, ZZ), ZZ) == (4, [1, 1, 0])

    assert dup_deflate(dup_from_raw_dict({8: 1}, ZZ), ZZ) == (8, [1, 0])
    assert dup_deflate(dup_from_raw_dict({7: 1}, ZZ), ZZ) == (7, [1, 0])
    assert dup_deflate(dup_from_raw_dict({1: 1}, ZZ), ZZ) == (1, [1, 0])
Exemplo n.º 17
0
def test_dup_zz_cyclotomic_factor():
    assert dup_zz_cyclotomic_factor([], ZZ) is None
    assert dup_zz_cyclotomic_factor([1], ZZ) is None

    f = dup_from_raw_dict({10: 2, 0: -1}, ZZ)
    assert dup_zz_cyclotomic_factor(f, ZZ) is None
    f = dup_from_raw_dict({10: 1, 0: -3}, ZZ)
    assert dup_zz_cyclotomic_factor(f, ZZ) is None
    f = dup_from_raw_dict({10: 1, 5: 1, 0: -1}, ZZ)
    assert dup_zz_cyclotomic_factor(f, ZZ) is None

    f = dup_from_raw_dict({1: 1, 0: 1}, ZZ)
    assert dup_zz_cyclotomic_factor(f, ZZ) == \
         [[1, 1]]

    f = dup_from_raw_dict({1: 1, 0: -1}, ZZ)
    assert dup_zz_cyclotomic_factor(f, ZZ) == \
        [[1, -1]]

    f = dup_from_raw_dict({2: 1, 0: 1}, ZZ)
    assert dup_zz_cyclotomic_factor(f, ZZ) == \
        [[1, 0, 1]]

    f = dup_from_raw_dict({2: 1, 0: -1}, ZZ)
    assert dup_zz_cyclotomic_factor(f, ZZ) == \
        [[1,-1],
         [1, 1]]

    f = dup_from_raw_dict({27: 1, 0: 1}, ZZ)
    assert dup_zz_cyclotomic_factor(f, ZZ) == \
        [[1, 1],
         [1, -1, 1],
         [1, 0, 0, -1, 0, 0, 1],
         [1, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 1]]

    f = dup_from_raw_dict({27: 1, 0: -1}, ZZ)
    assert dup_zz_cyclotomic_factor(f, ZZ) == \
        [[1, -1],
         [1, 1, 1],
         [1, 0, 0, 1, 0, 0, 1],
         [1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1]]
Exemplo n.º 18
0
def test_dup_zz_cyclotomic_factor():
    assert dup_zz_cyclotomic_factor([], ZZ) is None
    assert dup_zz_cyclotomic_factor([1], ZZ) is None

    f = dup_from_raw_dict({10:2, 0:-1}, ZZ)
    assert dup_zz_cyclotomic_factor(f, ZZ) is None
    f = dup_from_raw_dict({10:1, 0:-3}, ZZ)
    assert dup_zz_cyclotomic_factor(f, ZZ) is None
    f = dup_from_raw_dict({10:1, 5:1, 0:-1}, ZZ)
    assert dup_zz_cyclotomic_factor(f, ZZ) is None

    f = dup_from_raw_dict({1:1,0:1}, ZZ)
    assert dup_zz_cyclotomic_factor(f, ZZ) == \
         [[1, 1]]

    f = dup_from_raw_dict({1:1,0:-1}, ZZ)
    assert dup_zz_cyclotomic_factor(f, ZZ) == \
        [[1, -1]]

    f = dup_from_raw_dict({2:1,0:1}, ZZ)
    assert dup_zz_cyclotomic_factor(f, ZZ) == \
        [[1, 0, 1]]

    f = dup_from_raw_dict({2:1,0:-1}, ZZ)
    assert dup_zz_cyclotomic_factor(f, ZZ) == \
        [[1,-1],
         [1, 1]]

    f = dup_from_raw_dict({27:1,0:1}, ZZ)
    assert dup_zz_cyclotomic_factor(f, ZZ) == \
        [[1, 1],
         [1, -1, 1],
         [1, 0, 0, -1, 0, 0, 1],
         [1, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 1]]

    f = dup_from_raw_dict({27:1,0:-1}, ZZ)
    assert dup_zz_cyclotomic_factor(f, ZZ) == \
        [[1, -1],
         [1, 1, 1],
         [1, 0, 0, 1, 0, 0, 1],
         [1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1]]
Exemplo n.º 19
0
def dmp_fateman_poly_F_3(n, K):
    """Fateman's GCD benchmark: sparse inputs (deg f ~ vars f) """
    u = dup_from_raw_dict({n+1: K.one}, K)

    for i in xrange(0, n-1):
        u = dmp_add_term([u], dmp_one(i, K), n+1, i+1, K)

    v = dmp_add_term(u, dmp_ground(K(2), n-2), 0, n, K)

    f = dmp_sqr(dmp_add_term([dmp_neg(v, n-1, K)], dmp_one(n-1, K), n+1, n, K), n, K)
    g = dmp_sqr(dmp_add_term([v], dmp_one(n-1, K), n+1, n, K), n, K)

    v = dmp_add_term(u, dmp_one(n-2, K), 0, n-1, K)

    h = dmp_sqr(dmp_add_term([v], dmp_one(n-1, K), n+1, n, K), n, K)

    return dmp_mul(f, h, n, K), dmp_mul(g, h, n, K), h
Exemplo n.º 20
0
def dmp_fateman_poly_F_3(n, K):
    """Fateman's GCD benchmark: sparse inputs (deg f ~ vars f) """
    u = dup_from_raw_dict({n+1: K.one}, K)

    for i in xrange(0, n-1):
        u = dmp_add_term([u], dmp_one(i, K), n+1, i+1, K)

    v = dmp_add_term(u, dmp_ground(K(2), n-2), 0, n, K)

    f = dmp_sqr(dmp_add_term([dmp_neg(v, n-1, K)], dmp_one(n-1, K), n+1, n, K), n, K)
    g = dmp_sqr(dmp_add_term([v], dmp_one(n-1, K), n+1, n, K), n, K)

    v = dmp_add_term(u, dmp_one(n-2, K), 0, n-1, K)

    h = dmp_sqr(dmp_add_term([v], dmp_one(n-1, K), n+1, n, K), n, K)

    return dmp_mul(f, h, n, K), dmp_mul(g, h, n, K), h
Exemplo n.º 21
0
def test_dup_deflate():
    assert dup_deflate([], ZZ) == (1, [])
    assert dup_deflate([2], ZZ) == (1, [2])
    assert dup_deflate([1, 2, 3], ZZ) == (1, [1, 2, 3])
    assert dup_deflate([1, 0, 2, 0, 3], ZZ) == (2, [1, 2, 3])

    assert dup_deflate(dup_from_raw_dict({
        7: 1,
        1: 1
    }, ZZ), ZZ) == (
        1,
        [1, 0, 0, 0, 0, 0, 1, 0],
    )
    assert dup_deflate(dup_from_raw_dict({7: 1, 0: 1}, ZZ), ZZ) == (7, [1, 1])
    assert dup_deflate(dup_from_raw_dict({
        7: 1,
        3: 1
    }, ZZ), ZZ) == (
        1,
        [1, 0, 0, 0, 1, 0, 0, 0],
    )

    assert dup_deflate(dup_from_raw_dict({
        7: 1,
        4: 1
    }, ZZ), ZZ) == (
        1,
        [1, 0, 0, 1, 0, 0, 0, 0],
    )
    assert dup_deflate(dup_from_raw_dict({
        8: 1,
        4: 1
    }, ZZ), ZZ) == (4, [1, 1, 0])

    assert dup_deflate(dup_from_raw_dict({8: 1}, ZZ), ZZ) == (8, [1, 0])
    assert dup_deflate(dup_from_raw_dict({7: 1}, ZZ), ZZ) == (7, [1, 0])
    assert dup_deflate(dup_from_raw_dict({1: 1}, ZZ), ZZ) == (1, [1, 0])
Exemplo n.º 22
0
def test_dup_zz_factor():
    assert dup_zz_factor([], ZZ) == (0, [])
    assert dup_zz_factor([7], ZZ) == (7, [])
    assert dup_zz_factor([-7], ZZ) == (-7, [])

    assert dup_zz_factor_sqf([], ZZ) == (0, [])
    assert dup_zz_factor_sqf([7], ZZ) == (7, [])
    assert dup_zz_factor_sqf([-7], ZZ) == (-7, [])

    assert dup_zz_factor([2,4], ZZ) == \
        (2, [([1, 2], 1)])
    assert dup_zz_factor_sqf([2,4], ZZ) == \
        (2, [([1, 2], 1)])

    f = [1, 0, 0, 1, 1]

    for i in xrange(0, 20):
        assert dup_zz_factor(f, ZZ) == (1, [(f, 1)])

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

    assert dup_zz_factor([18,12,2], ZZ) == \
        (2, [([3, 1], 2)])

    assert dup_zz_factor([-9,0,1], ZZ) == \
        (-1, [([3,-1], 1),
              ([3, 1], 1)])

    assert dup_zz_factor_sqf([-9,0,1], ZZ) == \
        (-1, [[3,-1],
              [3, 1]])

    assert dup_zz_factor([1,-6,11,-6], ZZ) == \
        (1, [([1,-3], 1),
             ([1,-2], 1),
             ([1,-1], 1)])

    assert dup_zz_factor_sqf([1,-6,11,-6], ZZ) == \
        (1, [[1,-3],
             [1,-2],
             [1,-1]])

    assert dup_zz_factor([3,10,13,10], ZZ) == \
        (1, [([1,2], 1),
             ([3,4,5], 1)])

    assert dup_zz_factor_sqf([3,10,13,10], ZZ) == \
        (1, [[1,2],
             [3,4,5]])

    assert dup_zz_factor([-1,0,0,0,1,0,0], ZZ) == \
        (-1, [([1,-1], 1),
              ([1, 1], 1),
              ([1, 0], 2),
              ([1, 0, 1], 1)])

    f = [1080, 5184, 2099, 744, 2736, -648, 129, 0, -324]

    assert dup_zz_factor(f, ZZ) == \
        (1, [([5, 24, 9, 0, 12], 1),
             ([216, 0, 31, 0, -27], 1)])

    f = [
        -29802322387695312500000000000000000000, 0, 0, 0, 0,
        2980232238769531250000000000000000, 0, 0, 0, 0,
        1743435859680175781250000000000, 0, 0, 0, 0,
        114142894744873046875000000, 0, 0, 0, 0, -210106372833251953125, 0, 0,
        0, 0, 95367431640625
    ]

    assert dup_zz_factor(f, ZZ) == \
        (-95367431640625, [([5, -1], 1),
                           ([100, 10, -1], 2),
                           ([625, 125, 25, 5, 1], 1),
                           ([10000, -3000, 400, -20, 1], 2),
                           ([10000,  2000, 400,  30, 1], 2)])

    f = dup_from_raw_dict({10: 1, 0: -1}, ZZ)

    F_0 = dup_zz_factor(f, ZZ, cyclotomic=True)
    F_1 = dup_zz_factor(f, ZZ, cyclotomic=False)

    assert F_0 == F_1 == \
        (1, [([1,-1], 1),
             ([1, 1], 1),
             ([1,-1, 1,-1, 1], 1),
             ([1, 1, 1, 1, 1], 1)])

    f = dup_from_raw_dict({10: 1, 0: 1}, ZZ)

    F_0 = dup_zz_factor(f, ZZ, cyclotomic=True)
    F_1 = dup_zz_factor(f, ZZ, cyclotomic=False)

    assert F_0 == F_1 == \
        (1, [([1, 0, 1], 1),
             ([1, 0, -1, 0, 1, 0, -1, 0, 1], 1)])
Exemplo n.º 23
0
def test_dup_zz_factor():
    assert dup_zz_factor([], ZZ) == (0, [])
    assert dup_zz_factor([7], ZZ) == (7, [])
    assert dup_zz_factor([-7], ZZ) == (-7, [])

    assert dup_zz_factor_sqf([], ZZ) == (0, [])
    assert dup_zz_factor_sqf([7], ZZ) == (7, [])
    assert dup_zz_factor_sqf([-7], ZZ) == (-7, [])

    assert dup_zz_factor([2,4], ZZ) == \
        (2, [([1, 2], 1)])
    assert dup_zz_factor_sqf([2,4], ZZ) == \
        (2, [([1, 2], 1)])

    f = [1,0,0,1,1]

    for i in xrange(0, 20):
        assert dup_zz_factor(f, ZZ) == (1, [(f, 1)])

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

    assert dup_zz_factor([18,12,2], ZZ) == \
        (2, [([3, 1], 2)])

    assert dup_zz_factor([-9,0,1], ZZ) == \
        (-1, [([3,-1], 1),
              ([3, 1], 1)])

    assert dup_zz_factor_sqf([-9,0,1], ZZ) == \
        (-1, [[3,-1],
              [3, 1]])

    assert dup_zz_factor([1,-6,11,-6], ZZ) == \
        (1, [([1,-3], 1),
             ([1,-2], 1),
             ([1,-1], 1)])

    assert dup_zz_factor_sqf([1,-6,11,-6], ZZ) == \
        (1, [[1,-3],
             [1,-2],
             [1,-1]])

    assert dup_zz_factor([3,10,13,10], ZZ) == \
        (1, [([1,2], 1),
             ([3,4,5], 1)])

    assert dup_zz_factor_sqf([3,10,13,10], ZZ) == \
        (1, [[1,2],
             [3,4,5]])

    assert dup_zz_factor([-1,0,0,0,1,0,0], ZZ) == \
        (-1, [([1,-1], 1),
              ([1, 1], 1),
              ([1, 0], 2),
              ([1, 0, 1], 1)])

    f = [1080, 5184, 2099, 744, 2736, -648, 129, 0, -324]

    assert dup_zz_factor(f, ZZ) == \
        (1, [([5, 24, 9, 0, 12], 1),
             ([216, 0, 31, 0, -27], 1)])

    f = [-29802322387695312500000000000000000000,
          0, 0, 0, 0,
          2980232238769531250000000000000000,
          0, 0, 0, 0,
          1743435859680175781250000000000,
          0, 0, 0, 0,
          114142894744873046875000000,
          0, 0, 0, 0,
          -210106372833251953125,
          0, 0, 0, 0,
          95367431640625]

    assert dup_zz_factor(f, ZZ) == \
        (-95367431640625, [([5, -1], 1),
                           ([100, 10, -1], 2),
                           ([625, 125, 25, 5, 1], 1),
                           ([10000, -3000, 400, -20, 1], 2),
                           ([10000,  2000, 400,  30, 1], 2)])

    f = dup_from_raw_dict({10:1, 0:-1}, ZZ)

    config.setup('USE_CYCLOTOMIC_FACTOR', True)
    F_0 = dup_zz_factor(f, ZZ)

    config.setup('USE_CYCLOTOMIC_FACTOR', False)
    F_1 = dup_zz_factor(f, ZZ)

    assert F_0 == F_1 == \
        (1, [([1,-1], 1),
             ([1, 1], 1),
             ([1,-1, 1,-1, 1], 1),
             ([1, 1, 1, 1, 1], 1)])

    config.setup('USE_CYCLOTOMIC_FACTOR')

    f = dup_from_raw_dict({10:1, 0:1}, ZZ)

    config.setup('USE_CYCLOTOMIC_FACTOR', True)
    F_0 = dup_zz_factor(f, ZZ)

    config.setup('USE_CYCLOTOMIC_FACTOR', False)
    F_1 = dup_zz_factor(f, ZZ)

    assert F_0 == F_1 == \
        (1, [([1, 0, 1], 1),
             ([1, 0, -1, 0, 1, 0, -1, 0, 1], 1)])

    config.setup('USE_CYCLOTOMIC_FACTOR')