
// Make newform 5520.2.a.bf in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_5520_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_5520_2_a_bf();".
// 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_5520_2_a_bf();".
// 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_5520_a();"
function MakeCharacter_5520_a()
    N := 5520;
    order := 1;
    char_gens := [4831, 1381, 1841, 4417, 1201];
    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_5520_a_Hecke(Kf)
    return MakeCharacter_5520_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], [1], [1], [0], [4], [-2], [2], [-4], [1], [6], [8], [-2], [-6], [4], [0], [6], [12], [-10], [-4], [-8], [10], [8], [12], [10], [2], [-2], [0], [20], [-10], [2], [-8], [-12], [-6], [12], [-10], [0], [-10], [20], [8], [-10], [-12], [-2], [16], [2], [-2], [16], [20], [8], [-20], [-18], [26], [0], [2], [4], [-14], [-8], [-10], [24], [22], [10], [4], [22], [20], [-24], [26], [-10], [-20], [2], [-20], [14], [18], [-24], [-24], [14], [4], [-16], [-10], [-18], [-30], [-38], [12], [-2], [-16], [18], [0], [-20], [18], [-22], [38], [-24], [12], [-16], [-16], [44], [36], [8], [6], [-6], [20], [-2], [4], [30], [-36], [-22], [20], [34], [-36], [34], [-40], [26], [8], [-18], [-38], [-12], [-32], [18], [28], [8], [-26], [12], [14], [-30], [6], [-20], [4], [-34], [30], [16], [-16], [-26], [20], [24], [-40], [14], [10], [50], [6], [-20], [-18], [-38], [-4], [-18], [-48], [-28], [-34], [24], [22], [-38], [-4], [-48], [-2], [2], [20], [24], [36], [-32], [-48], [-46], [-38], [-18], [-12], [10], [-48], [-28], [34], [-8], [8], [-10], [-62], [-18], [-12], [-50], [-56], [58], [-56], [42], [-20], [-18], [32], [-10], [56], [-20], [38], [-6], [-48], [6], [-18], [12], [-22], [0], [-30], [-4], [-4], [6], [-20], [-38], [-14], [38], [-46], [24], [30], [-40], [22], [-30], [-28], [-42], [-24], [36], [10], [-20], [18], [-2], [48], [20], [24], [26], [-24], [-30], [72], [54], [-10], [-16], [-46], [-24], [68], [-42], [-54], [-48], [32], [-52], [-18], [-20], [-8], [-54], [68], [0], [-30], [70], [-4], [24], [-4], [-20], [0], [-50], [-30], [24], [-24], [60], [-12], [-64], [-42], [-78], [40], [-22], [-42], [20], [-2], [52], [-34], [58], [24], [12], [70], [38], [-62], [12], [30], [10], [52], [-18], [62], [-4], [10], [40], [-14], [16], [-4], [30], [-22], [60], [0], [-64], [72], [-2], [-68], [0], [-46], [6], [-16], [-30], [14], [28], [-22], [-12], [30], [62], [-24], [-66], [44], [-28], [10], [-2], [-56], [36], [68], [-30], [-68], [54], [8], [6], [32], [38], [-14], [-28], [-24], [-22], [-12], [48], [-46], [50], [28], [-38], [-42], [-40], [-6], [2], [-4], [28], [80], [86], [-82], [-74], [8], [-44], [20], [12], [38], [50], [42], [8], [-10], [-54], [-34], [-48], [30], [-44], [-74], [-52], [80], [70], [68], [74], [22], [24], [46], [42], [-32], [4], [18], [24], [78], [-54], [80], [-44], [-76], [10], [-42], [-32], [74], [-12], [44], [-16], [86], [0], [-82], [84], [-16], [-14], [66], [58], [-18], [-6], [-32], [-14], [12], [24], [8], [22], [52], [16], [66], [-18], [60], [-44], [-56], [90], [-40], [90], [-60], [30], [-98], [2], [88], [42], [-34], [16], [38], [-30], [-20], [84], [82], [-18], [-76], [-52], [58], [54], [-96], [64], [-30], [-24], [46], [-34], [32], [28], [74], [-42], [-68], [-6], [-52], [-88]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_5520_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_5520_2_a_bf();".
// 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_5520_2_a_bf(:prec:=1)
    chi := MakeCharacter_5520_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_5520_2_a_bf();".
// 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_5520_2_a_bf( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_5520_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![0, 1]>,<11,R![-4, 1]>,<13,R![2, 1]>,<17,R![-2, 1]>,<19,R![4, 1]>],Snew);
    return Vf;
end function;