
// Make newform 6422.2.a.d in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_6422_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_6422_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_6422_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_6422_a();"
function MakeCharacter_6422_a()
    N := 6422;
    order := 1;
    char_gens := [4903, 4733];
    v := [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_6422_a_Hecke(Kf)
    return MakeCharacter_6422_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], [2], [-3], [0], [-6], [0], [7], [1], [-4], [-9], [0], [3], [5], [0], [-2], [11], [-10], [13], [10], [6], [-1], [-10], [-6], [2], [18], [1], [-12], [-4], [14], [-11], [2], [12], [-5], [-10], [21], [10], [13], [-16], [-8], [18], [6], [19], [8], [9], [6], [-2], [-12], [10], [-12], [10], [-18], [-14], [-7], [14], [17], [0], [26], [28], [-15], [-15], [26], [-9], [20], [24], [30], [31], [2], [1], [-24], [2], [-9], [-10], [-28], [-1], [-10], [-8], [9], [14], [-19], [-7], [30], [19], [-12], [-31], [-10], [-18], [-6], [3], [-27], [-20], [20], [16], [2], [20], [-22], [6], [-25], [17], [18], [13], [4], [-3], [36], [26], [-12], [23], [-14], [-33], [-24], [-47], [-22], [21], [19], [34], [22], [-15], [-20], [-6], [-26], [40], [-29], [5], [-6], [-22], [50], [6], [9], [2], [50], [1], [28], [22], [-48], [-6], [-6], [14], [18], [34], [26], [-33], [-32], [30], [-46], [-16], [-1], [-14], [17], [-51], [44], [24], [-45], [-45], [34], [10], [-8], [6], [38], [35], [15], [-30], [-2], [-30], [-20], [-20], [-31], [46], [-48], [-3], [-2], [-27], [26], [19], [32], [-13], [-8], [15], [-12], [21], [-26], [-10], [26], [-2], [15], [18], [-22], [13], [-14], [-60], [13], [64], [-11], [38], [64], [42], [28], [42], [-29], [-39], [-1], [4], [-1], [-26], [55], [-35], [-42], [-11], [54], [-22], [23], [36], [-13], [46], [-26], [-46], [-72], [-35], [52], [-7], [12], [-62], [27], [0], [25], [26], [-24], [5], [13], [-56], [32], [10], [-11], [0], [-18], [33], [-26], [42], [30], [-3], [-66], [38], [-24], [22], [12], [51], [41], [48], [-50], [-10], [-16], [46], [-66], [-2], [-52], [-3], [22], [-60], [22], [-46], [38], [7], [-38], [66], [-55], [63], [67], [48], [-70], [55], [-22], [37], [67], [24], [-9], [-58], [14], [-26], [24], [23], [-39], [54], [58], [54], [6], [27], [-38], [-52], [-33], [-21], [-46], [-3], [-42], [70], [-23], [-16], [74], [-3], [8], [-71], [60], [-68], [35], [-17], [-58], [-12], [-58], [41], [-64], [39], [-10], [-55], [34], [-10], [9], [22], [-78], [-43], [12], [32], [-2], [71], [32], [-66], [-29], [12], [30], [-83], [-56], [44], [64], [17], [-35], [-53], [20], [82], [-14], [88], [-5], [-62], [-85], [34], [74], [-77], [59], [66], [-6], [-50], [-43], [10], [58], [42], [-34], [38], [33], [76], [-25], [77], [36], [-88], [-25], [-10], [1], [7], [-10], [-90], [-74], [38], [-10], [-16], [21], [-48], [44], [60], [-73], [18], [-3], [80], [-12], [6], [-21], [9], [14], [99], [98], [-66], [-24], [-48], [-10], [-58], [20], [48], [25], [39], [-4], [102], [-78], [66], [78], [58], [58], [73], [42], [-62], [40], [41], [-97], [50], [22], [-87], [56], [66], [-59], [33], [-84], [-36], [-33], [-35], [62], [-34], [22], [-24], [51], [47], [-4], [70], [6], [91], [-64], [59], [-66], [48]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_6422_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_6422_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_6422_2_a_d(:prec:=1)
    chi := MakeCharacter_6422_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_6422_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_6422_2_a_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_6422_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![-2, 1]>,<5,R![3, 1]>,<7,R![0, 1]>],Snew);
    return Vf;
end function;