Exemple #1
0
def mod_xp(position, fcoeff, a1):  # mod_xp(p) is x^p mod f(x)
    # multiply elements one by one
    result = np.poly1d(1)
    for x in position:
        result = np.poly1d((result * np.poly1d(x_powermod(x, fcoeff, a1)) ).coeffs % p)
        result = quot_remain_p(ZZ.map(np.poly1d(result).coeffs), ZZ.map(fcoeff), p, ZZ)[1]
    return result
def test_dup_cancel():
    f = ZZ.map([2, 0, -2])
    g = ZZ.map([1, -2, 1])

    p = [ZZ(2), ZZ(2)]
    q = [ZZ(1), -ZZ(1)]

    assert dup_cancel(f, g, ZZ) == (p, q)
    assert dup_cancel(f, g, ZZ, include=False) == (ZZ(1), ZZ(1), p, q)

    f = [-ZZ(1), -ZZ(2)]
    g = [ ZZ(3), -ZZ(4)]

    F = [ ZZ(1), ZZ(2)]
    G = [-ZZ(3), ZZ(4)]

    assert dup_cancel(f, g, ZZ) == (f, g)
    assert dup_cancel(F, G, ZZ) == (f, g)

    assert dup_cancel([], [], ZZ) == ([], [])
    assert dup_cancel([], [], ZZ, include=False) == (ZZ(1), ZZ(1), [], [])

    assert dup_cancel([ZZ(1), ZZ(0)], [], ZZ) == ([ZZ(1)], [])
    assert dup_cancel(
        [ZZ(1), ZZ(0)], [], ZZ, include=False) == (ZZ(1), ZZ(1), [ZZ(1)], [])

    assert dup_cancel([], [ZZ(1), ZZ(0)], ZZ) == ([], [ZZ(1)])
    assert dup_cancel(
        [], [ZZ(1), ZZ(0)], ZZ, include=False) == (ZZ(1), ZZ(1), [], [ZZ(1)])
Exemple #3
0
def x_powermod(k, fcoeff, a1):  # this function genrates x^(2^k) mod function f(x)
    if (k > 0):
        p1 = np.poly1d(x_powermod((k - 1), fcoeff, a1)) ** 2
        result = quot_remain_p(ZZ.map(p1.coeffs), ZZ.map(fcoeff), p, ZZ)[1]
    else:
        result = a1
    return [item % p for item in result]
Exemple #4
0
def test_dup_cancel():
    f = ZZ.map([2, 0, -2])
    g = ZZ.map([1, -2, 1])

    p = [ZZ(2), ZZ(2)]
    q = [ZZ(1), -ZZ(1)]

    assert dup_cancel(f, g, ZZ) == (p, q)
    assert dup_cancel(f, g, ZZ, include=False) == (ZZ(1), ZZ(1), p, q)

    f = [-ZZ(1), -ZZ(2)]
    g = [ZZ(3), -ZZ(4)]

    F = [ZZ(1), ZZ(2)]
    G = [-ZZ(3), ZZ(4)]

    assert dup_cancel(f, g, ZZ) == (f, g)
    assert dup_cancel(F, G, ZZ) == (f, g)

    assert dup_cancel([], [], ZZ) == ([], [])
    assert dup_cancel([], [], ZZ, include=False) == (ZZ(1), ZZ(1), [], [])

    assert dup_cancel([ZZ(1), ZZ(0)], [], ZZ) == ([ZZ(1)], [])
    assert dup_cancel([ZZ(1), ZZ(0)], [], ZZ,
                      include=False) == (ZZ(1), ZZ(1), [ZZ(1)], [])

    assert dup_cancel([], [ZZ(1), ZZ(0)], ZZ) == ([], [ZZ(1)])
    assert dup_cancel([], [ZZ(1), ZZ(0)], ZZ,
                      include=False) == (ZZ(1), ZZ(1), [], [ZZ(1)])
Exemple #5
0
def test_gf_berlekamp():
    f = gf_from_int_poly([1, -3, 1, -3, -1, -3, 1], 11)

    Q = [[1, 0, 0, 0, 0, 0], [3, 5, 8, 8, 6, 5], [3, 6, 6, 1, 10, 0],
         [9, 4, 10, 3, 7, 9], [7, 8, 10, 0, 0, 8], [8, 10, 7, 8, 10, 8]]

    V = [[1, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 0], [0, 0, 7, 9, 0, 1]]

    assert gf_Qmatrix(f, 11, ZZ) == Q
    assert gf_Qbasis(Q, 11, ZZ) == V

    assert gf_berlekamp(f, 11, ZZ) == \
        [[1, 1], [1, 5, 3], [1, 2, 3, 4]]

    f = ZZ.map([1, 0, 1, 0, 10, 10, 8, 2, 8])

    Q = ZZ.map([[1, 0, 0, 0, 0, 0, 0, 0], [2, 1, 7, 11, 10, 12, 5, 11],
                [3, 6, 4, 3, 0, 4, 7, 2], [4, 3, 6, 5, 1, 6, 2, 3],
                [2, 11, 8, 8, 3, 1, 3, 11], [6, 11, 8, 6, 2, 7, 10, 9],
                [5, 11, 7, 10, 0, 11, 7, 12], [3, 3, 12, 5, 0, 11, 9, 12]])

    V = [[1, 0, 0, 0, 0, 0, 0, 0], [0, 5, 5, 0, 9, 5, 1, 0],
         [0, 9, 11, 9, 10, 12, 0, 1]]

    assert gf_Qmatrix(f, 13, ZZ) == Q
    assert gf_Qbasis(Q, 13, ZZ) == V

    assert gf_berlekamp(f, 13, ZZ) == \
        [[1, 3], [1, 8, 4, 12], [1, 2, 3, 4, 6]]
Exemple #6
0
def test_dup_count_complex_roots_1():
    # z-1
    assert dup_count_complex_roots(ZZ.map([1, -1]), ZZ, a, b) == 1
    assert dup_count_complex_roots(ZZ.map([1, -1]), ZZ, c, d) == 1

    # z+1
    assert dup_count_complex_roots(ZZ.map([1, 1]), ZZ, a, b) == 1
    assert dup_count_complex_roots(ZZ.map([1, 1]), ZZ, c, d) == 0
Exemple #7
0
def test_gf_monic():
    assert gf_monic(ZZ.map([]), 11, ZZ) == (0, [])

    assert gf_monic(ZZ.map([1]), 11, ZZ) == (1, [1])
    assert gf_monic(ZZ.map([2]), 11, ZZ) == (2, [1])

    assert gf_monic(ZZ.map([1, 2, 3, 4]), 11, ZZ) == (1, [1, 2, 3, 4])
    assert gf_monic(ZZ.map([2, 3, 4, 5]), 11, ZZ) == (2, [1, 7, 2, 8])
Exemple #8
0
def test_gf_monic():
    assert gf_monic(ZZ.map([]), 11, ZZ) == (0, [])

    assert gf_monic(ZZ.map([1]), 11, ZZ) == (1, [1])
    assert gf_monic(ZZ.map([2]), 11, ZZ) == (2, [1])

    assert gf_monic(ZZ.map([1, 2, 3, 4]), 11, ZZ) == (1, [1, 2, 3, 4])
    assert gf_monic(ZZ.map([2, 3, 4, 5]), 11, ZZ) == (2, [1, 7, 2, 8])
Exemple #9
0
def test_dup_count_complex_roots_1():
    # z-1
    assert dup_count_complex_roots(ZZ.map([1, -1]), ZZ, a, b) == 1
    assert dup_count_complex_roots(ZZ.map([1, -1]), ZZ, c, d) == 1

    # z+1
    assert dup_count_complex_roots(ZZ.map([1, 1]), ZZ, a, b) == 1
    assert dup_count_complex_roots(ZZ.map([1, 1]), ZZ, c, d) == 0
Exemple #10
0
def test_Domain_map():
    seq = ZZ.map([1, 2, 3, 4])

    assert all([ ZZ.of_type(elt) for elt in seq ])

    seq = ZZ.map([[1, 2, 3, 4]])

    assert all([ ZZ.of_type(elt) for elt in seq[0] ]) and len(seq) == 1
Exemple #11
0
def test_Domain_map():
    seq = ZZ.map([1, 2, 3, 4])

    assert all(ZZ.of_type(elt) for elt in seq)

    seq = ZZ.map([[1, 2, 3, 4]])

    assert all(ZZ.of_type(elt) for elt in seq[0]) and len(seq) == 1
