alpar@1253
|
1 |
/* -*- C++ -*-
|
alpar@1253
|
2 |
* src/lemon/lin_expr.h - Part of LEMON, a generic C++ optimization library
|
alpar@1253
|
3 |
*
|
alpar@1253
|
4 |
* Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@1253
|
5 |
* (Egervary Combinatorial Optimization Research Group, EGRES).
|
alpar@1253
|
6 |
*
|
alpar@1253
|
7 |
* Permission to use, modify and distribute this software is granted
|
alpar@1253
|
8 |
* provided that this copyright notice appears in all copies. For
|
alpar@1253
|
9 |
* precise terms see the accompanying LICENSE file.
|
alpar@1253
|
10 |
*
|
alpar@1253
|
11 |
* This software is provided "AS IS" with no warranty of any kind,
|
alpar@1253
|
12 |
* express or implied, and with no claim as to its suitability for any
|
alpar@1253
|
13 |
* purpose.
|
alpar@1253
|
14 |
*
|
alpar@1253
|
15 |
*/
|
alpar@1253
|
16 |
|
alpar@1253
|
17 |
#ifndef LEMON_LIN_EXPR_H
|
alpar@1253
|
18 |
#define LEMON_LIN_EXPR_H
|
alpar@1253
|
19 |
|
alpar@1253
|
20 |
#include<vector>
|
alpar@1253
|
21 |
|
alpar@1253
|
22 |
|
alpar@1253
|
23 |
#include<map>
|
alpar@1253
|
24 |
|
alpar@1253
|
25 |
///\file
|
alpar@1253
|
26 |
///\brief Classes to handle linear expressions
|
alpar@1253
|
27 |
namespace lemon {
|
alpar@1253
|
28 |
|
alpar@1253
|
29 |
/// Class to handle sparse linear expressions
|
alpar@1259
|
30 |
template <class _V>
|
alpar@1259
|
31 |
class SparseLinExpr : public std::map<_V, typename _V::ExprValue>
|
alpar@1253
|
32 |
{
|
alpar@1253
|
33 |
public:
|
alpar@1253
|
34 |
typedef _V Var;
|
alpar@1259
|
35 |
typedef typename _V::ExprValue Coeff;
|
alpar@1253
|
36 |
|
alpar@1253
|
37 |
protected:
|
alpar@1259
|
38 |
typedef typename std::map<_V, typename _V::ExprValue> Base;
|
alpar@1253
|
39 |
|
alpar@1253
|
40 |
Coeff const_comp;
|
alpar@1253
|
41 |
public:
|
alpar@1259
|
42 |
///\e
|
alpar@1259
|
43 |
SparseLinExpr() : Base(), const_comp(0) { }
|
alpar@1259
|
44 |
///\e
|
alpar@1259
|
45 |
SparseLinExpr(const Var &v) : const_comp(0) {
|
alpar@1253
|
46 |
Base::insert(std::make_pair(v, 1));
|
alpar@1253
|
47 |
}
|
alpar@1259
|
48 |
///\e
|
alpar@1253
|
49 |
SparseLinExpr(const Coeff &v) : const_comp(v) {}
|
alpar@1253
|
50 |
|
alpar@1259
|
51 |
///\e
|
alpar@1253
|
52 |
void set(const Var &v,const Coeff &c) {
|
alpar@1253
|
53 |
return Base::insert(std::make_pair(v, c));
|
alpar@1253
|
54 |
}
|
alpar@1253
|
55 |
// Coeff &operator[](const Var &v) { return data[v]; }
|
alpar@1253
|
56 |
// const Coeff &operator[](const Var &v) const { return data[v]; }
|
alpar@1253
|
57 |
|
alpar@1259
|
58 |
///\e
|
alpar@1253
|
59 |
Coeff &constComp() { return const_comp; }
|
alpar@1259
|
60 |
///\e
|
alpar@1253
|
61 |
const Coeff &constComp() const { return const_comp; }
|
alpar@1253
|
62 |
|
alpar@1253
|
63 |
///Removes the components with zero coefficient.
|
alpar@1253
|
64 |
void simplify() {
|
alpar@1253
|
65 |
for (typename Base::iterator i=Base::begin(); i!=Base::end();) {
|
alpar@1253
|
66 |
typename Base::iterator j=i;
|
alpar@1253
|
67 |
++j;
|
alpar@1253
|
68 |
if ((*i).second==0) Base::erase(i);
|
alpar@1253
|
69 |
j=i;
|
alpar@1253
|
70 |
}
|
alpar@1253
|
71 |
}
|
alpar@1253
|
72 |
|
alpar@1259
|
73 |
///\e
|
alpar@1259
|
74 |
SparseLinExpr &operator+=(const SparseLinExpr &e) {
|
alpar@1259
|
75 |
for (typename Base::const_iterator j=e.begin(); j!=e.end(); ++j)
|
alpar@1259
|
76 |
(*this)[j->first]+=j->second;
|
alpar@1259
|
77 |
///\todo it might be speeded up using "hints"
|
alpar@1259
|
78 |
const_comp+=e.const_comp;
|
alpar@1259
|
79 |
return *this;
|
alpar@1259
|
80 |
}
|
alpar@1259
|
81 |
///\e
|
alpar@1259
|
82 |
SparseLinExpr &operator-=(const SparseLinExpr &e) {
|
alpar@1259
|
83 |
for (typename Base::const_iterator j=e.begin(); j!=e.end(); ++j)
|
alpar@1259
|
84 |
(*this)[j->first]-=j->second;
|
alpar@1259
|
85 |
const_comp-=e.const_comp;
|
alpar@1259
|
86 |
return *this;
|
alpar@1259
|
87 |
}
|
alpar@1259
|
88 |
///\e
|
alpar@1259
|
89 |
SparseLinExpr &operator*=(const Coeff &c) {
|
alpar@1259
|
90 |
for (typename Base::iterator j=Base::begin(); j!=Base::end(); ++j)
|
alpar@1259
|
91 |
j->second*=c;
|
alpar@1259
|
92 |
const_comp*=c;
|
alpar@1259
|
93 |
return *this;
|
alpar@1259
|
94 |
}
|
alpar@1259
|
95 |
///\e
|
alpar@1259
|
96 |
SparseLinExpr &operator/=(const Coeff &c) {
|
alpar@1259
|
97 |
for (typename Base::iterator j=Base::begin(); j!=Base::end(); ++j)
|
alpar@1259
|
98 |
j->second/=c;
|
alpar@1259
|
99 |
const_comp/=c;
|
alpar@1259
|
100 |
return *this;
|
alpar@1259
|
101 |
}
|
alpar@1259
|
102 |
|
alpar@1253
|
103 |
};
|
alpar@1253
|
104 |
|
alpar@1259
|
105 |
///\e
|
alpar@1259
|
106 |
|
alpar@1259
|
107 |
///\relates SparseLinExpr
|
alpar@1259
|
108 |
///
|
alpar@1259
|
109 |
template <class V>
|
alpar@1259
|
110 |
SparseLinExpr<V> operator+(const SparseLinExpr<V> &a,
|
alpar@1259
|
111 |
const SparseLinExpr<V> &b) {
|
alpar@1259
|
112 |
SparseLinExpr<V> tmp(a);
|
alpar@1259
|
113 |
tmp+=b; ///\todo Don't STL have some special 'merge' algorithm?
|
alpar@1259
|
114 |
return tmp;
|
alpar@1259
|
115 |
}
|
alpar@1259
|
116 |
|
alpar@1259
|
117 |
///\e
|
alpar@1259
|
118 |
|
alpar@1259
|
119 |
///\relates SparseLinExpr
|
alpar@1259
|
120 |
///
|
alpar@1259
|
121 |
template <class V>
|
alpar@1259
|
122 |
SparseLinExpr<V> operator-(const SparseLinExpr<V> &a,
|
alpar@1259
|
123 |
const SparseLinExpr<V> &b) {
|
alpar@1259
|
124 |
SparseLinExpr<V> tmp(a);
|
alpar@1259
|
125 |
tmp-=b; ///\todo Don't STL have some special 'merge' algorithm?
|
alpar@1259
|
126 |
return tmp;
|
alpar@1259
|
127 |
}
|
alpar@1259
|
128 |
|
alpar@1259
|
129 |
///\e
|
alpar@1259
|
130 |
|
alpar@1259
|
131 |
///\relates SparseLinExpr
|
alpar@1259
|
132 |
///
|
alpar@1259
|
133 |
template <class V>
|
alpar@1259
|
134 |
SparseLinExpr<V> operator*(const typename V::ExprValue &c,
|
alpar@1259
|
135 |
const SparseLinExpr<V> &e) {
|
alpar@1259
|
136 |
SparseLinExpr<V> tmp(e);
|
alpar@1259
|
137 |
tmp*=c;
|
alpar@1259
|
138 |
return tmp;
|
alpar@1259
|
139 |
}
|
alpar@1259
|
140 |
|
alpar@1259
|
141 |
///\e
|
alpar@1259
|
142 |
|
alpar@1259
|
143 |
///\relates SparseLinExpr
|
alpar@1259
|
144 |
///
|
alpar@1259
|
145 |
template <class V>
|
alpar@1259
|
146 |
SparseLinExpr<V> operator*(const SparseLinExpr<V> &e,
|
alpar@1259
|
147 |
const typename V::ExprValue &c) {
|
alpar@1259
|
148 |
SparseLinExpr<V> tmp(e);
|
alpar@1259
|
149 |
tmp*=c;
|
alpar@1259
|
150 |
return tmp;
|
alpar@1259
|
151 |
}
|
alpar@1259
|
152 |
|
alpar@1259
|
153 |
|
alpar@1259
|
154 |
///\e
|
alpar@1259
|
155 |
|
alpar@1259
|
156 |
///\relates SparseLinExpr
|
alpar@1259
|
157 |
///
|
alpar@1259
|
158 |
template <class V>
|
alpar@1259
|
159 |
SparseLinExpr<V> operator/(const SparseLinExpr<V> &e,
|
alpar@1259
|
160 |
const typename V::ExprValue &c) {
|
alpar@1259
|
161 |
SparseLinExpr<V> tmp(e);
|
alpar@1259
|
162 |
tmp/=c;
|
alpar@1259
|
163 |
return tmp;
|
alpar@1259
|
164 |
}
|
alpar@1259
|
165 |
|
alpar@1259
|
166 |
///\e
|
alpar@1259
|
167 |
|
alpar@1259
|
168 |
///\relates SparseLinExpr
|
alpar@1259
|
169 |
///
|
alpar@1259
|
170 |
template <class V>
|
alpar@1259
|
171 |
SparseLinExpr<V> operator+(const typename V::ExprValue &c,
|
alpar@1259
|
172 |
const SparseLinExpr<V> &e) {
|
alpar@1259
|
173 |
SparseLinExpr<V> tmp(e);
|
alpar@1259
|
174 |
tmp.constComp()+=c;
|
alpar@1259
|
175 |
return tmp;
|
alpar@1259
|
176 |
}
|
alpar@1259
|
177 |
|
alpar@1259
|
178 |
///\e
|
alpar@1259
|
179 |
|
alpar@1259
|
180 |
///\relates SparseLinExpr
|
alpar@1259
|
181 |
///
|
alpar@1259
|
182 |
template <class V>
|
alpar@1259
|
183 |
SparseLinExpr<V> operator+(const SparseLinExpr<V> &e,
|
alpar@1259
|
184 |
const typename V::ExprValue &c) {
|
alpar@1259
|
185 |
SparseLinExpr<V> tmp(e);
|
alpar@1259
|
186 |
tmp.constComp()+=c;
|
alpar@1259
|
187 |
return tmp;
|
alpar@1259
|
188 |
}
|
alpar@1259
|
189 |
|
alpar@1259
|
190 |
///\e
|
alpar@1259
|
191 |
|
alpar@1259
|
192 |
///\relates SparseLinExpr
|
alpar@1259
|
193 |
///
|
alpar@1259
|
194 |
template <class V>
|
alpar@1259
|
195 |
SparseLinExpr<V> operator+(const V &v,const SparseLinExpr<V> &e) {
|
alpar@1259
|
196 |
SparseLinExpr<V> tmp(e);
|
alpar@1259
|
197 |
tmp[v]+=1;
|
alpar@1259
|
198 |
return tmp;
|
alpar@1259
|
199 |
}
|
alpar@1259
|
200 |
|
alpar@1259
|
201 |
///\e
|
alpar@1259
|
202 |
|
alpar@1259
|
203 |
///\relates SparseLinExpr
|
alpar@1259
|
204 |
///
|
alpar@1259
|
205 |
template <class V>
|
alpar@1259
|
206 |
SparseLinExpr<V> operator+(const SparseLinExpr<V> &e,const V &v) {
|
alpar@1259
|
207 |
SparseLinExpr<V> tmp(e);
|
alpar@1259
|
208 |
tmp[v]+=1;
|
alpar@1259
|
209 |
return tmp;
|
alpar@1259
|
210 |
}
|
alpar@1259
|
211 |
|
alpar@1259
|
212 |
///\e
|
alpar@1259
|
213 |
|
alpar@1259
|
214 |
///\relates SparseLinExpr
|
alpar@1259
|
215 |
///
|
alpar@1259
|
216 |
template <class V>
|
alpar@1259
|
217 |
SparseLinExpr<V> operator-(const typename V::ExprValue &c,
|
alpar@1259
|
218 |
const SparseLinExpr<V> &e) {
|
alpar@1259
|
219 |
SparseLinExpr<V> tmp(e);
|
alpar@1259
|
220 |
tmp*=-1;
|
alpar@1259
|
221 |
tmp.constComp()+=c;
|
alpar@1259
|
222 |
return tmp;
|
alpar@1259
|
223 |
}
|
alpar@1259
|
224 |
|
alpar@1259
|
225 |
///\e
|
alpar@1259
|
226 |
|
alpar@1259
|
227 |
///\relates SparseLinExpr
|
alpar@1259
|
228 |
///
|
alpar@1259
|
229 |
template <class V>
|
alpar@1259
|
230 |
SparseLinExpr<V> operator-(const SparseLinExpr<V> &e,
|
alpar@1259
|
231 |
const typename V::ExprValue &c) {
|
alpar@1259
|
232 |
SparseLinExpr<V> tmp(e);
|
alpar@1259
|
233 |
tmp.constComp()-=c;
|
alpar@1259
|
234 |
return tmp;
|
alpar@1259
|
235 |
}
|
alpar@1259
|
236 |
|
alpar@1259
|
237 |
///\e
|
alpar@1259
|
238 |
|
alpar@1259
|
239 |
///\relates SparseLinExpr
|
alpar@1259
|
240 |
///
|
alpar@1259
|
241 |
template <class V>
|
alpar@1259
|
242 |
SparseLinExpr<V> operator-(const V &v,const SparseLinExpr<V> &e) {
|
alpar@1259
|
243 |
SparseLinExpr<V> tmp(e);
|
alpar@1259
|
244 |
tmp*=-1;
|
alpar@1259
|
245 |
tmp[v]+=1;
|
alpar@1259
|
246 |
return tmp;
|
alpar@1259
|
247 |
}
|
alpar@1259
|
248 |
|
alpar@1259
|
249 |
///\e
|
alpar@1259
|
250 |
|
alpar@1259
|
251 |
///\relates SparseLinExpr
|
alpar@1259
|
252 |
///
|
alpar@1259
|
253 |
template <class V>
|
alpar@1259
|
254 |
SparseLinExpr<V> operator-(const SparseLinExpr<V> &e,const V &v) {
|
alpar@1259
|
255 |
SparseLinExpr<V> tmp(e);
|
alpar@1259
|
256 |
tmp[v]-=1;
|
alpar@1259
|
257 |
return tmp;
|
alpar@1259
|
258 |
}
|
alpar@1259
|
259 |
|
alpar@1259
|
260 |
///\e
|
alpar@1259
|
261 |
|
alpar@1259
|
262 |
///\relates SparseLinExpr
|
alpar@1259
|
263 |
///
|
alpar@1259
|
264 |
template <class V>
|
alpar@1259
|
265 |
SparseLinExpr<V> operator+(const V &v1,const V &v2) {
|
alpar@1259
|
266 |
SparseLinExpr<V> tmp(v1);
|
alpar@1259
|
267 |
tmp[v2]+=1;
|
alpar@1259
|
268 |
return tmp;
|
alpar@1259
|
269 |
}
|
alpar@1259
|
270 |
|
alpar@1259
|
271 |
///\e
|
alpar@1259
|
272 |
|
alpar@1259
|
273 |
///\relates SparseLinExpr
|
alpar@1259
|
274 |
///
|
alpar@1259
|
275 |
template <class V>
|
alpar@1259
|
276 |
SparseLinExpr<V> operator*(const V &v,const typename V::ExprValue &c) {
|
alpar@1259
|
277 |
SparseLinExpr<V> tmp;
|
alpar@1259
|
278 |
tmp[v]=c;
|
alpar@1259
|
279 |
return tmp;
|
alpar@1259
|
280 |
}
|
alpar@1259
|
281 |
|
alpar@1259
|
282 |
///\e
|
alpar@1259
|
283 |
|
alpar@1259
|
284 |
///\relates SparseLinExpr
|
alpar@1259
|
285 |
///
|
alpar@1259
|
286 |
template <class V>
|
alpar@1259
|
287 |
SparseLinExpr<V> operator*(const typename V::ExprValue &c,const V &v) {
|
alpar@1259
|
288 |
SparseLinExpr<V> tmp;
|
alpar@1259
|
289 |
tmp[v]=c;
|
alpar@1259
|
290 |
return tmp;
|
alpar@1259
|
291 |
}
|
alpar@1259
|
292 |
|
alpar@1259
|
293 |
///\e
|
alpar@1259
|
294 |
|
alpar@1259
|
295 |
///\relates SparseLinExpr
|
alpar@1259
|
296 |
///
|
alpar@1259
|
297 |
template <class V>
|
alpar@1259
|
298 |
SparseLinExpr<V> operator/(const V &v,const typename V::ExprValue &c) {
|
alpar@1259
|
299 |
SparseLinExpr<V> tmp;
|
alpar@1259
|
300 |
tmp[v]=1/c;
|
alpar@1259
|
301 |
return tmp;
|
alpar@1259
|
302 |
}
|
alpar@1259
|
303 |
|
alpar@1259
|
304 |
|
alpar@1253
|
305 |
} //namespace lemon
|
alpar@1253
|
306 |
|
alpar@1253
|
307 |
#endif //LEMON_LIN_EXPR_H
|