
// Make newform 7938.2.a.bc in Magma, downloaded from the LMFDB on 01 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_7938_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_7938_2_a_bc();".
// 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_7938_2_a_bc();".
// 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_7938_a();"
function MakeCharacter_7938_a()
    N := 7938;
    order := 1;
    char_gens := [6077, 3727];
    v := [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_7938_a_Hecke(Kf)
    return MakeCharacter_7938_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], [3], [0], [-6], [1], [-3], [-2], [-6], [-9], [10], [-7], [-6], [-4], [-6], [-6], [6], [-11], [2], [12], [7], [2], [-6], [-3], [-14], [18], [16], [0], [5], [-15], [-4], [0], [-3], [-14], [3], [2], [-23], [-16], [-6], [-9], [6], [-2], [6], [11], [-9], [10], [2], [28], [24], [25], [21], [18], [19], [6], [-27], [6], [3], [-14], [14], [21], [16], [-21], [16], [30], [19], [-21], [-28], [14], [-12], [-26], [-18], [0], [10], [-22], [2], [0], [-30], [13], [21], [-5], [-12], [17], [-36], [-17], [28], [-30], [-30], [23], [-6], [-22], [30], [6], [-34], [-12], [14], [6], [-30], [6], [-44], [-43], [2], [-9], [-24], [-3], [-22], [-17], [-18], [-3], [-30], [7], [22], [14], [-15], [4], [-34], [9], [22], [-24], [18], [0], [13], [-1], [6], [36], [4], [27], [5], [24], [-8], [-26], [50], [24], [-4], [-22], [33], [-5], [-9], [-8], [-9], [-15], [-2], [-45], [-16], [0], [34], [-48], [10], [9], [4], [-6], [41], [18], [-34], [-42], [20], [-36], [2], [21], [7], [3], [36], [-3], [2], [-6], [18], [-36], [-10], [37], [35], [-6], [-12], [-23], [6], [35], [-8], [18], [20], [-45], [-44], [-59], [-28], [24], [41], [42], [-60], [-33], [5], [10], [-10], [-18], [7], [12], [-34], [15], [0], [-6], [59], [-58], [-27], [6], [39], [34], [-2], [-65], [54], [-6], [46], [18], [42], [-68], [2], [27], [-16], [36], [-36], [-50], [-4], [33], [24], [-57], [-55], [22], [57], [56], [0], [5], [42], [-36], [-44], [30], [-31], [-20], [-34], [-3], [-26], [0], [-5], [42], [-66], [-30], [6], [70], [40], [50], [66], [54], [76], [-24], [-4], [-6], [41], [-75], [12], [31], [39], [66], [-19], [-2], [-57], [-74], [26], [-66], [25], [34], [18], [-38], [-21], [45], [32], [51], [13], [74], [-74], [56], [-5], [40], [-42], [-7], [-37], [24], [6], [26], [-60], [49], [-26], [36], [59], [18], [-14], [33], [-54], [54], [21], [-54], [17], [3], [-2], [3], [60], [-20], [-65], [-18], [-16], [24], [-58], [-58], [-60], [-14], [-30], [-7], [-24], [-6], [-15], [2], [-60], [46], [-30], [12], [-77], [-54], [10], [14], [39], [62], [-30], [7], [-16], [-8], [-18], [90], [14], [15], [-26], [0], [56], [30], [-67], [-27], [-2], [-2], [-7], [-54], [63], [-64], [51], [-42], [-11], [-28], [0], [-57], [40], [-37], [51], [28], [-7], [-15], [0], [-84], [-54], [-96], [77], [-15], [72], [48], [-14], [-1], [18], [-22], [-82], [12], [-56], [84], [-69], [4], [-31], [6], [-12], [55], [30], [-17], [-21], [-39], [20], [93], [70], [12], [-82], [-23], [74], [-6], [-13], [15], [66], [64], [-42], [-1], [-38], [45], [-64], [63], [85], [-66], [-76], [69], [-93], [46], [41], [45], [-2], [-24], [-53], [-57], [30], [74], [-49], [-21], [-18], [-98], [-3], [-78], [-18], [-98], [36], [90], [43], [63], [-78], [33], [-56], [-30], [67], [12], [-94], [18]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_7938_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_7938_2_a_bc();".
// 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_7938_2_a_bc(:prec:=1)
    chi := MakeCharacter_7938_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(3023) - 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_7938_2_a_bc();".
// 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_7938_2_a_bc( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_7938_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![-3, 1]>,<11,R![6, 1]>,<13,R![-1, 1]>,<17,R![3, 1]>,<23,R![6, 1]>],Snew);
    return Vf;
end function;