
// Make newform 336.4.q.l in Magma, downloaded from the LMFDB on 07 June 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_336_q();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_336_q_Hecke();"
// To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
// To make the newform (type ModFrm), type "MakeNewformModFrm_336_4_q_l();".
// 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_336_4_q_l();".
// 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 := [189, -747, 802, -111, 58, -3, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0], [213, -331, -81, -43, -3, -1], [3, -28, 29, -2, 1, 0], [723, -1366, 157, -108, 5, -2], [-219, 40, -41, 2, -1, 0], [-10203, 23415, -2249, 1687, -67, 29]];
    Rf_basisdens := [1, 66, 6, 132, 12, 132];
    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_336_q();"
function MakeCharacter_336_q()
    N := 336;
    order := 3;
    char_gens := [127, 85, 113, 241];
    v := [3, 3, 3, 2];
    // 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;

// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_336_q_Hecke();"
function MakeCharacter_336_q_Hecke(Kf)
    N := 336;
    order := 3;
    char_gens := [127, 85, 113, 241];
    char_values := [[1, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0], [0, 0, 0, -1, 0, 0]];
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    values := ConvertToHeckeField(char_values : pass_field := true, Kf := Kf); // the value of chi on the gens as elements in the Hecke field
    F := Universe(values);// the Hecke field
    chi := DirichletCharacterFromValuesOnUnitGenerators(DirichletGroup(N,F),values);
    return chi;
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]];
    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, 0, 0], [3, 0, 0, -3, 0, 0], [0, 0, 0, 4, -1, -1], [2, -1, -1, -3, -1, 0], [-6, 1, 1, 6, 0, 1], [-7, 0, 3, 0, -1, 0], [34, 1, 1, -34, 0, -2], [0, 8, 0, 69, -5, -5], [0, 7, 0, -94, 2, 2], [-28, 0, 5, 0, 11, 0], [-39, 1, 1, 39, 0, 14], [0, 3, 0, 113, -13, -13], [-166, 0, 0, 0, -18, 0], [-13, 0, -20, 0, 3, 0], [0, -11, 0, -36, -18, -18], [134, -4, -4, -134, 0, 17], [94, 16, 16, -94, 0, -3], [0, 20, 0, 22, -32, -32], [-25, -6, -6, 25, 0, 25], [-16, 0, 51, 0, 14, 0], [47, 39, 39, -47, 0, 33], [0, -15, 0, -821, 0, 0], [32, 0, 40, 0, 19, 0], [0, -24, 0, 60, -70, -70], [-982, 0, -13, 0, 5, 0], [78, -28, -28, -78, 0, -16], [0, -20, 0, 481, 23, 23], [0, -8, 0, -898, 9, 9], [567, 23, 23, -567, 0, 13], [188, 0, 39, 0, 68, 0], [571, 0, 11, 0, 100, 0], [0, -23, 0, -302, 91, 91], [1388, -46, -46, -1388, 0, -46], [-521, 0, -118, 0, -97, 0], [0, -114, 0, 636, 62, 62], [-460, -49, -49, 460, 0, 97], [-618, -78, -78, 618, 0, 66], [0, 86, 0, -800, -52, -52], [368, 0, 111, 0, 206, 0], [0, -117, 0, -2034, 28, 28], [-2752, -21, -21, 2752, 0, 140], [-287, 0, -63, 0, -49, 0], [0, -211, 0, -2568, 120, 120], [-2367, -104, -104, 2367, 0, 22], [2158, 0, -71, 0, -108, 0], [2100, 102, 102, -2100, 0, -88], [2092, 0, 34, 0, 2, 0], [-932, 0, 295, 0, 99, 0], [770, 85, 85, -770, 0, -213], [0, 161, 0, -3225, 61, 61], [0, -107, 0, -660, -184, -184], [1238, 0, 274, 0, 86, 0], [1198, -121, -121, -1198, 0, 237], [2786, 0, -286, 0, -211, 0], [0, 275, 0, -2488, 166, 166], [-2152, 32, 32, 2152, 0, 54], [1326, 19, 19, -1326, 0, 49], [0, 21, 0, -1452, 19, 19], [-2965, -147, -147, 2965, 0, 483], [1354, 0, 349, 0, -216, 0], [3289, 66, 66, -3289, 0, 295], [-4376, 0, -359, 0, 303, 0], [-4815, 0, 58, 0, -285, 0], [-4586, -130, -130, 4586, 0, -50], [0, -98, 0, -4967, -50, -50], [0, 233, 0, 2248, 273, 273], [0, 160, 0, 499, 249, 249], [1729, 0, -226, 0, 72, 0], [-4846, 65, 65, 4846, 0, 126], [-698, 0, 502, 0, 460, 0], [5408, 3, 3, -5408, 0, 208], [0, 560, 0, 2920, -102, -102], [-3213, -103, -103, 3213, 0, -230], [0, -285, 0, -787, 163, 163], [2609, 0, -44, 0, -185, 0], [0, -49, 0, -10502, -180, -180], [-336, -599, -599, 336, 0, 398], [0, -145, 0, -2235, -561, -561], [0, 339, 0, -3282, -318, -318], [3443, 42, 42, -3443, 0, -398], [-3366, 0, 632, 0, 182, 0], [-3027, 0, 479, 0, 537, 0], [-4120, -107, -107, 4120, 0, 284], [13103, 0, -71, 0, -153, 0], [0, -395, 0, 5780, -501, -501], [0, -450, 0, 9090, -37, -37], [4792, 0, -81, 0, 464, 0], [0, -142, 0, -7269, -66, -66], [756, 0, -270, 0, -570, 0], [-627, 0, 26, 0, 1059, 0], [0, 311, 0, -4576, -476, -476], [-5454, 45, 45, 5454, 0, -172], [6655, 299, 299, -6655, 0, 506], [7396, 0, 433, 0, 783, 0], [0, -938, 0, -959, 337, 337], [-2330, 0, -466, 0, -62, 0], [0, 598, 0, -1760, 437, 437], [1136, 872, 872, -1136, 0, -786], [0, -694, 0, 2015, 41, 41], [0, -135, 0, -2933, -745, -745], [9732, 0, 372, 0, -752, 0], [9522, -637, -637, -9522, 0, 183], [-8076, 274, 274, 8076, 0, -945], [0, -204, 0, -14498, -590, -590], [-127, 10, 10, 127, 0, -641], [-39, -476, -476, 39, 0, 376], [-2348, 0, -95, 0, -67, 0], [0, -1196, 0, 1402, -526, -526], [7764, -246, -246, -7764, 0, -502], [16591, 0, 308, 0, 940, 0], [0, 1215, 0, 5147, -446, -446], [-10174, 488, 488, 10174, 0, -226], [16362, 0, 272, 0, -656, 0], [6159, 602, 602, -6159, 0, -1339], [-1336, 0, -547, 0, -1189, 0], [2030, 1325, 1325, -2030, 0, 40], [-8281, 0, 378, 0, -837, 0], [-7118, 114, 114, 7118, 0, 1628], [0, 491, 0, 6090, -1023, -1023], [162, 0, 495, 0, 670, 0], [-127, -143, -143, 127, 0, -1077], [-4525, 0, -896, 0, -102, 0], [0, 1017, 0, 2520, 495, 495], [678, 570, 570, -678, 0, -213], [0, -20, 0, 7791, 641, 641], [-9962, 0, 2056, 0, 961, 0], [0, -64, 0, 26602, -200, -200], [0, 974, 0, -234, 836, 836], [-6587, 0, 35, 0, -572, 0], [0, 879, 0, 11659, -757, -757], [3601, 1548, 1548, -3601, 0, -1401], [10850, 0, 724, 0, 680, 0], [0, 815, 0, -799, -460, -460], [322, 0, -1944, 0, -936, 0], [0, 535, 0, -5446, -1434, -1434], [3051, 0, -1328, 0, 994, 0], [-5874, -1354, -1354, 5874, 0, 480], [2388, 472, 472, -2388, 0, 134], [-12158, 0, -1328, 0, -1257, 0], [8996, 1039, 1039, -8996, 0, -2284], [-3812, 0, 584, 0, 626, 0], [0, -572, 0, 33636, -671, -671], [13968, -144, -144, -13968, 0, 200], [-4080, 0, 62, 0, 139, 0], [-8609, 1173, 1173, 8609, 0, 111], [5820, 0, 1572, 0, 2328, 0], [-873, 0, -563, 0, -1505, 0], [-4760, 346, 346, 4760, 0, 558], [0, -312, 0, 26756, 658, 658], [0, -620, 0, -20586, 1236, 1236], [0, 288, 0, 17586, 782, 782], [11778, 0, 843, 0, 820, 0], [3945, 0, -1100, 0, 279, 0], [0, -998, 0, 610, -622, -622], [-5101, -762, -762, 5101, 0, -1497], [-1326, 0, 807, 0, 2480, 0], [0, -106, 0, -13749, 517, 517], [0, -987, 0, -1528, 502, 502], [-705, 0, 560, 0, -644, 0], [-16950, 1212, 1212, 16950, 0, -381], [0, 1566, 0, 6466, 254, 254], [15118, 0, 1639, 0, 1180, 0], [-23263, 0, -707, 0, 1324, 0], [0, 212, 0, 5050, -1703, -1703], [4546, -1668, -1668, -4546, 0, -28], [-13430, 451, 451, 13430, 0, -426], [-4439, 401, 401, 4439, 0, 2722], [-12947, -2347, -2347, 12947, 0, 1467]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_336_q_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_336_4_q_l();".
// 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_336_4_q_l(:prec:=6)
    chi := MakeCharacter_336_q();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 4));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(997) - 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_336_4_q_l();".
// 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_336_4_q_l( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_336_q();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![2704, 11440, 47828, 2524, 341, -11, 1]>],Snew);
    return Vf;
end function;