
// Make newform 6021.2.a.b in Magma, downloaded from the LMFDB on 01 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_6021_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_6021_2_a_b();".
// 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_6021_2_a_b();".
// 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_6021_a();"
function MakeCharacter_6021_a()
    N := 6021;
    order := 1;
    char_gens := [893, 4240];
    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_6021_a_Hecke(Kf)
    return MakeCharacter_6021_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], [0], [3], [1], [0], [0], [0], [0], [-9], [0], [-3], [-2], [6], [11], [-8], [-12], [-4], [-2], [-14], [-1], [13], [-4], [-18], [-16], [12], [-16], [-8], [13], [15], [-1], [16], [-16], [18], [-4], [-10], [6], [-10], [-16], [-24], [-17], [4], [-1], [-5], [20], [12], [9], [7], [-1], [-8], [18], [11], [16], [-21], [28], [-12], [16], [5], [-16], [20], [30], [-16], [3], [-4], [19], [26], [-2], [16], [26], [-14], [-5], [-28], [30], [-16], [4], [12], [-3], [18], [26], [16], [10], [0], [-4], [0], [2], [-30], [-34], [-27], [-22], [22], [15], [-39], [6], [19], [-3], [-17], [11], [14], [-35], [10], [-30], [-20], [7], [9], [-39], [0], [-7], [-45], [14], [2], [-36], [-16], [14], [-48], [-6], [-38], [15], [28], [-28], [41], [-32], [-6], [-33], [32], [30], [18], [38], [28], [24], [37], [23], [14], [0], [-15], [16], [-37], [30], [45], [-6], [-54], [15], [-52], [-42], [-42], [41], [-20], [-7], [8], [-48], [-38], [-17], [38], [18], [-2], [22], [44], [-50], [24], [14], [2], [-14], [-8], [-3], [-4], [49], [9], [17], [4], [10], [-32], [-18], [8], [-10], [42], [52], [-50], [-50], [-12], [-18], [11], [-39], [30], [-30], [26], [-11], [32], [-8], [61], [41], [5], [-30], [-37], [-33], [-41], [22], [-42], [30], [-21], [53], [30], [41], [31], [55], [-2], [0], [-26], [48], [1], [-60], [2], [-16], [40], [-26], [8], [31], [-5], [32], [5], [-55], [42], [-45], [10], [-58], [15], [-18], [-42], [-34], [49], [-52], [17], [67], [30], [-25], [-75], [-14], [-4], [47], [74], [-29], [-9], [-25], [-9], [58], [-46], [-53], [18], [-43], [40], [-68], [37], [33], [1], [15], [27], [-50], [14], [-40], [43], [53], [20], [-24], [-64], [18], [0], [51], [18], [28], [42], [-69], [-48], [-68], [22], [-68], [46], [50], [4], [42], [-12], [-32], [21], [-49], [12], [11], [-72], [12], [45], [-57], [2], [49], [-16], [26], [-19], [12], [21], [28], [-10], [35], [-68], [-21], [25], [-17], [-88], [-66], [-18], [-39], [-13], [50], [-37], [-74], [-12], [-46], [74], [54], [54], [-36], [40], [34], [75], [-88], [30], [79], [-13], [17], [-38], [19], [54], [-56], [28], [-5], [48], [34], [38], [-28], [20], [-79], [-68], [-54], [23], [64], [-68], [26], [-14], [22], [45], [-65], [-78], [-50], [-26], [8], [-90], [0], [-38], [88], [-23], [-17], [-38], [-14], [4], [-6], [57], [-45], [6], [70], [34], [9], [25], [-26], [-2], [-79], [53], [-24], [71], [29], [57], [-28], [-80], [-58], [48], [-38], [-18], [-14], [-14], [39], [-6], [50], [60], [-82], [-5], [-33], [44], [12], [74], [-4], [84], [-49], [95], [-27], [101], [-12], [44], [24], [-31], [-66], [38], [6], [9], [7], [-29], [-79], [-52], [12], [2], [-33], [39], [44], [-53], [-1], [-42], [59], [-75], [44], [93], [-77], [58], [-45], [-16], [84], [-82], [42], [-25], [66]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_6021_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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