
// Make newform 21.20.g.a in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_21_g();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_21_g_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_20_g_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_20_g_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_g();"
function MakeCharacter_21_g()
    N := 21;
    order := 6;
    char_gens := [8, 10];
    v := [3, 5];
    // 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_g_Hecke();"
function MakeCharacter_21_g_Hecke(Kf)
    N := 21;
    order := 6;
    char_gens := [8, 10];
    char_values := [[-1, 0], [0, 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 := 20;
    raw_aps := [[0, 0], [19683, 19683], [0, 0], [103363346, -109870239], [0, 0], [43912988783, -87825977566], [0, 0], [1365151658466, -682575829233], [0, 0], [0, 0], [-159714263521255, -159714263521255], [0, 1581011344413647], [0, 0], [6012529514786335, 0], [0, 0], [0, 0], [0, 0], [134845233947509688, -67422616973754844], [441174519368848177, -441174519368848177], [0, 0], [-95532016723177113, -95532016723177113], [0, -262389124703851223], [0, 0], [0, 0], [-6681938461866185624, 13363876923732371248], [0, 0], [-28087551260395354706, 14043775630197677353], [0, 0], [31747562688102374753, -31747562688102374753], [0, 0], [39231633130515917513, 0], [0, 0], [0, 0], [-223922312400419814623, 447844624800839629246], [0, 0], [35477738026300062356, -35477738026300062356], [-221284846756712616684, -221284846756712616684], [0, -469693890495101967416], [0, 0], [0, 0], [0, 0], [920132281409159735265, -1840264562818319470530], [0, 0], [6850175515881270829175, -6850175515881270829175], [0, 0], [-5734576667772526056338, -5734576667772526056338], [23993633518540125844336, 0], [10635602415627922409382, -21271204831255844818764], [0, 0], [-16598013738435285240706, 8299006869217642620353], [0, 0], [0, 0], [-42245544516257646335536, -42245544516257646335536], [0, 0], [0, 0], [0, 0], [0, 0], [267212487701352875754220, -133606243850676437877110], [222577479218738457554863, -222577479218738457554863], [0, 0], [150875935069223378961343, 150875935069223378961343], [0, 0], [-395635976283240710065693, 791271952566481420131386], [0, 0], [-394290155083885392283062, 197145077541942696141531], [0, 0], [0, 865639244803816466052299], [1068898478829993747661315, 0], [0, 0], [-875320851539995264643380, 1750641703079990529286760], [0, 0], [0, 0], [-2656422928925977253164677, -2656422928925977253164677], [0, -1276525759638668539887419], [6060019493412995682094273, 0], [0, 0], [0, 0], [-526867197610765904511674, 263433598805382952255837], [0, 0], [-7379502364814386890719687, -7379502364814386890719687], [0, 0], [4688466430515900439470821, 0], [0, 0], [-12741113002129631930216939, 25482226004259263860433878], [-6784215864168666199584636, 3392107932084333099792318], [0, 0], [0, 0], [0, 25353254113621366040941433], [0, 0], [40412070562325128849493411, 0], [0, 0], [0, 0], [-61366911361088250764970925, 61366911361088250764970925], [0, 0], [0, -41786517836733187068383407], [0, 0], [0, 0], [0, 0], [-102236892460963125835375258, 51118446230481562917687629], [0, 40716826719859551134520331], [122629445986436967864882920, 0], [0, 0], [0, 0], [0, 0], [-230473453716405141209542229, 230473453716405141209542229], [192684847088556779447663947, 192684847088556779447663947], [0, 0], [0, 0], [0, 0], [-180130742218423563706440001, 360261484436847127412880002], [-576801402948732562987676478, 288400701474366281493838239], [399970594391527533204258130, -399970594391527533204258130], [0, 0], [-69427896403552993330825495, -69427896403552993330825495], [-788320581073647308319595996, 0], [0, 0], [-479946823541287462629195473, 959893647082574925258390946], [0, 0], [0, 0], [0, 0], [298795231284922915574159511, 298795231284922915574159511], [-857490775360610500970464931, 0], [0, 0], [0, 0], [1506464888938962604726064782, -753232444469481302363032391], [0, 0], [0, 2343137651405417828603875442], [0, 0], [1670819457135461550941273809, -3341638914270923101882547618], [-2973487732456481605176531614, 1486743866228240802588265807], [1520508241214667953235021583, -1520508241214667953235021583], [0, 0], [0, 1426022947599470143674316061], [334562026271243523570738622, 0], [0, 0], [-484847214306056779426452015, 969694428612113558852904030], [0, 0], [-3459962509081250704369115546, -3459962509081250704369115546], [0, 0], [0, 0], [-1686488498447418293042930754, 3372976996894836586085861508], [0, 0], [-4202192601349453037437234436, 4202192601349453037437234436], [0, 0], [4166263299162336462450947697, 4166263299162336462450947697], [0, 0], [-7548451980703116362404845741, 15096903961406232724809691482], [0, 0], [9501912671955701915818177180, -4750956335977850957909088590], [0, 0], [0, 14774614621844625461708110042], [0, 0], [-19366696596764639388492702299, 0], [0, 0], [-18986928025362925801034867233, 18986928025362925801034867233], [0, 0], [0, 8739378484556187236090757371], [0, 0], [10575766520767392004005045883, -21151533041534784008010091766], [0, 0], [0, 0], [0, 0], [38303102904031909296977480327, 0], [0, 0], [0, 0], [0, 0], [51419583363705970040861016437, -51419583363705970040861016437], [-13930609248972454955576770679, -13930609248972454955576770679]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_21_g_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_21_20_g_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_20_g_a(:prec:=2)
    chi := MakeCharacter_21_g();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 20));
    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_20_g_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_20_g_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_21_g();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,20,sign)));
    Vf := Kernel([<2,R![0, 1]>],Snew);
    return Vf;
end function;