
// Make newform 5586.2.a.v in Magma, downloaded from the LMFDB on 16 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_v();".
// 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_v();".
// 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], [-1], [-6], [2], [1], [-1], [4], [0], [8], [-12], [-7], [-9], [0], [-2], [-3], [-2], [2], [1], [12], [-1], [-18], [4], [-17], [8], [0], [-20], [4], [-18], [4], [-11], [19], [15], [-8], [-13], [5], [-2], [4], [-6], [6], [-9], [2], [17], [-25], [14], [-10], [18], [-14], [14], [24], [8], [-21], [6], [-12], [-20], [-15], [-13], [20], [1], [12], [-32], [16], [19], [-18], [-10], [-6], [15], [-14], [-18], [-11], [20], [16], [-32], [26], [-38], [-30], [-30], [14], [15], [-18], [18], [20], [6], [-4], [-20], [-5], [-17], [19], [25], [29], [2], [-21], [-5], [-33], [42], [0], [-28], [-15], [-36], [-29], [38], [6], [3], [31], [44], [-9], [18], [8], [24], [-6], [41], [7], [35], [26], [8], [3], [-30], [20], [10], [14], [48], [18], [20], [-41], [35], [0], [25], [6], [-36], [12], [18], [-51], [-31], [-5], [-20], [-30], [12], [19], [46], [-25], [13], [-12], [4], [50], [29], [-12], [-7], [4], [22], [-30], [-16], [48], [12], [36], [-23], [3], [17], [58], [28], [20], [32], [-24], [54], [24], [38], [-58], [2], [3], [22], [4], [-48], [-21], [-36], [-29], [45], [-1], [2], [41], [33], [2], [52], [52], [64], [-54], [-18], [-46], [-48], [33], [34], [-36], [4], [32], [36], [32], [-7], [-11], [-33], [-27], [-18], [-24], [-52], [-42], [12], [15], [59], [-6], [-30], [-10], [-13], [-26], [-15], [-62], [42], [-16], [-59], [-28], [-28], [65], [-68], [52], [60], [-8], [18], [53], [-34], [22], [-6], [-63], [-39], [-14], [-62], [-48], [20], [0], [-23], [42], [69], [16], [2], [35], [11], [-36], [-58], [-57], [72], [-58], [62], [-21], [-47], [54], [-48], [-62], [-19], [-67], [-49], [-42], [34], [-58], [-20], [-50], [-74], [-32], [15], [64], [30], [7], [-26], [57], [-60], [8], [-15], [-19], [40], [-48], [11], [12], [40], [21], [18], [48], [39], [14], [24], [23], [-30], [-58], [23], [36], [-9], [-15], [-12], [-68], [8], [-42], [26], [54], [-30], [-87], [-35], [78], [-65], [42], [-43], [-16], [-62], [-58], [56], [53], [-21], [73], [64], [26], [-20], [2], [-45], [46], [-10], [21], [26], [-38], [30], [-80], [1], [-22], [-76], [70], [-16], [26], [53], [32], [-24], [11], [-68], [-41], [6], [-92], [-73], [55], [26], [-51], [-54], [42], [-72], [-60], [14], [74], [60], [3], [-28], [-16], [-3], [24], [22], [50], [-29], [-45], [89], [-64], [-86], [50], [-8], [80], [25], [26], [-78], [-54], [-50], [-40], [-80], [9], [34], [40], [-2], [48], [-67], [-53], [-13], [2], [-60], [55], [-8], [-21], [-86], [4], [23], [-53], [-80], [32], [-4], [6], [-7], [20], [-92], [-96], [-26], [14], [48], [-43], [-4], [75], [-62], [-56], [-30], [-13], [-86], [-30], [-94], [56], [34], [17], [-16], [60], [29], [-13], [44], [82], [81], [-74], [-82], [62], [53], [-18], [86], [70], [58], [15], [-51], [47]];
    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_v();".
// 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_v(: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_v();".
// 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_v( : 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![1, 1]>,<13,R![6, 1]>,<17,R![-2, 1]>],Snew);
    return Vf;
end function;