def zzX_fateman_poly_F_2(n): """Fateman's GCD benchmark: linearly dense quartic inputs """ u = [1, 0] for i in xrange(1, n): u = [zzX_const(i, 1), u] v = zzX_add_term(u, zzX_const(n - 1, 2)) f = zzX_sqr([zzX_const(n, 1), zzX_neg(v)]) g = zzX_sqr([zzX_const(n, 1), v]) v = zzX_add_term(u, zzX_const(n - 1, 1)) h = zzX_sqr([zzX_const(n, 1), v]) return zzX_mul(f, h), zzX_mul(g, h), h
def zzX_fateman_poly_F_3(n): """Fateman's GCD benchmark: sparse inputs (deg f ~ vars f) """ u = zzx_from_dict({n + 1: 1}) for i in xrange(1, n): u = zzX_add_term([u], zzX_const(i, 1), n + 1) v = zzX_add_term(u, zzX_const(n - 1, 2)) f = zzX_sqr(zzX_add_term([zzX_neg(v)], zzX_const(n, 1), n + 1)) g = zzX_sqr(zzX_add_term([v], zzX_const(n, 1), n + 1)) v = zzX_add_term(u, zzX_const(n - 1, 1)) h = zzX_sqr(zzX_add_term([v], zzX_const(n, 1), n + 1)) return zzX_mul(f, h), zzX_mul(g, h), h
def zzX_fateman_poly_F_2(n): """Fateman's GCD benchmark: linearly dense quartic inputs """ u = [1, 0] for i in xrange(1, n): u = [zzX_const(i, 1), u] v = zzX_add_term(u, zzX_const(n-1, 2)) f = zzX_sqr([zzX_const(n, 1), zzX_neg(v)]) g = zzX_sqr([zzX_const(n, 1), v]) v = zzX_add_term(u, zzX_const(n-1, 1)) h = zzX_sqr([zzX_const(n, 1), v]) return zzX_mul(f,h), zzX_mul(g,h), h
def zzX_fateman_poly_F_3(n): """Fateman's GCD benchmark: sparse inputs (deg f ~ vars f) """ u = zzx_from_dict({n+1:1}) for i in xrange(1, n): u = zzX_add_term([u], zzX_const(i, 1), n+1) v = zzX_add_term(u, zzX_const(n-1, 2)) f = zzX_sqr(zzX_add_term([zzX_neg(v)], zzX_const(n, 1), n+1)) g = zzX_sqr(zzX_add_term([v], zzX_const(n, 1), n+1)) v = zzX_add_term(u, zzX_const(n-1, 1)) h = zzX_sqr(zzX_add_term([v], zzX_const(n, 1), n+1)) return zzX_mul(f,h), zzX_mul(g,h), h
def test_zzX_content(): f, g, F = [3, 2, 1], [1], [] for i in xrange(0, 5): g = zzX_mul(g, f) F.insert(0, g) assert zzX_content(F) == f assert zzX_one_p(zzX_content(f_4)) assert zzX_one_p(zzX_content(f_5)) assert zzX_one_p(zzX_content(f_6))
def test_zzX_content(): f, g, F = [3,2,1], [1], [] for i in xrange(0, 5): g = zzX_mul(g, f) F.insert(0, g) assert zzX_content(F) == f assert zzX_one_p(zzX_content(f_4)) assert zzX_one_p(zzX_content(f_5)) assert zzX_one_p(zzX_content(f_6))
def zzX_fateman_poly_F_1(n): """Fateman's GCD benchmark: trivial GCD """ u = [1, 0] for i in xrange(1, n+1): u = [zzX_const(i, 1), u] v = [1, 0, 0] for i in xrange(1, n+1): v = [zzX_const(i, 1), zzX_zero(i), v] U = zzX_add_term(u, zzX_const(n, 1)) V = zzX_add_term(u, zzX_const(n, 2)) W = zzX_add_term(v, zzX_const(n, 1)) Y = zzX_lift(n-1, [[-3, 0], [], [1, 0, -1]]) F = zzX_mul(U, V) G = zzX_mul(W, Y) H = zzX_const(n+1, 1) return F, G, H
def zzX_fateman_poly_F_1(n): """Fateman's GCD benchmark: trivial GCD """ u = [1, 0] for i in xrange(1, n + 1): u = [zzX_const(i, 1), u] v = [1, 0, 0] for i in xrange(1, n + 1): v = [zzX_const(i, 1), zzX_zero(i), v] U = zzX_add_term(u, zzX_const(n, 1)) V = zzX_add_term(u, zzX_const(n, 2)) W = zzX_add_term(v, zzX_const(n, 1)) Y = zzX_lift(n - 1, [[-3, 0], [], [1, 0, -1]]) F = zzX_mul(U, V) G = zzX_mul(W, Y) H = zzX_const(n + 1, 1) return F, G, H
def test_zzX_primitive(): f, g, F = [3, 2, 1], [1], [] for i in xrange(0, 5): g = zzX_mul(g, f) F.insert(0, g) assert zzX_primitive(F) == (f, [zzX_quo(cf, f) for cf in F]) cont, f = zzX_primitive(f_4) assert zzX_one_p(cont) and f == f_4 cont, f = zzX_primitive(f_5) assert zzX_one_p(cont) and f == f_5 cont, f = zzX_primitive(f_6) assert zzX_one_p(cont) and f == f_6
def test_zzX_primitive(): f, g, F = [3,2,1], [1], [] for i in xrange(0, 5): g = zzX_mul(g, f) F.insert(0, g) assert zzX_primitive(F) == (f, [ zzX_quo(cf, f) for cf in F ]) cont, f = zzX_primitive(f_4) assert zzX_one_p(cont) and f == f_4 cont, f = zzX_primitive(f_5) assert zzX_one_p(cont) and f == f_5 cont, f = zzX_primitive(f_6) assert zzX_one_p(cont) and f == f_6
def test_zzX_expand(): zzX_expand(f_0, f_1, f_2) == zzX_mul(f_0, zzX_mul(f_1, f_2))
def test_zzX_mul(): assert zzX_mul([[[]]], [[[]]]) == [[[]]] assert zzX_mul([[[1]]], [[[]]]) == [[[]]] assert zzX_mul([[[]]], [[[1]]]) == [[[]]] assert zzX_mul([[[2]]], [[[1]]]) == [[[2]]] assert zzX_mul([[[1]]], [[[2]]]) == [[[2]]]
def test_zzx_gcd(): assert zzx_heu_gcd([], []) == ([], [], []) assert zzx_mod_gcd([], []) == ([], [], []) assert zzx_heu_gcd([2], []) == ([2], [1], []) assert zzx_mod_gcd([2], []) == ([2], [1], []) assert zzx_heu_gcd([], [2, 4]) == ([2, 4], [], [1]) assert zzx_mod_gcd([], [2, 4]) == ([2, 4], [], [1]) assert zzx_heu_gcd([2, 4], []) == ([2, 4], [1], []) assert zzx_mod_gcd([2, 4], []) == ([2, 4], [1], []) assert zzx_heu_gcd([2], [2]) == ([2], [1], [1]) assert zzx_mod_gcd([2], [2]) == ([2], [1], [1]) assert zzx_heu_gcd([1, 2, 1], [1]) == ([1], [1, 2, 1], [1]) assert zzx_mod_gcd([1, 2, 1], [1]) == ([1], [1, 2, 1], [1]) assert zzx_heu_gcd([1, 2, 1], [2]) == ([1], [1, 2, 1], [2]) assert zzx_mod_gcd([1, 2, 1], [2]) == ([1], [1, 2, 1], [2]) assert zzx_heu_gcd([2, 4, 2], [2]) == ([2], [1, 2, 1], [1]) assert zzx_mod_gcd([2, 4, 2], [2]) == ([2], [1, 2, 1], [1]) assert zzx_heu_gcd([2], [2, 4, 2]) == ([2], [1], [1, 2, 1]) assert zzx_mod_gcd([2], [2, 4, 2]) == ([2], [1], [1, 2, 1]) assert zzx_heu_gcd([2, 4, 2], [1, 1]) == ([1, 1], [2, 2], [1]) assert zzx_mod_gcd([2, 4, 2], [1, 1]) == ([1, 1], [2, 2], [1]) assert zzx_heu_gcd([1, 1], [2, 4, 2]) == ([1, 1], [1], [2, 2]) assert zzx_mod_gcd([1, 1], [2, 4, 2]) == ([1, 1], [1], [2, 2]) f, g = [1, -31], [1, 0] assert zzx_heu_gcd(f, g) == ([1], f, g) assert zzx_mod_gcd(f, g) == ([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 zzx_heu_gcd(f, g) == (h, cff, cfg) assert zzx_mod_gcd(f, g) == (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 zzx_heu_gcd(f, g) == (h, cff, cfg) assert zzx_mod_gcd(f, g) == (h, cff, cfg) assert zzx_gcd(f, g) == h assert zzx_cofactors(f, g) == (h, cff, cfg) f, g, h = zzX_fateman_poly_F_3(4) H, cff, cfg = zzX_cofactors(f, g) assert H == h and zzX_mul(H, cff) == f \ and zzX_mul(H, cfg) == g
def test_zzX_heu_gcd(): f,g,h = zzX_fateman_poly_F_1(2) H, cff, cfg = zzX_heu_gcd(f, g) assert H == h and zzX_mul(H, cff) == f \ and zzX_mul(H, cfg) == g f,g,h = zzX_fateman_poly_F_1(4) H, cff, cfg = zzX_heu_gcd(f, g) assert H == h and zzX_mul(H, cff) == f \ and zzX_mul(H, cfg) == g f,g,h = zzX_fateman_poly_F_1(6) H, cff, cfg = zzX_heu_gcd(f, g) assert H == h and zzX_mul(H, cff) == f \ and zzX_mul(H, cfg) == g f,g,h = zzX_fateman_poly_F_2(2) H, cff, cfg = zzX_heu_gcd(f, g) assert H == h and zzX_mul(H, cff) == f \ and zzX_mul(H, cfg) == g f,g,h = zzX_fateman_poly_F_3(2) H, cff, cfg = zzX_heu_gcd(f, g) assert H == h and zzX_mul(H, cff) == f \ and zzX_mul(H, cfg) == g f,g,h = zzX_fateman_poly_F_1(8) H, cff, cfg = zzX_heu_gcd(f, g) assert H == h and zzX_mul(H, cff) == f \ and zzX_mul(H, cfg) == g
def test_zzx_gcd(): assert zzx_heu_gcd([], []) == ([], [], []) assert zzx_mod_gcd([], []) == ([], [], []) assert zzx_heu_gcd([2], []) == ([2], [1], []) assert zzx_mod_gcd([2], []) == ([2], [1], []) assert zzx_heu_gcd([], [2,4]) == ([2,4], [], [1]) assert zzx_mod_gcd([], [2,4]) == ([2,4], [], [1]) assert zzx_heu_gcd([2,4], []) == ([2,4], [1], []) assert zzx_mod_gcd([2,4], []) == ([2,4], [1], []) assert zzx_heu_gcd([2], [2]) == ([2], [1], [1]) assert zzx_mod_gcd([2], [2]) == ([2], [1], [1]) assert zzx_heu_gcd([1,2,1], [1]) == ([1], [1, 2, 1], [1]) assert zzx_mod_gcd([1,2,1], [1]) == ([1], [1, 2, 1], [1]) assert zzx_heu_gcd([1,2,1], [2]) == ([1], [1, 2, 1], [2]) assert zzx_mod_gcd([1,2,1], [2]) == ([1], [1, 2, 1], [2]) assert zzx_heu_gcd([2,4,2], [2]) == ([2], [1, 2, 1], [1]) assert zzx_mod_gcd([2,4,2], [2]) == ([2], [1, 2, 1], [1]) assert zzx_heu_gcd([2], [2,4,2]) == ([2], [1], [1, 2, 1]) assert zzx_mod_gcd([2], [2,4,2]) == ([2], [1], [1, 2, 1]) assert zzx_heu_gcd([2,4,2], [1,1]) == ([1, 1], [2, 2], [1]) assert zzx_mod_gcd([2,4,2], [1,1]) == ([1, 1], [2, 2], [1]) assert zzx_heu_gcd([1,1], [2,4,2]) == ([1, 1], [1], [2, 2]) assert zzx_mod_gcd([1,1], [2,4,2]) == ([1, 1], [1], [2, 2]) f, g = [1, -31], [1, 0] assert zzx_heu_gcd(f, g) == ([1], f, g) assert zzx_mod_gcd(f, g) == ([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 zzx_heu_gcd(f, g) == (h, cff, cfg) assert zzx_mod_gcd(f, g) == (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 zzx_heu_gcd(f, g) == (h, cff, cfg) assert zzx_mod_gcd(f, g) == (h, cff, cfg) assert zzx_gcd(f, g) == h assert zzx_cofactors(f, g) == (h, cff, cfg) f,g,h = zzX_fateman_poly_F_3(4) H, cff, cfg = zzX_cofactors(f, g) assert H == h and zzX_mul(H, cff) == f \ and zzX_mul(H, cfg) == g
def test_zzX_heu_gcd(): f, g, h = zzX_fateman_poly_F_1(2) H, cff, cfg = zzX_heu_gcd(f, g) assert H == h and zzX_mul(H, cff) == f \ and zzX_mul(H, cfg) == g f, g, h = zzX_fateman_poly_F_1(4) H, cff, cfg = zzX_heu_gcd(f, g) assert H == h and zzX_mul(H, cff) == f \ and zzX_mul(H, cfg) == g f, g, h = zzX_fateman_poly_F_1(6) H, cff, cfg = zzX_heu_gcd(f, g) assert H == h and zzX_mul(H, cff) == f \ and zzX_mul(H, cfg) == g f, g, h = zzX_fateman_poly_F_2(2) H, cff, cfg = zzX_heu_gcd(f, g) assert H == h and zzX_mul(H, cff) == f \ and zzX_mul(H, cfg) == g f, g, h = zzX_fateman_poly_F_3(2) H, cff, cfg = zzX_heu_gcd(f, g) assert H == h and zzX_mul(H, cff) == f \ and zzX_mul(H, cfg) == g f, g, h = zzX_fateman_poly_F_1(8) H, cff, cfg = zzX_heu_gcd(f, g) assert H == h and zzX_mul(H, cff) == f \ and zzX_mul(H, cfg) == g