
// Make newform 4005.2.a.l in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4005_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_4005_2_a_l();".
// 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_4005_2_a_l();".
// 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, -3, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 1, 0, 0], [-1, -1, 1, 0], [1, -2, -1, 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_4005_a();"
function MakeCharacter_4005_a()
    N := 4005;
    order := 1;
    char_gens := [3116, 802, 181];
    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_4005_a_Hecke(Kf)
    return MakeCharacter_4005_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, 1, 1, 0], [0, 0, 0, 0], [1, 0, 0, 0], [1, 2, 0, -2], [3, 0, 1, 0], [-3, -1, 1, 3], [2, -1, 1, -3], [-1, -3, 2, 1], [0, -1, -1, 3], [2, 4, 1, -2], [-3, 5, 0, -2], [2, -3, -4, 3], [0, -3, -2, 5], [3, -3, 1, -1], [6, 4, 1, -3], [-5, 1, 3, 5], [7, 2, -2, -2], [3, 3, 1, -10], [0, -1, -2, -2], [-1, 0, -2, -4], [3, 5, -1, -6], [-7, -5, -3, 6], [2, -4, -6, 1], [1, 0, 0, 0], [9, -5, 2, 3], [6, 3, 2, -13], [5, 2, 2, 2], [6, -6, -2, 4], [5, 1, -1, -4], [-1, -2, 7, 3], [4, -3, 2, -3], [7, -3, -1, 6], [5, 1, -1, -14], [-17, 3, 2, 7], [12, -7, -8, -1], [-9, -7, 2, 2], [-1, 4, 10, -3], [8, 4, -7, -6], [-1, -4, -2, 7], [6, 6, -7, 6], [3, -2, 0, 10], [-1, 12, -2, 1], [-3, -2, 9, -3], [6, -4, -3, 10], [-4, 1, 6, -3], [-4, -4, 3, -3], [12, -8, -16, 0], [2, 12, -4, -1], [13, -8, -2, -6], [3, 4, 3, 0], [3, -1, 0, -12], [14, 13, 7, -14], [-8, 6, 5, 10], [-3, -8, 4, 1], [-1, -1, -6, -5], [5, 9, -7, 1], [9, -15, -10, 4], [-5, -8, 8, 11], [12, 2, -11, -3], [-13, -3, -4, 19], [-2, 4, -2, 5], [4, 9, 3, -7], [-4, -7, -7, 9], [20, -6, -10, -5], [-7, -13, -4, 12], [-4, 0, -12, 1], [-15, -9, 3, 6], [5, -7, 0, -4], [23, -5, -7, -11], [17, -17, -14, 3], [7, -11, -7, 14], [8, 1, -4, -6], [-1, -15, 7, 0], [10, 1, 9, 0], [10, -1, -15, -10], [-6, -15, 8, 13], [-21, -1, 5, 5], [10, 2, -11, -9], [4, -8, -11, 6], [20, -5, -17, 6], [-5, 3, 16, -11], [7, -12, 10, 15], [9, 4, 6, -13], [-13, -11, 10, -7], [-3, -8, 2, 20], [8, 14, -3, -22], [-15, -15, 2, 7], [17, -6, -12, -3], [-10, 13, -8, -4], [-1, -1, 7, 14], [18, -3, 7, 11], [-11, -7, -3, 27], [11, -4, 7, -15], [-11, -3, -5, 23], [5, 10, -9, 2], [5, 8, -3, 9], [-5, -8, -15, 2], [-8, 6, 12, -5], [12, -3, -19, -3], [-20, 10, 8, -5], [2, -7, 12, -2], [-17, -1, 8, 12], [13, -12, 7, 12], [-2, -9, 15, 26], [-3, -6, 7, -20], [21, -25, -7, 7], [-11, 13, -5, 1], [13, -5, -18, 16], [-2, -10, -1, 1], [-3, 16, 10, -10], [-19, 5, 8, 20], [-6, 16, 0, -2], [29, -9, -5, -22], [-7, -7, -2, -3], [15, 5, -14, 12], [14, -1, 1, 2], [9, 6, -1, -30], [-7, -8, -11, 13], [6, 8, -6, 15], [17, 8, 6, -28], [-1, 10, -12, -2], [1, 25, 14, -16], [24, 19, 4, -16], [19, -7, -12, -8], [7, -8, -3, -11], [-20, 18, 15, 5], [5, 17, 10, -26], [-7, 17, 15, 2], [24, 0, -5, 12], [20, -3, 9, -7], [-25, -14, 4, 19], [-10, 4, 8, 7], [9, -2, -16, -1], [-19, 12, 4, 6], [-6, 20, 6, -4], [-28, 1, 2, 5], [-6, -15, -3, 9], [-2, 32, -2, -21], [24, -30, -9, 4], [-12, 20, 4, -3], [-23, -16, -3, 16], [4, 8, -17, -21], [11, -30, -10, 15], [-9, 20, 19, -17], [-26, 18, 6, -5], [8, -17, -12, 3], [-9, -6, 11, 1], [17, 12, -12, -5], [2, 16, -7, 6], [-4, 15, 1, 12], [24, 10, -8, -11], [-7, -16, -3, 21], [5, 6, 23, -11], [9, -23, 2, 4], [-31, 17, 23, 0], [-48, 27, 18, 10], [-38, 11, 14, 4], [-3, 7, 6, -9], [-1, 17, 1, -18], [-18, 6, 10, 12], [18, 9, -11, -14], [-18, -9, 1, 24], [7, 9, -12, 23], [19, -4, 0, 9], [-11, 7, 14, 7], [1, 2, 2, 9], [12, 19, 9, -16], [-7, 19, 0, -7], [32, 17, -16, 6], [-27, -19, 17, -8], [8, -20, -18, 5], [2, -4, -4, 5], [21, 5, -1, 4], [-18, 4, 4, -5], [0, 11, 15, -4], [-10, -2, 28, -5], [13, -19, -15, 15], [-4, 12, -10, -32], [-15, 26, -2, 0], [-3, 8, -19, 2], [12, -17, -3, 5], [29, 19, 10, -24], [4, -9, -6, -24], [-38, 2, 3, 3], [-18, 1, -6, -21], [-13, -30, -20, 26], [-39, -2, 3, -15], [8, -6, 1, 2], [-25, -4, 29, -3], [19, -6, -3, 5], [9, 3, -2, -9], [-8, 10, 1, -17], [9, 5, 17, 5], [-8, 0, 17, -3], [4, 15, 15, 2], [-32, -25, -14, 24], [-35, -1, -3, 4], [-29, 3, 2, 10], [2, -7, 7, 13], [-9, 21, 24, -8], [1, -4, -4, 14], [-1, -27, -19, 21], [-7, 1, -20, 6], [-13, 1, 16, -11], [9, 0, 23, -6], [5, -2, 17, -5], [14, 8, -22, 24], [3, -22, 2, 6], [25, -15, -24, -21], [-21, -4, -8, 10], [-20, -6, -4, 18], [7, 10, -13, 12], [9, 20, 15, -34], [13, 13, -3, 21], [13, 14, 10, -14], [2, 14, 10, 1], [6, -25, -6, 26], [-19, 13, 9, 10], [-25, 5, -5, -18], [-9, 35, 9, -7], [-10, 4, -21, -11], [14, 22, -6, -20], [-14, 1, -19, 8], [23, -8, 7, -15], [37, 2, -1, -13], [27, -11, -18, 22], [20, 11, 11, -22], [-2, 8, 16, -7], [7, -3, -11, 11], [34, -27, 0, 17], [-37, 9, 7, 13], [35, -30, -16, 9], [14, 13, -4, -31], [13, 14, -6, 11], [-31, 31, 29, -9], [-20, 35, 10, -9], [12, 0, 4, -37], [37, 4, 13, -19], [-7, 7, -25, 3], [26, -8, -18, -6], [-4, 1, 14, 20], [27, 19, 3, -15], [15, -15, 0, 25], [-13, -15, 1, 18], [9, -12, -22, 17], [-31, 9, 7, 2], [0, 29, 28, -32], [3, 24, 7, -14], [-25, 27, 16, -22], [18, 1, 6, -32], [-5, -6, 13, 31], [21, -12, -1, -15], [-15, 35, 2, -27], [34, -16, -13, 12], [-15, -12, 34, 4], [11, -9, -32, 25], [29, 6, -7, 1], [1, 22, -26, -11], [-62, 18, 19, 12], [-13, -31, 6, 34], [34, 14, -14, 18], [-11, 5, -9, 9], [-18, -29, 20, 11], [-12, -9, -17, -1], [40, 3, -13, -16], [16, -22, -11, 15], [18, 21, 5, -1], [-21, -32, -31, 29], [-7, 3, 22, 22], [6, -17, -9, -19], [-40, 19, 21, 6], [3, -6, 4, 19], [5, 25, 14, -38], [-15, 1, 24, 1], [9, 12, 20, 1], [19, -6, -3, -34], [-42, 27, 13, -2], [-18, 32, 6, -5], [-42, -21, 2, 11], [31, 0, -13, -17], [-34, -10, -19, 15], [50, -12, -15, -28], [-23, -9, 5, 47], [27, 14, 18, -52], [-46, -21, -12, 27], [18, 35, -8, 2], [7, -18, 5, -2], [-50, 30, 31, 3], [-34, -7, -9, 6], [-3, 1, 17, -25], [-16, 29, 36, -13], [5, 19, 10, -4], [10, -19, 0, 30], [61, -18, -10, -20], [3, 13, 3, 5], [-40, 1, -1, 50], [-9, -26, 0, 0], [8, -21, 7, 39], [-4, -36, -24, 25], [3, -15, -26, -1], [-10, 6, -24, -1], [-4, -3, -40, -13], [-30, -3, 27, 6], [-8, -15, 27, 2], [-3, -34, 7, 21], [-6, 15, 14, 22], [39, 8, -35, 1], [-25, -15, -17, 26], [7, -4, 13, -28], [9, -5, -12, 28], [0, 5, 4, 5], [-12, -37, -21, 24], [26, -16, -42, -9], [-17, 1, -16, 56], [9, -2, 12, -24], [-23, 7, -9, 33], [41, 3, -25, -10], [-25, -24, -6, 36], [-36, 42, 20, -17], [-59, 5, 0, -1], [-22, 13, 7, 38], [-16, -23, -9, 8], [25, -46, -24, 12], [-2, 0, 28, 31], [-2, -10, -3, 51], [23, 23, -20, -7], [-7, -14, 0, 37], [-5, -12, -11, 54], [-31, -1, 17, -14], [2, -7, 36, 16], [-5, 11, 26, -3], [-4, -13, -27, 30], [20, -4, 0, 41], [4, 36, 40, -36], [-38, 2, 26, -11], [40, 28, 7, -39], [-39, 8, 1, 8], [8, 16, -17, 29], [5, -23, -13, 17], [44, -30, -48, 2], [44, 10, -20, -28], [-43, 9, 25, 43], [12, -47, 3, -4], [41, -1, -7, -56], [31, 43, 18, -36], [4, -22, 1, 36], [-12, -10, -33, 30], [-24, 22, 20, -24], [38, -20, -8, -17], [-51, 11, 27, -16], [13, 20, 8, -45], [-18, 34, -24, -41], [14, 40, 9, -18], [27, 31, -8, -13], [-61, 6, 7, -8], [-59, 42, 15, -7], [27, 41, -6, -35], [2, -14, 9, 26], [30, 30, 31, -39], [5, 0, 21, -7], [39, 23, 8, -43], [19, -15, 6, -6], [-7, -29, -1, 2], [-11, -29, -17, 43], [29, 12, -20, 6], [-63, 0, 11, 32], [10, -9, -4, -30], [-15, -12, 9, 40], [-31, 4, 42, -8], [-56, 27, 39, -4], [-30, -18, -10, 17], [30, -11, 13, -4], [-12, 10, 35, 14], [-33, 11, 16, 40], [12, -56, 6, 14], [-13, 46, -9, -18], [7, -6, -7, -19], [34, -5, -5, 5], [22, 7, -6, 23], [-14, 14, -24, -22], [29, 13, -9, -39], [2, 31, -25, -35], [27, 9, 25, -9], [-1, 37, 32, -17], [-23, 15, 9, 47], [-20, -14, 32, 14], [-38, -34, -12, 49], [-37, 24, -16, -9], [4, 27, -16, -48], [-14, -9, -17, -5], [18, -46, -11, 12], [6, 16, 2, -1], [-40, 27, 7, 11], [-7, 0, 10, -31], [40, -25, -29, -4], [-18, -40, -3, 30], [30, 6, -48, -3], [4, 5, -8, -16], [49, -5, -4, -49], [-58, 22, 34, 16], [42, 5, 13, -50], [54, -43, -30, -2], [9, 14, -16, 16], [-14, 9, -5, -36], [9, 29, -14, -10], [17, -1, 24, -5], [-12, -23, 11, 40], [-20, 19, -3, 3], [-14, -41, -3, 40], [-49, 6, 26, -5], [21, -5, 7, 32], [-30, -42, -14, 48], [28, -26, -14, 5], [5, -6, -6, -31], [-48, 4, 12, 19], [-30, 7, 22, -14], [-38, 43, 6, -1], [-41, 5, 12, 8], [12, -33, -39, 23], [45, 11, -20, 16], [-43, 38, -3, -20], [6, -13, -9, -17], [-33, -7, 42, -3], [21, 3, 39, 16], [-18, -7, 27, 44], [-66, 10, 5, 48], [23, 13, 20, -8], [-40, -34, -25, 34], [-63, 11, 29, -16], [62, 6, -22, 3]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4005_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_4005_2_a_l();".
// 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_4005_2_a_l(:prec:=4)
    chi := MakeCharacter_4005_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_4005_2_a_l();".
// 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_4005_2_a_l( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4005_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![-1, -7, -5, 1, 1]>,<7,R![11, 18, -12, -2, 1]>,<11,R![109, -147, 70, -14, 1]>],Snew);
    return Vf;
end function;