{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6259b3ae-fea1-4c95-9ed3-426acb08885a",
   "metadata": {},
   "source": [
    "# Introduction\n",
    "<a id=\"sec1\"></a>\n",
    "\n",
    "This Jupyter Notebook (.ipynb) document comprises the SageMath code and computational output for the paper *Dimension formulas for modular form spaces of rational weights, the classification of eta-quotient characters and an extension of Martin's theorem* (Click [here](https://arxiv.org/abs/2408.00246) to find the arXiv version) by **Xiao-Jie Zhu**. The .ipynb file itself is avaliable [here](http://xjzmath.com/code/). The corresponding pdf file can be generated by `nbconvert`, a built-in tool of Jupyter Notebook, with the following script:\n",
    "\n",
    "    $ jupyter nbconvert --to pdf SageCodeDimensionRationalWeight.ipynb\n",
    "(This requires Jupyter and XeLaTex installed. See [Instruction](https://nbconvert.readthedocs.io/en/latest/install.html#installing-tex).)\n",
    "\n",
    "To execute the programs below, one need to open the .ipynb file in Jupyter (with a SageMath kernel linked to it) or in a Python IDE like [PyCharm](https://www.jetbrains.com/pycharm/). Alternatively, one can copy the code here and paste it into his/her own SageMath session. I recommend copying, pasting, and executing all the code in the same order as in this document, as the code in one cell may call functions defined in a previous cell.\n",
    "\n",
    "All code below was executed in SageMath 10.7 (which is based on Python 3.12). Click [here](https://doc.sagemath.org/html/en/installation/index.html) to find Sage Installation Guide.\n",
    "\n",
    "Note: the text in a shaded box is SageMath code; **the output of the code** follows immediately after and is in `monospaced font`.\n",
    "\n",
    "[Back to the homepage.](http://xjzmath.com)\n",
    "\n",
    "<a id=\"sec2\"></a>\n",
    "# Summary of this document\n",
    "\n",
    "In construction.\n",
    "\n",
    "1. [Procedures](#sec3)\n",
    "\n",
    "    1. [SageMath code for computing the characters, q-series expansions, and orders of eta-quotients](#sec31)\n",
    "\n",
    "    2. [SageMath code for D-covers (Appendix A of the paper)](#sec32)\n",
    "\n",
    "    3. [SageMath code for $(x_c)_{c\\mid N}$ and $(r_n)_{n\\mid N}$ (Section 5 of the paper)](#sec33)\n",
    "\n",
    "    4. [SageMath code for computing dimensions (Section 4 of the paper)](#sec34)\n",
    "  \n",
    "    5. [SageMath code for the classification of eta-qoutient characters](#sec35)\n",
    "\n",
    "3. [Genrating the tables in the paper](#sec4)\n",
    "\n",
    "   1. [Table 1 of the paper](#sec41)\n",
    "   \n",
    "   2. [Table 2 of the paper](#sec42)\n",
    "  \n",
    "   3. [Table 3 of the paper](#sec43)\n",
    "  \n",
    "   4. [More admissible eta-quotients of type I](#sec44)\n",
    "  \n",
    "   5. [Admissible eta-quotients of type II](#sec45)\n",
    "\n",
    "5. [Numerical checks of some theorems](#sec5)\n",
    "\n",
    "<a id=\"sec3\"></a>\n",
    "# Procedures\n",
    "\n",
    "<a id=\"sec31\"></a>\n",
    "### SageMath code for computing the characters, q-series expansions, and orders of eta-quotients\n",
    "\n",
    "The following cell contains fundamental SageMath functions for computation with the characters, q-series expansions, and orders of eta-quotients of fractional exponents. \n",
    "\n",
    "The code was originally created for the ariticle [*Double coset operators and Eta-quotients*](https://doi.org/10.1016/j.jnt.2023.02.017) by **Hai-Gang Zhou** and **Xiao-Jie Zhu**. It has been modified to suit the current paper. Some code for improving performance has been removed to shorten the length. The code should run on SageMath\n",
    "9.3 (or higher version) within Jupyter Notebook.\n",
    "\n",
    "**An important remark**: `eta_char_simple`, `eta_char_gamma0N`, `eta_char_gamma0N_exp` defined below can compute the character of an eta-quotient of fractional exponents. However, for fractional exponents we must set the parameter `b_algorithm_ds=True` (the default value) when calling `eta_char_simple_exp`. If we call `eta_char_simple_exp(...,b_algorithm_ds=False)`, then it will use Petersson's formula, which is more efficient but can only deal with integral exponents. Moreover, all functions below for computing orders at cusps and $q$ series expansions are valid for fractional exponents. For fractional exponents, the computation may be expansive since it involves taking roots of power series.\n",
    "\n",
    "For the complete code, see [here](https://github.com/Xiao-Jie-Zhu/double_coset_operators_and_eta_quotients)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "27baa16c-89cf-4a17-a938-53016c3aab5e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Define Sage functions successfully.\n"
     ]
    }
   ],
   "source": [
    "import functools\n",
    "import itertools\n",
    "from itertools import product as cp  # the python version of Catersian product\n",
    "\n",
    "def eta_char_simple(A, n):\n",
    "    '''\n",
    "    Calculate the logarithm of the character of\n",
    "    Dedekind eta(n\\tau) times 1/2pi*I at matrix A.\n",
    "    '''\n",
    "    (a, b), (c, d) = A[0], A[1]\n",
    "    if c > 0:\n",
    "        re = (a + d) / (24*c) * n + 1/2 * dedekind_sum(-d, c/n) - 1/8\n",
    "    elif c < 0:\n",
    "        re = (a + d) / (24*c) * n + 1/2 * dedekind_sum(d, -c/n) + 1/8\n",
    "    elif c == 0 and a > 0:\n",
    "        re = b / 24 * n\n",
    "    else:\n",
    "        re = -b / 24 * n - 1/4\n",
    "    return re\n",
    "\n",
    "def eta_char_simple_exp(A, n, b_algorithm_ds=True):\n",
    "    '''\n",
    "    Calculate the character of Dedekind eta(n\\tau) at matrix A\n",
    "    using eta_char_simple if b_algorithm_ds=True or a formula of Petersson\n",
    "    if b_algorithm_ds=False.\n",
    "    Return an symbolic expression.\n",
    "    '''\n",
    "    if b_algorithm_ds:\n",
    "        return exp(2 * sage.all.pi * sage.all.I * eta_char_simple(A, n))\n",
    "    else:\n",
    "        (a, b), (c, d) = A[0], A[1]\n",
    "        temp = kronecker(d, abs(c)) if c % 2 == 1 else kronecker(c, d)\n",
    "        if c % 2 == 1:\n",
    "            expo = ((a+d-3)*c - b*d*(c^2-1)) / 24\n",
    "        else:\n",
    "            expo = ((a-2*d)*c - b*d*(c^2-1) + 3*d - 3) / 24\n",
    "        return temp * exp(2 * sage.all.pi * sage.all.I * expo)\n",
    "    \n",
    "def eta_char_gamma0N(N, es, *A):\n",
    "    '''\n",
    "    Calculate the logarithm character of prod_{n mid N}eta^es[n]_(n tau)\n",
    "    at some matrix. The meaning of the argument can be deduced from first\n",
    "    four lines of the function body.\n",
    "    '''\n",
    "    if len(A) == 1: B = matrix(A[0])\n",
    "    elif len(A) == 4: B = matrix(ZZ, [[A[0], A[1]], [A[2], A[3]]])\n",
    "    elif len(A) == 2: B = matrix(ZZ, [A[0], A[1]])\n",
    "    else:\n",
    "        raise TypeError('The first argument should be a matrix,'\n",
    "                        ' or a sequence of four numbers, or...')\n",
    "    return sum(eta_char_simple(B, n) * es.get(n, 0) for n in N.divisors())\n",
    "\n",
    "def eta_char_gamma0N_exp(N, es, *A):\n",
    "    '''\n",
    "    Calculate the character of prod_{n mid N}eta^es[n]_(n\\tau) at some matrix.\n",
    "    Just call eta_char_gamma0N.\n",
    "    '''\n",
    "    return exp(2 * sage.all.pi * sage.all.I * eta_char_gamma0N(N, es, *A))\n",
    "\n",
    "def eta_order_gamma0N(N, es, cusp):\n",
    "    '''\n",
    "    Calculate the order of an eta quotient given by es at cusp.\n",
    "    The cusp argument should be some element in QQ.\n",
    "    To calculate order at infty, set cusp=1/N.\n",
    "    '''\n",
    "    d = cusp.denominator()\n",
    "    return sum(gcd(n, d)^2 / n * es.get(n, 0) for n in N.divisors()) / 24\n",
    "\n",
    "# We create a power series ring as the parent of all q-series\n",
    "R.<q> = PowerSeriesRing(QQ, sparse=True)\n",
    "_eta_power_cache = 0\n",
    "def eta_power_series(precision=100):\n",
    "    '''\n",
    "    Compute the power series expansion of the Dedekind eta function\n",
    "    excluding the q^(1/24) part.\n",
    "    Use _eta_power_cache as cache.\n",
    "    '''\n",
    "    global _eta_power_cache\n",
    "    if _eta_power_cache == 0 or _eta_power_cache.prec() < precision:\n",
    "        result = R(0)\n",
    "        v_max = floor(sqrt(24 * precision + 1))\n",
    "        for v in srange(1, v_max+1):\n",
    "            if v % 12 == 1 or v % 12 == 11:\n",
    "                result += q^((v^2 - 1) / 24)\n",
    "            elif v % 12 == 5 or v % 12 == 7:\n",
    "                result += -q^((v^2 - 1) / 24)\n",
    "        result += O(q^precision)\n",
    "        _eta_power_cache = result       \n",
    "    return _eta_power_cache + O(q^precision)\n",
    "            \n",
    "_eta_factor_cache = {}\n",
    "def eta_factor(ex, m, precision=100):\n",
    "    '''\n",
    "    Return the power series of q^(-m*ex/24)*eta(m tau)^ex.\n",
    "    '''\n",
    "    global _eta_factor_cache\n",
    "    f = _eta_factor_cache.get((ex, m))\n",
    "    if f == None or f.prec() < precision:       \n",
    "        if ex != 1: s = eta_power_series(precision) ^ ex\n",
    "        else: s = eta_power_series(precision)           \n",
    "        if m != 1: _eta_factor_cache[(ex, m)] = s.substitute({q: q^m})\n",
    "        else: _eta_factor_cache[(ex, m)] = s           \n",
    "    return _eta_factor_cache[(ex, m)] + O(q^precision)\n",
    "\n",
    "def eta_factor_first_exp(ex, m):\n",
    "    '''\n",
    "    Return the exponent of the first non-zero q-term of eta(m\\tau)^ex.\n",
    "    '''\n",
    "    return (ex * m) / 24\n",
    "\n",
    "def eta_gamma_N(N, es, precision=100):\n",
    "    '''\n",
    "    Return certain eta-quotient on Gamma_0(N), as a pair whose first component\n",
    "    is the exponent of the first non-zero power of q,\n",
    "    and second component is the corresponding power series on q.\n",
    "    The argument es is a Python dict where each pair (k, v) means eta(k\\tau)^v.\n",
    "    '''\n",
    "    N_divs = N.divisors()\n",
    "    re = prod(eta_factor(es.get(n, 0), n, precision) for n in N_divs)\n",
    "    ex = sum(eta_factor_first_exp(es.get(n, 0), n) for n in N_divs)\n",
    "    return ex, re\n",
    "\n",
    "def eta_gamma_N_series(N, es, precision=100):\n",
    "    '''\n",
    "    Just call eta_gamma_N(N, es, precision)[1].\n",
    "    It returns the power series part of an eta-quotients,\n",
    "    dropping the fractional-power part.\n",
    "    '''\n",
    "    return eta_gamma_N(N, es, precision)[1]\n",
    "\n",
    "def coeff_eta_quotient(N, es, n):\n",
    "    '''\n",
    "    Return the n-th coefficient of the power series part of \n",
    "    the eta-quotient given by the Python dict es of level N.\n",
    "    For example, a call of coeff_eta_quotient(4, {1: 3, 2: -2, 4: 10}, 17)\n",
    "    returns the 17-th coefficient of the q-series of\n",
    "    q^(39/24)eta^3(tau)eta^-2(2tau)eta^10(4tau).\n",
    "    If n is not a non-negative integer, then return 0.\n",
    "    '''\n",
    "    if n not in ZZ or n < 0:\n",
    "        return 0\n",
    "    f = eta_gamma_N(N, es, precision=n+1)[1]\n",
    "    return f[n]\n",
    "\n",
    "print('Define Sage functions successfully.')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b886069-f8c9-409e-b4d8-f4fd604856f2",
   "metadata": {},
   "source": [
    "<a id=\"sec32\"></a>\n",
    "### SageMath code for D-covers (Appendix A of the paper)\n",
    "\n",
    "The $D$-cover of $\\mathrm{GL}_2^{+}(\\mathbb{R})$ is defined in Eq. (2.1) in the paper. See Appendix A of the paper for more details. We realize a $D$-cover in Sagemath by a Python class, `D_cover`, where we can do arithmetic such as multiplication, division, and taking the reciprocal. Immediately after this class is a function `etaPsi` that realizes $\\Psi$, which is defined in Eq. (2.7) in the paper."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4f0a792e-8edf-4a53-a036-473602df8d65",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Define Sage functions successfully.\n"
     ]
    }
   ],
   "source": [
    "class D_cover:\n",
    "    def __init__(self, a, b, c, d, D, ep):\n",
    "        self.mat = matrix([[a, b], [c, d]])\n",
    "        self.D = D\n",
    "        self.ep = ep\n",
    "        F = CyclotomicField(D)\n",
    "        self.F = F\n",
    "        assert ep in F\n",
    "        \n",
    "    def __mul__(self, other):\n",
    "        assert self.D == other.D\n",
    "        (a1, b1), (c1, d1) = self.mat\n",
    "        (a2, b2), (c2, d2) = other.mat\n",
    "        mat = self.mat * other.mat\n",
    "        (a3, b3), (c3, d3) = mat\n",
    "        if c1 * c2 < 0:\n",
    "            ep = self.ep * other.ep\n",
    "        elif c1 == c2 == 0 and d1 < 0 and d2 < 0:\n",
    "            ep = self.ep * other.ep * self.F.gen()\n",
    "        elif c1 >= 0 and c2 >= 0 and c3 >= 0:\n",
    "            ep = self.ep * other.ep\n",
    "        elif c1 >= 0 and c2 >= 0 and c3 < 0:\n",
    "            ep = self.ep * other.ep * self.F.gen()\n",
    "        elif c1 < 0 and c2 < 0 and c3 < 0:\n",
    "            ep = self.ep * other.ep\n",
    "        elif c1 < 0 and c2 < 0 and c3 >= 0:\n",
    "            ep = self.ep * other.ep / self.F.gen()\n",
    "        elif c1 == 0 and c2 < 0:\n",
    "            ep = self.ep * other.ep\n",
    "        elif c2 == 0 and c1 < 0:\n",
    "            ep = self.ep * other.ep\n",
    "        else:\n",
    "            assert False\n",
    "        return D_cover(a3, b3, c3, d3, self.D, ep)\n",
    "    \n",
    "    def __pow__(self, other):\n",
    "        assert other in ZZ\n",
    "        if other == 0:\n",
    "            return D_cover(1, 0, 0, 1, self.D, 1)\n",
    "        elif other > 0:\n",
    "            re = self\n",
    "            for idx in srange(other-1):\n",
    "                re = re * self\n",
    "            return re\n",
    "        else:\n",
    "            (a1, b1), (c1, d1) = self.mat\n",
    "            inv_mat = self.mat ** (-1)\n",
    "            (a2, b2), (c2, d2) = inv_mat\n",
    "            temp = D_cover(a2, b2, c2, d2, self.D, 1)\n",
    "            ep0 = (self * temp).ep\n",
    "            inv = D_cover(a2, b2, c2, d2, self.D, ep0**(-1))\n",
    "            re = inv\n",
    "            for idx in srange(-other-1):\n",
    "                re = re * inv\n",
    "            return re\n",
    "    \n",
    "    def __truediv__(self, other):\n",
    "        return self.__mul__(other^(-1))\n",
    "    \n",
    "    def __eq__(self, other):\n",
    "        return (self.mat, self.D, self.ep) == (other.mat, other.D, other.ep)\n",
    "            \n",
    "    def __str__(self):\n",
    "        (a, b), (c, d) = self.mat\n",
    "        return f'([{a},{b};{c},{d}], {self.ep})'\n",
    "    def __repr__(self):\n",
    "        (a, b), (c, d) = self.mat\n",
    "        return f'([{a},{b};{c},{d}], {self.ep})'\n",
    "    \n",
    "def etaPsi(a, b, c, d):\n",
    "    if c > 0:\n",
    "        return (a+d)/c + 12*dedekind_sum(-d,c) - 3\n",
    "    elif c < 0:\n",
    "        return (a+d)/c + 12*dedekind_sum(d,-c) + 3\n",
    "    elif a > 0:\n",
    "        return b\n",
    "    else:\n",
    "        return -b - 6\n",
    "\n",
    "print('Define Sage functions successfully.')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2eea86d0-7a3c-4984-a3ed-90ed76be3226",
   "metadata": {},
   "source": [
    "Below are some usage examples. We first create a cyclotomic field $\\mathbb{Q}(\\zeta_5)$ stored in the Python variable `F`, where $\\zeta_5=\\mathfrak{e}\\left(1/5\\right)$. Then we create two elements `e1`, `e2` in $\\widetilde{\\mathrm{GL}_2^{+}(\\mathbb{R})^5}$, and compute their product, inverses, and powers. Note that `e1` is the element $(\\left(\\begin{smallmatrix}{0} & {-1} \\\\ {1} & {0}\\end{smallmatrix}\\right),\\mathfrak{e}\\left(0/5\\right))$, and `e2` is $(\\left(\\begin{smallmatrix}{3} & {2} \\\\ {-11} & {-7}\\end{smallmatrix}\\right),\\mathfrak{e}\\left(2/5\\right))$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "74ae49f1-d6ae-47fe-90c3-d5713bfa28a8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The product of e1 and e2 is:\n",
      "   ([11,7;3,2], zeta_5^2)\n",
      "The fourth power of e1 is:\n",
      "   ([1,0;0,1], zeta_5)\n",
      "The reciprocal of e2 is:\n",
      "   ([-7,-2;11,3], zeta_5^3)\n"
     ]
    }
   ],
   "source": [
    "D = 5\n",
    "F.<zeta_5> = CyclotomicField(D)\n",
    "e1 = D_cover(0, -1, 1, 0, 5, zeta_5^0)\n",
    "e2 = D_cover(3, 2, -11, -7, 5, zeta_5^2)\n",
    "prod_e1_and_e2 = e1 * e2\n",
    "e1_power_4 = e1^4\n",
    "reciprocal_e2 = e2^(-1)\n",
    "print(f'The product of e1 and e2 is:\\n   {prod_e1_and_e2}')\n",
    "print(f'The fourth power of e1 is:\\n   {e1_power_4}')\n",
    "print(f'The reciprocal of e2 is:\\n   {reciprocal_e2}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "25e868d1-7118-4044-9d72-64998ee38dd6",
   "metadata": {},
   "source": [
    "<a id=\"sec33\"></a>\n",
    "### SageMath code for $(x_c)_{c\\mid N}$ and $(r_n)_{n\\mid N}$ (Section 5 of the paper)\n",
    "\n",
    "The following cell contains some Sage functions that compute the vectors and matrices occuring in Section 5 of the paper. The functions `rn_to_xc` and `rn_to_xc_list` both take an argument representing $(r_n)_{n\\mid N}$, and return $(x_c)_{c\\mid N}$ defined in Eq. (5.1) in the paper. The difference is: the parameter of `rn_to_xc` is a Python dictionary, while that of `rn_to_xc_list` is a list. The function `matrix_AN(N)` computes the matrix $A_N$ defined in Section 5 of the paper, prior to Proposition 5.1; `matrix_Apa(p, a)` is equivalent to `matrix_AN(p^a)`, but requires `p` to be a prime and is more efficient. Finally, `matrix_AN_inverse(N)` computes $A_N^{-1}$. It calls `matrix_Bpa(p, a)` several times to do this. When $N$ is large, `matrix_AN_inverse(N)` is more efficient than `matrix_AN(N)^(-1)`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "aaf12119-4d6f-479d-89fa-b7b7d7df5831",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Define Sage functions successfully.\n"
     ]
    }
   ],
   "source": [
    "def rn_to_xc(N, mathbf_r):\n",
    "    '''\n",
    "    input:  N is the level\n",
    "            mathbf_r is a Python dict representing the vector $(r_n)_{n\\\\mid N}$\n",
    "    output: a Python dict representing the vector $(x_c)_{c\\\\mid N}$\n",
    "    example: rn_to_xc(4, {1: 2, 2:-2, 4:6 })\n",
    "    '''\n",
    "    re = {}\n",
    "    for c in N.divisors():\n",
    "        re[c] = sum(N/gcd(N,c^2) * gcd(n,c)^2/n * mathbf_r[n] for n in N.divisors())\n",
    "    return re\n",
    "\n",
    "def rn_to_xc_list(N, mathbf_r):\n",
    "    '''\n",
    "    input:  N is the level\n",
    "            mathbf_r is a Python list representing the vector $(r_n)_{n\\\\mid N}$\n",
    "    output: a Python list representing the vector $(x_c)_{c\\\\mid N}$\n",
    "    example: rn_to_xc_list(4, [2, -2, 6])\n",
    "    '''\n",
    "    re = []\n",
    "    for c in N.divisors():\n",
    "        re.append( sum(N/gcd(N,c^2) * gcd(n,c)^2/n * mathbf_r[j] for j, n in enumerate(N.divisors())) )\n",
    "    return re\n",
    "\n",
    "def matrix_AN(N):\n",
    "    div = N.divisors()\n",
    "    rc = len(div)\n",
    "    A = matrix(QQ, rc, rc)\n",
    "    for ci, c in enumerate(div):\n",
    "        for ni, n in enumerate(div):\n",
    "            A[ci, ni] = N * gcd(n, c)^2 / gcd(N, c^2) / n\n",
    "    return A\n",
    "\n",
    "\n",
    "def matrix_Apa(p, a):\n",
    "    A = matrix(QQ, a+1, a+1)\n",
    "    for i,j in cp(srange(a+1), repeat=2):\n",
    "        if i <= j and i <= a/2:\n",
    "            A[i,j] = p^(a-j)\n",
    "        elif i <= j and i > a/2:\n",
    "            A[i,j] = p^(2*i-j)\n",
    "        elif i > j and i <= a/2:\n",
    "            A[i,j] = p^(a-2*i+j)\n",
    "        else:\n",
    "            A[i, j] = p^j\n",
    "    return A\n",
    "\n",
    "def matrix_Bpa(p, a):\n",
    "    B = matrix(QQ, a+1, a+1)\n",
    "    for i,j in cp(srange(a+1), repeat=2):\n",
    "        if i == j:\n",
    "            if i == 0 or i == a:\n",
    "                B[i,j] = p\n",
    "            else:\n",
    "                B[i,j] = (p^2+1) * p^(min(i, a-i) - 1)\n",
    "        elif i - j == 1:\n",
    "            B[i,j] = -p^(min(j, a-j))\n",
    "        elif j - i == 1:\n",
    "            B[i, j] = -p^(min(j, a-j))\n",
    "        else:\n",
    "            B[i, j] = 0\n",
    "    return B\n",
    "\n",
    "def matrix_AN_inverse(N):\n",
    "    div = N.divisors()\n",
    "    rc = len(div)\n",
    "    Ai = matrix(QQ, rc, rc)\n",
    "    for ci, c in enumerate(div):\n",
    "        for ni, n in enumerate(div):\n",
    "            f1 = 1 / N * prod(p / (p^2-1) for p in N.prime_divisors())\n",
    "            N_factor = N.factor()\n",
    "            f2 = prod(matrix_Bpa(p, a)[c.valuation(p), n.valuation(p)] for p, a in N_factor)\n",
    "            Ai[ci, ni] = f1 * f2\n",
    "    return Ai\n",
    "    \n",
    "print('Define Sage functions successfully.')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cd5696fc-39c3-48cf-b6d7-8af223009ec5",
   "metadata": {},
   "source": [
    "<a id=\"sec34\"></a>\n",
    "### SageMath code for computing dimensions (Section 4 of the paper)\n",
    "\n",
    "The following cell contains Sage functions related to Theorem 4.2, the main theorem of the paper. In view of its importance, we recall the theorem here.\n",
    "\n",
    "Let $N\\in\\mathbb{Z}_{\\geq 1}$, $r_n\\in\\mathbb{Q}$ for any $n \\mid N$ and $t\\in\\mathbb{Z}_{\\geq 0}$. Let $D$ be a positive integer such that $Dr_n\\in2\\mathbb{Z}$ for any $n \\mid N$. Let $\\chi\\colon\\widetilde{\\Gamma_0(N)^D}\\to\\mathbb{C}^\\times$ be the character (2.8) and $m$, $\\varepsilon_2$, $\\varepsilon_3$ are given by (3.2), (3.3), (3.4) respectively. Set $k=\\frac{1}{2}\\sum_n r_n+2t$ and\n",
    "$$\n",
    "x_c=\\sum_{n\\mid N}\\frac{N}{(N,c^2)}\\cdot\\frac{(n,c)^2}{n}r_n \\tag{4.4}\n",
    "$$\n",
    "with $c\\mid N$. Suppose either\n",
    "$$\n",
    "\\sum_{c\\mid N}\\phi(c,N/c)\\cdot\\left(\\left[\\frac{x_c}{24}\\right]+1\\right)>0,\\, t \\geq 1 \\tag{4.5}\n",
    "$$\n",
    "or\n",
    "$$\n",
    "k>2-\\frac{6}{m}\\varepsilon_2-\\frac{8}{m}\\varepsilon_3-\\frac{12}{m}\\sum_{c\\mid N}\\phi(c,N/c)\\cdot\\left(1-\\left\\{\\frac{x_c}{24}\\right\\}\\right),\\,t=0. \\tag{4.6}\n",
    "$$\n",
    "Then we have\n",
    "$$\n",
    "\\dim_\\mathbb{C} M_k(\\Gamma_0(N), \\chi)=\\frac{k-1}{12}m+\\left(\\frac{1}{4}-\\left\\{\\frac{t}{2}\\right\\}\\right)\\varepsilon_2+\\left(\\frac{1}{3}-\\left\\{-\\frac{t}{3}\\right\\}\\right)\\varepsilon_3\n",
    "+\\sum_{c\\mid N}\\phi(c,N/c)\\cdot\\left(\\frac{1}{2}-\\left\\{\\frac{x_c}{24}\\right\\}\\right). \\tag{4.7}\n",
    "$$\n",
    "\n",
    "The function `k_inequality_RHS(k, N, xc)` computes the right-hand side of the inequality in (4.6), where the parameter `xc` represents $(x_c)_{c\\mid N}$, and takes a Python dict; `k_inequality_RHS2(k, N, rn)` has the same functionality, but its parameter `r_n` represents $(r_n)_{n\\mid N}$. The function `compute_dim(N, rn)` computes the right-hand side of (4.7) with $t=0$, where `rn` represents $(r_n)_{n\\mid N}$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ea039b86-dc46-4396-8585-c99b05640775",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Define Sage functions successfully.\n"
     ]
    }
   ],
   "source": [
    "def k_inequality_RHS(k, N, xc):\n",
    "    G = Gamma0(N)\n",
    "    m = G.index()\n",
    "    e2 = G.nu2()\n",
    "    e3 = G.nu3()\n",
    "    einfty = G.ncusps()\n",
    "    rhs = 2 - (6*e2 + 8*e3 + 12*einfty) / m\n",
    "    for c in N.divisors():\n",
    "        rhs += 12 / m * euler_phi(gcd(c, N/c)) * (xc[c]/24 - floor(xc[c]/24))\n",
    "    return rhs\n",
    "\n",
    "def k_inequality_RHS2(k, N, rn):\n",
    "    xc = rn_to_xc(N, rn)\n",
    "    return k_inequality_RHS(k, N, xc)\n",
    "\n",
    "def compute_dim(N, rn):\n",
    "    xc = rn_to_xc(N, rn)\n",
    "    G = Gamma0(N)\n",
    "    m = G.index()\n",
    "    e2 = G.nu2()\n",
    "    e3 = G.nu3()\n",
    "    einfty = G.ncusps()\n",
    "    k = sum(rn[n] for n in N.divisors()) / 2    \n",
    "    re = (k-1)*m / 12 + e2 / 4 + e3 / 3 + einfty / 2\n",
    "    for c in N.divisors():\n",
    "        re -= euler_phi(gcd(c, N/c)) * (xc[c]/24 - floor(xc[c]/24))\n",
    "    return re\n",
    "\n",
    "print('Define Sage functions successfully.')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f451a1e5-3883-4a90-81b2-51104646c325",
   "metadata": {},
   "source": [
    "### SageMath code for the classification of eta-qoutient characters\n",
    "<a id=\"sec35\"></a>\n",
    "\n",
    "In Section 6 of the paper, we defined the sets $S(N,k)$, $S(N)$, $Z(N)$, and the quotients $\\mathscr{S}(N,k)$ and $\\mathscr{S}(N)$. It was shown that the elements of $\\mathscr{S}(N,k)$ are in one-to-one corespondence with characters of eta-quotients of level $N$, cover index $2$ and weight $k$, and that there is a bejection between $\\mathscr{S}(N,k)$ and $\\mathscr{S}(N)$. The functions `check_char_trivial`, `check_char_same`, and `mathscr_SN0` in the following cell concern these objects. `check_char_trivial(N, vec_r)` checks the triviality of the character $\\chi_{\\mathbf{r}}$, where $\\mathbf{r}=(r_n)_{n\\mid N}$ is represented by the vector `vec_r`; `check_char_same(N, vec_r1, vec_r2)` checks whether $\\chi_{\\mathbf{r}_1}=\\chi_{\\mathbf{r}_2}$. They use Newman's criterion (see Lemma 6.1 of the paper). `mathscr_SN0(N)` returns a complete set of representatives of $\\mathscr{S}(N)$ by enumeration and eliminating the equivalent items.\n",
    "\n",
    "The main result of Section 6 of the paper is Theorem 6.8, which gives a good representative set of $\\mathscr{S}(N)$ (generally different from the one returned by `mathscr_SN0(N)`). It requires two more concepts: one is $\\mathscr{B}_N$, which is defined in Definition 6.4, and the other is the sequence $\\Delta_j$, which is defined in Definition 6.7. The Sage functions `mathscrB(N)` and `Delta_seq(N)` compute $\\mathscr{B}_N$ and the sequence $\\Delta_j$, respectively. They both do their work merely by the definitions. Once we obtain $\\Delta_j$, a good representative set of $\\mathscr{S}(N)$ is obtained by Eq. (6.9) of the paper. This set should be the same as what `mathscr_SN0(N)` returns."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f36aacf9-7154-4770-b4dc-16ce5054dcb3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Define Sage functions successfully.\n"
     ]
    }
   ],
   "source": [
    "def check_char_trivial(N, vec_r):\n",
    "    #ensure that sum(vec_r)=0 and vec_r is integral\n",
    "    s1 = sum(n * vec_r[ni] for ni, n in enumerate(N.divisors()))\n",
    "    s2 = sum(N / n * vec_r[ni] for ni, n in enumerate(N.divisors()))\n",
    "    p1 = prod(n for ni, n in enumerate(N.divisors()) if vec_r[ni] % 2 == 1)\n",
    "    return s1 % 24 == 0 and s2 % 24 == 0 and p1.is_square()\n",
    "\n",
    "def check_char_same(N, vec_r1, vec_r2):\n",
    "    vec_r = [vec_r1[j] - vec_r2[j] for j in srange(len(N.divisors()))]\n",
    "    return check_char_trivial(N, vec_r)\n",
    "\n",
    "def mathscr_SN0(N):\n",
    "    re = []\n",
    "    divs = N.divisors()\n",
    "    n_divs = len(divs)\n",
    "    for vec_r in cp(srange(24), repeat=n_divs-1):\n",
    "        vec_r = vec_r + (-sum(vec_r),)\n",
    "        for vec_r2 in re:\n",
    "            if check_char_same(N, vec_r, vec_r2):\n",
    "                break\n",
    "        else:\n",
    "            re.append(vec_r)        \n",
    "    return re\n",
    "\n",
    "def mathscrB(N):\n",
    "    '''\n",
    "    return $\\\\mathscr{B}_N$ as a Python list\n",
    "    '''\n",
    "    re = []\n",
    "    for p in N.prime_divisors():\n",
    "        if p != 2 and p != 3:\n",
    "            re.append(p)\n",
    "        elif p == 2:\n",
    "            a = valuation(N, p)\n",
    "            if a >= 3:\n",
    "                re += [2^(a-2), 2^(a-1), 2^a]\n",
    "            elif a == 2:\n",
    "                re += [2,4]\n",
    "            else:\n",
    "                re.append(2)\n",
    "        else:   #p=3\n",
    "            a = valuation(N, p)\n",
    "            if a >= 2:\n",
    "                re += [3^(a-1), 3^a]\n",
    "            else:\n",
    "                re.append(3)\n",
    "    return re\n",
    "\n",
    "def find_solution_ZN(N, n_precede_zeros):\n",
    "    '''\n",
    "    An auxiliary function used by Delta_seq\n",
    "    '''\n",
    "    BN = mathscrB(N)\n",
    "    t = len(BN)\n",
    "    for cn_tuple in cp(srange(1, 25), repeat=t-n_precede_zeros):\n",
    "        cn_tuple = (0,) * n_precede_zeros + cn_tuple\n",
    "        cn_dict = {BN[j]: cn_tuple[j] for j in srange(t)}\n",
    "        if sum( (n-1)*cn_dict[n] for n in cn_dict.keys() ) % 24 != 0:\n",
    "            continue\n",
    "        if sum( (N/n-N)*cn_dict[n] for n in cn_dict.keys() ) % 24 != 0:\n",
    "            continue       \n",
    "        temp = True\n",
    "        for p in N.prime_divisors():\n",
    "            if p != 2 and p != 3:\n",
    "                if cn_dict[p] % 2 != 0:\n",
    "                    temp = False\n",
    "                    break\n",
    "            elif p == 2:\n",
    "                BN2 = [n for n in BN if n % 2 == 0]\n",
    "                prd = prod(n for n in BN2 if cn_dict[n] % 2 == 1)\n",
    "                if not prd.is_square():\n",
    "                    temp = False\n",
    "                    break\n",
    "            else:      #p=3\n",
    "                BN3 = [n for n in BN if n % 3 == 0]\n",
    "                prd = prod(n for n in BN3 if cn_dict[n] % 2 == 1)\n",
    "                if not prd.is_square():\n",
    "                    temp = False\n",
    "                    break\n",
    "        if temp == True:\n",
    "            #we have find a solution cn_list (or cn_dict) with n_precede_zeros 0\n",
    "            return cn_tuple[n_precede_zeros]\n",
    "        \n",
    "def Delta_seq(N):\n",
    "    if N == 1:\n",
    "        return (1,)   \n",
    "    #here N>=2\n",
    "    BN = mathscrB(N)\n",
    "    t = len(BN)\n",
    "    Delta = []\n",
    "    for j in srange(t):\n",
    "        Delta.append(find_solution_ZN(N, j)) \n",
    "    return tuple(Delta)\n",
    "\n",
    "print('Define Sage functions successfully.')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ccd047b3-ef0d-4655-b93f-0b67345b7894",
   "metadata": {},
   "source": [
    "As some examples, we compute `mathscrB(N)` and `Delta_seq(N)` for the first few `N`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "59bc37b8-a820-488b-b15f-a8711d2b6c79",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "N=1, Delta sequence=(1,), product_Delta=1, #scrSN=1\n",
      "N=2, Delta sequence=(24,), product_Delta=24, #scrSN=24\n",
      "N=3, Delta sequence=(12,), product_Delta=12, #scrSN=12\n",
      "N=4, Delta sequence=(24, 8), product_Delta=192, #scrSN=192\n",
      "N=5, Delta sequence=(6,), product_Delta=6, #scrSN=6\n",
      "N=6, Delta sequence=(24, 12), product_Delta=288, #scrSN=288\n",
      "N=7, Delta sequence=(4,), product_Delta=4, #scrSN=4\n",
      "N=8, Delta sequence=(2, 8, 24), product_Delta=384, #scrSN=384\n",
      "N=9, Delta sequence=(12, 3), product_Delta=36, #scrSN=36\n",
      "N=10, Delta sequence=(8, 6), product_Delta=48, #scrSN=48\n",
      "N=11, Delta sequence=(12,), product_Delta=12, #scrSN=12\n"
     ]
    }
   ],
   "source": [
    "for N in srange(1, 12):\n",
    "    De = Delta_seq(N)\n",
    "    scrSN = mathscr_SN0(N)\n",
    "    print(f'N={N}, Delta sequence={De}, product_Delta={prod(De)}, #scrSN={len(scrSN)}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "81d13b6e-6d44-4e66-b4f8-aeb3c1bca8f7",
   "metadata": {},
   "source": [
    "The reader may compare the above output with Examples 6.10-6.12 of the paper.\n",
    "\n",
    "# Genrating the tables in the paper\n",
    "<a id=\"sec4\"></a>\n",
    "\n",
    "There are totally three tables in the paper. They are all generated by programs, using the Sage functions defined in the previous cells. The table in weight $3/2$ is omitted in the paper (see Section 7.3 of the paper). These tables, together with the SageMath code used to generated them, can be found below. Also, there are code listing each individual $\\dim_\\mathbb{C} M_k(\\Gamma_0(N), \\chi)$.\n",
    "\n",
    "The admissible eta-quotients of type I listed in Table 3 of the paper are not complete, since this table only concerns the level $N=1, p_1, p_1^2, p_1^3$ or $p_1p_2$ where $p_1$, $p_2$ are distinct primes. Below the reader may also find the code to produce admissible eta-quotients of type I of level $N=12,16,18,20,24,28,30,32,36$. We also provide the code to produce admissible eta-quotients of type II.\n",
    "\n",
    "### Table 1 of the paper\n",
    "<a id=\"sec41\"></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e8f8fa6-39d7-40dc-a123-028c3414fac3",
   "metadata": {},
   "source": [
    "### Table 2 of the paper\n",
    "<a id=\"sec42\"></a>\n",
    "\n",
    "### Table 3 of the paper\n",
    "<a id=\"sec43\"></a>\n",
    "\n",
    "### More admissible eta-quotients of type I\n",
    "<a id=\"sec44\"></a>\n",
    "\n",
    "### Admissible eta-quotients of type II\n",
    "<a id=\"sec45\"></a>\n",
    "\n",
    "# Numerical checks of some theorems\n",
    "<a id=\"sec5\"></a>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "SageMath 10.7",
   "language": "sage",
   "name": "sagemath"
  },
  "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.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
