
// Make newform 5850.2.a.bu in Magma, downloaded from the LMFDB on 24 April 2024.
// 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_bu();".
// 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_bu();".
// 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)
    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+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], [5], [1], [5], [0], [0], [7], [-9], [8], [2], [-8], [-9], [11], [-1], [-7], [15], [8], [-4], [-4], [-9], [-16], [-2], [7], [-6], [6], [6], [2], [8], [2], [12], [-2], [22], [-21], [5], [4], [-4], [-11], [-2], [-11], [-18], [10], [6], [18], [16], [-16], [29], [-2], [6], [-1], [-22], [6], [27], [26], [-9], [7], [-26], [-30], [22], [-14], [-12], [18], [-21], [0], [-20], [13], [-6], [32], [-2], [-27], [36], [17], [-15], [0], [-2], [34], [0], [-14], [2], [28], [-12], [-34], [4], [-32], [-36], [-2], [6], [11], [-32], [-29], [1], [-30], [11], [-2], [-6], [26], [18], [28], [2], [2], [-24], [9], [-32], [42], [-15], [22], [-6], [-35], [-2], [-2], [42], [-28], [-36], [-33], [28], [-48], [-27], [-44], [30], [-11], [18], [19], [7], [25], [-46], [18], [38], [-20], [3], [39], [-40], [-33], [8], [-8], [14], [5], [-13], [34], [-29], [16], [28], [-21], [-37], [16], [8], [-26], [36], [27], [-40], [33], [-32], [4], [-40], [-40], [-50], [42], [-51], [-8], [-41], [51], [17], [34], [-12], [-31], [36], [11], [-44], [-63], [-20], [-28], [-22], [-16], [50], [35], [-53], [54], [22], [-13], [-40], [44], [-6], [-52], [-29], [42], [55], [1], [40], [-17], [-46], [59], [28], [32], [22], [-49], [48], [-57], [-10], [-38], [-28], [48], [6], [-55], [-63], [18], [-23], [-6], [-20], [-34], [-29], [-6], [-12], [5], [-7], [-28], [-30], [51], [-27], [16], [46], [-24], [-48], [-1], [-12], [-45], [-10], [-14], [-70], [11], [26], [52], [35], [70], [-12], [-32], [-22], [-12], [56], [52], [-4], [-52], [-22], [-16], [40], [18], [-52], [-24], [-55], [6], [32], [36], [12], [75], [-42], [-45], [66], [-45], [47], [-30], [-46], [44], [-28], [10], [-20], [-8], [18], [48], [63], [-43], [50], [5], [66], [46], [-57], [21], [-36], [-32], [-16], [-34], [30], [40], [0], [-74], [31], [36], [53], [6], [41], [45], [69], [78], [-18], [-59], [21], [-15], [24], [42], [-64], [-77], [-45], [-62], [-82], [68], [78], [-80], [-40], [50], [8], [-17], [44], [-12], [-11], [80], [-71], [66], [-7], [32], [-16], [-37], [-6], [-38], [6], [-51], [44], [-13], [-47], [-19], [18], [-11], [26], [-2], [-86], [21], [-83], [27], [46], [36], [-38], [22], [6], [30], [54], [-20], [-96], [28], [79], [79], [31], [15], [77], [-40], [-30], [48], [10], [19], [13], [-3], [-17], [-55], [24], [-58], [44], [96], [38], [-10], [18], [1], [11], [6], [-70], [80], [5], [-34], [79], [95], [50], [-48], [-21], [98], [2], [-12], [-17], [-82], [-12], [72], [31], [97], [-84], [18], [20], [-12], [11], [-22], [-63], [-19], [-65], [63], [-10], [58], [-76], [85], [-25], [-14], [66], [2], [82], [-70], [-29], [-52], [-65], [-53], [102], [-22], [46], [-35], [60], [-52], [22], [38], [-1], [100], [5], [40], [-44], [-56], [76], [-78], [65], [-70]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_5850_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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