
// Make newform 2850.2.a.bg in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_2850_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_2850_2_a_bg();".
// 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_2850_2_a_bg();".
// 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
        poly := [-10, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_2850_a();"
function MakeCharacter_2850_a()
    N := 2850;
    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_2850_a_Hecke(Kf)
    return MakeCharacter_2850_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], [-1, 0], [0, 0], [0, 1], [1, 1], [2, 1], [4, -1], [-1, 0], [1, -2], [-7, 1], [-1, 2], [10, 0], [0, -2], [-6, -1], [6, 0], [5, -1], [2, -3], [-1, -1], [3, -3], [-2, 1], [-1, 0], [1, -2], [-3, 3], [-1, -2], [10, -3], [2, -5], [3, 4], [16, -1], [14, -2], [-3, 4], [3, 4], [-3, -3], [-4, 2], [12, 3], [4, 4], [-10, 0], [12, 2], [-10, -4], [-10, 4], [1, -1], [-8, -4], [6, 1], [9, 4], [-6, -1], [-2, -7], [-6, -4], [-3, -5], [-13, -4], [-2, 0], [5, 1], [4, -5], [-18, -2], [20, -3], [2, -4], [-13, -2], [13, 6], [6, 2], [2, -1], [9, 5], [-5, 4], [12, 4], [7, 3], [23, -3], [-26, 0], [-17, 2], [1, 1], [-19, 5], [16, -6], [-20, 0], [-21, 1], [8, 5], [-18, 2], [2, -3], [6, -7], [4, 6], [6, -3], [-22, -4], [-5, 7], [1, 4], [-14, 2], [8, 0], [6, 4], [6, -2], [6, 7], [1, 4], [-15, 1], [-11, 0], [16, -2], [12, 0], [-28, 2], [-15, -7], [-21, -6], [-16, -4], [0, 0], [10, -5], [-4, -8], [1, 5], [1, 6], [-4, -4], [3, -1], [-1, -5], [-26, 1], [28, 1], [22, 0], [0, -5], [-23, 2], [-3, -5], [4, -2], [-20, 5], [30, 5], [21, -6], [-2, -8], [-18, 2], [14, -1], [20, 8], [-12, -8], [-6, 4], [-9, 0], [-12, 6], [-34, -5], [12, 8], [-4, -9], [9, 3], [4, -13], [4, 3], [-12, -6], [33, -3], [13, 2], [-18, -10], [41, 3], [0, 2], [-8, 4], [-26, 0], [-27, 7], [-30, -3], [41, -2], [-10, 6], [25, 3], [16, 10], [-20, -1], [27, -5], [40, -1], [-2, -8], [2, 1], [12, -11], [-16, 1], [26, 2], [32, -2], [-18, 4], [36, 6], [14, 10], [8, 2], [-20, -3], [28, -2], [-26, -4], [-26, 6], [-22, 1], [-12, -8], [-14, -4], [17, 5], [-48, 0], [-47, 2], [18, 2], [22, -3], [-16, -12], [-14, -7], [-1, 8], [23, -3], [30, 4], [4, 9], [8, 10], [-14, 9], [-14, 4], [11, 4], [17, 12], [14, 2], [8, 8], [-4, 17], [-2, -4], [7, -3], [2, -17], [4, 3], [44, 1], [13, 4], [-11, 2], [6, -9], [-12, -10], [35, 5], [-40, 0], [5, -2], [16, -6], [45, -1], [1, -1], [-3, -9], [-18, 0], [-16, -16], [-23, 8], [-17, -15], [2, 4], [29, -8], [5, -1], [-22, 4], [22, -11], [-8, 9], [9, 15], [-44, 6], [-14, -9], [14, 11], [14, -2], [-18, 4], [-15, -8], [-8, 5], [12, -3], [42, 0], [-52, -2], [-30, -2], [-30, 6], [-17, -14], [18, -12], [14, 14], [18, 6], [1, -22], [24, -12], [-14, 15], [-32, -11], [-12, 2], [61, 4], [-2, 7], [-18, -10], [-25, -13], [-14, -6], [13, -5], [-45, 6], [15, -12], [36, -5], [40, -8], [-44, 6], [-16, 1], [-32, -10], [-14, -9], [14, 13], [-8, 6], [34, -2], [-56, 5], [-12, 6], [-37, 0], [24, 4], [30, -12], [-25, -7], [31, 4], [-13, -3], [-6, 9], [43, 6], [6, -13], [-48, -6], [47, 5], [18, -16], [-39, 5], [-46, 2], [-8, -8], [-9, -6], [30, -15], [35, 3], [24, 11], [-18, 10], [-67, -1], [14, 2], [52, 1], [55, 5], [38, 1], [24, 8], [-27, 17], [41, 12], [6, -10], [-16, 12], [-24, 7], [1, -13], [10, 5], [-28, -8], [-17, -5], [12, 9], [-52, -9], [-10, -6], [-12, -15], [48, 8], [-12, -4], [21, 14], [27, -5], [8, 10], [35, -8], [-44, 13], [-13, 7], [12, 0], [60, 3], [40, -8], [16, 17], [34, 0], [-34, -3], [-42, 4], [-54, 2], [23, -18], [-39, 3], [-34, 9], [68, 4], [-62, -4], [-24, 2], [30, 11], [56, 6], [15, -6], [-77, 3], [37, -16], [56, 5], [-59, 2], [25, 11], [-22, 20], [24, -15], [29, 13], [18, 18], [-28, 8], [-62, -5], [-28, -2], [32, 6], [78, 0], [-8, 4], [-20, 11], [6, -3], [34, 16], [-46, 8], [28, 13], [-52, 5], [-19, 9], [-13, -13], [-16, -14], [1, 19], [-26, 0], [-13, 7], [-70, -6], [17, -10], [55, 0], [-40, -6], [-10, -3], [78, -1], [29, -3], [43, -8], [-9, 3], [10, 6], [-25, 9], [-21, -21], [48, 10], [-10, 19], [-1, -5], [-48, -13], [4, 22], [-9, -2], [36, 4], [37, 10], [-57, -12], [35, 11], [2, 17], [-24, -11], [-33, 13], [-4, -17], [22, 19], [-70, -2], [-36, -16], [-56, 1], [-46, -4], [-58, 0], [8, -2], [77, -1], [-21, -7], [-16, 4], [-33, -3], [20, -22], [18, 22], [-12, 3], [27, 0], [-55, 14], [40, 18], [6, 4], [-30, 0], [18, 24], [-36, -14], [20, 5], [-39, -5], [32, -17], [8, -4], [-50, -6], [-4, -26], [-26, -11], [-16, 11], [-69, 5], [43, -11], [-6, 22], [2, 7], [-46, 7], [30, -4], [-35, 2], [25, 9], [-50, -3], [-22, -10], [14, 14], [-10, 0], [-43, -6], [-3, -27], [62, -8], [11, 21], [15, 0], [-7, -3], [-33, -3], [-4, -28], [10, -12], [66, 11], [-36, 10], [-11, 4], [-18, 6], [-24, 18], [25, 6], [-8, -8], [-2, 11], [-50, -4], [58, 0], [43, -20], [10, 26], [24, 13], [70, -4], [8, 2], [0, 6], [-62, -13], [-9, 18]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_2850_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_2850_2_a_bg();".
// 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_2850_2_a_bg(:prec:=2)
    chi := MakeCharacter_2850_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_2850_2_a_bg();".
// 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_2850_2_a_bg( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_2850_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![-10, 0, 1]>,<11,R![-9, -2, 1]>,<13,R![-6, -4, 1]>,<23,R![-39, -2, 1]>],Snew);
    return Vf;
end function;