
// Make newform 8040.2.a.m in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_8040_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_8040_2_a_m();".
// 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_8040_2_a_m();".
// 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_8040_a();"
function MakeCharacter_8040_a()
    N := 8040;
    order := 1;
    char_gens := [6031, 4021, 2681, 3217, 5161];
    v := [1, 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_8040_a_Hecke(Kf)
    return MakeCharacter_8040_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], [1, 0], [0, 1], [4, -1], [0, 2], [-4, 2], [4, -2], [-4, 0], [2, 2], [2, 2], [2, -3], [6, 0], [8, -2], [0, -2], [-2, 2], [-2, -4], [-2, 3], [1, 0], [10, -1], [-6, -2], [-2, 0], [-12, 1], [10, -5], [12, 1], [6, -5], [8, -2], [8, -4], [10, 1], [2, -1], [0, 2], [10, -2], [-2, -1], [-6, 6], [-10, 2], [-8, -1], [-6, 8], [-16, 1], [0, 0], [-16, 3], [8, -4], [2, -10], [0, -2], [22, -2], [-6, -4], [4, -7], [-8, 4], [8, 2], [-4, -4], [-2, -9], [-2, -1], [-20, 6], [10, -5], [0, 5], [16, -6], [-8, 8], [-2, 6], [14, 0], [18, -5], [2, 0], [0, 3], [24, -3], [8, 5], [12, -8], [0, 5], [-20, 2], [-6, -1], [-4, 6], [4, -12], [2, 8], [6, -4], [-2, 9], [-16, 8], [20, 2], [-2, -10], [28, -1], [-10, 6], [22, -5], [-10, 14], [10, 8], [-6, -2], [18, 6], [-14, 1], [0, -2], [-20, -1], [-20, -4], [-18, 11], [-14, 2], [6, 10], [0, -12], [0, -9], [-6, 5], [8, -17], [-2, 6], [6, -3], [-4, -7], [-14, 0], [-14, 2], [0, 3], [22, 4], [28, 0], [12, -6], [-28, 2], [-26, -3], [8, 8], [0, 2], [-20, -2], [-30, 7], [36, -2], [34, -9], [0, 6], [14, -20], [28, -4], [16, -6], [2, 14], [14, -4], [-12, -4], [-12, -5], [-14, 10], [18, 6], [2, 4], [20, 6], [-30, 4], [4, 10], [24, -2], [10, 1], [2, 2], [-30, -6], [-40, 0], [-16, 10], [-14, 6], [0, 2], [16, -16], [16, 6], [6, -9], [14, 10], [16, -19], [20, 6], [-24, 15], [-14, 0], [-14, 19], [-6, 22], [-40, 6], [-24, -9], [-14, -14], [-6, 6], [34, 4], [26, -1], [28, -4], [4, 2], [2, -16], [42, -5], [-12, -4], [16, 0], [-32, 11], [22, -5], [10, -16], [10, -14], [16, 3], [18, -24], [-28, 13], [-20, 22], [0, -14], [14, -14], [4, 10], [-24, 0], [-10, 8], [38, -11], [-2, 1], [-6, -12], [14, -8], [6, 14], [-30, -6], [-16, 9], [-14, -6], [18, -18], [-42, 5], [-34, 12], [4, -1], [-58, -2], [-40, 10], [-14, 4], [18, 0], [12, 14], [-20, -15], [-38, -2], [12, 4], [-44, 6], [-38, 18], [-20, -10], [6, 4], [12, 1], [6, 11], [2, -11], [-4, 6], [-8, -14], [-18, 1], [-24, 2], [-26, -8], [-8, -16], [-22, -10], [16, 11], [8, -4], [30, -4], [-36, 0], [16, -14], [-20, 9], [52, -9], [-6, 5], [42, -9], [22, 12], [-42, 7], [-16, 8], [-4, -4], [-48, 12], [30, -16], [-32, -10], [22, -13], [36, -11], [-8, 23], [2, 20], [-32, -8], [-38, 0], [0, -20], [-20, 12], [-38, -6], [12, -10], [-16, 22], [40, -22], [0, 24], [-12, 22], [-6, -11], [-40, 15], [34, 4], [-48, -1], [-24, 12], [-10, 9], [40, -9], [-34, 26], [6, -22], [-8, 7], [42, -1], [20, -13], [22, -17], [2, -27], [24, 4], [16, 0], [28, -3], [-18, 14], [-12, -17], [14, -11], [-10, 27], [-52, -5], [-22, 31], [-6, -6], [-48, 15], [26, -1], [-24, 11], [-48, 1], [-14, -6], [16, 4], [-12, 11], [-18, 9], [48, -14], [-48, 0], [16, -24], [-42, 13], [-2, -8], [8, 4], [-46, 18], [-6, 23], [-56, -6], [-4, -9], [28, 12], [42, -10], [-16, -23], [-60, 12], [6, -6], [2, 3], [-40, -4], [44, -20], [4, 17], [-36, -9], [-6, 1], [32, -12], [10, -13], [34, 16], [-4, -11], [-58, 12], [-54, 14], [-22, 18], [-44, 20], [12, -8], [-26, 24], [40, -14], [-42, 4], [18, 18], [38, 8], [46, -10], [-24, -12], [20, -21], [-12, 21], [4, -1], [-4, 11], [4, 4], [48, 10], [12, -36], [-10, -28], [26, -18], [20, -6], [52, -13], [-38, -2], [-10, 9], [24, -29], [-40, 0], [-42, 16], [-2, 8], [-36, 12], [18, -24], [-38, 18], [-16, 26], [18, -14], [-22, 2], [-16, -25], [-12, 20], [62, -11], [44, 6], [-8, 25], [4, 15], [-58, 22], [-14, -16], [12, -11], [-6, -20], [20, 14], [48, 0], [-52, 15], [-66, 4], [-44, 16], [38, -6], [-8, 18], [-10, -24], [-8, -16], [-78, 7], [-12, -19], [-40, -3], [52, 16], [66, 0], [-36, -10], [-58, -14], [-42, -14], [16, -28], [-52, -15], [58, -4], [-52, 20], [18, 24], [-14, -3], [-2, 5], [64, -11], [10, -39], [68, -9], [26, -25], [-2, -3], [4, 8], [18, 14], [-24, -9], [-56, 7], [20, -5], [-8, 26], [22, -16], [44, -19], [48, -6], [28, 16], [-6, -20], [-34, 8], [-44, 16], [26, 14], [-24, -18], [6, -14], [2, 32], [-6, 24], [-22, 31], [-30, -15], [-68, 12], [14, -21], [70, -7], [-12, 3], [46, -24], [-10, -3], [-32, 28], [-36, -13], [-66, 4], [-30, 20], [-46, 10], [-12, 22], [-4, -2], [-10, -28], [-8, 1], [6, 12], [22, -7], [-38, -4], [14, 4], [-44, -2], [-4, 11], [-34, 43], [58, 17], [-30, 16], [60, -10], [-6, 36], [-16, 9], [20, 3], [50, -30], [12, 9], [32, -15], [-8, 20], [-36, -5], [-2, -18], [10, -34], [24, -12], [36, 6], [12, 36], [-10, -17], [10, 7], [-48, 0], [-20, 9], [0, 25], [0, -10], [-16, 7], [-14, 0], [16, 4], [8, -30], [-14, -28], [64, -20], [56, 14], [40, -8], [-2, -17], [14, 4], [22, -26], [30, 3], [16, -32], [12, 5], [20, 1], [-14, 8], [18, 19], [-6, 21], [2, -24], [8, -10], [-4, -32], [4, 33], [62, -24], [-54, 24], [24, 2], [-8, 36], [-40, 43], [-6, -4], [82, -2], [42, -27], [90, 7], [14, 26], [-22, 5], [18, -20], [50, -10]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_8040_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_8040_2_a_m();".
// 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_8040_2_a_m(:prec:=2)
    chi := MakeCharacter_8040_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(3259) - 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_8040_2_a_m();".
// 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_8040_2_a_m( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_8040_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![-4, -1, 1]>,<11,R![8, -7, 1]>,<13,R![-16, -2, 1]>,<17,R![-8, 6, 1]>],Snew);
    return Vf;
end function;