Fix license headers
[linpy.git] / examples / nsad2010.py
1 #!/usr/bin/env python3
2
3 from pypol import *
4
5
6 class Transformer:
7
8 def __new__(cls, polyhedron, range_symbols, domain_symbols):
9 self = object().__new__(cls)
10 self.polyhedron = polyhedron
11 self.range_symbols = range_symbols
12 self.domain_symbols = domain_symbols
13 return self
14
15 @property
16 def symbols(self):
17 return self.range_symbols + self.domain_symbols
18
19 def star(self):
20 delta_symbols = [symbol.asdummy() for symbol in self.range_symbols]
21 k = Dummy('k')
22 polyhedron = self.polyhedron
23 for x, xprime, dx in zip(self.range_symbols, self.domain_symbols, delta_symbols):
24 polyhedron &= Eq(dx, xprime - x)
25 polyhedron = polyhedron.project(self.symbols)
26 equalities, inequalities = [], []
27 for equality in polyhedron.equalities:
28 equality += (k-1) * equality.constant
29 equalities.append(equality)
30 for inequality in polyhedron.inequalities:
31 inequality += (k-1) * inequality.constant
32 inequalities.append(inequality)
33 polyhedron = Polyhedron(equalities, inequalities) & Ge(k, 0)
34 polyhedron = polyhedron.project([k])
35 for x, xprime, dx in zip(self.range_symbols, self.domain_symbols, delta_symbols):
36 polyhedron &= Eq(dx, xprime - x)
37 polyhedron = polyhedron.project(delta_symbols)
38 return Transformer(polyhedron, self.range_symbols, self.domain_symbols)
39
40
41 if __name__ == '__main__':
42 i, iprime, j, jprime = symbols("i i' j j'")
43 transformer = Transformer(Eq(iprime, i + 2) & Eq(jprime, j + 1),
44 [i, j], [iprime, jprime])
45 print('T =', transformer.polyhedron)
46 print('T* =', transformer.star().polyhedron)