Skip to content

Commit ac77bc5

Browse files
committed
add poly_con_rel and poly_gen_rel; fix coefficents calling in generators
1 parent cbc48b8 commit ac77bc5

4 files changed

Lines changed: 93 additions & 38 deletions

File tree

pplite/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
- Acadia Larsen (2024): initial version.
1111
"""
1212

13-
__version__ = "0.0.52"
13+
__version__ = "0.0.53"
1414

1515
from .linear_algebra import (
1616
Variable, Linear_Expression, Affine_Expression
@@ -33,5 +33,5 @@
3333
)
3434

3535
from .polyhedron import (
36-
NNC_Polyhedron, Polyhedron_Constraint_Rel
36+
NNC_Polyhedron, Polyhedron_Constraint_Rel, Polyhedron_Generator_Rel
3737
)

pplite/generators.pyx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,12 +319,19 @@ cdef class PPliteGenerator(object):
319319
>>> g = PPliteGenerator('point', e, 7)
320320
>>> g.coefficient(x)
321321
mpz(2)
322+
>>> g.coefficient(0)
323+
mpz(2)
324+
>>> g.coefficient(1)
325+
mpz(3)
326+
>>> g.coefficient(3)
327+
mpz(0)
322328
"""
323329
cdef Var* vv
324330
if isinstance(v, Variable):
325331
vv = (<Variable> v).thisptr
326332
else:
327-
vv = (<Variable> Variable(v)).thisptr
333+
var = Variable(v)
334+
vv = (<Variable> var).thisptr
328335
cdef FLINT_Integer n
329336
n = self.thisptr.coeff(vv[0])
330337
return FLINT_Integer_to_Python(n)

pplite/polyhedron.pyx

Lines changed: 72 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,10 @@ cdef class NNC_Polyhedron(object):
302302
cc = (<Constraint> constraint).thisptr[0]
303303
p_c_r = self.thisptr[0].relation_with(cc)
304304
result = Polyhedron_Constraint_Rel()
305-
result.thisptr[0] = p_c_r
305+
result.thisptr = new Poly_Con_Rel(p_c_r)
306306
return result
307307
else:
308-
raise TypeError()
308+
raise TypeError("Here is a useful error")
309309

