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