For example, if ``x`` is a :class:`Symbol`, then ``x + 1`` is an instance of :class:`LinExpr`.
.. class:: LinExpr(coefficients=None, constant=0)
- LinExpr(string)
+ LinExpr(string)
Return a linear expression from a dictionary or a sequence, that maps symbols to their coefficients, and a constant term.
The coefficients and the constant term must be rational numbers.
- For example, the linear expression ``x + 2y + 1`` can be constructed using one of the following instructions:
+ For example, the linear expression ``x + 2*y + 1`` can be constructed using one of the following instructions:
>>> x, y = symbols('x y')
>>> LinExpr({x: 1, y: 2}, 1)
Alternatively, linear expressions can be constructed from a string:
- >>> LinExpr('x + 2*y + 1')
+ >>> LinExpr('x + 2y + 1')
:class:`LinExpr` instances are hashable, and should be treated as immutable.
>>> x, y = symbols('x y')
>>> x < y
- Le(x - y + 1, 0)
+ x + 1 <= y
.. method:: scaleint()
They are implemented by the :class:`Rational` class, that inherits from both :class:`LinExpr` and :class:`fractions.Fraction` classes.
.. class:: Rational(numerator, denominator=1)
- Rational(string)
+ Rational(string)
The first version requires that the *numerator* and *denominator* are instances of :class:`numbers.Rational` and returns a new :class:`Rational` instance with the value ``numerator/denominator``.
If the denominator is ``0``, it raises a :exc:`ZeroDivisionError`.
This space can be unbounded.
.. class:: Polyhedron(equalities, inequalities)
- Polyhedron(string)
- Polyhedron(geometric object)
+ Polyhedron(string)
+ Polyhedron(geometric object)
Return a polyhedron from two sequences of linear expressions: *equalities* is a list of expressions equal to ``0``, and *inequalities* is a list of expressions greater or equal to ``0``.
For example, the polyhedron ``0 <= x <= 2, 0 <= y <= 2`` can be constructed with:
>>> x, y = symbols('x y')
>>> square = Polyhedron([], [x, 2 - x, y, 2 - y])
+ >>> square
+ And(0 <= x, x <= 2, 0 <= y, y <= 2)
It may be easier to use comparison operators :meth:`LinExpr.__lt__`, :meth:`LinExpr.__le__`, :meth:`LinExpr.__ge__`, :meth:`LinExpr.__gt__`, or functions :func:`Lt`, :func:`Le`, :func:`Eq`, :func:`Ge` and :func:`Gt`, using one of the following instructions:
>>> square = Polyhedron('0 <= x <= 2, 0 <= y <= 2')
>>> square2 = Polyhedron('2 <= x <= 4, 2 <= y <= 4')
>>> Polyhedron(square | square2)
+ And(x <= 4, 0 <= x, y <= 4, 0 <= y, x <= y + 2, y <= x + 2)
A polyhedron is a :class:`Domain` instance, and, therefore, inherits the functionalities of this class.
It is also a :class:`GeometricObject` instance.
Unlike polyhedra, domains allow exact computation of union, subtraction and complementary operations.
.. class:: Domain(*polyhedra)
- Domain(string)
- Domain(geometric object)
+ Domain(string)
+ Domain(geometric object)
Return a domain from a sequence of polyhedra.
>>> square = Polyhedron('0 <= x <= 2, 0 <= y <= 2')
>>> square2 = Polyhedron('2 <= x <= 4, 2 <= y <= 4')
- >>> dom = Domain([square, square2])
+ >>> dom = Domain(square, square2)
+ >>> dom
+ Or(And(x <= 2, 0 <= x, y <= 2, 0 <= y), And(x <= 4, 2 <= x, y <= 4, 2 <= y))
It is also possible to build domains from polyhedra using arithmetic operators :meth:`Domain.__and__`, :meth:`Domain.__or__` or functions :func:`And` and :func:`Or`, using one of the following instructions: