
// Make newform 8046.2.a.a in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_8046_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_8046_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_8046_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_8046_a();"
function MakeCharacter_8046_a()
    N := 8046;
    order := 1;
    char_gens := [299, 3727];
    v := [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_8046_a_Hecke(Kf)
    return MakeCharacter_8046_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 := [[-1], [0], [2], [1], [0], [-2], [4], [4], [-5], [1], [-5], [-6], [-9], [-4], [-6], [1], [2], [-6], [0], [-3], [-6], [-8], [6], [1], [10], [14], [7], [-16], [-17], [-4], [-8], [0], [21], [-9], [-1], [20], [-21], [-11], [-16], [13], [-3], [-5], [10], [-4], [24], [20], [19], [-14], [18], [-10], [17], [-9], [-22], [-25], [5], [30], [-23], [16], [-27], [-2], [-3], [7], [6], [31], [20], [-2], [4], [31], [-5], [19], [-10], [16], [29], [23], [-10], [-24], [26], [2], [24], [-38], [-23], [-26], [26], [-2], [10], [-27], [-27], [-34], [2], [13], [-20], [13], [32], [24], [-9], [12], [6], [18], [-22], [-18], [-18], [-13], [-13], [-21], [-12], [7], [12], [-17], [-16], [-23], [-34], [32], [18], [-20], [16], [-34], [2], [31], [-4], [-19], [-3], [46], [14], [-12], [-6], [10], [-46], [-28], [22], [-17], [-8], [-33], [29], [25], [-22], [10], [-19], [46], [-34], [-54], [12], [33], [-44], [-12], [13], [-9], [-10], [-24], [-18], [34], [-18], [3], [20], [10], [48], [4], [7], [10], [32], [-25], [33], [-5], [-32], [0], [-18], [-39], [2], [-10], [-30], [-6], [55], [56], [-35], [14], [5], [18], [35], [-28], [5], [48], [-28], [-44], [9], [-48], [-1], [30], [27], [50], [-13], [-52], [-43], [-27], [39], [28], [3], [-60], [19], [-41], [22], [26], [9], [20], [-10], [-64], [63], [-49], [56], [-30], [-41], [-9], [-4], [-12], [28], [22], [18], [-10], [16], [22], [-10], [48], [7], [-70], [-42], [-65], [-53], [8], [-47], [51], [-14], [33], [-24], [-10], [-4], [50], [-34], [24], [-6], [2], [12], [-56], [12], [7], [-17], [53], [44], [-50], [40], [-57], [32], [41], [-48], [21], [24], [-5], [39], [4], [-54], [-13], [-54], [42], [-39], [48], [-56], [34], [9], [55], [4], [-66], [73], [46], [-46], [-17], [-57], [23], [36], [47], [52], [-20], [20], [-54], [-36], [4], [39], [30], [36], [-48], [66], [-12], [-2], [-6], [9], [43], [26], [-21], [34], [66], [70], [21], [36], [13], [-64], [-34], [56], [48], [-12], [25], [42], [-43], [24], [-72], [81], [47], [24], [-1], [44], [88], [-30], [-84], [-16], [-53], [-32], [22], [-32], [42], [-8], [78], [47], [-11], [52], [19], [-75], [-24], [0], [-40], [-50], [20], [73], [-33], [45], [46], [-30], [-66], [-6], [-32], [24], [44], [-29], [22], [-80], [-42], [-31], [87], [60], [-24], [50], [-66], [60], [70], [42], [62], [-42], [-6], [-60], [-84], [-28], [22], [-14], [-47], [-68], [12], [-34], [-7], [87], [-14], [24], [49], [-13], [44], [-42], [48], [25], [-46], [-23], [-24], [-45], [-6], [-84], [50], [30], [-26], [-57], [36], [71], [-70], [38], [29], [-25], [66], [-90], [-60], [-44], [19], [0], [78], [74], [98], [54], [-100], [64], [52], [26], [93], [60], [82], [-43], [43], [39], [-30], [77], [-16], [-24], [-84], [-52], [12], [-48], [18], [-4], [-42], [40], [-24]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_8046_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_8046_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_8046_2_a_a(:prec:=1)
    chi := MakeCharacter_8046_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_8046_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_8046_2_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_8046_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![-2, 1]>,<11,R![0, 1]>],Snew);
    return Vf;
end function;