
// Make newform 4114.2.a.h in Magma, downloaded from the LMFDB on 21 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4114_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_4114_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_4114_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_4114_a();"
function MakeCharacter_4114_a()
    N := 4114;
    order := 1;
    char_gens := [3027, 2179];
    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_4114_a_Hecke(Kf)
    return MakeCharacter_4114_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], [0, 1], [0, -1], [-2, 3], [0, 0], [1, -2], [1, 0], [4, -4], [-5, 2], [8, -2], [-5, 4], [5, -4], [6, -6], [5, 1], [2, -7], [-6, 1], [-9, -2], [-9, 8], [10, -5], [-10, 9], [7, -1], [-9, 2], [-2, -5], [7, -12], [3, -4], [-11, 4], [-9, 4], [1, 6], [-15, -1], [-3, 8], [4, -7], [-6, 2], [1, -10], [5, 6], [-12, 13], [-5, 16], [-13, 5], [-4, 4], [-12, 0], [12, -2], [-7, -6], [1, -2], [5, -15], [9, -14], [4, -13], [0, 13], [1, -15], [24, -6], [-1, 2], [-12, 9], [-11, 10], [15, -10], [-9, 8], [-14, 4], [-8, 12], [-10, -3], [0, -16], [-4, 3], [9, -18], [-16, 4], [0, 15], [-1, -16], [-4, 8], [5, 15], [10, 3], [-8, 10], [-2, 19], [0, 9], [-13, 3], [15, 4], [-10, -13], [6, -6], [-18, 6], [10, -20], [11, 6], [-4, 17], [-23, 12], [-16, 21], [-4, 7], [-1, 16], [-23, 10], [-5, -11], [-8, 8], [2, -3], [-27, 5], [-15, 12], [-15, -7], [2, -26], [7, -22], [16, -25], [1, -1], [-1, 22], [-34, 10], [-29, 8], [-1, 20], [-21, 7], [15, 12], [19, -9], [10, 8], [16, -8], [-21, 18], [-4, 17], [13, -10], [34, -6], [30, -10], [33, -4], [-31, 1], [-6, -19], [-29, 9], [-13, 0], [-20, 31], [-5, -15], [-24, 1], [-19, -2], [5, -34], [9, 21], [13, -27], [-29, -1], [7, 2], [-2, 12], [12, -30], [-14, 9], [6, 28], [13, -14], [25, 12], [15, -39], [15, 10], [-22, -10], [16, -20], [37, -14], [16, -20], [10, -1], [19, 0], [-19, 10], [6, 19], [-12, 24], [13, 15], [-22, 1], [-15, 16], [33, 11], [-1, -4], [15, 20], [23, 3], [22, 7], [-28, 10], [10, 6], [20, 23], [-4, 5], [25, 14], [-35, 2], [18, -9], [-42, 24], [31, 10], [3, 3], [22, -26], [-26, -16], [22, -25], [-28, -7], [16, -16], [35, 2], [3, -6], [-21, 40], [-17, 9], [19, 24], [-33, 0], [7, 6], [54, -12], [-31, 10], [-23, -9], [13, 16], [9, -8], [-41, 14], [8, -12], [10, 26], [9, -12], [38, 0], [3, 29], [-5, 1], [0, -20], [34, -33], [-4, 28], [-25, 18], [-22, 22], [-22, 20], [-5, 22], [-49, 0], [22, -8], [14, 18], [-12, 27], [-33, 32], [45, 0], [-2, 18], [-1, 33], [-33, 1], [20, -16], [-25, 23], [41, 9], [25, -38], [-15, -22], [-14, 11], [29, -6], [-17, -30], [-17, 26], [33, 6], [20, -30], [1, 13], [-17, 12], [47, 9], [-28, 14], [-11, 1], [46, -7], [-8, 27], [11, -8], [22, -1], [-2, 2], [32, 2], [41, 5], [-23, 24], [-13, 0], [-38, 25], [-3, 6], [-30, -22], [-35, -10], [-3, 24], [22, 6], [-2, -15], [-17, 42], [-6, -11], [24, -39], [-38, 51], [24, -15], [-53, 12], [-9, -12], [2, 12], [13, 36], [-50, -6], [7, -41], [27, 19], [-23, 10], [50, -31], [-65, 0], [-8, 23], [-31, 11], [0, -36], [37, -14], [25, -18], [24, -34], [11, 25], [-43, 2], [11, -32], [26, -43], [46, -37], [-6, 8], [-69, 6], [-15, -2], [63, -22], [24, -35], [5, 21], [1, -36], [55, -6], [-18, -24], [13, -22], [26, 10], [-19, 32], [-4, -42], [-11, 25], [44, -34], [10, -47], [-42, -4], [43, 2], [-56, 13], [13, -44], [56, -23], [-58, 29], [-42, 41], [-60, 4], [-24, 12], [-53, -9], [7, -42], [-13, -8], [-17, 25], [-31, -12], [6, -31], [1, -17], [26, -62], [40, -33], [37, 7], [-17, -29], [15, -10], [44, 19], [28, 6], [7, 1], [-6, -14], [-42, 57], [-38, 31], [-32, -9], [9, 18], [6, -8], [18, -35], [-55, -6], [-1, 8], [-3, 32], [-32, -32], [-49, -11], [-52, 38], [35, 28], [-12, 6], [-9, 30], [6, 16], [62, -6], [-9, -8], [-71, -11], [18, 9], [-36, 55], [32, -58], [-46, -2], [22, -61], [13, 4], [26, -54], [37, -69], [-10, -34], [-13, -20], [-34, 42], [47, -50], [-35, 55], [38, -17], [-51, 24], [-20, -8], [-22, 17], [-31, 52], [-2, 36], [5, 35], [20, -32], [-28, 65], [-2, 35], [58, -12], [-22, 32], [-45, 40], [-17, -15], [-31, 0], [44, -9], [-2, 8], [-23, -6], [2, -9], [-73, 18], [-13, 20], [4, -29], [-4, -3], [-24, 52], [-46, -22], [-67, -6], [-6, -52], [-63, 4], [41, 29], [-14, 44], [37, -17], [78, -19], [-8, 27], [33, -22], [81, -18], [-40, 59], [50, 14], [-50, 6], [-33, -37], [53, -67], [-28, -31], [-14, 3], [-59, 25], [13, -58], [-8, -39], [48, -64], [14, -32], [-52, 21], [72, -10], [5, -9], [-9, 16], [43, -59], [8, -9], [23, -35], [3, -27], [77, -9], [41, 30], [19, -16], [-4, 12], [19, 42], [-52, 13], [-77, 0], [-6, 63], [-33, -24], [55, 8], [-32, 58], [29, -7], [-53, 1], [-18, 14], [-17, -14], [58, -33], [32, -71], [-36, 22], [-69, 37], [-14, 34], [-53, 49], [-38, 54], [-17, -26], [-49, 2], [-13, -26], [-3, -44], [-44, 22], [77, -42], [30, 18], [-63, 9], [-14, 20], [-44, 19], [45, -20], [-41, 36], [66, 13], [10, 30], [-34, 25], [-19, 4], [-10, 56], [-13, -34], [-6, -23], [63, -12], [60, 7], [74, -25], [63, -62], [27, -26], [-59, 4], [-17, -10], [33, 38], [4, -51]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4114_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_4114_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_4114_2_a_h(:prec:=2)
    chi := MakeCharacter_4114_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_4114_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_4114_2_a_h( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4114_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![-1, -1, 1]>,<5,R![-1, 1, 1]>,<7,R![-11, 1, 1]>],Snew);
    return Vf;
end function;