{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Regular subdivisons\n",
    "\n",
    "*Regular subdivsions* of point sets appear in several different applications. `polymake` allows to define regular subdivisions of point configurations (e.g. the lattice points of a lattice polytope) via weights on the points. The weights define a *height function* on the points, and the subdivision is described by the lower hull of the polytope defined by the lifted points (see [here](http://link.springer.com/book/10.1007%2F978-3-642-12971-1) for example to get an idea of the mathematical backround). Let us look at an example on how to create a regular subdivision.\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$M = new Matrix<Rational>([[1,0,0],[1,2,0],[1,1,1],[1,0,2],[1,2,2],[1,1,3]]);\n",
    "$w = new Vector<Rational>([0,0,1,0,0,3]);\n",
    "$S = new fan::SubdivisionOfPoints(POINTS=>$M,WEIGHTS=>$w);\n",
    "print $S->MAXIMAL_CELLS;"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    \n",
    "    {0 1 3 4}\n",
    "    {3 4 5}\n",
    "\n",
    "\n",
    "The six points (given by the six rows of the matrix `M`) define a pentagon with interior point `(1,1)`. The weights `w` must be given in the same order as the vertices. So in our case the interior point has weight `1`. Switching to the `fan` application we may create the regular subdivision via the polymake object `SubdivisionOfPoints`, whose input parameters are the points `M` and weights `w`. A maximal cell of the subdivision is given by a set of indices, representing the points that cell contains. In our example we got two maximal cells and the first one is a quadrilateral with vertices 0, 1, 3 and 4.\n",
    "\n",
    "We may visualize the regular subdivision if it is at most three-dimensional. \n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$S->VISUAL;"
   ]
  },
  {
   "attachments": {
    "subdiv_of_points_1.gif": {
     "image/gif": [
      "[encoded data, 4536 characters]"
     ]
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "If you use javaview for visualization, then this might look similar to the following:\n",
    "\n",
    "![{{:tutorial:subdiv_of_points_1.gif?300|}}](attachment:subdiv_of_points_1.gif)\n",
    "\n",
    "Note that the quadrilateral contains point 2 in its interior and that this point is colored black. This corresponds to the fact that the lifted point 2 lies above the convex hull of the lifted points 0,1,3 and 4. Therefore the maximal cell describing the quadrilateral does not contain the point 2. We may change the lifting function by giving point 2 height `0` as well:\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$w = new Vector<Rational>([0,0,0,0,0,3]);\n",
    "$S = new fan::SubdivisionOfPoints(POINTS=>$M,WEIGHTS=>$w);\n",
    "print $S->MAXIMAL_CELLS;"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    \n",
    "    {0 1 2 3 4}\n",
    "    {3 4 5}\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$S->VISUAL;"
   ]
  },
  {
   "attachments": {
    "subdiv_of_points_2.gif": {
     "image/gif": [
      "[encoded data, 4452 characters]"
     ]
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "![{{:tutorial:subdiv_of_points_2.gif?300|}}](attachment:subdiv_of_points_2.gif)\n",
    "\n",
    "This new height function generates the same polyhedral complex, yet a different subdivision of the point set. Note that the maximal cell that describes the quadrilateral now contains the point 2, but it is not a vertex of that cell. This is the reason for the yellow coloring. This corresponds to the fact that its lifting lies in the convex hull of the lifted quadrilateral, whereat it is not a vertex of the lifted polytope. Changing the height of point 2 again to `-1` the subdivision should become a triangulation of the point set. \n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$w = new Vector<Rational>([0,0,-1,0,0,3]);\n",
    "$S = new fan::SubdivisionOfPoints(POINTS=>$M,WEIGHTS=>$w);\n",
    "print $S->MAXIMAL_CELLS;"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    \n",
    "    {0 2 3}\n",
    "    {0 1 2}\n",
    "    {1 2 4}\n",
    "    {2 3 4}\n",
    "    {3 4 5}\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$S->VISUAL;"
   ]
  },
  {
   "attachments": {
    "subdiv_of_points_3.gif": {
     "image/gif": [
      "[encoded data, 5836 characters]"
     ]
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "![{{:tutorial:subdiv_of_points_3.gif?300|}}](attachment:subdiv_of_points_3.gif)\n",
    "\n",
    "Indeed, the regular subdivision arising from this height function is a triangulation. Since in this case point 2 is a vertex of the subdivision it is colored red as well.\n",
    "\n",
    "### Generic Weights\n",
    "\n",
    "The weights `w` are called *generic* if they define a triangulation of the point set. If it is known in advance that the given weights are generic, then we can obtain the subdivison as a simplicial complex. If you want analyse your simplicial complex then you might want to use the polymake objects: `SimplicialComplex` or `GeometricSimplicialComplex`. The first one is a purely combinatoric version. The latter one has an embedding into a space via the property `COORDINATES`. Both objects live in the application `topaz`. The weights in the third example above are generic, so they define a simplicial complex.\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$SC = new topaz::GeometricSimplicialComplex(COORDINATES=>$M, INPUT_FACES=>regular_subdivision($M,$w));"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "Note that `regular_subdivision($M,$w)` produces a list of the maximal cells in the regular subdivision of the point configuration `M` respecting the weights `w`.\n",
    "Now we can ask for many other properties, e.g.\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6 10 5\n"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print $SC->F_VECTOR;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1 3 1 0\n",
       "\n"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print $SC->H_VECTOR;"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Check the properties of `SimplicialComplex` for more available properties.\n",
    "\n",
    "\n",
    "\n",
    "### Non-generic Weights\n",
    "\n",
    "If your weights are not generic or you are unsure, then you should create a `PolyhedralComplex`. Since every `SubdivisionOfPoints` Object has its underlying `PolyhedralComplex` as a property this can be done simply by:\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$M = new Matrix<Rational>([[1,0,0,0],[1,0,1,0],[1,1,0,0],[1,1,1,0],[1,0,0,1],[1,1,0,1],[1,0,1,1],[1,1,1,1],[1,0,0,2]]);\n",
    "$w = new Vector<Rational>([1,0,0,1,0,1,1,0,1]);\n",
    "$S = new fan::SubdivisionOfPoints(POINTS=>$M,WEIGHTS=>$w);\n",
    "$PC = $S->POLYHEDRAL_COMPLEX;\n",
    "print $PC->MAXIMAL_POLYTOPES;"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    \n",
    "    {0 1 2 4}\n",
    "    {2 4 5 7 8}\n",
    "    {1 2 3 7}\n",
    "    {1 2 4 7}\n",
    "    {1 4 6 7 8}\n",
    "    \n",
    "\n",
    "\n",
    "As with an simplicial complex you can do some computation with it. For example:\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8 18 16 5\n"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print $PC->F_VECTOR;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print $PC->SIMPLICIAL;"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$PC->VISUAL;"
   ]
  },
  {
   "attachments": {
    "reg_subdiv_polytopal_subdiv.gif": {
     "image/gif": [
      "R0lGODlh+gDfAMYAAP///wAAALQAAF68fT0AACBBK0ePX/Ly8ufn5/z8/FdXV0RERDo6OvX19X5+fj4+PqqqqlFRUUxMTJ2dneR+fq2trRsbG+Pj4/j4+CsrK9PT01lZWRQUFEFBQaenp/Dw8Dw8PFxcXAUFBW1tbfr6+mNjY8XFxRYWFoeHhx8fH7u7u4iIiDs7O2tra1VVVVtbW9LS0jc3NyoqKuLi4ubm5vHx8VEAAIoAAG0AABMoGuF+fhImGQMHBAcPCidPNQkTDAQJBQgRCwMGBAkUDA0cEgQKBgsXDxEkFwIGAxAjFgULBwwZEAABAQYOCQQIBYKCggcQCgsYEAYMCLS0tGVlZd0AAFOmb0sAAIAAANzc3CkpKUdHR6Ghob29vRMTE/T09Kh+ftra2unp6XZ2dqysrCEhIbe3tyAgINHR0WFhYe/v79bW1jsAAGfOicsAAJsAADQAAF26fF67fF27fFBSN167fSlSN1y5e1y4e1YAAEeOX7MAAP///////////////ywAAAAA+gDfAAAH/oAAgoOEhYaHiImKi4yNjo+QkZKTlJWWl5iZmpucnZ6foKGio6SlpqeoigGprK2ur6ECsrC0tba3hwECq7i9vr+mA7zAxMXGlAPJx8vMtgEEkAHJws3V1qYEBMON0wPX3+Cd2ZHd1OHn6JLb3OXr6e/whAbuquXK8fjxBgXk9ub5AMHN4/fI37SACK0FCFCAHiKDyRwmnIgrwD6CjaRB9Eaxoy+LBR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gHOHQCqd07kDvhd102kpg4b3i+fCBGSu6Tq03W+Vda3EsrOBOMfiXEiSWgWFHGBqP2FA3RuQfk6YZGVe4bbsqLIqxEqMVM9bH0t1VkK2Q46eJpnP0gCLtvKVXKyvGyK+F8o9nNWX26u7Lm3OIiLtbLqee+S48PsSaUyWvMlcSTykCM4Jpd5d84XTJHEUz5UpD34YNDKeAyjP4EDFnNudryvVLRBZOpGjJSaTQhr5YkCNNCDBEBQBiAJFThvcSTB+sZBkexAPAAAYAnKG5w1OFmB2t6YUuBACeXsgaClw71yai0eI63kLZwAau/rbO0ooANq1RfciFENvYsU42n2dzNEkupA3YrtQwe52Dl3D3cEENGrbHTW5n+to+sw526shdbmMjO9npphbZ2D3uALgBt5JjhKm5lrYA0NsNbgC0eEin72ln9mv+xvYqAD7XyLEk3uqGGxm7/XBwM9VvHOFqDhqh7IYdV1lvEPjAOW7w2GI8AG94w48gs+/DYZwaIV95RiB+6n6DCQ5wuErH9UY4OOGc4zQH8NREPhctW3zMCiN6X3aO5I8p3S9Mt1rSn46YqCttYFSPTMuP/nHf0EsrWz/6C7+u76BLncxkJ/nR6eyorCPHAWHnejnmMIA5xCEOF3J7dAJAh7Uvn3sOc6jDAOTwobRHIw928DvbkwH4ORCe7po1vCTykIeSR5zxycB7SSWvjsorvrTX5PwlLH95/Ij+Ep9fvHNOr4nUS8r0rH+V65Eel9hvgvRCN6vtQTX7Gvd195vovaImC/zbC5/2jym+J+yth+NP6EzK9wTA7w053CNd7xtLC+QsHrfof2P7wtnDHrDvfUwEQPzkL7/51c/+9rv//ZwIBAA7"
     ]
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "![{{:tutorial:reg_subdiv_polytopal_subdiv.gif?300}}](attachment:reg_subdiv_polytopal_subdiv.gif)\n",
    "\n",
    "Check the interactive help to get more information.\n",
    "For the image we have used the javaview option *Explode Group of Geometries* to make the cells of the subdivision visible. \n",
    "\n",
    "### Tropical Plücker Vectors and Matroid Decompositions of Hypersimplices\n",
    "\n",
    "A *tropical Plücker vector* (which is a special lifting function on the vertices of the *(d,n)*-hypersimplex induces a particularly interesting kind of regular subdivision.  The example below is for *d=2* and *n=4*.\n",
    " \n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$p=new Vector<Rational>([1,0,0,0,0,0]);\n",
    "$msd=regular_subdivision(hypersimplex(2,4)->VERTICES,$p);"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "Each cell of this subdivision is a *matroid polytope*, that is, the convex hull of characteristic vectors of the bases of some matroid on *n* elements of rank *d*.  The vertices of the hypersimplices happen to be listed in lexicographical ordering.  With this extra knowledge we can cook up suitable labels for pretty-printing the maximal cells. \n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0:12 13 14 23 24\n",
       "1:13 14 23 24 34\n",
       "\n"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print rows_labeled($msd,undef,[\"12\",\"13\",\"14\",\"23\",\"24\",\"34\"]);"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "In this case the *(2,4)*-hypersimplex (a.k.a. the regular octahedron) is split into two egyptian pyramids.\n",
    "\n",
    "## Secondary cone and regularity of subdivisions\n",
    "\n",
    "The set of all the weight vectors which induce the same regular subdivision form a convex cone. In this section we will learn how to compute that cone with polymake and even how we can check whether a given subdivision is regular or not.\n",
    "\n",
    "### Secondary Cone of a Regular Subdivision\n",
    "\n",
    "Let us look at the following regular subdivision (using homogeneous coordinates):\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$points = new Matrix([[1,2,0],[1,0,2],[1,-2,0],[1,0,-2],[1,1,0],[1,0,1],[1,-1,0],[1,0,-1]]);\n",
    "$cells = new Array<Set<Int>>([[0,1,4,5],[0,3,4,7],[2,3,6,7],[1,2,5,6],[4,5,6,7]]);\n",
    "$S = new fan::SubdivisionOfPoints(POINTS=>$points, MAXIMAL_CELLS=>$cells);"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "we can visualize the subdivision with the following command:\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$S->VISUAL;"
   ]
  },
  {
   "attachments": {
    "square_reg_subdiv.png": {
     "image/png": [
      "[encoded data, 10696 characters]"
     ]
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "![{{:tutorial:square_reg_subdiv.png?400|}}](attachment:square_reg_subdiv.png)\n",
    "\n",
    "As one can see from the picture this subdivision should be regular, since we can easily find a weight vector which induces this subdivision. Just lift all the points in the inner square to 0 and the points on the outer square to 1. But now we want to take a look at all vectors which induce this subdivision. This can be achieved by using the method `secondary_cone`.\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4\n",
       "    \n"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "$sc = $S->secondary_cone();\n",
    "print $sc->DIM;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1 0 0 1 0 -1/2 -1/2 0\n",
       "    \n"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print $sc->RAYS;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3/2 3/2 -1/2 -1/2 1 1 0 0\n",
       "-26/19 12/19 34/19 -4/19 -11/19 8/19 1 0\n",
       "50/49 -8/49 10/49 68/49 40/49 11/49 20/49 1\n",
       "\n"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print $sc->LINEALITY_SPACE;"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We see that this cone is 4-dimensional with a 3-dimensional lineality space. Every vector of the **interior** of that cone induces weights that produces the same regular subdivision. The vectors on the boundary give rise to a coarsening. As you see by looking at the coordinates, we do not need to lift the inner square to 0, there are also weight vectors which lift them in a different fashion but still produce the same subdivision.\n",
    "\n",
    "With the options `lift_to_zero` or `lift_face_to_zero` one can specify points or a face which must be lifted to zero. Let us see what happens if we force polymake to only allow weights that lift the inner square to zero. As we see from our input, the face which describes the inner square is the fifth one (but since polymake starts counting at zero, it has number 4).\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$sc_fixed = $S->secondary_cone(lift_face_to_zero=>4);"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1\n",
       "    \n"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print $sc_fixed->DIM;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1 1 1 1 0 0 0 0\n",
       "    \n"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print $sc_fixed->RAYS;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   \n",
       "\n"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print $sc_fixed->LINEALITY_SPACE;"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now you see that the lineality space of that cone is gone. The only way to lift our points now is by raising all points on the outer square to the same height which should be greater than 0.\n",
    "\n",
    "### Regularity of Subdivisions\n",
    "\n",
    "You might run into the situation where you want to check whether a subdivision is regular or not. Don't worry, polymake comes to rescue along with it's side-kick the `is_regular` function. The function takes the same input as the `secondary_cone` function. The output is a pair of a boolean and a vector. The boolean tells you whether or not the subdivision is regular and the vector is a weight vector which induces your subdivision. Let us check if our subdivision of the example above is regular.\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "true <0 0 1 1 0 0 1/2 1/2>\n",
       "    \n"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print is_regular($points, $cells);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$lambda = new Vector(\"0 0 1 1 0 0 1/2 1/2\");"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{4 5 6 7}\n",
       "{2 3 6 7}\n",
       "{1 2 5 6}\n",
       "{0 3 4 7}\n",
       "{0 1 4 5}\n",
       "\n"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print regular_subdivision($points, $lambda);"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As you can see polymake tells us that our subdivision is indeed regular. And the vector `0 0 1 1 0 0 1/2 1/2` produces the same subdivision we started with. Now let us see what happens if we use a subdivision which is not regular. We use the same points as above but different cells.\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$nreg_cells = new Array<Set<Int>>([[0,1,5],[0,4,5],[0,3,4],[3,4,7],[2,3,7],[2,6,7],[1,2,6],[1,5,6],[4,5,6,7]]);\n",
    "$nreg_pc = new fan::PolyhedralComplex(POINTS=>$points, INPUT_POLYTOPES=>$nreg_cells);\n",
    "$nreg_pc->VISUAL;"
   ]
  },
  {
   "attachments": {
    "square_nreg_subdiv.png": {
     "image/png": [
      "[encoded data, 15612 characters]"
     ]
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "![{{:tutorial:square_nreg_subdiv.png?400|}}](attachment:square_nreg_subdiv.png)\n",
    "\n",
    "This is quite similar to the `mother of all examples` (See: \"Triangulations\" from DeLoera, Rambau and Santos). So with a similar argument we can see that this subdivision is indeed not regular. How does polymake feel about this?\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "false <>\n",
       "\n"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print is_regular($points, $nreg_cells);"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Just as expected polymake tells us that the subdivision is not regular. But although there is no weight function which induces this particular subdivision we can still create a `SubdivisionOfPoints` object with the given cells. What happens if we ask for the secondary cone of that subdivision?\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4\n",
       "\n"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "$nreg_sub = new fan::SubdivisionOfPoints(POINTS=>$points, MAXIMAL_CELLS=>$nreg_cells);\n",
    "$nreg_sc = $nreg_sub->secondary_cone();\n",
    "print $nreg_sc->DIM;"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This might be weird at first. Although our subdivision is not regular (meaning there does not exist a secondary cone for that subdivision) the secondary cone seems to be 4 dimensional. Let us investigate.\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1 1 1 1 0 0 0 0\n",
       "    \n"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print $nreg_sc->RAYS;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3/2 3/2 -1/2 -1/2 1 1 0 0\n",
       "-26/19 12/19 34/19 -4/19 -11/19 8/19 1 0\n",
       "50/49 -8/49 10/49 68/49 40/49 11/49 20/49 1\n",
       "\n"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print $nreg_sc->LINEALITY_SPACE;"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This looks familiar, doesn't it? We got the same secondary cone as we did in our first example. The reason for this is that the first example is the finest regular coarsening of our non regular subdivision. And this is exactly what we see here. In other words, the subdivision of our first example is the closest regular thing similar to our non regular subdivision and this is exactly what the `secondary_cone` function produces. If a subdivision is not regular it gives you the secondary cone of the next best regular subdivision.\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "polymake",
   "language": "polymake",
   "name": "polymake"
  },
  "language_info": {
   "codemirror_mode": "perl",
   "file_extension": ".pm",
   "mimetype": "text/x-polymake",
   "name": "polymake"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}