
// Make newform 24.8.a.c in Magma, downloaded from the LMFDB on 20 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_24_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_24_8_a_c();".
// 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_24_8_a_c();".
// 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_24_a();"
function MakeCharacter_24_a()
    N := 24;
    order := 1;
    char_gens := [7, 13, 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_24_a_Hecke(Kf)
    return MakeCharacter_24_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], [110], [504], [3812], [9574], [26098], [-38308], [-71128], [74262], [-275680], [-266610], [684762], [245956], [478800], [-569410], [-1525324], [-2640458], [1416236], [-3511304], [4738618], [4661488], [-5729252], [11993514], [7150754], [-8783730], [-8016104], [-5151228], [-24127994], [2048274], [1366336], [38464492], [7625850], [5323236], [7613662], [-25022104], [39314518], [-62838708], [10413336], [-80355066], [-84008388], [115468926], [99715392], [-18615742], [93038382], [73968568], [185162524], [-120861728], [-282315252], [-89191346], [-232239718], [321986320], [-200366190], [-87056012], [522878722], [-406215496], [382347046], [284164784], [293751838], [415399402], [50642900], [74771406], [85257660], [939128712], [-343039542], [-1039599114], [1100218916], [1282722354], [1258218516], [135370902], [-849220958], [220121304], [466504912], [-298453186], [1462182644], [1587019392], [-314439442], [-852757402], [692521842], [617356650], [-1655118516], [70147214], [1813871056], [-2599702574], [-1674308664], [-252710732], [755311298], [-151584070], [-778405402], [2410524656], [-1761915492], [643811488], [3164213272], [3624060036], [-1354831620], [-4663887288], [-1342922442], [-1453998086], [490309348], [-4828891050], [-3087925300], [4098885254], [4971047228], [3713157962], [-2362051404], [-181145662], [2319762212], [2278061618], [-4882528536], [-6747581270], [-9059283616], [-8487765682], [2344071450], [-1015405820], [7019107720], [-4527757822], [-8630937492], [2574005176], [-9318265818], [10442219100], [-10476122338], [13889123170], [-748893490], [11558080580], [334337468], [-5553834378], [13081703534], [-11084656720], [-7794161880], [6835521686], [17386249100], [2255370200], [-20502658992], [-257872066], [-13445215862], [8285405186], [14343001326], [-3831371684], [-1958590570], [2074149082], [-5715081788], [28274766270], [-2092831288], [-3714529868], [3379244854], [16490706936], [-4770281890], [16151444778], [-341593068], [6078778016], [12385229958], [-13780117742], [1892959484], [4239115336], [-9513671324], [-11623545232], [-9229430936], [-40935344414], [-1750850598], [5911019526], [-22608905412], [11177284554], [15551750072], [8345078820], [-9851799246], [37088357352], [6435262048], [-12307114034]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_24_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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