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