
// Make newform 5202.2.a.f in Magma, downloaded from the LMFDB on 23 September 2025.
// To make the character of type GrpDrchElt, type "MakeCharacter_5202_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_5202_2_a_f();".
// 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_5202_2_a_f();".
// 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_5202_a();"
function MakeCharacter_5202_a()
    N := 5202;
    order := 1;
    char_gens := [2891, 4627];
    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_5202_a_Hecke( : Kf := Kf)
    return MakeCharacter_5202_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]];
    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], [0], [4], [-3], [-4], [-5], [0], [1], [6], [-4], [7], [-1], [-8], [3], [6], [-12], [6], [7], [9], [0], [14], [8], [2], [0], [-1], [4], [7], [2], [-2], [20], [15], [-12], [2], [-5], [10], [15], [-23], [-16], [16], [6], [0], [-11], [-8], [21], [-10], [24], [1], [16], [-2], [-14], [14], [-6], [22], [2], [-2], [-22], [6], [-11], [21], [-12], [9], [-22], [12], [24], [29], [30], [7], [14], [-20], [14], [-18], [-18], [27], [-18], [-5], [-20], [8], [-22], [16], [-31], [-14], [-29], [-32], [-5], [-31], [16], [-22], [34], [-6], [35], [-6], [36], [-23], [6], [29], [18], [0], [-28], [-4], [-2], [-27], [-32], [-24], [0], [23], [11], [-10], [-4], [32], [-15], [21], [-7], [30], [44], [21], [26], [19], [28], [-36], [-24], [9], [1], [-14], [10], [-17], [38], [5], [-14], [43], [-29], [-17], [-22], [25], [11], [-48], [6], [16], [-13], [22], [14], [-1], [-34], [-29], [22], [-49], [30], [11], [36], [-3], [-10], [14], [-4], [36], [36], [-7], [-28], [0], [38], [47], [20], [-4], [48], [4], [-20], [-44], [-54], [-20], [35], [-25], [-10], [48], [49], [-28], [-27], [-31], [12], [-55], [10], [12], [-39], [64], [-30], [41], [-48], [-56], [34], [-7], [4], [-53], [-4], [11], [38], [-27], [20], [22], [-6], [-11], [30], [-6], [26], [54], [-48], [43], [33], [-48], [18], [37], [12], [-18], [51], [-11], [-6], [-64], [2], [-40], [37], [19], [-30], [-58], [52], [-17], [12], [42], [-29], [-60], [5], [-12], [10], [-73], [-58], [11], [-44], [29], [30], [-49], [-20], [18], [36], [-36], [-70], [56], [55], [40], [-2], [2], [-42], [53], [56], [33], [-26], [-53], [74], [78], [-73], [-8], [66], [61], [-19], [18], [-9], [72], [44], [9], [-37], [2], [-56], [-18], [-24], [-57], [66], [-2], [9], [-55], [-57], [-62], [-29], [38], [-75], [-1], [54], [-42], [-32], [-58], [57], [20], [-54], [-74], [54], [-39], [2], [16], [58], [-28], [16], [-46], [-60], [0], [-22], [14], [-55], [14], [-36], [11], [30], [17], [86], [-78], [-35], [52], [17], [-36], [6], [24], [36], [-44], [-27], [18], [-34], [23], [24], [39], [22], [-90], [-25], [18], [62], [-20], [-72], [-72], [6], [-7], [-52], [64], [88], [83], [-30], [69], [-24], [47], [-72], [54], [-32], [-70], [8], [-10], [6], [-90], [-15], [38], [-64], [52], [-53], [40], [17], [-83], [-20], [64], [76], [42], [50], [6], [34], [-76], [-30], [-60], [46], [66], [0], [-71], [36], [65], [24], [-66], [36], [-71], [-78], [18], [-17], [-94], [-47], [-70], [-90], [3], [90], [-73], [-14], [-1], [59], [84], [50], [-25], [-90], [0], [8], [24], [39], [19], [-38], [43], [22], [27], [90], [8], [40], [-12], [77], [19], [32], [-100], [-50], [-2], [58], [56], [-1], [57], [-14], [48], [-23], [48], [86], [-98], [67], [64], [10], [-59], [-24], [-48], [-12], [65], [10]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_5202_a_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_5202_2_a_f();".
// 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_5202_2_a_f(:prec:=1)
    chi := MakeCharacter_5202_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_5202_2_a_f();".
// 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_5202_2_a_f( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_5202_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![-4, 1]>,<7,R![3, 1]>,<23,R![-6, 1]>,<47,R![-6, 1]>],Snew);
    return Vf;
end function;