
// Make newform 4002.2.a.ba in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4002_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_4002_2_a_ba();".
// 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_4002_2_a_ba();".
// 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 := [14, 8, -7, -2, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, -4, -1, 1], [-5, 0, 1, 0], [4, 2, -1, 0]];
    Rf_basisdens := [1, 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_4002_a();"
function MakeCharacter_4002_a()
    N := 4002;
    order := 1;
    char_gens := [2669, 3133, 553];
    v := [1, 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_4002_a_Hecke(Kf)
    return MakeCharacter_4002_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, 0], [-1, 0, 0, 0], [0, 0, 0, 1], [1, 0, -1, 0], [1, -1, 1, -1], [-1, -1, -1, 1], [0, 0, 2, -1], [1, -2, 1, 0], [1, 0, 0, 0], [-1, 0, 0, 0], [1, 1, -1, 1], [2, -1, -2, -1], [2, -1, 0, -1], [5, 0, 1, -2], [3, 0, 1, 0], [-1, 2, -3, 1], [-7, 0, -1, 2], [8, 2, 0, -2], [5, 0, 1, 3], [5, 1, -1, 3], [2, 0, 4, 0], [0, 1, -4, 2], [5, -2, 1, 1], [-9, 2, -5, 1], [-9, 2, -1, 1], [-10, 0, 0, 0], [7, 0, 5, -4], [3, 3, -1, 2], [1, 2, -1, -1], [6, 2, 0, -3], [8, -2, 0, 2], [-9, 5, -3, 1], [7, 2, -1, -5], [-10, 0, -2, 0], [0, -4, 0, -1], [6, 3, -2, -3], [8, -1, 0, -1], [4, -3, 0, -1], [-2, 4, 2, 0], [0, -1, -2, 3], [-12, 4, -4, 2], [-2, -3, 2, 0], [1, -1, -1, -4], [12, 2, -2, -4], [12, -1, 2, -1], [6, 0, -2, -4], [0, -1, 4, 1], [-6, 0, 2, -2], [7, 1, -1, 0], [8, -1, 4, 3], [10, 2, -4, 2], [16, 0, 4, -2], [4, 1, -2, -1], [4, -2, 2, 4], [-1, -5, 3, -5], [9, 1, 3, -8], [5, -3, 1, -1], [12, 0, 4, 4], [-1, -7, 3, -5], [-8, 2, 2, -2], [10, -3, 8, 0], [12, 4, 0, -6], [0, 0, 0, 4], [15, 2, -3, -2], [10, -1, 8, -3], [-13, -3, 3, 7], [-8, -1, -8, 9], [-9, -2, 3, 7], [-25, 0, -3, 4], [6, 6, -4, 0], [16, 2, -2, -2], [11, 5, -3, -4], [-4, 1, -8, -6], [1, 10, -5, 3], [-12, -2, 2, 2], [5, 5, -5, -5], [0, -4, 8, 4], [-11, -5, 1, 9], [16, 0, 6, -6], [-19, 5, -3, 3], [1, -1, 1, 4], [-2, -2, 6, -8], [11, 1, 1, -1], [4, 3, 2, 4], [26, -1, 2, -3], [2, -6, 6, 2], [-16, -5, 2, 3], [12, -7, 6, 1], [-5, 1, 3, -1], [-4, 4, -8, 2], [-21, -1, -5, 7], [7, -2, 1, -3], [22, 5, 2, -11], [13, 1, -1, -3], [11, -1, 5, -3], [-13, -7, 5, -2], [-2, -1, 8, 3], [13, -9, 9, -3], [11, 0, -5, 7], [7, -2, 3, 6], [-8, 2, 4, -2], [2, 4, -6, 11], [14, -2, -4, 0], [4, -4, -2, -3], [-4, 5, 2, 2], [3, 1, -5, 1], [3, 0, -3, 2], [2, -2, -4, 2], [8, -4, 8, -10], [0, 0, -10, 0], [-13, 1, -11, 11], [11, -2, -3, -1], [-12, 2, 2, -7], [7, 2, 7, 2], [7, 2, 5, -6], [-20, -2, -2, -1], [34, -7, 16, -6], [25, 1, -1, 1], [2, 6, 8, -4], [-4, 0, -6, -8], [1, -6, 7, -1], [-11, 2, -15, 4], [-4, 0, 0, -6], [5, -4, 3, -10], [-25, -1, -3, -5], [8, 10, -4, -1], [-14, 5, 2, -2], [-3, -3, -1, 3], [-5, 4, -7, -1], [-2, 2, -10, 2], [14, 6, 2, -10], [-13, -5, 1, -10], [15, 4, -7, -1], [22, -9, 14, -5], [-15, 9, -3, 5], [-14, 5, 4, 2], [29, -1, 11, -11], [-16, -1, -6, 4], [11, 3, 5, -15], [-4, 0, -10, -2], [0, 12, -4, 0], [2, 2, -4, -4], [-9, 7, -7, 5], [-24, 0, -10, 12], [-31, 2, 5, 0], [-25, 5, -3, 2], [-5, 0, -13, 4], [-1, 9, -9, 9], [-4, 9, -4, -5], [-3, 5, -9, 15], [9, -1, 9, 3], [-11, -8, -3, 7], [7, -5, 13, -1], [-18, 4, -14, 12], [-24, 2, -6, -6], [-1, 2, -11, 9], [-1, -12, 9, 4], [-22, -8, 4, -8], [20, -1, 2, 5], [-1, -10, 5, -3], [3, 1, 1, 5], [20, 2, 2, 0], [5, 3, 3, -3], [-17, 3, -5, 1], [-5, 1, 3, 1], [-17, 2, -11, 7], [6, -1, 2, -9], [21, -4, 1, -2], [-18, 5, -4, 7], [-17, -5, -9, 11], [5, 6, -7, 7], [-4, -8, 6, 0], [-6, -2, 6, 0], [8, -1, 2, -2], [2, 1, -2, 1], [5, -1, 5, -13], [-20, -2, -4, 0], [14, -4, 12, 2], [26, 1, -2, -4], [-25, -6, 5, -9], [-29, -3, -3, 1], [16, -6, 14, -12], [-15, 1, -7, -11], [-20, -8, 6, 0], [-23, 7, 3, 5], [-15, 0, -9, 3], [-21, 6, 7, 0], [-3, -16, 1, 2], [43, -3, 3, 1], [-14, -4, 6, -4], [-1, -6, -13, 8], [35, 3, 1, 3], [0, 0, -14, 14], [15, 13, 3, -9], [21, -3, 9, -7], [-17, -1, 3, -3], [1, -6, 9, 9], [-14, 1, 2, -10], [-18, 6, 4, -4], [18, 12, 2, -10], [0, 8, -8, 10], [-14, 13, 6, -3], [-13, 4, -1, -8], [13, -2, 13, -1], [-19, 11, -11, -3], [2, -5, 12, -15], [24, 7, 4, -6], [9, -4, -9, -2], [30, -10, 12, 2], [20, -12, 12, 0], [12, 4, 10, -6], [-4, 5, -10, 13], [14, -1, -14, 4], [7, -9, 3, -9], [5, 6, 15, -14], [34, 2, 12, -10], [24, -3, 12, -9], [-8, -4, -14, 2], [27, 1, 5, -3], [9, 3, -7, 13], [0, 2, 2, -8], [-2, 0, -6, 4], [24, -5, -10, 3], [31, -6, 17, -1], [44, -4, -4, -2], [-23, 6, -11, -2], [-8, -2, -2, 7], [6, 4, 2, -16], [16, 3, 0, 6], [-6, 2, -2, 4], [-24, 16, -10, 2], [-20, 3, -10, 2], [-11, -2, -5, -7], [22, 7, -4, -19], [2, -9, 16, -8], [-37, -3, -3, -4], [5, -8, 1, 5], [-17, -15, 9, 9], [-7, -2, -1, -12], [-5, -2, -15, -8], [-15, 1, -15, 13], [26, -6, 14, 8], [-4, 8, -16, 10], [-27, 0, 5, -6], [20, 2, 18, -18], [-25, 9, -15, 5], [8, 4, 0, -12], [21, 4, 9, -13], [1, 2, 9, 7], [25, 0, -1, 7], [16, 6, -8, 2], [-2, 6, 4, -6], [-5, -9, -15, 12], [-39, -14, 1, 17], [-4, 0, -10, 14], [-23, 6, -5, 2], [6, -3, -10, 5], [-11, -10, -3, -6], [-26, 4, -20, 16], [-6, 2, 0, 14], [-19, 6, -13, 7], [4, 0, -6, 10], [-10, 0, -12, -8], [6, -2, 18, 0], [26, 11, -4, -11], [-17, 10, -1, 8], [-38, 12, -18, 2], [32, -20, 14, -5], [3, 6, -13, 6], [38, 9, -8, -5], [21, 11, 5, -11], [20, 3, -2, -12], [-14, 9, 4, -3], [19, -10, 13, -17], [-9, -1, -13, 21], [-17, 11, 1, -7], [-21, 11, -19, -9], [-1, 5, 7, -13], [-16, 1, -14, -12], [17, -1, 5, 14], [16, -4, 4, -10], [35, 2, 1, 12], [-24, -10, -4, 18], [-32, 5, -8, -8], [14, 11, -10, 17], [-1, 2, 5, 10], [-16, 5, -2, 2], [-4, 6, -12, -4], [-16, 11, 0, 11], [-15, -11, 1, -15], [9, 18, -5, 3], [29, -6, 1, -5], [-7, 1, 13, -3], [51, -1, -1, 3], [14, -10, 20, -2], [40, 6, 0, 9], [-16, -9, -4, 16], [-16, 2, -2, 6], [1, -12, 11, -20], [4, -7, 16, -1], [-12, 15, -6, 6], [-18, 12, -2, 11], [43, 6, -7, 1], [14, -14, 14, -2], [-20, 14, -14, -2], [51, 4, 7, -14], [22, -4, 2, -12], [-16, -1, 8, -10], [-29, 1, 5, -11], [-6, -22, 4, 6], [-10, 0, 10, 12], [4, -4, 8, -10], [-18, -20, 8, 12], [27, -19, 9, -3], [-6, 14, 2, -2], [-14, 1, 12, -13], [-37, -7, 1, -7], [26, 0, -2, 16], [-6, 3, -4, -1], [-18, 3, -20, 19], [23, 0, 15, -12], [16, -7, 18, 11], [-20, -3, -10, 21], [36, 10, 4, -8], [34, 8, -4, -6], [12, 21, -6, 4], [-43, 2, -3, 9], [24, 0, -12, 12], [5, -16, -5, 7], [-20, 2, 8, -15], [31, 12, 3, -4], [46, 5, 12, -21], [-16, 1, -16, 25], [-39, 1, -17, 13], [-28, 2, 14, -6], [27, 6, 1, -2], [2, -13, -6, 0], [21, 12, 5, -9], [27, -3, 7, 9], [-43, -2, -1, 10], [-17, 8, 7, 8], [29, 11, 9, -21], [-30, -2, -16, 8], [17, -8, 23, -12], [14, -4, 14, -15], [9, 5, 11, 1], [-33, 12, -5, 10], [10, 3, -10, -11], [-43, 0, 7, 7], [-18, -16, 6, -6], [31, 11, 1, -23], [-6, -8, -4, 8], [-16, 12, -6, 12], [14, -2, 14, 0], [6, -3, 22, -5], [-38, 11, -28, 7], [-5, -16, 5, 9], [1, 11, 1, 0], [4, 2, 6, 4], [43, -1, -3, 11], [-7, -10, 15, 13], [45, -3, 9, 1], [25, -2, 19, -24], [27, -4, 23, -11], [14, 13, -14, 7], [-13, -9, 3, -9], [-29, 7, -13, -5], [-43, 6, 3, 0], [24, -1, 6, 12], [16, -4, 28, 0], [-26, 8, -22, 18], [28, 4, 0, 6], [22, -4, 18, -14], [41, 2, 3, -8], [-2, 0, 8, 12], [15, 19, 1, -13], [-5, -13, 13, -6], [18, 7, 8, 8], [-26, -10, 0, 4], [21, 12, 1, -10], [18, -14, 22, -14], [30, -2, -8, -2], [-20, 2, -12, 4], [-22, -7, -16, 21], [3, 14, 3, -1], [6, -10, 10, -2], [-4, -10, -4, 4], [-47, 17, -23, 3], [13, 4, 5, 0], [1, 6, 19, -5], [-38, -1, 8, -10], [-36, 5, -14, 17], [32, 6, 8, 2], [4, -3, -12, -9], [11, -1, 13, -4], [-30, -3, 0, -1], [-9, 10, -11, -2], [-28, 12, -2, 7], [3, 10, -17, 17], [19, -17, 7, -3], [-2, 2, -16, 2], [42, 10, -4, -6], [-10, -3, 6, 8], [6, -6, 16, -6], [-24, -19, 10, 19], [50, 0, -10, 4], [23, -12, 1, -3], [-10, -5, -12, -3], [14, -2, 4, -4], [47, -2, 21, -6], [-74, 2, 4, -6], [24, -9, 18, -7], [-21, 3, 7, -12], [0, 15, -10, -10], [-6, -19, 0, -8], [-2, 4, -24, 18], [-45, -6, 9, 0], [38, -15, 6, 3], [11, -12, 3, -3], [7, 11, 1, -6], [-34, 10, -18, 9], [10, 0, -14, -20], [-51, 4, -1, 8], [-28, 26, -20, 6], [-30, -10, 12, 6], [30, -4, -16, 4], [-17, 1, -25, 6], [46, 17, -8, -5], [16, 0, -4, 12], [-15, 12, 3, -12]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4002_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_4002_2_a_ba();".
// 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_4002_2_a_ba(:prec:=4)
    chi := MakeCharacter_4002_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_4002_2_a_ba();".
// 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_4002_2_a_ba( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4002_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![-4, 20, -11, -2, 1]>,<7,R![-32, 32, 1, -6, 1]>,<11,R![8, -24, -26, 0, 1]>],Snew);
    return Vf;
end function;