
// Make newform 8470.2.a.bv in Magma, downloaded from the LMFDB on 19 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_bv();".
// 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_bv();".
// 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;
    Rf_num := [[1, 0], [-1, 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, -1], [1, 0], [-1, 0], [0, 0], [0, 2], [2, 0], [-3, -1], [-5, -1], [1, -1], [0, 4], [-1, -1], [-1, -3], [-8, 0], [-4, 2], [-3, 1], [-8, 0], [10, 2], [-6, 0], [-8, 0], [-4, -2], [3, 1], [8, 0], [-6, -4], [-5, 3], [6, -6], [14, 0], [-16, 0], [9, 3], [2, 4], [-10, 2], [-5, 5], [-2, -8], [3, 5], [1, 3], [-13, -3], [-12, 2], [6, 4], [4, -4], [8, 2], [14, 2], [14, 0], [2, 2], [-18, -4], [-8, -6], [-10, 2], [4, -8], [-10, 4], [6, 2], [-16, -2], [6, 4], [5, -1], [3, 9], [-10, -2], [-5, -9], [-6, -10], [-8, 2], [2, -2], [-16, -2], [6, -10], [-6, 6], [-4, -2], [14, 2], [4, -12], [-5, 7], [-9, -5], [-2, 6], [2, -12], [0, 0], [-16, 4], [-21, -1], [-1, 5], [2, -12], [-16, 10], [4, 4], [14, 8], [16, -10], [12, -6], [-8, -2], [7, 1], [10, -6], [0, -14], [-13, -7], [-3, 1], [6, -6], [6, 4], [4, 10], [22, 4], [12, -4], [-9, 3], [25, 5], [12, 8], [-5, -13], [-8, 0], [2, -14], [-12, -12], [-2, -4], [4, -2], [-26, 2], [-13, 1], [-16, -4], [-2, -12], [-12, -12], [-20, 12], [14, -6], [-5, 3], [-7, -3], [16, 10], [0, 8], [25, 3], [8, -12], [26, 0], [-36, 2], [6, -2], [-12, 12], [2, 0], [-37, -5], [-30, 0], [9, -11], [10, 10], [6, -4], [6, 8], [28, 2], [-44, 2], [-14, 10], [3, -3], [6, -4], [-10, -10], [-12, -10], [-4, -18], [0, 20], [34, -2], [24, 8], [-23, 9], [11, 13], [-3, -17], [-6, -16], [-20, 12], [-20, -6], [2, 10], [7, 5], [-29, -3], [-1, -17], [-4, 16], [-12, -2], [4, 0], [36, 6], [4, -2], [26, 10], [-17, -1], [4, 14], [30, 4], [-20, 6], [24, -8], [20, -2], [-10, 6], [27, 5], [8, 10], [-22, -4], [14, 18], [16, 2], [14, -8], [-2, -14], [4, -12], [26, -12], [-22, 4], [6, 14], [36, -2], [-10, -18], [-12, 22], [-14, 2], [-14, -8], [-7, 15], [-18, -4], [20, -12], [-52, 2], [28, 4], [2, -20], [-8, 20], [-38, -10], [-27, 5], [27, -7], [-35, 5], [30, 0], [1, 17], [8, -22], [-14, 16], [27, 7], [8, -16], [-8, 4], [21, 9], [4, -4], [24, -12], [-50, 4], [20, -12], [-31, 9], [14, -2], [-59, 1], [0, -18], [40, 12], [-1, 9], [-8, -4], [-2, -4], [-57, -3], [14, -18], [-18, -16], [-26, -14], [-8, 16], [-20, 16], [-12, 4], [-10, 8], [40, 6], [23, -1], [5, 1], [20, 16], [30, -8], [6, 10], [7, -15], [15, -5], [-7, 21], [61, 3], [-38, -6], [-28, -10], [-31, 5], [8, 8], [-5, 9], [1, -3], [-26, -10], [-60, 0], [-22, 2], [-21, -9], [3, 21], [-45, 9], [40, -12], [61, -3], [28, -16], [50, 4], [-8, 22], [48, 8], [2, 14], [-28, -18], [39, -7], [-12, 10], [-6, 4], [-24, -2], [-38, 10], [18, -12], [-18, -2], [-36, -8], [10, 6], [24, 6], [-3, 3], [13, -3], [-32, 14], [36, 10], [-16, 28], [48, 14], [28, -4], [26, 12], [6, -32], [-18, -22], [-4, 24], [22, -18], [20, 10], [51, -9], [16, 0], [-38, 0], [-22, -4], [16, 4], [54, 8], [30, 8], [-6, -16], [-7, 21], [5, -21], [-20, -6], [-32, 2], [-38, 0], [25, -1], [12, 0], [-43, 11], [36, -20], [38, -10], [4, 24], [-6, 6], [26, 6], [-46, -14], [36, 2], [-48, 14], [-18, 6], [-5, 13], [-26, 16], [-57, -5], [-54, 0], [9, -33], [-22, 12], [24, 0], [12, -28], [-4, -2], [29, -5], [-10, -6], [0, -2], [34, 0], [29, -13], [-24, 22], [-8, 8], [26, 0], [0, -30], [64, 10], [12, 4], [6, 36], [-12, 16], [-60, 6], [60, -4], [68, 2], [-8, 8], [-25, 5], [-42, -2], [9, -9], [17, 13], [6, -22], [69, -5], [-26, -16], [-46, 10], [-1, -21], [-50, 0], [32, -14], [46, -6], [-67, -7], [44, -20], [14, -12], [-29, 21], [-21, 15], [-50, 14], [42, 6], [-2, 14], [39, 11], [12, -6], [0, -2], [-20, -10], [-8, 28], [45, -3], [-10, 12], [-56, 8], [66, 10], [-21, 19], [-64, 0], [16, 2], [6, -8], [-22, 18], [10, -16], [-55, -5], [-32, 26], [-4, -30], [16, -16], [17, 15], [34, -16], [-36, -8], [-41, -7], [-22, 4], [41, -3], [-22, 12], [27, -3], [-3, 17], [20, 4], [3, 15], [12, -14], [-36, 2], [24, 16], [-42, 6], [-16, -20], [70, -2], [24, 16], [33, -21], [-6, -14], [-21, 7], [-30, 2], [-33, 9], [0, 22], [49, -9], [-68, 10], [64, 2], [20, -2], [-20, 20], [-74, -8], [-59, 15], [-20, 10], [-40, 0], [14, 16], [12, 12], [-2, -28], [36, 14], [-14, 0], [-42, 6], [-67, -7], [0, 8], [-22, -4], [-6, 6], [-20, -26], [50, -10], [63, -7], [6, 2], [48, 6], [-24, -16], [53, -3], [12, -20], [34, -10], [23, -5], [-34, -6], [71, 3], [40, -16], [30, 32], [60, -6], [-22, 16], [-34, 10], [10, 28], [50, 8], [-73, -7], [-54, -16], [-5, 39], [44, 0], [-6, -12], [-12, -2], [-13, -17], [-45, -7], [-49, -5], [2, 8], [-14, 24], [-50, 18], [-42, -18], [44, -24], [44, 2], [2, 30], [30, 14], [32, 10], [10, 12], [56, 6], [10, 10], [-16, 26], [-36, -2], [-40, 0], [5, -7], [-78, 12], [35, -31], [-25, -23], [1, -9], [-42, -8], [-28, -4], [-16, -28], [-2, -28]];
    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_bv();".
// 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_bv(: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_bv();".
// 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_bv( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_8470_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![-4, 2, 1]>,<13,R![-20, 0, 1]>,<17,R![-2, 1]>,<19,R![4, 6, 1]>],Snew);
    return Vf;
end function;