
// Make newform 8.6.a.a in Magma, downloaded from the LMFDB on 23 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_6_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_8_6_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_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 := 6;
    raw_aps := [[0], [20], [-74], [-24], [124], [478], [-1198], [3044], [184], [-3282], [-5728], [10326], [-8886], [-9188], [23664], [11686], [16876], [-18482], [-15532], [-31960], [-4886], [44560], [67364], [71994], [48866], [51606], [180424], [-65700], [-112706], [-23502], [-94592], [70292], [277290], [-130308], [-401530], [-75976], [-394322], [-11724], [-551928], [432894], [559620], [604710], [-409152], [540866], [-629898], [283048], [142756], [889696], [1143156], [-695786], [-347126], [-1642960], [-1167438], [-790612], [-129790], [70888], [1790174], [-1773616], [-275450], [594170], [1092428], [333654], [1059972], [-1336488], [1644186], [-1723698], [2749628], [-3414894], [730764], [-2297490], [-1170718], [3886536], [933040], [-392218], [-4729300], [1897344], [-3722954], [3338078], [4274898], [-2573190], [5268276], [-973354], [3557360], [-1954958], [-3296808], [-5058196], [2127298], [289130], [2668702], [7586192], [-1419612], [-1884064], [-6013880], [4292316], [1345092], [202008], [9783438], [-10483030], [6210172], [5080878], [3346868], [7003774], [-12981884], [1899418], [-1662996], [8773442], [5183932], [8498578], [11247096], [-3464390], [-999712], [9813398], [-5347446], [-6827684], [-3609704], [-13385342], [-9911148], [-17835928], [-4323234], [19785828], [15777158], [6787618], [-14994218], [11558044], [-220156], [4789326], [4268918], [-16195952], [6534264], [13161710], [-14234764], [-21583544], [18659376], [-2566810], [-25958566], [5532674], [8329398], [-13652252], [-8546258], [7584842], [6184732], [-2781018], [16389464], [22951148], [-3501362], [5296680], [-20294842], [-4827846], [-13021044], [-39238688], [13462206], [-917710], [24548804], [-16146280], [-20336132], [10772560], [41856568], [29984546], [14240202], [-41454594], [-15407868], [-20632806], [11872424], [1532220], [17432082], [2232696], [22250080], [53266198]];
    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_6_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_8_6_a_a(:prec:=1)
    chi := MakeCharacter_8_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 6));
    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_6_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_8_6_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_8_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([],Snew);
    return Vf;
end function;