
// Make newform 22.8.a.c in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_22_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_22_8_a_c();".
// 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_22_8_a_c();".
// 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_22_a();"
function MakeCharacter_22_a()
    N := 22;
    order := 1;
    char_gens := [13];
    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_22_a_Hecke(Kf)
    return MakeCharacter_22_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 := 8;
    raw_aps := [[8], [-21], [-551], [62], [-1331], [1500], [-29930], [29512], [31499], [-75168], [-235845], [75507], [-270288], [-1028030], [-771840], [765778], [-392007], [1248460], [3498133], [1101753], [-1122996], [-4362946], [-4437790], [-521233], [-2129831], [12426586], [-9367176], [11021090], [19707210], [-12398783], [32323488], [-37228114], [7607041], [15113978], [-39629954], [74057914], [47360873], [-84072428], [-60536292], [-6543782], [116986573], [-82277945], [51477525], [-107592812], [-112129714], [-23505256], [-186757924], [-138809361], [-65133486], [290274263], [-65356024], [-70301414], [163086760], [-85370891], [-306744818], [-365645010], [374619970], [275299292], [-263367298], [-177369410], [221037268], [157114256], [-335074112], [558671828], [-369423817], [527905269], [-601784053], [814101138], [-193524044], [735638934], [246985323], [1081860084], [-1004747581], [-1064414834], [957022343], [-1521585029], [1698550689], [161745238], [518083346], [-2319459566], [-163260060], [-170926594], [-25343298], [-1557974707], [-1512041960], [-1907095087], [-1751046173], [2917237924], [3093615964], [-280698279], [575352081], [2310687324], [-1345770277], [880452424], [-3602402860], [972775822], [1016442919], [1801284477], [-4608021592], [3013706944], [-6067592392], [4177245174], [2326162196], [-1781344480], [-1527131132], [5188471409], [3520062780], [3151650012], [-4068144864], [9120766714], [-9669530222], [-5809600992], [-5673491774], [-10206289165], [917510419], [7274876103], [3772626833], [-4772795195], [-11269220625], [1302598962], [-4902562131], [6929989598], [7874834798], [-2481706320], [-6431859963], [2489556330], [-263056449], [3669226371], [8533268055], [15203078540], [1003206986], [1636311572], [11628854765], [-4815065374], [-9547543788], [7360013212], [3646782402], [4896692224], [-618870915], [-1908761512], [-14226210094], [10347323366], [2725677875], [27674949796], [-22497006479], [-3243443553], [7373615310], [-9670489256], [16480425829], [-15074118992], [3629520396], [2978805653], [-29678744588], [25885215282], [7124093812], [-27588690828], [17715866738], [23699915058], [6843037072], [-27750392694], [-42271509255], [-38703815382], [36388434352], [-2837702541], [22017085981], [-12925233573], [-53774585488], [11818768078]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_22_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_22_8_a_c();".
// 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_22_8_a_c(:prec:=1)
    chi := MakeCharacter_22_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 8));
    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_22_8_a_c();".
// 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_22_8_a_c( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_22_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,8,sign)));
    Vf := Kernel([<3,R![21, 1]>],Snew);
    return Vf;
end function;