next | previous | forward | backward | up | top | index | toc | Macaulay2 web site
Polyhedra :: Working with polyhedra

Working with polyhedra

We start with a polyhedron in 2-space which is the convexHull of a given set of points.
i1 : V = matrix {{0,2,-2,0},{-1,1,1,1}}

o1 = | 0  2 -2 0 |
     | -1 1 1  1 |

              2        4
o1 : Matrix ZZ  <--- ZZ
i2 : P = convexHull V

o2 = P

o2 : Polyhedron

This gives an overview of the characteristics of the polyhedron. If we want to know more details, we can ask for them.

i3 : vertices P

o3 = | 0  -2 2 |
     | -1 1  1 |

              2        3
o3 : Matrix QQ  <--- QQ

Here we see that the point (0,1) is not a vertex and P is actually a triangle.

i4 : (HS,v) = halfspaces P

o4 = (| -1 -1 |, | 1 |)
      | 1  -1 |  | 1 |
      | 0  1  |  | 1 |

o4 : Sequence

This gives the defining affine half-spaces, i.e. P is given by all p such that HS*p =< v and that lie in the defining affine hyperplanes. To get the hyperplanes we use:

i5 : hyperplanes P

o5 = (0, 0)

o5 : Sequence

There are none, so the polyhedron is of full dimension. It is also compact, since P has no rays and the lineality space is of dimension zero.

i6 : rays P

o6 = 0

              2
o6 : Matrix QQ  <--- 0
i7 : linealitySpace P

o7 = 0

              2
o7 : Matrix QQ  <--- 0

Furthermore, we can construct the convex hull of a set of points and a set of rays.

i8 : R = matrix {{1},{0},{0}}

o8 = | 1 |
     | 0 |
     | 0 |

              3        1
o8 : Matrix ZZ  <--- ZZ
i9 : V1 = V || matrix {{1,1,1,1}}

o9 = | 0  2 -2 0 |
     | -1 1 1  1 |
     | 1  1 1  1 |

              3        4
o9 : Matrix ZZ  <--- ZZ
i10 : P1 = convexHull(V1,R)

o10 = P1

o10 : Polyhedron
i11 : vertices P1

o11 = | 0  -2 |
      | -1 1  |
      | 1  1  |

               3        2
o11 : Matrix QQ  <--- QQ

This polyhedron is not compact anymore and also not of full dimension.

i12 : rays P1

o12 = | 1 |
      | 0 |
      | 0 |

               3        1
o12 : Matrix QQ  <--- QQ
i13 : hyperplanes P1

o13 = (| 0 0 -1 |, | -1 |)

o13 : Sequence

On the other hand we can construct a polyhedron as the intersection of affine half-spaces and affine hyperplanes.

i14 : HS = transpose (V || matrix {{-1,2,0,1}})

o14 = | 0  -1 -1 |
      | 2  1  2  |
      | -2 1  0  |
      | 0  1  1  |

               4        3
o14 : Matrix ZZ  <--- ZZ
i15 : v = matrix {{1},{1},{1},{1}}

o15 = | 1 |
      | 1 |
      | 1 |
      | 1 |

               4        1
o15 : Matrix ZZ  <--- ZZ
i16 : hyperplanesTmp = matrix {{1,1,1}}

o16 = | 1 1 1 |

               1        3
o16 : Matrix ZZ  <--- ZZ
i17 : w = matrix {{3}}

o17 = | 3 |

               1        1
o17 : Matrix ZZ  <--- ZZ
i18 : P2 = intersection(HS,v,hyperplanesTmp,w)
Warning: This method is deprecated. Please consider using polyhedronFromHData instead.

o18 = P2

o18 : Polyhedron

This is a triangle in 3-space with the following vertices.

i19 : vertices P2

o19 = | 4   4  2  |
      | 9   5  5  |
      | -10 -6 -4 |

               3        3
o19 : Matrix QQ  <--- QQ

If we don't intersect with the hyperplane we get a full dimensional polyhedron.

i20 : P3 = intersection(HS,v)
Warning: This method is deprecated. Please consider using polyhedronFromHData instead.

o20 = P3

o20 : Polyhedron
i21 : vertices P3

o21 = | 0 0  0  |
      | 1 1  -3 |
      | 0 -2 2  |

               3        3
o21 : Matrix QQ  <--- QQ
i22 : linealitySpace P3

o22 = | 1  |
      | 2  |
      | -2 |

               3        1
o22 : Matrix QQ  <--- QQ

