
// Make newform 8550.2.a.w in Magma, downloaded from the LMFDB on 16 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_8550_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_8550_2_a_w();".
// 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_8550_2_a_w();".
// 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_8550_a();"
function MakeCharacter_8550_a()
    N := 8550;
    order := 1;
    char_gens := [1901, 1027, 1351];
    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_8550_a_Hecke(Kf)
    return MakeCharacter_8550_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 := [[1], [0], [0], [-2], [4], [-6], [4], [1], [0], [10], [-2], [2], [-8], [8], [0], [-6], [2], [2], [-4], [0], [10], [-2], [-10], [12], [2], [-6], [-4], [8], [12], [14], [20], [12], [16], [-12], [18], [18], [-20], [20], [-16], [-14], [22], [12], [-16], [-10], [6], [20], [16], [16], [12], [22], [-4], [-24], [2], [-16], [-6], [8], [26], [-20], [-20], [-24], [16], [22], [-4], [-32], [-6], [30], [-28], [22], [-6], [14], [0], [-16], [34], [-26], [16], [24], [-38], [16], [20], [-6], [0], [-20], [8], [-26], [-2], [-6], [8], [22], [-18], [-34], [6], [0], [-4], [-4], [-28], [-28], [26], [4], [44], [30], [36], [-30], [-8], [-24], [-36], [-42], [18], [16], [12], [-14], [12], [-8], [0], [4], [8], [48], [16], [28], [-30], [30], [-20], [2], [-18], [12], [28], [-10], [-26], [-8], [-30], [-8], [4], [24], [-26], [-8], [-46], [30], [-30], [-52], [38], [10], [-40], [10], [-34], [32], [-28], [-24], [-28], [-54], [28], [-56], [30], [-10], [-16], [0], [36], [12], [16], [-18], [30], [-26], [-10], [18], [-26], [14], [50], [0], [2], [-20], [42], [-22], [-26], [-28], [-40], [-30], [50], [-30], [4], [-58], [16], [-26], [54], [18], [-6], [-14], [24], [-6], [-34], [4], [-38], [-40], [50], [-2], [-48], [-42], [-26], [-34], [54], [-4], [8], [0], [54], [26], [34], [62], [36], [-10], [4], [24], [-50], [40], [-46], [22], [66], [-12], [4], [-26], [6], [36], [24], [-6], [72], [-18], [-24], [46], [28], [-50], [-62], [-72], [-12], [60], [4], [44], [30], [-28], [44], [0], [14], [-6], [0], [24], [52], [68], [26], [24], [-10], [-56], [50], [-34], [-16], [-64], [-40], [54], [60], [-58], [-74], [16], [26], [-28], [-42], [-70], [-48], [-8], [-22], [46], [20], [-76], [-58], [62], [-4], [58], [-20], [-44], [-22], [-4], [54], [-62], [10], [-72], [2], [-76], [-48], [-8], [12], [-28], [20], [-24], [-50], [-38], [-64], [-44], [-66], [-34], [-66], [38], [-34], [30], [38], [14], [62], [-56], [66], [-46], [-88], [64], [76], [82], [-24], [-32], [-40], [8], [76], [-10], [-52], [-12], [-40], [-18], [24], [60], [26], [-46], [0], [58], [58], [-28], [36], [-30], [8], [-52], [88], [26], [-14], [-62], [72], [-82], [44], [6], [-40], [-38], [-62], [70], [-34], [68], [-6], [26], [-50], [-54], [34], [-28], [0], [-42], [-60], [-58], [-46], [20], [68], [42], [64], [12], [-32], [-40], [68], [42], [-48], [62], [72], [58], [30], [-44], [78], [84], [76], [4], [-30], [-28], [92], [-70], [8], [-50], [-10], [-22], [54], [-60], [2], [12], [88], [0], [-28], [-4], [-36], [-8], [38], [78], [0], [68], [28], [54], [10], [60], [-80], [-30], [28], [28], [-16], [-78], [-70], [20], [-100], [-40], [-20], [-20], [26], [18], [-60], [44], [-22], [50], [20], [88], [-72], [48], [-34], [46], [-8], [-58], [10], [30], [-56], [30], [84], [32], [50], [60], [-36], [56], [12], [18], [54], [-80], [60], [-4], [-66], [-50], [-60], [-68], [18], [-18], [28], [-48], [-18], [-24], [-108], [-96], [-66], [6], [58], [-34], [8], [6], [-16], [-86], [-32], [46], [98], [40], [-104], [-6], [-50], [-18], [14], [44], [24], [-28], [28], [-50], [54], [-10], [18], [108], [-96], [10], [-14], [-60], [62], [74], [-10], [42], [106], [-14], [-8], [70], [82], [-80], [-94], [-66], [60], [30], [-52], [30], [88], [32], [-26], [-34], [-30]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_8550_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_8550_2_a_w();".
// 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_8550_2_a_w(:prec:=1)
    chi := MakeCharacter_8550_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(3593) - 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_8550_2_a_w();".
// 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_8550_2_a_w( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_8550_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![2, 1]>,<11,R![-4, 1]>,<13,R![6, 1]>,<17,R![-4, 1]>,<23,R![0, 1]>,<53,R![6, 1]>],Snew);
    return Vf;
end function;