
// Make newform 8470.2.a.bw in Magma, downloaded from the LMFDB on 16 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_bw();".
// 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_bw();".
// 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
        poly := [-3, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0], [0, 2]];
    Rf_basisdens := [1, 1];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
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, 0], [-1, 0], [1, 0], [1, 0], [0, 0], [1, -1], [-4, 1], [-1, -2], [-1, 1], [0, 2], [-4, -1], [-2, 2], [2, 3], [-2, 3], [-4, -1], [-4, 1], [3, -2], [-2, -2], [-4, -3], [8, 0], [-4, -1], [-11, -1], [-1, 0], [-14, 1], [-8, -2], [-15, -1], [2, 1], [-6, 0], [8, -3], [1, 0], [3, -3], [3, 4], [1, -2], [3, -4], [-10, -2], [-9, -1], [-3, 5], [-22, 0], [6, 3], [18, 0], [2, 3], [7, -1], [-7, -3], [19, 0], [4, 0], [-12, 4], [-24, 0], [0, 1], [-16, 0], [-18, 0], [-5, 4], [-9, 1], [-4, 0], [0, 0], [-8, -3], [13, -1], [7, 5], [16, -4], [10, -5], [17, 0], [7, 6], [-17, 1], [-8, 2], [6, -5], [-6, -6], [-12, 3], [-2, -1], [11, -6], [-6, -2], [-5, -1], [-24, -1], [8, 8], [-18, 1], [18, -2], [10, -4], [-18, 1], [4, 5], [-18, -2], [18, 4], [-4, 8], [11, -8], [-8, -4], [7, 5], [-2, -7], [-26, -1], [12, 8], [-15, -2], [-5, 10], [26, 4], [-5, 1], [-9, 2], [-6, -1], [-15, -5], [20, -1], [-2, 2], [6, -7], [-9, 5], [16, 0], [3, 2], [22, 0], [-38, -1], [4, -5], [15, -8], [-5, 8], [-12, -4], [18, -5], [-15, 4], [28, 0], [-15, 3], [20, 5], [14, 0], [30, 1], [-22, -6], [13, 2], [32, -2], [-21, 4], [0, 12], [6, 5], [-22, 1], [-12, 5], [-11, -3], [25, -4], [-23, 3], [-12, 6], [4, 0], [20, -3], [6, -4], [12, -8], [0, 6], [-17, 9], [4, 7], [40, 2], [5, 9], [-10, 5], [24, -7], [-2, 6], [-1, -1], [0, -14], [-11, 3], [18, 0], [8, -8], [30, -2], [-8, 0], [-8, 3], [-11, 3], [-18, -7], [13, 1], [0, -2], [8, 10], [-20, -6], [2, 3], [22, 8], [-14, 6], [-30, -1], [-8, 10], [-1, -3], [-24, -8], [-12, 4], [20, 4], [-38, 0], [40, 3], [-9, -12], [-16, -8], [-15, -8], [5, -4], [-6, -6], [5, -11], [-13, 5], [14, 2], [-7, -9], [10, -11], [15, 3], [24, 0], [-29, 4], [2, 2], [18, -4], [42, 1], [0, -7], [-38, -1], [10, -8], [-17, 1], [12, -4], [8, -13], [12, -5], [-40, -6], [27, 5], [-12, 4], [-13, -4], [50, -2], [18, -3], [16, 0], [48, 4], [32, 2], [-21, -1], [0, 3], [8, -3], [-19, -12], [-12, -6], [12, 2], [8, -4], [28, 0], [16, 4], [31, 3], [0, 13], [-20, -8], [11, 13], [-22, 6], [-10, 11], [35, -2], [40, -4], [2, 8], [37, 7], [7, -3], [13, -12], [-16, -9], [0, 0], [-13, 9], [36, 3], [-29, 1], [14, 0], [-16, -11], [-18, 5], [41, 0], [-29, -9], [-3, 2], [-44, 1], [-30, -2], [24, 2], [-22, 0], [18, -8], [-26, 7], [25, -6], [-25, -5], [27, 6], [-53, -2], [22, 9], [0, 6], [36, -7], [12, -16], [0, 11], [6, -15], [24, 6], [-8, 0], [-8, 9], [10, -9], [-20, -4], [2, -15], [-19, 12], [-16, -13], [-33, -1], [2, -16], [-40, -6], [35, -11], [36, 2], [-34, -4], [12, 3], [2, -8], [-52, 4], [10, 2], [-32, -10], [-52, -2], [12, 13], [46, 6], [-33, 3], [8, -10], [32, -8], [-36, 7], [12, -1], [14, -2], [0, 19], [7, -3], [-10, 6], [-14, 18], [-25, 3], [16, -9], [34, 8], [-24, 8], [32, 15], [55, -4], [0, 16], [38, -6], [-37, 5], [-12, 14], [-2, 22], [-7, -12], [9, 3], [-25, 2], [14, 4], [52, -4], [-16, -3], [58, -4], [45, -2], [-13, 12], [-53, 4], [72, -4], [-9, 19], [10, -7], [-35, -7], [-24, -2], [-8, -6], [26, 16], [-10, -7], [44, -6], [28, -13], [-72, -5], [-23, 6], [-14, 19], [-23, 5], [-41, -3], [24, 7], [-52, 2], [-24, 13], [34, -6], [-12, 1], [-49, 7], [-26, -15], [-45, -8], [9, 1], [22, 3], [18, -20], [-17, -10], [34, 12], [-15, 3], [-19, -13], [39, 14], [22, -8], [44, 3], [29, 10], [35, -9], [64, -1], [24, 0], [12, -2], [16, 19], [-24, 0], [34, -12], [57, 4], [-22, 13], [-58, 4], [6, -1], [-20, 12], [-22, 1], [-63, 2], [-13, 9], [21, 15], [42, 0], [18, 1], [7, 3], [-22, -8], [-24, 6], [-9, -9], [0, 2], [11, 14], [-30, -16], [-2, -2], [-60, -3], [-40, -9], [-26, -6], [81, 2], [47, 10], [24, -4], [70, -3], [62, -3], [55, -5], [18, 2], [17, 4], [-66, -8], [65, 1], [-59, -3], [-18, -8], [40, -4], [-3, -12], [-24, -18], [20, -3], [20, 13], [56, 11], [-61, -2], [-45, -1], [-8, -13], [-6, 6], [-14, -8], [31, 7], [-34, -12], [12, -18], [-23, -14], [-23, -6], [8, 14], [-12, -2], [-46, 6], [-14, -20], [4, 6], [38, -2], [59, -7], [60, -1], [1, -6], [5, -5], [-26, -6], [6, -20], [8, 13], [16, 23], [-24, 2], [-27, 7], [-65, -8], [37, -7], [54, -9], [-6, -18], [-82, 2], [-26, -22], [13, -10], [8, 10], [-71, -8], [-10, 2], [24, -13], [-20, -14], [-10, 2], [-29, 4], [-37, -15], [-81, 5], [-62, 9], [-20, -11], [-4, 29], [-6, 13], [-70, -6], [-31, 15], [51, 6], [-52, -2], [55, -5], [-60, 10], [-34, -2], [4, 12], [-72, 8], [-2, -15], [-46, -7], [46, 3], [-60, 6], [-29, -9], [12, -1], [-35, -12], [32, 18], [-52, -3], [16, -7], [24, 0], [-3, 24], [-8, 4], [-13, 13], [-20, -4], [21, 24], [-4, 0], [4, -9], [32, -17]];
    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_bw();".
// 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_bw(:prec:=2)
    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_bw();".
// 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_bw( : 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![-11, -2, 1]>,<17,R![4, 8, 1]>,<19,R![-47, 2, 1]>],Snew);
    return Vf;
end function;