Exemple #12
0
def test_gf_frobenius_map():
    f = ZZ.map([2, 0, 1, 0, 2, 2, 0, 2, 2, 2])
    g = ZZ.map([1, 1, 0, 2, 0, 1, 0, 2, 0, 1])
    p = 3
    b = gf_frobenius_monomial_base(g, p, ZZ)
    h = gf_frobenius_map(f, g, b, p, ZZ)
    h1 = gf_pow_mod(f, p, g, p, ZZ)
    assert h == h1
Exemple #13
0
def test_dmp_cancel():
    f = ZZ.map([[2], [0], [-2]])
    g = ZZ.map([[1], [-2], [1]])

    p = [[ZZ(2)], [ZZ(2)]]
    q = [[ZZ(1)], [-ZZ(1)]]

    assert dmp_cancel(f, g, 1, ZZ) == (p, q)
    assert dmp_cancel(f, g, 1, ZZ, include=False) == (ZZ(1), ZZ(1), p, q)
Exemple #14
0
def test_dup_primitive_prs():
    f = ZZ.map([1, 0, 1, 0, -3, -3, 8, 2, -5])
    g = ZZ.map([3, 0, 5, 0, -4, -9, 21])

    assert dup_primitive_prs(f, g, ZZ) == [
        f, g, [-ZZ(5), ZZ(0), ZZ(1), ZZ(0), -ZZ(3)], [ZZ(13),
                                                      ZZ(25), -ZZ(49)],
        [ZZ(4663), -ZZ(6150)], [ZZ(1)]
    ]
Exemple #15
0
def test_gf_frobenius_map():
    f = ZZ.map([2, 0, 1, 0, 2, 2, 0, 2, 2, 2])
    g = ZZ.map([1,1,0,2,0,1,0,2,0,1])
    p = 3
    n = 4
    b = gf_frobenius_monomial_base(g, p, ZZ)
    h = gf_frobenius_map(f, g, b, p, ZZ)
    h1 = gf_pow_mod(f, p, g, p, ZZ)
    assert h == h1
Exemple #16
0
def test_dmp_cancel():
    f = ZZ.map([[2], [0], [-2]])
    g = ZZ.map([[1], [-2], [1]])

    p = [[ZZ(2)], [ZZ(2)]]
    q = [[ZZ(1)], [-ZZ(1)]]

    assert dmp_cancel(f, g, 1, ZZ) == (p, q)
    assert dmp_cancel(f, g, 1, ZZ, multout=False) == (ZZ(1), ZZ(1), p, q)
Exemple #17
0
def test_dup_primitive_prs():
    f = ZZ.map([1, 0, 1, 0, -3, -3, 8, 2, -5])
    g = ZZ.map([3, 0, 5, 0, -4, -9, 21])

    assert dup_primitive_prs(f, g, ZZ) == [f, g,
        [-ZZ(5), ZZ(0), ZZ(1), ZZ(0), -ZZ(3)],
        [ZZ(13), ZZ(25), -ZZ(49)],
        [ZZ(4663), -ZZ(6150)],
        [ZZ(1)]]
Exemple #18
0
 def x_powermod(self, k, fcoeff,
                a1):  # this function genrates x^(2^k) mod function f(x)
     if (k > 0):
         p1 = np.poly1d((np.poly1d(self.x_powermod(
             (k - 1), fcoeff, a1))**2).coeffs % self.p)
         result = self.quot_remain_p(ZZ.map(p1.coeffs), ZZ.map(fcoeff),
                                     self.p, ZZ)[1]
     else:
         result = a1
     return [item % self.p for item in result]
def test_dup_lcm():
    assert dup_lcm([2], [6], ZZ) == [6]

    assert dup_lcm([2, 0, 0, 0], [6, 0], ZZ) == [6, 0, 0, 0]
    assert dup_lcm([2, 0, 0, 0], [3, 0], ZZ) == [6, 0, 0, 0]

    assert dup_lcm(ZZ.map([1, 1, 0]), ZZ.map([1, 0]), ZZ) == [1, 1, 0]
    assert dup_lcm(ZZ.map([1, 1, 0]), ZZ.map([2, 0]), ZZ) == [2, 2, 0]
    assert dup_lcm(ZZ.map([1, 2, 0]), ZZ.map([1, 0]), ZZ) == [1, 2, 0]
    assert dup_lcm(ZZ.map([2, 1, 0]), ZZ.map([1, 0]), ZZ) == [2, 1, 0]
    assert dup_lcm(ZZ.map([2, 1, 0]), ZZ.map([2, 0]), ZZ) == [4, 2, 0]
def inverse(f):
    # Fast inverse method
    global phid
    global q
    p = ZZ.map(f)
    mod = ZZ.map(phid)
    s, t, g = gf_gcdex(p, mod, q, ZZ)
    if len(g) == 1 and g[0] == 1:
        return s
    else:
        return [-1]
Exemple #21
0
def test_dup_lcm():
    assert dup_lcm([2], [6], ZZ) == [6]

    assert dup_lcm([2, 0, 0, 0], [6, 0], ZZ) == [6, 0, 0, 0]
    assert dup_lcm([2, 0, 0, 0], [3, 0], ZZ) == [6, 0, 0, 0]

    assert dup_lcm(ZZ.map([1, 1, 0]), ZZ.map([1, 0]), ZZ) == [1, 1, 0]
    assert dup_lcm(ZZ.map([1, 1, 0]), ZZ.map([2, 0]), ZZ) == [2, 2, 0]
    assert dup_lcm(ZZ.map([1, 2, 0]), ZZ.map([1, 0]), ZZ) == [1, 2, 0]
    assert dup_lcm(ZZ.map([2, 1, 0]), ZZ.map([1, 0]), ZZ) == [2, 1, 0]
    assert dup_lcm(ZZ.map([2, 1, 0]), ZZ.map([2, 0]), ZZ) == [4, 2, 0]
Exemple #22
0
def test_dup_count_complex_roots_8():
    # (z-I-1)*(z+I-1)*(z-I+1)*(z+I+1)*(z-1)*(z+1)*(z-I)*(z+I)*z
    assert dup_count_complex_roots(ZZ.map([1, 0, 0, 0, 3, 0, 0, 0, -4, 0]),
        ZZ, a, b) == 9
    assert dup_count_complex_roots(ZZ.map([1, 0, 0, 0, 3, 0, 0, 0, -4, 0]),
        ZZ, c, d) == 4

    # (z-I-1)*(z+I-1)*(z-I+1)*(z+I+1)*(z-1)*(z+1)*(z-I)*(z+I)*(z**2-2)*z
    assert dup_count_complex_roots(ZZ.map(
        [1, 0, -2, 0, 3, 0, -6, 0, -4, 0, 8, 0]), ZZ, a, b) == 9
    assert dup_count_complex_roots(ZZ.map(
        [1, 0, -2, 0, 3, 0, -6, 0, -4, 0, 8, 0]), ZZ, c, d) == 4
Exemple #23
0
def recursion(k, fcoeff, re):
    gcd = [1]
    if k > 1:
        gcd = quot_remain_p(ZZ.map(recursion((k - 2), fcoeff, re)), ZZ.map(recursion((k - 1), fcoeff, re)), p, ZZ)[1]
    elif any(re) == False:
        gcd = fcoeff
    else:
        if k == 0:
            gcd = re
        else:
            gcd = quot_remain_p(ZZ.map(fcoeff), ZZ.map(re), p, ZZ)[1]
    return [item % p for item in gcd]
Exemple #24
0
def test_dup_count_complex_roots_8():
    # (z-I-1)*(z+I-1)*(z-I+1)*(z+I+1)*(z-1)*(z+1)*(z-I)*(z+I)*z
    assert dup_count_complex_roots(ZZ.map([1, 0, 0, 0, 3, 0, 0, 0, -4, 0]), ZZ,
                                   a, b) == 9
    assert dup_count_complex_roots(ZZ.map([1, 0, 0, 0, 3, 0, 0, 0, -4, 0]), ZZ,
                                   c, d) == 4

    # (z-I-1)*(z+I-1)*(z-I+1)*(z+I+1)*(z-1)*(z+1)*(z-I)*(z+I)*(z**2-2)*z
    assert dup_count_complex_roots(
        ZZ.map([1, 0, -2, 0, 3, 0, -6, 0, -4, 0, 8, 0]), ZZ, a, b) == 9
    assert dup_count_complex_roots(
        ZZ.map([1, 0, -2, 0, 3, 0, -6, 0, -4, 0, 8, 0]), ZZ, c, d) == 4
