
// Make newform 5776.2.a.d in Magma, downloaded from the LMFDB on 25 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_d();".
// 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_d();".
// 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], [-3], [-2], [1], [3], [0], [1], [5], [-8], [2], [8], [-4], [-8], [1], [15], [2], [3], [2], [9], [-10], [6], [0], [2], [2], [-6], [-7], [15], [-14], [18], [-12], [-17], [0], [0], [2], [-2], [16], [-12], [6], [0], [-22], [-7], [6], [8], [25], [27], [14], [-17], [-10], [-6], [-15], [8], [-2], [-8], [-24], [-30], [-7], [28], [8], [6], [-9], [-12], [-7], [29], [27], [17], [32], [2], [10], [9], [15], [-28], [-29], [15], [-26], [-30], [8], [8], [20], [0], [13], [-18], [-14], [20], [26], [-10], [-7], [-28], [-4], [2], [20], [-2], [28], [-40], [-39], [30], [28], [29], [2], [28], [28], [-36], [-40], [28], [-37], [12], [34], [0], [8], [-22], [34], [18], [-10], [-32], [-42], [26], [-23], [-36], [5], [23], [-44], [-13], [4], [-42], [-28], [-30], [5], [17], [-36], [40], [-16], [32], [-2], [27], [-35], [-9], [-17], [-3], [-15], [-3], [12], [-29], [23], [15], [20], [-6], [12], [50], [54], [-13], [-18], [-34], [-2], [53], [12], [-5], [-55], [-7], [-7], [12], [46], [-48], [-28], [-8], [-6], [-8], [28], [-40], [-36], [-15], [-47], [48], [-1], [0], [30], [28], [-18], [4], [-40], [47], [-23], [26], [-28], [-39], [-30], [7], [-56], [10], [33], [-34], [-44], [-8], [-57], [32], [-24], [17], [-36], [-37], [-9], [30], [-68], [17], [-20], [-50], [8], [-35], [39], [45], [-13], [53], [-28], [36], [-27], [0], [18], [-3], [-42], [58], [-6], [-42], [50], [-50], [1], [53], [30], [46], [-30], [38], [18], [-6], [35], [-48], [38], [-14], [27], [30], [-16], [-50], [12], [4], [-22], [11], [25], [36], [-60], [32], [-28], [20], [21], [-2], [-13], [37], [-50], [44], [-10], [-18], [-52], [-18], [-2], [64], [-12], [0], [21], [18], [-65], [15], [27], [29], [-6], [33], [-47], [9], [-65], [82], [-9], [42], [-55], [8], [-12], [54], [-67], [-63], [-27], [32], [48], [9], [42], [-25], [-50], [-18], [12], [-54], [-28], [-14], [80], [2], [-46], [-35], [42], [-26], [-53], [5], [59], [-12], [82], [-57], [-5], [0], [-16], [-64], [-30], [-82], [-36], [72], [0], [-30], [-38], [-61], [-50], [37], [-57], [-27], [14], [39], [78], [-65], [49], [-32], [44], [52], [27], [-35], [-34], [-42], [-68], [-45], [36], [-73], [-73], [11], [-57], [-75], [-58], [1], [-20], [-18], [-27], [2], [48], [-8], [-38], [32], [-36], [10], [56], [-35], [-12], [-17], [-66], [88], [-58], [78], [95], [-38], [-44], [18], [-6], [38], [-42], [45], [-59], [5], [28], [8], [5], [-32], [14], [-75], [12], [18], [74], [-13], [78], [-45], [74], [83], [-32], [26], [-32], [-50], [-29], [-30], [-58], [12], [-74], [-50], [-20], [-68], [72], [-50], [-86], [18], [32], [-90], [73], [-2], [28], [-71], [-20], [-4], [18], [19], [-2], [13], [-48], [90], [8], [3], [-56], [90], [7], [67], [-35], [86], [-93], [-61], [-5], [88], [25]];
    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_d();".
// 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_d(: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_d();".
// 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_d( : 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![3, 1]>,<11,R![2, 1]>,<13,R![-1, 1]>],Snew);
    return Vf;
end function;