
// Make newform 8550.2.a.j in Magma, downloaded from the LMFDB on 17 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_j();".
// 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_j();".
// 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], [0], [4], [-4], [-6], [-1], [0], [2], [0], [-4], [12], [6], [0], [14], [10], [-6], [-4], [-12], [8], [-8], [-12], [8], [-10], [-12], [2], [-20], [-14], [10], [-10], [20], [6], [-12], [0], [16], [6], [-10], [-12], [2], [-6], [-10], [-6], [-10], [10], [-4], [-8], [10], [12], [-10], [-14], [-10], [26], [24], [2], [-16], [2], [-28], [-14], [-12], [-2], [-14], [0], [18], [4], [-18], [8], [18], [-32], [34], [-22], [-34], [-12], [12], [12], [-36], [4], [18], [-28], [-34], [-8], [-14], [12], [-18], [16], [12], [32], [-32], [-24], [16], [24], [-38], [-18], [-36], [-4], [-16], [-18], [12], [0], [2], [24], [30], [36], [-32], [4], [-8], [-32], [22], [12], [-42], [30], [-46], [-6], [-28], [28], [-36], [-26], [24], [-18], [26], [-2], [26], [30], [12], [-4], [-20], [22], [-6], [-40], [-26], [20], [44], [-8], [-2], [30], [50], [38], [48], [42], [-26], [-40], [4], [-24], [-12], [50], [-8], [-50], [42], [-28], [24], [-28], [-42], [-2], [0], [12], [48], [4], [42], [-20], [-58], [-40], [-42], [-44], [-38], [-34], [-56], [48], [-2], [-14], [-22], [-10], [-2], [14], [44], [-56], [-38], [28], [0], [38], [-10], [28], [34], [-8], [-10], [-64], [24], [-40], [8], [-42], [54], [22], [-28], [8], [-42], [32], [38], [18], [-22], [54], [24], [-42], [0], [20], [-54], [48], [34], [-12], [-68], [6], [20], [-4], [-36], [-4], [12], [56], [-34], [52], [60], [32], [-54], [-70], [-40], [-20], [28], [28], [-74], [22], [20], [50], [48], [58], [-4], [-40], [0], [-46], [-56], [-14], [-66], [48], [8], [44], [-28], [-12], [-2], [62], [-42], [-24], [10], [32], [56], [46], [30], [24], [22], [-14], [-24], [-38], [-32], [54], [64], [26], [12], [-2], [16], [38], [-8], [30], [30], [-48], [6], [-18], [32], [-40], [-56], [-38], [68], [32], [46], [70], [36], [24], [40], [48], [10], [70], [-54], [16], [22], [24], [60], [-16], [52], [-38], [46], [-16], [4], [-64], [86], [42], [-26], [0], [6], [-56], [4], [-4], [38], [-36], [-30], [2], [26], [-80], [40], [52], [-8], [16], [10], [-56], [-36], [-28], [-6], [0], [-40], [2], [66], [34], [18], [-80], [-4], [-8], [54], [30], [-34], [40], [-36], [36], [4], [62], [22], [6], [-16], [-28], [50], [-18], [-32], [-34], [-18], [-34], [-8], [80], [-22], [4], [-14], [88], [42], [-22], [-34], [-38], [68], [46], [-56], [-90], [-78], [-68], [-10], [38], [74], [-50], [-70], [-50], [32], [24], [16], [-58], [32], [-14], [42], [-14], [-48], [-6], [46], [-14], [6], [-76], [-54], [4], [12], [88], [58], [74], [60], [30], [6], [-84], [-26], [92], [26], [88], [88], [-40], [-100], [-14], [-82], [-82], [6], [-70], [-84], [58], [-48], [92], [80], [78], [-22], [36], [-60], [-8], [84], [12], [46], [-82], [-16], [-54], [92], [-72], [22], [-26], [82], [-36], [38], [20], [42], [78], [-24], [20], [-52], [-42], [14], [-66], [38], [100], [24], [-96], [30], [54], [-4], [2], [-34], [70], [16], [14], [-98], [-72], [-48], [96], [66], [68], [66], [-10], [-78], [70], [42], [-4], [-32], [18], [70], [-30], [-40], [56], [-48], [66], [-60], [-34], [-92], [36], [-66], [58], [-44], [-64], [104], [24], [-50], [-14], [84], [-38], [98], [-16], [40], [46], [-102], [24], [-56], [-28], [-20], [-66], [-30], [-40], [-6], [20], [-6], [80], [-28], [-48], [-84], [-86]];
    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_j();".
// 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_j(: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_j();".
// 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_j( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_8550_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![0, 1]>,<11,R![-4, 1]>,<13,R![4, 1]>,<17,R![6, 1]>,<23,R![0, 1]>,<53,R![-14, 1]>],Snew);
    return Vf;
end function;