
// Make newform 21.36.g.a in Magma, downloaded from the LMFDB on 24 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_36_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_36_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 := 36;
    raw_aps := [[0, 0], [-129140163, -129140163], [0, 0], [107883136881134, -662291479140141], [0, 0], [28978182513508262987, -57956365027016525974], [0, 0], [-19353743062037673386514, 9676871531018836693257], [0, 0], [0, 0], [33080106230765423821013075, 33080106230765423821013075], [0, 1434976029760582590688944323], [0, 0], [69468510852631863435274380025, 0], [0, 0], [0, 0], [0, 0], [14841374445538366850508261301448, -7420687222769183425254130650724], [110280454033501822232789985372223, -110280454033501822232789985372223], [0, 0], [277870862872726158208605910185663, 277870862872726158208605910185663], [0, 1359000989429499836036390683764907], [0, 0], [0, 0], [55733582330499826554335304401167384, -111467164660999653108670608802334768], [0, 0], [376007214920096922371045734363857226, -188003607460048461185522867181928613], [0, 0], [-861208099730485045896091656445036507, 861208099730485045896091656445036507], [0, 0], [-6751573039794389171918297732231564773, 0], [0, 0], [0, 0], [-15859854051150316478561227474557163793, 31719708102300632957122454949114327586], [0, 0], [-144281714965060340307971672498237137324, 144281714965060340307971672498237137324], [-99309414179004989226438560419354847316, -99309414179004989226438560419354847316], [0, -1020650683603098592927294892180505806264], [0, 0], [0, 0], [0, 0], [3646484017785257048116038350062340634525, -7292968035570514096232076700124681269050], [0, 0], [19781507759038196563688004334383040106375, -19781507759038196563688004334383040106375], [0, 0], [-18026834722601462114744618904729535665518, -18026834722601462114744618904729535665518], [-94615845663539624742983676265861752193424, 0], [18296516590831438229560288743298429093338, -36593033181662876459120577486596858186676], [0, 0], [141821427507722901633687532241498069722214, -70910713753861450816843766120749034861107], [0, 0], [0, 0], [440425180308388342841337615034809735032624, 440425180308388342841337615034809735032624], [0, 0], [0, 0], [0, 0], [0, 0], [4479816241725004862266630761957138190983700, -2239908120862502431133315380978569095491850], [9813809563213986781122241187689816206158227, -9813809563213986781122241187689816206158227], [0, 0], [4611743278808204692851554135095296904167937, 4611743278808204692851554135095296904167937], [0, 0], [38178522024747924111004410074338383008344493, -76357044049495848222008820148676766016688986], [0, 0], [-97904074269522786570985973315186488496629398, 48952037134761393285492986657593244248314699], [0, 0], [0, 205123126780508394209169366774116025748336949], [-341603009175228068875044751722942374142779525, 0], [0, 0], [233411669050121764072221370620295979798275700, -466823338100243528144442741240591959596551400], [0, 0], [0, 0], [759467223475967210559319723962088473762507057, 759467223475967210559319723962088473762507057], [0, 1061252653240415499084768994859770103662335289], [233656084476083565029244214203811778153160343, 0], [0, 0], [0, 0], [6856712560331266103761859749462798237852786414, -3428356280165633051880929874731399118926393207], [0, 0], [-5745022763975380364813792260659301224003495407, -5745022763975380364813792260659301224003495407], [0, 0], [-15290221867439307300257133953296735010189720599, 0], [0, 0], [-14558015113695803518098466367925683342050299251, 29116030227391607036196932735851366684100598502], [35873258475169659981641904924758914283721424764, -17936629237584829990820952462379457141860712382], [0, 0], [0, 0], [0, -56790771263005287827281898226909740635660363423], [0, 0], [-68793885096025837739991561020132247776742797311, 0], [0, 0], [0, 0], [170580751032199498862366448683630331944192683625, -170580751032199498862366448683630331944192683625], [0, 0], [0, 323259431450920809324876081094244220109100896943], [0, 0], [0, 0], [0, 0], [-526115425327723095002195802296544860925117953302, 263057712663861547501097901148272430462558976651], [0, -1355123599174967357567495900435292896846125246049], [-1602324539101899378225167693042762487151550978200, 0], [0, 0], [0, 0], [0, 0], [1450104421894975633787654503837529705435347032901, -1450104421894975633787654503837529705435347032901], [-2313479500933232862695546040146535651335178395357, -2313479500933232862695546040146535651335178395357], [0, 0], [0, 0], [0, 0], [4656551219410546078605194896528651601238612578999, -9313102438821092157210389793057303202477225157998], [-6192310061083260434114217682639748187432176646282, 3096155030541630217057108841319874093716088323141], [-8936453022030749168368858000850168909926576861550, 8936453022030749168368858000850168909926576861550], [0, 0], [1320424453216185400560277394281070703839715073175, 1320424453216185400560277394281070703839715073175], [6875243838089780890527820119212927954050405969124, 0], [0, 0], [-2147847538667864788585286367312781076327668941287, 4295695077335729577170572734625562152655337882574], [0, 0], [0, 0], [0, 0], [-25459441166006895187889153077338883250839276758549, -25459441166006895187889153077338883250839276758549], [-28905730475152595574447742027215651082133262954539, 0], [0, 0], [0, 0], [-45484513757926513009093789542129802642060790286398, 22742256878963256504546894771064901321030395143199], [0, 0], [0, 110694027367363712321190672029844027615136886236082], [0, 0], [-81405712950190000671165409567136835581191972789709, 162811425900380001342330819134273671162383945579418], [55431958632335323449759246297883907914873322244874, -27715979316167661724879623148941953957436661122437], [5476975226875892907425531739928597604040170335393, -5476975226875892907425531739928597604040170335393], [0, 0], [0, 406647740876012291076522220091123721774547517816351], [-483680934428759977799690691639633588208127586576002, 0], [0, 0], [-359786287971562760724355348797016401164778320045775, 719572575943125521448710697594032802329556640091550], [0, 0], [458307364377089212795115730882537474696367898114266, 458307364377089212795115730882537474696367898114266], [0, 0], [0, 0], [-129355456318413735483735774531293455452306779576574, 258710912636827470967471549062586910904613559153148], [0, 0], [-1896193355274677938274083574067047167969130392615364, 1896193355274677938274083574067047167969130392615364], [0, 0], [-593113665619349968706587900659560993016559073711643, -593113665619349968706587900659560993016559073711643], [0, 0], [-1987615501613733890869628354424957284016587881088049, 3975231003227467781739256708849914568033175762176098], [0, 0], [-4491597137275569339657080939990901434210798902396700, 2245798568637784669828540469995450717105399451198350], [0, 0], [0, -3542484117848957871699116696910982781780297882892902], [0, 0], [3500475584515385484616235002492908122595162692937139, 0], [0, 0], [7897207471040835675598325635485848021628115905115673, -7897207471040835675598325635485848021628115905115673], [0, 0], [0, 7871446229172926677199077402580273952410228423472161], [0, 0], [9010652777893612376637395878413719141531687702442907, -18021305555787224753274791756827438283063375404885814], [0, 0], [0, 0], [0, 0], [-29330588827223330604842397579540772578500093318257027, 0], [0, 0], [0, 0], [0, 0], [7768789732491357933606517902130069664933028388023527, -7768789732491357933606517902130069664933028388023527], [-32778044503746027936408545852527486876324782497425691, -32778044503746027936408545852527486876324782497425691]];
    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_36_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_36_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, 36));
    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_36_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_36_g_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_21_g();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,36,sign)));
    Vf := Kernel([<2,R![0, 1]>],Snew);
    return Vf;
end function;