
// Make newform 7942.2.a.k in Magma, downloaded from the LMFDB on 19 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_k();".
// 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_k();".
// 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], [2], [2], [2], [-1], [-2], [-6], [0], [0], [-2], [-4], [-6], [-2], [0], [-8], [-2], [2], [0], [-10], [0], [14], [-8], [12], [-16], [8], [20], [4], [-12], [2], [-12], [8], [4], [-6], [-4], [12], [8], [-10], [4], [-8], [18], [-18], [2], [4], [14], [4], [-12], [12], [-8], [-28], [18], [-6], [-14], [26], [20], [8], [-18], [26], [14], [-20], [-22], [16], [-6], [8], [24], [26], [18], [-14], [-18], [-4], [-12], [14], [14], [28], [-34], [2], [-28], [-14], [22], [-16], [-6], [36], [22], [-24], [40], [40], [-12], [-36], [6], [4], [-28], [28], [30], [-32], [-32], [28], [6], [2], [32], [-24], [-8], [16], [12], [-16], [-26], [8], [-30], [12], [-22], [-44], [26], [-8], [8], [-10], [-4], [20], [12], [-28], [12], [10], [-40], [50], [-22], [42], [6], [-28], [0], [-38], [-48], [0], [-32], [28], [24], [8], [2], [-38], [-50], [-30], [44], [-18], [-26], [-8], [12], [-12], [-20], [-6], [-12], [16], [26], [-36], [32], [18], [-46], [-4], [-16], [10], [60], [10], [-34], [38], [2], [-36], [-10], [6], [-42], [0], [24], [-16], [-44], [-14], [-38], [-32], [-26], [-14], [-14], [-20], [6], [20], [-30], [8], [28], [-60], [0], [-6], [-42], [-36], [18], [-30], [-62], [-26], [-46], [-4], [-48], [42], [-30], [-40], [-24], [38], [46], [26], [54], [-14], [48], [-18], [-34], [-12], [-42], [48], [64], [30], [2], [-2], [-58], [24], [-30], [56], [-28], [-26], [-42], [28], [22], [62], [-40], [-16], [20], [-68], [-52], [-20], [-48], [-56], [-28], [-62], [-8], [16], [66], [28], [2], [22], [-4], [36], [-60], [14], [-76], [76], [70], [74], [22], [-16], [30], [40], [-66], [-28], [10], [-20], [-40], [-56], [44], [70], [-28], [-34], [58], [-64], [76], [40], [-66], [-18], [50], [-30], [-22], [52], [-48], [-2], [30], [74], [-26], [2], [68], [60], [66], [-38], [20], [48], [4], [-14], [34], [-68], [-68], [42], [22], [16], [42], [-2], [-12], [-46], [44], [42], [-36], [52], [46], [4], [16], [58], [6], [-34], [74], [20], [28], [-50], [50], [-52], [-12], [54], [66], [54], [34], [30], [-22], [-60], [-40], [50], [-90], [52], [-2], [-30], [-4], [-14], [20], [30], [-86], [-40], [16], [28], [90], [-10], [-56], [0], [-52], [74], [30], [-22], [74], [10], [-26], [54], [44], [-90], [80], [22], [-34], [0], [-54], [-20], [92], [-10], [0], [-58], [66], [-20], [36], [18], [-84], [-48], [22], [52], [-20], [12], [4], [-32], [16], [38], [68], [70], [-10], [-42], [-50], [38], [34], [34], [98], [78], [18], [-30], [10], [-62], [-30], [0], [-4], [24], [-10], [-76], [96], [20], [-86], [44], [36], [-16], [-50], [24], [-76], [2], [-80], [-30], [42], [72], [-76], [-42], [6], [-90], [2], [86], [68], [46], [4], [-30], [88], [-58], [6], [-40], [-80], [-82], [-56], [-94], [86], [40], [-60], [-52], [-38], [-10], [90], [-28], [26]];
    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_k();".
// 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_k(: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_k();".
// 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_k( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_7942_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![-2, 1]>,<5,R![-2, 1]>,<13,R![2, 1]>],Snew);
    return Vf;
end function;