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