
// Make newform 4002.2.a.w 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_w();".
// 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_w();".
// 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 := [-3, 0, 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_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], [1, 0], [3, 0], [3, 0], [-1, -1], [3, -1], [-3, -2], [-1, 2], [1, 0], [-1, 0], [-1, 1], [-2, -3], [0, 7], [-5, 4], [1, 6], [6, 2], [7, -2], [-4, 6], [-6, -2], [-3, 5], [-6, -4], [-5, -7], [-8, 2], [-2, -2], [4, 0], [10, -4], [1, 8], [4, -3], [-8, 0], [1, -6], [8, -2], [-13, 3], [-6, 4], [-6, 8], [5, 0], [2, -7], [-2, -1], [-22, -1], [6, -8], [10, 1], [0, -6], [7, -7], [-14, -7], [6, -2], [16, -1], [0, -10], [6, -3], [8, 0], [-10, 9], [8, -3], [6, -10], [6, 8], [12, -1], [-2, -2], [-21, 1], [10, -11], [-3, 5], [4, -12], [7, -7], [24, -2], [-7, -9], [6, 0], [-16, -4], [11, 10], [-8, 7], [19, 5], [0, -17], [-14, 4], [5, -8], [4, 6], [-8, 2], [-6, -1], [23, 5], [24, -4], [-10, -8], [15, 3], [0, -8], [9, -5], [2, -16], [7, 7], [6, -9], [20, 6], [25, 3], [-11, 13], [16, 3], [-6, 18], [0, 5], [-2, 19], [11, -11], [0, -18], [17, 3], [12, -8], [-10, 13], [-15, -5], [1, -11], [0, -5], [2, 7], [17, -13], [-20, 14], [11, 10], [-16, 10], [11, 4], [20, -10], [-21, -6], [9, 19], [-5, -17], [-3, -18], [30, -2], [-32, 6], [0, -12], [-5, -7], [-26, -8], [-35, -8], [-31, 0], [-17, 0], [5, -2], [3, 19], [-1, 9], [28, 6], [32, 0], [-16, 0], [19, 0], [-14, -12], [-25, -2], [-17, 13], [-3, 12], [13, 5], [33, -3], [4, 14], [4, 4], [-26, -6], [-18, 17], [-12, 10], [4, 3], [17, 3], [27, 11], [-1, -15], [-11, -3], [-3, 29], [6, 0], [-12, 24], [4, -2], [3, -25], [-28, -4], [-25, -6], [22, 1], [23, 12], [3, -17], [-10, -9], [25, -11], [-1, 7], [8, -4], [-13, 13], [-12, -2], [-38, -8], [-16, 0], [43, 6], [42, 0], [-40, 9], [-6, 18], [-9, 11], [-42, -2], [-27, 7], [-25, -9], [-7, -15], [12, 22], [16, 17], [-5, 28], [28, 9], [41, -5], [-12, -12], [52, 0], [-28, -10], [7, -19], [-18, 21], [-25, -9], [50, 2], [28, 0], [11, 9], [-30, -12], [33, 1], [6, 10], [31, -15], [-36, 4], [-9, 9], [0, 16], [23, -4], [-7, -30], [15, 23], [-38, -16], [11, 26], [-17, -7], [4, -18], [55, -7], [15, 5], [-21, 15], [10, 24], [-23, 17], [0, -18], [32, -16], [-14, 0], [22, -7], [-29, -4], [-44, -4], [-31, -5], [36, 11], [-35, -5], [45, 4], [-30, -6], [4, -8], [-6, -8], [-22, 7], [-15, 23], [9, 35], [33, 6], [16, 4], [-46, 13], [54, -8], [-25, -1], [5, -25], [0, 0], [8, 6], [-12, 9], [-46, 4], [16, 22], [35, 4], [15, 16], [12, 2], [21, 3], [18, -4], [26, 12], [-17, -31], [-2, 0], [0, 13], [-9, -15], [32, -11], [-24, 10], [27, -9], [5, 26], [-15, 18], [27, -19], [16, 6], [28, -14], [9, -30], [36, -6], [57, -3], [32, -4], [-42, 10], [0, -8], [-38, -14], [-40, 6], [30, 2], [-52, 9], [58, -12], [-38, -4], [-27, 12], [2, -1], [-31, -10], [42, 12], [-40, 8], [4, -4], [-30, 0], [-58, 4], [-4, -10], [34, -5], [-35, 18], [36, -20], [25, -2], [7, 18], [42, 7], [-3, 15], [49, -3], [-30, 17], [14, -12], [45, -3], [-25, -5], [-33, 1], [-37, -7], [-25, -5], [12, 39], [-6, 40], [-5, 6], [4, 6], [-7, -7], [4, 5], [9, -40], [29, -1], [18, -18], [-38, -23], [21, 23], [18, 26], [-4, 24], [53, -9], [-59, 7], [-12, 0], [-25, -8], [-45, -17], [44, 6], [27, 32], [30, 19], [55, -3], [-9, 4], [0, -20], [6, 2], [6, 28], [3, -8], [10, -24], [13, -29], [-21, -9], [10, -14], [-14, -40], [34, 20], [34, 8], [-9, -17], [18, -22], [32, 29], [-33, -5], [74, -4], [8, 17], [-4, -13], [-9, 16], [6, 9], [46, -5], [-22, -18], [12, 8], [71, 3], [-58, 8], [40, -24], [6, -14], [1, 34], [1, -18], [-8, 29], [4, 9], [-43, -5], [6, 0], [3, -16], [-27, -21], [-24, 4], [-13, 3], [15, 34], [-3, -10], [-25, 11], [32, -18], [-35, 26], [31, -10], [5, 7], [73, -12], [78, 7], [-18, -30], [32, -16], [-19, 25], [-46, 16], [20, -12], [-14, -4], [-74, -11], [-24, -33], [46, 18], [-14, 7], [-52, 16], [21, 19], [82, 4], [-39, -29], [-67, -12], [30, -14], [2, 45], [11, 45], [-55, 11], [-21, -16], [-23, -35], [68, 12], [-8, -32], [-30, 20], [-76, 12], [-33, 32], [-74, -4], [-37, -19], [-8, 31], [67, 9], [52, 2], [39, 6], [-22, 14], [-2, 34], [74, -6], [-22, 19], [22, -4], [-22, -30], [30, -14], [-31, 21], [61, -4], [-48, 4], [-19, 29], [-50, -9], [20, -26], [28, 1], [-30, -33], [28, -27], [15, 4], [-5, 36], [-8, -16], [-45, -17], [-14, -42], [72, -8], [3, -7], [-14, 14], [-22, 1], [-8, 6], [-18, 34], [-6, 15], [34, -8], [-47, 10], [4, -36], [0, -19], [-18, 41], [-41, -3], [13, 35], [-28, -8], [37, -2], [6, 11], [-12, -4], [2, 13], [-47, 32], [42, -4], [-13, 38], [-16, 22], [-32, -24], [-10, -4], [-48, -15], [36, 27], [0, -20], [-21, 14]];
    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_w();".
// 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_w(:prec:=2)
    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_w();".
// 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_w( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4002_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![-3, 1]>,<7,R![-3, 1]>,<11,R![-2, 2, 1]>],Snew);
    return Vf;
end function;