
// Make newform 8470.2.a.k in Magma, downloaded from the LMFDB on 20 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_8470_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_8470_2_a_k();".
// 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_8470_2_a_k();".
// 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_8470_a();"
function MakeCharacter_8470_a()
    N := 8470;
    order := 1;
    char_gens := [6777, 6051, 7141];
    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_8470_a_Hecke(Kf)
    return MakeCharacter_8470_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], [0], [-5], [-6], [-5], [-3], [0], [-10], [2], [0], [-8], [-6], [6], [9], [10], [14], [0], [-2], [1], [-9], [0], [8], [3], [-4], [-6], [10], [9], [7], [-21], [-3], [19], [6], [7], [5], [2], [0], [-6], [0], [-25], [15], [-11], [-12], [8], [-20], [-10], [-24], [-22], [21], [3], [4], [24], [6], [9], [-21], [-20], [-32], [15], [-29], [-27], [4], [0], [-34], [30], [8], [-23], [6], [-11], [30], [24], [-16], [22], [-34], [12], [30], [38], [-30], [28], [21], [-40], [3], [-4], [16], [-24], [-3], [1], [-30], [-7], [-3], [36], [-13], [6], [-34], [-24], [3], [-36], [-17], [-2], [-8], [-42], [-9], [21], [-32], [8], [15], [36], [3], [-14], [34], [16], [-42], [23], [-28], [-21], [-40], [-12], [-24], [30], [5], [-17], [3], [24], [20], [30], [2], [12], [-28], [13], [22], [-12], [-13], [20], [30], [22], [39], [28], [33], [-18], [-32], [18], [-16], [30], [17], [-12], [19], [-24], [-28], [0], [40], [-6], [-34], [-24], [-28], [45], [16], [36], [52], [42], [6], [57], [-32], [-9], [-27], [-42], [35], [25], [46], [-51], [12], [23], [48], [-35], [14], [42], [-8], [18], [64], [34], [53], [60], [50], [6], [12], [-9], [-8], [-19], [34], [-60], [56], [-60], [56], [-45], [42], [-42], [-29], [-64], [0], [-12], [60], [28], [-1], [46], [36], [39], [-34], [36], [33], [-40], [-50], [57], [-67], [27], [42], [-16], [31], [-18], [-15], [-54], [58], [-68], [-63], [-19], [9], [-14], [66], [12], [-38], [18], [-40], [-35], [55], [9], [17], [48], [-16], [6], [-36], [6], [12], [4], [-28], [62], [12], [36], [8], [27], [-14], [-57], [-26], [-60], [57], [8], [6], [18], [26], [-44], [-6], [-8], [-8], [-78], [-14], [1], [-72], [56], [-6], [6], [-38], [-66], [-13], [14], [38], [-41], [-26], [74], [48], [-62], [73], [-24], [-6], [17], [-12], [10], [49], [-45], [59], [42], [-28], [-30], [66], [-81], [21], [51], [64], [81], [80], [21], [-36], [-44], [10], [-48], [16], [6], [26], [-19], [0], [-55], [45], [10], [-72], [66], [-30], [-82], [-33], [-56], [21], [9], [-76], [6], [-53], [-22], [-21], [-1], [-3], [38], [-70], [-49], [27], [-78], [28], [36], [10], [72], [-38], [15], [-16], [-18], [-40], [-32], [-4], [-51], [33], [-1], [-18], [36], [-73], [74], [24], [63], [-44], [-49], [-18], [-26], [22], [78], [-54], [-51], [93], [0], [-8], [-60], [57], [30], [47], [-34], [-33], [-59], [10], [0], [35], [84], [6], [-38], [-34], [9], [27], [-2], [-66], [-2], [15], [-66], [88], [75], [-35], [-72], [80], [50], [46], [24], [-22], [-33], [54], [11], [39], [-26], [-62], [-66], [38], [12], [31], [-9], [13], [-36], [-42], [70], [-10], [-57], [-76], [15], [46], [-18], [-84], [-98], [-83], [15], [-93], [20], [78], [-6], [-60], [-2], [27], [63], [-28], [87], [36], [6], [-4], [36], [68], [-48], [-76], [24], [11], [66], [-5], [56], [-82], [-38], [-48], [-75], [4], [-81], [100], [27], [28], [18], [74]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_8470_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_8470_2_a_k();".
// 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_8470_2_a_k(:prec:=1)
    chi := MakeCharacter_8470_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(3169) - 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_8470_2_a_k();".
// 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_8470_2_a_k( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_8470_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![-1, 1]>,<13,R![5, 1]>,<17,R![6, 1]>,<19,R![5, 1]>],Snew);
    return Vf;
end function;