
// Make newform 8400.2.a.bz in Magma, downloaded from the LMFDB on 11 May 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_bz();".
// 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_bz();".
// 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+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], [1], [0], [-1], [2], [-5], [-3], [8], [1], [1], [-9], [-2], [7], [-7], [-12], [-13], [-15], [7], [4], [12], [2], [-10], [1], [6], [18], [0], [11], [10], [-4], [-14], [-14], [-16], [8], [-16], [-3], [-10], [6], [-5], [14], [-8], [-6], [2], [-11], [-6], [3], [-16], [-23], [-17], [-13], [-14], [-24], [0], [26], [21], [-15], [-19], [18], [20], [10], [-16], [-8], [16], [-10], [14], [-22], [23], [19], [-5], [6], [-3], [-18], [-15], [-7], [-4], [-27], [8], [-26], [-7], [12], [-30], [-21], [-22], [-27], [38], [19], [28], [12], [15], [32], [-32], [13], [36], [-6], [10], [3], [-14], [36], [-11], [-24], [16], [-1], [-46], [-3], [-6], [-27], [30], [9], [-30], [29], [-8], [-44], [-14], [10], [6], [-2], [-22], [-34], [18], [30], [-10], [-42], [19], [0], [2], [-18], [49], [8], [50], [-37], [17], [-35], [-21], [-20], [34], [30], [40], [52], [14], [-22], [-28], [-2], [18], [46], [38], [-7], [-4], [49], [-10], [40], [8], [8], [7], [19], [-42], [-21], [-13], [32], [-37], [-14], [0], [36], [48], [-8], [36], [54], [-40], [-12], [2], [-33], [-12], [26], [17], [21], [-1], [-44], [-5], [36], [-21], [19], [-49], [26], [53], [-22], [-1], [24], [-34], [36], [-38], [-7], [-36], [40], [-8], [-8], [26], [60], [7], [-43], [26], [-2], [-4], [22], [-15], [-35], [34], [53], [32], [27], [-54], [70], [-14], [-18], [-38], [-62], [57], [18], [-8], [-12], [-30], [-41], [-21], [40], [3], [-26], [44], [28], [46], [-54], [0], [20], [-66], [-60], [-64], [-40], [66], [64], [24], [2], [46], [-10], [26], [12], [38], [-23], [-18], [-31], [24], [19], [-28], [52], [-72], [-16], [-13], [-49], [-46], [56], [30], [30], [-58], [22], [-14], [44], [20], [49], [-2], [73], [46], [25], [7], [-11], [-9], [-75], [79], [-44], [-16], [52], [4], [48], [54], [47], [-27], [48], [-56], [0], [-49], [12], [-3], [-53], [30], [-31], [78], [70], [28], [4], [40], [54], [-12], [-23], [62], [-23], [-18], [76], [-69], [-66], [20], [-20], [-59], [-22], [-35], [83], [-86], [26], [45], [-30], [72], [-35], [10], [-36], [73], [20], [12], [66], [-42], [38], [-20], [-34], [16], [-71], [62], [-88], [-73], [22], [63], [-55], [55], [-59], [-60], [4], [69], [10], [-41], [-14], [6], [0], [-72], [-51], [-88], [2], [3], [-72], [62], [28], [-77], [-21], [-31], [62], [-21], [14], [-25], [-18], [-19], [36], [74], [64], [88], [70], [25], [34], [84], [-31], [-68], [40], [-87], [-69], [-1], [-16], [-99], [-48], [-20], [45], [-88], [-18], [-92], [60], [-44], [40], [-64], [-30], [-18], [-44], [-6], [7], [96], [-102], [-46], [-27], [74], [53], [-51], [-20], [90], [-1], [50], [52], [31], [48], [105], [18], [-14], [-74], [40], [104], [42], [-56], [-39], [35], [52], [-59], [-35], [57], [-70], [-9], [-97], [-69], [-21], [40], [-12], [30], [-66], [-16], [62], [-10], [98], [-61], [-18], [35], [-74], [27], [-60], [92], [96], [-63], [-100], [-4], [33], [80], [-24], [-90], [-18], [-92], [97], [-33], [28], [-66], [93], [57], [-106], [6], [-73], [-55], [-30], [-4], [10], [80], [-50], [30], [94], [-62], [99], [-42], [8], [34], [2], [24], [-87], [28], [46], [-54], [-7], [-64], [94], [-32], [11], [-104], [108], [-27], [44], [40], [23], [-64], [76], [-41], [54], [-89], [-2], [36], [-27], [78], [63], [-13], [23], [-35], [-64], [-44], [-8], [-108], [-46], [-117], [-48], [-38], [28], [-84], [16], [4], [11], [25], [46], [36], [87], [-11], [45], [-62], [-1], [-48], [63], [88], [-55], [-90], [18], [96], [-54], [-70], [24]];
    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_bz();".
// 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_bz(: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_bz();".
// 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_bz( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_8400_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<11,R![-2, 1]>,<13,R![5, 1]>,<17,R![3, 1]>,<19,R![-8, 1]>,<23,R![-1, 1]>],Snew);
    return Vf;
end function;