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