{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Groups in polymake\n",
    "\n",
    "`polymake` can deal with symmetry groups acting on polytopes, point/vector configurations and simplicial complexes. This can take the guise of permutation or matrix groups acting on the set of vertices (points), facets, or coordinates of realizations of polytopes or point/vector configurations, or abstract permutation groups acting on the set of vertices or facets of a simplicial complex.\n",
    " \n",
    "Some functionality builds on `PermLib`, a `C++`-library for permutation computations written by [Thomas Rehn](http://www.math.uni-rostock.de/~rehn/index.html), but much functionality is built in natively into `polymake`. For example, you can natively calculate conjugation classes of permutation or matrix groups, projectors to the isotypic components of representations, or the invariant polynomials of a matrix representation.\n",
    "\n",
    "\n",
    "## General properties of groups\n",
    "\n",
    "We start with the description of permutation groups in `polymake`. An object of type [ Group](https///polymake.org/release_docs/latest/group.html#group__Group__5 ) records the abstract properties of the groups that do not depend on any particular representation, which essentially are just the `ORDER`, `CHARACTER_TABLE`, and `CONJUGACY_CLASS_SIZES`. Moreover, a `Group` object can contain several subobjects that encode actions (representations) of the group, most notably a `PERMUTATION_ACTION` that encodes permutations of indices. If the Group object is contained inside a `Cone`, `Polytope`, `PointConfiguration`, or `VectorConfiguration`, it may be encoded more specifically as a `RAY_ACTION`, `FACET_ACTION`, etc. See the [ documentation](https///polymake.org/doku.php/release_docs/latest/group.html ) for more information on the action types.\n",
    "\n",
    "In order to access the complete set of functions dealing with groups, you should switch to the corresponding [ application](howto/lingo# application).\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "application \"group\";"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "### Conjugacy classes and character tables\n",
    "\n",
    "As we mentioned before, the only properties that are stored directly in the `Group` object are the order, the sizes of the conjugacy classes, and sometimes the character table. Other properties, such as representatives for the conjugacy classes themselves, depend on the action chosen, and are thus stored inside the corresponding `*_ACTION` object. \n",
    "\n",
    "For the symmetric groups, we currently include the `CHARACTER_TABLE` up to degree 7; for the `group::dihedral_group` we provide the conjugacy classes in full generality, and the `CHARACTER_TABLE` in an exact form for the dihedral groups of order 10, 16, 20, and 24 (where it can be expressed using a single quadratic extension of the rationals). For the other dihedral groups, we provide `AccurateFloat` representations of the entries of the character table, as `polymake` currently cannot work with arbitrary cyclotomic integers.\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1 1 1 1 1 1 1 1\n",
       "1 1 1 1 1 1 -1 -1\n",
       "1 -1 1 -1 1 -1 1 -1\n",
       "1 -1 1 -1 1 -1 -1 1\n",
       "2 1/2+1/2r5 -1/2+1/2r5 1/2-1/2r5 -1/2-1/2r5 -2 0 0\n",
       "2 -1/2+1/2r5 -1/2-1/2r5 -1/2-1/2r5 -1/2+1/2r5 2 0 0\n",
       "2 1/2-1/2r5 -1/2-1/2r5 1/2+1/2r5 -1/2+1/2r5 -2 0 0\n",
       "2 -1/2-1/2r5 -1/2+1/2r5 -1/2+1/2r5 -1/2-1/2r5 2 0 0\n",
       "    \n"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print dihedral_group(20)->CHARACTER_TABLE;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1 1 1 1 1 1 1\n",
       "1 1 1 1 1 1 -1\n",
       "2 3788669096982621/2251799813685248 3741725795518811/4503599627370496 -2563716210467435/9007199254740992 -2949230557375555/2251799813685248 -8642344396869719/4503599627370496 0\n",
       "2 3741725795518811/4503599627370496 -2949230557375555/2251799813685248 -1080293049608715/562949953421312 -2563716210467439/9007199254740992 3788669096982621/2251799813685248 0\n",
       "2 -2563716210467435/9007199254740992 -1080293049608715/562949953421312 7483451591037615/9007199254740992 3788669096982621/2251799813685248 -2949230557375555/2251799813685248 0\n",
       "2 -2949230557375555/2251799813685248 -2563716210467439/9007199254740992 3788669096982621/2251799813685248 -8642344396869719/4503599627370496 7483451591037615/9007199254740992 0\n",
       "2 -8642344396869719/4503599627370496 3788669096982621/2251799813685248 -2949230557375555/2251799813685248 7483451591037615/9007199254740992 -2563716210467435/9007199254740992 0\n",
       "\n"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print dihedral_group(22)->CHARACTER_TABLE;"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "### Important notice\n",
    "\n",
    "For internal consistency, it is crucial that the `GROUP->*_ACTION->CONJUGACY_CLASS_REPRESENTATIVES` (and thus the `->CONJUGACY_CLASSES` themselves) be ordered in accordance with the columns of the `GROUP->CHARACTER_TABLE`. This is guaranteed to be the case for the character tables provided natively by `polymake`, but if you import character tables from GAP or other sources, correctly ordering the conjugacy classes is up to you.\n",
    "\n",
    "## Permutation groups\n",
    "\n",
    "An instance of a group action is created by specifying its property `GENERATORS`, a set of permutations or matrices that generates the group. The Group object itself is then constructed by passing the action. In the following example we create a symmetric group of degree 3, and then compute its order.\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "polymake: used package permlib\n",
       "  A callable C++ library for permutation computations. \n",
       "  Written by Thomas Rehn.\n",
       "  http://www.math.uni-rostock.de/~rehn/software/permlib.html \n",
       "    \n"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "$p = new PermutationAction(GENERATORS => [[1,0,2],[0,2,1]]);\n",
    "$g = new Group(PERMUTATION_ACTION => $p);\n",
    "print $g->ORDER;"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    6\n",
    "\n",
    "\n",
    "Of course, there is a user function for creating symmetric groups given the degree, as well as for several other standard constructions. See the [ docs](https///polymake.org/release_docs/latest/group.html#group__Producing_a_group__15 ) for a comprehensive list.\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1 0 2\n",
       "0 2 1\n",
       "\n"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "$h = symmetric_group(3);\n",
    "print $h->PERMUTATION_ACTION->GENERATORS;"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "### Properties of permutation actions\n",
    "\n",
    "We can compute some interesting properties of a PermutationAction:\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0 1 2\n",
       "0 2 1\n",
       "1 0 2\n",
       "1 2 0\n",
       "2 0 1\n",
       "2 1 0\n",
       "\n"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "$p = new PermutationAction(GENERATORS => [[1,0,2],[0,2,1]]);\n",
    "print all_group_elements($p);"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "There also exist basic functions to compute orbits and stabilizers, for instance:\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0 2 1\n"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "$p = new PermutationAction(GENERATORS => [[1,0,2],[0,2,1]]);\n",
    "$s = stabilizer_of_set($p,new Set<Int>(1,2));\n",
    "print $s->PERMUTATION_ACTION->GENERATORS;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0}\n",
       "{2 1}\n",
       "\n"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print $s->PERMUTATION_ACTION->ORBITS;"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "### A note on permutations in polymake\n",
    "\n",
    "`polymake` natively uses index notation for permutations: a permutation g ∈ S<sub>n</sub> is an `Array<Int> of length n with entries 0, . . . , n − 1 which corresponds to the second row of the common permutation notation. For instance, the permutation\n",
    "\n",
    "    \n",
    "    0 1 2\n",
    "    1 0 2\n",
    "\n",
    "which is equal to (0 1) ∈ S<sub>3</sub> in cyclic notation, is represented in `polymake` by the integer array [1, 0, 2]. Methods for conversion between the notation in `polymake` and the 1-based cyclic notation as used, for instance, in GAP are also available:\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1,2),\n",
       "(2,3)\n"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "$p = new PermutationAction(GENERATORS=>[[1,0,2],[0,2,1]]);\n",
    "print action_to_cyclic_notation($p);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0 2 3 4 1\n",
       "1 2 4 0 3\n",
       "\n"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "$AGL_1_5 = group_from_cyclic_notation1(\"(2,3,4,5), (1,2,3,5,4)\");\n",
    "print $AGL_1_5->PERMUTATION_ACTION->GENERATORS;"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "## Symmetry groups of polymake objects\n",
    "\n",
    "We switch to the polytope application for the following section:\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "application 'polytope';"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "### Polytopes\n",
    "\n",
    "There is more than one way to associate a group with any given polytope, depending on which kind of structural information you want to preserve. You can find some functions concerning symmetry groups of polytopes [ here](https///polymake.org/release_docs/latest/polytope.html#polytope__Symmetry__36 ). It is possibile to attach the group objects described above to polytopes or cones by using the property `GROUP`. As there are many possible groups that operate on a polytope, the property can contain multiple subobjects; see [ here](https///polymake.org/doku.php/scripting/start#multiple_subobjects ) for information on how to handle those.\n",
    "\n",
    "One interesting group is the group of *combinatorial* automorphisms, the ones preserving the face lattice. Since the face lattice of a polytope is atomic and coatomic this group coincides with group of (bipartite) graph automorphisms of the vertex/facet incidences.\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(<0 1 4 5 2 3> <0 1 4 5 2 3 6 7>)\n",
       "(<2 3 0 1 4 5> <0 2 1 3 4 6 5 7>)\n",
       "(<1 0 2 3 4 5> <1 0 3 2 5 4 7 6>)\n",
       "\n"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "$c = cube(3);\n",
    "$aut = automorphisms($c->VERTICES_IN_FACETS);\n",
    "print $aut;"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This says that the combinatorial automorphisms are generated by three elements, one per line in the output.  Each generator is written as a pair of permutations.  The first one gives the action on the FACETS, the second one gives the action on the VERTICES. Note that `automorphisms` does not necessarily output a minimal representation.\n",
    "\n",
    "Let's wrap some of this information up in a Group object:\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "@g = map { $_->first } @{$aut};\n",
    "$fperm = new group::PermutationAction(GENERATORS=>\\@g);\n",
    "$g = new group::Group(FACETS_ACTION=>$fperm);           # note how we use the FACETS_ACTION property this time\n",
    "$g->name = \"fullCombinatorialGroupOnFacets\";            # is is advisable to give multiple objects a meaningful name\n",
    "$c->add(\"GROUP\",$g);"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "Now we can, e.g., compute the generators of the action on the vertices from the action on the facets:\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0 1 4 5 2 3 6 7\n",
       "0 2 1 3 4 6 5 7\n",
       "1 0 3 2 5 4 7 6\n",
       "\n"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print $c->GROUP->VERTICES_ACTION->GENERATORS;"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "Many standard constructions of polytopes come with an option to compute the canonical symmetry group during construction in a more efficient way than computing the face lattice and then solving the graph automorphism problem. If you type the name of the function you want to execute and then hit F1 twice, the available options will be displayed. You will find a description of the action that will be computed too. For example, the following creates a cube, but with the action on the facets already attached:\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1 0 2 3 4 5\n",
       "2 3 0 1 4 5\n",
       "0 1 4 5 2 3\n",
       "\n"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "$cg = cube(3,group=>1);\n",
    "print $cg->GROUP->FACETS_ACTION->GENERATORS;"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "#### Orbit polytopes\n",
    "\n",
    "Given a group with either a `COORDINATE_ACTION` or a `MATRIX_ACTION`, you can calculate the convex hull of the orbits of a tuple of points:\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "48\n",
       "\n"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "$cg = cube(3,group=>1);\n",
    "print orbit_polytope(new Matrix([[1,1,2,1],[1,5/2,1,0]]), $cg->GROUP->MATRIX_ACTION)->N_VERTICES;"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "See [the documentation](https///polymake.org/release_docs/latest/polytope.html#polytope__orbit_polytope__319) for more options.\n",
    "\n",
    "### Quotient spaces\n",
    "\n",
    "One way of constructing interesting topological spaces is by identifying points on the boundary of a fundamental region. Polymake can do this in the case where the fundamental region is a convex polytope. For example, a cylinder is obtained by identifying opposite sides of a square, and the [quarter_turn_manifold()](https///polymake.org/release_docs/latest/polytope.html#polytope__quarter_turn_manifold__238) (see [here](http://www.math.cornell.edu/~dwh/books/eg99/Ch20/Ch20.html)) is obtained from the boundary of a 3-dimensional cube by identifying opposite faces by a quarter turn. \n",
    "\n",
    "For example, to obtain a topological space homeomorphic to a cylinder, type\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2 3 0 1\n"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "$p = cylinder_2();\n",
    "print $p->QUOTIENT_SPACE->IDENTIFICATION_ACTION->GENERATORS;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0 2}\n",
       "{1 3}\n"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print $p->QUOTIENT_SPACE->IDENTIFICATION_ACTION->ORBITS;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{{0} {1}}\n",
       "{{0 1} {0 2} {1 3}}\n",
       "{{0 1 2 3}}\n"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print $p->QUOTIENT_SPACE->FACES;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2 3 1\n",
       "\n"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print $p->QUOTIENT_SPACE->F_VECTOR;"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Thus, vertices 0,2 and vertices 1,3 of a square (a 2-dimensional cube) are identified, and after identification two vertices, three edges, and one two-dimensional face remain. In  order to get a simplicial complex without identifications among the vertices, you can calculate the second barycentric subdivision by asking for the property SIMPLICIAL_COMPLEX:\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "26 72 48\n"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print $p->QUOTIENT_SPACE->SIMPLICIAL_COMPLEX->F_VECTOR;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "({} 0)\n",
       "({} 0)\n",
       "({} 1)\n",
       "\n"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print $p->QUOTIENT_SPACE->SIMPLICIAL_COMPLEX->HOMOLOGY;"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "An easy way to make projective spaces is to identify opposite faces in a centrally symmetric polytope, using the function [cs_quotient()](https///polymake.org/release_docs/latest/polytope.html#polytope__cs_quotient__239). For example, to calculate the homology of real 3-dimensional projective space **RP<sup>3</sup>, write\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "({} 0)\n",
       "({(2 1)} 0)\n",
       "({} 0)\n",
       "({} 1)\n",
       "\n"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "$m = cs_quotient(cube(3));\n",
    "print $m->QUOTIENT_SPACE->SIMPLICIAL_COMPLEX->HOMOLOGY;"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "As another example, the [Davis Manifold](https///people.math.osu.edu/davis.12/old_papers/4-mfld.pdf) is a 4-dimensional hyperbolic manifold obtained by identifying opposite vertices of a 120-cell:\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "300 600 360 60 1\n",
       "\n"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "$m=davis_manifold();\n",
    "print $m->QUOTIENT_SPACE->F_VECTOR;"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Calculating the homology takes a little bit longer:\n",
    "\n",
    "   polytope > print $m->QUOTIENT_SPACE->SIMPLICIAL_COMPLEX->F_VECTOR;\n",
    "    94321 1146960 3644640 4320000 1728000\n",
    "   polytope > print $m->QUOTIENT_SPACE->SIMPLICIAL_COMPLEX->HOMOLOGY;\n",
    "    ({} 0)\n",
    "    ({(2 1)} 0)\n",
    "    ({} 0)\n",
    "    ({(2 1)} 0)\n",
    "    ({} 0)\n",
    "\n",
    "\n",
    "## Matrix groups\n",
    "\n",
    "Let's switch back to `group`.\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "application 'group';"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "Polymake can also deal with groups given by matrices that act on the ambient space. They are stored in the property `GROUP.MATRIX_ACTION`, and are paramterized by the number type of the matrices. One way to get a `MATRIX_ACTION` is to convert a permutation action on the vertices of a polytope:\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "type: Group as Polytope<QuadraticExtension<Rational>>::GROUP\n",
       "    \n"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "$d = polytope::dodecahedron();\n",
    "$d->GROUP->properties();"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    VERTICES_ACTION\n",
    "    type: PermutationAction<Int, Rational>\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$d->GROUP->MATRIX_ACTION;"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<1 0 0 0\n",
       "0 -1 0 0\n",
       "0 0 1 0\n",
       "0 0 0 1\n",
       ">\n",
       "<1 0 0 0\n",
       "0 1/4-1/4r5 1/2 -1/4-1/4r5\n",
       "0 1/2 1/4+1/4r5 -1/4+1/4r5\n",
       "0 -1/4-1/4r5 -1/4+1/4r5 1/2\n",
       ">\n",
       "<1 0 0 0\n",
       "0 1 0 0\n",
       "0 0 1 0\n",
       "0 0 0 -1\n",
       ">\n",
       "\n"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print $d->GROUP->MATRIX_ACTION->GENERATORS;"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As we can see, the property `MATRIX_ACTION` was calculated on the fly, specifically by solving matrix equations involving the `VERTICES` and `VERTICES_ACTION->GENERATORS`. Moreover, in this case the matrices are calculated exactly by adjoining the square root of 5 to the rationals.\n",
    "\n",
    "Of course, not every combinatorial symmetry group of a concrete point configuration has a realization as a matrix group, in which case the above computation will fail. A sure-fire way to get a matrix group is to calculate the `REGULAR_REPRESENTATION` of a permutation group, which yields the action by permutation matrices on the ambient space of dimension = number of points.\n",
    "\n",
    "### Orbits\n",
    "\n",
    "Once you have a matrix group, you may calculate the orbit of an arbitrary vector under it:\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$s = symmetric_group(3); "
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$a = $s->REGULAR_REPRESENTATION;"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{<3 2 1> <1 2 3> <2 1 3> <1 3 2> <3 1 2> <2 3 1>}\n",
       "\n"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print orbit($a->GENERATORS, new Vector([1,2,3]));"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "### Invariant polynomials\n",
    "\n",
    "Or you can regard the matrices as acting on polynomials, and calculate a set of invariant polynomials of a given maximum degree. For this, recall that the action of a matrix on a polynomial is exemplified by\n",
    "\n",
    "    [ 1  1 ]\n",
    "    [ 1 -1 ]  .  ( x^2 - y^2 )  =  ( x + y )^2 - ( x - y )^2.\n",
    "\n",
    "You can calculate the polynomials left invariant by the matrices sending the vertices of a dodecahedron into each other as follows:\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$d = polytope::dodecahedron();"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$d->GROUP->MATRIX_ACTION;"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "x_0^2 + x_1^2 + x_2^2\n",
       "x_0^4 + 2*x_0^2*x_1^2 + 2*x_0^2*x_2^2 + x_1^4 + 2*x_1^2*x_2^2 + x_2^4\n",
       "\n"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print join \"\\n\", @{invariant_polynomials($d->GROUP->MATRIX_ACTION, 5)};"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This is consistent with the Molien series of this action starting out as 1 + x^2 + x^4 + 2x^6 + ..., so in particular no invariant of degree exactly 5 is found. See [this paper](http://www.ams.org/journals/bull/1979-01-03/S0273-0979-1979-14597-X/S0273-0979-1979-14597-X.pdf) by Stanley for more information.\n",
    "\n",
    "## Decomposition into irreps, and bases of isotypic components\n",
    "\n",
    "You can calculate \n",
    "\n",
    "\n",
    "*  the character of a permutation action or matrix action, \n",
    "\n",
    "*  the decomposition of the action into irreducible representations, and \n",
    "\n",
    "*  the projection operators to (and vector space bases of) the isotypic components. \n",
    "\n",
    "For `MATRIX_ACTION`s, the character can always be calculated, but for the rest of these computations the `CHARACTER_TABLE` must be known:\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-2 0 2 1 4 1 1/2-1/2r5 3/2-1/2r5 1/2+1/2r5 3/2+1/2r5\n",
       "    \n"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print $d->GROUP->MATRIX_ACTION->CHARACTER;"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "     group > print irreducible_decomposition($d->GROUP->MATRIX_ACTION->CHARACTER, $d->GROUP);\n",
    "    polymake:  WARNING: available properties insufficient to compute 'CHARACTER_TABLE'\n",
    "\n",
    "This didn't work, because the dodecahedron doesn't (yet) come with a character table; this might change in future versions, though.\n",
    "\n",
    "It does work, for instance, for the symmetric group of order 5! (in fact, up to order 7!):\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$s=symmetric_group(5);"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1 -1 1 1 -1 -1 1\n",
       "4 -2 0 1 1 0 -1\n",
       "5 -1 1 -1 -1 1 0\n",
       "6 0 -2 0 0 0 1\n",
       "5 1 1 -1 1 -1 0\n",
       "4 2 0 1 -1 0 -1\n",
       "1 1 1 1 1 1 1\n",
       "    \n"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print $s->CHARACTER_TABLE;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$s->REGULAR_REPRESENTATION;"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5 3 1 2 0 1 0\n",
       "    \n"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print $s->REGULAR_REPRESENTATION->CHARACTER;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0 0 0 0 0 1 1\n",
       "\n"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print irreducible_decomposition($s->REGULAR_REPRESENTATION->CHARACTER,$s);"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So the regular (permutation) representation decomposes into one copy each of the invariant subspaces associated to the characters in the last two lines of the character table. The first entries there, 4 and 1, say that these components should have dimensions 4 and 1, respectively:\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4/5 -1/5 -1/5 -1/5 -1/5\n",
       "-1/5 4/5 -1/5 -1/5 -1/5\n",
       "-1/5 -1/5 4/5 -1/5 -1/5\n",
       "-1/5 -1/5 -1/5 4/5 -1/5\n",
       "    \n"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print isotypic_basis($s, $s->REGULAR_REPRESENTATION, 5);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1/5 1/5 1/5 1/5 1/5\n",
       "\n"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print isotypic_basis($s, $s->REGULAR_REPRESENTATION, 6);"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\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
}