{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Toric polymake tutorial\n",
    "This tutorial will go over the basics of polymake and then focus on the `application \"fulton\"` for toric varieties. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Quick start\n",
    "Below you see a small sample session.\n",
    "First switch to the application with the following command."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "application \"fulton\";"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Start by creating a cone."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$c = new Cone(INPUT_RAYS=>[[-2,1],[1,1]]);\n",
    "print $c->HILBERT_BASIS;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can ask for the binomials generating the associated toric ideal in two ways: The first gives a matrix of the binomial exponents, the second gives you the actual binomials."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$toric = $c->CONE_TORIC_IDEAL;\n",
    "print $toric->BINOMIAL_GENERATORS, \"\\n\";\n",
    "print join(\"\\n\", @{$toric->GENERATORS});"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Asking for properties of the toric variety is done in the same manner as for polytopes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$tv = new NormalToricVariety($c);\n",
    "print \"Is smooth? \", $tv->SMOOTH, \"\\n\";\n",
    "print \"Is affine? \", $tv->AFFINE, \"\\n\";\n",
    "print \"Is orbifold? \", $tv->ORBIFOLD, \"\\n\";\n",
    "print \"Is complete? \", $tv->COMPLETE, \"\\n\";\n",
    "$tv->properties();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Affine toric varieties\n",
    "Affine toric varieties arise from cones. To dualize a (pointed) cone just construct a new cone switching facets and rays."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$sigma = new Cone(INPUT_RAYS=>[[1,0,0],[0,1,0],[1,0,1],[0,1,1]]);\n",
    "$sigmad = polarize($sigma);\n",
    "print $sigmad->HILBERT_BASIS, \"\\n\";\n",
    "print $sigmad->CONE_TORIC_IDEAL->BINOMIAL_GENERATORS, \"\\n\";\n",
    "print $sigmad->CONE_TORIC_IDEAL->GENERATORS, \"\\n\";"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The exponent vectors of the binomials generate the lattice of relations of the Hilbert basis of the dual cone. In particular we have"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$hb = $sigmad->HILBERT_BASIS;\n",
    "$binomials = $sigmad->CONE_TORIC_IDEAL->BINOMIAL_GENERATORS;\n",
    "print $binomials * $hb; "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Cyclic quotient singularities\n",
    "Two-dimensional pointed cones in two-dimensional space can always be generated by two rays in the standard form `[[1,0],[-q,n]]` with `0<q<n` and `q` and `n` coprime."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$N7Q3 = new CyclicQuotient(N=>7, Q=>3);\n",
    "print primitive($N7Q3->RAYS);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "CQS are deeply connected with the associated continued fractions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$cf = new Vector<Rational>($N7Q3->CONTINUED_FRACTION);\n",
    "print $cf, \"\\n\";\n",
    "print $cf->[0] -1/( $cf->[1] -1/ $cf->[2]), \"\\n\";"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$dcf = new Vector<Rational>($N7Q3->DUAL_CONTINUED_FRACTION);\n",
    "print $dcf, \"\\n\";\n",
    "print $dcf->[0] -1/ $dcf->[1], \"\\n\";"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If we start with the Hilbert basis of the dual cone, sorted by first coordinate:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print $N7Q3->WEIGHT_CONE->HILBERT_BASIS, \"\\n\";\n",
    "$sorted = new Matrix(sort(@{$N7Q3->WEIGHT_CONE->HILBERT_BASIS}));\n",
    "print $sorted;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then the dual continued fraction expansion of `n/(n-q)` gives us relations among these elements:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print $sorted->[0] + $sorted->[2] == $dcf->[0] * $sorted->[1],\"\\n\";\n",
    "print $sorted->[1] + $sorted->[3] == $dcf->[1] * $sorted->[2],\"\\n\";"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q: Derive and prove the general relation formula."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Projective varieties\n",
    "Projective toric varieties arise from polytopes. Normal projective toric varieties arise from fans. To construct a projective toric variety, simply take the normal fan of a polytope and give it to the `NormalToricVariety` constructor."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$PP2 = new NormalToricVariety(normal_fan(simplex(2)));\n",
    "print $PP2->RAYS;\n",
    "print $PP2->MAXIMAL_CONES;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$PP2->VISUAL;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The Hasse diagram looks as follows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$PP2->HASSE_DIAGRAM->VISUAL;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now consider the following two cones:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$c1 = new Cone(simplex(2));\n",
    "print $c1->HILBERT_BASIS, \"\\n\";\n",
    "$c2 = new Cone(simplex(2,2));\n",
    "print $c2->HILBERT_BASIS, \"\\n\";"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Lets look at the associated toric ideals:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print \"C1: \", join(\"\\n\", @{$c1->CONE_TORIC_IDEAL->GROEBNER(ORDER_NAME=>\"dp\")->BASIS}),\"\\n\";\n",
    "print \"C2: \", join(\"\\n\", @{$c2->CONE_TORIC_IDEAL->GROEBNER(ORDER_NAME=>\"dp\")->BASIS}),\"\\n\";"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The first ideal is actually `0`. Both ideals are homogeneous and hence, they define projective varieties. \n",
    "\n",
    "Q: What are these projective varieties?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### A non-projective toric variety\n",
    "Not every fan is the normal fan of a polytope. Here we give an example."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$f = new PolyhedralFan(INPUT_RAYS=>\n",
    "[[1,0,0],[0,1,0],[-1,-1,-1],\n",
    "[0,0,1],[2,1,1],[1,2,1],[1,1,2]],\n",
    "INPUT_CONES=>[[0,1,2],[0,2,3],\n",
    "[1,2,3],[4,5,6],[0,1,4],[1,3,5],\n",
    "[0,3,6],[1,4,5],[3,5,6],[0,4,6]]\n",
    ");\n",
    "$tv = new NormalToricVariety($f);\n",
    "print \"Projective? \", $tv->PROJECTIVE, \"\\n\";\n",
    "print \"Smooth? \", $tv->SMOOTH, \"\\n\";\n",
    "print \"Complete? \", $tv->COMPLETE, \"\\n\";"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$tv->VISUAL;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Hirzebruch surfaces\n",
    "Hirzebruch surfaces come from two-dimensional complete fans with exactly four rays. Smoothness makes it possible to bring these fans into a standard form such that we arrive at a one-parameter family."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$h1 = hirzebruch_surface(1);\n",
    "print $h1->RAYS;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$h1->VISUAL;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$h2 = hirzebruch_surface(2);\n",
    "print $h2->RAYS;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$h2->VISUAL;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q: Construct the/an associated polytope.\n",
    "\n",
    "Q: Prove the standard form for complete smooth fans in two dimensions with exactly four rays."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "polymake has a method to reconstruct a polytope from a regular fan / projective toric variety."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$A = generating_polyhedron_facets($h2);\n",
    "print $A;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This polytope has the given fan as a normal fan."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$P = new Polytope(INEQUALITIES=>$A);\n",
    "$FF = normal_fan($P);\n",
    "print $FF->RAYS;\n",
    "print $FF->MAXIMAL_CONES;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The polytope we just constructed looks like:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$P->VISUAL;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The polytope is not unique. Any polytope with the same facet vectors and combinatorics will have the same normal fan.\n",
    "\n",
    "Its normal fan is exactly the fan we started with."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$FF->VISUAL;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Simple, not smooth"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "$p = new Polytope(POINTS=>[\n",
    "[1, 0, 0, 0],\n",
    "[1, 1, 1, 0],\n",
    "[1, 1, 0, 1],\n",
    "[1, 0, 1, 1]]);\n",
    "print \"Simple? \", $p->SIMPLE, \"\\n\";\n",
    "print \"Smooth? \", $p->SMOOTH, \"\\n\";"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Non-normal toric varieties\n",
    "polymake only handles normal toric varieties. Nevertheless we can use it to get at the toric ideal of a non-normal toric variety by giving the semigroup generators directly as a mock Hilbert basis. For example for the Neil parabola use:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$c = new Cone(HILBERT_BASIS_GENERATORS=>[[[2],[3]],[[]]]);\n",
    "print $c->CONE_TORIC_IDEAL->BINOMIAL_GENERATORS;\n",
    "print $c->CONE_TORIC_IDEAL->GENERATORS;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If we build a new cone from this semigroup, we see that it was not saturated. The semigroup generated by 2 and 3 does not come from a cone."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$cc = new Cone(INPUT_RAYS=>$c->HILBERT_BASIS_GENERATORS->[0]);\n",
    "print $cc->RAYS, \"\\n\";\n",
    "print $cc->HILBERT_BASIS, \"\\n\";"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q: What are necessary conditions for a semigroup to come from a cone?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Smooth vs normal\n",
    "It is an open question by Oda whether smoothness and normality of polytopes are equivalent."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print $p->VERTICES, \"\\n\";\n",
    "print \"Normal? \", $p->NORMAL, \"\\n\";\n",
    "print \"Smooth? \", $p->SMOOTH, \"\\n\";"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "help \"NORMAL\";\n",
    "help \"SMOOTH\";"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print $p->HILBERT_BASIS;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Dealing with torus invariant divisors\n",
    "The application `fulton` allows one to compute several properties of divisors and even divisor classes. First build a divisor from its representation as a sum of primitive divisors. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$tv = hirzebruch_surface(4);\n",
    "$d = $tv->DIVISOR(COEFFICIENTS=>[1,1,1,1]);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then treat them like any polymake object."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print \"Effective? \", $d->EFFECTIVE, \"\\n\";\n",
    "print \"Ample? \", $d->AMPLE, \"\\n\";\n",
    "print \"Basepoint free? \", $d->BASEPOINT_FREE, \"\\n\";\n",
    "print \"Nef? \", $d->NEF, \"\\n\";\n",
    "print \"Cartier? \", $d->CARTIER, \"\\n\";"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The following code produces the vertices of the polytope of global sections."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$pd = $d->SECTION_POLYTOPE;\n",
    "print $pd->VERTICES;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Furthermore one can deal with rational divisor classes, rather than divisors, using the class group exact sequence. Its projection matrix can be produced as follows."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print $tv->RATIONAL_DIVISOR_CLASS_GROUP->PROJECTION;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then one can access the nef cone, effective cone and Mori cone of the toric variety:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print \"Nef cone:\\n\", $tv->NEF_CONE->RAYS,\"\\n\";\n",
    "print \"Effective cone:\\n\", $tv->EFFECTIVE_CONE->RAYS,\"\\n\";\n",
    "print \"Mori cone:\\n\", $tv->MORI_CONE->RAYS,\"\\n\";"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Accessing Singular\n",
    "[Singular](https://www.singular.uni-kl.de/) is a computer algebra system developed in Kaiserslautern. It can be accessed from polymake on different levels. For ideals there are low-level c++ methods providing basic funtionality to compute Groebner bases, radicals, etc. At top level for users there are perl methods giving the opportunity to send command strings to Singular if something has not been accessed on c++ level yet. This allows fast proof of concept implementations."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "application \"fulton\";\n",
    "$c = new Cone(INPUT_RAYS=>[[-1,1],[0,1],[1,1]]);\n",
    "$tv = new NormalToricVariety($c);\n",
    "$toric = $c->CONE_TORIC_IDEAL;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "singular_eval(\"listvar();\");"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$radical = $toric->RADICAL;\n",
    "print join(\"\\n\", @{$radical->GENERATORS});"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "singular_eval(\"listvar();\");"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$ideal = $tv->WEIGHT_CONE->CONE_TORIC_IDEAL;\n",
    "print $ideal;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It is possible to execute arbitrary Singular commands from polymake using the `singular_eval` command. First build your command as a string."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$cmd = \"ring r = 0,(x_0,x_1,x_2),dp;\";\n",
    "print $cmd;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then hand this string to `singular_eval`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "singular_eval($cmd);\n",
    "singular_eval(\"r;\");"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "One can also retrieve variables from Singular to polymake using the `singular_get_var` command in the following manner."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "singular_eval(\"int n = nvars(r);\");\n",
    "$n = singular_get_var(\"n\");\n",
    "print $n,\"\\n\";"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It is possible to retrieve the following datatypes from Singular: `int`, `intmat`, `intvec` and `poly`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "singular_eval(\"poly p = x_2^2-x_0*x_1\");\n",
    "$p = singular_get_var(\"p\");\n",
    "print $p,\"\\n\";"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To load a Singular library, you can use the `load_singular_library` command with the library name. Afterwards all library methods are accessible, as in the following example."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "load_singular_library(\"deform.lib\");\n",
    "singular_eval(\"ideal i = x_0*x_1, x_2;\");\n",
    "singular_eval(\"def L = versal(i);\");\n",
    "singular_eval(\"L;\");"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "polymake",
   "language": "polymake",
   "name": "polymake"
  },
  "language_info": {
   "codemirror_mode": "perl",
   "file_extension": ".pl",
   "mimetype": "text/x-polymake",
   "name": "polymake"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
