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