
// Make newform 8400.2.a.v in Magma, downloaded from the LMFDB on 19 April 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_v();".
// 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_v();".
// 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], [2], [0], [6], [0], [-6], [-10], [0], [6], [-8], [-12], [6], [0], [-6], [4], [-6], [14], [-4], [0], [-6], [2], [-10], [16], [4], [10], [-10], [-4], [-12], [2], [6], [14], [0], [22], [-16], [-24], [0], [-18], [-26], [-10], [16], [18], [22], [8], [-16], [8], [-6], [6], [-30], [-26], [8], [20], [-8], [14], [-30], [0], [-26], [-12], [16], [-4], [8], [-14], [-26], [0], [16], [4], [-14], [-24], [22], [16], [-4], [20], [-16], [-10], [2], [2], [-6], [4], [10], [-14], [-30], [-10], [-28], [18], [-12], [18], [36], [32], [-24], [-12], [22], [20], [-24], [-30], [-38], [20], [-34], [36], [-2], [-20], [34], [-28], [-22], [-28], [-40], [-42], [-10], [-32], [24], [-18], [22], [4], [46], [44], [0], [-38], [-22], [-38], [12], [-48], [12], [10], [-46], [26], [-20], [-16], [-14], [4], [16], [-4], [-12], [-10], [-42], [-32], [-12], [-48], [54], [38], [26], [44], [-36], [30], [12], [10], [28], [2], [32], [56], [-54], [36], [-12], [-28], [30], [-44], [30], [-18], [-6], [-4], [-18], [40], [-32], [30], [28], [-52], [-22], [-14], [40], [6], [-18], [-22], [32], [26], [-26], [32], [-10], [-8], [6], [32], [-56], [-4], [-24], [16], [26], [0], [28], [-50], [60], [2], [12], [52], [62], [-12], [-16], [66], [-4], [12], [-64], [-6], [62], [-50], [-42], [8], [56], [-22], [36], [30], [10], [60], [10], [0], [-8], [-12], [-50], [-4], [70], [-32], [2], [58], [-50], [-50], [-56], [-12], [26], [12], [46], [40], [30], [-52], [38], [-24], [62], [-36], [20], [42], [-30], [6], [28], [-12], [-2], [64], [38], [-60], [-36], [56], [60], [16], [-40], [40], [-42], [40], [74], [-8], [14], [2], [-28], [-32], [-22], [-36], [12], [10], [-42], [8], [-14], [-14], [-70], [-68], [-54], [62], [32], [34], [4], [-14], [0], [-52], [34], [6], [56], [-44], [52], [-36], [14], [-36], [-10], [76], [-26], [-58], [14], [18], [56], [46], [68], [28], [-14], [-62], [52], [72], [-20], [-26], [-62], [8], [44], [-68], [80], [-12], [10], [-62], [-60], [-12], [74], [66], [-68], [48], [-66], [60], [6], [-54], [-38], [26], [-36], [-50], [48], [2], [30], [-68], [76], [-64], [30], [-2], [-54], [58], [28], [-88], [28], [30], [76], [-18], [16], [76], [54], [38], [-40], [90], [90], [50], [-4], [-84], [-48], [-2], [4], [-78], [-8], [-2], [48], [-20], [84], [-50], [-56], [92], [10], [48], [-6], [52], [68], [-12], [-16], [-74], [-66], [90], [88], [-50], [-14], [8], [4], [-50], [10], [66], [-58], [30], [22], [-68], [18], [70], [-48], [-20], [34], [-20], [-12], [54], [-92], [-30], [-100], [42], [96], [10], [2], [20], [-70], [-50], [6], [56], [36], [-30], [-38], [8], [-6], [-28], [-8], [-74], [-18], [-12], [72], [24], [-18], [18], [-96], [-8], [20], [-10], [-54], [40], [-88], [-70], [32], [-36], [-46], [18], [60], [-46], [-54], [8], [16], [-42], [62], [38], [-74], [76], [-54], [4], [6], [26], [66], [10], [34], [28], [44], [62], [-110], [-32], [76], [84], [-50], [44], [-30], [-66], [-72], [-50], [54], [-4], [-48], [-46], [18], [76], [80], [92], [-12], [-66], [-6], [-56], [76], [-36], [30], [-6], [6], [-30], [-78], [-48], [-90], [14], [-6], [38], [26], [80], [36], [-10], [60], [-10], [8], [-26], [-96], [86], [-84], [-58], [90], [-4], [50], [-102], [28], [-82], [-24], [-86], [4], [76], [88], [-32], [70], [-100], [-76], [80], [-60], [-110], [-22], [-56], [-112], [74], [30], [98], [-56], [52], [-60], [30], [48], [-38], [28], [-82], [36], [-28], [-82], [68], [94]];
    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_v();".
// 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_v(: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_v();".
// 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_v( : 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![-2, 1]>,<17,R![0, 1]>,<19,R![-6, 1]>,<23,R![0, 1]>],Snew);
    return Vf;
end function;