
// Make newform 336.7.bh.a in Magma, downloaded from the LMFDB on 19 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_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_336_7_bh_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 := [5192355364, 1200774512, 201523590, 17325616, 1155979, 35442, 1061, -2, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0, 0, 0], [-314616662632701367834, -174297213726189823636, -11775455453028959461, -957859907329660610, -26857091404894553, -1021792250749710, 6041228263913, -896877863432], [890254789664279551983072, 101426167143129490957192, 11155237836159615910206, 279788965160726470064, 12386413249503110037, -391000809124369202, 18229925255082312, -183863909546285], [51400375695712227877126, 10908788665654789245724, 1297671150940055659922, 70457180636704215266, 2391093571624293239, 28776968386772265, 880422014972009, 39954530257266], [1016125809744114733509902, 140929163194706907910800, 19555608599214840237151, 965806004381997551388, 43129073166676770636, 432526205355998853, 11747984640696868, 718139017476094], [973864425514123824592632, 217432763210238211497844, 25339769573425769017226, 1747422265337618494262, 61629572271591505654, 1519321793044335929, -1023724879792003, 1615003316699301], [39694930904486128572266, 2818480818201421499716, 66584118021960249722, -42654633113513188174, -1725692575549182196, -91481808542443869, 1641279106520201, -85566866050425], [-155002313043282618164356, 55591103128615125986216, 4842950476900008799095, 990648586051920832168, 26148375894322597861, 1845278294272487085, -28769556653539830, 1502047347016715]];
    Rf_basisdens := [1, 731815900224411036034, 1463631800448822072068, 43047994130847708002, 731815900224411036034, 1463631800448822072068, 56293530786493156618, 731815900224411036034];
    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], [-49, 25, -1, 0, 0, 0, 0, 0], [-37, 15, 1, 0, 1, 0, 0, 1], [-90, 96, -6, -3, -2, -6, 1, 1], [234, -466, 0, 3, 3, 2, 4, 7], [85, 64, 13, 7, -11, -13, 4, -4], [-615, 315, 1, 7, -16, 0, -16, -7], [10, -3898, 13, 7, 3, -26, -3, 7], [-8611, 91, -184, 20, -20, 92, 1, -21], [-2012, -1829, -69, -3, 60, 69, -57, 57], [-61, 4103, -104, 94, -43, 208, 43, 94], [-13920, 28164, 0, 80, 80, -262, 62, 142], [21287, -98, 430, -35, 35, -215, -117, 152], [-17030, 8810, -671, -171, 81, 0, 81, 171], [49193, -49036, -157, -24, -264, -157, -240, -240], [55813, 54598, 361, -14, -413, -361, 427, -427], [-3988, 2208, -556, -480, 128, 0, 128, 480], [-193502, 194073, -571, 156, -37, -571, -193, -193], [90889, -1824, 898, -520, 520, -449, 1375, -855], [-99161, -104097, 4054, -83, -358, -4054, 441, -441], [2882, 121730, 2041, -296, -841, -4082, 841, -296], [76056, -152524, 0, -369, -369, 1861, 1449, 1080], [100710, -48426, -2160, -378, -1698, 0, -1698, 378], [-571, -3798, 0, -613, -613, 6140, 1200, 587], [-345942, -342486, -1104, -966, 2142, 1104, -1176, 1176], [100073, -52431, 4591, -1531, 198, 0, 198, 1531], [985, 512652, -713, -2863, -1698, 1426, 1698, -2863], [-331717, 328307, 3410, -833, 426, 3410, 1259, 1259], [96661, -8576, 15430, -1470, 1470, -7715, 861, 609], [625222, -4297, 9038, -4417, 4417, -4519, -222, 4639], [-1640031, 816518, 8076, -1288, -1081, 0, -1081, 1288], [-1981238, 1990096, -8858, -1974, 164, -8858, 2138, 2138], [254877, -501650, 0, -5137, -5137, -9763, -1659, -6796], [-5162, -2359748, -2462, -1338, 2700, 4924, -2700, -1338], [1276824, -1257502, -19322, -4733, -4945, -19322, -212, -212], [686412, 649900, 36120, -488, 292, -36120, 196, -196], [-5990, 4790468, -3988, -2026, 2002, 7976, -2002, -2026], [1058307, -2146222, 0, -1664, -1664, 29741, 133, -1531], [7571160, -3797530, 28233, -3901, -4333, 0, -4333, 3901], [5523715, -5511614, -12101, -4449, 3257, -12101, 7706, 7706], [414130, -818438, 0, -8525, -8525, -18244, -8422, -16947], [-17284, 5674518, -13159, 3289, 4125, 26318, -4125, 3289], [-6307355, 6311077, -3722, -4364, -14008, -3722, -9644, -9644], [-6801241, 5596, 8162, 9860, -9860, -4081, -9677, -183], [-481998, -501620, -5462, -498, -12044, 5462, 12542, -12542], [2438062, 11564, -23604, -630, 630, 11802, 238, 392], [1462875, -2954413, 0, -556, -556, 35580, 6917, 6361], [533100, 479888, 74046, -8371, 18788, -74046, -10417, 10417], [13674659, -6830143, -11832, 5982, -2541, 0, -2541, -5982], [-1686, -6348560, -20409, -7463, -18723, 40818, 18723, -7463], [3510630, 29702, -19524, -5812, 5812, 9762, -19940, 25752], [-2632578, -2650650, 7294, 16307, -21696, -7294, 5389, -5389], [8433630, -16789492, 0, -2777, -2777, -99379, -21611, -24388], [18189876, -9104960, -1149, 4345, 21193, 0, 21193, -4345], [5111926, -5037536, -74390, -22812, -29212, -74390, -6400, -6400], [-3943720, -3962908, -5392, 10667, -22957, 5392, 12290, -12290], [-7136125, 3537781, 73580, 29275, -13017, 0, -13017, -29275], [-6521915, 6546925, -25010, 36953, 43944, -25010, 6991, 6991], [-13114737, -27494, 2406, -17180, 17180, -1203, 26291, -9111], [7963568, 7971903, 60699, 48508, -13991, -60699, -34517, 34517], [-6940755, 13748573, 0, 2500, 2500, 98396, -34541, -32041], [7619497, -15162654, 0, 20957, 20957, -49845, 26495, 47452], [-1602532, -1502426, -124846, -19622, 7252, 124846, 12370, -12370], [6718050, -3309091, -42800, 59238, -57068, 0, -57068, -59238], [5033, -1270309, 39692, 14763, 34659, -79384, -34659, 14763], [-151845, 14572147, -108393, 101205, 43452, 216786, -43452, 101205], [-5845547, 29802, 30948, 51360, -51360, -15474, -45276, -6084], [-7629917, 7510216, 119701, 80643, 43687, 119701, -36956, -36956], [6847930, -13518790, 0, 38886, 38886, -162638, 14432, 53318], [-2067545, -1995962, 117445, -31829, 126343, -117445, -94514, 94514], [158952, 15647254, 37860, -23126, -121092, -75720, 121092, -23126], [-141878, -434941, 146293, -5511, -67874, -146293, 73385, -73385], [303477, 27135031, 220812, -65208, -82665, -441624, 82665, -65208], [-17673771, 6702, 158330, 124433, -124433, -79165, -85867, -38566], [-13440058, 6644696, 8609, -97595, 142057, 0, 142057, 97595], [10458585, -10543458, 84873, 17637, -86257, 84873, -103894, -103894], [20119, -135943, 127356, 34372, 124411, 0, 124411, -34372], [-88110, 29825312, -20211, 8573, 67899, 40422, -67899, 8573], [7744283, 7550723, -42920, -59284, -58956, 42920, 118240, -118240], [17373954, -34224112, 0, -175838, -175838, -549766, -25970, -201808], [37021490, 2258, -181840, 14711, -14711, 90920, 88662, -103373], [-30875495, 30950074, -74579, -260811, -151307, -74579, 109504, 109504], [32283220, -64677664, 0, -37919, -37919, 142404, 31180, -6739], [66275439, -33289559, 72276, -209753, 231403, 0, 231403, 209753], [-275313, -32379834, -123129, 5211, 152184, 246258, -152184, 5211], [-35071249, -216042, 300374, -164402, 164402, -150187, 65855, 98547], [-412560, 81037271, -274908, -816, 137652, 549816, -137652, -816], [5274612, -10531458, 0, -72970, -72970, 78224, 95990, 23020], [-85923395, 140454, -249030, 27063, -27063, 124515, -15939, -11124], [-9886326, 4959338, 169365, -23551, -201715, 0, -201715, 23551], [-36313331, -37667748, 927913, -170781, -42471, -927913, 213252, -213252], [-27725756, 27349299, 376457, -79907, 158352, 376457, 238259, 238259], [-933391, 463526, -779536, -227427, 227427, 389768, -73758, 301185], [333885, -9788963, 385963, 160923, 52078, -771926, -52078, 160923], [37288794, -74372644, 0, -66336, -66336, -141164, 63780, -2556], [-10750199, 4715633, 1253683, 20028, 65250, 0, 65250, -20028], [21877532, 22436764, -396100, 87634, -6068, 396100, -81566, 81566], [104723515, -52255613, -56113, 302777, -156176, 0, -156176, -302777], [359865, 107947771, 476490, 271230, 116625, -952980, -116625, 271230], [-100397644, -928650, 1460636, -35314, 35314, -730318, 198332, -163018], [111599310, -111738210, 138900, -118431, -51361, 138900, 67070, 67070], [-6616953, -5010664, -578247, 286292, 227729, 578247, -514021, 514021], [32332, 140059332, -101996, -60754, -134328, 203992, 134328, -60754], [-125464918, 126198817, -733899, -43632, 99153, -733899, 142785, 142785], [22713959, 21430011, 1394872, -8292, 63754, -1394872, -55462, 55462], [161959443, -323936330, 0, 176839, 176839, -165282, -182726, -5887], [-63476906, 31362000, 696610, -145760, 56296, 0, 56296, 145760], [-233277912, 233054424, 223488, 132696, -278138, 223488, -410834, -410834], [66998567, -134702268, 0, 628594, 628594, 304836, -400298, 228296], [89259420, -43574094, -1934425, -90196, -176807, 0, -176807, 90196], [64946916, -65633418, 686502, 55364, -253192, 686502, -308556, -308556], [-211478246, 33340, -529244, 44910, -44910, 264622, 231282, -276192], [55417220, 54836859, 416695, -240474, 158641, -416695, 81833, -81833], [-195982543, -1808805, 2626088, -515434, 515434, -1313044, 495761, 19673], [107801381, -108141652, 340271, 704019, 771029, 340271, 67010, 67010], [71641997, -145057162, 0, 785295, 785295, 1018635, -754533, 30762], [32776152, 31194758, 1276398, -347068, 194570, -1276398, 152498, -152498], [7817, 161524839, -139240, -373295, -147057, 278480, 147057, -373295], [239986659, -393892, 1003314, -263800, 263800, -501657, -107765, 371565], [-3730843, -1756679, -1771858, 122589, -21436, 1771858, -101153, 101153], [97897531, -972236, 1490092, 22192, -22192, -745046, 227190, -249382], [183851781, -92270391, 425346, -39339, 263655, 0, 263655, 39339], [-123801137, 124656538, -855401, 541644, 673629, -855401, 131985, 131985], [-31924075, 16133349, -844503, -1011743, 501880, 0, 501880, 1011743], [-360356548, 237353, 730154, 1031178, -1031178, -365077, -602430, -428748], [994384, 39197126, 1161848, 609608, 167464, -2323696, -167464, 609608], [-230476828, 113755074, 1922354, -226894, 1044326, 0, 1044326, 226894], [-58943662, 117544083, 0, -421295, -421295, 701145, 357904, -63391], [-185395607, 91995219, 798996, -1028674, 606173, 0, 606173, 1028674], [42242660, -42118559, -124101, 34596, -193131, -124101, -227727, -227727], [54860894, 846402, -1794940, 1372096, -1372096, 897470, 51068, -1423164], [-1038804, 269914630, -479629, 84624, 559175, 959258, -559175, 84624], [27315452, 73056, 1124456, 562238, -562238, -562228, -635284, 73046], [304319008, -149783522, -3794627, 770459, -957337, 0, -957337, -770459], [180876137, -363254500, 0, -829468, -829468, 1526458, 24232, -805236], [119249218, 119627122, 121576, -719678, 969418, -121576, -249740, 249740], [139635966, 141644752, 505294, 1003338, 253702, -505294, -1257040, 1257040], [-24487320, 45636089, 0, 830396, 830396, 2314761, -1023790, -193394], [-170174249, 170269234, -94985, 686457, 839613, -94985, 153156, 153156], [129157386, -260531530, 0, -317590, -317590, 1836486, -380272, -697862], [1826641, 11697775, 2013943, 864600, 187302, -4027886, -187302, 864600], [402865812, -402747770, -118042, 1575458, 794554, -118042, -780904, -780904], [244378846, 1255390, -2612354, 357339, -357339, 1306177, 50787, -408126], [42746693, 46693121, -3278422, 379213, -45210, 3278422, -334003, 334003], [231214104, -462153520, 0, 510478, 510478, -3634, 271054, 781532], [211667644, -419573514, 0, -235491, -235491, -3469012, 292762, 57271], [126248686, 130744944, -2081762, 1478982, -271734, 2081762, -1207248, 1207248], [-287651494, 144505420, -174778, 1514290, -1184568, 0, -1184568, -1514290], [-1703036, 591922154, -995360, 1768432, 707676, 1990720, -707676, 1768432], [1192922, 51207646, 621922, -843062, -571000, -1243844, 571000, -843062], [-286467075, 576164902, 0, 1278094, 1278094, -3989025, -758273, 519821], [-181213459, 2798214, -4002006, -467475, 467475, 2001003, -797211, 1264686], [-786276986, 394263264, -2230982, -352400, -18560, 0, -18560, 352400], [-22963192, 22687799, 275393, -1724146, -1290821, 275393, 433325, 433325], [222865299, -3259292, 7597998, 224770, -224770, -3798999, -539707, 314937], [-1471839, 552444211, -1042943, -671445, 428896, 2085886, -428896, -671445], [391776164, -194522166, -2881519, 991973, 149687, 0, 149687, -991973], [189766461, -373403866, 0, 304882, 304882, -4856084, 1272972, 1577854], [46949621, 46284490, -2605393, -111986, -1523276, 2605393, 1635262, -1635262], [-1774828, 186591918, -1687048, -1048748, 87780, 3374096, -87780, -1048748], [-719098883, -2426144, 3356254, -149724, 149724, -1678127, 748017, -598293], [-251520732, -768953, 3122270, 666605, -666605, -1561135, -792182, 125577], [-1056398041, 529097850, -814741, 243857, -982918, 0, -982918, -243857], [-185491342, 182717462, 2773880, 2045664, -301256, 2773880, -2346920, -2346920], [103056733, 114401732, -7310423, 1059719, 957569, 7310423, -2017288, 2017288], [-216582736, 216750503, -167767, -1221799, -1351910, -167767, -130111, -130111], [-254070521, -242020225, -6063774, 1629733, 1363528, 6063774, -2993261, 2993261]];
    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_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_336_7_bh_a(: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_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_336_7_bh_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_336_bh();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,7,sign)));
    Vf := Kernel([<5,R![60368490000, 1156200318000, 7385884190100, 87230302380, -117296451, -5449878, 10275, 294, 1]>],Snew);
    return Vf;
end function;