
// Make newform 336.6.q.c 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_6_q_c();".
// 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_6_q_c();".
// 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 := [1, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    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], [1, 0], [1, 0], [0, -1]];
    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 := 6;
    raw_aps := [[0, 0], [9, -9], [0, 6], [-126, 133], [-666, 666], [-559, 0], [1740, -1740], [0, 1157], [0, -3468], [3372, 0], [6293, -6293], [0, -3131], [-4866, 0], [11407, 0], [0, 2310], [28296, -28296], [20544, -20544], [0, 4630], [-18745, 18745], [38226, 0], [-70589, 70589], [0, -62293], [-79818, 0], [0, 18120], [124754, 0], [93390, -93390], [0, -167731], [0, 69180], [219559, -219559], [-39354, 0], [-317093, 0], [0, -154830], [-67332, 67332], [365215, 0], [0, 168060], [153536, -153536], [-202418, 202418], [0, -179764], [-217302, 0], [0, 73980], [789366, -789366], [-477739, 0], [0, 358974], [181933, -181933], [717924, 0], [203096, -203096], [-1170980, 0], [-1246352, 0], [-918942, 918942], [0, -1203749], [0, 919062], [625338, 0], [-1253822, 1253822], [1513332, 0], [0, 1554930], [-1115316, 1115316], [-35670, 35670], [0, -292768], [-863213, 863213], [1471104, 0], [688841, -688841], [722832, 0], [20125, 0], [-1743558, 1743558], [0, -1808543], [0, -1023552], [0, -1007533], [-1565707, 0], [757284, -757284], [-455638, 0], [3631386, -3631386], [0, -4024836], [2577875, -2577875], [0, 2531329], [3066769, 0], [0, -3925200], [4026690, -4026690], [0, -4574399], [0, 2269440], [4045963, -4045963], [3912810, 0], [-2780863, 0], [-4382070, 4382070], [1249463, 0], [0, -6744208], [0, 478896], [724506, 0], [0, 2339563], [2982468, 0], [-4284227, 0], [0, -5740350], [2650512, -2650512], [2805539, -2805539], [4684500, 0], [0, 1475753], [-63606, 0], [0, -6211566], [-1412052, 1412052], [0, 5229347], [0, -4413725], [11903764, 0], [-12913302, 12913302], [-11369838, 11369838], [0, 5697954], [-7042207, 7042207], [2581969, -2581969], [-4694592, 0], [0, -13423506], [5046012, -5046012], [-10639081, 0], [0, 1416083], [-9463034, 9463034], [12938778, 0], [-3803761, 3803761], [9174976, 0], [-10245372, 10245372], [5723455, 0], [-9987942, 9987942], [0, 1198878], [-11806536, 0], [-4720391, 4720391], [-8708257, 0], [0, -5111052], [-17719776, 17719776], [0, -22599337], [-818148, 0], [0, 5091826], [0, -480858], [14078329, 0], [0, -2039321], [-16495699, 16495699], [23812086, 0], [0, 1924961], [8980922, 0], [0, -14599092], [27838055, 0], [28285722, -28285722], [9872, -9872], [-10891908, 0], [5235294, -5235294], [-12614744, 0], [0, 15342378], [-14959504, 14959504], [-28049790, 0], [-14731097, 14731097], [-23634660, 0], [14396459, 0], [-12470976, 12470976], [0, -12505888], [0, 28028922], [0, -23117282], [-9598908, 0], [-443183, 0], [0, -24009666], [17585375, -17585375], [29559852, 0], [0, -3914821], [0, 3220086], [-51650353, 0], [7286976, -7286976], [0, -31244778], [-26827320, 0], [10025665, 0], [0, 115800], [-8738094, 8738094], [23841264, -23841264], [-33371923, 33371923], [-54468659, 54468659]];
    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_6_q_c();".
// 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_6_q_c(:prec:=2)
    chi := MakeCharacter_336_q();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 6));
    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_6_q_c();".
// 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_6_q_c( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_336_q();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<5,R![36, -6, 1]>],Snew);
    return Vf;
end function;