
// Make newform 1840.4.a.k in Magma, downloaded from the LMFDB on 08 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1840_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_1840_4_a_k();".
// 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_1840_4_a_k();".
// 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 := [108, -45, -60, -2, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 1, 0, 0], [54, -45, -5, 1], [-153, -126, -1, 2]];
    Rf_basisdens := [1, 1, 9, 9];
    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_1840_a();"
function MakeCharacter_1840_a()
    N := 1840;
    order := 1;
    char_gens := [1151, 1381, 737, 1201];
    v := [1, 1, 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_1840_a_Hecke(Kf)
    return MakeCharacter_1840_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, 0, 0], [-4, 1, 0, 0], [5, 0, 0, 0], [-1, -1, -2, 0], [-6, -1, 1, 2], [15, 4, 0, 1], [18, -2, -6, -3], [-39, -8, 5, -3], [23, 0, 0, 0], [-34, 9, 10, -5], [17, -27, -15, -8], [-96, 3, 33, 1], [28, 32, -15, -11], [32, -52, 18, 4], [-106, 16, 33, -4], [-114, 37, 7, 11], [76, 79, -7, -1], [-66, -47, 31, 0], [140, -43, -33, 7], [-68, 72, -21, -23], [-140, -44, -17, 16], [220, 92, 20, 28], [290, -13, 47, 9], [-512, -92, 30, 16], [-198, -13, 55, 30], [266, 17, -29, 13], [-54, -89, 157, -32], [-194, -187, 69, 49], [-637, 162, -81, 15], [-270, -11, -41, 47], [-784, -2, -5, -66], [38, -30, -163, 12], [643, -166, 97, 3], [292, 149, 50, 7], [-1707, 84, 55, -1], [-1628, 17, -14, -112], [1478, -287, -125, -55], [-551, 298, -68, 59], [-1864, 180, -124, 24], [-704, -107, 155, -142], [-614, -112, 209, -124], [1007, -132, 79, 101], [-688, -158, 120, 80], [1272, 334, -47, -114], [2680, 33, -20, -18], [-1394, 112, 170, -44], [-1122, 299, -241, 105], [-1308, -186, -322, 114], [-3102, 424, -212, 62], [-278, -346, 114, 50], [1672, 334, 15, 118], [12, 261, -292, 97], [-612, 98, 116, 122], [-193, 608, 487, 163], [1480, -370, -193, -22], [-66, -408, -520, -47], [-2130, -233, 16, -79], [-1043, -737, 142, 116], [-626, -420, 149, -114], [-5114, 2, -82, 116], [-2718, 105, -229, -165], [-1202, 243, -417, -71], [1304, -259, -443, -228], [3196, -224, -539, -210], [1337, -667, -508, -74], [-2757, 82, 531, 33], [-1760, 103, -680, 41], [187, -530, -705, -253], [3492, -859, 37, 26], [-1154, 775, 508, 331], [-3768, 316, -549, -18], [1546, 1362, -248, -210], [-1504, 1183, 537, 121], [-2764, -200, -638, 182], [-3486, 167, -223, 12], [2832, 11, -349, 133], [-4058, 129, -253, 134], [3612, -1061, 236, 56], [-4200, -144, 330, -438], [8611, 277, -261, 176], [6936, 766, 264, 138], [2407, 822, 387, -265], [118, -1852, -108, 94], [1199, 251, 126, -78], [-7191, -176, 600, -7], [-6093, 604, -126, 363], [-3143, -141, 228, -446], [7858, -945, 363, -271], [-4148, 976, -371, 240], [-6874, 384, -546, -416], [-1456, 2211, -25, -321], [1850, -1034, 520, 238], [4896, -522, -545, -410], [4122, -321, 438, 129], [-6996, 595, -642, 467], [-4353, -1067, 864, 306], [-502, 646, -311, 82], [-2378, -1072, 1286, 596], [1150, 426, -734, 14], [1168, 494, -893, -392], [-5473, -220, 1628, 167], [-10500, -425, -921, 9], [-3104, 1391, 241, 105], [882, -2052, 360, -12], [2846, -2023, -181, 128], [2338, 514, -81, 380], [9400, -293, 726, 94], [7144, 2290, 932, 68], [15540, 1663, -317, -346], [907, -65, 1689, 38], [13152, -1820, -332, -224], [-7570, -2172, -1458, -408], [3470, -812, 1036, 367], [2899, -38, -27, 681], [1256, 2038, -148, -748], [-1244, 3154, -384, -182], [2506, -755, 139, 329], [-4346, 700, 2381, 818], [507, 1608, 1564, 701], [3364, -1238, -694, -334], [-1406, -537, 891, 998], [-1466, 1330, 961, 700], [606, -2981, -471, -423], [-11955, -176, 406, 389], [5250, -968, 1858, -182], [-4887, -936, 1875, -153], [2026, -1669, 2967, -868], [-8237, 185, 930, -20], [-10922, 882, 360, 105], [-18806, 2705, -1147, 195], [-7816, 577, 850, 561], [-8881, -1230, -1655, 529], [2604, 1336, 286, 850], [-8188, 2741, -1325, 323], [-12649, 2149, -385, -1068], [-786, 1114, 42, 742], [8940, 146, -484, -374], [6800, 3003, -2087, -645], [-5208, 2719, 981, 1203], [6628, 4288, 68, -739], [25210, -1735, -726, 75], [-6078, -72, -616, -416], [10458, 3626, -221, -80], [-14784, 2041, -1417, 745], [-10218, -1503, -513, 415], [-6946, 4290, -1622, 400], [-20015, 2228, -1301, 409], [4606, 2920, 2063, -826], [13862, 1077, 1926, 667], [-33100, -424, -815, -476], [16364, 61, 2487, 24], [8208, 80, -660, 1152], [21771, 84, 1639, 1045], [2704, 4994, -2067, -1124], [-3526, 1421, 3329, 161], [-23726, -2488, -534, 226], [-8548, 2084, 1366, 504], [-5202, -1125, -1062, -1053], [25495, -1578, -3171, -829], [-16662, -1315, -869, 452], [-16065, -4412, -130, 595], [17166, 1611, 3095, 82], [-4020, 1922, 2891, -366], [19426, 243, -2437, -1132], [4797, 2825, -580, 934], [-4398, 4632, -2922, 351], [17416, 3592, -3418, -167], [-4170, -1774, 1562, 290], [5088, 3854, -694, -256], [20, -2249, -4212, -16], [12596, 1152, 4228, -1340], [-22842, 395, -1488, 1057], [-5986, 358, 2280, 1496], [-20566, -2592, 524, -1475], [4600, -3575, -3416, -1156], [-2462, 2236, -2200, -1078], [-20278, -3834, -3005, -780], [-12236, -1383, -1962, -1235], [35406, 1682, 546, 881], [1457, -694, -2613, -771], [8136, 176, 509, 480], [13390, -2133, -257, -180], [22607, -7228, 1358, -707], [7170, 1530, -4794, 72], [-31874, 3226, -792, 73], [-38427, 3426, 1575, 129], [39, 1456, -2215, 731], [20156, -6013, -2519, -903], [25988, 2176, -2801, -727], [-20884, 572, -278, 1315], [-19128, 60, 1933, -12], [-11928, -4150, 5958, 390], [5108, 494, -856, 230], [-52008, -3165, 2783, 841], [10286, 2970, -1802, 346], [7891, -6547, 824, -1732], [7186, 1486, -5576, 292], [19278, -3928, 2570, 1328], [-41070, 632, -982, -1436], [-32234, 3314, -159, 766], [-12894, 3009, 631, 968], [-27167, -748, 569, 775], [6696, -331, -1468, -2194], [-3412, -2398, -6406, 658], [-8896, -7892, -580, 636], [-10094, 4160, -1731, 610], [15198, -7926, -2968, -1938], [16764, -10915, 1676, 1126], [7761, -3723, 4331, 418], [-46690, -2860, 3993, 700], [-23590, 7734, 2293, 938], [-32066, -2328, 149, -790], [-1069, 5562, -5497, -919], [29934, 104, -2688, 306], [15132, -9307, 3755, 907], [-410, -440, 112, -518], [5080, -9556, -1529, 572], [-23726, -3439, -22, -1039], [3409, -3255, -7502, -780], [11818, 5708, 499, -930], [47858, 3007, -354, -1383], [-38874, -5148, -230, -992], [29120, 544, 807, -1165], [-8963, 3397, -2834, -760], [-16636, 6790, 1777, -764], [37698, -387, 484, 1035], [14125, 3610, -3457, 1745], [109, 177, 799, -216], [22729, -8502, 5337, -1905], [-50080, 47, -2907, -2551], [11713, 10072, 683, -1587], [30410, -6329, 1603, 3330], [-8696, 9470, 1364, -86], [-45716, 258, 1302, -515], [39966, -8791, 3737, -435], [39924, 5820, -930, 84], [26346, -7640, 4054, -2154], [28058, -5926, 1876, -2276], [-47546, 8949, 2486, 1727], [-41388, 1070, -925, 2449], [23662, 1201, 341, -2263], [42626, 2431, 304, -345], [-30290, 5060, 1513, 1262], [-19684, 8808, -4921, 1444], [44042, 3612, -272, 406], [18027, 324, 1796, -2321], [-13642, 12948, 833, 1064], [30333, -6472, -1549, 973], [-11434, 1281, -1027, -1654], [-35394, -9986, 6012, 2199], [56152, 1320, 3360, 1142], [5024, 4744, -5658, 3626], [573, -3035, -4310, 368], [-26720, 582, 1092, -2710], [-65426, 972, -2012, -192], [-54380, 5359, 5540, 1193], [-49470, -4513, -3763, -1084], [-21580, -3898, 5612, -124], [32659, -8448, 4022, 3053], [-40838, -6636, 9189, 986], [-18894, -2200, -5336, 1093], [52018, 405, -61, 3851], [-17616, 7113, -1443, 895], [-34782, -2383, 2415, -335], [-42726, -5378, 1458, -6], [21670, -8463, 3819, -624], [-11338, 13229, -4649, 1192], [-23346, -3936, -5072, 2096], [-39032, 2373, 1419, -3135], [-37467, 7406, 1418, -443], [5894, -4581, -5112, 525], [25204, -1467, 2049, -909], [40085, 8487, -11556, -1668], [-7912, -6270, -1528, -1570], [69490, 3294, 1023, 1700], [-25820, 6220, 4703, 64], [14584, -4977, -701, -3486], [-10898, -4825, -4395, -1093], [12742, -7494, 2946, 638], [39009, 12514, 3255, -1581], [-39448, 698, 435, -1276], [-23852, -6008, 5710, 2924], [-8426, 6023, -1867, -2692], [66610, 2854, 4194, -2650], [-32697, 9206, -6515, 595], [-54000, -7564, -6586, 619], [40190, -9579, 6351, -3309], [-39092, -3296, 1340, 3326], [472, 2279, 9107, -590], [-12772, -4239, -8837, -3524], [-28798, 3736, 3336, -3712], [31020, -11059, 5535, -3351], [59696, 8446, -1360, -948], [-18925, 4742, -4251, 2801], [-40992, -8959, -7488, -3092], [570, -1523, -9267, -624], [15410, -16118, -1104, -1280], [-93356, -5985, 10120, 2202], [-54430, -4603, 1516, 2797], [-22388, -1, 8204, -38], [96501, 213, -1002, -998], [23286, 6355, -8145, -4149], [62248, -6706, 3258, -408], [-53184, 6672, -5935, -2138], [-50661, -11098, -1303, -1431], [-42268, 16246, 1259, 1318], [10012, 11833, -2875, 2300], [22565, 5162, 4479, 765], [-43870, 10996, 5308, 3634], [94952, 6153, -13269, -546], [-37946, 19416, -5935, 1316], [559, -3142, 1977, 5311], [38220, 15354, -26, -952], [75936, 1645, -3506, 2848], [10953, -16311, -6348, -3732], [-8614, -5720, -6056, -5674], [29382, 707, 8174, 1579], [-14181, -7311, 6363, 3126], [21650, -11987, 2925, 4203], [-7960, 19107, -2426, -2678], [-71274, -3229, -5209, -6960], [31422, -11786, 7944, 3799], [12562, 5844, 2005, 3498], [-1338, 5662, -1640, -3320], [-62778, -3122, 5248, 353], [-51756, -5588, -6464, 2244], [-46203, -2606, 53, 1587], [44337, -11174, 6572, -1521], [-10657, -3814, 10129, -4847], [27490, -20243, -8023, -1879], [-63716, 6323, 1406, 2493], [20372, -7905, 6368, 1026], [2152, 2363, -7320, 315], [60873, -9454, -7528, -3425], [-45100, -7552, 6544, -96], [-114439, -9912, 4650, 1433], [43286, -12889, -4101, -4098], [-25178, 10392, 5028, 208], [-19168, 3365, -9912, 4290], [1710, -9202, -15208, -657], [298, -13536, -1505, 502], [-20666, 11753, 8405, 1742], [85568, -11590, -824, 1510], [39290, 1064, -14348, -318], [65482, 155, -3503, -3915], [-30064, 11055, -763, -4347], [4614, -11383, -5408, 1617], [12609, 4470, 3351, -6295], [46726, -13208, 1904, 2480], [42158, -12027, 7435, 3133], [98942, 635, 6192, 5053], [56814, 7150, 6835, 2592], [-43348, 67, -8949, 4955], [156498, 8256, -3166, -1539], [-26678, 2458, 10548, 578], [18238, 8022, 2838, 7528], [1408, -956, -11344, 494], [-14171, -10604, 6263, -955], [98574, 8832, -13483, -1802], [-14726, 2840, 2078, 4508], [-19484, -3591, -13259, -3035], [-51606, -10855, -4402, -467], [7548, 2360, -12835, -3380], [138174, -6997, -669, 1908], [80788, -24527, 5302, 1466], [-18850, -1688, 6405, -390], [48552, -5241, -7988, -5152], [2296, -4867, 6359, -6593], [-31826, -9744, 10976, 738], [-67182, -8003, -16360, 2785], [-11272, 17279, -15663, 511], [-112930, -1436, -4011, -9598], [250, -4037, 701, 4652], [-23216, -5828, 10900, 4652], [80701, 6332, 4330, 2851], [49714, -1631, 480, -5081], [-137256, 3632, -12, -1170], [98022, -5660, -194, -2103], [83918, -1512, 8794, 7822], [-3836, -430, 17431, -5384], [-59054, -7143, -12475, 1826], [-43202, -3895, 6411, -1370], [-35764, -19578, -5094, -1232], [-121626, -4800, -4649, -2110], [70244, 4704, 6414, -2960], [-664, 23462, -2933, 796], [6729, 5365, 4535, -4728], [87383, 8674, 10135, -3679], [-67266, 2315, -3511, 2109], [31195, -17705, -15804, -5648], [-43122, 1648, -11038, 5996], [-82399, 15336, -669, -4795], [-80692, 1175, 9046, -977], [11704, 22244, -8520, 2688], [38802, 6176, -6208, -32], [20783, -2081, -646, -2114], [-97886, -4760, -20, 5312], [44324, -3604, 358, 6088], [29719, 17974, -6291, -2403], [-128994, -1083, 20247, 5139], [-10816, 24507, 1392, 705], [103996, 8592, 3999, 88], [-6148, -9801, -1329, -7991], [70693, -21227, 7868, -4438], [71616, -8363, 11346, -147], [42040, 2314, -7451, -2947], [-73194, 785, 3097, 1761], [-34021, -3740, -12368, 2257], [-30946, 168, 8244, 4810], [-63748, -26076, -10172, 2872], [-3944, -17722, -13881, -3814], [-18244, 2661, -5050, 3366], [39064, -2556, -532, 1196], [98142, 10845, 10005, 6224], [41999, 7101, 1850, 220], [-17626, 13404, -1017, -2590], [-68641, 1620, -4145, 781], [45762, 11592, 14874, -4714], [-20054, 16518, 21116, 4193], [-30381, 2482, -15549, -6747], [-137870, -3391, -1327, -5878], [-9522, -30486, -2838, 4288], [44426, -11278, 8056, -1100], [-95946, -7556, -4002, 4518], [-63994, 22248, -11883, 4848], [46154, 3006, 20605, -2396], [92604, -23212, 4408, 7928], [48711, 18767, -7018, 1036], [222612, 946, -1961, -2066], [10008, 1534, 19569, 7245]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1840_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1840_4_a_k();".
// 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_1840_4_a_k(:prec:=4)
    chi := MakeCharacter_1840_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_1840_4_a_k();".
// 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_1840_4_a_k( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1840_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<3,R![-904, -365, 12, 14, 1]>,<7,R![-9114, -4865, -592, 8, 1]>],Snew);
    return Vf;
end function;