3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-04-25 01:55:32 +00:00

Merge branch 'upstream-master' into release-1.0

Conflicts:
	src/cmd_context/check_logic.cpp
	src/cmd_context/cmd_context.cpp
	src/cmd_context/cmd_context.h
	src/smt/params/smt_params_helper.pyg
	src/smt/smt_context.cpp
This commit is contained in:
Murphy Berzish 2017-02-18 15:04:44 -05:00
commit 235ea79043
588 changed files with 21784 additions and 15202 deletions

View file

@ -13,9 +13,6 @@ Copyright (c) 2015 Microsoft Corporation
expr_rand::expr_rand(ast_manager& m):
m_manager(m),
m_num_vars(0),
m_num_apps(0),
m_num_nodes(0),
m_max_steps(10),
m_funcs(m)
{}

View file

@ -24,9 +24,6 @@ Revision History:
class expr_rand {
ast_manager& m_manager;
unsigned m_num_vars;
unsigned m_num_apps;
unsigned m_num_nodes;
unsigned m_max_steps;
random_gen m_random;

View file

@ -0,0 +1,115 @@
/*++
Copyright (c) 2016 Microsoft Corporation
--*/
#include "inc_sat_solver.h"
#include "bv_decl_plugin.h"
#include "datatype_decl_plugin.h"
#include "reg_decl_plugins.h"
#include "ast_pp.h"
#include "dt2bv_tactic.h"
#include "tactic.h"
#include "model_smt2_pp.h"
#include "fd_solver.h"
static expr_ref mk_const(ast_manager& m, char const* name, sort* s) {
return expr_ref(m.mk_const(symbol(name), s), m);
}
static expr_ref mk_bool(ast_manager& m, char const* name) {
return expr_ref(m.mk_const(symbol(name), m.mk_bool_sort()), m);
}
static expr_ref mk_bv(ast_manager& m, char const* name, unsigned sz) {
bv_util bv(m);
return expr_ref(m.mk_const(symbol(name), bv.mk_sort(sz)), m);
}
static void test1() {
ast_manager m;
reg_decl_plugins(m);
bv_util bv(m);
params_ref p;
ref<solver> solver = mk_inc_sat_solver(m, p);
expr_ref a = mk_bool(m, "a"), b = mk_bool(m, "b"), c = mk_bool(m, "c");
expr_ref ba = mk_bv(m, "ba", 3), bb = mk_bv(m, "bb", 3), bc = mk_bv(m, "bc", 3);
solver->assert_expr(m.mk_implies(a, b));
solver->assert_expr(m.mk_implies(b, c));
expr_ref_vector asms(m), vars(m), conseq(m);
asms.push_back(a);
vars.push_back(b);
vars.push_back(c);
vars.push_back(bb);
vars.push_back(bc);
solver->assert_expr(m.mk_eq(ba, bc));
solver->assert_expr(m.mk_eq(bv.mk_numeral(2, 3), ba));
solver->get_consequences(asms, vars, conseq);
std::cout << conseq << "\n";
}
void test2() {
ast_manager m;
reg_decl_plugins(m);
bv_util bv(m);
datatype_util dtutil(m);
params_ref p;
datatype_decl_plugin & dt = *(static_cast<datatype_decl_plugin*>(m.get_plugin(m.get_family_id("datatype"))));
sort_ref_vector new_sorts(m);
constructor_decl* R = mk_constructor_decl(symbol("R"), symbol("is-R"), 0, 0);
constructor_decl* G = mk_constructor_decl(symbol("G"), symbol("is-G"), 0, 0);
constructor_decl* B = mk_constructor_decl(symbol("B"), symbol("is-B"), 0, 0);
constructor_decl* constrs[3] = { R, G, B };
datatype_decl * enum_sort = mk_datatype_decl(symbol("RGB"), 3, constrs);
VERIFY(dt.mk_datatypes(1, &enum_sort, new_sorts));
del_constructor_decls(3, constrs);
sort* rgb = new_sorts[0].get();
expr_ref x = mk_const(m, "x", rgb), y = mk_const(m, "y", rgb), z = mk_const(m, "z", rgb);
ptr_vector<func_decl> const& enums = *dtutil.get_datatype_constructors(rgb);
expr_ref r = expr_ref(m.mk_const(enums[0]), m);
expr_ref g = expr_ref(m.mk_const(enums[1]), m);
expr_ref b = expr_ref(m.mk_const(enums[2]), m);
ref<solver> fd_solver = mk_fd_solver(m, p);
fd_solver->assert_expr(m.mk_not(m.mk_eq(x, r)));
fd_solver->assert_expr(m.mk_not(m.mk_eq(x, b)));
expr_ref_vector asms(m), vars(m), conseq(m);
vars.push_back(x);
vars.push_back(y);
VERIFY(l_true == fd_solver->get_consequences(asms, vars, conseq));
std::cout << conseq << "\n";
conseq.reset();
fd_solver->push();
fd_solver->assert_expr(m.mk_not(m.mk_eq(x, g)));
VERIFY(l_false == fd_solver->check_sat(0,0));
fd_solver->pop(1);
VERIFY(l_true == fd_solver->get_consequences(asms, vars, conseq));
std::cout << conseq << "\n";
conseq.reset();
model_ref mr;
fd_solver->get_model(mr);
model_smt2_pp(std::cout << "model:\n", m, *mr.get(), 0);
VERIFY(l_true == fd_solver->check_sat(0,0));
fd_solver->get_model(mr);
SASSERT(mr.get());
model_smt2_pp(std::cout, m, *mr.get(), 0);
}
void tst_get_consequences() {
test1();
test2();
}

View file

@ -193,7 +193,6 @@ int main(int argc, char ** argv) {
TST(polynomial);
TST(upolynomial);
TST(algebraic);
TST(polynomial_factorization);
TST(prime_generator);
TST(permutation);
TST(nlsat);
@ -228,6 +227,8 @@ int main(int argc, char ** argv) {
TST(pdr);
TST_ARGV(ddnf);
TST(model_evaluator);
TST(get_consequences);
TST(pb2bv);
//TST_ARGV(hs);
}

View file

@ -287,9 +287,76 @@ static void test7() {
mbo.display(std::cout);
}
static void test8() {
opt::model_based_opt mbo;
unsigned x0 = mbo.add_var(rational(2));
unsigned x = mbo.add_var(rational(1));
unsigned y = mbo.add_var(rational(3));
unsigned z = mbo.add_var(rational(4));
unsigned u = mbo.add_var(rational(5));
unsigned v = mbo.add_var(rational(6));
unsigned w = mbo.add_var(rational(6));
add_ineq(mbo, x0, 1, y, -1, 0, opt::t_le);
add_ineq(mbo, x, 1, y, -1, 0, opt::t_lt);
add_ineq(mbo, y, 1, u, -1, 0, opt::t_le);
add_ineq(mbo, y, 1, z, -1, 1, opt::t_le);
add_ineq(mbo, y, 1, v, -1, 1, opt::t_le);
mbo.display(std::cout);
mbo.project(1, &y);
mbo.display(std::cout);
}
static void test9() {
opt::model_based_opt mbo;
unsigned x0 = mbo.add_var(rational(2), true);
unsigned x = mbo.add_var(rational(1), true);
unsigned y = mbo.add_var(rational(3), true);
unsigned z = mbo.add_var(rational(4), true);
unsigned u = mbo.add_var(rational(5), true);
unsigned v = mbo.add_var(rational(6), true);
add_ineq(mbo, x0, 1, y, -1, 0, opt::t_le);
add_ineq(mbo, x, 1, y, -1, 0, opt::t_lt);
add_ineq(mbo, y, 1, u, -1, 0, opt::t_le);
add_ineq(mbo, y, 1, z, -1, 1, opt::t_le);
add_ineq(mbo, y, 1, v, -1, 1, opt::t_le);
mbo.display(std::cout);
mbo.project(1, &y);
mbo.display(std::cout);
}
static void test10() {
opt::model_based_opt mbo;
unsigned x0 = mbo.add_var(rational(2), true);
unsigned x = mbo.add_var(rational(1), true);
unsigned y = mbo.add_var(rational(3), true);
unsigned z = mbo.add_var(rational(4), true);
unsigned u = mbo.add_var(rational(5), true);
unsigned v = mbo.add_var(rational(6), true);
add_ineq(mbo, x0, 1, y, -2, 0, opt::t_le);
add_ineq(mbo, x, 1, y, -2, 0, opt::t_lt);
add_ineq(mbo, y, 3, u, -4, 0, opt::t_le);
add_ineq(mbo, y, 3, z, -5, 1, opt::t_le);
add_ineq(mbo, y, 3, v, -6, 1, opt::t_le);
mbo.display(std::cout);
mbo.project(1, &y);
mbo.display(std::cout);
mbo.project(1, &x0);
mbo.display(std::cout);
}
// test with mix of upper and lower bounds
void tst_model_based_opt() {
test10();
return;
check_random_ineqs();
test1();
test2();
@ -298,5 +365,6 @@ void tst_model_based_opt() {
test5();
test6();
test7();
test8();
test9();
}

View file

@ -207,7 +207,7 @@ static void tst_set64(unsigned N, unsigned prec) {
mpff_manager fm(prec);
scoped_mpff a(fm);
fm.set(a, INT64_MAX);
fm.set(a, static_cast<int64>(INT64_MAX));
SASSERT(fm.is_int64(a));
SASSERT(fm.is_uint64(a));
fm.inc(a);
@ -221,7 +221,7 @@ static void tst_set64(unsigned N, unsigned prec) {
SASSERT(fm.is_int64(a));
SASSERT(fm.is_uint64(a));
fm.set(a, INT64_MIN);
fm.set(a, static_cast<int64>(INT64_MIN));
SASSERT(fm.is_int64(a));
SASSERT(!fm.is_uint64(a));
fm.dec(a);
@ -235,7 +235,7 @@ static void tst_set64(unsigned N, unsigned prec) {
SASSERT(fm.is_int64(a));
SASSERT(!fm.is_uint64(a));
fm.set(a, UINT64_MAX);
fm.set(a, static_cast<uint64>(UINT64_MAX));
SASSERT(fm.is_uint64(a));
SASSERT(!fm.is_int64(a));
fm.inc(a);
@ -600,7 +600,7 @@ static void tst_div(unsigned prec) {
scoped_mpff a(m), b(m), c(m);
m.round_to_plus_inf();
m.set(a, 1);
m.set(b, UINT64_MAX);
m.set(b, static_cast<uint64>(UINT64_MAX));
m.div(a, b, c);
m.display_raw(std::cout, a); std::cout << "\n";
m.display_raw(std::cout, b); std::cout << "\n";

View file

@ -280,7 +280,7 @@ void tst_int_min_bug() {
mpz big;
mpz expected;
mpz r;
m.set(big, UINT64_MAX);
m.set(big, static_cast<uint64>(UINT64_MAX));
m.set(expected, "18446744075857035263");
m.sub(big, intmin, r);
std::cout << "r: " << m.to_string(r) << "\nexpected: " << m.to_string(expected) << "\n";

195
src/test/pb2bv.cpp Normal file
View file

@ -0,0 +1,195 @@
/*++
Copyright (c) 2015 Microsoft Corporation
--*/
#include "trace.h"
#include "vector.h"
#include "ast.h"
#include "ast_pp.h"
#include "statistics.h"
#include "reg_decl_plugins.h"
#include "pb2bv_rewriter.h"
#include "smt_kernel.h"
#include "model_smt2_pp.h"
#include "smt_params.h"
#include "ast_util.h"
#include "pb_decl_plugin.h"
#include "th_rewriter.h"
#include "fd_solver.h"
#include "solver.h"
static void test1() {
ast_manager m;
reg_decl_plugins(m);
pb_util pb(m);
params_ref p;
pb2bv_rewriter rw(m, p);
expr_ref_vector vars(m);
unsigned N = 5;
for (unsigned i = 0; i < N; ++i) {
std::stringstream strm;
strm << "b" << i;
vars.push_back(m.mk_const(symbol(strm.str().c_str()), m.mk_bool_sort()));
}
for (unsigned k = 1; k <= N; ++k) {
expr_ref fml(m), result(m);
proof_ref proof(m);
fml = pb.mk_at_least_k(vars.size(), vars.c_ptr(), k);
rw(fml, result, proof);
std::cout << fml << " |-> " << result << "\n";
}
expr_ref_vector lemmas(m);
rw.flush_side_constraints(lemmas);
std::cout << lemmas << "\n";
}
static void test_semantics(ast_manager& m, expr_ref_vector const& vars, vector<rational> const& coeffs, unsigned k, unsigned kind) {
pb_util pb(m);
params_ref p;
pb2bv_rewriter rw(m, p);
unsigned N = vars.size();
expr_ref fml1(m), fml2(m), result1(m), result2(m);
proof_ref proof(m);
expr_ref_vector lemmas(m);
th_rewriter th_rw(m);
switch (kind) {
case 0: fml1 = pb.mk_ge(vars.size(), coeffs.c_ptr(), vars.c_ptr(), rational(k)); break;
case 1: fml1 = pb.mk_le(vars.size(), coeffs.c_ptr(), vars.c_ptr(), rational(k)); break;
default: fml1 = pb.mk_eq(vars.size(), coeffs.c_ptr(), vars.c_ptr(), rational(k)); break;
}
rw(fml1, result1, proof);
rw.flush_side_constraints(lemmas);
std::cout << lemmas << "\n";
for (unsigned values = 0; values < static_cast<unsigned>(1 << N); ++values) {
smt_params fp;
smt::kernel solver(m, fp);
expr_ref_vector tf(m);
for (unsigned i = 0; i < N; ++i) {
bool is_true = 0 != (values & (1 << i));
tf.push_back(is_true ? m.mk_true() : m.mk_false());
solver.assert_expr(is_true ? vars[i] : m.mk_not(vars[i]));
}
solver.assert_expr(lemmas);
switch (kind) {
case 0: fml2 = pb.mk_ge(tf.size(), coeffs.c_ptr(), tf.c_ptr(), rational(k)); break;
case 1: fml2 = pb.mk_le(tf.size(), coeffs.c_ptr(), tf.c_ptr(), rational(k)); break;
default: fml2 = pb.mk_eq(tf.size(), coeffs.c_ptr(), tf.c_ptr(), rational(k)); break;
}
std::cout << fml1 << " " << fml2 << "\n";
th_rw(fml2, result2, proof);
SASSERT(m.is_true(result2) || m.is_false(result2));
lbool res = solver.check();
SASSERT(res == l_true);
solver.assert_expr(m.is_true(result2) ? m.mk_not(result1) : result1.get());
res = solver.check();
SASSERT(res == l_false);
}
}
static void test_semantics(ast_manager& m, expr_ref_vector const& vars, vector<rational> const& coeffs, unsigned k) {
test_semantics(m, vars, coeffs, k, 0);
test_semantics(m, vars, coeffs, k, 1);
test_semantics(m, vars, coeffs, k, 2);
}
static void test2() {
ast_manager m;
reg_decl_plugins(m);
expr_ref_vector vars(m);
unsigned N = 4;
for (unsigned i = 0; i < N; ++i) {
std::stringstream strm;
strm << "b" << i;
vars.push_back(m.mk_const(symbol(strm.str().c_str()), m.mk_bool_sort()));
}
for (unsigned coeff = 0; coeff < static_cast<unsigned>(1 << N); ++coeff) {
vector<rational> coeffs;
for (unsigned i = 0; i < N; ++i) {
bool is_one = 0 != (coeff & (1 << i));
coeffs.push_back(is_one ? rational(1) : rational(2));
}
for (unsigned i = 0; i <= N; ++i) {
test_semantics(m, vars, coeffs, i);
}
}
}
static void test_solver_semantics(ast_manager& m, expr_ref_vector const& vars, vector<rational> const& coeffs, unsigned k, unsigned kind) {
pb_util pb(m);
params_ref p;
unsigned N = vars.size();
expr_ref fml1(m), fml2(m), result1(m), result2(m);
proof_ref proof(m);
th_rewriter th_rw(m);
switch (kind) {
case 0: fml1 = pb.mk_ge(vars.size(), coeffs.c_ptr(), vars.c_ptr(), rational(k)); break;
case 1: fml1 = pb.mk_le(vars.size(), coeffs.c_ptr(), vars.c_ptr(), rational(k)); break;
default: fml1 = pb.mk_eq(vars.size(), coeffs.c_ptr(), vars.c_ptr(), rational(k)); break;
}
result1 = m.mk_fresh_const("xx", m.mk_bool_sort());
for (unsigned values = 0; values < static_cast<unsigned>(1 << N); ++values) {
ref<solver> slv = mk_fd_solver(m, p);
expr_ref_vector tf(m);
for (unsigned i = 0; i < N; ++i) {
bool is_true = 0 != (values & (1 << i));
tf.push_back(is_true ? m.mk_true() : m.mk_false());
slv->assert_expr(is_true ? vars[i] : m.mk_not(vars[i]));
}
slv->assert_expr(m.mk_eq(result1, fml1));
switch (kind) {
case 0: fml2 = pb.mk_ge(tf.size(), coeffs.c_ptr(), tf.c_ptr(), rational(k)); break;
case 1: fml2 = pb.mk_le(tf.size(), coeffs.c_ptr(), tf.c_ptr(), rational(k)); break;
default: fml2 = pb.mk_eq(tf.size(), coeffs.c_ptr(), tf.c_ptr(), rational(k)); break;
}
std::cout << fml1 << " " << fml2 << "\n";
th_rw(fml2, result2, proof);
SASSERT(m.is_true(result2) || m.is_false(result2));
lbool res = slv->check_sat(0,0);
SASSERT(res == l_true);
slv->assert_expr(m.is_true(result2) ? m.mk_not(result1) : result1.get());
res = slv->check_sat(0,0);
SASSERT(res == l_false);
}
}
static void test_solver_semantics(ast_manager& m, expr_ref_vector const& vars, vector<rational> const& coeffs, unsigned k) {
test_solver_semantics(m, vars, coeffs, k, 0);
test_solver_semantics(m, vars, coeffs, k, 1);
test_solver_semantics(m, vars, coeffs, k, 2);
}
static void test3() {
ast_manager m;
reg_decl_plugins(m);
expr_ref_vector vars(m);
unsigned N = 4;
for (unsigned i = 0; i < N; ++i) {
std::stringstream strm;
strm << "b" << i;
vars.push_back(m.mk_const(symbol(strm.str().c_str()), m.mk_bool_sort()));
}
for (unsigned coeff = 0; coeff < static_cast<unsigned>(1 << N); ++coeff) {
vector<rational> coeffs;
for (unsigned i = 0; i < N; ++i) {
bool is_one = 0 != (coeff & (1 << i));
coeffs.push_back(is_one ? rational(1) : rational(2));
}
for (unsigned i = 0; i <= N; ++i) {
test_solver_semantics(m, vars, coeffs, i);
}
}
}
void tst_pb2bv() {
test1();
test2();
test3();
}

View file

@ -18,7 +18,6 @@ Notes:
--*/
#if !defined(__clang__)
#include"polynomial.h"
#include"polynomial_factorization.h"
#include"polynomial_var2value.h"
#include"polynomial_cache.h"
#include"linear_eq_solver.h"

View file

@ -1,746 +0,0 @@
/*++
Copyright (c) 2011 Microsoft Corporation
Module Name:
polynomial_factorization.cpp
Abstract:
Testing of factorization.
Author:
Dejan (t-dejanj) 2011-11-29
Notes:
--*/
#include"upolynomial_factorization_int.h"
#include"timeit.h"
#include"polynomial.h"
#include"rlimit.h"
#if 0
#include"polynomial_factorization.h"
#endif
using std::cout;
using std::endl;
// some prime numbers
unsigned primes[] = {
2, 3, 5, 7, 11, 13, 17, 19, 23, 29
};
// [i,l]: how many factors the Knuth example has over p_i, when i = 0 it's Z, p_1 = 2, for l=0 distinct, for l = 1 total
unsigned knuth_factors[2][11] = {
// x^8 + x^6 + 10*x^4 + 10*x^3 + 8*x^2 + 2*x + 8
{2, 2, 3, 3, 2, 3, 1, 4, 3, 1, 1},
{8, 2, 3, 3, 2, 3, 1, 4, 3, 1, 1},
};
// [k,l,i]: how many factors the S_k has over p_i, when i = 0 it's Z, p_1 = 2, for l=0 distinct, for l = 1 total
unsigned swinnerton_dyer_factors[5][2][11] = {
// S1 = (x^2) - 2
{
// 2, 3, 5, 7,11,13,17,19,23,29, Z
{1, 1, 1, 2, 1, 1, 2, 1, 2, 1, 1},
{2, 1, 1, 2, 1, 1, 2, 1, 2, 1, 1}
},
// S2 = (x^4) - 10*(x^2) + 1
{
{1, 1, 2, 2, 2, 2, 2, 2, 4, 2, 1},
{4, 2, 2, 2, 2, 2, 2, 2, 4, 2, 1}
},
// S3 = (x^8) - 40*(x^6) + 352*(x^4) - 960*(x^2) + 576
{
{1, 2, 2, 4, 4, 4, 4, 4, 4, 4, 1},
{8, 6, 4, 4, 4, 4, 4, 4, 4, 4, 1}
},
// S4 = (x^16) - 136*(x^14) + 6476*(x^12) - 141912*(x^10) + 1513334*(x^8) - 7453176*(x^6) + 13950764*(x^4) - 5596840*(x^2) + 46225
{
{1, 4, 3, 4, 8, 8, 8, 8, 8, 8, 1},
{16, 12, 10, 8, 8, 8, 8, 8, 8, 8, 1}
},
// SA = S1*S2*S3*S4
{
//p = 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, Z
{ 2, 6, 3, 6, 15, 11, 16, 15, 18, 15, 1},
{30, 21, 17, 16, 15, 15, 16, 15, 18, 15, 1}
}
};
int random_polynomial[20][2][11] = {
{
// 3*x^10 + 2*x^9 + 4*x^8 + 4*x^7 + 4*x^6 + x^5 + 3*x^2 + 3*x
{ 4, 3, 4, 4, 3, 4, 4, 4, 3, 4, 2 },
{ 7, 7, 4, 4, 3, 4, 4, 4, 3, 4, 2 },
},
{
// 4*x^9 + 4*x^8 + x^7 + x^6 + 2*x^5 + 3*x^4 + 4*x^2 + 4*x
{ 2, 2, 3, 3, 4, 2, 5, 3, 4, 2, 2 },
{ 5, 2, 3, 3, 4, 2, 5, 3, 5, 2, 2 },
},
{
// 3*x^10 + 4*x^9 + 3*x^8 + x^6 + 4*x^5 + 4*x^4 + x^2
{ 3, 2, 4, 4, 5, 3, 4, 2, 4, 5, 2 },
{ 6, 3, 5, 5, 6, 4, 5, 3, 5, 7, 3 },
},
{
// x^10 + 4*x^9 + x^8 + 3*x^7 + 3*x^4 + 3*x^3 + x^2 + 4*x
{ 3, 4, 4, 3, 3, 3, 4, 4, 5, 3, 2 },
{ 8, 4, 4, 3, 3, 3, 4, 4, 5, 3, 2 },
},
{
// x^9 + 2*x^8 + 3*x^7 + x^6 + 2*x^5 + 4*x^4 + 3*x^2
{ 3, 3, 3, 3, 4, 4, 4, 3, 3, 4, 2 },
{ 5, 6, 4, 5, 5, 6, 5, 4, 4, 5, 3 },
},
{
// x^10 + x^9 + 4*x^7 + x^6 + 3*x^5 + x^4 + x^3 + x
{ 3, 2, 3, 3, 3, 5, 3, 2, 4, 4, 2 },
{ 3, 2, 3, 3, 3, 5, 3, 2, 4, 4, 2 },
},
{
// 4*x^10 + 4*x^9 + x^8 + 2*x^7 + 3*x^6 + 4*x^5 + 3*x^4 + x^3 + 2*x^2 + 4*x
{ 3, 3, 2, 5, 3, 4, 2, 4, 5, 5, 2 },
{ 5, 3, 2, 5, 3, 4, 2, 4, 5, 5, 2 },
},
{
// 3*x^10 + 4*x^9 + 3*x^8 + x^7 + x^6 + 2*x^5 + x^4 + 2*x^3 + 2*x^2 + x
{ 3, 4, 6, 4, 4, 4, 4, 6, 6, 4, 3 },
{ 4, 4, 7, 4, 4, 4, 4, 6, 6, 4, 3 },
},
{
// 4*x^10 + x^9 + x^7 + 2*x^5 + 3*x^3 + x^2 + 4*x
{ 3, 3, 3, 4, 4, 5, 4, 5, 2, 4, 2 },
{ 4, 4, 3, 4, 4, 5, 4, 5, 2, 4, 2 },
},
{
// x^10 + 3*x^9 + 3*x^8 + x^7 + 3*x^6 + 3*x^5 + 3*x^4 + x^2 + 3*x
{ 2, 3, 4, 4, 3, 3, 4, 3, 3, 4, 2 },
{ 2, 4, 5, 4, 3, 3, 4, 3, 3, 4, 2 },
},
{
// x^10 + x^9 + 2*x^8 + x^7 + 4*x^6 + 2*x^5 + 3*x^4 + 4*x^3 + x^2 + 2*x
{ 3, 4, 4, 3, 3, 3, 3, 4, 5, 3, 2 },
{ 4, 4, 4, 3, 3, 3, 3, 4, 5, 3, 2 },
},
{
// 3*x^9 + x^8 + 3*x^7 + 3*x^6 + x^5 + 2*x^4 + 4*x^3 + 4*x^2 + 3*x
{ 4, 3, 3, 3, 5, 3, 6, 4, 2, 2, 2 },
{ 6, 4, 3, 3, 5, 3, 6, 4, 2, 2, 2 },
},
{
// 2*x^10 + 3*x^9 + 2*x^8 + 4*x^7 + x^6 + 3*x^5 + 2*x^3 + 3*x^2 + 2*x + 2
{ 3, 3, 3, 5, 4, 5, 6, 7, 4, 6, 3 },
{ 8, 4, 3, 7, 4, 5, 6, 7, 4, 7, 3 },
},
{
// 3*x^10 + x^9 + 4*x^8 + 2*x^7 + x^6 + 4*x^5 + x^4 + 3*x^3 + x + 2
{ 3, 3, 3, 2, 6, 4, 4, 4, 3, 3, 2 },
{ 3, 3, 3, 2, 6, 5, 4, 5, 3, 3, 2 },
},
{
// 4*x^10 + 2*x^9 + x^8 + x^6 + x^5 + 3*x^4 + 4*x^3 + x^2 + x
{ 3, 4, 2, 4, 4, 4, 4, 2, 3, 3, 2 },
{ 6, 4, 2, 4, 4, 4, 4, 2, 3, 3, 2 },
},
{
// 4*x^10 + 2*x^7 + 4*x^6 + 2*x^3 + x
{ 1, 3, 3, 3, 4, 4, 4, 3, 3, 2, 2 },
{ 1, 3, 3, 3, 4, 4, 4, 3, 3, 2, 2 },
},
{
// 4*x^10 + x^9 + x^8 + 4*x^7 + 4*x^4 + 2*x^2 + x + 4
{ 3, 4, 2, 5, 3, 6, 3, 6, 3, 3, 2 },
{ 3, 6, 2, 5, 3, 6, 3, 6, 3, 3, 2 },
},
{
// 3*x^10 + 2*x^8 + x^7 + x^6 + 3*x^4 + 3*x^3 + 4*x^2 + 3*x
{ 4, 3, 4, 3, 3, 3, 2, 4, 4, 3, 2 },
{ 5, 4, 4, 3, 3, 3, 2, 4, 4, 3, 2 },
},
{
// x^10 + 2*x^9 + 2*x^6 + 4*x^3 + 4*x^2
{ 1, 2, 2, 3, 3, 4, 3, 3, 3, 3, 2 },
{ 10, 3, 3, 4, 4, 6, 4, 4, 4, 4, 3 },
},
{
// x^10 + 2*x^9 + 2*x^8 + 4*x^7 + 4*x^6 + x^5 + x^3 + x^2 + 3*x
{ 2, 4, 2, 3, 3, 3, 5, 5, 6, 2, 2 },
{ 2, 5, 2, 3, 3, 3, 5, 5, 6, 2, 2 },
}
};
#if 0
static void tst_square_free_finite_1() {
polynomial::numeral_manager nm;
reslimit rl; polynomial::manager pm(rl, nm);
// example from Knuth, p. 442
polynomial_ref x(pm);
x = pm.mk_polynomial(pm.mk_var());
// polynomials \prod_{i < p} (x - i)^i
for (unsigned prime_i = 0; prime_i < 5; ++ prime_i)
{
int p = primes[prime_i];
// make the polynomial
polynomial_ref f(pm);
f = x - 1;
for (int i = 2; i < p; ++ i) {
f = f*((x + (-i))^i);
}
cout << "Factoring " << f << " into square-free over Z_" << p << endl;
// convert to univariate over Z_p
upolynomial::zp_manager upm(nm);
upm.set_zp(p);
upolynomial::numeral_vector f_u;
upm.to_numeral_vector(f, f_u);
cout << "Input: "; upm.display(cout, f_u); cout << endl;
// factor it
upolynomial::zp_factors f_factors(upm);
cout << "Start: " << f_factors << endl;
upolynomial::zp_square_free_factor(upm, f_u, f_factors);
upolynomial::numeral_vector mult;
f_factors.multiply(mult);
cout << "Multiplied: "; upm.display(cout, mult); cout << endl;
SASSERT(upm.eq(mult, f_u));
// remove the temps
upm.reset(f_u);
upm.reset(mult);
}
}
static void tst_factor_finite_1() {
polynomial::numeral_manager nm;
reslimit rl; polynomial::manager pm(rl, nm);
// example from Knuth, p. 442
polynomial_ref x(pm);
x = pm.mk_polynomial(pm.mk_var());
polynomial_ref K(pm);
K = (x^8) + (x^6) + 10*(x^4) + 10*(x^3) + 8*(x^2) + 2*x + 8;
// factor them for all the prime numbers
for (unsigned prime_i = 0; prime_i < sizeof(primes)/sizeof(unsigned); ++ prime_i)
{
// make the Z_p
unsigned prime = primes[prime_i];
upolynomial::zp_manager upm(nm);
upm.set_zp(prime);
// make the polynomial in Z_p
upolynomial::numeral_vector K_u;
upm.to_numeral_vector(K, K_u);
cout << "Factoring " << K << "("; upm.display(cout, K_u); cout << ") in Z_" << prime << endl;
cout << "Expecting " << knuth_factors[0][prime_i] << " distinct factors, " << knuth_factors[1][prime_i] << " total" << endl;
// factor it
upolynomial::zp_factors factors(upm);
/* bool factorized = */ upolynomial::zp_factor(upm, K_u, factors);
// check the result
unsigned distinct = factors.distinct_factors();
unsigned total = factors.total_factors();
cout << "Got " << factors << endl;
cout << "Thats " << distinct << " distinct factors, " << total << " total" << endl;
SASSERT(knuth_factors[0][prime_i] == distinct);
SASSERT(knuth_factors[1][prime_i] == total);
upolynomial::numeral_vector multiplied;
factors.multiply(multiplied);
SASSERT(upm.eq(K_u, multiplied));
upm.reset(multiplied);
// remove the temp
upm.reset(K_u);
}
}
static void tst_factor_finite_2() {
polynomial::numeral_manager nm;
reslimit rl; polynomial::manager pm(rl, nm);
polynomial_ref x(pm);
x = pm.mk_polynomial(pm.mk_var());
// Swinnerton-Dyer polynomials (irreducible, modular factors of degree at most 2)
polynomial_ref S1 = (x^2) - 2;
polynomial_ref S2 = (x^4) - 10*(x^2) + 1;
polynomial_ref S3 = (x^8) - 40*(x^6) + 352*(x^4) - 960*(x^2) + 576;
polynomial_ref S4 = (x^16) - 136*(x^14) + 6476*(x^12) - 141912*(x^10) + 1513334*(x^8) - 7453176*(x^6) + 13950764*(x^4) - 5596840*(x^2) + 46225;
vector<polynomial_ref> S;
S.push_back(S1);
S.push_back(S2);
S.push_back(S3);
S.push_back(S4);
S.push_back(S1*S2*S3*S4);
// factor all the S_i them for all the prime numbers
for (unsigned S_i = 0; S_i < S.size(); ++ S_i) {
for (unsigned prime_i = 0; prime_i < sizeof(primes)/sizeof(unsigned); ++ prime_i) {
unsigned prime = primes[prime_i];
upolynomial::zp_manager upm(nm);
upm.set_zp(prime);
upolynomial::numeral_vector S_i_u;
upm.to_numeral_vector(S[S_i], S_i_u);
cout << "Factoring "; upm.display(cout, S_i_u); cout << " over Z_" << prime << endl;
cout << "Expecting " << swinnerton_dyer_factors[S_i][0][prime_i] << " distinct factors, " << swinnerton_dyer_factors[S_i][1][prime_i] << " total" << endl;
upolynomial::zp_factors factors(upm);
upolynomial::zp_factor(upm, S_i_u, factors);
// check the result
unsigned distinct = factors.distinct_factors();
unsigned total = factors.total_factors();
cout << "Got " << factors << endl;
cout << "Thats " << distinct << " distinct factors, " << total << " total" << endl;
SASSERT(swinnerton_dyer_factors[S_i][0][prime_i] == distinct);
SASSERT(swinnerton_dyer_factors[S_i][1][prime_i] == total);
upolynomial::numeral_vector multiplied;
factors.multiply(multiplied);
SASSERT(upm.eq(S_i_u, multiplied));
upm.reset(multiplied);
// remove the temp
upm.reset(S_i_u);
}
}
}
static void tst_factor_finite_3() {
polynomial::numeral_manager nm;
reslimit rl; polynomial::manager pm(rl, nm);
polynomial_ref x(pm);
x = pm.mk_polynomial(pm.mk_var());
// random polynomials
vector<polynomial_ref> random_p;
random_p.push_back( 3*(x^10) + 2*(x^9) + 4*(x^8) + 4*(x^7) + 4*(x^6) + 1*(x^5) + 3*(x^2) + 3*x + 0 );
random_p.push_back( 4*(x^9) + 4*(x^8) + 1*(x^7) + 1*(x^6) + 2*(x^5) + 3*(x^4) + 4*(x^2) + 4*x + 0 );
random_p.push_back( 3*(x^10) + 4*(x^9) + 3*(x^8) + 1*(x^6) + 4*(x^5) + 4*(x^4) + 1*(x^2) + 0 );
random_p.push_back( 1*(x^10) + 4*(x^9) + 1*(x^8) + 3*(x^7) + 3*(x^4) + 3*(x^3) + 1*(x^2) + 4*x + 0 );
random_p.push_back( 1*(x^9) + 2*(x^8) + 3*(x^7) + 1*(x^6) + 2*(x^5) + 4*(x^4) + 3*(x^2) + 0 );
random_p.push_back( 1*(x^10) + 1*(x^9) + 4*(x^7) + 1*(x^6) + 3*(x^5) + 1*(x^4) + 1*(x^3) + 1*x + 0 );
random_p.push_back( 4*(x^10) + 4*(x^9) + 1*(x^8) + 2*(x^7) + 3*(x^6) + 4*(x^5) + 3*(x^4) + 1*(x^3) + 2*(x^2) + 4*x + 0 );
random_p.push_back( 3*(x^10) + 4*(x^9) + 3*(x^8) + 1*(x^7) + 1*(x^6) + 2*(x^5) + 1*(x^4) + 2*(x^3) + 2*(x^2) + 1*x + 0 );
random_p.push_back( 4*(x^10) + 1*(x^9) + 1*(x^7) + 2*(x^5) + 3*(x^3) + 1*(x^2) + 4*x + 0 );
random_p.push_back( 1*(x^10) + 3*(x^9) + 3*(x^8) + 1*(x^7) + 3*(x^6) + 3*(x^5) + 3*(x^4) + 1*(x^2) + 3*x + 0 );
random_p.push_back( 1*(x^10) + 1*(x^9) + 2*(x^8) + 1*(x^7) + 4*(x^6) + 2*(x^5) + 3*(x^4) + 4*(x^3) + 1*(x^2) + 2*x + 0 );
random_p.push_back( 3*(x^9) + 1*(x^8) + 3*(x^7) + 3*(x^6) + 1*(x^5) + 2*(x^4) + 4*(x^3) + 4*(x^2) + 3*x + 0 );
random_p.push_back( 2*(x^10) + 3*(x^9) + 2*(x^8) + 4*(x^7) + 1*(x^6) + 3*(x^5) + 2*(x^3) + 3*(x^2) + 2*x + 2 );
random_p.push_back( 3*(x^10) + 1*(x^9) + 4*(x^8) + 2*(x^7) + 1*(x^6) + 4*(x^5) + 1*(x^4) + 3*(x^3) + 1*x + 2 );
random_p.push_back( 4*(x^10) + 2*(x^9) + 1*(x^8) + 1*(x^6) + 1*(x^5) + 3*(x^4) + 4*(x^3) + 1*(x^2) + 1*x + 0 );
random_p.push_back( 4*(x^10) + 2*(x^7) + 4*(x^6) + 2*(x^3) + 1*x + 0 );
random_p.push_back( 4*(x^10) + 1*(x^9) + 1*(x^8) + 4*(x^7) + 4*(x^4) + 2*(x^2) + 1*x + 4 );
random_p.push_back( 3*(x^10) + 2*(x^8) + 1*(x^7) + 1*(x^6) + 3*(x^4) + 3*(x^3) + 4*(x^2) + 3*x + 0 );
random_p.push_back( 1*(x^10) + 2*(x^9) + 2*(x^6) + 4*(x^3) + 4*(x^2) + 0 );
random_p.push_back( 1*(x^10) + 2*(x^9) + 2*(x^8) + 4*(x^7) + 4*(x^6) + 1*(x^5) + 1*(x^3) + 1*(x^2) + 3*x + 0 );
// factor all the randoms them for all the prime numbers
for (unsigned random_i = 0; random_i < random_p.size(); ++ random_i) {
for (unsigned prime_i = 0; prime_i < sizeof(primes)/sizeof(unsigned); ++ prime_i) {
unsigned prime = primes[prime_i];
upolynomial::zp_manager upm(nm);
upm.set_zp(prime);
upolynomial::numeral_vector poly;
upm.to_numeral_vector(random_p[random_i], poly);
cout << "Factoring "; upm.display(cout, poly); cout << " over Z_" << prime << endl;
cout << "Expecting " << swinnerton_dyer_factors[random_i][0][prime_i] << " distinct factors, " << random_polynomial[random_i][1][prime_i] << " total" << endl;
upolynomial::zp_factors factors(upm);
upolynomial::zp_factor(upm, poly, factors);
// check the result
unsigned distinct = factors.distinct_factors();
unsigned total = factors.total_factors();
cout << "Got " << factors << endl;
cout << "Thats " << distinct << " distinct factors, " << total << " total" << endl;
// SASSERT(random_polynomial[random_i][0][prime_i] == distinct);
// SASSERT(random_polynomial[random_i][1][prime_i] == total);
upolynomial::numeral_vector multiplied;
factors.multiply(multiplied);
bool equal = upm.eq(poly, multiplied);
cout << (equal ? "equal" : "not equal") << endl;
SASSERT(equal);
upm.reset(multiplied);
// remove the temp
upm.reset(poly);
}
}
}
static void tst_factor_enumeration() {
polynomial::numeral_manager nm;
reslimit rl; polynomial::manager pm(rl, nm);
polynomial_ref x(pm);
x = pm.mk_polynomial(pm.mk_var());
vector<polynomial_ref> factors;
for (int i = 0; i < 5; ++ i) {
polynomial_ref factor(pm);
factor = x + i;
factors.push_back(factor);
}
upolynomial::manager upm(nm);
upolynomial::zp_manager upm_13(nm);
upm_13.set_zp(13);
upolynomial::zp_factors factors_13(upm_13);
upolynomial::numeral constant;
nm.set(constant, 10);
factors_13.set_constant(constant);
for (unsigned i = 0; i < 5; ++ i) {
upolynomial::numeral_vector ufactor;
upm_13.to_numeral_vector(factors[i], ufactor);
factors_13.push_back(ufactor, 1);
upm.reset(ufactor);
}
cout << "All: " << factors_13 << endl;
upolynomial::factorization_degree_set degrees(factors_13);
degrees.display(cout); cout << endl;
scoped_mpz_vector left(nm), right(nm);
upolynomial::ufactorization_combination_iterator it(factors_13, degrees);
unsigned i = 0;
it.display(cout);
bool remove = false;
while (it.next(remove)) {
it.left(left);
it.right(right);
cout << "Left " << i << ": "; upm.display(cout, left); cout << endl;
cout << "Right " << i << ": "; upm.display(cout, right); cout << endl;
i ++;
if (i % 3 == 0) {
remove = true;
} else {
remove = false;
}
it.display(cout);
}
// SASSERT(i == 15);
return;
for (unsigned i = 0; i < 5; ++ i) {
factors_13.set_degree(i, factors_13.get_degree(i) + i);
}
cout << "Different: " << factors_13 << " of degree " << factors_13.get_degree() << endl;
upolynomial::factorization_degree_set degrees1(factors_13);
degrees1.display(cout); cout << endl; // [0, ..., 15]
polynomial_ref tmp1 = (x^3) + 1;
polynomial_ref tmp2 = (x^5) + 2;
polynomial_ref tmp3 = (x^7) + 3;
upolynomial::numeral_vector up1, up2, up3;
upm_13.to_numeral_vector(tmp1, up1);
upm_13.to_numeral_vector(tmp2, up2);
upm_13.to_numeral_vector(tmp3, up3);
upolynomial::zp_factors tmp(upm_13);
tmp.push_back(up1, 1);
tmp.push_back(up2, 1);
tmp.push_back(up3, 1);
upm_13.reset(up1);
upm_13.reset(up2);
upm_13.reset(up3);
cout << "Different: " << tmp << " of degree " << tmp.get_degree() << endl;
upolynomial::factorization_degree_set degrees2(tmp);
degrees2.display(cout); cout << endl;
tmp1 = (x^2) + 1;
tmp2 = (x^10) + 2;
tmp3 = x + 3;
upm_13.to_numeral_vector(tmp1, up1);
upm_13.to_numeral_vector(tmp2, up2);
upm_13.to_numeral_vector(tmp3, up3);
tmp.clear();
tmp.push_back(up1, 2);
tmp.push_back(up2, 1);
tmp.push_back(up3, 1);
cout << "Different: " << tmp << " of degree " << tmp.get_degree() << endl;
upm_13.reset(up1);
upm_13.reset(up2);
upm_13.reset(up3);
upolynomial::factorization_degree_set degrees3(tmp);
degrees3.display(cout); cout << endl;
degrees1.intersect(degrees3);
degrees1.display(cout); cout << endl;
}
static void tst_factor_square_free_univariate_1(unsigned max_length) {
polynomial::numeral_manager nm;
upolynomial::numeral test;
upolynomial::numeral p;
nm.set(test, -9);
nm.set(p, 5);
nm.mod(test, p, test);
reslimit rl; polynomial::manager pm(rl, nm);
polynomial_ref x(pm);
x = pm.mk_polynomial(pm.mk_var());
cout << "R.<x> = QQ['x']" << endl;
// let's start with \prod (p_i x^{p_{i+1} - p_{i+1})
unsigned n_primes = sizeof(primes)/sizeof(unsigned);
max_length = std::min(max_length, n_primes);
for(unsigned length = 1; length < max_length; ++ length) {
// starting from prime_i going for length
for(unsigned start_i = 0; start_i < n_primes; ++ start_i) {
polynomial_ref f(pm);
bool first = true;
for (unsigned prime_i = 0; prime_i < length; ++ prime_i) {
int p1 = primes[(start_i + prime_i) % n_primes];
int p2 = primes[(start_i + prime_i + 1) % n_primes];
if (first) {
f = (p1*(x^p2) - p2);
first = false;
} else {
f = f*(p1*(x^p2) - p2);
}
}
upolynomial::manager upm(nm);
scoped_mpz_vector f_u(nm);
upm.to_numeral_vector(f, f_u);
cout << "factoring "; upm.display(cout, f_u); cout << endl;
cout << "expecting " << length << " factors ";
upolynomial::factors factors(upm);
/* bool ok = */ upolynomial::factor_square_free(upm, f_u, factors);
cout << "got " << factors << endl;
SASSERT(factors.distinct_factors() == length);
}
}
}
static void tst_factor_square_free_univariate_2() {
polynomial::numeral_manager nm;
reslimit rl; polynomial::manager pm(rl, nm);
polynomial_ref x(pm);
x = pm.mk_polynomial(pm.mk_var());
// Swinnerton-Dyer polynomials (irreducible, modular factors of degree at most 2)
polynomial_ref S1 = (x^2) - 2;
polynomial_ref S2 = (x^4) - 10*(x^2) + 1;
polynomial_ref S3 = (x^8) - 40*(x^6) + 352*(x^4) - 960*(x^2) + 576;
polynomial_ref S4 = (x^16) - 136*(x^14) + 6476*(x^12) - 141912*(x^10) + 1513334*(x^8) - 7453176*(x^6) + 13950764*(x^4) - 5596840*(x^2) + 46225;
vector<polynomial_ref> S;
S.push_back(S1);
S.push_back(S2);
S.push_back(S3);
S.push_back(S4);
upolynomial::manager upm(nm);
// factor all the S_i them for all the prime numbers
for (unsigned S_i = 0; S_i < S.size(); ++ S_i) {
upolynomial::numeral_vector S_i_u;
upm.to_numeral_vector(S[S_i], S_i_u);
cout << "Factoring "; upm.display(cout, S_i_u); cout << " over Z " << endl;
upolynomial::factors factors(upm);
upolynomial::factor_square_free(upm, S_i_u, factors);
// check the result
cout << "Got " << factors << endl;
// remove the temp
upm.reset(S_i_u);
}
}
static void tst_factor_square_free_univariate_3() {
polynomial::numeral_manager nm;
reslimit rl; polynomial::manager pm(rl, nm);
polynomial_ref x(pm);
x = pm.mk_polynomial(pm.mk_var());
polynomial_ref deg70 = (x^70) - 6*(x^65) - (x^60) + 60*(x^55) - 54*(x^50) - 230*(x^45) + 274*(x^40) + 542*(x^35) - 615*(x^30) - 1120*(x^25) + 1500*(x^20) - 160*(x^15) - 395*(x^10) + 76*(x^5) + 34;
upolynomial::manager upm(nm);
upolynomial::numeral_vector deg70_u;
upm.to_numeral_vector(deg70, deg70_u);
cout << "Factoring "; upm.display(cout, deg70_u); cout << " over Z " << endl;
upolynomial::factors factors(upm);
upolynomial::factor_square_free(upm, deg70_u, factors);
cout << "Got " << factors << endl;
upm.reset(deg70_u);
}
#endif
void tst_factor_swinnerton_dyer_big(unsigned max) {
polynomial::numeral_manager nm;
reslimit rl; polynomial::manager pm(rl, nm);
polynomial_ref x(pm);
x = pm.mk_polynomial(pm.mk_var());
vector<polynomial_ref> roots;
vector<polynomial::var> vars;
unsigned n = std::min(max, static_cast<unsigned>(sizeof(primes)/sizeof(unsigned)));
for(unsigned prime_i = 0; prime_i < n; ++ prime_i) {
int prime = primes[prime_i];
cout << "Computing Swinnerton-Dyer[" << prime_i + 1 << "]" << endl;
polynomial_ref y(pm);
vars.push_back(pm.mk_var());
y = pm.mk_polynomial(vars.back());
polynomial_ref p(pm);
p = (y^2) - prime;
roots.push_back(p);
polynomial_ref computation = x;
for (unsigned i = 0; i < roots.size(); ++ i) {
polynomial_ref var(pm);
var = pm.mk_polynomial(vars[i]);
computation = computation - var;
}
{
timeit timer(true, "computing swinnerton-dyer");
for (unsigned i = 0; i < roots.size(); ++ i) {
polynomial_ref tmp(pm);
pm.resultant(computation, roots[i], vars[i], tmp);
computation = tmp;
}
}
cout << "Computed Swinnerton-Dyer[" << prime_i + 1 << "], degree = " << pm.total_degree(computation) << ", size = " << pm.size(computation) << endl;
cout << "Starting factoring " << endl;
{
timeit timer(true, "factoring swinnerton-dyer");
reslimit rl;
upolynomial::manager upm(rl, nm);
scoped_mpz_vector sd_u(nm);
upm.to_numeral_vector(computation, sd_u);
upolynomial::factors factors(upm);
upolynomial::factor_square_free(upm, sd_u, factors);
cout << "Got " << factors.distinct_factors() << " factors" << endl;
}
}
}
static void tst_factor_square_free_multivariate_1(unsigned max_n) {
#if 0
polynomial::numeral_manager nm;
upolynomial::numeral test;
upolynomial::numeral p;
nm.set(test, -9);
nm.set(p, 5);
nm.mod(test, p, test);
reslimit rl; polynomial::manager pm(rl, nm);
polynomial_ref x(pm);
x = pm.mk_polynomial(pm.mk_var());
polynomial_ref y(pm);
y = pm.mk_polynomial(pm.mk_var());
// lets start simple x^n - y^n
for (unsigned prime_i = 0; prime_i < sizeof(primes)/sizeof(unsigned); ++ prime_i) {
unsigned prime = primes[prime_i];
if (prime > max_n) {
break;
}
polynomial_ref f = (x^prime) - (y^prime);
cout << "factoring: " << f << endl;
// factor
polynomial::factors factors(pm);
polynomial::factor_square_free_primitive(f, factors);
cout << "got: " << factors << endl;
}
#endif
}
void tst_polynomial_factorization() {
enable_trace("polynomial::factorization");
// enable_trace("polynomial::factorization::bughunt");
enable_trace("polynomial::factorization::multivariate");
// enable_trace("upolynomial");
// Z_p square-free factorization tests
// tst_square_free_finite_1();
// Z_p factorization tests
// tst_factor_finite_1();
// tst_factor_finite_2();
// tst_factor_finite_3();
// Z factorization
// tst_factor_enumeration();
// tst_factor_square_free_univariate_1(3);
// tst_factor_square_free_univariate_2();
// tst_factor_square_free_univariate_3();
// tst_factor_swinnerton_dyer_big(3);
// Multivariate factorization
tst_factor_square_free_multivariate_1(3);
}

View file

@ -375,6 +375,9 @@ static void add_random_ineq(
case opt::t_le:
fml = a.mk_le(t1, t2);
break;
case opt::t_mod:
NOT_IMPLEMENTED_YET();
break;
}
fmls.push_back(fml);
mbo.add_constraint(vars, rational(coeff), rel);
@ -382,8 +385,7 @@ static void add_random_ineq(
static void test_maximize(opt::model_based_opt& mbo, ast_manager& m, unsigned num_vars, expr_ref_vector const& fmls, app* t) {
qe::arith_project_plugin plugin(m);
model mdl(m);
expr_ref bound(m);
model mdl(m);
arith_util a(m);
for (unsigned i = 0; i < num_vars; ++i) {
app_ref var(m);
@ -391,7 +393,8 @@ static void test_maximize(opt::model_based_opt& mbo, ast_manager& m, unsigned nu
rational val = mbo.get_value(i);
mdl.register_decl(var->get_decl(), a.mk_numeral(val, false));
}
opt::inf_eps value1 = plugin.maximize(fmls, mdl, t, bound);
expr_ref ge(m), gt(m);
opt::inf_eps value1 = plugin.maximize(fmls, mdl, t, ge, gt);
opt::inf_eps value2 = mbo.maximize();
std::cout << "optimal: " << value1 << " " << value2 << "\n";
mbo.display(std::cout);
@ -438,10 +441,158 @@ static void check_random_ineqs() {
}
}
static void test_project() {
ast_manager m;
reg_decl_plugins(m);
qe::arith_project_plugin plugin(m);
arith_util a(m);
app_ref_vector vars(m);
expr_ref_vector lits(m), ds(m);
model mdl(m);
app_ref x(m), y(m), z(m), u(m);
x = m.mk_const(symbol("x"), a.mk_int());
y = m.mk_const(symbol("y"), a.mk_int());
z = m.mk_const(symbol("z"), a.mk_int());
u = m.mk_const(symbol("u"), a.mk_int());
func_decl_ref f(m);
sort* int_sort = a.mk_int();
f = m.mk_func_decl(symbol("f"), 1, &int_sort, int_sort);
// test non-projection
mdl.register_decl(x->get_decl(), a.mk_int(0));
mdl.register_decl(y->get_decl(), a.mk_int(1));
mdl.register_decl(z->get_decl(), a.mk_int(2));
mdl.register_decl(u->get_decl(), a.mk_int(3));
func_interp* fi = alloc(func_interp, m, 1);
expr_ref_vector nums(m);
nums.push_back(a.mk_int(0));
nums.push_back(a.mk_int(1));
nums.push_back(a.mk_int(2));
fi->insert_new_entry(nums.c_ptr(), a.mk_int(1));
fi->insert_new_entry(nums.c_ptr()+1, a.mk_int(2));
fi->insert_new_entry(nums.c_ptr()+2, a.mk_int(3));
fi->set_else(a.mk_int(10));
mdl.register_decl(f, fi);
vars.reset();
lits.reset();
vars.push_back(x);
lits.push_back(x <= app_ref(m.mk_app(f, (expr*)x), m));
lits.push_back(x < y);
plugin(mdl, vars, lits);
std::cout << lits << "\n";
// test not-equals
vars.reset();
lits.reset();
vars.push_back(x);
lits.push_back(m.mk_not(m.mk_eq(x, y)));
plugin(mdl, vars, lits);
std::cout << lits << "\n";
// test negation of distinct using bound variables
mdl.register_decl(x->get_decl(), a.mk_int(0));
mdl.register_decl(y->get_decl(), a.mk_int(1));
mdl.register_decl(z->get_decl(), a.mk_int(0));
mdl.register_decl(u->get_decl(), a.mk_int(6));
vars.reset();
lits.reset();
ds.reset();
vars.push_back(x);
vars.push_back(y);
ds.push_back(x);
ds.push_back(y);
ds.push_back(z + 2);
ds.push_back(u);
ds.push_back(z);
lits.push_back(m.mk_not(m.mk_distinct(ds.size(), ds.c_ptr())));
plugin(mdl, vars, lits);
std::cout << lits << "\n";
// test negation of distinct, not using bound variables
mdl.register_decl(x->get_decl(), a.mk_int(0));
mdl.register_decl(y->get_decl(), a.mk_int(1));
mdl.register_decl(z->get_decl(), a.mk_int(0));
mdl.register_decl(u->get_decl(), a.mk_int(6));
vars.reset();
lits.reset();
ds.reset();
vars.push_back(x);
vars.push_back(y);
ds.push_back(x);
ds.push_back(y);
ds.push_back(z + 2);
ds.push_back(u);
ds.push_back(z + 10);
ds.push_back(u + 4);
lits.push_back(m.mk_not(m.mk_distinct(ds.size(), ds.c_ptr())));
plugin(mdl, vars, lits);
std::cout << lits << "\n";
// test distinct
mdl.register_decl(x->get_decl(), a.mk_int(0));
mdl.register_decl(y->get_decl(), a.mk_int(1));
mdl.register_decl(z->get_decl(), a.mk_int(0));
mdl.register_decl(u->get_decl(), a.mk_int(6));
vars.reset();
lits.reset();
ds.reset();
vars.push_back(x);
vars.push_back(y);
ds.push_back(x);
ds.push_back(y);
ds.push_back(z + 2);
ds.push_back(u);
lits.push_back(m.mk_distinct(ds.size(), ds.c_ptr()));
plugin(mdl, vars, lits);
std::cout << lits << "\n";
// equality over modulus
mdl.register_decl(y->get_decl(), a.mk_int(4));
mdl.register_decl(z->get_decl(), a.mk_int(8));
lits.reset();
vars.reset();
vars.push_back(y);
lits.push_back(m.mk_eq(a.mk_mod(y, a.mk_int(3)), a.mk_int(1)));
lits.push_back(m.mk_eq(2*y, z));
plugin(mdl, vars, lits);
std::cout << lits << "\n";
// inequality test
mdl.register_decl(x->get_decl(), a.mk_int(0));
mdl.register_decl(y->get_decl(), a.mk_int(1));
mdl.register_decl(z->get_decl(), a.mk_int(0));
mdl.register_decl(u->get_decl(), a.mk_int(6));
vars.reset();
lits.reset();
vars.push_back(x);
vars.push_back(y);
lits.push_back(z <= (x + (2*y)));
lits.push_back(2*x < u + 3);
lits.push_back(2*y <= u);
plugin(mdl, vars, lits);
std::cout << lits << "\n";
// non-unit equalities
mdl.register_decl(x->get_decl(), a.mk_int(1));
mdl.register_decl(z->get_decl(), a.mk_int(2));
mdl.register_decl(u->get_decl(), a.mk_int(3));
mdl.register_decl(y->get_decl(), a.mk_int(4));
lits.reset();
vars.reset();
vars.push_back(x);
lits.push_back(m.mk_eq(2*x, z));
lits.push_back(m.mk_eq(3*x, u));
plugin(mdl, vars, lits);
std::cout << lits << "\n";
}
void tst_qe_arith() {
test_project();
return;
check_random_ineqs();
return;
// enable_trace("qe");

View file

@ -22,7 +22,7 @@ struct ast_ext {
ast_ext(ast_manager& m):m(m) {}
typedef expr* T;
typedef expr_ref_vector vector;
T mk_ite(T a, T b, T c) {
T mk_ite(T a, T b, T c) {
return m.mk_ite(a, b, c);
}
T mk_le(T a, T b) {
@ -34,7 +34,7 @@ struct ast_ext {
}
T mk_default() {
return m.mk_false();
}
}
};
@ -164,17 +164,17 @@ struct ast_ext2 {
literal mk_false() { return m.mk_false(); }
literal mk_true() { return m.mk_true(); }
literal mk_max(literal a, literal b) {
return trail(m.mk_or(a, b));
literal mk_max(literal a, literal b) {
return trail(m.mk_or(a, b));
}
literal mk_min(literal a, literal b) { return trail(m.mk_and(a, b)); }
literal mk_not(literal a) { if (m.is_not(a,a)) return a;
return trail(m.mk_not(a));
literal mk_not(literal a) { if (m.is_not(a,a)) return a;
return trail(m.mk_not(a));
}
std::ostream& pp(std::ostream& out, literal lit) {
return out << mk_pp(lit, m);
}
literal fresh() {
literal fresh() {
return trail(m.mk_fresh_const("x", m.mk_bool_sort()));
}
void mk_clause(unsigned n, literal const* lits) {
@ -200,7 +200,7 @@ static void test_sorting_eq(unsigned n, unsigned k) {
// equality:
std::cout << "eq " << k << "\n";
solver.push();
result = sn.eq(k, in.size(), in.c_ptr());
result = sn.eq(true, k, in.size(), in.c_ptr());
solver.assert_expr(result);
for (unsigned i = 0; i < ext.m_clauses.size(); ++i) {
solver.assert_expr(ext.m_clauses[i].get());
@ -210,7 +210,7 @@ static void test_sorting_eq(unsigned n, unsigned k) {
solver.push();
for (unsigned i = 0; i < k; ++i) {
solver.assert_expr(in[i].get());
solver.assert_expr(in[i].get());
}
res = solver.check();
SASSERT(res == l_true);
@ -256,7 +256,7 @@ static void test_sorting_le(unsigned n, unsigned k) {
SASSERT(res == l_true);
for (unsigned i = 0; i < k; ++i) {
solver.assert_expr(in[i].get());
solver.assert_expr(in[i].get());
}
res = solver.check();
SASSERT(res == l_true);
@ -304,7 +304,7 @@ void test_sorting_ge(unsigned n, unsigned k) {
solver.push();
for (unsigned i = 0; i < n - k; ++i) {
solver.assert_expr(m.mk_not(in[i].get()));
solver.assert_expr(m.mk_not(in[i].get()));
}
res = solver.check();
SASSERT(res == l_true);
@ -332,7 +332,107 @@ void test_sorting5(unsigned n, unsigned k) {
test_sorting_ge(n, k);
}
expr_ref naive_at_most1(expr_ref_vector const& xs) {
ast_manager& m = xs.get_manager();
expr_ref_vector clauses(m);
for (unsigned i = 0; i < xs.size(); ++i) {
for (unsigned j = i + 1; j < xs.size(); ++j) {
clauses.push_back(m.mk_not(m.mk_and(xs[i], xs[j])));
}
}
return mk_and(clauses);
}
void test_at_most_1(unsigned n, bool full) {
ast_manager m;
reg_decl_plugins(m);
expr_ref_vector in(m), out(m);
for (unsigned i = 0; i < n; ++i) {
in.push_back(m.mk_fresh_const("a",m.mk_bool_sort()));
}
ast_ext2 ext(m);
psort_nw<ast_ext2> sn(ext);
expr_ref result1(m), result2(m);
result1 = sn.le(full, 1, in.size(), in.c_ptr());
result2 = naive_at_most1(in);
std::cout << "clauses: " << ext.m_clauses << "\n-----\n";
smt_params fp;
smt::kernel solver(m, fp);
for (unsigned i = 0; i < ext.m_clauses.size(); ++i) {
solver.assert_expr(ext.m_clauses[i].get());
}
lbool res;
if (full) {
solver.push();
solver.assert_expr(m.mk_not(m.mk_eq(result1, result2)));
std::cout << result1 << "\n";
res = solver.check();
SASSERT(res == l_false);
solver.pop(1);
}
if (n >= 9) return;
for (unsigned i = 0; i < static_cast<unsigned>(1 << n); ++i) {
std::cout << "checking: " << n << ": " << i << "\n";
solver.push();
unsigned k = 0;
for (unsigned j = 0; j < n; ++j) {
bool is_true = (i & (1 << j)) != 0;
expr_ref atom(m);
atom = is_true ? in[j].get() : m.mk_not(in[j].get());
solver.assert_expr(atom);
std::cout << atom << "\n";
if (is_true) ++k;
}
res = solver.check();
SASSERT(res == l_true);
if (k > 1) {
solver.assert_expr(result1);
}
else if (!full) {
solver.pop(1);
continue;
}
else {
solver.assert_expr(m.mk_not(result1));
}
res = solver.check();
SASSERT(res == l_false);
solver.pop(1);
}
}
static void test_at_most1() {
ast_manager m;
reg_decl_plugins(m);
expr_ref_vector in(m), out(m);
for (unsigned i = 0; i < 5; ++i) {
in.push_back(m.mk_fresh_const("a",m.mk_bool_sort()));
}
in[4] = in[3].get();
ast_ext2 ext(m);
psort_nw<ast_ext2> sn(ext);
expr_ref result(m);
result = sn.le(true, 1, in.size(), in.c_ptr());
std::cout << result << "\n";
std::cout << ext.m_clauses << "\n";
}
void tst_sorting_network() {
for (unsigned i = 1; i < 17; ++i) {
test_at_most_1(i, true);
test_at_most_1(i, false);
}
test_at_most1();
test_sorting_eq(11,7);
for (unsigned n = 3; n < 20; n += 2) {
for (unsigned k = 1; k < n; ++k) {

View file

@ -131,6 +131,32 @@ static void tst4() {
SASSERT(!s.contains(0));
}
#include "map.h"
template <typename Value>
struct uint_map : public map<uint_set, Value, uint_set::hash, uint_set::eq> {};
static void tst5() {
uint_set s;
std::cout << s.get_hash() << "\n";
s.insert(1);
std::cout << s.get_hash() << "\n";
s.insert(2);
std::cout << s.get_hash() << "\n";
s.insert(44);
std::cout << s.get_hash() << "\n";
uint_map<unsigned> m;
m.insert(s, 1);
s.insert(4);
m.insert(s, 3);
uint_map<unsigned>::iterator it = m.begin(), end = m.end();
for (; it != end; ++it) {
std::cout << it->m_key << " : " << it->m_value << "\n";
}
}
void tst_uint_set() {
for (unsigned i = 0; i < 100; i++) {
tst1(1 + rand()%31);
@ -146,5 +172,6 @@ void tst_uint_set() {
tst3(12);
tst3(100);
tst4();
tst5();
}