
// Make newform 8281.2.a.b in Magma, downloaded from the LMFDB on 25 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_b();".
// 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_b();".
// 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_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 := [[-2], [2], [1], [0], [2], [0], [-6], [-3], [3], [3], [-3], [-6], [10], [-1], [11], [-9], [-8], [8], [12], [14], [-9], [-9], [11], [-5], [-9], [-6], [0], [-12], [-18], [-15], [0], [-12], [-2], [18], [-10], [-6], [18], [12], [1], [-6], [-9], [-18], [0], [12], [4], [2], [-9], [-21], [-20], [-6], [3], [-8], [-21], [-6], [6], [9], [-24], [0], [17], [-2], [4], [1], [-33], [30], [-10], [-20], [-12], [-27], [-24], [27], [2], [-22], [18], [-18], [18], [-8], [30], [-21], [-28], [-9], [-6], [30], [-4], [-20], [36], [27], [-4], [42], [2], [30], [-42], [29], [-12], [-12], [-6], [24], [37], [24], [0], [18], [17], [-32], [36], [15], [-9], [-42], [43], [13], [-21], [-44], [-18], [-24], [-8], [36], [12], [-39], [-12], [18], [-18], [-39], [3], [1], [18], [40], [15], [-15], [30], [-18], [28], [15], [-30], [-44], [-13], [9], [-19], [9], [14], [21], [-12], [-15], [12], [-16], [40], [4], [2], [16], [45], [-36], [-4], [-32], [-18], [12], [36], [-36], [-9], [-27], [36], [-7], [-2], [-1], [16], [-3], [54], [12], [10], [29], [-56], [-36], [30], [6], [-2], [30], [9], [-30], [36], [-12], [-12], [4], [-54], [-26], [24], [30], [54], [5], [-46], [-54], [-25], [21], [-24], [5], [18], [20], [-61], [-11], [-27], [54], [-18], [-27], [-61], [24], [26], [-18], [-21], [0], [23], [-36], [-39], [39], [-34], [40], [27], [42], [-36], [-52], [35], [-27], [27], [36], [4], [-26], [5], [12], [8], [-24], [-48], [-1], [12], [-27], [-10], [-2], [22], [-18], [60], [54], [72], [-25], [54], [4], [9], [-24], [56], [-52], [68], [48], [19], [-6], [63], [17], [-6], [45], [24], [22], [-8], [44], [24], [40], [-45], [12], [-36], [-57], [-45], [-39], [15], [18], [26], [8], [40], [43], [6], [-30], [-14], [-6], [30], [49], [-58], [24], [-10], [-18], [-60], [-36], [-24], [36], [-48], [75], [48], [15], [27], [-64], [-24], [-78], [3], [12], [-44], [-19], [1], [-36], [72], [0], [54], [-51], [18], [-86], [-61], [51], [-31], [42], [12], [-54], [28], [63], [-6], [22], [-18], [81], [-50], [-20], [-43], [-10], [9], [9], [0], [48], [-30], [48], [92], [34], [84], [87], [-72], [-54], [-90], [63], [26], [43], [-54], [-31], [-42], [26], [-42], [36], [30], [30], [-10], [-80], [-38], [-12], [38], [18], [61], [90], [57], [90], [46], [64], [-18], [90], [55], [-41], [18], [-34], [-12], [84], [-57], [38], [-18], [-53], [-43], [-66], [-25], [-36], [18], [-26], [-33], [-50], [47], [28], [48], [18], [60], [72], [25], [-8], [-42], [2], [-69], [31], [6], [-4], [42], [30], [-84], [-61], [4], [44], [-88], [9], [48], [0], [45], [-80], [81], [99], [42], [35], [-95], [-38], [36], [70], [-45], [-65], [36], [-63], [57], [44], [-85], [18], [-32], [-28], [58], [30], [3], [-42], [-70], [-42], [-18], [-35], [-69], [-4], [-105], [0]];
    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_b();".
// 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_b(:prec:=1)
    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_b();".
// 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_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_8281_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![2, 1]>,<3,R![-2, 1]>,<5,R![-1, 1]>,<11,R![-2, 1]>,<17,R![6, 1]>],Snew);
    return Vf;
end function;