
// Make newform 8.22.a.b in Magma, downloaded from the LMFDB on 25 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_22_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_22_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 := [96223, -4963, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [-211989, 640, 64], [-29657664, 662400, 8896]];
    Rf_basisdens := [1, 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 := 22;
    raw_aps := [[0, 0, 0], [32255, -1, 0], [-8037261, 9, 1], [98664098, -4014, 4], [-13444955723, -241515, 2856], [44577735635, 1219041, -8551], [2599270326072, -78703794, 206750], [11929396534227, 10179315, -319144], [64589693893622, 1679798214, 5214236], [1869113920498543, 1660970493, 33737045], [3748921264001176, -5920500456, 74881888], [-8090763662016401, -208805051403, 10140941], [-99386278879320634, -272353907700, 1361738284], [-11111339343224811, 85889919573, -5014813136], [-40291335714295108, -276404718564, 18460661896], [-379481145606515761, 43814693061, 21744109661], [3075197953846188001, 30637171373409, 79875012000], [-2184976859120054997, 28283296240809, -115270423775], [-5264341440545960537, -29752893747513, 323347301080], [-13713239988715498270, 137472678497106, -934927894380], [-6474024821894165672, -93484221354954, -378453267642], [-43911866990278864876, 279671030545140, 2322314505128], [21337821507520406283, 529310118462219, -4513980072832], [143296519800052541736, 1887497379621558, 4599855411654], [-108019528253374798368, -751754024242938, -21789767623018], [-1012889936741223802329, -978391150066035, 28898650386405], [-622079960262428883094, 10234360348812954, 3527075138084], [650153821231116137741, 1353403226310957, 1677328066072], [239115949166365030215, -18779062870293723, -71374029145283], [-687435853262627860286, 16200717160515408, -5634224853808], [-3556508164414050034960, -22866921029557584, 37664432452304], [-6631895400803170575641, -94180882021009209, -171939995196056], [3781959339831168794230, 30298353725840796, -736963025532100], [-6623674716556003350755, -117334094737899555, -878907612919024], [23908375048715462097603, 227577210016664457, 951388667274881], [46065885380720604528598, -407930468238913626, -1992524725236452], [24515649808998272710051, 930726537560543457, -1661046303470439], [124706922169512945041127, 328058904075181479, 266365952767632], [-57375712026338202457590, -176564201615504262, -4173189848211564], [-253001989349909587112837, 13181293939044681, -7375563885313087], [236420943485574808318435, -1810425170706668253, -1302763221763600], [-10218363847460694367237, 1015802140170198465, 5829823047884281], [-458864669448916846153856, -1302903981536226624, -6134729658632848], [1118472954281741312816496, -2600273307570251274, 19581158262285830], [204562612824215351163227, 1852287483257280657, 45662710019749705], [-232520752205166591236750, -13485536002406724318, 17952036612467844], [1694509996799377205633883, -14009718176637674085, 62006676773506864], [478446557228710410084008, 8182616283768464424, -82369721623600096], [3011706467668312231141767, 50501246891287263111, 43198929988768512], [3806914519058409872568027, -7319269633320943119, -10456128044506199], [-967594495406846113576728, -33657191541729650970, -41024188712535434], [-2487789507735911765385860, -30999060923310242532, 102823530583061560], [7594135199490765415898296, -22083893295664799250, 121479254667622718], [-3052683484109425986800335, 137276625552453751473, -214043229851681376], [-13992708457285977959506046, 165517427924901929088, -383174897169779072], [-33459136430883062030236238, -44272975719998551518, -57847022375766252], [683966057217923702274031, -303973766856477998451, 725259931836274789], [4623990844458813789325812, 77192142637949316756, -940456759395332104], [49412666009065754681436847, 26072464516091003205, 55545929531243229], [-12224408576481962407743440, 341130398040716243166, 1631012862934587310], [35569112158909302989488449, 355479997940485997409, -110457406097026408], [-7502647828896331912261877, -319611286298786710239, -994173576842885287], [21685451783464957386813611, -728353846791356897205, -4083531709894722632], [78254517520858197262437678, -431011234186015386114, -1765628247201403428], [-66176590213574579769367434, -203230244664241766676, -55261348840699380], [-112752877437080821004529073, 590234798926467628605, -240558404395120235], [-183072911235682180628270891, 462019717266310673397, -2114139846996262568], [104721007555062080334388010, 335438402859823268664, 9599091980624578168], [-241562504937785122946152759, -720859264026372687735, 9613424393587812304], [-89528479502603400887404857, 3571938770726226328245, -2685052020913615539], [-115407624000233329854505190, 3590482361022153162072, 4124131419084940696], [336969731685061839230115690, -3513434640054362516070, 8434128668609777508], [-469624031683388710342482772, -691996549133197122804, 11737217096626148936], [-955018686908341739812012633, 577044218926922893821, 24607912363170112725], [-163804936910872217726153695, 2086268488298028869985, -50918094617127373200], [1621742006611455461683594848, 2132027336756402962272, 1335262387508952768], [475129548492192266882543487, -9160213113126562002075, -9826376179099831043], [281581647258226658962880807, -10898353770286563646875, -27425855951935473027], [264388302616636554817799112, -13465267725088711750626, -22592871211775745042], [-1116986678433441286928083322, 15303049357458884549724, 8967691856573514236], [273395950694763567076971151, -2937333387166190111313, 102249554360681715800], [539465927010866853609602627, -12850582324096406035719, -125007134720120719503], [-1407118508244933954119536308, -24040490741053250792724, -55370902908363172936], [3371217311821741374190063912, -13441149777047686169634, -177832546452743679570], [-5177315779205777278949545458, 3571771342317328575198, 139900304356219170780], [5756780238996887605288306537, 14518790446016693189961, 60325944826070830696], [-1426848681527965035131549552, 51106640090930407370646, -125619413150832656730], [3920796783881283922422607222, 62868806322234793932636, 41661368513218358524], [-7951598527996086691433018849, -4433580814044491507907, -45462647785035435627], [-2867969954707861974307016332, 3111660214673802070356, 329962661114623045992], [9329736083272943000116165043, 66447538042641607562643, 103955925742515034600], [4878501570185095072813996200, -68609365131348654027480, -291464157913705608352], [5869195024452213946664562362, -15505335944223427970262, -558348633546690299116], [-2743158045135674502404446027, -6306136281658703752011, 362215822689700223104], [9153743570473230695699676515, -189640315753971967284381, 489321238147822164736], [3155640167565242516970346990, 100155437200317708998910, 246274329462071887244], [-7166763670180981262900298765, 184081493351006657547921, -146498498855398628151], [-20335555251700938415153820026, 6949505634917581226988, 874488989221904875788], [7227555437379616334944396997, -157199030405999954335899, -886714634746559430280], [11045654731382960427339228459, -280746850382177338573623, -2410470400319197872063], [-34649592323249195297906821329, 104143294872515356744527, 1281419974029580543256], [-24844270107581692683732754157, -511396412855641622497791, -81953014681495269447], [39362795767195766165223269675, -154709162897671805201973, -2105215690623897501832], [-17921566811379924524699201546, 766577981757787263966060, -2470615368101908333428], [-73283784957444358010085878999, 180425843810388863045001, 931193745149624627144], [21910720164420919094900116986, 197463055005692587798488, 1174963710893949115928], [76624934594326640790616680421, 609151831547308239547845, 1339816669296562622568], [-29750711677265493144384935486, 706906711944690241598064, 1042418316776176883952], [62219612771525827013010865366, 787855867322387713374438, 899457394732290352460], [-53837627785951332177255768664, -231929506554512394859722, -3060533708166426080698], [6094741955213150601003565304, -296577521873560421886024, -7144802958762136981488], [-61681210263484720020759649373, 337515418091292530396697, 3339991503946759719313], [124874897663962974382690885072, -877887011372481418777938, 6673134388863833567486], [-158582072791387427486499551171, 722041239889375470060957, -4154088431570220173304], [-360984465912083411391197506226, -622584463098191398366626, -971781652412864388084], [-94306027406840418287084893760, 94567809941976369288006, -6551135219955439966506], [106543826221581679645449293655, -2374928164988896225431177, 4622901754315478835096], [258626364491434203685239173650, -1622298026841011310164094, 2935781373467390576596], [106244331885334629683035245375, -1173760002265318860359907, 1518298244576405300853], [512220502998740844190482806427, 648783258437051646087195, -5056549008470034372192], [-202594060781671088425341457157, -2786356975909848446550303, -2098539759080303324135], [-315895091900220977165484411952, 2163886133004834262895862, -18875858979620718482170], [255763722768724830053154051743, 3614743231993986152783589, 2887093424569213297277], [80311231552465803241399782533, -1577117038624144512930171, 14449545559984516575488], [-306150393202493023934487776221, -2380738057514524776084381, 16777517565539450646960], [-465146748078209716717141720037, -5591989598050924474450599, -5289939944901266086831], [727338398323568981622051342879, -3952697588082027247133115, 4778709266202501498077], [-534669590712071809760457657900, 554318325594546338033460, 25820142947076965146504], [779707678028279162865370091574, 1630627823343028351052358, 26345004407574790945740], [565561394363770075595838881887, 5947909716809442605760621, -30500926053195201236411], [913483835369836530563298738503, 4932219240003113912419911, -7081652297576649172416], [-418258309185770544950103756166, -2607577086838510738782486, -6131101013171088916204], [602148415584691079297489325404, -11870699671960875164568708, -31417555160610146770664], [1201596498673217619817685776563, -1057794162975182258733735, 49244604487068482288081], [122272630953289998909029374838, 1997350841679590616186924, -95004990781200417971892], [-1028205240583716769736118490576, -8762567086032408348808842, 17077972405343468344710], [1090999908275727913559749525903, -13115336562710917051739211, -56986260926371813298995], [1730711915635582377146126117163, 2172834083541205260235755, -15403243582496158371440], [416936481807846359612613995411, 103458470989091766484113, 23274991706806090760393], [2353094361234994485895910832742, 23212460230983075024886572, 58313966371734024121420], [796439780754461229765997165165, -11354291449941747393231795, -125661714919906162174104], [1485164928704061205988251965031, 733234228657721631238653, 128863441130266714788053], [691325824929027431036274669806, 295143929852570571088062, 152312032641883510036764], [-2609589341279303014257287099751, 1465512133264814064883257, 58253883612019050635192], [-867402588284747209156622474157, -21906754903388610567823311, -32986181301821596716695], [129670887582649685411687446066, 5514991182395842228544610, -176953730873143291635868], [-154295134482166805244815503057, 36816229671995042236981125, 13055698424555999117085], [3927255323783866095790941136374, 424543616076002598210636, 167229024570955820647020], [-2300025631339906432937796237911, -2890094340027865055028759, 40780307342836568897456], [-1627867321133349012565106779224, 49255388197748089362089640, -181491860134874794784192], [-7611986981623857533218348519265, 20806594447057552447774749, 123695512535526102400373], [-1352282834545617052218711103734, 5942813082347000277964152, -158906069111221198448968], [1174912272413871764885980991755, 22614117999354946032879339, 138463450189113589154152], [6451414720113658245623772658974, -45493084395385384288900626, 18091887456436037106908], [-1443318573772930874918713784627, 46149615007694989151092173, -109667929767836448141120], [6882069573964474005623740823860, 26000179418064245264005716, -308362568088819854994600], [-3653062580712590080369396473674, -137732857805442185667239418, -342533785408537668025284], [-14945279786719564996727610919312, 40305447808699870365357078, 208469453948715164398374], [1613016248972044486050591631176, 74111118089605966956142854, -204210986320842514436202], [-8055625874182926998496554236201, 9149677674125149053846837, -356639260339006583775539], [3280669527116355640207237410731, -98195145433649768248853301, -387495546964156169062952], [11123078900112872437371674586246, 34457683617474947967433788, -445742766631629882167076], [-18548179776540431324162923328814, 128864620164191086186975746, -543472582741866210222556], [27712126065132040796602342567837, -56460359040814285057379043, 479814466213237019978144], [-8170719964520996386273619273800, -180582036697062136915778034, -81667872929007352086434], [8565226006495594563321002292054, 112247663259696745793841126, -904152364983914479157844], [8321845039037890948164475281272, 181768250969791101979229880, -629419873631196712782736], [7969775091660831058120016846143, 304448718929040180724043205, -182756063615290457461155]];
    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_22_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_22_a_b(:prec:=3)
    chi := MakeCharacter_8_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 22));
    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_22_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_22_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_8_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,22,sign)));
    Vf := Kernel([<3,R![-156439878638400, -20431242576, -96764, 1]>],Snew);
    return Vf;
end function;