
// Make newform 966.4.a.e in Magma, downloaded from the LMFDB on 16 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_966_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_966_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_966_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 := [-29, -28, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 1, 0], [-19, -2, 1]];
    Rf_basisdens := [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_966_a();"
function MakeCharacter_966_a()
    N := 966;
    order := 1;
    char_gens := [323, 829, 925];
    v := [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_966_a_Hecke(Kf)
    return MakeCharacter_966_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 := [[2, 0, 0], [-3, 0, 0], [2, 0, 1], [-7, 0, 0], [-19, -1, -1], [9, -5, -2], [9, 19, -1], [-9, -1, -9], [-23, 0, 0], [-79, 37, 5], [-31, -35, -7], [-25, -1, -5], [-131, -35, 9], [-183, 19, 4], [-118, 4, 14], [6, -28, -5], [-288, 48, -3], [-91, -35, 38], [-64, -88, 23], [43, -23, 8], [-193, 187, 9], [-479, 59, -19], [-127, -143, -29], [415, 125, -30], [-227, -229, -79], [490, -142, 53], [-464, -60, -24], [-1259, 83, 44], [-698, -28, 27], [-464, -90, 107], [-421, -159, -188], [-229, 173, -11], [405, -259, 41], [126, 278, 1], [206, -252, 78], [-1038, -6, -12], [1860, -6, 68], [-920, -200, 139], [-1407, 461, 1], [-1929, -107, -183], [872, 208, -73], [1413, 161, -65], [-1650, -62, 24], [1112, -162, 44], [-973, 363, -124], [1946, -474, 109], [-3759, -185, -93], [410, 600, -61], [-68, 290, 167], [-18, 1000, 199], [-2810, 444, 159], [-530, 394, 59], [823, 85, 279], [3132, 82, -44], [1210, -1400, -154], [1091, 161, -121], [3963, -75, -30], [1435, 331, 569], [1345, 365, 326], [92, 730, 216], [3116, 370, -47], [1304, -786, 142], [531, 345, 269], [-889, -1073, 202], [6348, -532, 4], [3378, 464, 225], [-22, 26, -26], [-2991, -811, -204], [-2171, 1115, 357], [3458, -1550, 25], [2281, 633, 341], [-498, 366, -253], [7801, -701, -144], [543, 655, -413], [1936, -920, -472], [-3581, -687, -181], [-921, 1595, -503], [-4534, 954, -92], [-3379, -315, 47], [5207, 23, 787], [2956, 1510, -231], [1973, 985, -482], [-6373, 357, -128], [8008, 244, 2], [-1743, -247, 389], [-7868, -1184, 380], [-1126, -168, -619], [1222, -920, 273], [-5574, 834, -99], [1267, 1273, -293], [-7769, -721, 153], [-2039, -1289, 1263], [1941, 1327, -311], [-2599, -1757, 706], [6850, -1386, -723], [32, 1468, -19], [-3942, -546, -668], [-1406, 250, -278], [3478, 44, 608], [1974, 1888, 570], [12758, 522, 613], [2976, -1078, -1120], [2483, 2219, -616], [-11172, 274, -254], [-3061, -1595, -1489], [-18, 600, -24], [-5279, -4129, 450], [-1608, 2962, 412], [-13371, 971, 306], [3128, -2422, -881], [-6679, 241, 438], [4039, 2763, 889], [4007, 103, -1054], [13689, 1397, 446], [14729, -1157, 375], [5721, -1691, -612], [-111, 4165, 746], [1194, 1364, -1723], [1038, -4320, -1421], [-411, 1755, 1663], [-51, -4799, -1121], [7941, -979, 567], [-12433, 4111, -110], [-7777, -2263, 623], [-12301, 1757, 452], [4606, -3184, -1719], [2173, -2963, -1464], [-17949, -2405, -175], [1497, -1001, 2031], [8680, -242, -880], [-10347, 4843, -225], [-7910, 6218, 1479], [-20874, -3266, 995], [9793, 2945, -211], [-9512, 6082, 434], [-1980, 4324, 1242], [-2669, -5885, -23], [8050, 3312, -629], [16710, 1492, 542], [-13205, 1443, 298], [-7087, -5425, 721], [-354, -16, 1446], [-2062, -5922, -369], [5292, -4208, 305], [1921, -9, -1993], [-22979, 1899, -228], [-17424, 3212, -1134], [17988, 3810, 1392], [-25218, -3710, 1626], [-4124, 2936, 622], [27088, -5862, -882], [-17736, -3528, -1982], [-3293, 1329, -2302], [-13341, -4633, 710], [-3278, -7430, 927], [-9726, 5494, -768], [-18095, 1067, 2115], [-23284, -354, -1581], [-10823, 707, 2095], [9992, 4406, 1246], [-5514, -6970, -1750], [1635, -3689, -446], [-6810, -1334, -526], [5537, -3123, 524], [-9083, 1593, 1064], [-16429, 5793, 511], [-23595, -6505, 1148], [-323, 7651, 3207], [13018, 4516, -2443], [-4029, -2959, -2742], [-35095, -5017, 174], [-20550, 4070, -2010], [15035, -1323, 700], [-13323, 2709, 1909], [-21300, -5862, 2074], [13315, -723, 67], [19149, 7535, 788], [1102, -424, 661], [14942, -5226, -2401], [-25050, -2972, 51], [3378, 10134, -1983], [-4163, 1929, -1154], [17070, -10944, -1489], [-5095, 8597, 1816], [12763, 3369, 317], [25197, -5483, -2000], [-14802, 888, -1380], [10883, -3769, -683], [-40354, -4540, -221], [-7257, -1589, -2537], [11494, -6272, -181], [9878, 3530, -4329], [17004, -2564, -2126], [-13985, -6495, 1001], [12215, -2891, 1880], [34059, -11319, -730], [-14449, 1831, 1032], [23728, -6346, -3136], [24914, -3834, -374], [-11830, 428, -5343], [34578, 2180, -1700], [17568, 3210, -1946], [39122, -5074, 439], [-19539, -6829, -2584], [22837, 7409, 4605], [1514, 10706, -486], [-423, 1147, -2079], [30450, -3498, 1315], [-2806, -1396, 3087], [26191, 6493, -1465], [28030, 308, 534], [16568, 3260, -2325], [42151, -43, 2315], [6140, 7214, 3210], [14157, -4527, 14], [28514, 3050, 941], [-14943, 963, -4773], [50983, -3541, -2718], [-15177, 781, 2791], [8283, -5541, -1071], [4375, 5867, -576], [-21270, -3706, 154], [47589, 917, 3776], [15512, 5312, -1292], [33289, -697, 1885], [24912, 7406, 3389], [-61195, 555, 781], [-27052, 2376, -5104], [37753, -1037, -2624], [-7117, -13715, -5033], [-7023, -4947, 5599], [43739, -6837, 586], [-2372, -2920, -797], [48168, 4278, -2374], [-46543, -5695, 3644], [17049, -4117, -3838], [-23246, 1302, -2040], [3649, 15769, 469], [1123, 8925, -1969], [33839, 4411, -1661], [10301, 9931, -2965], [-10474, -4494, 3333], [-64224, 850, -1648], [-35967, -10503, -4515], [34635, 1875, 1591], [-66221, 1339, -1258], [-15648, -3262, 1534], [-216, 1510, -4574], [-32845, 12453, 513], [-14099, -8073, -3043], [42435, 2439, 3311], [15682, -21422, -2522], [-31269, -7767, 97], [-12957, -14271, -6951], [-20797, 1561, -2737], [-7596, 3928, 730], [-46923, 4605, -591], [5581, 3333, -2631], [-10543, 4395, -4040], [-7385, 7215, -4373], [-19443, 17011, 4310], [-32138, -282, -7402], [-53302, 3262, 4673], [40477, 14985, 1449], [28617, -9419, -2302], [5057, 13337, 5207], [28086, 12372, -516], [39343, 9193, 1159], [64543, -7951, -1637], [29082, 5812, -1721], [-4347, 20967, -1065], [-10309, -13503, -3135], [49087, -2819, 4628], [-64794, -6102, 884], [-1027, -16235, -2607], [-11942, -3656, 5148], [-25299, 8547, 4657], [7641, 18369, 3933], [62619, -1989, 3419], [8895, -10001, 3877], [-13826, 15624, -730], [44315, 12269, -542], [21707, 10713, 5075], [-19129, 2975, -6507], [11288, 6076, -3919], [-61475, -8157, -6587], [5971, -1589, 3775], [-18, 6948, -7576], [-18257, -4841, 2694], [-32976, 19502, 2482], [20905, 5305, -5406], [69709, 12557, -4442], [-1867, -2123, 304], [26211, 503, 5767], [18720, -20706, 3710], [12447, 2683, -1915], [35524, -2420, 4756], [88871, -6611, -3110], [-50241, -8883, 6559], [-61329, 4297, -2499], [15853, -29961, -2626], [97898, -14444, -3036], [-6785, -3315, 6819], [-81118, -1730, -2095], [-7528, 5276, -3552], [14315, -4861, -201], [-17770, -17374, -7785], [-23629, -14061, -853], [-36096, 8968, -3166], [40603, -7453, 6986], [-73779, -8807, 3695], [-81081, 16795, 1575], [-117236, -2618, -301], [-18091, -13317, -11367], [70547, -143, 5251], [-36616, 29540, 600], [29789, -17393, 253], [78309, -3085, -2699], [-29939, 13675, -3497], [8178, -12440, -8270], [-38386, -16052, 4120], [-25067, 4313, -10236], [-119988, -12592, 2219], [77367, -1531, -249], [-12663, 19141, -1845], [-52926, -10950, -1461], [-251, 18323, 273], [93780, -1696, 3055], [-112946, -10422, 504], [-80376, 1930, 6568], [-53532, 14210, 422], [18161, 1117, 9563], [-51447, -7375, -2988], [29778, 16790, -493], [104418, 12818, 648], [-41714, 4650, -1390], [-356, 24910, -1368], [-69926, -8798, -3204], [-71994, -24248, 1141], [54236, 14304, 5033], [5517, -13995, -2133], [-55894, 36748, 6044], [25353, -5253, -3594], [-4549, 24209, 6581], [16010, -21292, -7788], [-86937, -21691, 2450], [61170, 5814, -2869], [-39076, -5292, 7981], [-119822, -2902, -8103], [28995, -21913, 5263], [15399, 7133, -5780], [-54403, -639, -4257], [3989, -2299, 7263], [-37034, 8630, 9069], [-33371, -19615, 3307], [-81720, -13074, -9074], [-48292, -16516, -6750], [33883, 2171, 555], [-38373, 10383, 3459], [-37675, 26263, 2569], [-31635, 6725, -7249], [-121995, 7445, 689], [49787, 21401, -7113], [14988, 11792, 4280], [108286, 13654, -2096], [-84544, -2146, -534], [-185975, 2419, 1380], [109222, 18546, 6600], [1773, -7843, -7120], [77439, -4179, 7056], [-27845, -6719, -715], [-31836, -8772, 4564], [-44032, -17134, -7712], [139812, 1544, 5708], [28293, 2709, 12279], [-30500, -4576, -5097], [-127712, -15588, -10355], [-3930, -25254, -11408], [-89118, 16426, -3502], [-19603, -6051, -1423], [11703, 29599, 11555], [-9481, 12291, -2098], [-92338, -28586, 444], [41597, -8835, 11932], [-66230, 20488, 5789], [-61691, -10071, -14378], [23767, 15877, 8760], [-86462, -25574, 6910], [-47111, -24889, 15075], [61824, -7480, -9974], [57731, -57, 6073], [-49344, -28608, -15213], [73290, 4922, 7646], [94910, -39814, -4688], [68505, 10879, 4632], [-99358, 13056, 2275], [70891, 19705, 8925], [-165943, -7289, -8701], [-36647, 8567, 9958], [138827, 9763, 15313], [-54117, 8249, -8235], [4937, 38389, 11488], [-19308, -4604, 2982], [84977, 13859, 2030], [22655, 16149, 18056], [128229, -1335, -3856], [16592, -39982, -1566], [62028, -32134, -4372], [-60848, 9186, -1074], [56472, 34796, 6267], [-80839, 14489, -11528], [-9510, -23744, -15174], [-69952, 38828, -2380], [-101951, 31143, -3150], [32917, 17057, 9406], [24226, 16990, -11850], [40399, -24067, -7087], [-22955, -16611, -13959], [-85847, 17247, -6031], [-17582, -7802, -6520], [30593, -14091, -2955], [85707, -15625, -2831], [-17642, -13194, -18469], [-92369, 17333, 908], [-115201, -1693, -13084], [131994, -3146, 10964], [-108061, 3909, -1726], [-74508, 4182, 2896], [21690, -13814, -2847], [-76065, -5841, -5158]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_966_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_966_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_966_4_a_e(:prec:=3)
    chi := MakeCharacter_966_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_966_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_966_4_a_e( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_966_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![878, -191, -5, 1]>],Snew);
    return Vf;
end function;