
// Make newform 336.8.a.p in Magma, downloaded from the LMFDB on 09 June 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_336_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_336_8_a_p();".
// 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_336_8_a_p();".
// 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 := [-67, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0], [0, 16]];
    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_336_a();"
function MakeCharacter_336_a()
    N := 336;
    order := 1;
    char_gens := [127, 85, 113, 241];
    v := [1, 1, 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_336_a_Hecke(Kf)
    return MakeCharacter_336_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]];
    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 := 8;
    raw_aps := [[0, 0], [27, 0], [-12, 1], [343, 0], [-1062, 35], [-542, -36], [-14628, -171], [12908, -138], [-34158, -3], [105654, -758], [-217920, 474], [-14214, 252], [374880, -865], [-198548, 2544], [-420084, -5094], [-123342, 4032], [-1099752, -9522], [-975554, 5106], [-766024, 26646], [-1012002, 16995], [-854514, -25914], [-524084, 39714], [2447148, -27588], [-30432, 20059], [-13023426, 17898], [1169808, -10813], [3654256, -112398], [-2854230, 41555], [1683990, 90576], [12702570, -58808], [2539596, -126402], [-34223724, 119708], [-19139442, 290202], [-13683084, 272220], [-5863950, -388632], [-49317896, -26796], [2182630, 557106], [15742160, 363378], [-7086492, -7630], [39207504, 53807], [-105071610, -149915], [-56338286, -163428], [-63214770, 338471], [-46361482, -789228], [-131761278, 483112], [36172848, -1071900], [-47685172, 168912], [11899208, 1628496], [22707072, 1512966], [-7293310, -1745376], [-107452386, -1732902], [57077562, 696021], [-265178002, -1109202], [46469448, -1022134], [-32340600, 1010171], [126455874, -683467], [177148956, -2174391], [-86872552, -1993338], [279177250, 1180080], [65453478, 993866], [-137037964, 2593506], [-125943744, 3248951], [425731220, -394026], [873807012, 701754], [622138586, -1266888], [743054490, 1405404], [-472870604, -3864336], [-216166310, 2023332], [-210026706, 2557477], [-528454514, -1924398], [407943660, 1909045], [942410958, 1753027], [1336605360, 1480704], [636051422, 3422280], [23491476, -12349896], [3379848, 5427852], [594162558, 5234226], [-699455706, 5624130], [1065165774, 785334], [409858646, -4439394], [-530331264, -8249190], [-51461038, 2209776], [683102430, -10812657], [-1093263110, -14662812], [666883368, -12143508], [1899423438, -5026971], [2437435314, -716304], [-577959302, -6423312], [2396921544, 6323591], [1657074500, 7230498], [-945254568, 2506686], [250270116, -24117398], [-484448080, 2577552], [-257940642, 30518605], [44557052, -16824060], [2718589608, 12769428], [2877902220, 7771397], [2276798028, 9323817], [-2546339692, 6534840], [-402245782, -7173648], [-6090623848, -6701046], [-3165532638, -14140532], [994055976, -23744422], [-1606907058, 24850414], [76365352, -39306018], [797795674, 29896524], [-5932992864, -6308902], [-4420257492, -8372079], [5487100278, -5138889], [1489253730, -14030196], [296727616, -8705148], [-5126184530, -30608964], [1950789174, -51564078], [-8655367684, 5637648], [-1984778444, 17443110], [-6057007818, 41980566], [719590108, 44959686], [-360432852, 51524590], [-5691606018, 2891118], [-3743724822, -71847945], [-5433207746, -14342094], [-451973090, 31132620], [6449673288, 8422023], [5994086406, 38603961], [-6968717324, 38573436], [771268422, 61944234], [9615077646, 27774216], [-6853794816, 78207336], [2728652944, -11767734], [-13043724030, 9428580], [-12480146016, 36035994], [-1879138962, -46830429], [6316341632, -42177552], [10227989342, 34552920], [770429184, -32039877], [1379209370, 134007132], [-162787752, -164733265], [-8754292156, -62858928], [8412352896, 28633879], [-9996190470, 72777372], [4555956844, 28358964], [4291041306, 112606964], [18389785548, 67196502], [10025979978, 47714019], [4512864410, -82888836], [17040713292, 81172586], [11598681878, 111689802], [-14633415888, 26932459], [11450632252, -8805822], [21804319314, -82537239], [16426111366, -73279620], [10575075012, -21202443], [-1484101316, -68171916], [-6102559596, -85054478], [-7615890308, -228176592], [2148394350, -41811705], [24378190096, -111514992], [25513566480, 71083739], [6236008818, 212548104], [-16483605684, 150105005], [-4729904046, 209316787], [-8490263430, 207655784], [34119555764, -8588178], [-47750228652, -57994116], [8186494782, -327948570], [-6663761712, -16528200], [-45719045768, -57585600], [-4083629906, 160264194], [-2529872786, -105879588], [-18570646656, -107857489], [-3197175438, 106951671], [7275176550, -240188574], [6327688038, -75059697], [20790056726, 108343704], [6651785368, 262819716], [5772646788, -270369727], [-11995063408, 144911754], [-32659690290, 151229458], [50518435032, -96143982], [3309827158, -240333678], [-32657342108, 268158582], [16683006564, 190562316], [-32478537934, -90421104], [17603569644, 351291149], [-46828805310, -153036159], [-66487585608, 26205179], [-24251629730, 277817856], [-24975801316, -152173896], [-3664770266, 29276448], [-63702529944, -144478600], [-42231983378, 90880110], [-12866933910, 287313123], [26311515364, -210082356], [-46954158168, -113551809], [6740072874, 176377871], [-82476687324, 178357641], [18637530302, 33684180], [37179555402, -38176992], [25012085664, -548438133], [-71837202180, 288936990], [-71802489270, -193919812], [4729429480, -833610384], [-14242841478, 490885536], [-27629670290, -164908278], [-3699885684, -589909604], [-17273220288, -660808213], [16054617480, -157220304], [78242084370, -468589509], [9701825130, -720162804], [1693724060, 175264410], [123089465058, 105101016], [51763138044, 471501033], [-39694044400, -329871960], [-22510096908, 558752864], [9498081156, 243217294], [-37732687262, -519655032], [-19799393096, -168822048], [29020452852, 262911085], [6137230290, -134932171], [-15918594426, 599494242], [-54238978, 999864528], [26524905296, 1029152346], [66882434922, 524559884], [73090102672, 305057112], [46089729780, 211988008], [123678380042, 68880060], [-19804964544, -421200017], [46934612826, -1075791803], [-31882066064, -158259690], [60675914598, 621915201], [-89025453002, -799956120], [118515711716, 776060670], [39967983544, -22277712], [-18390777558, -737885672], [98627231052, 133994574], [-124241733120, 480306576], [-102082365410, -599454654], [115946653914, -607896724], [70621204662, -456803703], [-70773607320, 114166828], [161432791278, 41566257], [-105692196996, 308547684], [-7774979856, -1923498312], [87865602826, 646691952], [115925571168, 1030656471], [136458874692, -1015093882], [-25215653296, 1338361194], [-21520990560, 878657014], [191866754504, 265976334], [9874635282, -229773039], [172522691678, 593049816], [143586169788, -1292888511], [-135076383642, 11434463], [13291612510, 520120494], [-137061715344, 1159975687], [207258863898, 68246623], [81250908626, -1174869096], [-91863814740, -546946230], [-87738191160, 864804459], [33494066882, 1938384720], [-101021528948, 831622986], [135732403470, -85395431], [176012582786, -1240842528], [-13832944306, -1952140374], [160261109220, -1083938299], [-294658334684, -197339616], [-47132892942, -729195228], [-153585979812, 815249545], [-76361303200, -797492406], [-52327326114, -616605350], [-146313720530, 1914629046], [52965655060, 373086180], [-157054134626, -206931618], [-79262039672, 1014790044], [-84527098490, 469017546], [66416142352, -291780606], [192607156098, -341228929], [-111653944566, 2265056208], [-329807002754, -40626948], [41565134352, 711419494], [-53850325608, 880946944], [-235902006108, 141030318], [166713644880, -2014751240], [-14046438914, -407293494], [-160492177964, 1051515528], [-7572112962, -1032461721], [99067825522, -867972888], [-196961262438, 309755064], [-85914684944, 100298478], [178135150776, -1776779757], [127823377914, 58911700], [56539801956, -2183575348], [251747389926, 969696810], [27893378784, 81551922], [-346263877130, 867938256], [-329556599424, 2120540191], [179225355080, 1619251212], [-232231309716, -1540273315], [30306637368, -577458698], [115870087372, -4111289316], [35328369210, -3110285532], [-86436687930, 2275894182], [33986780948, 44867874], [-35585431638, -1201225825], [-376448899512, -338097570], [-153110867334, 2983260924], [-262048165578, 67102185], [18983579034, -1743584976], [-111858718194, -1175947333], [-197399778110, -975386232], [129265991064, 2354285712], [258805534518, 800815034], [494794165902, 1262317578], [-281864073396, 62413284], [343683668274, 2348493013], [-567046755154, 150385806], [-638916620556, 754765992], [124634776362, 1003035405], [-235983198026, 4276896222], [293847042582, 1915694886], [65876896684, 468183042], [-350548050746, 3390883596], [-671404624968, 256242063], [173496223624, -3058395672], [649634139762, 439825188], [363183066502, -972066798], [140840136152, -3599359242], [-237348527372, -2166429834], [-283814191962, -3885978389], [590782505406, -715458282], [-588557183714, 2388103296], [-435378180978, -4046052282], [456887058952, -3928017762], [-149017773408, -2667194454], [486020235008, 2284243686], [-124687224936, 2478481322], [103974247574, -4089144540], [779263402056, -2298740661], [595329175526, 154783074], [182330480264, 578574750], [289269528766, -1180173168], [-762752609850, -2679559438], [-920833237860, 1289696829], [290588792240, 5221195644], [-554771772858, 1401339782], [55211075946, 3033493463], [417462907306, 2169395592], [-338904823684, 5918448264], [5996507592, -1941677748], [643819708752, 4493082467], [-203385269884, -616137270], [333580742838, -2868622740], [245075219898, 3576913012], [780264583320, -2550420576], [482609951422, -3397139664], [-454877779740, -2109924847], [513510577296, -132731400], [531475052388, -3075568476], [516748660890, -3242852488], [-104770081770, -9404318577], [173620721790, -2835793680], [452951340216, 5963217187], [227384663982, 4499270031], [-139777402638, -408689345], [4796334916, -4542681540], [-735311629754, -2540710092], [-571301580636, -5023456075], [498022947524, 2298745410], [-1112307244722, 978670764], [262445542878, -6963341103], [653546911644, -2522433402], [-167060962266, 4869664203], [-32654626662, -4717290756], [98633054320, 2559395790], [-249946971542, -5555922432], [-433080991212, 4532167916], [-230979650934, 4687031885], [743250355994, -1046897604], [13052298252, -7050739551], [-642187203906, 3873735438], [-446722458216, -2777566429], [-321992331126, 3623920788], [219928271072, 6361997220], [-274488272838, 1171097132], [-643943220468, 1999055328], [-479243942592, -8928057464], [-138087781616, -1653581364], [1014969517258, 2758636356], [-870089577728, -7031642022], [-1294899967452, 572796378], [-1280630172098, -3675839904], [-478854874860, -7428708279], [224737757850, 4355603787], [538626958228, 5129483742], [545302118094, 4807802947], [-495662227886, -6444966588], [330440061944, 2508149484], [-1155338459244, -1786973607], [-202095255024, -7871741718], [-852736723866, -5756465490], [-78189579010, 1543574598], [494507888826, -875134408], [-1356862528804, -6011032098], [628484283504, 5191585383], [-173503471932, -3127858719], [501426677000, -10644680976], [-638619274086, 5822211936], [574066823250, -6089850484], [-3811043012, -5300095284], [-389619832272, 10173599730], [-200493502558, 3900240060], [640048168206, 10102582610], [-922180031850, -108582863], [-49280156860, -13902234792], [-541731413226, -4507669632], [-178316375196, -1732312491], [-190994416014, -10260539583], [1003439642680, -1673300922], [695197806372, 7659899109], [-1032710474940, -4207960194], [-829772661738, -9417967530], [-162438532802, -4875340290], [-1270784287170, 10567216591], [-1544778933636, -3454603052], [413303859530, -7895952012], [90132548496, 1640286963], [-740580984114, -1954180327], [-110129702298, -1962088026], [-4846338108, 16024293360], [1015478812116, 12507901386]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_336_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_336_8_a_p();".
// 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_336_8_a_p(:prec:=2)
    chi := MakeCharacter_336_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 8));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(2999) - 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_336_8_a_p();".
// 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_336_8_a_p( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_336_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,8,sign)));
    Vf := Kernel([<5,R![-17008, 24, 1]>],Snew);
    return Vf;
end function;