
// Make newform 1850.2.a.i in Magma, downloaded from the LMFDB on 17 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_i();".
// 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_i();".
// 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], [-1], [3], [0], [-3], [-6], [-2], [-3], [3], [1], [3], [1], [-4], [-13], [0], [-15], [0], [-2], [0], [-8], [4], [-18], [7], [-10], [-8], [-2], [-3], [7], [4], [-12], [-8], [3], [18], [16], [3], [5], [-12], [-9], [24], [2], [21], [10], [18], [8], [-3], [-23], [-13], [-6], [-4], [9], [24], [6], [22], [1], [-10], [-10], [20], [-24], [20], [-3], [-34], [25], [-26], [-21], [30], [-12], [28], [-22], [-19], [16], [-13], [-22], [16], [12], [15], [-18], [34], [0], [12], [34], [-31], [22], [37], [34], [-10], [-9], [-1], [-12], [37], [-8], [-34], [-20], [-22], [24], [36], [-23], [44], [-2], [-23], [40], [11], [10], [-31], [-30], [-35], [-12], [0], [21], [-22], [-11], [-36], [-37], [-41], [-33], [-11], [0], [-30], [36], [-13], [34], [-34], [31], [5], [-22], [-19], [-18], [-52], [41], [27], [-3], [-22], [50], [-21], [8], [41], [10], [26], [34], [-16], [8], [-24], [11], [-39], [36], [-22], [-33], [-36], [-33], [43], [-9], [-29], [33], [12], [0], [32], [-3], [14], [-6], [-9], [-36], [-48], [9], [-35], [27], [33], [12], [41], [-6], [54], [-47], [-3], [24], [2], [54], [2], [18], [19], [-8], [-38], [-12], [-2], [-57], [-33], [38], [1], [29], [-60], [-40], [-6], [-8], [14], [-28], [-66], [14], [6], [-8], [42], [-34], [46], [-60], [-13], [-7], [5], [48], [-38], [-4], [38], [-27], [-37], [33], [-4], [42], [-17], [63], [48], [44], [-8], [45], [-18], [-24], [-1], [48], [36], [38], [24], [-54], [-37], [50], [31], [-56], [8], [-18], [14], [-7], [-74], [12], [24], [-48], [-39], [56], [64], [-45], [6], [-47], [-22], [28], [13], [16], [-10], [42], [-21], [-20], [34], [-3], [-56], [-78], [71], [17], [-46], [72], [38], [-77], [-14], [37], [60], [12], [16], [26], [-50], [43], [10], [-25], [74], [9], [2], [-2], [17], [60], [80], [57], [-79], [-16], [-4], [18], [-53], [-77], [-80], [-18], [27], [20], [-44], [43], [27], [-2], [-26], [30], [-78], [58], [-1], [-78], [68], [-12], [-54], [-3], [-33], [-18], [58], [-8], [72], [10], [-33], [-48], [12], [88], [33], [32], [80], [24], [28], [14], [-85], [-79], [44], [-10], [-83], [-33], [-24], [-55], [-18], [22], [7], [-44], [-30], [32], [4], [-76], [-46], [-72], [-54], [18], [-30], [47], [24], [64], [-74], [-38], [-23], [-41], [-35], [52], [-59], [16], [-4], [0], [-41], [-26], [54], [-12], [92], [-54], [-2], [-40], [78], [-47], [36], [-12], [-38], [56], [-14], [-87], [-24], [85], [-29], [-12], [20], [-56], [30], [-8], [33], [-54], [34], [-30], [-70], [-31], [-92], [-56], [-13], [0], [31], [-88], [-70], [43], [-66], [-74], [20], [-23], [-3], [32], [48], [-65], [66], [-10], [-90], [-56], [52], [85], [-4], [15], [-28], [-63], [9], [102], [-3], [-42], [-20], [-55], [106], [54], [-2], [58], [-8], [39], [54], [-9], [24], [-47], [-32], [-59]];
    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_i();".
// 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_i(: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_i();".
// 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_i( : 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![1, 1]>],Snew);
    return Vf;
end function;