
// Make newform 5010.2.a.h in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_5010_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_5010_2_a_h();".
// 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_5010_2_a_h();".
// 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_5010_a();"
function MakeCharacter_5010_a()
    N := 5010;
    order := 1;
    char_gens := [3341, 3007, 4681];
    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_5010_a_Hecke(Kf)
    return MakeCharacter_5010_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], [1], [-2], [0], [4], [-1], [3], [2], [4], [10], [7], [-12], [-8], [-9], [4], [6], [5], [-12], [11], [3], [14], [6], [0], [18], [6], [3], [-9], [16], [-5], [-15], [-9], [0], [-18], [-9], [18], [-11], [-1], [-8], [-2], [-14], [24], [-9], [2], [-10], [-5], [21], [18], [20], [3], [9], [-10], [-28], [-22], [-30], [18], [-29], [-2], [16], [-24], [14], [-7], [11], [-29], [-18], [10], [16], [26], [-14], [-17], [3], [11], [24], [30], [-36], [26], [27], [3], [-17], [0], [30], [-9], [14], [13], [24], [-40], [10], [-7], [-12], [-1], [-10], [-16], [30], [-32], [-14], [-15], [-17], [-36], [-18], [-12], [-16], [25], [38], [-44], [-38], [42], [20], [-9], [21], [-22], [-37], [-47], [32], [-12], [5], [-49], [32], [-18], [9], [-10], [-47], [6], [-16], [-36], [30], [-18], [20], [-28], [13], [40], [-54], [-32], [-5], [6], [0], [-31], [-31], [21], [12], [28], [-46], [-2], [30], [25], [28], [-49], [39], [20], [-18], [38], [-14], [44], [-29], [-2], [37], [-4], [6], [7], [-24], [-25], [-26], [-40], [48], [54], [53], [40], [-17], [9], [48], [-51], [-56], [-11], [54], [39], [0], [44], [12], [-56], [-22], [24], [14], [16], [34], [-51], [39], [-37], [-10], [10], [-66], [34], [-54], [-11], [-57], [-9], [-31], [-9], [-67], [35], [-36], [54], [-42], [48], [-52], [21], [18], [-1], [3], [-33], [11], [38], [42], [2], [-36], [53], [-50], [-2], [60], [-46], [-19], [59], [14], [45], [-25], [-42], [2], [69], [-30], [-8], [66], [12], [-66], [-40], [26], [-56], [-3], [48], [-4], [72], [0], [-2], [-15], [-66], [-18], [21], [20], [56], [-3], [5], [51], [58], [20], [-16], [-14], [39], [52], [-37], [60], [-42], [62], [-16], [-12], [-45], [56], [-66], [20], [44], [63], [-19], [-16], [13], [-30], [-73], [-32], [-28], [34], [-60], [19], [-46], [62], [39], [61], [0], [-42], [48], [24], [54], [79], [46], [35], [-9], [-10], [66], [-70], [-61], [46], [-78], [-21], [-36], [70], [-43], [3], [-2], [-54], [-60], [-33], [-72], [-28], [38], [-46], [13], [-65], [52], [12], [-12], [53], [84], [12], [-14], [-54], [11], [-18], [77], [-12], [-44], [41], [59], [20], [24], [58], [66], [54], [35], [-31], [54], [-21], [44], [6], [-70], [-8], [26], [-33], [70], [9], [52], [72], [-24], [80], [28], [-22], [20], [82], [30], [-38], [-39], [-42], [-15], [-45], [24], [-26], [-33], [-78], [-72], [52], [4], [45], [-63], [26], [65], [40], [6], [66], [51], [88], [38], [-17], [-96], [60], [41], [-56], [53], [-26], [2], [75], [21], [-45], [5], [-66], [-32], [-21], [16], [-36], [-30], [-43], [-88], [-9], [-10], [84], [-82], [96], [34], [-18], [10], [20], [-38], [41], [68], [37], [60], [48], [34], [-81], [-68], [31], [44], [90], [-60], [53], [42], [-64], [-72], [34], [100], [14], [84], [-87], [45], [30], [-73], [46]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_5010_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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