
// Make newform 21.12.g.a in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_21_g();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_21_g_Hecke();"
// To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
// To make the newform (type ModFrm), type "MakeNewformModFrm_21_12_g_a();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose lines below.
// The precision argument determines an initial guess on how many Fourier coefficients to use.
// This guess is increased enough to uniquely determine the newform.
// To make the Hecke irreducible modular symbols subspace (type ModSym)
// containing the newform, type "MakeNewformModSym_21_12_g_a();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose line below.
// The default sign is -1.  You can change this with the optional parameter "sign".
function ConvertToHeckeField(input: pass_field := false, Kf := [])
    if not pass_field then
        poly := [1, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_21_g();"
function MakeCharacter_21_g()
    N := 21;
    order := 6;
    char_gens := [8, 10];
    v := [3, 5];
    // chi(gens[i]) = zeta^v[i]
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    F := CyclotomicField(order);
    chi := DirichletCharacterFromValuesOnUnitGenerators(DirichletGroup(N,F),[F|F.1^e:e in v]);
    return MinimalBaseRingCharacter(chi);
end function;

// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_21_g_Hecke();"
function MakeCharacter_21_g_Hecke(Kf)
    N := 21;
    order := 6;
    char_gens := [8, 10];
    char_values := [[-1, 0], [0, 1]];
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    values := ConvertToHeckeField(char_values : pass_field := true, Kf := Kf); // the value of chi on the gens as elements in the Hecke field
    F := Universe(values);// the Hecke field
    chi := DirichletCharacterFromValuesOnUnitGenerators(DirichletGroup(N,F),values);
    return chi;
end function;


function ExtendMultiplicatively(weight, aps, character)
    prec := NextPrime(NthPrime(#aps)) - 1; // we will able to figure out a_0 ... a_prec
    primes := PrimesUpTo(prec);
    prime_powers := primes;
    assert #primes eq #aps;
    log_prec := Floor(Log(prec)/Log(2)); // prec < 2^(log_prec+1)
    F := Universe(aps);
    FXY<X, Y> := PolynomialRing(F, 2);
    // 1/(1 - a_p T + p^(weight - 1) * char(p) T^2) = 1 + a_p T + a_{p^2} T^2 + ...
    R<T> := PowerSeriesRing(FXY : Precision := log_prec + 1);
    recursion := Coefficients(1/(1 - X*T + Y*T^2));
    coeffs := [F!0: i in [1..(prec+1)]];
    coeffs[1] := 1; //a_1
    for i := 1 to #primes do
        p := primes[i];
        coeffs[p] := aps[i];
        b := p^(weight - 1) * F!character(p);
        r := 2;
        p_power := p * p;
        //deals with powers of p
        while p_power le prec do
            Append(~prime_powers, p_power);
            coeffs[p_power] := Evaluate(recursion[r + 1], [aps[i], b]);
            p_power *:= p;
            r +:= 1;
        end while;    
    end for;
    Sort(~prime_powers);
    for pp in prime_powers do
        for k := 1 to Floor(prec/pp) do
            if GCD(k, pp) eq 1 then
                coeffs[pp*k] := coeffs[pp]*coeffs[k];
            end if;
        end for;
    end for;
    return coeffs;
end function;


function qexpCoeffs()
    // To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
    weight := 12;
    raw_aps := [[0, 0], [-243, -243], [0, 0], [-51346, 25539], [0, 0], [704747, -1409494], [0, 0], [16824846, -8412423], [0, 0], [0, 0], [67664195, 67664195], [0, 663545123], [0, 0], [-1768358135, 0], [0, 0], [0, 0], [0, 0], [14349096968, -7174548484], [21410042863, -21410042863], [0, 0], [1424119743, 1424119743], [0, -21410392133], [0, 0], [0, 0], [72858114904, -145716229808], [0, 0], [-202206813974, 101103406987], [0, 0], [276932823173, -276932823173], [0, 0], [-744180695893, 0], [0, 0], [0, 0], [-695744844353, 1391489688706], [0, 0], [1879586495156, -1879586495156], [1341449695884, 1341449695884], [0, -1045396961144], [0, 0], [0, 0], [0, 0], [2926575711165, -5853151422330], [0, 0], [-1874749034425, 1874749034425], [0, 0], [4964504038162, 4964504038162], [-1606477746704, 0], [6604312147098, -13208624294196], [0, 0], [18479173230374, -9239586615187], [0, 0], [0, 0], [-6118106871376, -6118106871376], [0, 0], [0, 0], [0, 0], [0, 0], [-10464425852780, 5232212926390], [-31159345022093, 31159345022093], [0, 0], [-23635953243983, -23635953243983], [0, 0], [28551731165693, -57103462331386], [0, 0], [122585911267242, -61292955633621], [0, 0], [0, 43047742781669], [159215900252635, 0], [0, 0], [-52323699964780, 104647399929560], [0, 0], [0, 0], [-146491325258463, -146491325258463], [0, 165261257700409], [-286872997588217, 0], [0, 0], [0, 0], [325417217020654, -162708608510327], [0, 0], [89770152550033, 89770152550033], [0, 0], [-453143217297559, 0], [0, 0], [-1913154575891, 3826309151782], [-153205935435396, 76602967717698], [0, 0], [0, 0], [0, -852063517863583], [0, 0], [73862659573649, 0], [0, 0], [0, 0], [403931846394425, -403931846394425], [0, 0], [0, 1372545572773343], [0, 0], [0, 0], [0, 0], [2040062079075338, -1020031039537669], [0, 1941203075931871], [-2196042785816920, 0], [0, 0], [0, 0], [0, 0], [-2889129049208459, 2889129049208459], [-1599793543939517, -1599793543939517], [0, 0], [0, 0], [0, 0], [841134891953399, -1682269783906798], [4302460353323478, -2151230176661739], [-3370117000017230, 3370117000017230], [0, 0], [-2449360674457945, -2449360674457945], [3702574407997124, 0], [0, 0], [-153268810929527, 306537621859054], [0, 0], [0, 0], [0, 0], [-914043270707829, -914043270707829], [-4567496816840459, 0], [0, 0], [0, 0], [7709885132214562, -3854942566107281], [0, 0], [0, -9537098821055278], [0, 0], [6320681927369251, -12641363854738502], [-1908744961440566, 954372480720283], [9409195978620913, -9409195978620913], [0, 0], [0, -1676270586315889], [-10454396622565922, 0], [0, 0], [-6852944769422415, 13705889538844830], [0, 0], [6146301342529786, 6146301342529786], [0, 0], [0, 0], [-10400320155723294, 20800640311446588], [0, 0], [1890774381096796, -1890774381096796], [0, 0], [2971706472068037, 2971706472068037], [0, 0], [10782139548343311, -21564279096686622], [0, 0], [-4980650750704220, 2490325375352110], [0, 0], [0, -13162592902057862], [0, 0], [-18253700744782781, 0], [0, 0], [-22628265839332567, 22628265839332567], [0, 0], [0, 7171409850996401], [0, 0], [-23401579137589253, 46803158275178506], [0, 0], [0, 0], [0, 0], [29145016585712813, 0], [0, 0], [0, 0], [0, 0], [-8047986833417833, 8047986833417833], [33798037651085509, 33798037651085509]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_21_g_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_21_12_g_a();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose lines below.
// The precision argument determines an initial guess on how many Fourier coefficients to use.
// This guess is increased enough to uniquely determine the newform.
function MakeNewformModFrm_21_12_g_a(:prec:=2)
    chi := MakeCharacter_21_g();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 12));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(997) - 1;
    while true do
        trunc_vec := Vector(Kf, [0] cat [f_vec[i]: i in [1..prec]]);
        B := Basis(S, prec + 1);
        S_basismat := Matrix([AbsEltseq(g): g in B]);
        if Rank(S_basismat) eq Min(NumberOfRows(S_basismat), NumberOfColumns(S_basismat)) then
            S_basismat := ChangeRing(S_basismat,Kf);
            f_lincom := Solution(S_basismat,trunc_vec);
            f := &+[f_lincom[i]*Basis(S)[i] : i in [1..#Basis(S)]];
            return f;
        end if;
        error if prec eq maxprec, "Unable to distinguish newform within newspace";
        prec := Min(Ceiling(1.25 * prec), maxprec);
    end while;
end function;


// To make the Hecke irreducible modular symbols subspace (type ModSym)
// containing the newform, type "MakeNewformModSym_21_12_g_a();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose line below.
// The default sign is -1.  You can change this with the optional parameter "sign".
function MakeNewformModSym_21_12_g_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_21_g();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,12,sign)));
    Vf := Kernel([<2,R![0, 1]>],Snew);
    return Vf;
end function;