
// Make newform 882.4.a.j in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_882_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_882_4_a_j();".
// 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_882_4_a_j();".
// 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_882_a();"
function MakeCharacter_882_a()
    N := 882;
    order := 1;
    char_gens := [785, 199];
    v := [1, 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_882_a_Hecke(Kf)
    return MakeCharacter_882_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 := 4;
    raw_aps := [[2], [0], [-8], [0], [-40], [4], [84], [148], [-84], [-58], [-136], [-222], [-420], [-164], [-488], [-478], [-548], [692], [-908], [524], [440], [1216], [684], [-604], [-832], [-464], [-632], [160], [-2198], [-770], [-184], [1452], [-646], [-3012], [3170], [-1880], [604], [1116], [1784], [344], [-1392], [4052], [3108], [50], [162], [1544], [-1204], [2000], [-388], [4180], [1322], [-2412], [-4336], [-764], [-4300], [3860], [2800], [-4880], [-6674], [9402], [-9100], [-5952], [-3004], [-688], [5592], [2922], [-7492], [10766], [3984], [-180], [10428], [-8684], [5648], [-2546], [8268], [10872], [-10434], [-3044], [-8910], [5616], [8932], [-5538], [6700], [-5048], [-1344], [4392], [-3666], [26], [-7656], [12608], [3068], [-6456], [11896], [264], [-2628], [13568], [-20656], [-3628], [4852], [-7130], [-12788], [-2406], [25412], [9690], [5604], [-21568], [-20300], [-13812], [21996], [8368], [21504], [-10270], [-28358], [16292], [11256], [-15518], [-10452], [-72], [-11962], [6016], [26068], [-20530], [10056], [-6152], [-14716], [-28202], [22114], [9288], [23848], [-34756], [26044], [-36204], [-11424], [-16622], [38524], [-18440], [13968], [10916], [-12360], [-3402], [292], [-6910], [568], [12144], [14828], [22824], [-41780], [21420], [-18132], [-24036], [-4374], [46348], [-20660], [1800], [-41996], [41308], [3936], [7212], [-38976], [-53544], [21392], [-21162], [8224], [8140], [-32158], [41416], [12296], [57652], [-8414], [3336], [44880], [47532], [-54900], [-43338], [-7864], [7580], [55820], [8046], [37312], [13220], [-24696], [-50908], [31074], [-46068], [43700], [-64696], [66854], [-21612], [7030], [-28456], [37288], [64528], [30476], [51480], [1336], [59788], [11230], [-44774], [9844], [-62160], [22442], [-24200], [64204], [-48152], [-70260], [-46656], [-24328], [-36864], [24070], [6028], [30414], [-3768], [52408], [-16116], [-19184], [-31448], [-24536], [-7980], [65316], [-100858], [-74462], [43344], [-38706], [-38464], [51708], [-10606], [-30044], [-2012], [-31488], [-42432], [-62550], [-3268], [48328], [-8746], [-62172], [-3008], [24752], [-12014], [-47240], [-30368], [-69656], [-33292], [9808], [84854], [-67356], [60024], [67568], [-23700], [-59708], [102988], [77478], [-17636], [11724], [53240], [-24000], [-66280], [-100174], [-66292], [-82176], [71608], [11528], [83512], [-102308], [130212], [-8172], [52108], [-113718], [94572], [-5380], [-28562], [-74532], [-58124], [-138008], [9904], [50248], [133984], [-66088], [-17018], [60010], [45708], [88880], [-58360], [-74104], [49540], [-51676], [7692], [-76078], [62610], [79688], [12084], [-33366], [-76484], [-132438], [104380], [5526], [13208], [-47200], [-98000], [142452], [-28612], [74120], [-35946], [142912], [4032], [-119052], [16766], [-135088], [32524], [129684], [35518], [149616], [41502], [59058], [-96220], [-54284], [9592], [-2828], [68492], [-52168], [-112046], [58300], [-55546], [27312], [-53048], [159254], [95232], [167004], [-81324], [-23892], [-76018], [23510], [90582], [-33912], [-6172], [-28852], [126716], [-99590], [-844], [38872], [-66168], [-15154], [40986], [-2320], [93072], [-106138], [-8304], [-124140], [-102740], [183408], [-118848], [168204], [199082], [215978], [34072], [25166], [-48356], [-57432], [37564], [-106466], [-207052], [93278], [-83868], [120156], [-170096], [36596], [-179466], [-177408], [6944], [-25606], [-35328], [118204], [31268], [-68014], [-150064], [-89786], [-144356], [-159500], [141328], [-31004], [186992], [129672], [57846], [-138000], [183806], [93820], [-239568], [-123912], [-30084], [-221572], [-88296], [-74962], [118232], [146000], [174332], [-144860], [-152522], [-48600], [-66780], [-57124], [-76434], [-55820], [71374], [259664], [-125284], [21168], [-146792], [116854], [-55026], [-152564], [9540], [-84656], [-85554], [-32272], [22732], [219030], [111696], [90972], [15176], [75476], [118848], [308678], [-46492], [-123276], [94740], [73640], [4400], [-180840], [83994], [-164932], [-239336]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_882_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_882_4_a_j();".
// 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_882_4_a_j(:prec:=1)
    chi := MakeCharacter_882_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 4));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(2999) - 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_882_4_a_j();".
// 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_882_4_a_j( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_882_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![8, 1]>,<11,R![40, 1]>,<13,R![-4, 1]>],Snew);
    return Vf;
end function;