
// Make newform 336.4.q.k in Magma, downloaded from the LMFDB on 23 April 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_k();".
// 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_k();".
// 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 := [36, -144, 582, 12, 25, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0], [118833, -1584, 6402, 328, 275, -11], [118830, -1872, 7566, -922, 325, -13], [-14256, 57618, 1825, 2475, -99, 100], [-117648, 475494, 6815, 20425, -817, 801], [-2514, -947238, -30587, -41481, 1083, -1676]];
    Rf_basisdens := [1, 7203, 7203, 14406, 7203, 14406];
    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], [-1, 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+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, 0, 0], [0, 0, 0, 3, 0, 0], [-4, 0, 1, -4, 1, 0], [1, 1, 1, -2, 1, 1], [0, 1, 0, -11, -2, 1], [20, -1, 2, 0, 0, 0], [0, 1, 0, 17, -3, 1], [-67, 0, -1, -67, -1, -2], [73, 0, -3, 73, -3, -7], [21, 5, -10, 0, 0, 0], [0, -5, 0, 34, -7, -5], [-86, 0, -4, -86, -4, -5], [78, 4, 10, 0, 0, 0], [-125, -18, 15, 0, 0, 0], [-71, 0, 3, -71, 3, -25], [0, -20, 0, 134, -9, -20], [0, -10, 0, -368, -39, -10], [10, 0, 40, 10, 40, 20], [0, -16, 0, 199, 31, -16], [-99, 33, -21, 0, 0, 0], [0, -31, 0, 332, -8, -31], [302, 0, -45, 302, -45, 3], [-162, 6, -33, 0, 0, 0], [-580, 0, -26, -580, -26, -48], [23, 1, -50, 0, 0, 0], [0, 12, 0, 806, -56, 12], [-889, 0, 21, -889, 21, -24], [868, 0, 5, 868, 5, 6], [0, 71, 0, 590, 118, 71], [687, -1, 23, 0, 0, 0], [-554, -19, 23, 0, 0, 0], [-851, 0, 32, -851, 32, 93], [0, 34, 0, 1034, -60, 34], [85, -124, -19, 0, 0, 0], [-1738, 0, 12, -1738, 12, -2], [0, -3, 0, 331, -120, -3], [0, -2, 0, -1564, 164, -2], [-826, 0, 134, -826, 134, 154], [-3, 145, 31, 0, 0, 0], [767, 0, -223, 767, -223, -61], [0, -15, 0, 2875, 71, -15], [-1210, -55, 152, 0, 0, 0], [-635, 0, -115, -635, -115, -177], [0, 12, 0, 1715, -54, 12], [-429, 65, -115, 0, 0, 0], [0, 46, 0, 238, 98, 46], [1222, -182, 328, 0, 0, 0], [-491, 133, -92, 0, 0, 0], [0, -103, 0, -2987, -60, -103], [-2306, 0, -120, -2306, -120, 69], [-1369, 0, -57, -1369, -57, -71], [3564, -6, 168, 0, 0, 0], [0, 9, 0, -2851, -12, 9], [-3378, -80, 271, 0, 0, 0], [-2671, 0, 155, -2671, 155, 35], [0, 16, 0, 3976, -170, 16], [0, 91, 0, -2269, 60, 91], [-1735, 0, 80, -1735, 80, -161], [0, -383, 0, 3344, -142, -383], [-1641, 1, 265, 0, 0, 0], [0, -124, 0, 109, -23, -124], [357, -47, 526, 0, 0, 0], [-3341, 28, -191, 0, 0, 0], [0, -94, 0, -1556, -72, -94], [-1313, 0, 300, -1313, 300, -318], [-3205, 0, -126, -3205, -126, 49], [2015, 0, -315, 2015, -315, -378], [3011, 26, 26, 0, 0, 0], [0, 39, 0, 2335, -97, 39], [-4264, -94, 134, 0, 0, 0], [0, -661, 0, 1505, -319, -661], [-3836, 0, 82, -3836, 82, 244], [0, -149, 0, 3940, -439, -149], [-4046, 0, 108, -4046, 108, -57], [4081, 242, -409, 0, 0, 0], [-1583, 0, 495, -1583, 495, 377], [0, 313, 0, 2339, 885, 313], [-4622, 0, -736, -4622, -736, -209], [251, 0, 423, 251, 423, -17], [0, 666, 0, -85, -168, 666], [-5454, -84, -162, 0, 0, 0], [5312, -489, 204, 0, 0, 0], [0, -169, 0, -179, 657, -169], [-9142, -169, -130, 0, 0, 0], [-11911, 0, 256, -11911, 256, 527], [4330, 0, 69, 4330, 69, 404], [-1449, -837, 171, 0, 0, 0], [2269, 0, -728, 2269, -728, 14], [-1650, 466, -1340, 0, 0, 0], [-1283, 10, -989, 0, 0, 0], [1351, 0, -93, 1351, -93, -139], [0, 303, 0, 1039, 389, 303], [0, 473, 0, 5242, 115, 473], [-3399, -247, -262, 0, 0, 0], [-553, 0, 95, -553, 95, 64], [1080, -330, -240, 0, 0, 0], [3098, 0, -43, 3098, -43, 566], [0, 76, 0, 548, 858, 76], [-1771, 0, -757, -1771, -757, -260], [-8642, 0, 378, -8642, 378, 417], [3868, 400, 388, 0, 0, 0], [0, 371, 0, 15959, -490, 371], [0, -464, 0, -7436, -755, -464], [2066, 0, 394, 2066, 394, 780], [0, 300, 0, 397, -111, 300], [0, 16, 0, 7895, 416, 16], [2373, -11, 544, 0, 0, 0], [12446, 0, 474, 12446, 474, 28], [0, 314, 0, 4402, 1500, 314], [-661, 820, 400, 0, 0, 0], [3056, 0, 231, 3056, 231, -471], [0, -116, 0, 4970, -298, -116], [-702, 504, 528, 0, 0, 0], [0, -772, 0, 6115, -413, -772], [1531, -1041, -594, 0, 0, 0], [0, -899, 0, -10279, -585, -899], [1117, 372, 225, 0, 0, 0], [0, -210, 0, -68, 914, -210], [-5221, 0, -2004, -5221, -2004, -1109], [-8757, 729, 951, 0, 0, 0], [0, -1043, 0, 10622, -118, -1043], [-9733, -136, -262, 0, 0, 0], [13067, 0, -204, 13067, -204, -1007], [0, -280, 0, 11998, -519, -280], [-5413, 0, -2235, -5413, -2235, -1074], [-11898, 696, 75, 0, 0, 0], [-2522, 0, 1076, -2522, 1076, -548], [14644, 0, -834, 14644, -834, 62], [-1772, -75, 2163, 0, 0, 0], [3484, 0, 1292, 3484, 1292, 1195], [0, -506, 0, -2021, 1955, -506], [-15282, 0, -384, 0, 0, 0], [-11278, 0, 1469, -11278, 1469, 289], [4490, -192, 108, 0, 0, 0], [11015, 0, -1497, 11015, -1497, -1265], [-3877, -984, 762, 0, 0, 0], [0, -666, 0, -26284, -578, -666], [0, 1064, 0, 7444, -1598, 1064], [17634, 1688, -835, 0, 0, 0], [0, 271, 0, -8551, 1545, 271], [-3620, -984, 186, 0, 0, 0], [-664, 0, -733, -664, -733, 836], [0, -668, 0, -9956, -1468, -668], [-10152, 792, -1431, 0, 0, 0], [0, 1065, 0, -10492, 282, 1065], [-2460, -1328, 460, 0, 0, 0], [13232, -1107, -324, 0, 0, 0], [0, -682, 0, -16918, -1648, -682], [17480, 0, -606, 17480, -606, 756], [-7910, 0, 1404, -7910, 1404, -160], [-8078, 0, -1226, -8078, -1226, 392], [3861, 1215, 1035, 0, 0, 0], [-11243, -954, -189, 0, 0, 0], [35476, 0, -488, 35476, -488, -350], [0, -684, 0, -28289, 885, -684], [-13401, 1533, -603, 0, 0, 0], [15299, 0, 189, 15299, 189, 738], [17363, 0, 2153, 17363, 2153, 1289], [-21973, 1108, -1112, 0, 0, 0], [0, 2268, 0, -7294, 733, 2268], [-25832, 0, -2300, -25832, -2300, -1394], [-8967, 295, -2945, 0, 0, 0], [9394, 1351, -2819, 0, 0, 0], [13276, 0, 1713, 13276, 1713, 1682], [0, -1748, 0, -10270, 384, -1748], [0, 2273, 0, -28259, 1389, 2273], [0, 2007, 0, 12286, 1233, 2007], [0, 193, 0, 2966, -1294, 193]];
    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_k();".
// 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_k(: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_k();".
// 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_k( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_336_q();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![1527696, 237312, 50460, 360, 313, 11, 1]>],Snew);
    return Vf;
end function;