
// Make newform 1764.4.a.p in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1764_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_1764_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_1764_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 := [-14, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0], [-2, 3]];
    Rf_basisdens := [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_1764_a();"
function MakeCharacter_1764_a()
    N := 1764;
    order := 1;
    char_gens := [883, 785, 1081];
    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_1764_a_Hecke(Kf)
    return MakeCharacter_1764_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], [0, 0], [-2, -1], [0, 0], [26, 1], [-33, -5], [16, 8], [-85, -1], [96, 0], [28, 17], [51, 10], [-77, 19], [70, -34], [-239, 19], [98, 16], [-156, -27], [-636, -3], [-146, 36], [-433, 9], [686, -32], [691, 21], [-93, -4], [-178, 43], [400, -22], [-410, 21], [-1662, 12], [259, -69], [536, -95], [729, 11], [758, -2], [-1299, -112], [-582, -9], [-420, 54], [-961, 137], [1308, 102], [-1672, 9], [-1754, -30], [68, -300], [-2070, -84], [904, -82], [-122, -238], [-237, 19], [2606, -98], [443, -210], [-4880, 14], [-1880, 204], [-844, -378], [-296, -291], [2250, 321], [371, -313], [1242, -150], [-1502, 266], [-3566, -207], [-1848, -231], [-5254, 52], [1628, 550], [-4522, 85], [3544, 27], [1885, 325], [-4156, 370], [-4729, 269], [-5448, 243], [-5699, -267], [-3422, -238], [-2679, 706], [-1432, 493], [1891, -401], [2949, -436], [4944, -84], [-3674, -288], [-4030, -566], [7452, 498], [421, 138], [703, 463], [-8009, -329], [4884, 114], [-2890, 568], [-2943, 205], [964, -1108], [-2965, 26], [9094, 86], [945, -601], [-1286, -946], [-919, -121], [-1784, -261], [3364, -409], [9170, 286], [-5819, -1106], [5172, 942], [-6389, 211], [62, 346], [-3180, 1206], [-5573, -110], [1948, 335], [689, 471], [10526, -206], [-1198, -791], [-3572, -274], [1933, 777], [-987, -541], [-18532, 12], [3586, -391], [11598, -501], [7898, -98], [2049, -733], [4729, 672], [-5088, -705], [-86, 1778], [6964, -742], [-1623, -548], [14397, -242], [21722, 474], [11926, -472], [4081, 1167], [2768, -201], [-14000, 650], [3919, -867], [8938, -940], [-25474, -155], [-4212, -156], [-1703, -177], [6463, -1886], [-15940, -1199], [6876, 393], [-967, 1385], [-4528, -2387], [-2074, 1752], [20966, -920], [18385, -348], [16431, 1117], [19341, 83], [-13458, -48], [15201, -16], [17522, 300], [4240, 644], [12689, -1570], [19758, -288], [9832, -6], [20120, 277], [10994, -1982], [4816, 402], [-33666, 447], [5384, 3456], [-650, 617], [-9289, 2651], [7716, -1692], [181, 3195], [27904, 194], [15544, 978], [-11458, -3068], [25826, -2046], [-22424, -1018], [6367, -2537], [3910, 974], [9615, -1369], [-20392, -1034], [-24061, -2085], [-35938, -236], [-8935, -2860], [-29524, -287], [-19486, -974], [21476, 1246], [7271, 1224], [27316, 1151], [30770, -2180], [33540, 1368], [42725, -534], [-25187, -2901], [-50203, -234], [-30656, -811], [912, -4959], [-13814, -3288], [-11788, -2384], [-19683, 1424], [35007, -224], [1120, 1076], [8871, 653], [-9450, 3777], [-39728, 2304], [23057, -949], [13473, 1658], [-15952, 751], [23225, 2667], [-12508, 46], [-15074, 3224], [-12068, 557], [-26266, 672], [6333, -1961], [-14127, -154], [-3310, -1010], [-17473, -2512], [-21858, -2553], [-45544, -2100], [26636, 3898], [33196, 1751], [-8680, -3410], [-21749, 4069], [-15130, 1560], [-1278, 804], [51772, -2182], [-24238, -335], [13341, -2075], [27874, 384], [-25682, -744], [28942, 2318], [-53286, 2205], [-24848, -48], [9330, 27], [-1372, -5564], [47884, 1002], [-69737, -1547], [-21464, 2069], [-48628, 813], [-2268, 99], [-43876, 2248], [-14375, 1659], [-60736, -237], [-26436, 972], [33892, 638], [3886, 1844], [60237, -331], [36751, -4377], [37150, -3832], [37208, 5733], [-54982, -2651], [-27886, -1614], [-120, 5142], [11798, -1310], [47757, -1184], [74030, 586], [1389, -541], [-51236, -2604], [54427, -1196], [-54286, -536], [-13261, -3007], [6698, 766], [635, -4834], [-19990, -2411], [-25284, 4056], [-20814, -5718], [9098, -1754], [-39437, -3069], [-69209, 3132], [31958, -4596], [-12520, 5980], [-53908, 112], [-27087, 6736], [-22262, 7406], [-85000, -840], [-31976, 2216], [5213, -2037], [13832, -7322], [41262, -636], [16339, -75], [-48256, -5489], [-16648, -1001], [108353, -1713], [23997, 8545], [59714, 3265], [-65246, 3639], [-106265, -1001], [-69608, -4597], [-11765, 4809], [-49195, -7159], [9774, -2628], [4779, -4043], [-12762, -423], [-84038, -1258], [-32428, -1824], [-15990, -723], [37725, -3347], [81164, 3078], [-64941, -1472], [-6157, 4554], [19318, 9435], [-24956, -732], [-26354, -3337], [9639, 2999], [-13685, 8965], [-13240, 9448], [6104, -2570], [-9741, -1192], [78762, 3006], [53689, 645], [59596, -6354], [-25182, -6558], [58905, -5326], [-1078, 4525], [-13409, -8136], [-102622, 190], [-107860, -1922], [-4148, -9877], [58314, 6150], [-75028, -6680], [-18301, -4425], [26030, 2803], [-18645, 10507], [63364, -1294], [-68794, -3086], [-81243, -8561], [8482, -8808], [23190, 1185], [-17452, 6627], [-28, -12113], [-47783, 1303], [-143067, 1715], [74462, 985], [111847, -507], [41728, 6422], [-5669, -6323], [73572, -4362], [63670, 1292], [-31962, 11586], [-55936, -4272], [128600, -128], [9935, 11237], [33586, 1691], [-96192, 2088], [75983, -7372], [35014, 3134], [18151, 8679], [-35585, 7645], [49334, -9863], [-65645, -1085], [67226, -3614], [-171145, -238], [-52717, 8589], [18127, 13617], [80084, 1102], [-51550, 2857], [42761, 10671], [44482, -3403], [30531, 7987], [91642, -1822], [95927, 6825], [123074, -3314], [-84478, -1146], [156156, -1218], [162851, -3484], [-75521, -2220], [-18849, -12127], [77632, 6284], [-21808, 2227], [109593, 1100], [21410, -713], [128130, 3567], [47743, -771], [-44524, 12162], [50406, 7764], [121194, 8277], [-139916, -8136], [-34726, -10425], [-20306, -7876], [-76664, -3015], [-24049, -8847], [102962, -3128], [37958, 7204], [-76892, 7805], [-120496, -7508], [-39598, 4384], [36722, 4968], [84534, -5586], [21690, -5052], [-55562, 4562], [-95397, -3101], [-36490, -3045], [-51048, -726], [77805, -9835], [-9154, -14451], [-5140, 4861], [-47521, -4657], [-62594, 2636], [-22118, -13657], [195413, 3653], [-50415, -1999], [114952, -10144], [52760, -17420], [99970, -7155], [-23148, 14370], [74863, 1197], [1504, -12754], [5188, 4970], [-9328, -4656], [75508, 10304], [-64988, 8574], [192852, 4890], [57611, -14709], [-158486, 8598], [-50345, 1147], [86718, 3234], [127753, 6928], [2102, -14876], [91052, 8311], [162413, -5485], [-83586, -12588], [-19990, 16491], [-47804, 12477], [18322, 7928], [154275, 11651], [-20550, -9915], [129322, 12390], [89412, 5604], [8264, -36], [-45590, 10568], [173282, -5783], [-126877, 2240], [-30778, 7992], [-48404, 16166], [83287, 6129], [-180548, -4975], [223438, 93], [76308, -1365], [-139536, 4725], [82868, 3354], [66769, 9223], [150778, -6391], [-105370, -1766], [-59677, 2459], [81132, -12516], [-85616, 4406], [78424, 14687], [89921, 3647], [12744, -23574], [-111862, 5287], [-260678, -5004], [-36350, -7252], [-76834, 18007], [-24568, 11056], [-84980, 9018], [65864, 9172]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1764_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1764_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_1764_4_a_p(:prec:=2)
    chi := MakeCharacter_1764_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_1764_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_1764_4_a_p( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1764_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![-126, 3, 1]>,<11,R![522, -51, 1]>,<13,R![-2276, 61, 1]>],Snew);
    return Vf;
end function;