
// Make newform 4004.2.a.e in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4004_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_4004_2_a_e();".
// 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_4004_2_a_e();".
// 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, 4, -4, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 1, 0, 0], [-2, 0, 1, 0], [0, -3, 0, 1]];
    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_4004_a();"
function MakeCharacter_4004_a()
    N := 4004;
    order := 1;
    char_gens := [2003, 3433, 365, 925];
    v := [1, 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_4004_a_Hecke(Kf)
    return MakeCharacter_4004_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, 0, 0, 0], [0, 0, -1, -1], [1, -1, 0, 1], [-1, 0, 0, 0], [1, 0, 0, 0], [-1, 0, 0, 0], [-1, 1, 2, -1], [-2, 0, 1, -2], [0, 0, -1, 0], [0, 3, 0, 0], [1, 2, 0, 0], [0, -1, 3, -1], [1, -3, -3, 2], [1, -1, 2, 4], [-4, 4, 1, -2], [-5, -4, 2, -1], [-1, -1, 6, 6], [-3, 1, -1, 3], [-5, 4, 0, 0], [-6, 0, 0, -4], [3, -1, -5, 1], [6, -3, 2, 7], [-1, -1, 1, 5], [-10, 4, -3, -8], [8, 1, 1, 10], [7, -5, 1, 11], [-2, 5, 0, -1], [7, -5, -1, 8], [-11, 4, 0, -8], [-5, -1, -4, -3], [0, 0, -3, 1], [1, -1, -7, -2], [-3, 4, 5, 12], [-2, -5, 7, 3], [-12, 2, 1, -12], [0, 0, -5, 3], [-7, 2, -1, -7], [3, -10, -5, -3], [-3, 4, 3, -7], [-7, 2, -8, -9], [-11, 2, 4, -9], [-3, -6, 3, 0], [-3, -9, 1, 7], [2, -1, -8, 8], [4, 1, -8, -1], [4, -5, 2, 10], [-8, -2, -3, -10], [10, -6, 4, 3], [10, -6, -8, 1], [-13, -3, -2, -12], [-3, 16, 0, -5], [10, -6, -7, -4], [-9, 10, 10, 2], [8, 6, -2, 8], [-12, -1, -6, -7], [0, 4, 6, 8], [-4, -1, 3, 5], [0, -3, -9, -10], [-1, -3, -2, -9], [-12, 3, -7, -5], [4, -11, -5, -10], [19, 6, 0, -2], [-5, 4, 12, 3], [1, 12, -8, -4], [-15, 9, 8, -1], [1, -3, -10, 9], [0, 0, -11, 2], [-18, -1, -4, -9], [-6, -8, 7, -5], [2, 0, -5, 1], [-4, -2, -2, -20], [-1, 3, -2, -1], [0, 15, -3, 0], [-9, 5, 3, -11], [11, -1, -3, 20], [17, 4, -1, 3], [-11, 3, 1, 4], [-11, -1, -6, 6], [-14, 1, 0, -24], [3, -14, 9, 8], [-12, 0, 13, 3], [-14, 11, -13, -22], [0, -1, 16, 0], [6, -11, 4, 10], [12, -4, -13, -9], [-1, 6, -7, -12], [5, 2, 13, 3], [-18, 10, -1, -18], [-10, 13, 7, -19], [1, -2, 12, -2], [-11, -5, 5, -8], [-12, 8, 4, -17], [10, -19, -4, 10], [12, 10, 2, 3], [-6, 4, -12, -1], [6, 9, 9, 6], [5, 3, -2, -5], [7, -4, -3, 7], [-25, 4, 0, -15], [-1, -9, -2, 13], [0, -5, -5, -10], [7, -10, -2, -3], [2, 5, 17, -14], [-16, -1, 1, 0], [-13, 11, 19, 14], [12, -1, -5, 20], [9, -11, -4, 11], [7, 5, -3, 9], [11, -12, 1, 17], [4, -16, 2, 9], [4, 0, -4, 17], [-1, -6, 3, 6], [16, -8, 7, 15], [-7, -10, 4, 7], [-7, -4, 9, 4], [12, -1, 1, 7], [4, 7, 9, -11], [17, -15, -12, -14], [15, -14, 11, 14], [12, -16, -5, 3], [3, -3, -20, 7], [-6, -1, 13, -11], [5, -6, 3, 14], [-10, 8, 9, -11], [2, -6, -17, -19], [7, -11, -17, -20], [1, 5, 11, 0], [8, 5, -6, 9], [27, 13, -3, 4], [0, -15, 6, 7], [1, 9, 4, 2], [20, -5, -2, 0], [12, 14, -4, -1], [21, -4, 13, 38], [15, -5, 16, 2], [12, 11, 1, -14], [17, 1, 0, 30], [19, -5, 6, 19], [43, -16, 10, 34], [-16, 8, -7, 3], [32, 2, -3, 14], [7, 5, 13, -11], [-1, 15, 1, -12], [19, -3, -8, 19], [-19, 4, 10, -11], [20, -15, -4, -6], [9, -27, -13, 7], [-7, 13, 2, -25], [7, 4, 2, -2], [-9, 11, -4, -10], [7, 4, 17, 4], [15, -16, -7, 12], [6, -21, -8, -12], [-11, 5, 1, -18], [-25, 18, 15, 7], [9, -7, -19, -6], [-28, 4, 0, -1], [0, 1, 11, 18], [-5, 12, 20, -18], [-36, 3, 0, -37], [-16, 10, -7, -13], [33, -11, 4, 4], [11, 1, 11, 9], [2, -3, -10, -1], [9, 20, 1, 12], [-14, 13, 4, 1], [-34, -12, 8, -10], [10, -15, -1, 22], [-28, 9, 8, 10], [-1, -3, -4, -7], [9, 7, -17, -15], [32, -13, 6, 34], [15, -7, -18, -3], [-4, -6, -8, -18], [-7, -3, 10, -10], [-18, 0, -7, -7], [-13, 1, -17, -17], [-3, 6, 19, 8], [8, 14, -30, -15], [-21, 16, 12, -6], [6, -1, -20, 7], [8, -12, -2, 9], [-33, 6, -12, -16], [-16, 6, 4, -8], [-8, 7, -37, -23], [23, 9, -4, 13], [-39, -1, -10, -13], [12, -7, -19, -28], [-3, 4, -18, -1], [27, -4, 5, -1], [-24, 22, 10, -7], [2, -7, -6, -10], [12, 13, 2, 12], [-7, 4, -6, -5], [2, -11, 2, 18], [27, 0, 10, 10], [-11, 6, -2, -21], [-13, 5, 15, -23], [9, -20, -10, -22], [21, -9, 11, 7], [-17, -9, 21, 10], [-2, 2, -8, -29], [-22, 6, -14, -27], [10, -11, 6, 30], [2, -16, -10, -4], [-32, 1, -4, -17], [-11, 3, 12, -22], [-8, 7, 8, -15], [36, -19, -14, -4], [-47, 14, -10, -37], [-10, 5, -17, -11], [-2, 0, 2, -26], [-27, 5, 9, 1], [5, -2, -10, -26], [44, -10, -8, 12], [-49, 16, -10, -25], [-17, 30, 1, -23], [-11, 32, 0, -12], [21, -31, 0, 14], [3, 6, 25, 18], [-15, -5, -6, 6], [-8, 13, -6, -29], [9, -15, 11, 36], [2, -9, -2, -7], [0, 7, -27, 0], [2, -8, -2, 33], [-6, -16, 2, 20], [5, -15, 12, 19], [11, -8, 22, 8], [-28, -10, 1, -5], [-13, 1, -5, 3], [-34, 25, -8, -22], [16, -23, 11, 22], [11, 10, -26, 0], [26, -20, -8, 19], [39, 16, -12, 9], [-13, -16, 9, 16], [33, 2, 7, 35], [30, 15, 0, -11], [-28, 2, 4, -19], [1, -31, -12, 21], [-12, 4, -9, -28], [14, -2, -3, 14], [20, -29, -6, 8], [27, 12, -10, -6], [-1, -30, 19, 14], [-2, 11, -14, -19], [-41, 18, -27, -31], [6, -6, 20, 17], [13, -12, 30, 33], [12, 15, 0, -12], [-9, 15, -12, -6], [-6, -15, 0, 28], [34, -26, 20, 33], [30, -14, 9, 27], [9, -12, 0, 17], [-54, 14, -10, -16], [28, -3, -6, 3], [6, -13, 14, -4], [14, 16, -1, 14], [5, 6, 2, -12], [-21, 9, 14, 31], [-28, -5, 24, 17], [-12, 13, 2, -13], [-2, 14, 26, 9], [-12, -10, -25, 19], [-26, 34, -16, -27], [3, -28, -1, -5], [-7, 12, 12, -7], [-16, 18, -16, -39], [9, -12, -9, 34], [-29, -14, 22, 7], [-36, 2, 27, 18], [9, -9, 1, -2], [-30, -6, 11, 4], [12, 9, 25, -12], [-35, -11, 17, -2], [-10, 3, 34, 12], [-2, 6, 25, 8], [-41, 20, -16, -36], [10, -5, 10, -9], [-27, 16, -3, -33], [-21, 19, 1, 3], [-3, 7, -36, -38], [-55, 11, -7, -10], [-53, 24, -16, -44], [6, -11, -6, 42], [6, 9, 7, 11], [52, -13, -7, 1], [3, 15, 0, -31], [-31, 4, 12, -18], [-50, 5, 5, -32], [45, -11, 3, 41], [13, -24, -20, 9], [-10, -25, -2, -15], [26, -20, 15, 13], [-48, 0, -1, -36], [19, 9, -25, -6], [-33, 4, -4, 8], [8, -19, 1, -14], [-12, 46, -1, -17], [-29, 1, 16, 18], [-25, 4, 8, 22], [4, -12, -14, -1], [-50, 6, 8, -11], [2, -18, 10, 15], [-5, -12, 7, 16], [25, 11, -11, 25], [-31, 17, 15, 7], [-25, -11, -10, 3], [-18, 3, 30, 19], [-35, -6, -8, -35], [37, 4, 19, 12], [-58, 21, -9, -17], [61, -22, 25, 57], [-39, 13, -35, -38], [6, -3, 12, 14], [17, -3, 0, 30], [-1, 4, -24, -35], [-7, 14, 14, 12], [18, -9, 7, 18], [12, -2, 4, 7], [-42, 32, 9, -9], [12, 15, 10, 17], [-5, -3, 22, 19], [-19, 28, 7, -18], [-41, 7, -3, -15], [15, 19, 11, 22], [4, -30, 19, 26], [-8, -8, 1, -39], [42, -33, 9, 25], [-27, -9, -16, -9], [9, 2, 10, 49], [-11, 12, 12, 0], [-31, 32, 1, -3], [-23, 2, -10, 26], [-34, 13, 20, 14], [-13, 14, 25, -18], [16, 19, -32, -3], [54, -25, 26, 67], [-14, 11, 15, 21], [15, -19, -40, -12], [23, 3, -18, 32], [-35, -5, 18, 20], [-20, 42, 19, -10], [0, 32, 11, -6], [44, -32, -18, 12], [-18, 20, 4, 6], [-13, 0, 23, -17], [-19, -26, 3, 2], [-16, -12, 8, 1], [11, -5, 27, 6], [9, 7, 32, 11], [35, -20, 8, 8], [11, 15, 3, -4], [-23, -18, -13, -12], [16, 8, -3, -11], [21, -3, 36, 47], [4, 5, 20, 6], [-34, 22, -33, -39], [-50, 24, -7, -33], [-18, 10, -31, -20], [-12, 5, 2, -2], [-40, 23, 19, 18], [36, 0, 32, 30], [-26, -3, -1, -19], [27, -14, -26, -15], [16, -38, -1, 14], [-26, 31, -22, -44], [-13, 21, 19, 5], [3, 10, 2, -10], [-25, 2, 10, -9], [3, 25, 22, -5], [-5, 2, -36, -24], [9, 27, 5, -13], [19, 28, -27, -6], [-21, 52, 20, -31], [-27, -13, -7, -23], [-22, -5, 6, -37], [34, 10, -14, 13], [-47, 5, 23, 27], [67, -33, -4, 25], [-1, -22, 12, 19], [10, -5, 20, 30], [-29, 14, -40, -37], [-38, 23, -18, -45], [44, -5, 3, -9], [-36, -29, 7, 9], [-9, -5, 35, 30], [-30, -9, 11, 0], [35, -24, -26, 12], [27, 6, 10, -11], [-45, 18, -10, -19], [-11, 17, -37, -10], [5, -5, -30, -38], [-28, 1, -24, -43], [25, 3, 37, 0], [3, -35, 5, 10], [-42, 20, -18, -45], [34, -31, -24, 19], [19, -16, 3, 14], [-17, 3, -10, -33], [-19, -9, -29, -12], [-28, 12, -27, -16], [30, -10, -10, 45], [6, 9, 4, 16], [18, -10, -18, 10], [34, -18, 1, 28], [-10, 23, -14, 8], [-11, -11, -10, 2], [-41, 9, -2, -28], [31, -10, -46, -28], [18, 7, -7, 14], [-26, 16, 6, 29], [-20, 0, 24, 17], [-6, -6, -12, 27], [36, 14, -19, 46], [-13, 0, -40, -44], [5, 34, 19, -9], [21, 15, 9, -4], [-15, -16, 25, 1], [65, 8, -1, 20], [-29, 8, 24, 36], [18, -6, -5, 20], [51, -26, -8, 22], [42, -31, -19, 10], [-3, 9, 32, 13], [41, 0, 17, 8], [-58, -2, -1, -28], [-8, 14, -31, -53]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4004_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_4004_2_a_e();".
// 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_4004_2_a_e(:prec:=4)
    chi := MakeCharacter_4004_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_4004_2_a_e();".
// 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_4004_2_a_e( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4004_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![1, 4, -4, -1, 1]>],Snew);
    return Vf;
end function;