
// Make newform 8640.2.a.ce in Magma, downloaded from the LMFDB on 23 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_ce();".
// 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_ce();".
// 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], [3], [-2], [5], [-8], [1], [-6], [-2], [0], [-5], [-10], [4], [-4], [2], [-8], [-7], [-9], [-2], [-5], [3], [6], [-12], [-13], [0], [-17], [6], [10], [10], [8], [12], [-6], [-13], [4], [-1], [-2], [-19], [12], [12], [22], [-5], [4], [5], [0], [17], [23], [-8], [-10], [-6], [-24], [26], [1], [6], [0], [28], [-16], [-13], [30], [0], [12], [-18], [16], [24], [-7], [-20], [-21], [7], [10], [-19], [-12], [18], [-21], [11], [-11], [24], [-24], [2], [6], [-7], [32], [-1], [-30], [-38], [32], [-12], [4], [22], [12], [-33], [-28], [-6], [-5], [-22], [16], [-26], [-34], [38], [19], [-33], [37], [-12], [36], [0], [5], [17], [-18], [-22], [10], [-34], [-1], [-1], [18], [-19], [11], [0], [-12], [38], [4], [-32], [-7], [33], [-12], [-18], [-20], [34], [-25], [-30], [16], [22], [-20], [32], [-25], [53], [-18], [-37], [-18], [-41], [16], [-16], [-12], [-48], [-11], [28], [51], [4], [-21], [-26], [7], [4], [-9], [-38], [-19], [48], [-21], [-40], [-40], [-34], [33], [22], [48], [40], [13], [-12], [-62], [-54], [25], [42], [29], [40], [30], [-10], [-48], [-45], [5], [30], [12], [-42], [-11], [-34], [-23], [20], [38], [20], [12], [2], [-3], [13], [-55], [8], [19], [-56], [-9], [-28], [48], [14], [31], [9], [54], [58], [60], [-33], [-3], [49], [-54], [-30], [31], [46], [-18], [-7], [-31], [40], [33], [-62], [-6], [-37], [68], [18], [-2], [54], [-49], [4], [-60], [-40], [62], [49], [12], [-38], [-73], [-60], [45], [52], [-56], [10], [41], [12], [69], [52], [18], [-36], [6], [-15], [41], [69], [-12], [38], [-23], [40], [68], [2], [14], [16], [-36], [9], [34], [-36], [-1], [-40], [18], [-22], [47], [-34], [55], [-45], [-24], [-20], [-46], [-46], [-52], [-40], [-59], [-47], [26], [-21], [30], [43], [-6], [6], [-66], [-42], [-50], [-28], [-50], [-63], [21], [48], [-29], [48], [-35], [-18], [82], [14], [54], [12], [58], [24], [49], [-24], [-42], [71], [9], [-70], [-28], [0], [-77], [14], [-48], [55], [72], [-21], [-84], [36], [78], [-43], [-62], [10], [36], [-66], [54], [6], [-79], [77], [26], [-88], [-42], [-35], [80], [4], [18], [14], [-57], [26], [-17], [-22], [68], [2], [25], [-34], [50], [48], [-25], [38], [78], [51], [8], [36], [-26], [4], [36], [82], [-51], [51], [-20], [-88], [69], [-66], [-38], [-18], [66], [2], [47], [40], [54], [52], [65], [1], [24], [-45], [-1], [-36], [-9], [-4], [18], [21], [29], [-14], [-60], [41], [-78], [67], [4], [-30], [17], [-22], [33], [44], [40], [13], [-51], [-24], [-54], [-16], [46], [-85], [50], [-37], [31], [-36], [28], [78], [14], [-6], [32], [26], [4], [68], [73], [74], [9], [-78], [-66], [-60], [4], [-57], [-13], [-34], [64], [-31], [-42], [46], [-2], [38], [-24], [16], [26], [-52], [-22], [6], [104], [2], [-59], [-4], [7], [-58], [27], [-52], [53], [-74], [47], [-29], [-68], [-94], [-31], [-60], [17], [32], [44], [-12], [-81], [14], [-1], [-70], [96], [-4], [47], [-18], [-70], [90], [37], [-48], [60], [80], [-42], [25], [37], [11], [-55], [110], [-2], [-48], [-4], [72], [-84], [-35], [-84], [-74], [-52], [-60], [-24], [-64], [-5], [-58], [-62]];
    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_ce();".
// 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_ce(: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_ce();".
// 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_ce( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_8640_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![-3, 1]>,<11,R![2, 1]>,<13,R![-5, 1]>,<17,R![8, 1]>,<19,R![-1, 1]>],Snew);
    return Vf;
end function;