
// Make newform 1850.2.a.n in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1850_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_1850_2_a_n();".
// 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_1850_2_a_n();".
// 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_1850_a();"
function MakeCharacter_1850_a()
    N := 1850;
    order := 1;
    char_gens := [1777, 1001];
    v := [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_1850_a_Hecke(Kf)
    return MakeCharacter_1850_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], [2], [0], [0], [4], [-2], [8], [-5], [1], [10], [-4], [1], [7], [-9], [6], [-3], [-11], [2], [2], [14], [3], [-11], [8], [-2], [-8], [-9], [17], [-10], [-16], [-2], [4], [-12], [-10], [-6], [3], [-8], [7], [-9], [23], [-2], [-12], [-7], [-19], [20], [7], [1], [-22], [6], [-7], [-1], [-29], [-13], [10], [-21], [8], [14], [31], [-4], [10], [28], [3], [9], [-12], [21], [-10], [-27], [-24], [-1], [33], [-7], [-4], [-26], [14], [22], [16], [-29], [32], [-13], [-2], [16], [8], [-26], [-3], [26], [3], [0], [-30], [-24], [24], [24], [36], [24], [-24], [34], [-29], [-36], [-11], [15], [-20], [-30], [-4], [0], [27], [44], [-6], [-44], [27], [31], [0], [-37], [-8], [43], [18], [-10], [-40], [11], [-28], [7], [-30], [0], [4], [19], [-3], [16], [10], [-10], [24], [28], [0], [2], [18], [-34], [26], [-8], [13], [-46], [42], [-4], [32], [8], [44], [45], [34], [-12], [-16], [-6], [-30], [-6], [51], [6], [27], [-15], [32], [42], [-3], [5], [24], [34], [38], [6], [-41], [41], [-13], [20], [62], [-42], [-23], [-60], [25], [48], [-8], [0], [0], [-1], [40], [10], [3], [15], [-36], [50], [-57], [9], [16], [32], [66], [45], [-47], [19], [-14], [-10], [-14], [34], [-21], [-42], [-12], [-35], [30], [68], [-2], [-36], [-62], [30], [23], [38], [60], [48], [-56], [-8], [6], [-20], [2], [-36], [32], [-28], [-45], [-45], [-31], [54], [21], [6], [-49], [-26], [54], [39], [-32], [-26], [-59], [10], [48], [27], [11], [-8], [0], [-7], [4], [-52], [53], [72], [-15], [68], [-23], [4], [-4], [40], [-3], [23], [8], [-63], [-32], [-33], [-52], [30], [-18], [-56], [-52], [-60], [41], [-6], [-18], [-44], [-48], [-9], [61], [-55], [48], [22], [6], [-72], [-14], [-38], [42], [-23], [46], [-39], [9], [-44], [-42], [-44], [-77], [-52], [84], [39], [16], [9], [8], [-8], [-50], [-69], [57], [2], [-60], [-35], [26], [-42], [14], [-14], [40], [57], [-75], [-14], [-38], [-27], [2], [49], [31], [28], [-67], [80], [-44], [-58], [14], [-3], [-78], [26], [-64], [-80], [28], [24], [-35], [36], [-4], [3], [-18], [40], [-91], [-52], [-86], [-20], [21], [34], [-26], [-24], [-47], [3], [40], [-2], [49], [11], [30], [-48], [41], [39], [-54], [43], [36], [-87], [89], [30], [-60], [39], [-4], [43], [10], [81], [31], [66], [-33], [77], [-77], [61], [46], [-15], [88], [-11], [-66], [36], [-84], [-87], [0], [-81], [-35], [-36], [-33], [56], [-50], [8], [18], [-46], [36], [-42], [-20], [36], [59], [29], [-55], [-54], [42], [-58], [19], [25], [30], [66], [-81], [0], [48], [-70], [-19], [33], [-22], [47], [93], [82], [-65], [96], [66], [-64], [-54], [51], [64], [-84], [86], [51], [96], [52], [-2], [6], [46], [72], [-15], [-3], [66], [-58], [24], [-72], [11], [83], [50], [81], [108], [75]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1850_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1850_2_a_n();".
// 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_1850_2_a_n(:prec:=1)
    chi := MakeCharacter_1850_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_1850_2_a_n();".
// 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_1850_2_a_n( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1850_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![-2, 1]>,<7,R![0, 1]>],Snew);
    return Vf;
end function;