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