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