messy and needs reformatting but returning correct values for now
[linpy.git] / pypol / isl.py
index ecc32c7..32ce305 100644 (file)
@@ -3,15 +3,23 @@ import ctypes, ctypes.util
 from . import _isl
 
 
 from . import _isl
 
 
+__all__ = [
+    'Context',
+    'BasicSet',
+]
+
+
 libisl = ctypes.CDLL(ctypes.util.find_library('isl'))
 
 libisl.isl_printer_get_str.restype = ctypes.c_char_p
 libisl = ctypes.CDLL(ctypes.util.find_library('isl'))
 
 libisl.isl_printer_get_str.restype = ctypes.c_char_p
-libisl.isl_dim_set = _isl.isl_dim_set
+libisl.isl_dim_set = _isl.dim_set
 
 
 class IslObject:
 
 
 
 class IslObject:
 
-    __slots__ = ('_ptr')
+    __slots__ = (
+        '_ptr',
+    )
 
     def __init__(self, ptr):
         self._ptr = ptr
 
     def __init__(self, ptr):
         self._ptr = ptr
@@ -49,3 +57,80 @@ class BasicSet(IslObject):
 
     def __del__(self):
         libisl.isl_basic_set_free(self)
 
     def __del__(self):
         libisl.isl_basic_set_free(self)
+
+    def constraints(self):
+        return _isl.basic_set_constraints(self._ptr)
+        
+    def _fromisl(self, cls, symbols):
+        constraints = self.constraints()
+        equalities = []
+        inequalities = []
+        co = []
+        eq_string = ""
+        in_string = ""
+        string = ""
+        for constraint in constraints:
+            ls = libisl.isl_basic_set_get_local_space(self)
+            ctx = libisl.isl_local_space_get_ctx(ls)
+            p = libisl.isl_printer_to_str(ctx)
+            if libisl.isl_constraint_is_equality(constraint): #check if equality
+                constant = libisl.isl_constraint_get_constant_val(constraint)
+                const = libisl.isl_printer_print_val(p, constant)
+                const = libisl.isl_printer_get_str(const).decode()
+                const = int(const)
+                libisl.isl_printer_free(p)
+                for symbol in symbols:
+                    p = libisl.isl_printer_to_str(ctx)
+                    dim = symbols.index(symbol)
+                    coefficient = libisl.isl_constraint_get_coefficient_val(constraint, libisl.isl_dim_set, dim)
+                    coeff = libisl.isl_printer_print_val(p, coefficient)
+                    coeff = libisl.isl_printer_get_str(coeff).decode()
+                    coeff = int(coeff)
+                    if coeff!=0:
+                        co.append('{}{}'.format(coeff, symbols[dim])) 
+                        for value in co:
+                            string += '{}+'.format(value)
+                        equalities.append('{}{}==0'.format(string, const))
+                        co = []
+                        string = ''                     
+                    libisl.isl_printer_free(p)
+            else: #same for inequality
+                constant = libisl.isl_constraint_get_constant_val(constraint)
+                const = libisl.isl_printer_print_val(p, constant)
+                const = libisl.isl_printer_get_str(const).decode()
+                const = int(const)
+                libisl.isl_printer_free(p)
+                for symbol in symbols:
+                    p = libisl.isl_printer_to_str(ctx)
+                    dim = symbols.index(symbol)
+                    coefficient = libisl.isl_constraint_get_coefficient_val(constraint, libisl.isl_dim_set, dim)
+                    coeff = libisl.isl_printer_print_val(p, coefficient)
+                    coeff = libisl.isl_printer_get_str(coeff).decode()
+                    coeff = int(coeff)
+                    if coeff!=0:
+                        co.append('{}{}'.format(coeff, symbols[dim])) 
+                        for value in co:
+                            string += '{} + '.format(value)
+                        inequalities.append('{}{} <= 0'.format(string, const))
+                        co = []
+                        string = ""
+                    libisl.isl_printer_free(p)
+                    
+        for equations in equalities:
+            eq_string += ' {}'.format(equations)
+            eq_strings = eq_string.split()
+        print(eq_strings)
+        
+        for equations in inequalities:
+            in_string += ', {}'.format(equations)
+        print(in_string)
+        if eq_string and in_string:
+            final = '{}, {}'.format(eq_string, in_string)
+        elif eq_string != '':
+            final = '{}'.format(eq_strings)
+        elif in_string != '' :
+            final = '{}'.format(in_string)
+            
+                               
+        return ('{}({!r})'.format(cls.__name__,final))  
+