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