
// Make newform 7220.2.a.e in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_7220_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_7220_2_a_e();".
// 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_7220_2_a_e();".
// 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_7220_a();"
function MakeCharacter_7220_a()
    N := 7220;
    order := 1;
    char_gens := [3611, 5777, 6861];
    v := [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_7220_a_Hecke(Kf)
    return MakeCharacter_7220_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], [2], [-1], [-4], [-3], [6], [2], [0], [4], [1], [-5], [-4], [2], [0], [6], [-6], [-1], [-7], [-14], [15], [12], [-1], [16], [17], [12], [11], [0], [-12], [7], [14], [18], [-20], [12], [0], [-17], [3], [18], [18], [8], [0], [-15], [2], [-13], [16], [2], [23], [-1], [8], [-4], [21], [-10], [15], [5], [31], [18], [10], [-1], [17], [16], [-26], [-26], [16], [-14], [0], [34], [-12], [-20], [4], [12], [-2], [-36], [0], [-28], [-12], [-19], [-18], [-21], [-8], [15], [-1], [-5], [25], [27], [0], [1], [-12], [-1], [-2], [35], [-8], [-20], [-3], [34], [-25], [36], [22], [30], [-45], [-10], [-25], [-8], [-40], [10], [-17], [-17], [-20], [-12], [-4], [-24], [17], [-14], [-22], [-8], [-20], [-33], [9], [-2], [-30], [12], [-36], [41], [-28], [-32], [6], [37], [-42], [5], [29], [26], [-12], [-19], [52], [19], [-14], [54], [13], [-54], [38], [-22], [45], [-13], [25], [26], [-18], [-30], [0], [38], [0], [-1], [-18], [40], [-7], [8], [28], [32], [7], [8], [-21], [2], [35], [-10], [-28], [-12], [60], [12], [-36], [32], [-10], [-15], [22], [-51], [42], [-33], [-2], [16], [42], [41], [-6], [24], [2], [-26], [61], [58], [-8], [-18], [49], [28], [-54], [-18], [9], [-10], [-48], [-1], [-35], [12], [-10], [-14], [28], [-52], [42], [34], [-23], [32], [46], [36], [6], [-27], [-26], [13], [-36], [-12], [-3], [4], [-10], [-28], [42], [-2], [38], [-46], [-24], [65], [16], [21], [40], [-38], [50], [56], [-20], [52], [-7], [10], [1], [73], [-30], [-26], [-42], [67], [-40], [9], [-27], [-6], [-20], [-8], [2], [-30], [-48], [-6], [64], [-31], [32], [-62], [15], [44], [-10], [-4], [-49], [19], [52], [-6], [58], [-26], [-72], [45], [-26], [50], [-13], [-35], [-65], [-16], [-24], [46], [34], [36], [-71], [-42], [64], [54], [30], [66], [-21], [28], [-3], [18], [-14], [62], [48], [32], [34], [29], [-53], [-59], [-14], [0], [25], [46], [83], [-24], [40], [16], [-78], [-68], [-22], [-81], [-46], [-51], [-38], [-4], [85], [-48], [-54], [10], [-15], [-57], [14], [-16], [49], [-63], [63], [-44], [30], [-20], [-28], [-3], [-46], [-16], [-15], [-52], [12], [8], [-18], [33], [-54], [35], [-54], [20], [22], [26], [72], [-15], [34], [-30], [-36], [35], [-24], [-22], [63], [-35], [-22], [63], [-22], [37], [-2], [-35], [94], [-23], [14], [-36], [37], [-16], [44], [-90], [15], [-32], [-36], [-40], [-60], [2], [-66], [9], [32], [65], [14], [43], [-16], [-92], [3], [-11], [-44], [51], [14], [87], [-98], [-10], [84], [-21], [72], [-45], [44], [46], [-36], [70], [-94], [39], [64], [7], [-36], [-3], [41], [-20], [45], [82], [18], [-70], [-12], [-42], [8], [32], [71], [-52], [36], [-22], [-44], [-46], [-17], [6], [91], [5], [12], [2], [36], [-41], [90], [-80], [-73], [50], [-66], [-66], [7], [-13], [-21]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_7220_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_7220_2_a_e();".
// 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_7220_2_a_e(:prec:=1)
    chi := MakeCharacter_7220_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_7220_2_a_e();".
// 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_7220_2_a_e( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_7220_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![-2, 1]>,<7,R![4, 1]>,<13,R![-6, 1]>],Snew);
    return Vf;
end function;