
// Make newform 9702.2.a.k in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_9702_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_9702_2_a_k();".
// 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_9702_2_a_k();".
// 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_9702_a();"
function MakeCharacter_9702_a()
    N := 9702;
    order := 1;
    char_gens := [4313, 199, 5293];
    v := [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_9702_a_Hecke(Kf)
    return MakeCharacter_9702_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 := [[-1], [0], [-1], [0], [1], [-2], [5], [6], [-7], [8], [-10], [-8], [7], [4], [-1], [-6], [6], [-1], [3], [8], [-10], [-9], [-15], [-12], [-13], [12], [-12], [13], [19], [-14], [17], [12], [18], [-2], [4], [9], [-4], [13], [-8], [-14], [2], [-20], [-20], [8], [24], [26], [-20], [-10], [-17], [-20], [-21], [-16], [-12], [24], [0], [-18], [3], [-16], [-18], [19], [-24], [6], [-8], [17], [-22], [3], [13], [18], [13], [13], [-6], [-20], [2], [-13], [-17], [-16], [-15], [-12], [12], [-10], [10], [16], [-10], [13], [-7], [-24], [-36], [8], [-30], [4], [6], [-4], [-10], [-9], [-28], [0], [-22], [10], [34], [-1], [6], [16], [-28], [42], [16], [7], [42], [-6], [-3], [-8], [-29], [-11], [24], [7], [-42], [12], [-4], [-45], [-13], [-19], [-14], [22], [2], [-24], [-15], [-42], [-42], [-5], [-20], [37], [6], [12], [-38], [-10], [-39], [-34], [-33], [-14], [23], [-9], [-2], [-6], [-10], [43], [20], [21], [7], [-7], [-25], [5], [-41], [-54], [-47], [-2], [17], [39], [39], [-30], [-42], [-14], [58], [19], [35], [-22], [10], [15], [-8], [30], [4], [-34], [-9], [10], [-22], [-38], [-56], [-56], [26], [-38], [-21], [-46], [-6], [-19], [-38], [-17], [32], [54], [37], [-3], [20], [64], [-34], [-13], [35], [-38], [-4], [-42], [-40], [-22], [18], [18], [-10], [56], [-10], [40], [48], [-18], [-14], [39], [-15], [52], [-28], [-35], [-28], [-58], [-24], [14], [-40], [3], [3], [-50], [54], [59], [-18], [40], [3], [19], [16], [-51], [65], [52], [-52], [-8], [28], [33], [39], [12], [6], [36], [-6], [35], [-54], [14], [-18], [44], [-54], [-30], [-22], [-20], [0], [48], [54], [-22], [-49], [-37], [-22], [12], [-58], [38], [-58], [52], [55], [-40], [33], [-23], [66], [12], [-57], [-26], [22], [34], [-16], [-71], [18], [-25], [32], [76], [-42], [41], [16], [-39], [-24], [68], [54], [65], [38], [75], [10], [80], [-36], [-78], [81], [68], [39], [47], [-41], [-10], [-6], [19], [71], [-54], [-80], [-4], [8], [-54], [17], [71], [-34], [-4], [-24], [39], [78], [22], [-35], [-3], [66], [0], [64], [12], [81], [-18], [-68], [-25], [-14], [-2], [66], [90], [41], [-9], [-66], [-30], [-51], [34], [40], [53], [-14], [48], [64], [-6], [-18], [48], [-34], [72], [-40], [-62], [-18], [-12], [-14], [4], [-42], [-21], [30], [35], [-7], [19], [6], [42], [-11], [-95], [14], [57], [-34], [49], [-16], [33], [-68], [9], [-32], [4], [-88], [12], [1], [62], [-50], [16], [-12], [36], [-44], [82], [-55], [18], [-49], [68], [29], [-2], [74], [-89], [-40], [30], [58], [65], [-77], [61], [-16], [13], [-9], [-96], [40], [98], [20], [-8], [-18], [-54], [0], [30], [12], [41], [-8], [-15], [7], [-38], [-36], [18], [-78], [-20], [52], [-63], [64], [-44], [94], [-52], [71], [-57], [-28], [100], [-74], [63], [18], [-54], [-11], [88], [-27], [-17], [-29], [9], [-86], [34], [-46], [98], [61], [-31], [24], [-30], [34], [86], [44], [-1], [-40], [18], [-11], [-67], [72], [24], [-65], [-99], [37], [54], [-13], [27], [13], [54], [60], [-2], [92], [-58], [-88], [-22], [-33], [72], [93], [5], [-71], [-12], [0], [46], [-32], [11], [-33], [-4], [14], [-11], [-3], [55], [-82], [96], [-24], [93], [-50], [-48], [-35], [-31], [-23], [-94], [-31], [-54], [12], [-89], [-107], [63], [4], [52], [-4], [9], [-81], [-1], [104], [62], [77], [14], [15], [-54], [33], [-88], [14], [-45], [-8], [-102], [-38], [27], [81], [-22], [-24], [52], [47], [-55], [-26], [115], [43], [-96], [103], [62], [0], [-36], [-23], [-66], [-16], [96], [-118], [24], [36], [20], [96], [-42], [-32], [-43], [106], [25], [-40], [-16], [57], [36], [27], [56], [56], [-33], [30], [-110], [-76]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_9702_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_9702_2_a_k();".
// 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_9702_2_a_k(:prec:=1)
    chi := MakeCharacter_9702_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(4027) - 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_9702_2_a_k();".
// 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_9702_2_a_k( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_9702_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![1, 1]>,<13,R![2, 1]>,<17,R![-5, 1]>,<19,R![-6, 1]>,<23,R![7, 1]>,<29,R![-8, 1]>],Snew);
    return Vf;
end function;