
// Make newform 1216.4.a.p in Magma, downloaded from the LMFDB on 17 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1216_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_1216_4_a_p();".
// 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_1216_4_a_p();".
// 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 := [-18, -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_1216_a();"
function MakeCharacter_1216_a()
    N := 1216;
    order := 1;
    char_gens := [191, 837, 705];
    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_1216_a_Hecke(Kf)
    return MakeCharacter_1216_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], [5, -1], [6, -3], [11, -4], [-8, -1], [-15, 13], [-41, 2], [19, 0], [-43, 13], [9, -21], [-84, -44], [-82, -28], [-20, -10], [342, -7], [230, -71], [601, 17], [-131, -25], [-212, 111], [573, 77], [-158, 116], [97, 184], [-646, -58], [-238, -194], [-212, 188], [698, -102], [1150, 116], [630, 160], [-13, 193], [727, -333], [262, 260], [-1630, 260], [-798, 111], [1827, 174], [-1414, -303], [-1166, 179], [-1210, 560], [-138, 28], [-2236, 96], [-120, -162], [162, 480], [388, -514], [3702, -248], [-1081, 1108], [1442, 378], [-1348, 646], [-209, -36], [-1199, 463], [-3246, -32], [545, 1093], [3644, -307], [2016, -1011], [-3537, -576], [-980, 442], [-168, -567], [-6060, 492], [-2394, 237], [5074, 86], [1285, 405], [-1078, -1381], [1060, 14], [640, -449], [1087, -133], [1236, -748], [-6229, -890], [4251, -19], [1911, 1173], [2031, 1751], [7508, -396], [6028, -883], [-2872, 773], [8127, 621], [7077, -810], [340, -1764], [-1397, 1359], [-4339, 1443], [-2402, 1982], [2320, -1477], [1230, -413], [1536, 72], [-5304, 752], [-4260, 1416], [-6091, -103], [-1670, -1510], [-2358, -268], [2552, 1766], [4464, -663], [-5310, -2214], [8069, -1038], [822, -2997], [66, -1679], [-3484, -1187], [-7092, -120], [8554, -672], [-1356, 3144], [-8314, -2313], [13757, 1153], [-7442, 806], [-8492, -1408], [5647, 2653], [9208, -3837], [-8188, -216], [3266, 2131], [9336, 546], [-8784, 504], [19292, 1176], [-3245, -1712], [-3314, 5039], [3370, 3068], [-18248, -2032], [-10176, -298], [-13442, -1410], [-18956, 909], [-1404, -3381], [-8978, 34], [25306, -657], [-19214, -598], [-2552, -1937], [10575, -180], [13218, -609], [11383, -1543], [8735, 2927], [-3040, 2970], [13695, 57], [15948, -3096], [13576, -1019], [10856, 4222], [14722, -540], [-8773, -3356], [-12149, -1506], [-23020, 590], [8710, 1453], [-8556, 2736], [-29392, -1192], [24808, -3021], [11591, 46], [37521, -502], [-861, -4011], [4369, 1933], [7381, -91], [35037, -2106], [5827, -6077], [3354, -7137], [-2623, 3890], [-4071, -543], [33247, -2649], [4860, 8562], [4110, -4472], [2424, 8814], [-32180, 1879], [17734, -1696], [-17801, 5721], [13608, -4581], [396, -7369], [-90, 708], [6043, 6631], [20556, -7470], [767, -9301], [27135, 1113], [14249, -8928], [36053, 2707], [16360, -5356], [29202, -162], [-7080, 6160], [14868, -3072], [32652, 648], [19254, 3546], [8456, -9034], [-5630, 183], [-27740, -6260], [4722, -8073], [28275, 3333], [-29047, 4349], [44328, -3744], [817, 8599], [-16, 1940], [53292, -63], [6936, 6332], [-16832, 11165], [13008, -8456], [1838, 8825], [8489, 7700], [21439, -145], [13958, 7460], [-9580, 11782], [45031, -5800], [6708, 9177], [12499, 3177], [-26948, 2110], [10294, -608], [-24107, -3919], [8334, -2212], [-11406, -2091], [25696, -260], [-9837, 5925], [1692, -11220], [37764, -8808], [-53663, 5308], [-14296, -934], [-9691, -6113], [37809, 5868], [26310, 4170], [-28684, -1702], [-17271, 6469], [33784, -1490], [17106, -11322], [-18030, 459], [29455, 9846], [-34599, 5649], [37317, -492], [44537, 2823], [2483, 15393], [34300, -418], [-45858, -2393], [-49309, -5705], [11696, -626], [44346, -11584], [-41495, 6537], [21834, 13878], [-18826, -5762], [-344, -12973], [-6974, -13152], [-9202, 92], [-10214, -6124], [40411, 114], [18071, -16859], [-12860, -849], [40818, -10416], [36298, 10910], [-26538, 5944], [-48408, -11649], [34732, -19245], [-53435, -6251], [-27584, 7296], [41610, -6384], [-15276, -535], [-21727, -122], [-81448, -2385], [-12546, 4293], [54226, -2758], [-27652, 1684], [1000, 18050], [17068, -21431], [42143, -529], [-62547, -9077], [-31952, -13150], [38242, -11395], [-11306, -1719], [38748, 2160], [-95956, -1044], [32895, -13494], [15362, -12484], [-1053, 11400], [10833, 11973], [17698, -14690], [-48638, 11957], [24666, 6702], [-14868, 15685], [-40336, 9486], [-78342, 4428], [734, -1380], [-44944, 14810], [45360, 7818], [-3522, 5005], [-3871, -3565], [43408, -13687], [-44359, 12213], [75727, -8209], [-24909, -2820], [-3945, 21065], [-8400, 7551], [73681, -9315], [15987, 18917], [13033, -31976], [-29659, 21782], [-40870, 11262], [44971, 14427], [-20156, 31019], [65497, 2361], [-4336, 19242], [-35018, 14537], [-25146, 18540], [-47351, -4851], [-36645, -9366], [-59199, 6439], [-12206, 11533], [46050, -10305], [18653, 9798], [6322, -12634], [-54091, -5770], [-9622, 31762], [-26466, 15252], [51962, 14665], [74702, -11834], [9168, -28662], [4886, 4832], [41856, 282], [82670, 4502], [86884, -9049], [-154733, 623], [-34444, 16365], [-96368, 9079], [-46457, 13781], [-19869, -16844], [112213, 12041], [97654, -4673], [-42182, -13454], [13753, 4349], [-97481, 2025], [-5502, -5625], [-118888, -5638], [-74390, -22657], [-49766, -7804], [-10338, 630], [17392, -14654], [2784, 14616], [-2888, -14516], [50372, 23629], [-38862, -18042], [51307, -19580], [-20104, -1469], [4679, -32415], [84883, -7454], [14625, 15171], [-101610, 17026], [75193, 11219], [82678, -9146], [-73199, 22045], [-70637, 1537], [112260, -9090], [158022, 7971], [14414, 28217], [56255, 5857], [11385, 22777], [56604, -8805], [-79416, 10055], [-81054, -14541], [-42261, 14683], [61852, 6854], [-4593, 22244], [146171, 3947], [56387, 2327], [-6901, -1130], [-79455, 8235], [20754, 28078], [24997, 21107], [9444, -22380], [21422, -14368], [-16249, -38019], [-13230, 8058], [129554, 9559], [24772, 26386], [40078, -8558], [124566, -14355], [169500, 670], [-718, -1066], [50784, 36048], [-143991, 15393], [-61104, 7236], [-117797, 23354], [-173550, -9360], [152906, -16513], [45678, -43452], [18858, -6864], [-44167, 29677], [105476, 6585], [105024, 22922], [-46398, -6756], [224530, -216], [22170, 2582], [124762, -3970], [-33937, -27171], [-25753, 50974], [71585, 24223], [-64386, -24581], [28386, 31111], [91123, 25343], [128336, 19840], [-175324, 4209], [-72095, -19162], [-136812, -4684], [51702, -47904], [58456, -36451], [-25515, 6640], [-58132, -8471], [96617, 7983], [-208350, -3966], [-57113, 24567], [54000, 1906], [179920, -8747], [107844, 9570], [47070, 33428], [39615, -23511], [618, 4458], [-154072, -51], [-167760, -16698], [-17594, 7012], [-61058, -1746], [72532, 11474], [35408, 5886], [39108, 5526], [109678, -20346], [-71536, -9647], [51230, -19498], [143252, 1186], [79118, 34810], [36331, 21582], [-66360, 24769], [79440, -19230], [-215245, -1923], [-215798, 12443], [-70288, 35748], [143780, 26227], [-189951, 22155], [76952, 19665], [-191397, -17959], [-30832, -45290], [-158166, 17250], [-46412, 53844], [-101179, 1075], [40468, -52558], [239154, 4542], [-102949, -27823], [87149, 11644], [-37145, 23477], [77782, -60230], [-195981, 23667], [109737, -25461], [-19021, -2024], [-147386, 26293], [-36693, 5646]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1216_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1216_4_a_p();".
// 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_1216_4_a_p(:prec:=2)
    chi := MakeCharacter_1216_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_1216_4_a_p();".
// 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_1216_4_a_p( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1216_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<3,R![2, -9, 1]>,<5,R![-144, -9, 1]>],Snew);
    return Vf;
end function;