
// Make newform 304.8.a.d in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_304_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_304_8_a_d();".
// 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_304_8_a_d();".
// 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 := [-158, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    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_304_a();"
function MakeCharacter_304_a()
    N := 304;
    order := 1;
    char_gens := [191, 229, 97];
    v := [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_304_a_Hecke(Kf)
    return MakeCharacter_304_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 := 8;
    raw_aps := [[0, 0], [36, -3], [93, -31], [1133, -28], [1723, -151], [-6938, 449], [-16023, -210], [6859, 0], [43362, -4199], [-4788, -3173], [-132756, 6568], [-70326, -8608], [143726, 51678], [62579, -41289], [-725295, -20435], [-457730, -30183], [427288, 114433], [-791651, 76547], [868450, 111319], [1562970, 291244], [-1151887, -196048], [-1422876, 208826], [4970568, 118218], [-1981580, 457000], [3338292, -783058], [1525126, 98700], [-6127622, -132656], [-4108928, 267383], [-2890458, 621003], [1054590, -354736], [32226974, -174352], [3764781, 332433], [-6315279, 1584970], [-4400657, 3222175], [14358691, 4488559], [20991490, 1560744], [-1727554, 2379412], [44993500, -4131024], [96571766, -1139086], [119113390, 1076272], [-1818178, 3892214], [41855362, 3007896], [24513905, -7537244], [-44488632, -4015662], [25513206, -5274586], [71651689, -8457108], [95536324, -4358639], [96937826, 8139556], [-115027406, -8403549], [-98583769, -10726383], [48962553, 20952771], [142158997, -6907088], [77294098, 2953578], [-217457451, 19712431], [485240, 20426692], [-456648387, -4291311], [149300212, 32607034], [-292990878, -17725703], [-342818525, -9312225], [-367345606, 14672802], [-50363949, -7231335], [-100863510, -24745017], [-376044252, -3936168], [387047195, -16573082], [134427844, -41023069], [324832112, -39409183], [433974534, -1679443], [-845829944, 11204964], [306139717, 47687907], [355717699, -61124535], [-456074904, -79344453], [339891657, -12382098], [948763208, 32599164], [73278042, 113547355], [-814198724, -5378603], [-804771136, -49004126], [-327607115, -53061905], [1079472383, -68764545], [-421115652, 70933036], [703797356, 34036060], [-1169614484, -65236616], [2602524154, -3631231], [-1590445108, 8967242], [788861434, -131661952], [-201638866, 164051002], [2384785253, -2206721], [-1193439620, -48953962], [-3514182441, -19315610], [-235726441, 270749191], [3761226565, -7490035], [823051045, 107317563], [-1953646636, 39646552], [2773024390, -50199952], [-1110645404, -226335136], [-3904988391, -1942343], [-423966290, 162408973], [-1879755588, 123611022], [1869931696, -285925428], [518875184, 380414587], [1462014125, 421789983], [5807126460, 5463784], [-457638773, -120331585], [1299569998, -227923334], [-455451960, -315144200], [584770772, 398129992], [-5142102701, 106667576], [4244404225, 110669521], [432170846, 78272940], [4777947396, 110048460], [1691828110, 634257858], [3717599740, -527217594], [342602215, -74739327], [-331414093, 21906437], [2497808448, -116109002], [-1252364897, 281197443], [5146030540, -322857818], [-9076051533, 169170697], [1794422153, -175601364], [-3541326729, 594549651], [5963435900, 572792103], [-191529282, 958987043], [2027970674, -271744890], [4791405592, -485143447], [1708468684, 115428480], [3128435113, -939500549], [2556696186, -805357402], [2014216714, 455351740], [-8089128619, -148105892], [10235645867, 482002038], [10737404446, -70144130], [-10925979753, 421486859], [566255032, 588298292], [1480549856, 990279616], [-1850563163, -494074737], [-19368518827, -22556422], [-160546005, 984021422], [1024058884, -298186511], [-5666669342, 3710771], [-8375671154, 258905317], [22469027491, 271303306], [-3909191806, -741155179], [17540889655, -913080581], [-23107844655, -401616502], [10317174778, 695084483], [8505752366, 310738883], [-3279086182, -1244244246], [-4812945838, 1241047232], [192298230, -1500977670], [-18966535881, 625578961], [9634724630, -671992348], [11083200444, 816003725], [13767959871, 614508093], [10572925823, -980871087], [-9302989522, 2028333700], [-3016008254, -1547404443], [8033747618, 2618077250], [4443539690, -2441423585], [-7627452396, -1273258633], [-32870462631, -191158760], [-41200687196, 395665895], [26587469644, -343623020], [4288562244, -508622362], [-19671191856, -553424496], [29161748132, -1868982096], [25726032040, -594403116], [-10151182680, -1054591854], [18851592966, -2266648118], [-3800397409, -2423054453], [-5603938228, 2945374120], [22890312799, -289237541], [-3390957208, 1248991391], [32775689202, -164088755], [-26915596768, 3224622320], [-6170825164, -1468489143], [-2929459748, 3096511108], [32009579769, 2131507455], [11290975004, 3439355428], [-19315064613, 2057532705], [-343427960, 3510515072], [-29774916335, 1319970221], [43673304423, -2232530756], [17279175490, 2568673089], [-24824391490, 747564124], [68350229850, -52744550], [-22832039219, 4330128832], [5272321291, -3815381219], [-22430036000, -2700793387], [-9346591966, 2760584658], [28596329010, 127540828], [-2465099658, 1165504557], [-4744553770, 2008254376], [-16394437205, 581071283], [-59929527664, -17381064], [51276504872, -4038507235], [-46204884600, -966054820], [-46695802492, -1929135112], [-50298127139, 1154188292], [16627308686, 2892796658], [57077198792, 3047028721], [-14314534929, 1949128524], [109331622440, 486825194], [55691360182, -1234904386], [-42649506190, -1930121243], [-57365283094, -2561674866], [-50311025480, -2797158454], [31907681155, -2238206145], [50004658927, -5462717034], [-54001169354, -3883671785], [83159566657, -437251268], [-14372759692, -1356547199], [52648626504, 3607062559], [-16083345322, -2177627762], [3093307547, 6550528691], [-8156001874, -647746447], [135368803862, 604219874], [11204074774, -1608411548], [70676622074, 5818473725], [-28817877896, 9204593074], [-29918149952, -8201746542], [41601215445, -10380629529], [-76648181458, 1218174080], [-3116935118, -8822167080], [-95980089718, -3272561720], [-116846195561, 5403174450], [98397351072, -2321654141], [11028139461, 11941113915], [-63167872406, 596081544], [48767082548, -9844083358], [-39959962518, -5703000904], [-58448663837, 1126987321], [-17928947431, -5646631465], [-37324896142, -11434678721], [-71796217812, -4014202788], [-112889910894, 6571031752], [-108800761655, -9236613465], [-3294806611, 6078547454], [-77427304197, 6816588225], [41747257605, -8299533991], [94779032132, 9248206254], [6796965588, -1264105368], [155840643898, 7334576694], [81504648153, 9651812239], [-2167760770, 2088469371], [522521468, -4308179121], [-112239491778, 7475422098], [123855301929, -6909511863], [-78675777791, 8293600877], [-71499075200, -14370242524], [187729145080, 1493232852], [20009494571, -12806669014], [-73973237278, 15983209900], [-78121038325, 8523291424], [126069144966, 9429118033], [-49866831596, 2241003158], [62261819489, -6762978215], [-12377972248, -4582795934], [77055878327, -1124843983], [156287554418, 12250904302], [69765779190, -3860008488], [-138982585262, -768102084], [-31241542918, 12107872158], [-48945647654, -840233954], [-22529145763, -18694692031], [100004657200, -17606413889], [67530223445, 4211584103], [26138758878, -881851553], [-21345172478, -11851868897], [118672208903, 15206775884], [21328941412, 1678016919], [172174065161, 6261441419], [-233034832182, -7580328219], [10635162360, -13785997361], [31013419953, -1693280160], [-122665876823, 5296574694], [-14097584236, -9792137178], [247111384630, -6259485041], [354898637635, 5396135485], [97976149534, -7026240855], [-165107970646, 13317734950], [2853586667, -12284381617], [20765627926, 5478113452], [-134765608138, -10174278687], [164357858519, 16604812994], [-86384082876, -7330535949], [148357164067, 9992715203], [-48417462681, 24878148531], [227462124923, -3873225838], [329132949944, 9632071790], [-197766690767, -18575514322], [253171028692, -9620410554], [-102686017610, 29173639252], [-282249241865, -22867526417], [-243752020468, 8062540034], [-209085157838, -10102630522], [391216658826, -8467190000], [-38701079954, 11345336082], [326749991748, 13219210478], [-226374233675, 23223790323], [287201059738, 26621318661], [-22401353035, 475784507], [-433778478813, -14705061631], [280350941744, 12173699761], [-97062194283, 37350874060], [124873679918, 5179328307], [293562710953, -8005779839], [-106916250640, -10135625334], [355481010970, -5590636205], [170906257792, 7339119137], [167486145983, -41426004613], [24858370982, -20962045566], [573105242663, -12291535733], [-386245654622, -7909740356], [-321687701436, 23516239378], [157153009262, -50533392670], [-306708029048, -27424957856], [-511397160376, 7659526760], [95239542613, 22003168891], [207965311292, 4026739234], [-193948755313, -34228556652], [71724729199, -41413800115], [-244675470704, 22174206755], [-126739186947, 22569832326], [492686816696, -24435079753], [297518584724, -10402467530], [97905617944, -12013754939], [494587074928, 11689780102], [-2205393494, -68989862845], [125750213900, -38052856701], [-170783492246, -41381170734], [83099367567, 39308984639], [46840221505, -45974817147], [540190001916, -20503538787], [-207501271846, 20870906149], [251347866915, 24262416653], [585071168227, -9552818447], [-479020916423, -8254883323], [918655193530, 9968369947], [-271896601538, -35331354170], [-386027559461, 54478854740], [76070997758, 44771405271], [-280796932722, 33794070383], [702605087089, -17224487846], [-185455333636, -18429046789], [169782793500, -31178500150], [162618921740, 35828361343], [562932027808, 35750373540], [655763436026, 10528084304], [-59961132852, -49326558057], [279060609564, 49123450762], [382073833927, -25649342405], [775921348026, -18844676006], [689168025032, 45726474390], [-392069031139, -49826968103], [532032855126, -17082256386], [550110619460, -1965517734], [112510052936, -47415546120], [925084967674, -14258780947], [958571060436, -17014160604], [714074516149, 4019825918], [-543356694950, -17762362148], [143630750895, -18137420869], [194546636594, -28583918324], [-286695450646, -28372173724], [221798588334, 39163104535], [-339760365711, 12632669663], [144727146766, 83323856162], [-333279814622, -25694055164], [-446856925794, -24509644440], [723234083940, -1252839010], [-211574618816, -47046913238], [-509260045892, -32434494929], [-305093668489, 45628996326], [1342621959416, -7271187729], [1208073010831, 34636250703], [-9805230553, -19044551349], [114582569598, -43153328111], [466212807904, 22363503416], [-31860280951, 95841391815], [-92127931337, -37374452366], [70151078464, 106050957940], [-4734173006, -23468195960], [-992920522647, 25566571979], [210834088831, 5737668728], [1397482453425, -25735285113], [-800102779672, 49812457013], [-884631624044, 2229038738], [1514773605958, -15124591141], [-534051889530, 100522126058], [1426644401249, -14551238973], [607356676758, -12298476538], [916218248266, 11270267516], [70739035964, -15816585035], [1282538545388, 29369178998], [417240498953, 80116383579], [-43970026046, 16707806854], [324914976066, -51241035716], [9547464616, 95180270858], [-813203700638, 62979250314], [-54254304390, -65004204410], [395197881734, -16806037330], [763044443792, 38420489250], [134893647245, -59452545633], [-1100524583804, 68273352526], [-1820447970, 74132116558], [-1197408185292, -1700933778], [186589163603, 44212836766], [-1315267746249, 1314796589], [126238427358, 94970694770], [-1488392145736, -29726453145], [-301946365879, -46157158563], [-859761858416, 10053667488], [-201515500303, 106199526887], [655414862568, -99131891011], [1462828679261, -24453801497], [1022626957144, -97445471769], [-766166560518, 116630534646], [-303575145388, -71901853654], [-765618623596, -34298231768], [-1043622103004, -6689392067], [-1974970065046, 42297677434], [-65070608828, -95272575350], [73504935700, -135285738175], [620403381963, 12109639996], [141330902836, -28536134289], [306770384952, -58993703674], [1620423375422, -87574519657], [-546696108908, -66508053055], [-444206811629, -101756716072], [362602310335, -172271628765], [-983065216373, 146080544054]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_304_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_304_8_a_d();".
// 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_304_8_a_d(:prec:=2)
    chi := MakeCharacter_304_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_304_8_a_d();".
// 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_304_8_a_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_304_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,8,sign)));
    Vf := Kernel([<3,R![-234, -69, 1]>],Snew);
    return Vf;
end function;