
// Make newform 5586.2.a.q in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_5586_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_5586_2_a_q();".
// 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_5586_2_a_q();".
// 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_5586_a();"
function MakeCharacter_5586_a()
    N := 5586;
    order := 1;
    char_gens := [3725, 4903, 4999];
    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_5586_a_Hecke(Kf)
    return MakeCharacter_5586_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 := [[-1], [1], [1], [0], [-5], [-4], [2], [1], [6], [-3], [-1], [6], [6], [-4], [-6], [-3], [9], [10], [10], [-16], [-6], [7], [-9], [-8], [-3], [-10], [-8], [-13], [-20], [-10], [-7], [-13], [-10], [-20], [-18], [-1], [12], [4], [-24], [2], [0], [-16], [12], [7], [10], [-16], [8], [21], [3], [20], [-8], [24], [9], [3], [-18], [-16], [-3], [-11], [6], [30], [-12], [9], [-10], [-2], [9], [9], [30], [31], [-32], [20], [-30], [-18], [-25], [4], [8], [-2], [6], [28], [8], [-31], [-20], [30], [10], [-14], [-37], [23], [26], [-1], [-26], [-32], [-12], [10], [13], [15], [-6], [-22], [21], [-6], [38], [32], [42], [9], [-11], [16], [40], [27], [21], [4], [32], [1], [-47], [26], [-32], [-22], [-25], [-44], [-40], [24], [-15], [-8], [-8], [19], [-21], [-13], [46], [-33], [4], [-6], [1], [44], [-24], [46], [-7], [36], [-28], [-15], [26], [18], [-1], [30], [-28], [-33], [48], [-35], [-28], [34], [-12], [10], [26], [4], [2], [-42], [6], [-6], [2], [-38], [-48], [-28], [7], [-53], [36], [34], [5], [15], [26], [-52], [-37], [-50], [-51], [49], [-49], [4], [32], [53], [45], [36], [-26], [1], [-16], [64], [-3], [-21], [-54], [-40], [-30], [21], [38], [-8], [49], [4], [51], [57], [8], [2], [-21], [8], [-62], [-14], [22], [-24], [-5], [-20], [16], [6], [-28], [15], [16], [-41], [58], [52], [-6], [57], [-23], [3], [-28], [46], [-53], [64], [26], [-10], [-6], [56], [58], [41], [39], [-50], [-12], [46], [27], [0], [-46], [-52], [-33], [44], [50], [8], [-1], [9], [28], [-42], [-48], [-68], [-49], [-38], [26], [-42], [-47], [-12], [30], [36], [-8], [50], [-68], [-6], [23], [-69], [-28], [54], [73], [53], [-40], [-15], [-4], [70], [0], [-12], [27], [-14], [22], [1], [-64], [-2], [1], [-49], [35], [-52], [9], [46], [-22], [4], [-70], [13], [-54], [2], [58], [48], [3], [-53], [-77], [30], [6], [9], [22], [4], [-36], [-23], [-40], [-78], [-4], [-72], [6], [51], [27], [13], [28], [2], [-35], [-76], [-36], [-4], [-14], [54], [-8], [80], [-42], [-66], [-29], [60], [17], [-56], [10], [22], [-15], [20], [-46], [-7], [80], [36], [58], [57], [-38], [43], [-80], [-48], [-92], [68], [56], [6], [53], [-69], [-34], [72], [-39], [45], [-3], [27], [-20], [26], [-6], [-1], [4], [85], [50], [-19], [-32], [-76], [50], [-93], [-78], [54], [-8], [-78], [-24], [60], [80], [37], [46], [-56], [71], [81], [20], [-54], [-15], [4], [-40], [92], [20], [-21], [40], [-6], [62], [-66], [16], [12], [-52], [-28], [-80], [-8], [-32], [-86], [79], [42], [-81], [12], [-96], [-7], [53], [-82], [-12], [57], [20], [102], [88], [28], [-55], [-25], [-52], [-2], [84], [-41], [21], [13], [71], [-6], [-62], [47], [-68], [40], [2], [-64], [63], [40], [18], [-47], [94], [94], [3], [-24], [28], [58]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_5586_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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