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