
// Make newform 5200.2.a.j in Magma, downloaded from the LMFDB on 21 December 2025.
// To make the character of type GrpDrchElt, type "MakeCharacter_5200_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_5200_2_a_j();".
// 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_5200_2_a_j();".
// 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_5200_a();"
function MakeCharacter_5200_a()
    N := 5200;
    order := 1;
    char_gens := [1951, 1301, 4577, 1601];
    v := [1, 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_5200_a_Hecke( : Kf := Kf)
    return MakeCharacter_5200_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 := [[0], [-1], [0], [-4], [-1], [1], [7], [3], [0], [-4], [-6], [8], [-5], [-4], [12], [10], [-4], [8], [-9], [8], [-13], [-8], [3], [-11], [10], [8], [-6], [3], [-12], [-11], [10], [-4], [-15], [-7], [-16], [18], [-16], [11], [-10], [-10], [-23], [-20], [18], [1], [-12], [18], [23], [-26], [20], [-8], [18], [20], [-25], [-9], [-6], [-28], [24], [-16], [22], [-6], [-31], [26], [-27], [0], [-6], [-6], [-13], [13], [27], [-34], [14], [0], [24], [-4], [-15], [-6], [-16], [-14], [-9], [7], [-19], [-8], [-12], [-25], [14], [-11], [-9], [25], [-6], [10], [4], [-14], [-4], [-36], [-20], [40], [12], [1], [15], [-14], [-17], [-2], [-24], [45], [-40], [3], [-9], [-37], [-6], [31], [26], [-2], [6], [28], [36], [42], [-4], [6], [-30], [-29], [18], [46], [-12], [1], [-13], [-40], [26], [-6], [10], [-2], [0], [-36], [10], [-36], [19], [-53], [-14], [-20], [28], [2], [20], [32], [-4], [-33], [-22], [10], [8], [-37], [9], [-30], [32], [6], [11], [-20], [52], [50], [-4], [6], [21], [14], [-56], [15], [-44], [-47], [-39], [-4], [18], [-16], [43], [36], [25], [38], [-46], [23], [16], [7], [-19], [-54], [-10], [-22], [-8], [-7], [-12], [7], [-14], [-36], [58], [65], [31], [-14], [-31], [-1], [-25], [-50], [43], [-14], [45], [24], [-17], [4], [46], [12], [-48], [47], [-36], [-6], [2], [-12], [-58], [-44], [-2], [6], [24], [-7], [38], [-13], [-6], [-27], [-42], [14], [-14], [-6], [-45], [22], [-69], [-6], [-35], [-8], [-2], [5], [-58], [47], [28], [11], [12], [-2], [23], [-48], [-73], [10], [41], [55], [-32], [-46], [23], [18], [-32], [-24], [-23], [54], [-4], [-51], [-12], [-21], [18], [-15], [54], [-69], [64], [21], [-44], [-19], [56], [-8], [-19], [65], [66], [-54], [-51], [52], [68], [43], [-9], [-52], [-6], [-24], [-39], [-8], [-70], [-67], [-36], [32], [-48], [-44], [83], [72], [-10], [-12], [64], [-18], [18], [60], [15], [-23], [36], [-30], [6], [48], [-40], [47], [63], [-16], [-38], [35], [-81], [25], [-4], [-76], [-64], [28], [74], [42], [-43], [-44], [52], [-18], [-1], [8], [71], [7], [-12], [37], [-90], [18], [-62], [74], [17], [52], [-66], [8], [2], [-28], [-40], [69], [11], [-12], [-14], [66], [-47], [20], [-42], [75], [-6], [-16], [-36], [-20], [28], [71], [22], [-48], [37], [-22], [30], [24], [-44], [-79], [-14], [-69], [86], [50], [-54], [43], [-70], [-12], [37], [77], [-6], [-94], [5], [48], [-23], [-70], [40], [58], [52], [-37], [-22], [27], [-15], [-10], [40], [-75], [2], [-41], [12], [42], [-58], [58], [12], [-78], [65], [42], [-103], [-35], [12], [-1], [-4], [-99], [61], [2], [-64], [59], [-62], [41], [18], [-50], [-92], [-50], [8], [-79], [-23], [-76], [-39], [28], [49], [-70], [-66], [10], [-19], [50], [52], [34], [-76], [55], [-11], [92], [52], [-87], [79], [-40]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_5200_a_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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