messy and needs reformatting but returning correct values for now
[linpy.git] / pypol / isl.py
index 5a20514..32ce305 100644 (file)
@@ -1,27 +1,25 @@
-"""
-note: for islpy
-isl format: basic set: ("{[x, y] : x >= 0 and x < 5 and y >= 0 and y < x+4 }")
-"""
-
 import ctypes, ctypes.util
-import functools
-import math
-import numbers
-import operator
-import re
 
 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.isl_dim_set = _isl.isl_dim_set
+libisl.isl_dim_set = _isl.dim_set
 
 
 class IslObject:
 
-    __slots__ = ('_ptr')
+    __slots__ = (
+        '_ptr',
+    )
 
     def __init__(self, ptr):
         self._ptr = ptr
@@ -34,7 +32,8 @@ class IslObject:
 class Context(IslObject):
 
     def __init__(self):
-        self._ptr = libisl.isl_ctx_alloc()
+        ptr = libisl.isl_ctx_alloc()
+        super().__init__(ptr)
 
     #comment out so does not delete itself after being created
     #def __del__(self):
@@ -53,6 +52,85 @@ class BasicSet(IslObject):
         ctx = libisl.isl_local_space_get_ctx(ls)
         p = libisl.isl_printer_to_str(ctx)
         p = libisl.isl_printer_print_basic_set(p, self)
-        string = libisl.isl_printer_get_str(p)
-        string = str(string.decode())
+        string = libisl.isl_printer_get_str(p).decode()
         return string
+
+    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))  
+