Exemple #25
0
def roots_f(startP, g_x, resss):
    if np.poly1d(g_x).order == 1:
        resss.append(monic(g_x))
        return resss
    else:
        r = GCD(startP, P, Y, g_x)
        if np.poly1d(r).order < np.poly1d(g_x).order and np.poly1d(r).order != 0:
            resss.append(monic(r))
            g_x = quot_remain_p(ZZ.map(g_x), ZZ.map(np.poly1d(monic(r))), p, ZZ)[0]
            return roots_f(startP, g_x, resss)
        else:
            startP = rand_startP()
            return roots_f(startP, g_x, resss)
Exemple #26
0
def test_gf_trace_map():
    f = ZZ.map([1, 1, 4, 9, 1])
    a = [1, 1, 1]
    c = ZZ.map([1, 0])
    b = gf_pow_mod(c, 11, f, 11, ZZ)

    assert gf_trace_map(a, b, c, 0, f, 11, ZZ) == ([1, 1, 1], [1, 1, 1])
    assert gf_trace_map(a, b, c, 1, f, 11, ZZ) == ([5, 2, 10, 3], [5, 3, 0, 4])
    assert gf_trace_map(a, b, c, 2, f, 11, ZZ) == ([5, 9, 5, 3], [10, 1, 5, 7])
    assert gf_trace_map(a, b, c, 3, f, 11, ZZ) == ([1, 10, 6, 0], [7])
    assert gf_trace_map(a, b, c, 4, f, 11, ZZ) == ([1, 1, 1], [1, 1, 8])
    assert gf_trace_map(a, b, c, 5, f, 11, ZZ) == ([5, 2, 10, 3], [5, 3, 0, 0])
    assert gf_trace_map(a, b, c, 11, f, 11, ZZ) == ([1, 10, 6, 0], [10])
Exemple #27
0
def find_inv(poly, mod, constant = -a):
    xN_minus_a = np.zeros(N+1)
    xN_minus_a[0] = 1
    xN_minus_a[-1] = constant

    f_poly = ZZ.map(poly)
    x_mod = ZZ.map(xN_minus_a)
    s, t, g = gf_gcdex(f_poly, x_mod, mod, ZZ)

    # Recall that s*f + t*g = h. 
    # If g = 1, then s is the inverse of f mod t (= x^n-a)
    if len(g) == 1 and g[0] == 1:
        return trunc_polynomial(s)
    return trunc_polynomial(0)
Exemple #28
0
def test_gf_compose():
    assert gf_compose([], [1, 0], 11, ZZ) == []
    assert gf_compose_mod([], [1, 0], [1, 0], 11, ZZ) == []

    assert gf_compose([1], [], 11, ZZ) == [1]
    assert gf_compose([1, 0], [], 11, ZZ) == []
    assert gf_compose([1, 0], [1, 0], 11, ZZ) == [1, 0]

    f = ZZ.map([1, 1, 4, 9, 1])
    g = ZZ.map([1, 1, 1])
    h = ZZ.map([1, 0, 0, 2])

    assert gf_compose(g, h, 11, ZZ) == [1, 0, 0, 5, 0, 0, 7]
    assert gf_compose_mod(g, h, f, 11, ZZ) == [3, 9, 6, 10]
Exemple #29
0
def test_gf_compose():
    assert gf_compose([], [1, 0], 11, ZZ) == []
    assert gf_compose_mod([], [1, 0], [1, 0], 11, ZZ) == []

    assert gf_compose([1], [], 11, ZZ) == [1]
    assert gf_compose([1, 0], [], 11, ZZ) == []
    assert gf_compose([1, 0], [1, 0], 11, ZZ) == [1, 0]

    f = ZZ.map([1, 1, 4, 9, 1])
    g = ZZ.map([1, 1, 1])
    h = ZZ.map([1, 0, 0, 2])

    assert gf_compose(g, h, 11, ZZ) == [1, 0, 0, 5, 0, 0, 7]
    assert gf_compose_mod(g, h, f, 11, ZZ) == [3, 9, 6, 10]
Exemple #30
0
def test_dup_count_real_roots():
    assert dup_count_real_roots([], ZZ) == 0
    assert dup_count_real_roots([7], ZZ) == 0

    assert dup_count_real_roots(ZZ.map([1, -1]), ZZ) == 1
    assert dup_count_real_roots(ZZ.map([1, -1]), ZZ, inf=1) == 1
    assert dup_count_real_roots(ZZ.map([1, -1]), ZZ, sup=0) == 0
    assert dup_count_real_roots(ZZ.map([1, -1]), ZZ, sup=1) == 1
    assert dup_count_real_roots(ZZ.map([1, -1]), ZZ, inf=0, sup=1) == 1
    assert dup_count_real_roots(ZZ.map([1, -1]), ZZ, inf=0, sup=2) == 1
    assert dup_count_real_roots(ZZ.map([1, -1]), ZZ, inf=1, sup=2) == 1

    assert dup_count_real_roots(ZZ.map([1, 0, -2]), ZZ) == 2
    assert dup_count_real_roots(ZZ.map([1, 0, -2]), ZZ, sup=0) == 1
    assert dup_count_real_roots(ZZ.map([1, 0, -2]), ZZ, inf=-1, sup=1) == 0
Exemple #31
0
def test_dup_count_real_roots():
    assert dup_count_real_roots([], ZZ) == 0
    assert dup_count_real_roots([7], ZZ) == 0

    assert dup_count_real_roots(ZZ.map([1, -1]), ZZ) == 1
    assert dup_count_real_roots(ZZ.map([1, -1]), ZZ, inf=1) == 1
    assert dup_count_real_roots(ZZ.map([1, -1]), ZZ, sup=0) == 0
    assert dup_count_real_roots(ZZ.map([1, -1]), ZZ, sup=1) == 1
    assert dup_count_real_roots(ZZ.map([1, -1]), ZZ, inf=0, sup=1) == 1
    assert dup_count_real_roots(ZZ.map([1, -1]), ZZ, inf=0, sup=2) == 1
    assert dup_count_real_roots(ZZ.map([1, -1]), ZZ, inf=1, sup=2) == 1

    assert dup_count_real_roots(ZZ.map([1, 0, -2]), ZZ) == 2
    assert dup_count_real_roots(ZZ.map([1, 0, -2]), ZZ, sup=0) == 1
    assert dup_count_real_roots(ZZ.map([1, 0, -2]), ZZ, inf=-1, sup=1) == 0
Exemple #32
0
def test_dup_count_complex_roots_exclude():
    f = ZZ.map([1, 0, 0, 0, -1, 0])  # z*(z-1)*(z+1)*(z-I)*(z+I)

    a, b = (-QQ(1), QQ(0)), (QQ(1), QQ(1))

    assert dup_count_complex_roots(f, ZZ, a, b) == 4

    assert dup_count_complex_roots(f, ZZ, a, b, exclude=['S']) == 3
    assert dup_count_complex_roots(f, ZZ, a, b, exclude=['N']) == 3

    assert dup_count_complex_roots(f, ZZ, a, b, exclude=['S', 'N']) == 2

    assert dup_count_complex_roots(f, ZZ, a, b, exclude=['E']) == 4
    assert dup_count_complex_roots(f, ZZ, a, b, exclude=['W']) == 4

    assert dup_count_complex_roots(f, ZZ, a, b, exclude=['E', 'W']) == 4

    assert dup_count_complex_roots(f, ZZ, a, b, exclude=['N', 'S', 'E',
                                                         'W']) == 2

    assert dup_count_complex_roots(f, ZZ, a, b, exclude=['SW']) == 3
    assert dup_count_complex_roots(f, ZZ, a, b, exclude=['SE']) == 3

    assert dup_count_complex_roots(f, ZZ, a, b, exclude=['SW', 'SE']) == 2
    assert dup_count_complex_roots(f, ZZ, a, b, exclude=['SW', 'SE', 'S']) == 1
    assert dup_count_complex_roots(f, ZZ, a, b, exclude=['SW', 'SE', 'S',
                                                         'N']) == 0

    a, b = (QQ(0), QQ(0)), (QQ(1), QQ(1))

    assert dup_count_complex_roots(f, ZZ, a, b, exclude=True) == 1
