"""
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',
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
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):
_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
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):
__all__ = [
- 'Expression',
+ 'LinExpr',
'Symbol', 'Dummy', 'symbols',
'Rational',
]
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)
return wrapper
-class Expression:
+class LinExpr:
"""
This class implements linear expressions.
"""
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):
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__
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):
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__
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
"""
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
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
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)
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
return expr
-class Symbol(Expression):
+class Symbol(LinExpr):
def __new__(cls, name):
"""
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.
"""
from .islhelper import mainctx, libisl
from .geometry import GeometricObject, Point
-from .linexprs import Expression, Rational
+from .linexprs import LinExpr, Rational
from .domains import 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')
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)
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:
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:
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)
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')
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):