Beispiel #1
0
def test_zzX_diff():
    assert zzX_diff([]) == []

    assert zzX_diff([[]]) == [[]]

    assert zzX_diff([[[]]]) == [[[]]]
    assert zzX_diff([[[1], [2]]]) == [[[]]]

    assert zzX_diff([[[1]], [[]]]) == [[[1]]]
    assert zzX_diff([[[3]], [[1]], [[]]]) == [[[6]], [[1]]]

    assert zzX_diff([1, -1, 0, 0, 2]) == [4, -3, 0, 0]

    f = zzX_from_poly(W_1)

    assert zzX_diff(f, 2) == zzX_diff(zzX_diff(f))
    assert zzX_diff(f, 3) == zzX_diff(zzX_diff(zzX_diff(f)))
    assert zzX_diff(f, 4) == zzX_diff(zzX_diff(zzX_diff(zzX_diff(f))))

    assert zzX_diff_for(f, 2, 2) == zzX_swap(zzX_diff(zzX_swap(f, 1, 2), 2), 1,
                                             2)
    assert zzX_diff_for(f, 2, 3) == zzX_swap(zzX_diff(zzX_swap(f, 1, 2), 3), 1,
                                             2)
    assert zzX_diff_for(f, 3, 2) == zzX_swap(zzX_diff(zzX_swap(f, 1, 3), 2), 1,
                                             3)
    assert zzX_diff_for(f, 3, 3) == zzX_swap(zzX_diff(zzX_swap(f, 1, 3), 3), 1,
                                             3)
def poly_factors(f, *symbols, **flags):
    """Factor polynomials over rationals.

       >>> from sympy.polys.factortools import poly_factors
       >>> from sympy.abc import x, y

       >>> poly_factors(x**2 - y**2, x, y)
       (1, [(Poly(x - y, x, y), 1), (Poly(x + y, x, y), 1)])

    """
    if not isinstance(f, Poly):
        f = Poly(f, *symbols)
    elif symbols:
        raise SymbolsError("Redundant symbols were given")

    symbols = list(f.symbols)

    try:
        denom, F = f.as_integer()
    except CoefficientError:
        other = set([])

        for coeff in f.iter_coeffs():
            other |= coeff.atoms(Symbol)

        symbols += sorted(other)

        F = Poly(f, *symbols)
        denom, F = F.as_integer()

    cont, factors = zzX_factor(zzX_from_poly(F))

    for i, (h, k) in enumerate(factors):
        h = zzX_to_poly(h, *symbols)

        if f.symbols != symbols:
            h = h.as_poly(*f.symbols)

        factors[i] = (h, k)

    return Rational(cont, denom), factors
Beispiel #3
0
def poly_factors(f, *symbols, **flags):
    """Factor polynomials over rationals.

       >>> from sympy import *
       >>> x, y = symbols("x y")

       >>> poly_factors(x**2 - y**2, x, y)
       (1, [(Poly(x - y, x, y), 1), (Poly(x + y, x, y), 1)])

    """
    if not isinstance(f, Poly):
        f = Poly(f, *symbols)
    elif symbols:
        raise SymbolsError("Redundant symbols were given")

    symbols = list(f.symbols)

    try:
        denom, F = f.as_integer()
    except CoefficientError:
        other = set([])

        for coeff in f.iter_coeffs():
            other |= coeff.atoms(Symbol)

        symbols += sorted(other)

        F = Poly(f, *symbols)
        denom, F = F.as_integer()

    cont, factors = zzX_factor(zzX_from_poly(F))

    for i, (h, k) in enumerate(factors):
        h = zzX_to_poly(h, *symbols)

        if f.symbols != symbols:
            h = h.as_poly(*f.symbols)

        factors[i] = (h, k)

    return Rational(cont, denom), factors
