
// Make newform 5544.2.a.i in Magma, downloaded from the LMFDB on 02 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_5544_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_5544_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_5544_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_5544_a();"
function MakeCharacter_5544_a()
    N := 5544;
    order := 1;
    char_gens := [4159, 2773, 4313, 1585, 2521];
    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_5544_a_Hecke(Kf)
    return MakeCharacter_5544_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], [1], [1], [-1], [2], [-1], [-4], [5], [-4], [-3], [-6], [2], [9], [-2], [1], [-2], [-11], [2], [-11], [-14], [-6], [14], [10], [-12], [8], [17], [16], [-16], [-12], [-6], [2], [-20], [-3], [-6], [-2], [-11], [8], [-6], [-18], [-12], [12], [-18], [-22], [14], [16], [16], [18], [-8], [-6], [-5], [-21], [-9], [-13], [21], [-18], [3], [-8], [-3], [-29], [8], [-16], [0], [26], [-24], [-12], [8], [-4], [-5], [-15], [24], [-10], [-36], [-5], [20], [-10], [-2], [18], [18], [15], [-13], [9], [6], [11], [-16], [0], [-4], [-12], [-19], [13], [24], [-20], [-27], [3], [36], [-26], [-5], [21], [10], [24], [21], [-8], [-18], [4], [-12], [11], [-24], [-14], [-11], [11], [-26], [2], [26], [36], [-4], [18], [7], [4], [9], [-30], [28], [36], [-10], [-10], [30], [27], [47], [4], [38], [-32], [9], [-31], [37], [30], [51], [-37], [-15], [-3], [-19], [5], [11], [-7], [-27], [-44], [-51], [30], [-56], [58], [24], [-20], [-17], [3], [6], [36], [-24], [-26], [-27], [-10], [-42], [18], [21], [-4], [-45], [-12], [36], [5], [38], [2], [-17], [45], [20], [12], [4], [8], [30], [-16], [34], [9], [45], [25], [12], [46], [-20], [-4], [-45], [4], [16], [10], [42], [66], [47], [1], [1], [-51], [49], [-22], [14], [-24], [42], [50], [37], [-4], [29], [-60], [2], [58], [-25], [26], [-10], [-24], [5], [23], [-12], [-4], [64], [-38], [20], [8], [-6], [2], [1], [-2], [-25], [-50], [12], [-27], [20], [41], [-37], [50], [-35], [22], [65], [-2], [48], [4], [-31], [-42], [45], [-48], [32], [62], [-26], [12], [46], [-8], [-3], [-58], [-79], [22], [-42], [-6], [50], [4], [24], [-31], [-7], [21], [1], [-2], [-48], [-34], [-51], [-75], [58], [34], [-5], [50], [22], [-8], [25], [46], [-34], [17], [-50], [42], [-44], [18], [52], [66], [-32], [72], [41], [23], [10], [-65], [74], [-52], [64], [-48], [44], [-71], [-60], [50], [10], [34], [10], [-58], [55], [-29], [27], [52], [-64], [-31], [-69], [-72], [-16], [-56], [-14], [-56], [-68], [45], [68], [16], [-26], [-72], [0], [-4], [78], [15], [-85], [48], [-72], [75], [-26], [-68], [4], [67], [57], [52], [42], [-37], [4], [-58], [-79], [11], [36], [-92], [28], [-86], [30], [-48], [55], [-32], [17], [-72], [8], [-2], [-6], [-72], [5], [70], [15], [-58], [14], [21], [-26], [45], [-6], [22], [-78], [18], [25], [-30], [63], [12], [-74], [-76], [-32], [-18], [27], [-49], [-88], [-89], [-21], [35], [-19], [-54], [-74], [73], [-6], [-28], [-47], [44], [45], [-67], [-18], [-8], [-89], [21], [-42], [24], [62], [-46], [48], [-49], [-51], [72], [13], [-21], [-72], [-96], [42], [-38], [-23], [-49], [6], [10], [36], [6], [-46], [12], [-32], [-50], [35], [41], [-22], [90], [0], [34], [-1], [36], [64], [82], [3], [74], [33], [-62], [28]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_5544_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_5544_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_5544_2_a_i(:prec:=1)
    chi := MakeCharacter_5544_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_5544_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_5544_2_a_i( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_5544_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![1, 1]>,<13,R![1, 1]>,<17,R![-2, 1]>],Snew);
    return Vf;
end function;