6b47fe83b2f1a339ea89e43a1d9a7da20556c615
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
))
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
, symbols
):
156 # use to remove certain variables
157 if isinstance(symbols
, str):
158 symbols
= symbols
.replace(',', ' ').split()
160 symbols
= list(symbols
)
161 for i
, symbol
in enumerate(symbols
):
162 if isinstance(symbol
, Symbol
):
163 symbols
[i
] = symbol
.name
164 elif not isinstance(symbol
, str):
165 raise TypeError('symbols must be strings or Symbol instances')
166 islset
= self
._toislset
(self
.polyhedra
, self
.symbols
)
167 # the trick is to walk symbols in reverse order, to avoid index updates
168 for index
, symbol
in reversed(list(enumerate(self
.symbols
))):
169 if symbol
in symbols
:
170 islset
= libisl
.isl_set_project_out(islset
, libisl
.isl_dim_set
, index
, 1)
172 symbols
= [symbol
for symbol
in self
.symbols
if symbol
not in symbols
]
173 return Domain
._fromislset
(islset
, symbols
)
176 from .polyhedra
import Polyhedron
177 islset
= self
._toislset
(self
.polyhedra
, self
.symbols
)
178 islbset
= libisl
.isl_set_sample(islset
)
179 return Polyhedron
._fromislbasicset
(islbset
, self
.symbols
)
181 def intersection(self
, *others
):
184 symbols
= self
._xsymbols
((self
,) + others
)
185 islset1
= self
._toislset
(self
.polyhedra
, symbols
)
187 islset2
= other
._toislset
(other
.polyhedra
, symbols
)
188 islset1
= libisl
.isl_set_intersect(islset1
, islset2
)
189 return self
._fromislset
(islset1
, symbols
)
191 def __and__(self
, other
):
192 return self
.intersection(other
)
194 def union(self
, *others
):
197 symbols
= self
._xsymbols
((self
,) + others
)
198 islset1
= self
._toislset
(self
.polyhedra
, symbols
)
200 islset2
= other
._toislset
(other
.polyhedra
, symbols
)
201 islset1
= libisl
.isl_set_union(islset1
, islset2
)
202 return self
._fromislset
(islset1
, symbols
)
204 def __or__(self
, other
):
205 return self
.union(other
)
207 def __add__(self
, other
):
208 return self
.union(other
)
210 def difference(self
, other
):
211 symbols
= self
._xsymbols
([self
, other
])
212 islset1
= self
._toislset
(self
.polyhedra
, symbols
)
213 islset2
= other
._toislset
(other
.polyhedra
, symbols
)
214 islset
= libisl
.isl_set_subtract(islset1
, islset2
)
215 return self
._fromislset
(islset
, symbols
)
217 def __sub__(self
, other
):
218 return self
.difference(other
)
221 islset
= self
._toislset
(self
.polyhedra
, self
.symbols
)
222 islset
= libisl
.isl_set_lexmin(islset
)
223 return self
._fromislset
(islset
, self
.symbols
)
226 islset
= self
._toislset
(self
.polyhedra
, self
.symbols
)
227 islset
= libisl
.isl_set_lexmax(islset
)
228 return self
._fromislset
(islset
, self
.symbols
)
231 def _fromislset(cls
, islset
, symbols
):
232 from .polyhedra
import Polyhedron
233 islset
= libisl
.isl_set_remove_divs(islset
)
234 islbsets
= isl_set_basic_sets(islset
)
235 libisl
.isl_set_free(islset
)
237 for islbset
in islbsets
:
238 polyhedron
= Polyhedron
._fromislbasicset
(islbset
, symbols
)
239 polyhedra
.append(polyhedron
)
240 if len(polyhedra
) == 0:
241 from .polyhedra
import Empty
243 elif len(polyhedra
) == 1:
246 self
= object().__new
__(Domain
)
247 self
._polyhedra
= tuple(polyhedra
)
248 self
._symbols
= cls
._xsymbols
(polyhedra
)
249 self
._dimension
= len(self
._symbols
)
252 def _toislset(cls
, polyhedra
, symbols
):
253 polyhedron
= polyhedra
[0]
254 islbset
= polyhedron
._toislbasicset
(polyhedron
.equalities
,
255 polyhedron
.inequalities
, symbols
)
256 islset1
= libisl
.isl_set_from_basic_set(islbset
)
257 for polyhedron
in polyhedra
[1:]:
258 islbset
= polyhedron
._toislbasicset
(polyhedron
.equalities
,
259 polyhedron
.inequalities
, symbols
)
260 islset2
= libisl
.isl_set_from_basic_set(islbset
)
261 islset1
= libisl
.isl_set_union(islset1
, islset2
)
265 def _fromast(cls
, node
):
266 from .polyhedra
import Polyhedron
267 if isinstance(node
, ast
.Module
) and len(node
.body
) == 1:
268 return cls
._fromast
(node
.body
[0])
269 elif isinstance(node
, ast
.Expr
):
270 return cls
._fromast
(node
.value
)
271 elif isinstance(node
, ast
.UnaryOp
):
272 domain
= cls
._fromast
(node
.operand
)
273 if isinstance(node
.operand
, ast
.invert
):
275 elif isinstance(node
, ast
.BinOp
):
276 domain1
= cls
._fromast
(node
.left
)
277 domain2
= cls
._fromast
(node
.right
)
278 if isinstance(node
.op
, ast
.BitAnd
):
279 return And(domain1
, domain2
)
280 elif isinstance(node
.op
, ast
.BitOr
):
281 return Or(domain1
, domain2
)
282 elif isinstance(node
, ast
.Compare
):
285 left
= Expression
._fromast
(node
.left
)
286 for i
in range(len(node
.ops
)):
288 right
= Expression
._fromast
(node
.comparators
[i
])
289 if isinstance(op
, ast
.Lt
):
290 inequalities
.append(right
- left
- 1)
291 elif isinstance(op
, ast
.LtE
):
292 inequalities
.append(right
- left
)
293 elif isinstance(op
, ast
.Eq
):
294 equalities
.append(left
- right
)
295 elif isinstance(op
, ast
.GtE
):
296 inequalities
.append(left
- right
)
297 elif isinstance(op
, ast
.Gt
):
298 inequalities
.append(left
- right
- 1)
303 return Polyhedron(equalities
, inequalities
)
304 raise SyntaxError('invalid syntax')
306 _RE_BRACES
= re
.compile(r
'^\{\s*|\s*\}$')
307 _RE_EQ
= re
.compile(r
'([^<=>])=([^<=>])')
308 _RE_AND
= re
.compile(r
'\band\b|,|&&|/\\|∧|∩')
309 _RE_OR
= re
.compile(r
'\bor\b|;|\|\||\\/|∨|∪')
310 _RE_NOT
= re
.compile(r
'\bnot\b|!|¬')
311 _RE_NUM_VAR
= Expression
._RE
_NUM
_VAR
312 _RE_OPERATORS
= re
.compile(r
'(&|\||~)')
315 def fromstring(cls
, string
):
316 # remove curly brackets
317 string
= cls
._RE
_BRACES
.sub(r
'', string
)
318 # replace '=' by '=='
319 string
= cls
._RE
_EQ
.sub(r
'\1==\2', string
)
320 # replace 'and', 'or', 'not'
321 string
= cls
._RE
_AND
.sub(r
' & ', string
)
322 string
= cls
._RE
_OR
.sub(r
' | ', string
)
323 string
= cls
._RE
_NOT
.sub(r
' ~', string
)
324 # add implicit multiplication operators, e.g. '5x' -> '5*x'
325 string
= cls
._RE
_NUM
_VAR
.sub(r
'\1*\2', string
)
326 # add parentheses to force precedence
327 tokens
= cls
._RE
_OPERATORS
.split(string
)
328 for i
, token
in enumerate(tokens
):
330 token
= '({})'.format(token
)
332 string
= ''.join(tokens
)
333 tree
= ast
.parse(string
, 'eval')
334 return cls
._fromast
(tree
)
337 assert len(self
.polyhedra
) >= 2
338 strings
= [repr(polyhedron
) for polyhedron
in self
.polyhedra
]
339 return 'Or({})'.format(', '.join(strings
))
342 def fromsympy(cls
, expr
):
343 raise NotImplementedError
346 raise NotImplementedError
349 if len(domains
) == 0:
350 from .polyhedra
import Universe
353 return domains
[0].intersection(*domains
[1:])
356 if len(domains
) == 0:
357 from .polyhedra
import Empty
360 return domains
[0].union(*domains
[1:])