Add GPL License
[linpy.git] / examples / nsad2010.py
1 #!/usr/bin/env python3
2
3 """
4 This file is part of Linpy.
5
6 Linpy is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 Linpy is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Linpy. If not, see <http://www.gnu.org/licenses/>.
18 """
19
20 from pypol import *
21
22
23 class Transformer:
24
25 def __new__(cls, polyhedron, range_symbols, domain_symbols):
26 self = object().__new__(cls)
27 self.polyhedron = polyhedron
28 self.range_symbols = range_symbols
29 self.domain_symbols = domain_symbols
30 return self
31
32 @property
33 def symbols(self):
34 return self.range_symbols + self.domain_symbols
35
36 def star(self):
37 delta_symbols = [symbol.asdummy() for symbol in self.range_symbols]
38 k = Dummy('k')
39 polyhedron = self.polyhedron
40 for x, xprime, dx in zip(self.range_symbols, self.domain_symbols, delta_symbols):
41 polyhedron &= Eq(dx, xprime - x)
42 polyhedron = polyhedron.project(self.symbols)
43 equalities, inequalities = [], []
44 for equality in polyhedron.equalities:
45 equality += (k-1) * equality.constant
46 equalities.append(equality)
47 for inequality in polyhedron.inequalities:
48 inequality += (k-1) * inequality.constant
49 inequalities.append(inequality)
50 polyhedron = Polyhedron(equalities, inequalities) & Ge(k, 0)
51 polyhedron = polyhedron.project([k])
52 for x, xprime, dx in zip(self.range_symbols, self.domain_symbols, delta_symbols):
53 polyhedron &= Eq(dx, xprime - x)
54 polyhedron = polyhedron.project(delta_symbols)
55 return Transformer(polyhedron, self.range_symbols, self.domain_symbols)
56
57
58 if __name__ == '__main__':
59 i, iprime, j, jprime = symbols("i i' j j'")
60 transformer = Transformer(Eq(iprime, i + 2) & Eq(jprime, j + 1),
61 [i, j], [iprime, jprime])
62 print('T =', transformer.polyhedron)
63 print('T* =', transformer.star().polyhedron)
64
65 # Copyright 2014 MINES ParisTech