Rename Constant into Rational
authorVivien Maisonneuve <v.maisonneuve@gmail.com>
Wed, 2 Jul 2014 20:30:43 +0000 (22:30 +0200)
committerVivien Maisonneuve <v.maisonneuve@gmail.com>
Wed, 2 Jul 2014 20:30:43 +0000 (22:30 +0200)
pypol/__init__.py
pypol/domains.py
pypol/linexprs.py
pypol/polyhedra.py
pypol/tests/test_linexprs.py

index fc70bea..89f80fc 100644 (file)
@@ -2,13 +2,13 @@
 A polyhedral library based on ISL.
 """
 
 A polyhedral library based on ISL.
 """
 
-from .linexprs import Expression, Constant, Symbol, symbols
+from .linexprs import Expression, Symbol, symbols, Rational
 from .polyhedra import Polyhedron, Eq, Ne, Le, Lt, Ge, Gt, Ne, Empty, Universe
 from .domains import Domain, And, Or, Not
 
 
 __all__ = [
 from .polyhedra import Polyhedron, Eq, Ne, Le, Lt, Ge, Gt, Ne, Empty, Universe
 from .domains import Domain, And, Or, Not
 
 
 __all__ = [
-    'Expression', 'Constant', 'Symbol', 'symbols',
+    'Expression', 'Symbol', 'symbols', 'Rational',
     'Polyhedron', 'Eq', 'Ne', 'Le', 'Lt', 'Ge', 'Gt', 'Empty', 'Universe',
     'Domain', 'And', 'Or', 'Not',
 ]
     'Polyhedron', 'Eq', 'Ne', 'Le', 'Lt', 'Ge', 'Gt', 'Empty', 'Universe',
     'Domain', 'And', 'Or', 'Not',
 ]
index 9187081..abc2d91 100644 (file)
@@ -340,6 +340,7 @@ class Domain:
     def tosympy(self):
         raise NotImplementedError
 
     def tosympy(self):
         raise NotImplementedError
 
+
 def And(*domains):
     if len(domains) == 0:
         from .polyhedra import Universe
 def And(*domains):
     if len(domains) == 0:
         from .polyhedra import Universe
index ccd1564..9a1ed64 100644 (file)
@@ -10,7 +10,7 @@ from fractions import Fraction, gcd
 __all__ = [
     'Expression',
     'Symbol', 'symbols',
 __all__ = [
     'Expression',
     'Symbol', 'symbols',
-    'Constant',
+    'Rational',
 ]
 
 
 ]
 
 
@@ -20,7 +20,7 @@ def _polymorphic(func):
         if isinstance(right, Expression):
             return func(left, right)
         elif isinstance(right, numbers.Rational):
         if isinstance(right, Expression):
             return func(left, right)
         elif isinstance(right, numbers.Rational):
-            right = Constant(right)
+            right = Rational(right)
             return func(left, right)
         return NotImplemented
     return wrapper
             return func(left, right)
         return NotImplemented
     return wrapper
@@ -44,7 +44,7 @@ class Expression:
                 raise TypeError('too many arguments')
             return Expression.fromstring(coefficients)
         if coefficients is None:
                 raise TypeError('too many arguments')
             return Expression.fromstring(coefficients)
         if coefficients is None:
-            return Constant(constant)
+            return Rational(constant)
         if isinstance(coefficients, dict):
             coefficients = coefficients.items()
         for symbol, coefficient in coefficients:
         if isinstance(coefficients, dict):
             coefficients = coefficients.items()
         for symbol, coefficient in coefficients:
@@ -53,7 +53,7 @@ class Expression:
         coefficients = [(symbol, coefficient)
             for symbol, coefficient in coefficients if coefficient != 0]
         if len(coefficients) == 0:
         coefficients = [(symbol, coefficient)
             for symbol, coefficient in coefficients if coefficient != 0]
         if len(coefficients) == 0:
-            return Constant(constant)
+            return Rational(constant)
         if len(coefficients) == 1 and constant == 0:
             symbol, coefficient = coefficients[0]
             if coefficient == 1:
         if len(coefficients) == 1 and constant == 0:
             symbol, coefficient = coefficients[0]
             if coefficient == 1:
