
// Make newform 539.4.a.g in Magma, downloaded from the LMFDB on 16 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_539_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_539_4_a_g();".
// 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_539_4_a_g();".
// 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 := [1, 5, -12, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [-3, -12, 0, 1], [3, -11, -1, 1], [-4, 13, 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_539_a();"
function MakeCharacter_539_a()
    N := 539;
    order := 1;
    char_gens := [199, 442];
    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_539_a_Hecke(Kf)
    return MakeCharacter_539_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 := 4;
    raw_aps := [[0, 0, 1, 0], [-4, -1, 0, 0], [-3, -2, 1, 1], [0, 0, 0, 0], [-11, 0, 0, 0], [-8, 3, 10, 10], [-5, 5, -13, -9], [-55, 4, 15, 7], [10, 18, -2, -6], [-90, 14, 4, -8], [15, 9, -7, 25], [20, -16, 14, -32], [-27, 23, -51, -23], [66, 2, -74, 0], [25, 39, 15, -1], [150, -8, -4, 10], [-4, -3, -124, 24], [14, -29, -24, -120], [364, -62, 120, 124], [480, -26, 92, 96], [-449, 47, -217, -61], [-320, -6, 28, -114], [-87, 76, -25, 43], [770, 48, -44, 104], [174, -154, 176, -32], [-18, -201, 172, -100], [-713, 9, 93, -71], [18, -36, -102, 226], [-868, -94, 174, -34], [-180, 124, -166, -102], [-80, 156, -164, -8], [-813, 18, 405, -107], [-944, -36, -74, 64], [-481, 72, -303, -215], [-1484, -240, 142, 262], [884, -294, 468, 122], [-73, 210, -289, 127], [1388, 130, -104, -488], [2406, -176, 218, -34], [1470, 357, 384, -256], [896, -80, 156, -132], [1969, -68, 93, -311], [1850, -336, -246, -290], [-1526, 168, -708, -552], [-818, -80, -532, -12], [-689, -79, 45, 697], [50, -356, -198, -286], [-279, -211, -285, 271], [-2421, -350, -187, 245], [-587, 40, -407, -307], [-756, -390, 150, -250], [-1172, 408, -520, 404], [-2243, -241, -19, 441], [-1136, 231, -440, -212], [-1278, -20, 1132, 168], [-220, 432, -156, -404], [-459, 72, 161, 5], [-2502, -62, 198, -2], [-3872, -432, -270, 494], [432, -406, 58, 702], [-2489, 414, -983, -391], [-4258, -21, -536, 120], [-2641, -190, 1233, -607], [-1475, 261, 551, -813], [-1044, 100, -342, 1122], [3546, -448, 332, 916], [276, -296, -456, -820], [1236, 830, 1650, 10], [1010, 466, -650, -292], [1184, -17, 966, 1270], [-2882, 784, -608, -548], [976, 46, -232, -398], [1707, 1107, -499, -475], [3390, 612, 28, -72], [1600, 322, -292, 928], [3905, -981, 1235, 1535], [-3296, 44, 54, -2002], [-3445, 898, -829, 571], [508, 1024, -1878, 850], [-959, -413, 373, -151], [-8388, -369, -992, -688], [-6454, 640, 16, -694], [-1324, 1190, 644, 1218], [-4858, 1382, -8, 152], [-1390, 406, -178, -2670], [-6708, -440, -2352, -884], [2720, -1688, -994, -1248], [-734, -1940, 2476, 212], [1710, -1539, 1740, 2684], [48, -258, 2644, -324], [-6152, -1051, -2748, -164], [76, 726, -804, -1272], [-1914, -250, -54, 626], [-5340, -46, -2020, -1158], [-3904, 1522, 572, 628], [7242, 1110, -562, -1394], [2001, 2232, -1107, 417], [-6282, 762, -1680, -688], [-7293, -1262, -2571, -495], [-8622, -680, -1128, 1160], [14920, 184, 660, 852], [3138, -580, 800, 3412], [-6729, -804, 1913, 1537], [-8372, 442, -2702, -3086], [9184, 156, -524, -2876], [-4192, 1266, 118, -826], [13554, 501, -838, -710], [1865, -583, -831, 653], [18700, 570, 264, -24], [-15661, -269, 799, -1765], [2788, -600, 540, -1384], [670, 454, 1332, -112], [2982, 184, 1100, -726], [492, -503, -3528, -32], [962, -288, -4366, -182], [1478, 1964, -172, 2758], [-8684, 1235, -308, 2768], [12191, -1411, 1209, 873], [1084, -408, 1698, 862], [-9586, -194, 634, -1768], [-7647, -1466, 1333, -2023], [-6108, -2230, 1446, 1594], [-3528, -153, 1026, 1866], [18124, -124, -2680, -952], [2994, 1027, -942, 66], [9224, -2158, 2386, -1342], [-11076, 1568, -4158, -1770], [12193, 1239, -2269, -2057], [-4825, 849, 4129, -487], [-7498, -861, -2792, -1480], [4512, 924, 2924, 2556], [156, 2064, 3424, 340], [-1430, 2538, -4122, -1434], [7408, 972, 2206, -3678], [-2007, -1921, 2461, 4561], [18507, 2013, -2993, 2419], [-1045, 260, -181, -2789], [-6679, -2028, 4263, 179], [-15769, 1710, -1657, 2207], [-5982, 2312, 5476, 840], [12259, 2982, -5315, 1], [-13368, 1632, -3518, -3318], [12642, -2500, 4642, 6322], [13024, -1614, 2920, 3546], [-3303, -2986, 1509, -763], [-1607, -409, 7403, 655], [5168, -2221, -3102, -5302], [3241, 1259, 1861, -2887], [-1420, 307, 3156, 976], [1490, 3348, -4550, -3234], [-16512, 318, 5394, 2566], [5420, 270, 1274, -4654], [4188, 3916, -4536, -2168], [2080, -1490, -256, -7012], [14728, 2222, -2412, -3224], [-9426, 774, 3378, 3326], [4440, -2696, 1560, -4992], [-13586, 1554, 5648, 1196], [-271, -3035, 5593, 2413], [670, -865, -1272, -1736], [20640, -4016, 2420, -2820], [8646, 660, -3620, -1832], [-12872, 2178, -2424, 2066], [21692, 1057, -1352, -5704], [22886, 436, -564, -184], [35165, -1325, 6819, 2331], [-14840, -2694, -1092, 3152], [1908, 921, 1810, 6114], [-11910, -2284, 3156, 9140], [5757, -1192, -5447, -9583], [-6706, -8748, 4302, -1994], [-8497, -2786, 455, -6525], [-9684, -854, -224, 3170], [-6668, -526, -4834, -942], [3283, 1613, -1487, 6933], [-21256, -3618, 7910, -3054], [-28208, -4798, 632, -158], [-15708, 2512, 7174, -592], [-16022, -2326, -2090, 6626], [-4770, 3503, 4668, -244], [-11172, 896, 10644, 1540], [1175, 792, 1241, -2799], [-14160, -3364, 5014, -4374], [15963, -1049, 1415, 10171], [16620, -4052, 3260, -2336], [-3331, -3424, 8801, 477], [37230, 5332, -3604, -1272], [-17182, -873, -8858, -2742], [-14696, -1446, -1142, 2514], [3038, -4312, 546, -3582], [-11068, -1778, -13694, -4626], [1762, -4436, -718, -5950], [2008, -594, 7604, 3168], [-27409, -1314, -3233, 5467], [4080, 4234, -5920, 542], [15330, 2568, 6780, -6464], [-32988, 3718, -3110, -2666], [-5588, -5488, -5162, -672], [-2979, 353, -2747, 8953], [-23370, 574, 2898, 4522], [14408, 6412, -9006, 4850], [-21874, -5712, 2194, 2370], [10491, 1944, 1203, -317], [-14237, 251, -133, -1001], [12706, -2801, -4258, -13918], [-29199, -1946, 7669, 1129], [34969, 115, 199, -5297], [-23796, -5164, -7208, 5728], [17518, 4992, -2080, -14532], [-11326, 289, 6086, 2826], [-4912, -1378, -13498, 2670], [-3781, 4560, -13253, -4109], [1352, -68, -10248, 3228], [35428, -1051, -8308, 2632], [25790, 2486, 5954, 7154], [10816, -1484, 11150, 8414], [3720, -422, 7300, 9338], [-8401, -711, 8203, -6633], [17192, -3882, 13012, -2588], [51248, 56, 1838, 3882], [-49326, -4954, 848, 1208], [-6214, 422, -538, -8422], [11052, 3800, -1862, 32], [-23956, 1424, 9100, -580], [12147, -2134, 661, 14265], [4008, 2134, -12758, 1742], [-31956, 346, -934, -6874], [-35304, -5704, -11120, 3600], [-4922, 46, 10698, -8058], [-29950, -6770, -6410, 2344], [-26160, -972, -5006, -2360], [-34011, -1522, -6621, -4337], [22552, -1468, 2128, -6856], [10322, -8112, 1008, -9160], [-8418, -8417, 11458, -6146], [-3982, 2382, -4386, -3390], [-25234, 5678, -13912, -2368], [-41904, -2772, -6366, 382], [-57044, -8638, -4976, 1020], [43411, -4469, -2943, -1931], [1808, -4398, 1076, 8340], [11705, 2328, -6553, 15607], [-7999, 9769, 639, -4921], [-10834, 5128, 1068, -9850], [-25489, -2673, 2667, -6833], [-28658, 584, 786, -970], [-8483, -2559, -8277, 2627], [-5886, 1551, -494, -1534], [3100, -4792, 5856, 6576], [28288, -4244, -1240, 4032], [47106, -232, 11144, 8468], [-82909, 1831, -4277, 2623], [-17760, -2570, 10892, 4676], [-4818, -328, 14648, 14756], [16778, -2445, -7700, 13044], [-43922, -7968, 11622, -8018], [1540, 7220, -6266, 12476], [-49081, 452, 3185, -8835], [-11691, -2798, 377, -7523], [-24491, 2839, -8187, 5153], [46544, 4074, 3728, 674], [-33948, -11476, -1016, 2392], [10232, 4597, -398, -2310], [-21912, -1155, -15138, -14642], [-27544, 4756, 3462, 14362], [50706, 11341, -2318, -5094], [23214, 3152, -9164, -24118], [9312, 10442, -17138, 2938], [-17588, 3022, -14988, -3586], [19902, -1738, 11476, 14812], [-4053, 1426, 411, 4099], [25872, -8406, -2316, -6708], [-26676, -2310, 7082, -1422], [64004, 14832, -4176, -5244], [10071, -11609, 21975, 5499], [-33363, 5389, -14577, -12453], [-21188, -7740, 1640, 17580], [8438, 6920, 5292, 12936], [-48922, -6628, -4056, -5080], [41885, -5048, 2139, -9777], [-22824, -8620, 11496, 4280], [-9312, 322, 5404, -13608], [-35964, 1542, -10052, -9596], [-23880, -643, 16118, 16366], [-42647, -7192, 9407, 11579], [57058, -12626, -8066, -20394], [40556, 1160, -1654, 2288], [28696, 5204, 1050, 12314], [34311, -6755, -683, 15809], [47253, -5851, -5007, -12763], [43734, 560, 21300, 1310], [-26120, 1257, 8244, 1476], [9964, -4270, 11466, 7522], [31233, 1892, -9185, 15295], [-15528, -6256, 11530, 6474], [-25474, 2893, 15292, 12924], [32025, 6505, 20407, 4039], [-49287, 14692, -15611, -1287], [43967, -5920, -295, 17997], [15091, 14564, 2461, 10153], [33271, 10405, -12461, -6593], [-44716, -3250, 5342, 1830], [20220, -7162, 752, 11250], [36692, -438, 15944, 18696], [-2844, -454, 21984, 10172], [-50848, -11728, 2262, 1332], [-23564, 5888, 9768, -5400], [-121763, -6634, -2247, 5045], [848, 14028, -3176, -6808], [4612, 11812, -21554, -1122], [-53120, -5568, 1184, 3836], [-35580, -7724, 18402, 12454], [-6156, 15914, -12914, -9066], [23712, 17328, -220, -736], [25596, -3134, -11224, -15202], [-50031, 7717, -10623, 5517], [20014, -16511, 19862, 1486], [336, 3060, 20224, -13056], [12824, 2850, 14742, -8530], [25552, -13134, 22066, -2126], [-74835, 7914, -19909, -11141], [-12626, -10396, 12604, -18792], [-16458, -7585, -21280, -10456], [-33186, -10632, 10310, 5478], [-106730, 9764, -3200, 3208], [-6932, -15280, 18562, 12366], [5720, 2238, -7628, 4080], [65064, -12759, 25180, 8684], [-2672, 4618, -9872, -6482], [-53348, -1634, -10690, -4442], [-61062, 2540, 12104, -15260], [-5516, -9980, 4894, -9084], [-19314, 368, -4894, 3898], [35163, -6972, -1795, 9741], [-25364, 18514, -18236, -9402], [91106, 4411, 14526, 1434], [-23896, 8508, 25250, 24400], [51487, -7501, 35843, 23823], [29558, 6952, 15536, 11836], [42560, -9878, 15088, -2648], [47778, 3448, 6268, 7000], [76214, 2796, 6316, -5818], [-122186, -6451, 1488, 992], [23094, -6192, -29770, -1722], [38756, 824, 4474, 16210], [-111048, -16988, -948, 3508], [-94583, 18620, -32971, -11035], [69864, 11026, 26332, 6580], [35332, 14656, 7900, 14788], [128115, -6360, -12909, -16829], [-27593, -3596, -20743, -27631], [48488, 3592, -17874, 28240], [88442, 12912, -21308, 9152], [-29660, -10506, -11908, -4420], [-40204, 5104, 16990, 26082], [131487, -1725, -10821, 4903], [8699, 12345, -21743, -15091], [3459, -910, 15053, 22721], [-45478, -5256, 5400, -19816], [-56792, 13424, -25288, -10184], [-28904, 19240, -3462, 9902], [14619, 8147, 28399, -13205], [-140552, 476, 10104, 4408], [-7612, -3646, 3804, 23494], [-5918, 591, 9398, -19710], [146798, 704, 19956, 2762], [-13510, -11947, 23396, 15964], [54068, -11080, -9152, -19164], [8774, 10412, 8456, -7800], [77756, -794, 15000, -13012], [-41474, -1043, -1526, 2214], [-43600, 2740, 26896, 1864], [41466, -6440, -30816, -14160], [-8636, -1332, -45116, -2868], [72010, 18128, -6344, 11874], [113240, -10951, 33540, 21172], [79716, -13442, 31228, 13658], [33125, -5015, 2809, -11739], [94463, 10557, -12497, 13923], [35471, -2675, 27783, 4819], [-29833, 6974, -18017, -19965], [154234, 1292, -1944, 7302], [121692, -934, -17424, -6490], [-146254, -13796, 6584, 6700], [-124305, -20608, -6711, 12985], [12757, 17945, 46311, 12731], [37054, 6104, 8502, 6378], [-100483, 14386, -551, 22937], [130606, -1044, -5306, 1198], [91587, -4043, 14265, -1123], [100094, 5376, 3996, -18526], [-4413, 12436, 8419, -3449], [-64012, 14794, -26800, -4692], [-34142, 5653, 4770, 31298], [86300, -14220, 21172, 29828], [80064, -11602, 41594, 18734], [10368, 8998, 50712, 4616], [13788, -4448, 4490, 29854], [89692, -7970, 25688, 12016], [-105156, -16070, -3962, 5066], [65152, 30093, -3750, -14526], [-115134, 10144, -20976, -8304], [41564, 23752, -13140, 1980], [-21356, 32944, -29942, 2782], [154780, -3373, -8866, -8786], [-9490, 10124, -29374, -402], [90802, 5936, 37020, 540], [-10038, 12084, -17564, 15800], [-16268, -10057, -5288, 28976], [30540, -773, -28704, -30360], [111045, 5033, -4427, -29679], [20082, 622, -19032, -41852], [-60824, 4772, -1172, -34592], [-7824, 21998, -23928, 13634], [-9872, -6098, 15690, -9598], [25431, -22770, 46671, 2087], [-103356, 2744, -5052, 11580], [-102511, 1507, -16101, -36217], [190648, 1040, -3458, -9998], [-66084, -19072, 33628, -14068], [-73016, -24240, 4366, -2578], [60274, -10317, 38980, 6812], [-50764, -16098, 31024, -24728], [-37979, -12098, 21363, -15401], [46628, -26504, 32834, 4790], [-32253, 6624, 2883, -9113], [89416, 17210, 19092, 1928], [75844, 26382, 4230, -8094], [15550, -24871, 1514, 14806], [14764, -2294, -3900, -724]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_539_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_539_4_a_g();".
// 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_539_4_a_g(:prec:=4)
    chi := MakeCharacter_539_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_539_4_a_g();".
// 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_539_4_a_g( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_539_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![112, -40, -27, 2, 1]>,<3,R![-1016, -436, 6, 14, 1]>],Snew);
    return Vf;
end function;