
// Make newform 2166.4.a.j 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_j();".
// 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_j();".
// 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, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    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], [-3, 0], [-4, -5], [-12, 7], [3, 8], [-12, 5], [11, -34], [0, 0], [-35, 16], [22, 86], [200, -161], [154, -160], [-128, -75], [-159, 94], [176, -19], [217, 332], [18, 424], [372, -250], [131, -129], [352, -245], [161, 74], [-375, -250], [69, -212], [404, 372], [1030, -122], [-397, 808], [235, 641], [595, -852], [-155, 1340], [365, -219], [884, 30], [-1639, 1142], [1397, -1091], [-1538, -114], [-871, -1123], [140, -1616], [-1747, 332], [1038, 1055], [3216, 16], [2992, -328], [-953, 2421], [232, -2865], [2369, -2804], [-408, 2182], [2712, -1551], [-2345, 430], [2063, -2282], [-1698, 1347], [4103, -4258], [153, 794], [-5336, 1578], [-1118, 3491], [-528, 1845], [-2367, -522], [-4211, 1570], [3028, -6650], [-4911, 2402], [-4393, 3375], [2620, -1427], [784, 4056], [463, 2215], [-3748, 4567], [-1705, -1842], [1871, -2648], [-1632, -2120], [1171, -4249], [-9251, 546], [-6776, 4025], [-1496, 3410], [2840, -1475], [-7651, 1918], [7638, 469], [-9335, 2418], [-9737, 1420], [-6082, -4116], [-12423, 1157], [2585, 2470], [-10391, 7750], [267, -1730], [-794, -4482], [-537, -6116], [-9923, 3295], [1327, -9545], [-3316, 10658], [-6565, 830], [-8403, -1088], [-6637, -6931], [4701, -6019], [-3575, -4701], [9132, 5762], [-6093, 7729], [-4117, -2721], [-11255, -922], [-7996, 1736], [-9533, -724], [3626, 6239], [-5324, 7263], [-5713, 4950], [3488, -1740], [-9130, -5806], [-2049, -2534], [-7446, 3890], [10035, -8169], [5459, 9407], [6742, -5910], [4031, 4436], [14134, -8865], [8016, 1064], [-13136, 5787], [-9353, -1895], [-12785, 10333], [-23249, 224], [23100, -10592], [16523, -841], [-3974, 14502], [24547, -10320], [-6384, -5721], [12304, -4130], [-6471, -9342], [345, -3960], [-19073, 14925], [9608, -15460], [12230, -2912], [9946, 5449], [28657, 1060], [-5223, -11120], [8205, -6365], [-9589, 12123], [2535, 17893], [10135, -15404], [-10169, -15087], [13093, -17150], [10551, 3187], [-14951, 7080], [-21912, 7588], [-16600, 12355], [-1529, -20361], [16741, -28949], [-16600, 8583], [31093, -3616], [7149, 13816], [-8263, 5355], [24705, -2214], [-20505, 39303], [-18118, -9339], [-4738, -19074], [2388, -6800], [-22629, 30486], [37087, 5111], [10161, 6664], [19681, -6254], [-17278, 36280], [-19993, 35272], [522, -4346], [-51589, -654], [-15531, -599], [26531, -10982], [2931, -18662], [18074, 3280], [-3008, 26085], [-25000, 26888], [30307, -9648], [5520, -36777], [16407, 8370], [10278, -98], [19288, -25510], [17587, -12410], [-15697, 37762], [-3361, 6562], [26344, 4758], [38519, -5403], [-3774, -18328], [29935, -21551], [13403, 7340], [-1466, 33097], [24889, -32083], [20137, -19890], [-20858, 11955], [7476, 24344], [2358, 26474], [12262, 4534], [26922, 11095], [-28369, -4976], [18809, -43810], [-40401, -2947], [-29335, 23560], [-29491, 5975], [-20951, -10682], [6400, 6350], [-11881, 37641], [-33699, 39639], [-4429, 899], [9469, -41294], [62921, -20368], [-16778, 13539], [-17723, -2298], [-36864, 3612], [20570, -64029], [-19, -29949], [8498, -1585], [29798, -56821], [-73663, 885], [-49869, 86], [-1054, -45322], [2872, -52914], [22675, -25592], [-37263, 57566], [-20890, 22506], [45920, -5880], [-8970, 48559], [-13713, -15886], [4975, -22811], [-3619, -4761], [52477, -54451], [21798, -25961], [3515, -59436], [353, 9922], [-31766, 53391], [-15051, 32980], [-19744, -28471], [-47878, 42380], [-41654, -15657], [-43064, 38458], [4707, 23882], [-1798, 3509], [-53673, -23719], [34815, -1214], [9119, 48722], [13939, -41805], [-68543, 28630], [48228, -34145], [42827, 26121], [1667, 3835], [-49839, 25092], [32971, 4334], [35446, -42064], [-11966, 27867], [-53693, 22272], [-57987, 46024], [61647, -40320], [29708, -61970], [-8301, -62399], [-52111, 55178], [27558, -21266], [32360, 11341], [98851, -34842], [20803, -52098], [46071, 10957], [86648, -27076], [-49635, -16054], [-3636, 43315], [-67382, 66088], [-100632, 14332], [15419, -19638], [-15440, 79856], [-55660, 29590], [-240, -3901], [38204, 9590], [-33265, -21222], [14721, -7524], [69802, -293], [34348, 51932], [23400, -37350], [-57476, 60458], [14183, -39898], [-70758, 28361], [-102079, -1087], [-73521, -37229], [-53463, 30652], [-38485, -43384], [-65362, -22572], [92301, -6134], [-10330, 93896], [-28599, 75688], [31055, -59672], [84704, -64757], [-8036, 7840], [-110047, 32004], [32751, 53427], [7929, -51499], [-51426, 71513], [32399, -42589], [-36661, -40735], [-16972, -83432], [7810, 26413], [-78163, 53390], [-67403, 9402], [41151, -97634], [35145, -685], [-46035, 51425], [-82018, 7210], [-7160, -72357], [-100408, -19588], [47692, -106395], [27745, -27164], [-2369, -14052], [132482, 6920], [48758, -88820], [-9430, -90745], [-58771, 66590], [-6074, 88344], [-74442, -19823], [-45068, 9211], [-29868, 113917], [-66485, 3294], [-47646, 112860], [26343, 15417], [34299, -50008], [-76273, -52914], [-76039, 46104], [-83146, 9748], [-116803, 37781], [-44651, -36079], [38420, 45681], [-63287, -4448], [74333, 13854], [-115990, 107860], [-42146, 46856], [9581, -102641], [-45830, -66680], [67107, -59820], [97257, -110836], [-10745, 70249], [87823, -87250], [107218, -26460], [-34300, 85839], [73839, -60233], [47904, -66822], [47089, -46290], [-70183, 42912], [-11139, 98182], [2183, -15418], [-108465, 29585], [-30961, 112713], [-86598, 37420], [-126729, 35651], [-14445, -28765], [61693, -91130], [-101192, -31072], [6313, -92928], [47327, -121703], [4478, 32397], [-110950, 115740], [82210, 70719], [78840, -93384], [102674, -104493], [33301, -107708], [57105, 42548], [-50093, -5320], [-1371, 14400], [128495, -91981], [-101768, 168399], [202328, 16168], [-120161, 31648], [113065, -47915], [-24129, 39109], [-49450, 62270], [-43769, -54833], [3962, -10571], [68229, -56172], [152741, -74019], [55565, -40941], [-60517, 114977], [51377, 89166], [-21022, 162442], [-89581, 84683], [-122683, 104159], [188206, -14266], [-14967, -35352], [-12443, 69650], [133738, -3646], [23029, -52232], [-49333, -13804], [144574, -110744], [-33343, 8034], [96210, -193160], [179805, 47294], [165915, -61464], [147576, 31598], [20787, -47856], [-198536, 63566], [-27182, -71865], [-83264, 5876], [26921, 105476], [-5902, 36869], [80551, -191826], [74827, -5870], [117133, 45352], [-30647, 150775], [-82967, 49367], [-136307, 82784], [60181, -27576], [31706, -110192], [233801, -29409], [-74315, 91244], [72778, -138060], [59294, 5392], [167299, -99388], [-63606, 91321], [-180448, 73250], [-26773, -110294], [169685, -155269], [29573, 57527], [21942, 131764], [-107019, 90958], [-41051, -37924], [-69879, 22936], [151289, 37161], [-85636, 82703], [109026, -16337], [-131755, 75226], [204031, -20264], [200293, -44450], [233977, 9140], [-168176, -35460], [80127, -27850], [-79515, 129360], [100052, 59559], [30998, -89728], [-251896, 72313], [-146659, 17613], [-70877, 164427], [-90188, 161339], [-143585, 8750], [245624, -59037], [-226355, 37608], [-73440, 235241], [-4997, -167266], [5602, -48050], [-24854, -50542]];
    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_j();".
// 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_j(:prec:=2)
    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_j();".
// 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_j( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_2166_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![11, 13, 1]>,<13,R![59, 19, 1]>],Snew);
    return Vf;
end function;