Beispiel #4
0
def test_zzX_eval():
    assert zzX_eval([], 3) == 0

    assert zzX_eval([[]], 3) == []
    assert zzX_eval([[[]]], 3) == [[]]

    assert zzX_eval([[1, 2]], 0) == [1, 2]

    assert zzX_eval([[[1]]], 3) == [[1]]
    assert zzX_eval([[[1, 2]]], 3) == [[1, 2]]

    assert zzX_eval([[3, 2], [1, 2]], 3) == [10, 8]
    assert zzX_eval([[[3, 2]], [[1, 2]]], 3) == [[10, 8]]

    f = zzX_from_poly(W_1)

    assert zzX_eval_for(f, 2, -2) == zzX_from_poly(W_1.subs(y, -2))
    assert zzX_eval_for(f, 2, 7) == zzX_from_poly(W_1.subs(y, 7))
    assert zzX_eval_for(f, 3, -2) == zzX_from_poly(W_1.subs(z, -2))
    assert zzX_eval_for(f, 3, 7) == zzX_from_poly(W_1.subs(z, 7))

    assert zzX_diff_eval(f, 3, 2,
                         7) == zzX_from_poly(W_1.diff(z, 2).subs(z, 7))

    assert zzX_eval_list(f_0, []) == f_0

    assert zzX_eval_list([[]], [1]) == []
    assert zzX_eval_list([[[]]], [1]) == [[]]
    assert zzX_eval_list([[[]]], [1, 2]) == []

    assert zzX_eval_list(f_0, [1, -17, 8]) == 84496
    assert zzX_eval_list(f_0, [-17, 8]) == [-1409, 3, 85902]
    assert zzX_eval_list(f_0, [8]) == [[83, 2], [3], [302, 81, 1]]

    assert zzX_eval_list(f_1, [-17, 8]) == [-136, 15699, 9166, -27144]

    assert zzX_eval_list(f_2, [-12, 3]) == [-1377, 0, -702, -1224, 0, -624]
    assert zzX_eval_list(f_3,
                         [-12, 3]) == [144, 82, -5181, -28872, -14868, -540]

    assert zzX_eval_list(f_4, [25, -1]) == [
        152587890625, 9765625, -59605407714843750, -3839159765625, -1562475,
        9536712644531250, 610349546750, -4, 24414375000, 1562520
    ]
    assert zzX_eval_list(f_5, [25, -1]) == [-1, -78, -2028, -17576]

    assert zzX_eval_list(f_6, [0, 2, 4]) == [5040, 0, 0, 4480]
def test_zzX_from_to():
    assert zzX_from_dict({}, 2) == [[]]

    f = [3,0,0,2,0,0,0,0,8]
    g = (3*x**8 + 2*x**5 + 8).as_poly(x)

    assert zzX_from_poly(g) == f
    assert zzX_to_poly(f, x) == g

    assert zzX_from_poly(F_0) == f_0
    assert zzX_from_poly(F_1) == f_1
    assert zzX_from_poly(F_2) == f_2
    assert zzX_from_poly(F_3) == f_3
    assert zzX_from_poly(F_4) == f_4
    assert zzX_from_poly(F_5) == f_5
    assert zzX_from_poly(F_6) == f_6

    assert zzX_to_poly(f_0, x,y,z) == F_0
    assert zzX_to_poly(f_1, x,y,z) == F_1
    assert zzX_to_poly(f_2, x,y,z) == F_2
    assert zzX_to_poly(f_3, x,y,z) == F_3
    assert zzX_to_poly(f_4, x,y,z) == F_4
    assert zzX_to_poly(f_5, x,y,z) == F_5
    assert zzX_to_poly(f_6, x,y,z,t) == F_6
