
// Make newform 9996.2.a.l in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_9996_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_9996_2_a_l();".
// 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_9996_2_a_l();".
// 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_9996_a();"
function MakeCharacter_9996_a()
    N := 9996;
    order := 1;
    char_gens := [4999, 6665, 7549, 4117];
    v := [1, 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_9996_a_Hecke(Kf)
    return MakeCharacter_9996_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 := 2;
    raw_aps := [[0], [1], [-2], [0], [0], [-6], [1], [2], [0], [8], [0], [2], [-2], [8], [8], [-2], [-12], [4], [12], [0], [-8], [-8], [0], [-10], [-12], [-2], [-10], [-12], [-2], [8], [20], [4], [-18], [-20], [-6], [12], [-18], [-12], [8], [-14], [-18], [-4], [2], [-6], [-16], [4], [-4], [10], [-4], [-10], [-24], [-26], [12], [0], [-18], [-6], [30], [22], [-14], [-18], [4], [10], [6], [24], [8], [8], [20], [18], [-16], [18], [-14], [18], [8], [-14], [-28], [-4], [-14], [-8], [-24], [-10], [12], [10], [-4], [30], [-32], [6], [4], [22], [-14], [32], [-24], [-16], [16], [-26], [-4], [-16], [-2], [-38], [-10], [-30], [28], [14], [28], [6], [-28], [-6], [24], [-42], [-34], [44], [-28], [-6], [0], [32], [20], [28], [20], [-28], [-28], [-42], [-22], [-26], [-14], [12], [32], [30], [14], [0], [-34], [18], [36], [-4], [-40], [26], [-22], [34], [6], [-24], [2], [-44], [-36], [0], [24], [-20], [6], [-48], [28], [-50], [10], [-6], [18], [34], [8], [24], [-12], [-28], [16], [22], [26], [54], [12], [46], [52], [-20], [-58], [32], [-40], [20], [-42], [42], [34], [26], [32], [-26], [-26], [-62], [28], [-48], [-38], [-26], [-36], [20], [14], [-66], [54], [10], [26], [-46], [18], [-24], [64], [-4], [-40], [30], [24], [18], [-58], [-10], [54], [24], [-12], [44], [62], [-42], [-40], [50], [-2], [14], [20], [50], [-50], [18], [8], [24], [24], [-8], [0], [-30], [-36], [-42], [26], [-24], [-2], [-8], [52], [-26], [-34], [68], [-74], [28], [46], [-64], [-48], [2], [-2], [-52], [8], [-44], [48], [-24], [-56], [10], [-62], [14], [-18], [-32], [-28], [36], [-4], [-6], [22], [10], [-18], [48], [-46], [-54], [46], [28], [18], [-58], [-16], [-10], [0], [32], [74], [10], [-50], [38], [4], [18], [-36], [52], [10], [-76], [74], [10], [58], [-2], [54], [-8], [-84], [-32], [-72], [26], [52], [14], [38], [36], [-18], [-2], [24], [-12], [26], [-28], [78], [-30], [26], [-70], [60], [10], [-70], [66], [-40], [-68], [28], [34], [14], [-56], [6], [54], [56], [-40], [52], [-52], [-74], [-22], [-60], [-4], [48], [-70], [-80], [70], [30], [-64], [48], [90], [84], [-24], [40], [56], [82], [-88], [44], [40], [52], [-4], [30], [-66], [40], [-18], [-14], [-74], [-34], [84], [62], [20], [92], [28], [56], [-6], [50], [78], [-54], [-8], [-26], [90], [-40], [-28], [0], [-58], [70], [-2], [-30], [76], [74], [-42], [-18], [-4], [10], [-58], [-52], [-28], [78], [-2], [-14], [-84], [24], [-42], [70], [-2], [-6], [-58], [-64], [0], [44], [-40], [-68], [-34], [-76], [60], [66], [54], [6], [66], [-90], [18], [-10], [-66], [-68], [-18], [28], [14], [64], [78], [34], [24], [70], [62], [38], [52], [-8], [42], [-2], [-4], [-34], [-50], [-60], [8], [50], [60], [54], [-48], [24], [16], [-16], [-82], [-84], [48], [-34], [-72], [-50], [-62], [36], [24], [40], [-102], [-22], [-54], [4], [-10], [52], [60], [-50], [2], [28], [102], [-86], [-32], [76], [-58], [-88], [96], [84], [58], [94], [-26], [10], [96], [60], [52], [-52], [-32], [-6], [-34], [50], [46], [44], [0], [52], [50], [24], [-106], [-24], [-58], [-52], [-28], [24], [-10], [-24], [-62], [-78], [-54], [48], [90], [16], [-90], [10], [-12], [-84], [-48], [58], [-60], [34], [-2], [88], [-76], [0], [58], [-24], [-84], [52], [-46], [8], [48], [-14], [46], [78], [-68], [54], [56], [24], [0], [22], [12], [-12], [-58], [-90], [16], [94], [94], [102], [40], [-58], [-32], [56], [52], [6], [22], [72], [-110], [-108], [-78], [104], [-34], [56], [24], [30], [-114], [-50], [-68], [-20], [-68], [14], [-60], [74], [48], [-16], [36], [-92], [-98], [60], [10], [8], [94], [100], [-78], [0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_9996_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_9996_2_a_l();".
// 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_9996_2_a_l(:prec:=1)
    chi := MakeCharacter_9996_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 2));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(4027) - 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_9996_2_a_l();".
// 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_9996_2_a_l( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_9996_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![2, 1]>,<11,R![0, 1]>,<13,R![6, 1]>],Snew);
    return Vf;
end function;