Exemple #33
0
def test_dup_count_complex_roots_exclude():
    f = ZZ.map([1, 0, 0, 0, -1, 0])  # z*(z-1)*(z+1)*(z-I)*(z+I)

    a, b = (-QQ(1), QQ(0)), (QQ(1), QQ(1))

    assert dup_count_complex_roots(f, ZZ, a, b) == 4

    assert dup_count_complex_roots(f, ZZ, a, b, exclude=['S']) == 3
    assert dup_count_complex_roots(f, ZZ, a, b, exclude=['N']) == 3

    assert dup_count_complex_roots(f, ZZ, a, b, exclude=['S', 'N']) == 2

    assert dup_count_complex_roots(f, ZZ, a, b, exclude=['E']) == 4
    assert dup_count_complex_roots(f, ZZ, a, b, exclude=['W']) == 4

    assert dup_count_complex_roots(f, ZZ, a, b, exclude=['E', 'W']) == 4

    assert dup_count_complex_roots(
        f, ZZ, a, b, exclude=['N', 'S', 'E', 'W']) == 2

    assert dup_count_complex_roots(f, ZZ, a, b, exclude=['SW']) == 3
    assert dup_count_complex_roots(f, ZZ, a, b, exclude=['SE']) == 3

    assert dup_count_complex_roots(f, ZZ, a, b, exclude=['SW', 'SE']) == 2
    assert dup_count_complex_roots(f, ZZ, a, b, exclude=['SW', 'SE', 'S']) == 1
    assert dup_count_complex_roots(
        f, ZZ, a, b, exclude=['SW', 'SE', 'S', 'N']) == 0

    a, b = (QQ(0), QQ(0)), (QQ(1), QQ(1))

    assert dup_count_complex_roots(f, ZZ, a, b, exclude=True) == 1
Exemple #34
0
def test_dup_count_complex_roots_implicit():
    f = ZZ.map([1, 0, 0, 0, -1, 0])  # z*(z-1)*(z+1)*(z-I)*(z+I)

    assert dup_count_complex_roots(f, ZZ) == 5

    assert dup_count_complex_roots(f, ZZ, sup=(0, 0)) == 3
    assert dup_count_complex_roots(f, ZZ, inf=(0, 0)) == 3
Exemple #35
0
def test_dup_count_complex_roots_implicit():
    f = ZZ.map([1, 0, 0, 0, -1, 0])  # z*(z-1)*(z+1)*(z-I)*(z+I)

    assert dup_count_complex_roots(f, ZZ) == 5

    assert dup_count_complex_roots(f, ZZ, sup=(0, 0)) == 3
    assert dup_count_complex_roots(f, ZZ, inf=(0, 0)) == 3
Exemple #36
0
def test_dup_cancel():
    f = ZZ.map([2, 0, -2])
    g = ZZ.map([1, -2, 1])

    p = [ZZ(2), ZZ(2)]
    q = [ZZ(1), -ZZ(1)]

    assert dup_cancel(f, g, ZZ) == (p, q)
    assert dup_cancel(f, g, ZZ, multout=False) == (ZZ(1), ZZ(1), p, q)

    f = [-ZZ(1),-ZZ(2)]
    g = [ ZZ(3),-ZZ(4)]

    F = [ ZZ(1), ZZ(2)]
    G = [-ZZ(3), ZZ(4)]

    dup_cancel(f, g, ZZ) == (f, g)
    dup_cancel(F, G, ZZ) == (f, g)
Exemple #37
0
def simple_factor(a, method='print_x2'):
    poly = np.zeros(a + 1, dtype=np.int32)
    poly[0] = 1
    poly[-1] = 1
    factor_p = factor(ZZ.map(poly), 5, ZZ)
    factor_temp = factor_p[1]
    length = len(factor_temp)
    for i in range(0, length):
        _print_methods[method](factor_temp[i])
Exemple #38
0
def test_dmp_cancel():
    f = ZZ.map([[2], [0], [-2]])
    g = ZZ.map([[1], [-2], [1]])

    p = [[ZZ(2)], [ZZ(2)]]
    q = [[ZZ(1)], [-ZZ(1)]]

    assert dmp_cancel(f, g, 1, ZZ) == (p, q)
    assert dmp_cancel(f, g, 1, ZZ, include=False) == (ZZ(1), ZZ(1), p, q)

    assert dmp_cancel([[]], [[]], 1, ZZ) == ([[]], [[]])
    assert dmp_cancel([[]], [[]], 1, ZZ, include=False) == (ZZ(1), ZZ(1), [[]], [[]])

    assert dmp_cancel([[ZZ(1), ZZ(0)]], [[]], 1, ZZ) == ([[ZZ(1)]], [[]])
    assert dmp_cancel([[ZZ(1), ZZ(0)]], [[]], 1, ZZ, include=False) == (ZZ(1), ZZ(1), [[ZZ(1)]], [[]])

    assert dmp_cancel([[]], [[ZZ(1), ZZ(0)]], 1, ZZ) == ([[]], [[ZZ(1)]])
    assert dmp_cancel([[]], [[ZZ(1), ZZ(0)]], 1, ZZ, include=False) == (ZZ(1), ZZ(1), [[]], [[ZZ(1)]])
Exemple #39
0
 def roots_f(self, startP, g_x, resss):
     if np.poly1d(g_x).order == 1:
         resss.append(self.monic(g_x))
         return resss
     else:
         P = (self.p - 1) / 2
         Y = [-1]
         r = self.GCD(startP, P, Y, g_x)
         if np.poly1d(r).order < np.poly1d(g_x).order and np.poly1d(
                 r).order != 0:
             resss.append(self.monic(r))
             g_x = self.quot_remain_p(ZZ.map(g_x),
                                      ZZ.map(np.poly1d(self.monic(r))),
                                      self.p, ZZ)[0]
             return self.roots_f(startP, g_x, resss)
         else:
             startP = self.rand_startP()
             return self.roots_f(startP, g_x, resss)
Exemple #40
0
def test_dup_cancel():
    f = ZZ.map([2, 0, -2])
    g = ZZ.map([1, -2, 1])

    p = [ZZ(2), ZZ(2)]
    q = [ZZ(1), -ZZ(1)]

    assert dup_cancel(f, g, ZZ) == (p, q)
    assert dup_cancel(f, g, ZZ, multout=False) == (ZZ(1), ZZ(1), p, q)

    f = [-ZZ(1), -ZZ(2)]
    g = [ZZ(3), -ZZ(4)]

    F = [ZZ(1), ZZ(2)]
    G = [-ZZ(3), ZZ(4)]

    dup_cancel(f, g, ZZ) == (f, g)
    dup_cancel(F, G, ZZ) == (f, g)
Exemple #41
0
def simple_factor(a):
    poly=np.zeros(a+1,dtype=np.int32)
    poly[0]=1
    poly[-1]=1
    factor_p=factor(ZZ.map(poly),5,ZZ)
    factor_temp=factor_p[1]
    length=len(factor_temp)
    for i in range(0,length):
       for j in range(0,factor_temp[i][1]):
          print_x(factor_temp[i][0])
Exemple #42
0
def test_dup_isolate_all_roots_sqf():
    f = ZZ.map([4, -1, 2, 5, 0])

    assert dup_isolate_all_roots_sqf(f, ZZ) == \
        ([(-1, 0), (0, 0)],
         [((0, -QQ(5, 2)), (QQ(5, 2), 0)), ((0, 0), (QQ(5, 2), QQ(5, 2)))])

    assert dup_isolate_all_roots_sqf(f, ZZ, eps=QQ(1, 10)) == \
        ([(QQ(-7, 8), QQ(-6, 7)), (0, 0)],
         [((QQ(35, 64), -QQ(35, 32)), (QQ(5, 8), -QQ(65, 64))),
          ((QQ(35, 64), QQ(65, 64)), (QQ(5, 8), QQ(35, 32)))])
Exemple #43
0
def test_dup_isolate_all_roots_sqf():
    f = ZZ.map([4, -1, 2, 5, 0])

    assert dup_isolate_all_roots_sqf(f, ZZ) == \
        ([(-1, 0), (0, 0)],
         [((0, -QQ(5, 2)), (QQ(5, 2), 0)), ((0, 0), (QQ(5, 2), QQ(5, 2)))])

    assert dup_isolate_all_roots_sqf(f, ZZ, eps=QQ(1, 10)) == \
        ([(QQ(-7, 8), QQ(-6, 7)), (0, 0)],
         [((QQ(35, 64), -QQ(35, 32)), (QQ(5, 8), -QQ(65, 64))),
          ((QQ(35, 64), QQ(65, 64)), (QQ(5, 8), QQ(35, 32)))])
