
// Make newform 7.20.a.a in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_7_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_7_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_7_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 := [14876898628, -30288715, -330513, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 2, 0, 0], [37566932, -190203, -366, 1], [-330444, -278, 2, 0]];
    Rf_basisdens := [1, 1, 336, 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_7_a();"
function MakeCharacter_7_a()
    N := 7;
    order := 1;
    char_gens := [3];
    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_7_a_Hecke(Kf)
    return MakeCharacter_7_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 := [[-86, 1, 0, 0], [-7378, -8, -1, 0], [-621336, -596, 37, -19], [40353607, 0, 0, 0], [-1304964084, -6624916, -106078, -14837], [-6016350644, -2146180, 999999, 146317], [-280765706954, 183765080, -871550, 339722], [-422285933238, 43223968, -11456667, 5265926], [-5084223856956, -2848483748, 339037216, 13736027], [4441719907250, 15246132984, 1263405598, -483646822], [109639019254916, 34257192072, 2623875498, 432209046], [92503722469346, 524751609696, -18330792858, 2354384892], [-880934867101298, 2056557099552, 11590933214, -7123192004], [-1116002143328516, 350928515108, 30328947558, 10243806361], [-3167254054015484, -3928744805216, -246666282882, -29719020612], [3890153977883494, 16954771880120, -543383823248, 37478263382], [-32163791676828790, -33285178938280, 553195797405, -298678774164], [-24900900465722272, -124783511562852, 698140817397, 84194837529], [-139979901041512192, 85104518022084, 1567111855680, -517738334979], [-107865764952189912, -191337152058168, -11140377101700, -121702205550], [-35430235156126206, -41926451379680, -13390174429668, 2412586324208], [-68974841162370040, -1349758981225248, 24224630191524, -1119661089456], [-562803756520580110, 2484539719755696, -211558160135, -1306324513294], [-1777276676641770958, 1173222089790136, 53331319663064, -4178029139162], [-54612291060150194, -9554838219163216, 66745224111258, 12652066373464], [-330342845653049712, 599854654569764, -133085933534611, -5607141432893], [8258089399425154868, -1700482156130808, -84026293226370, -38320338541266], [-6153146895378669808, 12116633133246868, 445294587579212, -21846219190967], [-1671156873833063758, -21606627405601584, -460566963113166, -64642642201872], [7817130647935861930, 2073004742354580, -831994282747642, -29659379850923], [42507286035949381156, 76636274396218844, 20520493229628, -228768438324605], [146293799478944131770, 11251334968927552, 1276229212209877, -72827740079122], [-59911805927660728926, 23187731407680848, -1597283196093676, -73845067223492], [142976073132407283046, 203709093283456688, -2501208580047093, -456789817738658], [-427693014182269644474, -330772837387882352, -6309426064344788, 441508749897116], [-378452837734004111404, 363204189340881108, -2199723598947420, 1569639647687961], [-91117929042682416132, 252665129719331116, 28171778254794999, 2936934800896025], [-177337464996275030764, -548660406226435068, -32670437352856002, 2564683641051105], [-377714481766471263212, 232106111526281568, -8076405025808182, 304612334906356], [137802611815639973380, 1015469597310851980, -9610256311021077, -196445769311991], [-196580807033162581352, 661456364281807364, 34893979918090636, 13158152366494037], [-296012206958240103412, -3160167216296166348, 23930362280967975, -2497576157991309], [-3516809867303154698816, 1316038515770262136, -70675010006679876, -5710379000118738], [1531676477922160943258, -1896840916709205732, -82036960821891558, 3892671327384807], [-3789137651413373363346, 8175974775651410376, -12371619210969060, 26115770731059666], [4137694418717809133996, -4498007409904005432, 93705282128295522, 3919592376101430], [4081918863671752156672, -4521763200966774940, -51840641335757400, -36453928142390795], [-2756926499534745644224, 19729457611774314696, 344735805743789796, -16105145493429630], [7401053681899537626138, 10908066997877944928, 196939171352127413, -9233382375715754], [-18918973880246398977844, -2102743966067947956, -332154509417693541, 51105926590471641], [-3657229188109895795350, -32670996988989863552, -133788056385912376, -319247019300272], [-1472192026278836020988, -25418011560551779836, 76371604442472920, 132111771599461261], [-17865604123298742697762, -21639763685019175968, -288616496249140830, -99496716184126428], [32182909205345110572726, 18211020751376034816, -126764905192037469, 305879678941939506], [-11030011149261553315998, 44609714616985518544, -1346317868071461416, -260333246272090252], [24098421224154153505128, -17450446813433375032, -1753175157916809508, -236296382221773038], [-17669914469326207173740, -198008344905590194948, -355725922543091381, 136287669474122933], [9300384293495572990520, -51347595157529281608, -2850104264248709928, 459726409533115974], [-177775634282875826336962, 95849093152482960, -936456527843036928, 444328459541588436], [-9039316661050765719862, 111580325861453364768, -1467196902900781808, -723331436616658552], [-22400778198671947584694, -202408623220483080888, 112946750217066933, 420223859837101896], [184959084805737563909896, 109608504767730239124, 2775150599600912057, 652796359617199735], [167237841629947365090538, 342123135296947746944, 271872067538120805, -1452825018219472034], [181510743414547769006696, -171862745817754382704, -10776541522779805704, -518693636473476060], [5782977615135014882958, -61559647697688407312, 641769569902108062, 214172901862368800], [-297079475701119619790570, -186113673402274200920, 13271314191627837324, 94849180885093578], [425238451811367168697500, -47727573548673494012, 15810487051281248214, 2083517050758487313], [-337968024705720503483782, 2602329999909125436, 7768749437904525714, 440380197728437599], [740457742133195960866988, -569417258121963069772, 20023677098898796638, 2495165983033531581], [255596677358583595454716, 1036002001226507596004, -16906585183590956301, 508763645537844919], [-611945751625549153507638, -569013117277386063544, 11925640980361402832, -361075702552504598], [-348019005598711871961988, 139300857380472029644, 4168429243131262340, -7023904283873311841], [701367146852744871908160, -116908538997732368600, 10556490198042366444, -4610151785309807446], [-1661046636716097375061050, 1417631480053345164400, -34068552488992771092, 2613027303271302068], [107824268225807862375124, 3535097083613421166484, 43291328126151821562, -4243743161831978819], [-3362632850065316954226684, 1413513090071355531952, -22090599252164664002, 9458160603004471088], [1603031432018184483577506, 2149902105442400956112, 32848907270779338830, -10584516137174820008], [-1949654020625630023790836, 197178400111006649796, 55945309784094678915, -8964611058685001361], [82802975853336352344498, 1797292654007847003652, -69828248531945915270, 20336678544945924665], [3333921399604696871444462, 2165105113514599054584, 97388551175665551570, -9046738353915766254], [1924578622432344986608726, 3351388749596539774680, 62348089207587715931, -21097130586669264128], [1298513206025925518989070, -3452249521718823955392, 173432935152576837864, 41207896147565463552], [-3555159435858646029756644, 1725068603205524896652, -209462569914432903152, 25777800020501340791], [1251909817897394626516558, 7001916425949690781064, 244019348333335200654, 13935798245651485870], [6120360941249661755723712, 5468712559713024421240, -338963861800985064768, -11971499894423744410], [-9970274919360359325348896, -688271722871481414740, 303671051646007019352, 9402390521896288575], [15152077345076812900685202, -6964621723520133241440, 72596954705352246792, -35301305591311451400], [-3827410215765857318693038, 7845349358200830903996, -369125236855490340582, 59038152110641117551], [12218504917974209694461348, -13079341570580973852876, -435052755827375669921, -73456169764829848765], [-10796818581539827426894648, 13324301820336242548728, -41628782266022073024, -59610725759775520026], [4527390204826630016353586, 5310694438944052087408, -333330362673537922999, 18845191188846845890], [821206879212814168580532, 1277419623971285882552, 140061425294183083466, 98075515895901222898], [-13101045378025486495453068, -15178480236307316506244, 157509869235656919408, 53954791695186399203], [-21311637606720679588037216, -11132629636460961064692, 1434000121706333294288, 67021883162109849223], [-15516717640651452230851672, -22292105065370593281060, 932265652999254422688, -175645178666594145237], [-8066833643564206622129112, -36542674425485862435096, -444555454766598349356, -172620293412387663798], [32075016471471861068891436, -11109282604197170796812, -714967436222493592493, -61336794141731869333], [4140397509827947028854918, -1898602784887754120632, 389569892402050274662, -370637246693813039602], [-72697488797535395381637406, -9219355646155274458872, 10230279928174963953, 158963180850137190240], [19689117837253947790358190, -111425659482164496934064, 365410756657456850340, 7346771739376042460], [-71755057259427992707132772, 31550906818128882035732, 1428921452121926332626, -61151032333359834611], [33792916991107843717896502, 160559958216435892520648, 73711953010638247732, -47508888777535211614], [3776203515862159327845182, 53784153500033708505992, -3432438258217964193801, 152983195021396918308], [25769703271173932231907418, -34947770293248700217468, 1525132186872645367842, 20164577539461564297], [121316194733808515732115348, -98535256745387243147516, 757635931487272264434, 66634377461311101113], [119537529688463208583664618, -195260938824424919779848, -86475671123928553944, 341974741492246018998], [4980611276303276218033310, -151975177357337943297024, -6053838711370001064569, -269602507478259177334], [-155776861495188239179848550, -82421324931583771769840, 8856976945850686215908, 446051575252570571020], [-28249792431121998996737560, 168164967793469233354640, 7801163200221974060520, 913125161192001617220], [127534433760065190585101218, 20722197114802249022312, -3815651976827734660680, 175139567425166652514], [-82483199538780450899939136, 242216221999021767987424, -1607047366978999461024, -505267347301897388200], [7406226447837022654747370, -106481130009307742310024, -1649083670855392864050, 958218734091532474074], [-380407956106203436906337342, -40683962758000572700932, -2413741988995343322862, -697313139208057881329], [52131804118933334528434538, -215409347294320987457064, 1477597093533114206085, -1273219253686510250628], [71102308932473506577665408, 327849726966294315750560, 11131553555125104019716, -337120502723866300832], [-272401371433146581638078062, 204567610698576094268132, -8762768687406791365630, 882496764725349264577], [331346907465789334462505374, 426081378306877413236504, 10110547030855707023175, -184313249130273589160], [150541880593137616381974316, 134378761338426339451112, 12684829262821713595954, 1815434724711706326206], [-417463955385706124241334374, 283885636216244776913464, 4034629694057804355682, -1979524317336750878590], [61448180153448913711771916, -181039940206268646756444, 12833040571669933795978, -419632699701367875535], [-162463132343590997420399160, -149761891621144686338252, -2739812203590781349871, -792861491105709235633], [-263505457040868326379096886, -362267869892812630357440, -20477616609944541337668, 81476583323830076856], [-61317270721503100242778724, -1103536985990020929370652, 2225499659935975555071, -647024920186635454209], [376686360427199483290248328, 381024251558414404387268, -5830909615399665628328, -206845923164995440211], [26842918197977657138633450, 615938346203882186664528, -27784429972392597705171, -3699367731120802484238], [-314784710843281126322466726, -517473946291529493021960, -8103285525813393088278, 340205625046262784546], [141312326070663881628723842, 70666226398953706197552, 38239379848589470543566, -1320917870345834137776], [-162455620160868517768020820, -233415094678869041630480, -12312910776553974930670, 2679155270378536075000], [-53485102804546820366256404, -966746577416483477396800, 5920383706099473588390, -3573429249426486354476], [-645516480673115692276085680, 1817465731585204821424668, 34195074950196734105709, 6078837362526552590217], [-392962120884432961913515180, -994486214159303305786044, -33640946973520384637538, -5629111856361604872399], [-415734064364463618171986180, 1079503338387883293313212, -693271124382663399748, -2947899327100310464133], [694231810097763080459166444, 2431332174812087895890044, -23305399950983693024160, 2229909023515188730595], [-1306923676292055329102987270, 1309049585748371216904488, -24729895858495600956330, -12497494103242832320442], [-1423930740601071956292599130, 882213016484440230833344, -3186235037432984099414, 5724104991695012705612], [-1137582042801699082303249594, 533121088753560682727808, 57952310355887204426022, 2651101073343095549124], [147076484678216445197047984, -1092606290220398563810036, -77629004853457895930975, -10765644938186749522483], [384398745872135927820835010, 3127928762765190566009232, -49908085535372318440047, -9992476813178648530854], [-2005664129271038714161384508, 3851634484100631354310764, 25994097287227154371139, -5437146924904020450191], [-4180958638445037963490126910, -870969577394672924681860, 1510354103076737763250, -6787073785780821951121], [-915876512854309914650058910, 2440123500706539445292808, -96935156760435702771951, -7802470363143974186208], [677634534680193160620533422, -3625452156202276983141488, 36692439521616997937784, 9155756309829685187940], [-34889877290284135018361008, -621467359349067007841928, 151292282912196157126584, 10108415999810871011766], [-1198121137179650135706329136, -401327178101174408064828, 81135893000819012160252, 6541723812094979806725], [-916288655874117341557323408, -2587002293349728831716964, 38885329730028770453709, 10373794898398156292201], [-7503466496155012420308279580, -2742381824076277871501976, -79552324956590887269722, -1657565420819525704426], [6785340178662975513386293532, -574542437657184930828564, -105141502281403425943413, 16549341992406419557761], [3207234852406994345237578686, 3137595151039269076261448, -7314781706919894140494, 20878415556501453798166], [4964591686701815807778701222, -1176434648363575816883280, 22455890278250857419987, 40349388166695096600654], [-1374855428856302545809008032, -2089918715479234781868824, -95783516687235920954572, -12090587345980848795734], [2043351579839226617832907946, -1031784776935632870021072, 34187634603619751734326, 6734783011130516915136], [-5076059026678896955828769598, -4488275234476989194670984, 277442663852999367629172, -12215666173767732545058], [3956131523425314075713421872, -1418846777072072536575444, 277561246927852664867412, -5452759409919369243609], [-4084044135913383425966801796, 2692660195111732586603680, -130984757955701333540162, -67059219191215674141892], [-1525792545985804331588255032, 7629333301282775015758044, 158585565592238680993860, 41968624095690605445219], [623484847633916625486887036, 9819863028093341980666692, -123119436682459241183012, 45740483915023759478837], [-4104031328839671191217858928, -1073216942817198002955240, -306752839811410785764328, -57058981241149527130434], [2115877747296115359386564806, -2193397793634598154439416, 371408881044304684858314, 32757888707908464442614], [-8719433235327504024388507214, -2706566213290657698883984, -162329950871729841977028, 12230633290765890155188], [13075436419843230189790608632, 1040300700243886522430756, -511799118418923230357883, -45818819259549980619501], [13631567204011265815991268988, -20769418978030784007314780, 82800586505559726430242, -30087861135186241617279], [-9488759986818654347974343190, -10009277986168384032602256, 318722950077338317104216, -54865757214594773694468], [-6172390976735158567145184428, -2823727090625096289032884, 62183401942607823939768, -105521043511973080759433], [-3716524999712665449540718966, 21355516944049050793119832, -327797835778256975707819, 54692589575977930106428], [1733516162654984961386392682, 19521228193910906587970240, -158520409840675348834284, 105081065567911555799400], [12353545547322754386393676692, -16669413066563600405786200, 188035147370874107902310, 2900903649942706857190], [883142475505658605801627088, 10433980886063862057956544, -405936279015975007805736, 17570043824823989417760], [-10268752485390620961479866096, -22037917362103493891099652, 770894727307106986908729, -89772652897034125505559]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_7_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_7_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_7_20_a_a(:prec:=4)
    chi := MakeCharacter_7_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_7_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_7_20_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_7_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,20,sign)));
    Vf := Kernel([<2,R![207467274240, -467202816, -1278192, 342, 1]>],Snew);
    return Vf;
end function;