
// Make newform 5.20.a.a in Magma, downloaded from the LMFDB on 18 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_5_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_5_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_5_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 := [-646704, -22777, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [-30381, 104, 2], [91108, 738, -6]];
    Rf_basisdens := [1, 35, 35];
    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_5_a();"
function MakeCharacter_5_a()
    N := 5;
    order := 1;
    char_gens := [2];
    v := [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_5_a_Hecke(Kf)
    return MakeCharacter_5_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 := [[-335, 1, 0], [-24478, 18, -13], [1953125, 0, 0], [-18318318, -44498, -30963], [-2854948528, 2097940, 1637230], [-28548921090, -16253608, 4569380], [28946200346, -419322056, 205644916], [427611297876, 823817048, 307290116], [1362994724638, 154917186, -1481795237], [-24413692018026, 39133027952, 27451585384], [-94856262455188, -42652946780, -49909386010], [-143744977087986, 1379364009648, 11520078664], [693182242891602, -1886964293080, -536377941860], [365535421859474, -3394737590086, 525514034959], [-6812102764971382, -6507187638290, 596140161133], [2997775190704582, -22572443604152, -737811714228], [1004876734267828, 10591847238544, -23792666129352], [-13586251587741658, 67547157960800, 45709817443600], [-351763465234425834, -5238511075346, 13790531546061], [98344235061088532, -414937837689100, -86448548028450], [160488471498859618, 211121551372376, -25653244426332], [665876807810103624, 873237784655752, -246712757010516], [-500678249285396094, 716015554178850, -903195337524789], [347946659763910842, 1839171741416016, 128775009058872], [540011182022204218, 254078334113160, -3886383361239892], [2963110372755933262, 15011733912668880, -1021789689532040], [-3757303315337394618, -9385008409306902, 951456208075447], [-840659509081616218, 19974377407410102, -4903522530821863], [-26849200505753167802, -4010649815494896, 4313435686670168], [-4759171144568003710, -40048180767288368, 6639074608445560], [12152247534576958250, -62898736297592194, 44954789040759685], [18487949129221035552, -87225706071867060, 13680208647389730], [-71357419264009029926, -14552718611067072, -70214254228710176], [-55341242083431123388, 19559290516369776, 72093254506710792], [660227166313537700190, 145646695494414320, 6878340828038440], [-206924949359265618988, 28748610875735780, -242258386047418490], [28987882631058523062, -168004744598773008, 182816159759079592], [-211618205433513494630, -236882282227003878, 38457851420910815], [-879010419596065571414, 965610341359769814, 165666082717528681], [-364928256964627894682, 1091125978729576176, -299594495908620632], [-662796546187321986356, 1682658934191486112, 587987888231185104], [1370387057898956768622, 624026367499394400, 295691996145014800], [-4132333909252889488828, -685045466514936820, 523542284779539810], [-1881994001781788007926, 1425729522077490264, -1300481736347316316], [-418000272559677642122, 777085024820703240, -2792907067792202132], [-3981497332755156383080, -4778519838223114240, -538014828460662080], [4584175963313886907552, 7807098613433613180, -3302063363005215190], [271794615918299861438, -3839255632616287014, 9189433509997548063], [-18694679717311106403050, 12085378290944784206, 5658141596966753885], [30822390777354190140254, -1670072173763273408, -2003603644601880736], [1124828135229217170706, 6278054961756049800, 3296660801055742636], [-2881257862339626169768, 4673278192670182936, -9563906699184940988], [26674541366721597940362, 5518103080805246600, 1382294941975894700], [185192288861571598752, 46712010088490644500, 7712638541086032750], [72990722142245422506642, 14628407734142808432, 31532173135510394472], [-106626032347655212137650, 31517966183637608162, 3164317289999707595], [-42589738070438429898634, 154104568631561219968, 9525396772395240256], [-44740718967204947579628, -122620537209973261780, 26522523682826521490], [35259609357782207035550, 154347656605439252656, -2826224992558611640], [104279451433387666794962, 111794184257315668920, -74741393481462422860], [-169600688270395708870254, 68134997673522720970, 46351568533606638951], [-26323398913414791612386, -308831000029342893216, -41270504786203051376], [29307612335016167952582, -234997754198964678898, -1595343199871090563], [457251998347730776814332, 82037591922590540220, 157506073504867920490], [-234316997417269574377670, 221228715360805802352, 34877705629782449000], [-421627330700775196647954, 57468119648111685944, -313582047663358335884], [539312201275900903635592, 327605602753656068860, -283635068249662796630], [-345646111045805626574366, -346485800128786034992, 359420037041669076984], [-760168932661576514443242, 538221341638873214230, 12398340325903429673], [167811529507441661317990, -2183168434424176278880, -68441130402785874960], [-780491562199132598863598, -1008543262298846386992, 412322886136637932792], [-204109730537039727387112, -1363338267448719209376, -227689244780877362992], [1121994674916612468811826, 3349294611427468215334, -916448757681230907679], [-2294398791129118314430242, 793890200010105038096, -508916245188402874792], [-463469184163979171664916, -1474683550195576856968, -817191818126786015756], [854369368648292004828286, -1225897912793470725510, 304484688952815429391], [399438492537414064351262, -3058260494455950585024, 1025412001787909684192], [-3391965751492257500434882, 6652248859141613572760, 42473878924592085508], [1073890459874285191927282, -3253875432085289098560, -973865934204190039520], [-756000347307164128145582, -1961769534004824742536, 17872207510638600788], [1218597257842570688866476, 1323939271967299661648, -2837304744709284622184], [-4911979325535639382113778, -5304591800970419163280, -133174768595727652760], [-219756296868627102762748, -7165036624660866993060, 3810532719643525672730], [-3423212701110430592140214, 837762736779653492840, -1402188417861924644484], [-7064225046573393411928408, -5756244546302220366784, 2375241588466339034272], [937183897612181675941514, -6665745945980826729966, -280115133626112497101], [14310605433229496951982490, -1272260443788176670680, 2220372074616367574940], [-8118339483454606522979078, 238237222974933738272, -3701663471540461034448], [11671238339096283215240902, 8383393393752153294480, 4002241508418748443160], [18031779123065424642909950, -15010952457396518103638, 2500010173332790764695], [-1850935830255418687410114, -2806416900809750105386, 1179582608495579913481], [-9023998003746295897763336, -27556478821723298971128, 4480120999661143052524], [-6466666919504398294787206, 26431716256591487759238, -1701251402743076375631], [-11655087024851125600550768, -7279341789290729676940, -10064834655599549337730], [-17925162479805915229067660, 37760601026709440741720, 7243418008772419444740], [13465139817307243923070982, -27827435809318215875302, 18642666667587829594047], [18591592544696625400341798, -19652596901379478143296, -5420294555931555905632], [16402750521498150294511322, 40518008609001892440320, -4285315274338704466560], [-21215633330374968925960062, -21155370621045546019414, -430243542465183396937], [-63390108130346098572722858, 102716178032279935072240, 11532769183368326759080], [-36820867174259901619954682, -105516821736806054950690, 33585949292761566537333], [-71404624521563148148618138, 21533797338451212188592, -12242850070518256919608], [-63346128856269249287216830, 161453353281173332808322, -18058581706289844478885], [9269811098654916932976146, 126656318441484135542808, -15075300895306286523964], [-51294254857217947047509848, -200461635391838957221140, 8693388164454682988370], [-130370875467636728846230110, 72535166954272313287776, 5254408705516858627600], [190470704804013914454270814, 52712221025373637587198, 3232163039847009711589], [-188835092152059366144790126, -224788824103741214453536, 58864971868074009003984], [107926373336105511346977840, -221239359490867063787080, 48743997420692096273140], [27480485847765413947262002, 40406001166001244619800, 72632210383270642204100], [639742206771709485285722, -25018862969966980375378, -103503947304668095009523], [-190346900232687891001269690, 283399569525248198744792, 95146497791537243207780], [372026491172888074773677906, -147453741589932819742776, -22734951164695660231924], [-50319864439528043617376564, 120910493826401476930728, -105481299306029492632324], [76950259758721571057446932, 223503060844300730646980, 88039151106409710061910], [-189750618267078369919964678, -157141328223253589582520, -114182627099344364428340], [111706647596323225152336234, -611606880720060145662806, 38140732615501753811319], [448848418417392962643433178, -92096614634509603222010, 197548125383406305223793], [-546309944633024667667795058, -426417266607145641766072, 87039559469672773766732], [227719560822214021276061548, 440027830394930281561504, 117907233508386946117968], [-143507233299971374394720018, -14002504875910192461280, -275453712788235177383760], [174980646463010633484580298, 766119594187572885537016, -253519293188274589423852], [-225588111147599132026033530, 338028444611143209312216, 16432674725625341482980], [-475736647506459612041010654, -981267872671227759761430, 330429461348216606326551], [-598138209574305519091463928, 526925791473451978791380, 134945269378362616581710], [447671866779751475785697142, -366830813989021507167280, 221241132225555446629240], [238467881345396767128796878, -754959007814555216466256, -467252155182946124208952], [-210944145693748603780610184, 688170714095126218210968, -230768257952025741749244], [1423226110267111586211835730, -732467976021510196051554, 287598514561394398654965], [963777802625556836554720646, 1024591529855646563260320, 21009725430625505492976], [-1699883197291606648840407348, -654722539407928507010704, 21468549703418802018632], [298197364335512184459703894, -664566230779335117924326, -507684475308003012249921], [-233579432575366897120402708, -2421410879174865063691180, 269850408862711069254190], [301801271538753545600617262, 605113798920923006187392, -613327067905710999958208], [-1365953083333415121543974998, 348578451180394001143680, -660983682203870552885440], [-1190356971025154093504249934, 1400784026519808779495568, -85038234485896850269544], [202805890488769126763399158, 1084893523205220588561096, 1191768484388212398388108], [-943807510679363079907763426, -2064546635174558254994122, 34896465213914058823449], [-37110581066662325310622722, 2665996536524728702703240, -777353415571168523147732], [-2711493425661803866063840582, 1599206785895750160347264, -1329261737125970957858112], [-146642654266415863378658288, -2142140503330796880897740, 957671524887457370448670], [-1360189591804298667254627458, 4229119558895904022914480, -29580007648007738326840], [3779418426311357010245710638, 645219805138373158440786, 892816251037768447059763], [-6308807582485836480608109210, 478288861379835268690726, 742313744614079713690625], [1509565239896123120661315494, 654481771068437017215712, 690430963188563551300304], [2547928495557351624802738752, 5504738967943510288386296, -2928386950752095972701868], [7017568403268082777724442342, -7421302963208904083308472, -10695037477338569570868], [5820735772816399428133460602, 4077564477153385821256912, 786599627901987177943032], [2669418894937288388017766468, 5333628467004687890399264, 1257261358525401171559888], [3867426213990967540741771910, 1320781995734632991361842, 2090008944008877924754755], [3211713761833156970826446070, 11634580511814278009547616, 889558997313593717608080], [-3219868771336221849833928038, -10325264614092190029934280, 1736669845061319900682740], [823551234548186786383399826, -15142166140392028688042190, -920759623187833072632669], [-934233129536083333491713406, -8025928964330445424770162, -3242733666910256318927331], [3378036683635148403073985582, -10180467797978659498278098, -3588953264653897125596563], [8540453831765867187470130972, 12653039678750189011763140, -300601655035600968939370], [10794058556894167302474621536, -3797730639345253399898872, -2177389128764004158301524], [-332249069679329681876216646, -10884393790734858079509208, -1215448854176245001220836], [7377160476026384380085103234, -19865434597571012530716392, -5517689739822592292825916], [-1777134935058701169130192458, -16498499573638912661311360, 3259696162015859080482880], [-16097587219939700269645871482, 13786078060090061085628110, 3424664656411965273141533], [-3742010609864148008291996598, 2619060814376100769554208, 6126080464359926087327792], [-9097578048812752286358672094, 19910241576262263536127774, 8393333468532850250539701], [-12664898954321639639595469288, -23300348504348573935637460, -2102319685704044999637070], [8506145793164814649289956730, 27583141389282292427764696, 2326332030155648010810340], [36922141208828595450477988686, -3691313806677871354816110, -3303279193743244142585709], [4716779323935867090536387772, 3948463883214989469847380, -12958914457391506324206290], [10336189629909033107659532998, 7658128198787530217320600, 1627073576234973428285188]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_5_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_5_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_5_20_a_a(:prec:=3)
    chi := MakeCharacter_5_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_5_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_5_20_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_5_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,20,sign)));
    Vf := Kernel([<2,R![-578651136, -757856, 1006, 1]>],Snew);
    return Vf;
end function;