
// Make newform 8400.2.a.di in Magma, downloaded from the LMFDB on 25 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_di();".
// 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_di();".
// 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
        poly := [1, -3, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [-1, 2, 0], [-4, -2, 2]];
    Rf_basisdens := [1, 1, 1];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
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, 0, 0], [-1, 0, 0], [0, 0, 0], [-1, 0, 0], [1, 1, -1], [-3, 1, 0], [-2, 2, 1], [1, 1, 2], [3, 1, 2], [-2, 0, 0], [5, 1, 0], [-4, 0, -2], [-3, 1, 0], [2, 2, 0], [0, 0, 0], [-5, -1, 0], [2, -2, -2], [2, 4, 0], [-6, -2, -2], [7, 3, -1], [-7, 1, 2], [-2, -2, 0], [2, -2, -2], [-3, -3, -2], [-1, -1, -2], [-1, -1, 2], [8, 0, -2], [7, 1, -2], [8, 2, 2], [-3, -3, 0], [4, 0, -2], [-4, 0, 4], [-1, -1, 2], [-9, 3, 0], [8, 2, 0], [0, 0, -2], [-5, -1, 2], [4, 0, 0], [-6, 2, -2], [-6, 2, 1], [-7, 1, 3], [8, 2, -4], [13, 1, 5], [6, 2, -4], [1, -3, -4], [-1, -1, -2], [2, -2, -2], [4, 4, 0], [8, 0, 2], [4, 2, 4], [-3, 5, -2], [11, -1, 3], [0, -2, 0], [-2, -6, -2], [-12, 0, -3], [17, 3, 2], [-3, 1, -6], [-9, -5, 0], [0, -4, 2], [0, 6, 4], [-4, 0, -2], [6, -2, -1], [-16, -4, 2], [24, 0, -2], [-13, -5, 4], [-5, 7, 4], [-10, 2, -2], [-4, 4, 4], [-1, 1, -2], [10, -4, -4], [6, -6, -5], [21, 1, 1], [-4, 4, 0], [-12, -4, -2], [-12, 0, 2], [-8, -4, -6], [14, -4, -4], [-15, -3, 2], [12, -2, 0], [12, 6, -4], [8, 4, 2], [12, -2, -6], [-13, -1, -1], [-5, -5, -4], [-13, 3, -2], [7, -7, -2], [-6, 0, -4], [2, 2, -4], [-17, -5, 0], [4, 0, 6], [-12, 4, 2], [24, 0, -2], [10, -6, 2], [9, -7, -5], [-24, -4, -2], [0, 4, -2], [-9, -5, 2], [17, 1, 4], [16, -4, 0], [12, 2, 6], [22, 2, 6], [3, 7, 0], [-2, -2, -4], [-4, -2, 4], [-16, 4, 0], [19, 3, 4], [2, 2, 8], [26, 6, 1], [13, 1, -3], [-2, 8, 4], [-4, -4, -8], [-2, 6, 2], [29, 5, 2], [-5, 7, 0], [6, -2, 8], [0, 6, -4], [-20, -8, 2], [14, -2, -2], [9, -3, -6], [3, 3, 9], [-30, 4, 4], [8, 4, 4], [-6, -6, -3], [11, 5, -2], [-3, -7, 0], [-6, -4, -4], [-14, -8, 0], [2, -6, -4], [-12, 4, -6], [-23, -3, -8], [-28, 0, -4], [7, 5, 6], [2, 10, 10], [-4, -12, -6], [11, -5, -6], [-6, 4, 0], [14, -2, 3], [0, 4, -4], [2, -6, 1], [-6, 4, 8], [7, 3, -8], [-4, 10, 8], [-20, 0, -2], [27, -3, -2], [12, -2, 8], [30, -2, 2], [13, 9, 14], [-8, -4, -7], [-49, -1, 2], [-5, 1, -6], [-18, -2, -2], [-17, -5, -10], [-8, 8, 2], [0, 8, -4], [-8, 8, 2], [5, 1, -11], [10, -6, -2], [-5, 3, -2], [1, 9, -4], [-11, 5, 4], [-15, -9, -2], [29, -3, -4], [2, 14, 8], [-10, 10, 0], [-11, 5, -2], [26, -10, -4], [-30, -6, -6], [13, 9, -2], [-26, -4, -8], [34, 2, 3], [-11, -11, 7], [-2, -4, -12], [-25, -5, -5], [34, -2, 4], [21, 5, -6], [-1, -1, -8], [14, 10, 8], [18, 8, 4], [0, 0, 2], [6, 4, 4], [-22, -10, 0], [-2, 10, 8], [6, 2, -10], [-10, 10, 3], [-3, -1, -6], [-23, -7, -14], [14, 6, -6], [12, -8, -8], [-10, -8, -8], [14, -2, -4], [-25, -9, -10], [11, 5, 2], [-4, -8, -2], [1, 5, 0], [5, -5, -6], [6, 2, 9], [22, -4, 8], [-18, 2, 10], [-8, -12, 5], [26, 2, 6], [28, 2, -4], [31, -9, -2], [-45, -1, 4], [4, 2, -8], [-38, -2, 0], [6, 6, 1], [13, -3, 6], [15, 1, 6], [6, 8, 0], [-13, 3, -10], [22, 6, 4], [-21, -1, 2], [16, -16, -4], [-10, 2, -10], [34, 2, 10], [14, 8, 16], [-2, -2, 10], [-31, -11, 2], [-15, -5, 6], [45, 1, -2], [-14, 8, 0], [9, -3, -4], [6, 0, 0], [12, 4, 4], [4, 8, 8], [-14, -8, 0], [8, -12, -7], [29, -7, 5], [0, -8, -6], [9, 1, 7], [12, -12, -2], [-11, -3, -10], [8, -16, -14], [30, 0, 8], [12, 16, -2], [14, 2, -4], [16, 18, 0], [-3, 1, 12], [-31, 1, -1], [-22, 2, -4], [17, -1, 6], [-57, 3, 4], [-4, 4, 0], [14, 0, 0], [-20, -8, -15], [-14, 10, -2], [-20, 4, 6], [4, 0, -6], [-2, -6, 4], [11, -7, 6], [-10, -2, 6], [5, 1, 2], [49, -5, -2], [-36, -2, 0], [2, -14, -5], [-19, -3, -1], [18, -8, -8], [32, -4, 2], [-2, -2, -1], [-49, -1, 4], [-8, -12, -14], [-13, 5, 6], [-36, 2, -4], [-9, -5, -2], [10, -2, 9], [9, 5, -4], [-8, -2, 4], [-47, 1, 4], [46, -6, -2], [-47, -3, 2], [-6, -16, -12], [2, 18, 8], [19, 3, 6], [10, -6, 2], [-57, -1, -2], [20, -4, -2], [7, 9, 10], [4, 6, -10], [-12, 10, 6], [22, 2, -2], [38, 2, -4], [-6, -6, 12], [-10, 2, 12], [-38, 8, 0], [-48, 4, 4], [-15, -11, -15], [26, 2, -12], [27, 15, -8], [49, 5, 4], [27, -13, -6], [-4, -6, 8], [18, 14, 6], [-35, -3, -18], [-44, -8, -6], [12, -20, -6], [-1, 11, 4], [-7, 9, -2], [0, 8, 7], [6, 2, 14], [36, 8, -8], [-55, -7, 4], [37, -7, -2], [-4, -4, -20], [9, -17, -2], [-4, 16, 12], [52, 4, 0], [53, -5, 2], [-28, 10, 4], [3, -1, -9], [10, -18, -6], [-14, 6, 0], [-22, 4, -4], [-48, -6, -12], [0, 8, 10], [55, 5, -2], [-44, 10, 4], [-28, 0, -8], [-31, 5, 9], [-73, -1, -2], [-24, 2, -12], [-21, -5, -10], [0, -20, -4], [9, 13, 16], [20, 12, -8], [27, 11, 20], [-60, 2, -4], [36, -8, 4], [48, 12, 2], [-7, 11, -10], [15, 11, -2], [-8, -2, -6], [29, 9, -6], [35, -1, 4], [6, 14, -4], [-26, -6, -24], [-14, -14, -12], [14, -8, -8], [-36, -16, 11], [20, -22, -12], [0, 0, 14], [-24, -8, 6], [21, 5, -10], [-31, 21, 10], [-32, 8, 6], [-27, -7, -2], [3, 11, 9], [-56, -10, 8], [-12, 12, 10], [18, -6, -8], [-2, -10, -5], [-3, -11, -14], [4, 0, 4], [-12, 10, -12], [20, 4, -8], [-8, -10, 14], [-38, -2, 7], [-2, 14, 16], [0, 12, 4], [-3, 21, 0], [7, 5, 6], [56, 8, -2], [15, 3, 0], [15, 5, 6], [-13, -13, 1], [20, -16, -8], [28, 0, -8], [12, 4, 1], [-10, -6, 8], [60, 2, 6], [-43, 13, -1], [7, 15, -10], [-9, 21, 10], [4, -18, -8], [11, -25, -8], [-8, 12, 10], [4, -8, -8], [-39, -11, 5], [3, 11, -16], [9, -15, -16], [17, -7, 6], [47, 7, -6], [-43, -11, 4], [18, 10, 6], [-1, -1, -8], [19, 7, -8], [48, 4, 14], [38, 6, -2], [13, 9, 4], [-6, 22, -2], [28, 12, 4], [-18, -8, -8], [24, -16, -5], [-9, 7, 9], [16, -4, 10], [7, -13, -1], [0, 8, 8], [-43, 13, 10], [-21, 7, -10], [24, -20, -4], [-26, 16, 4], [-18, -12, 12], [57, -7, 0], [-30, -10, -28], [-8, 4, -13], [49, 1, 8], [45, -11, -2], [36, 0, -6], [-30, 16, 4], [-32, -4, -4], [26, -2, -6], [-19, 5, -8], [35, -1, -6], [-61, -3, -10], [42, 6, -2], [2, 6, 4], [49, -3, -6], [15, -5, 7], [36, -4, -2], [-26, 2, -1], [-8, -16, -8], [-26, -12, 8], [-31, -19, 6], [15, 21, -2], [10, -18, 0], [7, 15, 8], [6, -18, -5], [-15, -1, -10], [40, 10, 16], [-35, 9, 8], [42, -14, -2], [56, 2, 0], [-21, -5, 13], [26, 6, -8], [-44, 0, 6], [-13, -9, 2], [53, 1, -4], [-6, -4, 16], [4, -6, 2], [2, -10, -2], [29, -3, -10], [-38, -22, 4], [4, 18, 20], [-20, 6, -10], [-29, 23, 7], [32, -6, 4], [-41, -1, 0], [-4, 8, -4], [2, -2, 4], [2, 4, 4], [8, 14, 16], [-64, -4, -16], [-22, 2, -6], [-23, -1, -18], [-23, 5, -14], [48, -12, -8], [28, -6, -16], [-28, -14, -14], [38, -14, -6], [-53, -9, 4], [-67, -11, 0], [20, -8, -10], [0, 16, 24], [-25, 23, 9], [-22, -16, 16], [-8, -20, 4], [-20, 4, 4], [10, 18, -10], [16, -12, -12], [12, 26, 0], [-67, -15, 12], [-58, 2, -8], [-29, -11, 2], [2, 10, -6], [-2, -16, -8], [11, 11, 13], [-13, -1, -4], [2, -4, 24], [-5, 15, -4], [-28, -16, 2], [-25, -21, 8], [5, -3, 2], [5, 13, -8], [13, 5, -18], [15, -29, -12], [-24, 8, 10], [61, 3, -2], [32, 6, -10], [20, -32, -16], [-19, -11, -2], [16, -8, 12], [19, 15, 14], [-42, -10, 10], [12, 2, 6], [16, 16, 5], [21, -3, 7], [6, 8, 4], [36, 8, 6], [17, 5, 20], [19, -9, -8], [8, 20, -12], [-38, -16, -8], [52, 4, 14], [-37, 19, 0], [10, 2, -10], [-34, 2, 10], [10, -2, 13], [55, 5, -2], [25, -3, -4], [-14, -10, -10], [-4, 16, 10], [14, 2, 4], [30, 22, -6], [-5, 19, 16], [-7, 5, 8], [46, -14, -12], [62, -6, -4], [-9, 3, -10], [-14, 16, -8], [31, -13, 3], [24, 8, -4], [54, -6, -10], [28, -8, -6], [14, 16, 12], [27, 17, -10], [18, 16, -16], [0, 4, 12], [13, 5, 2], [36, 12, 21], [27, 5, 18], [51, 3, -14], [62, 14, -14], [1, 1, -10]];
    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_di();".
// 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_di(:prec:=3)
    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_di();".
// 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_di( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_8400_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<11,R![104, -36, -2, 1]>,<13,R![-8, 20, 10, 1]>,<17,R![-272, -32, 8, 1]>,<19,R![200, -60, -2, 1]>,<23,R![304, -40, -8, 1]>],Snew);
    return Vf;
end function;