05c9c8fc7886369e9654dbda9f7354762a716dbb
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 # next instruction should NOT be required
175 islbset
= libisl
.isl_basic_set_finalize(islbset
)
176 return Polyhedron
._fromislbasicset
(islbset
, self
.symbols
)
178 def intersection(self
, *others
):
181 symbols
= self
._xsymbols
((self
,) + others
)
182 islset1
= self
._toislset
(self
.polyhedra
, symbols
)
184 islset2
= other
._toislset
(other
.polyhedra
, symbols
)
185 islset1
= libisl
.isl_set_intersect(islset1
, islset2
)
186 return self
._fromislset
(islset1
, symbols
)
188 def __and__(self
, other
):
189 return self
.intersection(other
)
191 def union(self
, *others
):
194 symbols
= self
._xsymbols
((self
,) + others
)
195 islset1
= self
._toislset
(self
.polyhedra
, symbols
)
197 islset2
= other
._toislset
(other
.polyhedra
, symbols
)
198 islset1
= libisl
.isl_set_union(islset1
, islset2
)
199 return self
._fromislset
(islset1
, symbols
)
201 def __or__(self
, other
):
202 return self
.union(other
)
204 def __add__(self
, other
):
205 return self
.union(other
)
207 def difference(self
, other
):
208 symbols
= self
._xsymbols
([self
, other
])
209 islset1
= self
._toislset
(self
.polyhedra
, symbols
)
210 islset2
= other
._toislset
(other
.polyhedra
, symbols
)
211 islset
= libisl
.isl_set_subtract(islset1
, islset2
)
212 return self
._fromislset
(islset
, symbols
)
214 def __sub__(self
, other
):
215 return self
.difference(other
)
218 islset
= self
._toislset
(self
.polyhedra
, self
.symbols
)
219 islset
= libisl
.isl_set_lexmin(islset
)
220 return self
._fromislset
(islset
, self
.symbols
)
223 islset
= self
._toislset
(self
.polyhedra
, self
.symbols
)
224 islset
= libisl
.isl_set_lexmax(islset
)
225 return self
._fromislset
(islset
, self
.symbols
)
227 def num_parameters(self
):
228 #could be useful with large, complicated polyhedrons
229 islbset
= self
._toislbasicset
(self
.equalities
, self
.inequalities
, self
.symbols
)
230 num
= libisl
.isl_basic_set_dim(islbset
, libisl
.isl_dim_set
)
233 def involves_dims(self
, dims
):
234 #could be useful with large, complicated polyhedrons
235 islset
= self
._toislset
(self
.polyhedra
, self
.symbols
)
237 symbols
= sorted(list(self
.symbols
))
242 first
= symbols
.index(dims
[0])
248 value
= bool(libisl
.isl_set_involves_dims(islset
, libisl
.isl_dim_set
, first
, n
))
249 libisl
.isl_set_free(islset
)
254 islbset
= self
._toislbasicset
(self
.equalities
, self
.inequalities
, self
.symbols
)
255 vertices
= libisl
.isl_basic_set_compute_vertices(islbset
);
256 vertexes
= islhelper
.isl_vertices_vertices(vertices
)
257 #vertex = libisl.isl_vertices_get_n_vertices(vertices)
258 for verts
in vertexes
:
259 expr
= libisl
.isl_vertex_get_expr(verts
);
260 this
= islhelper
.isl_set_to_str(expr
)
263 raise TypeError('set must be bounded')
269 symbols
= self
.symbols
271 islset
= self
._toislset
(self
.polyhedra
, self
.symbols
)
272 points
= islhelper
.isl_set_points(islset
)
275 coordinate
= libisl
.isl_point_get_coordinate_val(point
, libisl
.isl_dim_set
, symbols
.index(sym
))
276 coordinate
= islhelper
.isl_val_to_int(coordinate
)
277 coordinates
.append(coordinate
)
279 raise TypeError('set must be bounded')
283 def _fromislset(cls
, islset
, symbols
):
284 from .polyhedra
import Polyhedron
285 islset
= libisl
.isl_set_remove_divs(islset
)
286 islbsets
= isl_set_basic_sets(islset
)
287 libisl
.isl_set_free(islset
)
289 for islbset
in islbsets
:
290 polyhedron
= Polyhedron
._fromislbasicset
(islbset
, symbols
)
291 polyhedra
.append(polyhedron
)
292 if len(polyhedra
) == 0:
293 from .polyhedra
import Empty
295 elif len(polyhedra
) == 1:
298 self
= object().__new
__(Domain
)
299 self
._polyhedra
= tuple(polyhedra
)
300 self
._symbols
= cls
._xsymbols
(polyhedra
)
301 self
._dimension
= len(self
._symbols
)
304 def _toislset(cls
, polyhedra
, symbols
):
305 polyhedron
= polyhedra
[0]
306 islbset
= polyhedron
._toislbasicset
(polyhedron
.equalities
,
307 polyhedron
.inequalities
, symbols
)
308 islset1
= libisl
.isl_set_from_basic_set(islbset
)
309 for polyhedron
in polyhedra
[1:]:
310 islbset
= polyhedron
._toislbasicset
(polyhedron
.equalities
,
311 polyhedron
.inequalities
, symbols
)
312 islset2
= libisl
.isl_set_from_basic_set(islbset
)
313 islset1
= libisl
.isl_set_union(islset1
, islset2
)
317 def _fromast(cls
, node
):
318 from .polyhedra
import Polyhedron
319 if isinstance(node
, ast
.Module
) and len(node
.body
) == 1:
320 return cls
._fromast
(node
.body
[0])
321 elif isinstance(node
, ast
.Expr
):
322 return cls
._fromast
(node
.value
)
323 elif isinstance(node
, ast
.UnaryOp
):
324 domain
= cls
._fromast
(node
.operand
)
325 if isinstance(node
.operand
, ast
.invert
):
327 elif isinstance(node
, ast
.BinOp
):
328 domain1
= cls
._fromast
(node
.left
)
329 domain2
= cls
._fromast
(node
.right
)
330 if isinstance(node
.op
, ast
.BitAnd
):
331 return And(domain1
, domain2
)
332 elif isinstance(node
.op
, ast
.BitOr
):
333 return Or(domain1
, domain2
)
334 elif isinstance(node
, ast
.Compare
):
337 left
= Expression
._fromast
(node
.left
)
338 for i
in range(len(node
.ops
)):
340 right
= Expression
._fromast
(node
.comparators
[i
])
341 if isinstance(op
, ast
.Lt
):
342 inequalities
.append(right
- left
- 1)
343 elif isinstance(op
, ast
.LtE
):
344 inequalities
.append(right
- left
)
345 elif isinstance(op
, ast
.Eq
):
346 equalities
.append(left
- right
)
347 elif isinstance(op
, ast
.GtE
):
348 inequalities
.append(left
- right
)
349 elif isinstance(op
, ast
.Gt
):
350 inequalities
.append(left
- right
- 1)
355 return Polyhedron(equalities
, inequalities
)
356 raise SyntaxError('invalid syntax')
358 _RE_BRACES
= re
.compile(r
'^\{\s*|\s*\}$')
359 _RE_EQ
= re
.compile(r
'([^<=>])=([^<=>])')
360 _RE_AND
= re
.compile(r
'\band\b|,|&&|/\\|∧|∩')
361 _RE_OR
= re
.compile(r
'\bor\b|;|\|\||\\/|∨|∪')
362 _RE_NOT
= re
.compile(r
'\bnot\b|!|¬')
363 _RE_NUM_VAR
= Expression
._RE
_NUM
_VAR
364 _RE_OPERATORS
= re
.compile(r
'(&|\||~)')
367 def fromstring(cls
, string
):
368 # remove curly brackets
369 string
= cls
._RE
_BRACES
.sub(r
'', string
)
370 # replace '=' by '=='
371 string
= cls
._RE
_EQ
.sub(r
'\1==\2', string
)
372 # replace 'and', 'or', 'not'
373 string
= cls
._RE
_AND
.sub(r
' & ', string
)
374 string
= cls
._RE
_OR
.sub(r
' | ', string
)
375 string
= cls
._RE
_NOT
.sub(r
' ~', string
)
376 # add implicit multiplication operators, e.g. '5x' -> '5*x'
377 string
= cls
._RE
_NUM
_VAR
.sub(r
'\1*\2', string
)
378 # add parentheses to force precedence
379 tokens
= cls
._RE
_OPERATORS
.split(string
)
380 for i
, token
in enumerate(tokens
):
382 token
= '({})'.format(token
)
384 string
= ''.join(tokens
)
385 tree
= ast
.parse(string
, 'eval')
386 return cls
._fromast
(tree
)
389 assert len(self
.polyhedra
) >= 2
390 strings
= [repr(polyhedron
) for polyhedron
in self
.polyhedra
]
391 return 'Or({})'.format(', '.join(strings
))
394 def fromsympy(cls
, expr
):
396 from .polyhedra
import Lt
, Le
, Eq
, Ne
, Ge
, Gt
398 sympy
.And
: And
, sympy
.Or
: Or
, sympy
.Not
: Not
,
399 sympy
.Lt
: Lt
, sympy
.Le
: Le
,
400 sympy
.Eq
: Eq
, sympy
.Ne
: Ne
,
401 sympy
.Ge
: Ge
, sympy
.Gt
: Gt
,
403 if expr
.func
in funcmap
:
404 args
= [Domain
.fromsympy(arg
) for arg
in expr
.args
]
405 return funcmap
[expr
.func
](*args
)
406 elif isinstance(expr
, sympy
.Expr
):
407 return Expression
.fromsympy(expr
)
408 raise ValueError('non-domain expression: {!r}'.format(expr
))
412 polyhedra
= [polyhedron
.tosympy() for polyhedron
in polyhedra
]
413 return sympy
.Or(*polyhedra
)
417 if len(domains
) == 0:
418 from .polyhedra
import Universe
421 return domains
[0].intersection(*domains
[1:])
424 if len(domains
) == 0:
425 from .polyhedra
import Empty
428 return domains
[0].union(*domains
[1:])