
// Make newform 5070.2.a.d 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_d();".
// 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_d();".
// 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], [-5], [0], [-2], [-2], [-1], [5], [-11], [3], [-2], [-11], [9], [6], [-15], [10], [16], [0], [-6], [-11], [6], [2], [-2], [2], [10], [10], [-2], [11], [2], [-1], [-11], [2], [17], [8], [-7], [-15], [3], [20], [-13], [-16], [4], [-24], [-8], [24], [-4], [26], [0], [10], [1], [-20], [-7], [25], [-17], [21], [14], [-13], [-11], [-10], [19], [6], [0], [20], [20], [16], [-28], [-2], [34], [20], [-18], [12], [-16], [-19], [-2], [31], [5], [3], [36], [30], [-4], [-16], [40], [28], [-4], [20], [12], [38], [-39], [14], [6], [-24], [2], [-16], [28], [0], [29], [18], [-31], [0], [-12], [26], [12], [-22], [-16], [-22], [-18], [31], [42], [-3], [18], [-29], [41], [-10], [40], [-30], [8], [0], [34], [-39], [32], [8], [-4], [-12], [-30], [13], [-8], [24], [-40], [6], [44], [51], [-23], [10], [20], [43], [32], [-31], [12], [-30], [20], [-51], [-2], [-50], [14], [-54], [-49], [-25], [-50], [39], [-19], [-18], [-1], [3], [19], [-18], [-16], [-16], [-50], [-50], [8], [-51], [20], [48], [-21], [-3], [-17], [-2], [33], [-38], [4], [32], [12], [42], [-19], [52], [34], [38], [52], [-26], [-32], [-3], [-15], [19], [3], [-17], [19], [25], [-37], [0], [-50], [26], [4], [-21], [-16], [-18], [-39], [-53], [-45], [3], [-30], [63], [-30], [-26], [4], [56], [40], [-50], [-26], [-38], [16], [-41], [-22], [-60], [-66], [55], [10], [-18], [-68], [54], [56], [40], [-18], [20], [-54], [-54], [3], [-16], [-18], [-23], [23], [22], [48], [0], [61], [-24], [27], [-50], [20], [0], [32], [54], [44], [14], [10], [6], [-4], [-12], [12], [21], [-43], [48], [3], [-55], [44], [-24], [-28], [32], [-6], [-22], [-6], [-38], [72], [-22], [69], [34], [18], [30], [-13], [28], [68], [-29], [8], [31], [64], [-32], [-30], [-34], [33], [-63], [-24], [-37], [5], [-44], [33], [64], [16], [32], [51], [24], [-57], [-62], [41], [63], [42], [-63], [3], [74], [-27], [-17], [-42], [72], [-51], [-20], [40], [-80], [-6], [68], [16], [22], [-25], [-27], [30], [-67], [-72], [-34], [-45], [-42], [48], [6], [-16], [-28], [9], [32], [46], [-55], [-34], [-59], [5], [84], [-24], [52], [9], [-20], [76], [-10], [-86], [33], [-5], [-44], [37], [62], [55], [-9], [24], [-15], [58], [51], [-30], [-62], [-90], [64], [38], [-54], [-42], [-23], [52], [-45], [-27], [-75], [6], [-30], [1], [16], [68], [-46], [4], [-38], [-13], [68], [56], [72], [-42], [-49], [-66], [12], [90], [56], [68], [0], [61], [-70], [84], [13], [-66], [16], [101], [29], [-61], [-8], [-22], [30], [-21], [17], [-48], [14], [19], [24], [86], [-17], [4], [-39], [42], [26], [34], [-43], [-10], [62], [71], [-52], [78], [-88], [78], [30], [72], [-21], [12], [58], [97], [96], [-2], [-43], [-8], [67], [64], [18], [48], [-84], [102], [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_d();".
// 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_d(: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_d();".
// 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_d( : 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![5, 1]>,<17,R![2, 1]>,<31,R![11, 1]>],Snew);
    return Vf;
end function;