
// Make newform 2166.4.a.w in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_2166_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_2166_4_a_w();".
// 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_2166_4_a_w();".
// 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, -7, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [-2, 6, 0], [-19, -2, 4]];
    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_2166_a();"
function MakeCharacter_2166_a()
    N := 2166;
    order := 1;
    char_gens := [1445, 1807];
    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_2166_a_Hecke(Kf)
    return MakeCharacter_2166_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], [1, 0, -1], [-6, -2, 1], [-18, 1, 2], [-25, 2, 0], [-17, -1, 3], [0, 0, 0], [-81, 10, 5], [3, 13, -1], [-33, 3, -8], [-94, 13, -23], [-4, 26, -4], [-108, -18, -7], [-260, -18, 14], [30, -13, 28], [-315, -24, 9], [-123, 2, -30], [-29, -5, 26], [-335, 1, 31], [35, -40, -14], [101, -43, 18], [-709, -17, -21], [-380, -1, 24], [-463, 7, 7], [75, -59, 5], [580, 46, 5], [137, -81, 67], [147, -1, -7], [56, 51, -44], [-882, -28, -94], [-815, 65, 39], [-1855, 9, -53], [1104, 28, -5], [-275, -94, 81], [-860, -126, 84], [1896, -9, 149], [521, -33, -66], [-117, 120, 63], [-2083, 55, 105], [-545, -112, -159], [1583, -119, 49], [680, -15, -170], [442, -215, 17], [784, -57, -76], [999, -223, -126], [-454, -12, 47], [3317, 35, 54], [3260, 185, -166], [-1101, 332, 42], [-1189, 229, -159], [-2452, -107, 6], [-2749, 82, -200], [-1148, -288, -82], [-106, 297, -272], [3673, -43, -33], [1042, 281, 30], [4612, -52, 164], [1195, 29, -55], [-155, 30, -1], [492, -128, -80], [1250, 508, -480], [699, 455, -61], [-3607, -24, -191], [1571, 255, 165], [-3919, 220, 219], [-3136, -574, 123], [4130, -11, 79], [2392, -269, -278], [1267, 98, -388], [-1053, -72, 489], [1088, 220, 228], [5507, -239, 284], [3121, -369, -441], [-8286, 266, -163], [-406, -577, 398], [-706, -179, -132], [3643, 802, -552], [-10886, -21, 122], [-5755, 85, -161], [-3904, -11, 588], [-10385, -93, 297], [5642, -368, 564], [-8332, -223, 99], [2806, 708, -363], [12591, -55, 67], [-10679, -129, -151], [-3222, 155, -99], [-8584, 197, 296], [-2160, -510, 557], [4996, -686, 400], [5263, -125, 889], [-7402, -656, 346], [1969, 21, -109], [5767, 95, 110], [-15935, -133, -147], [-6040, -290, -726], [-5289, 338, 91], [-6549, -479, -552], [-3802, 111, -675], [-3080, 782, 463], [-9945, 395, -287], [-3785, -379, -51], [8400, 370, 380], [10991, -327, -306], [7241, -231, -897], [9235, -340, -213], [6222, 413, -74], [1404, 1503, -96], [-3024, 897, 235], [-4289, 513, -228], [8119, -1003, -295], [-13173, 138, -513], [-10411, -473, -700], [-5068, 780, -501], [-1326, -282, -816], [-5639, 89, -664], [-3641, 669, -733], [1673, -497, -237], [-1654, -195, -1082], [-7282, -932, -56], [14280, 181, -513], [1840, 894, 2], [6538, 633, -40], [3600, 580, 820], [-1905, -392, 1541], [-9563, 1068, 148], [3193, 952, -1325], [14911, 395, -334], [-5127, -1769, 799], [4521, -115, 1050], [-5942, -431, -902], [-12824, -786, 777], [-4231, -860, 1868], [5958, 1437, -486], [-4287, -858, -1378], [-13874, -490, 984], [24919, 1637, -680], [22141, -961, -825], [15455, -38, 429], [-2004, -46, 1676], [9789, -897, -159], [-8014, 1440, -1182], [11079, 257, -473], [-1903, -1050, 554], [17736, -1453, 1246], [2792, -1435, 1873], [1130, 2068, -396], [-5099, -1315, 866], [27394, -1408, -288], [-6754, -527, 1273], [39985, -790, -393], [-390, -36, -553], [-18365, 1452, 395], [-11852, -120, 1512], [-9876, -1590, 276], [21650, -1464, 351], [-8171, 798, 395], [3662, 1959, -285], [-35286, -1250, 746], [-36277, 91, 465], [1399, 2332, 295], [29225, 1805, -998], [-1650, -1552, 2320], [-3010, 2274, 1600], [-4244, -1879, 84], [-1115, -1657, -188], [20920, -973, 2441], [-2740, 634, -458], [-26852, -1366, 732], [-17943, -750, -363], [-24449, 846, 294], [-5773, 361, -2487], [13148, 1521, 927], [-646, 1632, 2123], [-2604, 1530, -882], [-6728, -2576, 2253], [-18050, 3164, -60], [-22650, 1662, 1015], [17454, -655, 4047], [9979, 2339, 1523], [607, -573, -2689], [-11734, 979, -2669], [-19192, -151, 494], [15915, 4209, -3171], [1480, -2782, 2010], [13915, 1239, -3051], [5885, -2759, -332], [16933, -2919, -1749], [896, -461, 1416], [14931, 1836, 916], [-45324, -722, -256], [13208, 3928, -1121], [-21052, 904, 738], [18313, -2948, -2357], [13028, -770, 3564], [-11605, 777, 129], [10744, 3525, -1601], [-17795, -957, -187], [-48156, -168, 1470], [3949, 2468, -3351], [-14191, -3167, 2509], [10311, 1392, -1472], [-14112, -941, 2901], [-29733, -1634, 2189], [16248, -1094, 4052], [20906, -1380, -426], [17774, 5128, -3030], [13038, 1530, -102], [-25925, -1647, 2976], [-19198, -327, 1587], [63099, -335, 737], [-15247, 3383, 1018], [-4174, 4681, -630], [5639, 3106, -4515], [-18749, 616, -3862], [-30720, 2230, -2168], [-18298, 1003, 72], [5661, 2791, 1061], [12208, 3989, -132], [2183, -3240, 1340], [-17606, -2778, 621], [-29064, 2045, -3398], [-2040, -16, -4017], [19810, -884, -614], [-7295, -732, -482], [-19120, -3606, 3550], [48563, -1346, 1479], [-13154, 84, 630], [-28286, 2480, -5202], [9216, 4723, 103], [-18459, -1895, 516], [-18051, -283, 3713], [-16075, -217, -1987], [16046, -1716, 738], [-45172, -2879, 2340], [12016, -903, -4061], [-50634, 694, 1934], [25982, 2036, 1550], [-4464, -2631, 1002], [-46240, 1673, -3022], [-38750, 2012, 3554], [-8707, 1621, 1820], [6476, -1705, 5043], [-2573, 1058, -4689], [-13701, -69, 1515], [1288, -1552, -2536], [16419, -2314, 3799], [7613, 2555, 654], [-25320, 1941, 708], [3028, 5219, -2041], [7395, 718, -1111], [7769, 209, 4049], [12931, 2205, 2601], [-6429, 4204, 527], [-36663, -2317, 2953], [67199, -646, -2618], [16371, 1429, 5508], [29082, 750, 282], [-256, 1177, -2407], [60622, -4048, -1169], [-20997, -4824, 3075], [27035, 2306, 4678], [-67608, -1669, 1631], [16218, 4947, -2178], [-21695, 3545, 844], [43187, -4301, -1773], [56786, -3560, 306], [-55417, 785, 1860], [-46422, 3144, 2262], [44655, 1838, 2474], [-22447, -6371, 5653], [-8196, -278, 10], [-29284, -5988, 3756], [23976, 3781, -4683], [-16783, 5303, 292], [7756, 884, -882], [-28655, 4316, 4540], [-54605, -1492, 2242], [54230, 1648, 1596], [-35577, 648, -3147], [-81824, -2470, 1448], [22948, 167, -1950], [-75542, -4019, -7], [7837, -3211, 1097], [8147, -3071, -1029], [-50045, 6366, 1582], [7941, -3366, -6813], [30089, -1883, -2098], [-4067, 2791, 4699], [76870, 158, 36], [-40856, 1475, -2610], [48009, 1137, 195], [28602, 1736, -4676], [20476, -3255, 6243], [39884, -1849, 1964], [-10094, 708, -174], [-53228, 939, -5944], [-7908, 7147, -6088], [35095, -5031, -6200], [-130805, -1819, -1135], [-29274, 356, -4040], [42744, 5712, -3139], [-25872, -5416, 3706], [-77754, -980, -491], [14793, -7448, -3486], [-24536, -3964, 834], [49533, -4100, 1842], [68264, -3383, 2658], [104472, -1609, 2709], [7777, -4637, 2923], [76160, -1211, 1440], [-1517, 1053, -1471], [-2458, -2334, 467], [-34829, 2732, -9003], [-29559, -1915, 6755], [-13365, 3048, -597], [-89900, 2502, -2146], [50899, -2870, -7182], [11644, 2137, 2788], [-15722, -2038, 1543], [14242, 3399, -6773], [-70626, -1284, -1962], [-10033, 3809, -8496], [56985, -5886, 5811], [-73629, 4262, -1060], [-114409, 2065, 2188], [-13312, 550, -4163], [-11757, 124, -3481], [25948, 4356, 3512], [-23470, 7183, 1625], [-25000, -1238, -6612], [5102, -2496, -8382], [-48845, -6620, 913], [19062, -2240, 5458], [-62259, -1526, 7103], [-7292, -5273, 1127], [66374, -1826, 1764], [-48296, -1115, 2211], [-74492, -3886, -277], [36761, 4111, -2657], [-131925, 1860, -315], [-101818, -2933, -3474], [16696, -6342, 3252], [-87250, -5343, 3082], [-20706, -5189, -6064], [-45357, 9841, 25], [191298, -910, 1694], [-96554, -3869, 3844], [-2316, -6195, 9762], [-7042, 142, -1889], [-45933, -6746, 3492], [82621, -465, 5261], [17250, -9682, -1966], [5422, 3948, -3432], [49544, -3688, -8698], [-31044, 1909, -1540], [107881, -7438, -1557], [-53308, 7053, 4948], [7777, 3071, -2451], [-86393, -2341, -43], [12422, 6125, -3112], [76231, -2782, 6843], [34157, 10002, -551], [-51037, 4131, 6291], [125251, 9459, -6039], [74998, 10439, -6258], [-50038, -2712, 5171], [88833, -1734, 1576], [-107398, 4141, -10044], [-1386, -2494, -3313], [84813, 2977, 7013], [98824, -4536, 4190], [148387, -4895, 1296], [90064, 7339, -729], [-10087, -204, 1363], [-17192, 1875, -8224], [86429, 1179, -4725], [-122154, 4425, -864], [1798, -4757, 9611], [27968, 2083, 2658], [2970, -14054, 848], [-207863, -391, -1483], [42191, 4669, -1383], [87752, 12612, -7885], [-8501, 319, -5351], [24511, -4171, -760], [-40481, 2753, -8371], [44044, 776, 8720], [105751, -6243, -5767], [-64075, -340, -3702], [-122122, 462, 1936], [69372, 1152, -4266], [39172, -5446, 9443], [155787, 8604, -3015], [-131015, 2871, 3519], [70907, -9647, 2572], [-62683, 9906, -3353], [20647, 5999, -3128], [22095, -3897, -12693], [-125868, 4159, 1971], [70432, -5441, -9134], [-38967, 11465, -2131], [126210, -194, -1984], [13089, -6381, 11679], [51994, 16096, -6489], [176505, 3060, 1652], [208642, 1147, 820], [116504, -5300, 11260], [-21779, 654, 2645], [19248, 5739, 2515], [74025, -6246, -5289], [50943, 5068, -1543], [-32904, 1886, 5829], [54325, -2191, -2329], [-29097, -7122, 4863], [45113, -9782, 9207], [8302, -6084, -1302], [-158272, -3227, -144], [-61397, 3876, -523], [-57519, 5406, 6651], [55055, -1846, 7158], [-106257, 9652, 839], [77717, 3398, -6703], [-3269, -4588, 1846], [-25679, 5336, 2529], [-33922, -12909, 15262], [24894, -6370, -5312], [-45574, -7218, 177], [-8652, -4376, -5806]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_2166_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_2166_4_a_w();".
// 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_2166_4_a_w(:prec:=3)
    chi := MakeCharacter_2166_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_2166_4_a_w();".
// 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_2166_4_a_w( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_2166_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![-1368, -300, -2, 1]>,<13,R![-13207, 819, 75, 1]>],Snew);
    return Vf;
end function;