
// Make newform 847.4.a.e in Magma, downloaded from the LMFDB on 28 January 2025.
// To make the character of type GrpDrchElt, type "MakeCharacter_847_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_847_4_a_e();".
// 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_847_4_a_e();".
// 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 := [15, 5, -10, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 2, 0, 0], [-5, -1, 1, 0], [3, -7, -1, 1]];
    Rf_basisdens := [1, 1, 1, 1];
    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_847_a();"
function MakeCharacter_847_a()
    N := 847;
    order := 1;
    char_gens := [122, 365];
    v := [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_847_a_Hecke(Kf)
    return MakeCharacter_847_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]];
    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 := 4;
    raw_aps := [[1, 0, -1, 0], [-3, 0, 0, 1], [-5, 1, -2, -1], [7, 0, 0, 0], [0, 0, 0, 0], [32, 3, -10, 4], [18, 1, 10, 6], [54, -26, -12, -2], [59, -21, -10, 1], [42, -30, -40, -12], [-87, -32, -32, 33], [47, 49, 2, -1], [-22, 53, -18, 14], [116, 16, -12, -36], [16, -1, -14, 30], [-214, 14, -68, 4], [-285, -42, 12, 19], [38, 99, 6, 42], [-371, -7, -70, -61], [585, -7, -14, 11], [382, 51, -62, -2], [242, 26, 76, -54], [456, 48, 204, -20], [317, 141, 166, -99], [-521, -59, 30, -57], [12, -1, -294, -32], [-564, 151, -30, 10], [406, -282, -336, 14], [-688, 132, -12, -66], [849, 101, 242, -63], [638, -182, 164, 202], [-270, 144, -180, -130], [129, -33, 174, 361], [-58, 118, 200, 102], [-350, 20, 300, -40], [-1182, 286, 228, 58], [-1213, 145, 398, 127], [-2500, 38, -244, -188], [466, -98, 52, 206], [868, -257, 310, 48], [1289, 227, -202, -253], [-1839, 737, 470, 237], [365, -293, 366, -117], [-722, 88, 372, -312], [678, 392, -456, 44], [696, 243, -158, -82], [494, 86, 624, 22], [521, -258, 788, 133], [1180, -158, 700, -68], [-195, 353, -562, 89], [1590, 158, 360, 636], [72, -392, 308, -88], [-1356, -245, -54, 44], [395, 544, -392, 399], [-718, -336, -824, -216], [122, -238, -104, 606], [-314, -128, -976, -320], [428, 226, -480, 688], [-76, -310, 352, -18], [712, -296, -420, -22], [4096, 494, -268, 448], [-2338, 207, 58, 146], [1272, 622, 280, -320], [-4480, -945, -222, 462], [-2899, 261, -1186, -1195], [-67, 181, 754, -71], [-2281, -23, 1106, 629], [246, -42, 0, -876], [802, 914, 1332, -874], [-18, 7, -1178, 154], [1821, 101, 870, -75], [-3492, -648, -548, 1088], [-1947, 1550, 772, -439], [-3156, -246, -1724, -1250], [2379, -865, 934, 1021], [10161, -1240, -976, -143], [-875, 489, 1442, 417], [-486, -354, 900, -952], [-782, 20, 992, -296], [-2262, 771, 1658, 330], [-1872, 829, 166, 374], [-3058, -706, 1028, -128], [-2796, 1976, 848, 632], [-7805, -303, -1250, -581], [-4718, 744, 2524, 50], [1769, -1989, 94, 787], [4995, 1561, 1434, 23], [-1104, -378, 2620, -74], [106, 431, 2678, 390], [4141, 801, -2910, 175], [2443, 1852, 1448, -233], [-3320, -582, -864, -1276], [-3141, -657, 3182, -271], [-4134, 574, 1200, -826], [276, -1358, 116, -980], [-4260, 98, 1776, 56], [-8411, 1469, 2686, -367], [1491, -211, -794, -1077], [6268, -434, -1176, -348], [-1652, -1822, 720, 14], [1232, -500, -4640, -76], [-6762, 3360, 1276, 1548], [-2636, -2692, -1104, 1112], [6270, 2202, 2016, -1684], [-1612, -660, -2472, 1640], [8635, -2663, -2306, -1725], [2604, 995, -5118, -10], [3182, 2475, 2446, -1850], [4864, 1930, -124, 2836], [3048, -2093, -2606, -344], [6834, -406, 3940, 94], [8824, -2620, -3728, -1042], [-6622, -1014, -2324, -1096], [-6799, -878, -3132, -1775], [6659, 953, -614, -1283], [13899, 1845, -822, -1137], [-269, -3500, -1368, 1047], [-13029, -2806, 1788, -1185], [-11077, 2975, 1886, 1195], [2882, -642, 2460, 2822], [-3961, 2921, 2430, 1891], [23960, 16, -1260, 210], [-9810, 3313, 634, -730], [4564, -912, -3304, -2792], [-12609, -1998, 2004, -1833], [-648, 1204, 5184, -362], [9931, -329, -3274, -493], [-2379, 56, 2584, -203], [1165, -1476, -1952, 2341], [-5436, 2161, 5830, 516], [4218, 2814, 4884, 2154], [2608, 844, 4208, -2384], [14305, 1289, -1830, 179], [7174, -3132, 3328, 144], [7932, -3415, -774, 1976], [6380, -2471, 2274, 432], [18450, 1616, 816, 2716], [-5286, -34, -1088, 1450], [3083, 3497, 5862, -1593], [12306, 1604, -792, -3524], [-10202, -2188, -4728, -2214], [-4842, 928, 3096, 2404], [8399, -427, -2430, 2593], [1168, -1540, 2724, -1552], [15647, -1095, 2606, 1883], [2085, 2760, 4224, 3325], [-9414, -259, 2686, 2794], [-852, -1719, 1030, -664], [14223, -4744, -920, -2685], [-14052, 632, 4384, 1356], [-1714, 134, -4260, 232], [8519, -3803, -794, 3239], [-25404, -2124, -3704, 2600], [-2950, -4796, -2496, -1262], [-7000, -2566, 6348, -704], [21776, -454, 1684, 2724], [6396, 1280, -2604, -3796], [9010, -6726, -2788, 332], [19552, 1117, 794, -3740], [-2474, -3261, -7814, -326], [9557, 4439, -2850, 3599], [34740, 1214, 484, -1326], [-4434, 6730, 4120, 710], [-16555, -146, -84, 2509], [-12947, 6373, 6418, 3165], [2733, 46, 1596, -2967], [-10496, -1006, 2628, -1420], [-12884, -7201, 1514, 1392], [-11696, -6082, -1040, 4590], [-6479, -1663, 2446, -507], [9828, -968, -1836, -1192], [3322, 1494, -860, 1472], [-2822, 5986, 2092, 3818], [-11156, 2832, 316, -4786], [-20601, -520, 1376, 71], [-23277, 2509, -554, -3341], [4914, -2422, -4904, -4162], [-15077, 3685, 3538, 1939], [19886, -28, 1420, 102], [-6658, -2519, -2718, 2206], [-3668, 360, 9248, -4588], [9408, -372, -1172, -4476], [36529, -2507, -1206, -427], [16004, -657, 1206, 2556], [-17711, -5377, 4782, -4057], [1582, -5488, -5416, 304], [-15260, 1158, 5584, 550], [-5964, 8655, 3522, 3942], [-960, -5828, 3880, -4014], [13362, 10430, 2132, -3474], [13663, -4603, -4938, 2711], [11122, -4870, 3712, 2050], [-15947, 945, -3062, -981], [-4294, -838, 4220, -2408], [56866, 1498, 3292, 254], [7257, 4105, 8646, -4399], [-40344, 300, 1216, 3722], [18019, 6069, 9802, -3629], [4562, -2189, -12710, -3238], [-29334, -740, -5244, -7094], [7374, 4068, 8648, 4380], [22788, -2404, -6388, 2188], [-5922, -2940, -11888, -5744], [-9426, -157, -2882, -1130], [25597, -450, 3332, -7027], [-8157, -3495, -2754, -3209], [5671, 1708, -5696, 2583], [-974, 6266, 824, 5314], [22612, 2254, 11420, 1490], [-32292, -511, -1306, -2054], [-18940, -3388, 1400, -2578], [19481, -1035, -2722, 7381], [2767, -2659, -9230, -7671], [18948, -2723, -2098, -3398], [3360, -5646, -12108, -4628], [589, 537, 5454, 429], [63992, -5564, -2284, -548], [57398, -5349, -7130, -4402], [-13940, 378, -8500, -4032], [-31453, 735, 1398, 2291], [-32540, 1208, 7052, 362], [-10982, -10944, -9728, 3370], [-20743, 5167, 12046, -4351], [-38331, 4495, 1198, -1341], [-25860, -6590, -528, -5644], [-14898, -1290, -7876, 5408], [-53770, 6438, -1132, -48], [7424, -2544, 1192, 8264], [9212, 7146, 9192, -5888], [39020, 3108, 8468, -5268], [-30919, 5975, -6690, -5563], [-22694, 2084, 4896, 9286], [-11168, -7920, -476, -4424], [-12260, -1698, -3172, 11554], [-48125, -1128, -832, 5959], [49982, -10076, -7328, -7154], [255, 6845, 14958, -3105], [36194, -13940, -14960, -112], [17631, -5533, 2238, -6255], [24963, -1330, -15196, -3881], [-2361, -1421, 2574, -12719], [-7422, 3898, 224, -10566], [10748, -7627, -14330, -946], [-14751, -10633, -154, 3637], [-18642, -2283, -5982, -2310], [-25910, 7554, -1264, -9618], [-5584, 14609, 14702, 690], [-22716, 1385, -17546, -1022], [10166, 1350, 2092, -946], [50354, -3966, -1820, 6270], [-13528, -7930, -5060, 794], [-8336, 1249, 14934, 4596], [51329, -1639, -8614, -2037], [2246, 9808, -8512, -2092], [18228, 8017, -8786, -1260], [-41906, 498, -928, 4790], [51438, 3142, 17372, -2040], [17466, 3270, 12584, 5970], [41371, -2387, -1034, -353], [-38468, 9747, 19602, 3900], [58108, 1168, -11240, 2328], [6712, 5940, -476, -12716], [-10362, -2765, 8286, -7038], [39992, 3735, 15410, 8180], [41838, 9500, 9192, 3836], [34652, 4355, 27058, 5118], [30595, 9841, -5566, -4157], [9007, 5573, -12906, 10159], [-21654, 730, -604, -1914], [44800, -4132, -4500, -594], [53179, -18967, -12258, -4393], [-40921, -1445, 686, -1375], [16286, -2202, 20164, 6336], [-12356, 2324, 15492, -3852], [62302, -12277, -14318, -9482], [69781, -446, -7556, -5231], [-28421, 17589, 1106, 3457], [51102, -9180, -11120, -6052], [51038, 10040, -744, 6920], [22536, 5072, -9300, 5788], [-32692, -1872, -7364, -5572], [25517, -15087, 690, 239], [-48302, 10692, 10376, 2754], [4014, 14545, -2170, -6906], [-82034, 1190, -10400, -4810], [-25431, -2107, 9690, -1693], [-9558, 7930, 28204, 2896], [19466, 6800, 19164, -4840], [-11165, -11716, -352, -3773], [5584, 9079, -2110, 2128], [-14491, -2357, -25298, -1663], [22812, -17121, -4126, 2694], [82108, -6564, -12496, -3458], [5292, -13932, 7456, 1080], [-36350, 6712, -5492, -9192], [31784, 6377, 1898, 17512], [19363, 7846, -1372, -10073], [-1167, -719, -13226, 6013], [5958, 19162, 4168, 3798], [80544, -168, -10716, 148], [-47048, 4633, -6330, -1436], [-11612, 15716, -10796, -4894], [41582, 7502, -6184, -4626], [23460, -1902, 17052, 6452], [-10569, 9767, 29246, 9737], [9379, 8613, 994, 8759], [28143, 421, 1162, 4029], [-17417, -4387, 4614, -14429], [-13712, 16820, 6728, -7448], [-23100, 6414, -912, -1426], [-5050, 7378, 9664, 19986], [35122, 536, -27968, 3236], [32646, -8862, -8492, -2564], [-47979, -2817, 1662, -4889], [-73454, 7250, -7480, 650], [34418, -2317, -11306, 12386], [29597, -634, -22636, -1779], [28938, -9774, -25356, -5882], [83366, -3834, -2468, -3200], [-99454, 4330, 7036, 1624], [10556, 6962, 10064, -7412], [17785, 729, -29966, 4121], [-7866, 7199, 28262, -2974], [-42343, -17945, 12086, 151], [22742, -9052, 25096, 5064], [12825, 14837, 35550, 6433], [23207, 10059, 8118, 2265], [23145, -4574, -3492, -13959], [62302, 16314, 19484, -1114], [-11208, -10508, -33864, 8526], [-51816, 11726, -5832, 6682], [-79314, 7062, 5372, -1456], [17502, -4774, -24728, 146], [-23322, 22630, 11848, -12546], [12870, 10910, -832, 4138], [115555, -2452, 728, -6497], [-11439, -9041, 6958, -10499], [11626, -3347, -12910, -4090], [48821, 12197, 334, 8917], [47444, 11022, 28180, -6992], [-58491, -4635, -32998, -4471], [-34357, 16917, 58, 9047], [49084, -16697, -31434, -3920], [122539, -1759, 8634, 2261], [73491, -17817, -234, -8749], [26016, -21308, -8148, -5900], [73769, 7417, 31710, 2989], [-78485, -15481, -25314, 2261], [544, 8408, 14516, 2848], [-11651, 19721, -4570, -8367], [14368, -96, -8284, 1772], [22547, -3703, -10518, 18223], [-95254, -12780, 21056, -11284], [63018, -5508, -4028, -19070], [44956, 11014, 26660, -5554], [-79220, -9821, -8290, 5964], [-67101, 9856, -6128, -5981], [12434, 2764, -23664, -1754], [10552, 5526, -6872, -4138], [-70383, 143, -22434, 1015], [-131742, -13500, -12008, 6528], [63394, 2261, 27730, 6022], [27527, 16153, 10346, -12839], [30368, 7440, -17576, -5928], [-99227, -3074, -11660, 21389], [27617, 23023, -4346, 20025], [-73192, -14109, 3054, -3124], [33252, -13308, -13324, 10236], [-21148, -16530, -17128, 9146], [77549, -29707, -12662, 2827], [20980, -8499, -19370, -12582], [44436, 9032, -10792, 23692], [-56096, 23182, 19512, 27442], [44568, -13348, 28540, 136], [-5315, 25059, 2822, -11479], [86596, 18575, 18794, -2066], [36962, -370, -19552, -13854], [15106, -22159, -48282, -9226], [-67200, 27131, 9646, 15128], [-34840, 22815, 12706, 7056], [-30094, -26706, 18636, -6552], [-773, 19921, 11922, -11401], [7290, 15602, -4064, 6002], [17430, 9044, 13948, -23352], [18772, 10092, -1664, 2824], [89273, -18550, -34084, -7435], [23608, -6256, 9904, 18376], [26591, -35603, -15794, -9141], [20510, 3066, 8604, 28950], [-45403, 1538, -16548, -5559], [21197, -1037, 758, 17317], [43633, -8919, 2478, 9213], [17253, -13975, 33034, 3347], [-66383, 7086, 28924, 1585], [-60537, -13863, -39414, -2575], [79850, 1178, 19708, 896], [58280, -762, 40484, 4484], [-25518, 9936, 50168, 6896], [-79436, 7954, 25804, 6660], [-93568, 2264, 11204, -3082], [29782, -10973, 25046, -15126], [43081, -24147, 12458, 24841], [89208, -14648, 3312, -776], [-21270, -12592, 10248, -472], [165440, -27217, -12350, -9436], [81970, -24134, -6344, -7594], [19914, -15156, -21368, -11028], [17000, -2506, -37520, -3410], [-16476, 36137, 33710, -2830], [-155911, -13584, 6656, -11171], [-2408, 2305, -44686, 15820], [-24064, -16420, 17896, -6010], [7964, -6676, -13472, -16584], [-55880, 4292, 42976, -8896], [49506, 17098, 3152, 1824], [52047, 4361, 19806, 20683], [-74112, -15072, -8072, 1216], [133641, -10792, 27200, 21297], [115866, -6340, -824, -712], [29090, 10498, 10652, 20046], [-130113, 10331, 12598, -8009], [-111520, -4603, 20830, 1392], [41524, 22190, 45972, -16840], [77874, 9076, -45364, 2694], [-3002, 4840, -512, 22420], [71953, 26385, -418, 19221], [5763, 10635, -22170, 20565], [162152, -9904, -19784, -9174], [62601, -22228, 29120, -203], [-63166, 27444, 10860, 11618]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_847_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_847_4_a_e();".
// 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_847_4_a_e(:prec:=4)
    chi := MakeCharacter_847_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 4));
    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_847_4_a_e();".
// 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_847_4_a_e( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_847_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![66, 56, -19, -4, 1]>],Snew);
    return Vf;
end function;