
// Make newform 48.5.g.a in Magma, downloaded from the LMFDB on 25 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_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_48_5_g_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
        poly := [1, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0], [-1, 2]];
    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, -3], [-42, 0], [0, 44], [0, -12], [-182, 0], [-246, 0], [0, -68], [0, -432], [78, 0], [0, 852], [530, 0], [-918, 0], [0, -492], [0, 2184], [-4626, 0], [0, -132], [1346, 0], [0, 628], [0, -1056], [-926, 0], [0, -2540], [0, 6924], [11586, 0], [-13118, 0], [-5490, 0], [0, -3292], [0, 5820], [-16166, 0], [1842, 0], [0, -228], [0, -204], [-13254, 0], [0, 7628], [438, 0], [0, -16196], [19346, 0], [0, -20932], [0, -10824], [-34410, 0], [0, 9732], [15706, 0], [0, 1536], [-26782, 0], [-52482, 0], [0, -13324], [0, 13796], [0, 492], [0, 43980], [-48470, 0], [48738, 0], [0, 40992], [73138, 0], [0, -53052], [-48894, 0], [0, 45144], [-71538, 0], [0, -62468], [-120518, 0], [-3054, 0], [0, 76764], [151662, 0], [0, -3132], [0, -81480], [-128686, 0], [-73986, 0], [0, 32924], [98674, 0], [0, 32868], [181346, 0], [-4302, 0], [0, -106944], [0, 105476], [151778, 0], [0, 20788], [0, -37728], [105750, 0], [-27934, 0], [237882, 0], [-20270, 0], [0, -13908], [116698, 0], [0, 205272], [-199726, 0], [0, 84564], [0, 29124], [149994, 0], [284338, 0], [-183402, 0], [0, 99548], [0, -39684], [0, -144312], [0, 156524], [0, -131556], [0, -143676], [0, 257808], [-39330, 0], [-464598, 0], [0, -78236], [360442, 0], [0, -151060], [-233274, 0], [0, 242316], [470058, 0], [0, 184972], [-341038, 0], [0, -65892], [-96846, 0], [0, -300096], [-627742, 0], [0, -76900], [247202, 0], [-31758, 0], [0, 378796], [0, 241180], [-152214, 0], [0, -412308], [0, -149856], [-330714, 0], [0, 146580], [-722158, 0], [-552910, 0], [609030, 0], [0, -13692], [0, 249324], [44958, 0], [533002, 0], [0, 168648], [0, -436356], [-832982, 0], [0, 403044], [0, 266184], [0, 541012], [295786, 0], [-1026150, 0], [362306, 0], [1026078, 0], [0, -490772], [761478, 0], [247674, 0], [0, -531276], [-250242, 0], [0, -231380], [0, -9972], [-15686, 0], [0, -66672], [345938, 0], [-267990, 0], [0, -301612], [0, 477408], [1116290, 0], [19170, 0], [0, 328460], [0, -629400], [0, 528964], [0, -574752], [0, -56052], [-1278822, 0], [-981262, 0], [284406, 0], [0, -573372], [602922, 0], [0, -332444], [0, -715452], [-1040742, 0], [0, 547752], [0, 355652], [-535870, 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_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_48_5_g_a(: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_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_48_5_g_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_48_g();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,5,sign)));
    Vf := Kernel([<5,R![42, 1]>],Snew);
    return Vf;
end function;