Beispiel #6
0
def test_zzX_from_to():
    assert zzX_from_dict({}, 2) == [[]]

    f = [3, 0, 0, 2, 0, 0, 0, 0, 8]
    g = (3 * x**8 + 2 * x**5 + 8).as_poly(x)

    assert zzX_from_poly(g) == f
    assert zzX_to_poly(f, x) == g

    assert zzX_from_poly(F_0) == f_0
    assert zzX_from_poly(F_1) == f_1
    assert zzX_from_poly(F_2) == f_2
    assert zzX_from_poly(F_3) == f_3
    assert zzX_from_poly(F_4) == f_4
    assert zzX_from_poly(F_5) == f_5
    assert zzX_from_poly(F_6) == f_6

    assert zzX_to_poly(f_0, x, y, z) == F_0
    assert zzX_to_poly(f_1, x, y, z) == F_1
    assert zzX_to_poly(f_2, x, y, z) == F_2
    assert zzX_to_poly(f_3, x, y, z) == F_3
    assert zzX_to_poly(f_4, x, y, z) == F_4
    assert zzX_to_poly(f_5, x, y, z) == F_5
    assert zzX_to_poly(f_6, x, y, z, t) == F_6
def test_zzX_diff():
    assert zzX_diff([]) == []

    assert zzX_diff([[]]) == [[]]

    assert zzX_diff([[[]]]) == [[[]]]
    assert zzX_diff([[[1], [2]]]) == [[[]]]

    assert zzX_diff([[[1]], [[]]]) == [[[1]]]
    assert zzX_diff([[[3]], [[1]], [[]]]) == [[[6]], [[1]]]

    assert zzX_diff([1,-1,0,0,2]) == [4,-3,0,0]

    f = zzX_from_poly(W_1)

    assert zzX_diff(f, 2) ==                   zzX_diff(zzX_diff(f))
    assert zzX_diff(f, 3) ==          zzX_diff(zzX_diff(zzX_diff(f)))
    assert zzX_diff(f, 4) == zzX_diff(zzX_diff(zzX_diff(zzX_diff(f))))

    assert zzX_diff_for(f, 2, 2) == zzX_swap(zzX_diff(zzX_swap(f, 1, 2), 2), 1, 2)
    assert zzX_diff_for(f, 2, 3) == zzX_swap(zzX_diff(zzX_swap(f, 1, 2), 3), 1, 2)
    assert zzX_diff_for(f, 3, 2) == zzX_swap(zzX_diff(zzX_swap(f, 1, 3), 2), 1, 3)
    assert zzX_diff_for(f, 3, 3) == zzX_swap(zzX_diff(zzX_swap(f, 1, 3), 3), 1, 3)
def test_zzX_eval():
    assert zzX_eval([], 3) == 0

    assert zzX_eval([[]], 3) == []
    assert zzX_eval([[[]]], 3) == [[]]

    assert zzX_eval([[1,2]], 0) == [1,2]

    assert zzX_eval([[[1]]], 3) == [[1]]
    assert zzX_eval([[[1, 2]]], 3) == [[1, 2]]

    assert zzX_eval([[3, 2], [1, 2]], 3) == [10, 8]
    assert zzX_eval([[[3, 2]], [[1, 2]]], 3) == [[10, 8]]

    f = zzX_from_poly(W_1)

    assert zzX_eval_for(f, 2,-2) == zzX_from_poly(W_1.subs(y,-2))
    assert zzX_eval_for(f, 2, 7) == zzX_from_poly(W_1.subs(y, 7))
    assert zzX_eval_for(f, 3,-2) == zzX_from_poly(W_1.subs(z,-2))
    assert zzX_eval_for(f, 3, 7) == zzX_from_poly(W_1.subs(z, 7))

    assert zzX_diff_eval(f, 3, 2, 7) == zzX_from_poly(W_1.diff(z, 2).subs(z, 7))

    assert zzX_eval_list(f_0, []) == f_0

    assert zzX_eval_list([[]], [1]) == []
    assert zzX_eval_list([[[]]], [1]) == [[]]
    assert zzX_eval_list([[[]]], [1, 2]) == []

    assert zzX_eval_list(f_0, [1,-17,8]) == 84496
    assert zzX_eval_list(f_0, [-17, 8]) == [-1409, 3, 85902]
    assert zzX_eval_list(f_0, [8]) == [[83, 2], [3], [302, 81, 1]]

    assert zzX_eval_list(f_1, [-17, 8]) == [-136, 15699, 9166, -27144]

    assert zzX_eval_list(f_2, [-12, 3]) == [-1377, 0, -702, -1224, 0, -624]
    assert zzX_eval_list(f_3, [-12, 3]) == [144, 82, -5181, -28872, -14868, -540]

    assert zzX_eval_list(f_4, [25, -1]) == [152587890625, 9765625, -59605407714843750,
        -3839159765625, -1562475, 9536712644531250, 610349546750, -4, 24414375000, 1562520]
    assert zzX_eval_list(f_5, [25, -1]) == [-1, -78, -2028, -17576]

    assert zzX_eval_list(f_6, [0, 2, 4]) == [5040, 0, 0, 4480]
