
// Make newform 336.7.bh.f in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_336_bh();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_336_bh_Hecke();"
// 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_7_bh_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_336_7_bh_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 := [35378704, 214128, 1125468, 16988, 29845, 306, 193, -2, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0, 0, 0], [-197665427324196, -437053447260, -5229627581347, -22043091213, -27650057549, 557057579, -177960445, 1636073], [-699349243077611748, 1213473343058610, -18502659469095411, -77989456039869, -157398820606197, 1970894968827, -629632122285, -5696588781], [354239377144818384, -10543462268940180, 36446552419931358, -165575031962703, 297416713541916, -10845607382811, 1968223271760, -31448254527], [-945621140688043924, -1146402000884955580, -6861081933950833, -17480931945541207, 245097685796969, -143960676182679, 2826191904405, -369495651653], [-279233571033238804, 1160586789283378480, -8110688256075038, 17037777662459983, -61817459723356, 111593476518171, -1876757118200, 474632212627], [136874662243533112, -77338538608714000, 3476685341973407, 3429047277089390, 89098109702407, 21775570970562, -201842164189, 156913428428], [1295333628407081938, -161285957516505460, 25949036508994771, 8835382339852409, 491941235511447, 69581678069333, -646807832245, 580833884751]];
    Rf_basisdens := [1, 164920217026180, 14306828827021115, 28613657654042230, 57227315308084460, 57227315308084460, 11445463061616892, 28613657654042230];
    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_bh();"
function MakeCharacter_336_bh()
    N := 336;
    order := 6;
    char_gens := [127, 85, 113, 241];
    v := [6, 6, 6, 5];
    // 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;

// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_336_bh_Hecke();"
function MakeCharacter_336_bh_Hecke(Kf)
    N := 336;
    order := 6;
    char_gens := [127, 85, 113, 241];
    char_values := [[1, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0], [0, -1, 0, 0, 0, 0, 0, 0]];
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    values := ConvertToHeckeField(char_values : pass_field := true, Kf := Kf); // the value of chi on the gens as elements in the Hecke field
    F := Universe(values);// the Hecke field
    chi := DirichletCharacterFromValuesOnUnitGenerators(DirichletGroup(N,F),values);
    return chi;
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 := 7;
    raw_aps := [[0, 0, 0, 0, 0, 0, 0, 0], [9, -9, 0, 0, 0, 0, 0, 0], [77, 39, 1, 0, -1, -1, -1, 1], [-12, 121, -2, -1, -1, -2, 1, 2], [-448, -455, -14, -20, 14, -12, -6, -7], [692, 1393, -9, 18, -9, 14, 0, 0], [780, -814, -94, -47, 0, 0, 28, -34], [-3837, -1905, -2, -58, -27, -56, -56, 27], [24, 612, -267, 0, 24, -28, 28, 24], [-322, -23, -80, -57, 23, -173, -346, -46], [-12290, 12446, 68, 34, 0, 0, -51, 156], [-45, 20989, 1443, 0, -45, -160, 160, -45], [8906, 17768, -226, 182, 44, -700, 0, 0], [-11732, -221, 284, 505, 221, -94, -188, -442], [53854, 26844, -893, -1454, 166, 922, 922, -166], [89965, 89078, -1774, -3460, 1774, 502, 251, -887], [-59760, 60137, -838, -419, 0, 0, 586, 377], [70256, 35188, 2512, 4784, -120, 3320, 3320, 120], [-66137, -67368, -2462, -427, 2462, -2216, -1108, -1231], [57942, -282, 6697, 6979, 282, -598, -1196, -564], [162483, -161598, 2836, 1418, 0, 0, 976, 885], [-2508, -172730, -6036, 0, -2508, -109, 109, -2508], [24001, 49693, 5776, -4085, -1691, -5336, 0, 0], [688434, 345966, -284, -7564, -3498, -6330, -6330, 3498], [-6797, -20527, -7281, 348, 6933, -2656, 0, 0], [18996, -21942, 18228, 9114, 0, 0, -606, -2946], [644315, 323197, 20086, 36014, -2079, 3640, 3640, 2079], [973, 908942, -35624, 0, 973, -2698, 2698, 973], [-569283, -572822, -7078, -8414, 7078, -15884, -7942, -3539], [520868, -4016, -17245, -13229, 4016, -4966, -9932, -8032], [-343138, 122, 63364, 63242, -122, -3611, -7222, 244], [-3165957, -1584324, -12191, -19000, 2691, -10104, -10104, -2691], [-407754, -412986, -10464, -2384, 10464, 11612, 5806, -5232], [1395044, 2791159, 23596, -22525, -1071, -12832, 0, 0], [7454, -1294880, 70628, 0, 7454, -4324, 4324, 7454], [1204045, 1192864, -22362, 31035, 22362, -2854, -1427, -11181], [-98616, 118488, -74640, -37320, 0, 0, 3276, 19872], [7662, -2636624, -53916, 0, 7662, -9686, 9686, 7662], [1471628, 2960270, 11815, 5199, -17014, -16024, 0, 0], [2984796, 1472832, -77883, -77502, 39132, 768, 768, -39132], [1532736, 1517604, -30264, 42441, 30264, 56756, 28378, -15132], [1369434, 2749161, 8129, 2164, -10293, -56530, 0, 0], [-22042, -1670432, 59529, 0, -22042, -28574, 28574, -22042], [332513, 335195, 5364, 209172, -5364, 36980, 18490, 2682], [-4374424, 32146, 23579, -8567, -32146, 27118, 54236, 64292], [-2996722, 3032566, 138544, 69272, 0, 0, -56918, 35844], [5135988, -814, -132236, -131422, 814, -51924, -103848, -1628], [1388826, 2698113, -177078, 97539, 79539, -90399, 0, 0], [2193330, -2160531, 11768, 5884, 0, 0, -15426, 32799], [12955523, 6516217, -14697, -183216, -76911, -17094, -17094, 76911], [-13760, 970766, -262363, 0, -13760, -9864, 9864, -13760], [2795746, 49142, -387914, -437056, -49142, -2058, -4116, 98284], [1780616, -1780697, -256352, -128176, 0, 0, -162818, -81], [-3122641, -6153883, 91768, -369, -91399, 197396, 0, 0], [11584716, 5848722, 223291, 221126, -112728, -6288, -6288, 112728], [-77322, -13068, 128508, 128020, -128508, 72068, 36034, 64254], [4651293, -4493882, 326302, 163151, 0, 0, 91553, 157411], [-2245117, -1063037, 175758, 113430, -119043, 260705, 260705, 119043], [-19011111, -19027240, -32258, 422936, 32258, -20632, -10316, -16129], [-7546972, -21872, -53679, -31807, 21872, 34944, 69888, -43744], [3435515, -3434726, 999086, 499543, 0, 0, 10350, 789], [-1231774, -2439355, 181910, -157717, -24193, 299549, 0, 0], [-3931694, -7723747, -392680, 532321, -139641, 28068, 0, 0], [-4660440, 4879912, -1720, -860, 0, 0, -33826, 219472], [26657010, 13379823, 64882, -75508, -102636, 199018, 199018, 102636], [43657, -152365, -500206, 0, 43657, 275641, -275641, 43657], [-79905, -10407091, 247518, 0, -79905, -7098, 7098, -79905], [100117, -61842, -817542, -755700, 61842, 107346, 214692, -123684], [16270162, 16353308, 166292, 286307, -166292, 54740, 27370, 83146], [-2105710, -4173236, 497136, -458952, -38184, -145634, 0, 0], [-20115648, 20186570, -153642, -76821, 0, 0, -479990, 70922], [37156, -729130, -1311318, 0, 37156, 223446, -223446, 37156], [-904746, 737940, 262280, 131140, 0, 0, 160261, -166806], [-117483, -13071511, 26739, 0, -117483, -63582, 63582, -117483], [9446876, -367785, -357462, 10323, 367785, -101954, -203908, -735570], [29950766, 15021102, 539791, 896706, -91438, 552950, 552950, 91438], [-2158388, -2068468, 179840, -1739643, -179840, -384804, -192402, 89920], [34570399, 17450693, 87941, -486092, -330987, -621600, -621600, 330987], [198536, 36054526, 445581, 0, 198536, 84066, -84066, 198536], [-3304217, 3393821, 122928, 61464, 0, 0, 428932, 89604], [-13719556, -27295210, 822652, -678750, -143902, 1113908, 0, 0], [4535352, -342115, -1756985, -1414870, 342115, 162616, 325232, -684230], [-23432270, -23650834, -437128, -637215, 437128, 586760, 293380, -218564], [-7146230, -14489335, -718007, 521132, 196875, 794364, 0, 0], [36071727, 17760039, -293402, 516494, 551649, -463563, -463563, -551649], [150915, -76681056, 504588, 0, 150915, -500460, 500460, 150915], [84410076, 248922, 448331, 199409, -248922, 240448, 480896, 497844], [-374232, 26630257, 2038980, 0, -374232, -301884, 301884, -374232], [22255814, 44387120, -1074234, 949726, 124508, 1015346, 0, 0], [49967200, 303225, 2521086, 2217861, -303225, 83454, 166908, 606450], [23079066, 11266560, -2203717, -3315542, 545946, 394542, 394542, -545946], [-45473688, 45595670, -160410, -80205, 0, 0, 717652, 121982], [-3379168, -3486732, -215128, 2680924, 215128, -82166, -41083, -107564], [33546685, 81833, 2463545, 2381712, -81833, -527360, -1054720, 163666], [-673675, 88752027, -748446, 0, -673675, 200864, -200864, -673675], [-31080878, -62777888, -174968, -441164, 616132, -944228, 0, 0], [-123659189, -61346997, 1839415, 1748440, -965195, -486065, -486065, 965195], [-22413900, 23103910, -715608, -357804, 0, 0, 1397792, 690010], [133684765, 66766223, 1411270, 3127178, 152319, -1237432, -1237432, -152319], [698313, 39267701, 2013669, 0, 698313, 19770, -19770, 698313], [112946272, -89664, 61248, 150912, 89664, 189538, 379076, -179328], [10314685, 10135874, -357622, 302729, 357622, -34782, -17391, -178811], [80032536, -79966173, -52394, -26197, 0, 0, -981396, 66363], [-26432, 19475240, -3407582, 0, -26432, -326350, 326350, -26432], [-8146303, -8599570, -906534, -3128817, 906534, 957804, 478902, -453267], [76954285, -77325265, -1112588, -556294, 0, 0, 113794, -370980], [-158750487, -317327157, 1241583, -1067766, -173817, -414456, 0, 0], [-68658110, -34225344, 2086566, 3758288, -207422, 1269058, 1269058, 207422], [62834198, 62164522, -1339352, -1891714, 1339352, -1087812, -543906, -669676], [-56458555, -111338114, 3571366, -1992370, -1578996, 1086402, 0, 0], [-18315906, -8623860, 349024, -1438324, -1068186, -811399, -811399, 1068186], [-65690380, -66121470, -862180, -396896, 862180, 3625788, 1812894, -431090], [293197076, -324062, 2232844, 2556906, 324062, -414772, -829544, -648124], [3205867, -2901172, -84450, -42225, 0, 0, -2314574, 304695], [-109165442, -370479, -4673166, -4302687, 370479, 617269, 1234538, -740958], [-76132690, -77235482, -2205584, 2077171, 2205584, -284948, -142474, -1102792], [-71321974, -142297997, 176846, 169105, -345951, 1232208, 0, 0], [50352714, -48847968, 5216892, 2608446, 0, 0, -572448, 1504746], [1064273, -114298157, -4361896, 0, 1064273, 1290499, -1290499, 1064273], [149353000, 355526, -7051203, -7406729, -355526, 523146, 1046292, 711052], [-1178553, 332634, 5372724, 2686362, 0, 0, 968210, -845919], [144993261, 1127200, 5025902, 3898702, -1127200, -91024, -182048, 2254400], [68413863, 34700157, -1571130, -5115162, -986451, -1150629, -1150629, 986451], [139938236, 139614763, -646946, -3897553, 646946, 3454760, 1727380, -323473], [-74610661, -36870503, -5840748, -13420806, -869655, 1587702, 1587702, 869655], [183727384, 550343, 2186293, 1635950, -550343, -836297, -1672594, 1100686], [-724896, 75704050, -7804824, 0, -724896, 1280512, -1280512, -724896], [46939628, 23501670, -1098218, -2323860, -63712, 2540420, 2540420, 63712], [77948498, 157153912, 3147750, -1890834, -1256916, 1287795, 0, 0], [48843253, 25935503, 3901365, 1747224, -3027753, -504726, -504726, 3027753], [-310889149, -308517754, 4742790, 9896955, -4742790, -3287208, -1643604, 2371395], [113596722, -731814, -4750490, -4018676, 731814, -1093202, -2186404, -1463628], [-41270, -23628374, 4360974, 0, -41270, 1733533, -1733533, -41270], [-94876496, 1069290, 6803646, 5734356, -1069290, -377936, -755872, 2138580], [-96604496, -50082984, -3308463, 506018, 3561472, 1202428, 1202428, -3561472], [-93277803, -186811416, -6688470, 6432660, 255810, 3678694, 0, 0], [3621936, -2879842, -7614340, -3807170, 0, 0, 975238, 742094], [53361800, -54550592, -7712300, -3856150, 0, 0, -2140088, -1188792], [-51914772, -101184051, 1092263, 1553230, -2645493, 1901055, 0, 0], [399652784, 400020466, 735364, 7077751, -735364, 1749620, 874810, 367682], [-110781654, -221333940, 4310552, -4081184, -229368, 1606146, 0, 0], [-593737, 296584573, 6869613, 0, -593737, -41947, 41947, -593737], [426593834, 425997602, -1192464, 1121394, 1192464, 4327048, 2163524, -596232], [292680257, 1085737, 14062938, 12977201, -1085737, 75830, 151660, 2171474], [-93147363, 87766224, -6856328, -3428164, 0, 0, 548000, -5381139], [-309477706, -619275298, -1596620, 1276734, 319886, 3405332, 0, 0], [-500416272, -1001313777, -4573145, 4091912, 481233, -536218, 0, 0], [109216356, -109136824, 2340052, 1170026, 0, 0, 2085148, 79532], [-263264506, -128745644, 10092860, 8639284, -5773218, -850456, -850456, 5773218], [1160940, 47998650, -13251680, 0, 1160940, 187400, -187400, 1160940], [4601718, 272850902, 3514320, 0, 4601718, -1341808, 1341808, 4601718], [-74601888, -153238284, -6118623, 2084115, 4034508, -3232356, 0, 0], [410395556, -1891629, -13317498, -11425869, 1891629, 2639958, 5279916, -3783258], [632973238, 315760944, 4454526, 11811752, 1451350, 186886, 186886, -1451350], [238193833, 238503328, 618990, 23674533, -618990, 25096, 12548, 309495], [-300290782, 162916, 8720893, 8557977, -162916, -2895162, -5790324, 325832], [4002209, -5047491, 11960448, 0, 4002209, -1139002, 1139002, 4002209], [-362373688, -182839044, -3264313, 80174, 3304400, -2786062, -2786062, -3304400], [-242325515, -482253844, -13783866, 16181052, -2397186, -4224200, 0, 0], [-278628747, 276507406, 16172080, 8086040, 0, 0, 1520387, -2121341], [1293884, 18646798, -5319968, 0, 1293884, 418612, -418612, 1293884], [338069636, 434206, -16315991, -16750197, -434206, -3041122, -6082244, 868412], [-310537212, -3154376, -11190334, -8035958, 3154376, -1171319, -2342638, -6308752], [73396913, 34922958, -10422336, -13742678, 3550997, -1544128, -1544128, -3550997], [365614338, 362683242, -5862192, 6263040, 5862192, -1811216, -905608, -2931096], [-298507404, 288001538, -11705538, -5852769, 0, 0, -4115168, -10505866], [787778554, 787747534, -62040, 710046, 62040, 385750, 192875, -31020], [-331392983, 330949016, 20354908, 10177454, 0, 0, -4066386, -443967]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_336_bh_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_336_7_bh_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_336_7_bh_f(:prec:=8)
    chi := MakeCharacter_336_bh();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 7));
    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_336_7_bh_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_336_7_bh_f( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_336_bh();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,7,sign)));
    Vf := Kernel([<5,R![54227831439690000, -2057883988770000, 28839143385900, -106548914700, -982673451, 5570334, 59091, -462, 1]>],Snew);
    return Vf;
end function;