
// Make newform 48.8.a.b in Magma, downloaded from the LMFDB on 17 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_8_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_8_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 := 8;
    raw_aps := [[0], [-27], [-114], [1576], [-7332], [-3802], [-6606], [-24860], [-41448], [-41610], [-33152], [-36466], [-639078], [156412], [433776], [786078], [-745140], [-1660618], [3290836], [-5716152], [2659898], [-3807440], [-2229468], [5991210], [-4060126], [-17281938], [14362312], [-6454404], [-884410], [12132498], [-6868064], [39520788], [19174074], [-13244900], [57362430], [31087288], [-33783466], [-62665868], [-62707224], [-27052122], [134280900], [114660542], [-16360512], [-154198462], [83228814], [76172200], [-35244572], [189131392], [176100276], [65039630], [-251318502], [-213079440], [257283602], [-123057972], [-443333886], [-298924728], [208907910], [11274928], [-658964386], [-105122838], [-330161108], [-87100242], [391708996], [204935928], [877202378], [-440830506], [-1112230052], [288198194], [1106006316], [-1321843690], [1203951138], [1320572520], [-1751069744], [-487945282], [-1110068660], [-1869115008], [-273895410], [624551654], [555500082], [-2157700630], [-1677967500], [-525233458], [-170592912], [-1687969582], [1178496280], [-715754868], [-1373583870], [1847520314], [3094137222], [-3004508048], [2992515876], [-1840414560], [426675976], [-60754692], [-3245877500], [744381432], [-444155370], [3049626042], [1403064412], [4211059862], [-1999559564], [-3374032986], [5580213252], [-888309750], [1791706828], [3821026754], [-4362188964], [6385307058], [-804297000], [-4871616598], [-7175168384], [3478905038], [-239378406], [5529588220], [6136833688], [10703801922], [1398026452], [5316047496], [3244034118], [5165059620], [-3225154018], [-2006334622], [10021103214], [5848606332], [-2586855932], [-1744599978], [-11205076690], [936568080], [-4204452104], [-11549138602], [13965501940], [-14383247448], [670840528], [-19156941826], [17911981002], [-21407177470], [-755162802], [-20466541724], [-10309775946], [-416427750], [5826870268], [-20833271778], [-4234025768], [-5705967924], [25161237110], [-22704795960], [28687212638], [5349500394], [14032979500], [-32499400608], [-33869400826], [-15270829038], [11204511172], [-6972321864], [-1180948004], [-12791515632], [-35235279560], [-21776443230], [11579464154], [-38393035098], [-11184090684], [-5198349942], [269243176], [-4372831332], [-37499076366], [30618970392], [17270335168], [-3750774706]];
    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_8_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_8_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, 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_48_8_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_8_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_48_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,8,sign)));
    Vf := Kernel([<5,R![114, 1]>],Snew);
    return Vf;
end function;