
// Make newform 9800.2.a.m in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_9800_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_9800_2_a_m();".
// 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_9800_2_a_m();".
// 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_9800_a();"
function MakeCharacter_9800_a()
    N := 9800;
    order := 1;
    char_gens := [7351, 4901, 1177, 5001];
    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_9800_a_Hecke(Kf)
    return MakeCharacter_9800_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], [-1], [0], [0], [-5], [-7], [3], [-2], [-8], [-5], [-10], [-4], [-6], [-2], [7], [10], [-10], [-12], [2], [0], [2], [-7], [-4], [-8], [-17], [10], [-1], [-2], [13], [12], [6], [-8], [-12], [-16], [2], [1], [-2], [-8], [11], [-9], [12], [10], [-17], [0], [-18], [10], [-19], [11], [-1], [-10], [-8], [25], [20], [-20], [14], [22], [0], [-20], [28], [11], [-31], [-5], [-7], [-30], [5], [-26], [28], [16], [-28], [-26], [-21], [-16], [7], [0], [-16], [0], [33], [-13], [-3], [14], [-26], [29], [-21], [-2], [-28], [-10], [15], [6], [-12], [-32], [-35], [12], [-20], [-27], [-17], [9], [-8], [-14], [0], [15], [-28], [12], [-4], [18], [-12], [33], [12], [-11], [39], [10], [29], [-22], [12], [36], [19], [-26], [39], [12], [6], [23], [4], [32], [-11], [12], [36], [-5], [-21], [0], [-32], [5], [-25], [-6], [-23], [24], [-48], [34], [49], [-45], [-1], [13], [40], [37], [14], [24], [2], [-4], [-38], [26], [-44], [-6], [-6], [-32], [36], [48], [-28], [-48], [-7], [-6], [3], [-44], [2], [42], [50], [6], [42], [-3], [-12], [39], [-39], [-14], [-20], [34], [-13], [24], [-38], [-36], [-13], [33], [-52], [16], [40], [-56], [-26], [-6], [-12], [-56], [22], [-29], [-27], [0], [6], [-66], [-15], [-18], [-48], [-63], [-37], [-34], [-33], [43], [-18], [-58], [38], [-2], [30], [-25], [36], [-48], [-54], [40], [-50], [4], [-2], [-33], [-34], [-68], [-4], [-6], [-22], [-36], [-6], [-46], [-39], [54], [-57], [-10], [-21], [-1], [19], [16], [-40], [30], [-8], [41], [35], [27], [58], [44], [-63], [0], [68], [-4], [39], [38], [5], [-6], [72], [-2], [-47], [-54], [50], [22], [6], [44], [6], [3], [-42], [-44], [-3], [-27], [-42], [-60], [10], [-39], [-18], [0], [-19], [-6], [-30], [-18], [-22], [-74], [-21], [80], [-14], [-57], [-16], [14], [53], [-2], [-85], [45], [56], [28], [55], [27], [-46], [-22], [50], [2], [-15], [17], [-22], [-26], [46], [74], [48], [-63], [-8], [55], [31], [56], [-25], [36], [-19], [8], [-26], [-4], [4], [22], [-44], [-61], [-78], [-54], [10], [-64], [14], [-9], [-65], [-15], [-70], [14], [-72], [-46], [-22], [74], [73], [52], [10], [-54], [-50], [42], [-52], [-7], [4], [-9], [-87], [-6], [-82], [-33], [36], [-26], [-24], [15], [-12], [-84], [-80], [32], [56], [-17], [-30], [-88], [-43], [35], [-43], [-66], [-90], [64], [62], [-56], [84], [-82], [-10], [27], [68], [90], [-1], [-58], [65], [7], [0], [78], [-21], [-34], [42], [-36], [75], [-7], [64], [1], [18], [-100], [22], [52], [-22], [31], [-77], [73], [-16], [-7], [-55], [87], [-27], [88], [-65], [96], [-8], [94], [79], [86], [-72], [-18], [20], [79], [4], [-42], [-44], [39], [-4], [-8], [-17], [-98], [24], [52], [4], [0], [3], [24], [2], [-89], [37], [-22], [62], [56], [57], [15], [-42], [42], [70], [80], [32], [20], [-4], [9], [29], [8], [-29], [7], [-6], [28], [-35], [91], [-34], [-3], [72], [22], [5], [-96], [-96], [-16], [-20], [24], [-24], [70], [-86], [-71], [-55], [-12], [67], [42], [-77], [83], [-15], [-93], [-3], [-22], [56], [-11], [66], [44], [28], [48], [6], [25]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_9800_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_9800_2_a_m();".
// 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_9800_2_a_m(:prec:=1)
    chi := MakeCharacter_9800_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(3361) - 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_9800_2_a_m();".
// 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_9800_2_a_m( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_9800_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![1, 1]>,<11,R![5, 1]>,<13,R![7, 1]>,<19,R![2, 1]>,<23,R![8, 1]>],Snew);
    return Vf;
end function;