
// Make newform 8470.2.a.bt 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_bt();".
// 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_bt();".
// 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], [1, 1], [-1, 0], [-1, 0], [0, 0], [-2, 0], [0, 1], [-2, -3], [6, 0], [0, -2], [2, -2], [0, -4], [-1, 7], [3, -3], [6, 2], [-2, 2], [-6, -1], [10, -4], [-2, 5], [0, -6], [-9, -3], [2, 4], [1, -7], [5, 5], [5, -9], [4, -8], [-4, -8], [-17, 1], [-6, 4], [2, -7], [-8, 12], [-10, 11], [7, -5], [-12, -4], [-8, -10], [-12, 6], [10, -6], [-2, 3], [0, -4], [-12, 0], [-8, 1], [10, -4], [-8, 4], [2, 0], [-6, 0], [0, -12], [13, -15], [16, -6], [0, 15], [-18, 18], [-19, 5], [0, 0], [-18, 15], [-12, 24], [15, 9], [-4, -10], [4, -10], [-10, 12], [-16, 6], [3, 13], [12, 0], [-10, -4], [13, 9], [0, -18], [4, -3], [-24, 2], [-15, 13], [14, 1], [6, -7], [-12, 4], [12, -25], [4, -14], [12, 10], [18, 4], [-23, -3], [-24, 0], [6, -24], [-4, -8], [1, -17], [-2, -16], [1, 7], [26, -20], [-28, -2], [-24, 25], [6, 14], [7, 13], [-16, 25], [7, 15], [6, -8], [12, -12], [8, -16], [12, 16], [12, -24], [5, 17], [-33, 11], [-12, 18], [-32, 4], [18, 11], [28, -21], [24, -24], [26, -5], [-22, -10], [-31, 17], [0, 5], [0, 0], [-12, 23], [-28, 19], [-4, -5], [-22, -2], [21, 5], [-6, 22], [18, 12], [2, 13], [30, -7], [-14, -6], [29, 7], [19, -21], [-16, 10], [-12, 24], [4, -5], [-20, -16], [34, 3], [-22, 22], [-44, 8], [26, -3], [-4, 10], [-12, -8], [0, 20], [0, 24], [10, 2], [36, -7], [38, -16], [-10, -14], [-6, 16], [14, 11], [-18, 16], [-2, -14], [12, -39], [26, -20], [-24, 45], [-17, 29], [-44, 8], [36, -24], [10, 19], [18, 10], [-2, 4], [14, -6], [-15, 15], [-22, 9], [10, -2], [-18, 0], [9, -3], [17, -9], [-28, 20], [-2, 15], [-16, -22], [-30, 24], [-34, 19], [20, -33], [-18, -10], [-50, 5], [11, -25], [28, -34], [12, -24], [-2, 28], [8, -14], [-2, 14], [-6, 18], [-35, -1], [22, -40], [-53, 11], [-24, 14], [6, 10], [26, -15], [-48, -6], [-6, 0], [-9, 11], [-24, 42], [24, -48], [-48, 10], [-52, 6], [-44, 20], [-12, 28], [30, -36], [12, 18], [-42, 14], [14, 22], [23, 7], [48, -19], [22, -40], [32, -41], [-34, 11], [16, -7], [-2, -14], [-44, 4], [-2, 28], [-25, -15], [-50, 24], [2, 28], [-28, 58], [-22, -16], [24, -18], [2, 42], [-7, -35], [43, -11], [-8, 32], [-36, 2], [10, 31], [22, -4], [53, -5], [22, -45], [-30, 18], [-30, -18], [7, -41], [24, -4], [39, 3], [0, 14], [-9, -9], [-22, 4], [-12, 10], [-34, 40], [-28, -18], [18, 20], [-18, 34], [8, -40], [6, 0], [24, -21], [26, -40], [10, 14], [-43, -11], [0, -36], [13, 27], [2, 30], [-30, 12], [1, 33], [-18, -6], [67, -1], [40, -2], [-25, 55], [46, -2], [-18, 23], [21, -33], [-22, 12], [-10, -24], [-20, 31], [56, -16], [-8, 0], [15, -51], [16, -12], [42, -50], [42, -26], [-22, 4], [-6, 48], [-46, 21], [0, 24], [-8, -11], [20, 6], [-12, -5], [54, 0], [-12, 21], [6, 32], [40, -64], [0, -42], [42, 12], [2, 20], [51, -3], [10, -50], [6, -11], [10, -43], [16, 26], [-2, 6], [55, -7], [-48, 27], [42, 18], [0, 19], [62, 6], [10, -55], [4, 24], [-56, 3], [-12, 28], [58, -38], [10, -6], [-6, 34], [36, -44], [-20, 21], [-62, 28], [-10, -33], [-38, 46], [-38, 24], [-2, -32], [-34, 62], [41, -7], [45, -35], [-54, 9], [36, 10], [28, -8], [48, -42], [-46, 58], [23, -43], [-63, 9], [1, -21], [-22, 40], [20, 36], [65, 7], [8, -21], [63, -9], [17, 11], [40, -42], [-40, 22], [-42, 22], [10, -2], [22, -38], [24, -35], [50, -3], [0, 26], [-47, -19], [-11, -37], [-36, 22], [27, -3], [15, 33], [35, -15], [-61, 43], [6, -28], [24, 18], [30, -29], [-25, 15], [-48, 16], [24, -16], [-74, 8], [26, 20], [24, -48], [50, -34], [-24, 4], [9, -39], [0, -3], [-40, -16], [-20, 0], [72, -33], [-26, 69], [40, -34], [-28, 54], [27, -53], [14, -26], [-42, -12], [-28, -2], [16, 8], [-24, -4], [16, 24], [30, 24], [-30, -6], [-34, 45], [-33, -5], [2, 46], [18, 0], [44, -30], [-29, 1], [0, 44], [-18, 9], [-62, 7], [-26, 4], [-50, 2], [34, -52], [40, -64], [0, 33], [48, 0], [1, -7], [10, 4], [40, -10], [77, -21], [37, -47], [16, 32], [2, 38], [46, -14], [-40, 34], [-60, -22], [6, -31], [54, -20], [16, -45], [-57, 11], [-27, 3], [-2, 8], [-13, -17], [12, 42], [-38, 64], [-25, 33], [26, -16], [56, -2], [26, -56], [54, -9], [24, 24], [-48, -33], [-32, 46], [27, 23], [-36, -4], [18, -42], [54, -36], [-6, -42], [50, 11], [-3, -47], [-36, -6], [-16, 34], [-69, 23], [-18, 6], [-68, 17], [28, 16], [-58, -12], [-72, 18], [-8, 5], [48, 24], [54, 7], [32, -5], [-6, -42], [48, -24], [-12, 39], [45, 21], [42, -14], [-46, 50], [-36, -24], [20, -7], [-24, 42], [36, -24], [30, -6], [8, 26], [15, -27], [-27, -13], [-62, 16], [-15, 27], [-12, 59], [69, -21], [-54, 24], [54, 3], [-12, -48], [28, 24], [4, -44], [-74, 54], [58, -1], [-16, -33], [-6, -36], [53, -33], [60, -40], [17, 41], [-21, -3], [60, -34], [-52, -28], [-18, 9], [-38, -16], [64, -12], [42, -64], [38, 15]];
    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_bt();".
// 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_bt(: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_bt();".
// 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_bt( : 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, -3, 1]>,<13,R![2, 1]>,<17,R![-1, -1, 1]>,<19,R![1, 7, 1]>],Snew);
    return Vf;
end function;