Remove misplaced methods
[linpy.git] / pypol / linear.py
1 import ctypes, ctypes.util
2 import functools
3 import numbers
4
5 from fractions import Fraction, gcd
6
7 from . import isl
8 from .isl import libisl
9
10
11 __all__ = [
12 'Expression',
13 'constant', 'symbol', 'symbols',
14 'eq', 'le', 'lt', 'ge', 'gt',
15 'Polyhedron',
16 'empty', 'universe'
17 ]
18
19
20 def _polymorphic_method(func):
21 @functools.wraps(func)
22 def wrapper(a, b):
23 if isinstance(b, Expression):
24 return func(a, b)
25 if isinstance(b, numbers.Rational):
26 b = constant(b)
27 return func(a, b)
28 return NotImplemented
29 return wrapper
30
31 def _polymorphic_operator(func):
32 # A polymorphic operator should call a polymorphic method, hence we just
33 # have to test the left operand.
34 @functools.wraps(func)
35 def wrapper(a, b):
36 if isinstance(a, numbers.Rational):
37 a = constant(a)
38 return func(a, b)
39 elif isinstance(a, Expression):
40 return func(a, b)
41 raise TypeError('arguments must be linear expressions')
42 return wrapper
43
44
45 _main_ctx = isl.Context()
46
47
48 class Expression:
49 """
50 This class implements linear expressions.
51 """
52
53 def __new__(cls, coefficients=None, constant=0):
54 if isinstance(coefficients, str):
55 if constant:
56 raise TypeError('too many arguments')
57 return cls.fromstring(coefficients)
58 self = super().__new__(cls)
59 self._coefficients = {}
60 if isinstance(coefficients, dict):
61 coefficients = coefficients.items()
62 if coefficients is not None:
63 for symbol, coefficient in coefficients:
64 if isinstance(symbol, Expression) and symbol.issymbol():
65 symbol = str(symbol)
66 elif not isinstance(symbol, str):
67 raise TypeError('symbols must be strings')
68 if not isinstance(coefficient, numbers.Rational):
69 raise TypeError('coefficients must be rational numbers')
70 if coefficient != 0:
71 self._coefficients[symbol] = coefficient
72 if not isinstance(constant, numbers.Rational):
73 raise TypeError('constant must be a rational number')
74 self._constant = constant
75 self._symbols = tuple(sorted(self._coefficients))
76 self._dimension = len(self._symbols)
77 return self
78
79 @property
80 def symbols(self):
81 return self._symbols
82
83 @property
84 def dimension(self):
85 return self._dimension
86
87 def coefficient(self, symbol):
88 if isinstance(symbol, Expression) and symbol.issymbol():
89 symbol = str(symbol)
90 elif not isinstance(symbol, str):
91 raise TypeError('symbol must be a string')
92 try:
93 return self._coefficients[symbol]
94 except KeyError:
95 return 0
96
97 __getitem__ = coefficient
98
99 def coefficients(self):
100 for symbol in self.symbols:
101 yield symbol, self.coefficient(symbol)
102
103 @property
104 def constant(self):
105 return self._constant
106
107 def isconstant(self):
108 return len(self._coefficients) == 0
109
110 def values(self):
111 for symbol in self.symbols:
112 yield self.coefficient(symbol)
113 yield self.constant
114
115 def values_int(self):
116 for symbol in self.symbols:
117 return self.coefficient(symbol)
118 return int(self.constant)
119
120 @property
121 def symbol(self):
122 if not self.issymbol():
123 raise ValueError('not a symbol: {}'.format(self))
124 for symbol in self.symbols:
125 return symbol
126
127 def issymbol(self):
128 return len(self._coefficients) == 1 and self._constant == 0
129
130 def __bool__(self):
131 return (not self.isconstant()) or bool(self.constant)
132
133 def __pos__(self):
134 return self
135
136 def __neg__(self):
137 return self * -1
138
139 @_polymorphic_method
140 def __add__(self, other):
141 coefficients = dict(self.coefficients())
142 for symbol, coefficient in other.coefficients:
143 if symbol in coefficients:
144 coefficients[symbol] += coefficient
145 else:
146 coefficients[symbol] = coefficient
147 constant = self.constant + other.constant
148 return Expression(coefficients, constant)
149
150 __radd__ = __add__
151
152 @_polymorphic_method
153 def __sub__(self, other):
154 coefficients = dict(self.coefficients())
155 for symbol, coefficient in other.coefficients:
156 if symbol in coefficients:
157 coefficients[symbol] -= coefficient
158 else:
159 coefficients[symbol] = -coefficient
160 constant = self.constant - other.constant
161 return Expression(coefficients, constant)
162
163 def __rsub__(self, other):
164 return -(self - other)
165
166 @_polymorphic_method
167 def __mul__(self, other):
168 if other.isconstant():
169 coefficients = dict(self.coefficients())
170 for symbol in coefficients:
171 coefficients[symbol] *= other.constant
172 constant = self.constant * other.constant
173 return Expression(coefficients, constant)
174 if isinstance(other, Expression) and not self.isconstant():
175 raise ValueError('non-linear expression: '
176 '{} * {}'.format(self._parenstr(), other._parenstr()))
177 return NotImplemented
178
179 __rmul__ = __mul__
180
181 @_polymorphic_method
182 def __truediv__(self, other):
183 if other.isconstant():
184 coefficients = dict(self.coefficients())
185 for symbol in coefficients:
186 coefficients[symbol] = \
187 Fraction(coefficients[symbol], other.constant)
188 constant = Fraction(self.constant, other.constant)
189 return Expression(coefficients, constant)
190 if isinstance(other, Expression):
191 raise ValueError('non-linear expression: '
192 '{} / {}'.format(self._parenstr(), other._parenstr()))
193 return NotImplemented
194
195 def __rtruediv__(self, other):
196 if isinstance(other, self):
197 if self.isconstant():
198 constant = Fraction(other, self.constant)
199 return Expression(constant=constant)
200 else:
201 raise ValueError('non-linear expression: '
202 '{} / {}'.format(other._parenstr(), self._parenstr()))
203 return NotImplemented
204
205 def __str__(self):
206 string = ''
207 i = 0
208 for symbol in symbols:
209 coefficient = self[symbol]
210 if coefficient == 1:
211 if i == 0:
212 string += symbol
213 else:
214 string += ' + {}'.format(symbol)
215 elif coefficient == -1:
216 if i == 0:
217 string += '-{}'.format(symbol)
218 else:
219 string += ' - {}'.format(symbol)
220 else:
221 if i == 0:
222 string += '{}*{}'.format(coefficient, symbol)
223 elif coefficient > 0:
224 string += ' + {}*{}'.format(coefficient, symbol)
225 else:
226 assert coefficient < 0
227 coefficient *= -1
228 string += ' - {}*{}'.format(coefficient, symbol)
229 i += 1
230 constant = self.constant
231 if constant != 0 and i == 0:
232 string += '{}'.format(constant)
233 elif constant > 0:
234 string += ' + {}'.format(constant)
235 elif constant < 0:
236 constant *= -1
237 string += ' - {}'.format(constant)
238 if string == '':
239 string = '0'
240 return string
241
242 def _parenstr(self, always=False):
243 string = str(self)
244 if not always and (self.isconstant() or self.issymbol()):
245 return string
246 else:
247 return '({})'.format(string)
248
249 def __repr__(self):
250 string = '{}({{'.format(self.__class__.__name__)
251 for i, (symbol, coefficient) in enumerate(self.coefficients()):
252 if i != 0:
253 string += ', '
254 string += '{!r}: {!r}'.format(symbol, coefficient)
255 string += '}}, {!r})'.format(self.constant)
256 return string
257
258 @classmethod
259 def fromstring(cls, string):
260 raise NotImplementedError
261
262 @_polymorphic_method
263 def __eq__(self, other):
264 # "normal" equality
265 # see http://docs.sympy.org/dev/tutorial/gotchas.html#equals-signs
266 return isinstance(other, Expression) and \
267 self._coefficients == other._coefficients and \
268 self.constant == other.constant
269
270 def __hash__(self):
271 return hash((self._coefficients, self._constant))
272
273 def _toint(self):
274 lcm = functools.reduce(lambda a, b: a*b // gcd(a, b),
275 [value.denominator for value in self.values()])
276 return self * lcm
277
278 @_polymorphic_method
279 def _eq(self, other):
280 return Polyhedron(equalities=[(self - other)._toint()])
281
282 @_polymorphic_method
283 def __le__(self, other):
284 return Polyhedron(inequalities=[(other - self)._toint()])
285
286 @_polymorphic_method
287 def __lt__(self, other):
288 return Polyhedron(inequalities=[(other - self)._toint() - 1])
289
290 @_polymorphic_method
291 def __ge__(self, other):
292 return Polyhedron(inequalities=[(self - other)._toint()])
293
294 @_polymorphic_method
295 def __gt__(self, other):
296 return Polyhedron(inequalities=[(self - other)._toint() - 1])
297
298
299 def constant(numerator=0, denominator=None):
300 if denominator is None and isinstance(numerator, numbers.Rational):
301 return Expression(constant=numerator)
302 else:
303 return Expression(constant=Fraction(numerator, denominator))
304
305 def symbol(name):
306 if not isinstance(name, str):
307 raise TypeError('name must be a string')
308 return Expression(coefficients={name: 1})
309
310 def symbols(names):
311 if isinstance(names, str):
312 names = names.replace(',', ' ').split()
313 return (symbol(name) for name in names)
314
315
316 @_polymorphic_operator
317 def eq(a, b):
318 return a._eq(b)
319
320 @_polymorphic_operator
321 def le(a, b):
322 return a <= b
323
324 @_polymorphic_operator
325 def lt(a, b):
326 return a < b
327
328 @_polymorphic_operator
329 def ge(a, b):
330 return a >= b
331
332 @_polymorphic_operator
333 def gt(a, b):
334 return a > b
335
336
337 class Polyhedron:
338 """
339 This class implements polyhedrons.
340 """
341
342 def __new__(cls, equalities=None, inequalities=None):
343 if isinstance(equalities, str):
344 if inequalities is not None:
345 raise TypeError('too many arguments')
346 return cls.fromstring(equalities)
347 self = super().__new__(cls)
348 self._equalities = []
349 if equalities is not None:
350 for constraint in equalities:
351 for value in constraint.values():
352 if value.denominator != 1:
353 raise TypeError('non-integer constraint: '
354 '{} == 0'.format(constraint))
355 self._equalities.append(constraint)
356 self._equalities = tuple(self._equalities)
357 self._inequalities = []
358 if inequalities is not None:
359 for constraint in inequalities:
360 for value in constraint.values():
361 if value.denominator != 1:
362 raise TypeError('non-integer constraint: '
363 '{} <= 0'.format(constraint))
364 self._inequalities.append(constraint)
365 self._inequalities = tuple(self._inequalities)
366 self._constraints = self._equalities + self._inequalities
367 self._symbols = set()
368 for constraint in self._constraints:
369 self.symbols.update(constraint.symbols)
370 self._symbols = tuple(sorted(self._symbols))
371 return self
372
373 @property
374 def equalities(self):
375 return self._equalities
376
377 @property
378 def inequalities(self):
379 return self._inequalities
380
381 def isempty(self):
382 return bool(libisl.isl_basic_set_is_empty(self._bset))
383
384 @property
385 def constraints(self):
386 return self._constraints
387
388 @property
389 def symbols(self):
390 return self._symbols
391
392 @property
393 def dimension(self):
394 return len(self.symbols)
395
396 def __bool__(self):
397 return not self.is_empty()
398
399 def __contains__(self, value):
400 # is the value in the polyhedron?
401 raise NotImplementedError
402
403 def __eq__(self, other):
404 raise NotImplementedError
405
406 def isempty(self):
407 return self == empty
408
409 def isuniverse(self):
410 return self == universe
411
412 def isdisjoint(self, other):
413 # return true if the polyhedron has no elements in common with other
414 raise NotImplementedError
415
416 def issubset(self, other):
417 raise NotImplementedError
418
419 def __le__(self, other):
420 return self.issubset(other)
421
422 def __lt__(self, other):
423 raise NotImplementedError
424
425 def issuperset(self, other):
426 # test whether every element in other is in the polyhedron
427 raise NotImplementedError
428
429 def __ge__(self, other):
430 return self.issuperset(other)
431
432 def __gt__(self, other):
433 raise NotImplementedError
434
435 def union(self, *others):
436 # return a new polyhedron with elements from the polyhedron and all
437 # others (convex union)
438 raise NotImplementedError
439
440 def __or__(self, other):
441 return self.union(other)
442
443 def intersection(self, *others):
444 # return a new polyhedron with elements common to the polyhedron and all
445 # others
446 # a poor man's implementation could be:
447 # equalities = list(self.equalities)
448 # inequalities = list(self.inequalities)
449 # for other in others:
450 # equalities.extend(other.equalities)
451 # inequalities.extend(other.inequalities)
452 # return self.__class__(equalities, inequalities)
453 raise NotImplementedError
454
455 def __and__(self, other):
456 return self.intersection(other)
457
458 def difference(self, *others):
459 # return a new polyhedron with elements in the polyhedron that are not
460 # in the others
461 raise NotImplementedError
462
463 def __sub__(self, other):
464 return self.difference(other)
465
466 def __str__(self):
467 constraints = []
468 for constraint in self.equalities:
469 constraints.append('{} == 0'.format(constraint))
470 for constraint in self.inequalities:
471 constraints.append('{} >= 0'.format(constraint))
472 return '{{{}}}'.format(', '.join(constraints))
473
474 def __repr__(self):
475 equalities = list(self.equalities)
476 inequalities = list(self.inequalities)
477 return '{}(equalities={!r}, inequalities={!r})' \
478 ''.format(self.__class__.__name__, equalities, inequalities)
479
480 @classmethod
481 def fromstring(cls, string):
482 raise NotImplementedError
483
484 def _symbolunion(self, *others):
485 symbols = set(self.symbols)
486 for other in others:
487 symbols.update(other.symbols)
488 return sorted(symbols)
489
490 def _to_isl(self, symbols=None):
491 if symbols is None:
492 symbols = self.symbols
493 num_coefficients = len(symbols)
494 space = libisl.isl_space_set_alloc(_main_ctx, 0, num_coefficients)
495 bset = libisl.isl_basic_set_universe(libisl.isl_space_copy(space))
496 ls = libisl.isl_local_space_from_space(space)
497 ceq = libisl.isl_equality_alloc(libisl.isl_local_space_copy(ls))
498 cin = libisl.isl_inequality_alloc(libisl.isl_local_space_copy(ls))
499 '''if there are equalities/inequalities, take each constant and coefficient and add as a constraint to the basic set'''
500 if list(self.equalities): #check if any equalities exist
501 for eq in self.equalities:
502 coeff_eq = dict(eq.coefficients())
503 if eq.constant:
504 value = eq.constant
505 ceq = libisl.isl_constraint_set_constant_si(ceq, value)
506 for eq in coeff_eq:
507 num = coeff_eq.get(eq)
508 iden = symbols.index(eq)
509 ceq = libisl.isl_constraint_set_coefficient_si(ceq, libisl.isl_dim_set, iden, num) #use 3 for type isl_dim_set
510 bset = libisl.isl_basic_set_add_constraint(bset, ceq)
511 if list(self.inequalities): #check if any inequalities exist
512 for ineq in self.inequalities:
513 coeff_in = dict(ineq.coefficients())
514 if ineq.constant:
515 value = ineq.constant
516 cin = libisl.isl_constraint_set_constant_si(cin, value)
517 for ineq in coeff_in:
518 num = coeff_in.get(ineq)
519 iden = symbols.index(ineq)
520 cin = libisl.isl_constraint_set_coefficient_si(cin, libisl.isl_dim_set, iden, num) #use 3 for type isl_dim_set
521 bset = libisl.isl_basic_set_add_constraint(bset, cin)
522 bset = isl.BasicSet(bset)
523 return bset
524
525 @classmethod
526 def from_isl(cls, bset):
527 '''takes basic set in isl form and puts back into python version of polyhedron
528 isl example code gives isl form as:
529 "{[i] : exists (a : i = 2a and i >= 10 and i <= 42)}")
530 our printer is giving form as:
531 b'{ [i0] : 1 = 0 }' '''
532 raise NotImplementedError
533 equalities = ...
534 inequalities = ...
535 return cls(equalities, inequalities)
536 #bset = self
537 # if self._equalities:
538 # constraints = libisl.isl_basic_set_equalities_matrix(bset, 3)
539 # elif self._inequalities:
540 # constraints = libisl.isl_basic_set_inequalities_matrix(bset, 3)
541 # print(constraints)
542 # return constraints
543
544 empty = None #eq(0,1)
545 universe = None #Polyhedron()
546
547
548 if __name__ == '__main__':
549 ex1 = Expression(coefficients={'a': 1, 'x': 2}, constant=2)
550 ex2 = Expression(coefficients={'a': 3 , 'b': 2}, constant=3)
551 p = Polyhedron(inequalities=[ex1, ex2])
552 bs = p._to_isl()
553 print(bs)