
// Make newform 8470.2.a.bk 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_bk();".
// 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_bk();".
// 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 := [-1, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    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], [0, -1], [-1, 0], [1, 0], [0, 0], [-4, 2], [-1, 1], [-1, -1], [-6, 4], [4, 0], [-2, 4], [6, -6], [8, -1], [-4, 1], [-8, -2], [-4, 4], [-3, 1], [-8, 0], [-3, 5], [0, -4], [0, -7], [-2, -6], [8, -5], [8, -3], [-4, 11], [16, -2], [4, 6], [0, 5], [-6, 10], [7, -11], [8, -10], [13, -3], [-8, -3], [-8, 4], [2, -6], [4, 10], [10, -10], [-21, -1], [10, 0], [8, -2], [-13, 3], [6, -18], [-2, 0], [-10, 4], [14, -12], [0, 2], [20, 5], [-10, -8], [3, -9], [-2, 6], [8, 7], [14, -6], [-9, -3], [-8, 16], [-20, 9], [-12, 0], [14, 0], [16, -6], [12, -18], [0, 11], [-16, 16], [6, 6], [24, -17], [2, -2], [-1, -7], [-8, 16], [8, -7], [23, -15], [-19, 13], [8, -4], [9, -13], [22, -6], [16, 8], [0, 8], [-12, -3], [14, -6], [8, 0], [-16, 30], [0, 17], [-14, 28], [16, -19], [2, 4], [10, 6], [19, -11], [-8, 20], [-28, 1], [-3, 19], [-20, 9], [10, 18], [-8, -14], [12, -8], [-6, 18], [18, -32], [-20, 29], [0, -1], [-22, 6], [22, -10], [23, 9], [11, -29], [34, -10], [35, -5], [-28, 20], [0, 27], [21, -25], [-12, 24], [13, 7], [-9, -1], [-11, -1], [24, 0], [-8, 9], [-34, 0], [6, 26], [31, -11], [35, -9], [-24, 2], [36, -7], [-8, 9], [18, -16], [-6, 0], [13, 5], [-14, 32], [9, 3], [2, -8], [0, -28], [-21, 11], [12, -10], [36, -2], [38, -20], [20, -28], [-16, -2], [7, -21], [-22, 0], [-30, -12], [-16, 26], [31, -15], [6, 12], [4, 20], [-7, 13], [28, 4], [41, -9], [32, 11], [-12, 38], [-10, 38], [-5, 19], [-10, 8], [12, -14], [-46, 6], [4, 27], [39, 3], [-18, 36], [8, -18], [-32, 41], [36, -1], [-22, 0], [3, 11], [16, -26], [-18, -2], [31, -7], [25, -33], [2, 32], [-15, 41], [32, -3], [-26, 0], [24, -28], [38, -4], [28, 2], [-58, -2], [42, -18], [24, -23], [-20, 24], [40, -25], [-30, 4], [6, -40], [-33, 1], [0, 24], [18, -20], [36, 11], [44, -28], [-24, 8], [24, -38], [-14, -16], [-24, 8], [26, -8], [26, 8], [22, -30], [30, -28], [6, 6], [-20, 21], [-39, 11], [24, -12], [-3, -17], [-25, 7], [-29, 55], [30, -12], [24, -56], [-30, 32], [-20, -9], [-14, 26], [-2, 16], [10, 28], [-10, -22], [2, 14], [42, -2], [28, 21], [-32, 7], [14, 4], [4, -38], [-65, 7], [-10, -16], [-8, 1], [-11, 43], [24, -42], [-14, 22], [24, 25], [34, 4], [24, -13], [-12, 8], [32, -21], [-8, 16], [26, -44], [-32, 12], [42, -6], [2, -20], [4, -38], [-24, 36], [-4, -2], [-15, 27], [50, -20], [50, 16], [-4, -31], [36, -28], [-12, -23], [0, 10], [-42, 20], [-44, -17], [34, -8], [-44, 23], [32, -56], [16, 27], [-24, -28], [-17, -9], [40, -51], [2, -16], [-22, 54], [-31, 11], [6, 4], [-4, 42], [40, -29], [4, 24], [30, -24], [-4, 34], [-6, 32], [-50, 30], [-11, -25], [20, 18], [-13, 43], [8, 10], [27, -53], [6, 8], [-69, 17], [-6, 22], [-20, -24], [-34, 16], [32, -28], [-30, 60], [-8, -9], [-62, 12], [-51, 3], [-59, 5], [-6, -28], [-16, 18], [32, -19], [13, 39], [22, -36], [25, 19], [-32, 30], [-27, -15], [-52, 8], [19, 9], [20, 4], [22, -32], [-24, -12], [44, 4], [-8, -20], [-11, 13], [-46, 18], [33, 3], [30, 16], [-36, 26], [18, -36], [16, -18], [-60, 19], [-20, 35], [-25, 11], [24, -26], [-50, 16], [-42, 30], [44, -10], [-68, -5], [-28, 47], [-60, 3], [46, -16], [-48, 10], [36, -21], [-39, 29], [8, -11], [-48, 39], [64, 10], [20, -44], [28, 12], [-72, 8], [-36, 58], [29, -37], [25, 13], [28, -8], [-28, 1], [44, -39], [-26, 40], [-20, -15], [8, 51], [60, 11], [-24, 53], [6, -42], [-16, -12], [-59, 23], [48, -5], [-8, -40], [20, -8], [8, 32], [-42, 24], [58, 2], [40, 8], [14, -34], [20, -33], [51, 7], [-36, -8], [-28, 60], [57, -17], [-19, -13], [-58, 18], [26, -58], [-44, -15], [-4, -42], [-62, -10], [42, -62], [32, 16], [8, 16], [16, -40], [24, 2], [42, 26], [27, -29], [20, 41], [-46, 60], [-4, 4], [28, 26], [-12, 17], [-20, 32], [47, -17], [13, -37], [18, 36], [-54, 2], [-22, 40], [-38, -32], [-41, 15], [-20, 24], [-8, -13], [-16, 42], [-36, -18], [8, -23], [-60, 57], [32, -48], [18, 28], [-6, -50], [0, -48], [-10, 8], [-13, -53], [4, 12], [-15, 71], [-24, 63], [8, 7], [76, -38], [4, 29], [-20, 70], [-58, -16], [20, 43], [26, -30], [-6, -48], [-10, -26], [-9, 27], [80, -6], [-17, 65], [42, -78], [16, 35], [32, 0], [-22, 40], [38, -80], [18, -22], [-21, -23], [36, -55], [14, -38], [16, 42], [20, -39], [-8, 0], [43, -63], [24, 40], [-10, 32], [-26, 46], [-39, -21], [-48, 52], [-87, 29], [5, -1], [26, 12], [-32, 40], [-63, -15], [64, -5], [22, 22], [-20, -34], [44, -76], [19, -3], [-20, 0], [-44, 0], [40, -26], [72, -18], [-12, 63], [-72, -9], [-38, -10], [12, -37], [63, 9], [40, 1], [46, -22], [-53, 37], [8, -48], [-16, -8], [-80, 16], [-32, -10], [63, 5], [-33, 21], [48, 4], [-28, 15], [58, -8], [-60, 43], [-60, -21], [-2, -54], [-54, -2], [21, -25], [42, 36], [-12, 0], [-6, 4], [35, -3]];
    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_bk();".
// 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_bk(: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_bk();".
// 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_bk( : 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, 1]>,<13,R![4, 6, 1]>,<17,R![-1, 1, 1]>,<19,R![1, 3, 1]>],Snew);
    return Vf;
end function;