
// Make newform 8032.2.a.d in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_8032_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_8032_2_a_d();".
// 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_8032_2_a_d();".
// 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_8032_a();"
function MakeCharacter_8032_a()
    N := 8032;
    order := 1;
    char_gens := [6527, 3013, 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_8032_a_Hecke(Kf)
    return MakeCharacter_8032_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 := [[0], [0], [1], [3], [6], [-6], [-1], [0], [-3], [2], [3], [-2], [-11], [-6], [-6], [2], [-6], [-10], [-3], [12], [-9], [-9], [15], [10], [0], [-17], [0], [0], [-6], [-11], [0], [3], [-4], [-12], [14], [-18], [4], [-18], [18], [-5], [-21], [6], [-12], [4], [1], [12], [12], [6], [-9], [18], [26], [-6], [15], [1], [-4], [0], [28], [15], [-12], [16], [24], [-10], [0], [27], [-10], [-14], [-27], [-27], [18], [-28], [-20], [15], [0], [-31], [-18], [24], [20], [2], [-16], [12], [30], [0], [24], [-20], [30], [-27], [22], [12], [-16], [-30], [-30], [-12], [24], [-33], [-12], [33], [-34], [-34], [-39], [3], [3], [38], [-30], [-13], [39], [29], [-39], [-43], [24], [28], [3], [38], [-26], [-9], [-24], [38], [-36], [0], [-38], [-18], [22], [-28], [31], [-9], [12], [-40], [-15], [-24], [51], [0], [-12], [-48], [0], [21], [-2], [14], [-25], [6], [34], [-20], [-12], [-29], [-12], [-12], [18], [57], [-35], [22], [45], [-27], [14], [-40], [6], [48], [-3], [-12], [36], [38], [-2], [8], [9], [20], [-3], [-36], [4], [-24], [-18], [-10], [-15], [-14], [60], [-5], [15], [60], [45], [-5], [-18], [2], [0], [19], [45], [48], [-21], [-17], [-42], [-47], [-33], [27], [-18], [-12], [39], [-54], [-24], [32], [24], [29], [-67], [-27], [58], [0], [10], [51], [57], [30], [21], [-17], [6], [-24], [38], [-33], [2], [-38], [21], [3], [15], [19], [-12], [-5], [24], [43], [-36], [33], [-7], [6], [-6], [-17], [32], [60], [57], [45], [-9], [-60], [24], [2], [48], [-15], [0], [68], [-30], [-15], [12], [-9], [-72], [34], [2], [-6], [-36], [9], [36], [36], [-21], [32], [-21], [6], [4], [-60], [75], [45], [-49], [70], [54], [-60], [0], [-56], [4], [-24], [16], [-28], [-9], [38], [14], [12], [78], [0], [-51], [66], [-42], [62], [20], [-66], [-60], [0], [-12], [80], [-33], [-3], [-6], [68], [0], [82], [10], [-42], [-71], [-69], [-44], [-55], [48], [22], [57], [54], [-50], [23], [-66], [48], [39], [-63], [-66], [45], [-33], [29], [-24], [34], [-59], [12], [9], [-69], [-57], [12], [-6], [41], [27], [-32], [14], [-24], [-58], [75], [48], [-57], [-72], [-59], [24], [22], [-18], [12], [81], [-24], [70], [44], [-9], [-63], [-26], [22], [-14], [36], [59], [12], [66], [-36], [72], [-4], [3], [-18], [-35], [-51], [40], [8], [51], [-48], [92], [-69], [82], [-2], [-72], [-6], [3], [-55], [-82], [36], [20], [-33], [96], [-48], [-22], [-87], [-54], [0], [-87], [70], [-16], [12], [-2], [37], [66], [-61], [-84], [-48], [21], [36], [3], [-12], [-54], [-20], [75], [63], [12], [-48], [-9], [37], [63], [-98], [50], [91], [-30], [43], [14], [-54], [18], [14], [12], [-33], [-10], [26], [-36], [0], [42], [-67], [-24], [66], [40], [33], [20], [97], [-60], [24], [43], [-31], [24], [65], [96], [-36]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_8032_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_8032_2_a_d();".
// 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_8032_2_a_d(:prec:=1)
    chi := MakeCharacter_8032_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_8032_2_a_d();".
// 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_8032_2_a_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_8032_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![0, 1]>,<7,R![-3, 1]>,<11,R![-6, 1]>],Snew);
    return Vf;
end function;