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