Note that the vertices are given modulo the lineality space. Besides constructing polyhedra by hand, there are also some basic polyhedra implemented such as the hypercube, in this case with edge-length four.

i23 : P4 = hypercube(3,2)

o23 = P4

o23 : Polyhedron
i24 : vertices P4

o24 = | -2 2  -2 2  -2 2  -2 2 |
      | -2 -2 2  2  -2 -2 2  2 |
      | -2 -2 -2 -2 2  2  2  2 |

               3        8
o24 : Matrix QQ  <--- QQ

Another on is the crossPolytope, in this case with diameter six.

i25 : P5 = crossPolytope(3,3)

o25 = P5

o25 : Polyhedron
i26 : vertices P5

o26 = | -3 3 0  0 0  0 |
      | 0  0 -3 3 0  0 |
      | 0  0 0  0 -3 3 |

               3        6
o26 : Matrix QQ  <--- QQ

Furthermore the standard simplex (stdSimplex).

i27 : P6 = stdSimplex 2

o27 = P6

o27 : Polyhedron
i28 : vertices P6

o28 = | 1 0 0 |
      | 0 1 0 |
      | 0 0 1 |

               3        3
o28 : Matrix QQ  <--- QQ

Now that we can construct polyhedra, we can turn to the functions that can be applied to polyhedra. First of all, we can apply the convexHull function also to a pair of polyhedra:

i29 : P7 = convexHull(P4,P5)

o29 = P7

o29 : Polyhedron
i30 : vertices P7

o30 = | -3 3 0  0 0  -2 2  -2 2  -2 2  -2 2 0 |
      | 0  0 -3 3 0  -2 -2 2  2  -2 -2 2  2 0 |
      | 0  0 0  0 -3 -2 -2 -2 -2 2  2  2  2 3 |

               3        14
o30 : Matrix QQ  <--- QQ

Or we can intersect them by using intersection:

i31 : P8 = intersection(P4,P5)

o31 = P8

o31 : Polyhedron
i32 : vertices P8

o32 = | -1 1  -2 2  -2 2 -1 1 -1 1  0  0  -2 2  0  0  -2 2 0  0 -1 1 0  0 |
      | -2 -2 -1 -1 1  1 2  2 0  0  -1 1  0  0  -2 2  0  0 -2 2 0  0 -1 1 |
      | 0  0  0  0  0  0 0  0 -2 -2 -2 -2 -1 -1 -1 -1 1  1 1  1 2  2 2  2 |

               3        24
o32 : Matrix QQ  <--- QQ

Furthermore, both functions can be applied to a list containing any number of polyhedra and matrices defining vertices/rays or affine half-spaces/hyperplanes. All of these must be in the same ambient space. For example:

i33 : P9 = convexHull {(V1,R),P2,P6}

o33 = P9

o33 : Polyhedron
i34 : vertices P9

o34 = | 4   4  2  0  -2 |
      | 9   5  5  -1 1  |
      | -10 -6 -4 1  1  |

               3        5
o34 : Matrix QQ  <--- QQ

Further functions are for example the Minkowski sum (minkowskiSum) of two polyhedra.

i35 : Q = convexHull (-V)

o35 = Q

o35 : Polyhedron
i36 : P10 = P + Q

o36 = P10

o36 : Polyhedron
i37 : vertices P10

o37 = | -4 4 -2 2  -2 2 |
      | 0  0 -2 -2 2  2 |

               2        6
o37 : Matrix QQ  <--- QQ

In the other direction, we can also determine all Minkowski summands (see minkSummandCone) of a polyhedron.

i38 : (C,L,M) = minkSummandCone P10

o38 = (C, HashTable{0 => Polyhedron{...1...}}, | 1 0 |)
                    1 => Polyhedron{...1...}   | 0 1 |
                    2 => Polyhedron{...1...}   | 1 0 |
                    3 => Polyhedron{...1...}   | 1 0 |
                    4 => Polyhedron{...1...}   | 0 1 |

o38 : Sequence
i39 : apply(values L, vertices)

o39 = {| 0 4 |, | 0 4 2  |, | 0 2 |, | 0 2  |, | 0 4 2 |}
       | 0 0 |  | 0 0 -2 |  | 0 2 |  | 0 -2 |  | 0 0 2 |

o39 : List

Here the polyhedra in the hash table L are all possible Minkowski summands up to scalar multiplication and the columns of M give the minimal decompositions. So the hexagon P10 is not only the sum of two triangles but also the sum of three lines. Furthermore, we can take the direct product of two polyhedra.

i40 : P11 = P * Q
Warning: This method is deprecated. Please consider using polyhedronFromHData instead.

