
// Make newform 336.7.bh.b in Magma, downloaded from the LMFDB on 24 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_b();".
// 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_b();".
// 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 := [38787984, 3107772, 1563109, -92833, 38792, -787, 212, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0, 0, 0], [-22182278913510768, 97319550794346895, -780366646056751, 65749585575908, -4245938445433, 303603349712, -18232571539, 1585013359], [-22182278913510768, 2109023702500351, -780366646056751, 65749585575908, -4245938445433, 303603349712, -18232571539, 1585013359], [312743079434231442, -316744918301136878, -18697714244203045, -1133698910951701, -230507120454535, -15146056834333, -1233059081317, 13858370275], [129923180639535, 6368992827120, 710067329620, 1220086899616, -8215312820, 5777520100, 62465284, 32076220], [-1772851718373789258, -336404097761466014, -20889480107476681, -4947341926250533, -205148902375939, -32979536564113, -799568126245, -85151344841], [-44383880102164944, -88533841034081394, 8199002660156988, 212218491969083, 19286324891880, 4283641236335, 456963608136, 41883584669], [177777443566348128, -714157673331781651, 29812237950379993, -21344721106790528, 353235182738359, -138652672166732, -1518878968019, -661077962011]];
    Rf_basisdens := [1, 23802631772961636, 23802631772961636, 11901315886480818, 11465622241311, 11901315886480818, 3967105295493606, 23802631772961636];
    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, 0, -1, 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, 0, 9, 0, 0, 0, 0, 0], [-7, 0, -3, 1, 0, 0, -1, 0], [-75, -2, 37, 0, -3, -1, -2, 0], [267, 2, 267, -5, 2, -1, 5, 4], [862, 4, 1729, 6, 2, 6, 5, 5], [652, -37, -574, -39, 37, 24, -39, 15], [4087, -97, 2037, 1, -194, -14, 13, -14], [-29, -311, -3808, 4, 0, -25, 29, 25], [4218, 0, 61, -122, -25, 122, -61, 61], [-3090, -90, 3328, -119, 90, 282, -119, -163], [-202, 790, 11453, -71, 0, -273, 202, 273], [6232, 260, 12900, -346, 130, -346, 436, 436], [55439, 0, 257, 397, 1445, -397, -257, 257], [-14171, 247, -7292, -56, 494, -357, 413, -357], [-3184, -1028, -3184, 1306, -1028, 149, -1306, -1157], [-150135, 95, 151215, -540, -95, 377, -540, 163], [-65932, -764, -33220, -52, -1528, -456, 508, -456], [-95940, -4167, -95940, -842, -4167, 167, 842, 1009], [-27171, 0, 1706, -187, -827, 187, -1706, 1706], [74272, -2284, -75354, 541, 2284, -887, 541, 346], [-653, 13248, -140140, 484, 0, -169, 653, 169], [78670, -9514, 162183, -1609, -4757, -1609, 4843, 4843], [-295108, -1046, -147542, 126, -2092, -102, -24, -102], [134387, 2492, 270401, 2652, 1246, 2652, 1627, 1627], [25406, 4878, -34394, 4494, -4878, -1150, 4494, -3344], [-137687, -15415, -68029, -551, -30830, 2180, -1629, 2180], [-7009, 211, 219834, 221, 0, -6788, 7009, 6788], [-448902, -14134, -448902, 8073, -14134, 6641, -8073, -1432], [210741, 0, -752, 11611, -26137, -11611, 752, -752], [-747263, 0, -7674, 8391, 19686, -8391, 7674, -7674], [1970596, 13026, 989604, 5273, 26052, 3339, -8612, 3339], [-1111062, 24664, -1111062, 6842, 24664, 16300, -6842, 9458], [-123721, -26994, -231803, -3409, -13497, -3409, 15639, 15639], [-10918, -54922, 332716, -2386, 0, -13304, 10918, 13304], [-1690546, 9673, -1690546, 16049, 9673, 4433, -16049, -11616], [-2090520, -14900, 2077336, 6592, 14900, -6568, 6592, -24], [13200, 6882, 2222044, -13866, 0, -666, -13200, 666], [-110233, 6250, -234750, 7807, 3125, 7807, -14284, -14284], [-4240867, -31399, -2122600, -11254, -62798, 6921, 4333, 6921], [-764288, -36913, -764288, 10931, -36913, 35368, -10931, 24437], [-302370, 152808, -570339, -24420, 76404, -24420, 34401, 34401], [-54285, 79711, -2142752, 22818, 0, -31467, 54285, 31467], [5358755, -126172, 5358755, 44360, -126172, -6978, -44360, -51338], [-1378977, 0, 24058, -35531, 5081, 35531, -24058, 24058], [-1278562, -38076, 1299382, -10410, 38076, 33420, -10410, -23010], [885602, 0, 16226, 10426, -169618, -10426, -16226, 16226], [1454872, -79486, 2981463, -42520, -39743, -42520, 71719, 71719], [-1379249, -78546, 1485215, -52983, 78546, 109885, -52983, -56902], [-4813894, 58904, -2414883, 51637, 117808, -67509, 15872, -67509], [-13943, 209867, -622266, 15712, 0, 1769, 13943, -1769], [-7170600, 0, -24598, 16706, 13204, -16706, 24598, -24598], [3673295, -52390, -3615261, -29017, 52390, 97303, -29017, -68286], [-283752, 261294, -549057, -17889, 130647, -17889, 18447, 18447], [-14084365, 21303, -7057482, -98330, 42606, 67731, 30599, 67731], [-301704, -117628, -301704, 125272, -117628, 64586, -125272, -60686], [-2044502, 219185, 1985780, 29361, -219185, -69333, 29361, 39972], [14910862, -49991, 7449055, 56819, -99982, -69571, 12752, -69571], [-1501624, 63744, -1501624, -46673, 63744, -62953, 46673, -16280], [-6283167, 0, 2680, 98467, 224993, -98467, -2680, 2680], [-3208674, 279565, 3188190, 10242, -279565, 16253, 10242, -26495], [10838212, 14534, 21732057, 8648, 7267, 8648, 55633, 55633], [10540827, 83866, 21092495, -139639, 41933, -139639, 10841, 10841], [-921636, 116972, 683328, 119154, -116972, -211240, 119154, 92086], [22847558, 126530, 11452795, 17152, 253060, 40880, -58032, 40880], [-3006, 100685, -23254891, -34557, 0, -37563, 3006, 37563], [172283, -485437, -8559625, -119949, 0, 52334, -172283, -52334], [31239775, 0, -5678, -262882, 51064, 262882, 5678, -5678], [19738376, -227671, 19738376, -335709, -227671, -74142, 335709, 261567], [-8983792, -438912, -18045920, -175018, -219456, -175018, -78336, -78336], [-6834496, 39225, 7208830, -187167, -39225, 125432, -187167, 61735], [1628, 688282, 1704438, -17920, 0, -16292, -1628, 16292], [2690308, 142644, -2574998, -57655, -142644, 16356, -57655, 41299], [207118, 398124, -3723579, 116839, 0, 323957, -207118, -323957], [-6692335, 0, 8657, -256949, 1495439, 256949, -8657, 8657], [-7325639, 409323, -3560598, -75952, 818646, 280395, -204443, 280395], [-11825808, -766067, -11825808, 178265, -766067, 99256, -178265, -79009], [-513106, 530498, -262483, 39373, 1060996, -51233, 11860, -51233], [-380341, 384347, -49684562, -82852, 0, -463193, 380341, 463193], [-4140045, -502612, 4152741, -6348, 502612, -287564, -6348, 293912], [11266850, 155668, 22796370, -42482, 77834, -42482, 262670, 262670], [-97370914, 0, -122559, 190374, -276762, -190374, 122559, -122559], [-74268098, -573817, -74268098, 104009, -573817, -21692, -104009, -125701], [-4354470, 2738956, -8932807, 188852, 1369478, 188852, -223867, -223867], [-48184130, 98557, -23923093, 967103, 197114, -629159, -337944, -629159], [-129237, 1789765, -47659796, -90513, 0, -219750, 129237, 219750], [-22445019, 0, 509710, -274457, -1754335, 274457, -509710, 509710], [31852, -29048, 27142873, 407016, 0, 438868, -31852, -438868], [-4150846, 585340, -8717628, 469648, 292670, 469648, -415936, -415936], [-8377979, 0, -218649, 889311, -865113, -889311, 218649, -218649], [-38218391, -465553, -18940672, 152016, -931106, 185031, -337047, 185031], [17905396, 1536891, -18950302, 522453, -1536891, -283808, 522453, -238645], [-31214742, 1314100, -31214742, -836739, 1314100, 79264, 836739, 916003], [-24413403, 0, 487499, -290062, 785410, 290062, -487499, 487499], [388659, 1068341, -42690915, 63313, 0, 451972, -388659, -451972], [-29576754, -1752096, -59341764, 612516, -876048, 612516, -188256, -188256], [-7907275, -1168524, -4042755, -669413, -2337048, 491178, 178235, 491178], [-32550410, 1542920, 31686566, 431922, -1542920, 96282, 431922, -528204], [-108132659, 589917, -53757127, 40815, 1179834, 577590, -618405, 577590], [829560, 361602, -25139359, 461511, 0, 1291071, -829560, -1291071], [-2319698, 0, 552036, -932394, -998856, 932394, -552036, 552036], [-203619040, 747889, -203619040, -128321, 747889, 109481, 128321, 237802], [-28751937, 592229, 28081773, 335082, -592229, -1455949, 335082, 1120867], [-951516, 1872998, -6347848, -869748, 0, -1821264, 951516, 1821264], [29482962, -1671981, 29482962, -944294, -1671981, -1228549, 944294, -284255], [-2144711, 704166, 1715303, 214704, -704166, 965208, 214704, -1179912], [-5764211, 5914232, -12052671, 147098, 2957116, 147098, -524249, -524249], [-104433622, 2693848, -52043300, 210198, 5387696, 136824, -347022, 136824], [59619566, 1663598, 59619566, 1123608, 1663598, 195144, -1123608, -928464], [-14766967, 206604, -29696078, 676508, 103302, 676508, -162144, -162144], [-202250357, -1439994, -100792706, 680364, -2879988, -15419, -664945, -15419], [-87416690, 142800, -87416690, -951504, 142800, -828882, 951504, 122622], [-32357010, 0, 387934, -466466, 7497362, 466466, -387934, 387934], [54917164, 25333, -53613252, -651956, -25333, 1380551, -651956, -728595], [-40199258, 0, -1410009, 1117242, -2375877, -1117242, 1410009, -1410009], [-155310102, 888511, -155310102, 803759, 888511, 682792, -803759, -120967], [36468659, 3672258, 72341053, 46721, 1836129, 46721, -596265, -596265], [52605476, -2740490, -55233812, 1314168, 2740490, 123270, 1314168, -1437438], [970478, 2281487, 227302393, 64331, 0, 1034809, -970478, -1034809], [-184543449, 0, -327538, -504163, -1037891, 504163, 327538, -327538], [59780808, 304942, -56418938, -1680935, -304942, 1041437, -1680935, 639498], [-212081609, 0, -1176392, 123638, -5147606, -123638, 1176392, -1176392], [-304387900, -2353693, -153167989, -2346967, -4707386, 398889, 1948078, 398889], [41817013, -2999389, 41817013, 193586, -2999389, 36469, -193586, -157117], [-106269701, 3399393, -53369001, -1466883, 6798786, 998582, 468301, 998582], [79694247, 0, 148039, 1589563, 2360438, -1589563, -148039, 148039], [-589736, -4092944, -118210334, 1089312, 0, 499576, 589736, -499576], [465101710, -2389138, 232852898, 90972, -4778276, 513114, -604086, 513114], [-18708653, 9727924, -36873572, -1614225, 4863962, -1614225, 543734, 543734], [214274714, 1853896, 105548179, -3530137, 3707792, 351781, 3178356, 351781], [-422809402, 38019, -422809402, -869502, 38019, -122391, 869502, 747111], [-369104280, 0, -563590, 686510, -3156248, -686510, 563590, -563590], [-763947, -400780, -201739848, 692734, 0, -71213, 763947, 71213], [-328021244, 0, 1770430, -1454368, 6607312, 1454368, -1770430, 1770430], [-128785999, -3765651, -65397276, -1125566, -7531302, -882987, 2008553, -882987], [-15859493, -255496, -34738980, -257842, -127748, -257842, -3019994, -3019994], [223837258, -1112158, -221210278, -1313490, 1112158, 1989306, -1313490, -675816], [-143003124, 1880542, 137081592, 2960766, -1880542, -2697664, 2960766, -263102], [212691745, -9034852, 426852573, -488281, -4517426, -488281, 1469083, 1469083], [263179478, -7607453, 263179478, 2993161, -7607453, -303778, -2993161, -3296939], [355253772, 3210720, 711073392, -1663950, 1605360, -1663950, 565848, 565848], [1287315, 2258684, -518568025, 1294869, 0, 2582184, -1287315, -2582184], [-548500506, 2785074, -548500506, 1472362, 2785074, 2289668, -1472362, 817306], [-49106850, 0, -761017, -1926607, 4764217, 1926607, 761017, -761017], [17450290, 1191284, -18964524, 757117, -1191284, -80507, 757117, -676610], [-207371096, -9211732, -415812786, -2712430, -4605866, -2712430, -1070594, -1070594], [225460700, 7947328, 454109643, -2818772, 3973664, -2818772, 3188243, 3188243], [115696460, 573834, -111134672, -2280894, -573834, 4613756, -2280894, -2332862], [-155786954, 2017610, -76652596, 2735470, 4035220, -253708, -2481762, -253708], [2761640, 15427744, -218762010, -718516, 0, 2043124, -2761640, -2043124], [1606378, -822282, -152661666, -291158, 0, 1315220, -1606378, -1315220], [468549621, 6233814, 939545796, -3342411, 3116907, -3342411, 2446554, 2446554], [-100580191, 0, 1665749, -1222325, 5319095, 1222325, -1665749, 1665749], [234401794, 6762960, 115629924, -449434, 13525920, -2692512, 3141946, -2692512], [-50672356, -10316531, -50672356, 107324, -10316531, -3784411, -107324, -3891735], [-823713969, 0, -656284, 2707703, 1216447, -2707703, 656284, -656284], [4124413, 16020641, -16178777, -292351, 0, 3832062, -4124413, -3832062], [-219389683, 8377799, -108755356, -1824694, 16755598, 3703665, -1878971, 3703665], [14093697, 5438224, 30690524, 1634852, 2719112, 1634852, 2503130, 2503130], [-407168100, -5164304, 401557224, 2805438, 5164304, -3010511, 2805438, 205073], [4228704, -6387716, -362042318, -47604, 0, 4181100, -4228704, -4181100], [125423445, 0, 758578, -1749893, -8847145, 1749893, -758578, 758578], [-255440825, 0, 4895340, -2829045, -6882198, 2829045, -4895340, 4895340], [-681253853, -5732239, -343295914, -3162687, -11464478, -2175288, 5337975, -2175288], [-39922910, 4449112, -39922910, -3972624, 4449112, -1230552, 3972624, 2742072], [270430636, 6314751, -274348750, 1959057, -6314751, -6508580, 1959057, 4549523], [61390356, 2559526, 61390356, -4094463, 2559526, 1250236, 4094463, 5344699], [572810174, 9759190, -580161632, 3675729, -9759190, -6356863, 3675729, 2681134]];
    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_b();".
// 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_b(: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_b();".
// 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_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_336_bh();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,7,sign)));
    Vf := Kernel([<5,R![54693259182810000, -24205120650000, -9840546578700, 4356625500, 1536505749, -1767906, -41505, 42, 1]>],Snew);
    return Vf;
end function;