
// Make newform 8640.2.a.t in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_8640_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_8640_2_a_t();".
// 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_8640_2_a_t();".
// 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_8640_a();"
function MakeCharacter_8640_a()
    N := 8640;
    order := 1;
    char_gens := [2431, 3781, 6401, 3457];
    v := [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_8640_a_Hecke(Kf)
    return MakeCharacter_8640_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], [2], [-4], [2], [5], [5], [1], [2], [7], [6], [0], [-4], [4], [-9], [-14], [11], [-14], [0], [-12], [-3], [1], [0], [16], [-12], [4], [12], [19], [6], [6], [18], [-17], [-12], [16], [8], [-16], [14], [-3], [13], [4], [19], [14], [10], [5], [-16], [19], [-10], [-3], [29], [6], [-6], [11], [24], [27], [-24], [10], [-9], [4], [-14], [-8], [-7], [-22], [-22], [8], [27], [-12], [22], [-20], [3], [26], [18], [-22], [32], [23], [-27], [36], [14], [-30], [25], [-36], [-3], [14], [26], [-17], [-39], [6], [28], [-42], [-18], [13], [28], [40], [6], [25], [-15], [-28], [40], [-10], [-22], [28], [10], [-4], [-10], [5], [-16], [15], [-9], [18], [-11], [-36], [0], [33], [-32], [13], [0], [-6], [33], [-41], [-18], [42], [30], [2], [9], [19], [10], [-26], [46], [32], [0], [15], [0], [-3], [-2], [-30], [-35], [15], [40], [-3], [-24], [-44], [-46], [-20], [39], [2], [-16], [28], [21], [-41], [33], [32], [-54], [22], [-23], [-12], [-2], [32], [-8], [-56], [-34], [53], [6], [-30], [-14], [2], [-3], [-55], [48], [35], [25], [4], [17], [-58], [-4], [17], [30], [-55], [-26], [16], [14], [-28], [0], [-50], [-38], [-1], [-20], [58], [-32], [-31], [34], [-34], [39], [-5], [-56], [-31], [11], [-3], [-24], [14], [56], [48], [28], [-14], [-21], [-6], [46], [-44], [-31], [36], [4], [-50], [28], [56], [-36], [28], [45], [-8], [56], [57], [23], [-50], [55], [34], [-48], [-3], [-31], [35], [-40], [-34], [-20], [-4], [-3], [32], [10], [-18], [27], [9], [-3], [50], [12], [-4], [41], [70], [-61], [2], [-10], [-8], [-74], [-52], [71], [-66], [22], [-33], [41], [-29], [0], [7], [-6], [3], [-30], [16], [-5], [-3], [-44], [59], [-28], [8], [-78], [-54], [26], [-23], [2], [24], [-1], [-4], [-34], [11], [19], [17], [24], [72], [3], [-43], [-19], [-76], [10], [2], [54], [-15], [24], [8], [-21], [18], [-66], [-16], [-60], [-1], [-54], [56], [-42], [24], [15], [-16], [1], [87], [-32], [12], [5], [36], [16], [55], [60], [0], [-58], [11], [-53], [-40], [-72], [-2], [-38], [-17], [32], [-60], [-32], [-83], [54], [71], [88], [-16], [81], [-50], [62], [-32], [-52], [-64], [36], [-47], [-75], [-26], [-12], [-56], [57], [86], [-79], [26], [32], [-7], [-52], [12], [-39], [20], [30], [-70], [-16], [70], [-35], [-90], [-10], [-74], [-19], [10], [-98], [-40], [90], [-60], [-2], [63], [74], [-46], [54], [-11], [-37], [36], [63], [-8], [18], [-10], [-28], [30], [-10], [28], [19], [18], [33], [-60], [-44], [-35], [-78], [12], [40], [34], [3], [-6], [-22], [74], [-16], [-79], [-18], [19], [-36], [-18], [30], [46], [6], [-102], [27], [26], [-42], [-28], [-78], [-74], [-27], [-89], [-67], [-38], [-18], [22], [-32], [75], [99], [-60], [-58], [43], [14], [6], [33], [-21], [-18], [-17], [80], [70], [42], [77], [71], [6], [-8], [-53], [83], [20], [-37], [-1], [54], [57], [-110], [2], [-107], [-40], [0], [65], [25], [-54], [60], [63], [44], [-72], [-78], [75], [-12], [-36], [47], [-35], [-40], [-42], [-31], [-2], [56], [1], [4], [4], [-88], [2], [20], [0], [-65], [36], [-4], [18], [-88], [-8], [15], [-4], [54], [-24]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_8640_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_8640_2_a_t();".
// 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_8640_2_a_t(:prec:=1)
    chi := MakeCharacter_8640_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(3457) - 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_8640_2_a_t();".
// 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_8640_2_a_t( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_8640_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![-2, 1]>,<11,R![4, 1]>,<13,R![-2, 1]>,<17,R![-5, 1]>,<19,R![-5, 1]>],Snew);
    return Vf;
end function;