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