messy and needs reformatting but returning correct values for now
[linpy.git] / pypol / isl.py
1 import ctypes, ctypes.util
2
3 from . import _isl
4
5
6 __all__ = [
7 'Context',
8 'BasicSet',
9 ]
10
11
12 libisl = ctypes.CDLL(ctypes.util.find_library('isl'))
13
14 libisl.isl_printer_get_str.restype = ctypes.c_char_p
15 libisl.isl_dim_set = _isl.dim_set
16
17
18 class IslObject:
19
20 __slots__ = (
21 '_ptr',
22 )
23
24 def __init__(self, ptr):
25 self._ptr = ptr
26
27 @property
28 def _as_parameter_(self):
29 return self._ptr
30
31
32 class Context(IslObject):
33
34 def __init__(self):
35 ptr = libisl.isl_ctx_alloc()
36 super().__init__(ptr)
37
38 #comment out so does not delete itself after being created
39 #def __del__(self):
40 # libisl.isl_ctx_free(self)
41
42 def __eq__(self, other):
43 if not isinstance(other, Context):
44 return False
45 return self._ptr == other._ptr
46
47
48 class BasicSet(IslObject):
49
50 def __str__(self):
51 ls = libisl.isl_basic_set_get_local_space(self)
52 ctx = libisl.isl_local_space_get_ctx(ls)
53 p = libisl.isl_printer_to_str(ctx)
54 p = libisl.isl_printer_print_basic_set(p, self)
55 string = libisl.isl_printer_get_str(p).decode()
56 return string
57
58 def __del__(self):
59 libisl.isl_basic_set_free(self)
60
61 def constraints(self):
62 return _isl.basic_set_constraints(self._ptr)
63
64 def _fromisl(self, cls, symbols):
65 constraints = self.constraints()
66 equalities = []
67 inequalities = []
68 co = []
69 eq_string = ""
70 in_string = ""
71 string = ""
72 for constraint in constraints:
73 ls = libisl.isl_basic_set_get_local_space(self)
74 ctx = libisl.isl_local_space_get_ctx(ls)
75 p = libisl.isl_printer_to_str(ctx)
76 if libisl.isl_constraint_is_equality(constraint): #check if equality
77 constant = libisl.isl_constraint_get_constant_val(constraint)
78 const = libisl.isl_printer_print_val(p, constant)
79 const = libisl.isl_printer_get_str(const).decode()
80 const = int(const)
81 libisl.isl_printer_free(p)
82 for symbol in symbols:
83 p = libisl.isl_printer_to_str(ctx)
84 dim = symbols.index(symbol)
85 coefficient = libisl.isl_constraint_get_coefficient_val(constraint, libisl.isl_dim_set, dim)
86 coeff = libisl.isl_printer_print_val(p, coefficient)
87 coeff = libisl.isl_printer_get_str(coeff).decode()
88 coeff = int(coeff)
89 if coeff!=0:
90 co.append('{}{}'.format(coeff, symbols[dim]))
91 for value in co:
92 string += '{}+'.format(value)
93 equalities.append('{}{}==0'.format(string, const))
94 co = []
95 string = ''
96 libisl.isl_printer_free(p)
97 else: #same for inequality
98 constant = libisl.isl_constraint_get_constant_val(constraint)
99 const = libisl.isl_printer_print_val(p, constant)
100 const = libisl.isl_printer_get_str(const).decode()
101 const = int(const)
102 libisl.isl_printer_free(p)
103 for symbol in symbols:
104 p = libisl.isl_printer_to_str(ctx)
105 dim = symbols.index(symbol)
106 coefficient = libisl.isl_constraint_get_coefficient_val(constraint, libisl.isl_dim_set, dim)
107 coeff = libisl.isl_printer_print_val(p, coefficient)
108 coeff = libisl.isl_printer_get_str(coeff).decode()
109 coeff = int(coeff)
110 if coeff!=0:
111 co.append('{}{}'.format(coeff, symbols[dim]))
112 for value in co:
113 string += '{} + '.format(value)
114 inequalities.append('{}{} <= 0'.format(string, const))
115 co = []
116 string = ""
117 libisl.isl_printer_free(p)
118
119 for equations in equalities:
120 eq_string += ' {}'.format(equations)
121 eq_strings = eq_string.split()
122 print(eq_strings)
123
124 for equations in inequalities:
125 in_string += ', {}'.format(equations)
126 print(in_string)
127 if eq_string and in_string:
128 final = '{}, {}'.format(eq_string, in_string)
129 elif eq_string != '':
130 final = '{}'.format(eq_strings)
131 elif in_string != '' :
132 final = '{}'.format(in_string)
133
134
135 return ('{}({!r})'.format(cls.__name__,final))
136