16 #ifndef PAAL_EXPRESSIONS_HPP
17 #define PAAL_EXPRESSIONS_HPP
25 #include <boost/range/adaptor/transformed.hpp>
26 #include <boost/range/algorithm/count_if.hpp>
29 #include <unordered_map>
35 struct linear_expression_traits {
46 typedef std::unordered_map<col_id, double> Elements;
47 typedef Elements::const_iterator ExprIter;
55 m_coefs.emplace(col, coef);
72 for (
auto &elem : m_coefs) {
83 auto elem = m_coefs.find(col);
84 if (elem != m_coefs.end()) {
96 return boost::count_if(m_coefs, [](std::pair<col_id, double> x) {
97 return !linear_expression_traits::CMP.e(x.second, 0);
102 template <
typename Operation>
104 for (
auto new_elem : expr.m_coefs) {
105 auto elem = m_coefs.find(new_elem.first);
106 if (elem == m_coefs.end()) {
107 new_elem.second = op(0., new_elem.second);
108 m_coefs.insert(new_elem);
110 elem->second = op(elem->second, new_elem.second);
119 inline std::string col_id_to_string(col_id col) {
120 return " x_" + std::to_string(col.get());
123 template <
typename Stream,
typename Pr
intCol>
124 void print_expression(Stream &o,
const linear_expression &expr,
125 PrintCol print_col) {
127 boost::adaptors::transformed(
128 [&](std::pair<col_id, double> col_and_val) {
130 print_col(col_and_val.first);
137 template <
typename Stream>
139 detail::print_expression(o, expr, detail::col_id_to_string);
146 expr_left += expr_right;
153 expr_left -= expr_right;
182 #endif // PAAL_EXPRESSIONS_HPP
linear_expression operator-(linear_expression expr_left, const linear_expression &expr_right)
linear_expression - linear_expression operator.
linear_expression()
Constructor.
linear_expression & operator+=(const linear_expression &expr)
Addition operator.
const Elements & get_elements() const
Returns the iterator range of the elements in the expression.
linear_expression operator*(linear_expression expr, double val)
linear_expression * double operator.
std::string pretty_to_string(double x, double epsilon=1e-9)
pretty_to_string prints double which is close to int as int
void print_collection(Stream &o, Range &&r, const std::string &del)
prints collection with delimiters without trailing delimiter
linear_expression operator+(linear_expression expr_left, const linear_expression &expr_right)
linear_expression + linear_expression operator.
linear_expression & operator-=(const linear_expression &expr)
Subtraction operator.
linear_expression operator/(linear_expression expr, double val)
linear_expression / double operator.
linear_expression(col_id col, double coef=1.)
Constructor.
linear_expression & operator*=(double val)
Multiplication by a constant operator.
This file contains set of simple useful functors or functor adapters.
double get_coefficient(col_id col) const
Returns the coefficient for a given column.
int non_zeros() const
Returns the size (number of nonzero coefficients) of the expression.
Stream & operator<<(Stream &o, const single_bounded_expression< LowerBoundTag, BoundDirection > &expr)
operator<< for Lower Bound
linear_expression & operator/=(double val)
Division by a constant operator.