From: Vivien Maisonneuve Date: Tue, 19 Aug 2014 21:42:18 +0000 (+0200) Subject: Replace examples by tutorial in documentation X-Git-Tag: 1.0~19 X-Git-Url: https://scm.cri.ensmp.fr/git/linpy.git/commitdiff_plain/98edd00eb4b05e85f7cb1b85cff2f4d733909c57?ds=sidebyside Replace examples by tutorial in documentation --- diff --git a/doc/examples.rst b/doc/examples.rst deleted file mode 100644 index b552b7f..0000000 --- a/doc/examples.rst +++ /dev/null @@ -1,118 +0,0 @@ - -.. _examples: - -Examples -======== - -Basic Examples --------------- - -To create any polyhedron, first define the symbols used. -Then use the polyhedron functions to define the constraints. -The following is a simple running example illustrating some different operations and properties that can be performed by LinPy with two squares. - ->>> from linpy import * ->>> x, y = symbols('x y') ->>> # define the constraints of the polyhedron ->>> square1 = Le(0, x) & Le(x, 2) & Le(0, y) & Le(y, 2) ->>> square1 -And(Ge(x, 0), Ge(-x + 2, 0), Ge(y, 0), Ge(-y + 2, 0)) - -Binary operations and properties examples: - ->>> # create a polyhedron from a string ->>> square2 = Polyhedron('1 <= x') & Polyhedron('x <= 3') & \ - Polyhedron('1 <= y') & Polyhedron('y <= 3') ->>> #test equality ->>> square1 == square2 -False ->>> # compute the union of two polyhedra ->>> square1 | square2 -Or(And(Ge(x, 0), Ge(-x + 2, 0), Ge(y, 0), Ge(-y + 2, 0)), \ - And(Ge(x - 1, 0), Ge(-x + 3, 0), Ge(y - 1, 0), Ge(-y + 3, 0))) ->>> # check if square1 and square2 are disjoint ->>> square1.disjoint(square2) -False ->>> # compute the intersection of two polyhedra ->>> square1 & square2 -And(Ge(x - 1, 0), Ge(-x + 2, 0), Ge(y - 1, 0), Ge(-y + 2, 0)) ->>> # compute the convex union of two polyhedra ->>> Polyhedron(square1 | sqaure2) -And(Ge(x, 0), Ge(y, 0), Ge(-y + 3, 0), Ge(-x + 3, 0), \ - Ge(x - y + 2, 0), Ge(-x + y + 2, 0)) - -Unary operation and properties examples: - ->>> square1.isempty() -False ->>> # compute the complement of square1 ->>> ~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))) ->>> square1.symbols() -(x, y) ->>> square1.inequalities -(x, -x + 2, y, -y + 2) ->>> # project out the variable x ->>> square1.project([x]) -And(Ge(-y + 2, 0), Ge(y, 0)) - -Plot Examples -------------- - -LinPy can use the matplotlib plotting library to plot 2D and 3D polygons. -This can be a useful tool to visualize and compare polygons. -The user has the option to pass plot objects to the :meth:`Domain.plot` method, which provides great flexibility. -Also, keyword arguments can be passed such as color and the degree of transparency of a polygon. - ->>> import matplotlib.pyplot as plt ->>> from matplotlib import pylab ->>> from mpl_toolkits.mplot3d import Axes3D ->>> from linpy import * ->>> # define the symbols ->>> x, y, z = symbols('x y z') ->>> fig = plt.figure() ->>> cham_plot = fig.add_subplot(1, 1, 1, projection='3d', aspect='equal') ->>> cham_plot.set_title('Chamfered cube') ->>> cham = Le(0, x) & Le(x, 3) & Le(0, y) & Le(y, 3) & Le(0, z) & \ - Le(z, 3) & Le(z - 2, x) & Le(x, z + 2) & Le(1 - z, x) & \ - Le(x, 5 - z) & Le(z - 2, y) & Le(y, z + 2) & Le(1 - z, y) & \ - Le(y, 5 - z) & Le(y - 2, x) & Le(x, y + 2) & Le(1 - y, x) & Le(x, 5 - y) ->>> cham.plot(cham_plot, facecolor='red', alpha=0.75) ->>> pylab.show() - -.. figure:: images/cham_cube.jpg - :align: center - -LinPy can also inspect a polygon's vertices and the integer points included in the polygon. - ->>> diamond = Ge(y, x - 1) & Le(y, x + 1) & Ge(y, -x - 1) & Le(y, -x + 1) ->>> diamond.vertices() -[Point({x: Fraction(0, 1), y: Fraction(1, 1)}), \ - Point({x: Fraction(-1, 1), y: Fraction(0, 1)}), \ - Point({x: Fraction(1, 1), y: Fraction(0, 1)}), \ - Point({x: Fraction(0, 1), y: Fraction(-1, 1)})] ->>> diamond.points() -[Point({x: -1, y: 0}), Point({x: 0, y: -1}), Point({x: 0, y: 0}), \ - Point({x: 0, y: 1}), Point({x: 1, y: 0})] - -The user also can pass another plot to the :meth:`Domain.plot` method. -This can be useful to compare two polyhedra on the same axis. -This example illustrates the union of two squares. - ->>> from linpy import * ->>> import matplotlib.pyplot as plt ->>> from matplotlib import pylab ->>> x, y = symbols('x y') ->>> square1 = Le(0, x) & Le(x, 2) & Le(0, y) & Le(y, 2) ->>> square2 = Le(1, x) & Le(x, 3) & Le(1, y) & Le(y, 3) ->>> fig = plt.figure() ->>> plot = fig.add_subplot(1, 1, 1, aspect='equal') ->>> square1.plot(plot, facecolor='red', alpha=0.3) ->>> square2.plot(plot, facecolor='blue', alpha=0.3) ->>> squares = Polyhedron(square1 + square2) ->>> squares.plot(plot, facecolor='blue', alpha=0.3) ->>> pylab.show() - -.. figure:: images/union.jpg - :align: center diff --git a/doc/index.rst b/doc/index.rst index a1809d6..d4defce 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -8,7 +8,7 @@ Integer Set Library (isl) is a C library for manipulating sets and relations of LinPy is a free software, licensed under the `GPLv3 license `_. Its source code is available `here `_. -To have an overview of LinPy's functionalities, you may wish to consult the :ref:`examples` section. +To have an overview of LinPy's functionalities, you may wish to consult the :ref:`tutorial` section. .. only:: html @@ -18,7 +18,7 @@ To have an overview of LinPy's functionalities, you may wish to consult the :ref :maxdepth: 2 install.rst - examples.rst + tutorial.rst reference.rst .. only:: html diff --git a/doc/install.rst b/doc/install.rst index 339b6c6..92aad27 100644 --- a/doc/install.rst +++ b/doc/install.rst @@ -1,4 +1,6 @@ +.. _install: + Installation ============ @@ -26,13 +28,13 @@ Install Using pip .. warning:: The project has not been published in PyPI yet, so this section is not relevant. - Instead, see the :ref:`source` section to install LinPy. + Instead, see the :ref:`install_source` section to install LinPy. LinPy can be installed using pip with the command:: sudo pip install linpy -.. _source: +.. _install_source: Install From Source ------------------- diff --git a/doc/reference.rst b/doc/reference.rst index 8e36056..64c37c6 100644 --- a/doc/reference.rst +++ b/doc/reference.rst @@ -1,4 +1,6 @@ +.. _reference: + Module Reference ================ @@ -307,7 +309,7 @@ Domains ------- A *domain* is a union of polyhedra. -Unlike polyhedra, domains allow exact computation of union and complementary operations. +Unlike polyhedra, domains allow exact computation of union, subtraction and complementary operations. .. class:: Domain(*polyhedra) Domain(string) diff --git a/doc/tutorial.rst b/doc/tutorial.rst new file mode 100644 index 0000000..8598cbd --- /dev/null +++ b/doc/tutorial.rst @@ -0,0 +1,116 @@ + +.. _tutorial: + +Tutorial +======== + +Polyhedra +--------- + +The following example shows how we can manipulate polyhedra using LinPy. +Let us define two square polyhedra, corresponding to the sets ``square1 = {(x, y) | 0 <= x <= 2, 0 <= y <= 2}`` and ``square2 = {(x, y) | 2 <= x <= 4, 2 <= y <= 4}``. +First, we need define the symbols used, for instance with the :func:`symbols` function. + +>>> from linpy import * +>>> x, y = symbols('x y') + +Then, we can build the :class:`Polyhedron` object ``square1`` from its constraints: + +>>> 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)) + +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)) + +The usual polyhedral operations are available, including intersection: + +>>> inter = square1.intersection(square2) +>>> inter +And(Ge(x - 1, 0), Ge(-x + 2, 0), Ge(y - 1, 0), Ge(-y + 2, 0)) + +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 projection: + +>>> square1.project([y]) +And(Ge(x, 0), Ge(-x + 2, 0)) + +Equality and inclusion tests are also provided. +Special values :data:`Empty` and :data:`Universe` represent the empty and universe polyhedra. + +>>> inter <= square1 +True +>>> inter == Empty +False + + +Domains +------- + +LinPy is also able to manipulate polyhedral *domains*, that is, unions of polyhedra. +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 +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))) +>>> union <= hull +True + +Unlike polyhedra, domains allow exact computation of union, subtraction and complementary operations. + +>>> diff = 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))) +>>> ~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))) + + +Plotting +-------- + +LinPy can use the :mod:`matplotlib` plotting library, if available, to plot bounded polyhedra and domains. + +>>> import matplotlib.pyplot as plt +>>> from matplotlib import pylab +>>> fig = plt.figure() +>>> plot = fig.add_subplot(1, 1, 1, aspect='equal') +>>> square1.plot(plot, facecolor='red', alpha=0.3) +>>> square2.plot(plot, facecolor='blue', alpha=0.3) +>>> hull.plot(plot, facecolor='blue', alpha=0.3) +>>> pylab.show() + +Note that you can pass a plot object to the :meth:`Domain.plot` method, which provides great flexibility. +Also, keyword arguments can be passed such as color and the degree of transparency of a polygon. + +.. figure:: images/union.jpg + :align: center + +3D plots are also supported. + +>>> import matplotlib.pyplot as plt +>>> from matplotlib import pylab +>>> from mpl_toolkits.mplot3d import Axes3D +>>> from linpy import * +>>> x, y, z = symbols('x y z') +>>> fig = plt.figure() +>>> plot = fig.add_subplot(1, 1, 1, projection='3d', aspect='equal') +>>> plot.set_title('Chamfered cube') +>>> poly = Le(0, x, 3) & Le(0, y, 3) & Le(0, z, 3) & \ + Le(z - 2, x) & Le(x, z + 2) & Le(1 - z, x) & Le(x, 5 - z) & \ + Le(z - 2, y) & Le(y, z + 2) & Le(1 - z, y) & Le(y, 5 - z) & \ + Le(y - 2, x) & Le(x, y + 2) & Le(1 - y, x) & Le(x, 5 - y) +>>> poly.plot(plot, facecolor='red', alpha=0.75) +>>> pylab.show() + +.. figure:: images/cham_cube.jpg + :align: center diff --git a/linpy/domains.py b/linpy/domains.py index b950e1e..519059d 100644 --- a/linpy/domains.py +++ b/linpy/domains.py @@ -38,7 +38,7 @@ __all__ = [ class Domain(GeometricObject): """ A domain is a union of polyhedra. Unlike polyhedra, domains allow exact - computation of union and complementary operations. + computation of union, subtraction and complementary operations. A domain with a unique polyhedron is automatically subclassed as a Polyhedron instance.