
// Make newform 5070.2.a.bw in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_5070_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_5070_2_a_bw();".
// 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_5070_2_a_bw();".
// 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, -2, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 1, 0], [-2, 0, 1]];
    Rf_basisdens := [1, 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_5070_a();"
function MakeCharacter_5070_a()
    N := 5070;
    order := 1;
    char_gens := [1691, 4057, 1861];
    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_5070_a_Hecke(Kf)
    return MakeCharacter_5070_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, 0, 0], [-1, 0, 0], [2, -1, -1], [-1, 2, -1], [0, 0, 0], [-4, 3, -2], [1, -1, 3], [0, 1, -2], [4, -7, 5], [5, 2, 1], [4, 5, -3], [7, -3, 1], [4, -4, -1], [5, -6, 2], [-1, 3, -3], [4, -4, 1], [2, 0, 1], [4, 3, -2], [-2, 1, 2], [6, -7, 8], [9, -3, 7], [-3, 8, -7], [0, 3, 6], [6, 1, -5], [0, 4, -2], [-13, 6, -8], [-8, 2, 5], [15, -9, 14], [2, 2, 9], [-7, -1, 3], [12, -8, 5], [-5, 7, -13], [-1, 2, -4], [-7, 1, -3], [0, -4, -1], [-7, 1, 2], [2, -6, -11], [1, 0, 9], [2, 0, -11], [-13, 8, 6], [-12, 15, -11], [-7, 5, 3], [11, 5, -8], [-8, 13, 6], [0, 15, 0], [4, -2, 8], [-3, 15, -8], [5, -8, 6], [1, 1, -11], [-7, -4, -6], [-21, 0, -5], [15, -1, -4], [-2, 3, -8], [-3, 1, 12], [-4, -1, -3], [2, 6, -3], [8, -3, 12], [9, -5, 11], [-4, -3, -12], [6, -17, 6], [0, -12, -2], [-11, 9, -18], [-2, -14, 1], [-7, 2, 9], [18, -16, 9], [5, 7, 7], [19, -12, 14], [-3, 11, -14], [3, 10, -3], [-19, 13, -21], [-27, 3, -1], [-1, 8, -17], [-10, -10, -2], [-20, 11, -5], [10, -12, 7], [-12, 2, -7], [7, -6, -17], [13, -6, 23], [5, -20, 16], [-8, 19, -7], [25, -7, 3], [-24, -10, 7], [-24, 12, -7], [3, -5, 15], [6, -21, 20], [-17, 9, -2], [-4, 3, 0], [11, -11, 17], [-8, 20, 3], [1, -11, -14], [3, -16, 16], [28, -11, 18], [12, 0, 18], [8, -10, 0], [13, -13, 15], [-18, 4, -27], [-3, -12, -9], [-18, 4, 4], [-8, -5, -5], [-12, -3, 1], [-11, 21, -1], [-25, 14, -33], [11, -27, 14], [19, 3, 10], [16, -20, 12], [-1, -15, 14], [18, -8, 28], [-11, -1, 10], [-27, 9, 3], [-6, -4, -13], [13, 0, 17], [-22, 5, -17], [13, -17, 22], [-8, 25, -16], [0, -22, 20], [5, 23, -3], [19, 5, 11], [-36, 14, -14], [-37, 17, -26], [-6, 34, -19], [-35, 6, -16], [1, -16, 4], [-8, -8, 7], [-6, -19, 13], [36, -12, 29], [-14, 19, -26], [4, -20, 17], [4, 19, -4], [-23, -1, 13], [-10, -13, -4], [-8, 2, 21], [-9, 22, -24], [-19, 25, 3], [28, -6, 9], [28, -25, 27], [-1, -2, -5], [-32, 9, 9], [16, -13, -5], [-7, 20, 1], [12, -20, -2], [4, -34, 7], [-27, 8, -23], [8, -4, -19], [18, -5, 4], [13, -12, 8], [-2, -2, 19], [-10, -4, 7], [21, 7, -11], [-29, 21, -20], [7, 7, -10], [10, 25, -10], [-10, 5, -30], [50, -14, 26], [27, -22, 11], [5, -20, -16], [26, -35, 36], [-17, -9, 11], [-7, -25, 16], [-9, 36, -16], [-10, 15, 3], [-19, 13, -13], [-15, -24, 5], [10, -5, -15], [-14, 15, -17], [-3, 43, -14], [31, 9, -8], [14, -1, -17], [-18, 7, -28], [36, -18, 41], [-9, -13, -15], [-10, 18, -25], [22, -13, -11], [14, -19, -12], [38, -7, 11], [-20, 29, -24], [38, -15, 34], [6, 13, 6], [-12, 11, -8], [14, -4, -28], [4, 11, 10], [1, -8, 7], [-22, 16, 13], [-1, 32, 1], [-7, -15, -20], [-21, -8, 4], [6, 12, -8], [5, -13, -13], [-39, 10, -23], [1, 21, 12], [-22, 7, -16], [1, 1, -35], [43, -1, 7], [9, -6, 14], [-6, 34, -30], [-6, -23, -17], [-30, 0, 8], [-4, -33, 16], [-12, -1, -8], [1, 8, -7], [-2, -26, 11], [25, -6, 10], [5, -30, 20], [-14, 24, -18], [-37, -5, 8], [21, -30, 24], [-4, 8, 13], [23, -21, 43], [0, -20, -4], [35, -33, 31], [-26, 6, -4], [21, -10, 13], [-20, 28, -9], [19, -21, 9], [-32, 18, -46], [-28, -4, 9], [0, 18, -11], [-41, 24, -8], [-1, 10, -9], [-27, -3, 22], [9, 12, -20], [-9, 48, -20], [-16, 0, 21], [8, -11, 9], [-29, 40, -33], [42, -27, 42], [3, -28, -15], [11, -24, 3], [16, 23, -12], [-6, -23, 20], [13, -53, 32], [7, -10, 11], [-13, 5, -20], [12, -5, 39], [-5, 6, 30], [-6, 16, -17], [-3, 11, 29], [-5, -5, -26], [-35, -15, 8], [-9, 7, -30], [-30, 27, 20], [33, -1, 22], [-21, 20, 20], [11, 46, -16], [-44, -14, 4], [15, 1, 24], [-8, -6, 3], [19, 0, 2], [4, 0, -25], [39, -33, 21], [-45, 1, -5], [5, -37, -1], [2, 15, -33], [-7, 27, 6], [-48, 25, -23], [12, 6, 9], [0, -2, -16], [-13, -12, 5], [-31, 27, -30], [-45, -1, 4], [-41, 10, -35], [5, -4, 21], [-18, 21, -19], [-34, 20, -33], [30, 16, 8], [23, 34, -14], [3, -7, 18], [34, 13, -17], [12, -7, 10], [8, 21, -4], [-3, 26, 16], [-1, 1, 2], [-1, -3, 29], [14, 36, -27], [-8, -19, 10], [13, 40, -22], [38, -20, 5], [2, 24, -22], [-8, 29, -37], [45, -39, 53], [11, -12, -6], [0, -16, 20], [36, -32, -1], [41, -35, 23], [-1, 4, -21], [20, 13, -16], [9, 6, 2], [53, -42, 54], [-13, -8, 24], [-45, 21, -31], [-14, 20, -36], [-8, 13, 19], [17, -1, -18], [9, 37, -23], [-46, -9, -12], [-3, 17, -14], [54, -4, 0], [-13, -13, 4], [-9, -11, 5], [-53, 36, -32], [-21, -5, 23], [11, -6, -10], [56, -26, 52], [4, 21, -30], [-30, 4, 0], [-8, 2, 20], [-2, -36, -15], [-6, -2, -35], [7, -11, -11], [-39, -6, -6], [32, -9, 22], [-14, 19, 26], [-30, 36, -35], [10, 28, -1], [-30, 17, 4], [11, 36, 1], [-14, 53, 3], [-21, 16, 3], [-36, 33, 1], [-21, 8, -19], [12, 7, -21], [-16, 12, -6], [-15, 17, -5], [6, 16, 18], [-24, 45, -20], [-10, -27, -8], [-4, 30, -27], [44, -9, 8], [-21, -8, 34], [39, -17, 6], [57, -11, -11], [-8, -15, 9], [21, 15, -23], [-33, 8, 14], [15, 23, -8], [23, -20, 31], [2, -9, -28], [-77, 18, -14], [19, 21, 3], [30, -35, -22], [3, -29, -18], [-53, 22, -28], [-7, 11, -16], [8, -19, 29], [-20, 10, 16], [-12, 10, -17], [49, -33, 6], [8, 12, 0], [27, -14, 17], [-3, 48, -13], [33, -26, -12], [7, 39, -27], [30, 9, 11], [10, 8, 3], [30, 4, 31], [30, -36, -20], [40, -11, 8], [44, -29, 48], [-9, 39, -42], [-10, -15, 10], [-21, -1, 1], [37, 9, -19], [-10, -21, 5], [-2, 6, 29], [-25, 6, 3], [-74, 15, -7], [-61, 28, -58], [13, -29, -41], [-40, 6, -30], [-35, 23, -25], [47, 4, -17], [21, -33, -25], [23, -62, 52], [-64, 10, -6], [-64, 12, -14], [39, 3, -5], [21, 57, -20], [45, -5, 15], [-28, -12, 1], [-42, 13, -22], [41, 22, -14], [54, -23, 47], [-12, -42, -4], [69, -13, 30], [44, -41, -1], [-9, 4, -44], [31, -27, 20], [-29, 53, -4], [65, 2, 6], [-40, 16, 12], [-22, -2, -1], [-60, -17, 19], [49, -4, 20], [59, -12, 42], [-12, 63, -31], [-1, -5, -23], [38, -19, -11], [-59, 15, -26], [-37, 4, 12], [14, -15, -5], [17, -30, 32], [30, -38, -15], [5, -17, -7], [-32, 11, -67], [49, -20, -3], [11, 23, 19], [26, 26, 12], [-12, 31, -8], [-9, -23, 33], [50, -54, 36], [37, -33, -9], [15, 5, -44], [9, 3, 45], [-22, -3, 2], [-56, 36, 5], [12, 28, 23], [-20, 39, -51], [43, -15, -29], [8, -53, 5], [42, -17, 28], [-63, 8, -3], [27, -35, 41], [33, 32, -5], [21, -3, 24], [-32, 35, -7], [42, -11, 24], [63, -19, -9], [47, 6, -4], [53, 4, -14], [-44, 3, 3]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_5070_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_5070_2_a_bw();".
// 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_5070_2_a_bw(:prec:=3)
    chi := MakeCharacter_5070_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(2999) - 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_5070_2_a_bw();".
// 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_5070_2_a_bw( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_5070_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![13, 5, -6, 1]>,<11,R![7, -7, 0, 1]>,<17,R![-7, 0, 7, 1]>,<31,R![-83, 69, -16, 1]>],Snew);
    return Vf;
end function;