
// Make newform 1849.2.a.a in Magma, downloaded from the LMFDB on 16 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1849_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_1849_2_a_a();".
// 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_1849_2_a_a();".
// 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_1849_a();"
function MakeCharacter_1849_a()
    N := 1849;
    order := 1;
    char_gens := [3];
    v := [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_1849_a_Hecke(Kf)
    return MakeCharacter_1849_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], [1], [-3], [0], [-5], [3], [-1], [-7], [-3], [5], [9], [-10], [0], [-8], [-5], [12], [13], [-3], [1], [-11], [-5], [9], [1], [-2], [-9], [7], [12], [7], [2], [16], [4], [18], [13], [21], [8], [1], [1], [-3], [-6], [1], [7], [19], [6], [11], [-8], [-8], [-20], [7], [-9], [9], [25], [-15], [7], [-6], [-21], [14], [23], [-7], [-17], [3], [-14], [5], [-3], [17], [-18], [-19], [3], [-37], [1], [-25], [19], [13], [-11], [20], [-8], [6], [-21], [-37], [18], [28], [37], [0], [-27], [-13], [-37], [21], [-6], [3], [23], [-21], [-15], [-9], [9], [17], [9], [27], [-35], [21], [31], [1], [-6], [4], [-17], [-13], [5], [13], [1], [-31], [-26], [43], [-30], [3], [-21], [9], [18], [12], [12], [50], [23], [-14], [-47], [14], [9], [-5], [7], [26], [-31], [32], [-26], [4], [-21], [27], [5], [37], [-33], [-26], [-5], [-21], [-34], [47], [10], [-31], [33], [-11], [52], [-5], [35], [-4], [27], [23], [46], [13], [16], [20], [52], [20], [57], [-35], [-9], [-12], [41], [-8], [35], [-41], [51], [-8], [14], [-3], [11], [-15], [38], [8], [58], [-55], [15], [-33], [37], [-15], [-59], [-5], [52], [25], [18], [-51], [33], [-34], [-21], [46], [27], [-42], [28], [-7], [13], [21], [10], [-13], [-17], [15], [63], [-21], [20], [-43], [26], [3], [-31], [-56], [-49], [22], [36], [-7], [18], [-45], [27], [9], [-5], [-45], [57], [-15], [3], [5], [5], [-3], [-16], [-72], [-5], [-53], [-69], [-55], [20], [25], [-59], [35], [-19], [-68], [3], [50], [3], [7], [29], [-39], [-75], [37], [26], [-63], [64], [7], [-63], [5], [24], [7], [55], [-56], [-35], [-34], [33], [57], [-1], [29], [-53], [5], [51], [10], [58], [-27], [28], [-46], [30], [-61], [15], [-62], [-44], [5], [-40], [43], [47], [1], [-3], [43], [83], [7], [-53], [-4], [49], [1], [24], [71], [29], [45], [39], [27], [67], [-66], [44], [-14], [3], [-8], [-81], [-16], [47], [27], [-43], [24], [43], [-69], [-22], [53], [22], [-7], [10], [24], [-39], [35], [-29], [85], [-25], [-16], [32], [-69], [-6], [-51], [-7], [9], [55], [45], [38], [53], [29], [-41], [-35], [37], [62], [-33], [21], [47], [-3], [-59], [41], [6], [56], [63], [37], [-3], [8], [-78], [39], [21], [55], [-3], [-62], [49], [-7], [10], [-7], [67], [37], [73], [63], [15], [-41], [-31], [-23], [60], [-36], [72], [-42], [-7], [-49], [50], [-9], [-84], [9], [-15], [27], [41], [60], [24], [-61], [-51], [-7], [-34], [-13], [-7], [-67], [-29], [25], [63], [58], [91], [52], [-85], [90], [-89], [8], [8], [38], [-91], [21], [-4], [38], [71], [-30], [27], [91], [69], [76], [78], [-1], [-12], [-7], [-29], [-10], [-1], [-101], [-23], [-21], [24], [-31], [-30], [-24], [-11], [-17], [103], [-93], [33], [21], [-20], [-106], [-84], [16]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1849_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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