
// Make newform 336.4.q.m in Magma, downloaded from the LMFDB on 25 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_m();".
// 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_m();".
// 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 := [746496, 0, 168048, 0, 9457, 0, 173, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0, 0, 0], [-673920, 869616, 0, 67439, 0, 691, 0, -1], [-208224, 0, -31781, 0, 911, 0, 19, 0], [109164672, -19439568, 8185728, 233803, 143232, 29087, 768, 283], [104194944, 14802480, 3526576, 892215, -149776, 19035, -2384, 135], [-9334656, -2960496, -1680944, -178443, -62320, -3807, -560, -27], [-624672, 1055052, -95343, 181903, 2733, 5387, 57, 43], [-12451968, 78636528, -7751472, 5991727, -417648, 147443, -4272, 1087]];
    Rf_basisdens := [1, 1347840, 42120, 4043520, 1347840, 269568, 252720, 2021760];
    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, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0], [-1, -1, 0, 0, 0, 0, 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, 0, 0, 0, 0], [-1, -1, 0, 0, 0, 0, 1, 0], [-2, 0, 0, 0, 0, -1, 0, 0], [-1, -4, -2, -1, 1, 1, 2, -1], [6, 0, -4, 0, -1, -1, 0, 0], [-1, 26, 2, 3, 1, 0, -3, -2], [-5, -5, 1, -1, 1, -3, -4, 0], [26, 26, 1, 0, 1, -3, 2, 1], [-16, 4, 6, 3, -2, -7, -1, -1], [-5, -62, 7, -5, 5, 5, -7, -5], [-143, -143, -2, 3, -2, 6, -12, 1], [110, 8, -14, 6, 2, -8, -2, -2], [71, 12, -15, 9, -1, -16, -3, -3], [-24, -24, -1, -4, -1, 3, 26, -5], [-4, -7, -9, -12, 4, 6, 11, -2], [8, -54, -6, 20, -8, -11, 3, 5], [68, 68, -2, 6, -2, 6, 38, 4], [-2, -21, -12, 26, 2, -5, 5, -9], [209, -4, -31, -3, 2, 7, 1, 1], [1, -49, 2, 9, -1, -3, -4, -1], [444, 444, 4, -9, 4, -12, -29, -5], [-864, -4, -27, -3, -1, 4, 1, 1], [94, 94, 8, -16, 8, -24, -42, -8], [-159, -16, 90, -12, -9, 11, 4, 4], [4, -484, 22, -36, -4, 6, -12, 14], [-611, -611, 7, -7, 7, -21, 18, 0], [798, 798, -3, 3, -3, 9, 18, 0], [13, 519, 10, 21, -13, -15, -12, 11], [771, -4, 47, -3, 22, 27, 1, 1], [-91, -16, -25, -12, 27, 47, 4, 4], [-744, -744, 10, 13, 10, -30, 54, 23], [-18, 1246, -122, -58, 18, 28, 132, -8], [-985, -36, -37, -27, -15, 30, 9, 9], [-714, -714, -6, 24, -6, 18, -14, 18], [17, -365, -11, -83, -17, 8, 36, 42], [-18, -1778, 68, 14, 18, 10, -76, -26], [1350, 1350, -12, 46, -12, 36, 74, 34], [-1693, -44, 93, -33, 20, 75, 11, 11], [-794, -794, -1, 22, -1, 3, 94, 21], [-19, -930, -8, -71, 19, 32, 21, -6], [-786, 16, -158, 12, 1, -19, -4, -4], [-26, -26, 39, -20, 39, -117, 48, 19], [8, 1763, 88, -32, -8, 2, -78, 18], [1325, -52, 121, -39, -20, 45, 13, 13], [2, 1728, -8, 26, -2, -8, 2, -4], [-118, -32, -104, -24, -86, -46, 8, 8], [278, -36, 144, -27, -2, 43, 9, 9], [27, -12, -182, 163, -27, -61, 148, -7], [97, 97, 28, -97, 28, -84, -34, -69], [-1900, -1900, 21, -42, 21, -63, -86, -21], [1238, 104, 140, 78, 36, -94, -26, -26], [17, -2416, -70, -23, -17, -7, 80, 27], [-3276, 108, 61, 81, 21, -114, -27, -27], [-3312, -3312, 41, -44, 41, -123, -110, -3], [-16, 294, -186, -16, 16, 16, 186, -16], [-19, 923, -269, 161, 19, -26, 224, -64], [-4515, -4515, 27, 0, 27, -81, 25, 27], [-105, 823, 30, -33, 105, 87, -48, -123], [2377, 60, -87, 45, -4, -79, -15, -15], [78, -2329, 170, 138, -78, -93, -185, 63], [1932, -108, -90, -81, -90, 45, 27, 27], [1489, -20, -121, -15, -59, -34, 5, 5], [-38, 390, 348, 90, 38, 6, -380, -70], [-67, -67, 0, 82, 0, 0, -212, 82], [2115, 2115, -21, -12, -21, 63, -129, -33], [2255, 2255, 57, -81, 57, -171, 222, -24], [1791, -72, -570, -54, -124, -34, 18, 18], [151, 4648, -244, 339, -151, -198, 197, 104], [-6908, 72, -86, 54, 80, -10, -18, -18], [-35, 2938, 68, 137, 35, -8, -111, -78], [-2424, -2424, 18, 78, 18, -54, 84, 96], [27, -978, 145, -157, -27, 19, -99, 73], [1319, 1319, -12, 141, -12, 36, 94, 129], [1745, 108, 13, 81, 119, -16, -27, -27], [-2912, -2912, -67, 108, -67, 201, -220, 41], [143, 2228, 74, 179, -143, -152, -83, 134], [-1581, -1581, 32, -179, 32, -96, 290, -147], [3180, 3180, 15, 30, 15, -45, 240, 45], [-162, -2423, -4, -162, 162, 162, 4, -162], [-1022, -168, -62, -126, -194, 16, 42, 42], [3178, -472, -2, -354, -75, 515, 118, 118], [-117, 990, -252, -113, 117, 116, 251, -118], [-7448, -480, -306, -360, -63, 537, 120, 120], [-5663, -5663, -1, 12, -1, 3, 557, 11], [-3954, -3954, -63, 217, -63, 189, 670, 154], [2297, 260, -373, 195, 96, -229, -65, -65], [535, 535, 28, 130, 28, -84, -596, 158], [-4068, -248, -652, -186, 132, 442, 62, 62], [-4085, 84, 219, 63, -21, -126, -21, -21], [-2090, -2090, -35, 180, -35, 105, 180, 145], [155, 4070, -54, 615, -155, -270, -61, 40], [185, 12174, -295, 393, -185, -237, 243, 133], [5837, 128, -338, 96, -47, -207, -32, -32], [-2429, -2429, 25, 1, 25, -75, -448, 26], [3594, -344, 460, -258, -128, 302, 86, 86], [-8695, -8695, 86, 36, 86, -258, -249, 122], [152, 1584, 444, 320, -152, -194, -486, 110], [-91, -91, 65, -155, 65, -195, -1124, -90], [6811, 6811, 24, -253, 24, -72, -856, -229], [-398, -472, 164, -354, 14, 604, 118, 118], [61, -1455, -755, 929, -61, -278, 538, -156], [214, -1960, -450, 130, -214, -193, 471, 235], [2504, 2504, 180, -232, 180, -540, 134, -52], [-122, 7967, -694, -638, 122, 251, 823, 7], [84, -9549, -804, 436, -84, -172, 716, -4], [45, 88, -336, 66, 327, 217, -22, -22], [-5368, -5368, -112, 44, -112, 336, -1206, -68], [150, -7676, -128, 518, -150, -242, 36, 58], [-11113, -144, 184, -108, -192, -12, 36, 36], [2138, 2138, -138, 239, -138, 414, 529, 101], [-16, -16752, -542, 224, 16, -44, 482, -76], [3778, 800, 432, 600, 280, -720, -200, -200], [-98, -4171, 658, -590, 98, 221, -535, 25], [-8536, 436, 598, 327, 106, -439, -109, -109], [291, 112, 180, 311, -291, -296, -185, 286], [4639, 172, -1153, 129, 181, -34, -43, -43], [-90, 9636, 846, -218, 90, 122, -814, -58], [13223, 13223, 35, -110, 35, -105, 1253, -75], [10021, 212, -363, 159, 92, -173, -53, -53], [-69, 2903, -408, 555, 69, -87, 252, -225], [-2677, -152, 1502, -114, 146, 336, 38, 38], [-13455, -13455, -189, 332, -189, 567, -1527, 143], [-434, -10690, 430, -1462, 434, 691, -173, -177], [10655, 10655, 17, 291, 17, -51, -614, 308], [-947, -328, 111, -246, -322, 88, 82, 82], [-174, -174, 120, -32, 120, -360, -104, 88], [-10218, -10218, 154, -320, 154, -462, 20, -166], [4397, -776, -777, -582, -339, 631, 194, 194], [8511, 8511, 262, -377, 262, -786, -1296, -115], [292, -4999, -1044, 352, -292, -307, 1029, 277], [1958, 240, 820, 180, 168, -132, -60, -60], [9500, 9500, -148, 113, -148, 444, -431, -35], [-8108, 728, -392, 546, -330, -1240, -182, -182], [-5762, -5762, 305, -152, 305, -915, 662, 153], [4659, 264, -506, 198, 122, -208, -66, -66], [66, 21972, -254, 466, -66, -166, 154, -34], [496, -184, -1920, 1032, -496, -630, 1786, 362], [3291, -152, 881, -114, 210, 400, 38, 38], [-367, -2606, 632, -787, 367, 472, -527, -262], [-7414, 688, -794, 516, 84, -776, -172, -172], [18509, 18509, -48, 588, -48, 144, -853, 540], [-120, 1752, 1288, 744, 120, -96, -1504, -336], [12138, -892, -405, -669, -361, 754, 223, 223], [-329, -991, 336, -473, 329, 365, -300, -293], [-9862, -376, -356, -282, 262, 732, 94, 94], [6064, -1472, -354, -1104, 73, 1913, 368, 368], [-346, 6764, -164, -906, 346, 486, 304, -206], [-876, -876, 2, 262, 2, -6, 612, 264], [20710, 20710, 112, -300, 112, -336, -2048, -188], [1658, 1658, 70, -398, 70, -210, 1284, -328], [28719, -636, -301, -477, -180, 615, 159, 159], [-19607, 492, 501, 369, -161, -776, -123, -123], [3448, 3448, -186, 728, -186, 558, -434, 542], [-214, -10853, -2388, 110, 214, 133, 2307, -295], [-2797, 324, 1195, 243, 112, -293, -81, -81], [11119, 11119, 271, -441, 271, -813, 2330, -170], [7050, 7050, 97, -390, 97, -291, 2160, -293], [33303, 1040, -1108, 780, 124, -1176, -260, -260], [60, 1587, -171, -1596, -60, 354, 585, 474], [14494, 14494, -152, 394, -152, 456, -1468, 242], [14741, 924, -2089, 693, 142, -1013, -231, -231], [-2145, 112, -855, 84, 61, -79, -28, -28], [-22690, -22690, 325, 175, 325, -975, -210, 500], [228, 6346, 356, -108, -228, -144, -272, 312], [-27, 9000, 1704, -1863, 27, 486, -1245, 432], [-349, -15046, 103, -1517, 349, 641, 189, -57], [495, 1753, -1302, 1095, -495, -645, 1152, 345]];
    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_m();".
// 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_m(:prec:=8)
    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_m();".
// 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_m( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_336_q();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![784, 7168, 57220, 76256, 89261, -676, 313, 4, 1]>],Snew);
    return Vf;
end function;