
// Make newform 8024.2.a.r 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_r();".
// 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_r();".
// 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, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0], [-1, 2]];
    Rf_basisdens := [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_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], [1, 0], [-2, -1], [0, 1], [3, -1], [-1, 1], [-1, 0], [-1, -2], [7, -1], [-4, 3], [7, 1], [-8, 0], [5, -2], [-3, -1], [6, 2], [-6, 3], [-1, 0], [-9, -1], [5, 1], [-4, 0], [3, -3], [6, 1], [1, 1], [-2, 4], [-2, 0], [-3, -3], [2, 0], [3, 6], [-13, -3], [0, 0], [-2, -3], [-6, 0], [3, -4], [0, 0], [10, 2], [-3, 1], [-1, -9], [0, 0], [-14, -1], [-10, 0], [-5, 3], [-18, 1], [-6, 6], [3, -8], [-10, 0], [14, 3], [0, 10], [-18, 2], [-9, 5], [-12, 2], [5, 9], [-6, 7], [-13, 2], [5, -2], [-15, -4], [0, -7], [-2, -4], [0, -13], [-28, 1], [-21, 0], [1, -7], [-2, 5], [3, -4], [-12, -3], [1, 1], [-8, 2], [11, 10], [-9, 5], [-3, -7], [-20, -2], [10, 10], [4, 3], [-7, 5], [18, -4], [15, 4], [26, -2], [-12, -2], [4, 12], [-2, -12], [-16, -10], [6, 0], [-4, 10], [-31, 1], [7, 8], [-4, -12], [0, -10], [-1, 14], [-23, -7], [16, -2], [11, 3], [15, -3], [-24, -4], [-20, -7], [-3, 6], [5, 0], [6, -10], [-7, 5], [30, 0], [11, 12], [-37, 3], [-12, -12], [-14, -13], [23, -3], [-34, 0], [-3, -3], [-13, -6], [-26, 0], [-7, 0], [-8, -5], [-14, -14], [38, -1], [16, -10], [5, -4], [11, 12], [2, -10], [28, -2], [3, -6], [28, -7], [16, -3], [10, 2], [-6, 15], [-6, 8], [-2, 0], [29, 3], [25, 5], [12, 2], [2, 9], [-26, 0], [32, 8], [28, 2], [8, -14], [-26, 6], [34, -4], [4, -9], [25, 4], [-24, 0], [-19, 3], [6, -10], [-38, 4], [14, 12], [3, -9], [-13, -3], [-11, -7], [-20, -12], [-16, 11], [23, -3], [-10, 13], [-23, -15], [15, -17], [17, -1], [-12, 11], [17, 13], [-3, 14], [-11, 7], [21, 0], [36, 9], [15, 11], [-3, -3], [-6, -8], [-30, 0], [13, -4], [-10, 16], [-12, -18], [-9, -20], [28, 8], [6, 0], [-7, 1], [2, 1], [22, -2], [-14, -10], [-10, 10], [-8, 4], [40, 5], [3, -23], [18, -3], [-15, 12], [-27, 14], [12, 6], [-8, 0], [16, -11], [0, 11], [-1, 26], [-43, 1], [17, -6], [-6, 22], [41, -1], [28, -6], [10, -12], [7, 17], [-22, 16], [-16, 0], [-23, -13], [1, 21], [4, -14], [21, 12], [-28, -6], [20, -6], [-19, 13], [6, -18], [12, -6], [4, -22], [-34, 13], [32, 6], [-39, 3], [23, 12], [14, -10], [-39, -9], [17, -1], [13, 3], [-17, -1], [-18, -2], [-10, -25], [14, -19], [-23, 18], [58, -1], [-9, 11], [-40, 0], [24, -10], [37, -9], [-12, 10], [-2, -4], [-19, 13], [-10, 10], [6, -25], [-23, -7], [13, 9], [-9, -24], [19, 11], [-47, -9], [-3, 0], [9, -27], [-27, 13], [4, 0], [6, -18], [22, -20], [-2, -13], [19, 5], [-4, 2], [-25, -13], [0, 4], [7, -10], [-65, -1], [20, -11], [30, 5], [11, 2], [20, -12], [-45, 3], [40, 8], [1, 14], [-8, 7], [16, 19], [67, -1], [5, -17], [-1, 2], [-44, 3], [18, -2], [-36, 11], [13, 23], [12, -16], [18, -24], [-15, 19], [7, -6], [36, -11], [14, -8], [45, -8], [-13, -9], [-16, 3], [-35, -3], [41, -4], [14, 21], [-7, -11], [16, 24], [14, 22], [-4, 25], [13, 30], [-40, -10], [46, -6], [20, -15], [3, -1], [-65, 4], [-2, 22], [48, 10], [-28, 6], [-10, 10], [23, 3], [-15, -7], [-3, 27], [42, -9], [2, -12], [-39, 18], [66, 4], [-8, 0], [-28, 6], [9, -11], [22, -19], [-6, 28], [10, -10], [4, 3], [-4, -4], [7, 1], [-19, -4], [4, 0], [32, 2], [46, -18], [47, 10], [15, -11], [9, -10], [20, -18], [-31, 5], [24, -10], [-42, 13], [-36, -3], [27, -10], [55, -3], [32, -15], [-1, -21], [-4, -14], [48, 1], [24, 22], [-14, -28], [44, 4], [1, 35], [-70, 3], [-16, -1], [37, -12], [-13, 17], [59, -9], [-46, -2], [-34, 18], [-9, 17], [-38, 10], [-18, -8], [-13, 11], [-23, -6], [-10, 2], [-1, -16], [26, 25], [0, 6], [-4, -18], [32, -13], [60, -6], [-50, -20], [33, 23], [-10, -10], [-37, 1], [-10, -10], [-45, 1], [-7, 14], [22, -6], [-1, 19], [57, 11], [-49, -2], [32, -17], [38, -16], [-48, -10], [-75, -9], [38, 6], [-25, -30], [-13, -10], [0, 37], [3, 37], [65, 10], [-16, -33], [-64, 6], [-6, -18], [27, 5], [-50, 4], [-64, 5], [46, 6], [-30, -14], [41, -1], [5, -21], [14, 16], [-53, -9], [30, -29], [65, 7], [36, 0], [-42, 4], [5, -9], [5, 36], [-6, 1], [-28, 6], [-10, -6], [30, -2], [-21, -24], [-34, 16], [12, -3], [-60, -6], [-4, -28], [-25, -31], [-70, 4], [-65, 9], [-15, 13], [-21, -3], [26, 2], [41, 23], [-56, -3], [47, 0], [-27, 14], [-6, 1], [-42, 8], [16, -14], [0, 7], [-10, -32], [20, 3], [-24, -24], [58, -10], [-3, -34], [52, -14], [-20, -20], [51, -8], [-8, 14], [38, 10], [-19, 34], [-45, 4], [44, -13], [-5, -9], [24, 20], [-28, 24], [14, 33], [-25, -23], [16, 30], [16, -16], [3, -14], [37, 6], [-26, 13], [68, 0], [60, 14], [37, 6], [-72, -8]];
    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_r();".
// 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_r(: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_r();".
// 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_r( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_8024_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![-1, 1]>,<5,R![-1, 4, 1]>,<7,R![-5, 0, 1]>],Snew);
    return Vf;
end function;