from pypol import *
-def affine_derivative_closure(T, x0s):
+def affine_derivative_closure(T, x0s, xs):
- xs = [Symbol("{}'".format(x0.name)) for x0 in x0s]
- dxs = [Symbol('d{}'.format(x0.name)) for x0 in x0s]
- k = Symbol('k')
+ dxs = [x0.asdummy() for x0 in x0s]
+ k = Dummy('k')
for x in T.symbols:
assert x in x0s + xs
- for dx in dxs:
- assert dx.name not in T.symbols
- assert k.name not in T.symbols
T0 = T
T = Eq(i, i0 + 2) & Eq(j, j0 + 1)
print('T =', T)
-Tstar = affine_derivative_closure(T, [i0, j0])
+Tstar = affine_derivative_closure(T, [i0, j0], [i, j])
print('T* =', Tstar)
A polyhedral library based on ISL.
"""
-from .linexprs import Expression, Symbol, symbols, Rational
+from .linexprs import Expression, Symbol, symbols, Dummy, Rational
from .polyhedra import Polyhedron, Eq, Ne, Le, Lt, Ge, Gt, Ne, Empty, Universe
from .domains import Domain, And, Or, Not
__all__ = [
- 'Expression', 'Symbol', 'symbols', 'Rational',
+ 'Expression', 'Symbol', 'symbols', 'Dummy', 'Rational',
'Polyhedron', 'Eq', 'Ne', 'Le', 'Lt', 'Ge', 'Gt', 'Empty', 'Universe',
'Domain', 'And', 'Or', 'Not',
]
__all__ = [
'Expression',
- 'Symbol', 'symbols',
+ 'Symbol', 'symbols', 'Dummy',
'Rational',
]
return self._name
def __hash__(self):
- return hash(self._name)
+ return hash(self.sortkey())
def coefficient(self, symbol):
if not isinstance(symbol, Symbol):
yield 1
def __eq__(self, other):
- return isinstance(other, Symbol) and self.name == other.name
+ return not isinstance(other, Dummy) and isinstance(other, Symbol) \
+ and self.name == other.name
+
+ def asdummy(self):
+ return Dummy(self.name)
@classmethod
def _fromast(cls, node):
return tuple(Symbol(name) for name in names)
+class Dummy(Symbol):
+
+ __slots__ = (
+ '_name',
+ '_index',
+ )
+
+ _count = 0
+
+ def __new__(cls, name=None):
+ if name is None:
+ name = 'Dummy_{}'.format(Dummy._count)
+ self = object().__new__(cls)
+ self._name = name.strip()
+ self._index = Dummy._count
+ Dummy._count += 1
+ return self
+
+ def __hash__(self):
+ return hash(self.sortkey())
+
+ def sortkey(self):
+ return self._name, self._index
+
+ def __eq__(self, other):
+ return isinstance(other, Dummy) and self._index == other._index
+
+
class Rational(Expression):
__slots__ = (
self.assertListEqual(list(symbols(['x', 'y'])), [self.x, self.y])
+class TestDummy(unittest.TestCase):
+
+ def setUp(self):
+ self.x = Dummy('x')
+
+ def test_new(self):
+ self.assertEqual(self.x.name, 'x')
+ self.assertTrue(Dummy().name.startswith('Dummy'))
+
+ def test_eq(self):
+ self.assertEqual(self.x, self.x)
+ self.assertNotEqual(self.x, Symbol('x'))
+ self.assertNotEqual(Symbol('x'), self.x)
+ self.assertNotEqual(self.x, Dummy('x'))
+ self.assertNotEqual(Dummy(), Dummy())
+
+
class TestRational(unittest.TestCase):
def setUp(self):