mirror of
https://github.com/Z3Prover/z3
synced 2025-04-28 11:25:51 +00:00
code review (#98)
* streamline type conversions Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com> * nits Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com> * updates Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com> * na Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com> * use fixed array allocation for sum Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com> * use fixed array allocation for sum Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com> * revert creation time allocation Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com> * fix assertion Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com> * separate grobner_core Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com> * grobner_core simplifications Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
parent
9661f75246
commit
14094bb052
12 changed files with 685 additions and 717 deletions
|
@ -19,6 +19,7 @@
|
|||
--*/
|
||||
#pragma once
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include "util/map.h"
|
||||
#include "math/lp/nex.h"
|
||||
namespace nla {
|
||||
|
@ -59,7 +60,7 @@ class nex_creator {
|
|||
public:
|
||||
static std::string ch(unsigned j) {
|
||||
std::stringstream s;
|
||||
s << "v" << j;
|
||||
s << "v" << j;
|
||||
return s.str();
|
||||
}
|
||||
|
||||
|
@ -71,51 +72,76 @@ public:
|
|||
unsigned get_number_of_vars() const {
|
||||
return m_active_vars_weights.size();
|
||||
}
|
||||
|
||||
|
||||
void set_var_weight(unsigned j, unsigned weight) {
|
||||
m_active_vars_weights[j] = weight;
|
||||
}
|
||||
|
||||
private:
|
||||
svector<unsigned>& active_vars_weights() { return m_active_vars_weights;}
|
||||
const svector<unsigned>& active_vars_weights() const { return m_active_vars_weights;}
|
||||
svector<unsigned>& active_vars_weights() { return m_active_vars_weights; }
|
||||
const svector<unsigned>& active_vars_weights() const { return m_active_vars_weights; }
|
||||
|
||||
nex_mul* mk_mul(const vector<nex_pow>& v) {
|
||||
auto r = alloc(nex_mul, rational::zero(), v);
|
||||
add_to_allocated(r);
|
||||
return r;
|
||||
}
|
||||
|
||||
void mul_args() { }
|
||||
|
||||
template <typename K>
|
||||
void mul_args(K e) {
|
||||
m_mk_mul *= e;
|
||||
}
|
||||
|
||||
template <typename K, typename ...Args>
|
||||
void mul_args(K e, Args ... es) {
|
||||
m_mk_mul *= e;
|
||||
mul_args(es...);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void add_sum(T) { }
|
||||
|
||||
template <typename T, typename K, typename ...Args>
|
||||
void add_sum(T& r, K e, Args ... es) {
|
||||
r += e;
|
||||
add_sum(r, es ...);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public:
|
||||
nex* simplify(nex* e);
|
||||
|
||||
bool gt(lpvar j, lpvar k) const{
|
||||
|
||||
bool gt(lpvar j, lpvar k) const {
|
||||
unsigned wj = m_active_vars_weights[j];
|
||||
unsigned wk = m_active_vars_weights[k];
|
||||
return wj != wk ? wj > wk : j > k;
|
||||
}
|
||||
|
||||
void simplify_children_of_mul(vector<nex_pow>& children, rational&);
|
||||
|
||||
// just compare the underlying expressions
|
||||
bool gt_on_nex_pow(const nex_pow & a, const nex_pow& b) const {
|
||||
return gt(a.e(), b.e());
|
||||
}
|
||||
|
||||
void simplify_children_of_mul(vector<nex_pow> & children, rational&);
|
||||
|
||||
nex * clone(const nex* a) {
|
||||
nex* clone(const nex* a) {
|
||||
switch (a->type()) {
|
||||
case expr_type::VAR:
|
||||
case expr_type::VAR:
|
||||
return mk_var(to_var(a)->var());
|
||||
case expr_type::SCALAR:
|
||||
case expr_type::SCALAR:
|
||||
return mk_scalar(to_scalar(a)->value());
|
||||
case expr_type::MUL: {
|
||||
auto m = to_mul(a);
|
||||
auto r = mk_mul();
|
||||
for (const auto& p : m->children()) {
|
||||
r->add_child_in_power(clone(p.e()), p.pow());
|
||||
mul_factory mf(*this);
|
||||
for (const auto& p : a->to_mul()) {
|
||||
mf *= nex_pow(clone(p.e()), p.pow());
|
||||
}
|
||||
r->coeff() = m->coeff();
|
||||
return r;
|
||||
mf *= a->to_mul().coeff();
|
||||
return mf.mk();
|
||||
}
|
||||
case expr_type::SUM: {
|
||||
auto r = mk_sum();
|
||||
for (nex * e : *to_sum(a)) {
|
||||
r->add_child(clone(e));
|
||||
sum_factory sf(*this);
|
||||
for (nex const* e : a->to_sum()) {
|
||||
sf += clone(e);
|
||||
}
|
||||
return r;
|
||||
return sf.mk();
|
||||
}
|
||||
default:
|
||||
UNREACHABLE();
|
||||
|
@ -126,86 +152,110 @@ public:
|
|||
|
||||
const std::unordered_map<lpvar, occ>& occurences_map() const { return m_occurences_map; }
|
||||
std::unordered_map<lpvar, occ>& occurences_map() { return m_occurences_map; }
|
||||
const std::unordered_map<lpvar, unsigned> & powers() const { return m_powers; }
|
||||
std::unordered_map<lpvar, unsigned> & powers() { return m_powers; }
|
||||
|
||||
const std::unordered_map<lpvar, unsigned>& powers() const { return m_powers; }
|
||||
std::unordered_map<lpvar, unsigned>& powers() { return m_powers; }
|
||||
|
||||
void add_to_allocated(nex* r) { m_allocated.push_back(r); }
|
||||
|
||||
// NSB: we can use region allocation, but still need to invoke destructor
|
||||
// because of 'rational' (and m_children in nex_mul unless we get rid of this)
|
||||
void pop(unsigned sz) {
|
||||
for (unsigned j = sz; j < m_allocated.size(); j ++)
|
||||
delete m_allocated[j];
|
||||
for (unsigned j = sz; j < m_allocated.size(); j++)
|
||||
dealloc(m_allocated[j]);
|
||||
m_allocated.resize(sz);
|
||||
}
|
||||
|
||||
void clear() {
|
||||
for (auto e: m_allocated)
|
||||
delete e;
|
||||
for (auto e : m_allocated)
|
||||
dealloc(e);
|
||||
m_allocated.clear();
|
||||
}
|
||||
|
||||
nex_creator() : m_mk_mul(*this) {}
|
||||
|
||||
~nex_creator() {
|
||||
clear();
|
||||
}
|
||||
unsigned size() const { return m_allocated.size(); }
|
||||
|
||||
class mul_factory {
|
||||
nex_creator& c;
|
||||
rational m_coeff;
|
||||
vector<nex_pow> m_args;
|
||||
public:
|
||||
mul_factory(nex_creator& c) :c(c), m_coeff(1) {}
|
||||
void reset() { m_coeff = rational::one(); m_args.reset(); }
|
||||
void operator*=(rational const& coeff) { m_coeff *= coeff; }
|
||||
void operator*=(nex_pow const& p) { m_args.push_back(p); }
|
||||
void operator*=(nex const* n) { m_args.push_back(nex_pow(n, 1)); }
|
||||
bool empty() const { return m_args.empty(); }
|
||||
nex_mul* mk() {
|
||||
auto r = alloc(nex_mul, m_coeff, m_args);
|
||||
c.add_to_allocated(r);
|
||||
return r;
|
||||
}
|
||||
nex* mk_reduced() {
|
||||
if (m_args.empty()) return c.mk_scalar(m_coeff);
|
||||
if (m_coeff.is_one() && m_args.size() == 1 && m_args[0].pow() == 1) return m_args[0].e();
|
||||
return mk();
|
||||
}
|
||||
};
|
||||
|
||||
class sum_factory {
|
||||
nex_creator& c;
|
||||
ptr_vector<nex> m_args;
|
||||
public:
|
||||
sum_factory(nex_creator& c) :c(c) {}
|
||||
void reset() { m_args.reset(); }
|
||||
void operator+=(nex const* n) { m_args.push_back(const_cast<nex*>(n)); }
|
||||
void operator+=(nex* n) { m_args.push_back(n); }
|
||||
bool empty() const { return m_args.empty(); }
|
||||
nex_sum* mk() { return c.mk_sum(m_args); }
|
||||
};
|
||||
|
||||
mul_factory m_mk_mul;
|
||||
|
||||
nex_sum* mk_sum() {
|
||||
auto r = new nex_sum();
|
||||
add_to_allocated(r);
|
||||
return r;
|
||||
}
|
||||
template <typename T>
|
||||
void add_children(T) { }
|
||||
|
||||
template <typename T, typename K, typename ...Args>
|
||||
void add_children(T r, K e, Args ... es) {
|
||||
r->add_child(e);
|
||||
add_children(r, es ...);
|
||||
ptr_vector<nex> v0;
|
||||
return mk_sum(v0);
|
||||
}
|
||||
|
||||
nex_sum* mk_sum(const ptr_vector<nex>& v) {
|
||||
auto r = new nex_sum(v);
|
||||
nex_sum* mk_sum(const ptr_vector<nex>& v) {
|
||||
auto r = alloc(nex_sum, v);
|
||||
add_to_allocated(r);
|
||||
return r;
|
||||
}
|
||||
|
||||
nex_mul* mk_mul(const vector<nex_pow>& v) {
|
||||
auto r = new nex_mul();
|
||||
add_to_allocated(r);
|
||||
r->children() = v;
|
||||
return r;
|
||||
}
|
||||
|
||||
template <typename K, typename...Args>
|
||||
nex_sum* mk_sum(K e, Args... es) {
|
||||
auto r = new nex_sum();
|
||||
add_to_allocated(r);
|
||||
r->add_child(e);
|
||||
add_children(r, es...);
|
||||
return r;
|
||||
sum_factory sf(*this);
|
||||
sf += e;
|
||||
add_sum(sf, es...);
|
||||
return sf.mk();
|
||||
}
|
||||
|
||||
nex_var* mk_var(lpvar j) {
|
||||
auto r = new nex_var(j);
|
||||
auto r = alloc(nex_var, j);
|
||||
add_to_allocated(r);
|
||||
return r;
|
||||
}
|
||||
|
||||
nex_mul* mk_mul() {
|
||||
auto r = new nex_mul();
|
||||
auto r = alloc(nex_mul);
|
||||
add_to_allocated(r);
|
||||
return r;
|
||||
}
|
||||
|
||||
template <typename K, typename...Args>
|
||||
nex_mul* mk_mul(K e, Args... es) {
|
||||
auto r = new nex_mul();
|
||||
add_to_allocated(r);
|
||||
add_children(r, e, es...);
|
||||
return r;
|
||||
m_mk_mul.reset();
|
||||
m_mk_mul *= e;
|
||||
mul_args(es...);
|
||||
return m_mk_mul.mk();
|
||||
}
|
||||
|
||||
nex_scalar* mk_scalar(const rational& v) {
|
||||
auto r = new nex_scalar(v);
|
||||
auto r = alloc(nex_scalar, v);
|
||||
add_to_allocated(r);
|
||||
return r;
|
||||
}
|
||||
|
@ -227,30 +277,31 @@ public:
|
|||
|
||||
void mul_to_powers(vector<nex_pow>& children);
|
||||
|
||||
|
||||
void sort_join_sum(ptr_vector<nex> & children);
|
||||
bool fill_join_map_for_sum(ptr_vector<nex> & children,
|
||||
std::map<nex*, rational, nex_lt>& map,
|
||||
std::unordered_set<nex*>& existing_nex,
|
||||
void sort_join_sum(nex_sum & sum);
|
||||
bool fill_join_map_for_sum(nex_sum & sum,
|
||||
std::map<nex const*, rational, nex_lt>& map,
|
||||
std::unordered_set<nex const*>& existing_nex,
|
||||
rational& common_scalar);
|
||||
bool register_in_join_map(std::map<nex*, rational, nex_lt>&, nex*, const rational&) const;
|
||||
bool register_in_join_map(std::map<nex const*, rational, nex_lt>&, nex const*, const rational&) const;
|
||||
|
||||
void simplify_children_of_sum(ptr_vector<nex> & children);
|
||||
void simplify_children_of_sum(nex_sum & sum);
|
||||
|
||||
bool eat_scalar_pow(rational& r, const nex_pow& p, unsigned);
|
||||
|
||||
bool children_are_simplified(const vector<nex_pow>& children) const;
|
||||
bool gt(const nex* a, const nex* b) const;
|
||||
bool gt_nex_powers(const vector<nex_pow>&, const nex* b) const;
|
||||
bool gt_on_powers_mul(const vector<nex_pow>&, const nex_mul& b) const;
|
||||
bool gt(const nex& a, const nex& b) const;
|
||||
bool gt(const nex* a, const nex* b) const { return gt(*a, *b); }
|
||||
template <typename T>
|
||||
bool gt_on_powers_mul_same_degree(const T&, const nex_mul& b) const;
|
||||
bool gt_for_sort_join_sum(const nex* a, const nex* b) const;
|
||||
bool gt_on_mul_mul(const nex_mul& a, const nex_mul& b) const;
|
||||
bool gt_on_var_nex(const nex_var* a, const nex* b) const;
|
||||
bool gt_on_mul_nex(const nex_mul* a, const nex* b) const;
|
||||
bool gt_on_sum_sum(const nex_sum* a, const nex_sum* b) const;
|
||||
void process_map_pair(nex *e, const rational& coeff, ptr_vector<nex> & children, std::unordered_set<nex*>&);
|
||||
bool gt_on_mul_mul(const nex_mul& a, const nex_mul& b) const;
|
||||
bool gt_on_sum_sum(const nex_sum& a, const nex_sum& b) const;
|
||||
bool gt_on_var_nex(const nex_var& a, const nex& b) const;
|
||||
bool gt_on_mul_nex(nex_mul const&, const nex& b) const;
|
||||
bool gt_on_nex_pow(const nex_pow& a, const nex_pow& b) const {
|
||||
return (a.pow() > b.pow()) || (a.pow() == b.pow() && gt(a.e(), b.e()));
|
||||
}
|
||||
void process_map_pair(nex*e, const rational& coeff, nex_sum & sum, std::unordered_set<nex const*>&);
|
||||
#ifdef Z3DEBUG
|
||||
static
|
||||
bool equal(const nex*, const nex* );
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue