
// Make newform 2166.4.a.r in Magma, downloaded from the LMFDB on 14 May 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_r();".
// 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_r();".
// 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 := [-348, -106, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 2, 0], [-70, -6, 1]];
    Rf_basisdens := [1, 1, 2];
    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], [-2, 0, 1], [-4, 1, 1], [26, -3, -1], [16, -1, -4], [14, -1, 3], [0, 0, 0], [6, 7, 2], [94, -4, -10], [20, -5, 4], [-92, -1, -4], [-10, -14, -2], [60, 11, -7], [166, -2, 9], [-74, -10, 14], [100, 14, 44], [290, 1, -23], [80, 22, 28], [120, -18, 24], [82, 33, -21], [-192, 9, 0], [478, 12, 58], [270, -60, 6], [1332, 6, 12], [-518, 25, 12], [-688, -23, -68], [-772, -38, -92], [-736, 25, 40], [-394, -50, 94], [-244, 115, -20], [-302, -39, -95], [-1354, -33, -13], [-1312, -5, 61], [1162, -56, 27], [440, -59, -68], [1966, -34, 20], [-224, -52, -40], [-1480, 46, -68], [-22, -26, 142], [-2100, -24, 72], [2152, 53, 68], [-1126, 6, -133], [-1472, 122, 44], [1462, 103, -204], [3208, 19, -71], [888, 84, 96], [40, -217, -16], [-380, 248, 20], [466, -61, -97], [1602, 187, 293], [1954, 88, -33], [1172, -170, -200], [-4778, 37, -27], [-1246, 292, 142], [1362, 146, -35], [2010, 42, 234], [4648, 156, 12], [2098, 47, 11], [3018, -126, -90], [852, 211, 193], [-270, -282, 54], [2316, -150, -216], [526, 118, 153], [5494, -136, 20], [1522, -22, 62], [5508, 222, -216], [628, 356, -244], [250, 43, -3], [5098, 57, 399], [5446, -84, 40], [754, -506, -21], [-6016, 60, 204], [-2724, 39, -168], [1436, 526, 28], [2208, 234, -84], [6782, 380, 489], [2762, -51, 363], [-6074, -22, -418], [5324, 454, 256], [-4038, -394, 286], [-392, -613, -232], [10296, -12, -48], [8296, -10, 380], [-2528, 83, 824], [4226, -181, 189], [274, 404, 926], [-5594, 579, -15], [-54, -172, -77], [6256, -275, -41], [-394, 173, 897], [-8318, -153, 214], [-3444, -639, -768], [10546, 168, -146], [2952, 45, 291], [12514, -539, -330], [-5094, 552, 486], [-12522, 420, 270], [7384, 386, -304], [602, -393, -591], [9188, 406, -572], [6986, 660, -433], [-1396, 826, 232], [9642, -108, 270], [6948, 100, -764], [-3466, 1159, -83], [-4650, 739, -115], [-7310, 364, 432], [3920, 334, 772], [-9160, -668, -536], [-7388, -739, 272], [2758, 731, 1103], [-6722, -155, -681], [-10448, 524, 392], [-3244, 349, -323], [13006, -172, -526], [-2748, -457, -7], [-7066, 546, 359], [538, -876, -521], [12988, 392, -388], [20996, -65, 424], [5264, -290, -1160], [8862, -702, -138], [2860, 428, 44], [-19428, -227, 43], [-6982, 233, 900], [11054, -664, -124], [-2122, 248, -1065], [-9592, -187, 1595], [13722, -916, -52], [14744, -941, -767], [3360, -1380, -276], [18976, 917, 140], [-6754, -1499, -2159], [19990, -657, 307], [-1794, -1341, -1611], [-2246, 596, -802], [-2320, 964, 928], [11226, 1344, 2082], [19970, -675, 377], [12148, -202, -940], [15290, 110, -957], [7156, 929, 53], [4524, 612, -1872], [-27672, 393, -60], [2760, 1500, 1200], [-7842, -1112, -464], [5574, -264, 1578], [6920, 643, -1523], [-13960, -692, -596], [5332, 1607, 344], [4822, -2003, -1641], [-2760, -141, 2145], [5280, -2286, -1128], [-264, -1362, 564], [3400, 278, 176], [-1764, -1986, -144], [-17390, -378, 808], [15982, 1269, 27], [1506, 192, -1074], [7634, -1804, -882], [14962, 506, 2186], [3260, -38, 352], [-24268, 76, -932], [-28138, -962, -770], [-35744, -124, -688], [-9020, 35, 1724], [-41034, -345, -153], [22564, 938, -640], [12698, -196, -1053], [-6532, 2056, -128], [-11852, 1649, 1892], [-4226, -2555, -2670], [9730, 428, -2920], [16116, -1467, -1536], [30770, 1451, 585], [16144, -760, -2800], [-4502, 1008, 391], [-21108, -1245, 2016], [-18490, 1491, -291], [-528, 2381, 491], [6060, 1446, 2724], [-6216, 1725, 924], [4810, -1018, -898], [13398, -1112, -901], [11578, 832, 387], [38816, -47, -572], [8096, -1340, -512], [5644, 1190, 440], [-3378, -2125, 994], [-30684, 732, 1320], [27530, 1619, 1503], [-25300, 382, 208], [-3002, 3560, 2150], [-19134, 718, -2530], [17358, 654, -150], [20806, 621, 99], [1898, -2632, -2452], [27930, 1912, 248], [17786, 212, -2343], [23582, -3170, -1118], [-4164, 1833, -2388], [42048, 1071, -1368], [19052, 2008, 1216], [-12070, 494, 1326], [53138, -766, 627], [-4952, -1739, -3125], [-38292, 876, 2316], [17030, -979, 723], [-44380, 1408, 1060], [22982, -632, 1132], [12910, -1151, 516], [5160, -733, -1687], [-42532, 754, -908], [-14528, -2152, 2372], [-3536, 890, -892], [-57920, 372, -636], [28422, -942, -54], [-9792, -2352, -1092], [9334, 324, 1873], [7536, -1863, -1812], [32784, -3309, -2160], [-20890, -656, 1582], [-18524, -1, -3841], [-9996, -4554, -4524], [-18294, -843, -2679], [4194, 2040, -4050], [11336, -152, -1268], [-35612, 161, -544], [25770, -1415, -3085], [49510, 905, -2503], [-43252, 388, 3112], [64052, -1631, -212], [-10154, -652, -4870], [-44960, 1085, 2039], [8790, 2066, -3557], [17962, 349, -65], [47758, -756, 1465], [-6886, 596, -834], [15352, -382, 2948], [-30260, -346, 2276], [6828, -43, 1883], [-60496, -1548, -1692], [-5452, -179, 1264], [42058, 1064, 3218], [5258, -1006, 4353], [1052, 799, 5563], [-16796, 3698, -1240], [-60988, 868, -404], [-70762, -472, 1953], [23526, -1986, 792], [27938, 2295, -271], [20446, 3049, 234], [-12908, -2116, 4280], [-2114, -3122, -2319], [-20438, 3406, 2886], [622, -5043, -921], [62236, 1142, -3304], [10222, -1516, -4546], [-71542, 208, 2812], [-39720, 579, 312], [-74780, 902, -1060], [47490, -1717, 425], [63436, -2599, -40], [-25086, -4543, -6023], [9536, 424, -4340], [31662, 2874, 630], [31810, -3593, 1233], [-8948, 3500, 1508], [-40146, 1432, 53], [-15772, 3613, 2020], [-13736, -2392, 4484], [9086, 2221, -5741], [57916, -3295, -979], [-25564, -4484, -5120], [7388, -818, 5368], [54926, -2985, 767], [27716, 3013, -6116], [19664, 3352, 8104], [-8698, -3823, -4869], [-27928, 646, -32], [74524, 2642, -1312], [-6806, 4258, -4335], [49220, -5015, -3704], [53892, 2863, 253], [35294, -1630, -4209], [-2334, 4587, -2403], [16866, 5244, 798], [-13532, 3713, -3655], [15690, -4710, -6618], [-3918, -197, -700], [86222, -2713, 975], [-86958, -2190, -5490], [-50484, 2700, 4836], [-57228, -3531, 2664], [34214, 2153, 3732], [-14712, -2631, -612], [95630, 1454, -1527], [51884, 1858, -1304], [-45096, -4575, -1557], [39506, 5053, -1877], [38782, -4012, -6070], [36824, -5313, -5403], [70500, 1878, -4368], [-11792, 965, 6563], [29092, 1322, 6560], [-3852, -2280, 156], [-18424, 6373, -2240], [-7082, 2220, -3437], [-47122, 4412, 3728], [94434, 1558, 4667], [-27810, 1403, 748], [12534, -5640, 282], [30876, -2136, -24], [38118, 3797, 2266], [52684, 1388, 2120], [111914, 2873, -4935], [-21576, 6480, 5460], [-16962, 1159, -371], [60438, 2509, -7597], [-19144, 4486, 820], [104006, -4075, -817], [-68922, 2340, -3078], [-28840, -7385, -4616], [35198, -4630, -3264], [2588, 1066, -4952], [-1260, 1182, -1236], [-73392, -4704, -6732], [58600, -7210, -3076], [78938, 2780, -993], [38870, -3359, -6695], [70646, -5726, -2822], [91516, -4294, -808], [13394, -1783, 5343], [9628, -2297, -2639], [131214, 2057, -251], [-105532, -4073, 1852], [-19738, 1234, 622], [63506, -5033, -4007], [15344, -1552, -1876], [-95892, -4053, -4740], [54886, -3329, 1575], [48534, -54, -1362], [-74252, 1571, -2164], [68262, 2844, -1950], [-64692, -396, -2292], [8654, 6286, 6568], [-95576, -82, 2036], [-102880, -3302, 3064], [-28958, -2750, 4455], [-104152, 4570, -68], [-55884, 546, -3372], [-72306, 394, -4435], [-51276, 7143, -1992], [97484, -1721, -5420], [-27434, 2000, -5110], [-49158, -6355, -13274], [-74698, -1776, -7414], [-108090, 1819, 4007], [-59416, 5314, 1876], [54146, 4133, 3719], [-36226, 924, 824], [-65248, -620, -2708], [-1932, 3012, 672], [-140560, 1909, -3125], [32384, -764, -6116], [-62918, -5499, -8924], [-39932, -1789, -9784], [-5832, -8022, -9444], [100966, 2608, -5550], [-53524, -500, -1856], [-6402, -1360, -6479], [10126, -2950, 6614], [-46208, 6147, 837], [-129386, -1487, 4507], [40244, 4216, 604], [-22610, 2371, 10242], [-66070, -2569, 6737], [-51518, -6615, -10223], [44516, 5518, -6692], [-61746, 2382, 3186], [97874, -4231, -2043], [-53136, -3455, 2503], [-79190, 3463, -2547], [93336, -2088, -3108], [-117840, 936, 1392], [-10088, 2956, -1796], [132962, 1594, 2968], [43200, -2163, -3345], [48424, -10246, 1244], [-114604, -2396, -764], [-11922, 2064, 1266], [42194, 6420, 542], [-110264, -5707, 6395], [-25216, 8494, 6784], [-116524, -938, 1084], [-186580, 2395, 1708], [-6726, 4626, 6522], [-59744, -550, -2068], [-159754, 857, 348], [-27628, -2507, -13700], [-149574, 3797, 1645], [107168, 1891, 4108], [3330, 3468, 12678], [16810, 1442, -1942], [-58056, 853, -4925], [12822, 8021, 6347], [121530, -3252, 2850], [-65612, -4156, 9536], [-54134, 2149, -1419], [104744, -2144, -3452], [66070, -6416, -1635], [-31836, -4062, -6060], [108260, -1239, 8535], [46986, -716, -9284], [-1742, 5833, -2724], [73528, 8654, 13688], [59268, 5397, 9324], [-6954, -974, -11488], [-51848, 3416, 4424], [-32770, -10106, -3266], [-134108, -7825, -4564], [-143082, -478, -3755], [71268, 1392, 1968], [-129640, 3418, -1376], [120770, 514, -11498], [-48180, -3270, -10656], [-163886, -237, 3829], [-105960, -705, -7383], [-34970, -4544, 10449]];
    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_r();".
// 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_r(: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_r();".
// 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_r( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_2166_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![-1292, -316, 5, 1]>,<13,R![3456, -4056, -44, 1]>],Snew);
    return Vf;
end function;