25 assert(it !=
m_vars.end());
26 return std::distance(
m_vars.begin(), it);
38 using Poly = carl::MultivariatePolynomial<Rational>;
42 if (f.type() == carl::FormulaType::CONSTRAINT) {
43 auto f_vars = f.variables();
45 vars.begin(),
vars.end(), [&f_vars](
const auto& v){ return f_vars.count(v) > 0; }
47 return Formula(carl::FormulaType::TRUE);
54 std::vector<Formula> constraints = f.subformulas();
57 carl::carlVariables vs;
58 for (
const auto& f : constraints) carl::variables(f, vs);
62 std::size_t constant_col = var_idx.
size();
63 std::size_t delta_col = constant_col + 1;
64 Matrix m(constraints.size(), var_idx.
size() + constraints.size() + 2);
65 std::size_t non_zeros = 2*constraints.size();
66 for (
const auto& f : constraints) non_zeros += f.constraint().lhs().nr_terms();
70 for (std::size_t r = 0; r < constraints.size(); ++r) {
71 carl::Relation rel = constraints[r].constraint().relation();
74 assert(rel != carl::Relation::GREATER && rel != carl::Relation::GEQ);
75 assert(rel != carl::Relation::NEQ);
77 Poly lhs = constraints[r].constraint().lhs();
78 Rational constant_part = lhs.constant_part();
81 std::vector<Matrix::RowEntry> entries;
82 for (
const auto& term : lhs) {
83 entries.emplace_back(var_idx.
index(term.single_variable()), term.coeff());
87 [](
const auto& lhs,
const auto& rhs){ return lhs.col_index < rhs.col_index; }
91 if (!carl::is_zero(constant_part)) entries.emplace_back(constant_col, constant_part);
92 if (rel == carl::Relation::LESS) entries.emplace_back(delta_col,
Rational(1));
93 entries.emplace_back(delta_col + r + 1,
Rational(1));
101 if (res.
n_rows() == 0)
return Formula(carl::FormulaType::TRUE);
102 std::vector<Formula> conjuncts;
103 for (std::size_t i = 0; i < res.
n_rows(); ++i) {
107 for (; it !=row_end && it->col_index < constant_col; ++it) {
110 if (it != row_end && it->col_index == constant_col) {
115 if (it != row_end && it->col_index == delta_col) conjuncts.emplace_back(lhs, carl::Relation::LESS);
116 else conjuncts.emplace_back(lhs, carl::Relation::LEQ);
119 if (conjuncts.size() == 1)
return conjuncts.front();
127 const std::vector<std::size_t>& cols) {
129 Matrix m(constraints.rows(), constraints.cols() + 2 + constraints.rows());
130 std::size_t quantified_cols = cols.size();
132 for (std::size_t i = 0, q = 0; i < constraints.rows(); ++i) {
133 if (q < cols.size() && i == cols[q]) ++q;
137 for (std::size_t i = 0; i < constraints.rows(); ++i) {
138 std::vector<Matrix::RowEntry> row;
139 for (std::size_t j = 0; j < constraints.cols(); ++j) {
140 if (!carl::is_zero(constraints(i,var_idx.
var(j)))) {
141 row.emplace_back(j, constraints(i,var_idx.
var(j)));
144 if (!carl::is_zero(constants(i))) row.emplace_back(constraints.cols(), -constants(i));
145 row.emplace_back(constraints.cols() + 1 + i,
Rational(1));
151 EigenMat res_mat = EigenMat::Zero(res.
n_rows(), constraints.cols());
154 for (std::size_t i = 0; i < res.
n_rows(); ++i) {
156 if (e.col_index == constraints.cols()) {
157 res_const(i) = -e.value;
160 if (e.col_index > constraints.cols())
break;
161 res_mat(i, var_idx.
var(e.col_index)) = e.value;
165 return {res_mat, res_const};
171 const std::vector<std::size_t>& cols,
172 const std::string& filename) {
175 file <<
"H-representation\n";
177 file << constraints.rows() <<
" " << constraints.cols() + 1 <<
" rational\n";
178 for (std::size_t i = 0; i < constraints.rows(); ++i) {
180 for (std::size_t j = 0; j < constraints.cols(); ++j) {
183 file <<
" " << constants(i)*
lcm;
184 for (std::size_t j = 0; j < constraints.cols(); ++j) {
185 file <<
" " << constraints(i,j)*(-
lcm);
190 file <<
"project " << cols.size();
191 for (
const auto i : cols) file <<
" " << i+1;
row_view row_entries(const RowIndex ri) const
row_iterator row_end(RowIndex r) const
void reserve(std::size_t n)
row_iterator row_begin(RowIndex r) const
RowIndex append_row(const It &begin, const It &end)
Appends the row contained in the range between begin and end to the matrix.
std::size_t n_rows() const
void sort(T *array, int size, LessThan lt)
static bool find(V &ts, const T &t)
Eigen::Matrix< Rational, Eigen::Dynamic, Eigen::Dynamic > EigenMat
void write_matrix_to_ine(const EigenMat &constraints, const EigenVec &constants, const std::vector< std::size_t > &cols, const std::string &filename)
std::pair< EigenMat, EigenVec > eliminate_cols(const EigenMat &constraints, const EigenVec &constants, const std::vector< std::size_t > &cols)
carl::MultivariatePolynomial< Rational > Poly
Eigen::Matrix< Rational, Eigen::Dynamic, 1 > EigenVec
Formula eliminate_variables(const Formula &f, const std::vector< carl::Variable > &vars)
carl::Formula< carl::MultivariatePolynomial< Rational > > Formula
Numeric lcm(const Numeric &_valueA, const Numeric &_valueB)
Calculates the least common multiple of the two arguments.
std::vector< carl::Variable > vars(const std::pair< QuantifierType, std::vector< carl::Variable >> &p)
std::size_t add_variable(const Var v)
std::size_t index(Var v) const
Var var(std::size_t i) const
VariableIndex(const std::vector< Var > &vs)
std::vector< Var > m_vars