def test_zzX_wang():
    f = zzX_from_poly(W_1)
    p = nextprime(zzX_mignotte_bound(f))

    assert p == 6291469

    V_1, k_1, E_1 = [[1],[]], 1, -14
    V_2, k_2, E_2 = [[1, 0]], 2, 3
    V_3, k_3, E_3 = [[1],[ 1, 0]], 2, -11
    V_4, k_4, E_4 = [[1],[-1, 0]], 1, -17

    V = [V_1, V_2, V_3, V_4]
    K = [k_1, k_2, k_3, k_4]
    E = [E_1, E_2, E_3, E_4]

    V = zip(V, K)

    A = [-14, 3]

    U = zzX_eval_list(f, A)
    cu, u = zzx_primitive(U)

    assert cu == 1 and u == U == \
        [1036728, 915552, 55748, 105621, -17304, -26841, -644]

    assert zzX_wang_non_divisors(E, cu, 4) == [7, 3, 11, 17]
    assert zzx_sqf_p(u) and zzx_degree(u) == zzX_degree(f)

    _, H = zzx_factor_sqf(u)

    h_1 = [44,  42,   1]
    h_2 = [126, -9,  28]
    h_3 = [187,  0, -23]

    assert H == [h_1, h_2, h_3]

    LC_1 = [[-4], [-4,0]]
    LC_2 = [[-1,0,0], []]
    LC_3 = [[1], [], [-1,0,0]]

    LC = [LC_1, LC_2, LC_3]

    assert zzX_wang_lead_coeffs(f, V, cu, E, H, A) == (f, H, LC)

    H_1 = [[44L, 42L, 1L], [126L, -9L, 28L], [187L, 0L, -23L]]
    C_1 = [-70686, -5863, -17826, 2009, 5031, 74]

    H_2 = [[[-4, -12], [-3, 0], [1]], [[-9, 0], [-9], [-2, 0]], [[1, 0, -9], [], [1, -9]]]
    C_2 = [[9, 12, -45, -108, -324], [18, -216, -810, 0], [2, 9, -252, -288, -945], [-30, -414, 0], [2, -54, -3, 81], [12, 0]]

    H_3 = [[[-4, -12], [-3, 0], [1]], [[-9, 0], [-9], [-2, 0]], [[1, 0, -9], [], [1, -9]]]
    C_3 = [[-36, -108, 0], [-27, -36, -108], [-8, -42, 0], [-6, 0, 9], [2, 0]]

    T_1 = [[-3, 0], [-2], [1]]
    T_2 = [[[-1, 0], []], [[-3], []], [[-6]]]
    T_3 = [[[]], [[]], [[-1]]]

    assert zzX_diophantine(H_1, C_1,    [], 5, p) == T_1
    assert zzX_diophantine(H_2, C_2, [-14], 5, p) == T_2
    assert zzX_diophantine(H_3, C_3, [-14], 5, p) == T_3

    factors = zzX_wang_hensel_lifting(f, H, LC, A, p)

    f_1 = zzX_to_poly(factors[0], x, y, z)
    f_2 = zzX_to_poly(factors[1], x, y, z)
    f_3 = zzX_to_poly(factors[2], x, y, z)

    assert f_1 == -(4*(y + z)*x**2 + x*y*z - 1).as_poly(x, y, z)
    assert f_2 == -(y*z**2*x**2 + 3*x*z + 2*y).as_poly(x, y, z)
    assert f_3 ==  ((y**2 - z**2)*x**2 + y - z**2).as_poly(x, y, z)

    assert f_1*f_2*f_3 == W_1
