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