
// Make newform 9702.2.a.l in Magma, downloaded from the LMFDB on 24 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_l();".
// 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_l();".
// 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], [0], [0], [1], [-5], [6], [-2], [-6], [-3], [-8], [2], [-6], [-4], [6], [12], [-3], [7], [-13], [12], [10], [-1], [6], [6], [13], [3], [4], [0], [14], [-9], [-13], [6], [-15], [4], [6], [-1], [4], [17], [9], [-15], [-9], [10], [18], [-22], [9], [-14], [26], [10], [-6], [-20], [30], [-21], [10], [12], [27], [3], [-24], [25], [-1], [6], [22], [-18], [-32], [24], [7], [24], [-13], [-22], [-12], [34], [30], [9], [-38], [11], [-25], [12], [-12], [-2], [9], [4], [12], [-16], [-15], [-14], [19], [-12], [-30], [-22], [39], [26], [-12], [-3], [-4], [30], [8], [21], [6], [-18], [16], [5], [-28], [6], [18], [0], [20], [7], [9], [24], [0], [10], [-8], [26], [-21], [-44], [-16], [-9], [19], [36], [30], [12], [34], [-4], [42], [45], [-35], [3], [-10], [-6], [-50], [43], [2], [36], [-16], [-10], [30], [-2], [-24], [-14], [6], [-36], [16], [-27], [26], [-42], [16], [30], [58], [48], [-35], [-6], [-37], [-15], [-19], [57], [44], [-30], [-16], [15], [52], [-27], [24], [-24], [32], [-33], [18], [-6], [-16], [46], [20], [-42], [-30], [-14], [-15], [-34], [22], [-39], [8], [-6], [16], [34], [-28], [-18], [56], [24], [48], [6], [23], [55], [-4], [-39], [-26], [-24], [-13], [-60], [-12], [39], [-28], [50], [-6], [-15], [21], [-17], [-44], [-14], [33], [12], [4], [54], [-48], [25], [38], [-30], [-16], [36], [21], [55], [-13], [24], [24], [6], [59], [-11], [-39], [2], [-6], [35], [57], [-24], [-11], [-18], [44], [-38], [-19], [54], [-53], [33], [-50], [33], [-57], [0], [48], [40], [4], [50], [42], [0], [-38], [9], [-22], [-51], [23], [0], [-18], [-62], [45], [30], [74], [4], [18], [16], [-16], [48], [-14], [-11], [45], [-35], [48], [-39], [14], [66], [46], [-4], [-53], [-4], [-68], [-44], [-27], [5], [14], [78], [-51], [50], [-33], [37], [-2], [-18], [11], [-6], [46], [0], [-42], [-63], [-84], [-66], [59], [-45], [-62], [-12], [-18], [-62], [64], [-3], [65], [63], [41], [5], [51], [4], [30], [-70], [-69], [18], [24], [-4], [9], [-20], [-33], [-60], [-26], [-72], [4], [-25], [-15], [-40], [0], [43], [-64], [-80], [-39], [27], [-82], [-18], [-5], [-66], [-82], [-3], [-64], [48], [13], [16], [-46], [54], [-15], [50], [-54], [-90], [4], [17], [-72], [48], [4], [50], [-30], [-53], [14], [-18], [-48], [-30], [-42], [-72], [-43], [66], [-12], [0], [7], [-19], [30], [92], [-58], [57], [88], [-9], [6], [-71], [56], [-21], [48], [46], [-54], [28], [-12], [30], [53], [-78], [28], [-18], [-22], [-86], [68], [-42], [101], [0], [51], [-53], [84], [-4], [31], [81], [53], [87], [-41], [-39], [59], [93], [-39], [64], [-34], [-60], [4], [-21], [-80], [42], [69], [50], [104], [-18], [-33], [-8], [-51], [21], [-30], [43], [30], [12], [-65], [-36], [-45], [-6], [7], [51], [-5], [72], [17], [-96], [82], [27], [2], [-4], [-52], [37], [36], [18], [-61], [-48], [-86], [96], [-14], [-39], [94], [55], [38], [12], [-78], [-42], [-46], [-48], [-7], [-72], [58], [6], [-25], [-16], [60], [-70], [-110], [44], [35], [-51], [-54], [-80], [-37], [-36], [30], [-4], [75], [1], [12], [-92], [-27], [-78], [31], [12], [-89], [54], [-8], [48], [-4], [69], [-44], [68], [94], [-45], [53], [87], [48], [34], [55], [-12], [40], [26], [111], [88], [18], [35], [86], [-18], [-102], [-50], [29], [-44], [96], [45], [-56], [-96], [44], [53], [-6], [43], [72], [88], [26], [8], [60], [102], [-44], [-84], [58], [18], [18], [96], [8], [-15], [-40], [-45], [52], [111], [-104], [-42], [20], [-112], [-24], [-90], [-68], [66], [-12], [-67], [62], [-60], [28], [-87], [54], [52], [-30], [96], [27], [-11], [-49]];
    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_l();".
// 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_l(: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_l();".
// 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_l( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_9702_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![0, 1]>,<13,R![5, 1]>,<17,R![-6, 1]>,<19,R![2, 1]>,<23,R![6, 1]>,<29,R![3, 1]>],Snew);
    return Vf;
end function;