Beispiel #10
0
def test_zzX_wang():
    f = zzX_from_poly(W_1)
    p = nextprime(zzX_mignotte_bound(f))

    assert p == 6291469

    V_1, k_1, E_1 = [[1], []], 1, -14
    V_2, k_2, E_2 = [[1, 0]], 2, 3
    V_3, k_3, E_3 = [[1], [1, 0]], 2, -11
    V_4, k_4, E_4 = [[1], [-1, 0]], 1, -17

    V = [V_1, V_2, V_3, V_4]
    K = [k_1, k_2, k_3, k_4]
    E = [E_1, E_2, E_3, E_4]

    V = zip(V, K)

    A = [-14, 3]

    U = zzX_eval_list(f, A)
    cu, u = zzx_primitive(U)

    assert cu == 1 and u == U == \
        [1036728, 915552, 55748, 105621, -17304, -26841, -644]

    assert zzX_wang_non_divisors(E, cu, 4) == [7, 3, 11, 17]
    assert zzx_sqf_p(u) and zzx_degree(u) == zzX_degree(f)

    _, H = zzx_factor_sqf(u)

    h_1 = [44, 42, 1]
    h_2 = [126, -9, 28]
    h_3 = [187, 0, -23]

    assert H == [h_1, h_2, h_3]

    LC_1 = [[-4], [-4, 0]]
    LC_2 = [[-1, 0, 0], []]
    LC_3 = [[1], [], [-1, 0, 0]]

    LC = [LC_1, LC_2, LC_3]

    assert zzX_wang_lead_coeffs(f, V, cu, E, H, A) == (f, H, LC)

    H_1 = [[44L, 42L, 1L], [126L, -9L, 28L], [187L, 0L, -23L]]
    C_1 = [-70686, -5863, -17826, 2009, 5031, 74]

    H_2 = [[[-4, -12], [-3, 0], [1]], [[-9, 0], [-9], [-2, 0]],
           [[1, 0, -9], [], [1, -9]]]
    C_2 = [[9, 12, -45, -108, -324], [18, -216, -810, 0],
           [2, 9, -252, -288, -945], [-30, -414, 0], [2, -54, -3, 81], [12, 0]]

    H_3 = [[[-4, -12], [-3, 0], [1]], [[-9, 0], [-9], [-2, 0]],
           [[1, 0, -9], [], [1, -9]]]
    C_3 = [[-36, -108, 0], [-27, -36, -108], [-8, -42, 0], [-6, 0, 9], [2, 0]]

    T_1 = [[-3, 0], [-2], [1]]
    T_2 = [[[-1, 0], []], [[-3], []], [[-6]]]
    T_3 = [[[]], [[]], [[-1]]]

    assert zzX_diophantine(H_1, C_1, [], 5, p) == T_1
    assert zzX_diophantine(H_2, C_2, [-14], 5, p) == T_2
    assert zzX_diophantine(H_3, C_3, [-14], 5, p) == T_3

    factors = zzX_wang_hensel_lifting(f, H, LC, A, p)

    f_1 = zzX_to_poly(factors[0], x, y, z)
    f_2 = zzX_to_poly(factors[1], x, y, z)
    f_3 = zzX_to_poly(factors[2], x, y, z)

    assert f_1 == -(4 * (y + z) * x**2 + x * y * z - 1).as_poly(x, y, z)
    assert f_2 == -(y * z**2 * x**2 + 3 * x * z + 2 * y).as_poly(x, y, z)
    assert f_3 == ((y**2 - z**2) * x**2 + y - z**2).as_poly(x, y, z)

    assert f_1 * f_2 * f_3 == W_1