
// Make newform 8470.2.a.cr 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_cr();".
// 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_cr();".
// 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, 0, -4, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [-2, 0, 1, 0], [0, -3, 0, 1], [0, 5, 0, -1]];
    Rf_basisdens := [1, 1, 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, 0, 0], [-1, 0, 1, 0], [1, 0, 0, 0], [-1, 0, 0, 0], [0, 0, 0, 0], [0, -1, 1, 1], [2, 0, 0, 1], [-3, 0, -2, -1], [-2, 1, -2, 1], [-2, 2, 1, -1], [0, 0, 2, -3], [-4, -2, 1, -1], [2, -2, -3, -2], [-2, 0, 3, -2], [-4, 4, 1, 0], [0, -2, -1, 2], [-2, 1, -3, 0], [-2, 2, -2, 0], [0, -4, 3, 0], [-2, -2, -6, 2], [2, 0, -1, 0], [-6, -1, -1, 2], [2, 5, -6, 1], [0, 0, -5, 2], [-2, 2, 3, 3], [3, 2, 0, 0], [-10, 0, 0, -1], [2, 0, -3, 5], [0, -2, 0, -1], [-10, 3, -1, 1], [5, 2, 7, -4], [1, -2, 4, 1], [-8, 1, 5, -1], [-9, -4, -2, 1], [4, 2, -3, -3], [-4, -1, 9, 2], [-3, 0, 1, 5], [-12, 2, -4, -2], [-8, 2, 4, 5], [2, 4, -8, 0], [-10, 0, 1, 2], [2, -1, 0, -4], [-13, 0, -2, 1], [9, 2, 1, -3], [4, 4, 9, 1], [-4, -8, 1, -1], [-8, 0, -1, -3], [-4, 0, -4, -3], [0, 2, -2, -6], [8, -4, -10, -6], [3, -8, -1, 1], [-4, -3, 13, -2], [0, -4, 3, -1], [2, -4, -4, 0], [10, -4, 0, -3], [3, 2, 1, 2], [-8, 3, 4, -4], [-10, -10, 2, -2], [-4, 4, 5, 0], [-6, 7, 4, 4], [0, 3, 6, -1], [14, 1, -1, 1], [-10, -4, 10, -4], [-8, -6, -9, 4], [4, 2, 2, 6], [-6, 8, 2, -3], [4, 2, -3, 2], [-7, -6, 3, -7], [-12, 6, 6, 6], [5, 4, 2, 6], [0, 2, 10, -3], [8, -6, -14, -4], [-4, -2, 3, 10], [8, -10, -7, -1], [-2, 0, -1, -5], [8, -2, 4, 3], [-8, 2, -2, -5], [0, -8, 12, -2], [-2, -6, -4, -4], [-8, 4, -13, 1], [4, -9, 3, 6], [0, 12, -4, 2], [2, 1, 1, 0], [-18, -6, -4, -3], [6, -4, -6, -5], [-6, 14, 2, 0], [1, 6, 14, 4], [-23, 0, 5, -1], [-6, -10, -8, 8], [-18, 3, -6, -3], [-11, 8, 1, 2], [12, 2, 13, 0], [-14, -1, 4, -7], [-2, 2, 9, 8], [2, -8, 17, 1], [12, 6, -3, 2], [4, -9, 10, 0], [-2, 2, 11, -3], [10, 1, -2, 7], [14, 16, -4, 2], [-4, -14, 1, 0], [10, -12, -3, -2], [18, -11, 0, -3], [16, 5, 0, 2], [14, -6, 0, 8], [-20, 4, 9, 4], [-13, 2, 21, 0], [16, 8, 14, -6], [-9, 2, 16, 3], [10, -12, 6, 5], [-10, 12, 12, 2], [-4, 8, 12, 5], [0, 10, -2, -8], [-12, -1, 5, 6], [24, -4, 0, -6], [-7, 6, -18, -8], [2, 8, 8, 4], [0, 10, 0, -13], [-18, -14, -10, 3], [-20, 12, -7, -2], [6, 3, 10, 8], [-3, 2, 25, 3], [16, -15, 7, 3], [-2, -6, 13, 1], [16, 4, 14, 0], [12, -14, -6, -5], [0, 6, 1, 3], [8, 0, -2, 14], [-6, -6, -5, -1], [2, -5, -27, -1], [-6, 2, -5, -10], [-2, 4, -16, -8], [3, 0, -2, 11], [10, -2, -12, 5], [14, 4, 11, -2], [-12, -6, -13, 3], [-27, -10, -11, 5], [16, 14, 0, -2], [1, -14, 3, 3], [8, -2, -10, -2], [16, 4, 16, -6], [22, -4, -6, -6], [-22, -8, -4, -2], [14, -6, -10, -5], [0, 11, -4, -8], [4, 6, -3, -6], [-6, -1, 9, -13], [22, 10, 8, -4], [-4, -14, 14, 4], [16, -8, -20, -6], [-8, 0, -5, 8], [-24, 6, 5, -5], [-2, 8, -9, -11], [12, 2, -13, 2], [-16, 16, 2, 6], [11, 2, 0, -3], [-24, -14, -14, 8], [8, 8, 2, -4], [-4, 0, 1, 3], [2, 12, -12, 8], [-8, -12, -11, -6], [23, 6, 5, -5], [-2, 6, -22, -2], [22, -9, 3, 8], [8, 7, 3, 7], [-10, 0, -1, 9], [15, -14, -20, -3], [18, 13, -19, 7], [2, -12, 0, 6], [3, 12, 5, -9], [-38, 12, 1, 2], [4, 11, 2, -4], [2, -8, 12, 2], [27, 8, 7, 5], [8, 22, -9, 1], [-10, -12, 7, 3], [22, 0, 2, -5], [-4, -2, -19, 12], [-2, 12, 22, -3], [16, 8, -14, -2], [0, 23, -2, 1], [-6, 20, -8, 0], [-26, 0, 13, -2], [18, -8, 15, 6], [-32, 0, 4, -6], [-16, 3, 10, -8], [-14, -6, 8, 4], [-3, -14, -13, 8], [2, -8, -14, -8], [4, 6, -22, -5], [-24, -16, 3, -1], [-14, -6, -2, 14], [12, -8, -9, -11], [34, 17, 14, -8], [8, 8, -18, 3], [2, 16, -15, -4], [14, 1, 14, 8], [28, 4, -16, 6], [0, 16, 3, 1], [-2, -22, -12, 2], [-4, -8, -21, -1], [4, -8, 19, 1], [19, 10, 7, -1], [6, -4, 3, -6], [-16, -8, 18, -8], [-15, 2, -11, -7], [34, -8, -4, -4], [-14, 20, 8, 1], [10, -21, 14, 6], [-18, 20, 2, 6], [-10, -16, 22, -6], [16, 17, 0, 8], [-28, 13, -2, -1], [-13, 0, 5, 0], [2, -2, -7, 4], [24, 4, -7, -7], [-27, -14, -13, 6], [-2, -20, -3, -12], [6, 3, 2, 5], [-10, -8, -6, -12], [-20, 6, 3, -6], [-38, 12, 12, 3], [-1, -6, 16, -12], [2, -9, 4, 3], [-10, -17, -2, -1], [10, -12, 15, 6], [-14, 0, -3, 19], [10, 0, 4, 8], [-6, 16, 9, -1], [-28, 2, -17, -7], [-6, 14, -12, 7], [-27, 10, 0, -5], [18, -15, -3, -8], [4, 1, 24, -2], [-35, -2, 9, 4], [-2, -12, -8, 13], [14, -2, -1, 13], [4, 6, 5, 6], [-18, -18, -5, -3], [26, -2, 22, 7], [8, 10, 12, -11], [6, 6, -14, 18], [18, -6, -10, -4], [-18, 24, 20, 7], [42, -2, -10, 1], [34, -2, 23, 5], [42, 8, -9, -4], [-12, -19, -1, -8], [24, -4, -22, -1], [-17, 26, 15, 2], [6, 4, -24, 6], [24, -20, -3, -9], [10, 3, 30, 1], [16, 24, 19, -9], [18, 16, 2, 2], [-34, -6, -28, 5], [18, -4, 19, 5], [16, -12, -14, -8], [26, -2, 0, -2], [-30, 10, 5, -7], [18, -8, -14, 6], [-34, -6, -21, 0], [-20, 0, 0, -14], [14, -9, -7, 1], [6, 22, 6, 6], [4, 10, 12, -16], [16, 10, 9, -6], [-22, -28, 1, -4], [-44, 10, -10, -2], [-12, -22, 10, -5], [36, -1, -8, -6], [30, -4, 10, -10], [8, -22, -9, 7], [-16, 9, -27, -8], [-20, 18, 0, -9], [28, 2, 4, -12], [-2, -6, 3, -9], [-2, -24, -11, 8], [-12, -5, -12, 10], [20, 24, -8, 2], [12, 10, 20, -10], [-7, 2, 4, -11], [-2, -18, -6, 18], [24, 4, -6, -12], [-22, 11, -12, -5], [5, 16, 8, -3], [24, -3, 31, 5], [6, 20, -17, -3], [18, 2, -27, -1], [-8, -26, 20, -3], [34, -20, -13, -9], [-9, -20, 5, -2], [-3, 18, 3, 15], [15, -2, 0, 7], [8, 0, -30, 0], [21, 6, 30, 2], [30, 8, 34, -5], [-25, -24, -1, 7], [-4, 12, -10, 18], [-34, -12, 18, -4], [0, -10, -1, -13], [-20, -4, 9, 4], [2, 12, -2, 14], [-6, 2, 5, 20], [40, -4, -2, 9], [20, -17, 33, 11], [12, -22, -21, 0], [10, -3, -16, -16], [9, -18, -4, -5], [30, -4, -24, 5], [54, -2, -1, -3], [-12, 6, -12, 1], [-42, 0, 10, 8], [2, 14, -2, 9], [-29, 2, -23, 8], [38, 18, 4, -1], [-18, -1, -7, -2], [0, 1, 3, 4], [-16, -16, -13, 14], [-12, -4, 22, 10], [-21, 0, -10, 11], [4, 12, -14, 6], [-13, 10, 14, 6], [-22, 5, -18, 7], [-23, 4, -3, 15], [26, 20, -16, 10], [-38, -2, -30, 5], [-25, 12, -11, 2], [-13, 12, -13, -18], [-22, -12, -24, 3], [-26, -2, 6, -16], [42, 6, -6, 10], [-20, -32, -14, 11], [48, 4, 0, -2], [34, -28, -8, -4], [9, -8, -5, 16], [36, -12, 9, 16], [-24, -10, -31, -1], [-8, -8, -2, -19], [-32, 0, 29, 3], [24, 8, -9, -8], [14, -9, 21, 5], [43, 4, 4, -16], [-15, -14, 8, -7], [30, 24, -10, 6], [14, -4, 9, 18], [-8, -39, 10, 0], [6, 4, -1, -13], [8, -36, 3, -1], [49, -12, 13, -3], [8, 16, 8, 0], [40, 9, 7, -11], [-40, -22, 27, -17], [-32, 22, 19, 7], [32, 18, 18, -11], [-20, 2, -26, -9], [-6, -24, 14, -10], [1, 4, -2, 17], [-13, -12, -13, -13], [-16, -10, -18, 12], [-28, 12, -3, 6], [12, 12, -18, 1], [-30, 19, 22, 1], [-22, -12, -34, 14], [-15, -6, 3, -6], [-24, 18, 2, 18], [-12, -1, 7, -27], [9, -18, 5, 0], [-12, 2, 6, 14], [-34, -14, 11, 7], [-8, -7, 29, 4], [-18, -22, 20, -10], [12, -18, -3, -20], [-12, -4, 2, -17], [-12, -10, -10, 9], [-42, 3, -23, -6], [-18, 1, 21, -2], [-12, 2, -31, -8], [22, 16, -15, -11], [-14, -12, 2, 16], [-26, -7, 46, -6], [-10, -8, 14, -30], [-14, 6, -32, 10], [-11, 8, 35, -9], [-35, 22, -36, -17], [-2, -6, -52, -2], [56, 0, -6, 0], [-4, -8, 20, 2], [-36, 18, -21, -3], [-8, 0, -8, 2], [-6, 34, 4, -2], [-11, 6, -9, 5], [6, 34, -9, -10], [5, 2, -23, 12], [-15, -22, 28, -9], [20, 20, 10, 0], [-18, -12, 3, -1], [-50, 12, -4, 5], [26, 2, -8, 27], [-12, -12, -29, 21], [33, 8, 22, -20], [-18, 3, 3, 7], [9, 2, 7, 8], [-16, 12, 0, -7], [24, -8, -4, 2], [16, -30, -14, -2], [-58, 0, -4, 10], [2, 9, -24, 0], [-10, -14, 0, 0], [-26, 21, -23, -10], [-30, 0, -14, 10], [-48, 14, -27, -12], [14, -6, 32, 12], [14, 8, -19, 19], [-53, -10, 11, -7], [-16, 21, -8, 4], [-28, 11, -11, -26], [24, 6, -35, 12], [-8, -10, -33, -6], [-16, -4, -6, 3], [-54, -6, 9, -4], [-6, -4, 2, -8], [-22, 15, -4, -21], [27, 8, -38, 3], [14, 14, -1, 3], [5, 0, 7, 35], [64, 8, -3, 3], [-26, -12, 18, 0], [6, 4, -12, 8], [14, -18, 6, -8], [20, -32, -8, -3], [32, -38, -9, -4], [30, 0, -5, -6], [-22, 10, 29, 3], [31, -10, 3, 17], [-52, 2, 18, -11], [70, -3, -4, -2], [-10, -18, -14, -10], [12, -12, -1, 16], [-28, -16, -15, 8], [14, -22, 22, -8], [3, 18, 0, 2], [2, 22, 13, -9], [-22, 3, 23, 10], [-32, 24, 18, 0], [45, 18, -25, 7], [8, -4, -14, -20], [44, 4, 19, 6], [58, 12, 19, -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_cr();".
// 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_cr(: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_cr();".
// 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_cr( : 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, 2, 1]>,<13,R![-23, 48, -22, 0, 1]>,<17,R![-2, -4, 1]>,<19,R![-167, -60, 26, 12, 1]>],Snew);
    return Vf;
end function;