
// Make newform 8.20.a.a in Magma, downloaded from the LMFDB on 18 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_20_a_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_8_20_a_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 := [-363, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0], [-960, 1920]];
    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 := 20;
    raw_aps := [[0, 0], [-13956, -1], [613310, 44], [44255256, 3190], [-3581893804, -223467], [-5063461802, 71660], [-36022539470, 17338504], [-1560240236116, -26137813], [-7379603545144, 184291330], [-15124769622522, -893092484], [-61694781388960, -3864337064], [1007696585087262, -3072429508], [1270392479752122, -47315480368], [-2816827546694732, 16432445197], [-10974169793565168, 13905445988], [-4709062533452338, -810595524548], [49271224795203812, 1148874921753], [5146072688919910, -3405518362868], [37876814001992252, -6468769570097], [8703526283356888, -4032598137882], [-428754127529916134, 10723507910184], [-113145960722427536, -8826033368644], [383757850730492524, 1875501918907], [3046272947217587274, 7105196196264], [774074124761173538, -186089076998104], [2733260702633221086, 173719470669660], [-4551439153366217672, 441601419650558], [-7140639471828893580, 468303491348381], [-8122524698728499594, 103317192128668], [-5875944145276806318, -87896022943040], [79451309965306467712, 373213916099360], [-9293311786931300612, 766636796231615], [-93729933570108805926, -2475535162104176], [-271077829513075705644, 3048043473323477], [324816114843815610862, -84834755194004], [-17148545245575182008, -3774525566854814], [57272467154186196358, -13977182026602708], [-501888640923596872356, 34822680561831351], [1292906714155286580984, -7298372728452114], [-428926785084311309514, 30645153622894924], [-233312216690069025396, 72613000049104579], [-3061755365405240614578, 57094781039714188], [6965486668907826216000, -5605332048088496], [-863319763960083328702, 65402859110575976], [2554638442797910332414, -255394252705354612], [3632381542580714035288, 159106555230358662], [-22371037418659489729556, 23815819958977627], [-21180160165689933857888, -486808267053970072], [-875459883400680953316, 70138930349077287], [-13559891801255554890914, 461999719572326284], [11455313696884412534522, 35679049006259816], [25156105865616157078096, -937747839803578444], [-26085662808308132320302, -529436536902822136], [15759848509393685979940, -1438470831218752599], [5418450864115575296770, 1175714754073677824], [-54315502060717518578920, 3355239038630855670], [-49240514767346955354794, -4604333495664230404], [-137637647256038831254096, -1389584758427403124], [107978585702539001986414, 2732327901658202556], [191468958237563695229578, 3890855940406836488], [124056056270998330417028, -4009377981734477599], [31191914069778505622046, -5919435649550274356], [532596055193249949088524, 7133287001751357187], [-468920139627245747393496, -1407888093644506086], [-193410342183512086533654, 766956834067370064], [-141588008169125258743194, 18905368326771888380], [906347957488201157054228, -9656768355021600443], [689935269765023919405042, -4662374736888945632], [-559347214586085283761084, 54587615368139379761], [443115487200465011009478, -35241469668012449508], [1043702568545274621129250, 8187032778122706848], [-1565927427460650609682632, 3056340417300436926], [-74647037068544680896560, -31131824391626229292], [-81168405100030191963250, 94879927836853608156], [585597881923108797643940, 96981799980909318345], [1962328436978327314745472, 52491311459024942880], [1950443597514177735387710, 17225812403994425084], [-3307593525443451261788458, -22072951950989065188], [5915282050882510933376946, 18759235693170303816], [-7460158399238386986145014, -105691630828627031920], [-10164621841710459850179492, -68963588259790842281], [-4655052084736895500227682, 53979950858347349484], [-728271811532908604399984, 221403792815721322180], [-9309641547600011288749550, -202593850485513390264], [-2747778950171522092256856, 481230991773578262906], [-1288283083955003985829532, 227969855007826845977], [14648999813628854550606914, -212814734735979489048], [-13833875891991504023576422, 14379815545850757520], [27035153308126909066771862, -137116523857786505412], [-9230239644019387565569552, -388707063525315868452], [4690529863975477385781996, 353551016955425411387], [-26359829696553045505116512, 21110707427545910056], [-18055431664547704153340744, 873902814557499522398], [16122202758539852758218996, -599891403447951609571], [17224677339839621438160204, -62152652776479313805], [-30209103013275376638927768, 299456918522692743274], [29617583081469006139643046, 632947590559615174380], [58137119462080467340745050, -493011783779308059312], [11210628036315139849359700, -1755379791170253595819], [129938283851605705189489158, 54750542049973025292], [-35673222614364681204967972, 1845748468253405007287], [-123073109167123044080783434, -195353965599400732692], [-33863794263361928513982964, 903138971953784765635], [-155963085507256730467971862, 1151913453538925953104], [102131222165524036994499300, -4523012953042803176455], [54051837797532909263611586, 1988051288263168301984], [22862209709798509743757844, 5627501360151294665157], [-48037026429383642185603342, -3806331325148154554304], [74382248507322350731203720, -4452401628312690103694], [96682682942314781866648138, -3631183514854343611480], [-168463052179090500731692000, -1571340273767185092408], [-167812331384604684936400930, -4238720549169677009300], [-490236390980202075713136774, -1839736878515069861048], [-459798221203152112582443404, 2457323083608166096221], [-60229268248107616230350360, -2421198301558772468790], [301472847867986928165988994, -15729613943034995668440], [-333675107530484805473381124, 2347779588975334871679], [126835553484579045984353176, -7189707409019155752682], [231348848269967885323113942, -7997991975070181399364], [249766374820616320846089516, -14147119100285950165269], [90031462252885000087411694, 6089092946438453659276], [211470527509218699999178978, 15002106093868666027112], [-1283244496281182103905577826, -4337823888553228066436], [768555393667448911337397044, 1712998997446124613037], [-128938232548198570045549684, -2237583645301857007293], [497657507703872329825503462, 12127786247678204070220], [-1055709157492444735710786562, -21179372567196191551876], [-42498733355612549644512016, 48582983477176892212508], [-469519401561174086119130616, 27402437656748531373522], [1314688947720724972488266822, -25243769139541041111620], [625874881002081059248844828, 4403988384946759276455], [-1560878197131509132588290120, -58044231739370826967010], [-108948114057710367839676336, 13531260660896390988724], [7920138827521593023795726, 107264501614346024453804], [-2328620424260717901470378198, 24224682103376064530256], [-452119470907585736021160958, -118165628617137946258584], [-2641757156109975105508287810, -11455324682831975355716], [2893837548619627507079573164, -11649319067539103618165], [-461522026468723882715334394, -116586876444055384795796], [2031390014591953851570682042, 73983826994167035485264], [158113679229477383357599924, -46750614110858710544787], [-3057567028877773787979420978, -132365730420893662900772], [6199023798383195042197398056, 37021910935198848407706], [-4390575134117870912570679836, -140370334933094569652999], [5370849666239883327139199590, -111499547475334356051092], [172776610071668412627629016, 247382866014387534958054], [-3758210046344498046489320914, -78606674204369107113540], [1376239876071514994244247626, 42226860392386543663824], [-3021789747431695699399529916, 314225967155303746332209], [-4266508858582173391305298144, -156422348990831378825272], [2392232559548761724943344502, -165496666449974260129732], [2140789341712357633448798290, 48122921638050028701472], [-2193388362774207437725168180, -104796067362067675146509], [476747666709779910579582184, -405447500400154873209782], [15795506619608557484229450196, -190058952328948961505963], [-636677138353098554929964752, 501882081154137233996268], [-4857054627203186908400403896, -324510819889937631701214], [-5023892113206613884167564318, -421709595801478945155096], [-12900162484402906793966672070, 293189767893401805545448], [1967128844524506817356526134, 175207552383331730130524], [17333475370673251580079209100, -10236327092816865127837], [3945901691523076280760002922, -11283952381069283747568], [-8502191841105719434327824040, -30781492696057075681210], [-19583142593828640658070562156, -172660438231674780669947], [27835854456857424877483633266, 330467963078599889343368], [753517554279705518549726472, 81760353742849698733842], [-18404936024017735811335744352, -428387604277739119425688], [5367708803167451427500828254, -281643605612342930736900]];
    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_20_a_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_8_20_a_a(:prec:=2)
    chi := MakeCharacter_8_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 20));
    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_20_a_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_8_20_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_8_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,20,sign)));
    Vf := Kernel([<3,R![-1144314864, 27912, 1]>],Snew);
    return Vf;
end function;