Exemple #44
0
def test_gf_trace_map():
    f = ZZ.map([1, 1, 4, 9, 1])
    a = [1, 1, 1]
    c = ZZ.map([1, 0])
    b = gf_pow_mod(c, 11, f, 11, ZZ)

    assert gf_trace_map(a, b, c, 0, f, 11, ZZ) == \
        ([1, 1, 1], [1, 1, 1])
    assert gf_trace_map(a, b, c, 1, f, 11, ZZ) == \
        ([5, 2, 10, 3], [5, 3, 0, 4])
    assert gf_trace_map(a, b, c, 2, f, 11, ZZ) == \
        ([5, 9, 5, 3], [10, 1, 5, 7])
    assert gf_trace_map(a, b, c, 3, f, 11, ZZ) == \
        ([1, 10, 6, 0], [7])
    assert gf_trace_map(a, b, c, 4, f, 11, ZZ) == \
        ([1, 1, 1], [1, 1, 8])
    assert gf_trace_map(a, b, c, 5, f, 11, ZZ) == \
        ([5, 2, 10, 3], [5, 3, 0, 0])
    assert gf_trace_map(a, b, c, 11, f, 11, ZZ) == \
        ([1, 10, 6, 0], [10])
Exemple #45
0
def test_dup_isolate_complex_roots_sqf():
    f = ZZ.map([1, -2, 3])

    assert dup_isolate_complex_roots_sqf(f, ZZ) == \
        [((0, -6), (6, 0)), ((0, 0), (6, 6))]
    assert [ r.as_tuple() for r in dup_isolate_complex_roots_sqf(
        f, ZZ, blackbox=True) ] == \
        [((0, -6), (6, 0)), ((0, 0), (6, 6))]

    assert dup_isolate_complex_roots_sqf(f, ZZ, eps=QQ(1, 10)) == \
        [((QQ(15, 16), -QQ(3, 2)), (QQ(33, 32), -QQ(45, 32))),
         ((QQ(15, 16), QQ(45, 32)), (QQ(33, 32), QQ(3, 2)))]
    assert dup_isolate_complex_roots_sqf(f, ZZ, eps=QQ(1, 100)) == \
        [((QQ(255, 256), -QQ(363, 256)), (QQ(513, 512), -QQ(723, 512))),
         ((QQ(255, 256), QQ(723, 512)), (QQ(513, 512), QQ(363, 256)))]

    f = ZZ.map([7, -19, 20, 17, 20])

    assert dup_isolate_complex_roots_sqf(f, ZZ) == \
        [((-QQ(40, 7), -QQ(40, 7)), (0, 0)), ((-QQ(40, 7), 0), (0, QQ(40, 7))),
         ((0, -QQ(40, 7)), (QQ(40, 7), 0)), ((0, 0), (QQ(40, 7), QQ(40, 7)))]
Exemple #46
0
def test_dup_isolate_complex_roots_sqf():
    f = ZZ.map([1, -2, 3])

    assert dup_isolate_complex_roots_sqf(f, ZZ) == \
        [((0, -6), (6, 0)), ((0, 0), (6, 6))]
    assert [ r.as_tuple() for r in dup_isolate_complex_roots_sqf(
        f, ZZ, blackbox=True) ] == \
        [((0, -6), (6, 0)), ((0, 0), (6, 6))]

    assert dup_isolate_complex_roots_sqf(f, ZZ, eps=QQ(1, 10)) == \
        [((QQ(15, 16), -QQ(3, 2)), (QQ(33, 32), -QQ(45, 32))),
         ((QQ(15, 16), QQ(45, 32)), (QQ(33, 32), QQ(3, 2)))]
    assert dup_isolate_complex_roots_sqf(f, ZZ, eps=QQ(1, 100)) == \
        [((QQ(255, 256), -QQ(363, 256)), (QQ(513, 512), -QQ(723, 512))),
         ((QQ(255, 256), QQ(723, 512)), (QQ(513, 512), QQ(363, 256)))]

    f = ZZ.map([7, -19, 20, 17, 20])

    assert dup_isolate_complex_roots_sqf(f, ZZ) == \
        [((-QQ(40, 7), -QQ(40, 7)), (0, 0)), ((-QQ(40, 7), 0), (0, QQ(40, 7))),
         ((0, -QQ(40, 7)), (QQ(40, 7), 0)), ((0, 0), (QQ(40, 7), QQ(40, 7)))]
Exemple #47
0
def test_gf_division():
    raises(ZeroDivisionError, lambda: gf_div([1, 2, 3], [], 11, ZZ))
    raises(ZeroDivisionError, lambda: gf_rem([1, 2, 3], [], 11, ZZ))
    raises(ZeroDivisionError, lambda: gf_quo([1, 2, 3], [], 11, ZZ))
    raises(ZeroDivisionError, lambda: gf_quo([1, 2, 3], [], 11, ZZ))

    assert gf_div([1], [1, 2, 3], 7, ZZ) == ([], [1])
    assert gf_rem([1], [1, 2, 3], 7, ZZ) == [1]
    assert gf_quo([1], [1, 2, 3], 7, ZZ) == []

    f = ZZ.map([5, 4, 3, 2, 1, 0])
    g = ZZ.map([1, 2, 3])
    q = [5, 1, 0, 6]
    r = [3, 3]

    assert gf_div(f, g, 7, ZZ) == (q, r)
    assert gf_rem(f, g, 7, ZZ) == r
    assert gf_quo(f, g, 7, ZZ) == q

    raises(ExactQuotientFailed, lambda: gf_exquo(f, g, 7, ZZ))

    f = ZZ.map([5, 4, 3, 2, 1, 0])
    g = ZZ.map([1, 2, 3, 0])
    q = [5, 1, 0]
    r = [6, 1, 0]

    assert gf_div(f, g, 7, ZZ) == (q, r)
    assert gf_rem(f, g, 7, ZZ) == r
    assert gf_quo(f, g, 7, ZZ) == q

    raises(ExactQuotientFailed, lambda: gf_exquo(f, g, 7, ZZ))

    assert gf_quo(ZZ.map([1, 2, 1]), ZZ.map([1, 1]), 11, ZZ) == [1, 1]
Exemple #48
0
def test_gf_berlekamp():
    f = gf_from_int_poly([1, -3, 1, -3, -1, -3, 1], 11)

    Q = [[1, 0, 0, 0, 0, 0],
         [3, 5, 8, 8, 6, 5],
         [3, 6, 6, 1, 10, 0],
         [9, 4, 10, 3, 7, 9],
         [7, 8, 10, 0, 0, 8],
         [8, 10, 7, 8, 10, 8]]

    V = [[1, 0, 0, 0, 0, 0],
         [0, 1, 1, 1, 1, 0],
         [0, 0, 7, 9, 0, 1]]

    assert gf_Qmatrix(f, 11, ZZ) == Q
    assert gf_Qbasis(Q, 11, ZZ) == V

    assert gf_berlekamp(f, 11, ZZ) == \
        [[1, 1], [1, 5, 3], [1, 2, 3, 4]]

    f = ZZ.map([1, 0, 1, 0, 10, 10, 8, 2, 8])

    Q = ZZ.map([[1, 0, 0, 0, 0, 0, 0, 0],
         [2, 1, 7, 11, 10, 12, 5, 11],
         [3, 6, 4, 3, 0, 4, 7, 2],
         [4, 3, 6, 5, 1, 6, 2, 3],
         [2, 11, 8, 8, 3, 1, 3, 11],
         [6, 11, 8, 6, 2, 7, 10, 9],
         [5, 11, 7, 10, 0, 11, 7, 12],
         [3, 3, 12, 5, 0, 11, 9, 12]])

    V = [[1, 0, 0, 0, 0, 0, 0, 0],
         [0, 5, 5, 0, 9, 5, 1, 0],
         [0, 9, 11, 9, 10, 12, 0, 1]]

    assert gf_Qmatrix(f, 13, ZZ) == Q
    assert gf_Qbasis(Q, 13, ZZ) == V

    assert gf_berlekamp(f, 13, ZZ) == \
        [[1, 3], [1, 8, 4, 12], [1, 2, 3, 4, 6]]
