#!/usr/bin/env python3
"""
This file is part of Linpy.
Linpy is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Linpy is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Linpy. If not, see .
"""
from pypol import *
class Transformer:
def __new__(cls, polyhedron, range_symbols, domain_symbols):
self = object().__new__(cls)
self.polyhedron = polyhedron
self.range_symbols = range_symbols
self.domain_symbols = domain_symbols
return self
@property
def symbols(self):
return self.range_symbols + self.domain_symbols
def star(self):
delta_symbols = [symbol.asdummy() for symbol in self.range_symbols]
k = Dummy('k')
polyhedron = self.polyhedron
for x, xprime, dx in zip(self.range_symbols, self.domain_symbols, delta_symbols):
polyhedron &= Eq(dx, xprime - x)
polyhedron = polyhedron.project(self.symbols)
equalities, inequalities = [], []
for equality in polyhedron.equalities:
equality += (k-1) * equality.constant
equalities.append(equality)
for inequality in polyhedron.inequalities:
inequality += (k-1) * inequality.constant
inequalities.append(inequality)
polyhedron = Polyhedron(equalities, inequalities) & Ge(k, 0)
polyhedron = polyhedron.project([k])
for x, xprime, dx in zip(self.range_symbols, self.domain_symbols, delta_symbols):
polyhedron &= Eq(dx, xprime - x)
polyhedron = polyhedron.project(delta_symbols)
return Transformer(polyhedron, self.range_symbols, self.domain_symbols)
if __name__ == '__main__':
i, iprime, j, jprime = symbols("i i' j j'")
transformer = Transformer(Eq(iprime, i + 2) & Eq(jprime, j + 1),
[i, j], [iprime, jprime])
print('T =', transformer.polyhedron)
print('T* =', transformer.star().polyhedron)
# Copyright 2014 MINES ParisTech