o40 = P11

o40 : Polyhedron
i41 : vertices P11

o41 = | 0  -2 2  0  -2 2  0  -2 2 |
      | -1 1  1  -1 1  1  -1 1  1 |
      | -2 -2 -2 2  2  2  0  0  0 |
      | -1 -1 -1 -1 -1 -1 1  1  1 |

               4        9
o41 : Matrix QQ  <--- QQ

The result is in QQ^4.

i42 : ambDim P11

o42 = 4

To find out more about this polyhedron use for example.

i43 : fVector P11

o43 = {9, 18, 15, 6, 1}

o43 : List

The function fVector gives the number of faces of each dimension, so it has 9 vertices, 18 edges and so on. We can access the faces of a certain codimension via:

i44 : L = faces(1,P11)

o44 = {({0, 1, 3, 4, 6, 7}, {}), ({0, 2, 3, 5, 6, 8}, {}), ({1, 2, 4, 5, 7,
      -----------------------------------------------------------------------
      8}, {}), ({0, 1, 2, 3, 4, 5}, {}), ({0, 1, 2, 6, 7, 8}, {}), ({3, 4, 5,
      -----------------------------------------------------------------------
      6, 7, 8}, {})}

o44 : List
i45 : vertP11 = vertices P11

o45 = | 0  -2 2  0  -2 2  0  -2 2 |
      | -1 1  1  -1 1  1  -1 1  1 |
      | -2 -2 -2 2  2  2  0  0  0 |
      | -1 -1 -1 -1 -1 -1 1  1  1 |

               4        9