Exemple #49
0
def test_gf_division():
    raises(ZeroDivisionError, lambda: gf_div([1, 2, 3], [], 11, ZZ))
    raises(ZeroDivisionError, lambda: gf_rem([1, 2, 3], [], 11, ZZ))
    raises(ZeroDivisionError, lambda: gf_quo([1, 2, 3], [], 11, ZZ))
    raises(ZeroDivisionError, lambda: gf_quo([1, 2, 3], [], 11, ZZ))

    assert gf_div([1], [1, 2, 3], 7, ZZ) == ([], [1])
    assert gf_rem([1], [1, 2, 3], 7, ZZ) == [1]
    assert gf_quo([1], [1, 2, 3], 7, ZZ) == []

    f = ZZ.map([5, 4, 3, 2, 1, 0])
    g = ZZ.map([1, 2, 3])
    q = [5, 1, 0, 6]
    r = [3, 3]

    assert gf_div(f, g, 7, ZZ) == (q, r)
    assert gf_rem(f, g, 7, ZZ) == r
    assert gf_quo(f, g, 7, ZZ) == q

    raises(ExactQuotientFailed, lambda: gf_exquo(f, g, 7, ZZ))

    f = ZZ.map([5, 4, 3, 2, 1, 0])
    g = ZZ.map([1, 2, 3, 0])
    q = [5, 1, 0]
    r = [6, 1, 0]

    assert gf_div(f, g, 7, ZZ) == (q, r)
    assert gf_rem(f, g, 7, ZZ) == r
    assert gf_quo(f, g, 7, ZZ) == q

    raises(ExactQuotientFailed, lambda: gf_exquo(f, g, 7, ZZ))

    assert gf_quo(ZZ.map([1, 2, 1]), ZZ.map([1, 1]), 11, ZZ) == [1, 1]
Exemple #50
0
def test_dup_count_complex_roots_2():
    # (z-1)*(z)
    assert dup_count_complex_roots(ZZ.map([1, -1, 0]), ZZ, a, b) == 2
    assert dup_count_complex_roots(ZZ.map([1, -1, 0]), ZZ, c, d) == 2

    # (z-1)*(-z)
    assert dup_count_complex_roots(ZZ.map([-1, 1, 0]), ZZ, a, b) == 2
    assert dup_count_complex_roots(ZZ.map([-1, 1, 0]), ZZ, c, d) == 2

    # (z+1)*(z)
    assert dup_count_complex_roots(ZZ.map([1, 1, 0]), ZZ, a, b) == 2
    assert dup_count_complex_roots(ZZ.map([1, 1, 0]), ZZ, c, d) == 1

    # (z+1)*(-z)
    assert dup_count_complex_roots(ZZ.map([-1, -1, 0]), ZZ, a, b) == 2
    assert dup_count_complex_roots(ZZ.map([-1, -1, 0]), ZZ, c, d) == 1
Exemple #51
0
def test_dup_count_complex_roots_2():
    # (z-1)*(z)
    assert dup_count_complex_roots(ZZ.map([1, -1, 0]), ZZ, a, b) == 2
    assert dup_count_complex_roots(ZZ.map([1, -1, 0]), ZZ, c, d) == 2

    # (z-1)*(-z)
    assert dup_count_complex_roots(ZZ.map([-1, 1, 0]), ZZ, a, b) == 2
    assert dup_count_complex_roots(ZZ.map([-1, 1, 0]), ZZ, c, d) == 2

    # (z+1)*(z)
    assert dup_count_complex_roots(ZZ.map([1, 1, 0]), ZZ, a, b) == 2
    assert dup_count_complex_roots(ZZ.map([1, 1, 0]), ZZ, c, d) == 1

    # (z+1)*(-z)
    assert dup_count_complex_roots(ZZ.map([-1, -1, 0]), ZZ, a, b) == 2
    assert dup_count_complex_roots(ZZ.map([-1, -1, 0]), ZZ, c, d) == 1
def test_dmp_lcm():
    assert dmp_lcm([[2]], [[6]], 1, ZZ) == [[6]]
    assert dmp_lcm([[1], []], [[1, 0]], 1, ZZ) == [[1, 0], []]

    assert dmp_lcm(
        [[2], [], [], []], [[6, 0, 0], []], 1, ZZ) == [[6, 0, 0], [], [], []]
    assert dmp_lcm(
        [[2], [], [], []], [[3, 0, 0], []], 1, ZZ) == [[6, 0, 0], [], [], []]

    assert dmp_lcm(
        [[1, 0], [], []], [[1, 0, 0], []], 1, ZZ) == [[1, 0, 0], [], []]

    f = ZZ.map([[2, -3, -2, 3, 0, 0], []])
    g = ZZ.map([[1, 0, -2, 0, 1, 0]])
    h = [[2, -3, -4, 6, 2, -3, 0, 0], []]

    assert dmp_lcm(f, g, 1, ZZ) == h

    f = ZZ.map([[1], [-3, 0], [-9, 0, 0], [-5, 0, 0, 0]])
    g = ZZ.map([[1], [6, 0], [12, 0, 0], [10, 0, 0, 0], [3, 0, 0, 0, 0]])
    h = [[1], [1, 0], [-18, 0, 0], [-50, 0, 0, 0], [-47, 0, 0, 0, 0], [-15, 0, 0, 0, 0, 0]]

    assert dmp_lcm(f, g, 1, ZZ) == h
Exemple #53
0
def test_gf_ddf():
    f = gf_from_dict({15: ZZ(1), 0: ZZ(-1)}, 11, ZZ)
    g = [([1, 0, 0, 0, 0, 10], 1),
         ([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1], 2)]

    assert gf_ddf_zassenhaus(f, 11, ZZ) == g
    assert gf_ddf_shoup(f, 11, ZZ) == g

    f = gf_from_dict({63: ZZ(1), 0: ZZ(1)}, 2, ZZ)
    g = [([1, 1], 1),
         ([1, 1, 1], 2),
         ([1, 1, 1, 1, 1, 1, 1], 3),
         ([1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0,
           0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0,
           0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1], 6)]

    assert gf_ddf_zassenhaus(f, 2, ZZ) == g
    assert gf_ddf_shoup(f, 2, ZZ) == g

    f = gf_from_dict({6: ZZ(1), 5: ZZ(-1), 4: ZZ(1), 3: ZZ(1), 1: ZZ(-1)}, 3, ZZ)
    g = [([1, 1, 0], 1),
         ([1, 1, 0, 1, 2], 2)]

    assert gf_ddf_zassenhaus(f, 3, ZZ) == g
    assert gf_ddf_shoup(f, 3, ZZ) == g

    f = ZZ.map([1, 2, 5, 26, 677, 436, 791, 325, 456, 24, 577])
    g = [([1, 701], 1),
         ([1, 110, 559, 532, 694, 151, 110, 70, 735, 122], 9)]

    assert gf_ddf_zassenhaus(f, 809, ZZ) == g
    assert gf_ddf_shoup(f, 809, ZZ) == g

    p = ZZ(nextprime(int((2**15 * pi).evalf())))
    f = gf_from_dict({15: 1, 1: 1, 0: 1}, p, ZZ)
    g = [([1, 22730, 68144], 2),
         ([1, 64876, 83977, 10787, 12561, 68608, 52650, 88001, 84356], 4),
         ([1, 15347, 95022, 84569, 94508, 92335], 5)]

    assert gf_ddf_zassenhaus(f, p, ZZ) == g
    assert gf_ddf_shoup(f, p, ZZ) == g
