
// Make newform 48.6.a.b in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_48_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_48_6_a_b();".
// 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_48_6_a_b();".
// 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_48_a();"
function MakeCharacter_48_a()
    N := 48;
    order := 1;
    char_gens := [31, 37, 17];
    v := [1, 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_48_a_Hecke(Kf)
    return MakeCharacter_48_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 := 6;
    raw_aps := [[0], [-9], [38], [-120], [-524], [-962], [-1358], [2284], [-2552], [3966], [2992], [13206], [-15126], [7316], [6960], [-17482], [-33884], [39118], [-32996], [-14248], [-35990], [29888], [51884], [30714], [-48478], [93222], [-2296], [-38988], [6238], [213618], [-205072], [-350116], [-234486], [-16428], [-92426], [-350984], [46318], [394908], [-514344], [-497874], [-711252], [471366], [646080], [-826558], [-126138], [119144], [-341620], [523088], [-1032612], [50422], [-991030], [-514864], [480498], [768260], [316162], [1330168], [812590], [1992352], [356134], [644986], [677188], [414822], [362028], [-1543416], [-1542246], [33246], [1607380], [-1449582], [-474588], [-2988690], [3264802], [-1924296], [1500128], [-4701850], [-1515260], [-155520], [-3050842], [196574], [-752910], [5616954], [6352668], [5809910], [-1310992], [3761266], [-1001160], [-4015420], [322466], [-6649942], [1177934], [-5859488], [4280556], [-5756224], [5631272], [3468852], [-5988372], [-3130584], [3119646], [4349386], [2085236], [-2167218], [1025116], [9084014], [8459212], [-11633350], [4018404], [-3552958], [-6294956], [9019346], [12431496], [4744762], [-3497840], [-358762], [-12638838], [-10670476], [-13262056], [7298786], [1977468], [272792], [4138446], [-445812], [-3658810], [-14244446], [-13312762], [1494676], [11132044], [-15232674], [-8599210], [2848912], [8116152], [-11403826], [2287804], [29235928], [-17131104], [3317318], [12894778], [18762242], [6844422], [29127212], [31248542], [-12732374], [-26519404], [-28413450], [-32163944], [30143588], [-20116370], [22068504], [11273990], [-16741158], [32643492], [25402912], [3558462], [-41015630], [-35173556], [-43307128], [13319156], [-8925776], [-41398216], [34248130], [5444682], [35475246], [-12250452], [4771386], [43193896], [-17362956], [-17167950], [-35399256], [52334000], [-7210346]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_48_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_48_6_a_b();".
// 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_48_6_a_b(:prec:=1)
    chi := MakeCharacter_48_a();
    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_48_6_a_b();".
// 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_48_6_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_48_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<5,R![-38, 1]>],Snew);
    return Vf;
end function;