1{
2 "cells": [
3  {
4   "cell_type": "markdown",
5   "metadata": {},
6   "source": [
7    "# What is CddInterface\n",
8    "\n",
9    "Every convex polyhedron P has two representations, one as the intersection of finite halfspaces and the other as Minkowski sum of the convex hull of finite points and the nonnegative hull of finite directions. These are called H-representation and V-representation, respectively.\n",
10    "\n",
11    "CddInterface is a gap interface with the C package Cddlib which among other things can translate between H,V- representations of a polyhedron P and solve linear programming problems over P, i.e. a problem of maximizing and minimizing a linear function over P. A list of all available operations can be found in the manual.pdf."
12   ]
13  },
14  {
15   "cell_type": "markdown",
16   "metadata": {},
17   "source": [
18    "# H-representation of polyhedra\n",
19    "\n",
20    "Let $A$ be $m \\times d$ matrix and let $b$ be a column $m$-vector. The H-representation of the polyhedron defined by the system\n",
21    "$b+Ax \\geq 0$ of $m$ inequalities and $d$ variables $x= (x_1,\\dots,x_d)$ is as follows:\n",
22    "\n",
23    "    H-representation\n",
24    "    linearity t, [i_1, i_2, ...,i_t]\n",
25    "    begin\n",
26    "    m x (d+1) numbertype\n",
27    "    b     A\n",
28    "    end\n",
29    "\n",
30    "The linearity line is added when we want to specify that some rows of the system $b+Ax$ are equalities.\n",
31    "That is, $k\\in \\{i_1, i_2, \\dots,i_t\\}$ means that the row $k$ of the system $b+Ax$ is specified to be equality.\n",
32    "\n",
33    "For example, the H-representation of the polyhedron defined by the following system:\n",
34    "\n",
35    "$$4-3x_1+6x_2-5x_4 = 0$$\n",
36    "$$1+2x_1-2x_2-7x_3 \\geq 0$$\n",
37    "$$-3x_2+5x_4 = 0$$\n",
38    "\n",
39    "is the following\n",
40    "\n",
41    "    H-representation\n",
42    "    linearity 2, [ 1, 3 ]\n",
43    "    begin\n",
44    "    3 x 5 rational\n",
45    "    \n",
46    "    4 -3  6  0 -5\n",
47    "    1  2 -2 -7  0\n",
48    "    0  0 -3  0  5\n",
49    "    end\n"
50   ]
51  },
52  {
53   "cell_type": "code",
54   "execution_count": 10,
55   "metadata": {},
56   "outputs": [
57    {
58     "data": {
59      "text/plain": [
60       "true"
61      ]
62     },
63     "execution_count": 10,
64     "metadata": {
65      "text/plain": ""
66     },
67     "output_type": "execute_result"
68    }
69   ],
70   "source": [
71    "LoadPackage( \"CddInterface\" );"
72   ]
73  },
74  {
75   "cell_type": "code",
76   "execution_count": 22,
77   "metadata": {},
78   "outputs": [
79    {
80     "data": {
81      "text/plain": [
82       "<object>"
83      ]
84     },
85     "execution_count": 22,
86     "metadata": {
87      "text/plain": ""
88     },
89     "output_type": "execute_result"
90    }
91   ],
92   "source": [
93    "P1 := Cdd_PolyhedronByInequalities( [ [ 4, -3, 6, 0, -5 ], [ 1, 2, -2, -7, 0 ], [ 0, 0, -3, 0, 5 ] ], [ 1, 3 ] );"
94   ]
95  },
96  {
97   "cell_type": "code",
98   "execution_count": 23,
99   "metadata": {},
100   "outputs": [
101    {
102     "name": "stdout",
103     "output_type": "stream",
104     "text": [
105      "H-representation \n",
106      "linearity 2, [ 1, 3 ]\n",
107      "begin \n",
108      "   3 X 5  rational\n",
109      "                       \n",
110      "   4  -3   6   0  -5 \n",
111      "   1   2  -2  -7   0 \n",
112      "   0   0  -3   0   5 \n",
113      "end\n"
114     ]
115    }
116   ],
117   "source": [
118    "Display( P1 );"
119   ]
120  },
121  {
122   "cell_type": "markdown",
123   "metadata": {},
124   "source": [
125    "# V-representation of polyhedra\n",
126    "\n",
127    "Let $P$ be represented by $n$ gerating points and $s$ generating\n",
128    "directions (rays) as $$P = \\mathrm{conv}(v_1 , \\dots , v_n ) + \\mathrm{nonneg}(r_{1} , \\dots , r_{s} ).$$ Then the Polyhedra V-format is \n",
129    "for $P$ is:\n",
130    "\n",
131    "    V-representation\n",
132    "    linearity t, [i_1, i_2,...,i_t]\n",
133    "    begin\n",
134    "    (n+s) x (d+1) numbertype\n",
135    "    1  v_1\n",
136    "    1  v_2\n",
137    "      ..\n",
138    "    1  v_n\n",
139    "    0  r_1\n",
140    "    0  r_2\n",
141    "      ..\n",
142    "    0  r_s\n",
143    "    end\n",
144    "\n",
145    "In the above format the generating points and generating rays may appear mixed in arbitrary order. \n",
146    "Linearity for V-representation specifies a subset of generators whose coefficients are relaxed to\n",
147    "be free. That is, $k \\in \\{i_1 , i_2 , . . . , i_t \\}$ specifies that the $k$-th generator is specified to be free.\n",
148    "This means for each such a ray $r_k$ , the line generated by $r_k$ is in the polyhedron,\n",
149    "and for each such a vertex $v_k$ , its coefficient is no longer nonnegative but still the coefficients for all\n",
150    "$v_i$’s must sum up to one.\n",
151    "\n",
152    "For example the V-representation of the polyhedron defined as \n",
153    " $$P:= \\mathrm{conv}(\\; (2,3), (-2,-3), (3,5)\\; ) + \\mathrm{nonneg}(\\; (1,2) , (-1,-2), (2,11)\\;)$$\n",
154    "\n",
155    "is the following\n",
156    "\n",
157    "    V-representation\n",
158    "    linearity 2, [1, 3]\n",
159    "    begin\n",
160    "    4 x 3 rational\n",
161    "    1  2  3\n",
162    "    1  3  5\n",
163    "    0  1  2\n",
164    "    0  2  11\n",
165    "    end\n",
166    "    "
167   ]
168  },
169  {
170   "cell_type": "code",
171   "execution_count": 24,
172   "metadata": {},
173   "outputs": [
174    {
175     "data": {
176      "text/plain": [
177       "<object>"
178      ]
179     },
180     "execution_count": 24,
181     "metadata": {
182      "text/plain": ""
183     },
184     "output_type": "execute_result"
185    }
186   ],
187   "source": [
188    "P2 := Cdd_PolyhedronByGenerators( [ [ 1, 2, 3 ], [ 1, 3, 5 ], [ 0, 1, 2 ], [ 0, 2, 11 ] ], [ 1, 3] );"
189   ]
190  },
191  {
192   "cell_type": "code",
193   "execution_count": 25,
194   "metadata": {},
195   "outputs": [
196    {
197     "name": "stdout",
198     "output_type": "stream",
199     "text": [
200      "V-representation \n",
201      "linearity 2, [ 1, 3 ]\n",
202      "begin \n",
203      "   4 X 3  rational\n",
204      "               \n",
205      "   1   2   3 \n",
206      "   1   3   5 \n",
207      "   0   1   2 \n",
208      "   0   2  11 \n",
209      "end\n"
210     ]
211    }
212   ],
213   "source": [
214    "Display( P2 );"
215   ]
216  },
217  {
218   "cell_type": "markdown",
219   "metadata": {},
220   "source": [
221    "# H-Representation $\\leftrightarrow$ V-Representation\n",
222    "\n",
223    "The V-representation of a polyhedron can be computed via the command `Cdd_V_Rep` and the H-representation via the command `Cdd_H_Rep`."
224   ]
225  },
226  {
227   "cell_type": "code",
228   "execution_count": 26,
229   "metadata": {},
230   "outputs": [
231    {
232     "data": {
233      "text/plain": [
234       "<object>"
235      ]
236     },
237     "execution_count": 26,
238     "metadata": {
239      "text/plain": ""
240     },
241     "output_type": "execute_result"
242    }
243   ],
244   "source": [
245    "vP1 := Cdd_V_Rep( P1 );"
246   ]
247  },
248  {
249   "cell_type": "code",
250   "execution_count": 27,
251   "metadata": {},
252   "outputs": [
253    {
254     "name": "stdout",
255     "output_type": "stream",
256     "text": [
257      "V-representation \n",
258      "linearity 1, [ 3 ]\n",
259      "begin \n",
260      "   3 X 5  rational\n",
261      "                                      \n",
262      "   1    4/3      0  11/21      0 \n",
263      "   0      0      0     -1      0 \n",
264      "   0      5      5      0      3 \n",
265      "end\n"
266     ]
267    }
268   ],
269   "source": [
270    "Display( vP1 );"
271   ]
272  },
273  {
274   "cell_type": "code",
275   "execution_count": 28,
276   "metadata": {},
277   "outputs": [
278    {
279     "data": {
280      "text/plain": [
281       "<object>"
282      ]
283     },
284     "execution_count": 28,
285     "metadata": {
286      "text/plain": ""
287     },
288     "output_type": "execute_result"
289    }
290   ],
291   "source": [
292    "hP2 := Cdd_H_Rep( P2 );"
293   ]
294  },
295  {
296   "cell_type": "code",
297   "execution_count": 29,
298   "metadata": {},
299   "outputs": [
300    {
301     "name": "stdout",
302     "output_type": "stream",
303     "text": [
304      "H-representation \n",
305      "begin \n",
306      "   1 X 3  rational\n",
307      "               \n",
308      "   1  -2   1 \n",
309      "end\n"
310     ]
311    }
312   ],
313   "source": [
314    "Display( hP2 );"
315   ]
316  },
317  {
318   "cell_type": "code",
319   "execution_count": null,
320   "metadata": {},
321   "outputs": [],
322   "source": []
323  }
324 ],
325 "metadata": {
326  "kernelspec": {
327   "display_name": "GAP 4",
328   "language": "gap",
329   "name": "gap-4"
330  },
331  "language_info": {
332   "codemirror_mode": "gap",
333   "file_extension": ".g",
334   "mimetype": "text/x-gap",
335   "name": "GAP 4",
336   "nbconvert_exporter": "",
337   "pygments_lexer": "gap",
338   "version": "4.dev"
339  }
340 },
341 "nbformat": 4,
342 "nbformat_minor": 2
343}
344