
// Make newform 8470.2.a.f in Magma, downloaded from the LMFDB on 19 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_f();".
// 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_f();".
// 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], [-1], [-6], [6], [-4], [2], [0], [-7], [0], [9], [3], [-7], [11], [-3], [11], [11], [-12], [0], [-1], [6], [-1], [-9], [-16], [12], [2], [-12], [12], [5], [0], [8], [8], [5], [9], [12], [24], [-7], [0], [-4], [-9], [2], [-4], [11], [0], [-25], [24], [-24], [8], [-15], [21], [-18], [-15], [2], [-22], [-21], [-16], [12], [-34], [0], [2], [33], [26], [-22], [3], [14], [21], [-3], [29], [23], [8], [21], [12], [-16], [-18], [-4], [9], [8], [3], [2], [-10], [-12], [21], [8], [9], [-22], [12], [36], [-4], [-12], [32], [21], [-21], [30], [-16], [26], [35], [-18], [30], [-9], [-22], [11], [30], [39], [27], [-40], [5], [-1], [-18], [20], [29], [21], [32], [45], [21], [-36], [14], [8], [48], [15], [-43], [48], [26], [18], [-4], [32], [-4], [-6], [-40], [47], [36], [-22], [-12], [50], [-42], [3], [20], [-24], [-22], [-51], [50], [6], [26], [18], [47], [30], [17], [-12], [29], [24], [-1], [-21], [20], [12], [2], [39], [-36], [6], [-22], [21], [42], [-36], [5], [20], [-49], [42], [12], [17], [-48], [-4], [-16], [-36], [62], [-12], [-61], [-1], [-10], [15], [-46], [-54], [-12], [-6], [2], [44], [-10], [12], [29], [-21], [-40], [-45], [21], [39], [59], [-67], [-6], [3], [12], [8], [-16], [50], [51], [6], [-4], [36], [-3], [-28], [-16], [45], [62], [-54], [-24], [-46], [-28], [30], [-60], [69], [-22], [20], [-33], [-46], [-42], [8], [-27], [-9], [-43], [-36], [38], [53], [5], [42], [-34], [72], [-64], [21], [60], [36], [75], [-31], [-43], [20], [18], [6], [-31], [45], [-22], [-12], [53], [6], [-12], [-22], [78], [-6], [-40], [-64], [42], [-46], [-58], [69], [-10], [20], [-18], [20], [36], [36], [47], [9], [-7], [-64], [29], [65], [-22], [47], [-45], [-16], [29], [33], [-27], [-37], [-72], [50], [-82], [-60], [-70], [-69], [-52], [84], [36], [-42], [-30], [81], [-67], [-39], [-82], [12], [-15], [-16], [83], [-3], [5], [-39], [50], [-88], [9], [29], [51], [-85], [-45], [30], [-45], [-40], [18], [20], [12], [-39], [77], [9], [8], [14], [-51], [-34], [-24], [14], [-40], [50], [6], [6], [32], [57], [-28], [-12], [86], [78], [38], [51], [50], [32], [11], [60], [-27], [-37], [-78], [-60], [-25], [80], [42], [60], [5], [8], [18], [35], [-34], [6], [-42], [-60], [-42], [6], [-25], [54], [-30], [18], [2], [44], [42], [38], [-34], [-30], [-88], [-18], [-42], [-58], [65], [15], [45], [-49], [-18], [-67], [78], [96], [-64], [-30], [11], [27], [23], [-76], [20], [-45], [-19], [-30], [84], [62], [-39], [50], [-16], [78], [50], [-66], [50], [36], [-58], [-6], [-87], [-16], [-79], [42], [-52], [0], [-19], [27], [-60], [-52], [-10], [-51], [-48], [104], [-3], [-21], [-36], [-70], [18], [60], [-10], [42], [-69], [-39], [92], [0], [32], [-30], [92], [84], [86], [-24], [-82], [38], [68], [86], [84], [63], [50], [-15], [-46], [-6], [14], [-81], [32]];
    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_f();".
// 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_f(: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_f();".
// 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_f( : 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![1, 1]>],Snew);
    return Vf;
end function;