
// Make newform 8470.2.a.bl 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_bl();".
// 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_bl();".
// 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], [0, -1], [3, 1], [0, 1], [-1, -2], [6, 2], [-5, -1], [-4, 2], [-5, 1], [1, 5], [-1, 1], [-3, -1], [-1, 0], [4, 0], [9, -3], [-4, -4], [1, -5], [-7, -4], [5, -4], [-3, 1], [-2, -2], [1, -4], [7, 3], [4, 2], [-5, -3], [4, 5], [8, -3], [6, 1], [0, -7], [10, 5], [-6, -4], [-13, 0], [-7, 2], [2, -4], [5, -1], [-10, 0], [3, 7], [8, 7], [-20, -1], [1, -12], [-24, 0], [-2, -6], [-2, -2], [-1, -7], [4, -2], [0, 0], [9, -8], [-1, -2], [4, -4], [0, 6], [-21, -1], [16, -1], [-6, -13], [22, -6], [11, 9], [-6, 1], [-3, -12], [14, 5], [-10, -4], [-27, 3], [-20, -2], [13, -5], [-7, -3], [5, 12], [-12, -2], [-3, 0], [-1, -5], [-14, 0], [-13, -5], [-6, 4], [4, 6], [-11, 9], [-23, -7], [4, 8], [-6, 14], [-6, -18], [1, 6], [14, 14], [-15, 2], [7, -3], [-19, -3], [-12, 4], [-31, -2], [-13, 4], [14, -14], [35, 0], [-18, -7], [-17, -1], [-5, -16], [5, 13], [12, 6], [-3, 15], [-30, 3], [-18, -2], [7, -14], [0, 14], [11, -9], [-21, 11], [39, -2], [2, -7], [18, 10], [35, -3], [-24, 7], [4, 4], [-6, -5], [21, 9], [40, 2], [9, 15], [-8, -6], [-11, -6], [10, 2], [-3, 10], [-16, 14], [1, 17], [15, 11], [-27, -7], [10, 9], [13, -12], [-24, 1], [-6, -6], [16, 20], [-23, -9], [10, 0], [32, 0], [24, -16], [-4, 13], [17, 5], [-8, -18], [-22, 5], [-1, 15], [1, 13], [-20, 6], [-17, -6], [2, 16], [-29, -10], [-10, -8], [-10, -10], [10, 0], [-22, 16], [-11, 15], [2, -21], [-3, -5], [-44, 1], [-18, 6], [-10, 0], [-4, 20], [-7, 7], [-16, 10], [24, -6], [-13, -15], [-8, 4], [-48, 1], [-22, -16], [14, -10], [-16, 20], [30, 16], [5, 1], [-13, -4], [-4, 28], [-17, -6], [-18, 13], [30, 4], [2, -13], [-2, -3], [8, -6], [-31, 12], [-9, -7], [-28, -3], [-10, -24], [-15, -8], [18, -8], [12, -10], [-5, 13], [-13, -13], [13, 23], [-26, 2], [-11, -12], [8, 26], [5, 11], [3, 9], [48, -4], [20, -3], [-14, 18], [8, -3], [-4, -2], [-5, -25], [48, 0], [-14, 14], [8, 32], [14, -9], [19, 17], [45, -3], [-50, 3], [6, -14], [16, 4], [12, 4], [2, 18], [4, 8], [25, -6], [57, 5], [-12, -24], [-25, -18], [-10, 8], [-11, -19], [-24, -1], [-16, -18], [-54, 0], [-4, -21], [7, -12], [-18, 11], [9, -33], [-14, -6], [14, -17], [33, 5], [-17, 8], [32, -6], [5, -13], [-9, 23], [-33, 2], [39, 8], [-9, -4], [-25, -7], [-24, 14], [-24, 2], [-8, 6], [-14, -16], [-27, -7], [40, -9], [35, -16], [36, -1], [16, -19], [51, 13], [2, -22], [-27, 21], [30, -6], [27, 3], [-31, -17], [4, 8], [48, 8], [-7, 17], [-19, 5], [-10, 6], [11, 7], [7, 26], [41, 5], [20, -17], [-20, 26], [46, 6], [-27, 30], [-6, 18], [14, 0], [-17, 15], [-20, -18], [16, 28], [4, -24], [-12, -24], [24, 6], [15, 37], [-44, -20], [-28, -15], [-38, 6], [-16, 18], [-7, 33], [-9, 17], [36, 18], [9, 1], [34, 15], [-6, 4], [20, -14], [-39, 0], [11, 33], [-32, -26], [-62, -2], [-17, -7], [44, -3], [-14, -22], [2, 36], [34, -27], [10, -22], [-12, 16], [5, 22], [-24, -7], [66, -5], [-20, 18], [-32, -4], [25, 25], [-18, 28], [10, -5], [-59, -12], [-30, 31], [6, 2], [9, 14], [35, -3], [-11, -12], [24, -4], [26, -4], [42, -12], [-5, 33], [44, 2], [69, -7], [5, -29], [70, -3], [-51, 3], [54, -5], [18, 33], [23, -5], [-8, -28], [-7, 45], [-8, 30], [5, -15], [-58, -15], [13, -5], [11, -42], [-39, -24], [7, 45], [-12, -24], [70, -7], [68, 0], [-37, -14], [-21, -26], [33, 14], [-16, -14], [23, -19], [12, 33], [-6, 7], [5, -47], [-68, 12], [-10, -10], [-65, -3], [36, -28], [50, -16], [54, 13], [59, 9], [-24, 2], [-29, -29], [58, -18], [11, 31], [12, -23], [41, -10], [18, -11], [6, 24], [3, -35], [-48, -17], [-34, 32], [-6, 14], [21, 38], [-4, 28], [-42, 23], [12, 26], [-52, 26], [-33, 25], [-11, -5], [-38, 4], [62, -5], [-15, -8], [-14, 12], [25, 27], [7, -11], [39, 4], [-38, 4], [20, 33], [50, -24], [-44, -1], [-22, 23], [34, 12], [-32, -36], [-7, -32], [70, 2], [-23, -13], [-13, -15], [-5, 53], [5, 8], [-25, -12], [37, -9], [18, 40], [-24, -26], [58, -21], [-6, -32], [6, -18], [-21, -32], [-6, -19], [24, 32], [-78, 6], [16, 24], [-28, 18], [-30, -30], [-24, 16], [35, 18], [-31, 23], [36, 25], [-30, 13], [-28, -28], [44, -10], [-57, 17], [3, 17], [0, 16], [33, 40], [30, 11], [-44, -27], [-75, 3], [48, 4], [100, -2], [32, -10], [-4, 9], [12, 40], [35, -4], [14, -20], [-45, -33], [-78, -10], [56, -6], [59, -4], [-60, 3], [37, 34], [7, -9], [-65, -5], [69, 13], [-23, 3], [4, -34], [-65, -4], [-36, -13], [18, 30], [3, -28], [18, -6], [20, -34], [-32, 38], [4, 36], [-67, -11], [-23, 11], [-39, 29], [6, 10], [31, 28], [-65, 5], [4, -35], [50, -34], [-3, -35], [7, 9], [90, 2], [9, -20], [-50, -18], [45, 0], [74, -2], [9, 36], [-28, -32], [83, 7], [-53, -27]];
    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_bl();".
// 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_bl(: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_bl();".
// 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_bl( : 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![-3, 0, 1]>,<17,R![6, -6, 1]>,<19,R![-3, 0, 1]>],Snew);
    return Vf;
end function;