
// Make newform 7920.2.a.u in Magma, downloaded from the LMFDB on 07 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_7920_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_7920_2_a_u();".
// 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_7920_2_a_u();".
// 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_7920_a();"
function MakeCharacter_7920_a()
    N := 7920;
    order := 1;
    char_gens := [991, 5941, 3521, 6337, 6481];
    v := [1, 1, 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_7920_a_Hecke(Kf)
    return MakeCharacter_7920_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 := [[0], [0], [1], [-4], [-1], [2], [-2], [4], [4], [10], [-8], [-2], [-10], [-12], [-12], [6], [12], [10], [-4], [12], [-14], [-12], [12], [6], [2], [-14], [8], [12], [2], [6], [-20], [-12], [6], [12], [-6], [-4], [-2], [-12], [-16], [2], [-12], [-18], [-20], [-14], [10], [-24], [12], [-8], [-12], [14], [-10], [-8], [10], [12], [-10], [-24], [-18], [-4], [2], [6], [20], [-14], [20], [12], [-22], [-10], [-12], [-22], [-12], [2], [-10], [-32], [-24], [-38], [4], [-4], [6], [-18], [22], [34], [-12], [6], [0], [2], [-20], [12], [6], [-22], [-30], [16], [12], [-16], [-8], [4], [28], [32], [30], [-34], [4], [2], [20], [-22], [4], [6], [-4], [2], [12], [30], [12], [26], [12], [34], [-18], [-4], [32], [-10], [44], [-12], [22], [-36], [14], [-30], [-6], [-4], [-12], [-30], [-18], [36], [-40], [-46], [-52], [0], [40], [-34], [38], [10], [-10], [-4], [-2], [6], [52], [18], [0], [20], [46], [52], [2], [6], [20], [-12], [18], [-2], [-52], [-16], [52], [36], [-28], [6], [-46], [-22], [-20], [6], [-12], [12], [-2], [52], [40], [-38], [10], [-2], [-36], [6], [-16], [-46], [-8], [54], [12], [-18], [44], [-14], [56], [-12], [-34], [38], [12], [30], [10], [44], [18], [-64], [-14], [20], [-36], [-66], [-28], [-50], [-46], [-18], [46], [8], [-46], [-4], [38], [-46], [60], [-26], [64], [-20], [-10], [12], [-62], [30], [16], [52], [0], [26], [-4], [62], [36], [6], [-14], [-68], [-10], [-40], [-12], [-22], [-66], [60], [4], [36], [-58], [-68], [76], [-10], [44], [-32], [-14], [66], [12], [-44], [60], [-12], [32], [-10], [-34], [0], [-48], [60], [44], [-24], [58], [-18], [12], [58], [-54], [-60], [-26], [4], [-50], [42], [-44], [28], [10], [34], [30], [52], [54], [-10], [12], [-54], [-26], [28], [-22], [-60], [-6], [-24], [20], [58], [42], [12], [-16], [-40], [0], [10], [44], [4], [18], [-6], [-56], [-66], [78], [-28], [54], [-12], [50], [-30], [-80], [-42], [-20], [44], [74], [42], [28], [28], [-68], [50], [-60], [-2], [84], [-70], [-56], [-18], [30], [-76], [-40], [10], [60], [0], [2], [30], [-44], [-38], [-62], [0], [38], [-46], [52], [-36], [40], [-38], [-38], [14], [-84], [-44], [-44], [68], [6], [14], [74], [-52], [54], [-42], [74], [16], [62], [84], [46], [28], [-8], [70], [76], [58], [-82], [-4], [-46], [-42], [-60], [20], [46], [-84], [2], [86], [44], [4], [-52], [-38], [-22], [-92], [-78], [52], [36], [48], [-30], [-52], [70], [28], [-24], [26], [30], [-22], [94], [6], [20], [-2], [28], [-68], [-8], [-18], [-44], [-12], [-46], [14], [-84], [-28], [-60], [10], [-4], [-2], [4], [-14], [10], [-66], [52], [-90], [-86], [-44], [86], [46], [60], [-20], [-86], [-14], [84], [52], [-62], [6], [-8], [40], [-18], [-80], [-10], [34], [-52], [-60], [-86], [-18], [-36], [54], [4], [24], [-22], [20], [-68], [-44], [-82], [-42], [26], [-2], [44], [-4], [-108], [-18], [-22], [56], [2], [-34], [4], [-56], [2], [-78], [-36], [-12], [52], [54], [2], [70], [98], [52], [-22], [-2], [28], [-40], [36], [102], [68], [-70], [12], [20], [14], [20], [84], [-36], [60], [-62], [20], [58], [-74], [-48], [24], [102], [-22], [70]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_7920_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_7920_2_a_u();".
// 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_7920_2_a_u(:prec:=1)
    chi := MakeCharacter_7920_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(3449) - 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_7920_2_a_u();".
// 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_7920_2_a_u( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_7920_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![4, 1]>,<13,R![-2, 1]>,<17,R![2, 1]>,<19,R![-4, 1]>,<23,R![-4, 1]>,<29,R![-10, 1]>],Snew);
    return Vf;
end function;