
// Make newform 5184.2.a.i in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_5184_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_5184_2_a_i();".
// 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_5184_2_a_i();".
// 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_5184_a();"
function MakeCharacter_5184_a()
    N := 5184;
    order := 1;
    char_gens := [2431, 325, 1217];
    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_5184_a_Hecke(Kf)
    return MakeCharacter_5184_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], [-3], [5], [5], [2], [4], [1], [-9], [-1], [6], [-3], [-1], [3], [2], [11], [-7], [1], [-4], [-2], [1], [1], [18], [-13], [3], [-5], [12], [-10], [9], [16], [-5], [-3], [-3], [19], [17], [-7], [12], [21], [3], [12], [6], [27], [-13], [6], [-20], [-7], [-11], [21], [17], [-26], [17], [15], [-20], [-7], [7], [-14], [8], [29], [-19], [13], [-9], [12], [9], [-9], [15], [19], [-9], [-9], [21], [5], [0], [-23], [-35], [16], [-27], [35], [22], [21], [-33], [15], [1], [8], [-26], [33], [-23], [10], [11], [-25], [23], [12], [-1], [-32], [27], [-27], [-12], [3], [-10], [-40], [-30], [19], [-22], [-1], [25], [-15], [14], [17], [34], [21], [19], [-13], [-2], [-3], [11], [16], [1], [-15], [32], [-33], [21], [25], [19], [35], [-4], [-41], [18], [-11], [-32], [-39], [13], [16], [-11], [27], [10], [5], [-1], [42], [53], [-13], [50], [44], [-33], [-49], [-44], [38], [-21], [1], [-27], [9], [-56], [-3], [26], [4], [-55], [-49], [-41], [16], [29], [-42], [-33], [-27], [-30], [-25], [0], [-39], [15], [40], [-19], [14], [-37], [-15], [45], [-7], [-53], [35], [-7], [-17], [18], [-8], [37], [53], [-23], [-15], [-6], [45], [39], [2], [39], [7], [48], [22], [21], [-52], [-37], [-36], [-55], [19], [5], [41], [-24], [-41], [-19], [-23], [7], [36], [66], [-40], [-45], [41], [-67], [30], [27], [37], [-35], [-43], [47], [47], [37], [48], [27], [-11], [-45], [69], [-40], [27], [13], [9], [-72], [-15], [5], [9], [-52], [-1], [1], [27], [-37], [11], [-22], [-5], [-8], [-39], [28], [67], [-58], [69], [-13], [64], [-9], [65], [-40], [-27], [42], [17], [55], [3], [-72], [-2], [-13], [-42], [22], [-47], [-15], [-55], [-18], [-35], [-29], [-62], [-31], [-47], [19], [-67], [52], [-41], [-53], [-77], [8], [3], [-23], [-50], [53], [-59], [-29], [31], [37], [13], [52], [-10], [-57], [-67], [14], [-73], [72], [1], [39], [-3], [-25], [17], [-57], [60], [55], [-25], [-62], [-16], [35], [45], [38], [-51], [1], [-43], [-74], [-69], [42], [-79], [41], [16], [-10], [45], [81], [23], [-51], [59], [31], [10], [-56], [37], [-30], [-24], [-21], [-53], [-6], [29], [-57], [45], [-39], [-84], [12], [50], [-51], [-37], [36], [17], [-15], [39], [-3], [71], [8], [6], [-25], [39], [22], [-39], [38], [11], [57], [37], [38], [-71], [28], [1], [-9], [1], [-39], [12], [-59], [-4], [75], [-33], [-44], [-50], [37], [-12], [-43], [-1], [-29], [-98], [63], [12], [-2], [42], [11], [3], [-31], [48], [-75], [53], [-24], [-27], [-95], [-52], [21], [-29], [23], [-72], [63], [99], [19], [12], [21], [61], [35], [1], [-6], [-17], [29], [38], [-92], [-19], [-15], [73], [105], [-85], [35], [-56], [-13], [-21], [74], [24], [17], [18], [53], [67], [-50], [-61], [91], [46], [19], [41], [18], [-12], [31]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_5184_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_5184_2_a_i();".
// 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_5184_2_a_i(:prec:=1)
    chi := MakeCharacter_5184_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_5184_2_a_i();".
// 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_5184_2_a_i( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_5184_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![1, 1]>,<7,R![3, 1]>,<11,R![-5, 1]>],Snew);
    return Vf;
end function;