6#include "PrefixPrinter.h"
14#include "dlinear/libs/libgmp.h"
15#include "dlinear/symbolic/literal.h"
23 old_precision_{os.precision()} {}
28std::ostream &PrefixPrinter::operator()(
const Expression &e)
const {
return Print(e); }
31std::ostream &PrefixPrinter::operator()(
const Formula &f)
const {
return Print(f); }
33std::ostream &PrefixPrinter::VisitVariable(
const Expression &e)
const {
return os_ << get_variable(e); }
35std::ostream &PrefixPrinter::VisitConstant(
const Expression &e)
const {
36 const mpq_class &constant{get_constant_value(e)};
37 bool print_den = constant.get_den() != 1;
41 os_ << constant.get_num();
44 os_ << constant.get_den();
50std::ostream &PrefixPrinter::VisitUnaryFunction(
const std::string &name,
const Expression &e)
const {
51 os_ <<
"(" << name <<
" ";
52 Print(get_argument(e));
56std::ostream &PrefixPrinter::VisitBinaryFunction(
const std::string &name,
const Expression &e)
const {
57 os_ <<
"(" << name <<
" ";
58 Print(get_first_argument(e));
60 Print(get_second_argument(e));
64std::ostream &PrefixPrinter::VisitAddition(
const Expression &e)
const {
65 const mpq_class &constant{get_constant_in_addition(e)};
67 if (constant != 0.0) {
69 VisitConstant(constant);
71 for (
const auto &p : get_expr_to_coeff_map_in_addition(e)) {
72 const Expression &e_i{p.first};
73 const mpq_class &c_i{p.second};
88std::ostream &PrefixPrinter::VisitMultiplication(
const Expression &e)
const {
89 const mpq_class &constant{get_constant_in_multiplication(e)};
91 if (constant != 1.0) {
93 VisitConstant(constant);
95 for (
const auto &p : get_base_to_exponent_map_in_multiplication(e)) {
96 const Expression &b_i{p.first};
97 const Expression &e_i{p.second};
112std::ostream &PrefixPrinter::VisitDivision(
const Expression &e)
const {
return VisitBinaryFunction(
"/", e); }
114std::ostream &PrefixPrinter::VisitLog(
const Expression &e)
const {
return VisitUnaryFunction(
"log", e); }
116std::ostream &PrefixPrinter::VisitAbs(
const Expression &e)
const {
return VisitUnaryFunction(
"abs", e); }
118std::ostream &PrefixPrinter::VisitExp(
const Expression &e)
const {
return VisitUnaryFunction(
"exp", e); }
120std::ostream &PrefixPrinter::VisitSqrt(
const Expression &e)
const {
return VisitUnaryFunction(
"sqrt", e); }
122std::ostream &PrefixPrinter::VisitPow(
const Expression &e)
const {
return VisitBinaryFunction(
"^", e); }
124std::ostream &PrefixPrinter::VisitSin(
const Expression &e)
const {
return VisitUnaryFunction(
"sin", e); }
126std::ostream &PrefixPrinter::VisitCos(
const Expression &e)
const {
return VisitUnaryFunction(
"cos", e); }
128std::ostream &PrefixPrinter::VisitTan(
const Expression &e)
const {
return VisitUnaryFunction(
"tan", e); }
130std::ostream &PrefixPrinter::VisitAsin(
const Expression &e)
const {
return VisitUnaryFunction(
"asin", e); }
132std::ostream &PrefixPrinter::VisitAcos(
const Expression &e)
const {
return VisitUnaryFunction(
"acos", e); }
134std::ostream &PrefixPrinter::VisitAtan(
const Expression &e)
const {
return VisitUnaryFunction(
"atan", e); }
136std::ostream &PrefixPrinter::VisitAtan2(
const Expression &e)
const {
return VisitBinaryFunction(
"atan2", e); }
138std::ostream &PrefixPrinter::VisitSinh(
const Expression &e)
const {
return VisitUnaryFunction(
"sinh", e); }
140std::ostream &PrefixPrinter::VisitCosh(
const Expression &e)
const {
return VisitUnaryFunction(
"cosh", e); }
142std::ostream &PrefixPrinter::VisitTanh(
const Expression &e)
const {
return VisitUnaryFunction(
"tanh", e); }
144std::ostream &PrefixPrinter::VisitMin(
const Expression &e)
const {
return VisitBinaryFunction(
"min", e); }
146std::ostream &PrefixPrinter::VisitMax(
const Expression &e)
const {
return VisitBinaryFunction(
"max", e); }
148std::ostream &PrefixPrinter::VisitIfThenElse(
const Expression &e)
const {
150 Print(get_conditional_formula(e));
152 Print(get_then_expression(e));
154 Print(get_else_expression(e));
158std::ostream &PrefixPrinter::VisitUninterpretedFunction(
const Expression &)
const {
159 throw std::runtime_error(
"Not implemented.");
162std::ostream &PrefixPrinter::VisitFalse(
const Formula &)
const {
return os_ <<
"false"; }
164std::ostream &PrefixPrinter::VisitTrue(
const Formula &)
const {
return os_ <<
"true"; }
166std::ostream &PrefixPrinter::VisitVariable(
const Formula &f)
const {
return os_ << get_variable(f); }
168std::ostream &PrefixPrinter::VisitEqualTo(
const Formula &f)
const {
170 Print(get_lhs_expression(f));
172 Print(get_rhs_expression(f));
176std::ostream &PrefixPrinter::VisitNotEqualTo(
const Formula &f)
const {
178 Print(get_lhs_expression(f));
180 Print(get_rhs_expression(f));
184std::ostream &PrefixPrinter::VisitGreaterThan(
const Formula &f)
const {
186 Print(get_lhs_expression(f));
188 Print(get_rhs_expression(f));
192std::ostream &PrefixPrinter::VisitGreaterThanOrEqualTo(
const Formula &f)
const {
194 Print(get_lhs_expression(f));
196 Print(get_rhs_expression(f));
200std::ostream &PrefixPrinter::VisitLessThan(
const Formula &f)
const {
202 Print(get_lhs_expression(f));
204 Print(get_rhs_expression(f));
208std::ostream &PrefixPrinter::VisitLessThanOrEqualTo(
const Formula &f)
const {
210 Print(get_lhs_expression(f));
212 Print(get_rhs_expression(f));
216std::ostream &PrefixPrinter::VisitConjunction(
const Formula &f)
const {
218 for (
const auto &f_i : get_operands(f)) {
225std::ostream &PrefixPrinter::VisitDisjunction(
const Formula &f)
const {
227 for (
const auto &f_i : get_operands(f)) {
234std::ostream &PrefixPrinter::VisitNegation(
const Formula &f)
const {
236 Print(get_operand(f));
240std::ostream &PrefixPrinter::VisitForall(
const Formula &)
const {
throw std::runtime_error(
"Not implemented."); }
243 std::ostringstream oss;
252 std::ostringstream oss;
Simple dataclass used to store the configuration of the program.
OptionValue< bool > & m_with_timings()
Get read-only access to the with_timings parameter of the configuration.
Generic expression visitor implementing the visitor pattern.
Print expressions and formulas in prefix-form.
PrefixPrinter(std::ostream &os, const Config &config=Config{})
Constructs a new PrefixPrinter object with os.
std::ostream & os_
Stream to print to.
const std::streamsize old_precision_
Original precision of the stream.
std::ostream & Print(const Expression &e) const
Print the prefix form of the expression e to the ostream.
~PrefixPrinter() override
Restore the original precision of the ostream.
Represents a symbolic form of an expression.
Global namespace for the dlinear library.
std::string ToPrefix(const Expression &e)
Produce the prefix-string representation of the expression e.