
// Make newform 9702.2.a.bp 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_bp();".
// 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_bp();".
// 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], [-6], [4], [-6], [4], [-6], [2], [10], [-4], [8], [-6], [10], [0], [2], [-4], [-16], [-12], [-16], [-2], [-6], [6], [-14], [14], [-8], [-14], [2], [-16], [-22], [-22], [14], [6], [0], [-12], [-4], [12], [6], [-4], [-8], [12], [-6], [-6], [14], [8], [26], [18], [-8], [-22], [-8], [20], [0], [10], [0], [-24], [-12], [-6], [-14], [-18], [14], [30], [-18], [-6], [18], [-4], [22], [-16], [-26], [-6], [-8], [2], [-10], [-20], [-34], [30], [4], [-30], [-16], [4], [-10], [-40], [-26], [20], [4], [34], [10], [2], [16], [28], [-24], [20], [-12], [36], [12], [-16], [2], [6], [30], [12], [2], [-22], [42], [-28], [34], [24], [20], [0], [32], [-24], [-26], [30], [-4], [4], [-26], [24], [-34], [6], [-16], [32], [-26], [30], [-36], [0], [46], [26], [-50], [-22], [-6], [-52], [-16], [28], [-14], [8], [-16], [20], [-22], [-20], [-18], [-18], [38], [28], [12], [-24], [-42], [42], [24], [0], [-12], [18], [-50], [-28], [24], [20], [12], [-8], [10], [12], [-18], [-12], [-18], [24], [20], [46], [10], [16], [46], [26], [-48], [32], [20], [0], [-26], [30], [14], [-4], [14], [28], [14], [32], [10], [-46], [-24], [-24], [8], [46], [4], [14], [60], [10], [40], [12], [28], [-28], [-54], [38], [18], [60], [-44], [-66], [28], [4], [-44], [12], [48], [14], [36], [-34], [-44], [-50], [-12], [56], [48], [68], [-70], [-32], [48], [8], [46], [-34], [28], [-6], [-72], [-30], [74], [54], [-40], [-12], [-64], [-6], [-38], [-16], [-74], [12], [12], [6], [6], [-44], [-30], [-28], [34], [-2], [70], [-72], [44], [30], [-16], [-4], [-48], [62], [36], [-72], [-10], [54], [72], [58], [-18], [48], [76], [-16], [-12], [34], [-74], [-18], [-16], [34], [-50], [4], [10], [60], [12], [-14], [-8], [60], [-70], [12], [30], [-46], [-50], [-40], [40], [-8], [-2], [2], [-36], [-26], [46], [-82], [40], [18], [4], [66], [-26], [34], [18], [-74], [-32], [-70], [-50], [-80], [-18], [24], [-68], [28], [-2], [-60], [16], [-80], [-54], [-16], [46], [-62], [-44], [-48], [60], [0], [48], [-62], [-30], [66], [58], [62], [-68], [46], [-54], [-20], [68], [16], [22], [46], [-58], [-4], [-6], [52], [-76], [10], [8], [14], [-56], [6], [62], [-34], [76], [-34], [-68], [48], [20], [64], [-52], [-50], [86], [74], [8], [50], [-32], [-38], [90], [-18], [88], [34], [-56], [-40], [4], [4], [-14], [30], [48], [34], [36], [-52], [48], [-54], [56], [-50], [76], [56], [96], [44], [20], [-84], [6], [32], [-90], [-58], [30], [-20], [8], [-56], [58], [-38], [84], [92], [-8], [24], [-66], [-56], [-58], [-20], [-90], [50], [-18], [88], [38], [-74], [-52], [38], [86], [44], [84], [92], [18], [44], [-28], [-50], [28], [-72], [-30], [-66], [-24], [38], [-102], [48], [-66], [-30], [4], [-60], [10], [-28], [-88], [50], [56], [-4], [-66], [32], [-62], [-2], [-86], [-20], [-68], [32], [-42], [14], [-40], [60], [22], [-70], [-76], [78], [66], [32], [58], [0], [12], [-110], [2], [-2], [-66], [-78], [-6], [28], [28], [64], [62], [-66], [-66], [-32], [-16], [-78], [-32], [96], [-100], [-30], [10], [-84], [-58], [-34], [-14], [-24], [42], [82], [0], [-54], [-58], [50], [-32], [106], [80], [36], [96], [-2], [36], [-42], [-78], [108], [10], [52], [-6], [28], [-28], [94], [-56], [30], [8], [-2], [-34], [-116], [-94], [86], [2], [6], [-16], [-52], [-22], [88], [94], [76], [-30], [-36], [62], [34], [4], [-10], [-48], [-44], [-18], [86], [18], [-60], [-104], [88], [-26], [-80], [92], [-92], [-36], [112], [-50], [-82], [-80], [28], [18], [14], [2], [62], [-20], [-52], [84], [88], [-42], [-42], [-6], [-38], [6], [-60], [54], [92]];
    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_bp();".
// 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_bp(: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_bp();".
// 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_bp( : 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![6, 1]>,<17,R![-4, 1]>,<19,R![6, 1]>,<23,R![-4, 1]>,<29,R![6, 1]>],Snew);
    return Vf;
end function;