
// Make newform 21.32.g.a in Magma, downloaded from the LMFDB on 20 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_32_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_32_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 := 32;
    raw_aps := [[0, 0], [14348907, 14348907], [0, 0], [-1942145413486, 13418820913929], [0, 0], [-201292386637052593, 402584773274105186], [0, 0], [-123931229926110186894, 61965614963055093447], [0, 0], [0, 0], [91621807513192521639905, 91621807513192521639905], [0, 2258691937157207814386543], [0, 0], [-17781535151198423863008665, 0], [0, 0], [0, 0], [0, 0], [10803192165999113826726622808, -5401596082999556913363311404], [-30271048416507274043668039127, 30271048416507274043668039127], [0, 0], [76382928072020742980502400263, 76382928072020742980502400263], [0, -150372080821436019808890048863], [0, 0], [0, 0], [5837726661152743544476473998344, -11675453322305487088952947996688], [0, 0], [2940773531246419173832609906846, -1470386765623209586916304953423], [0, 0], [71292951824461433049138701794433, -71292951824461433049138701794433], [0, 0], [-262351405651425841776553354904383, 0], [0, 0], [0, 0], [1895698130852417955252120369380137, -3791396261704835910504240738760274], [0, 0], [-11145712608728974633293670687493404, 11145712608728974633293670687493404], [-11175157036984149628061979215480316, -11175157036984149628061979215480316], [0, -17059827769303599018539722472409944], [0, 0], [0, 0], [0, 0], [-34158926368236120864429118839937215, 68317852736472241728858237679874430], [0, 0], [-73905309438671067562608859015030825, 73905309438671067562608859015030825], [0, 0], [309104889341663207725033843357573102, 309104889341663207725033843357573102], [1477451876788609854679242235199671216, 0], [-2891298622760933063853315738656461242, 5782597245521866127706631477312922484], [0, 0], [5061632774944129081359190103768403134, -2530816387472064540679595051884201567], [0, 0], [0, 0], [-3430560631933223585381129231911039216, -3430560631933223585381129231911039216], [0, 0], [0, 0], [0, 0], [0, 0], [118059846092887261529569973725270238380, -59029923046443630764784986862635119190], [76902214537654170825075130836313173967, -76902214537654170825075130836313173967], [0, 0], [-113538807110000998604227277368256191433, -113538807110000998604227277368256191433], [0, 0], [-410016623529580944024926805584450897557, 820033247059161888049853611168901795114], [0, 0], [1096358092789337931482702043906339312042, -548179046394668965741351021953169656021], [0, 0], [0, 1717718145404910555438023713046550994499], [-2677750541012733903723540414590413651085, 0], [0, 0], [-2365199584755153490422476587521317555620, 4730399169510306980844953175042635111240], [0, 0], [0, 0], [-2679274651458876041190704902243501679613, -2679274651458876041190704902243501679613], [0, 13634099936479272882443443826653615161109], [-17208493500083511570825178594427097767687, 0], [0, 0], [0, 0], [11106672784403219385391353411137017476454, -5553336392201609692695676705568508738227], [0, 0], [22006368554147561140108570587268217787673, 22006368554147561140108570587268217787673], [0, 0], [67113692227934416684050503614838412467381, 0], [0, 0], [-76544898061754248621390482285263963101691, 153089796123508497242780964570527926203382], [161292303185663410845436815287197603976964, -80646151592831705422718407643598801988482], [0, 0], [0, 0], [0, -295276564031495965472723267490282361661863], [0, 0], [406180421765187984168275460116056210670939, 0], [0, 0], [0, 0], [-164567734247990898359526793150903070367925, 164567734247990898359526793150903070367925], [0, 0], [0, 817102397558879744292545593099538633806793], [0, 0], [0, 0], [0, 0], [-963517493742845995829667133612328093636362, 481758746871422997914833566806164046818181], [0, 4178383817848145611778675072803449272952571], [2060081000669134949547773001317384753685320, 0], [0, 0], [0, 0], [0, 0], [-1182468711701311023881163348409571851391069, 1182468711701311023881163348409571851391069], [-7152771209933814064187672626406966529191717, -7152771209933814064187672626406966529191717], [0, 0], [0, 0], [0, 0], [-12132896104968703443146342185297725792600001, 24265792209937406886292684370595451585200002], [4238465190871348571730452501231824095267858, -2119232595435674285865226250615912047633929], [-660148383291308717509214672036776958884670, 660148383291308717509214672036776958884670], [0, 0], [-16343590890723188334171793237972421626308655, -16343590890723188334171793237972421626308655], [-50070791416726097522544538102045445762081356, 0], [0, 0], [-15849331704945311144937855529022721127103897, 31698663409890622289875711058045442254207794], [0, 0], [0, 0], [0, 0], [7344771654198672952738147822657248152215191, 7344771654198672952738147822657248152215191], [67906944289487125599247931777451745044210541, 0], [0, 0], [0, 0], [-208257206423707695142892532795862414302653378, 104128603211853847571446266397931207151326689], [0, 0], [0, 181799739293285638954242771534536144546874722], [0, 0], [184637619320274678791480867099851268420205241, -369275238640549357582961734199702536840410482], [-378724698834056631377200698768419121798691166, 189362349417028315688600349384209560899345583], [-449534984889229717310748764244328077969905897, 449534984889229717310748764244328077969905897], [0, 0], [0, -654096807321926559394574775363172493076879259], [369449900185306658069124684441017933665455118, 0], [0, 0], [-348826714327938124252803354337201235441591535, 697653428655876248505606708674402470883183070], [0, 0], [-839067931466580541263751611692837334751933514, -839067931466580541263751611692837334751933514], [0, 0], [0, 0], [-795943291770793711162372524457898654485032594, 1591886583541587422324745048915797308970065188], [0, 0], [1770525105271326834683056838488319731710829036, -1770525105271326834683056838488319731710829036], [0, 0], [1605251903007217454119332336370679125135360017, 1605251903007217454119332336370679125135360017], [0, 0], [-1497865361306417163342394609609008664022368989, 2995730722612834326684789219218017328044737978], [0, 0], [-869854026703056998208907023139970833994997380, 434927013351528499104453511569985416997498690], [0, 0], [0, -868122225730873828269963850890968938864579862], [0, 0], [-28971931515350299761825433471455295347204371, 0], [0, 0], [13929737544464386259751625523364512241733601063, -13929737544464386259751625523364512241733601063], [0, 0], [0, 8356453820368162453461274771505574498147610451], [0, 0], [13316044215877305992328641310967402885024521547, -26632088431754611984657282621934805770049043094], [0, 0], [0, 0], [0, 0], [-36839770270623552452828868469630645177736684977, 0], [0, 0], [0, 0], [0, 0], [-44693443745306755401358328882624737774492677283, 44693443745306755401358328882624737774492677283], [-25717885781514982769150904999170311788717489911, -25717885781514982769150904999170311788717489911]];
    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_32_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_32_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, 32));
    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_32_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_32_g_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_21_g();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,32,sign)));
    Vf := Kernel([<2,R![0, 1]>],Snew);
    return Vf;
end function;