
// Make newform 5200.2.a.cg in Magma, downloaded from the LMFDB on 22 December 2024.
// 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_cg();".
// 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_cg();".
// 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
        poly := [1, -3, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 1, 0], [-2, -1, 1]];
    Rf_basisdens := [1, 1, 1];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
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)
    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, 0, 0], [0, 0, -1], [0, 0, 0], [1, 1, 1], [1, -3, 0], [-1, 0, 0], [-4, 2, 0], [-1, 1, 2], [2, 0, -1], [-1, 1, -1], [-1, 3, 2], [-5, -1, 3], [4, -4, 0], [-4, 6, 3], [7, -1, -1], [-8, 0, -2], [-1, 1, -4], [-3, -5, 3], [-1, 3, -1], [-3, -1, -4], [-3, -3, -3], [0, -4, -2], [1, -7, -5], [2, 4, 8], [-6, 8, 2], [-2, 4, 2], [0, -4, 3], [0, -6, 7], [2, -6, 0], [-4, -6, 0], [0, 0, -7], [2, -2, -2], [-4, -6, -10], [4, -4, -2], [0, 0, -6], [15, 1, 0], [2, -4, -8], [-9, -5, 1], [-9, 3, -7], [-16, 0, 2], [-10, 10, 8], [-5, -3, -9], [12, -4, 0], [-4, 6, 6], [-6, 8, 8], [-2, -2, 8], [10, 6, -4], [-7, -7, 5], [-15, 1, 1], [-8, 10, -4], [-14, 10, 2], [5, -11, -6], [14, 6, -2], [-18, 6, 2], [-6, 2, 10], [0, 8, 5], [-10, -4, -10], [-7, 7, -6], [18, 6, -4], [-18, -2, -2], [-20, 6, -1], [-3, -3, -11], [-5, -5, -3], [8, 0, -8], [2, 4, -2], [-3, 5, 1], [-17, -1, -8], [10, -8, -6], [-6, 14, 3], [8, -14, -8], [-5, -9, 7], [11, 5, -10], [-4, 4, 11], [-2, 0, 8], [3, -1, -10], [11, -5, 5], [-6, -8, 0], [-13, 7, -9], [8, -10, -6], [0, -8, 4], [22, -6, 2], [2, 2, 8], [-13, -5, 2], [20, -4, 8], [-4, 4, -10], [6, -10, -15], [-4, 8, -4], [2, -4, 2], [10, 8, -6], [-17, -1, -3], [-12, -12, -1], [-5, -3, 6], [23, 3, 9], [-2, 6, -8], [3, 9, 2], [16, -10, -1], [14, 8, -6], [7, -11, -1], [12, 0, 11], [-34, 12, 6], [-8, 14, -1], [-29, -1, -1], [-8, -10, 7], [-1, 1, -3], [0, 0, -6], [5, -3, -11], [-5, -5, 1], [-2, 4, -14], [8, 4, 12], [-10, 0, -20], [20, 6, 1], [8, 2, 12], [-8, -2, 14], [15, -3, 12], [-25, 3, -4], [10, 16, -12], [-5, 7, 7], [28, 4, 9], [-30, 4, 4], [6, 2, -18], [0, 16, 10], [2, 20, 6], [-2, 10, 8], [7, 11, -5], [-15, 21, 18], [-22, 8, 10], [22, -4, -10], [-10, 10, -6], [2, -16, 5], [2, -4, -8], [13, -3, 18], [15, -5, 13], [-2, -14, 14], [8, -20, 6], [24, -2, 10], [20, -10, -2], [-17, 15, 7], [7, -17, -19], [-8, -18, -6], [-11, 15, 5], [1, 17, 6], [16, 4, 18], [30, 2, 5], [-23, 1, -1], [-11, -13, 11], [9, 5, 4], [17, -19, -11], [-4, 0, -4], [2, -22, -20], [-7, 13, -3], [24, -10, -12], [-7, 11, -5], [10, -4, 17], [18, -2, -19], [0, -8, -5], [-2, 10, -4], [4, 4, -8], [-18, -2, 2], [-10, -10, 10], [12, -6, 0], [-19, 5, 7], [-16, 8, 4], [1, -19, 1], [26, 6, 8], [1, 17, 13], [15, -9, -7], [20, 4, 10], [14, 6, 16], [0, -14, -14], [16, -12, -10], [-15, 1, -16], [16, -6, 4], [-22, 14, 8], [11, 7, -9], [-4, 16, 18], [-6, 12, -4], [13, 19, -12], [-16, 2, -8], [-12, -22, 5], [18, -12, 16], [-17, 19, 7], [-20, 20, 20], [36, -12, -10], [4, 2, -14], [31, -21, -9], [-7, -9, 5], [0, 6, 18], [-5, -9, 3], [-10, 22, -2], [11, -7, 16], [-2, -4, -6], [-35, 1, -5], [-10, 6, 24], [18, -14, -12], [-24, 10, 1], [20, -16, -4], [10, -2, -14], [8, -22, 6], [3, -9, -25], [26, 20, -15], [-20, -18, 0], [6, -14, -2], [12, -10, 0], [9, -1, -11], [-15, 37, 6], [28, -4, 18], [-7, 17, 18], [-18, 10, 9], [42, -16, -12], [10, -22, -24], [-4, -12, -16], [-11, 31, 7], [-10, 4, -7], [1, -3, -7], [-5, -19, -12], [-4, -12, 8], [12, 8, -5], [-14, 32, -2], [-31, 9, 19], [-10, 16, -16], [17, -21, -1], [29, -15, -12], [28, 6, -14], [25, 13, -13], [-44, -12, 7], [-11, 15, 19], [12, 4, 20], [-22, 2, 18], [32, -34, -13], [-1, 5, -14], [-50, 12, -4], [14, -10, -8], [15, -19, 10], [-6, -20, -22], [4, -24, -13], [3, 7, -3], [12, 16, 24], [25, -11, -7], [6, 26, 0], [12, 20, -10], [11, 7, 13], [0, -12, -12], [-12, 18, -9], [-2, 28, 30], [32, -30, -10], [-12, -8, -14], [11, 23, 3], [23, 7, 22], [-19, -11, 16], [20, -20, 3], [-29, 15, 11], [4, 6, -2], [13, 1, -3], [-46, 4, -4], [-14, 12, 12], [37, -25, -14], [1, -5, 1], [-39, 13, 15], [-12, -2, 2], [-27, -3, 13], [4, 2, 17], [-42, 2, 11], [14, 20, 34], [-10, -14, -8], [-25, -17, 11], [-28, 12, 8], [-24, -10, -4], [22, -4, -12], [21, -3, -11], [0, -8, -10], [-6, 20, 6], [59, -5, 1], [-12, 10, 14], [-10, 34, 12], [16, -26, -16], [27, -13, -19], [-19, -15, 17], [16, 6, -12], [-16, 28, 4], [36, -28, -14], [18, -4, 17], [3, 3, -8], [-2, -8, -17], [8, -6, -12], [19, -17, 21], [2, -14, 10], [-14, -14, 2], [-20, 2, -8], [-23, 27, 4], [-29, 33, 5], [-3, 27, 23], [48, -18, -9], [-22, 20, 14], [15, 15, 2], [10, 12, 32], [-34, 4, 8], [-22, -14, -8], [8, -6, 38], [40, 12, -8], [-11, 21, -5], [-34, -14, 18], [-4, -42, 12], [10, -30, -32], [-52, 0, -9], [-12, 12, -2], [-43, -7, 1], [18, 22, 5], [18, -28, -2], [-19, -27, -8], [-20, 14, 2], [-34, 18, 11], [48, -18, -12], [7, -19, -17], [50, -16, -13], [-37, 43, 21], [21, -13, 1], [-39, -13, -4], [31, 21, 2], [17, 9, 5], [10, -8, -6], [-18, 6, 22], [-47, 9, 5], [21, -5, 15], [19, 15, -15], [-35, -7, -11], [15, 13, -11], [3, -3, 12], [33, 5, 7], [-10, -6, -41], [34, -44, -16], [-18, 26, 20], [-44, 14, 2], [16, -36, 12], [-59, 41, 17], [27, -3, -12], [-61, -5, -13], [-26, 26, 8], [-14, -8, 2], [-8, -8, -8], [30, -10, 23], [-6, 28, -4], [-4, -18, -16], [16, 20, 22], [0, 0, 26], [-10, -12, -40], [-38, -6, 10], [18, 4, 30], [15, -5, 27], [-35, 15, 10], [0, -22, -34], [-37, 19, 12], [20, 2, 26], [-14, 2, -20], [-14, -2, 1], [-50, -8, 0], [42, 20, 14], [43, 15, 6], [-51, 25, 22], [26, 18, 26], [3, 19, 27], [5, -3, -19], [-15, 27, 41], [-28, 24, 11], [19, -17, 14], [28, -10, -27], [-12, -26, -40], [24, 38, -24], [45, 1, 13], [2, 16, 16], [-14, -10, 0], [12, 32, 16], [7, -33, 15], [-37, 1, 5], [6, -38, 6], [-18, -14, 28], [-57, 17, 2], [32, 4, 10], [-6, 12, 30], [1, -1, 19], [48, -10, -12], [60, -6, 8], [1, -7, 21], [11, 35, 9], [-7, -31, -7], [-9, 23, 16], [-45, -17, -31], [39, -1, 26], [18, 26, -4], [17, 17, -5], [-18, -24, -1], [-8, 36, -12], [31, -1, -5], [-5, 9, 4], [-30, 24, 29], [53, -17, -14], [-64, -8, -12], [79, -1, 4], [-1, -43, -5], [36, 0, 26], [14, 6, 28], [-22, 2, 36], [-14, 28, 10], [-41, -9, 3], [15, -1, -25], [-30, 10, 12], [-72, -12, -10], [13, -3, 9], [-58, -12, 8], [11, 23, 13], [15, -31, -16], [42, -54, -34], [-22, 46, 16], [-30, -16, -45], [18, 14, 34], [28, 24, 24], [-10, -28, -8], [-7, 37, 10], [-58, 4, 15], [-9, 23, 27], [-50, 10, -19], [9, 27, -33], [-6, -4, 24], [-21, -29, -11], [62, -30, -16], [-14, 0, -18], [1, -35, 5], [-24, -14, 23], [54, -22, -14], [15, 9, -22], [-56, -20, 8]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_5200_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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