
// Make newform 8550.2.a.m in Magma, downloaded from the LMFDB on 20 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_m();".
// 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_m();".
// 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], [1], [6], [-5], [3], [1], [3], [-9], [-4], [-2], [0], [-8], [0], [-3], [-9], [-10], [-5], [6], [7], [-10], [-6], [12], [10], [-18], [-14], [-9], [11], [6], [-2], [0], [-9], [-4], [0], [-10], [22], [-20], [12], [6], [0], [2], [-3], [-14], [0], [11], [5], [-26], [-15], [-22], [-6], [21], [8], [-6], [12], [24], [6], [11], [-8], [0], [22], [-21], [-20], [21], [19], [-9], [-1], [4], [18], [-10], [-15], [-21], [28], [-23], [-7], [18], [-18], [-20], [0], [32], [12], [17], [-6], [-2], [-28], [-18], [18], [-17], [12], [4], [18], [-36], [-2], [36], [-4], [-21], [-30], [36], [-11], [2], [-44], [24], [-12], [24], [-4], [-11], [-12], [-30], [24], [-28], [22], [-2], [-6], [-10], [-16], [-6], [22], [27], [24], [45], [-13], [-44], [-33], [36], [-10], [-12], [-10], [-39], [37], [-32], [-16], [-36], [-40], [-2], [21], [5], [51], [31], [-39], [-9], [11], [0], [-41], [33], [11], [48], [46], [-12], [14], [-18], [-23], [18], [34], [-42], [37], [-48], [-7], [-33], [7], [-21], [-48], [30], [-32], [-12], [-12], [-30], [20], [-8], [20], [-24], [-39], [5], [0], [1], [32], [-30], [8], [-30], [-56], [8], [-53], [33], [22], [48], [51], [6], [61], [-8], [-58], [-27], [34], [-36], [56], [-51], [-12], [12], [41], [-44], [3], [-27], [-30], [8], [-5], [-28], [6], [-48], [47], [-15], [3], [-67], [-29], [24], [28], [27], [-60], [-46], [-23], [-30], [18], [18], [-58], [14], [-18], [61], [27], [62], [66], [42], [10], [-66], [-14], [29], [-40], [30], [-26], [-39], [-58], [72], [-18], [12], [-12], [74], [31], [-43], [36], [60], [16], [60], [-4], [-57], [58], [69], [-57], [62], [-12], [30], [2], [28], [-42], [-38], [28], [60], [32], [7], [66], [17], [45], [-3], [13], [66], [5], [-47], [55], [5], [-14], [-29], [6], [53], [20], [0], [-66], [23], [27], [17], [4], [-24], [79], [-6], [-19], [18], [-66], [36], [66], [-60], [46], [12], [26], [6], [-3], [46], [-14], [39], [-25], [69], [-4], [34], [81], [23], [24], [-20], [48], [-30], [-90], [4], [-24], [-16], [54], [18], [37], [-78], [-37], [-23], [39], [70], [-9], [2], [-7], [73], [24], [-12], [80], [-9], [-1], [30], [74], [-48], [47], [24], [-49], [43], [-71], [-57], [15], [-10], [-27], [-84], [2], [37], [66], [-72], [-40], [-2], [-48], [-8], [-22], [72], [9], [-48], [-33], [-54], [-8], [90], [42], [15], [-74], [-44], [-42], [-74], [38], [-90], [-61], [-9], [39], [-4], [16], [45], [-72], [-38], [3], [-44], [78], [-30], [-65], [-66], [5], [-42], [17], [4], [34], [24], [-82], [87], [54], [10], [-36], [-14], [74], [-48], [68], [72], [14], [66], [94], [-72], [-6], [11], [82], [-84], [-23], [24], [64], [54], [-27], [62], [-77], [-36], [-6], [28], [51], [48], [-42], [-83], [-15], [69], [-2], [87], [21], [-3], [-88], [21], [50], [42], [-94], [12], [22], [63], [-46], [-34], [-44], [-88], [54], [-21], [-58], [0], [35], [-18], [4], [42], [-16], [14], [-29], [48], [0], [54], [58], [-78], [62], [75], [22], [-108], [-79], [-40], [-51], [107], [-68], [43], [44], [24], [21], [-70], [-68], [-33], [0], [23], [-60], [-62], [48], [53], [-33], [57], [76], [0], [-56], [-18], [19], [-90], [-76], [15], [116], [2], [-62], [-72], [20], [-30], [-96], [-4], [-92], [-72], [-85], [-55], [102], [61], [24]];
    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_m();".
// 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_m(: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_m();".
// 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_m( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_8550_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![-1, 1]>,<11,R![-6, 1]>,<13,R![5, 1]>,<17,R![-3, 1]>,<23,R![-3, 1]>,<53,R![3, 1]>],Snew);
    return Vf;
end function;