@@ -62,17 +62,17 @@ class Expression:
         self._coefficients = OrderedDict()
         for symbol, coefficient in sorted(coefficients,
                 key=lambda item: item[0].name):
         self._coefficients = OrderedDict()
         for symbol, coefficient in sorted(coefficients,
                 key=lambda item: item[0].name):
-            if isinstance(coefficient, Constant):
+            if isinstance(coefficient, Rational):
                 coefficient = coefficient.constant
             if not isinstance(coefficient, numbers.Rational):
                 raise TypeError('coefficients must be rational numbers '
                 coefficient = coefficient.constant
             if not isinstance(coefficient, numbers.Rational):
                 raise TypeError('coefficients must be rational numbers '
-                    'or Constant instances')
+                    'or Rational instances')
             self._coefficients[symbol] = coefficient
             self._coefficients[symbol] = coefficient
-        if isinstance(constant, Constant):
+        if isinstance(constant, Rational):
             constant = constant.constant
         if not isinstance(constant, numbers.Rational):
             raise TypeError('constant must be a rational number '
             constant = constant.constant
         if not isinstance(constant, numbers.Rational):
             raise TypeError('constant must be a rational number '
-                'or a Constant instance')
+                'or a Rational instance')
         self._constant = constant
         self._symbols = tuple(self._coefficients)
         self._dimension = len(self._symbols)
         self._constant = constant
         self._symbols = tuple(self._coefficients)
         self._dimension = len(self._symbols)
@@ -127,7 +127,7 @@ class Expression:
 
     @_polymorphic
     def __add__(self, other):
 
     @_polymorphic
     def __add__(self, other):
-        coefficients = defaultdict(Constant, self.coefficients())
+        coefficients = defaultdict(Rational, self.coefficients())
         for symbol, coefficient in other.coefficients():
             coefficients[symbol] += coefficient
         constant = self.constant + other.constant
         for symbol, coefficient in other.coefficients():
             coefficients[symbol] += coefficient
         constant = self.constant + other.constant
@@ -137,7 +137,7 @@ class Expression:
 
     @_polymorphic
     def __sub__(self, other):
 
     @_polymorphic
     def __sub__(self, other):
-        coefficients = defaultdict(Constant, self.coefficients())
+        coefficients = defaultdict(Rational, self.coefficients())
         for symbol, coefficient in other.coefficients():
             coefficients[symbol] -= coefficient
         constant = self.constant - other.constant
         for symbol, coefficient in other.coefficients():
             coefficients[symbol] -= coefficient
         constant = self.constant - other.constant
@@ -166,8 +166,8 @@ class Expression:
         if other.isconstant():
             coefficients = dict(self.coefficients())
             for symbol in coefficients:
         if other.isconstant():
             coefficients = dict(self.coefficients())
             for symbol in coefficients:
