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