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