
// Make newform 5239.2.a.c in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_5239_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_5239_2_a_c();".
// 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_5239_2_a_c();".
// 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_5239_a();"
function MakeCharacter_5239_a()
    N := 5239;
    order := 1;
    char_gens := [1861, 1522];
    v := [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_5239_a_Hecke(Kf)
    return MakeCharacter_5239_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], [2], [4], [2], [-1], [0], [2], [-2], [4], [8], [-1], [3], [-4], [-4], [2], [-4], [-6], [10], [16], [-6], [7], [-16], [12], [15], [2], [3], [15], [9], [-2], [-17], [4], [1], [-6], [-14], [22], [-17], [13], [-12], [-3], [9], [0], [6], [-5], [-14], [-15], [-10], [19], [-19], [8], [29], [-13], [-16], [-14], [-22], [-21], [-24], [10], [15], [16], [-24], [-13], [18], [14], [-15], [16], [24], [4], [-8], [-2], [36], [15], [20], [4], [9], [20], [13], [-6], [6], [-29], [-3], [-40], [26], [18], [-16], [4], [-5], [6], [-31], [-11], [-5], [36], [-30], [16], [-20], [-24], [0], [30], [-15], [22], [20], [-12], [9], [31], [-30], [-18], [16], [-31], [42], [-24], [-24], [37], [-30], [-22], [-15], [35], [-42], [49], [6], [-21], [-20], [-42], [-10], [-16], [-32], [12], [-41], [-25], [-24], [-20], [-8], [23], [-33], [-35], [10], [22], [-2], [-47], [-3], [42], [-30], [-22], [10], [10], [36], [18], [18], [-16], [19], [-32], [-8], [-34], [-36], [-6], [-32], [-37], [22], [11], [-57], [-7], [21], [29], [-8], [37], [-57], [44], [48], [-46], [51], [13], [30], [-11], [35], [9], [-44], [35], [36], [-32], [-22], [11], [62], [-2], [-6], [-45], [51], [16], [20], [25], [-6], [-58], [-12], [40], [-54], [-40], [29], [-48], [26], [-35], [-47], [40], [-1], [-30], [-6], [-62], [-61], [-12], [6], [-4], [64], [-6], [-13], [34], [12], [-16], [-42], [-1], [-28], [25], [15], [35], [6], [18], [-10], [-16], [-4], [-36], [-14], [11], [-34], [-56], [-30], [26], [-25], [18], [28], [-64], [63], [2], [8], [16], [-30], [-60], [64], [30], [-46], [15], [48], [-35], [28], [-60], [37], [28], [40], [-24], [45], [22], [-28], [22], [47], [-58], [-8], [35], [62], [-19], [54], [18], [-28], [-22], [-76], [-68], [-30], [7], [65], [18], [-24], [47], [-24], [-10], [75], [-67], [44], [-63], [-50], [-20], [-44], [-82], [34], [0], [7], [-64], [-62], [26], [83], [41], [6], [-30], [48], [-18], [26], [-84], [44], [-19], [-50], [40], [-20], [18], [-52], [52], [-13], [-21], [-34], [46], [82], [27], [24], [66], [-20], [1], [-41], [-44], [-60], [0], [1], [-90], [34], [78], [-8], [20], [2], [28], [-32], [20], [78], [7], [-45], [-30], [48], [7], [-30], [20], [-8], [91], [18], [22], [-36], [74], [32], [-2], [7], [4], [9], [-41], [23], [68], [66], [57], [22], [21], [-31], [42], [-62], [88], [64], [70], [86], [-73], [-8], [64], [-41], [27], [-33], [55], [58], [-15], [-11], [-68], [84], [-71], [-46], [-25], [50], [-6], [-1], [-24], [73], [-49], [-40], [88], [4], [87], [-8], [-18], [-6], [90], [28], [-8], [75], [-56], [-63], [-70], [33], [5], [69], [62], [-16], [25], [65], [-12], [5], [-31], [36], [-24], [58], [-24], [8], [57], [15], [9], [-49], [18], [-24], [-84], [15], [75], [25], [14], [27], [-71], [49], [35], [108]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_5239_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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