-                coefficients[symbol] = Constant(coefficients[symbol], other.constant)
-            constant = Constant(self.constant, other.constant)
+                coefficients[symbol] = Rational(coefficients[symbol], other.constant)
+            constant = Rational(self.constant, other.constant)
             return Expression(coefficients, constant)
         if isinstance(other, Expression):
             raise ValueError('non-linear expression: '
             return Expression(coefficients, constant)
         if isinstance(other, Expression):
             raise ValueError('non-linear expression: '
@@ -177,7 +177,7 @@ class Expression:
     def __rtruediv__(self, other):
         if isinstance(other, self):
             if self.isconstant():
     def __rtruediv__(self, other):
         if isinstance(other, self):
             if self.isconstant():
-                return Constant(other, self.constant)
+                return Rational(other, self.constant)
             else:
                 raise ValueError('non-linear expression: '
                         '{} / {}'.format(other._parenstr(), self._parenstr()))
             else:
                 raise ValueError('non-linear expression: '
                         '{} / {}'.format(other._parenstr(), self._parenstr()))
@@ -242,7 +242,7 @@ class Expression:
         elif isinstance(node, ast.Name):
             return Symbol(node.id)
         elif isinstance(node, ast.Num):
         elif isinstance(node, ast.Name):
             return Symbol(node.id)
         elif isinstance(node, ast.Num):
-            return Constant(node.n)
+            return Rational(node.n)
         elif isinstance(node, ast.UnaryOp) and isinstance(node.op, ast.USub):
             return -cls._fromast(node.operand)
         elif isinstance(node, ast.BinOp):
         elif isinstance(node, ast.UnaryOp) and isinstance(node.op, ast.USub):
             return -cls._fromast(node.operand)
         elif isinstance(node, ast.BinOp):
@@ -414,7 +414,7 @@ def symbols(names):
     return tuple(Symbol(name) for name in names)
 
 
     return tuple(Symbol(name) for name in names)
 
 
-class Constant(Expression):
+class Rational(Expression):
 
     __slots__ = (
         '_constant',
 
     __slots__ = (
         '_constant',
@@ -422,7 +422,7 @@ class Constant(Expression):
 
     def __new__(cls, numerator=0, denominator=None):
         self = object().__new__(cls)
 
     def __new__(cls, numerator=0, denominator=None):
         self = object().__new__(cls)
-        if denominator is None and isinstance(numerator, Constant):
+        if denominator is None and isinstance(numerator, Rational):
             self._constant = numerator.constant
         else:
             self._constant = Fraction(numerator, denominator)
             self._constant = numerator.constant
         else:
             self._constant = Fraction(numerator, denominator)
@@ -455,7 +455,7 @@ class Constant(Expression):
 
     @_polymorphic
     def __eq__(self, other):
 
     @_polymorphic
     def __eq__(self, other):
-        return isinstance(other, Constant) and self.constant == other.constant
+        return isinstance(other, Rational) and self.constant == other.constant
 
     def __bool__(self):
         return self.constant != 0
 
     def __bool__(self):
         return self.constant != 0
@@ -464,14 +464,14 @@ class Constant(Expression):
     def fromstring(cls, string):
         if not isinstance(string, str):
             raise TypeError('string must be a string instance')
     def fromstring(cls, string):
         if not isinstance(string, str):
             raise TypeError('string must be a string instance')
-        return Constant(Fraction(string))
+        return Rational(Fraction(string))
 
     @classmethod
     def fromsympy(cls, expr):
         import sympy
         if isinstance(expr, sympy.Rational):
 
     @classmethod
     def fromsympy(cls, expr):
         import sympy
         if isinstance(expr, sympy.Rational):
-            return Constant(expr.p, expr.q)
+            return Rational(expr.p, expr.q)
         elif isinstance(expr, numbers.Rational):
         elif isinstance(expr, numbers.Rational):
-            return Constant(expr)
+            return Rational(expr)
         else:
             raise TypeError('expr must be a sympy.Rational instance')
         else:
             raise TypeError('expr must be a sympy.Rational instance')
index 6ef7cc1..fa2a5c6 100644 (file)
@@ -4,7 +4,7 @@ import numbers
 from . import islhelper
 
 from .islhelper import mainctx, libisl
 from . import islhelper
 
 from .islhelper import mainctx, libisl
-from .linexprs import Expression, Constant
+from .linexprs import Expression, Rational
 from .domains import Domain
 
 
 from .domains import Domain
 
 
@@ -219,12 +219,12 @@ def _polymorphic(func):
     @functools.wraps(func)
     def wrapper(left, right):
         if isinstance(left, numbers.Rational):
     @functools.wraps(func)
     def wrapper(left, right):
         if isinstance(left, numbers.Rational):
-            left = Constant(left)
+            left = Rational(left)
         elif not isinstance(left, Expression):
             raise TypeError('left must be a a rational number '
                 'or a linear expression')
         if isinstance(right, numbers.Rational):
         elif not isinstance(left, Expression):
             raise TypeError('left must be a a rational number '
                 'or a linear expression')
         if isinstance(right, numbers.Rational):
-            right = Constant(right)
+            right = Rational(right)
         elif not isinstance(right, Expression):
             raise TypeError('right must be a a rational number '
                 'or a linear expression')
         elif not isinstance(right, Expression):
             raise TypeError('right must be a a rational number '
                 'or a linear expression')
index 8dfd13e..adfec7d 100644 (file)
@@ -20,9 +20,9 @@ class TestExpression(unittest.TestCase):
 
     def test_new(self):
         self.assertIsInstance(self.x, Symbol)
 
     def test_new(self):
         self.assertIsInstance(self.x, Symbol)
-        self.assertIsInstance(self.pi, Constant)
+        self.assertIsInstance(self.pi, Rational)
         self.assertNotIsInstance(self.x + self.pi, Symbol)
         self.assertNotIsInstance(self.x + self.pi, Symbol)
-        self.assertNotIsInstance(self.x + self.pi, Constant)
+        self.assertNotIsInstance(self.x + self.pi, Rational)
         xx = Expression({self.x: 2})
         self.assertNotIsInstance(xx, Symbol)
         with self.assertRaises(TypeError):
         xx = Expression({self.x: 2})
         self.assertNotIsInstance(xx, Symbol)
         with self.assertRaises(TypeError):
@@ -231,18 +231,18 @@ class TestSymbol(unittest.TestCase):
         self.assertListEqual(list(symbols(['x', 'y'])), [self.x, self.y])
 
 
         self.assertListEqual(list(symbols(['x', 'y'])), [self.x, self.y])
 
 
-class TestConstant(unittest.TestCase):
+class TestRational(unittest.TestCase):
 
     def setUp(self):
 
     def setUp(self):
-        self.zero = Constant(0)
-        self.one = Constant(1)
-        self.pi = Constant(Fraction(22, 7))
+        self.zero = Rational(0)
+        self.one = Rational(1)
+        self.pi = Rational(Fraction(22, 7))
 
     def test_new(self):
 
     def test_new(self):
-        self.assertEqual(Constant(), self.zero)
-        self.assertEqual(Constant(1), self.one)
-        self.assertEqual(Constant(self.pi), self.pi)
-        self.assertEqual(Constant('22/7'), self.pi)
+        self.assertEqual(Rational(), self.zero)
+        self.assertEqual(Rational(1), self.one)
+        self.assertEqual(Rational(self.pi), self.pi)
+        self.assertEqual(Rational('22/7'), self.pi)
 
     def test_isconstant(self):
         self.assertTrue(self.zero.isconstant())
 
     def test_isconstant(self):
         self.assertTrue(self.zero.isconstant())
@@ -252,11 +252,11 @@ class TestConstant(unittest.TestCase):
         self.assertTrue(self.pi)
 
     def test_fromstring(self):
         self.assertTrue(self.pi)
 
     def test_fromstring(self):
-        self.assertEqual(Constant.fromstring('22/7'), self.pi)
+        self.assertEqual(Rational.fromstring('22/7'), self.pi)
         with self.assertRaises(ValueError):
         with self.assertRaises(ValueError):
-            Constant.fromstring('a')
+            Rational.fromstring('a')
         with self.assertRaises(TypeError):
         with self.assertRaises(TypeError):
-            Constant.fromstring(1)
+            Rational.fromstring(1)
 
     def test_repr(self):
         self.assertEqual(repr(self.zero), '0')
 
     def test_repr(self):
         self.assertEqual(repr(self.zero), '0')
@@ -266,6 +266,6 @@ class TestConstant(unittest.TestCase):
     @requires_sympy
     def test_fromsympy(self):
         import sympy
     @requires_sympy
     def test_fromsympy(self):
         import sympy
-        self.assertEqual(Constant.fromsympy(sympy.Rational(22, 7)), self.pi)
+        self.assertEqual(Rational.fromsympy(sympy.Rational(22, 7)), self.pi)
         with self.assertRaises(TypeError):
         with self.assertRaises(TypeError):
-            Constant.fromsympy(sympy.Symbol('x'))
+            Rational.fromsympy(sympy.Symbol('x'))