
// Make newform 9702.2.a.bq in Magma, downloaded from the LMFDB on 10 June 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_bq();".
// 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_bq();".
// 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]];
    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], [-4], [-1], [3], [1], [1], [-6], [-3], [-6], [1], [-1], [0], [7], [-6], [-4], [15], [-12], [0], [-16], [-8], [-7], [15], [-4], [-2], [-18], [-4], [-7], [-2], [4], [17], [-1], [11], [9], [-18], [-12], [-6], [25], [-22], [24], [14], [-27], [-4], [-20], [-16], [-2], [6], [11], [-16], [4], [-7], [-12], [14], [-18], [16], [-16], [27], [-20], [-15], [20], [-31], [33], [-12], [10], [-26], [20], [-32], [-8], [-2], [-20], [34], [-22], [29], [28], [-23], [-2], [-6], [-17], [-3], [28], [19], [11], [3], [8], [18], [21], [-32], [-23], [10], [20], [-12], [10], [2], [4], [18], [-44], [20], [7], [3], [12], [-39], [7], [22], [4], [-19], [40], [-22], [0], [40], [-26], [30], [40], [-34], [10], [8], [-48], [-50], [41], [-28], [-9], [-39], [34], [-29], [-9], [-37], [-14], [10], [-20], [6], [-34], [29], [-18], [22], [14], [11], [42], [-42], [-20], [-10], [50], [-30], [7], [-4], [36], [51], [-8], [0], [32], [-12], [14], [8], [-8], [21], [-33], [54], [-8], [-45], [51], [-14], [-35], [0], [-12], [19], [0], [44], [36], [30], [54], [22], [-38], [24], [16], [6], [-11], [6], [16], [-4], [-52], [46], [49], [-13], [9], [12], [0], [-32], [28], [-42], [53], [-46], [-28], [-6], [-30], [-66], [20], [-1], [54], [66], [26], [37], [-2], [-4], [55], [12], [-32], [-62], [34], [30], [18], [-16], [-36], [12], [40], [-3], [24], [-46], [60], [-34], [-42], [-23], [50], [-34], [-60], [72], [34], [9], [-15], [8], [-63], [15], [-17], [13], [-10], [-16], [45], [6], [60], [-40], [-23], [-56], [46], [7], [-39], [36], [-8], [57], [-11], [-44], [-24], [1], [21], [46], [6], [54], [-18], [-41], [54], [-28], [-32], [2], [-32], [30], [-38], [-74], [62], [58], [37], [-2], [57], [28], [-14], [-51], [-42], [-50], [47], [38], [38], [66], [42], [-8], [-54], [42], [19], [43], [14], [-47], [56], [-47], [-78], [81], [6], [-6], [-26], [66], [36], [30], [18], [51], [46], [87], [-39], [33], [82], [34], [60], [35], [-75], [-74], [22], [-42], [39], [-4], [-12], [-62], [-11], [-2], [-19], [5], [-75], [-67], [77], [56], [-57], [14], [-64], [-84], [78], [-42], [20], [16], [-59], [56], [-4], [-20], [7], [-67], [47], [-27], [26], [-56], [25], [-28], [-24], [-44], [65], [62], [50], [50], [37], [-59], [10], [-57], [-26], [-21], [-24], [-32], [41], [-56], [-58], [41], [-16], [-36], [80], [89], [-19], [-4], [-56], [-3], [-56], [-30], [94], [-28], [25], [21], [64], [-88], [66], [88], [44], [-68], [-16], [-19], [-83], [-96], [38], [19], [61], [39], [-37], [-34], [-61], [82], [25], [28], [-79], [60], [-30], [-71], [58], [-84], [-64], [42], [53], [-96], [-11], [3], [66], [-49], [-14], [-41], [-21], [77], [-104], [54], [2], [62], [-54], [62], [-54], [32], [59], [-42], [-3], [4], [-24], [-65], [-80], [62], [-13], [-90], [94], [104], [-43], [-78], [14], [38], [-16], [-109], [-39], [-42], [-6], [74], [-104], [63], [-53], [-62], [-50], [-62], [101], [-45], [18], [-69], [-34], [-12], [-44], [-76], [40], [18], [-80], [-86], [76], [13], [28], [-32], [-5], [-69], [94], [-6], [11], [3], [65], [22], [-69], [64], [64], [92], [-92], [-40], [-74], [-69], [-10], [57], [52], [66], [47], [-45], [-82], [-16], [14], [-32], [-11], [39], [-26], [-12], [-58], [118], [-5], [-28], [-45], [-96], [-19], [-84], [-1], [-48], [75], [-22], [-80], [1], [-50], [-100], [-68], [-26], [-97], [63], [76], [30], [-24], [-52], [-46], [-48], [-5], [52], [84], [-54], [-35], [-113], [24], [90], [16], [-72], [-41], [36], [-103], [0], [-25], [90], [-60], [41], [30], [-8], [2], [-4], [70], [48], [-10], [-77], [-13], [-74], [3], [-67], [88], [56], [-105], [48], [28]];
    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_bq();".
// 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_bq(: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_bq();".
// 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_bq( : 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![4, 1]>,<17,R![1, 1]>,<19,R![-3, 1]>,<23,R![-1, 1]>,<29,R![-1, 1]>],Snew);
    return Vf;
end function;