def test_dup_gcd():
    assert dup_zz_heu_gcd([], [], ZZ) == ([], [], [])
    assert dup_rr_prs_gcd([], [], ZZ) == ([], [], [])

    assert dup_zz_heu_gcd([2], [], ZZ) == ([2], [1], [])
    assert dup_rr_prs_gcd([2], [], ZZ) == ([2], [1], [])

    assert dup_zz_heu_gcd([-2], [], ZZ) == ([2], [-1], [])
    assert dup_rr_prs_gcd([-2], [], ZZ) == ([2], [-1], [])

    assert dup_zz_heu_gcd([], [-2], ZZ) == ([2], [], [-1])
    assert dup_rr_prs_gcd([], [-2], ZZ) == ([2], [], [-1])

    assert dup_zz_heu_gcd([], [2, 4], ZZ) == ([2, 4], [], [1])
    assert dup_rr_prs_gcd([], [2, 4], ZZ) == ([2, 4], [], [1])

    assert dup_zz_heu_gcd([2, 4], [], ZZ) == ([2, 4], [1], [])
    assert dup_rr_prs_gcd([2, 4], [], ZZ) == ([2, 4], [1], [])

    assert dup_zz_heu_gcd([2], [2], ZZ) == ([2], [1], [1])
    assert dup_rr_prs_gcd([2], [2], ZZ) == ([2], [1], [1])

    assert dup_zz_heu_gcd([-2], [2], ZZ) == ([2], [-1], [1])
    assert dup_rr_prs_gcd([-2], [2], ZZ) == ([2], [-1], [1])

    assert dup_zz_heu_gcd([2], [-2], ZZ) == ([2], [1], [-1])
    assert dup_rr_prs_gcd([2], [-2], ZZ) == ([2], [1], [-1])

    assert dup_zz_heu_gcd([-2], [-2], ZZ) == ([2], [-1], [-1])
    assert dup_rr_prs_gcd([-2], [-2], ZZ) == ([2], [-1], [-1])

    assert dup_zz_heu_gcd([1, 2, 1], [1], ZZ) == ([1], [1, 2, 1], [1])
    assert dup_rr_prs_gcd([1, 2, 1], [1], ZZ) == ([1], [1, 2, 1], [1])

    assert dup_zz_heu_gcd([1, 2, 1], [2], ZZ) == ([1], [1, 2, 1], [2])
    assert dup_rr_prs_gcd([1, 2, 1], [2], ZZ) == ([1], [1, 2, 1], [2])

    assert dup_zz_heu_gcd([2, 4, 2], [2], ZZ) == ([2], [1, 2, 1], [1])
    assert dup_rr_prs_gcd([2, 4, 2], [2], ZZ) == ([2], [1, 2, 1], [1])

    assert dup_zz_heu_gcd([2], [2, 4, 2], ZZ) == ([2], [1], [1, 2, 1])
    assert dup_rr_prs_gcd([2], [2, 4, 2], ZZ) == ([2], [1], [1, 2, 1])

    assert dup_zz_heu_gcd(ZZ.map(
        [2, 4, 2]), [1, 1], ZZ) == ([1, 1], [2, 2], [1])
    assert dup_rr_prs_gcd(ZZ.map(
        [2, 4, 2]), [1, 1], ZZ) == ([1, 1], [2, 2], [1])

    assert dup_zz_heu_gcd(ZZ.map(
        [1, 1]), [2, 4, 2], ZZ) == ([1, 1], [1], [2, 2])
    assert dup_rr_prs_gcd(ZZ.map(
        [1, 1]), [2, 4, 2], ZZ) == ([1, 1], [1], [2, 2])

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

    assert dup_zz_heu_gcd(f, g, ZZ) == ([1], f, g)
    assert dup_rr_prs_gcd(f, g, ZZ) == ([1], f, g)

    f = [1, 8, 21, 22, 8]
    g = [1, 6, 11, 6]

    h = [1, 3, 2]

    cff = [1, 5, 4]
    cfg = [1, 3]

    assert dup_zz_heu_gcd(f, g, ZZ) == (h, cff, cfg)
    assert dup_rr_prs_gcd(f, g, ZZ) == (h, cff, cfg)

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

    h = [1, 0, 2]

    cff = [1, 0, -2]
    cfg = [1, 0, 2]

    assert dup_zz_heu_gcd(f, g, ZZ) == (h, cff, cfg)
    assert dup_rr_prs_gcd(f, g, ZZ) == (h, cff, cfg)

    f = [1, 0, 1, 0, -3, -3, 8, 2, -5]
    g = [3, 0, 5, -0, -4, -9, 21]

    h = [1]

    cff = f
    cfg = g

    assert dup_zz_heu_gcd(f, g, ZZ) == (h, cff, cfg)
    assert dup_rr_prs_gcd(f, g, ZZ) == (h, cff, cfg)

    f = dup_normal([1, 0, 1, 0, -3, -3, 8, 2, -5], QQ)
    g = dup_normal([3, 0, 5, -0, -4, -9, 21], QQ)

    h = dup_normal([1], QQ)

    assert dup_qq_heu_gcd(f, g, QQ) == (h, cff, cfg)
    assert dup_ff_prs_gcd(f, g, QQ) == (h, cff, cfg)

    f = [-352518131239247345597970242177235495263669787845475025293906825864749649589178600387510272,
         0, 0, 0, 0, 0, 0,
         46818041807522713962450042363465092040687472354933295397472942006618953623327997952,
         0, 0, 0, 0, 0, 0,
         378182690892293941192071663536490788434899030680411695933646320291525827756032,
         0, 0, 0, 0, 0, 0,
         112806468807371824947796775491032386836656074179286744191026149539708928,
         0, 0, 0, 0, 0, 0,
         -12278371209708240950316872681744825481125965781519138077173235712,
         0, 0, 0, 0, 0, 0,
         289127344604779611146960547954288113529690984687482920704,
         0, 0, 0, 0, 0, 0,
         19007977035740498977629742919480623972236450681,
         0, 0, 0, 0, 0, 0,
         311973482284542371301330321821976049]

    g = [365431878023781158602430064717380211405897160759702125019136,
         0, 0, 0, 0, 0, 0,
         197599133478719444145775798221171663643171734081650688,
         0, 0, 0, 0, 0, 0,
         -9504116979659010018253915765478924103928886144,
         0, 0, 0, 0, 0, 0,
         -311973482284542371301330321821976049]

    f = dup_normal(f, ZZ)
    g = dup_normal(g, ZZ)

    assert dup_zz_heu_gcd(f, dup_diff(f, 1, ZZ), ZZ)[0] == g
    assert dup_rr_prs_gcd(f, dup_diff(f, 1, ZZ), ZZ)[0] == g

    f = [QQ(1, 2), QQ(1), QQ(1, 2)]
    g = [QQ(1, 2), QQ(1, 2)]

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

    assert dup_qq_heu_gcd(f, g, QQ) == (h, g, [QQ(1, 2)])
    assert dup_ff_prs_gcd(f, g, QQ) == (h, g, [QQ(1, 2)])

    f = ZZ.map([1317378933230047068160, 2945748836994210856960])
    g = ZZ.map([120352542776360960, 269116466014453760])

    h = ZZ.map([120352542776360960, 269116466014453760])
    cff = ZZ.map([10946])
    cfg = ZZ.map([1])

    assert dup_zz_heu_gcd(f, g, ZZ) == (h, cff, cfg)
