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