From: Vivien Maisonneuve Date: Wed, 20 Aug 2014 11:42:22 +0000 (+0200) Subject: Update documentation to match __repr__() changes X-Git-Tag: 1.0~9 X-Git-Url: https://svn.cri.ensmp.fr/git/linpy.git/commitdiff_plain/5d474779438016e3af4bfd13a4200a01ca9ec3c7?ds=sidebyside;hp=ddefc755b0637b08a0a2788be3a0678b52942f5b Update documentation to match __repr__() changes --- diff --git a/doc/reference.rst b/doc/reference.rst index ae82aca..e6f291d 100644 --- a/doc/reference.rst +++ b/doc/reference.rst @@ -84,12 +84,12 @@ Linear expressions are generally built using overloaded operators. 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) @@ -102,7 +102,7 @@ For example, if ``x`` is a :class:`Symbol`, then ``x + 1`` is an instance of :cl 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. @@ -177,7 +177,7 @@ For example, if ``x`` is a :class:`Symbol`, then ``x + 1`` is an instance of :cl >>> x, y = symbols('x y') >>> x < y - Le(x - y + 1, 0) + x + 1 <= y .. method:: scaleint() @@ -220,7 +220,7 @@ Apart from :mod:`Symbol`, a particular case of linear expressions are rational v 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`. @@ -243,14 +243,16 @@ A *convex polyhedron* (or simply "polyhedron") is the space defined by a system 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: @@ -268,6 +270,7 @@ This space can be unbounded. >>> 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. @@ -320,14 +323,16 @@ A *domain* is a union of polyhedra. 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: diff --git a/doc/tutorial.rst b/doc/tutorial.rst index 9b55a03..b13a22e 100644 --- a/doc/tutorial.rst +++ b/doc/tutorial.rst @@ -24,31 +24,32 @@ Then, we can build the :class:`Polyhedron` object ``square1`` from its constrain >>> square1 = Le(0, x, 2) & Le(0, y, 2) >>> square1 -And(Ge(x, 0), Ge(-x + 2, 0), Ge(y, 0), Ge(-y + 2, 0)) +And(0 <= x, x <= 2, 0 <= y, y <= 2) LinPy provides comparison functions :func:`Lt`, :func:`Le`, :func:`Eq`, :func:`Ne`, :func:`Ge` and :func:`Gt` to build constraints, and logical operators :func:`And`, :func:`Or`, :func:`Not` to combine them. Alternatively, a polyhedron can be built from a string: >>> square2 = Polyhedron('1 <= x <= 3, 1 <= y <= 3') >>> square2 -And(Ge(x - 1, 0), Ge(-x + 3, 0), Ge(y - 1, 0), Ge(-y + 3, 0)) +And(1 <= x, x <= 3, 1 <= y, y <= 3) The usual polyhedral operations are available, including intersection: ->>> inter = square1.intersection(square2) +>>> inter = square1.intersection(square2) # or square1 & square2 >>> inter -And(Ge(x - 1, 0), Ge(-x + 2, 0), Ge(y - 1, 0), Ge(-y + 2, 0)) +And(1 <= x, x <= 2, 1 <= y, y <= 2) convex union: >>> hull = square1.convex_union(square2) >>> hull -And(Ge(x, 0), Ge(y, 0), Ge(-x + y + 2, 0), Ge(x - y + 2, 0), Ge(-x + 3, 0), Ge(-y + 3, 0)) +And(0 <= x, 0 <= y, x <= y + 2, y <= x + 2, x <= 3, y <= 3) and projection: ->>> square1.project([y]) -And(Ge(x, 0), Ge(-x + 2, 0)) +>>> proj = square1.project([y]) +>>> proj +And(0 <= x, x <= 2) Equality and inclusion tests are also provided. Special values :data:`Empty` and :data:`Universe` represent the empty and universe polyhedra. @@ -68,19 +69,19 @@ LinPy is also able to manipulate polyhedral *domains*, that is, unions of polyhe An example of domain is the set union (as opposed to convex union) of polyhedra ``square1`` and ``square2``. The result is a :class:`Domain` object. ->>> union = square1 | square2 +>>> union = square1.union(square2) # or square1 | square2 >>> union -Or(And(Ge(-x + 2, 0), Ge(x, 0), Ge(-y + 2, 0), Ge(y, 0)), And(Ge(-x + 3, 0), Ge(x - 1, 0), Ge(-y + 3, 0), Ge(y - 1, 0))) +Or(And(x <= 2, 0 <= x, y <= 2, 0 <= y), And(x <= 3, 1 <= x, y <= 3, 1 <= y)) >>> union <= hull True Unlike polyhedra, domains allow exact computation of union, subtraction and complementary operations. ->>> diff = square1 - square2 +>>> diff = square1.difference(square2) # or square1 - square2 >>> diff -Or(And(Eq(x, 0), Ge(y, 0), Ge(-y + 2, 0)), And(Eq(y, 0), Ge(x - 1, 0), Ge(-x + 2, 0))) +Or(And(x == 0, 0 <= y, y <= 2), And(y == 0, 1 <= x, x <= 2)) >>> ~square1 -Or(Ge(-x - 1, 0), Ge(x - 3, 0), And(Ge(x, 0), Ge(-x + 2, 0), Ge(-y - 1, 0)), And(Ge(x, 0), Ge(-x + 2, 0), Ge(y - 3, 0))) +Or(x + 1 <= 0, 3 <= x, And(0 <= x, x <= 2, y + 1 <= 0), And(0 <= x, x <= 2, 3 <= y)) .. _tutorial_plot: diff --git a/examples/squares.py b/examples/squares.py index 15aed16..1a0cedb 100755 --- a/examples/squares.py +++ b/examples/squares.py @@ -31,7 +31,7 @@ if __name__ == '__main__': shell.push('square2') shell.push() - shell.push('inter = square1.intersection(square2)') + shell.push('inter = square1.intersection(square2) # or square1 & square2') shell.push('inter') shell.push() @@ -39,18 +39,19 @@ if __name__ == '__main__': shell.push('hull') shell.push() - shell.push('square1.project([y])') + shell.push('proj = square1.project([y])') + shell.push('proj') shell.push() shell.push('inter <= square1') shell.push('inter == Empty') shell.push() - shell.push('union = square1 | square2') + shell.push('union = square1.union(square2) # or square1 | square2') shell.push('union') shell.push('union <= hull') shell.push() - shell.push('diff = square1 - square2') + shell.push('diff = square1.difference(square2) # or square1 - square2') shell.push('diff') shell.push('~square1')