
// Make newform 4009.2.a.b in Magma, downloaded from the LMFDB on 18 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4009_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_4009_2_a_b();".
// 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_4009_2_a_b();".
// 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_4009_a();"
function MakeCharacter_4009_a()
    N := 4009;
    order := 1;
    char_gens := [2111, 1901];
    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_4009_a_Hecke(Kf)
    return MakeCharacter_4009_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, 0], [-1, 1, 0], [-2, 1, -2], [1, -1, -1], [1, 0, 2], [0, 0, 0], [-1, 0, -1], [1, 0, 0], [0, 1, -1], [3, -1, 2], [4, -2, 5], [-1, 7, -5], [0, -2, 5], [-1, 2, 0], [-4, 7, -2], [-8, -1, 2], [0, -3, -1], [2, -2, 0], [8, -6, 10], [0, 3, 4], [5, -3, 0], [-10, -1, 2], [0, -5, 2], [-5, 13, -6], [-1, -3, 1], [-8, 8, -7], [-2, 3, 0], [1, 8, 2], [-12, 7, -9], [-18, 2, -2], [-6, -3, -6], [-4, -4, 1], [7, -1, -2], [-3, 7, -9], [-9, -2, 4], [8, -8, 1], [-7, -5, 4], [15, 0, 8], [-12, 10, -5], [-2, -9, 9], [-10, -5, -1], [6, -14, 4], [6, -4, 5], [3, -8, 0], [-1, -2, 3], [-18, 8, -9], [1, 0, 0], [6, 11, 1], [15, -10, 16], [-6, -5, 1], [-19, -2, -5], [9, -6, -6], [13, 2, -6], [-6, -5, 12], [8, -17, 10], [-9, -1, -8], [-9, 12, -13], [-16, 6, 3], [-4, 2, -4], [9, 1, -13], [9, -9, -1], [-1, -10, 15], [9, -8, 5], [0, -11, 5], [9, 6, 1], [-14, 12, -18], [21, -9, 24], [-14, 10, 11], [0, -5, 1], [-13, 14, -18], [16, -15, 14], [-19, 11, -20], [8, 9, -14], [10, 0, 7], [-15, 16, 0], [12, 9, -8], [4, -16, 2], [-7, -9, -6], [14, 5, 3], [9, -3, -9], [12, -15, 14], [25, -7, -1], [-7, -2, 4], [5, -10, -2], [5, -1, 17], [8, 13, 0], [12, 1, 3], [-5, -3, -13], [-19, 7, -8], [3, -3, 7], [7, -22, 16], [2, -19, 3], [-1, 27, -5], [-5, 5, -9], [-2, -11, 16], [11, 1, 6], [11, 4, -16], [8, 9, -7], [18, 1, -5], [13, -9, -7], [-11, 12, 22], [-24, 0, -17], [-22, -7, -8], [-15, 16, -10], [11, -11, -7], [18, -18, 3], [-19, -7, -8], [13, 4, -17], [8, -8, -13], [-8, -6, 19], [34, -23, 40], [20, -8, -12], [-10, 9, -20], [24, 4, 1], [5, -12, -10], [-28, 18, -11], [-11, 1, -18], [-19, 18, 5], [-19, 8, -30], [-17, 15, -14], [-7, -3, 13], [3, 10, -8], [-29, 15, -34], [-25, 3, -17], [4, 4, -16], [-16, 8, -32], [11, 10, -15], [-12, 28, 2], [-11, 26, 7], [-2, 1, 19], [16, 14, -9], [3, -13, 18], [10, -16, 12], [6, -12, 28], [16, -1, 1], [21, -5, 16], [19, -2, 8], [-1, -9, -3], [-2, -22, 0], [7, 0, 6], [-17, -16, -4], [18, -21, -4], [-15, 19, -20], [-13, 5, 2], [20, -6, -14], [-37, 6, -7], [26, -21, 22], [-21, 9, 11], [-18, 12, 17], [-6, 23, 3], [7, 7, -23], [14, -8, 6], [-5, 26, -2], [-23, 11, -13], [7, 2, -20], [8, -24, 15], [15, -11, -24], [13, -5, 9], [10, -18, -2], [16, 1, 23], [4, -35, 24], [-5, 15, -6], [32, -3, 3], [-14, 16, 19], [14, -7, -8], [-30, 14, -25], [-11, -5, 17], [-5, 18, -2], [22, 11, -6], [22, -5, -20], [8, -5, 18], [-11, -2, -14], [-25, 25, -38], [19, -39, 10], [-25, 8, -30], [-33, 30, -34], [-17, 35, -8], [-4, -2, 13], [-32, 9, 11], [14, 16, -23], [21, 15, -1], [9, 20, -23], [-18, -15, -5], [-16, -7, 4], [-42, 8, -7], [17, -11, 21], [-9, 2, 2], [-33, 15, 16], [13, 24, -2], [13, -1, 12], [-12, 0, 9], [0, -15, 21], [23, -17, 1], [14, -17, 21], [-40, 0, -2], [-19, 16, 31], [0, -37, 18], [22, -10, -23], [22, 1, 18], [-32, -4, 15], [-30, -15, 6], [-43, 6, 10], [2, -4, -9], [-36, 15, -28], [-14, -8, 6], [16, -26, 3], [43, 13, -9], [4, -22, 16], [7, -3, 26], [-8, -16, -17], [12, 11, 10], [-28, 5, -23], [17, -11, -1], [26, 2, -8], [-12, 42, -13], [7, -9, 8], [13, -22, 2], [40, -5, 19], [26, -7, -8], [-22, 11, -26], [-19, -6, 12], [-16, 5, -1], [43, -13, 23], [18, -25, 13], [-15, 14, 4], [33, 18, -8], [-19, 25, 6], [-5, -25, 8], [-29, -22, 20], [13, -12, 4], [4, -22, 3], [-22, 34, -9], [-16, 38, -37], [-21, -8, -16], [13, 10, -14], [17, 6, -11], [38, -20, 20], [-15, 3, -2], [-33, 26, -16], [-38, -12, 7], [35, -39, 51], [25, -45, 26], [-39, 41, -39], [-22, 33, -35], [25, -13, 41], [20, -43, -1], [-6, 10, -1], [15, 26, 3], [-7, 11, -25], [-13, -28, 18], [-9, 16, 4], [-36, -2, -23], [21, 9, 15], [20, -8, 10], [6, -4, -40], [-23, -14, -22], [-26, 30, -7], [-34, 17, 8], [-30, 40, -29], [-7, -39, 27], [4, -34, -2], [24, -30, 29], [-11, -15, -25], [4, 22, -15], [-1, -21, -1], [0, -37, 1], [16, -36, 20], [-29, 4, 8], [36, 8, -9], [33, -8, -8], [-6, -12, 1], [-57, -1, -19], [16, -4, -10], [0, 5, 10], [37, 6, 19], [7, -47, 12], [-37, -5, 15], [-1, 44, -30], [-18, 28, -28], [-12, 8, 17], [14, -4, 5], [-47, -7, -9], [38, 5, 14], [10, 5, 28], [4, 47, -8], [17, -3, 4], [55, -32, 27], [13, -31, -19], [0, -47, 25], [-42, -9, 12], [-59, 14, -3], [47, -1, -5], [-22, -36, 8], [-14, -10, -3], [11, 34, -9], [-50, 28, -29], [-18, -8, 11], [-8, -9, 16], [-10, -14, 29], [37, 10, -5], [10, 20, -17], [28, -8, 18], [-72, 16, -26], [-12, 9, 16], [-18, 45, -39], [2, -37, -2], [-27, -19, 4], [4, -17, -19], [-25, 11, 11], [2, 12, -19], [38, -16, 38], [17, -14, 15], [-54, -12, 6], [48, -26, 54], [-21, -27, -12], [-6, -5, -7], [-20, 20, -47], [15, -6, 24], [15, 3, -8], [42, -15, 0], [40, -17, 21], [59, -30, 47], [27, 16, -22], [34, -7, -2], [4, 15, 12], [19, -6, -16], [-1, -20, 24], [-33, 40, -14], [-21, 5, 10], [-9, -32, -16], [-59, 1, -10], [-23, 12, -2], [20, -27, -9], [-46, -5, -1], [11, 16, -15], [37, -9, -17], [44, -24, 4], [-28, 6, 16], [-13, 7, -32], [51, -18, 26], [20, -20, 39], [44, -23, 40], [-11, -13, 3], [15, -38, -6], [-40, 24, 7], [-16, -2, 14], [9, -51, 41], [14, -18, 50], [-26, 45, -40], [-8, 36, -27], [-39, 22, -8], [22, 14, -5], [78, -11, 7], [29, 3, -27], [38, -9, -30], [-12, 20, -16], [-39, 5, 3], [-18, 0, -17], [-7, 32, -33], [2, -53, 10], [-9, 52, -37], [-25, -24, 2], [-48, -21, 2], [-26, 30, 13], [41, 5, -3], [-40, 21, 4], [20, -34, -9], [8, -30, -28], [1, 21, 25], [31, 1, 16], [-48, -1, -2], [60, -10, -21], [-2, -26, 39], [-55, -17, 2], [19, 4, -39], [69, -38, 52], [-47, 30, -5], [55, -39, 51], [36, -34, 49], [18, -6, -22], [-20, 25, -12], [-48, 28, 11], [-44, 41, -34], [26, -4, 10], [52, -20, 66], [12, 13, -5], [-44, 47, -16], [12, -7, -18], [-37, -1, 19], [-8, 7, 46], [-38, 50, -42], [-5, 8, -4], [-9, -35, 7], [-38, 28, -20], [29, 5, 13], [16, -41, 13], [-39, -16, 2], [-10, -53, 36], [9, 29, 1], [15, 15, -4], [25, 41, -7], [27, -15, 8], [55, -4, 33], [-5, -13, -4], [28, -13, -32], [37, -2, 7], [60, -3, -13], [-29, 20, -59], [-14, 39, 20], [-6, 18, 45], [5, -15, 3], [59, -32, 17], [-6, 24, -42], [11, -48, 39], [-56, 1, -19], [14, -72, 29], [-47, 39, -48], [-30, 54, -34], [55, 20, 10], [-10, -2, -36], [-38, 27, 17], [-38, 15, 2], [57, -42, 46], [-9, 27, -7], [14, 10, -11], [-4, 44, -22], [-8, -31, -1], [-15, 9, -7], [20, -12, -13], [-26, 42, -41]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4009_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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