
// Make newform 8024.2.a.q in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_8024_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_8024_2_a_q();".
// 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_8024_2_a_q();".
// 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 := [-4, -1, 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_8024_a();"
function MakeCharacter_8024_a()
    N := 8024;
    order := 1;
    char_gens := [2007, 4013, 3777, 3129];
    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_8024_a_Hecke(Kf)
    return MakeCharacter_8024_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, 1], [0, -1], [-2, 1], [2, -2], [-2, 0], [1, 0], [0, 1], [4, -2], [4, 1], [0, 4], [-2, 0], [-6, 1], [-8, 0], [0, -2], [-2, 1], [-1, 0], [-2, -2], [4, 0], [6, -6], [4, -6], [2, -1], [12, -2], [-2, 6], [4, -6], [-6, 8], [-8, 6], [-8, -3], [6, -8], [0, 0], [-8, -3], [-6, 0], [-6, 1], [-4, -4], [-2, 6], [0, -6], [-6, -2], [12, -4], [-6, 1], [-2, 0], [-8, 4], [0, 5], [8, -6], [-14, 1], [-12, 2], [6, 1], [-22, -2], [0, -8], [14, 2], [-14, -2], [-16, -2], [8, -9], [-10, 11], [-16, 3], [14, 3], [4, -1], [22, -2], [-4, 5], [0, 11], [-6, -7], [-10, -6], [-22, 7], [0, 11], [2, 9], [4, 4], [4, 6], [-8, -3], [8, 2], [2, 12], [6, 10], [18, 0], [-4, -3], [20, -6], [-6, 8], [-24, -5], [-8, -8], [30, -4], [-22, 6], [-24, -2], [-14, 0], [2, 4], [2, 6], [-8, 8], [2, -11], [6, -6], [4, 0], [-34, 3], [-6, 4], [-22, 0], [-8, 14], [-24, 2], [-10, 2], [-2, 13], [16, 7], [4, 1], [-12, 14], [22, -6], [-26, 0], [8, -5], [14, 4], [24, -4], [14, -3], [-16, 18], [-10, 6], [-22, -4], [-6, 15], [12, 4], [-10, -5], [-20, 1], [8, -16], [-26, 9], [26, -6], [26, -1], [-16, 15], [-20, 12], [-6, -12], [20, -11], [0, -5], [48, -1], [-16, -6], [-42, 1], [16, -8], [-4, -10], [34, 4], [6, 8], [14, -6], [-28, -3], [-24, -2], [-4, 8], [26, -8], [-4, 6], [-2, -10], [8, -6], [2, 3], [2, 15], [-2, 14], [14, 14], [16, 0], [-42, -4], [12, 0], [26, 0], [-10, 10], [32, 0], [12, 0], [10, -9], [-16, 12], [4, -7], [-28, 6], [-16, -2], [8, 6], [-12, -15], [-16, -6], [-8, 7], [8, 6], [12, 13], [14, -3], [-8, -2], [-32, 0], [-2, 16], [30, -8], [-24, -7], [6, 4], [8, 0], [-24, -5], [-2, 4], [-36, 6], [4, 10], [8, -11], [-36, 2], [22, 0], [-36, 8], [-26, 8], [-6, 9], [-18, -12], [4, -15], [58, -1], [-8, 3], [-14, -14], [36, -4], [26, -19], [12, -11], [44, 5], [22, -24], [-38, 1], [-24, 0], [18, 2], [-50, -2], [12, 6], [4, 24], [-44, 4], [24, -18], [-26, 20], [24, -12], [14, -20], [-28, 1], [4, -18], [2, -4], [-14, 16], [-8, -10], [16, 2], [-8, -6], [-22, 11], [-2, -8], [-6, 6], [32, 3], [26, 14], [-32, 8], [-4, -8], [-4, 28], [8, -24], [16, -20], [-30, 9], [50, -1], [-8, 19], [46, -3], [16, 2], [12, -20], [-10, 20], [-16, 24], [18, -12], [-38, 14], [-12, 28], [30, -26], [30, 9], [-16, -8], [50, -10], [22, 9], [20, -20], [-24, -4], [28, -27], [34, 4], [26, -16], [32, -8], [-38, 14], [24, 18], [16, 7], [-20, -16], [22, -8], [-34, -6], [-44, -12], [-20, -3], [8, 0], [-8, -25], [2, 3], [-70, 3], [-2, 6], [-24, 12], [-26, -14], [48, -9], [16, -15], [-30, -1], [-40, 22], [64, 2], [-14, 3], [34, -17], [36, 16], [8, -7], [2, -18], [26, 2], [10, 0], [28, -10], [8, -27], [4, 3], [-56, -2], [22, -5], [-12, -10], [6, -9], [50, 6], [12, 25], [2, -29], [-10, 10], [44, 0], [22, 8], [-20, 15], [22, -11], [48, -18], [36, -24], [-2, 29], [62, -6], [40, 15], [-20, 20], [44, -26], [36, -12], [2, 18], [14, -6], [-24, 30], [8, -10], [-4, -17], [-48, 18], [6, -23], [-22, 24], [-68, -4], [38, 16], [-34, 26], [-56, -3], [14, 2], [-24, 8], [-10, 35], [18, -28], [0, 2], [22, -21], [-22, 26], [-4, -22], [18, -8], [-16, 5], [-64, 0], [24, -5], [-10, -18], [8, -18], [-42, 0], [22, 15], [-8, -5], [-26, 11], [-20, 16], [36, -1], [-42, 14], [-36, 4], [-18, 1], [22, 8], [50, -4], [-8, 20], [-12, 8], [50, -3], [32, -15], [38, 3], [-50, 22], [50, -6], [-4, 0], [-40, 14], [-38, -6], [6, 18], [10, -30], [20, 0], [8, 7], [-48, 28], [40, 15], [-58, 3], [48, 0], [72, -10], [8, 5], [34, 4], [-18, -14], [-30, 26], [-56, 14], [34, 16], [22, -8], [74, -8], [-8, 31], [-24, -4], [42, -4], [-20, 12], [30, 7], [-6, -17], [28, 16], [22, 4], [-74, 0], [24, 20], [0, -31], [-2, 7], [-8, -29], [38, -6], [-42, 9], [20, 1], [18, -18], [76, 0], [-26, 34], [-26, -26], [80, 3], [-72, 4], [-60, 16], [-18, -2], [12, 4], [18, -12], [24, 14], [-16, -29], [10, 6], [52, 10], [2, 18], [38, 14], [26, -9], [20, -11], [6, -10], [30, -2], [-20, -14], [-20, -21], [-12, 10], [-8, -1], [22, -16], [-40, 32], [-72, 4], [-44, 0], [-58, 10], [-56, -4], [24, 12], [32, -24], [-24, -10], [36, -37], [38, 17], [-16, -29], [-50, 15], [44, -22], [46, -26], [-24, 17], [2, -24], [-62, -5], [-56, -4], [-54, -6], [-2, 17], [12, 26], [-24, 20], [-26, -11], [-34, -4], [-12, -2], [40, 7], [-14, -19], [40, -11], [-4, 32], [-32, -14], [-32, 16], [52, -11], [26, -2], [-20, -26], [-26, -22], [16, 1], [-22, 1], [42, -29], [-26, 26], [-102, 0], [16, 31], [6, -30]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_8024_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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