3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-04-28 11:25:51 +00:00

Simplify RCF C API. Add Z3_rcf_mk_roots (C API) and MkRoots (Python API). Implement basic root isolation support.

Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
Leonardo de Moura 2013-01-07 12:25:28 -08:00
parent 3c1f1a3b65
commit 09b5724d82
6 changed files with 216 additions and 118 deletions

View file

@ -23,29 +23,10 @@ Notes:
#include"z3.h"
#include"api_log_macros.h"
#include"api_context.h"
#include"api_rcf.h"
#include"realclosure.h"
extern "C" {
void Z3_API Z3_rcf_inc_ref(Z3_context c, Z3_rcf_num a) {
Z3_TRY;
LOG_Z3_rcf_inc_ref(c, a);
RESET_ERROR_CODE();
to_rcf_num(a)->inc_ref();
Z3_CATCH;
}
void Z3_API Z3_rcf_dec_ref(Z3_context c, Z3_rcf_num a) {
Z3_TRY;
LOG_Z3_rcf_dec_ref(c, a);
RESET_ERROR_CODE();
if (to_rcf_num(a)->ref_count() == 1) {
mk_c(c)->rcfm().del(to_rcnumeral(a));
}
to_rcf_num(a)->dec_ref();
Z3_CATCH;
}
static rcmanager & rcfm(Z3_context c) {
return mk_c(c)->rcfm();
}
@ -53,14 +34,24 @@ extern "C" {
static void reset_rcf_cancel(Z3_context c) {
rcfm(c).reset_cancel();
}
static Z3_rcf_num mk_rcf_num(Z3_context c, rcnumeral & a) {
Z3_rcf_num_ref * r = alloc(Z3_rcf_num_ref);
rcfm(c).swap(r->m_num, a);
mk_c(c)->save_object(r);
return of_rcf_num(r);
static rcnumeral to_rcnumeral(Z3_rcf_num a) {
return rcnumeral::mk(a);
}
static Z3_rcf_num from_rcnumeral(rcnumeral a) {
return reinterpret_cast<Z3_rcf_num>(a.c_ptr());
}
void Z3_API Z3_rcf_del(Z3_context c, Z3_rcf_num a) {
Z3_TRY;
LOG_Z3_rcf_del(c, a);
RESET_ERROR_CODE();
rcnumeral _a = to_rcnumeral(a);
rcfm(c).del(_a);
Z3_CATCH;
}
Z3_rcf_num Z3_API Z3_rcf_mk_rational(Z3_context c, Z3_string val) {
Z3_TRY;
LOG_Z3_rcf_mk_rational(c, val);
@ -68,9 +59,9 @@ extern "C" {
reset_rcf_cancel(c);
scoped_mpq q(rcfm(c).qm());
rcfm(c).qm().set(q, val);
scoped_rcnumeral r(rcfm(c));
rcnumeral r;
rcfm(c).set(r, q);
RETURN_Z3(mk_rcf_num(c, r));
RETURN_Z3(from_rcnumeral(r));
Z3_CATCH_RETURN(0);
}
@ -79,9 +70,9 @@ extern "C" {
LOG_Z3_rcf_mk_small_int(c, val);
RESET_ERROR_CODE();
reset_rcf_cancel(c);
scoped_rcnumeral r(rcfm(c));
rcnumeral r;
rcfm(c).set(r, val);
RETURN_Z3(mk_rcf_num(c, r));
RETURN_Z3(from_rcnumeral(r));
Z3_CATCH_RETURN(0);
}
@ -90,9 +81,9 @@ extern "C" {
LOG_Z3_rcf_mk_pi(c);
RESET_ERROR_CODE();
reset_rcf_cancel(c);
scoped_rcnumeral r(rcfm(c));
rcnumeral r;
rcfm(c).mk_pi(r);
RETURN_Z3(mk_rcf_num(c, r));
RETURN_Z3(from_rcnumeral(r));
Z3_CATCH_RETURN(0);
}
@ -101,9 +92,9 @@ extern "C" {
LOG_Z3_rcf_mk_e(c);
RESET_ERROR_CODE();
reset_rcf_cancel(c);
scoped_rcnumeral r(rcfm(c));
rcnumeral r;
rcfm(c).mk_e(r);
RETURN_Z3(mk_rcf_num(c, r));
RETURN_Z3(from_rcnumeral(r));
Z3_CATCH_RETURN(0);
}
@ -112,9 +103,37 @@ extern "C" {
LOG_Z3_rcf_mk_infinitesimal(c, name);
RESET_ERROR_CODE();
reset_rcf_cancel(c);
scoped_rcnumeral r(rcfm(c));
rcnumeral r;
rcfm(c).mk_infinitesimal(name, r);
RETURN_Z3(mk_rcf_num(c, r));
RETURN_Z3(from_rcnumeral(r));
Z3_CATCH_RETURN(0);
}
unsigned Z3_API Z3_rcf_mk_roots(Z3_context c, unsigned n, Z3_rcf_num const a[], Z3_rcf_num roots[]) {
Z3_TRY;
LOG_Z3_rcf_mk_roots(c, n, a, roots);
RESET_ERROR_CODE();
reset_rcf_cancel(c);
rcnumeral_vector av;
unsigned rz = 0;
for (unsigned i = 0; i < n; i++) {
if (!rcfm(c).is_zero(to_rcnumeral(a[i])))
rz = i + 1;
av.push_back(to_rcnumeral(a[i]));
}
if (rz == 0) {
// it is the zero polynomial
SET_ERROR_CODE(Z3_INVALID_ARG);
return 0;
}
av.shrink(rz);
rcnumeral_vector rs;
rcfm(c).isolate_roots(av.size(), av.c_ptr(), rs);
unsigned num_roots = rs.size();
for (unsigned i = 0; i < num_roots; i++) {
roots[i] = from_rcnumeral(rs[i]);
}
return num_roots;
Z3_CATCH_RETURN(0);
}
@ -123,9 +142,9 @@ extern "C" {
LOG_Z3_rcf_add(c, a, b);
RESET_ERROR_CODE();
reset_rcf_cancel(c);
scoped_rcnumeral r(rcfm(c));
rcnumeral r;
rcfm(c).add(to_rcnumeral(a), to_rcnumeral(b), r);
RETURN_Z3(mk_rcf_num(c, r));
RETURN_Z3(from_rcnumeral(r));
Z3_CATCH_RETURN(0);
}
@ -134,9 +153,9 @@ extern "C" {
LOG_Z3_rcf_sub(c, a, b);
RESET_ERROR_CODE();
reset_rcf_cancel(c);
scoped_rcnumeral r(rcfm(c));
rcnumeral r;
rcfm(c).sub(to_rcnumeral(a), to_rcnumeral(b), r);
RETURN_Z3(mk_rcf_num(c, r));
RETURN_Z3(from_rcnumeral(r));
Z3_CATCH_RETURN(0);
}
@ -145,9 +164,9 @@ extern "C" {
LOG_Z3_rcf_mul(c, a, b);
RESET_ERROR_CODE();
reset_rcf_cancel(c);
scoped_rcnumeral r(rcfm(c));
rcnumeral r;
rcfm(c).mul(to_rcnumeral(a), to_rcnumeral(b), r);
RETURN_Z3(mk_rcf_num(c, r));
RETURN_Z3(from_rcnumeral(r));
Z3_CATCH_RETURN(0);
}
@ -156,9 +175,9 @@ extern "C" {
LOG_Z3_rcf_div(c, a, b);
RESET_ERROR_CODE();
reset_rcf_cancel(c);
scoped_rcnumeral r(rcfm(c));
rcnumeral r;
rcfm(c).div(to_rcnumeral(a), to_rcnumeral(b), r);
RETURN_Z3(mk_rcf_num(c, r));
RETURN_Z3(from_rcnumeral(r));
Z3_CATCH_RETURN(0);
}
@ -167,9 +186,9 @@ extern "C" {
LOG_Z3_rcf_neg(c, a);
RESET_ERROR_CODE();
reset_rcf_cancel(c);
scoped_rcnumeral r(rcfm(c));
rcnumeral r;
rcfm(c).neg(to_rcnumeral(a), r);
RETURN_Z3(mk_rcf_num(c, r));
RETURN_Z3(from_rcnumeral(r));
Z3_CATCH_RETURN(0);
}
@ -178,9 +197,9 @@ extern "C" {
LOG_Z3_rcf_inv(c, a);
RESET_ERROR_CODE();
reset_rcf_cancel(c);
scoped_rcnumeral r(rcfm(c));
rcnumeral r;
rcfm(c).inv(to_rcnumeral(a), r);
RETURN_Z3(mk_rcf_num(c, r));
RETURN_Z3(from_rcnumeral(r));
Z3_CATCH_RETURN(0);
}