o45 : Matrix QQ  <--- QQ
i46 : apply(L, l -> vertP11_(l#0))

o46 = {| 0  -2 0  -2 0  -2 |, | 0  2  0  2  0  2 |, | -2 2  -2 2  -2 2 |, |
       | -1 1  -1 1  -1 1  |  | -1 1  -1 1  -1 1 |  | 1  1  1  1  1  1 |  |
       | -2 -2 2  2  0  0  |  | -2 -2 2  2  0  0 |  | -2 -2 2  2  0  0 |  |
       | -1 -1 -1 -1 1  1  |  | -1 -1 -1 -1 1  1 |  | -1 -1 -1 -1 1  1 |  |
      -----------------------------------------------------------------------
      0  -2 2  0  -2 2  |, | 0  -2 2  0  -2 2 |, | 0  -2 2  0  -2 2 |}
      -1 1  1  -1 1  1  |  | -1 1  1  -1 1  1 |  | -1 1  1  -1 1  1 |
      -2 -2 -2 2  2  2  |  | -2 -2 -2 0  0  0 |  | 2  2  2  0  0  0 |
      -1 -1 -1 -1 -1 -1 |  | -1 -1 -1 1  1  1 |  | -1 -1 -1 1  1  1 |

o46 : List

We can compute all lattice points of the polyhedron with latticePoints.

i47 : L = latticePoints P11

o47 = {| 1  |, | -2 |, | 2  |, | 0  |, | 1  |, | -1 |, | 1  |, | -1 |, | 0 
       | 0  |  | 1  |  | 1  |  | 1  |  | 1  |  | 1  |  | 1  |  | 0  |  | 0 
       | -2 |  | -2 |  | -2 |  | 2  |  | 2  |  | -2 |  | -2 |  | -2 |  | -2
       | -1 |  | -1 |  | -1 |  | -1 |  | -1 |  | -1 |  | -1 |  | -1 |  | -1
      -----------------------------------------------------------------------
      |, | 0  |, | 0  |, | 0  |, | 0  |, | 1  |, | -1 |, | 0  |, | 0  |, | 0 
      |  | -1 |  | 1  |  | -1 |  | -1 |  | 0  |  | 0  |  | 0  |  | -1 |  | -1
      |  | -2 |  | -2 |  | -1 |  | -1 |  | -1 |  | -1 |  | -1 |  | 0  |  | 0 
      |  | -1 |  | -1 |  | -1 |  | 0  |  | -1 |  | -1 |  | -1 |  | -1 |  | 0 
      -----------------------------------------------------------------------
      |, | 1  |, | -1 |, | 0  |, | 0  |, | -2 |, | 2  |, | -1 |, | 1  |, | 0 
      |  | 0  |  | 0  |  | 0  |  | -1 |  | 1  |  | 1  |  | 1  |  | 1  |  | 1 
      |  | -1 |  | -1 |  | -1 |  | 0  |  | -1 |  | -1 |  | -1 |  | -1 |  | -1
      |  | 0  |  | 0  |  | 0  |  | 1  |  | -1 |  | -1 |  | -1 |  | -1 |  | -1
      -----------------------------------------------------------------------
      |, | 1  |, | -1 |, | 0  |, | 0  |, | 0  |, | 1 |, | -1 |, 0, | -2 |, |
      |  | 0  |  | 0  |  | 0  |  | -1 |  | -1 |  | 0 |  | 0  |     | 1  |  |
      |  | 0  |  | 0  |  | 0  |  | 1  |  | 1  |  | 0 |  | 0  |     | -1 |  |
      |  | -1 |  | -1 |  | -1 |  | -1 |  | 0  |  | 0 |  | 0  |     | 0  |  |
      -----------------------------------------------------------------------
      2  |, | -1 |, | 1  |, | 0  |, | 1 |, | -1 |, | 0 |, | -2 |, | 2  |, |
      1  |  | 1  |  | 1  |  | 1  |  | 0 |  | 0  |  | 0 |  | 1  |  | 1  |  |
      -1 |  | -1 |  | -1 |  | -1 |  | 0 |  | 0  |  | 0 |  | 0  |  | 0  |  |
      0  |  | 0  |  | 0  |  | 0  |  | 1 |  | 1  |  | 1 |  | -1 |  | -1 |  |
      -----------------------------------------------------------------------
      -1 |, | 1  |, | 0  |, | 1  |, | -1 |, | 0  |, | 0  |, | 1 |, | -1 |, |
      1  |  | 1  |  | 1  |  | 0  |  | 0  |  | 0  |  | -1 |  | 0 |  | 0  |  |
      0  |  | 0  |  | 0  |  | 1  |  | 1  |  | 1  |  | 2  |  | 1 |  | 1  |  |
      -1 |  | -1 |  | -1 |  | -1 |  | -1 |  | -1 |  | -1 |  | 0 |  | 0  |  |
      -----------------------------------------------------------------------
      0 |, | -2 |, | 2 |, | -1 |, | 1 |, | 0 |, | -2 |, | 2 |, | -1 |, | 1 |,
      0 |  | 1  |  | 1 |  | 1  |  | 1 |  | 1 |  | 1  |  | 1 |  | 1  |  | 1 | 
      1 |  | 0  |  | 0 |  | 0  |  | 0 |  | 0 |  | 0  |  | 0 |  | 0  |  | 0 | 
      0 |  | 0  |  | 0 |  | 0  |  | 0 |  | 0 |  | 1  |  | 1 |  | 1  |  | 1 | 
      -----------------------------------------------------------------------
      | 0 |, | -2 |, | 2  |, | -1 |, | 1  |, | 0  |, | 1  |, | -1 |, | 0  |,
      | 1 |  | 1  |  | 1  |  | 1  |  | 1  |  | 1  |  | 0  |  | 0  |  | 0  | 
      | 0 |  | 1  |  | 1  |  | 1  |  | 1  |  | 1  |  | 2  |  | 2  |  | 2  | 
      | 1 |  | -1 |  | -1 |  | -1 |  | -1 |  | -1 |  | -1 |  | -1 |  | -1 | 
      -----------------------------------------------------------------------
      | -2 |, | 2 |, | -1 |, | 1 |, | 0 |, | -2 |, | 2  |, | -1 |}
      | 1  |  | 1 |  | 1  |  | 1 |  | 1 |  | 1  |  | 1  |  | 1  |
      | 1  |  | 1 |  | 1  |  | 1 |  | 1 |  | 2  |  | 2  |  | 2  |
      | 0  |  | 0 |  | 0  |  | 0 |  | 0 |  | -1 |  | -1 |  | -1 |

o47 : List
i48 : #L

o48 = 81

Evenmore the tail/recession cone of a polyhedron with tailCone.

i49 : C = tailCone P1

o49 = C

o49 : Cone
i50 : rays C

o50 = | 1 |
      | 0 |
      | 0 |

               3        1
o50 : Matrix ZZ  <--- ZZ

Finally, there is also a function to compute the polar of a polyhedron, i.e. all points in the dual space that are greater than -1 on all points of the polyhedron:

i51 : P12 = polar P11

o51 = P12

o51 : Polyhedron
i52 : vertices P12

o52 = | 1 -1 0  0 0  0  |
      | 1 1  -1 0 0  0  |
      | 0 0  0  0 1  -1 |
      | 0 0  0  1 -1 -1 |

               4        6
o52 : Matrix QQ  <--- QQ