
// Make newform 7942.2.a.q in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_7942_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_7942_2_a_q();".
// 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_7942_2_a_q();".
// 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_7942_a();"
function MakeCharacter_7942_a()
    N := 7942;
    order := 1;
    char_gens := [5777, 6139];
    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_7942_a_Hecke(Kf)
    return MakeCharacter_7942_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], [1], [1], [1], [2], [0], [0], [-6], [-8], [-10], [3], [-2], [-8], [0], [-9], [-14], [0], [12], [6], [0], [-11], [7], [14], [5], [10], [6], [17], [-10], [2], [20], [-20], [-23], [-5], [-4], [-9], [21], [-4], [9], [22], [-16], [-5], [-18], [4], [2], [10], [13], [-16], [25], [-1], [-18], [-5], [10], [-12], [-21], [-3], [-21], [15], [-2], [20], [-7], [0], [-23], [-10], [-21], [22], [-4], [-8], [-3], [28], [11], [3], [10], [-4], [2], [8], [-23], [-27], [10], [10], [0], [-15], [5], [30], [29], [22], [-5], [38], [26], [16], [6], [-32], [-2], [-15], [-32], [16], [-21], [-31], [-7], [8], [-1], [-30], [-17], [10], [-37], [25], [26], [14], [-16], [42], [-8], [4], [-15], [46], [-22], [39], [-16], [18], [-18], [-15], [-11], [44], [-36], [26], [-50], [-36], [43], [8], [26], [-36], [4], [41], [8], [-50], [-36], [2], [38], [11], [21], [-44], [23], [-20], [-16], [-8], [-10], [-16], [-30], [0], [-14], [-32], [-18], [-14], [20], [-35], [-38], [-12], [-39], [-6], [16], [24], [10], [34], [-1], [-20], [33], [14], [4], [52], [6], [7], [19], [-23], [-60], [-44], [-46], [37], [-57], [18], [-35], [50], [8], [-21], [-50], [36], [-16], [39], [-44], [0], [24], [-24], [49], [27], [-18], [57], [-51], [3], [-30], [66], [-14], [48], [12], [-19], [-13], [-56], [12], [-18], [28], [-27], [-22], [-10], [38], [-54], [44], [-28], [11], [7], [53], [46], [24], [-22], [36], [-49], [66], [-46], [-9], [20], [6], [-38], [57], [-39], [5], [-4], [-40], [-30], [70], [-56], [62], [-18], [-50], [-26], [-24], [-15], [66], [29], [-44], [13], [72], [-60], [11], [27], [44], [-48], [-30], [-61], [-56], [36], [26], [-52], [5], [0], [-79], [-36], [14], [-16], [3], [-22], [-5], [-42], [20], [26], [62], [-58], [49], [-39], [44], [-6], [-18], [74], [50], [35], [39], [-16], [65], [-56], [29], [-6], [26], [62], [28], [-42], [82], [18], [-20], [-56], [76], [4], [-14], [54], [-55], [-63], [-20], [-18], [-51], [2], [80], [6], [12], [9], [36], [10], [-64], [-31], [-6], [40], [48], [-52], [-54], [-45], [2], [86], [7], [85], [-26], [-92], [-2], [43], [22], [-36], [80], [48], [-46], [81], [-16], [4], [75], [-54], [-75], [48], [35], [-57], [26], [27], [16], [58], [-69], [20], [10], [22], [-65], [-57], [28], [-17], [27], [-45], [-4], [-70], [-48], [7], [92], [-50], [-64], [42], [-30], [-39], [56], [45], [-14], [-59], [34], [82], [-50], [-39], [20], [49], [38], [56], [37], [-88], [-28], [26], [25], [-61], [-85], [-94], [-28], [-42], [40], [-42], [15], [-28], [-78], [-13], [54], [42], [8], [54], [-56], [-42], [76], [-24], [56], [10], [-39], [-69], [-74], [-55], [33], [30], [66], [-50], [64], [36], [-40], [-95], [30], [13], [-69], [32], [-89], [-49], [39], [56], [-82], [-75], [14], [67], [24], [6], [-66], [21]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_7942_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_7942_2_a_q();".
// 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_7942_2_a_q(:prec:=1)
    chi := MakeCharacter_7942_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(2999) - 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_7942_2_a_q();".
// 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_7942_2_a_q( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_7942_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![0, 1]>,<5,R![-1, 1]>,<13,R![-2, 1]>],Snew);
    return Vf;
end function;