
// Make newform 8281.2.a.n in Magma, downloaded from the LMFDB on 17 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_n();".
// 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_n();".
// 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, -1, 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], [1, 1], [1, 1], [0, 0], [3, -3], [0, 0], [-5, 4], [3, -3], [2, -4], [-1, 5], [5, -6], [-4, 0], [4, -2], [-2, 9], [1, -2], [7, -2], [1, -2], [6, 0], [3, 6], [-2, 10], [-2, 0], [4, 0], [-3, 6], [13, -5], [14, 3], [-3, 9], [-1, 6], [-5, 1], [-7, 6], [-5, 4], [-16, -3], [4, 7], [1, 1], [10, -9], [3, -3], [11, -6], [-10, -3], [-6, 6], [11, 2], [9, 0], [-9, 0], [0, -6], [23, -1], [6, 0], [-1, 11], [5, 12], [-5, 6], [-4, 15], [-5, 4], [16, -18], [1, 1], [15, 6], [10, 9], [-4, 2], [14, -19], [9, 0], [-2, 19], [11, -12], [-5, 0], [-4, -10], [6, -12], [-10, 2], [0, -3], [8, -7], [4, -18], [-23, -2], [15, -3], [3, -9], [34, -2], [11, 6], [27, -3], [14, 5], [-18, 12], [-6, -9], [11, -3], [12, -21], [14, -16], [-18, 12], [5, -25], [29, -9], [-1, 8], [18, -12], [10, -11], [-1, 0], [-10, -3], [-2, -2], [-26, 4], [4, -12], [25, -23], [3, -6], [-25, 14], [-9, 21], [4, -21], [14, -1], [-10, -3], [-25, -1], [17, -22], [4, -14], [28, -18], [-11, -15], [-3, 0], [-6, 21], [34, -8], [2, 8], [-19, 27], [-5, -24], [-28, 5], [26, 11], [2, 17], [13, -33], [23, 0], [-20, -9], [-13, 8], [-10, 12], [19, -33], [-20, -17], [-7, 0], [-28, 2], [-2, 1], [2, -16], [0, -30], [19, -36], [-13, -19], [5, -4], [-1, -3], [8, 2], [-9, -9], [21, -18], [21, 6], [-8, -15], [36, -18], [-28, -7], [13, 6], [28, 0], [-24, -3], [1, -12], [-7, 20], [22, -12], [-8, 10], [15, -12], [15, 15], [-25, 20], [-19, -12], [18, -21], [7, -9], [-17, 19], [-15, 18], [-14, 25], [-14, 36], [14, -16], [9, -6], [1, 22], [29, 0], [-32, -14], [-24, 0], [21, -27], [-30, 0], [-31, 26], [45, -18], [43, -14], [-23, 37], [-25, 32], [39, 0], [-51, 12], [29, -4], [20, -1], [-21, 3], [-49, 0], [-24, 27], [5, -10], [10, -35], [-44, 3], [36, 9], [2, -21], [-13, 15], [-13, 38], [40, -30], [46, -5], [20, -39], [-10, 27], [11, 12], [39, -21], [-41, 18], [17, 11], [-43, 5], [-23, 22], [8, -6], [4, 15], [-8, 30], [-5, 19], [7, 18], [10, -35], [-5, 15], [-17, 25], [-25, 11], [15, -30], [10, -3], [24, 9], [54, -18], [19, -32], [28, -47], [-41, 27], [7, -42], [-17, 27], [1, 28], [45, 6], [32, -24], [7, 22], [32, -31], [-2, 24], [-23, -9], [-16, 5], [-31, -18], [39, 0], [-44, 43], [-11, 12], [-43, 12], [8, -37], [26, -25], [15, -27], [-17, 39], [-15, 36], [38, -16], [-60, 15], [-25, -19], [-15, -15], [-61, 14], [-22, 29], [30, -33], [19, -32], [-54, 18], [33, 12], [-20, 21], [-50, 10], [36, 0], [-31, -4], [24, -45], [7, -38], [8, -16], [-1, -16], [-39, 9], [-47, -12], [26, 15], [-29, -18], [-4, 14], [46, -2], [-14, 24], [11, 41], [-2, 6], [20, -1], [16, -3], [34, -47], [-29, -8], [31, 0], [-1, 8], [9, -12], [-43, 27], [-63, 24], [39, 18], [34, -27], [55, -24], [59, -28], [-41, 42], [-1, 18], [-10, -10], [-28, 36], [-25, 2], [12, -57], [-35, 72], [71, -4], [4, 27], [-34, 3], [-30, 0], [5, 36], [8, -6], [-17, 9], [51, -48], [32, -30], [-18, 30], [-9, 48], [54, 15], [-30, 51], [-15, 21], [-26, 6], [-7, -24], [-28, 38], [-34, 51], [-18, 27], [50, 6], [41, -31], [-31, 44], [44, -37], [-68, 16], [-16, -7], [-2, 6], [56, -25], [-59, 9], [10, 19], [-11, 49], [13, 15], [-36, -24], [-47, 43], [-18, 0], [21, -48], [-75, 6], [-49, 30], [53, -4], [-3, 24], [35, -10], [22, -42], [33, -36], [39, -30], [-35, 70], [-39, -15], [-10, 11], [20, -45], [-1, -28], [14, 8], [35, -12], [68, -10], [-7, 12], [16, -9], [55, 16], [19, -27], [16, 22], [-22, 51], [-35, 27], [-1, 45], [-71, 13], [-8, -14], [-31, 60], [47, 5], [40, -6], [41, -16], [-52, 45], [-18, 24], [49, -6], [14, 20], [2, 45], [7, -60], [88, -9], [20, -64], [-30, 69], [-28, 36], [-12, 6], [-18, -30], [-31, 9], [-6, 12], [-5, -38], [39, 0], [7, 36], [-24, -42], [49, 10], [2, -27], [-19, 18], [0, 57], [25, -23], [-82, 2], [35, -25], [36, -36], [70, -15], [4, -8], [30, -36], [31, -50], [20, 36], [-1, 18], [-50, 4], [23, -33], [16, -27], [38, 14], [-93, 6], [-23, 43], [24, 15], [-22, 18], [27, -33], [-23, -38], [-14, -26], [-14, -33], [-3, 24], [-31, 12], [44, -43], [-73, 8], [3, 36], [-25, 32], [-14, 21], [30, -30], [-23, 36], [17, 12], [23, -36], [-56, 34], [-37, 33], [-19, 56], [-46, 5], [25, -54], [-30, 33], [34, -18], [-12, 15], [72, -36], [-12, 51], [39, 21], [-21, 0], [22, -5], [-16, -12], [14, 17], [10, -17], [-15, 6], [30, -48], [-50, 10], [-58, 48], [-46, 44], [-40, 15], [-76, -16], [-41, -2], [-72, 18], [-19, 12], [-28, 26], [60, -24], [-5, 39], [43, 10], [15, 12], [27, 30], [-7, -42], [-85, 2], [11, 14], [20, 36], [-40, 89], [20, -4], [-9, -45], [23, -60], [25, 4]];
    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_n();".
// 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_n(: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_n();".
// 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_n( : 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, 3, 1]>,<3,R![1, -3, 1]>,<5,R![1, -3, 1]>,<11,R![-9, -3, 1]>,<17,R![-11, 6, 1]>],Snew);
    return Vf;
end function;