
// Make newform 8470.2.a.cq 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_cq();".
// 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_cq();".
// 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 := [11, 1, -8, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 1, 0, 0], [3, -4, -2, 1], [-11, 2, 4, -1]];
    Rf_basisdens := [1, 1, 2, 2];
    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, 0, 0], [0, 0, 0, 1], [-1, 0, 0, 0], [-1, 0, 0, 0], [0, 0, 0, 0], [1, -1, 2, 0], [-1, 0, -2, -1], [0, 1, 1, 2], [-2, 0, 0, 0], [-3, 1, 1, -1], [4, 0, 6, 0], [-2, 2, -2, 2], [-1, -1, 0, -3], [1, -1, 2, -3], [-1, 5, -3, -1], [6, -2, 4, -2], [0, -1, -7, 0], [-4, -2, 4, 0], [-4, 1, 1, 2], [1, -1, 6, -2], [4, 4, 1, 0], [-5, 1, -2, 0], [0, 0, -4, -3], [-5, -1, -6, 1], [8, 0, 11, 0], [-14, -2, -2, 2], [7, -3, -1, -1], [9, -5, 4, -1], [-2, -4, -8, 0], [10, 3, 3, -4], [6, -2, -4, 0], [-10, 5, -7, 0], [-1, -1, -4, 1], [0, 0, -8, -4], [3, 1, 10, 2], [7, 3, 5, -5], [3, 1, -5, -3], [8, -5, -3, 0], [-12, 0, 0, 0], [-3, 1, -4, 6], [7, 0, 4, -3], [6, -8, 2, -2], [-5, -5, 4, -2], [-6, 4, -10, 2], [-6, 4, -6, 6], [4, 0, -2, -6], [-12, -4, -8, 5], [-4, 6, -6, 0], [-9, -4, 3, 0], [4, -4, 2, -4], [7, -1, -2, 1], [-5, -5, 12, -2], [0, 7, 3, 2], [-6, 6, -12, 0], [-6, 2, 3, -2], [-18, 0, -6, 4], [-10, 4, -8, -6], [0, 2, 0, 8], [12, -6, 0, -2], [11, 1, 0, 7], [-1, -7, 7, 3], [-14, -4, 0, 0], [10, -4, 1, -12], [22, -4, 0, -2], [12, -9, 3, -4], [-8, 2, 14, 0], [-6, 2, -12, -7], [-6, 7, -13, -4], [-6, 7, 7, 4], [-12, 8, -2, -2], [9, -6, 3, -8], [-27, 3, 2, 2], [-3, 7, -6, -6], [8, -2, 2, -6], [-10, -4, -7, 4], [5, 5, 0, -10], [-13, 5, -6, 0], [-3, -7, 8, 2], [-16, 8, -21, -4], [-16, 2, -12, 0], [9, -5, -4, 1], [-13, -3, -2, 4], [-21, -1, -11, 7], [-1, 2, 21, 0], [-8, -4, 0, 2], [-7, -5, -2, -7], [-1, 6, 1, -4], [-5, 3, 6, -5], [-8, 2, 10, 6], [-14, 10, 2, 2], [15, -5, 0, 2], [-2, 2, -6, -14], [6, 2, -12, 4], [13, 1, 24, 3], [4, 2, 13, 2], [11, 1, 6, 2], [8, -4, 6, 6], [16, 3, 1, 0], [-1, 6, -18, 9], [-9, -9, 0, 2], [16, -5, 23, 6], [0, -4, 6, 4], [6, 4, -3, 0], [-21, -4, -14, 7], [-11, 7, -7, 5], [-3, 8, 6, 5], [23, -4, 7, 8], [1, 0, 10, 5], [-5, -11, 5, 7], [-11, -11, 0, 7], [-3, 3, -1, 5], [10, 0, 12, -4], [0, 5, -1, 8], [2, -5, 3, -8], [19, -3, 5, -1], [-16, -2, -18, 9], [8, 0, 12, 15], [7, -17, 5, -1], [-22, 6, 4, 4], [9, -6, 11, 0], [4, 12, -14, 2], [-22, 1, -15, 12], [19, 1, 7, 1], [6, 2, -4, 8], [2, -13, 7, -8], [-4, 10, -4, 14], [-25, -5, 8, -2], [-20, 16, -8, 0], [-11, -15, 4, 2], [-19, 1, -12, 12], [-20, 3, 1, 6], [-12, 6, -16, -4], [-21, 5, -27, -9], [-4, -10, -4, 8], [-18, -7, 11, 2], [-8, 6, -2, 14], [5, 1, -20, 0], [-5, 2, -3, -4], [-25, 1, -10, 16], [-3, 0, -11, 0], [-9, 5, 6, 9], [-9, 17, -15, 5], [-8, 16, -12, -4], [-30, 9, -19, -12], [-14, -2, -8, -10], [-34, 12, 6, 6], [-5, -5, -12, 4], [-23, 7, -32, -7], [-6, 7, -25, 4], [10, -14, 12, -2], [16, -10, 16, 4], [-9, 3, 4, 15], [7, 9, -6, -5], [1, -13, 5, 1], [-6, -3, 7, 14], [13, 5, 7, -7], [3, -13, -1, 3], [6, 5, -11, 8], [17, -6, 11, 0], [-8, 4, 8, 6], [8, 5, 17, -6], [15, -5, 10, -1], [18, -8, 20, 6], [-16, 8, 18, 6], [-20, 6, 2, 14], [-19, 13, -1, 1], [11, -9, 24, 4], [37, -1, 13, -1], [24, -4, 19, -4], [-33, 1, 2, 0], [-18, 20, -9, -4], [-16, 22, -10, 0], [-19, 9, -4, 4], [38, 3, 13, -6], [14, -8, 6, -4], [34, -4, 10, -10], [8, -2, -3, 2], [-4, -14, -4, -6], [-1, 3, 8, 2], [26, -12, 0, -10], [-6, 0, -32, -6], [-30, 6, -22, -10], [-30, 2, -16, 4], [-33, -1, -11, -3], [-34, 0, -2, -4], [8, -8, 26, -4], [-4, -4, -12, -2], [-13, -9, -20, -3], [-11, 16, -11, -8], [28, -10, 28, 12], [27, -2, 44, -5], [32, 3, -7, -4], [-25, 6, -23, -8], [34, 2, 0, -2], [-24, 0, -20, -8], [23, 7, 1, -15], [-10, 4, -38, 3], [-48, -10, -20, 12], [-21, -3, 6, -12], [13, -17, 34, -2], [-10, 8, -32, 4], [24, -10, 4, 6], [3, -1, 20, -12], [45, -3, 14, 1], [-9, 1, -10, -7], [-29, -3, -19, 17], [10, -16, -12, -6], [-30, 1, -13, -6], [15, -11, 18, 0], [31, 5, 10, -5], [8, 7, -13, 14], [-16, -12, -10, 12], [32, 0, -14, 0], [-36, 4, -4, 11], [8, 4, -4, -12], [43, 3, 18, -9], [38, -8, 12, -6], [-5, -5, -20, 1], [0, -6, -8, -20], [10, -4, -12, -6], [10, -12, 28, 0], [20, -2, -12, -10], [-37, -3, -2, 12], [22, -6, 32, 6], [20, -4, -16, -16], [-29, 17, 2, 20], [13, -18, 27, 4], [26, -4, 10, -10], [6, -2, -16, -6], [4, 8, -16, 1], [-2, 2, -8, 16], [25, -1, 22, 3], [-5, 3, 16, 0], [-25, 17, -18, -4], [-36, 6, -25, 2], [-7, -1, 19, -3], [-33, -5, 0, 13], [4, -26, 36, -10], [-16, 10, -23, -14], [2, -6, 38, -8], [50, -11, 35, 18], [23, 13, 12, -11], [39, -1, -1, 3], [-43, 1, 7, -1], [7, -24, 15, -8], [-22, 18, -24, -8], [-16, -4, -44, 0], [1, 7, -2, -11], [5, 3, 21, 5], [-2, -6, 6, 4], [10, 22, -20, -2], [-2, 4, 22, -2], [-4, 2, -22, -2], [38, -5, 1, 10], [40, 0, 18, -6], [-35, 22, 14, 11], [-31, 3, -34, -2], [-9, 22, -14, -3], [-55, 3, -26, -4], [0, 19, 19, 4], [24, -14, 2, 6], [4, -20, 12, 12], [-8, -6, -20, -14], [31, 11, 9, -11], [9, 11, 6, 8], [13, 11, 24, 3], [26, -10, 22, -4], [18, -17, 19, 16], [20, -3, 9, -14], [18, 8, -4, -14], [-2, 14, -8, 2], [7, -15, -16, -9], [9, -6, -25, -16], [13, -9, -17, 1], [-3, 6, 31, 16], [-5, 7, -24, -8], [-8, 9, -33, 8], [-9, -11, 21, 5], [11, -10, 24, 3], [22, -10, 40, -8], [5, 15, -37, 5], [27, -5, 44, -4], [-27, 13, -28, -8], [-36, 4, -36, 0], [-8, -17, -11, 6], [74, 4, 14, -6], [-22, 17, 25, 8], [0, 8, 4, 10], [28, -10, -4, -16], [-12, 26, 2, -6], [1, -5, 5, -9], [-7, -3, -8, 23], [37, 9, -8, -5], [20, 1, -9, -8], [36, -14, 28, 6], [28, -12, 14, 6], [56, -6, 18, -8], [-17, 21, -5, 1], [-11, 11, -26, 13], [8, 2, -13, 14], [9, 11, -4, 1], [-22, 16, 4, 12], [0, -12, 20, -4], [-13, -19, -10, 7], [51, 0, 35, -12], [1, -7, -2, -1], [29, 11, 26, -5], [-34, 16, -22, 8], [9, 17, 11, 5], [22, 14, -18, 16], [11, 7, -12, 0], [19, -19, 19, -23], [-3, 0, -23, 4], [-16, 1, 25, -6], [20, 18, -12, -6], [31, 3, 44, -9], [-17, 17, -20, 33], [-19, 17, 3, -7], [-53, -3, -32, -5], [22, 2, -9, -2], [-39, -5, 8, 7], [-39, 5, -48, 9], [-10, -8, 0, 8], [52, -10, 22, 0], [-52, -1, 5, 4], [15, 7, 8, 13], [11, -7, 35, -1], [41, 5, 24, -2], [-52, -6, -10, 18], [24, 18, 32, -8], [17, -7, 24, -10], [14, -6, 14, -8], [18, -18, 18, -18], [-12, -2, -17, 2], [24, -9, 41, 2], [40, -8, 28, 4], [19, 1, 1, -31], [-37, 8, -25, -8], [10, 11, -11, 14], [-24, 2, -54, 0], [-32, 6, -26, -4], [1, 1, 26, 15], [-16, 8, -20, -6], [45, 9, 13, -3], [34, 16, 22, -8], [-29, 7, 12, 6], [14, 10, 4, 4], [-42, -2, 0, 12], [-34, -4, 4, -4], [42, -18, 26, 0], [14, -17, 9, 6], [35, -5, 12, -7], [-51, -1, -7, 11], [17, -23, 29, -7], [3, 15, 23, -3], [-26, 18, -13, -2], [-2, 14, 6, -6], [4, 9, 33, -2], [4, -5, -25, 14], [2, 12, -30, 2], [2, -18, -16, 10], [6, -20, 30, -10], [10, 22, 2, 6], [4, -25, 17, 6], [-28, -4, 16, 16], [-13, -1, -16, -29], [33, -3, 31, 23], [-24, 14, -12, 10], [-4, 12, -49, 4], [-34, -10, -24, 9], [16, 8, -36, -4], [18, -14, 24, 18], [-15, -7, -16, -4], [-10, -24, 34, -8], [-24, -18, 18, 0], [22, -17, 11, -24], [11, 9, 26, -12], [-17, 16, 3, -16], [43, -13, -10, 3], [48, 6, -14, -11], [18, 4, 18, -10], [-23, -3, 30, -1], [-34, 16, -10, -8], [-26, 20, 20, 20], [65, 7, 44, -13], [21, 9, -7, -11], [11, -27, 6, 10], [-59, -1, -34, 8], [14, -7, -37, -14], [-24, 4, 24, -4], [-23, 18, -9, -4], [1, -9, 34, 6], [3, -25, 4, 9], [85, -1, 5, -11], [-1, -23, 9, 15], [-4, -14, -20, 12], [-42, -6, -24, 22], [16, 11, 17, 0], [14, 6, -16, 1], [-23, -7, 5, -17], [-80, -10, -14, 16], [-15, 9, -8, -19], [-30, 34, -2, 8], [17, -8, 9, -16], [-20, 36, -18, -6], [-6, 8, 4, 0], [0, -14, 16, 14], [36, -9, 27, 12], [7, 21, 27, -9], [-42, -23, -7, 20], [-60, 5, -15, 0], [-2, -6, -18, -20], [36, 4, 24, -24], [-33, -10, -10, -3], [-1, 7, -18, 9], [-50, -2, 4, 22], [66, 2, 20, 2], [-21, 5, -1, 11], [29, -18, 12, 5], [-39, -27, -17, 21], [15, 5, 9, -15], [23, -9, 12, 24], [6, 20, 2, 4], [15, 1, 4, 11], [-24, 6, -10, -15], [10, 4, 24, -4], [-21, -7, -6, 15], [19, -14, 4, -33], [23, -7, 36, -7], [-6, 28, 20, 0], [62, -7, 19, -14], [-13, -27, -1, -5], [3, 5, -41, -5], [7, 11, -24, -2], [3, -23, 11, -11], [22, 9, -17, 18], [-37, -4, 19, 4], [-29, 15, 9, 21], [-69, 1, -22, 27], [2, 6, 24, 16], [-16, -24, 8, 9], [-4, 20, -43, 8], [-33, 7, -29, 17], [-27, 3, -7, 9], [-28, 5, 15, 4], [26, -20, 18, -26], [17, -17, 29, -19], [-17, -29, 3, 19], [-20, 27, -21, 2]];
    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_cq();".
// 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_cq(:prec:=4)
    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_cq();".
// 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_cq( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_8470_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![11, 8, -7, -2, 1]>,<13,R![-4, -16, -13, 1, 1]>,<17,R![-19, -38, -17, 2, 1]>,<19,R![236, 18, -41, -3, 1]>],Snew);
    return Vf;
end function;