
// Make newform 3.8.a.a in Magma, downloaded from the LMFDB on 18 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_3_a();"
// To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
// To make the newform (type ModFrm), type "MakeNewformModFrm_3_8_a_a();".
// 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_3_8_a_a();".
// 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
        Kf := Rationals();
    end if;
    return [Kf!elt[1] : elt in input];
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_3_a();"
function MakeCharacter_3_a()
    N := 3;
    order := 1;
    char_gens := [2];
    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;

function MakeCharacter_3_a_Hecke(Kf)
    return MakeCharacter_3_a();
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 := 8;
    raw_aps := [[6], [-27], [390], [-64], [-948], [-5098], [28386], [-8620], [-15288], [36510], [-276808], [268526], [-629718], [685772], [583296], [-428058], [1306380], [300662], [-507244], [5560632], [1369082], [-6913720], [-4376748], [-8528310], [-8826814], [11986422], [7209392], [11426076], [4020950], [-17706318], [16788296], [16826772], [28044906], [-11827300], [20784630], [76112], [-32182474], [58343492], [-25836504], [63520062], [-80955900], [64503182], [56827392], [116376962], [-118816074], [-95010640], [179246372], [-206536888], [43395396], [-36193090], [92234682], [49846800], [199374002], [-394677828], [-142885374], [440240712], [275404590], [-424669768], [516158126], [-311042838], [-594308308], [115514742], [-260599804], [576794712], [-460074358], [62556126], [684235772], [-626312974], [-1253396004], [265350230], [-569635518], [932540760], [-852564664], [381183422], [-1483530820], [-761929728], [1609017990], [1880157926], [268592322], [89947850], [1690542660], [-1133327458], [2199426912], [-151737838], [990762560], [-1773756228], [-277010190], [2947584986], [-2766873378], [463552952], [-417921804], [-1509728880], [929459936], [5128027932], [-410649340], [5020408872], [-3249259170], [-2109501558], [-5289111988], [3046135142], [-4855369564], [1277621406], [4712645172], [4577997210], [4951192172], [8518472066], [-562247364], [3621104082], [-7481036760], [-5812702678], [3840510056], [1704841742], [-2808092454], [-2543654980], [-151146448], [-12362510478], [2867439812], [-4106397384], [6910997502], [3424439940], [-6764370178], [-1749591838], [8300111046], [-12123151668], [8218456532], [4722309342], [2789746910], [1519846560], [-8117607664], [-10324065658], [-13536483820], [-17193592248], [11247841832], [16306837166], [6140694522], [24506888450], [-10172166858], [-9791348044], [-9757815954], [-2787060150], [-7999831348], [-10240182138], [27868227152], [23512488876], [-12859810810], [-7998323880], [4208270462], [31930660986], [21800201180], [10472788512], [-17778707194], [-7642529838], [-27637547308], [32308710456], [22714240076], [7509250032], [-24937401040], [-8662051470], [28265471066], [-46708171458], [-46739235324], [38242017882], [-49001211184], [27292866732], [3944816706], [474320472], [12219675032], [-36069016114]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_3_8_a_a();".
// 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_3_8_a_a(:prec:=1)
    chi := MakeCharacter_3_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 8));
    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_3_8_a_a();".
// 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_3_8_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,8,sign)));
    Vf := Kernel([],Snew);
    return Vf;
end function;