{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "22b5f68b",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# Dirichlet Characters\n",
    "\n",
    "This notebook demonstrates how to use **SageMath** to work with Dirichlet characters, their conductors, and associated subfields. For complete documentation, see https://doc.sagemath.org/html/en/reference/modfrm/sage/modular/dirichlet.html.\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e9d9677",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 1. Define the Modulus and Dirichlet Group\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0351f445",
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Group of Dirichlet characters modulo 15 with values in Cyclotomic Field of order 4 and degree 2"
      ]
     },
     "execution_count": 1,
     "metadata": {
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "n = 15\n",
    "#Initiate the Group of Dirichlet Characters defined modulo n\n",
    "DC = DirichletGroup(n)\n",
    "DC\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a154f0",
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(Dirichlet character modulo 4 of conductor 4 mapping 3 |--> -1,\n",
       " Dirichlet character modulo 5 of conductor 5 mapping 2 |--> zeta4,\n",
       " Dirichlet character modulo 20 of conductor 20 mapping 11 |--> -1, 17 |--> zeta4,\n",
       " Dirichlet character modulo 20 of conductor 20 mapping 11 |--> -1, 17 |--> zeta4)"
      ]
     },
     "execution_count": 1,
     "metadata": {
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Multiplication of elements\n",
    "D4=DirichletGroup(4)\n",
    "D5=DirichletGroup(5)\n",
    "D20=DirichletGroup(20)\n",
    "\n",
    "alpha=D4.random_element()\n",
    "beta=D5.random_element()\n",
    "#We can multiply if we are explicit about where we want the multiplication to take place\n",
    "#We have two different ways to achieve this:\n",
    "alpha, beta, D20(alpha)*D20(beta), alpha.extend(20)*beta.extend(20)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c33c9ec",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 2. List Quadratic/Even Characters\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9aaee0",
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([Dirichlet character modulo 15 of conductor 1 mapping 11 |--> 1, 7 |--> 1,\n",
       "  Dirichlet character modulo 15 of conductor 3 mapping 11 |--> -1, 7 |--> 1,\n",
       "  Dirichlet character modulo 15 of conductor 5 mapping 11 |--> 1, 7 |--> -1,\n",
       "  Dirichlet character modulo 15 of conductor 15 mapping 11 |--> -1, 7 |--> -1],\n",
       " 4)"
      ]
     },
     "execution_count": 3,
     "metadata": {
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Calculate all quadratic characters defined modulo n\n",
    "quadratic_chars = [chi for chi in DC if (chi^2).is_trivial()]\n",
    "quadratic_chars, len(quadratic_chars)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "218ffbd7",
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([Dirichlet character modulo 15 of conductor 1 mapping 11 |--> 1, 7 |--> 1,\n",
       "  Dirichlet character modulo 15 of conductor 15 mapping 11 |--> -1, 7 |--> zeta4,\n",
       "  Dirichlet character modulo 15 of conductor 5 mapping 11 |--> 1, 7 |--> -1,\n",
       "  Dirichlet character modulo 15 of conductor 15 mapping 11 |--> -1, 7 |--> -zeta4],\n",
       " 4)"
      ]
     },
     "execution_count": 4,
     "metadata": {
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Calculate all even characters defined modulo n\n",
    "even_chars = [chi for chi in DC if chi.is_even()]\n",
    "even_chars, len(even_chars)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "927a91ef",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 3. Display Values of a Chosen Characters\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8b731e71",
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(Dirichlet character modulo 15 of conductor 1 mapping 11 |--> 1, 7 |--> 1,\n",
       "  [(1, 1), (2, 1), (4, 1), (7, 1), (8, 1), (11, 1), (13, 1), (14, 1)]),\n",
       " (Dirichlet character modulo 15 of conductor 3 mapping 11 |--> -1, 7 |--> 1,\n",
       "  [(1, 1), (2, -1), (4, 1), (7, 1), (8, -1), (11, -1), (13, 1), (14, -1)]),\n",
       " (Dirichlet character modulo 15 of conductor 5 mapping 11 |--> 1, 7 |--> zeta4,\n",
       "  [(1, 1),\n",
       "   (2, zeta4),\n",
       "   (4, -1),\n",
       "   (7, zeta4),\n",
       "   (8, -zeta4),\n",
       "   (11, 1),\n",
       "   (13, -zeta4),\n",
       "   (14, -1)]),\n",
       " (Dirichlet character modulo 15 of conductor 15 mapping 11 |--> -1, 7 |--> zeta4,\n",
       "  [(1, 1),\n",
       "   (2, -zeta4),\n",
       "   (4, -1),\n",
       "   (7, zeta4),\n",
       "   (8, zeta4),\n",
       "   (11, -1),\n",
       "   (13, -zeta4),\n",
       "   (14, 1)]),\n",
       " (Dirichlet character modulo 15 of conductor 5 mapping 11 |--> 1, 7 |--> -1,\n",
       "  [(1, 1), (2, -1), (4, 1), (7, -1), (8, -1), (11, 1), (13, -1), (14, 1)]),\n",
       " (Dirichlet character modulo 15 of conductor 15 mapping 11 |--> -1, 7 |--> -1,\n",
       "  [(1, 1), (2, 1), (4, 1), (7, -1), (8, 1), (11, -1), (13, -1), (14, -1)]),\n",
       " (Dirichlet character modulo 15 of conductor 5 mapping 11 |--> 1, 7 |--> -zeta4,\n",
       "  [(1, 1),\n",
       "   (2, -zeta4),\n",
       "   (4, -1),\n",
       "   (7, -zeta4),\n",
       "   (8, zeta4),\n",
       "   (11, 1),\n",
       "   (13, zeta4),\n",
       "   (14, -1)]),\n",
       " (Dirichlet character modulo 15 of conductor 15 mapping 11 |--> -1, 7 |--> -zeta4,\n",
       "  [(1, 1),\n",
       "   (2, zeta4),\n",
       "   (4, -1),\n",
       "   (7, -zeta4),\n",
       "   (8, -zeta4),\n",
       "   (11, -1),\n",
       "   (13, zeta4),\n",
       "   (14, 1)])]"
      ]
     },
     "execution_count": 5,
     "metadata": {
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Calculate the values of the characters in DC\n",
    "[(tau,[(k, tau(k)) for k in range(1, n) if gcd(k,n)==1]) for tau in DC]\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b8560",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "ATTENTION! Please note that Sage does not use the same convention to map a Dirichlet character with modulus n to a periodic function \\\\Z \\-&gt; \\\\C if the character is not primitive.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0dd988",
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(Dirichlet character modulo 15 of conductor 3 mapping 11 |--> -1, 7 |--> 1,\n",
       " [(1, 1, 1),\n",
       "  (2, -1, -1),\n",
       "  (3, 0, 0),\n",
       "  (4, 1, 1),\n",
       "  (5, 0, -1),\n",
       "  (6, 0, 0),\n",
       "  (7, 1, 1),\n",
       "  (8, -1, -1),\n",
       "  (9, 0, 0),\n",
       "  (10, 0, 1),\n",
       "  (11, -1, -1),\n",
       "  (12, 0, 0),\n",
       "  (13, 1, 1),\n",
       "  (14, -1, -1)])"
      ]
     },
     "execution_count": 6,
     "metadata": {
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "D15=DirichletGroup(15)\n",
    "chi = D15[1]\n",
    "#1. Convention SageMath\n",
    "#2. Convention [Childress]\n",
    "chi, [(k,chi(k),chi.primitive_character()(k)) for k in range(1,n)]\n",
    "#Look at value at for example 5 (in SageMath convention (15,5)=5, so \\chi(5)=0 (focusing of modulus); in [Childress] convention (5,3)=1, \\chi(5)\\neq 0 (focusing on conductor))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "90f43138",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 4. Compute the Conductor and Calculate Decompositions, Primitive Characters\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "218b2f5f",
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(Dirichlet character modulo 15 of conductor 1 mapping 11 |--> 1, 7 |--> 1, 1),\n",
       " (Dirichlet character modulo 15 of conductor 3 mapping 11 |--> -1, 7 |--> 1,\n",
       "  3),\n",
       " (Dirichlet character modulo 15 of conductor 5 mapping 11 |--> 1, 7 |--> zeta4,\n",
       "  5),\n",
       " (Dirichlet character modulo 15 of conductor 15 mapping 11 |--> -1, 7 |--> zeta4,\n",
       "  15),\n",
       " (Dirichlet character modulo 15 of conductor 5 mapping 11 |--> 1, 7 |--> -1,\n",
       "  5),\n",
       " (Dirichlet character modulo 15 of conductor 15 mapping 11 |--> -1, 7 |--> -1,\n",
       "  15),\n",
       " (Dirichlet character modulo 15 of conductor 5 mapping 11 |--> 1, 7 |--> -zeta4,\n",
       "  5),\n",
       " (Dirichlet character modulo 15 of conductor 15 mapping 11 |--> -1, 7 |--> -zeta4,\n",
       "  15)]"
      ]
     },
     "execution_count": 7,
     "metadata": {
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Compute the conductor for each element\n",
    "[(tau, tau.conductor()) for tau in DC]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a41942",
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(Dirichlet character modulo 15 of conductor 15 mapping 11 |--> -1, 7 |--> zeta4,\n",
       " [Dirichlet character modulo 3 of conductor 3 mapping 2 |--> -1,\n",
       "  Dirichlet character modulo 5 of conductor 5 mapping 2 |--> zeta4])"
      ]
     },
     "execution_count": 8,
     "metadata": {
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chi = D15[3]\n",
    "#Compute the decomposition of characters into factors (see p. 26 in [Childress], resp. lecture)\n",
    "chi, chi.decomposition()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "1dc549",
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(Dirichlet character modulo 15 of conductor 3 mapping 11 |--> -1, 7 |--> 1,\n",
       " Dirichlet character modulo 3 of conductor 3 mapping 2 |--> -1)"
      ]
     },
     "execution_count": 9,
     "metadata": {
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chi = D15[1]\n",
    "#Calculate primitve character\n",
    "chi, chi.primitive_character()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82ab5b25",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 5. Construct the Associated Number Field"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "b7a01d10",
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(Dirichlet character modulo 15 of conductor 15 mapping 11 |--> -1, 7 |--> -zeta4,\n",
       " Number Field in a with defining polynomial x^4 - x^3 - 4*x^2 + 4*x + 1)"
      ]
     },
     "execution_count": 10,
     "metadata": {
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chi = DC.random_element()\n",
    "# construct the numberfield associated to a character (i.e. the fixed field of its kernel)\n",
    "K = chi.fixed_field()\n",
    "chi, K\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c3a22",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "To compute fixed fields of groups of characters, you can calculate the fixed field on generators and composite these fields \\(see Exercise 4 on this week's sheet\\).\n",
    "\n",
    "You find an example below.\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "554115",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 6. Examples of Calculations \\(Example 11 in \\[Childress\\], seen at end of lecture\\)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "8d8c00",
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(Number Field in a with defining polynomial x^2 + 5,\n",
       " [Number Field in a0 with defining polynomial x^4 - 2*x^3 + x^2 + 5])"
      ]
     },
     "execution_count": 11,
     "metadata": {
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#returns generators of D5 and D4 (if DC is generated by mutiple elements you can access them using .gen(0), .gen(1),... or use .gens() to get list of a set of generating elements)\n",
    "chi=D5.gen()\n",
    "psi=D4.gen()\n",
    "\n",
    "sigma=D20(chi^2)*D20(psi)\n",
    "\n",
    "K=sigma.fixed_field()\n",
    "L1=(chi^2).fixed_field()\n",
    "L2=psi.fixed_field()\n",
    "#Compostium of two fields\n",
    "L=L1.composite_fields(L2)\n",
    "\n",
    "K,L"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "9f510e",
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([Dirichlet character modulo 1 of conductor 1], 2.0)"
      ]
     },
     "execution_count": 12,
     "metadata": {
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Calculation value of elements in X=<\\chi^2\\psi> to deduce elements in Y for each prime p=2,5\n",
    "p=2\n",
    "X=[trivial_character(1), sigma]\n",
    "Y=[tau for tau in X if tau.primitive_character()(p) != 0]\n",
    "Y, len(X)/len(Y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "f1c958",
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([Dirichlet character modulo 1 of conductor 1,\n",
       "  Dirichlet character modulo 5 of conductor 5 mapping 2 |--> -1],\n",
       " 2.0)"
      ]
     },
     "execution_count": 13,
     "metadata": {
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Calculation value of elements in X=<\\chi^2,\\psi> to deduce elements in Y for each prime p=2,5\n",
    "p=2\n",
    "X=[trivial_character(1),chi^2, psi, sigma]\n",
    "Y=[tau for tau in X if tau.primitive_character()(p) != 0]\n",
    "Y, len(X)/len(Y)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "63263f",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "We can use \\[X:Y\\]=e to deduce the ramification index of p=2,5 in L/Q and K/Q. From this we deduce that p=2,5 are unramified in L/K.\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "argv": [
    "sage-10.7",
    "--python",
    "-m",
    "sage.repl.ipython_kernel",
    "--matplotlib=inline",
    "-f",
    "{connection_file}"
   ],
   "display_name": "SageMath 10.7",
   "env": {
   },
   "language": "sagemath",
   "metadata": {
    "cocalc": {
     "description": "Open-source mathematical software system",
     "priority": 10,
     "url": "https://www.sagemath.org/"
    }
   },
   "name": "sage-10.7",
   "resource_dir": "/ext/jupyter/kernels/sage-10.7"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}