
// Make newform 8281.2.a.g in Magma, downloaded from the LMFDB on 20 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_g();".
// 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_g();".
// 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 := [[-1], [3], [3], [0], [3], [0], [2], [-1], [0], [7], [3], [-2], [3], [-7], [1], [3], [-4], [13], [3], [-13], [-13], [-3], [0], [6], [-5], [5], [-5], [8], [-7], [15], [11], [-5], [-10], [15], [-15], [21], [-19], [1], [-13], [-19], [17], [22], [-17], [-7], [1], [20], [7], [-9], [-4], [-13], [-21], [4], [-26], [23], [2], [-27], [-18], [-16], [22], [18], [-1], [11], [12], [9], [-19], [9], [29], [14], [-8], [23], [-25], [-17], [31], [-9], [33], [-21], [-33], [-1], [2], [14], [25], [-18], [-9], [-27], [16], [-11], [-15], [18], [35], [8], [7], [-35], [-16], [15], [31], [31], [-34], [17], [-4], [37], [-28], [-3], [-4], [10], [43], [-1], [-33], [27], [-25], [-35], [-11], [25], [33], [11], [-25], [-18], [-19], [-9], [18], [29], [-9], [-41], [-7], [-12], [-4], [42], [-11], [9], [8], [-9], [-1], [-51], [28], [3], [-9], [19], [-6], [-20], [-3], [11], [-4], [54], [40], [-4], [-11], [37], [-6], [33], [-25], [-37], [45], [-15], [20], [-24], [-47], [48], [-25], [-13], [-22], [-17], [12], [-33], [8], [1], [5], [47], [13], [-2], [14], [1], [-15], [3], [-5], [10], [13], [21], [-27], [-27], [5], [14], [55], [24], [-6], [-17], [-53], [30], [7], [43], [6], [24], [21], [-33], [-7], [-30], [12], [21], [9], [-13], [2], [35], [22], [35], [-22], [-11], [-17], [-27], [41], [-57], [21], [-24], [-58], [58], [7], [27], [-16], [-53], [-5], [-6], [-24], [46], [26], [-40], [-75], [-71], [57], [22], [6], [0], [33], [15], [59], [35], [-11], [-21], [-4], [71], [-57], [-34], [21], [45], [-57], [20], [-56], [-10], [-37], [75], [53], [21], [-52], [-41], [-19], [78], [9], [-51], [-39], [-39], [-14], [-7], [30], [15], [23], [-1], [-77], [41], [-14], [-43], [1], [6], [-21], [2], [9], [73], [35], [11], [-59], [48], [-27], [-35], [-11], [-27], [3], [-65], [-16], [11], [69], [81], [59], [54], [-8], [-43], [15], [-60], [-46], [-31], [-13], [21], [-53], [-51], [-8], [35], [-31], [-3], [-39], [64], [-73], [-79], [-33], [-46], [-24], [-29], [37], [-39], [-1], [17], [-41], [66], [-87], [-35], [-81], [-85], [-55], [-63], [-3], [25], [-19], [-66], [-85], [-53], [-60], [79], [22], [-61], [37], [84], [47], [4], [-71], [-46], [-29], [83], [13], [-17], [-6], [-29], [-39], [-84], [-51], [57], [-21], [-63], [1], [69], [-31], [-8], [-14], [66], [-13], [44], [-29], [32], [33], [-42], [-59], [52], [-81], [-50], [-17], [-12], [-42], [-72], [25], [11], [-6], [35], [31], [15], [-15], [-26], [69], [-87], [39], [41], [-72], [101], [-83], [61], [20], [-7], [11], [-13], [-31], [42], [-43], [92], [5], [70], [21], [42], [-68], [37], [71], [30], [-56], [11], [90], [9], [-43], [37], [-41], [76], [-11], [66], [-91], [-53], [39], [-43], [-99], [-3], [-65], [85], [-33], [95], [-27], [-68], [-61], [-54], [1], [-30], [-20], [48]];
    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_g();".
// 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_g(: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_g();".
// 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_g( : 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, 1]>,<3,R![-3, 1]>,<5,R![-3, 1]>,<11,R![-3, 1]>,<17,R![-2, 1]>],Snew);
    return Vf;
end function;