
// Make newform 8281.2.a.x in Magma, downloaded from the LMFDB on 18 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_8281_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_8281_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_8281_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 := [-2, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    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_8281_a();"
function MakeCharacter_8281_a()
    N := 8281;
    order := 1;
    char_gens := [1522, 3382];
    v := [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_8281_a_Hecke(Kf)
    return MakeCharacter_8281_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 := [[1, 1], [0, -1], [-1, -2], [0, 0], [-2, -1], [0, 0], [-3, 2], [-6, 0], [0, 1], [7, 2], [-4, -1], [1, -6], [-3, -2], [2, -1], [-2, -4], [-3, 0], [6, -3], [7, 2], [0, 3], [6, -4], [5, -4], [6, 3], [6, 5], [-4, 8], [8, -2], [9, -4], [14, 0], [6, -8], [8, -4], [9, -8], [2, -8], [10, -8], [3, 2], [-2, 7], [-3, 0], [2, 12], [-3, -6], [-14, -1], [6, 9], [4, 10], [0, 4], [-5, -10], [12, -3], [7, 10], [-12, 2], [2, 12], [12, -9], [-2, 0], [4, 7], [4, -6], [0, -2], [-6, 13], [-7, 6], [6, -12], [15, 0], [6, 9], [-18, 0], [14, -5], [-15, -12], [9, -6], [-10, 7], [-3, 18], [20, -9], [-20, -5], [-14, 0], [-27, 4], [-2, -19], [5, 6], [-16, -7], [2, 8], [-3, 2], [0, -12], [-16, 9], [1, -18], [-4, -3], [10, -17], [13, -10], [16, 16], [11, 14], [5, -12], [4, 11], [-13, 6], [-12, 0], [9, -6], [-14, -9], [20, -4], [-24, 6], [25, 0], [-7, -4], [6, 9], [10, -1], [20, 13], [-4, -12], [24, 8], [4, 17], [-18, -11], [1, 4], [-19, 4], [0, 12], [3, -6], [8, 5], [-1, 8], [10, 7], [16, -2], [12, -12], [25, 8], [24, -4], [3, 20], [34, 4], [-3, -24], [-38, -4], [-5, -14], [17, -10], [-26, 11], [4, 20], [5, 14], [-32, -12], [-6, 8], [12, -10], [-4, 8], [19, -10], [23, 6], [-12, 4], [6, 8], [4, 24], [28, 10], [5, 16], [-18, -13], [8, -12], [21, 0], [-8, -20], [8, 28], [-2, 11], [4, -18], [-32, -10], [24, -18], [-12, -4], [4, -7], [-4, 10], [21, -10], [4, -27], [6, -24], [8, -8], [6, -33], [35, -8], [2, -28], [35, 0], [21, 14], [-20, -5], [-18, -27], [-17, 0], [-21, -18], [32, -17], [-12, 16], [6, 9], [-24, 4], [14, 8], [-15, 24], [-23, 10], [-36, 12], [-20, 1], [-16, -16], [24, 3], [46, -4], [9, 28], [42, 0], [24, 3], [-3, -12], [-8, 16], [-3, -34], [36, 3], [13, 22], [10, 28], [31, 2], [8, -33], [-27, -8], [6, 24], [11, -26], [16, 0], [-4, -10], [-28, 23], [12, 24], [-31, -24], [-16, -4], [48, 8], [-29, -22], [4, 14], [-2, -4], [-3, 0], [-16, 16], [-11, 8], [36, -7], [0, 12], [42, 16], [-14, 11], [-6, 8], [-19, -30], [11, 16], [1, 16], [42, 0], [-31, 4], [-22, 16], [11, 24], [-17, -20], [-30, 0], [37, -4], [2, 39], [6, -15], [3, 4], [-40, -16], [-5, 12], [28, 14], [-14, -12], [30, -12], [-6, 36], [25, 22], [12, 9], [35, 8], [28, -8], [-26, 8], [-3, 30], [-58, -4], [21, 2], [4, 0], [-12, -28], [25, -10], [23, 2], [16, -20], [-34, 4], [-12, -36], [21, -24], [4, -28], [16, -33], [21, 6], [-8, -9], [42, 0], [50, 0], [-63, 6], [42, 16], [-6, 33], [-12, -7], [-4, -17], [-64, -8], [-5, -4], [7, -4], [-18, 27], [28, 3], [-4, 28], [-14, -39], [10, 8], [20, 24], [24, -6], [48, -8], [-35, 10], [40, -4], [72, -4], [-14, -24], [-20, -4], [12, 38], [1, 14], [52, 12], [-12, 48], [-23, 10], [37, -14], [-9, -26], [40, -23], [16, 16], [-3, 8], [-18, -24], [-11, -4], [59, 6], [-16, 32], [1, 24], [2, -16], [28, -24], [-32, 1], [-34, 1], [-41, -4], [-13, 36], [46, -4], [12, 44], [50, 12], [14, -7], [-31, -30], [-12, -24], [30, -12], [-41, 8], [-19, -34], [-40, -4], [27, 16], [18, -16], [12, 32], [51, 10], [-68, 7], [-12, -36], [17, -8], [-26, 4], [-15, 8], [40, 8], [48, 9], [33, 12], [21, -12], [0, -24], [-24, -24], [22, 24], [-37, 8], [28, -8], [-1, 26], [-18, 33], [-17, -36], [-12, 20], [60, 4], [23, 2], [30, 15], [2, 16], [-15, -36], [-60, 9], [-16, -17], [-24, 18], [39, -4], [4, -13], [18, 24], [-41, -8], [-32, -24], [6, -8], [-65, 8], [2, 20], [-26, 9], [34, -1], [-9, 40], [-77, 6], [39, -28], [-2, 20], [-6, -20], [-40, 0], [38, -8], [11, 20], [-24, -32], [13, -20], [10, 27], [12, 18], [29, 4], [-7, 14], [46, 17], [-12, 38], [26, -1], [-23, 24], [4, 0], [24, 3], [-34, 32], [36, -12], [20, 8], [-47, -26], [8, 17], [-41, 10], [3, -36], [48, -4], [-12, -32], [57, 0], [30, -20], [-5, -26], [-63, 12], [54, 0], [-2, 23], [4, 28], [-24, 30], [14, -32], [30, 21], [-31, -38], [58, -20], [8, 53], [2, -8], [-11, 2], [4, 63], [-49, 36], [42, -28], [62, -7], [-36, -24], [-21, 16], [-19, -6], [-50, -16], [29, -14], [58, -13], [24, 14], [30, -21], [6, 20], [-22, 1], [4, -34], [-16, -23], [-40, -13], [41, 38], [-63, 2], [6, 55], [18, 27], [46, -7], [-16, 42], [6, 27], [-36, 12], [34, 43], [-49, -4], [8, 0], [0, 22], [52, -16], [1, 2], [-15, 46], [-28, -19], [-12, 42], [81, -8], [-38, -27], [-32, 8], [-13, 16], [9, -58], [-20, -52], [6, 48], [-21, 30], [-63, 0], [-14, 20], [4, 15], [16, -34], [42, 9], [-5, -62], [-35, -28], [36, 0], [-60, -1], [-24, 54], [57, -34], [-26, -5], [19, -14], [26, -12], [28, -13]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_8281_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_8281_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_8281_2_a_x(:prec:=2)
    chi := MakeCharacter_8281_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_8281_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_8281_2_a_x( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_8281_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![-1, -2, 1]>,<3,R![-2, 0, 1]>,<5,R![-7, 2, 1]>,<11,R![2, 4, 1]>,<17,R![1, 6, 1]>],Snew);
    return Vf;
end function;