
// Make newform 21.18.g.a in Magma, downloaded from the LMFDB on 24 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_18_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_18_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 := 18;
    raw_aps := [[0, 0], [6561, 6561], [0, 0], [7390718, -17539617], [0, 0], [-2824265149, 5648530298], [0, 0], [-169289986506, 84644993253], [0, 0], [0, 0], [-2915506365985, -2915506365985], [0, -42101902631989], [0, 0], [-54897704698835, 0], [0, 0], [0, 0], [0, 0], [-3454795454866312, 1727397727433156], [269339188788571, -269339188788571], [0, 0], [4996068505776639, 4996068505776639], [0, 26467234205310223], [0, 0], [0, 0], [57267491945336968, -114534983890673936], [0, 0], [-283855719946615982, 141927859973307991], [0, 0], [-186046053315384403, 186046053315384403], [0, 0], [1484840837013219359, 0], [0, 0], [0, 0], [-758935103472115037, 1517870206944230074], [0, 0], [-2354586191814941764, 2354586191814941764], [4244323615632772788, 4244323615632772788], [0, 9690702541287797368], [0, 0], [0, 0], [0, 0], [-4224511906027950795, 8449023812055901590], [0, 0], [-9447127812687800425, 9447127812687800425], [0, 0], [34730214275626681858, 34730214275626681858], [-53770407809461996784, 0], [25218259147046582394, -50436518294093164788], [0, 0], [-179110173932250397834, 89555086966125198917], [0, 0], [0, 0], [-20030289547743233776, -20030289547743233776], [0, 0], [0, 0], [0, 0], [0, 0], [7941950570798078740, -3970975285399039370], [1152013674079170704059, -1152013674079170704059], [0, 0], [729300327454306471021, 729300327454306471021], [0, 0], [1189393716411240449201, -2378787432822480898402], [0, 0], [2235775243807246158426, -1117887621903623079213], [0, 0], [0, -2361606076121188402111], [-4462574801401518536285, 0], [0, 0], [-4604692903260708144340, 9209385806521416288680], [0, 0], [0, 0], [5420842837116468997869, 5420842837116468997869], [0, -6266409194029199843303], [10303586457161846444227, 0], [0, 0], [0, 0], [-23099767109996573119442, 11549883554998286559721], [0, 0], [-17975201629719240305423, -17975201629719240305423], [0, 0], [-38319435425665624584919, 0], [0, 0], [8594795402713365102517, -17189590805426730205034], [-66217181930351091076644, 33108590965175545538322], [0, 0], [0, 0], [0, 14201098738240209377249], [0, 0], [-14550210323582396279563, 0], [0, 0], [0, 0], [101340345198933077437325, -101340345198933077437325], [0, 0], [0, 6924854786628350054507], [0, 0], [0, 0], [0, 0], [-185064482093403937315246, 92532241046701968657623], [0, 307827330241409398151791], [370109122295989505170520, 0], [0, 0], [0, 0], [0, 0], [-290294862206183324928719, 290294862206183324928719], [-88298541436755090573989, -88298541436755090573989], [0, 0], [0, 0], [0, 0], [272331628972339542109799, -544663257944679084219598], [1022097781076144900784366, -511048890538072450392183], [-306159296026229419573430, 306159296026229419573430], [0, 0], [-103371684791661485324485, -103371684791661485324485], [-821877496151575921485076, 0], [0, 0], [-603639543187725527500331, 1207279086375451055000662], [0, 0], [0, 0], [0, 0], [-936867188483035654252509, -936867188483035654252509], [1751205325842696187218253, 0], [0, 0], [0, 0], [-66460129543024031096678, 33230064771512015548339], [0, 0], [0, -2778912746051794917723862], [0, 0], [-2094557850883508297347073, 4189115701767016594694146], [-3082609679535435533051558, 1541304839767717766525779], [3755758311002693407877317, -3755758311002693407877317], [0, 0], [0, 5399827229734327213812971], [-5172992680415009115451994, 0], [0, 0], [3552908604112720290834705, -7105817208225440581669410], [0, 0], [4135722510015745427685442, 4135722510015745427685442], [0, 0], [0, 0], [-137042886781838052230214, 274085773563676104460428], [0, 0], [5101506812680793894273908, -5101506812680793894273908], [0, 0], [3561043185668342849597133, 3561043185668342849597133], [0, 0], [692150061927085646214303, -1384300123854171292428606], [0, 0], [-18581436252305114239242860, 9290718126152557119621430], [0, 0], [0, -17094023741747077248929054], [0, 0], [-4383972555096387743981993, 0], [0, 0], [-26322112747561538665114099, 26322112747561538665114099], [0, 0], [0, -8536633440665620965022891], [0, 0], [-12706213749554455422753341, 25412427499108910845506682], [0, 0], [0, 0], [0, 0], [46659804538361180803210121, 0], [0, 0], [0, 0], [0, 0], [-58445606689740343613968093, 58445606689740343613968093], [-29195814877917541493063987, -29195814877917541493063987]];
    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_18_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_18_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, 18));
    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_18_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_18_g_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_21_g();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,18,sign)));
    Vf := Kernel([<2,R![0, 1]>],Snew);
    return Vf;
end function;