
// Make newform 5520.2.a.br in Magma, downloaded from the LMFDB on 25 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_br();".
// 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_br();".
// 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
        poly := [-4, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
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, 0], [1, 0], [1, 0], [0, -1], [0, -2], [2, 0], [2, -1], [2, 0], [-1, 0], [2, -3], [0, 3], [4, 1], [2, 3], [0, 0], [-4, 0], [6, -1], [-2, 1], [6, -2], [-4, 3], [-2, -1], [2, 4], [6, 2], [-8, 3], [-2, 2], [8, -2], [6, 5], [0, 0], [-8, 3], [-6, -4], [6, -1], [0, 4], [-2, 6], [10, -8], [8, -7], [2, 6], [0, 4], [0, 7], [12, -6], [-16, 0], [8, -4], [2, -2], [2, 2], [0, 8], [-10, 2], [8, 6], [14, -4], [4, 7], [16, 2], [-4, 0], [-10, -6], [-8, 0], [6, 5], [6, -8], [4, -6], [-4, 8], [12, -7], [-18, -1], [12, -7], [-2, 4], [-6, -4], [16, 1], [-2, -7], [-4, -2], [-2, -6], [8, -1], [-4, 0], [0, -1], [-16, -2], [-20, 0], [6, -3], [4, 12], [4, -4], [4, 1], [4, 6], [-22, 6], [12, 1], [-6, 0], [22, -2], [-14, 0], [-6, -1], [-12, 10], [22, 0], [8, -4], [-4, -7], [0, 0], [-8, 8], [22, 1], [4, -9], [2, -12], [24, 6], [-24, 3], [12, 8], [16, 4], [-18, 15], [24, -11], [12, -3], [-2, 12], [-22, -8], [-4, -16], [34, 4], [-4, 4], [-2, -9], [-8, -3], [-26, 8], [2, -6], [6, 2], [-4, -10], [-12, -4], [2, -2], [-6, 21], [16, 12], [-4, 14], [6, -15], [-30, 6], [-18, -6], [-18, 12], [20, 7], [4, 14], [-16, -10], [-8, -6], [22, -16], [42, 2], [-38, 1], [20, 4], [-32, 12], [-14, -10], [-2, 6], [2, -7], [-16, -11], [16, -11], [-8, 19], [-16, 0], [22, 4], [4, -1], [22, -13], [-2, 6], [22, -4], [-12, 15], [-2, 13], [18, 1], [-24, 3], [-42, 4], [16, 12], [-24, 1], [-2, -17], [-32, -2], [30, -12], [24, 6], [-28, -5], [16, 2], [-38, 12], [-6, 10], [4, -20], [16, 10], [24, -17], [-36, -4], [26, 2], [-2, -7], [-4, 18], [-22, 10], [24, 8], [-10, 16], [0, -8], [-4, -4], [-2, 5], [4, 19], [0, 15], [-38, -2], [6, 2], [-20, 12], [8, 4], [14, -17], [28, -6], [24, 13], [16, -16], [30, -18], [0, 12], [-42, -3], [20, -3], [18, 8], [-8, -6], [-28, 20], [14, -8], [-32, -2], [12, -7], [-58, 4], [-14, -16], [-8, -7], [-38, 11], [-10, 11], [14, -24], [-4, -6], [-38, 4], [-30, 11], [-4, 12], [-6, 1], [-58, 0], [32, 2], [14, -12], [-16, 18], [-42, 10], [-16, -20], [-50, 0], [22, -12], [4, -20], [-32, -2], [-6, 6], [16, 10], [-22, 11], [12, -24], [-46, 4], [-42, 8], [36, -20], [4, -24], [-26, 9], [46, -10], [48, 2], [-10, -9], [4, 17], [8, -2], [-14, 11], [-14, 12], [26, -11], [12, -23], [32, -2], [-6, 13], [18, 0], [18, 9], [-32, 12], [-30, -9], [6, 20], [-26, 0], [34, -4], [-6, 13], [-24, -1], [-8, 12], [10, -26], [-22, -4], [-46, -7], [6, -17], [-16, 3], [-44, -1], [8, 10], [-18, -8], [24, 4], [22, -18], [24, 0], [-40, 10], [14, 16], [20, -29], [-32, -14], [30, 16], [-4, -7], [-6, 8], [32, -22], [10, -15], [-38, 26], [-12, 28], [-12, 6], [-34, 0], [28, -7], [-32, 1], [-38, 25], [40, 3], [-4, 16], [-62, 2], [22, 10], [-22, 4], [-36, -11], [12, 12], [-14, 23], [40, -25], [-32, -11], [-46, 24], [66, 2], [0, 10], [40, 0], [6, -9], [6, -14], [-32, -4], [44, -18], [-6, 26], [-48, 0], [34, -6], [28, -2], [-30, 3], [48, 13], [10, 20], [16, 0], [58, 8], [-54, -4], [-16, -11], [44, -20], [-16, 24], [10, 2], [22, -2], [-34, 4], [-20, 30], [10, -13], [52, 10], [-32, 11], [30, 15], [-22, -10], [-52, -8], [-46, -14], [14, -10], [36, -26], [-42, -4], [4, -20], [62, -10], [-40, 0], [42, 0], [26, 2], [52, 0], [-12, 13], [14, 6], [-2, -7], [-2, -3], [48, 5], [10, -20], [42, 14], [32, 15], [30, 4], [32, 2], [34, 11], [-34, -2], [-26, -10], [-28, 2], [-16, 24], [-10, 33], [34, -7], [32, -14], [-32, -19], [-32, -14], [42, 2], [48, -16], [2, 20], [42, 4], [-26, 4], [-16, -17], [-2, -8], [6, 16], [18, -24], [22, 2], [-46, 17], [-30, -5], [18, 19], [44, 10], [32, 10], [14, -43], [36, -35], [-70, 6], [66, -11], [40, -15], [-10, 14], [-28, -6], [0, -34], [36, -10], [56, -6], [-28, 30], [-4, -23], [14, -21], [28, -6], [16, -28], [-28, 26], [-2, -24], [56, -12], [-20, 39], [-26, 6], [-38, -17], [0, 3], [36, -18], [-18, -22], [14, 10], [34, -28], [30, -27], [-4, -22], [-12, 29], [42, -10], [94, 0], [18, 4], [-22, -8], [-40, -16], [40, -10], [34, 2], [68, 6], [84, 3], [-34, 6], [-16, -1], [-28, 35], [34, 16], [20, 30], [-46, 23], [20, -28], [-8, -14], [0, 9], [2, -8], [-30, 6], [22, -10], [-38, 23], [2, 13], [0, 30], [24, -27], [-14, 5], [30, 9], [-24, -15], [44, -25], [58, -16], [36, -40], [70, 6], [50, 10], [-52, 14], [68, 0], [78, -6], [-12, 39], [82, 0], [34, -2], [40, -32], [6, 27], [-8, 12], [-10, -24], [92, -2], [-20, -18], [38, 14], [-74, -2], [-4, -12], [20, 0], [-10, -15], [52, 8], [50, -1]];
    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_br();".
// 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_br(:prec:=2)
    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_br();".
// 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_br( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_5520_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![-4, 1, 1]>,<11,R![-16, 2, 1]>,<13,R![-2, 1]>,<17,R![-2, -3, 1]>,<19,R![-2, 1]>],Snew);
    return Vf;
end function;