
// Make newform 8400.2.a.f in Magma, downloaded from the LMFDB on 16 November 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_8400_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_8400_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_8400_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_8400_a();"
function MakeCharacter_8400_a()
    N := 8400;
    order := 1;
    char_gens := [3151, 2101, 2801, 5377, 3601];
    v := [1, 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_8400_a_Hecke(Kf)
    return MakeCharacter_8400_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]];
    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], [-1], [0], [-1], [0], [-2], [-2], [0], [0], [6], [-4], [2], [10], [4], [0], [-2], [-4], [6], [-12], [-12], [2], [0], [4], [10], [2], [6], [8], [-12], [-18], [-6], [-8], [-4], [-6], [8], [-10], [0], [22], [-4], [-16], [10], [24], [6], [-12], [14], [-10], [-12], [12], [24], [-12], [-2], [18], [-20], [-22], [-4], [-26], [-8], [-18], [-28], [-14], [18], [20], [2], [-12], [-24], [-6], [6], [4], [14], [-4], [6], [14], [-4], [8], [-6], [-20], [8], [-10], [-2], [-30], [-38], [12], [22], [-20], [-14], [-28], [-4], [18], [-18], [-18], [-8], [-12], [-32], [32], [0], [4], [-16], [-34], [-6], [44], [-2], [-28], [-42], [12], [-6], [44], [34], [-28], [30], [12], [18], [8], [-46], [-30], [-40], [40], [-22], [20], [-16], [30], [-32], [-18], [-26], [2], [-36], [24], [6], [38], [40], [-32], [46], [20], [-24], [32], [-6], [-38], [10], [-46], [-12], [26], [2], [24], [-18], [-32], [-12], [14], [0], [-26], [-18], [48], [-24], [-38], [-14], [-12], [8], [44], [4], [24], [34], [2], [-18], [4], [-46], [-56], [44], [-38], [-8], [16], [-10], [-14], [50], [16], [-2], [-60], [-10], [4], [42], [20], [38], [0], [-34], [48], [-12], [-6], [-58], [-16], [38], [-38], [36], [26], [-40], [18], [36], [52], [-50], [68], [-10], [-46], [-46], [54], [-16], [-10], [-36], [62], [10], [12], [-38], [52], [60], [34], [32], [22], [-42], [-56], [12], [-48], [50], [56], [18], [24], [-2], [38], [20], [-22], [-16], [60], [-42], [-2], [36], [8], [-48], [34], [-32], [-8], [-30], [-20], [-64], [-14], [-10], [-48], [0], [44], [-40], [-56], [46], [54], [32], [-40], [-4], [4], [-24], [58], [2], [48], [26], [-38], [56], [22], [52], [18], [10], [-24], [-28], [46], [54], [30], [56], [-74], [-22], [28], [-26], [-2], [60], [26], [48], [66], [-16], [-44], [-2], [10], [-12], [0], [8], [-72], [6], [-20], [36], [22], [62], [4], [66], [54], [-60], [2], [-12], [-14], [-66], [-76], [-6], [44], [-12], [50], [-34], [-56], [-28], [-20], [78], [-12], [38], [36], [26], [-16], [-18], [-30], [20], [64], [2], [-4], [-12], [-14], [18], [-8], [-50], [-18], [-64], [42], [74], [84], [20], [-8], [-66], [30], [62], [-60], [-20], [-12], [-28], [-34], [-74], [82], [40], [58], [66], [38], [-80], [-50], [48], [46], [-4], [-24], [-78], [-64], [-82], [-42], [-16], [86], [6], [40], [68], [42], [24], [-22], [-6], [-48], [-40], [20], [-34], [-46], [-24], [90], [40], [0], [-32], [-10], [28], [-38], [12], [52], [-78], [66], [34], [30], [2], [0], [82], [32], [-48], [16], [6], [-52], [64], [-46], [-86], [72], [-28], [-12], [22], [-28], [42], [-84], [-2], [-34], [42], [48], [-98], [6], [-84], [-22], [34], [-68], [-76], [-6], [78], [-100], [92], [-90], [-18], [44], [-48], [14], [72], [-82], [86], [16], [60], [74], [-102], [28], [-54], [-32], [-48], [10], [0], [4], [72], [-58], [-30], [-54], [-90], [-20], [28], [-28], [58], [6], [28], [50], [34], [4], [32], [58], [-90], [-36], [-32], [-44], [-54], [6], [-66], [-66], [100], [94], [-78], [-52], [-32], [-32], [-26], [52], [78], [-24], [84], [42], [8], [48], [44], [-56], [82], [56], [70], [-58], [68], [40], [-82], [34], [42], [-38], [6], [-8], [-84], [46], [-4], [8], [80], [-42], [80], [-86], [-14], [-72], [-114], [-52], [22], [-4], [-12], [78], [88], [106], [80], [18], [-66], [-16], [-116], [42], [-92], [-116], [0], [-86], [-66], [100], [-66], [-10], [-98], [-60], [80], [58], [-116], [-6], [8], [-38], [-4], [-6], [-54], [100], [-18], [-56], [2]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_8400_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_8400_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_8400_2_a_f(:prec:=1)
    chi := MakeCharacter_8400_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(3833) - 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_8400_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_8400_2_a_f( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_8400_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<11,R![0, 1]>,<13,R![2, 1]>,<17,R![2, 1]>,<19,R![0, 1]>,<23,R![0, 1]>],Snew);
    return Vf;
end function;