
// Make newform 9.8.a.b in Magma, downloaded from the LMFDB on 16 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_9_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_9_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_9_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
        poly := [-10, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0], [0, 6]];
    Rf_basisdens := [1, 1];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_9_a();"
function MakeCharacter_9_a()
    N := 9;
    order := 1;
    char_gens := [2];
    v := [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_9_a_Hecke(Kf)
    return MakeCharacter_9_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, 1], [0, 0], [0, -16], [260, 0], [0, -320], [6890, 0], [0, 1248], [33176, 0], [0, 1664], [0, -7280], [1508, 0], [-380770, 0], [0, 4640], [7640, 0], [0, 29824], [0, 54288], [0, -142720], [-988858, 0], [3857360, 0], [0, 222720], [-2004730, 0], [2699684, 0], [0, 142912], [0, 408000], [-12957490, 0], [0, -294320], [5073260, 0], [0, -194688], [6593834, 0], [0, -250432], [19536140, 0], [0, 1963520], [0, -1118240], [-24466864, 0], [0, -976720], [-51210172, 0], [9638150, 0], [-71866600, 0], [0, -451456], [0, 2619760], [0, -3020160], [81574418, 0], [0, 8444800], [60037250, 0], [0, -10023792], [194000924, 0], [-10489648, 0], [225159740, 0], [0, -8932928], [-65259454, 0], [0, 9821760], [0, 2245760], [-256942738, 0], [0, 14439360], [0, -16841344], [0, -14283776], [0, -5793840], [435739772, 0], [-235119790, 0], [0, 11228800], [-69066400, 0], [0, -36465904], [-897048880, 0], [0, 8602880], [686241530, 0], [0, 27615760], [-572279968, 0], [-556882690, 0], [0, -46880704], [780705926, 0], [0, -28487744], [0, -3655680], [-373436380, 0], [278207150, 0], [-1368511144, 0], [0, 109599104], [0, -10371920], [299153270, 0], [0, 44542880], [1072684886, 0], [0, -74551360], [1415622338, 0], [0, -172379520], [1307082530, 0], [2545389404, 0], [0, -14226368], [0, 51508320], [-2831819770, 0], [0, -185593040], [1591629260, 0], [0, 93302976], [0, 148042240], [-3425987500, 0], [0, 90721280], [4168452704, 0], [0, -144318720], [0, -92868400], [0, -279708000], [4170750680, 0], [1789859162, 0], [2595088520, 0], [0, 189458160], [0, -136362304], [0, 223518880], [5889530192, 0], [436031570, 0], [0, 27837952], [0, 434323008], [0, 57100160], [9287018, 0], [-3720211060, 0], [8101111070, 0], [0, -302969728], [-2125111456, 0], [-10935335788, 0], [0, -90140960], [6492953480, 0], [0, 232951680], [0, 108675632], [0, -314712320], [-591741202, 0], [2508484850, 0], [0, 621829936], [0, -284875584], [13861748888, 0], [0, 317313360], [6364973186, 0], [0, -663087360], [2105111060, 0], [-4151892550, 0], [-10515145264, 0], [0, 189668480], [744143828, 0], [9808499090, 0], [0, 912844640], [-23509206526, 0], [0, -901989552], [10333193000, 0], [0, 156174512], [0, 1304091360], [230066408, 0], [0, 160416400], [-2375054860, 0], [0, -294201984], [-20657794534, 0], [0, -398412800], [24791903630, 0], [0, -154889696], [20809451624, 0], [0, -1246619520], [-16729544650, 0], [0, 1428535680], [-12270792040, 0], [0, -1783265536], [5424702920, 0], [0, 2061704320], [-11138069644, 0], [0, -2227226720], [37450221770, 0], [0, 550130480], [0, -481136768], [0, 529430304], [31735490540, 0], [0, -1718832960], [0, 1965486368], [0, -496539392], [1164812852, 0], [21893273630, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_9_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_9_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_9_8_a_b(:prec:=2)
    chi := MakeCharacter_9_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_9_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_9_8_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_9_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,8,sign)));
    Vf := Kernel([<2,R![-360, 0, 1]>],Snew);
    return Vf;
end function;