
// Make newform 5.8.a.a in Magma, downloaded from the LMFDB on 09 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_5_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_5_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_5_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_5_a();"
function MakeCharacter_5_a()
    N := 5;
    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_5_a_Hecke(Kf)
    return MakeCharacter_5_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 := [[-14], [-48], [125], [-1644], [172], [3862], [-12254], [-25940], [12972], [-81610], [-156888], [110126], [467882], [-499208], [-396884], [-1280498], [-1337420], [-923978], [-797304], [5103392], [-4267478], [-960], [6140832], [2010570], [-4881934], [9726702], [16315052], [-4089744], [-26831770], [-17481038], [-12501764], [-7756188], [36172026], [10909220], [-36458050], [7189552], [87993206], [-54887488], [8614596], [-51252378], [-50162660], [69081662], [154745432], [-15940558], [-168188034], [177772600], [-161996428], [17593172], [-203035864], [159558990], [-19498518], [-160220480], [-378778718], [161304252], [227387106], [457727612], [467286310], [-44593208], [316657086], [-225817638], [20821032], [-178825458], [-85515944], [-484705728], [570821162], [-550190554], [-939838788], [533631826], [1079338816], [-427217450], [-1489659198], [841275480], [-750461604], [171073822], [46623940], [-442265868], [-464032530], [-317792234], [-1195629198], [2213047130], [-802298740], [34471342], [1726923912], [488307682], [-2886397880], [926583192], [1355352850], [46342906], [-152117178], [1634503812], [-1113803704], [1277448240], [979672676], [-4921252468], [-3657864500], [-3883580548], [3900717430], [2883986042], [877188472], [-653484618], [-4592987384], [6831640806], [3425094912], [-7509302790], [-135841108], [1637750786], [-5972051424], [8313465842], [9783675800], [5401594202], [-2844394244], [-7021057538], [3351655146], [-3923624140], [-681544688], [9651992082], [-5078260608], [-2083303484], [6848810102], [6999128380], [-1995943778], [-11793904878], [7802218686], [4511529432], [-10633131068], [-4385141098], [-5988049970], [-11776791840], [8410505636], [14408392982], [8217083020], [-17253067508], [15849821752], [7138564606], [-2599929078], [-4964772190], [-14968115778], [-9799901624], [3931691366], [12632426330], [11665325772], [-8174997858], [-17521126228], [12222545536], [-10663419210], [-2313998680], [-4223767698], [-3521037494], [24493004980], [-5405729788], [-28915499514], [780642562], [-13690694368], [3584034276], [-30110804544], [-24879995528], [10842048760], [10704503090], [34278689226], [-3736949818], [33214992216], [46989547402], [-14229376204], [545473692], [-19791579614], [-47150273268], [38796825032], [-56639400834]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_5_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_5_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_5_8_a_a(:prec:=1)
    chi := MakeCharacter_5_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_5_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_5_8_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_5_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,8,sign)));
    Vf := Kernel([<2,R![14, 1]>],Snew);
    return Vf;
end function;