
// Make newform 304.6.a.f in Magma, downloaded from the LMFDB on 20 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_6_a_f();".
// 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_6_a_f();".
// 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 := [-360, -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 := 6;
    raw_aps := [[0, 0], [-1, -1], [-24, 3], [-59, 4], [-330, -1], [809, -5], [27, 10], [-361, 0], [1617, -49], [-1083, -315], [748, -316], [5330, -172], [8616, -602], [-5792, 281], [5520, 1115], [10593, -601], [39327, -73], [21398, 825], [-5453, 3101], [31878, -1268], [6617, 2984], [-33494, -134], [4134, 2446], [61956, 4276], [90590, -2622], [-70878, 2780], [-8198, -4336], [15345, 1633], [97391, -2307], [193998, -2228], [-158762, -9932], [69000, -4521], [-31161, -12090], [319492, -1263], [121728, -11587], [95530, 19600], [122570, 16036], [332692, -3984], [-84468, -21870], [157254, 31632], [46800, 22118], [141122, 30184], [-159423, 22844], [-119962, 3402], [-40824, 2842], [-301151, 22836], [-84725, 55], [266926, 20456], [-400425, -38675], [537146, -49837], [899874, -26319], [115665, 26016], [-341104, -1558], [314730, -35463], [1462188, 9324], [-120120, -33633], [896778, -28846], [-70823, -42225], [-945832, -9691], [-961392, -52346], [-546434, -39809], [1167231, 9797], [1350700, -5764], [-365007, 110402], [-3112411, 4945], [-58341, 29475], [3034345, -40033], [77372, 119364], [-899850, 81341], [2978654, -40045], [-57423, 107385], [-635337, -85758], [-2182604, -23724], [2444939, -116607], [-1084025, -59037], [1459734, 153490], [1902426, 27125], [2982548, -19019], [-2038704, 177888], [760328, 203944], [-1095780, 74616], [-1353163, 102959], [4863906, 71230], [3499346, 49228], [-653108, -223478], [1306434, -103815], [-4649850, -9342], [-2847775, -184614], [-3563364, -27603], [-8131004, -43933], [6271038, 132085], [-971196, -98664], [2456998, 37200], [2747220, 87672], [-1475540, -402417], [3906081, -95365], [4241118, -226246], [4274100, -83576], [950389, 311725], [3719090, 32421], [-6102716, -188664], [4238928, 125989], [-3713988, -196422], [-487776, 94200], [-5252228, 163992], [-3864877, 603200], [1166340, -157633], [-6892830, -343460], [9418920, 52216], [-8475844, -166514], [9532630, -102534], [-2587966, -91629], [2448786, -511017], [1059466, 617458], [-3742112, -265851], [4653078, 507730], [554230, -776681], [3288033, -53532], [8676120, 347529], [-2725971, -306127], [-6537529, -708751], [6691460, 535026], [34755, 113871], [-1254708, -209448], [3258850, -582323], [5595228, 117346], [-5980594, -759204], [8628957, -975940], [-3274367, 503034], [-6959584, 230930], [-1851752, 471637], [-8903028, -797544], [-1274912, 173848], [-3177358, -594723], [22323099, 225062], [-809515, 198178], [-23641929, 90843], [3744775, 458869], [11491797, -215701], [-10419327, -864018], [-12149723, -125549], [11174208, -359775], [11390131, -84458], [12691647, -705399], [20751887, -105327], [-4810416, -759690], [-18705910, 703432], [22096068, -435018], [-1052054, -914153], [29607642, 342232], [7661699, -1150641], [-5191578, 994143], [-9025958, -591937], [-13279134, 1168812], [-13268795, 10351], [29724096, 838326], [-4358549, 566569], [7582281, -393579], [-10229263, -1080720], [12950985, 817909], [-17950704, -104812], [24050574, 1445982], [-9329624, -888112], [-19703772, 476544], [-3866244, 1136832], [-9595914, -663234], [-43322852, 467962], [-15063736, 194841], [-39392260, 259172], [14014488, -899655], [21976905, 157197], [8778401, -1104685], [-28993224, 1728960], [19822271, 963395], [-18951416, 542332], [19493238, 2116437], [38560720, -563860], [-18482778, 1131203], [-38824544, -370552], [-21457336, -999457], [-30377057, 1328140], [-27180879, -875257], [271898, -2926276], [-36020016, -556402], [30362505, 489304], [-60108150, -637689], [4010087, -1697649], [-10150808, 3830446], [-15094810, -955096], [65836449, 1004083], [-18536506, -503084], [31310148, 271557], [3347800, 2701780], [5398863, -4257621], [-62800668, 1337100], [-17088396, 3946536], [-65503639, -931348], [6240740, 2473574], [9598443, 347555], [18320751, 2436996], [-72836034, 1403526], [46630408, -228890], [-45487639, -564893], [33893348, -1410658], [-8545626, -382314], [-7033680, -3987], [68785477, 627042], [27301479, 2435097], [47947029, 1811172], [50748571, 2605095], [-3559651, 2549373], [-2208840, 4934738], [-33605492, -3404419], [-30087267, 122599], [-19743708, -1910734], [-1280470, 1887016], [99926029, 744459], [39983622, -4437858], [-6684786, 2196338], [-50726814, -2747419], [6392438, 2147760], [40288618, 3209932], [-31509150, -1292948], [-2372015, -1453674], [-87331611, -598163], [-60342898, 337089], [5320194, -880512], [60184170, -1975118], [-68350838, 2755592], [31337670, -4487841], [28604078, 2104245], [43626715, -3803435], [4529152, -2967576], [15993210, -3200544], [31590454, 4158281], [51850371, 4175042], [81428894, 1861827], [-7089696, 6122523], [-1046226, -571846], [91529244, -2702668], [19259436, -793822], [-141907250, 1586185], [-46520045, 4187701], [41785121, 3994189], [-125041884, 2191762], [6674124, -4330049], [-15630464, 929547], [31311564, -6752760], [-32603084, 8047404], [51948189, 3374238], [77141582, 2884964], [-19549485, 5899608], [-44338251, 241431], [-56945218, -2627842], [-88310964, 1877891], [-93044178, 1123758], [47003258, 6134155], [118465996, 1305366], [64017990, -6333108], [-123363898, 4579548], [-106032884, 3826822], [-28660956, 2257866], [-176982256, -533789], [2253341, -5694859], [44544090, 4692661], [-97253105, -3103659], [51112575, 7753601], [-189558669, -1344708], [-48575771, -6608791], [36450930, 940689], [207588329, -780189], [190316689, 205109], [-124497391, -1270744], [111413647, -5336606], [147755078, 5665374], [75068287, -5537655], [-126485526, -2522581], [-71505031, -8805465], [25774292, -10315710], [-49783200, -6983791], [94700706, -8380188], [-134096723, 2131503], [-54968775, -7743522], [136176989, -1335047], [-20266892, 6707389], [-120449736, -5925555], [-44476567, 862830], [-22635942, 7259402], [56987959, 7737538], [-89816994, 1430186], [162400434, 1782780], [72652788, -1386287], [-53134854, -5485570], [98473956, 5593290], [-96162718, 9792560], [-147971508, 5042262], [-69136394, 2871010], [90896478, -3061447], [-109609155, 11822231], [226063246, -6634203], [42749558, -7854685], [72744507, 10296563], [149627509, 5602796], [69094131, 13230905], [177378220, 5091967], [196434830, 4589138], [20513703, 6517805], [-294037021, 1685559], [-25095288, -710595], [62132852, -10669690], [-92693904, 6159517], [105413814, -679780], [169144602, 8068398], [49372372, -16482518], [-193652208, 5530104], [54349040, -6567532], [-240972162, 1640725], [103876530, -7395750], [-332466061, -2344924], [-195983610, 5972999], [150187693, -3245727], [105507215, -4959478], [-39017475, 17477493], [80366902, -5915650], [-110018721, -11569169], [-12187726, -218410], [-14102345, -1376315], [-138861023, 12685081], [384162672, 1940994], [27538236, 2368749], [88177928, 13017359], [-105674469, -6497225], [56878573, 3999755], [141715218, 9244371], [93982678, -15415945], [15930576, -5655693], [253952075, 2346853], [-216445416, 12852070], [222721487, 506212], [-32327993, 4481521], [-66219133, 9079409], [60250479, -7681474], [-1520043, -13180323], [93295570, 9714578], [-33194199, -15550219], [-147023172, -4470396], [-98327302, -1329808], [111564301, 3112581], [-23094870, 8464686], [-162248616, 4569721], [181971064, 2593066], [-77189038, -3151750], [99685848, -2512533], [-394321928, 1140890], [8356346, 23105186], [54743616, -15272160], [22283277, 9747123], [243493560, 8781732], [-127837617, -6882254], [484788990, 2507592], [9847472, -29862655], [-13607418, -16868076], [-241262442, 3720024], [175216503, -13010843], [51661942, 1330065], [517467812, -2695718], [15938574, 15304356], [-435232370, 5793816], [53825798, 18075862], [-188206674, 12111086], [-17819435, -16905387], [-16531923, -14110774], [-158051235, 12289465], [306504676, -8334055], [419758760, -7883471], [-265791675, -17525641], [-88958448, -6538000], [303495230, -19458867], [428220333, -1885586], [243871484, 21634228], [224834082, 6672000], [-352090374, 628561], [65520403, 4894304], [60300150, 15734405], [252758323, 5529423], [-177204582, -6480138], [-231312317, -23371947], [258065036, -3898418], [342988090, -15774299], [95470152, 18345294], [255309638, -20011244], [107407779, 13605495], [199165446, -13207254], [162745618, -19331883], [457730580, 3629106], [-219332386, -23200540], [-167298410, 6981906], [-152546232, -19398542], [-122316020, 14910582], [22460160, 19424250], [-702576226, -1129710], [-481831254, -12106003], [-162289178, -7893374], [-128018040, 10447562], [204898374, 12383806], [-252010703, 11381850], [16592486, -16391993], [-9846588, -6668334], [53636545, 6547965], [-421193688, 10469795], [-170336200, 5304348], [-319026810, 8155021], [317255355, 13234059], [392205802, 22155753], [391643117, 13300045], [378018180, 17717290], [104665122, -2877066], [110585092, -7993116], [133322787, -2165305], [-123205800, -6196538], [402015930, 11587002], [89553863, 17580407], [-114566757, -1424140], [138507405, -8343379], [-195233398, 5836322], [385747203, -1953675], [-579396741, -3580293], [-66456693, 16114424], [141111592, 22856917], [160835745, 11793354]];
    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_6_a_f();".
// 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_6_a_f(:prec:=2)
    chi := MakeCharacter_304_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 6));
    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_6_a_f();".
// 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_6_a_f( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_304_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<3,R![-358, 3, 1]>],Snew);
    return Vf;
end function;