
// Make newform 8001.2.a.f in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_8001_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_8001_2_a_f();".
// 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_8001_2_a_f();".
// 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_8001_a();"
function MakeCharacter_8001_a()
    N := 8001;
    order := 1;
    char_gens := [3557, 1144, 7750];
    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_8001_a_Hecke(Kf)
    return MakeCharacter_8001_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], [0], [0], [-1], [0], [2], [-1], [-2], [0], [9], [0], [-1], [3], [-6], [-8], [-11], [12], [-10], [4], [-8], [-4], [-13], [6], [-12], [-7], [-4], [-16], [-6], [16], [6], [1], [-1], [6], [-7], [10], [8], [-12], [-15], [2], [-14], [-18], [-21], [-8], [22], [16], [20], [4], [-5], [12], [-17], [11], [21], [11], [-13], [22], [-12], [1], [4], [-20], [3], [-13], [24], [-21], [-4], [27], [34], [2], [10], [15], [-27], [-2], [27], [2], [4], [-36], [3], [10], [-16], [-9], [18], [-28], [-2], [12], [0], [-7], [-14], [-16], [35], [-40], [-36], [-4], [19], [30], [-44], [10], [-1], [-41], [30], [32], [24], [-12], [40], [32], [22], [14], [8], [-17], [0], [-21], [27], [-10], [46], [37], [1], [10], [-35], [38], [-38], [-14], [-4], [-28], [25], [21], [-16], [-37], [22], [21], [-21], [-3], [14], [31], [15], [20], [23], [-4], [-41], [-9], [14], [-34], [12], [36], [37], [27], [13], [-6], [54], [5], [-18], [5], [35], [3], [-42], [-12], [-20], [47], [-2], [11], [16], [-10], [-1], [-31], [-32], [-30], [-60], [4], [-9], [-14], [-42], [-37], [6], [44], [41], [8], [-10], [27], [-34], [-5], [42], [-50], [19], [-37], [-26], [-40], [50], [36], [-10], [-20], [0], [35], [-27], [-19], [-9], [64], [-15], [52], [9], [20], [-35], [-23], [-48], [-46], [-62], [-48], [-17], [-30], [48], [20], [-32], [-18], [46], [0], [45], [60], [-33], [-72], [38], [-10], [-42], [-53], [6], [-28], [40], [13], [53], [7], [21], [22], [-24], [-22], [63], [-26], [-22], [-32], [34], [41], [44], [-15], [45], [-21], [-6], [37], [-64], [42], [-13], [-44], [-29], [-11], [-65], [-38], [-55], [-39], [68], [-15], [-11], [44], [-20], [-4], [-42], [11], [-34], [26], [-12], [46], [78], [-28], [68], [65], [-5], [-69], [8], [59], [0], [62], [-82], [5], [-51], [2], [35], [-38], [40], [-46], [50], [35], [10], [20], [-48], [-20], [-24], [-59], [40], [9], [-60], [-10], [-12], [26], [-22], [73], [39], [-51], [36], [-44], [-18], [-15], [54], [-8], [44], [-34], [22], [60], [-38], [-36], [6], [10], [-88], [8], [-10], [-82], [12], [16], [26], [10], [56], [78], [68], [82], [-8], [-38], [44], [33], [-78], [18], [27], [-17], [56], [-44], [36], [4], [-24], [-50], [-74], [-32], [-76], [-84], [-41], [6], [-33], [14], [-73], [-16], [57], [-4], [76], [-63], [91], [-29], [14], [-13], [-54], [64], [-23], [26], [6], [-12], [11], [29], [49], [18], [-40], [-80], [-67], [-23], [75], [4], [70], [-82], [-60], [79], [-51], [-65], [42], [53], [-39], [-47], [72], [5], [30], [-24], [-81], [-44], [22], [37], [-14], [-6], [-12], [12], [10], [-58], [89], [-72], [96], [-50], [-33], [-56], [-102], [-6], [-38], [-32], [3], [36], [-36], [3], [33], [-64], [0], [-16], [-70], [33], [-87], [-10], [72], [-60], [-78], [-60], [89], [100], [-39], [4], [41], [74], [-108]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_8001_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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