
// Make newform 338.3.f.h in Magma, downloaded from the LMFDB on 04 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_338_f();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_338_f_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_f_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_338_3_f_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 := [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], [349, 4, -37, 0, 1, 0, 0, 0], [-349, 4, 37, 0, -1, 0, 0, 0], [1396, -12865, 0, 1718, 0, -74, 0, 1], [2024898, -296126, -345859, 44290, 19195, -2262, -349, 40], [2024898, 296126, -345859, -44290, 19195, 2262, -349, -40], [-6980, 0, 1053, 0, -55, 0, 1, 0], [-4828764, 1626083, 734994, -292601, -38390, 16901, 698, -318]];
    Rf_basisdens := [1, 8, 8, 2792, 86552, 86552, 62, 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_f();"
function MakeCharacter_338_f()
    N := 338;
    order := 12;
    char_gens := [171];
    v := [5];
    // 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_f_Hecke();"
function MakeCharacter_338_f_Hecke(Kf)
    N := 338;
    order := 12;
    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 := 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 := 3;
    raw_aps := [[0, 0, 0, -1, 1, 0, 0, 0], [0, 0, 1, 0, 0, -1, 0, 0], [0, -1, 0, 1, 0, 1, 0, 1], [-2, 0, 1, 1, 0, 1, -1, 0], [0, -1, 0, -5, -5, -1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0], [3, -1, -2, 3, 1, -8, -1, 1], [-6, -2, 1, -1, -10, 1, 3, -2], [6, -2, -1, -4, 14, -1, 0, 2], [-1, 0, -5, -2, 1, 3, -2, 1], [2, 3, 3, 1, -5, 6, -3, 0], [12, 6, 5, -14, -14, 18, -6, 5], [-17, 8, 6, 15, -15, 25, 8, -6], [12, 1, 2, 12, -1, 17, 6, -6], [4, -1, 1, 9, 3, -12, 1, -2], [-19, 2, 2, -5, 24, 26, -5, 10], [-44, -6, 1, 15, 22, 15, -7, 6], [11, 2, 0, -11, 6, -2, 4, 4], [8, -11, -12, -11, 11, -19, -11, 12], [-10, 6, 11, 25, 10, -25, 5, 6], [8, 3, 2, 27, 6, -33, 2, 1], [-12, 16, 16, 0, -40, -24, 0, 0], [-12, -5, 3, -43, 9, -52, -3, 8], [-66, -1, 6, 4, 4, -67, 1, 6], [-21, 10, 7, 12, -6, -12, -3, 10], [76, -16, -8, -39, -20, -8, 0, 2], [-22, 4, -4, 40, -58, 62, -4, 0], [14, 0, -1, 4, 24, -47, 28, -14], [7, -10, -8, -24, 1, -25, 8, 2], [25, -3, 0, -25, 77, -40, -1, -1], [-80, -26, -13, 28, -14, -13, 0, 24], [106, 2, 2, 8, 0, 2, 8, -16], [42, 13, -8, -19, -2, -19, 21, -13], [6, -21, 0, -6, -151, 65, 4, 4], [46, -25, -4, -39, -14, 39, 21, -25], [18, -5, -5, -81, 23, 58, -5, 0], [-90, -27, -27, -8, -46, -73, -8, 16], [-20, 6, 7, -67, 86, -67, -1, -6], [-96, 25, 8, 73, 73, -71, -25, 8], [97, -4, -8, 97, 4, 34, -25, 25], [148, 6, 3, -58, 60, 3, 0, -32], [70, 5, -5, -120, -50, 55, 20, 0], [-40, -1, 0, 40, 121, -61, 16, 16], [95, -26, -8, 39, 39, 69, 26, -8], [-32, -47, -24, -52, 52, -15, -47, 24], [42, 5, 10, 42, -5, -133, -18, 18], [-2, 0, 35, -172, -88, 141, -4, 2], [94, 17, 34, -87, 87, -77, 17, -34], [72, 36, 13, -35, 60, 35, -23, 36], [-123, 18, 48, 136, -171, 35, 48, -30], [20, -18, 18, -16, 174, -192, 24, 0], [152, -1, -29, -87, -123, 36, 29, -28], [172, 1, -12, -219, 60, -219, 13, -1], [26, 25, 50, 26, -25, 35, -30, 30], [116, 36, 18, -81, -36, 18, 0, 46], [-22, 0, -19, 70, -58, 135, -44, 22], [-91, -22, 0, 91, 138, -80, -35, -35], [30, 21, -2, -87, -87, 51, -21, -2], [43, -26, -52, 43, 26, -286, 6, -6], [-75, -16, -21, 86, -54, -32, -21, 5], [-380, -42, -21, 202, 160, -21, 0, -24], [-66, -6, 6, -3, 57, -3, -12, 6], [-70, 5, -47, -27, -23, 50, -47, 52], [170, -8, 8, -340, 98, -106, 0, 0], [24, 20, 20, 36, -124, -104, 36, -72], [-141, 26, -11, -26, 152, -178, 11, -37], [-226, -12, -37, 185, -16, -185, -25, -12], [-70, -51, 51, 182, -148, 97, 42, 0], [302, 31, 0, -302, -335, 183, 10, 10], [-18, -3, -34, 84, 84, -21, 3, -34], [76, -12, -1, 116, -116, -88, -12, 1], [0, 41, -11, -31, 11, 20, -11, 52], [8, 0, 87, -204, -92, 97, 16, -8], [-19, -61, 0, 19, -61, 0, 55, 55], [112, -5, -12, 163, -163, -117, -5, 12], [320, 2, -43, -131, 234, 131, -45, 2], [-42, -15, 15, 40, 72, -87, -44, 0], [71, 32, 43, -250, 168, -250, -11, -32], [100, -48, -33, -20, -20, 52, 48, -33], [-144, -12, 50, 201, -5, -201, 62, -12], [-38, 0, -51, 20, 116, -181, -76, 38], [111, -60, -85, -62, -26, -36, 85, -25], [134, -9, 22, -155, 155, -143, -9, -22], [-57, 30, 60, -57, -30, 266, -18, 18], [-14, -6, -3, 22, 12, -3, 0, -30], [0, -28, -28, 30, -16, -44, 30, -60], [-237, -32, -45, 18, 232, 18, 13, 32], [9, 48, 42, 65, -65, 39, 48, -42], [164, -65, -64, -395, -232, 395, 1, -65], [88, -81, -25, 87, 113, -200, -25, -56], [16, 38, -38, -64, 214, -176, -32, 0], [-382, 15, 10, 57, 57, -367, -15, 10], [-330, -8, 7, 33, -312, -33, 15, -8], [304, -78, -39, -150, -24, -39, 0, -4], [98, 105, 33, 43, -131, 174, -33, -72], [54, -49, 0, -54, -171, 61, 0, 0], [-1, 50, 12, -149, 149, 51, 50, -12], [402, -37, -37, -36, -62, -99, -36, 72], [36, 0, 27, 110, 214, -455, 72, -36], [71, 48, 97, 48, -26, -22, 97, -49], [-74, 84, 84, -14, 198, 282, -14, 28], [129, -40, 10, 245, 245, 89, 40, 10], [254, -7, -14, 254, 7, -263, 100, -100], [-113, 112, 56, 46, -154, 56, 0, 21], [112, 30, -30, -158, -122, 152, 66, 0], [-112, -49, -22, 329, 134, 195, 22, 27], [-310, -19, -26, 351, -351, 291, -19, 26], [-104, 1, -30, 89, -74, -15, -30, 31], [122, 126, 126, 34, -40, 86, 34, -68], [-1, 0, -9, -242, -53, 115, -2, 1], [118, -21, 0, -118, -171, 75, 58, 58], [-552, 4, 25, 356, -356, 556, 4, -25], [338, 7, 36, -151, 158, 151, 29, 7], [40, -113, -101, -129, 141, -12, -101, -12], [580, -30, 7, -311, -306, -311, -37, 30], [295, 13, 26, 295, -13, 656, 7, -7], [286, 16, -1, -143, 160, 143, -17, 16], [-314, 114, 57, 236, -354, 57, 0, -158], [35, 0, 2, -80, -130, 258, 70, -35], [-276, 63, 0, 276, -555, 309, -54, -54], [-16, -40, 111, -124, -124, -56, 40, 111], [515, -34, -17, -308, 331, -17, 0, 101], [150, 60, 60, 6, 24, 84, 6, -12], [168, 120, 37, -277, 192, -277, 83, -120], [178, -1, 22, 253, -253, -179, -1, -22], [256, -14, 14, -598, -328, 314, -86, 0], [-272, -45, -82, 337, -28, 337, 37, 45], [120, -29, -58, 120, 29, -733, 26, -26], [-68, 20, -20, -92, -252, 272, -228, 0], [46, 23, 76, -317, -122, -195, -76, 53], [-354, 121, 62, 437, 437, -233, -121, 62], [214, 3, -62, -147, 147, -211, 3, 62], [270, -122, -61, -182, -124, -61, 0, 94], [-23, 0, -58, 914, -78, 214, -46, 23], [23, 134, 75, -22, 58, -22, 59, -134], [-146, -33, -82, -86, 86, 113, -33, 82], [-67, -72, -23, 408, 292, -408, 49, -72], [-574, -54, 79, 303, 138, 303, -133, 54], [127, 24, 48, 127, -24, -60, -23, 23], [49, 0, -27, -310, 67, -107, 98, -49], [-444, 37, 71, 465, 373, 92, -71, 34], [456, 5, -80, -74, 74, -451, 5, 80], [-276, -11, -22, -276, 11, 5, 34, -34], [-76, -111, 25, 665, -101, -564, 25, -136], [-148, 64, 32, 47, -276, 32, 0, 54], [592, -60, -17, -567, -68, -567, -43, 60], [56, 25, -60, -247, 116, 131, -60, 85], [-359, 20, -20, 731, 370, -350, 13, 0], [290, -98, -98, -20, 144, 46, -20, 40], [320, -123, -27, -25, -293, 268, 27, 96], [546, -10, 32, 493, 997, -493, 42, -10], [254, 268, 134, -107, 56, 134, 0, -40], [-102, 20, -20, 264, 1010, -990, 60, 0], [-126, 0, -57, -138, 406, -755, -252, 126], [-218, -6, -3, 202, -448, -3, 0, -186], [-136, -106, -106, -116, -246, -352, -116, 232], [240, 99, 0, -240, 1205, -553, 0, 0], [-234, 170, 44, 59, -49, -59, -126, 170], [-191, 98, 98, -97, -208, -110, -97, 194], [427, 126, 86, -14, -513, 499, -86, -40], [-252, -97, 60, -551, -551, -349, 97, 60], [-93, -142, -284, -93, 142, -284, 17, -17], [-618, -65, -113, 165, 731, -566, 113, -48], [-1372, 45, 0, 1372, 27, 9, -20, -20], [35, -72, 124, 145, -145, -107, -72, -124], [6, -65, -133, -569, 139, 430, -133, 68], [54, 0, 99, 304, 8, -115, 108, -54], [-21, -222, 0, 21, -850, 314, -22, -22]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_338_f_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_338_3_f_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_338_3_f_h(:prec:=8)
    chi := MakeCharacter_338_f();
    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_f_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_338_3_f_h( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_338_f();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<3,R![97344, 3744, 12312, -468, 1209, -24, 39, 0, 1]>,<5,R![19044, -64584, 109512, -30312, 4245, -90, 18, -6, 1]>,<7,R![16384, 96256, 170624, 65776, 11257, 1430, 161, 8, 1]>],Snew);
    return Vf;
end function;