{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "Nerves\n",
    "------\n",
    "\n",
    "The nerve of a category (or group or monoid) is naturally constructed as a simplicial set.\n",
    "\n",
    "The **nerve** (or **classifying space**) of a group:\n",
    "- one vertex\n",
    "- one edge for each element of the group\n",
    "- one $n$-simplex for each $n$-tuple $(a_1, a_2, ..., a_n)$, non-degenerate if $a_i \\neq 1$ for all $i$\n",
    "- face maps: multiply consecutive elements:\n",
    "\n",
    "  - $d_{0} (a_1 a_2 \\cdots a_n) = a_{2} \\cdots a_{n}$\n",
    "  - $d_{n} (a_{1} a_{2} \\cdots a_{n}) = a_{1} \\cdots a_{n-1}$\n",
    "  - $d_{i} (a_{1} a_{2} \\cdots a_{n}) = a_{1} \\cdots (a_{i} a_{i+1}) \\cdots a_{n-1}$, $1 \\leq i \\leq n-1$\n",
    "\n",
    "- degeneracy maps: insert $1$ in the $j$th spot: \n",
    "\n",
    "  - $s_{j} (a_{1} \\cdots a_{n}) = a_{1} \\cdots a_{j} 1 a_{j+1} \\cdots a_{n}$, $0 \\leq j \\leq n$\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "\n",
    "Similar for monoids or categories: one vertex for each object, one 1-simplex for each morphism, one $n$-simplex for each collection of $n$ composable morphisms.\n",
    "\n",
    "Also, given the nerve of a category, you can recover the category.\n",
    "\n",
    "*Are categories in good enough shape in Sage to be able to define the nerve of a (finite) category?*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Example: real projective space\n",
    "------------------------------\n",
    "\n",
    "Infinite dimensional real projective space $\\mathbb{R} P^{\\infty}$ is the classifying space of the group $\\mathbb{Z}/2\\mathbb{Z}$. There is one non-degenerate simplex in each dimension. $\\mathbb{R} P^{n}$ is its $n$-skeleton.\n",
    "\n",
    "Look at the $f$-vectors for **simplicial complex** versions of $\\mathbb{R} P^{n}$ for small values of $n$:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 6, 15, 10]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplicial_complexes.RealProjectiveSpace(2).f_vector()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 11, 51, 80, 40]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplicial_complexes.RealProjectiveSpace(3).f_vector()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 16, 120, 330, 375, 150]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplicial_complexes.RealProjectiveSpace(4).f_vector()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "simplicial_complexes.RealProjectiveSpace?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For $\\mathbb{R}P^5$, the $f$-vector is (1, 63, 903, 4200, 8400, 7560, 2520).\n",
    "\n",
    "\n",
    "\n",
    "In comparison, as a **simplicial set**:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplicial_sets.RealProjectiveSpace(10).f_vector()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Some calculations..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 938 ms, sys: 101 ms, total: 1.04 s\n",
      "Wall time: 4.23 s\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Cohomology ring of Minimal triangulation of RP^4 over Finite Field of size 2"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%time simplicial_complexes.RealProjectiveSpace(4).cohomology_ring(GF(2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 340 ms, sys: 17.5 ms, total: 358 ms\n",
      "Wall time: 347 ms\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Cohomology ring of RP^36 over Finite Field of size 2"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%time simplicial_sets.RealProjectiveSpace(36).cohomology_ring(GF(2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "H = simplicial_sets.RealProjectiveSpace(36).cohomology_ring(GF(2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Finite family {(18, 0): h^{18,0}, (3, 0): h^{3,0}, (8, 0): h^{8,0}, (29, 0): h^{29,0}, (34, 0): h^{34,0}, (14, 0): h^{14,0}, (19, 0): h^{19,0}, (24, 0): h^{24,0}, (4, 0): h^{4,0}, (9, 0): h^{9,0}, (30, 0): h^{30,0}, (35, 0): h^{35,0}, (15, 0): h^{15,0}, (20, 0): h^{20,0}, (25, 0): h^{25,0}, (5, 0): h^{5,0}, (10, 0): h^{10,0}, (31, 0): h^{31,0}, (36, 0): h^{36,0}, (0, 0): h^{0,0}, (21, 0): h^{21,0}, (26, 0): h^{26,0}, (6, 0): h^{6,0}, (11, 0): h^{11,0}, (16, 0): h^{16,0}, (1, 0): h^{1,0}, (22, 0): h^{22,0}, (27, 0): h^{27,0}, (32, 0): h^{32,0}, (7, 0): h^{7,0}, (12, 0): h^{12,0}, (17, 0): h^{17,0}, (2, 0): h^{2,0}, (23, 0): h^{23,0}, (28, 0): h^{28,0}, (33, 0): h^{33,0}, (13, 0): h^{13,0}}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "H.basis()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "h^{1,0}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = list(H.basis(1))[0]\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "h^{25,0}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x**25"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "h^{8,0}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.Sq(1).Sq(2).Sq(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Classifying space of $C_{3}$\n",
    "----------------------------\n",
    "\n",
    "In Sage, the cyclic group $C_{3}$ has three elements, $1$, $f$, $f^{2}$. \n",
    "\n",
    "So its classifying space has two nondegenerate 1-simplices, four non-degenerate 2-simplices ($f*f$, $f^{2}*f$, $f*f^{2}$, $f^{2}*f^{2}$), eight non-degenerate 3-simplices, etc."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Simplicial set with 63 non-degenerate simplices"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "C3 = groups.misc.MultiplicativeAbelian([3])\n",
    "BC3 = C3.nerve_n_skeleton(5)\n",
    "BC3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0: 0, 1: C3, 2: 0, 3: C3, 4: 0, 5: Z^22}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "BC3.homology()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Finitely presented group < e0 | e0^3 >"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "BC3.fundamental_group()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Classifying space of $\\Sigma_{3}$\n",
    "---------------------------------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "Sigma3 = groups.permutation.Symmetric(3)\n",
    "BSigma3 = Sigma3.nerve_n_skeleton(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Simplicial set with 781 non-degenerate simplices"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "BSigma3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0: 0, 1: C2, 2: 0, 3: C6, 4: Z^520}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "BSigma3.homology()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Finitely presented group < e0, e1 | e0^2, e1^3, (e0*e1^-1)^2 >"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "BSigma3.fundamental_group()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Simplicial set with 12720 non-degenerate simplices"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "BSigma4 = groups.permutation.Symmetric(4).nerve_n_skeleton(3)\n",
    "BSigma4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Finitely presented group < e3, e19 | e19^3, (e3*e19^-1)^2, e3^4 >"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "BSigma4.fundamental_group()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 33, 1089]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "BD17 = groups.permutation.Dihedral(17).nerve_n_skeleton(2)\n",
    "BD17.f_vector()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Finitely presented group < e1, e4 | e4^2, (e4*e1^-1)^2, e1^17 >"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "BD17.fundamental_group()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Example: complex projective space\n",
    "---------------------------------\n",
    "\n",
    "$\\mathbb{C} P^{n}$ is the $2n$-skeleton of the classifying space of the Lie group $S^{1}$. Sage can't construct it that way, but work of Sergeraert (**Kenzo**, **CAT**) leads to constructions we can use in Sage.\n",
    "\n",
    "$f$-vectors as **simplicial complexes**:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 9, 36, 84, 90, 36]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplicial_complexes.ComplexProjectivePlane().f_vector()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'module' object has no attribute 'ComplexProjectiveSpace'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-27-4e74e8a803a4>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0msimplicial_complexes\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mComplexProjectiveSpace\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mInteger\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mf_vector\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m: 'module' object has no attribute 'ComplexProjectiveSpace'"
     ]
    }
   ],
   "source": [
    "simplicial_complexes.ComplexProjectiveSpace(3).f_vector()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As **simplicial sets**:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 0, 2, 3, 3]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplicial_sets.ComplexProjectiveSpace(2).f_vector()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 0, 3, 10, 25, 30, 15]"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplicial_sets.ComplexProjectiveSpace(3).f_vector()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 0, 4, 22, 97, 255, 390, 315, 105]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplicial_sets.ComplexProjectiveSpace(4).f_vector()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "CP3 = simplicial_sets.ComplexProjectiveSpace(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0: 0, 1: 0, 2: Z, 3: 0, 4: Z, 5: 0, 6: Z}"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "CP3.homology()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Group( <identity> of ... )"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "CP3.fundamental_group()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To do:\n",
    "------\n",
    "------\n",
    "\n",
    "- good conversions from simplicial complexes (and other objects) to simplicial sets\n",
    "- simplicial abelian groups, $k$-skeleton of $K(\\pi,n)$\n",
    "- infinite simplicial sets\n",
    "- general framework for simplicial objects in a category\n",
    "- higher homotopy groups (?)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sage.homology.simplicial_set import *\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "v = NonDegenerateSimplex(0)\n",
    "w = NonDegenerateSimplex(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "e = NonDegenerateSimplex(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Simplicial set with 3 non-degenerate simplices"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "SimplicialSet({e: (v, w)})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "SageMath 7.3.beta2",
   "language": "",
   "name": "sagemath"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
