
// Make newform 5472.2.a.i in Magma, downloaded from the LMFDB on 26 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_5472_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_5472_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_5472_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_5472_a();"
function MakeCharacter_5472_a()
    N := 5472;
    order := 1;
    char_gens := [4447, 2053, 1217, 3745];
    v := [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_5472_a_Hecke(Kf)
    return MakeCharacter_5472_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], [0], [-1], [-2], [-1], [-3], [-1], [-3], [-3], [8], [-10], [12], [8], [8], [9], [5], [10], [7], [10], [1], [-14], [-6], [4], [-6], [-10], [14], [-5], [9], [-2], [18], [8], [17], [-16], [-4], [10], [2], [8], [16], [6], [-24], [-18], [21], [10], [4], [11], [-1], [2], [21], [22], [-26], [-27], [20], [2], [12], [16], [-22], [3], [12], [0], [6], [-1], [12], [5], [-3], [-21], [13], [-4], [-30], [14], [-9], [3], [20], [21], [-5], [38], [6], [-8], [-8], [12], [-28], [-37], [26], [10], [28], [-6], [6], [1], [20], [-20], [6], [28], [18], [-28], [28], [-27], [-34], [16], [1], [22], [-28], [32], [-4], [36], [-32], [43], [4], [-34], [40], [40], [-2], [6], [-10], [-14], [-40], [-46], [50], [-3], [4], [31], [-39], [-32], [11], [-36], [18], [-36], [-22], [-15], [11], [-24], [-32], [40], [20], [38], [29], [13], [31], [-53], [37], [15], [-39], [12], [41], [-51], [-23], [24], [-2], [-20], [-22], [-38], [37], [18], [34], [10], [33], [-24], [49], [39], [-23], [33], [16], [2], [-8], [-44], [-16], [14], [-28], [-20], [0], [-36], [-5], [-41], [-16], [23], [-52], [26], [-4], [50], [-44], [20], [-19], [51], [-50], [48], [-11], [10], [-47], [-32], [14], [-43], [26], [24], [32], [47], [-48], [-24], [-39], [-32], [-3], [-29], [14], [24], [47], [-28], [-30], [36], [-33], [45], [-21], [7], [29], [32], [20], [23], [-12], [-50], [23], [6], [18], [-22], [26], [-2], [6], [11], [-33], [58], [-26], [-10], [-62], [66], [34], [-49], [36], [-54], [10], [-65], [-10], [-48], [-54], [-60], [36], [30], [1], [-17], [-28], [12], [0], [-52], [4], [-55], [34], [-3], [9], [30], [4], [-30], [-14], [28], [-18], [70], [-20], [68], [4], [11], [70], [35], [-9], [45], [25], [-6], [71], [61], [-63], [45], [2], [29], [-38], [-65], [-44], [68], [30], [31], [21], [19], [32], [24], [1], [-54], [-59], [10], [62], [12], [-14], [-52], [14], [8], [-82], [-22], [-1], [58], [6], [83], [-65], [-23], [-44], [-2], [29], [-35], [-24], [52], [-56], [-50], [-10], [68], [-24], [-72], [2], [14], [19], [50], [17], [23], [-3], [-26], [-39], [-70], [35], [29], [52], [-36], [-56], [27], [-9], [18], [2], [84], [29], [16], [7], [-11], [21], [-39], [-35], [54], [-31], [60], [-66], [33], [-6], [-12], [16], [-38], [24], [24], [-42], [20], [-7], [12], [41], [78], [-24], [90], [-70], [29], [-54], [72], [70], [58], [22], [78], [-55], [-63], [-67], [44], [8], [63], [-64], [-18], [-93], [-68], [-54], [38], [81], [10], [-49], [94], [89], [-84], [66], [-40], [26], [91], [-14], [50], [24], [6], [-10], [60], [-44], [12], [2], [30], [-6], [-32], [-26], [67], [-50], [36], [21], [76], [76], [30], [-15], [10], [13], [-68], [-30], [0], [13], [36], [-6], [-31], [71], [55], [-2], [83], [17], [-35], [68], [37]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_5472_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_5472_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_5472_2_a_i(:prec:=1)
    chi := MakeCharacter_5472_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_5472_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_5472_2_a_i( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_5472_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![0, 1]>,<7,R![1, 1]>,<11,R![2, 1]>,<13,R![1, 1]>,<23,R![3, 1]>],Snew);
    return Vf;
end function;