
// Make newform 21.23.h.a in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_21_h();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_21_h_Hecke();"
// To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
// To make the newform (type ModFrm), type "MakeNewformModFrm_21_23_h_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_21_23_h_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
        poly := [1, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_21_h();"
function MakeCharacter_21_h()
    N := 21;
    order := 6;
    char_gens := [8, 10];
    v := [3, 2];
    // 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;

// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_21_h_Hecke();"
function MakeCharacter_21_h_Hecke(Kf)
    N := 21;
    order := 6;
    char_gens := [8, 10];
    char_values := [[-1, 0], [-1, 1]];
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    values := ConvertToHeckeField(char_values : pass_field := true, Kf := Kf); // the value of chi on the gens as elements in the Hecke field
    F := Universe(values);// the Hecke field
    chi := DirichletCharacterFromValuesOnUnitGenerators(DirichletGroup(N,F),values);
    return chi;
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 := 23;
    raw_aps := [[0, 0], [0, 177147], [0, 0], [1984171195, -1970410467], [0, 0], [2094315786047, 0], [0, 0], [-20673935603651, 20673935603651], [0, 0], [0, 0], [0, -37081623937815587], [-84456886698164303, 84456886698164303], [0, 0], [1268503014678232811, 0], [0, 0], [0, 0], [0, 0], [67394602624928417446, -67394602624928417446], [0, -214129669585561202603], [0, 0], [0, -621369020009512258607], [1037582729766213568069, -1037582729766213568069], [0, 0], [0, 0], [-1618886660331792534142, 0], [0, 0], [2981019298647969692413, -2981019298647969692413], [0, 0], [0, -25083260444459930086511], [0, 0], [276555309459147790028603, 0], [0, 0], [0, 0], [-703680952564591156598749, 0], [0, 0], [0, -1671716651771127020236034], [0, 2541357050835456220574182], [3223266518878810335026038, -3223266518878810335026038], [0, 0], [0, 0], [0, 0], [-12033164120944811488043713, 0], [0, 0], [0, 24164953339008199870561489], [0, 0], [0, 35176217445621787621410334], [-71232934099287005789122006, 0], [4787842736316088763618642, 0], [0, 0], [74466198476943349941238849, -74466198476943349941238849], [0, 0], [0, 0], [0, -206285539140273775947061154], [0, 0], [0, 0], [0, 0], [0, 0], [-1075828211346369024981518642, 1075828211346369024981518642], [0, 502450401353166859062454897], [0, 0], [0, -189033517920073556562935267], [0, 0], [2120566463110036434050086139, 0], [0, 0], [5620879120915054364487023449, -5620879120915054364487023449], [0, 0], [8597412478154087470016570701, -8597412478154087470016570701], [12615877067694186449935418999, 0], [0, 0], [10498238154134642474038937498, 0], [0, 0], [0, 0], [0, 31843683775504542520412469541], [11577749817924780337449832873, -11577749817924780337449832873], [35944882260729673192595754731, 0], [0, 0], [0, 0], [2203025471658829839778463881, -2203025471658829839778463881], [0, 0], [0, -82972530219698143480886344751], [0, 0], [58062412667537046375392505239, 0], [0, 0], [200619473355512940823412073191, 0], [-215816844773662240916142176066, 215816844773662240916142176066], [0, 0], [0, 0], [-362843718257962874153345804303, 362843718257962874153345804303], [0, 0], [-413743943513766099205214742973, 0], [0, 0], [0, 0], [0, 567723255838545477244910970301], [0, 0], [-928589664510362871381228884651, 928589664510362871381228884651], [0, 0], [0, 0], [0, 0], [1519810199549418474091461393829, -1519810199549418474091461393829], [-1444463151232994553474747152759, 1444463151232994553474747152759], [2199046427134684714770117222794, 0], [0, 0], [0, 0], [0, 0], [0, -4139537000539752011802738313139], [0, 2957779676380995183948213040921], [0, 0], [0, 0], [0, 0], [5267557095627389450453356653599, 0], [5639988556335560177371334372077, -5639988556335560177371334372077], [0, -2172349042237292758263832878026], [0, 0], [0, 7773954966979991517765071699437], [1080522970077556083370201288514, 0], [0, 0], [15465936882223499310715276001027, 0], [0, 0], [0, 0], [0, 0], [0, -18543281451866156220422539930799], [-4794371895961536543415255852873, 0], [0, 0], [0, 0], [10282189099323571172227453082701, -10282189099323571172227453082701], [0, 0], [-45442926994333741138749942662666, 45442926994333741138749942662666], [0, 0], [-59886585827850803465370520897357, 0], [-62908297032249468573124693867967, 62908297032249468573124693867967], [0, -16733899118841508691379265253291], [0, 0], [82907578750286290946304461781181, -82907578750286290946304461781181], [15735602856058490280477162573098, 0], [0, 0], [-29659120451583875277676922408737, 0], [0, 0], [0, -30135950859528822282278544936938], [0, 0], [0, 0], [-124854539476120816589616379912486, 0], [0, 0], [0, 231079091778658325874357709784638], [0, 0], [0, -227680125859270679320034681449151], [0, 0], [-851655763699108009305658082569, 0], [0, 0], [-357193127029755577166549185586618, 357193127029755577166549185586618], [0, 0], [298836932452896163180239935111302, -298836932452896163180239935111302], [0, 0], [-175661731207138721633072049480973, 0], [0, 0], [0, 171416944334721344267969891474197], [0, 0], [738340859282947484079619386203437, -738340859282947484079619386203437], [0, 0], [-665292033322238136562697860263001, 0], [0, 0], [0, 0], [0, 0], [-533246510622667209929262648051397, 0], [0, 0], [0, 0], [0, 0], [0, 1745903614115766088484362342286893], [0, 1491940903956440857069051920643537]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_21_h_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_21_23_h_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_21_23_h_a(:prec:=2)
    chi := MakeCharacter_21_h();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 23));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(997) - 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_21_23_h_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_21_23_h_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_21_h();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,23,sign)));
    Vf := Kernel([<2,R![0, 1]>],Snew);
    return Vf;
end function;