
// Make newform 4022.2.a.a in Magma, downloaded from the LMFDB on 20 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4022_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_4022_2_a_a();".
// 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_4022_2_a_a();".
// 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_4022_a();"
function MakeCharacter_4022_a()
    N := 4022;
    order := 1;
    char_gens := [3];
    v := [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_4022_a_Hecke(Kf)
    return MakeCharacter_4022_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], [2], [-2], [1], [-6], [0], [-1], [5], [6], [-5], [-7], [-1], [4], [8], [13], [-3], [5], [5], [5], [2], [2], [0], [18], [12], [14], [-5], [5], [-4], [-6], [17], [15], [19], [-20], [-22], [-17], [-2], [11], [-2], [-11], [21], [6], [-8], [-2], [12], [-24], [0], [0], [16], [22], [12], [8], [25], [-19], [-6], [-11], [1], [-2], [-7], [-22], [0], [-8], [-11], [33], [-16], [33], [-16], [26], [-18], [14], [-6], [-36], [17], [7], [-6], [36], [-6], [14], [30], [23], [-6], [20], [-21], [18], [14], [36], [12], [10], [40], [-16], [8], [36], [-24], [-38], [-34], [15], [20], [-10], [-14], [7], [-28], [11], [18], [-28], [-2], [-11], [-7], [39], [-16], [-3], [1], [-12], [32], [28], [-48], [3], [-1], [45], [38], [-18], [-15], [34], [-45], [-51], [42], [-5], [32], [-30], [22], [0], [-44], [-40], [24], [6], [39], [54], [33], [-13], [-12], [25], [28], [-14], [42], [28], [-49], [-28], [-54], [-17], [0], [-32], [-25], [12], [-22], [-45], [-30], [24], [2], [42], [50], [-27], [58], [1], [31], [-36], [-31], [43], [-2], [-26], [-3], [-6], [-34], [35], [40], [-30], [-40], [-24], [48], [15], [4], [7], [-16], [-54], [-32], [-50], [27], [26], [2], [-10], [7], [6], [29], [34], [44], [8], [-8], [-30], [17], [-55], [-22], [0], [-9], [24], [16], [-8], [4], [8], [-48], [33], [63], [12], [22], [28], [-62], [12], [45], [-18], [32], [-21], [4], [-10], [-34], [0], [-42], [-71], [-43], [-2], [-27], [39], [2], [-50], [62], [-65], [13], [-12], [-9], [24], [-50], [44], [-54], [30], [-11], [-7], [64], [-54], [-36], [-21], [42], [13], [54], [-16], [36], [9], [32], [7], [-24], [-18], [14], [-35], [48], [23], [14], [-2], [-39], [70], [-22], [47], [72], [46], [-4], [27], [33], [20], [82], [-37], [77], [-62], [52], [-46], [-22], [34], [16], [16], [-6], [-26], [28], [48], [-42], [45], [-70], [16], [50], [6], [-86], [-71], [-73], [-30], [83], [-47], [-48], [-67], [-26], [11], [16], [22], [1], [-79], [86], [-2], [-20], [57], [3], [44], [90], [0], [-40], [-49], [60], [57], [58], [73], [-30], [17], [42], [32], [-56], [-43], [-65], [55], [-23], [75], [25], [26], [-92], [-48], [-23], [18], [-26], [-69], [22], [58], [89], [-47], [-24], [28], [-12], [-77], [-39], [52], [54], [22], [-15], [40], [40], [-66], [1], [66], [54], [74], [-72], [51], [18], [-60], [-1], [-71], [-73], [90], [30], [-66], [-20], [11], [-98], [-16], [-6], [20], [62], [79], [60], [-40], [-56], [-29], [46], [96], [-91], [38], [20], [64], [-7], [-12], [59], [-36], [94], [-6], [-100], [49], [-82], [-87], [4], [45], [48], [68], [-10], [-94], [45], [-6], [57], [-92], [26], [-3], [-62], [-60], [-18], [42], [-44], [1], [-1], [-66], [-8], [95], [78], [-6], [80], [8], [-12], [-34], [29], [102], [36], [-10], [23], [-72]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4022_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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