4cd46a4deb2fb7f84df7eb26acb6b5869b0ea3c0
1 # Copyright 2014 MINES ParisTech
3 # This file is part of LinPy.
5 # LinPy is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # LinPy is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with LinPy. If not, see <http://www.gnu.org/licenses/>.
23 from fractions
import Fraction
25 from . import islhelper
26 from .islhelper
import mainctx
, libisl
27 from .linexprs
import LinExpr
, Symbol
, Rational
28 from .geometry
import GeometricObject
, Point
, Vector
37 @functools.total_ordering
38 class Domain(GeometricObject
):
40 A domain is a union of polyhedra. Unlike polyhedra, domains allow exact
41 computation of union and complementary operations.
43 A domain with a unique polyhedron is automatically subclassed as a
53 def __new__(cls
, *polyhedra
):
55 Return a domain from a sequence of polyhedra.
57 >>> square = Polyhedron('0 <= x <= 2, 0 <= y <= 2')
58 >>> square2 = Polyhedron('2 <= x <= 4, 2 <= y <= 4')
59 >>> dom = Domain([square, square2])
61 It is also possible to build domains from polyhedra using arithmetic
62 operators Domain.__and__(), Domain.__or__() or functions And() and Or(),
63 using one of the following instructions:
65 >>> square = Polyhedron('0 <= x <= 2, 0 <= y <= 2')
66 >>> square2 = Polyhedron('2 <= x <= 4, 2 <= y <= 4')
67 >>> dom = square | square2
68 >>> dom = Or(square, square2)
70 Alternatively, a domain can be built from a string:
72 >>> dom = Domain('0 <= x <= 2, 0 <= y <= 2; 2 <= x <= 4, 2 <= y <= 4')
74 Finally, a domain can be built from a GeometricObject instance, calling
75 the GeometricObject.asdomain() method.
77 from .polyhedra
import Polyhedron
78 if len(polyhedra
) == 1:
79 argument
= polyhedra
[0]
80 if isinstance(argument
, str):
81 return cls
.fromstring(argument
)
82 elif isinstance(argument
, GeometricObject
):
83 return argument
.aspolyhedron()
85 raise TypeError('argument must be a string '
86 'or a GeometricObject instance')
88 for polyhedron
in polyhedra
:
89 if not isinstance(polyhedron
, Polyhedron
):
90 raise TypeError('arguments must be Polyhedron instances')
91 symbols
= cls
._xsymbols
(polyhedra
)
92 islset
= cls
._toislset
(polyhedra
, symbols
)
93 return cls
._fromislset
(islset
, symbols
)
96 def _xsymbols(cls
, iterator
):
98 Return the ordered tuple of symbols present in iterator.
101 for item
in iterator
:
102 symbols
.update(item
.symbols
)
103 return tuple(sorted(symbols
, key
=Symbol
.sortkey
))
108 The tuple of polyhedra present in the domain.
110 return self
._polyhedra
115 The tuple of symbols present in the domain equations, sorted according
123 The dimension of the domain, i.e. the number of symbols present in it.
125 return self
._dimension
129 Return True if the domain is empty, that is, equal to Empty.
131 islset
= self
._toislset
(self
.polyhedra
, self
.symbols
)
132 empty
= bool(libisl
.isl_set_is_empty(islset
))
133 libisl
.isl_set_free(islset
)
138 Return True if the domain is non-empty.
140 return not self
.isempty()
142 def isuniverse(self
):
144 Return True if the domain is universal, that is, equal to Universe.
146 islset
= self
._toislset
(self
.polyhedra
, self
.symbols
)
147 universe
= bool(libisl
.isl_set_plain_is_universe(islset
))
148 libisl
.isl_set_free(islset
)
153 Return True if the domain is bounded.
155 islset
= self
._toislset
(self
.polyhedra
, self
.symbols
)
156 bounded
= bool(libisl
.isl_set_is_bounded(islset
))
157 libisl
.isl_set_free(islset
)
160 def __eq__(self
, other
):
162 Return True if two domains are equal.
164 symbols
= self
._xsymbols
([self
, other
])
165 islset1
= self
._toislset
(self
.polyhedra
, symbols
)
166 islset2
= other
._toislset
(other
.polyhedra
, symbols
)
167 equal
= bool(libisl
.isl_set_is_equal(islset1
, islset2
))
168 libisl
.isl_set_free(islset1
)
169 libisl
.isl_set_free(islset2
)
172 def isdisjoint(self
, other
):
174 Return True if two domains have a null intersection.
176 symbols
= self
._xsymbols
([self
, other
])
177 islset1
= self
._toislset
(self
.polyhedra
, symbols
)
178 islset2
= self
._toislset
(other
.polyhedra
, symbols
)
179 equal
= bool(libisl
.isl_set_is_disjoint(islset1
, islset2
))
180 libisl
.isl_set_free(islset1
)
181 libisl
.isl_set_free(islset2
)
184 def issubset(self
, other
):
186 Report whether another domain contains the domain.
188 symbols
= self
._xsymbols
([self
, other
])
189 islset1
= self
._toislset
(self
.polyhedra
, symbols
)
190 islset2
= self
._toislset
(other
.polyhedra
, symbols
)
191 equal
= bool(libisl
.isl_set_is_subset(islset1
, islset2
))
192 libisl
.isl_set_free(islset1
)
193 libisl
.isl_set_free(islset2
)
196 def __le__(self
, other
):
197 return self
.issubset(other
)
198 __le__
.__doc
__ = issubset
.__doc
__
200 def __lt__(self
, other
):
202 Report whether another domain is contained within the domain.
204 symbols
= self
._xsymbols
([self
, other
])
205 islset1
= self
._toislset
(self
.polyhedra
, symbols
)
206 islset2
= self
._toislset
(other
.polyhedra
, symbols
)
207 equal
= bool(libisl
.isl_set_is_strict_subset(islset1
, islset2
))
208 libisl
.isl_set_free(islset1
)
209 libisl
.isl_set_free(islset2
)
212 def complement(self
):
214 Return the complementary domain of the domain.
216 islset
= self
._toislset
(self
.polyhedra
, self
.symbols
)
217 islset
= libisl
.isl_set_complement(islset
)
218 return self
._fromislset
(islset
, self
.symbols
)
220 def __invert__(self
):
221 return self
.complement()
222 __invert__
.__doc
__ = complement
.__doc
__
224 def make_disjoint(self
):
226 Return an equivalent domain, whose polyhedra are disjoint.
228 islset
= self
._toislset
(self
.polyhedra
, self
.symbols
)
229 islset
= libisl
.isl_set_make_disjoint(mainctx
, islset
)
230 return self
._fromislset
(islset
, self
.symbols
)
234 Simplify the representation of the domain by trying to combine pairs of
235 polyhedra into a single polyhedron, and return the resulting domain.
237 islset
= self
._toislset
(self
.polyhedra
, self
.symbols
)
238 islset
= libisl
.isl_set_coalesce(islset
)
239 return self
._fromislset
(islset
, self
.symbols
)
241 def detect_equalities(self
):
243 Simplify the representation of the domain by detecting implicit
244 equalities, and return the resulting domain.
246 islset
= self
._toislset
(self
.polyhedra
, self
.symbols
)
247 islset
= libisl
.isl_set_detect_equalities(islset
)
248 return self
._fromislset
(islset
, self
.symbols
)
250 def remove_redundancies(self
):
252 Remove redundant constraints in the domain, and return the resulting
255 islset
= self
._toislset
(self
.polyhedra
, self
.symbols
)
256 islset
= libisl
.isl_set_remove_redundancies(islset
)
257 return self
._fromislset
(islset
, self
.symbols
)
259 def aspolyhedron(self
):
260 from .polyhedra
import Polyhedron
261 islset
= self
._toislset
(self
.polyhedra
, self
.symbols
)
262 islbset
= libisl
.isl_set_polyhedral_hull(islset
)
263 return Polyhedron
._fromislbasicset
(islbset
, self
.symbols
)
268 def project(self
, symbols
):
270 Project out the sequence of symbols given in arguments, and return the
273 islset
= self
._toislset
(self
.polyhedra
, self
.symbols
)
275 for index
, symbol
in reversed(list(enumerate(self
.symbols
))):
276 if symbol
in symbols
:
279 islset
= libisl
.isl_set_project_out(islset
,
280 libisl
.isl_dim_set
, index
+ 1, n
)
283 islset
= libisl
.isl_set_project_out(islset
, libisl
.isl_dim_set
, 0, n
)
284 symbols
= [symbol
for symbol
in self
.symbols
if symbol
not in symbols
]
285 return Domain
._fromislset
(islset
, symbols
)
289 Return a sample of the domain, as an integer instance of Point. If the
290 domain is empty, a ValueError exception is raised.
292 islset
= self
._toislset
(self
.polyhedra
, self
.symbols
)
293 islpoint
= libisl
.isl_set_sample_point(islset
)
294 if bool(libisl
.isl_point_is_void(islpoint
)):
295 libisl
.isl_point_free(islpoint
)
296 raise ValueError('domain must be non-empty')
298 for index
, symbol
in enumerate(self
.symbols
):
299 coordinate
= libisl
.isl_point_get_coordinate_val(islpoint
,
300 libisl
.isl_dim_set
, index
)
301 coordinate
= islhelper
.isl_val_to_int(coordinate
)
302 point
[symbol
] = coordinate
303 libisl
.isl_point_free(islpoint
)
306 def intersection(self
, *others
):
308 Return the intersection of two or more domains as a new domain. As an
309 alternative, function And() can be used.
313 symbols
= self
._xsymbols
((self
,) + others
)
314 islset1
= self
._toislset
(self
.polyhedra
, symbols
)
316 islset2
= other
._toislset
(other
.polyhedra
, symbols
)
317 islset1
= libisl
.isl_set_intersect(islset1
, islset2
)
318 return self
._fromislset
(islset1
, symbols
)
320 def __and__(self
, other
):
321 return self
.intersection(other
)
322 __and__
.__doc
__ = intersection
.__doc
__
324 def union(self
, *others
):
326 Return the union of two or more domains as a new domain. As an
327 alternative, function Or() can be used.
331 symbols
= self
._xsymbols
((self
,) + others
)
332 islset1
= self
._toislset
(self
.polyhedra
, symbols
)
334 islset2
= other
._toislset
(other
.polyhedra
, symbols
)
335 islset1
= libisl
.isl_set_union(islset1
, islset2
)
336 return self
._fromislset
(islset1
, symbols
)
338 def __or__(self
, other
):
339 return self
.union(other
)
340 __or__
.__doc
__ = union
.__doc
__
342 def __add__(self
, other
):
343 return self
.union(other
)
344 __add__
.__doc
__ = union
.__doc
__
346 def difference(self
, other
):
348 Return the difference of two domains as a new domain.
350 symbols
= self
._xsymbols
([self
, other
])
351 islset1
= self
._toislset
(self
.polyhedra
, symbols
)
352 islset2
= other
._toislset
(other
.polyhedra
, symbols
)
353 islset
= libisl
.isl_set_subtract(islset1
, islset2
)
354 return self
._fromislset
(islset
, symbols
)
356 def __sub__(self
, other
):
357 return self
.difference(other
)
358 __sub__
.__doc
__ = difference
.__doc
__
362 Return the lexicographic minimum of the elements in the domain.
364 islset
= self
._toislset
(self
.polyhedra
, self
.symbols
)
365 islset
= libisl
.isl_set_lexmin(islset
)
366 return self
._fromislset
(islset
, self
.symbols
)
370 Return the lexicographic maximum of the elements in the domain.
372 islset
= self
._toislset
(self
.polyhedra
, self
.symbols
)
373 islset
= libisl
.isl_set_lexmax(islset
)
374 return self
._fromislset
(islset
, self
.symbols
)
376 _RE_COORDINATE
= re
.compile(r
'\((?P<num>\-?\d+)\)(/(?P<den>\d+))?')
380 Return the vertices of the domain, as a list of rational instances of
383 from .polyhedra
import Polyhedron
384 if not self
.isbounded():
385 raise ValueError('domain must be bounded')
386 islbset
= self
._toislbasicset
(self
.equalities
, self
.inequalities
,
388 vertices
= libisl
.isl_basic_set_compute_vertices(islbset
);
389 vertices
= islhelper
.isl_vertices_vertices(vertices
)
391 for vertex
in vertices
:
392 expr
= libisl
.isl_vertex_get_expr(vertex
)
394 if islhelper
.isl_version
< '0.13':
395 constraints
= islhelper
.isl_basic_set_constraints(expr
)
396 for constraint
in constraints
:
397 constant
= libisl
.isl_constraint_get_constant_val(constraint
)
398 constant
= islhelper
.isl_val_to_int(constant
)
399 for index
, symbol
in enumerate(self
.symbols
):
400 coefficient
= libisl
.isl_constraint_get_coefficient_val(constraint
,
401 libisl
.isl_dim_set
, index
)
402 coefficient
= islhelper
.isl_val_to_int(coefficient
)
404 coordinate
= -Fraction(constant
, coefficient
)
405 coordinates
.append((symbol
, coordinate
))
407 string
= islhelper
.isl_multi_aff_to_str(expr
)
408 matches
= self
._RE
_COORDINATE
.finditer(string
)
409 for symbol
, match
in zip(self
.symbols
, matches
):
410 numerator
= int(match
.group('num'))
411 denominator
= match
.group('den')
412 denominator
= 1 if denominator
is None else int(denominator
)
413 coordinate
= Fraction(numerator
, denominator
)
414 coordinates
.append((symbol
, coordinate
))
415 points
.append(Point(coordinates
))
420 Return the integer points of a bounded domain, as a list of integer
421 instances of Point. If the domain is not bounded, a ValueError exception
424 if not self
.isbounded():
425 raise ValueError('domain must be bounded')
426 from .polyhedra
import Universe
, Eq
427 islset
= self
._toislset
(self
.polyhedra
, self
.symbols
)
428 islpoints
= islhelper
.isl_set_points(islset
)
430 for islpoint
in islpoints
:
432 for index
, symbol
in enumerate(self
.symbols
):
433 coordinate
= libisl
.isl_point_get_coordinate_val(islpoint
,
434 libisl
.isl_dim_set
, index
)
435 coordinate
= islhelper
.isl_val_to_int(coordinate
)
436 coordinates
[symbol
] = coordinate
437 points
.append(Point(coordinates
))
440 def __contains__(self
, point
):
442 Return True if the point if contained within the domain.
444 for polyhedron
in self
.polyhedra
:
445 if point
in polyhedron
:
450 def _polygon_inner_point(cls
, points
):
451 symbols
= points
[0].symbols
452 coordinates
= {symbol
: 0 for symbol
in symbols
}
454 for symbol
, coordinate
in point
.coordinates():
455 coordinates
[symbol
] += coordinate
456 for symbol
in symbols
:
457 coordinates
[symbol
] /= len(points
)
458 return Point(coordinates
)
461 def _sort_polygon_2d(cls
, points
):
464 o
= cls
._polygon
_inner
_point
(points
)
468 dx
, dy
= (coordinate
for symbol
, coordinate
in om
.coordinates())
469 angle
= math
.atan2(dy
, dx
)
471 return sorted(points
, key
=angles
.get
)
474 def _sort_polygon_3d(cls
, points
):
477 o
= cls
._polygon
_inner
_point
(points
)
488 raise ValueError('degenerate polygon')
492 normprod
= norm_oa
* om
.norm()
493 cosinus
= max(oa
.dot(om
) / normprod
, -1.)
494 sinus
= u
.dot(oa
.cross(om
)) / normprod
495 angle
= math
.acos(cosinus
)
496 angle
= math
.copysign(angle
, sinus
)
498 return sorted(points
, key
=angles
.get
)
502 Return the list of faces of a bounded domain. Each face is represented
503 by a list of vertices, in the form of rational instances of Point. If
504 the domain is not bounded, a ValueError exception is raised.
507 for polyhedron
in self
.polyhedra
:
508 vertices
= polyhedron
.vertices()
509 for constraint
in polyhedron
.constraints
:
511 for vertex
in vertices
:
512 if constraint
.subs(vertex
.coordinates()) == 0:
518 def _plot_2d(self
, plot
=None, **kwargs
):
519 import matplotlib
.pyplot
as plt
520 from matplotlib
.patches
import Polygon
523 plot
= fig
.add_subplot(1, 1, 1)
524 xmin
, xmax
= plot
.get_xlim()
525 ymin
, ymax
= plot
.get_ylim()
526 for polyhedron
in self
.polyhedra
:
527 vertices
= polyhedron
._sort
_polygon
_2d
(polyhedron
.vertices())
528 xys
= [tuple(vertex
.values()) for vertex
in vertices
]
530 xmin
, xmax
= min(xmin
, float(min(xs
))), max(xmax
, float(max(xs
)))
531 ymin
, ymax
= min(ymin
, float(min(ys
))), max(ymax
, float(max(ys
)))
532 plot
.add_patch(Polygon(xys
, closed
=True, **kwargs
))
533 plot
.set_xlim(xmin
, xmax
)
534 plot
.set_ylim(ymin
, ymax
)
537 def _plot_3d(self
, plot
=None, **kwargs
):
538 import matplotlib
.pyplot
as plt
539 from mpl_toolkits
.mplot3d
import Axes3D
540 from mpl_toolkits
.mplot3d
.art3d
import Poly3DCollection
546 xmin
, xmax
= axes
.get_xlim()
547 ymin
, ymax
= axes
.get_ylim()
548 zmin
, zmax
= axes
.get_zlim()
550 for vertices
in self
.faces():
551 vertices
= self
._sort
_polygon
_3d
(vertices
)
552 vertices
.append(vertices
[0])
553 face_xyzs
= [tuple(vertex
.values()) for vertex
in vertices
]
554 xs
, ys
, zs
= zip(*face_xyzs
)
555 xmin
, xmax
= min(xmin
, float(min(xs
))), max(xmax
, float(max(xs
)))
556 ymin
, ymax
= min(ymin
, float(min(ys
))), max(ymax
, float(max(ys
)))
557 zmin
, zmax
= min(zmin
, float(min(zs
))), max(zmax
, float(max(zs
)))
558 poly_xyzs
.append(face_xyzs
)
559 collection
= Poly3DCollection(poly_xyzs
, **kwargs
)
560 axes
.add_collection3d(collection
)
561 axes
.set_xlim(xmin
, xmax
)
562 axes
.set_ylim(ymin
, ymax
)
563 axes
.set_zlim(zmin
, zmax
)
566 def plot(self
, plot
=None, **kwargs
):
568 Plot a 2D or 3D domain using matplotlib. Draw it to the current plot
569 object if present, otherwise create a new one. options are keyword
570 arguments passed to the matplotlib drawing functions, they can be used
571 to set the drawing color for example. Raise ValueError is the domain is
574 if not self
.isbounded():
575 raise ValueError('domain must be bounded')
576 elif self
.dimension
== 2:
577 return self
._plot
_2d
(plot
=plot
, **kwargs
)
578 elif self
.dimension
== 3:
579 return self
._plot
_3d
(plot
=plot
, **kwargs
)
581 raise ValueError('polyhedron must be 2 or 3-dimensional')
583 def subs(self
, symbol
, expression
=None):
585 Substitute the given symbol by an expression in the domain constraints.
586 To perform multiple substitutions at once, pass a sequence or a
587 dictionary of (old, new) pairs to subs. The syntax of this function is
588 similar to LinExpr.subs().
590 polyhedra
= [polyhedron
.subs(symbol
, expression
)
591 for polyhedron
in self
.polyhedra
]
592 return Domain(*polyhedra
)
595 def _fromislset(cls
, islset
, symbols
):
596 from .polyhedra
import Polyhedron
597 islset
= libisl
.isl_set_remove_divs(islset
)
598 islbsets
= islhelper
.isl_set_basic_sets(islset
)
599 libisl
.isl_set_free(islset
)
601 for islbset
in islbsets
:
602 polyhedron
= Polyhedron
._fromislbasicset
(islbset
, symbols
)
603 polyhedra
.append(polyhedron
)
604 if len(polyhedra
) == 0:
605 from .polyhedra
import Empty
607 elif len(polyhedra
) == 1:
610 self
= object().__new
__(Domain
)
611 self
._polyhedra
= tuple(polyhedra
)
612 self
._symbols
= cls
._xsymbols
(polyhedra
)
613 self
._dimension
= len(self
._symbols
)
617 def _toislset(cls
, polyhedra
, symbols
):
618 polyhedron
= polyhedra
[0]
619 islbset
= polyhedron
._toislbasicset
(polyhedron
.equalities
,
620 polyhedron
.inequalities
, symbols
)
621 islset1
= libisl
.isl_set_from_basic_set(islbset
)
622 for polyhedron
in polyhedra
[1:]:
623 islbset
= polyhedron
._toislbasicset
(polyhedron
.equalities
,
624 polyhedron
.inequalities
, symbols
)
625 islset2
= libisl
.isl_set_from_basic_set(islbset
)
626 islset1
= libisl
.isl_set_union(islset1
, islset2
)
630 def _fromast(cls
, node
):
631 from .polyhedra
import Polyhedron
632 if isinstance(node
, ast
.Module
) and len(node
.body
) == 1:
633 return cls
._fromast
(node
.body
[0])
634 elif isinstance(node
, ast
.Expr
):
635 return cls
._fromast
(node
.value
)
636 elif isinstance(node
, ast
.UnaryOp
):
637 domain
= cls
._fromast
(node
.operand
)
638 if isinstance(node
.operand
, ast
.invert
):
640 elif isinstance(node
, ast
.BinOp
):
641 domain1
= cls
._fromast
(node
.left
)
642 domain2
= cls
._fromast
(node
.right
)
643 if isinstance(node
.op
, ast
.BitAnd
):
644 return And(domain1
, domain2
)
645 elif isinstance(node
.op
, ast
.BitOr
):
646 return Or(domain1
, domain2
)
647 elif isinstance(node
, ast
.Compare
):
650 left
= LinExpr
._fromast
(node
.left
)
651 for i
in range(len(node
.ops
)):
653 right
= LinExpr
._fromast
(node
.comparators
[i
])
654 if isinstance(op
, ast
.Lt
):
655 inequalities
.append(right
- left
- 1)
656 elif isinstance(op
, ast
.LtE
):
657 inequalities
.append(right
- left
)
658 elif isinstance(op
, ast
.Eq
):
659 equalities
.append(left
- right
)
660 elif isinstance(op
, ast
.GtE
):
661 inequalities
.append(left
- right
)
662 elif isinstance(op
, ast
.Gt
):
663 inequalities
.append(left
- right
- 1)
668 return Polyhedron(equalities
, inequalities
)
669 raise SyntaxError('invalid syntax')
671 _RE_BRACES
= re
.compile(r
'^\{\s*|\s*\}$')
672 _RE_EQ
= re
.compile(r
'([^<=>])=([^<=>])')
673 _RE_AND
= re
.compile(r
'\band\b|,|&&|/\\|∧|∩')
674 _RE_OR
= re
.compile(r
'\bor\b|;|\|\||\\/|∨|∪')
675 _RE_NOT
= re
.compile(r
'\bnot\b|!|¬')
676 _RE_NUM_VAR
= LinExpr
._RE
_NUM
_VAR
677 _RE_OPERATORS
= re
.compile(r
'(&|\||~)')
680 def fromstring(cls
, string
):
682 Create a domain from a string. Raise SyntaxError if the string is not
685 # remove curly brackets
686 string
= cls
._RE
_BRACES
.sub(r
'', string
)
687 # replace '=' by '=='
688 string
= cls
._RE
_EQ
.sub(r
'\1==\2', string
)
689 # replace 'and', 'or', 'not'
690 string
= cls
._RE
_AND
.sub(r
' & ', string
)
691 string
= cls
._RE
_OR
.sub(r
' | ', string
)
692 string
= cls
._RE
_NOT
.sub(r
' ~', string
)
693 # add implicit multiplication operators, e.g. '5x' -> '5*x'
694 string
= cls
._RE
_NUM
_VAR
.sub(r
'\1*\2', string
)
695 # add parentheses to force precedence
696 tokens
= cls
._RE
_OPERATORS
.split(string
)
697 for i
, token
in enumerate(tokens
):
699 token
= '({})'.format(token
)
701 string
= ''.join(tokens
)
702 tree
= ast
.parse(string
, 'eval')
703 return cls
._fromast
(tree
)
706 assert len(self
.polyhedra
) >= 2
707 strings
= [repr(polyhedron
) for polyhedron
in self
.polyhedra
]
708 return 'Or({})'.format(', '.join(strings
))
710 def _repr_latex_(self
):
712 for polyhedron
in self
.polyhedra
:
713 strings
.append('({})'.format(polyhedron
._repr
_latex
_().strip('$')))
714 return '${}$'.format(' \\vee '.join(strings
))
717 def fromsympy(cls
, expr
):
719 Create a domain from a sympy expression.
722 from .polyhedra
import Lt
, Le
, Eq
, Ne
, Ge
, Gt
724 sympy
.And
: And
, sympy
.Or
: Or
, sympy
.Not
: Not
,
725 sympy
.Lt
: Lt
, sympy
.Le
: Le
,
726 sympy
.Eq
: Eq
, sympy
.Ne
: Ne
,
727 sympy
.Ge
: Ge
, sympy
.Gt
: Gt
,
729 if expr
.func
in funcmap
:
730 args
= [Domain
.fromsympy(arg
) for arg
in expr
.args
]
731 return funcmap
[expr
.func
](*args
)
732 elif isinstance(expr
, sympy
.Expr
):
733 return LinExpr
.fromsympy(expr
)
734 raise ValueError('non-domain expression: {!r}'.format(expr
))
738 Convert the domain to a sympy expression.
741 polyhedra
= [polyhedron
.tosympy() for polyhedron
in polyhedra
]
742 return sympy
.Or(*polyhedra
)
747 Create the intersection domain of the domains given in arguments.
749 if len(domains
) == 0:
750 from .polyhedra
import Universe
753 return domains
[0].intersection(*domains
[1:])
754 And
.__doc
__ = Domain
.intersection
.__doc
__
758 Create the union domain of the domains given in arguments.
760 if len(domains
) == 0:
761 from .polyhedra
import Empty
764 return domains
[0].union(*domains
[1:])
765 Or
.__doc
__ = Domain
.union
.__doc
__
769 Create the complementary domain of the domain given in argument.
772 Not
.__doc
__ = Domain
.complement
.__doc
__