
// Make newform 8002.2.a.b in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_8002_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_8002_2_a_b();".
// 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_8002_2_a_b();".
// 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_8002_a();"
function MakeCharacter_8002_a()
    N := 8002;
    order := 1;
    char_gens := [3];
    v := [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_8002_a_Hecke(Kf)
    return MakeCharacter_8002_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], [0], [0], [-2], [4], [0], [1], [3], [-4], [0], [6], [-8], [-12], [-10], [-13], [7], [12], [-16], [8], [4], [10], [-4], [15], [10], [7], [13], [11], [-18], [-6], [-18], [-12], [-13], [0], [-16], [-9], [-10], [8], [-8], [-24], [-26], [-19], [-27], [12], [6], [24], [11], [-28], [-2], [14], [-16], [4], [5], [12], [31], [-15], [32], [-8], [-20], [-6], [-6], [-15], [23], [-27], [22], [31], [16], [13], [3], [-25], [2], [-33], [3], [-22], [23], [-4], [16], [-36], [-18], [-23], [-14], [13], [-12], [-31], [22], [-12], [-9], [-22], [33], [28], [12], [-16], [23], [26], [36], [16], [0], [-28], [22], [20], [-16], [4], [6], [33], [3], [7], [8], [-24], [0], [22], [38], [16], [-15], [31], [-28], [-12], [21], [4], [25], [0], [-3], [13], [-9], [-14], [34], [-50], [9], [6], [8], [35], [-24], [49], [-46], [-23], [15], [48], [45], [-3], [34], [-18], [-38], [4], [48], [36], [-14], [-26], [36], [49], [42], [12], [-46], [18], [36], [-2], [-28], [30], [32], [24], [33], [14], [-20], [-36], [28], [-27], [-32], [6], [13], [-10], [11], [-52], [-44], [38], [6], [-58], [-6], [-16], [-12], [31], [-28], [-15], [36], [-44], [1], [-16], [-4], [-34], [17], [54], [4], [27], [-7], [-14], [20], [2], [-15], [-2], [9], [-46], [18], [32], [18], [4], [13], [46], [0], [36], [-2], [-34], [54], [44], [-35], [-50], [-58], [-12], [34], [-30], [47], [35], [-39], [-66], [-27], [-53], [-29], [70], [-34], [-16], [4], [61], [28], [3], [-14], [28], [-38], [10], [-42], [-42], [39], [24], [62], [-39], [-42], [-49], [-29], [-60], [-4], [3], [35], [-2], [20], [41], [-55], [32], [-24], [16], [60], [-6], [55], [-21], [-70], [80], [-27], [-56], [-50], [18], [23], [-72], [-4], [40], [-33], [-30], [5], [68], [-24], [-34], [2], [-49], [-1], [-16], [-20], [-41], [66], [-36], [-54], [-58], [2], [-13], [-11], [62], [-80], [-47], [-55], [51], [-41], [82], [-49], [7], [80], [-42], [-42], [-28], [76], [36], [71], [-63], [-38], [-21], [78], [27], [32], [-71], [24], [-30], [32], [1], [28], [20], [47], [-9], [-15], [5], [40], [-50], [31], [-16], [-63], [30], [83], [-77], [38], [-87], [40], [48], [41], [9], [61], [-42], [-16], [8], [-8], [48], [18], [-92], [-16], [-4], [-64], [-36], [18], [38], [66], [-54], [-18], [35], [-72], [27], [-26], [29], [-6], [-3], [-42], [-84], [4], [-58], [-32], [88], [52], [12], [-55], [52], [-58], [36], [27], [-20], [-85], [32], [-58], [69], [92], [-64], [-74], [58], [26], [-63], [48], [-60], [14], [63], [-60], [-22], [74], [63], [-92], [48], [24], [-7], [-52], [-78], [-64], [91], [83], [42], [-59], [42], [-46], [32], [54], [12], [73], [42], [22], [-48], [-76], [-51], [-51], [26], [6], [-58], [-96], [-14], [-50], [-82], [-62], [-1], [48], [29], [-98], [8], [10], [-85], [-14], [-34]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_8002_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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