2 * src/lemon/lin_expr.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
17 #ifndef LEMON_LIN_EXPR_H
18 #define LEMON_LIN_EXPR_H
26 ///\brief Classes to handle linear expressions
29 /// Class to handle sparse linear expressions
31 class SparseLinExpr : public std::map<_V, typename _V::ExprValue>
35 typedef typename _V::ExprValue Coeff;
38 typedef typename std::map<_V, typename _V::ExprValue> Base;
43 SparseLinExpr() : Base(), const_comp(0) { }
45 SparseLinExpr(const Var &v) : const_comp(0) {
46 Base::insert(std::make_pair(v, 1));
49 SparseLinExpr(const Coeff &v) : const_comp(v) {}
52 void set(const Var &v,const Coeff &c) {
53 return Base::insert(std::make_pair(v, c));
55 // Coeff &operator[](const Var &v) { return data[v]; }
56 // const Coeff &operator[](const Var &v) const { return data[v]; }
59 Coeff &constComp() { return const_comp; }
61 const Coeff &constComp() const { return const_comp; }
63 ///Removes the components with zero coefficient.
65 for (typename Base::iterator i=Base::begin(); i!=Base::end();) {
66 typename Base::iterator j=i;
68 if ((*i).second==0) Base::erase(i);
74 SparseLinExpr &operator+=(const SparseLinExpr &e) {
75 for (typename Base::const_iterator j=e.begin(); j!=e.end(); ++j)
76 (*this)[j->first]+=j->second;
77 ///\todo it might be speeded up using "hints"
78 const_comp+=e.const_comp;
82 SparseLinExpr &operator-=(const SparseLinExpr &e) {
83 for (typename Base::const_iterator j=e.begin(); j!=e.end(); ++j)
84 (*this)[j->first]-=j->second;
85 const_comp-=e.const_comp;
89 SparseLinExpr &operator*=(const Coeff &c) {
90 for (typename Base::iterator j=Base::begin(); j!=Base::end(); ++j)
96 SparseLinExpr &operator/=(const Coeff &c) {
97 for (typename Base::iterator j=Base::begin(); j!=Base::end(); ++j)
107 ///\relates SparseLinExpr
110 SparseLinExpr<V> operator+(const SparseLinExpr<V> &a,
111 const SparseLinExpr<V> &b) {
112 SparseLinExpr<V> tmp(a);
113 tmp+=b; ///\todo Don't STL have some special 'merge' algorithm?
119 ///\relates SparseLinExpr
122 SparseLinExpr<V> operator-(const SparseLinExpr<V> &a,
123 const SparseLinExpr<V> &b) {
124 SparseLinExpr<V> tmp(a);
125 tmp-=b; ///\todo Don't STL have some special 'merge' algorithm?
131 ///\relates SparseLinExpr
134 SparseLinExpr<V> operator*(const typename V::ExprValue &c,
135 const SparseLinExpr<V> &e) {
136 SparseLinExpr<V> tmp(e);
143 ///\relates SparseLinExpr
146 SparseLinExpr<V> operator*(const SparseLinExpr<V> &e,
147 const typename V::ExprValue &c) {
148 SparseLinExpr<V> tmp(e);
156 ///\relates SparseLinExpr
159 SparseLinExpr<V> operator/(const SparseLinExpr<V> &e,
160 const typename V::ExprValue &c) {
161 SparseLinExpr<V> tmp(e);
168 ///\relates SparseLinExpr
171 SparseLinExpr<V> operator+(const typename V::ExprValue &c,
172 const SparseLinExpr<V> &e) {
173 SparseLinExpr<V> tmp(e);
180 ///\relates SparseLinExpr
183 SparseLinExpr<V> operator+(const SparseLinExpr<V> &e,
184 const typename V::ExprValue &c) {
185 SparseLinExpr<V> tmp(e);
192 ///\relates SparseLinExpr
195 SparseLinExpr<V> operator+(const V &v,const SparseLinExpr<V> &e) {
196 SparseLinExpr<V> tmp(e);
203 ///\relates SparseLinExpr
206 SparseLinExpr<V> operator+(const SparseLinExpr<V> &e,const V &v) {
207 SparseLinExpr<V> tmp(e);
214 ///\relates SparseLinExpr
217 SparseLinExpr<V> operator-(const typename V::ExprValue &c,
218 const SparseLinExpr<V> &e) {
219 SparseLinExpr<V> tmp(e);
227 ///\relates SparseLinExpr
230 SparseLinExpr<V> operator-(const SparseLinExpr<V> &e,
231 const typename V::ExprValue &c) {
232 SparseLinExpr<V> tmp(e);
239 ///\relates SparseLinExpr
242 SparseLinExpr<V> operator-(const V &v,const SparseLinExpr<V> &e) {
243 SparseLinExpr<V> tmp(e);
251 ///\relates SparseLinExpr
254 SparseLinExpr<V> operator-(const SparseLinExpr<V> &e,const V &v) {
255 SparseLinExpr<V> tmp(e);
262 ///\relates SparseLinExpr
265 SparseLinExpr<V> operator+(const V &v1,const V &v2) {
266 SparseLinExpr<V> tmp(v1);
273 ///\relates SparseLinExpr
276 SparseLinExpr<V> operator*(const V &v,const typename V::ExprValue &c) {
277 SparseLinExpr<V> tmp;
284 ///\relates SparseLinExpr
287 SparseLinExpr<V> operator*(const typename V::ExprValue &c,const V &v) {
288 SparseLinExpr<V> tmp;
295 ///\relates SparseLinExpr
298 SparseLinExpr<V> operator/(const V &v,const typename V::ExprValue &c) {
299 SparseLinExpr<V> tmp;
307 #endif //LEMON_LIN_EXPR_H