
// Make newform 8050.2.a.s in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_8050_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_8050_2_a_s();".
// 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_8050_2_a_s();".
// 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_8050_a();"
function MakeCharacter_8050_a()
    N := 8050;
    order := 1;
    char_gens := [2577, 1151, 3501];
    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_8050_a_Hecke(Kf)
    return MakeCharacter_8050_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], [1], [0], [1], [-5], [2], [-5], [5], [1], [-2], [-4], [4], [-5], [-8], [-6], [-4], [4], [10], [1], [2], [-15], [-10], [3], [-15], [-2], [0], [-16], [11], [-2], [-13], [6], [0], [1], [17], [8], [-24], [8], [17], [-24], [-2], [-17], [-14], [-18], [-11], [22], [-2], [13], [10], [-12], [2], [-18], [-8], [7], [-5], [18], [18], [2], [2], [-2], [22], [-5], [-26], [-7], [-10], [-10], [20], [19], [23], [23], [28], [14], [-18], [28], [4], [-15], [-6], [-24], [-10], [33], [19], [-23], [8], [-28], [-29], [24], [1], [41], [5], [-18], [-20], [36], [-18], [30], [-36], [20], [6], [6], [-25], [21], [-14], [23], [-16], [20], [-47], [-12], [-3], [7], [33], [-8], [5], [32], [6], [-18], [20], [20], [-14], [16], [48], [-24], [49], [-30], [-14], [-36], [-41], [21], [0], [-36], [-24], [-6], [-4], [12], [-20], [-42], [2], [19], [-41], [20], [28], [34], [-26], [-28], [16], [18], [-39], [36], [-14], [42], [19], [25], [-20], [-28], [-14], [-37], [26], [-44], [24], [-38], [6], [-43], [0], [-12], [45], [-38], [-3], [-47], [-32], [0], [46], [37], [-2], [-35], [30], [6], [43], [-24], [-31], [-51], [-20], [-10], [14], [4], [5], [14], [29], [54], [-12], [-54], [-19], [1], [22], [17], [9], [-23], [36], [51], [42], [31], [38], [9], [54], [66], [58], [22], [-27], [12], [36], [-30], [-8], [30], [8], [2], [-60], [-8], [25], [-10], [55], [36], [27], [40], [-24], [-36], [-50], [-15], [-48], [-7], [72], [29], [-42], [8], [9], [-62], [55], [-50], [11], [-4], [-16], [-1], [-6], [-19], [-12], [19], [-41], [46], [46], [-47], [-10], [-70], [-28], [69], [-62], [-34], [-67], [22], [-15], [-24], [1], [-20], [39], [-28], [-61], [2], [49], [-30], [-52], [15], [25], [-30], [-30], [-49], [-24], [36], [5], [5], [62], [-46], [44], [47], [22], [-66], [21], [66], [52], [-36], [-46], [-69], [-60], [30], [-30], [-80], [-50], [-42], [-4], [-57], [23], [26], [-24], [74], [-76], [-20], [31], [-49], [-62], [74], [39], [89], [1], [-12], [-4], [36], [10], [-14], [-16], [-7], [64], [42], [-58], [65], [54], [37], [-21], [56], [13], [92], [-84], [-6], [-10], [25], [20], [-32], [0], [-66], [-26], [0], [73], [-89], [36], [-2], [-6], [-55], [62], [-28], [-35], [72], [-40], [-34], [-36], [28], [37], [42], [14], [47], [58], [38], [18], [4], [61], [-62], [-67], [42], [66], [8], [-7], [58], [-96], [7], [-73], [-48], [-80], [5], [-36], [-15], [62], [-62], [22], [48], [65], [2], [-43], [27], [-22], [88], [85], [-8], [-93], [-60], [94], [-88], [-12], [-92], [-42], [-5], [24], [-79], [59], [-62], [-63], [80], [83], [-5], [-44], [58], [57], [82], [-93], [48], [50], [-104], [30], [-12], [27], [-99], [60], [-27], [88], [-57], [-102], [-22], [66], [-33], [78], [-48], [62], [-52], [-69], [-89], [-80], [4], [15], [-91], [-36]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_8050_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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