
// Make newform 5850.2.a.bq in Magma, downloaded from the LMFDB on 11 September 2025.
// To make the character of type GrpDrchElt, type "MakeCharacter_5850_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_5850_2_a_bq();".
// 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_5850_2_a_bq();".
// 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_5850_a();"
function MakeCharacter_5850_a()
    N := 5850;
    order := 1;
    char_gens := [3251, 3277, 2251];
    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_5850_a_Hecke( : Kf := Kf)
    return MakeCharacter_5850_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]];
    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], [0], [-1], [2], [-8], [-4], [2], [-4], [-6], [-10], [0], [8], [6], [-8], [-2], [-4], [12], [-10], [-8], [12], [-10], [14], [-6], [4], [0], [-2], [-14], [20], [-4], [-6], [4], [-6], [-20], [18], [-20], [16], [-18], [-4], [22], [0], [-18], [-26], [24], [4], [8], [20], [-10], [-6], [-20], [2], [28], [2], [-28], [18], [-28], [-22], [22], [-16], [6], [-28], [0], [6], [-2], [-8], [-18], [32], [14], [-30], [-4], [12], [-6], [0], [8], [-6], [18], [30], [26], [36], [-26], [-12], [-34], [-24], [24], [-18], [38], [-14], [16], [-16], [4], [8], [-20], [8], [20], [18], [-26], [8], [-2], [40], [14], [24], [-10], [-28], [-2], [-20], [2], [-32], [26], [-36], [-38], [26], [-24], [12], [30], [-28], [-28], [46], [36], [22], [14], [22], [28], [32], [34], [-10], [-16], [28], [34], [0], [16], [-40], [-22], [6], [-14], [-26], [4], [-34], [-26], [24], [42], [4], [36], [-34], [-4], [-22], [-6], [-28], [40], [-14], [-2], [8], [44], [-8], [-48], [32], [14], [-26], [-30], [-4], [26], [-24], [4], [18], [-24], [32], [-22], [-30], [6], [32], [-2], [0], [22], [32], [6], [64], [10], [4], [-18], [8], [36], [10], [-6], [-48], [-22], [-14], [52], [10], [-4], [30], [36], [-12], [18], [-64], [-38], [-46], [2], [2], [36], [-30], [-16], [10], [50], [24], [-34], [52], [48], [-26], [52], [-34], [42], [60], [4], [20], [42], [-52], [-18], [-8], [-34], [-58], [-28], [-50], [-40], [32], [54], [42], [24], [28], [-32], [-14], [-20], [-20], [-42], [8], [-32], [2], [22], [20], [-8], [-36], [4], [44], [-2], [-30], [-56], [56], [-72], [32], [36], [34], [30], [-64], [-54], [30], [0], [-10], [-60], [54], [6], [-4], [24], [-26], [2], [66], [44], [-46], [-42], [-76], [6], [78], [-68], [-10], [-64], [-50], [64], [12], [-34], [-6], [-44], [-52], [12], [12], [-58], [-52], [-40], [-2], [22], [-28], [-18], [18], [-72], [42], [16], [34], [66], [-16], [-58], [-36], [-28], [86], [-82], [-64], [32], [-20], [-34], [72], [46], [60], [74], [60], [-86], [78], [-16], [0], [-38], [88], [-12], [-66], [-18], [-20], [38], [-30], [-80], [-22], [-62], [16], [-52], [-4], [6], [14], [-18], [88], [36], [24], [-84], [-34], [-46], [58], [4], [10], [-6], [-54], [-64], [-66], [44], [6], [-52], [-84], [-42], [-64], [70], [-94], [-4], [-74], [42], [36], [56], [-30], [48], [26], [6], [-60], [-48], [-32], [6], [-50], [40], [-38], [60], [-4], [-32], [42], [24], [82], [72], [-88], [78], [-66], [-26], [2], [-54], [96], [-30], [16], [-72], [-52], [74], [-4], [-12], [2], [-26], [-24], [-88], [100], [-58], [-28], [-58], [-68], [58], [-50], [-46], [-72], [26], [-38], [-56], [-78], [-66], [-4], [-64], [46], [22], [-56], [-68], [38], [-78], [-36], [-12], [-14], [12], [2], [-38], [48], [12], [-58], [78], [24], [22], [32], [80]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_5850_a_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_5850_2_a_bq();".
// 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_5850_2_a_bq(:prec:=1)
    chi := MakeCharacter_5850_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_5850_2_a_bq();".
// 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_5850_2_a_bq( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_5850_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![0, 1]>,<11,R![0, 1]>,<17,R![-2, 1]>,<23,R![4, 1]>,<31,R![4, 1]>],Snew);
    return Vf;
end function;