
// Make newform 338.3.d.f in Magma, downloaded from the LMFDB on 22 December 2025.
// To make the character of type GrpDrchElt, type "MakeCharacter_338_d();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_338_d_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_338_3_d_f();".
// 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_338_3_d_f();".
// 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 := [121801, 0, -25778, 0, 2067, 0, -74, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0, 0, 0], [2068174, 102689, -345859, -8968, 19195, 32, -349, 9], [-1981622, 102689, 345859, -8968, -19195, 32, 349, 9], [1396, -10073, 0, 1718, 0, -74, 0, 1], [-9700804, 398815, 1469988, -53258, -76780, 2294, 1396, -31], [0, 148063, 0, -22145, 0, 1131, 0, -20], [8604595, -1582807, -1135297, 292601, 49209, -16901, -698, 318], [8604595, 1582807, -1135297, -292601, 49209, 16901, -698, -318]];
    Rf_basisdens := [1, 86552, 86552, 2792, 86552, 21638, 86552, 86552];
    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_338_d();"
function MakeCharacter_338_d()
    N := 338;
    order := 4;
    char_gens := [171];
    v := [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;

// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_338_d_Hecke();"
function MakeCharacter_338_d_Hecke( : Kf := false)
    N := 338;
    order := 4;
    char_gens := [171];
    char_values := [[0, 0, 0, 0, 0, -1, 0, 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 := 3;
    raw_aps := [[-1, 0, 0, 0, 0, 1, 0, 0], [-1, 1, 0, 1, 0, 1, 0, 0], [0, -1, 0, 0, 0, 0, 0, 1], [-3, 1, 1, 1, -1, 0, -1, 0], [-6, 0, 1, 0, 0, -5, -1, 0], [0, 0, 0, 0, 0, 0, 0, 0], [3, -4, -3, 0, 0, -11, -1, -1], [1, -10, 0, 2, 2, -1, 0, -1], [-3, 4, 3, 0, -1, -10, 1, 1], [5, -3, 0, -4, 0, -4, -1, 1], [-5, -1, 0, 3, 3, 5, 0, 0], [14, -5, -23, -5, 5, -19, 6, 0], [-10, 25, 0, 2, 2, 10, 0, 8], [7, -6, -7, 0, -5, 10, 1, 1], [-1, -1, -10, -1, 1, -13, 2, 0], [-7, 19, -31, -7, 0, -7, 5, -5], [-29, 7, -15, 7, -7, -30, -1, 0], [-17, 2, 8, 6, 0, 6, -4, 4], [8, -19, 0, 1, 1, -8, 0, -11], [-25, 10, 0, -6, -6, 25, 0, -11], [-12, 3, -24, 3, -3, -30, -1, 0], [4, -40, 24, -16, 0, -16, 0, 0], [9, 43, 0, 3, 3, -9, 0, 8], [-51, -6, 61, -6, 6, -2, -1, 0], [-12, -6, 0, -10, -10, 12, 0, -7], [-31, 39, 31, 0, 6, 59, 8, 8], [-18, 22, 18, 0, 0, -40, 4, 4], [25, 9, -38, -15, 0, -15, 14, -14], [1, 24, 0, -8, -8, -1, 0, 2], [-21, -41, 36, -4, 0, -4, 1, -1], [41, -28, -41, 0, -11, -14, 13, 13], [92, 8, 6, 6, 0, 6, -8, 8], [61, -21, -19, -21, 21, 0, 8, 0], [11, 69, -82, -17, 0, -17, -4, 4], [39, -14, 0, 25, 25, -39, 0, 4], [-13, -5, 76, -5, 5, 53, 0, 0], [-101, -54, 65, 19, 0, 19, 8, -8], [65, 1, -85, 1, -1, -18, -7, 0], [18, -8, 63, -8, 8, 65, 25, 0], [118, -122, -118, 0, 21, -84, -4, -4], [-61, 58, 61, 0, 29, -2, -3, -3], [75, -70, -75, 0, 25, -125, 5, 5], [25, -45, 76, 15, 0, 15, -16, 16], [92, 8, -61, 8, -8, 47, -26, 0], [-37, -15, 0, -23, -23, 37, 0, -47], [65, -60, -65, 0, 23, -198, 5, 5], [133, -51, 90, 37, 0, 37, -2, 2], [-10, -77, 0, -17, -17, 10, 0, 17], [35, 60, 0, -36, -36, -35, 0, -13], [135, 18, -118, 18, -18, 53, 30, 0], [2, -20, -2, 0, 6, 172, -18, -18], [-123, 87, 0, -29, -29, 123, 0, -28], [245, -13, -73, -13, 13, 146, 12, 0], [81, -56, -81, 0, 55, -46, 25, 25], [-99, 81, 99, 0, -64, 117, -18, -18], [-95, -55, 80, 3, 0, 3, -22, 22], [148, -115, 23, -57, 0, -57, 35, -35], [-40, 2, -49, 2, -2, -85, 21, 0], [11, -37, -11, 0, -32, -297, -26, -26], [86, -16, -102, -16, 16, -48, -5, 0], [223, -202, -223, 0, 45, -362, 21, 21], [-21, 12, -45, 12, -12, -42, -6, 0], [13, 5, 32, 5, -5, 55, -52, 0], [162, -170, -162, 0, -8, -64, -8, -8], [-28, -88, 140, 16, 0, 16, -36, 36], [152, 26, 0, -11, -11, -152, 0, -37], [-185, -16, 0, 12, 12, 185, 0, 37], [-121, 70, 121, 0, -9, -27, -51, -51], [-343, 193, -142, 41, 0, 41, -10, 10], [-5, 34, 55, 34, -34, 118, -3, 0], [204, -88, 0, -11, -11, -204, 0, -12], [-93, 41, 72, 41, -41, 61, -52, 0], [133, -13, 84, 79, 0, 79, 8, -8], [25, 55, -6, -6, 0, -6, -55, 55], [280, -117, 0, 7, 7, -280, 0, -5], [131, 234, 0, -2, -2, -131, 0, 43], [-57, 42, 57, 0, -59, 129, -15, -15], [228, 11, -157, 11, -11, 93, -43, 0], [-34, 33, -19, 33, -33, 13, -48, 0], [-201, -5, 0, 12, 12, 201, 0, -50], [-45, 103, -78, -13, 0, -13, -38, 38], [-26, 62, 0, -85, -85, 26, 0, -25], [-12, -143, 0, -31, -31, 12, 0, -9], [-9, 39, 9, 0, 48, 275, 30, 30], [25, -22, -25, 0, 33, -34, 3, 3], [-88, 14, 74, 58, 0, 58, -30, 30], [8, -13, -245, -13, 13, -263, 45, 0], [26, 39, 0, 6, 6, -26, 0, 48], [395, -232, 0, 65, 65, -395, 0, 64], [49, -81, -168, -81, 81, -281, 56, 0], [54, -16, -54, 0, 6, 160, 38, 38], [-290, -10, 357, -10, 10, 47, 15, 0], [-33, -312, 0, 8, 8, 33, 0, -7], [-111, 150, 111, 0, 43, 174, 39, 39], [-131, -43, 0, 33, 33, 131, 0, -72], [-5, 61, -110, -49, 0, -49, 0, 0], [-200, 51, 0, 38, 38, 200, 0, 50], [437, -98, 63, 1, 0, 1, 36, -36], [-65, 205, -250, -9, 0, -9, 36, -36], [-70, 48, 0, 48, -48, 26, 49, 0], [38, 184, -296, -98, 0, -98, 14, -14], [354, -10, -99, -10, 10, 235, -40, 0], [147, -154, -147, 0, -107, -410, -7, -7], [-10, -46, 10, 0, -77, 108, -56, -56], [142, -112, -142, 0, 96, -264, 30, 30], [134, -329, 0, -22, -22, -134, 0, 27], [60, 291, 0, 7, 7, -60, 0, -19], [72, 1, -88, 1, -1, -14, -31, 0], [180, -6, -52, -92, 0, -92, -34, 34], [249, -61, 54, -8, 0, -8, -1, 1], [-155, 133, -38, 37, 0, 37, -58, 58], [-200, 556, 0, -21, -21, 200, 0, 4], [151, 158, 0, -7, -7, -151, 0, -36], [85, -113, 16, -113, 113, -125, 12, 0], [237, 37, 343, 37, -37, 654, -7, 0], [301, -288, -301, 0, 6, 355, 13, 13], [143, 160, 0, -16, -16, -143, 0, 1], [179, -236, -179, 0, 101, 118, -57, -57], [148, -163, 95, -33, 0, -33, 35, -35], [267, 255, -300, 9, 0, 9, 54, -54], [42, -111, -55, -111, 111, -235, -40, 0], [-291, 308, 291, 0, -84, -23, 17, 17], [198, 30, -78, -54, 0, -54, -6, 6], [443, -83, -275, -83, 83, 2, -37, 0], [432, -179, 0, -23, -23, -432, 0, -1], [242, -256, -242, 0, -100, -570, -14, -14], [-263, -37, -9, -37, 37, -346, 82, 0], [65, -94, -65, 0, -55, -798, -29, -29], [-48, 68, 48, 0, -208, -204, 20, 20], [-122, 317, 0, 76, 76, 122, 0, 53], [328, -62, 171, -62, 62, 375, 121, 0], [64, -211, 0, 65, 65, -64, 0, 3], [-121, 182, 121, 0, -33, 306, 61, 61], [-902, -113, 101, -35, 0, -35, -23, 23], [140, -59, -117, -59, 59, -95, -75, 0], [-199, 113, 0, 49, 49, 199, 0, -33], [-408, 292, 0, 72, 72, 408, 0, 23], [-569, 133, -5, 133, -133, -308, -79, 0], [174, -150, -174, 0, 47, -234, 24, 24], [435, -9, -116, -76, 0, -76, 49, -49], [373, -465, 0, 71, 71, -373, 0, 34], [377, -451, 0, 85, 85, -377, 0, 5], [-321, 310, 321, 0, -45, 326, -11, -11], [323, -111, -776, -111, 111, -675, 136, 0], [15, -47, -15, 0, -86, 229, -32, -32], [481, 43, 111, 43, -43, 678, 17, 0], [-166, 25, 272, 25, -25, 156, -85, 0], [-339, 359, 339, 0, 33, 709, 20, 20], [232, 124, -66, 78, 0, 78, 20, -20], [-293, 25, 0, -27, -27, 293, 0, 96], [-493, 997, 0, 10, 10, 493, 0, -32], [-241, 107, 241, 0, -94, 51, -134, -134], [-82, 102, 82, 0, 80, 1092, 20, 20], [-57, 475, -280, 69, 0, 69, -126, 126], [205, -202, -205, 0, 189, 246, 3, 3], [-10, -362, 236, -10, 0, -10, 116, -116], [-339, -553, 652, 99, 0, 99, 0, 0], [-59, -49, 0, -170, -170, 59, 0, -44], [101, -305, 13, -195, 0, -195, 97, -97], [-513, 14, 0, 86, 86, 513, 0, -40], [-780, -60, 289, -60, 60, -611, -97, 0], [-252, 110, 252, 0, -159, -32, -142, -142], [731, -165, 0, -113, -113, -731, 0, -48], [1347, -11, 16, 25, 0, 25, 20, -20], [252, -107, 0, -196, -196, -252, 0, -72], [-9, -65, 504, -65, 65, 365, -68, 0], [-295, 53, -62, 45, 0, 45, 54, -54], [265, 292, -558, -244, 0, -244, 22, -22]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_338_d_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_338_3_d_f();".
// 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_338_3_d_f(:prec:=8)
    chi := MakeCharacter_338_d();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 3));
    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_338_3_d_f();".
// 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_338_3_d_f( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_338_d();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<3,R![312, -12, -39, 0, 1]>,<5,R![19044, 64584, 109512, 30312, 4245, 90, 18, 6, 1]>,<7,R![16384, 69632, 147968, 38816, 5017, -146, 50, 10, 1]>],Snew);
    return Vf;
end function;