5 from . import islhelper
7 from .islhelper
import mainctx
, libisl
, isl_set_basic_sets
8 from .linexprs
import Expression
, Symbol
17 @functools.total_ordering
26 def __new__(cls
, *polyhedra
):
27 from .polyhedra
import Polyhedron
28 if len(polyhedra
) == 1:
29 polyhedron
= polyhedra
[0]
30 if isinstance(polyhedron
, str):
31 return cls
.fromstring(polyhedron
)
32 elif isinstance(polyhedron
, Polyhedron
):
35 raise TypeError('argument must be a string '
36 'or a Polyhedron instance')
38 for polyhedron
in polyhedra
:
39 if not isinstance(polyhedron
, Polyhedron
):
40 raise TypeError('arguments must be Polyhedron instances')
41 symbols
= cls
._xsymbols
(polyhedra
)
42 islset
= cls
._toislset
(polyhedra
, symbols
)
43 return cls
._fromislset
(islset
, symbols
)
46 def _xsymbols(cls
, iterator
):
48 Return the ordered tuple of symbols present in iterator.
52 symbols
.update(item
.symbols
)
53 return tuple(sorted(symbols
, key
=Symbol
.sortkey
))
57 return self
._polyhedra
65 return self
._dimension
68 islset
= self
._toislset
(self
.polyhedra
, self
.symbols
)
69 islset
= libisl
.isl_set_make_disjoint(mainctx
, islset
)
70 return self
._fromislset
(islset
, self
.symbols
)
73 islset
= self
._toislset
(self
.polyhedra
, self
.symbols
)
74 empty
= bool(libisl
.isl_set_is_empty(islset
))
75 libisl
.isl_set_free(islset
)
79 return not self
.isempty()
82 islset
= self
._toislset
(self
.polyhedra
, self
.symbols
)
83 universe
= bool(libisl
.isl_set_plain_is_universe(islset
))
84 libisl
.isl_set_free(islset
)
88 islset
= self
._toislset
(self
.polyhedra
, self
.symbols
)
89 bounded
= bool(libisl
.isl_set_is_bounded(islset
))
90 libisl
.isl_set_free(islset
)
93 def __eq__(self
, other
):
94 symbols
= self
._xsymbols
([self
, other
])
95 islset1
= self
._toislset
(self
.polyhedra
, symbols
)
96 islset2
= other
._toislset
(other
.polyhedra
, symbols
)
97 equal
= bool(libisl
.isl_set_is_equal(islset1
, islset2
))
98 libisl
.isl_set_free(islset1
)
99 libisl
.isl_set_free(islset2
)
102 def isdisjoint(self
, other
):
103 symbols
= self
._xsymbols
([self
, other
])
104 islset1
= self
._toislset
(self
.polyhedra
, symbols
)
105 islset2
= self
._toislset
(other
.polyhedra
, symbols
)
106 equal
= bool(libisl
.isl_set_is_disjoint(islset1
, islset2
))
107 libisl
.isl_set_free(islset1
)
108 libisl
.isl_set_free(islset2
)
111 def issubset(self
, other
):
112 symbols
= self
._xsymbols
([self
, other
])
113 islset1
= self
._toislset
(self
.polyhedra
, symbols
)
114 islset2
= self
._toislset
(other
.polyhedra
, symbols
)
115 equal
= bool(libisl
.isl_set_is_subset(islset1
, islset2
))
116 libisl
.isl_set_free(islset1
)
117 libisl
.isl_set_free(islset2
)
120 def __le__(self
, other
):
121 return self
.issubset(other
)
123 def __lt__(self
, other
):
124 symbols
= self
._xsymbols
([self
, other
])
125 islset1
= self
._toislset
(self
.polyhedra
, symbols
)
126 islset2
= self
._toislset
(other
.polyhedra
, symbols
)
127 equal
= bool(libisl
.isl_set_is_strict_subset(islset1
, islset2
))
128 libisl
.isl_set_free(islset1
)
129 libisl
.isl_set_free(islset2
)
132 def complement(self
):
133 islset
= self
._toislset
(self
.polyhedra
, self
.symbols
)
134 islset
= libisl
.isl_set_complement(islset
)
135 return self
._fromislset
(islset
, self
.symbols
)
137 def __invert__(self
):
138 return self
.complement()
141 #does not change anything in any of the examples
142 #isl seems to do this naturally
143 islset
= self
._toislset
(self
.polyhedra
, self
.symbols
)
144 islset
= libisl
.isl_set_remove_redundancies(islset
)
145 return self
._fromislset
(islset
, self
.symbols
)
147 def polyhedral_hull(self
):
148 # several types of hull are available
149 # polyhedral seems to be the more appropriate, to be checked
150 from .polyhedra
import Polyhedron
151 islset
= self
._toislset
(self
.polyhedra
, self
.symbols
)
152 islbset
= libisl
.isl_set_polyhedral_hull(islset
)
153 return Polyhedron
._fromislbasicset
(islbset
, self
.symbols
)
155 def project_out(self
, dims
):
156 # use to remove certain variables
157 islset
= self
._toislset
(self
.polyhedra
, self
.symbols
)
159 for index
, symbol
in reversed(list(enumerate(self
.symbols
))):
163 islset
= libisl
.isl_set_project_out(islset
, libisl
.isl_dim_set
, index
+ 1, n
)
166 islset
= libisl
.isl_set_project_out(islset
, libisl
.isl_dim_set
, 0, n
)
167 dims
= [symbol
for symbol
in self
.symbols
if symbol
not in dims
]
168 return Domain
._fromislset
(islset
, dims
)
171 from .polyhedra
import Polyhedron
172 islset
= self
._toislset
(self
.polyhedra
, self
.symbols
)
173 islbset
= libisl
.isl_set_sample(islset
)
174 return Polyhedron
._fromislbasicset
(islbset
, self
.symbols
)
176 def intersection(self
, *others
):
179 symbols
= self
._xsymbols
((self
,) + others
)
180 islset1
= self
._toislset
(self
.polyhedra
, symbols
)
182 islset2
= other
._toislset
(other
.polyhedra
, symbols
)
183 islset1
= libisl
.isl_set_intersect(islset1
, islset2
)
184 return self
._fromislset
(islset1
, symbols
)
186 def __and__(self
, other
):
187 return self
.intersection(other
)
189 def union(self
, *others
):
192 symbols
= self
._xsymbols
((self
,) + others
)
193 islset1
= self
._toislset
(self
.polyhedra
, symbols
)
195 islset2
= other
._toislset
(other
.polyhedra
, symbols
)
196 islset1
= libisl
.isl_set_union(islset1
, islset2
)
197 return self
._fromislset
(islset1
, symbols
)
199 def __or__(self
, other
):
200 return self
.union(other
)
202 def __add__(self
, other
):
203 return self
.union(other
)
205 def difference(self
, other
):
206 symbols
= self
._xsymbols
([self
, other
])
207 islset1
= self
._toislset
(self
.polyhedra
, symbols
)
208 islset2
= other
._toislset
(other
.polyhedra
, symbols
)
209 islset
= libisl
.isl_set_subtract(islset1
, islset2
)
210 return self
._fromislset
(islset
, symbols
)
212 def __sub__(self
, other
):
213 return self
.difference(other
)
216 islset
= self
._toislset
(self
.polyhedra
, self
.symbols
)
217 islset
= libisl
.isl_set_lexmin(islset
)
218 return self
._fromislset
(islset
, self
.symbols
)
221 islset
= self
._toislset
(self
.polyhedra
, self
.symbols
)
222 islset
= libisl
.isl_set_lexmax(islset
)
223 return self
._fromislset
(islset
, self
.symbols
)
225 def num_parameters(self
):
226 #could be useful with large, complicated polyhedrons
227 islbset
= self
._toislbasicset
(self
.equalities
, self
.inequalities
, self
.symbols
)
228 num
= libisl
.isl_basic_set_dim(islbset
, libisl
.isl_dim_set
)
231 def involves_dims(self
, dims
):
232 #could be useful with large, complicated polyhedrons
233 islset
= self
._toislset
(self
.polyhedra
, self
.symbols
)
235 symbols
= sorted(list(self
.symbols
))
240 first
= symbols
.index(dims
[0])
246 value
= bool(libisl
.isl_set_involves_dims(islset
, libisl
.isl_dim_set
, first
, n
))
247 libisl
.isl_set_free(islset
)
251 def _fromislset(cls
, islset
, symbols
):
252 from .polyhedra
import Polyhedron
253 islset
= libisl
.isl_set_remove_divs(islset
)
254 islbsets
= isl_set_basic_sets(islset
)
255 libisl
.isl_set_free(islset
)
257 for islbset
in islbsets
:
258 polyhedron
= Polyhedron
._fromislbasicset
(islbset
, symbols
)
259 polyhedra
.append(polyhedron
)
260 if len(polyhedra
) == 0:
261 from .polyhedra
import Empty
263 elif len(polyhedra
) == 1:
266 self
= object().__new
__(Domain
)
267 self
._polyhedra
= tuple(polyhedra
)
268 self
._symbols
= cls
._xsymbols
(polyhedra
)
269 self
._dimension
= len(self
._symbols
)
272 def _toislset(cls
, polyhedra
, symbols
):
273 polyhedron
= polyhedra
[0]
274 islbset
= polyhedron
._toislbasicset
(polyhedron
.equalities
,
275 polyhedron
.inequalities
, symbols
)
276 islset1
= libisl
.isl_set_from_basic_set(islbset
)
277 for polyhedron
in polyhedra
[1:]:
278 islbset
= polyhedron
._toislbasicset
(polyhedron
.equalities
,
279 polyhedron
.inequalities
, symbols
)
280 islset2
= libisl
.isl_set_from_basic_set(islbset
)
281 islset1
= libisl
.isl_set_union(islset1
, islset2
)
285 def _fromast(cls
, node
):
286 from .polyhedra
import Polyhedron
287 if isinstance(node
, ast
.Module
) and len(node
.body
) == 1:
288 return cls
._fromast
(node
.body
[0])
289 elif isinstance(node
, ast
.Expr
):
290 return cls
._fromast
(node
.value
)
291 elif isinstance(node
, ast
.UnaryOp
):
292 domain
= cls
._fromast
(node
.operand
)
293 if isinstance(node
.operand
, ast
.invert
):
295 elif isinstance(node
, ast
.BinOp
):
296 domain1
= cls
._fromast
(node
.left
)
297 domain2
= cls
._fromast
(node
.right
)
298 if isinstance(node
.op
, ast
.BitAnd
):
299 return And(domain1
, domain2
)
300 elif isinstance(node
.op
, ast
.BitOr
):
301 return Or(domain1
, domain2
)
302 elif isinstance(node
, ast
.Compare
):
305 left
= Expression
._fromast
(node
.left
)
306 for i
in range(len(node
.ops
)):
308 right
= Expression
._fromast
(node
.comparators
[i
])
309 if isinstance(op
, ast
.Lt
):
310 inequalities
.append(right
- left
- 1)
311 elif isinstance(op
, ast
.LtE
):
312 inequalities
.append(right
- left
)
313 elif isinstance(op
, ast
.Eq
):
314 equalities
.append(left
- right
)
315 elif isinstance(op
, ast
.GtE
):
316 inequalities
.append(left
- right
)
317 elif isinstance(op
, ast
.Gt
):
318 inequalities
.append(left
- right
- 1)
323 return Polyhedron(equalities
, inequalities
)
324 raise SyntaxError('invalid syntax')
326 _RE_BRACES
= re
.compile(r
'^\{\s*|\s*\}$')
327 _RE_EQ
= re
.compile(r
'([^<=>])=([^<=>])')
328 _RE_AND
= re
.compile(r
'\band\b|,|&&|/\\|∧|∩')
329 _RE_OR
= re
.compile(r
'\bor\b|;|\|\||\\/|∨|∪')
330 _RE_NOT
= re
.compile(r
'\bnot\b|!|¬')
331 _RE_NUM_VAR
= Expression
._RE
_NUM
_VAR
332 _RE_OPERATORS
= re
.compile(r
'(&|\||~)')
335 def fromstring(cls
, string
):
336 # remove curly brackets
337 string
= cls
._RE
_BRACES
.sub(r
'', string
)
338 # replace '=' by '=='
339 string
= cls
._RE
_EQ
.sub(r
'\1==\2', string
)
340 # replace 'and', 'or', 'not'
341 string
= cls
._RE
_AND
.sub(r
' & ', string
)
342 string
= cls
._RE
_OR
.sub(r
' | ', string
)
343 string
= cls
._RE
_NOT
.sub(r
' ~', string
)
344 # add implicit multiplication operators, e.g. '5x' -> '5*x'
345 string
= cls
._RE
_NUM
_VAR
.sub(r
'\1*\2', string
)
346 # add parentheses to force precedence
347 tokens
= cls
._RE
_OPERATORS
.split(string
)
348 for i
, token
in enumerate(tokens
):
350 token
= '({})'.format(token
)
352 string
= ''.join(tokens
)
353 tree
= ast
.parse(string
, 'eval')
354 return cls
._fromast
(tree
)
357 assert len(self
.polyhedra
) >= 2
358 strings
= [repr(polyhedron
) for polyhedron
in self
.polyhedra
]
359 return 'Or({})'.format(', '.join(strings
))
362 def fromsympy(cls
, expr
):
364 from .polyhedra
import Lt
, Le
, Eq
, Ne
, Ge
, Gt
366 sympy
.And
: And
, sympy
.Or
: Or
, sympy
.Not
: Not
,
367 sympy
.Lt
: Lt
, sympy
.Le
: Le
,
368 sympy
.Eq
: Eq
, sympy
.Ne
: Ne
,
369 sympy
.Ge
: Ge
, sympy
.Gt
: Gt
,
371 if expr
.func
in funcmap
:
372 args
= [Domain
.fromsympy(arg
) for arg
in expr
.args
]
373 return funcmap
[expr
.func
](*args
)
374 elif isinstance(expr
, sympy
.Expr
):
375 return Expression
.fromsympy(expr
)
376 raise ValueError('non-domain expression: {!r}'.format(expr
))
380 polyhedra
= [polyhedron
.tosympy() for polyhedron
in polyhedra
]
381 return sympy
.Or(*polyhedra
)
385 if len(domains
) == 0:
386 from .polyhedra
import Universe
389 return domains
[0].intersection(*domains
[1:])
392 if len(domains
) == 0:
393 from .polyhedra
import Empty
396 return domains
[0].union(*domains
[1:])