2 #ifndef LEMON_EXPRESSION_H
 
     3 #define LEMON_EXPRESSION_H
 
    11   /*! \brief Linear expression
 
    13     \c Expr<_Col,_Value> implements a class of linear expressions with the 
 
    14     operations of addition and multiplication with scalar. 
 
    18   template <typename _Col, typename _Value>
 
    21   template <typename _Col, typename _Value>
 
    26     typename std::map<_Col, _Value> Data; 
 
    30       for (typename Data::iterator i=data.begin(); 
 
    32 	if ((*i).second==0) data.erase(i);
 
    37       data.insert(std::make_pair(_col, 1));
 
    39     Expr& operator*=(_Value _value) {
 
    40       for (typename Data::iterator i=data.begin(); 
 
    42 	(*i).second *= _value;
 
    47     Expr& operator+=(const Expr<_Col, _Value>& expr) {
 
    48       for (typename Data::const_iterator j=expr.data.begin(); 
 
    49 	   j!=expr.data.end(); ++j) {
 
    50 	typename Data::iterator i=data.find((*j).first);
 
    52 	  data.insert(std::make_pair((*j).first, (*j).second));
 
    54 	  (*i).second+=(*j).second;
 
    60     Expr& operator-=(const Expr<_Col, _Value>& expr) {
 
    61       for (typename Data::const_iterator j=expr.data.begin(); 
 
    62 	   j!=expr.data.end(); ++j) {
 
    63 	typename Data::iterator i=data.find((*j).first);
 
    65 	  data.insert(std::make_pair((*j).first, -(*j).second));
 
    67 	  (*i).second+=-(*j).second;
 
    73     template <typename _C, typename _V> 
 
    74     friend std::ostream& operator<<(std::ostream& os, 
 
    75 				    const Expr<_C, _V>& expr);
 
    78   template <typename _Col, typename _Value>
 
    79   Expr<_Col, _Value> operator*(_Value _value, _Col _col) {
 
    80     Expr<_Col, _Value> tmp(_col);
 
    86   template <typename _Col, typename _Value>
 
    87   Expr<_Col, _Value> operator*(_Value _value, 
 
    88 			       const Expr<_Col, _Value>& expr) {
 
    89     Expr<_Col, _Value> tmp(expr);
 
    95   template <typename _Col, typename _Value>
 
    96   Expr<_Col, _Value> operator+(const Expr<_Col, _Value>& expr1, 
 
    97 			       const Expr<_Col, _Value>& expr2) {
 
    98     Expr<_Col, _Value> tmp(expr1);
 
   104   template <typename _Col, typename _Value>
 
   105   Expr<_Col, _Value> operator-(const Expr<_Col, _Value>& expr1, 
 
   106 			       const Expr<_Col, _Value>& expr2) {
 
   107     Expr<_Col, _Value> tmp(expr1);
 
   113   template <typename _Col, typename _Value>
 
   114   std::ostream& operator<<(std::ostream& os, 
 
   115 			   const Expr<_Col, _Value>& expr) {
 
   116     for (typename Expr<_Col, _Value>::Data::const_iterator i=
 
   118 	 i!=expr.data.end(); ++i) {
 
   119       os << (*i).second << "*" << (*i).first << " ";
 
   124   template <typename _Col, typename _Value>
 
   128     Expr<_Col, _Value> expr;
 
   131     LConstr(const Expr<_Col, _Value>& _expr, _Value _lo) : 
 
   132       expr(_expr), lo(_lo) { }
 
   135   template <typename _Col, typename _Value>
 
   136   LConstr<_Col, _Value> 
 
   137   operator<=(_Value lo, const Expr<_Col, _Value>& expr) {
 
   138     return LConstr<_Col, _Value>(expr, lo);
 
   141   template <typename _Col, typename _Value>
 
   145     Expr<_Col, _Value> expr;
 
   148     UConstr(const Expr<_Col, _Value>& _expr, _Value _up) : 
 
   149       expr(_expr), up(_up) { }
 
   152   template <typename _Col, typename _Value>
 
   153   UConstr<_Col, _Value> 
 
   154   operator<=(const Expr<_Col, _Value>& expr, _Value up) {
 
   155     return UConstr<_Col, _Value>(expr, up);
 
   158   template <typename _Col, typename _Value>
 
   162     Expr<_Col, _Value> expr;
 
   165     Constr(const Expr<_Col, _Value>& _expr, _Value _lo, _Value _up) : 
 
   166       expr(_expr), lo(_lo), up(_up) { }
 
   167     Constr(const LConstr<_Col, _Value>& _lconstr) : 
 
   170       up(std::numeric_limits<_Value>::infinity()) { }
 
   171     Constr(const UConstr<_Col, _Value>& _uconstr) : 
 
   173       lo(-std::numeric_limits<_Value>::infinity()), 
 
   177   template <typename _Col, typename _Value>
 
   179   operator<=(const LConstr<_Col, _Value>& lconstr, _Value up) {
 
   180     return Constr<_Col, _Value>(lconstr.expr, lconstr.lo, up);
 
   183   template <typename _Col, typename _Value>
 
   185   operator<=(_Value lo, const UConstr<_Col, _Value>& uconstr) {
 
   186     return Constr<_Col, _Value>(uconstr.expr, lo, uconstr.up);
 
   189   template <typename _Col, typename _Value>
 
   191   operator==(const Expr<_Col, _Value>& expr, _Value value) {
 
   192     return Constr<_Col, _Value>(expr, value, value);
 
   197 #endif //LEMON_EXPRESSION_H