
// Make newform 24.8.a.b 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_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_24_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_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], [-530], [120], [-7196], [-9626], [18674], [7004], [-63704], [29334], [87968], [227982], [-160806], [136132], [-1206960], [-398786], [1152436], [-2070602], [-4073428], [-383752], [3006010], [-4948112], [-9163492], [7304106], [-690526], [13266702], [4868248], [7918596], [-4930298], [2317074], [-22511936], [-13589524], [714618], [17881572], [-20039074], [-40763416], [55470166], [-1803444], [5992728], [125630598], [48649788], [-49754754], [111324480], [-134786494], [39755694], [-103320776], [-179474660], [285310816], [-27479796], [-85656242], [104906650], [-27718384], [-295271022], [-302885260], [41463554], [420007096], [156477350], [-271490512], [-98031842], [-404838934], [241438676], [-195051954], [-102082116], [304912776], [647440842], [-148934538], [968290340], [165423666], [-1124551404], [-255382890], [345150626], [-811306536], [-776624816], [1667904830], [-1250065420], [1433738880], [731612782], [1190591078], [-2446377870], [1836634986], [-2778538932], [1166249870], [-290479408], [2206505938], [99756360], [-818947660], [-2462406206], [-2761354054], [1483079014], [-1425912976], [1995789468], [566385056], [-1236092264], [-749612412], [-3656423172], [-2193895608], [4248654774], [-370617094], [6336450532], [-4007922858], [-3660460852], [5256297478], [-5019854276], [2804950090], [6104541876], [-302864446], [-2423700188], [-6855380942], [147202536], [7158843562], [4083334880], [-6810805426], [3951609114], [-10783545596], [11044348936], [-5937968894], [-8249448084], [-6491445064], [-1044060762], [11112564828], [-1701049570], [-7459384478], [-5804728754], [-7269028028], [11729799356], [-9887431434], [-14328441490], [-17360709968], [-12773966424], [6124190102], [10533521804], [7553916376], [-13127324976], [-5721255874], [16377144458], [-6596342782], [-6241259154], [16932756316], [12557859094], [-2518596646], [27076006084], [27008869950], [12388298312], [-22856233036], [-8687419850], [11679172344], [2254521950], [-4966046166], [15403923732], [4612253344], [35750464134], [-24830856430], [9379955708], [5989397576], [1765682788], [-41823154064], [-37165770776], [25657994210], [-5753525286], [-46059907194], [-93470916], [11728910538], [-16478577352], [-18843582684], [-42666068430], [4345270248], [13623873760], [-43278776882]];
    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_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_24_8_a_b(: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_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_24_8_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_24_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,8,sign)));
    Vf := Kernel([<5,R![530, 1]>],Snew);
    return Vf;
end function;