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


// To make the newform (type ModFrm), type "MakeNewformModFrm_4021_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_4021_2_a_a(:prec:=1)
    chi := MakeCharacter_4021_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_4021_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_4021_2_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4021_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![2, 1]>],Snew);
    return Vf;
end function;