{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# What is CddInterface\n",
    "\n",
    "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",
    "\n",
    "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."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# H-representation of polyhedra\n",
    "\n",
    "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",
    "$b+Ax \\geq 0$ of $m$ inequalities and $d$ variables $x= (x_1,\\dots,x_d)$ is as follows:\n",
    "\n",
    "    H-representation\n",
    "    linearity t, [i_1, i_2, ...,i_t]\n",
    "    begin\n",
    "    m x (d+1) numbertype\n",
    "    b     A\n",
    "    end\n",
    "\n",
    "The linearity line is added when we want to specify that some rows of the system $b+Ax$ are equalities.\n",
    "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",
    "\n",
    "For example, the H-representation of the polyhedron defined by the following system:\n",
    "\n",
    "$$4-3x_1+6x_2-5x_4 = 0$$\n",
    "$$1+2x_1-2x_2-7x_3 \\geq 0$$\n",
    "$$-3x_2+5x_4 = 0$$\n",
    "\n",
    "is the following\n",
    "\n",
    "    H-representation\n",
    "    linearity 2, [ 1, 3 ]\n",
    "    begin\n",
    "    3 x 5 rational\n",
    "    \n",
    "    4 -3  6  0 -5\n",
    "    1  2 -2 -7  0\n",
    "    0  0 -3  0  5\n",
    "    end\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "true"
      ]
     },
     "execution_count": 10,
     "metadata": {
      "text/plain": ""
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "LoadPackage( \"CddInterface\" );"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<object>"
      ]
     },
     "execution_count": 22,
     "metadata": {
      "text/plain": ""
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "P1 := Cdd_PolyhedronByInequalities( [ [ 4, -3, 6, 0, -5 ], [ 1, 2, -2, -7, 0 ], [ 0, 0, -3, 0, 5 ] ], [ 1, 3 ] );"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "H-representation \n",
      "linearity 2, [ 1, 3 ]\n",
      "begin \n",
      "   3 X 5  rational\n",
      "                       \n",
      "   4  -3   6   0  -5 \n",
      "   1   2  -2  -7   0 \n",
      "   0   0  -3   0   5 \n",
      "end\n"
     ]
    }
   ],
   "source": [
    "Display( P1 );"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# V-representation of polyhedra\n",
    "\n",
    "Let $P$ be represented by $n$ gerating points and $s$ generating\n",
    "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",
    "for $P$ is:\n",
    "\n",
    "    V-representation\n",
    "    linearity t, [i_1, i_2,...,i_t]\n",
    "    begin\n",
    "    (n+s) x (d+1) numbertype\n",
    "    1  v_1\n",
    "    1  v_2\n",
    "      ..\n",
    "    1  v_n\n",
    "    0  r_1\n",
    "    0  r_2\n",
    "      ..\n",
    "    0  r_s\n",
    "    end\n",
    "\n",
    "In the above format the generating points and generating rays may appear mixed in arbitrary order. \n",
    "Linearity for V-representation specifies a subset of generators whose coefficients are relaxed to\n",
    "be free. That is, $k \\in \\{i_1 , i_2 , . . . , i_t \\}$ specifies that the $k$-th generator is specified to be free.\n",
    "This means for each such a ray $r_k$ , the line generated by $r_k$ is in the polyhedron,\n",
    "and for each such a vertex $v_k$ , its coefficient is no longer nonnegative but still the coefficients for all\n",
    "$v_i$’s must sum up to one.\n",
    "\n",
    "For example the V-representation of the polyhedron defined as \n",
    " $$P:= \\mathrm{conv}(\\; (2,3), (-2,-3), (3,5)\\; ) + \\mathrm{nonneg}(\\; (1,2) , (-1,-2), (2,11)\\;)$$\n",
    "\n",
    "is the following\n",
    "\n",
    "    V-representation\n",
    "    linearity 2, [1, 3]\n",
    "    begin\n",
    "    4 x 3 rational\n",
    "    1  2  3\n",
    "    1  3  5\n",
    "    0  1  2\n",
    "    0  2  11\n",
    "    end\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<object>"
      ]
     },
     "execution_count": 24,
     "metadata": {
      "text/plain": ""
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "P2 := Cdd_PolyhedronByGenerators( [ [ 1, 2, 3 ], [ 1, 3, 5 ], [ 0, 1, 2 ], [ 0, 2, 11 ] ], [ 1, 3] );"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "V-representation \n",
      "linearity 2, [ 1, 3 ]\n",
      "begin \n",
      "   4 X 3  rational\n",
      "               \n",
      "   1   2   3 \n",
      "   1   3   5 \n",
      "   0   1   2 \n",
      "   0   2  11 \n",
      "end\n"
     ]
    }
   ],
   "source": [
    "Display( P2 );"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# H-Representation $\\leftrightarrow$ V-Representation\n",
    "\n",
    "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`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<object>"
      ]
     },
     "execution_count": 26,
     "metadata": {
      "text/plain": ""
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vP1 := Cdd_V_Rep( P1 );"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "V-representation \n",
      "linearity 1, [ 3 ]\n",
      "begin \n",
      "   3 X 5  rational\n",
      "                                      \n",
      "   1    4/3      0  11/21      0 \n",
      "   0      0      0     -1      0 \n",
      "   0      5      5      0      3 \n",
      "end\n"
     ]
    }
   ],
   "source": [
    "Display( vP1 );"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<object>"
      ]
     },
     "execution_count": 28,
     "metadata": {
      "text/plain": ""
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hP2 := Cdd_H_Rep( P2 );"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "H-representation \n",
      "begin \n",
      "   1 X 3  rational\n",
      "               \n",
      "   1  -2   1 \n",
      "end\n"
     ]
    }
   ],
   "source": [
    "Display( hP2 );"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "GAP 4",
   "language": "gap",
   "name": "gap-4"
  },
  "language_info": {
   "codemirror_mode": "gap",
   "file_extension": ".g",
   "mimetype": "text/x-gap",
   "name": "GAP 4",
   "nbconvert_exporter": "",
   "pygments_lexer": "gap",
   "version": "4.dev"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
