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