
// Make newform 8128.2.a.x in Magma, downloaded from the LMFDB on 14 June 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_8128_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_8128_2_a_x();".
// 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_8128_2_a_x();".
// 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, -4, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 1, 0], [-3, -1, 1]];
    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_8128_a();"
function MakeCharacter_8128_a()
    N := 8128;
    order := 1;
    char_gens := [255, 7621, 257];
    v := [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_8128_a_Hecke(Kf)
    return MakeCharacter_8128_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, 0, 0], [0, 0, 1], [-1, -1, 0], [2, 1, 0], [-1, 0, -1], [2, 0, -1], [0, -1, 1], [0, 1, -2], [1, -1, 1], [-2, -1, 0], [3, -1, 0], [-4, 6, 2], [0, 0, 0], [-1, -2, -2], [5, 2, 2], [-4, 4, 3], [-7, 2, 1], [7, -2, 0], [-4, -3, 0], [-5, -4, 0], [-8, -3, -2], [-3, 1, -5], [-3, 6, 5], [1, -1, -3], [-5, -3, 3], [-2, -5, 3], [1, -1, -5], [3, -6, -3], [1, 4, -2], [-1, 3, 3], [-1, 0, 0], [-4, 3, -3], [-1, 2, 6], [-7, 0, 2], [0, -4, -3], [-5, -2, 2], [-5, 1, -2], [14, 2, 4], [9, -10, -4], [-10, -2, 2], [-7, 3, 3], [6, 7, -1], [5, 1, -4], [3, 8, 7], [-6, 1, -7], [-3, 4, 4], [4, 1, 4], [-10, 3, 1], [-10, 0, -5], [-5, 7, -1], [-8, -5, -8], [-4, -6, -1], [5, -7, -6], [-11, -7, 2], [-2, -2, 3], [11, 7, -3], [1, -10, 4], [-9, -4, 5], [-2, -6, -7], [-14, 10, -1], [-6, 6, 3], [-15, 5, -2], [11, -6, -3], [10, -9, 2], [-7, -3, 8], [-6, -2, 8], [13, -11, -6], [-4, 7, -6], [-7, 4, -5], [-3, -3, 1], [13, 3, 10], [4, -1, -5], [14, 0, 6], [-5, 1, 4], [-1, -1, 7], [-8, 4, 3], [-15, 0, -5], [-3, -11, -2], [-33, 2, -1], [-2, -6, -4], [15, 2, 6], [20, 1, 3], [2, -8, -9], [11, 7, -5], [-18, 1, -6], [9, -15, -1], [3, -12, 0], [5, 0, 1], [12, -5, -4], [-3, 8, -4], [-4, -15, 11], [-14, 5, 6], [0, -10, 5], [13, -14, -7], [-4, -8, -11], [-3, 14, -5], [-13, -11, -5], [-24, 14, 6], [8, 8, 17], [5, 3, 11], [12, -22, -3], [1, 3, -14], [-7, -7, -6], [-31, 7, -2], [-1, 6, 3], [39, 3, -2], [-8, 14, 5], [-18, -3, 2], [7, -8, -5], [16, 1, 4], [15, 3, 8], [-16, 3, -10], [-4, -8, -11], [0, -9, 5], [8, -4, 2], [5, 0, 2], [-14, -4, 2], [13, -1, 16], [5, 6, -1], [-1, 4, -9], [4, -12, -1], [4, 14, -5], [21, 9, 4], [-18, -4, -14], [-9, -5, -11], [5, 1, -3], [-10, -3, -10], [15, -4, 4], [23, -1, 8], [5, 12, 3], [-18, 6, 3], [13, -14, -5], [-11, 24, 2], [-9, 3, 7], [8, 16, 4], [16, 1, 17], [12, -15, 10], [12, -17, 2], [8, -8, 18], [2, 10, -3], [-27, -2, 1], [-21, 7, -8], [-2, -5, -5], [7, 7, 6], [-23, -3, -15], [24, 0, 3], [20, 5, 7], [-13, 10, -11], [-18, 11, 4], [10, 11, -3], [20, 7, 0], [-27, 8, 10], [-1, 15, 3], [-5, 16, 5], [0, 16, 2], [-7, 19, 13], [15, 2, 11], [0, -5, -15], [21, 5, -8], [23, 8, 9], [25, 11, 8], [8, -14, -12], [2, -3, 11], [-34, 13, -2], [-38, 1, 4], [26, 5, -5], [19, -20, -1], [-21, -11, 6], [5, 17, -5], [26, -20, -4], [25, -18, -7], [-21, -4, -10], [-44, -1, -1], [-6, -8, 13], [-18, -3, -13], [17, -5, -9], [2, -2, 11], [39, -1, 7], [39, -1, -1], [4, 1, 11], [-5, 15, 15], [14, 9, -5], [3, 14, 11], [-4, -14, -8], [30, 6, -6], [27, 0, -7], [32, -7, 3], [31, -8, 12], [17, -22, 1], [10, -16, 5], [-24, 3, 7], [-4, 3, 0], [-28, -6, -2], [11, 13, -14], [12, 0, 21], [-9, -3, -15], [-9, 5, -20], [-10, -15, 7], [-11, 21, 10], [-13, -12, 0], [39, 2, 16], [-26, -3, 1], [4, 3, 5], [-2, 3, 1], [3, 6, -3], [-21, -9, 11], [-34, -5, -12], [15, 7, 16], [-8, -12, -20], [-35, 1, -19], [11, -19, -11], [-38, -1, 3], [0, 11, -3], [-10, -2, -9], [2, 11, 15], [-9, 16, -12], [9, 6, 18], [-4, 19, 4], [-6, 0, 18], [-24, 9, -2], [23, 2, 20], [-24, -6, -3], [-30, -12, 3], [6, -14, -11], [-39, -2, 4], [-19, 12, -1], [7, 17, -6], [0, 14, -11], [-16, -15, 6], [5, 19, -11], [-9, 10, 13], [6, -15, 19], [11, -20, -5], [27, 11, 30], [2, -10, -3], [9, -15, -20], [9, -13, 11], [-11, -20, -18], [-1, 10, -3], [36, 5, 0], [-18, 35, 3], [-5, 25, -12], [37, 11, -3], [-1, -3, -5], [2, -12, 2], [-13, -5, -17], [-36, -7, -3], [-39, 6, 8], [-31, -1, -17], [45, 1, 3], [-9, -31, 17], [-3, -21, -7], [-22, 26, 9], [-21, 1, -6], [24, -4, -5], [21, 15, 9], [-26, -13, -9], [22, -3, 0], [-42, -6, 6], [4, -15, 8], [-33, 25, 8], [55, 0, 9], [44, -2, 15], [17, 2, 25], [-16, 15, 5], [-19, 1, 1], [31, -10, 8], [-3, -30, 7], [0, -4, 5], [-21, 6, -30], [37, 2, -5], [-16, 10, -15], [-2, 30, 12], [-13, -17, -15], [45, -17, -1], [8, 23, -19], [1, 11, 20], [-6, 10, 3], [24, -22, 5], [-3, -27, -21], [27, 3, 15], [8, 18, -12], [43, 3, 7], [32, -5, -7], [31, 1, 3], [9, 14, 4], [0, -22, -13], [-36, 18, 9], [5, -8, 20], [-41, -3, 4], [-25, 20, -3], [13, 0, 5], [-17, 9, 5], [19, -17, -11], [34, 5, -4], [42, -9, 15], [-28, -18, -3], [-28, -8, -3], [10, -22, 2], [18, 6, 11], [-4, -11, -22], [-16, 28, 4], [-24, 1, 0], [4, 11, -13], [-30, -3, -32], [39, -7, 18], [21, -4, 1], [7, 13, -13], [17, 5, -2], [24, 12, -1], [20, -20, 2], [29, -14, -10], [45, 0, 12], [51, -3, -11], [16, -20, 0], [27, -20, -3], [-22, -18, 13], [-6, 11, -11], [-8, -5, 8], [19, 6, 1], [-5, -6, -16], [-1, 19, 15], [-48, 2, 6], [-19, 15, 9], [-20, 31, 14], [-37, -1, 0], [24, -11, 4], [68, 2, 1], [41, -15, 17], [-11, -7, 23], [-29, 12, -17], [-5, 12, -3], [-41, 10, 9], [-5, 8, -5], [-23, -19, -7], [-23, 19, 15], [-50, -8, -13], [50, -8, 8], [32, -18, 1], [-52, 22, -2], [24, -10, 3], [1, 3, -8], [3, 3, -3], [-2, -13, -17], [24, 2, -5], [51, 7, 21], [-4, -20, 11], [27, -14, -1], [8, 6, 26], [17, 19, -7], [-24, -32, 12], [24, -21, 16], [-42, -3, 9], [28, 6, -3], [2, -23, -10], [-4, -17, -4], [16, 1, 15], [-2, -2, -10], [-60, 15, 10], [6, 6, 13], [-8, 19, -4], [26, -11, 20], [45, 8, 22], [42, 5, 21], [10, -5, 5], [4, 19, 17], [-36, 16, -16], [-15, -22, -20], [39, -27, -4], [-35, 32, -1], [-48, -3, 11], [-23, 21, -7], [-11, 28, 0], [30, 21, 4], [-12, -10, -26], [25, 11, 1], [19, 30, -16], [14, -34, 12], [-14, 13, -5], [-68, 6, 3], [-35, 14, 4], [-14, -10, 7], [-53, 4, 0], [-6, 39, 6], [30, -1, 4], [23, 10, -8], [47, -4, 10], [-5, -28, 13], [3, 18, -1], [50, -12, 3], [-13, 17, 13], [-5, -24, -3], [-35, -3, 3], [-24, -10, 11], [-38, 14, 15], [5, 17, 5], [3, -23, 2], [19, -37, 1], [-56, -3, -16], [-18, 9, 10], [-17, 1, -10], [-39, -11, -1], [28, 20, 11], [1, -37, 8], [-11, 32, -4], [-32, 10, 22], [15, -27, 5], [34, 2, 10], [6, 23, 8], [32, -34, 0], [-14, 19, -29], [-32, 3, 8], [-50, 31, 12], [24, 26, -4], [-62, -12, -2], [-22, -8, -27], [-70, -4, -17], [12, 17, 32], [29, 14, 3], [8, 22, -14], [35, 7, 14], [19, -22, 11], [-30, 9, -22], [45, -28, 4], [9, -17, -18], [3, 4, 0], [-37, 23, -16], [-34, 19, -9], [4, -12, -4], [-23, 18, -14]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_8128_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_8128_2_a_x();".
// 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_8128_2_a_x(:prec:=3)
    chi := MakeCharacter_8128_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(2999) - 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_8128_2_a_x();".
// 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_8128_2_a_x( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_8128_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![3, -6, 1, 1]>,<5,R![-3, 1, 4, 1]>,<7,R![-3, 12, -7, 1]>,<11,R![-9, -5, 2, 1]>],Snew);
    return Vf;
end function;