
// Make newform 8.10.a.b in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_8_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_8_10_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_8_10_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_8_a();"
function MakeCharacter_8_a()
    N := 8;
    order := 1;
    char_gens := [7, 5];
    v := [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_8_a_Hecke(Kf)
    return MakeCharacter_8_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 := 10;
    raw_aps := [[0], [68], [1510], [10248], [3916], [-176594], [148370], [499796], [-1889768], [-920898], [1379360], [5064966], [-24100758], [25785196], [-60790224], [29496214], [51819388], [33426910], [144856196], [68397128], [168216202], [235398736], [-64639852], [-78782694], [-24113566], [-625963194], [800617576], [2453581740], [-929042546], [-1651290126], [-1830418304], [560254148], [6542324682], [5527223244], [-3253285642], [-8544191848], [-269870786], [10290309348], [123946152], [4927695822], [-5545642764], [11615045142], [-27240394560], [-28874327614], [38016874662], [25738576072], [5500638484], [20598256864], [-44684185308], [-5693229626], [17403231274], [-33598752016], [-60861916302], [87438894140], [71136807170], [-52474117880], [-137809547986], [118786402064], [-62071562378], [72092740058], [39015112796], [-93721442298], [-163239259788], [-55401189576], [235840578042], [-77407002402], [-79009093492], [215728294866], [-238976068452], [5144912382], [57117223010], [289030075752], [-168811928720], [-606879501610], [-467635872580], [-390199234176], [-161508322970], [818137886126], [-108197398254], [-756199508646], [664700208612], [-696689350522], [-1377280400464], [-226718778830], [4034838456], [1089297772796], [1061069230786], [752126750474], [1223345628142], [1007099500496], [1113615251988], [794323531232], [1752258145768], [-2499791790804], [-303713645004], [-1568823390216], [-2835716413026], [-1436428809910], [-978444162740], [-285542036802], [-2872963406236], [1405945120078], [-350499556748], [3177291705082], [-3021253222020], [-3015368286142], [3767563166092], [-4384780365614], [-167902839720], [2307286184218], [-1502476612000], [3940766099270], [-3750978228822], [3299467114924], [-7225478389640], [4438419304834], [-295078480188], [2078797845128], [-1808598289746], [-6581504557356], [7797469465334], [7861294979746], [-6113879586938], [345133444588], [5182276982516], [-9334837506978], [-763912346458], [-3651759101744], [4066919648472], [-10012489368706], [-5956221938908], [8851057970920], [13103874346224], [13107219512918], [4349019382202], [-9230953787902], [-4894146029850], [15777243858772], [13908966283198], [381207784298], [-9349154645396], [-11732988630378], [6285334384952], [18073776368252], [-13476625543970], [21412241302344], [12008680876982], [-28948974203622], [23141254571676], [-6845036013728], [22428883756686], [12421175090930], [-5528782562380], [18492419823992], [27249413891788], [-7311941424752], [-6174995919464], [4907099845538], [26624841962730], [-378798605106], [-39375238806540], [-37712168664966], [-3289522924600], [268793079564], [24463440471378], [15719943192024], [-11119464960032], [25082089921222]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_8_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_8_10_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_8_10_a_b(:prec:=1)
    chi := MakeCharacter_8_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 10));
    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_8_10_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_8_10_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_8_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,10,sign)));
    Vf := Kernel([<3,R![-68, 1]>],Snew);
    return Vf;
end function;