
// Make newform 8640.2.a.j 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_j();".
// 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_j();".
// 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], [3], [5], [-3], [0], [7], [5], [5], [6], [0], [-9], [9], [-8], [-4], [-14], [-12], [-8], [-2], [11], [6], [0], [6], [-9], [14], [-2], [0], [9], [10], [21], [10], [6], [13], [-5], [-21], [11], [-12], [22], [12], [8], [-10], [2], [12], [17], [-22], [4], [20], [-4], [6], [6], [-11], [-1], [-7], [28], [-1], [8], [-10], [22], [-16], [-30], [-5], [32], [32], [12], [-2], [-28], [32], [-6], [3], [12], [18], [-9], [-16], [7], [-7], [25], [-24], [-25], [5], [-36], [-12], [6], [16], [-28], [30], [32], [18], [-14], [36], [-12], [4], [28], [32], [39], [-31], [-4], [-23], [10], [43], [12], [-46], [-8], [-10], [-8], [-18], [-43], [34], [-3], [42], [-11], [-29], [26], [-40], [18], [-9], [-4], [-4], [0], [10], [34], [-12], [12], [-50], [-3], [-48], [-30], [32], [2], [-16], [37], [-33], [-13], [14], [23], [54], [23], [-48], [52], [14], [30], [-28], [-34], [28], [-2], [37], [-22], [14], [53], [-25], [18], [52], [37], [19], [-36], [-25], [52], [22], [-15], [22], [-17], [-14], [15], [9], [31], [-19], [3], [-7], [-42], [-39], [10], [-46], [20], [29], [-56], [4], [30], [-4], [46], [-40], [48], [11], [-39], [16], [66], [9], [-5], [30], [-30], [-54], [-58], [-28], [63], [12], [34], [-2], [1], [30], [8], [19], [-28], [14], [38], [-35], [-24], [33], [50], [-22], [-44], [-8], [-45], [36], [-42], [-26], [47], [6], [42], [-39], [-62], [12], [73], [-24], [8], [-10], [-62], [21], [-34], [28], [-21], [-26], [14], [-41], [34], [-36], [-60], [-53], [60], [-48], [-28], [-12], [-56], [-22], [-66], [-30], [-32], [54], [-59], [70], [-45], [39], [0], [40], [-78], [-34], [59], [4], [17], [66], [26], [36], [50], [22], [-35], [-65], [8], [-38], [-52], [-25], [-32], [-32], [-51], [70], [-23], [62], [8], [-72], [68], [-67], [1], [9], [53], [-57], [72], [-49], [6], [76], [-50], [61], [-18], [11], [-24], [59], [28], [62], [78], [80], [30], [-73], [68], [16], [44], [9], [2], [-86], [-42], [18], [-2], [-66], [-49], [-17], [-74], [42], [-9], [55], [47], [-67], [-16], [-28], [-62], [32], [20], [13], [76], [-71], [57], [-44], [-4], [63], [28], [-6], [14], [23], [-10], [-4], [-18], [-88], [38], [27], [2], [71], [-31], [66], [13], [36], [-71], [-10], [-36], [-64], [42], [-76], [80], [-23], [-10], [24], [3], [0], [1], [43], [-69], [-26], [56], [-12], [-39], [59], [-28], [-20], [-82], [91], [9], [22], [-23], [-54], [-56], [47], [5], [-60], [8], [50], [-58], [-51], [43], [36], [-13], [-82], [69], [-64], [50], [-1], [-47], [-91], [14], [28], [-37], [0], [-16], [-56], [-48], [-90], [99], [-16], [-73], [80], [-37], [-14], [-64], [39], [50], [4], [75], [82], [-90], [10], [-78], [-74], [42], [18], [48], [-82], [-5], [61], [-102], [19], [-25], [-86], [-38], [20], [100], [-22], [42], [-81], [-41], [20], [-64], [-7], [-88], [-41], [-22], [47], [16], [96], [-36], [-58], [46], [-51], [30], [-69], [39], [-37], [-40], [52], [96], [54], [90], [-8], [75], [-52], [-36], [-31], [-63], [32], [48], [87], [-60], [68], [-38], [-29], [28], [-40], [98], [4], [96], [40], [-73], [-13], [103], [-30], [-56], [0], [-114], [-14], [14], [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_j();".
// 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_j(: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_j();".
// 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_j( : 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![-3, 1]>,<13,R![-5, 1]>,<17,R![3, 1]>,<19,R![0, 1]>],Snew);
    return Vf;
end function;