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