
// Make newform 8041.2.a.a in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_8041_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_8041_2_a_a();".
// 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_8041_2_a_a();".
// 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
        Kf := Rationals();
    end if;
    return [Kf!elt[1] : elt in input];
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_8041_a();"
function MakeCharacter_8041_a()
    N := 8041;
    order := 1;
    char_gens := [6580, 2366, 562];
    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_8041_a_Hecke(Kf)
    return MakeCharacter_8041_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 := [[-2], [-2], [2], [3], [1], [-2], [1], [-8], [-4], [-3], [8], [6], [11], [-1], [-5], [1], [-3], [2], [-9], [2], [-7], [4], [0], [11], [-8], [0], [1], [-3], [-11], [-2], [16], [5], [-15], [7], [6], [-2], [23], [2], [-12], [-3], [5], [-2], [8], [-6], [26], [-16], [-1], [-16], [-13], [15], [-3], [-14], [-27], [4], [21], [-24], [-10], [-22], [1], [-32], [-12], [4], [2], [-30], [-2], [12], [-8], [-9], [7], [8], [-25], [-2], [28], [38], [-10], [-16], [15], [-18], [-28], [-12], [-40], [17], [21], [15], [-1], [-4], [18], [18], [0], [31], [21], [-21], [12], [6], [-20], [33], [-21], [-28], [6], [43], [-20], [-24], [-20], [4], [31], [-11], [-4], [-22], [-43], [26], [-40], [42], [-6], [0], [-27], [-30], [6], [-3], [-8], [-22], [-29], [11], [-23], [48], [38], [-2], [-4], [20], [13], [44], [-40], [-33], [0], [22], [-22], [-12], [-34], [-23], [42], [-19], [40], [-26], [44], [-15], [-46], [-34], [1], [26], [1], [-24], [-22], [-44], [-41], [8], [32], [20], [26], [0], [2], [15], [-42], [-44], [34], [20], [7], [-60], [-58], [-5], [6], [62], [30], [-47], [-23], [34], [-41], [30], [-9], [8], [36], [44], [20], [13], [26], [42], [-15], [-30], [-50], [-39], [-26], [0], [48], [-40], [-49], [-37], [-48], [-46], [-67], [-44], [45], [54], [30], [1], [-47], [34], [-15], [-2], [-61], [2], [47], [-27], [67], [15], [36], [-45], [-21], [-26], [-12], [54], [-54], [6], [-26], [32], [-51], [-64], [-60], [40], [-62], [18], [-38], [25], [-7], [19], [2], [22], [-56], [-12], [46], [-39], [14], [-19], [-12], [-66], [13], [-43], [-42], [-29], [-16], [42], [62], [54], [64], [46], [-35], [-40], [4], [-30], [-36], [-64], [58], [22], [73], [-42], [55], [31], [-48], [-76], [58], [-15], [-17], [-30], [22], [20], [37], [-44], [76], [-20], [28], [12], [-56], [28], [22], [-2], [35], [68], [29], [-27], [2], [-35], [-72], [-36], [48], [70], [-42], [-35], [-14], [-33], [-32], [42], [-76], [-22], [48], [-2], [45], [64], [-24], [-50], [35], [50], [-41], [44], [-21], [-12], [26], [32], [-80], [26], [-85], [-61], [-30], [30], [-60], [10], [30], [67], [-9], [-7], [88], [-82], [1], [-19], [53], [-40], [12], [-66], [-49], [84], [-34], [-77], [-18], [28], [75], [-13], [-18], [13], [81], [-9], [-12], [25], [15], [-38], [64], [-44], [-89], [-29], [4], [26], [35], [15], [-18], [79], [-47], [5], [-75], [-60], [48], [-39], [34], [50], [85], [-27], [-30], [-48], [0], [6], [-8], [-60], [48], [-40], [36], [-32], [50], [-7], [-61], [73], [-11], [64], [15], [13], [-23], [40], [-52], [84], [47], [-28], [85], [11], [20], [3], [-50], [40], [-25], [-45], [-6], [34], [12], [17], [-81], [2], [-51], [-28], [82], [22], [-89], [-53], [-46], [0], [-63], [26], [-36], [72], [59], [22], [-76], [-12], [-12], [102], [-94], [-26], [21], [-37]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_8041_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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