310310
def _relation_with_g(self, generator):
311311
cdef Gen gg
@@ -314,7 +314,7 @@ cdef class NNC_Polyhedron(object):
314314
gg = (<PPliteGenerator> generator).thisptr[0]
315315
p_g_r = self.thisptr[0].relation_with(gg)
316316
result = Polyhedron_Generator_Rel()
317-
result.thisptr[0] = p_g_r
317+
result.thisptr = new Poly_Gen_Rel(p_g_r)
318318
return result
319319
else:
320320
raise TypeError("A :class:`PPliteGenerator` or a :class:`Constraint` should be passed into this method.")
@@ -530,7 +530,7 @@ cdef class NNC_Polyhedron(object):
530530
>>> A = Variable(0)
531531
>>> B = Variable(1)
532532
>>> cons_list = [A >= 0, B == 5]
533-
>>> P = NNC_Polyhedron(dim_type = 2, spec_elem = "universe", topology = "nnc") #TODO: Make nicer python constructors
533+
>>> P = NNC_Polyhedron(dim_type = 2, spec_elem = "universe", topology = "nnc")
534534
>>> P.add_constraints(cons_list)
535535
>>> P_2 = NNC_Polyhedron(dim_type = 2, spec_elem = "universe", topology = "nnc")
536536
>>> P_2.add_constraint(A >= 0)
@@ -557,7 +557,7 @@ cdef class NNC_Polyhedron(object):
557557
>>> P_2.add_constraint(A >= 0)
558558
>>> P_2.add_constraint(B >= 0)
559559
>>> P_2.minimize()
560-
>>> P == P_2 # Test 01 finished
560+
>>> P == P_2
561561
True
562562
>>> P = NNC_Polyhedron(dim_type = 2, spec_elem = "universe", topology = "nnc")
563563
>>> P.add_constraint(A >= 0)
@@ -566,7 +566,7 @@ cdef class NNC_Polyhedron(object):
566566
>>> P.add_generator(Ray(-A))
567567
>>> P_2 = NNC_Polyhedron(dim_type = 2, spec_elem = "universe", topology = "nnc")
568568
>>> P_2.add_constraint(B >= 0)
569-
>>> P == P_2 # Test 03 finished
569+
>>> P == P_2
570570
True
571571
"""
572572
if isinstance(generator, PPliteGenerator):
@@ -672,8 +672,6 @@ cdef class NNC_Polyhedron(object):
672672
### Poly_Con_Rel and Poly_Gen_Rel ###
673673
#####################################
674674

675-
# TODO: Add full functionality of these classes.
676-
677675
cdef class Polyhedron_Constraint_Rel(object):
678676
def __cinit__(self):
679677
self.thisptr = NULL
@@ -702,33 +700,79 @@ cdef class Polyhedron_Constraint_Rel(object):
702700
>>> from pplite import Polyhedron_Constraint_Rel
703701
>>> Polyhedron_Constraint_Rel.nothing()
704702
nothing
705-
"""
706-
cdef Poly_Con_Rel rel = PPlite_NOTHING
707-
cdef Poly_Con_Rel * rel_pointer
708-
sig_on()
709-
try:
710-
rel_pointer = &rel
711-
relation = Polyhedron_Constraint_Rel()
712-
relation.thisptr = rel_pointer
713-
finally:
714-
sig_off()
703+
"""
704+
relation = Polyhedron_Constraint_Rel()
705+
relation.thisptr = new Poly_Con_Rel(PPlite_NOTHING())
715706
return relation
707+
708+
@classmethod
716709
def is_disjoint(cls):
717-
pass
710+
relation = Polyhedron_Constraint_Rel()
711+
relation.thisptr = new Poly_Con_Rel(PPlite_IS_DISJOINT())
712+
return relation
713+
714+
@classmethod
718715
def strictly_intersects(cls):
719-
pass
716+
relation = Polyhedron_Constraint_Rel()
717+
relation.thisptr = new Poly_Con_Rel(PPlite_STRICTLY_INTERSECTS())
718+
return relation
719+
720+
@classmethod
720721
def is_included(cls):
721-
pass
722+
relation = Polyhedron_Constraint_Rel()
723+
relation.thisptr = new Poly_Con_Rel(PPlite_IS_INCLUDED())
724+
return relation
725+
726+
@classmethod
722727
def saturates(cls):
723-
pass
724-
def implies(self, other):
725-
pass
728+
relation = Polyhedron_Constraint_Rel()
729+
relation.thisptr = new Poly_Con_Rel(PPlite_SATURATES())
730+
return relation
731+
732+
def implies(self, Polyhedron_Constraint_Rel y):
733+
return self.thisptr.implies(y.thisptr[0])
734+
726735

727736
cdef class Polyhedron_Generator_Rel(object):
728737
def __cinit__(self):
729738
self.thisptr = NULL
730739
def __dealloc__(self):
731740
del self.thisptr
741+
def __repr__(self):
742+
rel = []
743+
if self.implies(Polyhedron_Constraint_Rel.is_disjoint()):
744+
rel.append('is_disjoint')
745+
if self.implies(Polyhedron_Constraint_Rel.strictly_intersects()):
746+
rel.append('strictly_intersects')
747+
if self.implies(Polyhedron_Constraint_Rel.is_included()):
748+
rel.append('is_included')
749+
if self.implies(Polyhedron_Constraint_Rel.saturates()):
750+
rel.append('saturates')
751+
if rel:
752+
return ', '.join(rel)
753+
else:
754+
return 'nothing'
755+
756+
@classmethod
757+
def nothing(cls):
758+
"""
759+
TESTS::
760+
>>> from pplite import Polyhedron_Generator_Rel
761+
>>> Polyhedron_Generator_Rel.nothing()
762+
nothing
763+
"""
764+
relation = Polyhedron_Generator_Rel()
765+
relation.thisptr = new Poly_Gen_Rel(PPlite_Gen_NOTHING())
766+
return relation
767+
768+
@classmethod
769+
def subsumes(cls):
770+
relation = Polyhedron_Generator_Rel()
771+
relation.thisptr = new Poly_Gen_Rel(PPlite_SUBSUMES())
772+
return relation
773+
774+
def implies(self, Polyhedron_Generator_Rel y):
775+
return self.thisptr.implies(y.thisptr[0])
732776

733777

734778
# TODO Migrate helper functions to a helper function module.
@@ -760,11 +804,11 @@ cdef Spec_Elem string_to_Spec_Elem(s):
760804

761805

762806

763-
cdef Poly_Con_Rel _new_Poly_Con_Rel(s):
764-
if s == "nothing":
765-
return PPlite_NOTHING
807+
# cdef Poly_Con_Rel _new_Poly_Con_Rel(s):
808+
# if s == "nothing":
809+
# return PPlite_NOTHING
766810

767-
raise ValueError("Unrecognized string {0}.".format(s))
811+
# raise ValueError("Unrecognized string {0}.".format(s))
768812

769813
# cdef _new_Poly_Con_Rel_Nothing():
770814
# cdef Poly_Con_Rel rel = Polyhedron_Constraint_Rel()

pplite/pplite_decl.pxd

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,7 @@ cdef extern from "pplite/pplite.hh" namespace "pplite":
693693
cdef cppclass Poly_Con_Rel:
694694
ctypedef unsigned int Impl
695695
Poly_Con_Rel()
696+
Poly_Con_Rel(const Poly_Con_Rel& rel) # Implicit copy constructor
696697
Impl& impl()
697698
Impl impl()
698699
Poly_Con_Rel nothing()
@@ -702,22 +703,25 @@ cdef extern from "pplite/pplite.hh" namespace "pplite":
702703
Poly_Con_Rel saturates()
703704
cppbool implies(const Poly_Con_Rel& y)
704705

706+
cdef Poly_Con_Rel PPlite_NOTHING "pplite::Poly_Con_Rel::nothing"()
707+
cdef Poly_Con_Rel PPlite_IS_DISJOINT "pplite::Poly_Con_Rel::is_disjoint"()
708+
cdef Poly_Con_Rel PPlite_STRICTLY_INTERSECTS "pplite::Poly_Con_Rel::strictly_intersects"()
709+
cdef Poly_Con_Rel PPlite_IS_INCLUDED "pplite::Poly_Con_Rel::is_included"()
710+
cdef Poly_Con_Rel PPlite_SATURATES "pplite::Poly_Con_Rel::saturates"()
711+
705712
cdef cppclass Poly_Gen_Rel:
706713
ctypedef unsigned int Impl
707714
Poly_Gen_Rel()
715+
Poly_Gen_Rel(const Poly_Gen_Rel& rel)
708716
Impl& impl()
709717
Impl impl()
710718
Poly_Gen_Rel nothing()
711719
Poly_Gen_Rel subsumes()
712720
cppbool implies(const Poly_Gen_Rel& y)
713721

714-
Poly_Con_Rel PPlite_NOTHING "pplite::Poly_Con_Rel::nothing()"
715-
# Poly_Con_Rel IS_DISJOINT "Poly::Impl::IS_DISJOINT"
716-
# Poly_Con_Rel STRICTLY_INTERSECTS "Poly::Impl::STRICTLY_INTERSECTS"
717-
# Poly_Con_Rel IS_INCLUDED "Poly::Impl::IS_INCLUDED"
718-
# Poly_Con_Rel SATURATES "Poly::Impl::SATURATES"
719-
# Poly_Con_Rel EVERYTHING "Poly::Impl::EVERYTHING"
720-
# PPLite/U_Poly.hh
722+
cdef Poly_Gen_Rel PPlite_Gen_NOTHING "pplite::Poly_Gen_Rel::nothing"()
723+
cdef Poly_Gen_Rel PPlite_SUBSUMES "pplite::Poly_Gen_Rel::subsumes"()
724+
721725

722726
# cdef cppclass U_Wrap:
723727
# ctypedef struct Cons_Proxy:

0 commit comments

Comments
 (0)