
// Make newform 8.18.a.b in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_8_a();"
// To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
// To make the newform (type ModFrm), type "MakeNewformModFrm_8_18_a_b();".
// 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_8_18_a_b();".
// 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 := [-114, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0], [0, 1152]];
    Rf_basisdens := [1, 1];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    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_8_a();"
function MakeCharacter_8_a()
    N := 8;
    order := 1;
    char_gens := [7, 5];
    v := [1, 1];
    // 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;

function MakeCharacter_8_a_Hecke(Kf)
    return MakeCharacter_8_a();
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 := 18;
    raw_aps := [[0, 0], [5796, 1], [-395962, -68], [-9466296, -1582], [-235385620, -10269], [-1251761714, 49948], [-24222344174, 1366664], [-41241650380, 6247141], [-153548515624, -7579978], [994787693790, 46341932], [5376066787616, -263508856], [-25587226374810, -441997556], [-57113645522262, 2940525904], [-28378777101812, -4230485749], [100776581579184, 10149312940], [108637731793526, -56505234100], [-233155724445028, 65093222991], [1031908631813374, 101795756156], [-3318612981179164, -53283505807], [771842498992264, 360055439394], [-1867053400179254, -288694867608], [8877689253665488, -575220781772], [12726798779079028, -1526245359211], [-33608419329601830, -1843251450264], [-65489512430721694, -6403182311000], [5428096652238246, -8157019935060], [-87318027442574936, 2132025210730], [150512778418509900, 8616761561275], [79284874294983214, -12155573727220], [209009332558894962, -2513963964736], [-531378126078518144, 69516668246368], [1129927457112872740, -9287883789887], [564448812801876234, 40113981968144], [877548155484622188, -87342554671709], [689648720688222934, -260027985047236], [-6077769430263399592, -30821498063146], [-1432338303732463778, 313699565695644], [-6122367630907710396, -119917081412247], [-1910089977932676120, 632529777747258], [-1255689765937301394, -1084929368939908], [23885579433399448788, 156511274097773], [-8951558175871887114, -170087067253924], [-827614575352078656, -2294707673154064], [-25113614070528171070, 474420476466664], [-945597072375691770, 2396595992864668], [-23960480212284283640, 1200476450229762], [-12146290440178047500, 6398764690061429], [2280392231600467936, -6725608613631560], [-30488514413868126588, 91054552307001], [69396366176185511974, 766535799776540], [66063354284437610, 7442174132044456], [143677722431637749872, 9583028992946332], [-14336341436166620430, 1462686083494408], [-19407631630922399396, 7032220925441151], [-334585355892442492670, -9959466537036800], [234767097112309198664, -23213833656042414], [-41255700927885586226, 15135672359533420], [347778665520584771984, -26809247986169180], [374249149043200909654, -40371941335616436], [-70916001477774762086, 15512607455304904], [143404768561550136956, 63221194862603623], [-14509276318040564634, 126770828509862492], [356646232917674870100, -35040624816180307], [876984629283253688568, -68711161914441474], [1173932561650000966842, 52864038350889936], [-1095654280693947928578, 58952598697385260], [1149766180255620194348, -91679388581805325], [-537976126124245652910, 316639942381304864], [1232021566760430177468, 23626910901678679], [-2325945669158620813794, -44601331659996084], [-335039696312750877854, 219626634688668064], [-684242732938316557656, -182937960819110742], [-3901870562616531829520, 650776712144393980], [5517397739252304255926, 472705985670007404], [-3027361574490370261540, 570206998412433375], [6443384033949788016000, -132236979833944224], [-6183492617775477037882, 13781460402756172], [-18727081346977337412274, 122709175933733388], [-7599215116986539934318, -300249746830956216], [-7940483104421834226918, -24667123580980208], [2325076808576027720004, 1051405095711706313], [20087668878619592098022, -947009871843226884], [29892833511763178387248, -894631619130228532], [5175327162533474197426, -564514599283565496], [15482212241916269002104, 2012135838645994782], [-25534551334578113915876, 96275057234069455], [-14833145897408688601918, 2083052587725633384], [-21881931160151921772982, -3779771050216990192], [-8416810377415881931378, -1215806528601421140], [-2767393596707336804272, -1633427821799944044], [-46047629845428250238220, 3305412950127458197], [-43426578255613165741856, -4595583015983761672], [47134788901642487775016, 2868488752882901386], [52166910096500187079116, -5251433792643274565], [66353699760522974869524, -4914314011409639683], [-65594619633757983246408, -163508853155949586], [-59293725135366088651074, 4921235414330544348], [121507012776804499128970, -6839655723311557680], [67865362639554863306476, 11847798668130422179], [-33461759486617148119842, -9674523505405581060], [-86136460794363247764028, -9338174102580261911], [160396431410722320866798, 19655034210819894684], [6504201242603520716372, -15421232309568042259], [68345979508894610081210, -26731917737306511408], [242212517765199306909852, 13434905402996699503], [-458470043286476557172158, 2498472669080229280], [-61283910349125893570260, 29262096334872843123], [-377099858680436375419054, -2411164020215044032], [72968156189506025743128, -12798341013478411258], [10606952093281145286874, 13492724576343142504], [-343113583582281345563296, -24651719128684842024], [238383663113092061064614, -2016486865269229444], [791359516130383427714538, 15752843757515166344], [-9482488298579308801460, 22122991961791126779], [-156290190864617635898312, -38282143852941051378], [13605312492447843256706, 21038465629093732008], [-228798105939519333732060, 53036052514076801793], [-359289185411610779173688, -71995095684808819790], [-790454464411667843845554, -49555346754731857620], [-296470657444403786121804, 20991635020615001253], [188614926334769745375446, 8084028391606267868], [-68768449120392292326494, 131392942942964180968], [-648300531246938289557530, -100256180694550700212], [24929138350614314477708, -173119341967207740181], [-305204505287020426025260, -43189244143983027219], [-1808681226347963391451266, 64795958665104933308], [2187479890587852569352710, 24606560420517357772], [12614293552763047878992, 143424168606004553044], [-1346927119372731469035624, -8899782181789308378], [-770808908879586417849442, 34753874929783258988], [1111482780793050475815044, -100431114215738895687], [-1680304995137273937754072, 63699348611856974282], [-571410851392384711531920, 310903099529273458844], [3389343490577288883882806, -69976364911466993924], [-3981265512501824169700742, -95562317392594123056], [825554252445215545035266, 79990374753442645992], [-391372052938765610768826, -139458928804468324084], [1069034456241333487888948, -198424567158375292795], [2521924414892812396693982, 189098691271665053660], [-238406364785038250678870, 313694190678845506768], [-5714309796627237030450932, 53530770699236152491], [-1754222817109640237790858, 308097132215243881580], [-400320395598505270113544, -284800727573301778818], [-3980511631211711780480612, -138507726261730736785], [-3699744916818295968758786, -261326804022901118884], [-896810520149454437864568, -734728496662365865438], [-689208811328076903868522, 387030716362315107660], [10369629865666193916713178, -205911095027346308016], [-8063450417557484134011204, 25480497343467125335], [8645236153371382706968160, 102030994523455693528], [10698721550375529639098670, -71651491630979883476], [-1950249191257921159729550, -760313034080500684000], [-10001028935326007868751468, -46915430830973204227], [-12781011946760344587526856, 90639804060221047694], [-1616284629234533765992340, 747341825318778280227], [-2248009603939461236682736, 1043193745377181196484], [19920031599523951066351192, -445994304823093516266], [-1992026645043510603053918, 2033417594045336173416], [-3105586147078140750049494, -483360893966216674776], [24275176112141090596890990, -489958102772720418356], [-8551837375450790393328684, -349496531164921829171], [-13994827633316749105354950, -539038527140214332784], [-13453745068195220675383288, 1889856511992662255938], [-17718645004052372667727188, -555418424117794555213], [26641524992193321413229522, -1783659078728011028600], [-37435511883650537857212264, -44605034159013835290], [11315216649010741614871264, -1146046182085924964936], [16544999378647244624487718, 2803747382915453066700]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_8_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_8_18_a_b();".
// 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_8_18_a_b(:prec:=2)
    chi := MakeCharacter_8_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 18));
    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_8_18_a_b();".
// 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_8_18_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_8_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,18,sign)));
    Vf := Kernel([<3,R![-117696240, -11592, 1]>],Snew);
    return Vf;
end function;