Exemple #55
0
def test_gf_factor():
    assert gf_factor([], 11, ZZ) == (0, [])
    assert gf_factor([1], 11, ZZ) == (1, [])
    assert gf_factor([1, 1], 11, ZZ) == (1, [([1, 1], 1)])

    assert gf_factor_sqf([], 11, ZZ) == (0, [])
    assert gf_factor_sqf([1], 11, ZZ) == (1, [])
    assert gf_factor_sqf([1, 1], 11, ZZ) == (1, [[1, 1]])

    config.setup('GF_FACTOR_METHOD', 'berlekamp')

    assert gf_factor_sqf([], 11, ZZ) == (0, [])
    assert gf_factor_sqf([1], 11, ZZ) == (1, [])
    assert gf_factor_sqf([1, 1], 11, ZZ) == (1, [[1, 1]])

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')

    assert gf_factor_sqf([], 11, ZZ) == (0, [])
    assert gf_factor_sqf([1], 11, ZZ) == (1, [])
    assert gf_factor_sqf([1, 1], 11, ZZ) == (1, [[1, 1]])

    config.setup('GF_FACTOR_METHOD', 'shoup')

    assert gf_factor_sqf(ZZ.map([]), 11, ZZ) == (0, [])
    assert gf_factor_sqf(ZZ.map([1]), 11, ZZ) == (1, [])
    assert gf_factor_sqf(ZZ.map([1, 1]), 11, ZZ) == (1, [[1, 1]])

    f, p = ZZ.map([1, 0, 0, 1, 0]), 2

    g = (1, [([1, 0], 1),
             ([1, 1], 1),
             ([1, 1, 1], 1)])

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

    g = (1, [[1, 0],
             [1, 1],
             [1, 1, 1]])

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor_sqf(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor_sqf(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor_sqf(f, p, ZZ) == g

    f, p = gf_from_int_poly([1, -3, 1, -3, -1, -3, 1], 11), 11

    g = (1, [([1, 1], 1),
             ([1, 5, 3], 1),
             ([1, 2, 3, 4], 1)])

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

    f, p = [1, 5, 8, 4], 11

    g = (1, [([1, 1], 1), ([1, 2], 2)])

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

    f, p = [1, 1, 10, 1, 0, 10, 10, 10, 0, 0], 11

    g = (1, [([1, 0], 2), ([1, 9, 5], 1), ([1, 3, 0, 8, 5, 2], 1)])

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

    f, p = gf_from_dict({32: 1, 0: 1}, 11, ZZ), 11

    g = (1, [([1, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 10], 1),
             ([1, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 10], 1)])

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

    f, p = gf_from_dict({32: ZZ(8), 0: ZZ(5)}, 11, ZZ), 11

    g = (8, [([1, 3], 1),
             ([1, 8], 1),
             ([1, 0, 9], 1),
             ([1, 2, 2], 1),
             ([1, 9, 2], 1),
             ([1, 0, 5, 0, 7], 1),
             ([1, 0, 6, 0, 7], 1),
             ([1, 0, 0, 0, 1, 0, 0, 0, 6], 1),
             ([1, 0, 0, 0, 10, 0, 0, 0, 6], 1)])

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

    f, p = gf_from_dict({63: ZZ(8), 0: ZZ(5)}, 11, ZZ), 11

    g = (8, [([1, 7], 1),
             ([1, 4, 5], 1),
             ([1, 6, 8, 2], 1),
             ([1, 9, 9, 2], 1),
             ([1, 0, 0, 9, 0, 0, 4], 1),
             ([1, 2, 0, 8, 4, 6, 4], 1),
             ([1, 2, 3, 8, 0, 6, 4], 1),
             ([1, 2, 6, 0, 8, 4, 4], 1),
             ([1, 3, 3, 1, 6, 8, 4], 1),
             ([1, 5, 6, 0, 8, 6, 4], 1),
             ([1, 6, 2, 7, 9, 8, 4], 1),
             ([1, 10, 4, 7, 10, 7, 4], 1),
             ([1, 10, 10, 1, 4, 9, 4], 1)])

    config.setup('GF_FACTOR_METHOD', 'berlekamp')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

    # Gathen polynomials: x**n + x + 1 (mod p > 2**n * pi)

    p = ZZ(nextprime(int((2**15 * pi).evalf())))
    f = gf_from_dict({15: 1, 1: 1, 0: 1}, p, ZZ)

    assert gf_sqf_p(f, p, ZZ) is True

    g = (1, [([1, 22730, 68144], 1),
             ([1, 81553, 77449, 86810, 4724], 1),
             ([1, 86276, 56779, 14859, 31575], 1),
             ([1, 15347, 95022, 84569, 94508, 92335], 1)])

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

    g = (1, [[1, 22730, 68144],
             [1, 81553, 77449, 86810, 4724],
             [1, 86276, 56779, 14859, 31575],
             [1, 15347, 95022, 84569, 94508, 92335]])

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor_sqf(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor_sqf(f, p, ZZ) == g

    # Shoup polynomials: f = a_0 x**n + a_1 x**(n-1) + ... + a_n
    # (mod p > 2**(n-2) * pi), where a_n = a_{n-1}**2 + 1, a_0 = 1

    p = ZZ(nextprime(int((2**4 * pi).evalf())))
    f = ZZ.map([1, 2, 5, 26, 41, 39, 38])

    assert gf_sqf_p(f, p, ZZ) is True

    g = (1, [([1, 44, 26], 1),
             ([1, 11, 25, 18, 30], 1)])

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor(f, p, ZZ) == g

    g = (1, [[1, 44, 26],
             [1, 11, 25, 18, 30]])

    config.setup('GF_FACTOR_METHOD', 'zassenhaus')
    assert gf_factor_sqf(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'shoup')
    assert gf_factor_sqf(f, p, ZZ) == g

    config.setup('GF_FACTOR_METHOD', 'other')
    raises(KeyError, lambda: gf_factor([1, 1], 11, ZZ))
    config.setup('GF_FACTOR_METHOD')
def test_dmp_subresultants():
    assert dmp_resultant([[]], [[]], 1, ZZ) == []
    assert dmp_prs_resultant([[]], [[]], 1, ZZ)[0] == []
    assert dmp_zz_collins_resultant([[]], [[]], 1, ZZ) == []
    assert dmp_qq_collins_resultant([[]], [[]], 1, ZZ) == []

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

    assert dmp_resultant([[]], [[ZZ(1)]], 1, ZZ) == []
    assert dmp_prs_resultant([[]], [[ZZ(1)]], 1, ZZ)[0] == []
    assert dmp_zz_collins_resultant([[]], [[ZZ(1)]], 1, ZZ) == []
    assert dmp_qq_collins_resultant([[]], [[ZZ(1)]], 1, ZZ) == []

    f = dmp_normal(ZZ.map([[3, 0], [], [-1, 0, 0, -4]]), 1, ZZ)
    g = dmp_normal(ZZ.map([[1], [1, 0, 0, 0], [-9]]), 1, ZZ)

    a = dmp_normal(ZZ.map([[3, 0, 0, 0, 0], [1, 0, -27, 4]]), 1, ZZ)
    b = dmp_normal(ZZ.map(
        [[-3, 0, 0, -12, 1, 0, -54, 8, 729, -216, 16]]), 1, ZZ)

    r = dmp_LC(b, ZZ)

    assert dmp_subresultants(f, g, 1, ZZ) == [f, g, a, b]

    assert dmp_resultant(f, g, 1, ZZ) == r
    assert dmp_prs_resultant(f, g, 1, ZZ)[0] == r
    assert dmp_zz_collins_resultant(f, g, 1, ZZ) == r
    assert dmp_qq_collins_resultant(f, g, 1, ZZ) == r

    f = dmp_normal(ZZ.map([[-1], [], [], [5]]), 1, ZZ)
    g = dmp_normal(ZZ.map([[3, 1], [], []]), 1, ZZ)

    a = dmp_normal(ZZ.map([[45, 30, 5]]), 1, ZZ)
    b = dmp_normal(ZZ.map([[675, 675, 225, 25]]), 1, ZZ)

    r = dmp_LC(b, ZZ)

    assert dmp_subresultants(f, g, 1, ZZ) == [f, g, a]
    assert dmp_resultant(f, g, 1, ZZ) == r
    assert dmp_prs_resultant(f, g, 1, ZZ)[0] == r
    assert dmp_zz_collins_resultant(f, g, 1, ZZ) == r
    assert dmp_qq_collins_resultant(f, g, 1, ZZ) == r

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

    r = [[[[1]], [[-3], [-3, 0]], [[9, 0], []]], [[[-2], [-2, 0]], [[6],
         [12, 0], [6, 0, 0]], [[-18, 0], [-18, 0, 0], []]], [[[4, 0],
         []], [[-12, 0], [-12, 0, 0], []], [[36, 0, 0], [], []]]]

    assert dmp_zz_collins_resultant(f, g, 4, ZZ) == r

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

    r = [[[[QQ(
        1, 36)]], [[QQ(
            -1, 12)], [QQ(-1, 12), QQ(0, 1)]], [[QQ(1, 4), QQ(0, 1)], []]],
         [[[QQ(
             -1, 18)], [QQ(
                 -1, 18), QQ(
                     0, 1)]], [[QQ(1, 6)], [QQ(1, 3), QQ(0, 1)], [QQ(1, 6),
            QQ(
                0, 1), QQ(
                    0, 1)]], [[QQ(
                        -1, 2), QQ(
                            0, 1)], [QQ(-1, 2), QQ(0, 1), QQ(0, 1)], []]],
         [[[QQ(
             1, 9), QQ(
                 0, 1)], []], [[QQ(
                     -1, 3), QQ(0, 1)], [QQ(-1, 3), QQ(0, 1), QQ(0, 1)], []],
          [[QQ(1, 1), QQ(0, 1), QQ(0, 1)], [], []]]]

    assert dmp_qq_collins_resultant(f, g, 4, QQ) == r
Exemple #57
0
def test_dmp_add_ground():
    f = ZZ.map([[1], [2], [3], [4]])
    g = ZZ.map([[1], [2], [3], [8]])

    assert dmp_add_ground(f, ZZ(4), 1, ZZ) == g
Exemple #58
0
def dmp_sub_ground():
    f = ZZ.map([[1], [2], [3], [4]])
    g = ZZ.map([[1], [2], [3], []])

    assert dmp_sub_ground(f, ZZ(4), 1, ZZ) == g
Exemple #59
0
def dup_sub_ground():
    f = ZZ.map([1, 2, 3, 4])
    g = ZZ.map([1, 2, 3, 0])

    assert dup_sub_ground(f, ZZ(4), ZZ) == g
Exemple #60
0
def dup_add_ground():
    f = ZZ.map([1, 2, 3, 4])
    g = ZZ.map([1, 2, 3, 8])

    assert dup_add_ground(f, ZZ(4), ZZ) == g