
// Make newform 9.9.b.a in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_9_b();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_9_b_Hecke();"
// 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_9_b_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_9_9_b_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
        poly := [2, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0], [0, 3]];
    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_b();"
function MakeCharacter_9_b()
    N := 9;
    order := 2;
    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;

// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_9_b_Hecke();"
function MakeCharacter_9_b_Hecke(Kf)
    N := 9;
    order := 2;
    char_gens := [2];
    char_values := [[-1, 0]];
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    values := ConvertToHeckeField(char_values : pass_field := true, Kf := Kf); // the value of chi on the gens as elements in the Hecke field
    F := Universe(values);// the Hecke field
    chi := DirichletCharacterFromValuesOnUnitGenerators(DirichletGroup(N,F),values);
    return chi;
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 := 9;
    raw_aps := [[0, 1], [0, 0], [0, 233], [1652, 0], [0, -5036], [-26272, 0], [0, 3579], [46640, 0], [0, -77332], [0, -144953], [-196444, 0], [2819414, 0], [0, -166507], [-2213464, 0], [0, 384892], [0, 1222983], [0, 2768264], [-17405302, 0], [-14322664, 0], [0, -3687708], [-8906992, 0], [32758844, 0], [0, 20055628], [0, -13891371], [-24451744, 0], [0, -24079223], [164258540, 0], [0, 40783104], [-74343040, 0], [0, 43267877], [-241938700, 0], [0, -88673800], [0, -77690645], [-281930104, 0], [0, 67621943], [-690036412, 0], [-438415210, 0], [1209321416, 0], [0, -294075640], [0, 369510457], [0, 45033072], [1536803264, 0], [0, -145897352], [-3290482, 0], [0, -107277639], [-201948100, 0], [-3026789704, 0], [621078980, 0], [0, 898239892], [996529472, 0], [0, 171494043], [0, -939702208], [3713136080, 0], [0, 602426748], [0, -184166197], [0, -1039263752], [0, 1956670569], [7700471444, 0], [965520896, 0], [0, 338566981], [7830573776, 0], [0, -3452150713], [5210942888, 0], [0, 2195056136], [-14879420878, 0], [0, -101569367], [21023554568, 0], [-4291423120, 0], [0, 151287476], [-19050359638, 0], [0, 497300293], [0, -3080570100], [-8662208908, 0], [22852646954, 0], [11939497472, 0], [0, 665961056], [0, 5932304167], [-22283319094, 0], [0, 1384989179], [41193032768, 0], [0, 1327023884], [-9827209120, 0], [0, -15605980308], [20231340734, 0], [28226734772, 0], [0, 15111086620], [0, 7418448069], [-64916183200, 0], [0, -780274169], [39853323980, 0], [0, -11351712156], [0, -10772284052], [36729461180, 0], [0, -2739820960], [34631089568, 0], [0, 19607655516], [0, 14314209863], [0, -16069112421], [47370832256, 0], [16379621984, 0], [74860554920, 0], [0, -37259729481], [0, -30122165020], [0, -12794679701], [120161175632, 0], [-183000370318, 0], [0, -16878738152], [0, -1612533573], [0, 38058205292], [41420651918, 0], [-114643780732, 0], [-123749277622, 0], [0, -14828927035], [139494605504, 0], [-82529924740, 0], [0, 71742832747], [-229543938136, 0], [0, 24306522660], [0, -3264090247], [0, 65580407176], [200656311830, 0], [-193201962142, 0], [0, 2128611241], [0, -65823868212], [-141809869912, 0], [0, 4075294455], [-229325010592, 0], [0, -55182086940], [262995478412, 0], [-239665369312, 0], [40106094320, 0], [0, 12148506056], [145524264596, 0], [232953190976, 0], [0, -83974726459], [168418944818, 0], [0, 74262370263], [-625510273744, 0], [0, -135475795447], [0, 134078444181], [-639315180160, 0], [0, 17397174505], [434073593060, 0], [0, -23952486792], [88784088320, 0], [0, 91521218044], [73726740374, 0], [0, 132021149461], [195095030696, 0], [0, 181309785768], [-258388745878, 0], [0, -275294363925], [-61697314024, 0], [0, 3425915480], [1006858187240, 0], [0, -60214167992], [-208240684108, 0], [0, -312006753797], [-383971172002, 0], [0, -29314553017], [0, 119527853032], [0, -333067617861], [-385172127892, 0], [0, 103706920188], [0, -15798333403], [0, 303394441576], [1016225645204, 0], [1661356223690, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_9_b_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_9_9_b_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_9_9_b_a(:prec:=2)
    chi := MakeCharacter_9_b();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 9));
    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_9_b_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_9_9_b_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_9_b();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,9,sign)));
    Vf := Kernel([],Snew);
    return Vf;
end function;