
// Make newform 1849.2.a.j 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_j();".
// 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_j();".
// 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 := [[0, -1, 0], [0, 0, 1], [-1, 0, -1], [-1, 1, -2], [-1, 2, -3], [-1, -1, -3], [0, -2, 0], [5, -1, 1], [1, -4, 2], [6, 0, 0], [7, -3, 7], [3, -3, -1], [8, -2, 0], [0, 0, 0], [-8, 2, 1], [2, 3, 3], [-4, 3, -1], [-4, 4, -2], [4, -2, -2], [4, 4, -6], [3, 1, -5], [-2, -1, 3], [-3, -1, -6], [3, -2, 9], [-8, 5, 2], [4, 7, -3], [-1, 4, -2], [2, 1, 1], [8, -6, 4], [13, -9, 12], [-9, 0, -9], [10, -6, -1], [2, -7, -4], [-5, 3, -6], [4, 1, 0], [6, -13, 8], [-11, 5, -9], [11, -1, 4], [11, -5, 6], [-3, 5, -1], [10, -12, 8], [16, -9, 2], [2, 10, -4], [5, 2, 1], [5, 4, -1], [8, 7, 3], [-2, 3, -11], [12, -11, 4], [6, -3, 10], [-1, -17, 11], [16, -12, 12], [1, 9, -8], [-1, 2, -2], [-6, -8, -6], [1, 3, -3], [10, 3, 7], [7, 17, -6], [-13, 1, -2], [0, 11, -1], [-12, 1, 6], [22, 0, -4], [2, -10, 4], [14, -8, 13], [-1, -7, 1], [10, -12, 14], [13, 8, -1], [1, -10, -1], [-1, 7, -15], [-4, 13, -13], [-25, 7, -12], [-17, -2, -2], [-18, 14, -6], [10, -11, -4], [-24, 5, -6], [5, -20, 4], [-19, 12, -12], [-11, 22, -7], [4, -2, 7], [-8, 10, -8], [5, 8, -7], [15, 2, -9], [-17, 4, 0], [24, 6, -6], [-9, 1, 8], [10, -3, 4], [-2, 6, 12], [16, -7, 12], [-29, -6, 0], [15, -10, -13], [5, -9, -3], [4, -2, -21], [3, -23, 7], [0, -4, 7], [10, -9, -18], [-4, 20, -9], [-25, 16, -6], [-15, 0, 7], [4, 11, -19], [-14, 17, 4], [2, -3, 14], [-16, 4, -20], [27, -23, 32], [-11, 6, -5], [15, -18, 13], [2, 9, 15], [-4, 2, 21], [14, -13, 0], [-10, 18, -11], [2, 19, -14], [4, 6, -1], [4, 0, 21], [-7, -4, 15], [-19, 3, -12], [0, 6, -11], [-5, -7, 2], [7, -21, 22], [-14, 18, -14], [27, -19, 19], [-6, 4, -28], [-20, 2, -15], [10, 6, -15], [-29, 5, 2], [6, 13, 0], [-5, -26, 0], [9, -14, 12], [15, 14, -5], [22, -1, 12], [8, 14, -7], [20, 5, 8], [6, 2, -3], [-5, 16, -1], [32, 3, 6], [-12, 9, -3], [18, -13, 13], [14, -12, -1], [23, -3, 17], [1, -37, 17], [-8, 11, -18], [-24, 6, 11], [-13, 27, -20], [36, -13, 32], [13, -7, 11], [26, 2, 13], [-40, 14, -19], [-29, 22, -24], [-11, 8, 2], [-13, 8, 21], [-4, 11, 1], [-27, 31, -27], [12, -32, -7], [-11, 0, -2], [9, -26, 2], [16, 5, 23], [24, -10, 3], [-19, -11, 8], [3, -11, 2], [-11, 14, 23], [-2, -9, 30], [-10, 2, -23], [22, -31, 20], [-7, 8, 16], [-9, 27, 10], [-3, -1, 12], [18, -10, 6], [14, -18, 13], [5, 23, -20], [21, -26, -20], [-22, 1, 17], [-14, -14, 9], [-33, -20, 10], [6, 13, -10], [-16, 20, 0], [30, -2, 2], [-7, -9, -6], [-4, -2, -10], [34, 8, 1], [-1, -21, -1], [9, -23, 10], [16, -3, 16], [-8, 12, 0], [-18, -14, -1], [-3, -23, -5], [12, -15, 23], [8, 23, -8], [16, -19, 16], [26, 2, 17], [5, 17, 2], [8, -19, 17], [8, 10, 21], [28, 10, -10], [17, 12, 0], [15, 17, -17], [3, 20, -18], [-20, 28, -12], [10, -1, 11], [-17, -4, 6], [-23, -3, -26], [-13, 46, -12], [12, 7, -7], [16, -23, 0], [-20, 4, -2], [-4, 30, -18], [22, -4, -5], [-26, 10, 27], [-16, 32, -10], [-5, 18, 13], [43, -12, 16], [14, -24, 38], [43, -1, 15], [30, -21, 26], [-20, -14, 22], [-28, -11, -16], [5, -31, 28], [-4, 14, 6], [21, -1, 15], [2, -9, -38], [-2, -37, 22], [10, 12, 21], [-28, 21, -1], [21, -31, 6], [-7, -23, 14], [13, 8, 7], [4, 32, -12], [-7, 53, -20], [-4, 17, 32], [34, -14, 50], [48, -8, 10], [60, 6, -3], [43, -3, 17], [22, -44, 19], [-35, 2, 0], [2, -14, 19], [-40, 7, 2], [19, 27, -19], [-26, 8, 13], [8, -8, 14], [-6, 15, -30], [-25, 11, 6], [-34, -3, -20], [30, -31, 48], [-23, -28, 20], [-15, 25, 29], [1, 12, 15], [13, 18, -20], [-7, 24, 18], [4, -35, 18], [-46, -2, 5], [-13, -5, 4], [-17, 25, 17], [8, -33, 34], [-24, 4, 14], [-56, 18, -28], [27, -7, 33], [-4, -38, 6], [-8, 7, -26], [-6, -21, 0], [-15, -29, 6], [24, -7, -27], [-33, 18, -39], [-11, 45, -19], [14, 18, -18], [7, 7, -5], [-27, 22, -3], [36, -22, 3], [-24, -4, 10], [-1, 21, 7], [-15, 11, -37], [-6, -35, -9], [-11, -20, -12], [-19, -11, 0], [-7, 39, -22], [21, 14, 5], [-40, 3, 0], [-6, 40, -6], [19, 7, -9], [40, -38, 51], [-1, -6, -24], [-2, -42, 11], [-36, -5, -3], [48, 9, -2], [-32, -18, 19], [-15, -23, 26], [-9, 31, 15], [-17, 25, -39], [28, -44, 5], [51, -17, 44], [-19, 21, 31], [-31, -10, -6], [-37, -20, 3], [-50, 1, 7], [29, -21, 15], [-31, 32, 14], [45, -21, 3], [46, 15, -11], [4, 20, -19], [18, -23, -12], [-4, -6, 26], [24, -14, 0], [-28, 43, -12], [13, -37, 44], [30, -38, 43], [-27, 16, 1], [-12, -16, -10], [47, 8, 7], [19, 28, -15], [-42, 20, -33], [-26, -7, -21], [22, -2, -15], [-44, 8, -23], [-20, 13, -3], [-4, 19, -39], [-42, -9, -2], [-12, 14, -14], [-31, 33, -45], [7, -15, 31], [-10, -8, -3], [19, -45, 29], [-9, -28, 9], [51, 2, -1], [17, -18, 22], [-3, 53, -14], [43, 26, -4], [-38, -1, 1], [-26, 17, -22], [4, -9, 25], [8, -6, 0], [10, -5, 9], [-39, 21, -5], [-16, -15, -6], [20, -10, 25], [-43, 41, -28], [5, -27, 1], [42, 1, 22], [-30, 42, -17], [-2, 31, -4], [39, -8, 5], [22, 3, -8], [-80, 13, -17], [31, -31, -22], [2, 32, -41], [-13, 30, -17], [-8, 39, -2], [-16, -33, -7], [23, -18, 36], [0, 6, 17], [-21, 61, -32], [10, 25, -28], [-40, 9, -23], [62, -26, 53], [-14, 37, -42], [-79, 2, -2], [-8, 30, -10], [-6, 23, -13], [-29, 34, -25], [50, -3, -9], [37, -56, 36], [58, -10, -15], [30, 14, 12], [-15, -8, -1], [14, -22, 13], [-26, -23, 23], [63, -30, 17], [15, -27, 0], [-46, 18, -25], [-40, 14, -23], [-8, -1, 13], [21, -30, 5], [-41, 3, -11], [22, -44, -31], [34, -42, 15], [42, 10, -10], [-6, 6, -33], [67, -5, 11], [0, 27, -3], [-25, -18, -14], [17, 18, 36], [57, -41, 76], [-14, 24, -41], [22, -5, -32], [47, -11, 41], [50, 22, -5], [25, 38, -22], [32, -64, 25], [-10, 1, 38], [-7, -28, -16], [-31, 41, -52], [17, -26, 27], [6, 27, -30], [41, -24, 62], [-50, 41, -10], [41, -13, -10], [7, 13, -4], [-9, -41, 23], [73, -9, -1], [16, -25, 0], [-5, 49, 7], [3, -1, -12], [-1, -65, 17], [7, 12, -24], [47, -42, 53], [34, -41, 19], [-24, 42, -6], [10, -10, -17], [-28, -41, 14], [36, 5, -4], [-42, 4, -45], [-9, 23, 13], [-15, 21, -21], [-15, 52, -45], [-65, 18, -4], [12, -1, -5], [5, -7, 31], [-37, 9, -38], [-40, 32, 29], [48, -22, 41], [10, -43, 38], [21, -9, -42], [74, -14, 21], [-49, -20, 15], [-36, 22, 3], [-51, 3, -9], [-52, 63, -62], [-24, 8, 31], [-11, -51, 27], [11, 41, -30], [1, -23, 22], [21, -11, -26], [33, -13, 24], [18, -43, 44], [8, 16, 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_j();".
// 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_j(: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_j();".
// 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_j( : 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, -2, 1, 1]>],Snew);
    return Vf;
end function;