
// Make newform 1849.2.a.h in Magma, downloaded from the LMFDB on 25 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_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_1849_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
        poly := [-1, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
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], [2, -1], [0, 2], [3, -2], [-3, 1], [2, 1], [3, -5], [0, -2], [5, 1], [-3, 0], [0, 0], [3, -3], [7, -4], [0, 0], [3, 3], [-2, -1], [2, -5], [-1, 3], [-2, 3], [4, -11], [0, 3], [-2, -1], [8, -13], [0, -3], [-8, 2], [5, 2], [-9, 12], [10, 4], [-12, 3], [15, -1], [-6, 14], [-5, 8], [0, 6], [-9, -6], [-9, 0], [7, -3], [7, 3], [-14, 0], [13, -2], [-5, -2], [-1, 14], [-20, -1], [17, 4], [-1, -6], [2, 8], [3, 8], [8, -2], [-4, -2], [-1, -4], [-6, -6], [-10, 13], [-12, -6], [5, 15], [-23, 10], [-9, -9], [-17, 17], [6, -9], [12, -3], [10, -4], [5, -4], [12, 2], [-9, 5], [-9, 23], [-15, 24], [-2, 18], [16, -28], [3, -4], [28, 0], [-7, 5], [-17, 6], [-16, 2], [-14, -14], [13, -10], [-6, 0], [-15, 20], [-27, 18], [19, -26], [25, -12], [-18, 12], [-12, -5], [9, -2], [-1, -14], [-18, 11], [31, -6], [3, 3], [0, -4], [18, -24], [12, -6], [-18, 22], [15, -24], [20, 8], [-20, -10], [-3, -7], [-13, 14], [-12, 3], [11, -4], [15, -6], [14, -13], [-21, 24], [-33, 8], [21, 0], [-33, 9], [3, 7], [-21, 8], [37, -10], [14, -23], [-4, 19], [28, -4], [35, -2], [-22, 12], [22, -3], [12, 11], [6, -6], [-21, 15], [24, -5], [-17, -10], [15, 9], [26, 7], [23, -16], [-17, 20], [-23, 33], [1, 24], [33, 2], [0, 18], [-25, -15], [-4, 17], [19, -16], [-4, 11], [5, -21], [-6, 30], [-31, 30], [-21, 20], [25, -21], [18, -21], [2, 5], [10, -16], [-2, 22], [-5, -25], [-7, 2], [-7, -19], [-27, 33], [-4, -7], [31, -34], [3, -10], [-9, 31], [-16, 16], [23, 19], [-30, 10], [42, -12], [1, 17], [1, 14], [35, 2], [2, -18], [27, -42], [-18, 6], [30, -21], [9, -33], [-34, 2], [51, -7], [-25, 2], [13, -20], [29, -16], [14, -39], [-5, 20], [40, -2], [8, -4], [17, 10], [4, -25], [40, -36], [16, 5], [26, 8], [-39, -6], [35, -4], [-31, 33], [17, -39], [39, -3], [-7, -17], [-57, 3], [3, 14], [-7, -14], [-7, -11], [-28, 5], [10, -30], [-39, -6], [21, -14], [-26, 43], [16, -30], [10, 23], [-23, -27], [36, 12], [-22, 10], [-5, -19], [1, 24], [-3, 12], [-9, 10], [-39, 18], [-2, 23], [2, 7], [-24, 18], [22, -8], [6, 21], [31, -12], [31, -43], [0, 37], [-53, 22], [33, 2], [-22, -18], [34, -16], [-26, 31], [-39, -5], [-6, 33], [41, -16], [-25, -5], [-36, -14], [4, 20], [-20, -30], [27, -29], [43, -2], [-24, -7], [12, -18], [-41, -18], [15, -20], [4, -29], [-54, 1], [29, 8], [15, -25], [3, -42], [24, 19], [15, -10], [23, -16], [7, -24], [-34, 10], [2, -9], [-33, -12], [-18, -5], [-6, -17], [-14, 8], [21, -31], [-28, 35], [-29, 1], [-24, -15], [-3, 36], [-20, 9], [44, -6], [7, 10], [19, -10], [68, -12], [4, -34], [11, -32], [26, -19], [45, -36], [-8, -4], [3, 12], [-61, 27], [25, -13], [18, -35], [-38, -15], [-51, 0], [-17, 37], [-15, 34], [-28, 46], [-18, 18], [-1, -33], [-22, -30], [-13, -28], [21, -21], [-1, -5], [9, 42], [-22, -18], [15, 10], [9, 18], [-29, 45], [-3, 6], [40, -25], [-40, 51], [-25, 34], [-28, -2], [-35, 44], [42, -28], [33, -45], [-5, 14], [-39, 12], [-12, -9], [5, -12], [6, -40], [-10, 38], [6, -30], [-42, 42], [-28, 51], [-53, -11], [45, -36], [21, 27], [36, -4], [-33, 14], [4, -36], [-49, -11], [33, -57], [-54, 46], [33, 12], [10, -1], [-20, -6], [-30, -6], [-64, 16], [53, -20], [-36, -21], [55, -9], [-19, -16], [-53, 5], [-23, 22], [-11, 12], [42, 4], [-13, 53], [-6, 12], [-13, -27], [-44, 58], [-48, 46], [-15, 8], [-57, 23], [-45, 17], [6, 21], [-10, 18], [6, 9], [-42, 27], [24, -18], [3, 14], [-21, 48], [-11, -27], [48, -8], [50, -7], [-37, 14], [-52, -12], [-15, -36], [-40, 54], [-17, -14], [54, 6], [48, 13], [33, 6], [31, -35], [-24, -15], [18, 16], [-6, 18], [-40, 29], [76, -23], [-28, -18], [3, -27], [-48, 14], [-54, 20], [-56, 47], [10, 19], [-1, 23], [9, -60], [46, -6], [-41, 7], [-30, 21], [15, 24], [-68, 20], [48, -20], [26, -56], [-47, 4], [-38, 4], [-18, 49], [51, 17], [32, 31], [9, -5], [13, 18], [-13, 12], [18, -60], [3, 46], [5, 0], [57, -52], [-33, 33], [-47, 62], [46, -47], [-23, -45], [9, -66], [44, -28], [12, 39], [-9, -24], [-60, 8], [-9, 44], [-57, -19], [36, -13], [13, -10], [-62, 35], [0, -18], [32, -19], [-45, 6], [-11, -12], [-37, 16], [45, 9], [42, -24], [-14, 43], [-24, -30], [19, -12], [45, -9], [33, -10], [-56, -25], [54, 4], [21, -10], [16, 34], [55, 12], [27, -69], [-3, -45], [21, 0], [6, 21], [13, -54], [12, -48], [-14, -44], [1, -16], [-76, 23], [-60, 12], [18, -46], [-6, -30], [30, 3], [63, -12], [48, -48], [32, 29], [29, 30], [36, -22], [38, -34], [31, -4], [-39, -1], [9, -17], [28, -2], [67, 12], [-22, 61], [3, -61], [30, 28], [-16, -6], [15, 27]];
    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_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_1849_2_a_h(:prec:=2)
    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_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_1849_2_a_h( : 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, -3, 1]>],Snew);
    return Vf;
end function;