
// Make newform 336.4.q.h in Magma, downloaded from the LMFDB on 05 September 2025.
// 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_h();".
// 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_h();".
// 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 := [25, -5, -4, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [-25, 56, 4, 1], [-25, -4, 4, 1], [35, 28, 2, -7]];
    Rf_basisdens := [1, 20, 20, 10];
    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 := false)
    N := 336;
    order := 3;
    char_gens := [127, 85, 113, 241];
    char_values := [[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [-1, 0, -1, 0]];
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    values := ConvertToHeckeField(char_values : pass_field := ISA(Type(Kf), Fld), 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], [1, 2, 2, -1], [3, -3, -6, 1], [-1, 1, 25, -2], [33, 5, 5, 5], [-8, 8, 8, -16], [84, 2, 85, -1], [-96, 0, -96, 0], [-28, -17, -17, -17], [-10, 10, 41, -20], [96, -38, 77, 19], [70, -34, -34, -34], [239, -19, -19, -19], [82, 32, 98, -16], [-27, 27, 129, -54], [-3, 3, 633, -6], [-182, 72, -146, -36], [9, -9, 442, 18], [686, -32, -32, -32], [21, -21, -670, 42], [-89, -8, -93, 4], [178, -43, -43, -43], [-422, 44, -400, -22], [410, -21, -21, -21], [-12, 12, -1674, -24], [-328, 138, -259, -69], [-631, 190, -536, -95], [-11, 11, 718, -22], [-758, 2, 2, 2], [1299, 112, 112, 112], [-573, -18, -582, 9], [54, -54, 474, 108], [-961, 137, 137, 137], [1206, 204, 1308, -102], [9, -9, 1681, 18], [-30, 30, 1724, -60], [368, -600, 68, 300], [2070, 84, 84, 84], [-986, 164, -904, -82], [238, -238, 116, 476], [237, -19, -19, -19], [-2704, 196, -2606, -98], [210, -210, 653, 420], [4880, -14, -14, -14], [-204, 204, -2084, -408], [844, 378, 378, 378], [-296, -291, -291, -291], [321, -321, -1929, 642], [684, -626, 371, 313], [1392, -300, 1242, 150], [-1502, 266, 266, 266], [-207, 207, 3359, -414], [1848, 231, 231, 231], [5306, -104, 5254, 52], [-550, 550, 1078, -1100], [-85, 85, -4607, -170], [-3517, -54, -3544, 27], [-325, 325, 1560, -650], [4156, -370, -370, -370], [-269, 269, -4998, -538], [-5448, 243, 243, 243], [-5699, -267, -267, -267], [-238, 238, 3184, -476], [-3385, 1412, -2679, -706], [-1925, 986, -1432, -493], [2292, -802, 1891, 401], [2949, -436, -436, -436], [84, -84, 5028, 168], [3674, 288, 288, 288], [566, -566, -3464, 1132], [-6954, -996, -7452, 498], [-138, 138, 283, -276], [-240, -926, -703, 463], [8009, 329, 329, 329], [4770, 228, 4884, -114], [568, -568, 3458, 1136], [-3148, 410, -2943, -205], [2072, -2216, 964, 1108], [26, -26, 2991, 52], [-9094, -86, -86, -86], [945, -601, -601, -601], [946, -946, -340, 1892], [919, 121, 121, 121], [1523, 522, 1784, -261], [-3773, 818, -3364, -409], [-9170, -286, -286, -286], [4713, 2212, 5819, -1106], [5172, 942, 942, 942], [6389, -211, -211, -211], [-284, 692, 62, -346], [1206, -1206, 4386, 2412], [-110, 110, 5463, -220], [1948, 335, 335, 335], [218, 942, 689, -471], [-10526, 206, 206, 206], [407, 1582, 1198, -791], [274, -274, -3298, 548], [-1156, -1554, -1933, 777], [446, 1082, 987, -541], [18532, -12, -12, -12], [-391, 391, -3977, -782], [-501, 501, -12099, -1002], [7996, -196, 7898, 98], [-733, 733, -2782, -1466], [672, -672, -4057, 1344], [5088, 705, 705, 705], [1864, -3556, 86, 1778], [742, -742, 7706, 1484], [1623, 548, 548, 548], [-14639, 484, -14397, -242], [-474, 474, 21248, -948], [-11926, 472, 472, 472], [-1167, 1167, 2914, -2334], [-2768, 201, 201, 201], [650, -650, 14650, 1300], [3919, -867, -867, -867], [-940, 940, -9878, -1880], [-25319, -310, -25474, 155], [-4212, -156, -156, -156], [-177, 177, 1526, -354], [6463, -1886, -1886, -1886], [14741, 2398, 15940, -1199], [-393, 393, 6483, -786], [2352, -2770, 967, 1385], [4528, 2387, 2387, 2387], [3826, -3504, 2074, 1752], [21886, -1840, 20966, 920], [18385, -348, -348, -348], [15314, 2234, 16431, -1117], [83, -83, -19258, 166], [-13458, -48, -48, -48], [15217, -32, 15201, 16], [17522, 300, 300, 300], [-3596, -1288, -4240, 644], [-12689, 1570, 1570, 1570], [288, -288, 20046, 576], [6, -6, 9838, 12], [20120, 277, 277, 277], [-1982, 1982, -12976, -3964], [4816, 402, 402, 402], [-34113, 894, -33666, -447], [3456, -3456, -1928, 6912], [-650, 617, 617, 617], [2651, -2651, 11940, 5302], [-7716, 1692, 1692, 1692], [-181, -3195, -3195, -3195], [-194, 194, 27710, -388], [-14566, -1956, -15544, 978], [8390, 6136, 11458, -3068], [-27872, 4092, -25826, -2046], [-22424, -1018, -1018, -1018], [-6367, 2537, 2537, 2537], [2936, 1948, 3910, -974], [-1369, 1369, -10984, -2738], [-20392, -1034, -1034, -1034], [-21976, -4170, -24061, 2085], [35702, 472, 35938, -236], [8935, 2860, 2860, 2860], [287, -287, -29237, 574], [18512, 1948, 19486, -974], [-21476, -1246, -1246, -1246], [-7271, -1224, -1224, -1224], [26165, 2302, 27316, -1151], [-2180, 2180, -32950, -4360], [1368, -1368, -32172, 2736], [-534, 534, -43259, -1068], [-2901, 2901, 22286, -5802]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_336_q_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_336_4_q_h();".
// 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_h(:prec:=4)
    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_h();".
// 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_h( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_336_q();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![15876, 378, 135, -3, 1]>],Snew);
    return Vf;
end function;