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