
// Make newform 5776.2.a.e in Magma, downloaded from the LMFDB on 17 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_5776_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_5776_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_5776_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_5776_a();"
function MakeCharacter_5776_a()
    N := 5776;
    order := 1;
    char_gens := [5055, 1445, 2529];
    v := [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_5776_a_Hecke(Kf)
    return MakeCharacter_5776_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], [-4], [0], [-3], [2], [2], [0], [-6], [-4], [10], [2], [9], [4], [12], [-2], [1], [-8], [-9], [6], [-9], [4], [5], [-18], [1], [12], [14], [-16], [0], [-1], [6], [15], [9], [13], [-10], [-2], [8], [11], [-16], [-26], [-9], [14], [-12], [-6], [-18], [18], [12], [-10], [-19], [-8], [11], [-12], [21], [-5], [3], [-16], [4], [20], [12], [-13], [-13], [-4], [-25], [2], [-19], [6], [-13], [3], [9], [-28], [11], [2], [26], [4], [12], [8], [4], [-14], [-3], [-35], [-12], [-22], [30], [10], [14], [-1], [33], [-11], [6], [34], [-5], [-20], [26], [-16], [-7], [6], [16], [1], [-20], [4], [-28], [-40], [-11], [-46], [-17], [-37], [-4], [-21], [-38], [-13], [0], [-22], [3], [20], [8], [33], [-5], [14], [36], [-12], [20], [30], [-2], [28], [-36], [-12], [18], [34], [8], [22], [5], [6], [-38], [14], [17], [2], [52], [-41], [42], [21], [0], [-38], [26], [55], [-12], [0], [42], [-35], [-29], [-6], [40], [7], [43], [-32], [-49], [14], [26], [21], [11], [-10], [28], [1], [-54], [-53], [-47], [-32], [-8], [-32], [-34], [-6], [-17], [-22], [-50], [-17], [6], [-25], [-47], [36], [-28], [38], [-2], [20], [16], [-33], [16], [-20], [-28], [-8], [41], [14], [-53], [20], [-67], [-24], [39], [-6], [-53], [28], [-61], [-18], [-6], [40], [34], [11], [43], [22], [-50], [21], [-21], [55], [-47], [2], [36], [-49], [26], [42], [-66], [46], [-18], [10], [-42], [26], [21], [44], [-39], [-6], [-57], [-40], [28], [-4], [60], [59], [-18], [-45], [-47], [-18], [33], [22], [-53], [-30], [21], [55], [4], [-6], [-30], [-42], [68], [59], [43], [-12], [8], [-58], [-30], [-37], [6], [-20], [70], [29], [66], [26], [-26], [57], [-32], [-48], [-3], [-23], [24], [-37], [-68], [18], [-6], [-28], [-73], [-52], [-50], [-6], [59], [16], [-61], [12], [-18], [6], [-28], [-18], [-44], [-20], [-51], [-56], [-40], [38], [22], [20], [51], [-28], [-8], [-50], [-2], [-76], [-52], [51], [37], [-10], [56], [-11], [-27], [73], [81], [-28], [32], [-60], [-6], [-22], [69], [-59], [6], [42], [-73], [-70], [-1], [22], [35], [21], [-58], [-68], [34], [-38], [-85], [-1], [-38], [6], [-10], [-8], [-68], [76], [-33], [12], [-92], [3], [46], [46], [-32], [-11], [-70], [-4], [6], [47], [88], [12], [-90], [90], [-47], [45], [18], [-66], [-44], [-45], [-20], [-45], [17], [6], [50], [15], [-24], [-49], [88], [-50], [-92], [90], [-19], [-27], [-60], [-36], [-50], [86], [-32], [12], [-80], [-53], [-31], [69], [-48], [66], [-34], [-69], [5], [0], [-68], [58], [19], [-78], [19], [-42], [-51], [67], [-24], [-47], [-80], [-7], [5], [10], [34], [61], [72], [-78], [-32], [70], [-40], [45], [52], [59], [-77], [60], [-7], [-65], [-73], [78], [36], [-44], [98], [36], [-86], [-100], [66], [-69], [-14], [24], [-33], [-50], [-68], [-30]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_5776_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_5776_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_5776_2_a_e(:prec:=1)
    chi := MakeCharacter_5776_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(2999) - 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_5776_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_5776_2_a_e( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_5776_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![1, 1]>,<5,R![4, 1]>,<7,R![0, 1]>,<11,R![3, 1]>,<13,R![-2, 1]>],Snew);
    return Vf;
end function;