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