
// Make newform 48.5.g.b in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_48_g();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_48_g_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_48_5_g_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_5_g_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
        poly := [1, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0], [-3, 6]];
    Rf_basisdens := [1, 1];
    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_48_g();"
function MakeCharacter_48_g()
    N := 48;
    order := 2;
    char_gens := [31, 37, 17];
    v := [1, 2, 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_48_g_Hecke();"
function MakeCharacter_48_g_Hecke(Kf)
    N := 48;
    order := 2;
    char_gens := [31, 37, 17];
    char_values := [[-1, 0], [1, 0], [1, 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 := 5;
    raw_aps := [[0, 0], [0, -1], [6, 0], [0, -12], [0, -36], [-86, 0], [426, 0], [0, -12], [0, 144], [1182, 0], [0, 300], [-430, 0], [2250, 0], [0, -516], [0, -72], [-1602, 0], [0, -684], [2114, 0], [0, 252], [0, 576], [4066, 0], [0, 1068], [0, 1764], [-2046, 0], [-2942, 0], [-11682, 0], [0, -3204], [0, -1836], [17530, 0], [12018, 0], [0, -2076], [0, 4284], [26970, 0], [0, -444], [13926, 0], [0, 3492], [-30766, 0], [0, 5172], [0, -1944], [-14010, 0], [0, -2772], [41722, 0], [0, 10368], [-40990, 0], [5646, 0], [0, -8916], [0, 4044], [0, 5268], [0, -9756], [-75254, 0], [-100830, 0], [0, 9504], [-18062, 0], [0, 19404], [-18174, 0], [0, 1224], [-30882, 0], [0, 16452], [-38438, 0], [-38382, 0], [0, -4620], [43710, 0], [0, -11412], [0, -11736], [2258, 0], [121614, 0], [0, 37236], [-145166, 0], [0, -2772], [-29854, 0], [-4878, 0], [0, 30816], [0, -11844], [-189598, 0], [0, 27804], [0, -18720], [160326, 0], [93026, 0], [185370, 0], [90130, 0], [0, -14076], [-80774, 0], [0, -6552], [176978, 0], [0, -27252], [0, 13644], [53514, 0], [-38798, 0], [-280890, 0], [0, 22116], [0, -78732], [0, -61128], [0, 45876], [0, 14580], [0, 47340], [0, 72144], [-168210, 0], [154890, 0], [0, 7596], [-218342, 0], [0, 67716], [-185802, 0], [0, -47772], [361866, 0], [0, -53052], [-477358, 0], [0, 49140], [232818, 0], [0, -133344], [-171550, 0], [0, -132636], [-245086, 0], [511602, 0], [0, 2916], [0, -117564], [730122, 0], [0, 7812], [0, -9216], [-587754, 0], [0, 112284], [-250798, 0], [-257230, 0], [513270, 0], [0, 12492], [0, -36924], [392622, 0], [480106, 0], [0, 98424], [0, 73188], [477706, 0], [0, 27660], [0, 102744], [0, 143724], [-482870, 0], [96954, 0], [1054274, 0], [-921426, 0], [0, 38340], [-96522, 0], [-336486, 0], [0, -162084], [696462, 0], [0, -2892], [0, -162108], [-1070246, 0], [0, 92304], [929042, 0], [-365430, 0], [0, 40956], [0, 46368], [-1114366, 0], [-76446, 0], [0, 124068], [0, 77688], [0, -49332], [0, 137952], [0, -104364], [470010, 0], [-621070, 0], [1072038, 0], [0, 26604], [89610, 0], [0, -131652], [0, -38772], [-735750, 0], [0, -232776], [0, -167364], [-352702, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_48_g_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_48_5_g_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_5_g_b(:prec:=2)
    chi := MakeCharacter_48_g();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 5));
    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_5_g_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_5_g_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_48_g();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,5,sign)));
    Vf := Kernel([<5,R![-6, 1]>],Snew);
    return Vf;
end function;