
// Make newform 9576.2.a.n in Magma, downloaded from the LMFDB on 20 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_n();".
// 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_n();".
// 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], [0], [1], [0], [-2], [8], [-1], [4], [0], [0], [-2], [-2], [-4], [6], [12], [4], [-2], [4], [-6], [6], [4], [6], [-18], [14], [-8], [-8], [14], [2], [12], [8], [-14], [14], [-8], [-10], [0], [-10], [4], [12], [-14], [-6], [10], [20], [10], [2], [4], [-16], [16], [24], [26], [26], [0], [-18], [18], [18], [8], [6], [16], [10], [-20], [0], [2], [-28], [30], [2], [-32], [4], [-2], [-12], [22], [-8], [20], [-4], [-18], [-20], [-12], [-18], [30], [8], [-6], [6], [34], [10], [-34], [8], [12], [0], [-10], [-24], [8], [18], [18], [-12], [-12], [4], [-2], [-2], [-22], [36], [-18], [16], [22], [24], [-12], [-4], [10], [-6], [-12], [38], [-10], [32], [18], [-10], [0], [8], [32], [32], [6], [-38], [10], [-10], [2], [14], [34], [12], [-22], [-38], [42], [48], [50], [20], [-14], [-48], [-34], [12], [50], [-14], [-4], [2], [-2], [-20], [-10], [-16], [6], [2], [0], [-10], [-10], [52], [38], [38], [32], [28], [-36], [16], [-2], [40], [40], [2], [-50], [40], [32], [8], [-28], [-32], [-40], [-36], [6], [-2], [-16], [-42], [-2], [8], [-42], [-24], [-12], [-36], [18], [24], [-2], [-8], [-12], [-46], [-26], [-36], [56], [-54], [-36], [-10], [18], [-22], [-48], [-28], [-42], [-48], [26], [-58], [-10], [56], [34], [-12], [-32], [42], [-70], [-54], [0], [-4], [38], [-14], [-20], [-2], [-16], [56], [8], [-48], [-22], [8], [18], [38], [6], [66], [0], [-20], [-56], [12], [-10], [-46], [30], [56], [44], [-58], [-52], [-68], [12], [36], [66], [-2], [2], [-36], [52], [66], [-76], [-4], [38], [-22], [-70], [28], [24], [16], [36], [-66], [60], [0], [-14], [44], [8], [-58], [-44], [62], [18], [-4], [-66], [50], [14], [-12], [-44], [-12], [-60], [56], [-66], [-70], [-80], [14], [64], [-18], [44], [-60], [-50], [2], [42], [-12], [-40], [-2], [-70], [12], [72], [-30], [-24], [-36], [-70], [-2], [-42], [36], [60], [46], [-28], [56], [0], [-24], [-24], [-38], [-40], [-16], [14], [-28], [-50], [-42], [-2], [8], [-46], [-26], [86], [8], [-76], [36], [54], [-70], [18], [14], [-66], [-92], [-34], [78], [-56], [26], [14], [56], [-4], [2], [-46], [78], [48], [-4], [-2], [4], [-54], [38], [-50], [22], [-64], [46], [-50], [-54], [-20], [24], [-6], [74], [-88], [-56], [-40], [-44], [38], [-14], [-72], [-58], [-6], [-66], [-10], [-86], [18], [-10], [-4], [58], [-50], [56], [34], [60], [24], [-6], [-72], [-4], [24], [-28], [40], [22], [-44], [12], [74], [20], [-42], [-10], [-18], [-40], [62], [20], [12], [88], [-70], [20], [92], [-50], [42], [-48], [-28], [18], [14], [56], [46], [-100], [-10], [78], [78], [-76], [82], [-42], [68], [54], [-16], [-52], [38], [-34], [-58], [22], [20], [42], [-28], [50], [-72], [-40], [0], [-72], [2], [-24], [12], [-54], [58], [-6], [26], [-80], [54], [10], [64], [76], [96], [78], [24], [22], [-18], [-44], [-40], [-70], [-10], [46], [-70], [-50], [-96], [0], [72], [98], [46], [32], [24], [28], [-84], [-30], [48], [34], [-12], [50], [-88], [-56], [56], [-66], [22], [28], [66], [-100], [22], [-82], [-64], [-4], [-34], [-92], [-50], [-30], [-14], [90], [-4], [42], [-64], [22], [94], [-14], [-86], [80], [-36], [50], [112], [28], [24], [42], [24], [-22], [30], [-64], [46], [-68], [-42], [-60], [-56], [-26], [-24], [-84], [48], [-50], [20], [-54], [112], [26], [52], [14], [2], [-54], [-24], [-52], [-74], [102], [110], [58], [-16], [-6], [-20], [8], [-92], [58], [-70], [-86], [20], [-34], [-82], [24], [32]];
    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_n();".
// 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_n(: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_n();".
// 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_n( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_9576_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![0, 1]>,<11,R![0, 1]>,<13,R![2, 1]>,<17,R![-8, 1]>],Snew);
    return Vf;
end function;