2 #ifndef LEMON_EXPRESSION_H
3 #define LEMON_EXPRESSION_H
10 /*! \brief Linear expression
12 \c Expr<_Col,_Value> implements a class of linear expressions with the
13 operations of addition and multiplication with scalar.
17 template <typename _Col, typename _Value>
20 template <typename _Col, typename _Value>
25 typename std::map<_Col, _Value> Data;
29 for (typename Data::iterator i=data.begin();
31 if ((*i).second==0) data.erase(i);
36 data.insert(std::make_pair(_col, 1));
38 Expr& operator*=(_Value _value) {
39 for (typename Data::iterator i=data.begin();
41 (*i).second *= _value;
46 Expr& operator+=(const Expr<_Col, _Value>& expr) {
47 for (typename Data::const_iterator j=expr.data.begin();
48 j!=expr.data.end(); ++j) {
49 typename Data::iterator i=data.find((*j).first);
51 data.insert(std::make_pair((*j).first, (*j).second));
53 (*i).second+=(*j).second;
59 Expr& operator-=(const Expr<_Col, _Value>& expr) {
60 for (typename Data::const_iterator j=expr.data.begin();
61 j!=expr.data.end(); ++j) {
62 typename Data::iterator i=data.find((*j).first);
64 data.insert(std::make_pair((*j).first, -(*j).second));
66 (*i).second+=-(*j).second;
72 template <typename _C, typename _V>
73 friend std::ostream& operator<<(std::ostream& os,
74 const Expr<_C, _V>& expr);
77 template <typename _Col, typename _Value>
78 Expr<_Col, _Value> operator*(_Value _value, _Col _col) {
79 Expr<_Col, _Value> tmp(_col);
85 template <typename _Col, typename _Value>
86 Expr<_Col, _Value> operator*(_Value _value,
87 const Expr<_Col, _Value>& expr) {
88 Expr<_Col, _Value> tmp(expr);
94 template <typename _Col, typename _Value>
95 Expr<_Col, _Value> operator+(const Expr<_Col, _Value>& expr1,
96 const Expr<_Col, _Value>& expr2) {
97 Expr<_Col, _Value> tmp(expr1);
103 template <typename _Col, typename _Value>
104 Expr<_Col, _Value> operator-(const Expr<_Col, _Value>& expr1,
105 const Expr<_Col, _Value>& expr2) {
106 Expr<_Col, _Value> tmp(expr1);
112 template <typename _Col, typename _Value>
113 std::ostream& operator<<(std::ostream& os,
114 const Expr<_Col, _Value>& expr) {
115 for (typename Expr<_Col, _Value>::Data::const_iterator i=
117 i!=expr.data.end(); ++i) {
118 os << (*i).second << "*" << (*i).first << " ";
125 #endif //LEMON_EXPRESSION_H