
// Make newform 8024.2.a.p in Magma, downloaded from the LMFDB on 19 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_p();".
// 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_p();".
// 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], [-2, 1], [-2, 1], [-3, -1], [-4, 2], [1, 0], [-3, 0], [-3, 1], [6, -1], [0, -4], [2, -4], [-8, 3], [0, 0], [8, -2], [1, -6], [-1, 0], [-5, -3], [10, 2], [-8, 0], [-3, 5], [-2, 3], [-2, 4], [-2, -2], [7, -5], [-8, 2], [-6, 4], [-2, -1], [5, 5], [-6, 6], [16, -3], [4, -2], [-7, -2], [8, 0], [14, -2], [4, 6], [-2, 2], [12, -4], [2, 9], [-11, -3], [-8, -4], [-8, 5], [16, -6], [-18, -3], [12, -6], [-20, 3], [15, 5], [-9, -3], [-4, -4], [18, -2], [7, 3], [23, -4], [-12, -3], [17, -2], [-7, 4], [1, -2], [-15, -1], [1, -4], [12, -9], [-3, 2], [-4, 4], [13, 0], [19, -4], [6, -11], [2, -10], [-4, 14], [-12, -7], [-1, 7], [5, -11], [-10, -6], [-14, 0], [9, -4], [-17, 11], [-11, 9], [0, 3], [15, -3], [-14, 8], [25, -5], [-19, 5], [-8, 10], [1, 9], [-6, 6], [-3, 7], [7, -12], [12, -4], [24, 4], [-24, 9], [10, -12], [21, 1], [-18, -8], [22, -4], [16, -16], [22, 5], [-37, 0], [-24, -3], [-1, -1], [22, 2], [-16, -2], [3, -12], [2, -8], [-4, 8], [1, 6], [10, -16], [-24, 12], [-20, 2], [-5, 2], [24, 0], [-17, 6], [8, 13], [6, -6], [-36, -5], [-2, -2], [-26, -5], [0, -1], [23, -3], [16, -10], [18, -1], [27, 4], [8, -1], [10, 8], [-22, -3], [9, -21], [-10, 4], [-4, -6], [-11, -3], [16, 8], [-20, -3], [0, 14], [15, -15], [-11, -7], [30, 4], [20, -8], [17, 5], [13, 4], [-1, 6], [-4, 0], [36, 0], [-10, 18], [24, -6], [38, -2], [-43, 1], [-5, 9], [33, -5], [-18, 6], [9, 4], [5, 11], [-26, 7], [3, -5], [-26, 8], [0, -2], [-46, 3], [15, -9], [-41, -4], [-8, -10], [-30, -1], [18, 9], [-24, 14], [21, -1], [-32, 6], [10, -12], [32, 1], [47, -1], [-8, -16], [-5, -4], [-10, -12], [7, -25], [5, -3], [-26, -9], [-34, 8], [-5, 7], [16, -12], [4, 10], [22, 13], [20, 6], [-4, 1], [-22, -9], [12, 15], [-15, -9], [23, 5], [-34, -7], [21, 8], [-2, -5], [7, -5], [6, -11], [56, 0], [-30, 10], [42, -6], [2, 24], [-17, 17], [7, 21], [-33, 11], [4, 6], [-36, 8], [-17, -17], [-22, -13], [-19, 25], [-6, 4], [9, -19], [19, -1], [0, -6], [12, -10], [-18, -9], [-15, 1], [16, -16], [0, -21], [-50, 10], [10, -2], [-26, -2], [30, -14], [-38, 6], [-14, -14], [39, -8], [-48, -7], [-51, 10], [-36, -9], [-42, -4], [-20, -4], [-15, 21], [-5, -7], [-33, -5], [20, 4], [5, -1], [-34, 14], [-54, -3], [36, -4], [-14, -2], [54, 1], [-24, 24], [14, -10], [-8, -23], [-40, -2], [-17, -1], [22, 2], [12, -28], [50, -8], [-28, 27], [-45, 5], [55, -5], [-57, -3], [61, -1], [-2, 3], [-20, -20], [-48, 7], [49, 0], [22, 15], [46, -10], [-27, -5], [47, 5], [8, 23], [21, -24], [41, -12], [14, 16], [34, 0], [-20, 25], [-30, -1], [-4, 8], [14, 3], [35, -15], [-10, 22], [-33, -1], [-43, 9], [-7, 0], [-34, 9], [-34, 16], [-50, -5], [-12, -18], [59, 6], [18, 14], [-28, 1], [13, 4], [54, 2], [24, -28], [34, 12], [-4, 23], [-18, 13], [26, -20], [32, -20], [-19, 26], [22, -14], [41, 10], [-79, 3], [11, -5], [-52, 12], [64, -4], [7, 21], [24, 22], [45, 13], [52, -1], [-16, -6], [15, -12], [26, -24], [4, 20], [-27, 13], [-3, -1], [14, 7], [14, -22], [-21, -15], [-13, -6], [29, 21], [38, -4], [-17, -26], [-48, -12], [-33, -13], [12, 6], [24, -3], [-19, -17], [76, -1], [23, 1], [18, -36], [8, -2], [8, 5], [56, 3], [-38, -1], [-10, 30], [-36, 15], [-6, 2], [10, -26], [20, -13], [2, -20], [-63, 9], [70, -2], [31, 1], [-18, -15], [15, -10], [24, 1], [32, -28], [45, 3], [-24, 20], [12, -14], [-10, -2], [41, 3], [5, -29], [-32, 28], [31, 20], [-10, 14], [34, -3], [-1, 14], [-6, -18], [51, 7], [3, 30], [-2, 24], [-46, -10], [9, 7], [-8, -2], [-14, 24], [7, -21], [-43, 1], [-25, -12], [7, 33], [21, 21], [18, 30], [22, -9], [53, 0], [-53, 21], [-51, 9], [-52, -6], [-38, 18], [80, 1], [-34, 15], [-7, -18], [19, 1], [40, 7], [-8, 21], [-31, 11], [15, -39], [16, 16], [-11, -13], [-47, -2], [5, 27], [-1, -15], [-45, 21], [4, 12], [-42, 32], [-14, 4], [-30, 1], [35, 9], [24, -18], [-4, -16], [-70, 10], [9, -12], [34, 7], [-4, 0], [-38, -14], [29, -11], [56, -1], [21, 21], [65, 2], [49, 17], [-4, 12], [-40, 28], [-14, 10], [3, 25], [-10, -18], [-40, -4], [-4, -4], [-14, -12], [-59, 6], [57, 10], [32, -13], [47, 2], [-32, 22], [54, -2], [-4, -27], [-48, -14], [33, -24], [-93, -3], [38, 6], [-35, 14], [-66, 16], [-62, 18], [-24, 3], [11, -13], [-18, -12], [-52, -13], [79, 4], [-14, -5], [-15, -1], [31, -9], [10, -2], [-31, 20], [-14, -34], [14, -36], [-62, -2], [-32, 25], [-12, -9], [74, -13], [-11, -9], [-16, 2], [-61, 0], [24, -24]];
    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_p();".
// 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_p(: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_p();".
// 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_p( : 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![-2, 3, 1]>,<7,R![-2, 3, 1]>],Snew);
    return Vf;
end function;