
// Make newform 9576.2.a.e in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_9576_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_9576_2_a_e();".
// 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_9576_2_a_e();".
// 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_9576_a();"
function MakeCharacter_9576_a()
    N := 9576;
    order := 1;
    char_gens := [7183, 4789, 5321, 4105, 1009];
    v := [1, 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_9576_a_Hecke(Kf)
    return MakeCharacter_9576_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], [0], [-2], [-1], [4], [-6], [-2], [-1], [4], [-6], [4], [2], [-10], [12], [-4], [-14], [4], [6], [4], [8], [-6], [-12], [-12], [6], [2], [-18], [4], [-4], [18], [-2], [12], [20], [-2], [12], [6], [-20], [-2], [-4], [0], [-6], [12], [-22], [12], [10], [6], [24], [12], [20], [-12], [14], [22], [-28], [-14], [12], [-18], [12], [10], [-16], [-10], [-10], [28], [-14], [4], [-12], [26], [-14], [28], [2], [4], [14], [22], [36], [-32], [18], [12], [-32], [30], [-2], [-2], [10], [-4], [34], [24], [18], [-20], [4], [14], [-38], [30], [16], [20], [-12], [12], [12], [36], [4], [-22], [-26], [44], [6], [-12], [-2], [12], [-10], [-4], [34], [-12], [22], [0], [2], [28], [-2], [38], [20], [16], [30], [-12], [12], [-18], [44], [2], [-38], [-6], [36], [-4], [-42], [-42], [-20], [16], [-18], [28], [16], [36], [-18], [38], [34], [34], [-4], [-38], [-2], [-4], [-34], [-16], [4], [2], [0], [46], [-10], [4], [-56], [-38], [-42], [-44], [-48], [-52], [24], [-16], [46], [-22], [-38], [-12], [6], [56], [-36], [-34], [-24], [44], [22], [-6], [-58], [-28], [2], [28], [-6], [44], [30], [20], [-10], [28], [22], [-32], [12], [58], [22], [-60], [54], [34], [28], [-46], [-44], [58], [12], [-28], [18], [12], [38], [-30], [-26], [-58], [-20], [58], [-20], [-14], [-54], [-12], [54], [-40], [12], [6], [52], [-46], [30], [40], [-12], [-16], [10], [-40], [14], [-48], [-10], [-22], [-4], [-34], [56], [-36], [-26], [-10], [24], [-28], [4], [-10], [12], [-44], [54], [28], [52], [-14], [-34], [-4], [-72], [20], [52], [64], [50], [-34], [36], [-8], [-4], [-28], [-36], [22], [70], [-4], [-30], [78], [-36], [22], [-68], [42], [-6], [-52], [36], [38], [-30], [-66], [28], [58], [14], [52], [-34], [2], [-36], [-54], [32], [-22], [16], [-20], [50], [-70], [12], [-48], [40], [20], [58], [-68], [60], [-62], [-38], [56], [-2], [30], [4], [-10], [-20], [66], [70], [-4], [-74], [52], [-44], [-70], [10], [-40], [4], [-4], [-38], [20], [-46], [12], [-58], [-12], [-90], [-2], [60], [60], [-46], [68], [0], [-78], [-18], [68], [74], [-14], [-20], [-66], [-78], [-28], [36], [0], [-42], [-74], [-38], [-56], [12], [52], [-52], [26], [-50], [-22], [-24], [2], [62], [-6], [68], [-6], [76], [-10], [-52], [-56], [-18], [4], [18], [-66], [-28], [-78], [-26], [-36], [12], [38], [8], [-50], [-2], [72], [52], [28], [-6], [22], [4], [-14], [4], [20], [76], [-78], [-56], [30], [36], [-4], [-46], [14], [42], [-94], [30], [72], [-66], [-36], [64], [72], [-50], [-36], [-8], [66], [-38], [12], [-76], [-8], [-46], [-44], [-42], [76], [14], [-78], [-42], [28], [54], [66], [0], [94], [30], [60], [20], [-22], [-18], [44], [44], [106], [102], [104], [36], [46], [48], [42], [18], [-28], [60], [-6], [-6], [36], [-26], [-52], [-68], [-22], [108], [4], [0], [54], [-82], [-86], [74], [-92], [24], [-60], [46], [-70], [-72], [82], [-18], [4], [88], [2], [-30], [-76], [56], [-36], [-90], [-30], [10], [74], [60], [94], [-26], [28], [20], [-20], [-46], [52], [2], [-44], [20], [6], [60], [-4], [36], [-56], [-14], [60], [58], [-66], [-32], [28], [-6], [74], [6], [114], [82], [-88], [12], [54], [36], [-36], [68], [-38], [-8], [10], [-38], [-28], [6], [116], [-82], [-88], [36], [78], [72], [-74], [80], [74], [30], [-24], [-20], [2], [-4], [-20], [108], [10], [2], [4], [2], [-30], [-26], [-80], [-28], [6], [-28], [-18], [52], [58], [84], [-46], [78], [36], [18], [-64], [86]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_9576_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_9576_2_a_e();".
// 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_9576_2_a_e(:prec:=1)
    chi := MakeCharacter_9576_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(3833) - 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_9576_2_a_e();".
// 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_9576_2_a_e( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_9576_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![2, 1]>,<11,R![-4, 1]>,<13,R![6, 1]>,<17,R![2, 1]>],Snew);
    return Vf;
end function;