From: Vivien Maisonneuve Date: Mon, 18 Aug 2014 09:12:27 +0000 (+0200) Subject: Rename Expression class into LinExpr X-Git-Tag: 1.0~67 X-Git-Url: https://scm.cri.ensmp.fr/git/linpy.git/commitdiff_plain/7afcb0a1ded9e9a331b131689d68c085f712143f Rename Expression class into LinExpr --- diff --git a/linpy/__init__.py b/linpy/__init__.py index 0ae2e66..2fdc73e 100644 --- a/linpy/__init__.py +++ b/linpy/__init__.py @@ -20,13 +20,13 @@ A polyhedral library based on ISL """ from .geometry import Point, Vector -from .linexprs import Expression, Symbol, Dummy, symbols, Rational +from .linexprs import LinExpr, Symbol, Dummy, symbols, Rational from .polyhedra import Polyhedron, Eq, Ne, Le, Lt, Ge, Gt, Ne, Empty, Universe from .domains import Domain, And, Or, Not __all__ = [ - 'Expression', 'Symbol', 'Dummy', 'symbols', 'Rational', + 'LinExpr', 'Symbol', 'Dummy', 'symbols', 'Rational', 'Point', 'Vector', 'Polyhedron', 'Eq', 'Ne', 'Le', 'Lt', 'Ge', 'Gt', 'Empty', 'Universe', 'Domain', 'And', 'Or', 'Not', diff --git a/linpy/domains.py b/linpy/domains.py index 70e5e4f..238248b 100644 --- a/linpy/domains.py +++ b/linpy/domains.py @@ -31,7 +31,7 @@ from fractions import Fraction from . import islhelper from .islhelper import mainctx, libisl -from .linexprs import Expression, Symbol, Rational +from .linexprs import LinExpr, Symbol, Rational from .geometry import GeometricObject, Point, Vector @@ -616,10 +616,10 @@ class Domain(GeometricObject): elif isinstance(node, ast.Compare): equalities = [] inequalities = [] - left = Expression._fromast(node.left) + left = LinExpr._fromast(node.left) for i in range(len(node.ops)): op = node.ops[i] - right = Expression._fromast(node.comparators[i]) + right = LinExpr._fromast(node.comparators[i]) if isinstance(op, ast.Lt): inequalities.append(right - left - 1) elif isinstance(op, ast.LtE): @@ -642,7 +642,7 @@ class Domain(GeometricObject): _RE_AND = re.compile(r'\band\b|,|&&|/\\|∧|∩') _RE_OR = re.compile(r'\bor\b|;|\|\||\\/|∨|∪') _RE_NOT = re.compile(r'\bnot\b|!|¬') - _RE_NUM_VAR = Expression._RE_NUM_VAR + _RE_NUM_VAR = LinExpr._RE_NUM_VAR _RE_OPERATORS = re.compile(r'(&|\||~)') @classmethod @@ -701,7 +701,7 @@ class Domain(GeometricObject): args = [Domain.fromsympy(arg) for arg in expr.args] return funcmap[expr.func](*args) elif isinstance(expr, sympy.Expr): - return Expression.fromsympy(expr) + return LinExpr.fromsympy(expr) raise ValueError('non-domain expression: {!r}'.format(expr)) def tosympy(self): diff --git a/linpy/linexprs.py b/linpy/linexprs.py index bc36fda..8267be1 100644 --- a/linpy/linexprs.py +++ b/linpy/linexprs.py @@ -25,7 +25,7 @@ from fractions import Fraction, gcd __all__ = [ - 'Expression', + 'LinExpr', 'Symbol', 'Dummy', 'symbols', 'Rational', ] @@ -34,7 +34,7 @@ __all__ = [ def _polymorphic(func): @functools.wraps(func) def wrapper(left, right): - if isinstance(right, Expression): + if isinstance(right, LinExpr): return func(left, right) elif isinstance(right, numbers.Rational): right = Rational(right) @@ -43,7 +43,7 @@ def _polymorphic(func): return wrapper -class Expression: +class LinExpr: """ This class implements linear expressions. """ @@ -55,7 +55,7 @@ class Expression: if isinstance(coefficients, str): if constant != 0: raise TypeError('too many arguments') - return Expression.fromstring(coefficients) + return LinExpr.fromstring(coefficients) if coefficients is None: return Rational(constant) if isinstance(coefficients, Mapping): @@ -163,7 +163,7 @@ class Expression: for symbol, coefficient in other._coefficients.items(): coefficients[symbol] += coefficient constant = self._constant + other._constant - return Expression(coefficients, constant) + return LinExpr(coefficients, constant) __radd__ = __add__ @@ -176,7 +176,7 @@ class Expression: for symbol, coefficient in other._coefficients.items(): coefficients[symbol] -= coefficient constant = self._constant - other._constant - return Expression(coefficients, constant) + return LinExpr(coefficients, constant) @_polymorphic def __rsub__(self, other): @@ -190,7 +190,7 @@ class Expression: coefficients = ((symbol, coefficient * other) for symbol, coefficient in self._coefficients.items()) constant = self._constant * other - return Expression(coefficients, constant) + return LinExpr(coefficients, constant) return NotImplemented __rmul__ = __mul__ @@ -200,7 +200,7 @@ class Expression: coefficients = ((symbol, coefficient / other) for symbol, coefficient in self._coefficients.items()) constant = self._constant / other - return Expression(coefficients, constant) + return LinExpr(coefficients, constant) return NotImplemented @_polymorphic @@ -208,7 +208,7 @@ class Expression: """ Test whether two expressions are equal """ - return isinstance(other, Expression) and \ + return isinstance(other, LinExpr) and \ self._coefficients == other._coefficients and \ self._constant == other._constant @@ -256,7 +256,7 @@ class Expression: if othersymbol != symbol] coefficient = result._coefficients.get(symbol, 0) constant = result._constant - result = Expression(coefficients, constant) + coefficient*expression + result = LinExpr(coefficients, constant) + coefficient*expression return result @classmethod @@ -292,7 +292,7 @@ class Expression: Create an expression from a string. """ # add implicit multiplication operators, e.g. '5x' -> '5*x' - string = Expression._RE_NUM_VAR.sub(r'\1*\2', string) + string = LinExpr._RE_NUM_VAR.sub(r'\1*\2', string) tree = ast.parse(string, 'eval') return cls._fromast(tree) @@ -368,11 +368,11 @@ class Expression: coefficients.append((symbol, coefficient)) else: raise ValueError('non-linear expression: {!r}'.format(expr)) - return Expression(coefficients, constant) + return LinExpr(coefficients, constant) def tosympy(self): """ - Return an expression as a sympy object. + Return an expression as a sympy object. """ import sympy expr = 0 @@ -383,7 +383,7 @@ class Expression: return expr -class Symbol(Expression): +class Symbol(LinExpr): def __new__(cls, name): """ @@ -494,7 +494,7 @@ def symbols(names): return tuple(Symbol(name) for name in names) -class Rational(Expression, Fraction): +class Rational(LinExpr, Fraction): """ This class represents integers and rational numbers of any size. """ diff --git a/linpy/polyhedra.py b/linpy/polyhedra.py index 8eddb2d..346ffff 100644 --- a/linpy/polyhedra.py +++ b/linpy/polyhedra.py @@ -23,7 +23,7 @@ from . import islhelper from .islhelper import mainctx, libisl from .geometry import GeometricObject, Point -from .linexprs import Expression, Rational +from .linexprs import LinExpr, Rational from .domains import Domain @@ -50,7 +50,7 @@ class Polyhedron(Domain): """ Create and return a new Polyhedron from a string or list of equalities and inequalities. """ - + if isinstance(equalities, str): if inequalities is not None: raise TypeError('too many arguments') @@ -63,14 +63,14 @@ class Polyhedron(Domain): equalities = [] else: for i, equality in enumerate(equalities): - if not isinstance(equality, Expression): + if not isinstance(equality, LinExpr): raise TypeError('equalities must be linear expressions') equalities[i] = equality.scaleint() if inequalities is None: inequalities = [] else: for i, inequality in enumerate(inequalities): - if not isinstance(inequality, Expression): + if not isinstance(inequality, LinExpr): raise TypeError('inequalities must be linear expressions') inequalities[i] = inequality.scaleint() symbols = cls._xsymbols(equalities + inequalities) @@ -191,7 +191,7 @@ class Polyhedron(Domain): coefficient = islhelper.isl_val_to_int(coefficient) if coefficient != 0: coefficients[symbol] = coefficient - expression = Expression(coefficients, constant) + expression = LinExpr(coefficients, constant) if libisl.isl_constraint_is_equality(islconstraint): equalities.append(expression) else: @@ -345,13 +345,13 @@ Universe = UniverseType() def _polymorphic(func): @functools.wraps(func) def wrapper(left, right): - if not isinstance(left, Expression): + if not isinstance(left, LinExpr): if isinstance(left, numbers.Rational): left = Rational(left) else: raise TypeError('left must be a a rational number ' 'or a linear expression') - if not isinstance(right, Expression): + if not isinstance(right, LinExpr): if isinstance(right, numbers.Rational): right = Rational(right) else: diff --git a/linpy/tests/test_linexprs.py b/linpy/tests/test_linexprs.py index 16b9cde..fb7e4a2 100644 --- a/linpy/tests/test_linexprs.py +++ b/linpy/tests/test_linexprs.py @@ -24,34 +24,34 @@ from ..linexprs import * from .libhelper import requires_sympy -class TestExpression(unittest.TestCase): +class TestLinExpr(unittest.TestCase): def setUp(self): self.x = Symbol('x') self.y = Symbol('y') self.z = Symbol('z') - self.zero = Expression(constant=0) - self.one = Expression(constant=1) - self.pi = Expression(constant=Fraction(22, 7)) + self.zero = LinExpr(constant=0) + self.one = LinExpr(constant=1) + self.pi = LinExpr(constant=Fraction(22, 7)) self.expr = self.x - 2*self.y + 3 def test_new(self): - self.assertIsInstance(Expression(coefficients={self.x: 1}), Symbol) - self.assertIsInstance(Expression(constant=self.pi), Rational) + self.assertIsInstance(LinExpr(coefficients={self.x: 1}), Symbol) + self.assertIsInstance(LinExpr(constant=self.pi), Rational) self.assertNotIsInstance(self.x + self.pi, Symbol) self.assertNotIsInstance(self.x + self.pi, Rational) - xx = Expression({self.x: 2}) + xx = LinExpr({self.x: 2}) self.assertNotIsInstance(xx, Symbol) with self.assertRaises(TypeError): - Expression('x + y', 2) + LinExpr('x + y', 2) with self.assertRaises(TypeError): - Expression({0: 2}) + LinExpr({0: 2}) with self.assertRaises(TypeError): - Expression({'x': '2'}) - self.assertEqual(Expression(constant=1), Expression(constant=self.one)) - self.assertEqual(Expression(constant='1'), Expression(constant=self.one)) + LinExpr({'x': '2'}) + self.assertEqual(LinExpr(constant=1), LinExpr(constant=self.one)) + self.assertEqual(LinExpr(constant='1'), LinExpr(constant=self.one)) with self.assertRaises(ValueError): - Expression(constant='a') + LinExpr(constant='a') def test_coefficient(self): self.assertEqual(self.expr.coefficient(self.x), 1) @@ -174,15 +174,15 @@ class TestExpression(unittest.TestCase): self.expr.subs(self.x, 'x') def test_fromstring(self): - self.assertEqual(Expression.fromstring('x'), self.x) - self.assertEqual(Expression.fromstring('-x'), -self.x) - self.assertEqual(Expression.fromstring('22/7'), self.pi) - self.assertEqual(Expression.fromstring('x - 2y + 3'), self.expr) - self.assertEqual(Expression.fromstring('x - (3-1)y + 3'), self.expr) - self.assertEqual(Expression.fromstring('x - 2*y + 3'), self.expr) + self.assertEqual(LinExpr.fromstring('x'), self.x) + self.assertEqual(LinExpr.fromstring('-x'), -self.x) + self.assertEqual(LinExpr.fromstring('22/7'), self.pi) + self.assertEqual(LinExpr.fromstring('x - 2y + 3'), self.expr) + self.assertEqual(LinExpr.fromstring('x - (3-1)y + 3'), self.expr) + self.assertEqual(LinExpr.fromstring('x - 2*y + 3'), self.expr) def test_repr(self): - self.assertEqual(str(Expression()), '0') + self.assertEqual(str(LinExpr()), '0') self.assertEqual(str(self.x), 'x') self.assertEqual(str(-self.x), '-x') self.assertEqual(str(self.pi), '22/7') @@ -192,11 +192,11 @@ class TestExpression(unittest.TestCase): def test_fromsympy(self): import sympy sp_x, sp_y = sympy.symbols('x y') - self.assertEqual(Expression.fromsympy(sp_x), self.x) - self.assertEqual(Expression.fromsympy(sympy.Rational(22, 7)), self.pi) - self.assertEqual(Expression.fromsympy(sp_x - 2*sp_y + 3), self.expr) + self.assertEqual(LinExpr.fromsympy(sp_x), self.x) + self.assertEqual(LinExpr.fromsympy(sympy.Rational(22, 7)), self.pi) + self.assertEqual(LinExpr.fromsympy(sp_x - 2*sp_y + 3), self.expr) with self.assertRaises(ValueError): - Expression.fromsympy(sp_x*sp_y) + LinExpr.fromsympy(sp_x*sp_y) @requires_sympy def test_tosympy(self):