
// Make newform 1850.2.a.p in Magma, downloaded from the LMFDB on 24 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_p();".
// 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_p();".
// 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], [4], [0], [-2], [0], [5], [-3], [6], [-4], [-1], [-9], [7], [6], [-9], [3], [2], [-2], [-6], [-11], [-1], [0], [-6], [4], [3], [13], [18], [-16], [-18], [16], [-12], [-6], [14], [15], [-4], [13], [7], [3], [-6], [-12], [5], [-9], [4], [-27], [-13], [2], [-2], [9], [-13], [21], [9], [14], [-27], [0], [-6], [-21], [-16], [22], [12], [19], [3], [-32], [15], [22], [-33], [8], [25], [-15], [-19], [-12], [30], [-2], [-14], [20], [15], [-36], [25], [18], [20], [12], [26], [15], [-26], [-7], [-24], [6], [28], [0], [-32], [-12], [24], [16], [-18], [-43], [36], [9], [-33], [28], [-10], [28], [12], [-45], [-24], [2], [40], [3], [-15], [36], [-37], [-32], [25], [-18], [-46], [32], [-21], [4], [27], [6], [0], [32], [37], [39], [48], [-10], [18], [-40], [24], [16], [22], [-34], [6], [50], [-20], [45], [26], [6], [-20], [12], [12], [-28], [33], [34], [12], [56], [-6], [46], [42], [5], [6], [25], [-39], [-32], [30], [37], [-9], [32], [18], [-38], [30], [-33], [-9], [31], [-48], [18], [54], [-13], [28], [-7], [-24], [-24], [8], [-24], [-47], [-4], [6], [29], [3], [52], [50], [-53], [15], [-56], [36], [-18], [-15], [43], [43], [-10], [-30], [58], [-66], [53], [-18], [12], [-21], [14], [28], [-6], [-60], [42], [26], [13], [62], [-12], [36], [-16], [-36], [-42], [20], [22], [0], [16], [-24], [-39], [35], [13], [6], [57], [18], [-37], [50], [-66], [-29], [24], [26], [51], [42], [28], [-3], [-23], [-52], [44], [33], [-44], [12], [-67], [36], [39], [-60], [57], [-4], [4], [-64], [-21], [-27], [-56], [-33], [56], [-69], [-32], [-66], [-18], [56], [36], [12], [-43], [46], [18], [-56], [-32], [-57], [-31], [43], [-48], [-10], [-18], [60], [-50], [-66], [38], [-47], [10], [-13], [-17], [76], [6], [-16], [-37], [-36], [-24], [77], [12], [-19], [-8], [-12], [-14], [57], [35], [-54], [-60], [-3], [-18], [-66], [-62], [-6], [-28], [51], [27], [10], [-14], [-33], [-46], [-63], [-31], [16], [45], [-28], [-60], [-38], [-6], [81], [-6], [46], [-72], [-64], [-60], [-72], [-53], [48], [-28], [13], [-54], [-80], [27], [20], [38], [28], [33], [-66], [14], [-72], [83], [-45], [-88], [18], [29], [21], [-10], [-56], [-29], [81], [18], [-55], [-60], [15], [-19], [34], [36], [-3], [-40], [61], [-42], [-11], [35], [30], [69], [27], [-3], [9], [82], [-15], [-24], [-45], [34], [4], [-84], [-83], [20], [57], [11], [-48], [51], [32], [-38], [-84], [54], [-50], [0], [58], [-24], [-24], [-65], [-93], [-49], [6], [38], [-50], [-101], [45], [-34], [-6], [33], [64], [36], [22], [23], [-63], [50], [-21], [41], [90], [-77], [-48], [30], [56], [22], [3], [52], [96], [-86], [9], [96], [68], [-98], [-66], [18], [76], [39], [81], [-6], [-62], [-96], [72], [61], [57], [-66], [33], [-4], [57]];
    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_p();".
// 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_p(: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_p();".
// 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_p( : 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![-4, 1]>],Snew);
    return Vf;
end function;